blob: 9e09367c973922c11ffea6312c8877b63312bbc0 [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>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010041
42#include "rt2x00.h"
43#include "rt2800lib.h"
44#include "rt2800.h"
45
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010046/*
47 * Register access.
48 * All access to the CSR registers will go through the methods
49 * rt2800_register_read and rt2800_register_write.
50 * BBP and RF register require indirect register access,
51 * and use the CSR registers BBPCSR and RFCSR to achieve this.
52 * These indirect registers work with busy bits,
53 * and we will try maximal REGISTER_BUSY_COUNT times to access
54 * the register while taking a REGISTER_BUSY_DELAY us delay
55 * between each attampt. When the busy bit is still set at that time,
56 * the access attempt is considered to have failed,
57 * and we will print an error.
58 * The _lock versions must be used if you already hold the csr_mutex
59 */
60#define WAIT_FOR_BBP(__dev, __reg) \
61 rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
62#define WAIT_FOR_RFCSR(__dev, __reg) \
63 rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
64#define WAIT_FOR_RF(__dev, __reg) \
65 rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
66#define WAIT_FOR_MCU(__dev, __reg) \
67 rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
68 H2M_MAILBOX_CSR_OWNER, (__reg))
69
Helmut Schaabaff8002010-04-28 09:58:59 +020070static inline bool rt2800_is_305x_soc(struct rt2x00_dev *rt2x00dev)
71{
72 /* check for rt2872 on SoC */
73 if (!rt2x00_is_soc(rt2x00dev) ||
74 !rt2x00_rt(rt2x00dev, RT2872))
75 return false;
76
77 /* we know for sure that these rf chipsets are used on rt305x boards */
78 if (rt2x00_rf(rt2x00dev, RF3020) ||
79 rt2x00_rf(rt2x00dev, RF3021) ||
80 rt2x00_rf(rt2x00dev, RF3022))
81 return true;
82
83 NOTICE(rt2x00dev, "Unknown RF chipset on rt305x\n");
84 return false;
85}
86
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010087static void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
88 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010089{
90 u32 reg;
91
92 mutex_lock(&rt2x00dev->csr_mutex);
93
94 /*
95 * Wait until the BBP becomes available, afterwards we
96 * can safely write the new data into the register.
97 */
98 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
99 reg = 0;
100 rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
101 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
102 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
103 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
Ivo van Doornefc7d362010-06-29 21:49:26 +0200104 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100105
106 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
107 }
108
109 mutex_unlock(&rt2x00dev->csr_mutex);
110}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100111
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100112static void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
113 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100114{
115 u32 reg;
116
117 mutex_lock(&rt2x00dev->csr_mutex);
118
119 /*
120 * Wait until the BBP becomes available, afterwards we
121 * can safely write the read request into the register.
122 * After the data has been written, we wait until hardware
123 * returns the correct value, if at any time the register
124 * doesn't become available in time, reg will be 0xffffffff
125 * which means we return 0xff to the caller.
126 */
127 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
128 reg = 0;
129 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
130 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
131 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
Ivo van Doornefc7d362010-06-29 21:49:26 +0200132 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100133
134 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
135
136 WAIT_FOR_BBP(rt2x00dev, &reg);
137 }
138
139 *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
140
141 mutex_unlock(&rt2x00dev->csr_mutex);
142}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100143
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100144static void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
145 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100146{
147 u32 reg;
148
149 mutex_lock(&rt2x00dev->csr_mutex);
150
151 /*
152 * Wait until the RFCSR becomes available, afterwards we
153 * can safely write the new data into the register.
154 */
155 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
156 reg = 0;
157 rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
158 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
159 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
160 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
161
162 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
163 }
164
165 mutex_unlock(&rt2x00dev->csr_mutex);
166}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100167
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100168static void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
169 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100170{
171 u32 reg;
172
173 mutex_lock(&rt2x00dev->csr_mutex);
174
175 /*
176 * Wait until the RFCSR becomes available, afterwards we
177 * can safely write the read request into the register.
178 * After the data has been written, we wait until hardware
179 * returns the correct value, if at any time the register
180 * doesn't become available in time, reg will be 0xffffffff
181 * which means we return 0xff to the caller.
182 */
183 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
184 reg = 0;
185 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
186 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
187 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
188
189 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
190
191 WAIT_FOR_RFCSR(rt2x00dev, &reg);
192 }
193
194 *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
195
196 mutex_unlock(&rt2x00dev->csr_mutex);
197}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100198
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100199static void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
200 const unsigned int word, const u32 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100201{
202 u32 reg;
203
204 mutex_lock(&rt2x00dev->csr_mutex);
205
206 /*
207 * Wait until the RF becomes available, afterwards we
208 * can safely write the new data into the register.
209 */
210 if (WAIT_FOR_RF(rt2x00dev, &reg)) {
211 reg = 0;
212 rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
213 rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
214 rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
215 rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
216
217 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
218 rt2x00_rf_write(rt2x00dev, word, value);
219 }
220
221 mutex_unlock(&rt2x00dev->csr_mutex);
222}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100223
Woody Hung16ebd602012-07-31 21:53:33 +0800224static int rt2800_enable_wlan_rt3290(struct rt2x00_dev *rt2x00dev)
225{
226 u32 reg;
227 int i, count;
228
229 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
230 if (rt2x00_get_field32(reg, WLAN_EN))
231 return 0;
232
233 rt2x00_set_field32(&reg, WLAN_GPIO_OUT_OE_BIT_ALL, 0xff);
234 rt2x00_set_field32(&reg, FRC_WL_ANT_SET, 1);
235 rt2x00_set_field32(&reg, WLAN_CLK_EN, 0);
236 rt2x00_set_field32(&reg, WLAN_EN, 1);
237 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
238
239 udelay(REGISTER_BUSY_DELAY);
240
241 count = 0;
242 do {
243 /*
244 * Check PLL_LD & XTAL_RDY.
245 */
246 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
247 rt2800_register_read(rt2x00dev, CMB_CTRL, &reg);
248 if (rt2x00_get_field32(reg, PLL_LD) &&
249 rt2x00_get_field32(reg, XTAL_RDY))
250 break;
251 udelay(REGISTER_BUSY_DELAY);
252 }
253
254 if (i >= REGISTER_BUSY_COUNT) {
255
256 if (count >= 10)
257 return -EIO;
258
259 rt2800_register_write(rt2x00dev, 0x58, 0x018);
260 udelay(REGISTER_BUSY_DELAY);
261 rt2800_register_write(rt2x00dev, 0x58, 0x418);
262 udelay(REGISTER_BUSY_DELAY);
263 rt2800_register_write(rt2x00dev, 0x58, 0x618);
264 udelay(REGISTER_BUSY_DELAY);
265 count++;
266 } else {
267 count = 0;
268 }
269
270 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
271 rt2x00_set_field32(&reg, PCIE_APP0_CLK_REQ, 0);
272 rt2x00_set_field32(&reg, WLAN_CLK_EN, 1);
273 rt2x00_set_field32(&reg, WLAN_RESET, 1);
274 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
275 udelay(10);
276 rt2x00_set_field32(&reg, WLAN_RESET, 0);
277 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
278 udelay(10);
279 rt2800_register_write(rt2x00dev, INT_SOURCE_CSR, 0x7fffffff);
280 } while (count != 0);
281
282 return 0;
283}
284
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100285void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
286 const u8 command, const u8 token,
287 const u8 arg0, const u8 arg1)
288{
289 u32 reg;
290
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100291 /*
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100292 * SOC devices don't support MCU requests.
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100293 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100294 if (rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100295 return;
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100296
297 mutex_lock(&rt2x00dev->csr_mutex);
298
299 /*
300 * Wait until the MCU becomes available, afterwards we
301 * can safely write the new data into the register.
302 */
303 if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
304 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
305 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
306 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
307 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
308 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
309
310 reg = 0;
311 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
312 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
313 }
314
315 mutex_unlock(&rt2x00dev->csr_mutex);
316}
317EXPORT_SYMBOL_GPL(rt2800_mcu_request);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100318
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200319int rt2800_wait_csr_ready(struct rt2x00_dev *rt2x00dev)
320{
321 unsigned int i = 0;
322 u32 reg;
323
324 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
325 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
326 if (reg && reg != ~0)
327 return 0;
328 msleep(1);
329 }
330
331 ERROR(rt2x00dev, "Unstable hardware.\n");
332 return -EBUSY;
333}
334EXPORT_SYMBOL_GPL(rt2800_wait_csr_ready);
335
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100336int rt2800_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
337{
338 unsigned int i;
339 u32 reg;
340
Helmut Schaa08e53102010-11-04 20:37:47 +0100341 /*
342 * Some devices are really slow to respond here. Wait a whole second
343 * before timing out.
344 */
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100345 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
346 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
347 if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
348 !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
349 return 0;
350
Helmut Schaa08e53102010-11-04 20:37:47 +0100351 msleep(10);
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100352 }
353
Jakub Kicinski52b82432012-04-03 03:40:49 +0200354 ERROR(rt2x00dev, "WPDMA TX/RX busy [0x%08x].\n", reg);
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100355 return -EACCES;
356}
357EXPORT_SYMBOL_GPL(rt2800_wait_wpdma_ready);
358
Jakub Kicinskif7b395e2012-04-03 03:40:47 +0200359void rt2800_disable_wpdma(struct rt2x00_dev *rt2x00dev)
360{
361 u32 reg;
362
363 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
364 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
365 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
366 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
367 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
368 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
369 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
370}
371EXPORT_SYMBOL_GPL(rt2800_disable_wpdma);
372
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200373static bool rt2800_check_firmware_crc(const u8 *data, const size_t len)
374{
375 u16 fw_crc;
376 u16 crc;
377
378 /*
379 * The last 2 bytes in the firmware array are the crc checksum itself,
380 * this means that we should never pass those 2 bytes to the crc
381 * algorithm.
382 */
383 fw_crc = (data[len - 2] << 8 | data[len - 1]);
384
385 /*
386 * Use the crc ccitt algorithm.
387 * This will return the same value as the legacy driver which
388 * used bit ordering reversion on the both the firmware bytes
389 * before input input as well as on the final output.
390 * Obviously using crc ccitt directly is much more efficient.
391 */
392 crc = crc_ccitt(~0, data, len - 2);
393
394 /*
395 * There is a small difference between the crc-itu-t + bitrev and
396 * the crc-ccitt crc calculation. In the latter method the 2 bytes
397 * will be swapped, use swab16 to convert the crc to the correct
398 * value.
399 */
400 crc = swab16(crc);
401
402 return fw_crc == crc;
403}
404
405int rt2800_check_firmware(struct rt2x00_dev *rt2x00dev,
406 const u8 *data, const size_t len)
407{
408 size_t offset = 0;
409 size_t fw_len;
410 bool multiple;
411
412 /*
413 * PCI(e) & SOC devices require firmware with a length
414 * of 8kb. USB devices require firmware files with a length
415 * of 4kb. Certain USB chipsets however require different firmware,
416 * which Ralink only provides attached to the original firmware
417 * file. Thus for USB devices, firmware files have a length
Woody Hunga89534e2012-06-13 15:01:16 +0800418 * which is a multiple of 4kb. The firmware for rt3290 chip also
419 * have a length which is a multiple of 4kb.
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200420 */
Woody Hunga89534e2012-06-13 15:01:16 +0800421 if (rt2x00_is_usb(rt2x00dev) || rt2x00_rt(rt2x00dev, RT3290))
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200422 fw_len = 4096;
Woody Hunga89534e2012-06-13 15:01:16 +0800423 else
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200424 fw_len = 8192;
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200425
Woody Hunga89534e2012-06-13 15:01:16 +0800426 multiple = true;
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200427 /*
428 * Validate the firmware length
429 */
430 if (len != fw_len && (!multiple || (len % fw_len) != 0))
431 return FW_BAD_LENGTH;
432
433 /*
434 * Check if the chipset requires one of the upper parts
435 * of the firmware.
436 */
437 if (rt2x00_is_usb(rt2x00dev) &&
438 !rt2x00_rt(rt2x00dev, RT2860) &&
439 !rt2x00_rt(rt2x00dev, RT2872) &&
440 !rt2x00_rt(rt2x00dev, RT3070) &&
441 ((len / fw_len) == 1))
442 return FW_BAD_VERSION;
443
444 /*
445 * 8kb firmware files must be checked as if it were
446 * 2 separate firmware files.
447 */
448 while (offset < len) {
449 if (!rt2800_check_firmware_crc(data + offset, fw_len))
450 return FW_BAD_CRC;
451
452 offset += fw_len;
453 }
454
455 return FW_OK;
456}
457EXPORT_SYMBOL_GPL(rt2800_check_firmware);
458
459int rt2800_load_firmware(struct rt2x00_dev *rt2x00dev,
460 const u8 *data, const size_t len)
461{
462 unsigned int i;
463 u32 reg;
Woody Hung16ebd602012-07-31 21:53:33 +0800464 int retval;
465
466 if (rt2x00_rt(rt2x00dev, RT3290)) {
467 retval = rt2800_enable_wlan_rt3290(rt2x00dev);
468 if (retval)
469 return -EBUSY;
470 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200471
472 /*
Ivo van Doornb9eca242010-08-30 21:13:54 +0200473 * If driver doesn't wake up firmware here,
474 * rt2800_load_firmware will hang forever when interface is up again.
475 */
476 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0x00000000);
477
478 /*
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200479 * Wait for stable hardware.
480 */
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200481 if (rt2800_wait_csr_ready(rt2x00dev))
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200482 return -EBUSY;
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200483
Gabor Juhosadde5882011-03-03 11:46:45 +0100484 if (rt2x00_is_pci(rt2x00dev)) {
Woody Hunga89534e2012-06-13 15:01:16 +0800485 if (rt2x00_rt(rt2x00dev, RT3290) ||
486 rt2x00_rt(rt2x00dev, RT3572) ||
John Li2ed71882012-02-17 17:33:06 +0800487 rt2x00_rt(rt2x00dev, RT5390) ||
488 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +0100489 rt2800_register_read(rt2x00dev, AUX_CTRL, &reg);
490 rt2x00_set_field32(&reg, AUX_CTRL_FORCE_PCIE_CLK, 1);
491 rt2x00_set_field32(&reg, AUX_CTRL_WAKE_PCIE_EN, 1);
492 rt2800_register_write(rt2x00dev, AUX_CTRL, reg);
493 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200494 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
Gabor Juhosadde5882011-03-03 11:46:45 +0100495 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200496
Jakub Kicinskib7e1d222012-04-03 03:40:48 +0200497 rt2800_disable_wpdma(rt2x00dev);
498
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200499 /*
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200500 * Write firmware to the device.
501 */
502 rt2800_drv_write_firmware(rt2x00dev, data, len);
503
504 /*
505 * Wait for device to stabilize.
506 */
507 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
508 rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
509 if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
510 break;
511 msleep(1);
512 }
513
514 if (i == REGISTER_BUSY_COUNT) {
515 ERROR(rt2x00dev, "PBF system register not ready.\n");
516 return -EBUSY;
517 }
518
519 /*
Stanislaw Gruszka4ed1dd22012-01-24 14:09:07 +0100520 * Disable DMA, will be reenabled later when enabling
521 * the radio.
522 */
Jakub Kicinskif7b395e2012-04-03 03:40:47 +0200523 rt2800_disable_wpdma(rt2x00dev);
Stanislaw Gruszka4ed1dd22012-01-24 14:09:07 +0100524
525 /*
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200526 * Initialize firmware.
527 */
528 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
529 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
Stanislaw Gruszka0c17cf92012-01-24 14:09:06 +0100530 if (rt2x00_is_usb(rt2x00dev))
531 rt2800_register_write(rt2x00dev, H2M_INT_SRC, 0);
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200532 msleep(1);
533
534 return 0;
535}
536EXPORT_SYMBOL_GPL(rt2800_load_firmware);
537
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200538void rt2800_write_tx_data(struct queue_entry *entry,
539 struct txentry_desc *txdesc)
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200540{
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200541 __le32 *txwi = rt2800_drv_get_txwi(entry);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200542 u32 word;
543
544 /*
545 * Initialize TX Info descriptor
546 */
547 rt2x00_desc_read(txwi, 0, &word);
548 rt2x00_set_field32(&word, TXWI_W0_FRAG,
549 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
Ivo van Doorn84804cd2010-08-06 20:46:19 +0200550 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS,
551 test_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags));
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200552 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
553 rt2x00_set_field32(&word, TXWI_W0_TS,
554 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
555 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
556 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100557 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY,
558 txdesc->u.ht.mpdu_density);
559 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->u.ht.txop);
560 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->u.ht.mcs);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200561 rt2x00_set_field32(&word, TXWI_W0_BW,
562 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
563 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
564 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100565 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->u.ht.stbc);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200566 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
567 rt2x00_desc_write(txwi, 0, word);
568
569 rt2x00_desc_read(txwi, 1, &word);
570 rt2x00_set_field32(&word, TXWI_W1_ACK,
571 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
572 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
573 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100574 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->u.ht.ba_size);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200575 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
576 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
Helmut Schaaa2b13282011-09-08 14:38:01 +0200577 txdesc->key_idx : txdesc->u.ht.wcid);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200578 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
579 txdesc->length);
Helmut Schaa2b23cda2010-11-04 20:38:15 +0100580 rt2x00_set_field32(&word, TXWI_W1_PACKETID_QUEUE, entry->queue->qid);
Ivo van Doornbc8a9792010-10-02 11:32:43 +0200581 rt2x00_set_field32(&word, TXWI_W1_PACKETID_ENTRY, (entry->entry_idx % 3) + 1);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200582 rt2x00_desc_write(txwi, 1, word);
583
584 /*
585 * Always write 0 to IV/EIV fields, hardware will insert the IV
586 * from the IVEIV register when TXD_W3_WIV is set to 0.
587 * When TXD_W3_WIV is set to 1 it will use the IV data
588 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
589 * crypto entry in the registers should be used to encrypt the frame.
590 */
591 _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
592 _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
593}
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200594EXPORT_SYMBOL_GPL(rt2800_write_tx_data);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200595
Helmut Schaaff6133b2010-10-09 13:34:11 +0200596static int rt2800_agc_to_rssi(struct rt2x00_dev *rt2x00dev, u32 rxwi_w2)
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200597{
Luigi Tarenga7fc41752012-01-31 18:51:23 +0100598 s8 rssi0 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI0);
599 s8 rssi1 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI1);
600 s8 rssi2 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI2);
Ivo van Doorn74861922010-07-11 12:23:50 +0200601 u16 eeprom;
602 u8 offset0;
603 u8 offset1;
604 u8 offset2;
605
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200606 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Ivo van Doorn74861922010-07-11 12:23:50 +0200607 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &eeprom);
608 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET0);
609 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET1);
610 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
611 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_OFFSET2);
612 } else {
613 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &eeprom);
614 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET0);
615 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET1);
616 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
617 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_OFFSET2);
618 }
619
620 /*
621 * Convert the value from the descriptor into the RSSI value
622 * If the value in the descriptor is 0, it is considered invalid
623 * and the default (extremely low) rssi value is assumed
624 */
625 rssi0 = (rssi0) ? (-12 - offset0 - rt2x00dev->lna_gain - rssi0) : -128;
626 rssi1 = (rssi1) ? (-12 - offset1 - rt2x00dev->lna_gain - rssi1) : -128;
627 rssi2 = (rssi2) ? (-12 - offset2 - rt2x00dev->lna_gain - rssi2) : -128;
628
629 /*
630 * mac80211 only accepts a single RSSI value. Calculating the
631 * average doesn't deliver a fair answer either since -60:-60 would
632 * be considered equally good as -50:-70 while the second is the one
633 * which gives less energy...
634 */
635 rssi0 = max(rssi0, rssi1);
Luigi Tarenga7fc41752012-01-31 18:51:23 +0100636 return (int)max(rssi0, rssi2);
Ivo van Doorn74861922010-07-11 12:23:50 +0200637}
638
639void rt2800_process_rxwi(struct queue_entry *entry,
640 struct rxdone_entry_desc *rxdesc)
641{
642 __le32 *rxwi = (__le32 *) entry->skb->data;
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200643 u32 word;
644
645 rt2x00_desc_read(rxwi, 0, &word);
646
647 rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
648 rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
649
650 rt2x00_desc_read(rxwi, 1, &word);
651
652 if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
653 rxdesc->flags |= RX_FLAG_SHORT_GI;
654
655 if (rt2x00_get_field32(word, RXWI_W1_BW))
656 rxdesc->flags |= RX_FLAG_40MHZ;
657
658 /*
659 * Detect RX rate, always use MCS as signal type.
660 */
661 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
662 rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
663 rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
664
665 /*
666 * Mask of 0x8 bit to remove the short preamble flag.
667 */
668 if (rxdesc->rate_mode == RATE_MODE_CCK)
669 rxdesc->signal &= ~0x8;
670
671 rt2x00_desc_read(rxwi, 2, &word);
672
Ivo van Doorn74861922010-07-11 12:23:50 +0200673 /*
674 * Convert descriptor AGC value to RSSI value.
675 */
676 rxdesc->rssi = rt2800_agc_to_rssi(entry->queue->rt2x00dev, word);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200677
678 /*
679 * Remove RXWI descriptor from start of buffer.
680 */
Ivo van Doorn74861922010-07-11 12:23:50 +0200681 skb_pull(entry->skb, RXWI_DESC_SIZE);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200682}
683EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
684
Helmut Schaa31937c42011-09-07 20:10:02 +0200685void rt2800_txdone_entry(struct queue_entry *entry, u32 status, __le32 *txwi)
Helmut Schaa14433332010-10-02 11:27:03 +0200686{
687 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Helmut Schaab34793e2010-10-02 11:34:56 +0200688 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Helmut Schaa14433332010-10-02 11:27:03 +0200689 struct txdone_entry_desc txdesc;
690 u32 word;
691 u16 mcs, real_mcs;
Helmut Schaab34793e2010-10-02 11:34:56 +0200692 int aggr, ampdu;
Helmut Schaa14433332010-10-02 11:27:03 +0200693
694 /*
695 * Obtain the status about this packet.
696 */
697 txdesc.flags = 0;
Helmut Schaa14433332010-10-02 11:27:03 +0200698 rt2x00_desc_read(txwi, 0, &word);
Helmut Schaab34793e2010-10-02 11:34:56 +0200699
Helmut Schaa14433332010-10-02 11:27:03 +0200700 mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200701 ampdu = rt2x00_get_field32(word, TXWI_W0_AMPDU);
702
Helmut Schaa14433332010-10-02 11:27:03 +0200703 real_mcs = rt2x00_get_field32(status, TX_STA_FIFO_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200704 aggr = rt2x00_get_field32(status, TX_STA_FIFO_TX_AGGRE);
705
706 /*
707 * If a frame was meant to be sent as a single non-aggregated MPDU
708 * but ended up in an aggregate the used tx rate doesn't correlate
709 * with the one specified in the TXWI as the whole aggregate is sent
710 * with the same rate.
711 *
712 * For example: two frames are sent to rt2x00, the first one sets
713 * AMPDU=1 and requests MCS7 whereas the second frame sets AMDPU=0
714 * and requests MCS15. If the hw aggregates both frames into one
715 * AMDPU the tx status for both frames will contain MCS7 although
716 * the frame was sent successfully.
717 *
718 * Hence, replace the requested rate with the real tx rate to not
719 * confuse the rate control algortihm by providing clearly wrong
720 * data.
721 */
Helmut Schaa5356d962011-03-03 19:40:33 +0100722 if (unlikely(aggr == 1 && ampdu == 0 && real_mcs != mcs)) {
Helmut Schaab34793e2010-10-02 11:34:56 +0200723 skbdesc->tx_rate_idx = real_mcs;
724 mcs = real_mcs;
725 }
Helmut Schaa14433332010-10-02 11:27:03 +0200726
Helmut Schaaf16d2db2011-03-28 13:35:21 +0200727 if (aggr == 1 || ampdu == 1)
728 __set_bit(TXDONE_AMPDU, &txdesc.flags);
729
Helmut Schaa14433332010-10-02 11:27:03 +0200730 /*
731 * Ralink has a retry mechanism using a global fallback
732 * table. We setup this fallback table to try the immediate
733 * lower rate for all rates. In the TX_STA_FIFO, the MCS field
734 * always contains the MCS used for the last transmission, be
735 * it successful or not.
736 */
737 if (rt2x00_get_field32(status, TX_STA_FIFO_TX_SUCCESS)) {
738 /*
739 * Transmission succeeded. The number of retries is
740 * mcs - real_mcs
741 */
742 __set_bit(TXDONE_SUCCESS, &txdesc.flags);
743 txdesc.retry = ((mcs > real_mcs) ? mcs - real_mcs : 0);
744 } else {
745 /*
746 * Transmission failed. The number of retries is
747 * always 7 in this case (for a total number of 8
748 * frames sent).
749 */
750 __set_bit(TXDONE_FAILURE, &txdesc.flags);
751 txdesc.retry = rt2x00dev->long_retry;
752 }
753
754 /*
755 * the frame was retried at least once
756 * -> hw used fallback rates
757 */
758 if (txdesc.retry)
759 __set_bit(TXDONE_FALLBACK, &txdesc.flags);
760
761 rt2x00lib_txdone(entry, &txdesc);
762}
763EXPORT_SYMBOL_GPL(rt2800_txdone_entry);
764
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200765void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
766{
767 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
768 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
769 unsigned int beacon_base;
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100770 unsigned int padding_len;
Seth Forsheed76dfc62011-02-14 08:52:25 -0600771 u32 orig_reg, reg;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200772
773 /*
774 * Disable beaconing while we are reloading the beacon data,
775 * otherwise we might be sending out invalid data.
776 */
777 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Seth Forsheed76dfc62011-02-14 08:52:25 -0600778 orig_reg = reg;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200779 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
780 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
781
782 /*
783 * Add space for the TXWI in front of the skb.
784 */
Stanislaw Gruszkab52398b2011-07-30 13:32:56 +0200785 memset(skb_push(entry->skb, TXWI_DESC_SIZE), 0, TXWI_DESC_SIZE);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200786
787 /*
788 * Register descriptor details in skb frame descriptor.
789 */
790 skbdesc->flags |= SKBDESC_DESC_IN_SKB;
791 skbdesc->desc = entry->skb->data;
792 skbdesc->desc_len = TXWI_DESC_SIZE;
793
794 /*
795 * Add the TXWI for the beacon to the skb.
796 */
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200797 rt2800_write_tx_data(entry, txdesc);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200798
799 /*
800 * Dump beacon to userspace through debugfs.
801 */
802 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
803
804 /*
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100805 * Write entire beacon with TXWI and padding to register.
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200806 */
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100807 padding_len = roundup(entry->skb->len, 4) - entry->skb->len;
Seth Forsheed76dfc62011-02-14 08:52:25 -0600808 if (padding_len && skb_pad(entry->skb, padding_len)) {
809 ERROR(rt2x00dev, "Failure padding beacon, aborting\n");
810 /* skb freed by skb_pad() on failure */
811 entry->skb = NULL;
812 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, orig_reg);
813 return;
814 }
815
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200816 beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100817 rt2800_register_multiwrite(rt2x00dev, beacon_base, entry->skb->data,
818 entry->skb->len + padding_len);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200819
820 /*
821 * Enable beaconing again.
822 */
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200823 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
824 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
825
826 /*
827 * Clean up beacon skb.
828 */
829 dev_kfree_skb_any(entry->skb);
830 entry->skb = NULL;
831}
Ivo van Doorn50e888e2010-07-11 12:26:12 +0200832EXPORT_SYMBOL_GPL(rt2800_write_beacon);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200833
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100834static inline void rt2800_clear_beacon_register(struct rt2x00_dev *rt2x00dev,
835 unsigned int beacon_base)
Helmut Schaafdb87252010-06-29 21:48:06 +0200836{
837 int i;
838
839 /*
840 * For the Beacon base registers we only need to clear
841 * the whole TXWI which (when set to 0) will invalidate
842 * the entire beacon.
843 */
844 for (i = 0; i < TXWI_DESC_SIZE; i += sizeof(__le32))
845 rt2800_register_write(rt2x00dev, beacon_base + i, 0);
846}
847
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100848void rt2800_clear_beacon(struct queue_entry *entry)
849{
850 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
851 u32 reg;
852
853 /*
854 * Disable beaconing while we are reloading the beacon data,
855 * otherwise we might be sending out invalid data.
856 */
857 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
858 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
859 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
860
861 /*
862 * Clear beacon.
863 */
864 rt2800_clear_beacon_register(rt2x00dev,
865 HW_BEACON_OFFSET(entry->entry_idx));
866
867 /*
868 * Enabled beaconing again.
869 */
870 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
871 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
872}
873EXPORT_SYMBOL_GPL(rt2800_clear_beacon);
874
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100875#ifdef CONFIG_RT2X00_LIB_DEBUGFS
876const struct rt2x00debug rt2800_rt2x00debug = {
877 .owner = THIS_MODULE,
878 .csr = {
879 .read = rt2800_register_read,
880 .write = rt2800_register_write,
881 .flags = RT2X00DEBUGFS_OFFSET,
882 .word_base = CSR_REG_BASE,
883 .word_size = sizeof(u32),
884 .word_count = CSR_REG_SIZE / sizeof(u32),
885 },
886 .eeprom = {
887 .read = rt2x00_eeprom_read,
888 .write = rt2x00_eeprom_write,
889 .word_base = EEPROM_BASE,
890 .word_size = sizeof(u16),
891 .word_count = EEPROM_SIZE / sizeof(u16),
892 },
893 .bbp = {
894 .read = rt2800_bbp_read,
895 .write = rt2800_bbp_write,
896 .word_base = BBP_BASE,
897 .word_size = sizeof(u8),
898 .word_count = BBP_SIZE / sizeof(u8),
899 },
900 .rf = {
901 .read = rt2x00_rf_read,
902 .write = rt2800_rf_write,
903 .word_base = RF_BASE,
904 .word_size = sizeof(u32),
905 .word_count = RF_SIZE / sizeof(u32),
906 },
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200907 .rfcsr = {
908 .read = rt2800_rfcsr_read,
909 .write = rt2800_rfcsr_write,
910 .word_base = RFCSR_BASE,
911 .word_size = sizeof(u8),
912 .word_count = RFCSR_SIZE / sizeof(u8),
913 },
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100914};
915EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
916#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
917
918int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
919{
920 u32 reg;
921
Woody Hunga89534e2012-06-13 15:01:16 +0800922 if (rt2x00_rt(rt2x00dev, RT3290)) {
923 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
924 return rt2x00_get_field32(reg, WLAN_GPIO_IN_BIT0);
925 } else {
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +0200926 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
927 return rt2x00_get_field32(reg, GPIO_CTRL_VAL2);
Woody Hunga89534e2012-06-13 15:01:16 +0800928 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100929}
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
Gertjan van Wingerdeb3579d62009-12-30 11:36:34 +0100993static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100994 struct rt2x00_led *led, enum led_type type)
995{
996 led->rt2x00dev = rt2x00dev;
997 led->type = type;
998 led->led_dev.brightness_set = rt2800_brightness_set;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100999 led->flags = LED_INITIALIZED;
1000}
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001001#endif /* CONFIG_RT2X00_LIB_LEDS */
1002
1003/*
1004 * Configuration handlers.
1005 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001006static void rt2800_config_wcid(struct rt2x00_dev *rt2x00dev,
1007 const u8 *address,
1008 int wcid)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001009{
1010 struct mac_wcid_entry wcid_entry;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001011 u32 offset;
1012
1013 offset = MAC_WCID_ENTRY(wcid);
1014
1015 memset(&wcid_entry, 0xff, sizeof(wcid_entry));
1016 if (address)
1017 memcpy(wcid_entry.mac, address, ETH_ALEN);
1018
1019 rt2800_register_multiwrite(rt2x00dev, offset,
1020 &wcid_entry, sizeof(wcid_entry));
1021}
1022
1023static void rt2800_delete_wcid_attr(struct rt2x00_dev *rt2x00dev, int wcid)
1024{
1025 u32 offset;
1026 offset = MAC_WCID_ATTR_ENTRY(wcid);
1027 rt2800_register_write(rt2x00dev, offset, 0);
1028}
1029
1030static void rt2800_config_wcid_attr_bssidx(struct rt2x00_dev *rt2x00dev,
1031 int wcid, u32 bssidx)
1032{
1033 u32 offset = MAC_WCID_ATTR_ENTRY(wcid);
1034 u32 reg;
1035
1036 /*
1037 * The BSS Idx numbers is split in a main value of 3 bits,
1038 * and a extended field for adding one additional bit to the value.
1039 */
1040 rt2800_register_read(rt2x00dev, offset, &reg);
1041 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX, (bssidx & 0x7));
1042 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
1043 (bssidx & 0x8) >> 3);
1044 rt2800_register_write(rt2x00dev, offset, reg);
1045}
1046
1047static void rt2800_config_wcid_attr_cipher(struct rt2x00_dev *rt2x00dev,
1048 struct rt2x00lib_crypto *crypto,
1049 struct ieee80211_key_conf *key)
1050{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001051 struct mac_iveiv_entry iveiv_entry;
1052 u32 offset;
1053 u32 reg;
1054
1055 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
1056
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001057 if (crypto->cmd == SET_KEY) {
1058 rt2800_register_read(rt2x00dev, offset, &reg);
1059 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
1060 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
1061 /*
1062 * Both the cipher as the BSS Idx numbers are split in a main
1063 * value of 3 bits, and a extended field for adding one additional
1064 * bit to the value.
1065 */
1066 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
1067 (crypto->cipher & 0x7));
1068 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
1069 (crypto->cipher & 0x8) >> 3);
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001070 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
1071 rt2800_register_write(rt2x00dev, offset, reg);
1072 } else {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001073 /* Delete the cipher without touching the bssidx */
1074 rt2800_register_read(rt2x00dev, offset, &reg);
1075 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB, 0);
1076 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER, 0);
1077 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT, 0);
1078 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, 0);
1079 rt2800_register_write(rt2x00dev, offset, reg);
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001080 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001081
1082 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
1083
1084 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
1085 if ((crypto->cipher == CIPHER_TKIP) ||
1086 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
1087 (crypto->cipher == CIPHER_AES))
1088 iveiv_entry.iv[3] |= 0x20;
1089 iveiv_entry.iv[3] |= key->keyidx << 6;
1090 rt2800_register_multiwrite(rt2x00dev, offset,
1091 &iveiv_entry, sizeof(iveiv_entry));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001092}
1093
1094int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
1095 struct rt2x00lib_crypto *crypto,
1096 struct ieee80211_key_conf *key)
1097{
1098 struct hw_key_entry key_entry;
1099 struct rt2x00_field32 field;
1100 u32 offset;
1101 u32 reg;
1102
1103 if (crypto->cmd == SET_KEY) {
1104 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
1105
1106 memcpy(key_entry.key, crypto->key,
1107 sizeof(key_entry.key));
1108 memcpy(key_entry.tx_mic, crypto->tx_mic,
1109 sizeof(key_entry.tx_mic));
1110 memcpy(key_entry.rx_mic, crypto->rx_mic,
1111 sizeof(key_entry.rx_mic));
1112
1113 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
1114 rt2800_register_multiwrite(rt2x00dev, offset,
1115 &key_entry, sizeof(key_entry));
1116 }
1117
1118 /*
1119 * The cipher types are stored over multiple registers
1120 * starting with SHARED_KEY_MODE_BASE each word will have
1121 * 32 bits and contains the cipher types for 2 bssidx each.
1122 * Using the correct defines correctly will cause overhead,
1123 * so just calculate the correct offset.
1124 */
1125 field.bit_offset = 4 * (key->hw_key_idx % 8);
1126 field.bit_mask = 0x7 << field.bit_offset;
1127
1128 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
1129
1130 rt2800_register_read(rt2x00dev, offset, &reg);
1131 rt2x00_set_field32(&reg, field,
1132 (crypto->cmd == SET_KEY) * crypto->cipher);
1133 rt2800_register_write(rt2x00dev, offset, reg);
1134
1135 /*
1136 * Update WCID information
1137 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001138 rt2800_config_wcid(rt2x00dev, crypto->address, key->hw_key_idx);
1139 rt2800_config_wcid_attr_bssidx(rt2x00dev, key->hw_key_idx,
1140 crypto->bssidx);
1141 rt2800_config_wcid_attr_cipher(rt2x00dev, crypto, key);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001142
1143 return 0;
1144}
1145EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
1146
Helmut Schaaa2b13282011-09-08 14:38:01 +02001147static inline int rt2800_find_wcid(struct rt2x00_dev *rt2x00dev)
Helmut Schaa1ed38112011-03-03 19:44:33 +01001148{
Helmut Schaaa2b13282011-09-08 14:38:01 +02001149 struct mac_wcid_entry wcid_entry;
Helmut Schaa1ed38112011-03-03 19:44:33 +01001150 int idx;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001151 u32 offset;
Helmut Schaa1ed38112011-03-03 19:44:33 +01001152
1153 /*
Helmut Schaaa2b13282011-09-08 14:38:01 +02001154 * Search for the first free WCID entry and return the corresponding
1155 * index.
Helmut Schaa1ed38112011-03-03 19:44:33 +01001156 *
1157 * Make sure the WCID starts _after_ the last possible shared key
1158 * entry (>32).
1159 *
1160 * Since parts of the pairwise key table might be shared with
1161 * the beacon frame buffers 6 & 7 we should only write into the
1162 * first 222 entries.
1163 */
1164 for (idx = 33; idx <= 222; idx++) {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001165 offset = MAC_WCID_ENTRY(idx);
1166 rt2800_register_multiread(rt2x00dev, offset, &wcid_entry,
1167 sizeof(wcid_entry));
1168 if (is_broadcast_ether_addr(wcid_entry.mac))
Helmut Schaa1ed38112011-03-03 19:44:33 +01001169 return idx;
1170 }
Helmut Schaaa2b13282011-09-08 14:38:01 +02001171
1172 /*
1173 * Use -1 to indicate that we don't have any more space in the WCID
1174 * table.
1175 */
Helmut Schaa1ed38112011-03-03 19:44:33 +01001176 return -1;
1177}
1178
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001179int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
1180 struct rt2x00lib_crypto *crypto,
1181 struct ieee80211_key_conf *key)
1182{
1183 struct hw_key_entry key_entry;
1184 u32 offset;
1185
1186 if (crypto->cmd == SET_KEY) {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001187 /*
1188 * Allow key configuration only for STAs that are
1189 * known by the hw.
1190 */
1191 if (crypto->wcid < 0)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001192 return -ENOSPC;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001193 key->hw_key_idx = crypto->wcid;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001194
1195 memcpy(key_entry.key, crypto->key,
1196 sizeof(key_entry.key));
1197 memcpy(key_entry.tx_mic, crypto->tx_mic,
1198 sizeof(key_entry.tx_mic));
1199 memcpy(key_entry.rx_mic, crypto->rx_mic,
1200 sizeof(key_entry.rx_mic));
1201
1202 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
1203 rt2800_register_multiwrite(rt2x00dev, offset,
1204 &key_entry, sizeof(key_entry));
1205 }
1206
1207 /*
1208 * Update WCID information
1209 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001210 rt2800_config_wcid_attr_cipher(rt2x00dev, crypto, key);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001211
1212 return 0;
1213}
1214EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
1215
Helmut Schaaa2b13282011-09-08 14:38:01 +02001216int rt2800_sta_add(struct rt2x00_dev *rt2x00dev, struct ieee80211_vif *vif,
1217 struct ieee80211_sta *sta)
1218{
1219 int wcid;
1220 struct rt2x00_sta *sta_priv = sta_to_rt2x00_sta(sta);
1221
1222 /*
1223 * Find next free WCID.
1224 */
1225 wcid = rt2800_find_wcid(rt2x00dev);
1226
1227 /*
1228 * Store selected wcid even if it is invalid so that we can
1229 * later decide if the STA is uploaded into the hw.
1230 */
1231 sta_priv->wcid = wcid;
1232
1233 /*
1234 * No space left in the device, however, we can still communicate
1235 * with the STA -> No error.
1236 */
1237 if (wcid < 0)
1238 return 0;
1239
1240 /*
1241 * Clean up WCID attributes and write STA address to the device.
1242 */
1243 rt2800_delete_wcid_attr(rt2x00dev, wcid);
1244 rt2800_config_wcid(rt2x00dev, sta->addr, wcid);
1245 rt2800_config_wcid_attr_bssidx(rt2x00dev, wcid,
1246 rt2x00lib_get_bssidx(rt2x00dev, vif));
1247 return 0;
1248}
1249EXPORT_SYMBOL_GPL(rt2800_sta_add);
1250
1251int rt2800_sta_remove(struct rt2x00_dev *rt2x00dev, int wcid)
1252{
1253 /*
1254 * Remove WCID entry, no need to clean the attributes as they will
1255 * get renewed when the WCID is reused.
1256 */
1257 rt2800_config_wcid(rt2x00dev, NULL, wcid);
1258
1259 return 0;
1260}
1261EXPORT_SYMBOL_GPL(rt2800_sta_remove);
1262
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001263void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
1264 const unsigned int filter_flags)
1265{
1266 u32 reg;
1267
1268 /*
1269 * Start configuration steps.
1270 * Note that the version error will always be dropped
1271 * and broadcast frames will always be accepted since
1272 * there is no filter for it at this time.
1273 */
1274 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
1275 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
1276 !(filter_flags & FIF_FCSFAIL));
1277 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
1278 !(filter_flags & FIF_PLCPFAIL));
1279 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
1280 !(filter_flags & FIF_PROMISC_IN_BSS));
1281 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
1282 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
1283 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
1284 !(filter_flags & FIF_ALLMULTI));
1285 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
1286 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
1287 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
1288 !(filter_flags & FIF_CONTROL));
1289 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
1290 !(filter_flags & FIF_CONTROL));
1291 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
1292 !(filter_flags & FIF_CONTROL));
1293 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
1294 !(filter_flags & FIF_CONTROL));
1295 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
1296 !(filter_flags & FIF_CONTROL));
1297 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
1298 !(filter_flags & FIF_PSPOLL));
Helmut Schaa48839932011-11-24 09:13:26 +01001299 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA,
1300 !(filter_flags & FIF_CONTROL));
1301 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR,
1302 !(filter_flags & FIF_CONTROL));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001303 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
1304 !(filter_flags & FIF_CONTROL));
1305 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
1306}
1307EXPORT_SYMBOL_GPL(rt2800_config_filter);
1308
1309void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
1310 struct rt2x00intf_conf *conf, const unsigned int flags)
1311{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001312 u32 reg;
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001313 bool update_bssid = false;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001314
1315 if (flags & CONFIG_UPDATE_TYPE) {
1316 /*
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001317 * Enable synchronisation.
1318 */
1319 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001320 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001321 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Helmut Schaa15a533c2011-04-18 15:28:04 +02001322
1323 if (conf->sync == TSF_SYNC_AP_NONE) {
1324 /*
1325 * Tune beacon queue transmit parameters for AP mode
1326 */
1327 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1328 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 0);
1329 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 1);
1330 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1331 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 0);
1332 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1333 } else {
1334 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1335 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 4);
1336 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 2);
1337 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1338 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 16);
1339 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1340 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001341 }
1342
1343 if (flags & CONFIG_UPDATE_MAC) {
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001344 if (flags & CONFIG_UPDATE_TYPE &&
1345 conf->sync == TSF_SYNC_AP_NONE) {
1346 /*
1347 * The BSSID register has to be set to our own mac
1348 * address in AP mode.
1349 */
1350 memcpy(conf->bssid, conf->mac, sizeof(conf->mac));
1351 update_bssid = true;
1352 }
1353
Ivo van Doornc600c822010-08-30 21:14:15 +02001354 if (!is_zero_ether_addr((const u8 *)conf->mac)) {
1355 reg = le32_to_cpu(conf->mac[1]);
1356 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
1357 conf->mac[1] = cpu_to_le32(reg);
1358 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001359
1360 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
1361 conf->mac, sizeof(conf->mac));
1362 }
1363
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001364 if ((flags & CONFIG_UPDATE_BSSID) || update_bssid) {
Ivo van Doornc600c822010-08-30 21:14:15 +02001365 if (!is_zero_ether_addr((const u8 *)conf->bssid)) {
1366 reg = le32_to_cpu(conf->bssid[1]);
1367 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
1368 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 7);
1369 conf->bssid[1] = cpu_to_le32(reg);
1370 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001371
1372 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
1373 conf->bssid, sizeof(conf->bssid));
1374 }
1375}
1376EXPORT_SYMBOL_GPL(rt2800_config_intf);
1377
Helmut Schaa87c19152010-10-02 11:28:34 +02001378static void rt2800_config_ht_opmode(struct rt2x00_dev *rt2x00dev,
1379 struct rt2x00lib_erp *erp)
1380{
1381 bool any_sta_nongf = !!(erp->ht_opmode &
1382 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1383 u8 protection = erp->ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION;
1384 u8 mm20_mode, mm40_mode, gf20_mode, gf40_mode;
1385 u16 mm20_rate, mm40_rate, gf20_rate, gf40_rate;
1386 u32 reg;
1387
1388 /* default protection rate for HT20: OFDM 24M */
1389 mm20_rate = gf20_rate = 0x4004;
1390
1391 /* default protection rate for HT40: duplicate OFDM 24M */
1392 mm40_rate = gf40_rate = 0x4084;
1393
1394 switch (protection) {
1395 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
1396 /*
1397 * All STAs in this BSS are HT20/40 but there might be
1398 * STAs not supporting greenfield mode.
1399 * => Disable protection for HT transmissions.
1400 */
1401 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 0;
1402
1403 break;
1404 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
1405 /*
1406 * All STAs in this BSS are HT20 or HT20/40 but there
1407 * might be STAs not supporting greenfield mode.
1408 * => Protect all HT40 transmissions.
1409 */
1410 mm20_mode = gf20_mode = 0;
1411 mm40_mode = gf40_mode = 2;
1412
1413 break;
1414 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
1415 /*
1416 * Nonmember protection:
1417 * According to 802.11n we _should_ protect all
1418 * HT transmissions (but we don't have to).
1419 *
1420 * But if cts_protection is enabled we _shall_ protect
1421 * all HT transmissions using a CCK rate.
1422 *
1423 * And if any station is non GF we _shall_ protect
1424 * GF transmissions.
1425 *
1426 * We decide to protect everything
1427 * -> fall through to mixed mode.
1428 */
1429 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
1430 /*
1431 * Legacy STAs are present
1432 * => Protect all HT transmissions.
1433 */
1434 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 2;
1435
1436 /*
1437 * If erp protection is needed we have to protect HT
1438 * transmissions with CCK 11M long preamble.
1439 */
1440 if (erp->cts_protection) {
1441 /* don't duplicate RTS/CTS in CCK mode */
1442 mm20_rate = mm40_rate = 0x0003;
1443 gf20_rate = gf40_rate = 0x0003;
1444 }
1445 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001446 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001447
1448 /* check for STAs not supporting greenfield mode */
1449 if (any_sta_nongf)
1450 gf20_mode = gf40_mode = 2;
1451
1452 /* Update HT protection config */
1453 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1454 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, mm20_rate);
1455 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, mm20_mode);
1456 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1457
1458 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1459 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, mm40_rate);
1460 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, mm40_mode);
1461 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1462
1463 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1464 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, gf20_rate);
1465 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, gf20_mode);
1466 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1467
1468 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1469 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, gf40_rate);
1470 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, gf40_mode);
1471 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1472}
1473
Helmut Schaa02044642010-09-08 20:56:32 +02001474void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp,
1475 u32 changed)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001476{
1477 u32 reg;
1478
Helmut Schaa02044642010-09-08 20:56:32 +02001479 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1480 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1481 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
1482 !!erp->short_preamble);
1483 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
1484 !!erp->short_preamble);
1485 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1486 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001487
Helmut Schaa02044642010-09-08 20:56:32 +02001488 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1489 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
1490 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
1491 erp->cts_protection ? 2 : 0);
1492 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1493 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001494
Helmut Schaa02044642010-09-08 20:56:32 +02001495 if (changed & BSS_CHANGED_BASIC_RATES) {
1496 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
1497 erp->basic_rates);
1498 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1499 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001500
Helmut Schaa02044642010-09-08 20:56:32 +02001501 if (changed & BSS_CHANGED_ERP_SLOT) {
1502 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1503 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME,
1504 erp->slot_time);
1505 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001506
Helmut Schaa02044642010-09-08 20:56:32 +02001507 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
1508 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
1509 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1510 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001511
Helmut Schaa02044642010-09-08 20:56:32 +02001512 if (changed & BSS_CHANGED_BEACON_INT) {
1513 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1514 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
1515 erp->beacon_int * 16);
1516 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1517 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001518
1519 if (changed & BSS_CHANGED_HT)
1520 rt2800_config_ht_opmode(rt2x00dev, erp);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001521}
1522EXPORT_SYMBOL_GPL(rt2800_config_erp);
1523
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001524static void rt2800_config_3572bt_ant(struct rt2x00_dev *rt2x00dev)
1525{
1526 u32 reg;
1527 u16 eeprom;
1528 u8 led_ctrl, led_g_mode, led_r_mode;
1529
1530 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
1531 if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
1532 rt2x00_set_field32(&reg, GPIO_SWITCH_0, 1);
1533 rt2x00_set_field32(&reg, GPIO_SWITCH_1, 1);
1534 } else {
1535 rt2x00_set_field32(&reg, GPIO_SWITCH_0, 0);
1536 rt2x00_set_field32(&reg, GPIO_SWITCH_1, 0);
1537 }
1538 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
1539
1540 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1541 led_g_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 3 : 0;
1542 led_r_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 0 : 3;
1543 if (led_g_mode != rt2x00_get_field32(reg, LED_CFG_G_LED_MODE) ||
1544 led_r_mode != rt2x00_get_field32(reg, LED_CFG_R_LED_MODE)) {
1545 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
1546 led_ctrl = rt2x00_get_field16(eeprom, EEPROM_FREQ_LED_MODE);
1547 if (led_ctrl == 0 || led_ctrl > 0x40) {
1548 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, led_g_mode);
1549 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, led_r_mode);
1550 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1551 } else {
1552 rt2800_mcu_request(rt2x00dev, MCU_BAND_SELECT, 0xff,
1553 (led_g_mode << 2) | led_r_mode, 1);
1554 }
1555 }
1556}
1557
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001558static void rt2800_set_ant_diversity(struct rt2x00_dev *rt2x00dev,
1559 enum antenna ant)
1560{
1561 u32 reg;
1562 u8 eesk_pin = (ant == ANTENNA_A) ? 1 : 0;
1563 u8 gpio_bit3 = (ant == ANTENNA_A) ? 0 : 1;
1564
1565 if (rt2x00_is_pci(rt2x00dev)) {
1566 rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
1567 rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK, eesk_pin);
1568 rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
1569 } else if (rt2x00_is_usb(rt2x00dev))
1570 rt2800_mcu_request(rt2x00dev, MCU_ANT_SELECT, 0xff,
1571 eesk_pin, 0);
1572
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02001573 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
1574 rt2x00_set_field32(&reg, GPIO_CTRL_DIR3, 0);
1575 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, gpio_bit3);
1576 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001577}
1578
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001579void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
1580{
1581 u8 r1;
1582 u8 r3;
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001583 u16 eeprom;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001584
1585 rt2800_bbp_read(rt2x00dev, 1, &r1);
1586 rt2800_bbp_read(rt2x00dev, 3, &r3);
1587
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001588 if (rt2x00_rt(rt2x00dev, RT3572) &&
1589 test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
1590 rt2800_config_3572bt_ant(rt2x00dev);
1591
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001592 /*
1593 * Configure the TX antenna.
1594 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001595 switch (ant->tx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001596 case 1:
1597 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001598 break;
1599 case 2:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001600 if (rt2x00_rt(rt2x00dev, RT3572) &&
1601 test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
1602 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 1);
1603 else
1604 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001605 break;
1606 case 3:
Ivo van Doorne22557f2010-06-29 21:49:05 +02001607 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001608 break;
1609 }
1610
1611 /*
1612 * Configure the RX antenna.
1613 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001614 switch (ant->rx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001615 case 1:
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001616 if (rt2x00_rt(rt2x00dev, RT3070) ||
1617 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03001618 rt2x00_rt(rt2x00dev, RT3352) ||
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001619 rt2x00_rt(rt2x00dev, RT3390)) {
1620 rt2x00_eeprom_read(rt2x00dev,
1621 EEPROM_NIC_CONF1, &eeprom);
1622 if (rt2x00_get_field16(eeprom,
1623 EEPROM_NIC_CONF1_ANT_DIVERSITY))
1624 rt2800_set_ant_diversity(rt2x00dev,
1625 rt2x00dev->default_ant.rx);
1626 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001627 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
1628 break;
1629 case 2:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001630 if (rt2x00_rt(rt2x00dev, RT3572) &&
1631 test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
1632 rt2x00_set_field8(&r3, BBP3_RX_ADC, 1);
1633 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA,
1634 rt2x00dev->curr_band == IEEE80211_BAND_5GHZ);
1635 rt2800_set_ant_diversity(rt2x00dev, ANTENNA_B);
1636 } else {
1637 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
1638 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001639 break;
1640 case 3:
1641 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
1642 break;
1643 }
1644
1645 rt2800_bbp_write(rt2x00dev, 3, r3);
1646 rt2800_bbp_write(rt2x00dev, 1, r1);
1647}
1648EXPORT_SYMBOL_GPL(rt2800_config_ant);
1649
1650static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
1651 struct rt2x00lib_conf *libconf)
1652{
1653 u16 eeprom;
1654 short lna_gain;
1655
1656 if (libconf->rf.channel <= 14) {
1657 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1658 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
1659 } else if (libconf->rf.channel <= 64) {
1660 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1661 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
1662 } else if (libconf->rf.channel <= 128) {
1663 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
1664 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
1665 } else {
1666 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
1667 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
1668 }
1669
1670 rt2x00dev->lna_gain = lna_gain;
1671}
1672
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001673static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
1674 struct ieee80211_conf *conf,
1675 struct rf_channel *rf,
1676 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001677{
1678 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
1679
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001680 if (rt2x00dev->default_ant.tx_chain_num == 1)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001681 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
1682
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001683 if (rt2x00dev->default_ant.rx_chain_num == 1) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001684 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
1685 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001686 } else if (rt2x00dev->default_ant.rx_chain_num == 2)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001687 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1688
1689 if (rf->channel > 14) {
1690 /*
1691 * When TX power is below 0, we should increase it by 7 to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001692 * make it a positive value (Minimum value is -7).
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001693 * However this means that values between 0 and 7 have
1694 * double meaning, and we should set a 7DBm boost flag.
1695 */
1696 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001697 (info->default_power1 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001698
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001699 if (info->default_power1 < 0)
1700 info->default_power1 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001701
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001702 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001703
1704 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001705 (info->default_power2 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001706
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001707 if (info->default_power2 < 0)
1708 info->default_power2 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001709
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001710 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001711 } else {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001712 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G, info->default_power1);
1713 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001714 }
1715
1716 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
1717
1718 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1719 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1720 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1721 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1722
1723 udelay(200);
1724
1725 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1726 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1727 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
1728 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1729
1730 udelay(200);
1731
1732 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1733 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1734 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1735 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1736}
1737
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001738static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
1739 struct ieee80211_conf *conf,
1740 struct rf_channel *rf,
1741 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001742{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01001743 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01001744 u8 rfcsr, calib_tx, calib_rx;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001745
1746 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Stanislaw Gruszka7f4666a2012-01-30 16:17:56 +01001747
1748 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
1749 rt2x00_set_field8(&rfcsr, RFCSR3_K, rf->rf3);
1750 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001751
1752 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +02001753 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001754 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1755
1756 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001757 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001758 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
1759
Helmut Schaa5a673962010-04-23 15:54:43 +02001760 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001761 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER, info->default_power2);
Helmut Schaa5a673962010-04-23 15:54:43 +02001762 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
1763
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01001764 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
1765 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
1766 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
1767 if (rt2x00_rt(rt2x00dev, RT3390)) {
1768 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD,
1769 rt2x00dev->default_ant.rx_chain_num == 1);
1770 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD,
1771 rt2x00dev->default_ant.tx_chain_num == 1);
1772 } else {
1773 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
1774 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
1775 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
1776 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
1777
1778 switch (rt2x00dev->default_ant.tx_chain_num) {
1779 case 1:
1780 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
1781 /* fall through */
1782 case 2:
1783 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
1784 break;
1785 }
1786
1787 switch (rt2x00dev->default_ant.rx_chain_num) {
1788 case 1:
1789 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
1790 /* fall through */
1791 case 2:
1792 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
1793 break;
1794 }
1795 }
1796 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
1797
Stanislaw Gruszka3e0c7642012-01-30 16:17:58 +01001798 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1799 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1800 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1801 msleep(1);
1802 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1803 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1804
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001805 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
1806 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
1807 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
1808
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01001809 if (rt2x00_rt(rt2x00dev, RT3390)) {
1810 calib_tx = conf_is_ht40(conf) ? 0x68 : 0x4f;
1811 calib_rx = conf_is_ht40(conf) ? 0x6f : 0x4f;
1812 } else {
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01001813 if (conf_is_ht40(conf)) {
1814 calib_tx = drv_data->calibration_bw40;
1815 calib_rx = drv_data->calibration_bw40;
1816 } else {
1817 calib_tx = drv_data->calibration_bw20;
1818 calib_rx = drv_data->calibration_bw20;
1819 }
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01001820 }
1821
1822 rt2800_rfcsr_read(rt2x00dev, 24, &rfcsr);
1823 rt2x00_set_field8(&rfcsr, RFCSR24_TX_CALIB, calib_tx);
1824 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr);
1825
1826 rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
1827 rt2x00_set_field8(&rfcsr, RFCSR31_RX_CALIB, calib_rx);
1828 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001829
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001830 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001831 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001832 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Stanislaw Gruszka3e0c7642012-01-30 16:17:58 +01001833
1834 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1835 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1836 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1837 msleep(1);
1838 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1839 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001840}
1841
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001842static void rt2800_config_channel_rf3052(struct rt2x00_dev *rt2x00dev,
1843 struct ieee80211_conf *conf,
1844 struct rf_channel *rf,
1845 struct channel_info *info)
1846{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01001847 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001848 u8 rfcsr;
1849 u32 reg;
1850
1851 if (rf->channel <= 14) {
Gertjan van Wingerde5d137df2012-02-06 23:45:09 +01001852 rt2800_bbp_write(rt2x00dev, 25, drv_data->bbp25);
1853 rt2800_bbp_write(rt2x00dev, 26, drv_data->bbp26);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001854 } else {
1855 rt2800_bbp_write(rt2x00dev, 25, 0x09);
1856 rt2800_bbp_write(rt2x00dev, 26, 0xff);
1857 }
1858
1859 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
1860 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
1861
1862 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
1863 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
1864 if (rf->channel <= 14)
1865 rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 2);
1866 else
1867 rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 1);
1868 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1869
1870 rt2800_rfcsr_read(rt2x00dev, 5, &rfcsr);
1871 if (rf->channel <= 14)
1872 rt2x00_set_field8(&rfcsr, RFCSR5_R1, 1);
1873 else
1874 rt2x00_set_field8(&rfcsr, RFCSR5_R1, 2);
1875 rt2800_rfcsr_write(rt2x00dev, 5, rfcsr);
1876
1877 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
1878 if (rf->channel <= 14) {
1879 rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 3);
1880 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
Gertjan van Wingerde569ffa52012-02-06 23:45:10 +01001881 info->default_power1);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001882 } else {
1883 rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 7);
1884 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
1885 (info->default_power1 & 0x3) |
1886 ((info->default_power1 & 0xC) << 1));
1887 }
1888 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
1889
1890 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
1891 if (rf->channel <= 14) {
1892 rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 3);
1893 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
Gertjan van Wingerde569ffa52012-02-06 23:45:10 +01001894 info->default_power2);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001895 } else {
1896 rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 7);
1897 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
1898 (info->default_power2 & 0x3) |
1899 ((info->default_power2 & 0xC) << 1));
1900 }
1901 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
1902
1903 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001904 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
1905 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
1906 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
1907 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
Gertjan van Wingerde0cd461e2012-02-06 23:45:11 +01001908 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
1909 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001910 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
1911 if (rf->channel <= 14) {
1912 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
1913 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
1914 }
1915 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
1916 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
1917 } else {
1918 switch (rt2x00dev->default_ant.tx_chain_num) {
1919 case 1:
1920 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
1921 case 2:
1922 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
1923 break;
1924 }
1925
1926 switch (rt2x00dev->default_ant.rx_chain_num) {
1927 case 1:
1928 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
1929 case 2:
1930 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
1931 break;
1932 }
1933 }
1934 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
1935
1936 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
1937 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
1938 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
1939
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01001940 if (conf_is_ht40(conf)) {
1941 rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw40);
1942 rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw40);
1943 } else {
1944 rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw20);
1945 rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw20);
1946 }
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001947
1948 if (rf->channel <= 14) {
1949 rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
1950 rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
1951 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
1952 rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
1953 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01001954 rfcsr = 0x4c;
1955 rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
1956 drv_data->txmixer_gain_24g);
1957 rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001958 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
1959 rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
1960 rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
1961 rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
1962 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
1963 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
1964 rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
1965 } else {
Gertjan van Wingerde58b8ae12012-02-06 23:45:12 +01001966 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
1967 rt2x00_set_field8(&rfcsr, RFCSR7_BIT2, 1);
1968 rt2x00_set_field8(&rfcsr, RFCSR7_BIT3, 0);
1969 rt2x00_set_field8(&rfcsr, RFCSR7_BIT4, 1);
1970 rt2x00_set_field8(&rfcsr, RFCSR7_BITS67, 0);
1971 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001972 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
1973 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
1974 rt2800_rfcsr_write(rt2x00dev, 11, 0x00);
1975 rt2800_rfcsr_write(rt2x00dev, 15, 0x43);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01001976 rfcsr = 0x7a;
1977 rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
1978 drv_data->txmixer_gain_5g);
1979 rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001980 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
1981 if (rf->channel <= 64) {
1982 rt2800_rfcsr_write(rt2x00dev, 19, 0xb7);
1983 rt2800_rfcsr_write(rt2x00dev, 20, 0xf6);
1984 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
1985 } else if (rf->channel <= 128) {
1986 rt2800_rfcsr_write(rt2x00dev, 19, 0x74);
1987 rt2800_rfcsr_write(rt2x00dev, 20, 0xf4);
1988 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
1989 } else {
1990 rt2800_rfcsr_write(rt2x00dev, 19, 0x72);
1991 rt2800_rfcsr_write(rt2x00dev, 20, 0xf3);
1992 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
1993 }
1994 rt2800_rfcsr_write(rt2x00dev, 26, 0x87);
1995 rt2800_rfcsr_write(rt2x00dev, 27, 0x01);
1996 rt2800_rfcsr_write(rt2x00dev, 29, 0x9f);
1997 }
1998
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02001999 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
2000 rt2x00_set_field32(&reg, GPIO_CTRL_DIR7, 0);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002001 if (rf->channel <= 14)
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02002002 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 1);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002003 else
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02002004 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 0);
2005 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002006
2007 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
2008 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
2009 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
2010}
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002011
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002012#define POWER_BOUND 0x27
2013#define FREQ_OFFSET_BOUND 0x5f
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002014
Woody Hunga89534e2012-06-13 15:01:16 +08002015static void rt2800_config_channel_rf3290(struct rt2x00_dev *rt2x00dev,
2016 struct ieee80211_conf *conf,
2017 struct rf_channel *rf,
2018 struct channel_info *info)
2019{
2020 u8 rfcsr;
2021
2022 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2023 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2024 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2025 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
2026 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2027
2028 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002029 if (info->default_power1 > POWER_BOUND)
2030 rt2x00_set_field8(&rfcsr, RFCSR49_TX, POWER_BOUND);
Woody Hunga89534e2012-06-13 15:01:16 +08002031 else
2032 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2033 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
2034
2035 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002036 if (rt2x00dev->freq_offset > FREQ_OFFSET_BOUND)
2037 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, FREQ_OFFSET_BOUND);
Woody Hunga89534e2012-06-13 15:01:16 +08002038 else
2039 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, rt2x00dev->freq_offset);
2040 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
2041
2042 if (rf->channel <= 14) {
2043 if (rf->channel == 6)
2044 rt2800_bbp_write(rt2x00dev, 68, 0x0c);
2045 else
2046 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
2047
2048 if (rf->channel >= 1 && rf->channel <= 6)
2049 rt2800_bbp_write(rt2x00dev, 59, 0x0f);
2050 else if (rf->channel >= 7 && rf->channel <= 11)
2051 rt2800_bbp_write(rt2x00dev, 59, 0x0e);
2052 else if (rf->channel >= 12 && rf->channel <= 14)
2053 rt2800_bbp_write(rt2x00dev, 59, 0x0d);
2054 }
2055}
2056
Daniel Golle03839952012-09-09 14:24:39 +03002057static void rt2800_config_channel_rf3322(struct rt2x00_dev *rt2x00dev,
2058 struct ieee80211_conf *conf,
2059 struct rf_channel *rf,
2060 struct channel_info *info)
2061{
2062 u8 rfcsr;
2063
2064 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2065 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2066
2067 rt2800_rfcsr_write(rt2x00dev, 11, 0x42);
2068 rt2800_rfcsr_write(rt2x00dev, 12, 0x1c);
2069 rt2800_rfcsr_write(rt2x00dev, 13, 0x00);
2070
2071 if (info->default_power1 > POWER_BOUND)
2072 rt2800_rfcsr_write(rt2x00dev, 47, POWER_BOUND);
2073 else
2074 rt2800_rfcsr_write(rt2x00dev, 47, info->default_power1);
2075
2076 if (info->default_power2 > POWER_BOUND)
2077 rt2800_rfcsr_write(rt2x00dev, 48, POWER_BOUND);
2078 else
2079 rt2800_rfcsr_write(rt2x00dev, 48, info->default_power2);
2080
2081 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
2082 if (rt2x00dev->freq_offset > FREQ_OFFSET_BOUND)
2083 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, FREQ_OFFSET_BOUND);
2084 else
2085 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, rt2x00dev->freq_offset);
2086
2087 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
2088
2089 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2090 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2091 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2092
2093 if ( rt2x00dev->default_ant.tx_chain_num == 2 )
2094 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2095 else
2096 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
2097
2098 if ( rt2x00dev->default_ant.rx_chain_num == 2 )
2099 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2100 else
2101 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
2102
2103 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2104 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
2105
2106 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2107
2108 rt2800_rfcsr_write(rt2x00dev, 31, 80);
2109}
2110
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002111static void rt2800_config_channel_rf53xx(struct rt2x00_dev *rt2x00dev,
Gabor Juhosadde5882011-03-03 11:46:45 +01002112 struct ieee80211_conf *conf,
2113 struct rf_channel *rf,
2114 struct channel_info *info)
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002115{
Gabor Juhosadde5882011-03-03 11:46:45 +01002116 u8 rfcsr;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002117
Gabor Juhosadde5882011-03-03 11:46:45 +01002118 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2119 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2120 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2121 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
2122 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002123
Gabor Juhosadde5882011-03-03 11:46:45 +01002124 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002125 if (info->default_power1 > POWER_BOUND)
2126 rt2x00_set_field8(&rfcsr, RFCSR49_TX, POWER_BOUND);
Gabor Juhosadde5882011-03-03 11:46:45 +01002127 else
2128 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2129 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002130
Zero.Lincff3d1f2012-05-29 16:11:09 +08002131 if (rt2x00_rt(rt2x00dev, RT5392)) {
2132 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002133 if (info->default_power1 > POWER_BOUND)
2134 rt2x00_set_field8(&rfcsr, RFCSR50_TX, POWER_BOUND);
Zero.Lincff3d1f2012-05-29 16:11:09 +08002135 else
2136 rt2x00_set_field8(&rfcsr, RFCSR50_TX,
2137 info->default_power2);
2138 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
2139 }
2140
Gabor Juhosadde5882011-03-03 11:46:45 +01002141 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
Zero.Lincff3d1f2012-05-29 16:11:09 +08002142 if (rt2x00_rt(rt2x00dev, RT5392)) {
2143 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2144 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2145 }
Gabor Juhosadde5882011-03-03 11:46:45 +01002146 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2147 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
2148 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2149 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2150 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002151
Gabor Juhosadde5882011-03-03 11:46:45 +01002152 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002153 if (rt2x00dev->freq_offset > FREQ_OFFSET_BOUND)
2154 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, FREQ_OFFSET_BOUND);
Gabor Juhosadde5882011-03-03 11:46:45 +01002155 else
2156 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, rt2x00dev->freq_offset);
2157 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002158
Gabor Juhosadde5882011-03-03 11:46:45 +01002159 if (rf->channel <= 14) {
2160 int idx = rf->channel-1;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002161
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02002162 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002163 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
2164 /* r55/r59 value array of channel 1~14 */
2165 static const char r55_bt_rev[] = {0x83, 0x83,
2166 0x83, 0x73, 0x73, 0x63, 0x53, 0x53,
2167 0x53, 0x43, 0x43, 0x43, 0x43, 0x43};
2168 static const char r59_bt_rev[] = {0x0e, 0x0e,
2169 0x0e, 0x0e, 0x0e, 0x0b, 0x0a, 0x09,
2170 0x07, 0x07, 0x07, 0x07, 0x07, 0x07};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002171
Gabor Juhosadde5882011-03-03 11:46:45 +01002172 rt2800_rfcsr_write(rt2x00dev, 55,
2173 r55_bt_rev[idx]);
2174 rt2800_rfcsr_write(rt2x00dev, 59,
2175 r59_bt_rev[idx]);
2176 } else {
2177 static const char r59_bt[] = {0x8b, 0x8b, 0x8b,
2178 0x8b, 0x8b, 0x8b, 0x8b, 0x8a, 0x89,
2179 0x88, 0x88, 0x86, 0x85, 0x84};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002180
Gabor Juhosadde5882011-03-03 11:46:45 +01002181 rt2800_rfcsr_write(rt2x00dev, 59, r59_bt[idx]);
2182 }
2183 } else {
2184 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
2185 static const char r55_nonbt_rev[] = {0x23, 0x23,
2186 0x23, 0x23, 0x13, 0x13, 0x03, 0x03,
2187 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
2188 static const char r59_nonbt_rev[] = {0x07, 0x07,
2189 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
2190 0x07, 0x07, 0x06, 0x05, 0x04, 0x04};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002191
Gabor Juhosadde5882011-03-03 11:46:45 +01002192 rt2800_rfcsr_write(rt2x00dev, 55,
2193 r55_nonbt_rev[idx]);
2194 rt2800_rfcsr_write(rt2x00dev, 59,
2195 r59_nonbt_rev[idx]);
John Li2ed71882012-02-17 17:33:06 +08002196 } else if (rt2x00_rt(rt2x00dev, RT5390) ||
2197 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002198 static const char r59_non_bt[] = {0x8f, 0x8f,
2199 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8d,
2200 0x8a, 0x88, 0x88, 0x87, 0x87, 0x86};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002201
Gabor Juhosadde5882011-03-03 11:46:45 +01002202 rt2800_rfcsr_write(rt2x00dev, 59,
2203 r59_non_bt[idx]);
2204 }
2205 }
2206 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002207}
2208
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002209static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
2210 struct ieee80211_conf *conf,
2211 struct rf_channel *rf,
2212 struct channel_info *info)
2213{
2214 u32 reg;
2215 unsigned int tx_pin;
Woody Hunga89534e2012-06-13 15:01:16 +08002216 u8 bbp, rfcsr;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002217
Ivo van Doorn46323e12010-08-23 19:55:43 +02002218 if (rf->channel <= 14) {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002219 info->default_power1 = TXPOWER_G_TO_DEV(info->default_power1);
2220 info->default_power2 = TXPOWER_G_TO_DEV(info->default_power2);
Ivo van Doorn46323e12010-08-23 19:55:43 +02002221 } else {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002222 info->default_power1 = TXPOWER_A_TO_DEV(info->default_power1);
2223 info->default_power2 = TXPOWER_A_TO_DEV(info->default_power2);
Ivo van Doorn46323e12010-08-23 19:55:43 +02002224 }
2225
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002226 switch (rt2x00dev->chip.rf) {
2227 case RF2020:
2228 case RF3020:
2229 case RF3021:
2230 case RF3022:
2231 case RF3320:
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02002232 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002233 break;
2234 case RF3052:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002235 rt2800_config_channel_rf3052(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002236 break;
Woody Hunga89534e2012-06-13 15:01:16 +08002237 case RF3290:
2238 rt2800_config_channel_rf3290(rt2x00dev, conf, rf, info);
2239 break;
Daniel Golle03839952012-09-09 14:24:39 +03002240 case RF3322:
2241 rt2800_config_channel_rf3322(rt2x00dev, conf, rf, info);
2242 break;
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02002243 case RF5360:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002244 case RF5370:
John Li2ed71882012-02-17 17:33:06 +08002245 case RF5372:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002246 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08002247 case RF5392:
Gabor Juhosadde5882011-03-03 11:46:45 +01002248 rt2800_config_channel_rf53xx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002249 break;
2250 default:
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02002251 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002252 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002253
Woody Hunga89534e2012-06-13 15:01:16 +08002254 if (rt2x00_rf(rt2x00dev, RF3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03002255 rt2x00_rf(rt2x00dev, RF3322) ||
Woody Hunga89534e2012-06-13 15:01:16 +08002256 rt2x00_rf(rt2x00dev, RF5360) ||
2257 rt2x00_rf(rt2x00dev, RF5370) ||
2258 rt2x00_rf(rt2x00dev, RF5372) ||
2259 rt2x00_rf(rt2x00dev, RF5390) ||
2260 rt2x00_rf(rt2x00dev, RF5392)) {
2261 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2262 rt2x00_set_field8(&rfcsr, RFCSR30_TX_H20M, 0);
2263 rt2x00_set_field8(&rfcsr, RFCSR30_RX_H20M, 0);
2264 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2265
2266 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
2267 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
2268 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
2269 }
2270
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002271 /*
2272 * Change BBP settings
2273 */
Daniel Golle03839952012-09-09 14:24:39 +03002274 if (rt2x00_rt(rt2x00dev, RT3352)) {
2275 rt2800_bbp_write(rt2x00dev, 27, 0x0);
2276 rt2800_bbp_write(rt2x00dev, 62, 0x26 + rt2x00dev->lna_gain);
2277 rt2800_bbp_write(rt2x00dev, 27, 0x20);
2278 rt2800_bbp_write(rt2x00dev, 62, 0x26 + rt2x00dev->lna_gain);
2279 } else {
2280 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
2281 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
2282 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
2283 rt2800_bbp_write(rt2x00dev, 86, 0);
2284 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002285
2286 if (rf->channel <= 14) {
John Li2ed71882012-02-17 17:33:06 +08002287 if (!rt2x00_rt(rt2x00dev, RT5390) &&
2288 !rt2x00_rt(rt2x00dev, RT5392)) {
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02002289 if (test_bit(CAPABILITY_EXTERNAL_LNA_BG,
2290 &rt2x00dev->cap_flags)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002291 rt2800_bbp_write(rt2x00dev, 82, 0x62);
2292 rt2800_bbp_write(rt2x00dev, 75, 0x46);
2293 } else {
2294 rt2800_bbp_write(rt2x00dev, 82, 0x84);
2295 rt2800_bbp_write(rt2x00dev, 75, 0x50);
2296 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002297 }
2298 } else {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002299 if (rt2x00_rt(rt2x00dev, RT3572))
2300 rt2800_bbp_write(rt2x00dev, 82, 0x94);
2301 else
2302 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002303
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02002304 if (test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002305 rt2800_bbp_write(rt2x00dev, 75, 0x46);
2306 else
2307 rt2800_bbp_write(rt2x00dev, 75, 0x50);
2308 }
2309
2310 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02002311 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002312 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
2313 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
2314 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
2315
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002316 if (rt2x00_rt(rt2x00dev, RT3572))
2317 rt2800_rfcsr_write(rt2x00dev, 8, 0);
2318
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002319 tx_pin = 0;
2320
2321 /* Turn on unused PA or LNA when not using 1T or 1R */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01002322 if (rt2x00dev->default_ant.tx_chain_num == 2) {
Gertjan van Wingerde65f31b52011-05-18 20:25:05 +02002323 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN,
2324 rf->channel > 14);
2325 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN,
2326 rf->channel <= 14);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002327 }
2328
2329 /* Turn on unused PA or LNA when not using 1T or 1R */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01002330 if (rt2x00dev->default_ant.rx_chain_num == 2) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002331 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
2332 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
2333 }
2334
2335 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
2336 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
2337 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
2338 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
Gertjan van Wingerde8f96e912011-05-18 20:25:18 +02002339 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
2340 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
2341 else
2342 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN,
2343 rf->channel <= 14);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002344 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
2345
2346 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
2347
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002348 if (rt2x00_rt(rt2x00dev, RT3572))
2349 rt2800_rfcsr_write(rt2x00dev, 8, 0x80);
2350
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002351 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2352 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
2353 rt2800_bbp_write(rt2x00dev, 4, bbp);
2354
2355 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02002356 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002357 rt2800_bbp_write(rt2x00dev, 3, bbp);
2358
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02002359 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002360 if (conf_is_ht40(conf)) {
2361 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
2362 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
2363 rt2800_bbp_write(rt2x00dev, 73, 0x16);
2364 } else {
2365 rt2800_bbp_write(rt2x00dev, 69, 0x16);
2366 rt2800_bbp_write(rt2x00dev, 70, 0x08);
2367 rt2800_bbp_write(rt2x00dev, 73, 0x11);
2368 }
2369 }
2370
2371 msleep(1);
Helmut Schaa977206d2010-12-13 12:31:58 +01002372
2373 /*
2374 * Clear channel statistic counters
2375 */
2376 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &reg);
2377 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &reg);
2378 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &reg);
Daniel Golle03839952012-09-09 14:24:39 +03002379
2380 /*
2381 * Clear update flag
2382 */
2383 if (rt2x00_rt(rt2x00dev, RT3352)) {
2384 rt2800_bbp_read(rt2x00dev, 49, &bbp);
2385 rt2x00_set_field8(&bbp, BBP49_UPDATE_FLAG, 0);
2386 rt2800_bbp_write(rt2x00dev, 49, bbp);
2387 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002388}
2389
Helmut Schaa9e33a352011-03-28 13:33:40 +02002390static int rt2800_get_gain_calibration_delta(struct rt2x00_dev *rt2x00dev)
2391{
2392 u8 tssi_bounds[9];
2393 u8 current_tssi;
2394 u16 eeprom;
2395 u8 step;
2396 int i;
2397
2398 /*
2399 * Read TSSI boundaries for temperature compensation from
2400 * the EEPROM.
2401 *
2402 * Array idx 0 1 2 3 4 5 6 7 8
2403 * Matching Delta value -4 -3 -2 -1 0 +1 +2 +3 +4
2404 * Example TSSI bounds 0xF0 0xD0 0xB5 0xA0 0x88 0x45 0x25 0x15 0x00
2405 */
2406 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
2407 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG1, &eeprom);
2408 tssi_bounds[0] = rt2x00_get_field16(eeprom,
2409 EEPROM_TSSI_BOUND_BG1_MINUS4);
2410 tssi_bounds[1] = rt2x00_get_field16(eeprom,
2411 EEPROM_TSSI_BOUND_BG1_MINUS3);
2412
2413 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG2, &eeprom);
2414 tssi_bounds[2] = rt2x00_get_field16(eeprom,
2415 EEPROM_TSSI_BOUND_BG2_MINUS2);
2416 tssi_bounds[3] = rt2x00_get_field16(eeprom,
2417 EEPROM_TSSI_BOUND_BG2_MINUS1);
2418
2419 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG3, &eeprom);
2420 tssi_bounds[4] = rt2x00_get_field16(eeprom,
2421 EEPROM_TSSI_BOUND_BG3_REF);
2422 tssi_bounds[5] = rt2x00_get_field16(eeprom,
2423 EEPROM_TSSI_BOUND_BG3_PLUS1);
2424
2425 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG4, &eeprom);
2426 tssi_bounds[6] = rt2x00_get_field16(eeprom,
2427 EEPROM_TSSI_BOUND_BG4_PLUS2);
2428 tssi_bounds[7] = rt2x00_get_field16(eeprom,
2429 EEPROM_TSSI_BOUND_BG4_PLUS3);
2430
2431 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG5, &eeprom);
2432 tssi_bounds[8] = rt2x00_get_field16(eeprom,
2433 EEPROM_TSSI_BOUND_BG5_PLUS4);
2434
2435 step = rt2x00_get_field16(eeprom,
2436 EEPROM_TSSI_BOUND_BG5_AGC_STEP);
2437 } else {
2438 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A1, &eeprom);
2439 tssi_bounds[0] = rt2x00_get_field16(eeprom,
2440 EEPROM_TSSI_BOUND_A1_MINUS4);
2441 tssi_bounds[1] = rt2x00_get_field16(eeprom,
2442 EEPROM_TSSI_BOUND_A1_MINUS3);
2443
2444 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A2, &eeprom);
2445 tssi_bounds[2] = rt2x00_get_field16(eeprom,
2446 EEPROM_TSSI_BOUND_A2_MINUS2);
2447 tssi_bounds[3] = rt2x00_get_field16(eeprom,
2448 EEPROM_TSSI_BOUND_A2_MINUS1);
2449
2450 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A3, &eeprom);
2451 tssi_bounds[4] = rt2x00_get_field16(eeprom,
2452 EEPROM_TSSI_BOUND_A3_REF);
2453 tssi_bounds[5] = rt2x00_get_field16(eeprom,
2454 EEPROM_TSSI_BOUND_A3_PLUS1);
2455
2456 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A4, &eeprom);
2457 tssi_bounds[6] = rt2x00_get_field16(eeprom,
2458 EEPROM_TSSI_BOUND_A4_PLUS2);
2459 tssi_bounds[7] = rt2x00_get_field16(eeprom,
2460 EEPROM_TSSI_BOUND_A4_PLUS3);
2461
2462 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A5, &eeprom);
2463 tssi_bounds[8] = rt2x00_get_field16(eeprom,
2464 EEPROM_TSSI_BOUND_A5_PLUS4);
2465
2466 step = rt2x00_get_field16(eeprom,
2467 EEPROM_TSSI_BOUND_A5_AGC_STEP);
2468 }
2469
2470 /*
2471 * Check if temperature compensation is supported.
2472 */
2473 if (tssi_bounds[4] == 0xff)
2474 return 0;
2475
2476 /*
2477 * Read current TSSI (BBP 49).
2478 */
2479 rt2800_bbp_read(rt2x00dev, 49, &current_tssi);
2480
2481 /*
2482 * Compare TSSI value (BBP49) with the compensation boundaries
2483 * from the EEPROM and increase or decrease tx power.
2484 */
2485 for (i = 0; i <= 3; i++) {
2486 if (current_tssi > tssi_bounds[i])
2487 break;
2488 }
2489
2490 if (i == 4) {
2491 for (i = 8; i >= 5; i--) {
2492 if (current_tssi < tssi_bounds[i])
2493 break;
2494 }
2495 }
2496
2497 return (i - 4) * step;
2498}
2499
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002500static int rt2800_get_txpower_bw_comp(struct rt2x00_dev *rt2x00dev,
2501 enum ieee80211_band band)
2502{
2503 u16 eeprom;
2504 u8 comp_en;
2505 u8 comp_type;
Helmut Schaa75faae82011-03-28 13:31:30 +02002506 int comp_value = 0;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002507
2508 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_DELTA, &eeprom);
2509
Helmut Schaa75faae82011-03-28 13:31:30 +02002510 /*
2511 * HT40 compensation not required.
2512 */
2513 if (eeprom == 0xffff ||
2514 !test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002515 return 0;
2516
2517 if (band == IEEE80211_BAND_2GHZ) {
2518 comp_en = rt2x00_get_field16(eeprom,
2519 EEPROM_TXPOWER_DELTA_ENABLE_2G);
2520 if (comp_en) {
2521 comp_type = rt2x00_get_field16(eeprom,
2522 EEPROM_TXPOWER_DELTA_TYPE_2G);
2523 comp_value = rt2x00_get_field16(eeprom,
2524 EEPROM_TXPOWER_DELTA_VALUE_2G);
2525 if (!comp_type)
2526 comp_value = -comp_value;
2527 }
2528 } else {
2529 comp_en = rt2x00_get_field16(eeprom,
2530 EEPROM_TXPOWER_DELTA_ENABLE_5G);
2531 if (comp_en) {
2532 comp_type = rt2x00_get_field16(eeprom,
2533 EEPROM_TXPOWER_DELTA_TYPE_5G);
2534 comp_value = rt2x00_get_field16(eeprom,
2535 EEPROM_TXPOWER_DELTA_VALUE_5G);
2536 if (!comp_type)
2537 comp_value = -comp_value;
2538 }
2539 }
2540
2541 return comp_value;
2542}
2543
Helmut Schaafa71a162011-03-28 13:32:32 +02002544static u8 rt2800_compensate_txpower(struct rt2x00_dev *rt2x00dev, int is_rate_b,
2545 enum ieee80211_band band, int power_level,
2546 u8 txpower, int delta)
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002547{
2548 u32 reg;
2549 u16 eeprom;
2550 u8 criterion;
2551 u8 eirp_txpower;
2552 u8 eirp_txpower_criterion;
2553 u8 reg_limit;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002554
2555 if (!((band == IEEE80211_BAND_5GHZ) && is_rate_b))
2556 return txpower;
2557
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02002558 if (test_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags)) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002559 /*
2560 * Check if eirp txpower exceed txpower_limit.
2561 * We use OFDM 6M as criterion and its eirp txpower
2562 * is stored at EEPROM_EIRP_MAX_TX_POWER.
2563 * .11b data rate need add additional 4dbm
2564 * when calculating eirp txpower.
2565 */
2566 rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
2567 criterion = rt2x00_get_field32(reg, TX_PWR_CFG_0_6MBS);
2568
2569 rt2x00_eeprom_read(rt2x00dev,
2570 EEPROM_EIRP_MAX_TX_POWER, &eeprom);
2571
2572 if (band == IEEE80211_BAND_2GHZ)
2573 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
2574 EEPROM_EIRP_MAX_TX_POWER_2GHZ);
2575 else
2576 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
2577 EEPROM_EIRP_MAX_TX_POWER_5GHZ);
2578
2579 eirp_txpower = eirp_txpower_criterion + (txpower - criterion) +
Helmut Schaa2af242e2011-03-28 13:32:01 +02002580 (is_rate_b ? 4 : 0) + delta;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002581
2582 reg_limit = (eirp_txpower > power_level) ?
2583 (eirp_txpower - power_level) : 0;
2584 } else
2585 reg_limit = 0;
2586
Helmut Schaa2af242e2011-03-28 13:32:01 +02002587 return txpower + delta - reg_limit;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002588}
2589
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002590static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
Helmut Schaa9e33a352011-03-28 13:33:40 +02002591 enum ieee80211_band band,
2592 int power_level)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002593{
Helmut Schaa5e846002010-07-11 12:23:09 +02002594 u8 txpower;
Helmut Schaa5e846002010-07-11 12:23:09 +02002595 u16 eeprom;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002596 int i, is_rate_b;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002597 u32 reg;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002598 u8 r1;
Helmut Schaa5e846002010-07-11 12:23:09 +02002599 u32 offset;
Helmut Schaa2af242e2011-03-28 13:32:01 +02002600 int delta;
2601
2602 /*
2603 * Calculate HT40 compensation delta
2604 */
2605 delta = rt2800_get_txpower_bw_comp(rt2x00dev, band);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002606
Helmut Schaa5e846002010-07-11 12:23:09 +02002607 /*
Helmut Schaa9e33a352011-03-28 13:33:40 +02002608 * calculate temperature compensation delta
2609 */
2610 delta += rt2800_get_gain_calibration_delta(rt2x00dev);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002611
Helmut Schaa5e846002010-07-11 12:23:09 +02002612 /*
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002613 * set to normal bbp tx power control mode: +/- 0dBm
Helmut Schaa5e846002010-07-11 12:23:09 +02002614 */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002615 rt2800_bbp_read(rt2x00dev, 1, &r1);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002616 rt2x00_set_field8(&r1, BBP1_TX_POWER_CTRL, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002617 rt2800_bbp_write(rt2x00dev, 1, r1);
Helmut Schaa5e846002010-07-11 12:23:09 +02002618 offset = TX_PWR_CFG_0;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002619
Helmut Schaa5e846002010-07-11 12:23:09 +02002620 for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
2621 /* just to be safe */
2622 if (offset > TX_PWR_CFG_4)
2623 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002624
Helmut Schaa5e846002010-07-11 12:23:09 +02002625 rt2800_register_read(rt2x00dev, offset, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002626
Helmut Schaa5e846002010-07-11 12:23:09 +02002627 /* read the next four txpower values */
2628 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i,
2629 &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002630
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002631 is_rate_b = i ? 0 : 1;
2632 /*
2633 * TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02002634 * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002635 * TX_PWR_CFG_4: unknown
2636 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002637 txpower = rt2x00_get_field16(eeprom,
2638 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02002639 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002640 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002641 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002642
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002643 /*
2644 * TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02002645 * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002646 * TX_PWR_CFG_4: unknown
2647 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002648 txpower = rt2x00_get_field16(eeprom,
2649 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02002650 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002651 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002652 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002653
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002654 /*
2655 * TX_PWR_CFG_0: 5.5MBS, TX_PWR_CFG_1: 48MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02002656 * TX_PWR_CFG_2: MCS6, TX_PWR_CFG_3: MCS14,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002657 * TX_PWR_CFG_4: unknown
2658 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002659 txpower = rt2x00_get_field16(eeprom,
2660 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02002661 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002662 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002663 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002664
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002665 /*
2666 * TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02002667 * TX_PWR_CFG_2: MCS7, TX_PWR_CFG_3: MCS15,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002668 * TX_PWR_CFG_4: unknown
2669 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002670 txpower = rt2x00_get_field16(eeprom,
2671 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02002672 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002673 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002674 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002675
2676 /* read the next four txpower values */
2677 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i + 1,
2678 &eeprom);
2679
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002680 is_rate_b = 0;
2681 /*
2682 * TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
Helmut Schaa5e846002010-07-11 12:23:09 +02002683 * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002684 * TX_PWR_CFG_4: unknown
2685 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002686 txpower = rt2x00_get_field16(eeprom,
2687 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02002688 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002689 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002690 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002691
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002692 /*
2693 * TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
Helmut Schaa5e846002010-07-11 12:23:09 +02002694 * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002695 * TX_PWR_CFG_4: unknown
2696 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002697 txpower = rt2x00_get_field16(eeprom,
2698 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02002699 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002700 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002701 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002702
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002703 /*
2704 * TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
Helmut Schaa5e846002010-07-11 12:23:09 +02002705 * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002706 * TX_PWR_CFG_4: unknown
2707 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002708 txpower = rt2x00_get_field16(eeprom,
2709 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02002710 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002711 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002712 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002713
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002714 /*
2715 * TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
Helmut Schaa5e846002010-07-11 12:23:09 +02002716 * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002717 * TX_PWR_CFG_4: unknown
2718 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002719 txpower = rt2x00_get_field16(eeprom,
2720 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02002721 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002722 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002723 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002724
2725 rt2800_register_write(rt2x00dev, offset, reg);
2726
2727 /* next TX_PWR_CFG register */
2728 offset += 4;
2729 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002730}
2731
Helmut Schaa9e33a352011-03-28 13:33:40 +02002732void rt2800_gain_calibration(struct rt2x00_dev *rt2x00dev)
2733{
2734 rt2800_config_txpower(rt2x00dev, rt2x00dev->curr_band,
2735 rt2x00dev->tx_power);
2736}
2737EXPORT_SYMBOL_GPL(rt2800_gain_calibration);
2738
John Li2e9c43d2012-02-16 21:40:57 +08002739void rt2800_vco_calibration(struct rt2x00_dev *rt2x00dev)
2740{
2741 u32 tx_pin;
2742 u8 rfcsr;
2743
2744 /*
2745 * A voltage-controlled oscillator(VCO) is an electronic oscillator
2746 * designed to be controlled in oscillation frequency by a voltage
2747 * input. Maybe the temperature will affect the frequency of
2748 * oscillation to be shifted. The VCO calibration will be called
2749 * periodically to adjust the frequency to be precision.
2750 */
2751
2752 rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
2753 tx_pin &= TX_PIN_CFG_PA_PE_DISABLE;
2754 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
2755
2756 switch (rt2x00dev->chip.rf) {
2757 case RF2020:
2758 case RF3020:
2759 case RF3021:
2760 case RF3022:
2761 case RF3320:
2762 case RF3052:
2763 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
2764 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
2765 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
2766 break;
Woody Hunga89534e2012-06-13 15:01:16 +08002767 case RF3290:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02002768 case RF5360:
John Li2e9c43d2012-02-16 21:40:57 +08002769 case RF5370:
2770 case RF5372:
2771 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08002772 case RF5392:
John Li2e9c43d2012-02-16 21:40:57 +08002773 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
2774 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
2775 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
2776 break;
2777 default:
2778 return;
2779 }
2780
2781 mdelay(1);
2782
2783 rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
2784 if (rt2x00dev->rf_channel <= 14) {
2785 switch (rt2x00dev->default_ant.tx_chain_num) {
2786 case 3:
2787 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G2_EN, 1);
2788 /* fall through */
2789 case 2:
2790 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
2791 /* fall through */
2792 case 1:
2793 default:
2794 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
2795 break;
2796 }
2797 } else {
2798 switch (rt2x00dev->default_ant.tx_chain_num) {
2799 case 3:
2800 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A2_EN, 1);
2801 /* fall through */
2802 case 2:
2803 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
2804 /* fall through */
2805 case 1:
2806 default:
2807 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, 1);
2808 break;
2809 }
2810 }
2811 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
2812
2813}
2814EXPORT_SYMBOL_GPL(rt2800_vco_calibration);
2815
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002816static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
2817 struct rt2x00lib_conf *libconf)
2818{
2819 u32 reg;
2820
2821 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
2822 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
2823 libconf->conf->short_frame_max_tx_count);
2824 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
2825 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002826 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
2827}
2828
2829static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
2830 struct rt2x00lib_conf *libconf)
2831{
2832 enum dev_state state =
2833 (libconf->conf->flags & IEEE80211_CONF_PS) ?
2834 STATE_SLEEP : STATE_AWAKE;
2835 u32 reg;
2836
2837 if (state == STATE_SLEEP) {
2838 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
2839
2840 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
2841 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
2842 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
2843 libconf->conf->listen_interval - 1);
2844 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
2845 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
2846
2847 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
2848 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002849 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
2850 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
2851 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
2852 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
2853 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02002854
2855 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002856 }
2857}
2858
2859void rt2800_config(struct rt2x00_dev *rt2x00dev,
2860 struct rt2x00lib_conf *libconf,
2861 const unsigned int flags)
2862{
2863 /* Always recalculate LNA gain before changing configuration */
2864 rt2800_config_lna_gain(rt2x00dev, libconf);
2865
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002866 if (flags & IEEE80211_CONF_CHANGE_CHANNEL) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002867 rt2800_config_channel(rt2x00dev, libconf->conf,
2868 &libconf->rf, &libconf->channel);
Helmut Schaa9e33a352011-03-28 13:33:40 +02002869 rt2800_config_txpower(rt2x00dev, libconf->conf->channel->band,
2870 libconf->conf->power_level);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002871 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002872 if (flags & IEEE80211_CONF_CHANGE_POWER)
Helmut Schaa9e33a352011-03-28 13:33:40 +02002873 rt2800_config_txpower(rt2x00dev, libconf->conf->channel->band,
2874 libconf->conf->power_level);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002875 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
2876 rt2800_config_retry_limit(rt2x00dev, libconf);
2877 if (flags & IEEE80211_CONF_CHANGE_PS)
2878 rt2800_config_ps(rt2x00dev, libconf);
2879}
2880EXPORT_SYMBOL_GPL(rt2800_config);
2881
2882/*
2883 * Link tuning
2884 */
2885void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
2886{
2887 u32 reg;
2888
2889 /*
2890 * Update FCS error count from register.
2891 */
2892 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
2893 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
2894}
2895EXPORT_SYMBOL_GPL(rt2800_link_stats);
2896
2897static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
2898{
2899 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002900 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002901 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002902 rt2x00_rt(rt2x00dev, RT3090) ||
Woody Hunga89534e2012-06-13 15:01:16 +08002903 rt2x00_rt(rt2x00dev, RT3290) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01002904 rt2x00_rt(rt2x00dev, RT3390) ||
John Li2ed71882012-02-17 17:33:06 +08002905 rt2x00_rt(rt2x00dev, RT5390) ||
2906 rt2x00_rt(rt2x00dev, RT5392))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002907 return 0x1c + (2 * rt2x00dev->lna_gain);
2908 else
2909 return 0x2e + rt2x00dev->lna_gain;
2910 }
2911
2912 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
2913 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
2914 else
2915 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
2916}
2917
2918static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
2919 struct link_qual *qual, u8 vgc_level)
2920{
2921 if (qual->vgc_level != vgc_level) {
2922 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
2923 qual->vgc_level = vgc_level;
2924 qual->vgc_level_reg = vgc_level;
2925 }
2926}
2927
2928void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
2929{
2930 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
2931}
2932EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
2933
2934void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
2935 const u32 count)
2936{
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02002937 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002938 return;
2939
2940 /*
2941 * When RSSI is better then -80 increase VGC level with 0x10
2942 */
2943 rt2800_set_vgc(rt2x00dev, qual,
2944 rt2800_get_default_vgc(rt2x00dev) +
2945 ((qual->rssi > -80) * 0x10));
2946}
2947EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002948
2949/*
2950 * Initialization functions.
2951 */
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002952static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002953{
2954 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002955 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002956 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02002957 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002958
Jakub Kicinskif7b395e2012-04-03 03:40:47 +02002959 rt2800_disable_wpdma(rt2x00dev);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002960
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02002961 ret = rt2800_drv_init_registers(rt2x00dev);
2962 if (ret)
2963 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002964
2965 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
2966 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
2967 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
2968 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
2969 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
2970 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
2971
2972 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
2973 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
2974 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
2975 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
2976 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
2977 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
2978
2979 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
2980 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
2981
2982 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
2983
2984 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Helmut Schaa8544df32010-07-11 12:29:49 +02002985 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002986 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
2987 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
2988 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
2989 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
2990 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
2991 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
2992
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002993 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
2994
2995 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
2996 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
2997 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
2998 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
2999
Woody Hunga89534e2012-06-13 15:01:16 +08003000 if (rt2x00_rt(rt2x00dev, RT3290)) {
3001 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
3002 if (rt2x00_get_field32(reg, WLAN_EN) == 1) {
3003 rt2x00_set_field32(&reg, PCIE_APP0_CLK_REQ, 1);
3004 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
3005 }
3006
3007 rt2800_register_read(rt2x00dev, CMB_CTRL, &reg);
3008 if (!(rt2x00_get_field32(reg, LDO0_EN) == 1)) {
3009 rt2x00_set_field32(&reg, LDO0_EN, 1);
3010 rt2x00_set_field32(&reg, LDO_BGSEL, 3);
3011 rt2800_register_write(rt2x00dev, CMB_CTRL, reg);
3012 }
3013
3014 rt2800_register_read(rt2x00dev, OSC_CTRL, &reg);
3015 rt2x00_set_field32(&reg, OSC_ROSC_EN, 1);
3016 rt2x00_set_field32(&reg, OSC_CAL_REQ, 1);
3017 rt2x00_set_field32(&reg, OSC_REF_CYCLE, 0x27);
3018 rt2800_register_write(rt2x00dev, OSC_CTRL, reg);
3019
3020 rt2800_register_read(rt2x00dev, COEX_CFG0, &reg);
3021 rt2x00_set_field32(&reg, COEX_CFG_ANT, 0x5e);
3022 rt2800_register_write(rt2x00dev, COEX_CFG0, reg);
3023
3024 rt2800_register_read(rt2x00dev, COEX_CFG2, &reg);
3025 rt2x00_set_field32(&reg, BT_COEX_CFG1, 0x00);
3026 rt2x00_set_field32(&reg, BT_COEX_CFG0, 0x17);
3027 rt2x00_set_field32(&reg, WL_COEX_CFG1, 0x93);
3028 rt2x00_set_field32(&reg, WL_COEX_CFG0, 0x7f);
3029 rt2800_register_write(rt2x00dev, COEX_CFG2, reg);
3030
3031 rt2800_register_read(rt2x00dev, PLL_CTRL, &reg);
3032 rt2x00_set_field32(&reg, PLL_CONTROL, 1);
3033 rt2800_register_write(rt2x00dev, PLL_CTRL, reg);
3034 }
3035
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003036 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003037 rt2x00_rt(rt2x00dev, RT3090) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003038 rt2x00_rt(rt2x00dev, RT3290) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003039 rt2x00_rt(rt2x00dev, RT3390)) {
Woody Hunga89534e2012-06-13 15:01:16 +08003040
3041 if (rt2x00_rt(rt2x00dev, RT3290))
3042 rt2800_register_write(rt2x00dev, TX_SW_CFG0,
3043 0x00000404);
3044 else
3045 rt2800_register_write(rt2x00dev, TX_SW_CFG0,
3046 0x00000400);
3047
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003048 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003049 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003050 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
3051 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003052 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
3053 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003054 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
3055 0x0000002c);
3056 else
3057 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
3058 0x0000000f);
3059 } else {
3060 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
3061 }
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003062 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003063 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003064
3065 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
3066 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
3067 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
3068 } else {
3069 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
3070 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
3071 }
Helmut Schaac295a812010-06-03 10:52:13 +02003072 } else if (rt2800_is_305x_soc(rt2x00dev)) {
3073 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
3074 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Helmut Schaa961636b2011-04-18 15:28:27 +02003075 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000030);
Daniel Golle03839952012-09-09 14:24:39 +03003076 } else if (rt2x00_rt(rt2x00dev, RT3352)) {
3077 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000402);
3078 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
3079 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003080 } else if (rt2x00_rt(rt2x00dev, RT3572)) {
3081 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
3082 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
John Li2ed71882012-02-17 17:33:06 +08003083 } else if (rt2x00_rt(rt2x00dev, RT5390) ||
3084 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01003085 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000404);
3086 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
3087 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003088 } else {
3089 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
3090 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
3091 }
3092
3093 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
3094 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
3095 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
3096 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
3097 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
3098 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
3099 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
3100 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
3101 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
3102 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
3103
3104 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
3105 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003106 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003107 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
3108 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
3109
3110 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
3111 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02003112 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01003113 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02003114 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003115 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
3116 else
3117 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
3118 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
3119 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
3120 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
3121
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003122 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
3123 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
3124 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
3125 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
3126 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
3127 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
3128 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
3129 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
3130 rt2800_register_write(rt2x00dev, LED_CFG, reg);
3131
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003132 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
3133
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003134 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
3135 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
3136 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
3137 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
3138 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
3139 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
3140 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
3141 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
3142
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003143 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
3144 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003145 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003146 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
3147 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003148 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003149 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
3150 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
3151 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
3152
3153 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003154 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003155 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003156 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003157 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3158 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3159 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003160 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003161 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003162 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
3163 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003164 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
3165
3166 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003167 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003168 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003169 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003170 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3171 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3172 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003173 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003174 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003175 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
3176 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003177 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
3178
3179 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
3180 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
3181 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003182 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003183 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3184 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3185 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
3186 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
3187 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
3188 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003189 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003190 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
3191
3192 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
3193 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Helmut Schaad13a97f2010-10-02 11:29:08 +02003194 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003195 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003196 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3197 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3198 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
3199 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
3200 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
3201 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003202 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003203 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
3204
3205 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
3206 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
3207 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003208 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003209 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3210 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3211 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
3212 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
3213 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
3214 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003215 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003216 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
3217
3218 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
3219 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
3220 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003221 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003222 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3223 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3224 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
3225 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
3226 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
3227 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003228 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003229 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
3230
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01003231 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003232 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
3233
3234 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
3235 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
3236 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
3237 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
3238 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
3239 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
3240 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
3241 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
3242 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
3243 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
3244 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
3245 }
3246
Helmut Schaa961621a2010-11-04 20:36:59 +01003247 /*
3248 * The legacy driver also sets TXOP_CTRL_CFG_RESERVED_TRUN_EN to 1
3249 * although it is reserved.
3250 */
3251 rt2800_register_read(rt2x00dev, TXOP_CTRL_CFG, &reg);
3252 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TIMEOUT_TRUN_EN, 1);
3253 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_AC_TRUN_EN, 1);
3254 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TXRATEGRP_TRUN_EN, 1);
3255 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_USER_MODE_TRUN_EN, 1);
3256 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_MIMO_PS_TRUN_EN, 1);
3257 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_RESERVED_TRUN_EN, 1);
3258 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_LSIG_TXOP_EN, 0);
3259 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_EN, 0);
3260 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_DLY, 88);
3261 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CWMIN, 0);
3262 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, reg);
3263
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003264 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
3265
3266 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
3267 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
3268 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
3269 IEEE80211_MAX_RTS_THRESHOLD);
3270 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
3271 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
3272
3273 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003274
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02003275 /*
3276 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
3277 * time should be set to 16. However, the original Ralink driver uses
3278 * 16 for both and indeed using a value of 10 for CCK SIFS results in
3279 * connection problems with 11g + CTS protection. Hence, use the same
3280 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
3281 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003282 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02003283 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
3284 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003285 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
3286 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
3287 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
3288 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
3289
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003290 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
3291
3292 /*
3293 * ASIC will keep garbage value after boot, clear encryption keys.
3294 */
3295 for (i = 0; i < 4; i++)
3296 rt2800_register_write(rt2x00dev,
3297 SHARED_KEY_MODE_ENTRY(i), 0);
3298
3299 for (i = 0; i < 256; i++) {
Helmut Schaad7d259d2011-09-08 14:39:04 +02003300 rt2800_config_wcid(rt2x00dev, NULL, i);
3301 rt2800_delete_wcid_attr(rt2x00dev, i);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003302 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
3303 }
3304
3305 /*
3306 * Clear all beacons
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003307 */
Helmut Schaa69cf36a2011-01-30 13:16:03 +01003308 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE0);
3309 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE1);
3310 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE2);
3311 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE3);
3312 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE4);
3313 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE5);
3314 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE6);
3315 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE7);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003316
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01003317 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02003318 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
3319 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
3320 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
RA-Jay Hungc6fcc0e2011-01-30 13:21:22 +01003321 } else if (rt2x00_is_pcie(rt2x00dev)) {
3322 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
3323 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 125);
3324 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003325 }
3326
3327 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
3328 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
3329 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
3330 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
3331 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
3332 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
3333 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
3334 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
3335 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
3336 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
3337
3338 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
3339 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
3340 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
3341 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
3342 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
3343 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
3344 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
3345 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
3346 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
3347 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
3348
3349 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
3350 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
3351 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
3352 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
3353 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
3354 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
3355 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
3356 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
3357 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
3358 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
3359
3360 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
3361 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
3362 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
3363 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
3364 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
3365 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
3366
3367 /*
Helmut Schaa47ee3eb2010-09-08 20:56:04 +02003368 * Do not force the BA window size, we use the TXWI to set it
3369 */
3370 rt2800_register_read(rt2x00dev, AMPDU_BA_WINSIZE, &reg);
3371 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE_ENABLE, 0);
3372 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE, 0);
3373 rt2800_register_write(rt2x00dev, AMPDU_BA_WINSIZE, reg);
3374
3375 /*
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003376 * We must clear the error counters.
3377 * These registers are cleared on read,
3378 * so we may pass a useless variable to store the value.
3379 */
3380 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
3381 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
3382 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
3383 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
3384 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
3385 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
3386
Helmut Schaa9f926fb2010-07-11 12:28:23 +02003387 /*
3388 * Setup leadtime for pre tbtt interrupt to 6ms
3389 */
3390 rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
3391 rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
3392 rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
3393
Helmut Schaa977206d2010-12-13 12:31:58 +01003394 /*
3395 * Set up channel statistics timer
3396 */
3397 rt2800_register_read(rt2x00dev, CH_TIME_CFG, &reg);
3398 rt2x00_set_field32(&reg, CH_TIME_CFG_EIFS_BUSY, 1);
3399 rt2x00_set_field32(&reg, CH_TIME_CFG_NAV_BUSY, 1);
3400 rt2x00_set_field32(&reg, CH_TIME_CFG_RX_BUSY, 1);
3401 rt2x00_set_field32(&reg, CH_TIME_CFG_TX_BUSY, 1);
3402 rt2x00_set_field32(&reg, CH_TIME_CFG_TMR_EN, 1);
3403 rt2800_register_write(rt2x00dev, CH_TIME_CFG, reg);
3404
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003405 return 0;
3406}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003407
3408static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
3409{
3410 unsigned int i;
3411 u32 reg;
3412
3413 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
3414 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
3415 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
3416 return 0;
3417
3418 udelay(REGISTER_BUSY_DELAY);
3419 }
3420
3421 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
3422 return -EACCES;
3423}
3424
3425static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
3426{
3427 unsigned int i;
3428 u8 value;
3429
3430 /*
3431 * BBP was enabled after firmware was loaded,
3432 * but we need to reactivate it now.
3433 */
3434 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
3435 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
3436 msleep(1);
3437
3438 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
3439 rt2800_bbp_read(rt2x00dev, 0, &value);
3440 if ((value != 0xff) && (value != 0x00))
3441 return 0;
3442 udelay(REGISTER_BUSY_DELAY);
3443 }
3444
3445 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
3446 return -EACCES;
3447}
3448
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003449static int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003450{
3451 unsigned int i;
3452 u16 eeprom;
3453 u8 reg_id;
3454 u8 value;
3455
3456 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
3457 rt2800_wait_bbp_ready(rt2x00dev)))
3458 return -EACCES;
3459
Daniel Golle03839952012-09-09 14:24:39 +03003460 if (rt2x00_rt(rt2x00dev, RT3352)) {
3461 rt2800_bbp_write(rt2x00dev, 3, 0x00);
3462 rt2800_bbp_write(rt2x00dev, 4, 0x50);
3463 }
3464
Woody Hunga89534e2012-06-13 15:01:16 +08003465 if (rt2x00_rt(rt2x00dev, RT3290) ||
3466 rt2x00_rt(rt2x00dev, RT5390) ||
3467 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01003468 rt2800_bbp_read(rt2x00dev, 4, &value);
3469 rt2x00_set_field8(&value, BBP4_MAC_IF_CTRL, 1);
3470 rt2800_bbp_write(rt2x00dev, 4, value);
3471 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003472
Gabor Juhosadde5882011-03-03 11:46:45 +01003473 if (rt2800_is_305x_soc(rt2x00dev) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003474 rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003475 rt2x00_rt(rt2x00dev, RT3352) ||
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003476 rt2x00_rt(rt2x00dev, RT3572) ||
John Li2ed71882012-02-17 17:33:06 +08003477 rt2x00_rt(rt2x00dev, RT5390) ||
3478 rt2x00_rt(rt2x00dev, RT5392))
Helmut Schaabaff8002010-04-28 09:58:59 +02003479 rt2800_bbp_write(rt2x00dev, 31, 0x08);
3480
Daniel Golle03839952012-09-09 14:24:39 +03003481 if (rt2x00_rt(rt2x00dev, RT3352))
3482 rt2800_bbp_write(rt2x00dev, 47, 0x48);
3483
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003484 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
3485 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003486
Woody Hunga89534e2012-06-13 15:01:16 +08003487 if (rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003488 rt2x00_rt(rt2x00dev, RT3352) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003489 rt2x00_rt(rt2x00dev, RT5390) ||
3490 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01003491 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003492
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003493 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
3494 rt2800_bbp_write(rt2x00dev, 69, 0x16);
3495 rt2800_bbp_write(rt2x00dev, 73, 0x12);
Woody Hunga89534e2012-06-13 15:01:16 +08003496 } else if (rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003497 rt2x00_rt(rt2x00dev, RT3352) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003498 rt2x00_rt(rt2x00dev, RT5390) ||
3499 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01003500 rt2800_bbp_write(rt2x00dev, 69, 0x12);
3501 rt2800_bbp_write(rt2x00dev, 73, 0x13);
3502 rt2800_bbp_write(rt2x00dev, 75, 0x46);
3503 rt2800_bbp_write(rt2x00dev, 76, 0x28);
Woody Hunga89534e2012-06-13 15:01:16 +08003504
3505 if (rt2x00_rt(rt2x00dev, RT3290))
3506 rt2800_bbp_write(rt2x00dev, 77, 0x58);
3507 else
3508 rt2800_bbp_write(rt2x00dev, 77, 0x59);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003509 } else {
3510 rt2800_bbp_write(rt2x00dev, 69, 0x12);
3511 rt2800_bbp_write(rt2x00dev, 73, 0x10);
3512 }
3513
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003514 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003515
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003516 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003517 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003518 rt2x00_rt(rt2x00dev, RT3090) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01003519 rt2x00_rt(rt2x00dev, RT3390) ||
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003520 rt2x00_rt(rt2x00dev, RT3572) ||
John Li2ed71882012-02-17 17:33:06 +08003521 rt2x00_rt(rt2x00dev, RT5390) ||
3522 rt2x00_rt(rt2x00dev, RT5392)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003523 rt2800_bbp_write(rt2x00dev, 79, 0x13);
3524 rt2800_bbp_write(rt2x00dev, 80, 0x05);
3525 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Helmut Schaabaff8002010-04-28 09:58:59 +02003526 } else if (rt2800_is_305x_soc(rt2x00dev)) {
3527 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
3528 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Daniel Golle03839952012-09-09 14:24:39 +03003529 } else if (rt2x00_rt(rt2x00dev, RT3352)) {
3530 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
3531 rt2800_bbp_write(rt2x00dev, 80, 0x08);
3532 rt2800_bbp_write(rt2x00dev, 81, 0x37);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003533 } else {
3534 rt2800_bbp_write(rt2x00dev, 81, 0x37);
3535 }
3536
Woody Hunga89534e2012-06-13 15:01:16 +08003537 if (rt2x00_rt(rt2x00dev, RT3290)) {
3538 rt2800_bbp_write(rt2x00dev, 74, 0x0b);
3539 rt2800_bbp_write(rt2x00dev, 79, 0x18);
3540 rt2800_bbp_write(rt2x00dev, 80, 0x09);
3541 rt2800_bbp_write(rt2x00dev, 81, 0x33);
3542 }
3543
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003544 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Woody Hunga89534e2012-06-13 15:01:16 +08003545 if (rt2x00_rt(rt2x00dev, RT3290) ||
3546 rt2x00_rt(rt2x00dev, RT5390) ||
3547 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01003548 rt2800_bbp_write(rt2x00dev, 83, 0x7a);
3549 else
3550 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003551
Gertjan van Wingerde5ed8f452010-06-03 10:51:57 +02003552 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003553 rt2800_bbp_write(rt2x00dev, 84, 0x19);
Woody Hunga89534e2012-06-13 15:01:16 +08003554 else if (rt2x00_rt(rt2x00dev, RT3290) ||
3555 rt2x00_rt(rt2x00dev, RT5390) ||
3556 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01003557 rt2800_bbp_write(rt2x00dev, 84, 0x9a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003558 else
3559 rt2800_bbp_write(rt2x00dev, 84, 0x99);
3560
Woody Hunga89534e2012-06-13 15:01:16 +08003561 if (rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003562 rt2x00_rt(rt2x00dev, RT3352) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003563 rt2x00_rt(rt2x00dev, RT5390) ||
3564 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01003565 rt2800_bbp_write(rt2x00dev, 86, 0x38);
3566 else
3567 rt2800_bbp_write(rt2x00dev, 86, 0x00);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003568
Daniel Golle03839952012-09-09 14:24:39 +03003569 if (rt2x00_rt(rt2x00dev, RT3352) ||
3570 rt2x00_rt(rt2x00dev, RT5392))
John Li2ed71882012-02-17 17:33:06 +08003571 rt2800_bbp_write(rt2x00dev, 88, 0x90);
3572
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003573 rt2800_bbp_write(rt2x00dev, 91, 0x04);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003574
Woody Hunga89534e2012-06-13 15:01:16 +08003575 if (rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003576 rt2x00_rt(rt2x00dev, RT3352) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003577 rt2x00_rt(rt2x00dev, RT5390) ||
3578 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01003579 rt2800_bbp_write(rt2x00dev, 92, 0x02);
3580 else
3581 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003582
John Li2ed71882012-02-17 17:33:06 +08003583 if (rt2x00_rt(rt2x00dev, RT5392)) {
3584 rt2800_bbp_write(rt2x00dev, 95, 0x9a);
3585 rt2800_bbp_write(rt2x00dev, 98, 0x12);
3586 }
3587
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003588 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003589 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003590 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02003591 rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003592 rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003593 rt2x00_rt(rt2x00dev, RT3352) ||
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003594 rt2x00_rt(rt2x00dev, RT3572) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01003595 rt2x00_rt(rt2x00dev, RT5390) ||
John Li2ed71882012-02-17 17:33:06 +08003596 rt2x00_rt(rt2x00dev, RT5392) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02003597 rt2800_is_305x_soc(rt2x00dev))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003598 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
3599 else
3600 rt2800_bbp_write(rt2x00dev, 103, 0x00);
3601
Woody Hunga89534e2012-06-13 15:01:16 +08003602 if (rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003603 rt2x00_rt(rt2x00dev, RT3352) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003604 rt2x00_rt(rt2x00dev, RT5390) ||
3605 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01003606 rt2800_bbp_write(rt2x00dev, 104, 0x92);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003607
Helmut Schaabaff8002010-04-28 09:58:59 +02003608 if (rt2800_is_305x_soc(rt2x00dev))
3609 rt2800_bbp_write(rt2x00dev, 105, 0x01);
Woody Hunga89534e2012-06-13 15:01:16 +08003610 else if (rt2x00_rt(rt2x00dev, RT3290))
3611 rt2800_bbp_write(rt2x00dev, 105, 0x1c);
Daniel Golle03839952012-09-09 14:24:39 +03003612 else if (rt2x00_rt(rt2x00dev, RT3352))
3613 rt2800_bbp_write(rt2x00dev, 105, 0x34);
John Li2ed71882012-02-17 17:33:06 +08003614 else if (rt2x00_rt(rt2x00dev, RT5390) ||
3615 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01003616 rt2800_bbp_write(rt2x00dev, 105, 0x3c);
Helmut Schaabaff8002010-04-28 09:58:59 +02003617 else
3618 rt2800_bbp_write(rt2x00dev, 105, 0x05);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003619
Woody Hunga89534e2012-06-13 15:01:16 +08003620 if (rt2x00_rt(rt2x00dev, RT3290) ||
3621 rt2x00_rt(rt2x00dev, RT5390))
Gabor Juhosadde5882011-03-03 11:46:45 +01003622 rt2800_bbp_write(rt2x00dev, 106, 0x03);
Daniel Golle03839952012-09-09 14:24:39 +03003623 else if (rt2x00_rt(rt2x00dev, RT3352))
3624 rt2800_bbp_write(rt2x00dev, 106, 0x05);
John Li2ed71882012-02-17 17:33:06 +08003625 else if (rt2x00_rt(rt2x00dev, RT5392))
3626 rt2800_bbp_write(rt2x00dev, 106, 0x12);
Gabor Juhosadde5882011-03-03 11:46:45 +01003627 else
3628 rt2800_bbp_write(rt2x00dev, 106, 0x35);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003629
Daniel Golle03839952012-09-09 14:24:39 +03003630 if (rt2x00_rt(rt2x00dev, RT3352))
3631 rt2800_bbp_write(rt2x00dev, 120, 0x50);
3632
Woody Hunga89534e2012-06-13 15:01:16 +08003633 if (rt2x00_rt(rt2x00dev, RT3290) ||
3634 rt2x00_rt(rt2x00dev, RT5390) ||
3635 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01003636 rt2800_bbp_write(rt2x00dev, 128, 0x12);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003637
John Li2ed71882012-02-17 17:33:06 +08003638 if (rt2x00_rt(rt2x00dev, RT5392)) {
3639 rt2800_bbp_write(rt2x00dev, 134, 0xd0);
3640 rt2800_bbp_write(rt2x00dev, 135, 0xf6);
3641 }
3642
Daniel Golle03839952012-09-09 14:24:39 +03003643 if (rt2x00_rt(rt2x00dev, RT3352))
3644 rt2800_bbp_write(rt2x00dev, 137, 0x0f);
3645
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003646 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003647 rt2x00_rt(rt2x00dev, RT3090) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01003648 rt2x00_rt(rt2x00dev, RT3390) ||
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003649 rt2x00_rt(rt2x00dev, RT3572) ||
John Li2ed71882012-02-17 17:33:06 +08003650 rt2x00_rt(rt2x00dev, RT5390) ||
3651 rt2x00_rt(rt2x00dev, RT5392)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003652 rt2800_bbp_read(rt2x00dev, 138, &value);
3653
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003654 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
3655 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003656 value |= 0x20;
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003657 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003658 value &= ~0x02;
3659
3660 rt2800_bbp_write(rt2x00dev, 138, value);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003661 }
3662
Woody Hunga89534e2012-06-13 15:01:16 +08003663 if (rt2x00_rt(rt2x00dev, RT3290)) {
3664 rt2800_bbp_write(rt2x00dev, 67, 0x24);
3665 rt2800_bbp_write(rt2x00dev, 143, 0x04);
3666 rt2800_bbp_write(rt2x00dev, 142, 0x99);
3667 rt2800_bbp_write(rt2x00dev, 150, 0x30);
3668 rt2800_bbp_write(rt2x00dev, 151, 0x2e);
3669 rt2800_bbp_write(rt2x00dev, 152, 0x20);
3670 rt2800_bbp_write(rt2x00dev, 153, 0x34);
3671 rt2800_bbp_write(rt2x00dev, 154, 0x40);
3672 rt2800_bbp_write(rt2x00dev, 155, 0x3b);
3673 rt2800_bbp_write(rt2x00dev, 253, 0x04);
3674
3675 rt2800_bbp_read(rt2x00dev, 47, &value);
3676 rt2x00_set_field8(&value, BBP47_TSSI_ADC6, 1);
3677 rt2800_bbp_write(rt2x00dev, 47, value);
3678
3679 /* Use 5-bit ADC for Acquisition and 8-bit ADC for data */
3680 rt2800_bbp_read(rt2x00dev, 3, &value);
3681 rt2x00_set_field8(&value, BBP3_ADC_MODE_SWITCH, 1);
3682 rt2x00_set_field8(&value, BBP3_ADC_INIT_MODE, 1);
3683 rt2800_bbp_write(rt2x00dev, 3, value);
3684 }
3685
Daniel Golle03839952012-09-09 14:24:39 +03003686 if (rt2x00_rt(rt2x00dev, RT3352)) {
3687 rt2800_bbp_write(rt2x00dev, 163, 0xbd);
3688 /* Set ITxBF timeout to 0x9c40=1000msec */
3689 rt2800_bbp_write(rt2x00dev, 179, 0x02);
3690 rt2800_bbp_write(rt2x00dev, 180, 0x00);
3691 rt2800_bbp_write(rt2x00dev, 182, 0x40);
3692 rt2800_bbp_write(rt2x00dev, 180, 0x01);
3693 rt2800_bbp_write(rt2x00dev, 182, 0x9c);
3694 rt2800_bbp_write(rt2x00dev, 179, 0x00);
3695 /* Reprogram the inband interface to put right values in RXWI */
3696 rt2800_bbp_write(rt2x00dev, 142, 0x04);
3697 rt2800_bbp_write(rt2x00dev, 143, 0x3b);
3698 rt2800_bbp_write(rt2x00dev, 142, 0x06);
3699 rt2800_bbp_write(rt2x00dev, 143, 0xa0);
3700 rt2800_bbp_write(rt2x00dev, 142, 0x07);
3701 rt2800_bbp_write(rt2x00dev, 143, 0xa1);
3702 rt2800_bbp_write(rt2x00dev, 142, 0x08);
3703 rt2800_bbp_write(rt2x00dev, 143, 0xa2);
3704
3705 rt2800_bbp_write(rt2x00dev, 148, 0xc8);
3706 }
3707
John Li2ed71882012-02-17 17:33:06 +08003708 if (rt2x00_rt(rt2x00dev, RT5390) ||
3709 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01003710 int ant, div_mode;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003711
Gabor Juhosadde5882011-03-03 11:46:45 +01003712 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
3713 div_mode = rt2x00_get_field16(eeprom,
3714 EEPROM_NIC_CONF1_ANT_DIVERSITY);
3715 ant = (div_mode == 3) ? 1 : 0;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003716
Gabor Juhosadde5882011-03-03 11:46:45 +01003717 /* check if this is a Bluetooth combo card */
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02003718 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01003719 u32 reg;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003720
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02003721 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
3722 rt2x00_set_field32(&reg, GPIO_CTRL_DIR3, 0);
3723 rt2x00_set_field32(&reg, GPIO_CTRL_DIR6, 0);
3724 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, 0);
3725 rt2x00_set_field32(&reg, GPIO_CTRL_VAL6, 0);
Gabor Juhosadde5882011-03-03 11:46:45 +01003726 if (ant == 0)
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02003727 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, 1);
Gabor Juhosadde5882011-03-03 11:46:45 +01003728 else if (ant == 1)
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02003729 rt2x00_set_field32(&reg, GPIO_CTRL_VAL6, 1);
3730 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
Gabor Juhosadde5882011-03-03 11:46:45 +01003731 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003732
Anisse Astier0586a112012-04-23 12:33:11 +02003733 /* This chip has hardware antenna diversity*/
3734 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390R)) {
3735 rt2800_bbp_write(rt2x00dev, 150, 0); /* Disable Antenna Software OFDM */
3736 rt2800_bbp_write(rt2x00dev, 151, 0); /* Disable Antenna Software CCK */
3737 rt2800_bbp_write(rt2x00dev, 154, 0); /* Clear previously selected antenna */
3738 }
3739
Gabor Juhosadde5882011-03-03 11:46:45 +01003740 rt2800_bbp_read(rt2x00dev, 152, &value);
3741 if (ant == 0)
3742 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
3743 else
3744 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
3745 rt2800_bbp_write(rt2x00dev, 152, value);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003746
Gabor Juhosadde5882011-03-03 11:46:45 +01003747 /* Init frequency calibration */
3748 rt2800_bbp_write(rt2x00dev, 142, 1);
3749 rt2800_bbp_write(rt2x00dev, 143, 57);
3750 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003751
3752 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
3753 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
3754
3755 if (eeprom != 0xffff && eeprom != 0x0000) {
3756 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
3757 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
3758 rt2800_bbp_write(rt2x00dev, reg_id, value);
3759 }
3760 }
3761
3762 return 0;
3763}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003764
3765static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
3766 bool bw40, u8 rfcsr24, u8 filter_target)
3767{
3768 unsigned int i;
3769 u8 bbp;
3770 u8 rfcsr;
3771 u8 passband;
3772 u8 stopband;
3773 u8 overtuned = 0;
3774
3775 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
3776
3777 rt2800_bbp_read(rt2x00dev, 4, &bbp);
3778 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
3779 rt2800_bbp_write(rt2x00dev, 4, bbp);
3780
RA-Jay Hung80d184e2011-01-10 11:28:10 +01003781 rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
3782 rt2x00_set_field8(&rfcsr, RFCSR31_RX_H20M, bw40);
3783 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
3784
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003785 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
3786 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
3787 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
3788
3789 /*
3790 * Set power & frequency of passband test tone
3791 */
3792 rt2800_bbp_write(rt2x00dev, 24, 0);
3793
3794 for (i = 0; i < 100; i++) {
3795 rt2800_bbp_write(rt2x00dev, 25, 0x90);
3796 msleep(1);
3797
3798 rt2800_bbp_read(rt2x00dev, 55, &passband);
3799 if (passband)
3800 break;
3801 }
3802
3803 /*
3804 * Set power & frequency of stopband test tone
3805 */
3806 rt2800_bbp_write(rt2x00dev, 24, 0x06);
3807
3808 for (i = 0; i < 100; i++) {
3809 rt2800_bbp_write(rt2x00dev, 25, 0x90);
3810 msleep(1);
3811
3812 rt2800_bbp_read(rt2x00dev, 55, &stopband);
3813
3814 if ((passband - stopband) <= filter_target) {
3815 rfcsr24++;
3816 overtuned += ((passband - stopband) == filter_target);
3817 } else
3818 break;
3819
3820 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
3821 }
3822
3823 rfcsr24 -= !!overtuned;
3824
3825 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
3826 return rfcsr24;
3827}
3828
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003829static int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003830{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01003831 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003832 u8 rfcsr;
3833 u8 bbp;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003834 u32 reg;
3835 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003836
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003837 if (!rt2x00_rt(rt2x00dev, RT3070) &&
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003838 !rt2x00_rt(rt2x00dev, RT3071) &&
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003839 !rt2x00_rt(rt2x00dev, RT3090) &&
Woody Hunga89534e2012-06-13 15:01:16 +08003840 !rt2x00_rt(rt2x00dev, RT3290) &&
Daniel Golle03839952012-09-09 14:24:39 +03003841 !rt2x00_rt(rt2x00dev, RT3352) &&
Helmut Schaa23812382010-04-26 13:48:45 +02003842 !rt2x00_rt(rt2x00dev, RT3390) &&
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003843 !rt2x00_rt(rt2x00dev, RT3572) &&
Gabor Juhosadde5882011-03-03 11:46:45 +01003844 !rt2x00_rt(rt2x00dev, RT5390) &&
John Li2ed71882012-02-17 17:33:06 +08003845 !rt2x00_rt(rt2x00dev, RT5392) &&
Helmut Schaabaff8002010-04-28 09:58:59 +02003846 !rt2800_is_305x_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003847 return 0;
3848
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003849 /*
3850 * Init RF calibration.
3851 */
Woody Hunga89534e2012-06-13 15:01:16 +08003852 if (rt2x00_rt(rt2x00dev, RT3290) ||
3853 rt2x00_rt(rt2x00dev, RT5390) ||
3854 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01003855 rt2800_rfcsr_read(rt2x00dev, 2, &rfcsr);
3856 rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 1);
3857 rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
3858 msleep(1);
3859 rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 0);
3860 rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
3861 } else {
3862 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
3863 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
3864 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
3865 msleep(1);
3866 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
3867 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
3868 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003869
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003870 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003871 rt2x00_rt(rt2x00dev, RT3071) ||
3872 rt2x00_rt(rt2x00dev, RT3090)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003873 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
3874 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
3875 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
RA-Jay Hung80d184e2011-01-10 11:28:10 +01003876 rt2800_rfcsr_write(rt2x00dev, 7, 0x60);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003877 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003878 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003879 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
3880 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
3881 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
3882 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
3883 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
3884 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
3885 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
3886 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
3887 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
3888 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
3889 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
3890 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003891 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Woody Hunga89534e2012-06-13 15:01:16 +08003892 } else if (rt2x00_rt(rt2x00dev, RT3290)) {
3893 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
3894 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
3895 rt2800_rfcsr_write(rt2x00dev, 3, 0x08);
3896 rt2800_rfcsr_write(rt2x00dev, 4, 0x00);
3897 rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
3898 rt2800_rfcsr_write(rt2x00dev, 8, 0xf3);
3899 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
3900 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
3901 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
3902 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
3903 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
3904 rt2800_rfcsr_write(rt2x00dev, 18, 0x02);
3905 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
3906 rt2800_rfcsr_write(rt2x00dev, 25, 0x83);
3907 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
3908 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
3909 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
3910 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
3911 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
3912 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
3913 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
3914 rt2800_rfcsr_write(rt2x00dev, 34, 0x05);
3915 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
3916 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
3917 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
3918 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
3919 rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
3920 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
3921 rt2800_rfcsr_write(rt2x00dev, 42, 0xd5);
3922 rt2800_rfcsr_write(rt2x00dev, 43, 0x7b);
3923 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
3924 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
3925 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
3926 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
3927 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
3928 rt2800_rfcsr_write(rt2x00dev, 49, 0x98);
3929 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
3930 rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
3931 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
3932 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
3933 rt2800_rfcsr_write(rt2x00dev, 56, 0x02);
3934 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
3935 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
3936 rt2800_rfcsr_write(rt2x00dev, 59, 0x09);
3937 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
3938 rt2800_rfcsr_write(rt2x00dev, 61, 0xc1);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003939 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
3940 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
3941 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
3942 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
3943 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003944 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003945 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
3946 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
3947 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
3948 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
3949 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
3950 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003951 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003952 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
3953 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003954 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003955 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
3956 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
3957 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
3958 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
3959 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
3960 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
3961 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003962 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003963 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003964 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003965 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
3966 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
3967 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
3968 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
3969 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
3970 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
3971 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003972 } else if (rt2x00_rt(rt2x00dev, RT3572)) {
3973 rt2800_rfcsr_write(rt2x00dev, 0, 0x70);
3974 rt2800_rfcsr_write(rt2x00dev, 1, 0x81);
3975 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
3976 rt2800_rfcsr_write(rt2x00dev, 3, 0x02);
3977 rt2800_rfcsr_write(rt2x00dev, 4, 0x4c);
3978 rt2800_rfcsr_write(rt2x00dev, 5, 0x05);
3979 rt2800_rfcsr_write(rt2x00dev, 6, 0x4a);
3980 rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
3981 rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
3982 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
3983 rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
3984 rt2800_rfcsr_write(rt2x00dev, 12, 0x70);
3985 rt2800_rfcsr_write(rt2x00dev, 13, 0x65);
3986 rt2800_rfcsr_write(rt2x00dev, 14, 0xa0);
3987 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
3988 rt2800_rfcsr_write(rt2x00dev, 16, 0x4c);
3989 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
3990 rt2800_rfcsr_write(rt2x00dev, 18, 0xac);
3991 rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
3992 rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
3993 rt2800_rfcsr_write(rt2x00dev, 21, 0xd0);
3994 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
3995 rt2800_rfcsr_write(rt2x00dev, 23, 0x3c);
3996 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
3997 rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
3998 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
3999 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
4000 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
4001 rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
4002 rt2800_rfcsr_write(rt2x00dev, 30, 0x09);
4003 rt2800_rfcsr_write(rt2x00dev, 31, 0x10);
Helmut Schaabaff8002010-04-28 09:58:59 +02004004 } else if (rt2800_is_305x_soc(rt2x00dev)) {
Helmut Schaa23812382010-04-26 13:48:45 +02004005 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
4006 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
4007 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
4008 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
4009 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
4010 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
4011 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
4012 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
4013 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
4014 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
4015 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
4016 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
4017 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
4018 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
4019 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
4020 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
4021 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
4022 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
4023 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
4024 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
4025 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
4026 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
4027 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
4028 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
4029 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
4030 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
4031 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
4032 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
4033 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
4034 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
Helmut Schaabaff8002010-04-28 09:58:59 +02004035 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
4036 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
4037 return 0;
Daniel Golle03839952012-09-09 14:24:39 +03004038 } else if (rt2x00_rt(rt2x00dev, RT3352)) {
4039 rt2800_rfcsr_write(rt2x00dev, 0, 0xf0);
4040 rt2800_rfcsr_write(rt2x00dev, 1, 0x23);
4041 rt2800_rfcsr_write(rt2x00dev, 2, 0x50);
4042 rt2800_rfcsr_write(rt2x00dev, 3, 0x18);
4043 rt2800_rfcsr_write(rt2x00dev, 4, 0x00);
4044 rt2800_rfcsr_write(rt2x00dev, 5, 0x00);
4045 rt2800_rfcsr_write(rt2x00dev, 6, 0x33);
4046 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
4047 rt2800_rfcsr_write(rt2x00dev, 8, 0xf1);
4048 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
4049 rt2800_rfcsr_write(rt2x00dev, 10, 0xd2);
4050 rt2800_rfcsr_write(rt2x00dev, 11, 0x42);
4051 rt2800_rfcsr_write(rt2x00dev, 12, 0x1c);
4052 rt2800_rfcsr_write(rt2x00dev, 13, 0x00);
4053 rt2800_rfcsr_write(rt2x00dev, 14, 0x5a);
4054 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
4055 rt2800_rfcsr_write(rt2x00dev, 16, 0x01);
4056 rt2800_rfcsr_write(rt2x00dev, 18, 0x45);
4057 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
4058 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
4059 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
4060 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
4061 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
4062 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
4063 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
4064 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
4065 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
4066 rt2800_rfcsr_write(rt2x00dev, 28, 0x03);
4067 rt2800_rfcsr_write(rt2x00dev, 29, 0x00);
4068 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
4069 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
4070 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
4071 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
4072 rt2800_rfcsr_write(rt2x00dev, 34, 0x01);
4073 rt2800_rfcsr_write(rt2x00dev, 35, 0x03);
4074 rt2800_rfcsr_write(rt2x00dev, 36, 0xbd);
4075 rt2800_rfcsr_write(rt2x00dev, 37, 0x3c);
4076 rt2800_rfcsr_write(rt2x00dev, 38, 0x5f);
4077 rt2800_rfcsr_write(rt2x00dev, 39, 0xc5);
4078 rt2800_rfcsr_write(rt2x00dev, 40, 0x33);
4079 rt2800_rfcsr_write(rt2x00dev, 41, 0x5b);
4080 rt2800_rfcsr_write(rt2x00dev, 42, 0x5b);
4081 rt2800_rfcsr_write(rt2x00dev, 43, 0xdb);
4082 rt2800_rfcsr_write(rt2x00dev, 44, 0xdb);
4083 rt2800_rfcsr_write(rt2x00dev, 45, 0xdb);
4084 rt2800_rfcsr_write(rt2x00dev, 46, 0xdd);
4085 rt2800_rfcsr_write(rt2x00dev, 47, 0x0d);
4086 rt2800_rfcsr_write(rt2x00dev, 48, 0x14);
4087 rt2800_rfcsr_write(rt2x00dev, 49, 0x00);
4088 rt2800_rfcsr_write(rt2x00dev, 50, 0x2d);
4089 rt2800_rfcsr_write(rt2x00dev, 51, 0x7f);
4090 rt2800_rfcsr_write(rt2x00dev, 52, 0x00);
4091 rt2800_rfcsr_write(rt2x00dev, 53, 0x52);
4092 rt2800_rfcsr_write(rt2x00dev, 54, 0x1b);
4093 rt2800_rfcsr_write(rt2x00dev, 55, 0x7f);
4094 rt2800_rfcsr_write(rt2x00dev, 56, 0x00);
4095 rt2800_rfcsr_write(rt2x00dev, 57, 0x52);
4096 rt2800_rfcsr_write(rt2x00dev, 58, 0x1b);
4097 rt2800_rfcsr_write(rt2x00dev, 59, 0x00);
4098 rt2800_rfcsr_write(rt2x00dev, 60, 0x00);
4099 rt2800_rfcsr_write(rt2x00dev, 61, 0x00);
4100 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
4101 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
Gabor Juhosadde5882011-03-03 11:46:45 +01004102 } else if (rt2x00_rt(rt2x00dev, RT5390)) {
4103 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
4104 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
4105 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
4106 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
4107 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4108 rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
4109 else
4110 rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
4111 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
4112 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
4113 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
4114 rt2800_rfcsr_write(rt2x00dev, 12, 0xc6);
4115 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
4116 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
4117 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
4118 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
4119 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
4120 rt2800_rfcsr_write(rt2x00dev, 19, 0x00);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004121
Gabor Juhosadde5882011-03-03 11:46:45 +01004122 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
4123 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
4124 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
4125 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
4126 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
4127 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4128 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
4129 else
4130 rt2800_rfcsr_write(rt2x00dev, 25, 0xc0);
4131 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
4132 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
4133 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
4134 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004135
Gabor Juhosadde5882011-03-03 11:46:45 +01004136 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
4137 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
4138 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
4139 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
4140 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
4141 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
4142 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
4143 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
4144 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
4145 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004146
Gabor Juhosadde5882011-03-03 11:46:45 +01004147 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4148 rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
4149 else
4150 rt2800_rfcsr_write(rt2x00dev, 40, 0x4b);
4151 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
4152 rt2800_rfcsr_write(rt2x00dev, 42, 0xd2);
4153 rt2800_rfcsr_write(rt2x00dev, 43, 0x9a);
4154 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
4155 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
4156 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4157 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
4158 else
4159 rt2800_rfcsr_write(rt2x00dev, 46, 0x7b);
4160 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
4161 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
4162 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004163
Gabor Juhosadde5882011-03-03 11:46:45 +01004164 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
4165 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4166 rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
4167 else
4168 rt2800_rfcsr_write(rt2x00dev, 53, 0x84);
4169 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
4170 rt2800_rfcsr_write(rt2x00dev, 55, 0x44);
4171 rt2800_rfcsr_write(rt2x00dev, 56, 0x22);
4172 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
4173 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
4174 rt2800_rfcsr_write(rt2x00dev, 59, 0x63);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004175
Gabor Juhosadde5882011-03-03 11:46:45 +01004176 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
4177 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4178 rt2800_rfcsr_write(rt2x00dev, 61, 0xd1);
4179 else
4180 rt2800_rfcsr_write(rt2x00dev, 61, 0xdd);
4181 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
4182 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
John Li2ed71882012-02-17 17:33:06 +08004183 } else if (rt2x00_rt(rt2x00dev, RT5392)) {
4184 rt2800_rfcsr_write(rt2x00dev, 1, 0x17);
4185 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
4186 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
4187 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
4188 rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
4189 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
4190 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
4191 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
4192 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
4193 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
4194 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
4195 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
4196 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
4197 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
4198 rt2800_rfcsr_write(rt2x00dev, 19, 0x4d);
4199 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
4200 rt2800_rfcsr_write(rt2x00dev, 21, 0x8d);
4201 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
4202 rt2800_rfcsr_write(rt2x00dev, 23, 0x0b);
4203 rt2800_rfcsr_write(rt2x00dev, 24, 0x44);
4204 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
4205 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
4206 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
4207 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
4208 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
4209 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
4210 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
4211 rt2800_rfcsr_write(rt2x00dev, 32, 0x20);
4212 rt2800_rfcsr_write(rt2x00dev, 33, 0xC0);
4213 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
4214 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
4215 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
4216 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
4217 rt2800_rfcsr_write(rt2x00dev, 38, 0x89);
4218 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
4219 rt2800_rfcsr_write(rt2x00dev, 40, 0x0f);
4220 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
4221 rt2800_rfcsr_write(rt2x00dev, 42, 0xd5);
4222 rt2800_rfcsr_write(rt2x00dev, 43, 0x9b);
4223 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
4224 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
4225 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
4226 rt2800_rfcsr_write(rt2x00dev, 47, 0x0c);
4227 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
4228 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
4229 rt2800_rfcsr_write(rt2x00dev, 50, 0x94);
4230 rt2800_rfcsr_write(rt2x00dev, 51, 0x3a);
4231 rt2800_rfcsr_write(rt2x00dev, 52, 0x48);
4232 rt2800_rfcsr_write(rt2x00dev, 53, 0x44);
4233 rt2800_rfcsr_write(rt2x00dev, 54, 0x38);
4234 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
4235 rt2800_rfcsr_write(rt2x00dev, 56, 0xa1);
4236 rt2800_rfcsr_write(rt2x00dev, 57, 0x00);
4237 rt2800_rfcsr_write(rt2x00dev, 58, 0x39);
4238 rt2800_rfcsr_write(rt2x00dev, 59, 0x07);
4239 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
4240 rt2800_rfcsr_write(rt2x00dev, 61, 0x91);
4241 rt2800_rfcsr_write(rt2x00dev, 62, 0x39);
4242 rt2800_rfcsr_write(rt2x00dev, 63, 0x07);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004243 }
4244
4245 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
4246 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
4247 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
4248 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
4249 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004250 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
4251 rt2x00_rt(rt2x00dev, RT3090)) {
RA-Jay Hung80d184e2011-01-10 11:28:10 +01004252 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
4253
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004254 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
4255 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
4256 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
4257
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004258 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
4259 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004260 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
4261 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004262 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
4263 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004264 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
4265 else
4266 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
4267 }
4268 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
RA-Jay Hung80d184e2011-01-10 11:28:10 +01004269
4270 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
4271 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
4272 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004273 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
4274 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
4275 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
4276 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004277 } else if (rt2x00_rt(rt2x00dev, RT3572)) {
4278 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
4279 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
4280 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
4281
4282 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
4283 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
4284 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
4285 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
4286 msleep(1);
4287 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
Marc Kleine-Budded0f21fe2012-08-27 00:26:37 +02004288 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004289 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
4290 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004291 }
4292
4293 /*
4294 * Set RX Filter calibration for 20MHz and 40MHz
4295 */
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004296 if (rt2x00_rt(rt2x00dev, RT3070)) {
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01004297 drv_data->calibration_bw20 =
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004298 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01004299 drv_data->calibration_bw40 =
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004300 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004301 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004302 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03004303 rt2x00_rt(rt2x00dev, RT3352) ||
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004304 rt2x00_rt(rt2x00dev, RT3390) ||
4305 rt2x00_rt(rt2x00dev, RT3572)) {
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01004306 drv_data->calibration_bw20 =
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004307 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01004308 drv_data->calibration_bw40 =
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004309 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004310 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004311
Gertjan van Wingerde5d137df2012-02-06 23:45:09 +01004312 /*
4313 * Save BBP 25 & 26 values for later use in channel switching
4314 */
4315 rt2800_bbp_read(rt2x00dev, 25, &drv_data->bbp25);
4316 rt2800_bbp_read(rt2x00dev, 26, &drv_data->bbp26);
4317
John Li2ed71882012-02-17 17:33:06 +08004318 if (!rt2x00_rt(rt2x00dev, RT5390) &&
4319 !rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01004320 /*
4321 * Set back to initial state
4322 */
4323 rt2800_bbp_write(rt2x00dev, 24, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004324
Gabor Juhosadde5882011-03-03 11:46:45 +01004325 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
4326 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
4327 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004328
Gabor Juhosadde5882011-03-03 11:46:45 +01004329 /*
4330 * Set BBP back to BW20
4331 */
4332 rt2800_bbp_read(rt2x00dev, 4, &bbp);
4333 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
4334 rt2800_bbp_write(rt2x00dev, 4, bbp);
4335 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004336
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004337 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004338 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004339 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
4340 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004341 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
4342
4343 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
4344 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
4345 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
4346
John Li2ed71882012-02-17 17:33:06 +08004347 if (!rt2x00_rt(rt2x00dev, RT5390) &&
4348 !rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01004349 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
4350 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
4351 if (rt2x00_rt(rt2x00dev, RT3070) ||
4352 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
4353 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
4354 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02004355 if (!test_bit(CAPABILITY_EXTERNAL_LNA_BG,
4356 &rt2x00dev->cap_flags))
Gabor Juhosadde5882011-03-03 11:46:45 +01004357 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
4358 }
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01004359 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
4360 drv_data->txmixer_gain_24g);
Gabor Juhosadde5882011-03-03 11:46:45 +01004361 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
4362 }
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004363
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004364 if (rt2x00_rt(rt2x00dev, RT3090)) {
4365 rt2800_bbp_read(rt2x00dev, 138, &bbp);
4366
RA-Jay Hung80d184e2011-01-10 11:28:10 +01004367 /* Turn off unused DAC1 and ADC1 to reduce power consumption */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004368 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
4369 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004370 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004371 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004372 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
4373
4374 rt2800_bbp_write(rt2x00dev, 138, bbp);
4375 }
4376
4377 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004378 rt2x00_rt(rt2x00dev, RT3090) ||
4379 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004380 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
4381 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
4382 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
4383 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
4384 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
4385 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
4386 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
4387
4388 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
4389 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
4390 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
4391
4392 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
4393 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
4394 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
4395
4396 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
4397 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
4398 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
4399 }
4400
RA-Jay Hung80d184e2011-01-10 11:28:10 +01004401 if (rt2x00_rt(rt2x00dev, RT3070)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004402 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
RA-Jay Hung80d184e2011-01-10 11:28:10 +01004403 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004404 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
4405 else
4406 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
4407 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
4408 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
4409 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
4410 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
4411 }
4412
Woody Hunga89534e2012-06-13 15:01:16 +08004413 if (rt2x00_rt(rt2x00dev, RT3290)) {
4414 rt2800_rfcsr_read(rt2x00dev, 29, &rfcsr);
4415 rt2x00_set_field8(&rfcsr, RFCSR29_RSSI_GAIN, 3);
4416 rt2800_rfcsr_write(rt2x00dev, 29, rfcsr);
4417 }
4418
John Li2ed71882012-02-17 17:33:06 +08004419 if (rt2x00_rt(rt2x00dev, RT5390) ||
4420 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01004421 rt2800_rfcsr_read(rt2x00dev, 38, &rfcsr);
4422 rt2x00_set_field8(&rfcsr, RFCSR38_RX_LO1_EN, 0);
4423 rt2800_rfcsr_write(rt2x00dev, 38, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004424
Gabor Juhosadde5882011-03-03 11:46:45 +01004425 rt2800_rfcsr_read(rt2x00dev, 39, &rfcsr);
4426 rt2x00_set_field8(&rfcsr, RFCSR39_RX_LO2_EN, 0);
4427 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004428
Gabor Juhosadde5882011-03-03 11:46:45 +01004429 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
4430 rt2x00_set_field8(&rfcsr, RFCSR30_RX_VCM, 2);
4431 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
4432 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004433
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004434 return 0;
4435}
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02004436
4437int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
4438{
4439 u32 reg;
4440 u16 word;
4441
4442 /*
4443 * Initialize all registers.
4444 */
4445 if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
4446 rt2800_init_registers(rt2x00dev) ||
4447 rt2800_init_bbp(rt2x00dev) ||
4448 rt2800_init_rfcsr(rt2x00dev)))
4449 return -EIO;
4450
4451 /*
4452 * Send signal to firmware during boot time.
4453 */
4454 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
4455
4456 if (rt2x00_is_usb(rt2x00dev) &&
4457 (rt2x00_rt(rt2x00dev, RT3070) ||
4458 rt2x00_rt(rt2x00dev, RT3071) ||
4459 rt2x00_rt(rt2x00dev, RT3572))) {
4460 udelay(200);
4461 rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
4462 udelay(10);
4463 }
4464
4465 /*
4466 * Enable RX.
4467 */
4468 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
4469 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
4470 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
4471 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
4472
4473 udelay(50);
4474
4475 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
4476 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
4477 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
4478 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
4479 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
4480 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
4481
4482 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
4483 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
4484 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
4485 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
4486
4487 /*
4488 * Initialize LED control
4489 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004490 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_AG_CONF, &word);
4491 rt2800_mcu_request(rt2x00dev, MCU_LED_AG_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02004492 word & 0xff, (word >> 8) & 0xff);
4493
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004494 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_ACT_CONF, &word);
4495 rt2800_mcu_request(rt2x00dev, MCU_LED_ACT_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02004496 word & 0xff, (word >> 8) & 0xff);
4497
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004498 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_POLARITY, &word);
4499 rt2800_mcu_request(rt2x00dev, MCU_LED_LED_POLARITY, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02004500 word & 0xff, (word >> 8) & 0xff);
4501
4502 return 0;
4503}
4504EXPORT_SYMBOL_GPL(rt2800_enable_radio);
4505
4506void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
4507{
4508 u32 reg;
4509
Jakub Kicinskif7b395e2012-04-03 03:40:47 +02004510 rt2800_disable_wpdma(rt2x00dev);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02004511
4512 /* Wait for DMA, ignore error */
4513 rt2800_wait_wpdma_ready(rt2x00dev);
4514
4515 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
4516 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
4517 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
4518 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02004519}
4520EXPORT_SYMBOL_GPL(rt2800_disable_radio);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004521
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01004522int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
4523{
4524 u32 reg;
Woody Hunga89534e2012-06-13 15:01:16 +08004525 u16 efuse_ctrl_reg;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01004526
Woody Hunga89534e2012-06-13 15:01:16 +08004527 if (rt2x00_rt(rt2x00dev, RT3290))
4528 efuse_ctrl_reg = EFUSE_CTRL_3290;
4529 else
4530 efuse_ctrl_reg = EFUSE_CTRL;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01004531
Woody Hunga89534e2012-06-13 15:01:16 +08004532 rt2800_register_read(rt2x00dev, efuse_ctrl_reg, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01004533 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
4534}
4535EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
4536
4537static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
4538{
4539 u32 reg;
Woody Hunga89534e2012-06-13 15:01:16 +08004540 u16 efuse_ctrl_reg;
4541 u16 efuse_data0_reg;
4542 u16 efuse_data1_reg;
4543 u16 efuse_data2_reg;
4544 u16 efuse_data3_reg;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01004545
Woody Hunga89534e2012-06-13 15:01:16 +08004546 if (rt2x00_rt(rt2x00dev, RT3290)) {
4547 efuse_ctrl_reg = EFUSE_CTRL_3290;
4548 efuse_data0_reg = EFUSE_DATA0_3290;
4549 efuse_data1_reg = EFUSE_DATA1_3290;
4550 efuse_data2_reg = EFUSE_DATA2_3290;
4551 efuse_data3_reg = EFUSE_DATA3_3290;
4552 } else {
4553 efuse_ctrl_reg = EFUSE_CTRL;
4554 efuse_data0_reg = EFUSE_DATA0;
4555 efuse_data1_reg = EFUSE_DATA1;
4556 efuse_data2_reg = EFUSE_DATA2;
4557 efuse_data3_reg = EFUSE_DATA3;
4558 }
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01004559 mutex_lock(&rt2x00dev->csr_mutex);
4560
Woody Hunga89534e2012-06-13 15:01:16 +08004561 rt2800_register_read_lock(rt2x00dev, efuse_ctrl_reg, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01004562 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
4563 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
4564 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Woody Hunga89534e2012-06-13 15:01:16 +08004565 rt2800_register_write_lock(rt2x00dev, efuse_ctrl_reg, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01004566
4567 /* Wait until the EEPROM has been loaded */
Woody Hunga89534e2012-06-13 15:01:16 +08004568 rt2800_regbusy_read(rt2x00dev, efuse_ctrl_reg, EFUSE_CTRL_KICK, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01004569 /* Apparently the data is read from end to start */
Woody Hunga89534e2012-06-13 15:01:16 +08004570 rt2800_register_read_lock(rt2x00dev, efuse_data3_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05004571 /* The returned value is in CPU order, but eeprom is le */
Gertjan van Wingerde68fa64e2011-11-16 23:16:15 +01004572 *(u32 *)&rt2x00dev->eeprom[i] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08004573 rt2800_register_read_lock(rt2x00dev, efuse_data2_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05004574 *(u32 *)&rt2x00dev->eeprom[i + 2] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08004575 rt2800_register_read_lock(rt2x00dev, efuse_data1_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05004576 *(u32 *)&rt2x00dev->eeprom[i + 4] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08004577 rt2800_register_read_lock(rt2x00dev, efuse_data0_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05004578 *(u32 *)&rt2x00dev->eeprom[i + 6] = cpu_to_le32(reg);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01004579
4580 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01004581}
4582
4583void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
4584{
4585 unsigned int i;
4586
4587 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
4588 rt2800_efuse_read(rt2x00dev, i);
4589}
4590EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
4591
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02004592static int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004593{
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01004594 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004595 u16 word;
4596 u8 *mac;
4597 u8 default_lna_gain;
4598
4599 /*
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02004600 * Read the EEPROM.
4601 */
4602 rt2800_read_eeprom(rt2x00dev);
4603
4604 /*
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004605 * Start validation of the data that has been read.
4606 */
4607 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
4608 if (!is_valid_ether_addr(mac)) {
Joe Perchesf4f7f4142012-07-12 19:33:08 +00004609 eth_random_addr(mac);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004610 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
4611 }
4612
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004613 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004614 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004615 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
4616 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_TXPATH, 1);
4617 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RF_TYPE, RF2820);
4618 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004619 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01004620 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02004621 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004622 /*
4623 * There is a max of 2 RX streams for RT28x0 series
4624 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004625 if (rt2x00_get_field16(word, EEPROM_NIC_CONF0_RXPATH) > 2)
4626 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
4627 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004628 }
4629
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004630 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004631 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004632 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_HW_RADIO, 0);
4633 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC, 0);
4634 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G, 0);
4635 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G, 0);
4636 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_CARDBUS_ACCEL, 0);
4637 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_2G, 0);
4638 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_5G, 0);
4639 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_WPS_PBC, 0);
4640 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_2G, 0);
4641 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_5G, 0);
4642 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BROADBAND_EXT_LNA, 0);
4643 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_ANT_DIVERSITY, 0);
4644 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_INTERNAL_TX_ALC, 0);
4645 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BT_COEXIST, 0);
4646 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_DAC_TEST, 0);
4647 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF1, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004648 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
4649 }
4650
4651 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
4652 if ((word & 0x00ff) == 0x00ff) {
4653 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02004654 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
4655 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
4656 }
4657 if ((word & 0xff00) == 0xff00) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004658 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
4659 LED_MODE_TXRX_ACTIVITY);
4660 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
4661 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004662 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_AG_CONF, 0x5555);
4663 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_ACT_CONF, 0x2221);
4664 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_POLARITY, 0xa9f8);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02004665 EEPROM(rt2x00dev, "Led Mode: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004666 }
4667
4668 /*
4669 * During the LNA validation we are going to use
4670 * lna0 as correct value. Note that EEPROM_LNA
4671 * is never validated.
4672 */
4673 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
4674 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
4675
4676 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
4677 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
4678 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
4679 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
4680 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
4681 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
4682
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01004683 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &word);
4684 if ((word & 0x00ff) != 0x00ff) {
4685 drv_data->txmixer_gain_24g =
4686 rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_BG_VAL);
4687 } else {
4688 drv_data->txmixer_gain_24g = 0;
4689 }
4690
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004691 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
4692 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
4693 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
4694 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
4695 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
4696 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
4697 default_lna_gain);
4698 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
4699
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01004700 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_A, &word);
4701 if ((word & 0x00ff) != 0x00ff) {
4702 drv_data->txmixer_gain_5g =
4703 rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_A_VAL);
4704 } else {
4705 drv_data->txmixer_gain_5g = 0;
4706 }
4707
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004708 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
4709 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
4710 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
4711 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
4712 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
4713 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
4714
4715 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
4716 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
4717 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
4718 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
4719 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
4720 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
4721 default_lna_gain);
4722 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
4723
4724 return 0;
4725}
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004726
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02004727static int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004728{
4729 u32 reg;
4730 u16 value;
4731 u16 eeprom;
4732
4733 /*
4734 * Read EEPROM word for configuration.
4735 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004736 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004737
4738 /*
Gabor Juhosadde5882011-03-03 11:46:45 +01004739 * Identify RF chipset by EEPROM value
4740 * RT28xx/RT30xx: defined in "EEPROM_NIC_CONF0_RF_TYPE" field
4741 * RT53xx: defined in "EEPROM_CHIP_ID" field
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004742 */
Woody Hunga89534e2012-06-13 15:01:16 +08004743 if (rt2x00_rt(rt2x00dev, RT3290))
4744 rt2800_register_read(rt2x00dev, MAC_CSR0_3290, &reg);
4745 else
4746 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
4747
4748 if (rt2x00_get_field32(reg, MAC_CSR0_CHIPSET) == RT3290 ||
4749 rt2x00_get_field32(reg, MAC_CSR0_CHIPSET) == RT5390 ||
4750 rt2x00_get_field32(reg, MAC_CSR0_CHIPSET) == RT5392)
Gabor Juhosadde5882011-03-03 11:46:45 +01004751 rt2x00_eeprom_read(rt2x00dev, EEPROM_CHIP_ID, &value);
4752 else
4753 value = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RF_TYPE);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004754
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01004755 rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
4756 value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
Gertjan van Wingerde714fa662010-02-13 20:55:48 +01004757
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01004758 switch (rt2x00dev->chip.rt) {
4759 case RT2860:
4760 case RT2872:
4761 case RT2883:
4762 case RT3070:
4763 case RT3071:
4764 case RT3090:
Woody Hunga89534e2012-06-13 15:01:16 +08004765 case RT3290:
Daniel Golle03839952012-09-09 14:24:39 +03004766 case RT3352:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01004767 case RT3390:
4768 case RT3572:
4769 case RT5390:
John Li2ed71882012-02-17 17:33:06 +08004770 case RT5392:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01004771 break;
4772 default:
John Lib6df7f12012-02-08 21:25:24 +08004773 ERROR(rt2x00dev, "Invalid RT chipset 0x%04x detected.\n", rt2x00dev->chip.rt);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01004774 return -ENODEV;
4775 }
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004776
Larry Fingerd331eb52011-09-14 16:50:22 -05004777 switch (rt2x00dev->chip.rf) {
4778 case RF2820:
4779 case RF2850:
4780 case RF2720:
4781 case RF2750:
4782 case RF3020:
4783 case RF2020:
4784 case RF3021:
4785 case RF3022:
4786 case RF3052:
Woody Hunga89534e2012-06-13 15:01:16 +08004787 case RF3290:
Larry Fingerd331eb52011-09-14 16:50:22 -05004788 case RF3320:
Daniel Golle03839952012-09-09 14:24:39 +03004789 case RF3322:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02004790 case RF5360:
Larry Fingerd331eb52011-09-14 16:50:22 -05004791 case RF5370:
John Li2ed71882012-02-17 17:33:06 +08004792 case RF5372:
Larry Fingerd331eb52011-09-14 16:50:22 -05004793 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08004794 case RF5392:
Larry Fingerd331eb52011-09-14 16:50:22 -05004795 break;
4796 default:
John Lib6df7f12012-02-08 21:25:24 +08004797 ERROR(rt2x00dev, "Invalid RF chipset 0x%04x detected.\n",
Larry Fingerd331eb52011-09-14 16:50:22 -05004798 rt2x00dev->chip.rf);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004799 return -ENODEV;
4800 }
4801
4802 /*
4803 * Identify default antenna configuration.
4804 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01004805 rt2x00dev->default_ant.tx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004806 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01004807 rt2x00dev->default_ant.rx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004808 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004809
RA-Jay Hungd96aa642011-02-20 13:54:52 +01004810 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
4811
4812 if (rt2x00_rt(rt2x00dev, RT3070) ||
4813 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03004814 rt2x00_rt(rt2x00dev, RT3352) ||
RA-Jay Hungd96aa642011-02-20 13:54:52 +01004815 rt2x00_rt(rt2x00dev, RT3390)) {
4816 value = rt2x00_get_field16(eeprom,
4817 EEPROM_NIC_CONF1_ANT_DIVERSITY);
4818 switch (value) {
4819 case 0:
4820 case 1:
4821 case 2:
4822 rt2x00dev->default_ant.tx = ANTENNA_A;
4823 rt2x00dev->default_ant.rx = ANTENNA_A;
4824 break;
4825 case 3:
4826 rt2x00dev->default_ant.tx = ANTENNA_A;
4827 rt2x00dev->default_ant.rx = ANTENNA_B;
4828 break;
4829 }
4830 } else {
4831 rt2x00dev->default_ant.tx = ANTENNA_A;
4832 rt2x00dev->default_ant.rx = ANTENNA_A;
4833 }
4834
Anisse Astier0586a112012-04-23 12:33:11 +02004835 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390R)) {
4836 rt2x00dev->default_ant.tx = ANTENNA_HW_DIVERSITY; /* Unused */
4837 rt2x00dev->default_ant.rx = ANTENNA_HW_DIVERSITY; /* Unused */
4838 }
4839
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004840 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02004841 * Determine external LNA informations.
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004842 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004843 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02004844 __set_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004845 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02004846 __set_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004847
4848 /*
4849 * Detect if this device has an hardware controlled radio.
4850 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004851 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_HW_RADIO))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02004852 __set_bit(CAPABILITY_HW_BUTTON, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004853
4854 /*
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02004855 * Detect if this device has Bluetooth co-existence.
4856 */
4857 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_BT_COEXIST))
4858 __set_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags);
4859
4860 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02004861 * Read frequency offset and RF programming sequence.
4862 */
4863 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
4864 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
4865
4866 /*
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004867 * Store led settings, for correct led behaviour.
4868 */
4869#ifdef CONFIG_RT2X00_LIB_LEDS
4870 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
4871 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
4872 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
4873
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02004874 rt2x00dev->led_mcu_reg = eeprom;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004875#endif /* CONFIG_RT2X00_LIB_LEDS */
4876
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004877 /*
4878 * Check if support EIRP tx power limit feature.
4879 */
4880 rt2x00_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER, &eeprom);
4881
4882 if (rt2x00_get_field16(eeprom, EEPROM_EIRP_MAX_TX_POWER_2GHZ) <
4883 EIRP_MAX_TX_POWER_LIMIT)
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02004884 __set_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004885
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004886 return 0;
4887}
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01004888
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004889/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02004890 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004891 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
4892 */
4893static const struct rf_channel rf_vals[] = {
4894 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
4895 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
4896 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
4897 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
4898 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
4899 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
4900 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
4901 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
4902 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
4903 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
4904 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
4905 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
4906 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
4907 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
4908
4909 /* 802.11 UNI / HyperLan 2 */
4910 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
4911 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
4912 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
4913 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
4914 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
4915 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
4916 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
4917 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
4918 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
4919 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
4920 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
4921 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
4922
4923 /* 802.11 HyperLan 2 */
4924 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
4925 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
4926 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
4927 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
4928 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
4929 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
4930 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
4931 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
4932 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
4933 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
4934 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
4935 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
4936 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
4937 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
4938 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
4939 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
4940
4941 /* 802.11 UNII */
4942 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
4943 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
4944 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
4945 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
4946 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
4947 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
4948 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
4949 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
4950 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
4951 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
4952 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
4953
4954 /* 802.11 Japan */
4955 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
4956 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
4957 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
4958 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
4959 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
4960 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
4961 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
4962};
4963
4964/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02004965 * RF value list for rt3xxx
4966 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004967 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02004968static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004969 {1, 241, 2, 2 },
4970 {2, 241, 2, 7 },
4971 {3, 242, 2, 2 },
4972 {4, 242, 2, 7 },
4973 {5, 243, 2, 2 },
4974 {6, 243, 2, 7 },
4975 {7, 244, 2, 2 },
4976 {8, 244, 2, 7 },
4977 {9, 245, 2, 2 },
4978 {10, 245, 2, 7 },
4979 {11, 246, 2, 2 },
4980 {12, 246, 2, 7 },
4981 {13, 247, 2, 2 },
4982 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02004983
4984 /* 802.11 UNI / HyperLan 2 */
4985 {36, 0x56, 0, 4},
4986 {38, 0x56, 0, 6},
4987 {40, 0x56, 0, 8},
4988 {44, 0x57, 0, 0},
4989 {46, 0x57, 0, 2},
4990 {48, 0x57, 0, 4},
4991 {52, 0x57, 0, 8},
4992 {54, 0x57, 0, 10},
4993 {56, 0x58, 0, 0},
4994 {60, 0x58, 0, 4},
4995 {62, 0x58, 0, 6},
4996 {64, 0x58, 0, 8},
4997
4998 /* 802.11 HyperLan 2 */
4999 {100, 0x5b, 0, 8},
5000 {102, 0x5b, 0, 10},
5001 {104, 0x5c, 0, 0},
5002 {108, 0x5c, 0, 4},
5003 {110, 0x5c, 0, 6},
5004 {112, 0x5c, 0, 8},
5005 {116, 0x5d, 0, 0},
5006 {118, 0x5d, 0, 2},
5007 {120, 0x5d, 0, 4},
5008 {124, 0x5d, 0, 8},
5009 {126, 0x5d, 0, 10},
5010 {128, 0x5e, 0, 0},
5011 {132, 0x5e, 0, 4},
5012 {134, 0x5e, 0, 6},
5013 {136, 0x5e, 0, 8},
5014 {140, 0x5f, 0, 0},
5015
5016 /* 802.11 UNII */
5017 {149, 0x5f, 0, 9},
5018 {151, 0x5f, 0, 11},
5019 {153, 0x60, 0, 1},
5020 {157, 0x60, 0, 5},
5021 {159, 0x60, 0, 7},
5022 {161, 0x60, 0, 9},
5023 {165, 0x61, 0, 1},
5024 {167, 0x61, 0, 3},
5025 {169, 0x61, 0, 5},
5026 {171, 0x61, 0, 7},
5027 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005028};
5029
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02005030static int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005031{
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005032 struct hw_mode_spec *spec = &rt2x00dev->spec;
5033 struct channel_info *info;
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02005034 char *default_power1;
5035 char *default_power2;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005036 unsigned int i;
5037 u16 eeprom;
5038
5039 /*
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01005040 * Disable powersaving as default on PCI devices.
5041 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01005042 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01005043 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
5044
5045 /*
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005046 * Initialize all hw fields.
5047 */
5048 rt2x00dev->hw->flags =
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005049 IEEE80211_HW_SIGNAL_DBM |
5050 IEEE80211_HW_SUPPORTS_PS |
Helmut Schaa1df90802010-06-29 21:38:12 +02005051 IEEE80211_HW_PS_NULLFUNC_STACK |
Helmut Schaa9d4f09b2012-03-14 08:56:47 +01005052 IEEE80211_HW_AMPDU_AGGREGATION |
5053 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
5054
Helmut Schaa5a5b6ed2010-10-02 11:31:33 +02005055 /*
5056 * Don't set IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING for USB devices
5057 * unless we are capable of sending the buffered frames out after the
5058 * DTIM transmission using rt2x00lib_beacondone. This will send out
5059 * multicast and broadcast traffic immediately instead of buffering it
5060 * infinitly and thus dropping it after some time.
5061 */
5062 if (!rt2x00_is_usb(rt2x00dev))
5063 rt2x00dev->hw->flags |=
5064 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005065
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005066 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
5067 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
5068 rt2x00_eeprom_addr(rt2x00dev,
5069 EEPROM_MAC_ADDR_0));
5070
Helmut Schaa3f2bee22010-06-14 22:12:01 +02005071 /*
5072 * As rt2800 has a global fallback table we cannot specify
5073 * more then one tx rate per frame but since the hw will
5074 * try several rates (based on the fallback table) we should
Helmut Schaaba3b9e52010-10-02 11:32:16 +02005075 * initialize max_report_rates to the maximum number of rates
Helmut Schaa3f2bee22010-06-14 22:12:01 +02005076 * we are going to try. Otherwise mac80211 will truncate our
5077 * reported tx rates and the rc algortihm will end up with
5078 * incorrect data.
5079 */
Helmut Schaaba3b9e52010-10-02 11:32:16 +02005080 rt2x00dev->hw->max_rates = 1;
5081 rt2x00dev->hw->max_report_rates = 7;
Helmut Schaa3f2bee22010-06-14 22:12:01 +02005082 rt2x00dev->hw->max_rate_tries = 1;
5083
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005084 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005085
5086 /*
5087 * Initialize hw_mode information.
5088 */
5089 spec->supported_bands = SUPPORT_BAND_2GHZ;
5090 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
5091
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01005092 if (rt2x00_rf(rt2x00dev, RF2820) ||
Ivo van Doorn55f93212010-05-06 14:45:46 +02005093 rt2x00_rf(rt2x00dev, RF2720)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005094 spec->num_channels = 14;
5095 spec->channels = rf_vals;
Ivo van Doorn55f93212010-05-06 14:45:46 +02005096 } else if (rt2x00_rf(rt2x00dev, RF2850) ||
5097 rt2x00_rf(rt2x00dev, RF2750)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005098 spec->supported_bands |= SUPPORT_BAND_5GHZ;
5099 spec->num_channels = ARRAY_SIZE(rf_vals);
5100 spec->channels = rf_vals;
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01005101 } else if (rt2x00_rf(rt2x00dev, RF3020) ||
5102 rt2x00_rf(rt2x00dev, RF2020) ||
5103 rt2x00_rf(rt2x00dev, RF3021) ||
Gertjan van Wingerdef93bc9b2010-11-13 19:09:50 +01005104 rt2x00_rf(rt2x00dev, RF3022) ||
Woody Hunga89534e2012-06-13 15:01:16 +08005105 rt2x00_rf(rt2x00dev, RF3290) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01005106 rt2x00_rf(rt2x00dev, RF3320) ||
Daniel Golle03839952012-09-09 14:24:39 +03005107 rt2x00_rf(rt2x00dev, RF3322) ||
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02005108 rt2x00_rf(rt2x00dev, RF5360) ||
Gertjan van Wingerdeaca355b2011-05-04 21:41:36 +02005109 rt2x00_rf(rt2x00dev, RF5370) ||
John Li2ed71882012-02-17 17:33:06 +08005110 rt2x00_rf(rt2x00dev, RF5372) ||
Zero.Lincff3d1f2012-05-29 16:11:09 +08005111 rt2x00_rf(rt2x00dev, RF5390) ||
5112 rt2x00_rf(rt2x00dev, RF5392)) {
Ivo van Doorn55f93212010-05-06 14:45:46 +02005113 spec->num_channels = 14;
5114 spec->channels = rf_vals_3x;
5115 } else if (rt2x00_rf(rt2x00dev, RF3052)) {
5116 spec->supported_bands |= SUPPORT_BAND_5GHZ;
5117 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
5118 spec->channels = rf_vals_3x;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005119 }
5120
5121 /*
5122 * Initialize HT information.
5123 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01005124 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01005125 spec->ht.ht_supported = true;
5126 else
5127 spec->ht.ht_supported = false;
5128
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005129 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02005130 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005131 IEEE80211_HT_CAP_GRN_FLD |
5132 IEEE80211_HT_CAP_SGI_20 |
Ivo van Doornaa674632010-06-29 21:48:37 +02005133 IEEE80211_HT_CAP_SGI_40;
Helmut Schaa22cabaa2010-06-03 10:52:10 +02005134
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005135 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) >= 2)
Helmut Schaa22cabaa2010-06-03 10:52:10 +02005136 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
5137
Ivo van Doornaa674632010-06-29 21:48:37 +02005138 spec->ht.cap |=
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005139 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) <<
Ivo van Doornaa674632010-06-29 21:48:37 +02005140 IEEE80211_HT_CAP_RX_STBC_SHIFT;
5141
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005142 spec->ht.ampdu_factor = 3;
5143 spec->ht.ampdu_density = 4;
5144 spec->ht.mcs.tx_params =
5145 IEEE80211_HT_MCS_TX_DEFINED |
5146 IEEE80211_HT_MCS_TX_RX_DIFF |
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005147 ((rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) - 1) <<
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005148 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
5149
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005150 switch (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005151 case 3:
5152 spec->ht.mcs.rx_mask[2] = 0xff;
5153 case 2:
5154 spec->ht.mcs.rx_mask[1] = 0xff;
5155 case 1:
5156 spec->ht.mcs.rx_mask[0] = 0xff;
5157 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
5158 break;
5159 }
5160
5161 /*
5162 * Create channel information array
5163 */
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00005164 info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005165 if (!info)
5166 return -ENOMEM;
5167
5168 spec->channels_info = info;
5169
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02005170 default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
5171 default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005172
5173 for (i = 0; i < 14; i++) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01005174 info[i].default_power1 = default_power1[i];
5175 info[i].default_power2 = default_power2[i];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005176 }
5177
5178 if (spec->num_channels > 14) {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02005179 default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
5180 default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005181
5182 for (i = 14; i < spec->num_channels; i++) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01005183 info[i].default_power1 = default_power1[i];
5184 info[i].default_power2 = default_power2[i];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005185 }
5186 }
5187
John Li2e9c43d2012-02-16 21:40:57 +08005188 switch (rt2x00dev->chip.rf) {
5189 case RF2020:
5190 case RF3020:
5191 case RF3021:
5192 case RF3022:
5193 case RF3320:
5194 case RF3052:
Woody Hunga89534e2012-06-13 15:01:16 +08005195 case RF3290:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02005196 case RF5360:
John Li2e9c43d2012-02-16 21:40:57 +08005197 case RF5370:
5198 case RF5372:
5199 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08005200 case RF5392:
John Li2e9c43d2012-02-16 21:40:57 +08005201 __set_bit(CAPABILITY_VCO_RECALIBRATION, &rt2x00dev->cap_flags);
5202 break;
5203 }
5204
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005205 return 0;
5206}
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02005207
5208int rt2800_probe_hw(struct rt2x00_dev *rt2x00dev)
5209{
5210 int retval;
5211 u32 reg;
5212
5213 /*
5214 * Allocate eeprom data.
5215 */
5216 retval = rt2800_validate_eeprom(rt2x00dev);
5217 if (retval)
5218 return retval;
5219
5220 retval = rt2800_init_eeprom(rt2x00dev);
5221 if (retval)
5222 return retval;
5223
5224 /*
5225 * Enable rfkill polling by setting GPIO direction of the
5226 * rfkill switch GPIO pin correctly.
5227 */
5228 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
5229 rt2x00_set_field32(&reg, GPIO_CTRL_DIR2, 1);
5230 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
5231
5232 /*
5233 * Initialize hw specifications.
5234 */
5235 retval = rt2800_probe_hw_mode(rt2x00dev);
5236 if (retval)
5237 return retval;
5238
5239 /*
5240 * Set device capabilities.
5241 */
5242 __set_bit(CAPABILITY_CONTROL_FILTERS, &rt2x00dev->cap_flags);
5243 __set_bit(CAPABILITY_CONTROL_FILTER_PSPOLL, &rt2x00dev->cap_flags);
5244 if (!rt2x00_is_usb(rt2x00dev))
5245 __set_bit(CAPABILITY_PRE_TBTT_INTERRUPT, &rt2x00dev->cap_flags);
5246
5247 /*
5248 * Set device requirements.
5249 */
5250 if (!rt2x00_is_soc(rt2x00dev))
5251 __set_bit(REQUIRE_FIRMWARE, &rt2x00dev->cap_flags);
5252 __set_bit(REQUIRE_L2PAD, &rt2x00dev->cap_flags);
5253 __set_bit(REQUIRE_TXSTATUS_FIFO, &rt2x00dev->cap_flags);
5254 if (!rt2800_hwcrypt_disabled(rt2x00dev))
5255 __set_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags);
5256 __set_bit(CAPABILITY_LINK_TUNING, &rt2x00dev->cap_flags);
5257 __set_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags);
5258 if (rt2x00_is_usb(rt2x00dev))
5259 __set_bit(REQUIRE_PS_AUTOWAKE, &rt2x00dev->cap_flags);
5260 else {
5261 __set_bit(REQUIRE_DMA, &rt2x00dev->cap_flags);
5262 __set_bit(REQUIRE_TASKLET_CONTEXT, &rt2x00dev->cap_flags);
5263 }
5264
5265 /*
5266 * Set the rssi offset.
5267 */
5268 rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
5269
5270 return 0;
5271}
5272EXPORT_SYMBOL_GPL(rt2800_probe_hw);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005273
5274/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005275 * IEEE80211 stack callback functions.
5276 */
Helmut Schaae7836192010-07-11 12:28:54 +02005277void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx, u32 *iv32,
5278 u16 *iv16)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005279{
5280 struct rt2x00_dev *rt2x00dev = hw->priv;
5281 struct mac_iveiv_entry iveiv_entry;
5282 u32 offset;
5283
5284 offset = MAC_IVEIV_ENTRY(hw_key_idx);
5285 rt2800_register_multiread(rt2x00dev, offset,
5286 &iveiv_entry, sizeof(iveiv_entry));
5287
Julia Lawall855da5e2009-12-13 17:07:45 +01005288 memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
5289 memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005290}
Helmut Schaae7836192010-07-11 12:28:54 +02005291EXPORT_SYMBOL_GPL(rt2800_get_tkip_seq);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005292
Helmut Schaae7836192010-07-11 12:28:54 +02005293int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005294{
5295 struct rt2x00_dev *rt2x00dev = hw->priv;
5296 u32 reg;
5297 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
5298
5299 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
5300 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
5301 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
5302
5303 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
5304 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
5305 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
5306
5307 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
5308 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
5309 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
5310
5311 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
5312 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
5313 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
5314
5315 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
5316 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
5317 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
5318
5319 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
5320 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
5321 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
5322
5323 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
5324 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
5325 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
5326
5327 return 0;
5328}
Helmut Schaae7836192010-07-11 12:28:54 +02005329EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005330
Eliad Peller8a3a3c82011-10-02 10:15:52 +02005331int rt2800_conf_tx(struct ieee80211_hw *hw,
5332 struct ieee80211_vif *vif, u16 queue_idx,
Helmut Schaae7836192010-07-11 12:28:54 +02005333 const struct ieee80211_tx_queue_params *params)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005334{
5335 struct rt2x00_dev *rt2x00dev = hw->priv;
5336 struct data_queue *queue;
5337 struct rt2x00_field32 field;
5338 int retval;
5339 u32 reg;
5340 u32 offset;
5341
5342 /*
5343 * First pass the configuration through rt2x00lib, that will
5344 * update the queue settings and validate the input. After that
5345 * we are free to update the registers based on the value
5346 * in the queue parameter.
5347 */
Eliad Peller8a3a3c82011-10-02 10:15:52 +02005348 retval = rt2x00mac_conf_tx(hw, vif, queue_idx, params);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005349 if (retval)
5350 return retval;
5351
5352 /*
5353 * We only need to perform additional register initialization
5354 * for WMM queues/
5355 */
5356 if (queue_idx >= 4)
5357 return 0;
5358
Helmut Schaa11f818e2011-03-03 19:38:55 +01005359 queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005360
5361 /* Update WMM TXOP register */
5362 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
5363 field.bit_offset = (queue_idx & 1) * 16;
5364 field.bit_mask = 0xffff << field.bit_offset;
5365
5366 rt2800_register_read(rt2x00dev, offset, &reg);
5367 rt2x00_set_field32(&reg, field, queue->txop);
5368 rt2800_register_write(rt2x00dev, offset, reg);
5369
5370 /* Update WMM registers */
5371 field.bit_offset = queue_idx * 4;
5372 field.bit_mask = 0xf << field.bit_offset;
5373
5374 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
5375 rt2x00_set_field32(&reg, field, queue->aifs);
5376 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
5377
5378 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
5379 rt2x00_set_field32(&reg, field, queue->cw_min);
5380 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
5381
5382 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
5383 rt2x00_set_field32(&reg, field, queue->cw_max);
5384 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
5385
5386 /* Update EDCA registers */
5387 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
5388
5389 rt2800_register_read(rt2x00dev, offset, &reg);
5390 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
5391 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
5392 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
5393 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
5394 rt2800_register_write(rt2x00dev, offset, reg);
5395
5396 return 0;
5397}
Helmut Schaae7836192010-07-11 12:28:54 +02005398EXPORT_SYMBOL_GPL(rt2800_conf_tx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005399
Eliad Peller37a41b42011-09-21 14:06:11 +03005400u64 rt2800_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005401{
5402 struct rt2x00_dev *rt2x00dev = hw->priv;
5403 u64 tsf;
5404 u32 reg;
5405
5406 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
5407 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
5408 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
5409 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
5410
5411 return tsf;
5412}
Helmut Schaae7836192010-07-11 12:28:54 +02005413EXPORT_SYMBOL_GPL(rt2800_get_tsf);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005414
Helmut Schaae7836192010-07-11 12:28:54 +02005415int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5416 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01005417 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5418 u8 buf_size)
Helmut Schaa1df90802010-06-29 21:38:12 +02005419{
Helmut Schaaaf353232011-09-08 14:38:36 +02005420 struct rt2x00_sta *sta_priv = (struct rt2x00_sta *)sta->drv_priv;
Helmut Schaa1df90802010-06-29 21:38:12 +02005421 int ret = 0;
5422
Helmut Schaaaf353232011-09-08 14:38:36 +02005423 /*
5424 * Don't allow aggregation for stations the hardware isn't aware
5425 * of because tx status reports for frames to an unknown station
5426 * always contain wcid=255 and thus we can't distinguish between
5427 * multiple stations which leads to unwanted situations when the
5428 * hw reorders frames due to aggregation.
5429 */
5430 if (sta_priv->wcid < 0)
5431 return 1;
5432
Helmut Schaa1df90802010-06-29 21:38:12 +02005433 switch (action) {
5434 case IEEE80211_AMPDU_RX_START:
5435 case IEEE80211_AMPDU_RX_STOP:
Helmut Schaa58ed8262010-10-02 11:33:17 +02005436 /*
5437 * The hw itself takes care of setting up BlockAck mechanisms.
5438 * So, we only have to allow mac80211 to nagotiate a BlockAck
5439 * agreement. Once that is done, the hw will BlockAck incoming
5440 * AMPDUs without further setup.
5441 */
Helmut Schaa1df90802010-06-29 21:38:12 +02005442 break;
5443 case IEEE80211_AMPDU_TX_START:
5444 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
5445 break;
5446 case IEEE80211_AMPDU_TX_STOP:
5447 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
5448 break;
5449 case IEEE80211_AMPDU_TX_OPERATIONAL:
5450 break;
5451 default:
Ivo van Doorn4e9e58c2010-06-29 21:49:50 +02005452 WARNING((struct rt2x00_dev *)hw->priv, "Unknown AMPDU action\n");
Helmut Schaa1df90802010-06-29 21:38:12 +02005453 }
5454
5455 return ret;
5456}
Helmut Schaae7836192010-07-11 12:28:54 +02005457EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02005458
Helmut Schaa977206d2010-12-13 12:31:58 +01005459int rt2800_get_survey(struct ieee80211_hw *hw, int idx,
5460 struct survey_info *survey)
5461{
5462 struct rt2x00_dev *rt2x00dev = hw->priv;
5463 struct ieee80211_conf *conf = &hw->conf;
5464 u32 idle, busy, busy_ext;
5465
5466 if (idx != 0)
5467 return -ENOENT;
5468
5469 survey->channel = conf->channel;
5470
5471 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &idle);
5472 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &busy);
5473 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &busy_ext);
5474
5475 if (idle || busy) {
5476 survey->filled = SURVEY_INFO_CHANNEL_TIME |
5477 SURVEY_INFO_CHANNEL_TIME_BUSY |
5478 SURVEY_INFO_CHANNEL_TIME_EXT_BUSY;
5479
5480 survey->channel_time = (idle + busy) / 1000;
5481 survey->channel_time_busy = busy / 1000;
5482 survey->channel_time_ext_busy = busy_ext / 1000;
5483 }
5484
Helmut Schaa9931df22011-12-22 09:36:29 +01005485 if (!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
5486 survey->filled |= SURVEY_INFO_IN_USE;
5487
Helmut Schaa977206d2010-12-13 12:31:58 +01005488 return 0;
5489
5490}
5491EXPORT_SYMBOL_GPL(rt2800_get_survey);
5492
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02005493MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
5494MODULE_VERSION(DRV_VERSION);
5495MODULE_DESCRIPTION("Ralink RT2800 library");
5496MODULE_LICENSE("GPL");