blob: e36cf4a233fea499f0439202cc3bdc0e6e13d95e [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
Stanislaw Gruszka92941382013-01-26 18:13:50 +010083 WARNING(rt2x00dev, "Unknown RF chipset on rt305x\n");
Helmut Schaabaff8002010-04-28 09:58:59 +020084 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 Gruszka87561302013-03-16 19:19:45 +0100530 if (rt2x00_is_usb(rt2x00dev)) {
Stanislaw Gruszka0c17cf92012-01-24 14:09:06 +0100531 rt2800_register_write(rt2x00dev, H2M_INT_SRC, 0);
Stanislaw Gruszka87561302013-03-16 19:19:45 +0100532 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
533 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200534 msleep(1);
535
536 return 0;
537}
538EXPORT_SYMBOL_GPL(rt2800_load_firmware);
539
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200540void rt2800_write_tx_data(struct queue_entry *entry,
541 struct txentry_desc *txdesc)
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200542{
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200543 __le32 *txwi = rt2800_drv_get_txwi(entry);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200544 u32 word;
545
546 /*
547 * Initialize TX Info descriptor
548 */
549 rt2x00_desc_read(txwi, 0, &word);
550 rt2x00_set_field32(&word, TXWI_W0_FRAG,
551 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
Ivo van Doorn84804cd2010-08-06 20:46:19 +0200552 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS,
553 test_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags));
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200554 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
555 rt2x00_set_field32(&word, TXWI_W0_TS,
556 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
557 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
558 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100559 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY,
560 txdesc->u.ht.mpdu_density);
561 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->u.ht.txop);
562 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->u.ht.mcs);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200563 rt2x00_set_field32(&word, TXWI_W0_BW,
564 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
565 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
566 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100567 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->u.ht.stbc);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200568 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
569 rt2x00_desc_write(txwi, 0, word);
570
571 rt2x00_desc_read(txwi, 1, &word);
572 rt2x00_set_field32(&word, TXWI_W1_ACK,
573 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
574 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
575 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100576 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->u.ht.ba_size);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200577 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
578 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
Helmut Schaaa2b13282011-09-08 14:38:01 +0200579 txdesc->key_idx : txdesc->u.ht.wcid);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200580 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
581 txdesc->length);
Helmut Schaa2b23cda2010-11-04 20:38:15 +0100582 rt2x00_set_field32(&word, TXWI_W1_PACKETID_QUEUE, entry->queue->qid);
Ivo van Doornbc8a9792010-10-02 11:32:43 +0200583 rt2x00_set_field32(&word, TXWI_W1_PACKETID_ENTRY, (entry->entry_idx % 3) + 1);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200584 rt2x00_desc_write(txwi, 1, word);
585
586 /*
587 * Always write 0 to IV/EIV fields, hardware will insert the IV
588 * from the IVEIV register when TXD_W3_WIV is set to 0.
589 * When TXD_W3_WIV is set to 1 it will use the IV data
590 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
591 * crypto entry in the registers should be used to encrypt the frame.
592 */
593 _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
594 _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
595}
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200596EXPORT_SYMBOL_GPL(rt2800_write_tx_data);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200597
Helmut Schaaff6133b2010-10-09 13:34:11 +0200598static int rt2800_agc_to_rssi(struct rt2x00_dev *rt2x00dev, u32 rxwi_w2)
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200599{
Luigi Tarenga7fc41752012-01-31 18:51:23 +0100600 s8 rssi0 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI0);
601 s8 rssi1 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI1);
602 s8 rssi2 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI2);
Ivo van Doorn74861922010-07-11 12:23:50 +0200603 u16 eeprom;
604 u8 offset0;
605 u8 offset1;
606 u8 offset2;
607
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200608 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Ivo van Doorn74861922010-07-11 12:23:50 +0200609 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &eeprom);
610 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET0);
611 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET1);
612 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
613 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_OFFSET2);
614 } else {
615 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &eeprom);
616 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET0);
617 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET1);
618 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
619 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_OFFSET2);
620 }
621
622 /*
623 * Convert the value from the descriptor into the RSSI value
624 * If the value in the descriptor is 0, it is considered invalid
625 * and the default (extremely low) rssi value is assumed
626 */
627 rssi0 = (rssi0) ? (-12 - offset0 - rt2x00dev->lna_gain - rssi0) : -128;
628 rssi1 = (rssi1) ? (-12 - offset1 - rt2x00dev->lna_gain - rssi1) : -128;
629 rssi2 = (rssi2) ? (-12 - offset2 - rt2x00dev->lna_gain - rssi2) : -128;
630
631 /*
632 * mac80211 only accepts a single RSSI value. Calculating the
633 * average doesn't deliver a fair answer either since -60:-60 would
634 * be considered equally good as -50:-70 while the second is the one
635 * which gives less energy...
636 */
637 rssi0 = max(rssi0, rssi1);
Luigi Tarenga7fc41752012-01-31 18:51:23 +0100638 return (int)max(rssi0, rssi2);
Ivo van Doorn74861922010-07-11 12:23:50 +0200639}
640
641void rt2800_process_rxwi(struct queue_entry *entry,
642 struct rxdone_entry_desc *rxdesc)
643{
644 __le32 *rxwi = (__le32 *) entry->skb->data;
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200645 u32 word;
646
647 rt2x00_desc_read(rxwi, 0, &word);
648
649 rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
650 rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
651
652 rt2x00_desc_read(rxwi, 1, &word);
653
654 if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
655 rxdesc->flags |= RX_FLAG_SHORT_GI;
656
657 if (rt2x00_get_field32(word, RXWI_W1_BW))
658 rxdesc->flags |= RX_FLAG_40MHZ;
659
660 /*
661 * Detect RX rate, always use MCS as signal type.
662 */
663 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
664 rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
665 rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
666
667 /*
668 * Mask of 0x8 bit to remove the short preamble flag.
669 */
670 if (rxdesc->rate_mode == RATE_MODE_CCK)
671 rxdesc->signal &= ~0x8;
672
673 rt2x00_desc_read(rxwi, 2, &word);
674
Ivo van Doorn74861922010-07-11 12:23:50 +0200675 /*
676 * Convert descriptor AGC value to RSSI value.
677 */
678 rxdesc->rssi = rt2800_agc_to_rssi(entry->queue->rt2x00dev, word);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200679
680 /*
681 * Remove RXWI descriptor from start of buffer.
682 */
Ivo van Doorn74861922010-07-11 12:23:50 +0200683 skb_pull(entry->skb, RXWI_DESC_SIZE);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200684}
685EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
686
Helmut Schaa31937c42011-09-07 20:10:02 +0200687void rt2800_txdone_entry(struct queue_entry *entry, u32 status, __le32 *txwi)
Helmut Schaa14433332010-10-02 11:27:03 +0200688{
689 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Helmut Schaab34793e2010-10-02 11:34:56 +0200690 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Helmut Schaa14433332010-10-02 11:27:03 +0200691 struct txdone_entry_desc txdesc;
692 u32 word;
693 u16 mcs, real_mcs;
Helmut Schaab34793e2010-10-02 11:34:56 +0200694 int aggr, ampdu;
Helmut Schaa14433332010-10-02 11:27:03 +0200695
696 /*
697 * Obtain the status about this packet.
698 */
699 txdesc.flags = 0;
Helmut Schaa14433332010-10-02 11:27:03 +0200700 rt2x00_desc_read(txwi, 0, &word);
Helmut Schaab34793e2010-10-02 11:34:56 +0200701
Helmut Schaa14433332010-10-02 11:27:03 +0200702 mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200703 ampdu = rt2x00_get_field32(word, TXWI_W0_AMPDU);
704
Helmut Schaa14433332010-10-02 11:27:03 +0200705 real_mcs = rt2x00_get_field32(status, TX_STA_FIFO_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200706 aggr = rt2x00_get_field32(status, TX_STA_FIFO_TX_AGGRE);
707
708 /*
709 * If a frame was meant to be sent as a single non-aggregated MPDU
710 * but ended up in an aggregate the used tx rate doesn't correlate
711 * with the one specified in the TXWI as the whole aggregate is sent
712 * with the same rate.
713 *
714 * For example: two frames are sent to rt2x00, the first one sets
715 * AMPDU=1 and requests MCS7 whereas the second frame sets AMDPU=0
716 * and requests MCS15. If the hw aggregates both frames into one
717 * AMDPU the tx status for both frames will contain MCS7 although
718 * the frame was sent successfully.
719 *
720 * Hence, replace the requested rate with the real tx rate to not
721 * confuse the rate control algortihm by providing clearly wrong
722 * data.
723 */
Helmut Schaa5356d962011-03-03 19:40:33 +0100724 if (unlikely(aggr == 1 && ampdu == 0 && real_mcs != mcs)) {
Helmut Schaab34793e2010-10-02 11:34:56 +0200725 skbdesc->tx_rate_idx = real_mcs;
726 mcs = real_mcs;
727 }
Helmut Schaa14433332010-10-02 11:27:03 +0200728
Helmut Schaaf16d2db2011-03-28 13:35:21 +0200729 if (aggr == 1 || ampdu == 1)
730 __set_bit(TXDONE_AMPDU, &txdesc.flags);
731
Helmut Schaa14433332010-10-02 11:27:03 +0200732 /*
733 * Ralink has a retry mechanism using a global fallback
734 * table. We setup this fallback table to try the immediate
735 * lower rate for all rates. In the TX_STA_FIFO, the MCS field
736 * always contains the MCS used for the last transmission, be
737 * it successful or not.
738 */
739 if (rt2x00_get_field32(status, TX_STA_FIFO_TX_SUCCESS)) {
740 /*
741 * Transmission succeeded. The number of retries is
742 * mcs - real_mcs
743 */
744 __set_bit(TXDONE_SUCCESS, &txdesc.flags);
745 txdesc.retry = ((mcs > real_mcs) ? mcs - real_mcs : 0);
746 } else {
747 /*
748 * Transmission failed. The number of retries is
749 * always 7 in this case (for a total number of 8
750 * frames sent).
751 */
752 __set_bit(TXDONE_FAILURE, &txdesc.flags);
753 txdesc.retry = rt2x00dev->long_retry;
754 }
755
756 /*
757 * the frame was retried at least once
758 * -> hw used fallback rates
759 */
760 if (txdesc.retry)
761 __set_bit(TXDONE_FALLBACK, &txdesc.flags);
762
763 rt2x00lib_txdone(entry, &txdesc);
764}
765EXPORT_SYMBOL_GPL(rt2800_txdone_entry);
766
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200767void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
768{
769 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
770 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
771 unsigned int beacon_base;
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100772 unsigned int padding_len;
Seth Forsheed76dfc62011-02-14 08:52:25 -0600773 u32 orig_reg, reg;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200774
775 /*
776 * Disable beaconing while we are reloading the beacon data,
777 * otherwise we might be sending out invalid data.
778 */
779 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Seth Forsheed76dfc62011-02-14 08:52:25 -0600780 orig_reg = reg;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200781 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
782 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
783
784 /*
785 * Add space for the TXWI in front of the skb.
786 */
Stanislaw Gruszkab52398b2011-07-30 13:32:56 +0200787 memset(skb_push(entry->skb, TXWI_DESC_SIZE), 0, TXWI_DESC_SIZE);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200788
789 /*
790 * Register descriptor details in skb frame descriptor.
791 */
792 skbdesc->flags |= SKBDESC_DESC_IN_SKB;
793 skbdesc->desc = entry->skb->data;
794 skbdesc->desc_len = TXWI_DESC_SIZE;
795
796 /*
797 * Add the TXWI for the beacon to the skb.
798 */
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200799 rt2800_write_tx_data(entry, txdesc);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200800
801 /*
802 * Dump beacon to userspace through debugfs.
803 */
804 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
805
806 /*
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100807 * Write entire beacon with TXWI and padding to register.
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200808 */
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100809 padding_len = roundup(entry->skb->len, 4) - entry->skb->len;
Seth Forsheed76dfc62011-02-14 08:52:25 -0600810 if (padding_len && skb_pad(entry->skb, padding_len)) {
811 ERROR(rt2x00dev, "Failure padding beacon, aborting\n");
812 /* skb freed by skb_pad() on failure */
813 entry->skb = NULL;
814 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, orig_reg);
815 return;
816 }
817
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200818 beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100819 rt2800_register_multiwrite(rt2x00dev, beacon_base, entry->skb->data,
820 entry->skb->len + padding_len);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200821
822 /*
823 * Enable beaconing again.
824 */
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200825 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
826 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
827
828 /*
829 * Clean up beacon skb.
830 */
831 dev_kfree_skb_any(entry->skb);
832 entry->skb = NULL;
833}
Ivo van Doorn50e888e2010-07-11 12:26:12 +0200834EXPORT_SYMBOL_GPL(rt2800_write_beacon);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200835
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100836static inline void rt2800_clear_beacon_register(struct rt2x00_dev *rt2x00dev,
837 unsigned int beacon_base)
Helmut Schaafdb87252010-06-29 21:48:06 +0200838{
839 int i;
840
841 /*
842 * For the Beacon base registers we only need to clear
843 * the whole TXWI which (when set to 0) will invalidate
844 * the entire beacon.
845 */
846 for (i = 0; i < TXWI_DESC_SIZE; i += sizeof(__le32))
847 rt2800_register_write(rt2x00dev, beacon_base + i, 0);
848}
849
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100850void rt2800_clear_beacon(struct queue_entry *entry)
851{
852 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
853 u32 reg;
854
855 /*
856 * Disable beaconing while we are reloading the beacon data,
857 * otherwise we might be sending out invalid data.
858 */
859 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
860 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
861 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
862
863 /*
864 * Clear beacon.
865 */
866 rt2800_clear_beacon_register(rt2x00dev,
867 HW_BEACON_OFFSET(entry->entry_idx));
868
869 /*
870 * Enabled beaconing again.
871 */
872 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
873 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
874}
875EXPORT_SYMBOL_GPL(rt2800_clear_beacon);
876
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100877#ifdef CONFIG_RT2X00_LIB_DEBUGFS
878const struct rt2x00debug rt2800_rt2x00debug = {
879 .owner = THIS_MODULE,
880 .csr = {
881 .read = rt2800_register_read,
882 .write = rt2800_register_write,
883 .flags = RT2X00DEBUGFS_OFFSET,
884 .word_base = CSR_REG_BASE,
885 .word_size = sizeof(u32),
886 .word_count = CSR_REG_SIZE / sizeof(u32),
887 },
888 .eeprom = {
889 .read = rt2x00_eeprom_read,
890 .write = rt2x00_eeprom_write,
891 .word_base = EEPROM_BASE,
892 .word_size = sizeof(u16),
893 .word_count = EEPROM_SIZE / sizeof(u16),
894 },
895 .bbp = {
896 .read = rt2800_bbp_read,
897 .write = rt2800_bbp_write,
898 .word_base = BBP_BASE,
899 .word_size = sizeof(u8),
900 .word_count = BBP_SIZE / sizeof(u8),
901 },
902 .rf = {
903 .read = rt2x00_rf_read,
904 .write = rt2800_rf_write,
905 .word_base = RF_BASE,
906 .word_size = sizeof(u32),
907 .word_count = RF_SIZE / sizeof(u32),
908 },
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200909 .rfcsr = {
910 .read = rt2800_rfcsr_read,
911 .write = rt2800_rfcsr_write,
912 .word_base = RFCSR_BASE,
913 .word_size = sizeof(u8),
914 .word_count = RFCSR_SIZE / sizeof(u8),
915 },
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100916};
917EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
918#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
919
920int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
921{
922 u32 reg;
923
Woody Hunga89534e2012-06-13 15:01:16 +0800924 if (rt2x00_rt(rt2x00dev, RT3290)) {
925 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
926 return rt2x00_get_field32(reg, WLAN_GPIO_IN_BIT0);
927 } else {
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +0200928 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
929 return rt2x00_get_field32(reg, GPIO_CTRL_VAL2);
Woody Hunga89534e2012-06-13 15:01:16 +0800930 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100931}
932EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
933
934#ifdef CONFIG_RT2X00_LIB_LEDS
935static void rt2800_brightness_set(struct led_classdev *led_cdev,
936 enum led_brightness brightness)
937{
938 struct rt2x00_led *led =
939 container_of(led_cdev, struct rt2x00_led, led_dev);
940 unsigned int enabled = brightness != LED_OFF;
941 unsigned int bg_mode =
942 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
943 unsigned int polarity =
944 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
945 EEPROM_FREQ_LED_POLARITY);
946 unsigned int ledmode =
947 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
948 EEPROM_FREQ_LED_MODE);
Layne Edwards44704e52011-04-18 15:26:00 +0200949 u32 reg;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100950
Layne Edwards44704e52011-04-18 15:26:00 +0200951 /* Check for SoC (SOC devices don't support MCU requests) */
952 if (rt2x00_is_soc(led->rt2x00dev)) {
953 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
954
955 /* Set LED Polarity */
956 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, polarity);
957
958 /* Set LED Mode */
959 if (led->type == LED_TYPE_RADIO) {
960 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE,
961 enabled ? 3 : 0);
962 } else if (led->type == LED_TYPE_ASSOC) {
963 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE,
964 enabled ? 3 : 0);
965 } else if (led->type == LED_TYPE_QUALITY) {
966 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE,
967 enabled ? 3 : 0);
968 }
969
970 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
971
972 } else {
973 if (led->type == LED_TYPE_RADIO) {
974 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
975 enabled ? 0x20 : 0);
976 } else if (led->type == LED_TYPE_ASSOC) {
977 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
978 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
979 } else if (led->type == LED_TYPE_QUALITY) {
980 /*
981 * The brightness is divided into 6 levels (0 - 5),
982 * The specs tell us the following levels:
983 * 0, 1 ,3, 7, 15, 31
984 * to determine the level in a simple way we can simply
985 * work with bitshifting:
986 * (1 << level) - 1
987 */
988 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
989 (1 << brightness / (LED_FULL / 6)) - 1,
990 polarity);
991 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100992 }
993}
994
Gertjan van Wingerdeb3579d62009-12-30 11:36:34 +0100995static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100996 struct rt2x00_led *led, enum led_type type)
997{
998 led->rt2x00dev = rt2x00dev;
999 led->type = type;
1000 led->led_dev.brightness_set = rt2800_brightness_set;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001001 led->flags = LED_INITIALIZED;
1002}
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001003#endif /* CONFIG_RT2X00_LIB_LEDS */
1004
1005/*
1006 * Configuration handlers.
1007 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001008static void rt2800_config_wcid(struct rt2x00_dev *rt2x00dev,
1009 const u8 *address,
1010 int wcid)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001011{
1012 struct mac_wcid_entry wcid_entry;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001013 u32 offset;
1014
1015 offset = MAC_WCID_ENTRY(wcid);
1016
1017 memset(&wcid_entry, 0xff, sizeof(wcid_entry));
1018 if (address)
1019 memcpy(wcid_entry.mac, address, ETH_ALEN);
1020
1021 rt2800_register_multiwrite(rt2x00dev, offset,
1022 &wcid_entry, sizeof(wcid_entry));
1023}
1024
1025static void rt2800_delete_wcid_attr(struct rt2x00_dev *rt2x00dev, int wcid)
1026{
1027 u32 offset;
1028 offset = MAC_WCID_ATTR_ENTRY(wcid);
1029 rt2800_register_write(rt2x00dev, offset, 0);
1030}
1031
1032static void rt2800_config_wcid_attr_bssidx(struct rt2x00_dev *rt2x00dev,
1033 int wcid, u32 bssidx)
1034{
1035 u32 offset = MAC_WCID_ATTR_ENTRY(wcid);
1036 u32 reg;
1037
1038 /*
1039 * The BSS Idx numbers is split in a main value of 3 bits,
1040 * and a extended field for adding one additional bit to the value.
1041 */
1042 rt2800_register_read(rt2x00dev, offset, &reg);
1043 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX, (bssidx & 0x7));
1044 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
1045 (bssidx & 0x8) >> 3);
1046 rt2800_register_write(rt2x00dev, offset, reg);
1047}
1048
1049static void rt2800_config_wcid_attr_cipher(struct rt2x00_dev *rt2x00dev,
1050 struct rt2x00lib_crypto *crypto,
1051 struct ieee80211_key_conf *key)
1052{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001053 struct mac_iveiv_entry iveiv_entry;
1054 u32 offset;
1055 u32 reg;
1056
1057 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
1058
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001059 if (crypto->cmd == SET_KEY) {
1060 rt2800_register_read(rt2x00dev, offset, &reg);
1061 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
1062 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
1063 /*
1064 * Both the cipher as the BSS Idx numbers are split in a main
1065 * value of 3 bits, and a extended field for adding one additional
1066 * bit to the value.
1067 */
1068 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
1069 (crypto->cipher & 0x7));
1070 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
1071 (crypto->cipher & 0x8) >> 3);
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001072 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
1073 rt2800_register_write(rt2x00dev, offset, reg);
1074 } else {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001075 /* Delete the cipher without touching the bssidx */
1076 rt2800_register_read(rt2x00dev, offset, &reg);
1077 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB, 0);
1078 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER, 0);
1079 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT, 0);
1080 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, 0);
1081 rt2800_register_write(rt2x00dev, offset, reg);
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001082 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001083
1084 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
1085
1086 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
1087 if ((crypto->cipher == CIPHER_TKIP) ||
1088 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
1089 (crypto->cipher == CIPHER_AES))
1090 iveiv_entry.iv[3] |= 0x20;
1091 iveiv_entry.iv[3] |= key->keyidx << 6;
1092 rt2800_register_multiwrite(rt2x00dev, offset,
1093 &iveiv_entry, sizeof(iveiv_entry));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001094}
1095
1096int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
1097 struct rt2x00lib_crypto *crypto,
1098 struct ieee80211_key_conf *key)
1099{
1100 struct hw_key_entry key_entry;
1101 struct rt2x00_field32 field;
1102 u32 offset;
1103 u32 reg;
1104
1105 if (crypto->cmd == SET_KEY) {
1106 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
1107
1108 memcpy(key_entry.key, crypto->key,
1109 sizeof(key_entry.key));
1110 memcpy(key_entry.tx_mic, crypto->tx_mic,
1111 sizeof(key_entry.tx_mic));
1112 memcpy(key_entry.rx_mic, crypto->rx_mic,
1113 sizeof(key_entry.rx_mic));
1114
1115 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
1116 rt2800_register_multiwrite(rt2x00dev, offset,
1117 &key_entry, sizeof(key_entry));
1118 }
1119
1120 /*
1121 * The cipher types are stored over multiple registers
1122 * starting with SHARED_KEY_MODE_BASE each word will have
1123 * 32 bits and contains the cipher types for 2 bssidx each.
1124 * Using the correct defines correctly will cause overhead,
1125 * so just calculate the correct offset.
1126 */
1127 field.bit_offset = 4 * (key->hw_key_idx % 8);
1128 field.bit_mask = 0x7 << field.bit_offset;
1129
1130 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
1131
1132 rt2800_register_read(rt2x00dev, offset, &reg);
1133 rt2x00_set_field32(&reg, field,
1134 (crypto->cmd == SET_KEY) * crypto->cipher);
1135 rt2800_register_write(rt2x00dev, offset, reg);
1136
1137 /*
1138 * Update WCID information
1139 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001140 rt2800_config_wcid(rt2x00dev, crypto->address, key->hw_key_idx);
1141 rt2800_config_wcid_attr_bssidx(rt2x00dev, key->hw_key_idx,
1142 crypto->bssidx);
1143 rt2800_config_wcid_attr_cipher(rt2x00dev, crypto, key);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001144
1145 return 0;
1146}
1147EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
1148
Helmut Schaaa2b13282011-09-08 14:38:01 +02001149static inline int rt2800_find_wcid(struct rt2x00_dev *rt2x00dev)
Helmut Schaa1ed38112011-03-03 19:44:33 +01001150{
Helmut Schaaa2b13282011-09-08 14:38:01 +02001151 struct mac_wcid_entry wcid_entry;
Helmut Schaa1ed38112011-03-03 19:44:33 +01001152 int idx;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001153 u32 offset;
Helmut Schaa1ed38112011-03-03 19:44:33 +01001154
1155 /*
Helmut Schaaa2b13282011-09-08 14:38:01 +02001156 * Search for the first free WCID entry and return the corresponding
1157 * index.
Helmut Schaa1ed38112011-03-03 19:44:33 +01001158 *
1159 * Make sure the WCID starts _after_ the last possible shared key
1160 * entry (>32).
1161 *
1162 * Since parts of the pairwise key table might be shared with
1163 * the beacon frame buffers 6 & 7 we should only write into the
1164 * first 222 entries.
1165 */
1166 for (idx = 33; idx <= 222; idx++) {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001167 offset = MAC_WCID_ENTRY(idx);
1168 rt2800_register_multiread(rt2x00dev, offset, &wcid_entry,
1169 sizeof(wcid_entry));
1170 if (is_broadcast_ether_addr(wcid_entry.mac))
Helmut Schaa1ed38112011-03-03 19:44:33 +01001171 return idx;
1172 }
Helmut Schaaa2b13282011-09-08 14:38:01 +02001173
1174 /*
1175 * Use -1 to indicate that we don't have any more space in the WCID
1176 * table.
1177 */
Helmut Schaa1ed38112011-03-03 19:44:33 +01001178 return -1;
1179}
1180
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001181int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
1182 struct rt2x00lib_crypto *crypto,
1183 struct ieee80211_key_conf *key)
1184{
1185 struct hw_key_entry key_entry;
1186 u32 offset;
1187
1188 if (crypto->cmd == SET_KEY) {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001189 /*
1190 * Allow key configuration only for STAs that are
1191 * known by the hw.
1192 */
1193 if (crypto->wcid < 0)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001194 return -ENOSPC;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001195 key->hw_key_idx = crypto->wcid;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001196
1197 memcpy(key_entry.key, crypto->key,
1198 sizeof(key_entry.key));
1199 memcpy(key_entry.tx_mic, crypto->tx_mic,
1200 sizeof(key_entry.tx_mic));
1201 memcpy(key_entry.rx_mic, crypto->rx_mic,
1202 sizeof(key_entry.rx_mic));
1203
1204 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
1205 rt2800_register_multiwrite(rt2x00dev, offset,
1206 &key_entry, sizeof(key_entry));
1207 }
1208
1209 /*
1210 * Update WCID information
1211 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001212 rt2800_config_wcid_attr_cipher(rt2x00dev, crypto, key);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001213
1214 return 0;
1215}
1216EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
1217
Helmut Schaaa2b13282011-09-08 14:38:01 +02001218int rt2800_sta_add(struct rt2x00_dev *rt2x00dev, struct ieee80211_vif *vif,
1219 struct ieee80211_sta *sta)
1220{
1221 int wcid;
1222 struct rt2x00_sta *sta_priv = sta_to_rt2x00_sta(sta);
1223
1224 /*
1225 * Find next free WCID.
1226 */
1227 wcid = rt2800_find_wcid(rt2x00dev);
1228
1229 /*
1230 * Store selected wcid even if it is invalid so that we can
1231 * later decide if the STA is uploaded into the hw.
1232 */
1233 sta_priv->wcid = wcid;
1234
1235 /*
1236 * No space left in the device, however, we can still communicate
1237 * with the STA -> No error.
1238 */
1239 if (wcid < 0)
1240 return 0;
1241
1242 /*
1243 * Clean up WCID attributes and write STA address to the device.
1244 */
1245 rt2800_delete_wcid_attr(rt2x00dev, wcid);
1246 rt2800_config_wcid(rt2x00dev, sta->addr, wcid);
1247 rt2800_config_wcid_attr_bssidx(rt2x00dev, wcid,
1248 rt2x00lib_get_bssidx(rt2x00dev, vif));
1249 return 0;
1250}
1251EXPORT_SYMBOL_GPL(rt2800_sta_add);
1252
1253int rt2800_sta_remove(struct rt2x00_dev *rt2x00dev, int wcid)
1254{
1255 /*
1256 * Remove WCID entry, no need to clean the attributes as they will
1257 * get renewed when the WCID is reused.
1258 */
1259 rt2800_config_wcid(rt2x00dev, NULL, wcid);
1260
1261 return 0;
1262}
1263EXPORT_SYMBOL_GPL(rt2800_sta_remove);
1264
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001265void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
1266 const unsigned int filter_flags)
1267{
1268 u32 reg;
1269
1270 /*
1271 * Start configuration steps.
1272 * Note that the version error will always be dropped
1273 * and broadcast frames will always be accepted since
1274 * there is no filter for it at this time.
1275 */
1276 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
1277 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
1278 !(filter_flags & FIF_FCSFAIL));
1279 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
1280 !(filter_flags & FIF_PLCPFAIL));
1281 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
1282 !(filter_flags & FIF_PROMISC_IN_BSS));
1283 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
1284 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
1285 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
1286 !(filter_flags & FIF_ALLMULTI));
1287 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
1288 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
1289 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
1290 !(filter_flags & FIF_CONTROL));
1291 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
1292 !(filter_flags & FIF_CONTROL));
1293 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
1294 !(filter_flags & FIF_CONTROL));
1295 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
1296 !(filter_flags & FIF_CONTROL));
1297 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
1298 !(filter_flags & FIF_CONTROL));
1299 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
1300 !(filter_flags & FIF_PSPOLL));
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +01001301 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 0);
Helmut Schaa48839932011-11-24 09:13:26 +01001302 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR,
1303 !(filter_flags & FIF_CONTROL));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001304 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
1305 !(filter_flags & FIF_CONTROL));
1306 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
1307}
1308EXPORT_SYMBOL_GPL(rt2800_config_filter);
1309
1310void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
1311 struct rt2x00intf_conf *conf, const unsigned int flags)
1312{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001313 u32 reg;
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001314 bool update_bssid = false;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001315
1316 if (flags & CONFIG_UPDATE_TYPE) {
1317 /*
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001318 * Enable synchronisation.
1319 */
1320 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001321 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001322 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Helmut Schaa15a533c2011-04-18 15:28:04 +02001323
1324 if (conf->sync == TSF_SYNC_AP_NONE) {
1325 /*
1326 * Tune beacon queue transmit parameters for AP mode
1327 */
1328 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1329 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 0);
1330 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 1);
1331 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1332 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 0);
1333 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1334 } else {
1335 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1336 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 4);
1337 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 2);
1338 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1339 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 16);
1340 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1341 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001342 }
1343
1344 if (flags & CONFIG_UPDATE_MAC) {
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001345 if (flags & CONFIG_UPDATE_TYPE &&
1346 conf->sync == TSF_SYNC_AP_NONE) {
1347 /*
1348 * The BSSID register has to be set to our own mac
1349 * address in AP mode.
1350 */
1351 memcpy(conf->bssid, conf->mac, sizeof(conf->mac));
1352 update_bssid = true;
1353 }
1354
Ivo van Doornc600c822010-08-30 21:14:15 +02001355 if (!is_zero_ether_addr((const u8 *)conf->mac)) {
1356 reg = le32_to_cpu(conf->mac[1]);
1357 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
1358 conf->mac[1] = cpu_to_le32(reg);
1359 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001360
1361 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
1362 conf->mac, sizeof(conf->mac));
1363 }
1364
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001365 if ((flags & CONFIG_UPDATE_BSSID) || update_bssid) {
Ivo van Doornc600c822010-08-30 21:14:15 +02001366 if (!is_zero_ether_addr((const u8 *)conf->bssid)) {
1367 reg = le32_to_cpu(conf->bssid[1]);
1368 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
1369 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 7);
1370 conf->bssid[1] = cpu_to_le32(reg);
1371 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001372
1373 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
1374 conf->bssid, sizeof(conf->bssid));
1375 }
1376}
1377EXPORT_SYMBOL_GPL(rt2800_config_intf);
1378
Helmut Schaa87c19152010-10-02 11:28:34 +02001379static void rt2800_config_ht_opmode(struct rt2x00_dev *rt2x00dev,
1380 struct rt2x00lib_erp *erp)
1381{
1382 bool any_sta_nongf = !!(erp->ht_opmode &
1383 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1384 u8 protection = erp->ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION;
1385 u8 mm20_mode, mm40_mode, gf20_mode, gf40_mode;
1386 u16 mm20_rate, mm40_rate, gf20_rate, gf40_rate;
1387 u32 reg;
1388
1389 /* default protection rate for HT20: OFDM 24M */
1390 mm20_rate = gf20_rate = 0x4004;
1391
1392 /* default protection rate for HT40: duplicate OFDM 24M */
1393 mm40_rate = gf40_rate = 0x4084;
1394
1395 switch (protection) {
1396 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
1397 /*
1398 * All STAs in this BSS are HT20/40 but there might be
1399 * STAs not supporting greenfield mode.
1400 * => Disable protection for HT transmissions.
1401 */
1402 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 0;
1403
1404 break;
1405 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
1406 /*
1407 * All STAs in this BSS are HT20 or HT20/40 but there
1408 * might be STAs not supporting greenfield mode.
1409 * => Protect all HT40 transmissions.
1410 */
1411 mm20_mode = gf20_mode = 0;
1412 mm40_mode = gf40_mode = 2;
1413
1414 break;
1415 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
1416 /*
1417 * Nonmember protection:
1418 * According to 802.11n we _should_ protect all
1419 * HT transmissions (but we don't have to).
1420 *
1421 * But if cts_protection is enabled we _shall_ protect
1422 * all HT transmissions using a CCK rate.
1423 *
1424 * And if any station is non GF we _shall_ protect
1425 * GF transmissions.
1426 *
1427 * We decide to protect everything
1428 * -> fall through to mixed mode.
1429 */
1430 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
1431 /*
1432 * Legacy STAs are present
1433 * => Protect all HT transmissions.
1434 */
1435 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 2;
1436
1437 /*
1438 * If erp protection is needed we have to protect HT
1439 * transmissions with CCK 11M long preamble.
1440 */
1441 if (erp->cts_protection) {
1442 /* don't duplicate RTS/CTS in CCK mode */
1443 mm20_rate = mm40_rate = 0x0003;
1444 gf20_rate = gf40_rate = 0x0003;
1445 }
1446 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001447 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001448
1449 /* check for STAs not supporting greenfield mode */
1450 if (any_sta_nongf)
1451 gf20_mode = gf40_mode = 2;
1452
1453 /* Update HT protection config */
1454 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1455 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, mm20_rate);
1456 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, mm20_mode);
1457 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1458
1459 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1460 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, mm40_rate);
1461 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, mm40_mode);
1462 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1463
1464 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1465 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, gf20_rate);
1466 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, gf20_mode);
1467 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1468
1469 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1470 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, gf40_rate);
1471 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, gf40_mode);
1472 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1473}
1474
Helmut Schaa02044642010-09-08 20:56:32 +02001475void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp,
1476 u32 changed)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001477{
1478 u32 reg;
1479
Helmut Schaa02044642010-09-08 20:56:32 +02001480 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1481 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1482 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
1483 !!erp->short_preamble);
1484 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
1485 !!erp->short_preamble);
1486 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1487 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001488
Helmut Schaa02044642010-09-08 20:56:32 +02001489 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1490 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
1491 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
1492 erp->cts_protection ? 2 : 0);
1493 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1494 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001495
Helmut Schaa02044642010-09-08 20:56:32 +02001496 if (changed & BSS_CHANGED_BASIC_RATES) {
1497 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
1498 erp->basic_rates);
1499 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1500 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001501
Helmut Schaa02044642010-09-08 20:56:32 +02001502 if (changed & BSS_CHANGED_ERP_SLOT) {
1503 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1504 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME,
1505 erp->slot_time);
1506 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001507
Helmut Schaa02044642010-09-08 20:56:32 +02001508 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
1509 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
1510 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1511 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001512
Helmut Schaa02044642010-09-08 20:56:32 +02001513 if (changed & BSS_CHANGED_BEACON_INT) {
1514 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1515 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
1516 erp->beacon_int * 16);
1517 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1518 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001519
1520 if (changed & BSS_CHANGED_HT)
1521 rt2800_config_ht_opmode(rt2x00dev, erp);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001522}
1523EXPORT_SYMBOL_GPL(rt2800_config_erp);
1524
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001525static void rt2800_config_3572bt_ant(struct rt2x00_dev *rt2x00dev)
1526{
1527 u32 reg;
1528 u16 eeprom;
1529 u8 led_ctrl, led_g_mode, led_r_mode;
1530
1531 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
1532 if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
1533 rt2x00_set_field32(&reg, GPIO_SWITCH_0, 1);
1534 rt2x00_set_field32(&reg, GPIO_SWITCH_1, 1);
1535 } else {
1536 rt2x00_set_field32(&reg, GPIO_SWITCH_0, 0);
1537 rt2x00_set_field32(&reg, GPIO_SWITCH_1, 0);
1538 }
1539 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
1540
1541 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1542 led_g_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 3 : 0;
1543 led_r_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 0 : 3;
1544 if (led_g_mode != rt2x00_get_field32(reg, LED_CFG_G_LED_MODE) ||
1545 led_r_mode != rt2x00_get_field32(reg, LED_CFG_R_LED_MODE)) {
1546 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
1547 led_ctrl = rt2x00_get_field16(eeprom, EEPROM_FREQ_LED_MODE);
1548 if (led_ctrl == 0 || led_ctrl > 0x40) {
1549 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, led_g_mode);
1550 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, led_r_mode);
1551 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1552 } else {
1553 rt2800_mcu_request(rt2x00dev, MCU_BAND_SELECT, 0xff,
1554 (led_g_mode << 2) | led_r_mode, 1);
1555 }
1556 }
1557}
1558
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001559static void rt2800_set_ant_diversity(struct rt2x00_dev *rt2x00dev,
1560 enum antenna ant)
1561{
1562 u32 reg;
1563 u8 eesk_pin = (ant == ANTENNA_A) ? 1 : 0;
1564 u8 gpio_bit3 = (ant == ANTENNA_A) ? 0 : 1;
1565
1566 if (rt2x00_is_pci(rt2x00dev)) {
1567 rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
1568 rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK, eesk_pin);
1569 rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
1570 } else if (rt2x00_is_usb(rt2x00dev))
1571 rt2800_mcu_request(rt2x00dev, MCU_ANT_SELECT, 0xff,
1572 eesk_pin, 0);
1573
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02001574 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
1575 rt2x00_set_field32(&reg, GPIO_CTRL_DIR3, 0);
1576 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, gpio_bit3);
1577 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001578}
1579
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001580void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
1581{
1582 u8 r1;
1583 u8 r3;
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001584 u16 eeprom;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001585
1586 rt2800_bbp_read(rt2x00dev, 1, &r1);
1587 rt2800_bbp_read(rt2x00dev, 3, &r3);
1588
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001589 if (rt2x00_rt(rt2x00dev, RT3572) &&
1590 test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
1591 rt2800_config_3572bt_ant(rt2x00dev);
1592
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001593 /*
1594 * Configure the TX antenna.
1595 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001596 switch (ant->tx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001597 case 1:
1598 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001599 break;
1600 case 2:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001601 if (rt2x00_rt(rt2x00dev, RT3572) &&
1602 test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
1603 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 1);
1604 else
1605 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001606 break;
1607 case 3:
Ivo van Doorne22557f2010-06-29 21:49:05 +02001608 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001609 break;
1610 }
1611
1612 /*
1613 * Configure the RX antenna.
1614 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001615 switch (ant->rx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001616 case 1:
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001617 if (rt2x00_rt(rt2x00dev, RT3070) ||
1618 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03001619 rt2x00_rt(rt2x00dev, RT3352) ||
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001620 rt2x00_rt(rt2x00dev, RT3390)) {
1621 rt2x00_eeprom_read(rt2x00dev,
1622 EEPROM_NIC_CONF1, &eeprom);
1623 if (rt2x00_get_field16(eeprom,
1624 EEPROM_NIC_CONF1_ANT_DIVERSITY))
1625 rt2800_set_ant_diversity(rt2x00dev,
1626 rt2x00dev->default_ant.rx);
1627 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001628 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
1629 break;
1630 case 2:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001631 if (rt2x00_rt(rt2x00dev, RT3572) &&
1632 test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
1633 rt2x00_set_field8(&r3, BBP3_RX_ADC, 1);
1634 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA,
1635 rt2x00dev->curr_band == IEEE80211_BAND_5GHZ);
1636 rt2800_set_ant_diversity(rt2x00dev, ANTENNA_B);
1637 } else {
1638 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
1639 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001640 break;
1641 case 3:
1642 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
1643 break;
1644 }
1645
1646 rt2800_bbp_write(rt2x00dev, 3, r3);
1647 rt2800_bbp_write(rt2x00dev, 1, r1);
1648}
1649EXPORT_SYMBOL_GPL(rt2800_config_ant);
1650
1651static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
1652 struct rt2x00lib_conf *libconf)
1653{
1654 u16 eeprom;
1655 short lna_gain;
1656
1657 if (libconf->rf.channel <= 14) {
1658 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1659 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
1660 } else if (libconf->rf.channel <= 64) {
1661 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1662 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
1663 } else if (libconf->rf.channel <= 128) {
1664 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
1665 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
1666 } else {
1667 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
1668 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
1669 }
1670
1671 rt2x00dev->lna_gain = lna_gain;
1672}
1673
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001674static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
1675 struct ieee80211_conf *conf,
1676 struct rf_channel *rf,
1677 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001678{
1679 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
1680
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001681 if (rt2x00dev->default_ant.tx_chain_num == 1)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001682 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
1683
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001684 if (rt2x00dev->default_ant.rx_chain_num == 1) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001685 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
1686 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001687 } else if (rt2x00dev->default_ant.rx_chain_num == 2)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001688 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1689
1690 if (rf->channel > 14) {
1691 /*
1692 * When TX power is below 0, we should increase it by 7 to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001693 * make it a positive value (Minimum value is -7).
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001694 * However this means that values between 0 and 7 have
1695 * double meaning, and we should set a 7DBm boost flag.
1696 */
1697 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001698 (info->default_power1 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001699
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001700 if (info->default_power1 < 0)
1701 info->default_power1 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001702
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001703 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001704
1705 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001706 (info->default_power2 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001707
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001708 if (info->default_power2 < 0)
1709 info->default_power2 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001710
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001711 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001712 } else {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001713 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G, info->default_power1);
1714 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001715 }
1716
1717 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
1718
1719 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1720 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1721 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1722 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1723
1724 udelay(200);
1725
1726 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1727 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1728 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
1729 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1730
1731 udelay(200);
1732
1733 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1734 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1735 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1736 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1737}
1738
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001739static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
1740 struct ieee80211_conf *conf,
1741 struct rf_channel *rf,
1742 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001743{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01001744 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01001745 u8 rfcsr, calib_tx, calib_rx;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001746
1747 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Stanislaw Gruszka7f4666a2012-01-30 16:17:56 +01001748
1749 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
1750 rt2x00_set_field8(&rfcsr, RFCSR3_K, rf->rf3);
1751 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001752
1753 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +02001754 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001755 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1756
1757 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001758 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001759 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
1760
Helmut Schaa5a673962010-04-23 15:54:43 +02001761 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001762 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER, info->default_power2);
Helmut Schaa5a673962010-04-23 15:54:43 +02001763 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
1764
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01001765 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
1766 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
Gertjan van Wingerde7ad63032012-09-16 22:29:53 +02001767 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD,
1768 rt2x00dev->default_ant.rx_chain_num <= 1);
1769 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD,
1770 rt2x00dev->default_ant.rx_chain_num <= 2);
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01001771 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
Gertjan van Wingerde7ad63032012-09-16 22:29:53 +02001772 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD,
1773 rt2x00dev->default_ant.tx_chain_num <= 1);
1774 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD,
1775 rt2x00dev->default_ant.tx_chain_num <= 2);
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01001776 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
1777
Stanislaw Gruszka3e0c7642012-01-30 16:17:58 +01001778 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1779 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1780 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1781 msleep(1);
1782 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1783 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1784
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001785 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
1786 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
1787 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
1788
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01001789 if (rt2x00_rt(rt2x00dev, RT3390)) {
1790 calib_tx = conf_is_ht40(conf) ? 0x68 : 0x4f;
1791 calib_rx = conf_is_ht40(conf) ? 0x6f : 0x4f;
1792 } else {
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01001793 if (conf_is_ht40(conf)) {
1794 calib_tx = drv_data->calibration_bw40;
1795 calib_rx = drv_data->calibration_bw40;
1796 } else {
1797 calib_tx = drv_data->calibration_bw20;
1798 calib_rx = drv_data->calibration_bw20;
1799 }
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01001800 }
1801
1802 rt2800_rfcsr_read(rt2x00dev, 24, &rfcsr);
1803 rt2x00_set_field8(&rfcsr, RFCSR24_TX_CALIB, calib_tx);
1804 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr);
1805
1806 rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
1807 rt2x00_set_field8(&rfcsr, RFCSR31_RX_CALIB, calib_rx);
1808 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001809
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001810 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001811 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001812 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Stanislaw Gruszka3e0c7642012-01-30 16:17:58 +01001813
1814 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1815 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1816 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1817 msleep(1);
1818 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1819 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001820}
1821
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001822static void rt2800_config_channel_rf3052(struct rt2x00_dev *rt2x00dev,
1823 struct ieee80211_conf *conf,
1824 struct rf_channel *rf,
1825 struct channel_info *info)
1826{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01001827 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001828 u8 rfcsr;
1829 u32 reg;
1830
1831 if (rf->channel <= 14) {
Gertjan van Wingerde5d137df2012-02-06 23:45:09 +01001832 rt2800_bbp_write(rt2x00dev, 25, drv_data->bbp25);
1833 rt2800_bbp_write(rt2x00dev, 26, drv_data->bbp26);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001834 } else {
1835 rt2800_bbp_write(rt2x00dev, 25, 0x09);
1836 rt2800_bbp_write(rt2x00dev, 26, 0xff);
1837 }
1838
1839 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
1840 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
1841
1842 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
1843 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
1844 if (rf->channel <= 14)
1845 rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 2);
1846 else
1847 rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 1);
1848 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1849
1850 rt2800_rfcsr_read(rt2x00dev, 5, &rfcsr);
1851 if (rf->channel <= 14)
1852 rt2x00_set_field8(&rfcsr, RFCSR5_R1, 1);
1853 else
1854 rt2x00_set_field8(&rfcsr, RFCSR5_R1, 2);
1855 rt2800_rfcsr_write(rt2x00dev, 5, rfcsr);
1856
1857 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
1858 if (rf->channel <= 14) {
1859 rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 3);
1860 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
Gertjan van Wingerde569ffa52012-02-06 23:45:10 +01001861 info->default_power1);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001862 } else {
1863 rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 7);
1864 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
1865 (info->default_power1 & 0x3) |
1866 ((info->default_power1 & 0xC) << 1));
1867 }
1868 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
1869
1870 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
1871 if (rf->channel <= 14) {
1872 rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 3);
1873 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
Gertjan van Wingerde569ffa52012-02-06 23:45:10 +01001874 info->default_power2);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001875 } else {
1876 rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 7);
1877 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
1878 (info->default_power2 & 0x3) |
1879 ((info->default_power2 & 0xC) << 1));
1880 }
1881 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
1882
1883 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001884 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
1885 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
1886 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
1887 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
Gertjan van Wingerde0cd461e2012-02-06 23:45:11 +01001888 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
1889 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001890 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
1891 if (rf->channel <= 14) {
1892 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
1893 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
1894 }
1895 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
1896 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
1897 } else {
1898 switch (rt2x00dev->default_ant.tx_chain_num) {
1899 case 1:
1900 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
1901 case 2:
1902 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
1903 break;
1904 }
1905
1906 switch (rt2x00dev->default_ant.rx_chain_num) {
1907 case 1:
1908 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
1909 case 2:
1910 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
1911 break;
1912 }
1913 }
1914 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
1915
1916 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
1917 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
1918 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
1919
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01001920 if (conf_is_ht40(conf)) {
1921 rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw40);
1922 rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw40);
1923 } else {
1924 rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw20);
1925 rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw20);
1926 }
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001927
1928 if (rf->channel <= 14) {
1929 rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
1930 rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
1931 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
1932 rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
1933 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01001934 rfcsr = 0x4c;
1935 rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
1936 drv_data->txmixer_gain_24g);
1937 rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001938 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
1939 rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
1940 rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
1941 rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
1942 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
1943 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
1944 rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
1945 } else {
Gertjan van Wingerde58b8ae12012-02-06 23:45:12 +01001946 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
1947 rt2x00_set_field8(&rfcsr, RFCSR7_BIT2, 1);
1948 rt2x00_set_field8(&rfcsr, RFCSR7_BIT3, 0);
1949 rt2x00_set_field8(&rfcsr, RFCSR7_BIT4, 1);
1950 rt2x00_set_field8(&rfcsr, RFCSR7_BITS67, 0);
1951 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001952 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
1953 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
1954 rt2800_rfcsr_write(rt2x00dev, 11, 0x00);
1955 rt2800_rfcsr_write(rt2x00dev, 15, 0x43);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01001956 rfcsr = 0x7a;
1957 rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
1958 drv_data->txmixer_gain_5g);
1959 rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001960 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
1961 if (rf->channel <= 64) {
1962 rt2800_rfcsr_write(rt2x00dev, 19, 0xb7);
1963 rt2800_rfcsr_write(rt2x00dev, 20, 0xf6);
1964 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
1965 } else if (rf->channel <= 128) {
1966 rt2800_rfcsr_write(rt2x00dev, 19, 0x74);
1967 rt2800_rfcsr_write(rt2x00dev, 20, 0xf4);
1968 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
1969 } else {
1970 rt2800_rfcsr_write(rt2x00dev, 19, 0x72);
1971 rt2800_rfcsr_write(rt2x00dev, 20, 0xf3);
1972 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
1973 }
1974 rt2800_rfcsr_write(rt2x00dev, 26, 0x87);
1975 rt2800_rfcsr_write(rt2x00dev, 27, 0x01);
1976 rt2800_rfcsr_write(rt2x00dev, 29, 0x9f);
1977 }
1978
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02001979 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
1980 rt2x00_set_field32(&reg, GPIO_CTRL_DIR7, 0);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001981 if (rf->channel <= 14)
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02001982 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 1);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001983 else
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02001984 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 0);
1985 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001986
1987 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
1988 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
1989 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
1990}
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001991
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02001992#define POWER_BOUND 0x27
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01001993#define POWER_BOUND_5G 0x2b
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02001994#define FREQ_OFFSET_BOUND 0x5f
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001995
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01001996static void rt2800_adjust_freq_offset(struct rt2x00_dev *rt2x00dev)
1997{
1998 u8 rfcsr;
1999
2000 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
2001 if (rt2x00dev->freq_offset > FREQ_OFFSET_BOUND)
2002 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, FREQ_OFFSET_BOUND);
2003 else
2004 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, rt2x00dev->freq_offset);
2005 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
2006}
2007
Woody Hunga89534e2012-06-13 15:01:16 +08002008static void rt2800_config_channel_rf3290(struct rt2x00_dev *rt2x00dev,
2009 struct ieee80211_conf *conf,
2010 struct rf_channel *rf,
2011 struct channel_info *info)
2012{
2013 u8 rfcsr;
2014
2015 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2016 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2017 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2018 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
2019 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2020
2021 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002022 if (info->default_power1 > POWER_BOUND)
2023 rt2x00_set_field8(&rfcsr, RFCSR49_TX, POWER_BOUND);
Woody Hunga89534e2012-06-13 15:01:16 +08002024 else
2025 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2026 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
2027
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002028 rt2800_adjust_freq_offset(rt2x00dev);
Woody Hunga89534e2012-06-13 15:01:16 +08002029
2030 if (rf->channel <= 14) {
2031 if (rf->channel == 6)
2032 rt2800_bbp_write(rt2x00dev, 68, 0x0c);
2033 else
2034 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
2035
2036 if (rf->channel >= 1 && rf->channel <= 6)
2037 rt2800_bbp_write(rt2x00dev, 59, 0x0f);
2038 else if (rf->channel >= 7 && rf->channel <= 11)
2039 rt2800_bbp_write(rt2x00dev, 59, 0x0e);
2040 else if (rf->channel >= 12 && rf->channel <= 14)
2041 rt2800_bbp_write(rt2x00dev, 59, 0x0d);
2042 }
2043}
2044
Daniel Golle03839952012-09-09 14:24:39 +03002045static void rt2800_config_channel_rf3322(struct rt2x00_dev *rt2x00dev,
2046 struct ieee80211_conf *conf,
2047 struct rf_channel *rf,
2048 struct channel_info *info)
2049{
2050 u8 rfcsr;
2051
2052 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2053 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2054
2055 rt2800_rfcsr_write(rt2x00dev, 11, 0x42);
2056 rt2800_rfcsr_write(rt2x00dev, 12, 0x1c);
2057 rt2800_rfcsr_write(rt2x00dev, 13, 0x00);
2058
2059 if (info->default_power1 > POWER_BOUND)
2060 rt2800_rfcsr_write(rt2x00dev, 47, POWER_BOUND);
2061 else
2062 rt2800_rfcsr_write(rt2x00dev, 47, info->default_power1);
2063
2064 if (info->default_power2 > POWER_BOUND)
2065 rt2800_rfcsr_write(rt2x00dev, 48, POWER_BOUND);
2066 else
2067 rt2800_rfcsr_write(rt2x00dev, 48, info->default_power2);
2068
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002069 rt2800_adjust_freq_offset(rt2x00dev);
Daniel Golle03839952012-09-09 14:24:39 +03002070
2071 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2072 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2073 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2074
2075 if ( rt2x00dev->default_ant.tx_chain_num == 2 )
2076 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2077 else
2078 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
2079
2080 if ( rt2x00dev->default_ant.rx_chain_num == 2 )
2081 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2082 else
2083 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
2084
2085 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2086 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
2087
2088 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2089
2090 rt2800_rfcsr_write(rt2x00dev, 31, 80);
2091}
2092
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002093static void rt2800_config_channel_rf53xx(struct rt2x00_dev *rt2x00dev,
Gabor Juhosadde5882011-03-03 11:46:45 +01002094 struct ieee80211_conf *conf,
2095 struct rf_channel *rf,
2096 struct channel_info *info)
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002097{
Gabor Juhosadde5882011-03-03 11:46:45 +01002098 u8 rfcsr;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002099
Gabor Juhosadde5882011-03-03 11:46:45 +01002100 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2101 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2102 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2103 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
2104 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002105
Gabor Juhosadde5882011-03-03 11:46:45 +01002106 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002107 if (info->default_power1 > POWER_BOUND)
2108 rt2x00_set_field8(&rfcsr, RFCSR49_TX, POWER_BOUND);
Gabor Juhosadde5882011-03-03 11:46:45 +01002109 else
2110 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2111 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002112
Zero.Lincff3d1f2012-05-29 16:11:09 +08002113 if (rt2x00_rt(rt2x00dev, RT5392)) {
2114 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002115 if (info->default_power1 > POWER_BOUND)
2116 rt2x00_set_field8(&rfcsr, RFCSR50_TX, POWER_BOUND);
Zero.Lincff3d1f2012-05-29 16:11:09 +08002117 else
2118 rt2x00_set_field8(&rfcsr, RFCSR50_TX,
2119 info->default_power2);
2120 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
2121 }
2122
Gabor Juhosadde5882011-03-03 11:46:45 +01002123 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
Zero.Lincff3d1f2012-05-29 16:11:09 +08002124 if (rt2x00_rt(rt2x00dev, RT5392)) {
2125 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2126 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2127 }
Gabor Juhosadde5882011-03-03 11:46:45 +01002128 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2129 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
2130 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2131 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2132 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002133
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002134 rt2800_adjust_freq_offset(rt2x00dev);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002135
Gabor Juhosadde5882011-03-03 11:46:45 +01002136 if (rf->channel <= 14) {
2137 int idx = rf->channel-1;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002138
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02002139 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002140 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
2141 /* r55/r59 value array of channel 1~14 */
2142 static const char r55_bt_rev[] = {0x83, 0x83,
2143 0x83, 0x73, 0x73, 0x63, 0x53, 0x53,
2144 0x53, 0x43, 0x43, 0x43, 0x43, 0x43};
2145 static const char r59_bt_rev[] = {0x0e, 0x0e,
2146 0x0e, 0x0e, 0x0e, 0x0b, 0x0a, 0x09,
2147 0x07, 0x07, 0x07, 0x07, 0x07, 0x07};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002148
Gabor Juhosadde5882011-03-03 11:46:45 +01002149 rt2800_rfcsr_write(rt2x00dev, 55,
2150 r55_bt_rev[idx]);
2151 rt2800_rfcsr_write(rt2x00dev, 59,
2152 r59_bt_rev[idx]);
2153 } else {
2154 static const char r59_bt[] = {0x8b, 0x8b, 0x8b,
2155 0x8b, 0x8b, 0x8b, 0x8b, 0x8a, 0x89,
2156 0x88, 0x88, 0x86, 0x85, 0x84};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002157
Gabor Juhosadde5882011-03-03 11:46:45 +01002158 rt2800_rfcsr_write(rt2x00dev, 59, r59_bt[idx]);
2159 }
2160 } else {
2161 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
2162 static const char r55_nonbt_rev[] = {0x23, 0x23,
2163 0x23, 0x23, 0x13, 0x13, 0x03, 0x03,
2164 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
2165 static const char r59_nonbt_rev[] = {0x07, 0x07,
2166 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
2167 0x07, 0x07, 0x06, 0x05, 0x04, 0x04};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002168
Gabor Juhosadde5882011-03-03 11:46:45 +01002169 rt2800_rfcsr_write(rt2x00dev, 55,
2170 r55_nonbt_rev[idx]);
2171 rt2800_rfcsr_write(rt2x00dev, 59,
2172 r59_nonbt_rev[idx]);
John Li2ed71882012-02-17 17:33:06 +08002173 } else if (rt2x00_rt(rt2x00dev, RT5390) ||
Gabor Juhose6d227b2012-12-02 15:53:28 +01002174 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002175 static const char r59_non_bt[] = {0x8f, 0x8f,
2176 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8d,
2177 0x8a, 0x88, 0x88, 0x87, 0x87, 0x86};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002178
Gabor Juhosadde5882011-03-03 11:46:45 +01002179 rt2800_rfcsr_write(rt2x00dev, 59,
2180 r59_non_bt[idx]);
2181 }
2182 }
2183 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002184}
2185
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002186static void rt2800_config_channel_rf55xx(struct rt2x00_dev *rt2x00dev,
2187 struct ieee80211_conf *conf,
2188 struct rf_channel *rf,
2189 struct channel_info *info)
2190{
2191 u8 rfcsr, ep_reg;
Stanislaw Gruszkad5ae7a62013-03-16 19:19:42 +01002192 u32 reg;
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002193 int power_bound;
2194
2195 /* TODO */
2196 const bool is_11b = false;
2197 const bool is_type_ep = false;
2198
Stanislaw Gruszkad5ae7a62013-03-16 19:19:42 +01002199 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
2200 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL,
2201 (rf->channel > 14 || conf_is_ht40(conf)) ? 5 : 0);
2202 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002203
2204 /* Order of values on rf_channel entry: N, K, mod, R */
2205 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1 & 0xff);
2206
2207 rt2800_rfcsr_read(rt2x00dev, 9, &rfcsr);
2208 rt2x00_set_field8(&rfcsr, RFCSR9_K, rf->rf2 & 0xf);
2209 rt2x00_set_field8(&rfcsr, RFCSR9_N, (rf->rf1 & 0x100) >> 8);
2210 rt2x00_set_field8(&rfcsr, RFCSR9_MOD, ((rf->rf3 - 8) & 0x4) >> 2);
2211 rt2800_rfcsr_write(rt2x00dev, 9, rfcsr);
2212
2213 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2214 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf4 - 1);
2215 rt2x00_set_field8(&rfcsr, RFCSR11_MOD, (rf->rf3 - 8) & 0x3);
2216 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2217
2218 if (rf->channel <= 14) {
2219 rt2800_rfcsr_write(rt2x00dev, 10, 0x90);
2220 /* FIXME: RF11 owerwrite ? */
2221 rt2800_rfcsr_write(rt2x00dev, 11, 0x4A);
2222 rt2800_rfcsr_write(rt2x00dev, 12, 0x52);
2223 rt2800_rfcsr_write(rt2x00dev, 13, 0x42);
2224 rt2800_rfcsr_write(rt2x00dev, 22, 0x40);
2225 rt2800_rfcsr_write(rt2x00dev, 24, 0x4A);
2226 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
2227 rt2800_rfcsr_write(rt2x00dev, 27, 0x42);
2228 rt2800_rfcsr_write(rt2x00dev, 36, 0x80);
2229 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
2230 rt2800_rfcsr_write(rt2x00dev, 38, 0x89);
2231 rt2800_rfcsr_write(rt2x00dev, 39, 0x1B);
2232 rt2800_rfcsr_write(rt2x00dev, 40, 0x0D);
2233 rt2800_rfcsr_write(rt2x00dev, 41, 0x9B);
2234 rt2800_rfcsr_write(rt2x00dev, 42, 0xD5);
2235 rt2800_rfcsr_write(rt2x00dev, 43, 0x72);
2236 rt2800_rfcsr_write(rt2x00dev, 44, 0x0E);
2237 rt2800_rfcsr_write(rt2x00dev, 45, 0xA2);
2238 rt2800_rfcsr_write(rt2x00dev, 46, 0x6B);
2239 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
2240 rt2800_rfcsr_write(rt2x00dev, 51, 0x3E);
2241 rt2800_rfcsr_write(rt2x00dev, 52, 0x48);
2242 rt2800_rfcsr_write(rt2x00dev, 54, 0x38);
2243 rt2800_rfcsr_write(rt2x00dev, 56, 0xA1);
2244 rt2800_rfcsr_write(rt2x00dev, 57, 0x00);
2245 rt2800_rfcsr_write(rt2x00dev, 58, 0x39);
2246 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
2247 rt2800_rfcsr_write(rt2x00dev, 61, 0x91);
2248 rt2800_rfcsr_write(rt2x00dev, 62, 0x39);
2249
2250 /* TODO RF27 <- tssi */
2251
2252 rfcsr = rf->channel <= 10 ? 0x07 : 0x06;
2253 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
2254 rt2800_rfcsr_write(rt2x00dev, 59, rfcsr);
2255
2256 if (is_11b) {
2257 /* CCK */
2258 rt2800_rfcsr_write(rt2x00dev, 31, 0xF8);
2259 rt2800_rfcsr_write(rt2x00dev, 32, 0xC0);
2260 if (is_type_ep)
2261 rt2800_rfcsr_write(rt2x00dev, 55, 0x06);
2262 else
2263 rt2800_rfcsr_write(rt2x00dev, 55, 0x47);
2264 } else {
2265 /* OFDM */
2266 if (is_type_ep)
2267 rt2800_rfcsr_write(rt2x00dev, 55, 0x03);
2268 else
2269 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
2270 }
2271
2272 power_bound = POWER_BOUND;
2273 ep_reg = 0x2;
2274 } else {
2275 rt2800_rfcsr_write(rt2x00dev, 10, 0x97);
2276 /* FIMXE: RF11 overwrite */
2277 rt2800_rfcsr_write(rt2x00dev, 11, 0x40);
2278 rt2800_rfcsr_write(rt2x00dev, 25, 0xBF);
2279 rt2800_rfcsr_write(rt2x00dev, 27, 0x42);
2280 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
2281 rt2800_rfcsr_write(rt2x00dev, 37, 0x04);
2282 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
2283 rt2800_rfcsr_write(rt2x00dev, 40, 0x42);
2284 rt2800_rfcsr_write(rt2x00dev, 41, 0xBB);
2285 rt2800_rfcsr_write(rt2x00dev, 42, 0xD7);
2286 rt2800_rfcsr_write(rt2x00dev, 45, 0x41);
2287 rt2800_rfcsr_write(rt2x00dev, 48, 0x00);
2288 rt2800_rfcsr_write(rt2x00dev, 57, 0x77);
2289 rt2800_rfcsr_write(rt2x00dev, 60, 0x05);
2290 rt2800_rfcsr_write(rt2x00dev, 61, 0x01);
2291
2292 /* TODO RF27 <- tssi */
2293
2294 if (rf->channel >= 36 && rf->channel <= 64) {
2295
2296 rt2800_rfcsr_write(rt2x00dev, 12, 0x2E);
2297 rt2800_rfcsr_write(rt2x00dev, 13, 0x22);
2298 rt2800_rfcsr_write(rt2x00dev, 22, 0x60);
2299 rt2800_rfcsr_write(rt2x00dev, 23, 0x7F);
2300 if (rf->channel <= 50)
2301 rt2800_rfcsr_write(rt2x00dev, 24, 0x09);
2302 else if (rf->channel >= 52)
2303 rt2800_rfcsr_write(rt2x00dev, 24, 0x07);
2304 rt2800_rfcsr_write(rt2x00dev, 39, 0x1C);
2305 rt2800_rfcsr_write(rt2x00dev, 43, 0x5B);
2306 rt2800_rfcsr_write(rt2x00dev, 44, 0X40);
2307 rt2800_rfcsr_write(rt2x00dev, 46, 0X00);
2308 rt2800_rfcsr_write(rt2x00dev, 51, 0xFE);
2309 rt2800_rfcsr_write(rt2x00dev, 52, 0x0C);
2310 rt2800_rfcsr_write(rt2x00dev, 54, 0xF8);
2311 if (rf->channel <= 50) {
2312 rt2800_rfcsr_write(rt2x00dev, 55, 0x06),
2313 rt2800_rfcsr_write(rt2x00dev, 56, 0xD3);
2314 } else if (rf->channel >= 52) {
2315 rt2800_rfcsr_write(rt2x00dev, 55, 0x04);
2316 rt2800_rfcsr_write(rt2x00dev, 56, 0xBB);
2317 }
2318
2319 rt2800_rfcsr_write(rt2x00dev, 58, 0x15);
2320 rt2800_rfcsr_write(rt2x00dev, 59, 0x7F);
2321 rt2800_rfcsr_write(rt2x00dev, 62, 0x15);
2322
2323 } else if (rf->channel >= 100 && rf->channel <= 165) {
2324
2325 rt2800_rfcsr_write(rt2x00dev, 12, 0x0E);
2326 rt2800_rfcsr_write(rt2x00dev, 13, 0x42);
2327 rt2800_rfcsr_write(rt2x00dev, 22, 0x40);
2328 if (rf->channel <= 153) {
2329 rt2800_rfcsr_write(rt2x00dev, 23, 0x3C);
2330 rt2800_rfcsr_write(rt2x00dev, 24, 0x06);
2331 } else if (rf->channel >= 155) {
2332 rt2800_rfcsr_write(rt2x00dev, 23, 0x38);
2333 rt2800_rfcsr_write(rt2x00dev, 24, 0x05);
2334 }
2335 if (rf->channel <= 138) {
2336 rt2800_rfcsr_write(rt2x00dev, 39, 0x1A);
2337 rt2800_rfcsr_write(rt2x00dev, 43, 0x3B);
2338 rt2800_rfcsr_write(rt2x00dev, 44, 0x20);
2339 rt2800_rfcsr_write(rt2x00dev, 46, 0x18);
2340 } else if (rf->channel >= 140) {
2341 rt2800_rfcsr_write(rt2x00dev, 39, 0x18);
2342 rt2800_rfcsr_write(rt2x00dev, 43, 0x1B);
2343 rt2800_rfcsr_write(rt2x00dev, 44, 0x10);
2344 rt2800_rfcsr_write(rt2x00dev, 46, 0X08);
2345 }
2346 if (rf->channel <= 124)
2347 rt2800_rfcsr_write(rt2x00dev, 51, 0xFC);
2348 else if (rf->channel >= 126)
2349 rt2800_rfcsr_write(rt2x00dev, 51, 0xEC);
2350 if (rf->channel <= 138)
2351 rt2800_rfcsr_write(rt2x00dev, 52, 0x06);
2352 else if (rf->channel >= 140)
2353 rt2800_rfcsr_write(rt2x00dev, 52, 0x06);
2354 rt2800_rfcsr_write(rt2x00dev, 54, 0xEB);
2355 if (rf->channel <= 138)
2356 rt2800_rfcsr_write(rt2x00dev, 55, 0x01);
2357 else if (rf->channel >= 140)
2358 rt2800_rfcsr_write(rt2x00dev, 55, 0x00);
2359 if (rf->channel <= 128)
2360 rt2800_rfcsr_write(rt2x00dev, 56, 0xBB);
2361 else if (rf->channel >= 130)
2362 rt2800_rfcsr_write(rt2x00dev, 56, 0xAB);
2363 if (rf->channel <= 116)
2364 rt2800_rfcsr_write(rt2x00dev, 58, 0x1D);
2365 else if (rf->channel >= 118)
2366 rt2800_rfcsr_write(rt2x00dev, 58, 0x15);
2367 if (rf->channel <= 138)
2368 rt2800_rfcsr_write(rt2x00dev, 59, 0x3F);
2369 else if (rf->channel >= 140)
2370 rt2800_rfcsr_write(rt2x00dev, 59, 0x7C);
2371 if (rf->channel <= 116)
2372 rt2800_rfcsr_write(rt2x00dev, 62, 0x1D);
2373 else if (rf->channel >= 118)
2374 rt2800_rfcsr_write(rt2x00dev, 62, 0x15);
2375 }
2376
2377 power_bound = POWER_BOUND_5G;
2378 ep_reg = 0x3;
2379 }
2380
2381 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
2382 if (info->default_power1 > power_bound)
2383 rt2x00_set_field8(&rfcsr, RFCSR49_TX, power_bound);
2384 else
2385 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2386 if (is_type_ep)
2387 rt2x00_set_field8(&rfcsr, RFCSR49_EP, ep_reg);
2388 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
2389
2390 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
2391 if (info->default_power1 > power_bound)
2392 rt2x00_set_field8(&rfcsr, RFCSR50_TX, power_bound);
2393 else
2394 rt2x00_set_field8(&rfcsr, RFCSR50_TX, info->default_power2);
2395 if (is_type_ep)
2396 rt2x00_set_field8(&rfcsr, RFCSR50_EP, ep_reg);
2397 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
2398
2399 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2400 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2401 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
2402
2403 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD,
2404 rt2x00dev->default_ant.tx_chain_num >= 1);
2405 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD,
2406 rt2x00dev->default_ant.tx_chain_num == 2);
2407 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
2408
2409 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD,
2410 rt2x00dev->default_ant.rx_chain_num >= 1);
2411 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD,
2412 rt2x00dev->default_ant.rx_chain_num == 2);
2413 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2414
2415 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2416 rt2800_rfcsr_write(rt2x00dev, 6, 0xe4);
2417
2418 if (conf_is_ht40(conf))
2419 rt2800_rfcsr_write(rt2x00dev, 30, 0x16);
2420 else
2421 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
2422
2423 if (!is_11b) {
2424 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
2425 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
2426 }
2427
2428 /* TODO proper frequency adjustment */
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002429 rt2800_adjust_freq_offset(rt2x00dev);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002430
2431 /* TODO merge with others */
2432 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
2433 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
2434 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
Stanislaw Gruszka68031412013-03-16 19:19:44 +01002435
2436 /* BBP settings */
2437 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
2438 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
2439 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
2440
2441 rt2800_bbp_write(rt2x00dev, 79, (rf->channel <= 14) ? 0x1C : 0x18);
2442 rt2800_bbp_write(rt2x00dev, 80, (rf->channel <= 14) ? 0x0E : 0x08);
2443 rt2800_bbp_write(rt2x00dev, 81, (rf->channel <= 14) ? 0x3A : 0x38);
2444 rt2800_bbp_write(rt2x00dev, 82, (rf->channel <= 14) ? 0x62 : 0x92);
2445
2446 /* GLRT band configuration */
2447 rt2800_bbp_write(rt2x00dev, 195, 128);
2448 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0xE0 : 0xF0);
2449 rt2800_bbp_write(rt2x00dev, 195, 129);
2450 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x1F : 0x1E);
2451 rt2800_bbp_write(rt2x00dev, 195, 130);
2452 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x38 : 0x28);
2453 rt2800_bbp_write(rt2x00dev, 195, 131);
2454 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x32 : 0x20);
2455 rt2800_bbp_write(rt2x00dev, 195, 133);
2456 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x28 : 0x7F);
2457 rt2800_bbp_write(rt2x00dev, 195, 124);
2458 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x19 : 0x7F);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002459}
2460
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002461static void rt2800_iq_calibrate(struct rt2x00_dev *rt2x00dev, int channel)
2462{
2463 u8 cal;
2464
2465 /* TODO */
2466 if (WARN_ON_ONCE(channel > 14))
2467 return;
2468
2469 rt2800_bbp_write(rt2x00dev, 158, 0x2c);
2470 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_GAIN_CAL_TX0_2G);
2471 rt2800_bbp_write(rt2x00dev, 159, cal);
2472
2473 rt2800_bbp_write(rt2x00dev, 158, 0x2d);
2474 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_PHASE_CAL_TX0_2G);
2475 rt2800_bbp_write(rt2x00dev, 159, cal);
2476
2477 rt2800_bbp_write(rt2x00dev, 158, 0x4a);
2478 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_GAIN_CAL_TX1_2G);
2479 rt2800_bbp_write(rt2x00dev, 159, cal);
2480
2481 rt2800_bbp_write(rt2x00dev, 158, 0x4b);
2482 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_PHASE_CAL_TX1_2G);
2483 rt2800_bbp_write(rt2x00dev, 159, cal);
2484
2485 /* RF IQ compensation control */
2486 rt2800_bbp_write(rt2x00dev, 158, 0x04);
2487 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_RF_IQ_COMPENSATION_CONTROL);
2488 rt2800_bbp_write(rt2x00dev, 159, cal != 0xff ? cal : 0);
2489
2490 /* RF IQ imbalance compensation control */
2491 rt2800_bbp_write(rt2x00dev, 158, 0x03);
2492 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_RF_IQ_IMBALANCE_COMPENSATION_CONTROL);
2493 rt2800_bbp_write(rt2x00dev, 159, cal != 0xff ? cal : 0);
2494}
2495
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002496static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
2497 struct ieee80211_conf *conf,
2498 struct rf_channel *rf,
2499 struct channel_info *info)
2500{
2501 u32 reg;
2502 unsigned int tx_pin;
Woody Hunga89534e2012-06-13 15:01:16 +08002503 u8 bbp, rfcsr;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002504
Ivo van Doorn46323e12010-08-23 19:55:43 +02002505 if (rf->channel <= 14) {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002506 info->default_power1 = TXPOWER_G_TO_DEV(info->default_power1);
2507 info->default_power2 = TXPOWER_G_TO_DEV(info->default_power2);
Ivo van Doorn46323e12010-08-23 19:55:43 +02002508 } else {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002509 info->default_power1 = TXPOWER_A_TO_DEV(info->default_power1);
2510 info->default_power2 = TXPOWER_A_TO_DEV(info->default_power2);
Ivo van Doorn46323e12010-08-23 19:55:43 +02002511 }
2512
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002513 switch (rt2x00dev->chip.rf) {
2514 case RF2020:
2515 case RF3020:
2516 case RF3021:
2517 case RF3022:
2518 case RF3320:
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02002519 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002520 break;
2521 case RF3052:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002522 rt2800_config_channel_rf3052(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002523 break;
Woody Hunga89534e2012-06-13 15:01:16 +08002524 case RF3290:
2525 rt2800_config_channel_rf3290(rt2x00dev, conf, rf, info);
2526 break;
Daniel Golle03839952012-09-09 14:24:39 +03002527 case RF3322:
2528 rt2800_config_channel_rf3322(rt2x00dev, conf, rf, info);
2529 break;
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02002530 case RF5360:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002531 case RF5370:
John Li2ed71882012-02-17 17:33:06 +08002532 case RF5372:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002533 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08002534 case RF5392:
Gabor Juhosadde5882011-03-03 11:46:45 +01002535 rt2800_config_channel_rf53xx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002536 break;
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002537 case RF5592:
2538 rt2800_config_channel_rf55xx(rt2x00dev, conf, rf, info);
2539 break;
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002540 default:
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02002541 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002542 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002543
Woody Hunga89534e2012-06-13 15:01:16 +08002544 if (rt2x00_rf(rt2x00dev, RF3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03002545 rt2x00_rf(rt2x00dev, RF3322) ||
Woody Hunga89534e2012-06-13 15:01:16 +08002546 rt2x00_rf(rt2x00dev, RF5360) ||
2547 rt2x00_rf(rt2x00dev, RF5370) ||
2548 rt2x00_rf(rt2x00dev, RF5372) ||
2549 rt2x00_rf(rt2x00dev, RF5390) ||
2550 rt2x00_rf(rt2x00dev, RF5392)) {
2551 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2552 rt2x00_set_field8(&rfcsr, RFCSR30_TX_H20M, 0);
2553 rt2x00_set_field8(&rfcsr, RFCSR30_RX_H20M, 0);
2554 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2555
2556 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
Gabor Juhosd6d82022012-12-02 18:34:47 +01002557 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
Woody Hunga89534e2012-06-13 15:01:16 +08002558 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
2559 }
2560
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002561 /*
2562 * Change BBP settings
2563 */
Daniel Golle03839952012-09-09 14:24:39 +03002564 if (rt2x00_rt(rt2x00dev, RT3352)) {
2565 rt2800_bbp_write(rt2x00dev, 27, 0x0);
Daniel Gollecf193f62012-10-04 01:20:41 +02002566 rt2800_bbp_write(rt2x00dev, 66, 0x26 + rt2x00dev->lna_gain);
Daniel Golle03839952012-09-09 14:24:39 +03002567 rt2800_bbp_write(rt2x00dev, 27, 0x20);
Daniel Gollecf193f62012-10-04 01:20:41 +02002568 rt2800_bbp_write(rt2x00dev, 66, 0x26 + rt2x00dev->lna_gain);
Daniel Golle03839952012-09-09 14:24:39 +03002569 } else {
2570 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
2571 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
2572 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
2573 rt2800_bbp_write(rt2x00dev, 86, 0);
2574 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002575
2576 if (rf->channel <= 14) {
John Li2ed71882012-02-17 17:33:06 +08002577 if (!rt2x00_rt(rt2x00dev, RT5390) &&
Gabor Juhose6d227b2012-12-02 15:53:28 +01002578 !rt2x00_rt(rt2x00dev, RT5392)) {
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02002579 if (test_bit(CAPABILITY_EXTERNAL_LNA_BG,
2580 &rt2x00dev->cap_flags)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002581 rt2800_bbp_write(rt2x00dev, 82, 0x62);
2582 rt2800_bbp_write(rt2x00dev, 75, 0x46);
2583 } else {
2584 rt2800_bbp_write(rt2x00dev, 82, 0x84);
2585 rt2800_bbp_write(rt2x00dev, 75, 0x50);
2586 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002587 }
2588 } else {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002589 if (rt2x00_rt(rt2x00dev, RT3572))
2590 rt2800_bbp_write(rt2x00dev, 82, 0x94);
2591 else
2592 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002593
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02002594 if (test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002595 rt2800_bbp_write(rt2x00dev, 75, 0x46);
2596 else
2597 rt2800_bbp_write(rt2x00dev, 75, 0x50);
2598 }
2599
2600 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02002601 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002602 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
2603 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
2604 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
2605
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002606 if (rt2x00_rt(rt2x00dev, RT3572))
2607 rt2800_rfcsr_write(rt2x00dev, 8, 0);
2608
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002609 tx_pin = 0;
2610
2611 /* Turn on unused PA or LNA when not using 1T or 1R */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01002612 if (rt2x00dev->default_ant.tx_chain_num == 2) {
Gertjan van Wingerde65f31b52011-05-18 20:25:05 +02002613 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN,
2614 rf->channel > 14);
2615 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN,
2616 rf->channel <= 14);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002617 }
2618
2619 /* Turn on unused PA or LNA when not using 1T or 1R */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01002620 if (rt2x00dev->default_ant.rx_chain_num == 2) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002621 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
2622 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
2623 }
2624
2625 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
2626 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
2627 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
2628 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
Gertjan van Wingerde8f96e912011-05-18 20:25:18 +02002629 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
2630 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
2631 else
2632 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN,
2633 rf->channel <= 14);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002634 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
2635
2636 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
2637
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002638 if (rt2x00_rt(rt2x00dev, RT3572))
2639 rt2800_rfcsr_write(rt2x00dev, 8, 0x80);
2640
Stanislaw Gruszka68031412013-03-16 19:19:44 +01002641 if (rt2x00_rt(rt2x00dev, RT5592)) {
2642 rt2800_bbp_write(rt2x00dev, 195, 141);
2643 rt2800_bbp_write(rt2x00dev, 196, conf_is_ht40(conf) ? 0x10 : 0x1a);
2644
2645 /* TODO AGC adjust */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002646 rt2800_iq_calibrate(rt2x00dev, rf->channel);
Stanislaw Gruszka68031412013-03-16 19:19:44 +01002647 }
2648
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002649 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2650 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
2651 rt2800_bbp_write(rt2x00dev, 4, bbp);
2652
2653 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02002654 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002655 rt2800_bbp_write(rt2x00dev, 3, bbp);
2656
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02002657 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002658 if (conf_is_ht40(conf)) {
2659 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
2660 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
2661 rt2800_bbp_write(rt2x00dev, 73, 0x16);
2662 } else {
2663 rt2800_bbp_write(rt2x00dev, 69, 0x16);
2664 rt2800_bbp_write(rt2x00dev, 70, 0x08);
2665 rt2800_bbp_write(rt2x00dev, 73, 0x11);
2666 }
2667 }
2668
2669 msleep(1);
Helmut Schaa977206d2010-12-13 12:31:58 +01002670
2671 /*
2672 * Clear channel statistic counters
2673 */
2674 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &reg);
2675 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &reg);
2676 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &reg);
Daniel Golle03839952012-09-09 14:24:39 +03002677
2678 /*
2679 * Clear update flag
2680 */
2681 if (rt2x00_rt(rt2x00dev, RT3352)) {
2682 rt2800_bbp_read(rt2x00dev, 49, &bbp);
2683 rt2x00_set_field8(&bbp, BBP49_UPDATE_FLAG, 0);
2684 rt2800_bbp_write(rt2x00dev, 49, bbp);
2685 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002686}
2687
Helmut Schaa9e33a352011-03-28 13:33:40 +02002688static int rt2800_get_gain_calibration_delta(struct rt2x00_dev *rt2x00dev)
2689{
2690 u8 tssi_bounds[9];
2691 u8 current_tssi;
2692 u16 eeprom;
2693 u8 step;
2694 int i;
2695
2696 /*
2697 * Read TSSI boundaries for temperature compensation from
2698 * the EEPROM.
2699 *
2700 * Array idx 0 1 2 3 4 5 6 7 8
2701 * Matching Delta value -4 -3 -2 -1 0 +1 +2 +3 +4
2702 * Example TSSI bounds 0xF0 0xD0 0xB5 0xA0 0x88 0x45 0x25 0x15 0x00
2703 */
2704 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
2705 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG1, &eeprom);
2706 tssi_bounds[0] = rt2x00_get_field16(eeprom,
2707 EEPROM_TSSI_BOUND_BG1_MINUS4);
2708 tssi_bounds[1] = rt2x00_get_field16(eeprom,
2709 EEPROM_TSSI_BOUND_BG1_MINUS3);
2710
2711 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG2, &eeprom);
2712 tssi_bounds[2] = rt2x00_get_field16(eeprom,
2713 EEPROM_TSSI_BOUND_BG2_MINUS2);
2714 tssi_bounds[3] = rt2x00_get_field16(eeprom,
2715 EEPROM_TSSI_BOUND_BG2_MINUS1);
2716
2717 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG3, &eeprom);
2718 tssi_bounds[4] = rt2x00_get_field16(eeprom,
2719 EEPROM_TSSI_BOUND_BG3_REF);
2720 tssi_bounds[5] = rt2x00_get_field16(eeprom,
2721 EEPROM_TSSI_BOUND_BG3_PLUS1);
2722
2723 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG4, &eeprom);
2724 tssi_bounds[6] = rt2x00_get_field16(eeprom,
2725 EEPROM_TSSI_BOUND_BG4_PLUS2);
2726 tssi_bounds[7] = rt2x00_get_field16(eeprom,
2727 EEPROM_TSSI_BOUND_BG4_PLUS3);
2728
2729 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG5, &eeprom);
2730 tssi_bounds[8] = rt2x00_get_field16(eeprom,
2731 EEPROM_TSSI_BOUND_BG5_PLUS4);
2732
2733 step = rt2x00_get_field16(eeprom,
2734 EEPROM_TSSI_BOUND_BG5_AGC_STEP);
2735 } else {
2736 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A1, &eeprom);
2737 tssi_bounds[0] = rt2x00_get_field16(eeprom,
2738 EEPROM_TSSI_BOUND_A1_MINUS4);
2739 tssi_bounds[1] = rt2x00_get_field16(eeprom,
2740 EEPROM_TSSI_BOUND_A1_MINUS3);
2741
2742 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A2, &eeprom);
2743 tssi_bounds[2] = rt2x00_get_field16(eeprom,
2744 EEPROM_TSSI_BOUND_A2_MINUS2);
2745 tssi_bounds[3] = rt2x00_get_field16(eeprom,
2746 EEPROM_TSSI_BOUND_A2_MINUS1);
2747
2748 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A3, &eeprom);
2749 tssi_bounds[4] = rt2x00_get_field16(eeprom,
2750 EEPROM_TSSI_BOUND_A3_REF);
2751 tssi_bounds[5] = rt2x00_get_field16(eeprom,
2752 EEPROM_TSSI_BOUND_A3_PLUS1);
2753
2754 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A4, &eeprom);
2755 tssi_bounds[6] = rt2x00_get_field16(eeprom,
2756 EEPROM_TSSI_BOUND_A4_PLUS2);
2757 tssi_bounds[7] = rt2x00_get_field16(eeprom,
2758 EEPROM_TSSI_BOUND_A4_PLUS3);
2759
2760 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A5, &eeprom);
2761 tssi_bounds[8] = rt2x00_get_field16(eeprom,
2762 EEPROM_TSSI_BOUND_A5_PLUS4);
2763
2764 step = rt2x00_get_field16(eeprom,
2765 EEPROM_TSSI_BOUND_A5_AGC_STEP);
2766 }
2767
2768 /*
2769 * Check if temperature compensation is supported.
2770 */
Stanislaw Gruszkabf7e1ab2012-10-25 09:51:39 +02002771 if (tssi_bounds[4] == 0xff || step == 0xff)
Helmut Schaa9e33a352011-03-28 13:33:40 +02002772 return 0;
2773
2774 /*
2775 * Read current TSSI (BBP 49).
2776 */
2777 rt2800_bbp_read(rt2x00dev, 49, &current_tssi);
2778
2779 /*
2780 * Compare TSSI value (BBP49) with the compensation boundaries
2781 * from the EEPROM and increase or decrease tx power.
2782 */
2783 for (i = 0; i <= 3; i++) {
2784 if (current_tssi > tssi_bounds[i])
2785 break;
2786 }
2787
2788 if (i == 4) {
2789 for (i = 8; i >= 5; i--) {
2790 if (current_tssi < tssi_bounds[i])
2791 break;
2792 }
2793 }
2794
2795 return (i - 4) * step;
2796}
2797
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002798static int rt2800_get_txpower_bw_comp(struct rt2x00_dev *rt2x00dev,
2799 enum ieee80211_band band)
2800{
2801 u16 eeprom;
2802 u8 comp_en;
2803 u8 comp_type;
Helmut Schaa75faae82011-03-28 13:31:30 +02002804 int comp_value = 0;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002805
2806 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_DELTA, &eeprom);
2807
Helmut Schaa75faae82011-03-28 13:31:30 +02002808 /*
2809 * HT40 compensation not required.
2810 */
2811 if (eeprom == 0xffff ||
2812 !test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002813 return 0;
2814
2815 if (band == IEEE80211_BAND_2GHZ) {
2816 comp_en = rt2x00_get_field16(eeprom,
2817 EEPROM_TXPOWER_DELTA_ENABLE_2G);
2818 if (comp_en) {
2819 comp_type = rt2x00_get_field16(eeprom,
2820 EEPROM_TXPOWER_DELTA_TYPE_2G);
2821 comp_value = rt2x00_get_field16(eeprom,
2822 EEPROM_TXPOWER_DELTA_VALUE_2G);
2823 if (!comp_type)
2824 comp_value = -comp_value;
2825 }
2826 } else {
2827 comp_en = rt2x00_get_field16(eeprom,
2828 EEPROM_TXPOWER_DELTA_ENABLE_5G);
2829 if (comp_en) {
2830 comp_type = rt2x00_get_field16(eeprom,
2831 EEPROM_TXPOWER_DELTA_TYPE_5G);
2832 comp_value = rt2x00_get_field16(eeprom,
2833 EEPROM_TXPOWER_DELTA_VALUE_5G);
2834 if (!comp_type)
2835 comp_value = -comp_value;
2836 }
2837 }
2838
2839 return comp_value;
2840}
2841
Stanislaw Gruszka1e4cf242012-10-05 13:44:14 +02002842static int rt2800_get_txpower_reg_delta(struct rt2x00_dev *rt2x00dev,
2843 int power_level, int max_power)
2844{
2845 int delta;
2846
2847 if (test_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags))
2848 return 0;
2849
2850 /*
2851 * XXX: We don't know the maximum transmit power of our hardware since
2852 * the EEPROM doesn't expose it. We only know that we are calibrated
2853 * to 100% tx power.
2854 *
2855 * Hence, we assume the regulatory limit that cfg80211 calulated for
2856 * the current channel is our maximum and if we are requested to lower
2857 * the value we just reduce our tx power accordingly.
2858 */
2859 delta = power_level - max_power;
2860 return min(delta, 0);
2861}
2862
Helmut Schaafa71a162011-03-28 13:32:32 +02002863static u8 rt2800_compensate_txpower(struct rt2x00_dev *rt2x00dev, int is_rate_b,
2864 enum ieee80211_band band, int power_level,
2865 u8 txpower, int delta)
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002866{
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002867 u16 eeprom;
2868 u8 criterion;
2869 u8 eirp_txpower;
2870 u8 eirp_txpower_criterion;
2871 u8 reg_limit;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002872
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02002873 if (test_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags)) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002874 /*
2875 * Check if eirp txpower exceed txpower_limit.
2876 * We use OFDM 6M as criterion and its eirp txpower
2877 * is stored at EEPROM_EIRP_MAX_TX_POWER.
2878 * .11b data rate need add additional 4dbm
2879 * when calculating eirp txpower.
2880 */
Stanislaw Gruszkad9bceae2012-10-05 13:44:12 +02002881 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + 1,
2882 &eeprom);
2883 criterion = rt2x00_get_field16(eeprom,
2884 EEPROM_TXPOWER_BYRATE_RATE0);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002885
Stanislaw Gruszkad9bceae2012-10-05 13:44:12 +02002886 rt2x00_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER,
2887 &eeprom);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002888
2889 if (band == IEEE80211_BAND_2GHZ)
2890 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
2891 EEPROM_EIRP_MAX_TX_POWER_2GHZ);
2892 else
2893 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
2894 EEPROM_EIRP_MAX_TX_POWER_5GHZ);
2895
2896 eirp_txpower = eirp_txpower_criterion + (txpower - criterion) +
Helmut Schaa2af242e2011-03-28 13:32:01 +02002897 (is_rate_b ? 4 : 0) + delta;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002898
2899 reg_limit = (eirp_txpower > power_level) ?
2900 (eirp_txpower - power_level) : 0;
2901 } else
2902 reg_limit = 0;
2903
Stanislaw Gruszka19f3fa22012-10-05 13:44:10 +02002904 txpower = max(0, txpower + delta - reg_limit);
2905 return min_t(u8, txpower, 0xc);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002906}
2907
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02002908/*
2909 * We configure transmit power using MAC TX_PWR_CFG_{0,...,N} registers and
2910 * BBP R1 register. TX_PWR_CFG_X allow to configure per rate TX power values,
2911 * 4 bits for each rate (tune from 0 to 15 dBm). BBP_R1 controls transmit power
2912 * for all rates, but allow to set only 4 discrete values: -12, -6, 0 and 6 dBm.
2913 * Reference per rate transmit power values are located in the EEPROM at
2914 * EEPROM_TXPOWER_BYRATE offset. We adjust them and BBP R1 settings according to
2915 * current conditions (i.e. band, bandwidth, temperature, user settings).
2916 */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002917static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
Stanislaw Gruszka146c3b02012-10-05 13:44:13 +02002918 struct ieee80211_channel *chan,
Helmut Schaa9e33a352011-03-28 13:33:40 +02002919 int power_level)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002920{
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02002921 u8 txpower, r1;
Helmut Schaa5e846002010-07-11 12:23:09 +02002922 u16 eeprom;
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02002923 u32 reg, offset;
2924 int i, is_rate_b, delta, power_ctrl;
Stanislaw Gruszka146c3b02012-10-05 13:44:13 +02002925 enum ieee80211_band band = chan->band;
Helmut Schaa2af242e2011-03-28 13:32:01 +02002926
2927 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02002928 * Calculate HT40 compensation. For 40MHz we need to add or subtract
2929 * value read from EEPROM (different for 2GHz and for 5GHz).
Helmut Schaa2af242e2011-03-28 13:32:01 +02002930 */
2931 delta = rt2800_get_txpower_bw_comp(rt2x00dev, band);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002932
Helmut Schaa5e846002010-07-11 12:23:09 +02002933 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02002934 * Calculate temperature compensation. Depends on measurement of current
2935 * TSSI (Transmitter Signal Strength Indication) we know TX power (due
2936 * to temperature or maybe other factors) is smaller or bigger than
2937 * expected. We adjust it, based on TSSI reference and boundaries values
2938 * provided in EEPROM.
Helmut Schaa9e33a352011-03-28 13:33:40 +02002939 */
2940 delta += rt2800_get_gain_calibration_delta(rt2x00dev);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002941
Helmut Schaa5e846002010-07-11 12:23:09 +02002942 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02002943 * Decrease power according to user settings, on devices with unknown
2944 * maximum tx power. For other devices we take user power_level into
2945 * consideration on rt2800_compensate_txpower().
Stanislaw Gruszka1e4cf242012-10-05 13:44:14 +02002946 */
2947 delta += rt2800_get_txpower_reg_delta(rt2x00dev, power_level,
2948 chan->max_power);
2949
2950 /*
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02002951 * BBP_R1 controls TX power for all rates, it allow to set the following
2952 * gains -12, -6, 0, +6 dBm by setting values 2, 1, 0, 3 respectively.
2953 *
2954 * TODO: we do not use +6 dBm option to do not increase power beyond
2955 * regulatory limit, however this could be utilized for devices with
2956 * CAPABILITY_POWER_LIMIT.
Helmut Schaa5e846002010-07-11 12:23:09 +02002957 */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002958 rt2800_bbp_read(rt2x00dev, 1, &r1);
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02002959 if (delta <= -12) {
2960 power_ctrl = 2;
2961 delta += 12;
2962 } else if (delta <= -6) {
2963 power_ctrl = 1;
2964 delta += 6;
2965 } else {
2966 power_ctrl = 0;
2967 }
2968 rt2x00_set_field8(&r1, BBP1_TX_POWER_CTRL, power_ctrl);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002969 rt2800_bbp_write(rt2x00dev, 1, r1);
Helmut Schaa5e846002010-07-11 12:23:09 +02002970 offset = TX_PWR_CFG_0;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002971
Helmut Schaa5e846002010-07-11 12:23:09 +02002972 for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
2973 /* just to be safe */
2974 if (offset > TX_PWR_CFG_4)
2975 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002976
Helmut Schaa5e846002010-07-11 12:23:09 +02002977 rt2800_register_read(rt2x00dev, offset, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002978
Helmut Schaa5e846002010-07-11 12:23:09 +02002979 /* read the next four txpower values */
2980 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i,
2981 &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002982
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002983 is_rate_b = i ? 0 : 1;
2984 /*
2985 * TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02002986 * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002987 * TX_PWR_CFG_4: unknown
2988 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002989 txpower = rt2x00_get_field16(eeprom,
2990 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02002991 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002992 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002993 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002994
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002995 /*
2996 * TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02002997 * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002998 * TX_PWR_CFG_4: unknown
2999 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003000 txpower = rt2x00_get_field16(eeprom,
3001 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02003002 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003003 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003004 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003005
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003006 /*
3007 * TX_PWR_CFG_0: 5.5MBS, TX_PWR_CFG_1: 48MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02003008 * TX_PWR_CFG_2: MCS6, TX_PWR_CFG_3: MCS14,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003009 * TX_PWR_CFG_4: unknown
3010 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003011 txpower = rt2x00_get_field16(eeprom,
3012 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02003013 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003014 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003015 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003016
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003017 /*
3018 * TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02003019 * TX_PWR_CFG_2: MCS7, TX_PWR_CFG_3: MCS15,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003020 * TX_PWR_CFG_4: unknown
3021 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003022 txpower = rt2x00_get_field16(eeprom,
3023 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02003024 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003025 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003026 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003027
3028 /* read the next four txpower values */
3029 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i + 1,
3030 &eeprom);
3031
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003032 is_rate_b = 0;
3033 /*
3034 * TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
Helmut Schaa5e846002010-07-11 12:23:09 +02003035 * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003036 * TX_PWR_CFG_4: unknown
3037 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003038 txpower = rt2x00_get_field16(eeprom,
3039 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02003040 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003041 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003042 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003043
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003044 /*
3045 * TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
Helmut Schaa5e846002010-07-11 12:23:09 +02003046 * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003047 * TX_PWR_CFG_4: unknown
3048 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003049 txpower = rt2x00_get_field16(eeprom,
3050 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02003051 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003052 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003053 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003054
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003055 /*
3056 * TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
Helmut Schaa5e846002010-07-11 12:23:09 +02003057 * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003058 * TX_PWR_CFG_4: unknown
3059 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003060 txpower = rt2x00_get_field16(eeprom,
3061 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02003062 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003063 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003064 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003065
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003066 /*
3067 * TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
Helmut Schaa5e846002010-07-11 12:23:09 +02003068 * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003069 * TX_PWR_CFG_4: unknown
3070 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003071 txpower = rt2x00_get_field16(eeprom,
3072 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02003073 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003074 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003075 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003076
3077 rt2800_register_write(rt2x00dev, offset, reg);
3078
3079 /* next TX_PWR_CFG register */
3080 offset += 4;
3081 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003082}
3083
Helmut Schaa9e33a352011-03-28 13:33:40 +02003084void rt2800_gain_calibration(struct rt2x00_dev *rt2x00dev)
3085{
Stanislaw Gruszka146c3b02012-10-05 13:44:13 +02003086 rt2800_config_txpower(rt2x00dev, rt2x00dev->hw->conf.channel,
Helmut Schaa9e33a352011-03-28 13:33:40 +02003087 rt2x00dev->tx_power);
3088}
3089EXPORT_SYMBOL_GPL(rt2800_gain_calibration);
3090
John Li2e9c43d2012-02-16 21:40:57 +08003091void rt2800_vco_calibration(struct rt2x00_dev *rt2x00dev)
3092{
3093 u32 tx_pin;
3094 u8 rfcsr;
3095
3096 /*
3097 * A voltage-controlled oscillator(VCO) is an electronic oscillator
3098 * designed to be controlled in oscillation frequency by a voltage
3099 * input. Maybe the temperature will affect the frequency of
3100 * oscillation to be shifted. The VCO calibration will be called
3101 * periodically to adjust the frequency to be precision.
3102 */
3103
3104 rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
3105 tx_pin &= TX_PIN_CFG_PA_PE_DISABLE;
3106 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
3107
3108 switch (rt2x00dev->chip.rf) {
3109 case RF2020:
3110 case RF3020:
3111 case RF3021:
3112 case RF3022:
3113 case RF3320:
3114 case RF3052:
3115 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
3116 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
3117 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
3118 break;
Woody Hunga89534e2012-06-13 15:01:16 +08003119 case RF3290:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02003120 case RF5360:
John Li2e9c43d2012-02-16 21:40:57 +08003121 case RF5370:
3122 case RF5372:
3123 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08003124 case RF5392:
John Li2e9c43d2012-02-16 21:40:57 +08003125 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
Gabor Juhosd6d82022012-12-02 18:34:47 +01003126 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
John Li2e9c43d2012-02-16 21:40:57 +08003127 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
3128 break;
3129 default:
3130 return;
3131 }
3132
3133 mdelay(1);
3134
3135 rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
3136 if (rt2x00dev->rf_channel <= 14) {
3137 switch (rt2x00dev->default_ant.tx_chain_num) {
3138 case 3:
3139 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G2_EN, 1);
3140 /* fall through */
3141 case 2:
3142 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
3143 /* fall through */
3144 case 1:
3145 default:
3146 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
3147 break;
3148 }
3149 } else {
3150 switch (rt2x00dev->default_ant.tx_chain_num) {
3151 case 3:
3152 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A2_EN, 1);
3153 /* fall through */
3154 case 2:
3155 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
3156 /* fall through */
3157 case 1:
3158 default:
3159 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, 1);
3160 break;
3161 }
3162 }
3163 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
3164
3165}
3166EXPORT_SYMBOL_GPL(rt2800_vco_calibration);
3167
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003168static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
3169 struct rt2x00lib_conf *libconf)
3170{
3171 u32 reg;
3172
3173 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
3174 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
3175 libconf->conf->short_frame_max_tx_count);
3176 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
3177 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003178 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
3179}
3180
3181static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
3182 struct rt2x00lib_conf *libconf)
3183{
3184 enum dev_state state =
3185 (libconf->conf->flags & IEEE80211_CONF_PS) ?
3186 STATE_SLEEP : STATE_AWAKE;
3187 u32 reg;
3188
3189 if (state == STATE_SLEEP) {
3190 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
3191
3192 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
3193 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
3194 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
3195 libconf->conf->listen_interval - 1);
3196 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
3197 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
3198
3199 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
3200 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003201 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
3202 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
3203 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
3204 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
3205 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02003206
3207 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003208 }
3209}
3210
3211void rt2800_config(struct rt2x00_dev *rt2x00dev,
3212 struct rt2x00lib_conf *libconf,
3213 const unsigned int flags)
3214{
3215 /* Always recalculate LNA gain before changing configuration */
3216 rt2800_config_lna_gain(rt2x00dev, libconf);
3217
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003218 if (flags & IEEE80211_CONF_CHANGE_CHANNEL) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003219 rt2800_config_channel(rt2x00dev, libconf->conf,
3220 &libconf->rf, &libconf->channel);
Stanislaw Gruszka146c3b02012-10-05 13:44:13 +02003221 rt2800_config_txpower(rt2x00dev, libconf->conf->channel,
Helmut Schaa9e33a352011-03-28 13:33:40 +02003222 libconf->conf->power_level);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003223 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003224 if (flags & IEEE80211_CONF_CHANGE_POWER)
Stanislaw Gruszka146c3b02012-10-05 13:44:13 +02003225 rt2800_config_txpower(rt2x00dev, libconf->conf->channel,
Helmut Schaa9e33a352011-03-28 13:33:40 +02003226 libconf->conf->power_level);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003227 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
3228 rt2800_config_retry_limit(rt2x00dev, libconf);
3229 if (flags & IEEE80211_CONF_CHANGE_PS)
3230 rt2800_config_ps(rt2x00dev, libconf);
3231}
3232EXPORT_SYMBOL_GPL(rt2800_config);
3233
3234/*
3235 * Link tuning
3236 */
3237void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
3238{
3239 u32 reg;
3240
3241 /*
3242 * Update FCS error count from register.
3243 */
3244 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
3245 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
3246}
3247EXPORT_SYMBOL_GPL(rt2800_link_stats);
3248
3249static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
3250{
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02003251 u8 vgc;
3252
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003253 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003254 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003255 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003256 rt2x00_rt(rt2x00dev, RT3090) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003257 rt2x00_rt(rt2x00dev, RT3290) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01003258 rt2x00_rt(rt2x00dev, RT3390) ||
Gertjan van Wingerded961e442012-09-16 22:29:50 +02003259 rt2x00_rt(rt2x00dev, RT3572) ||
John Li2ed71882012-02-17 17:33:06 +08003260 rt2x00_rt(rt2x00dev, RT5390) ||
3261 rt2x00_rt(rt2x00dev, RT5392))
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02003262 vgc = 0x1c + (2 * rt2x00dev->lna_gain);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003263 else
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02003264 vgc = 0x2e + rt2x00dev->lna_gain;
3265 } else { /* 5GHZ band */
Gertjan van Wingerded961e442012-09-16 22:29:50 +02003266 if (rt2x00_rt(rt2x00dev, RT3572))
3267 vgc = 0x22 + (rt2x00dev->lna_gain * 5) / 3;
3268 else {
3269 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
3270 vgc = 0x32 + (rt2x00dev->lna_gain * 5) / 3;
3271 else
3272 vgc = 0x3a + (rt2x00dev->lna_gain * 5) / 3;
3273 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003274 }
3275
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02003276 return vgc;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003277}
3278
3279static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
3280 struct link_qual *qual, u8 vgc_level)
3281{
3282 if (qual->vgc_level != vgc_level) {
3283 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
3284 qual->vgc_level = vgc_level;
3285 qual->vgc_level_reg = vgc_level;
3286 }
3287}
3288
3289void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
3290{
3291 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
3292}
3293EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
3294
3295void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
3296 const u32 count)
3297{
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02003298 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003299 return;
3300
3301 /*
3302 * When RSSI is better then -80 increase VGC level with 0x10
3303 */
3304 rt2800_set_vgc(rt2x00dev, qual,
3305 rt2800_get_default_vgc(rt2x00dev) +
3306 ((qual->rssi > -80) * 0x10));
3307}
3308EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003309
3310/*
3311 * Initialization functions.
3312 */
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003313static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003314{
3315 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003316 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003317 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02003318 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003319
Jakub Kicinskif7b395e2012-04-03 03:40:47 +02003320 rt2800_disable_wpdma(rt2x00dev);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003321
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02003322 ret = rt2800_drv_init_registers(rt2x00dev);
3323 if (ret)
3324 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003325
3326 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
3327 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
3328 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
3329 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
3330 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
3331 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
3332
3333 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
3334 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
3335 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
3336 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
3337 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
3338 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
3339
3340 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
3341 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
3342
3343 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
3344
3345 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Helmut Schaa8544df32010-07-11 12:29:49 +02003346 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003347 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
3348 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
3349 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
3350 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
3351 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
3352 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
3353
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003354 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
3355
3356 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
3357 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
3358 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
3359 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
3360
Woody Hunga89534e2012-06-13 15:01:16 +08003361 if (rt2x00_rt(rt2x00dev, RT3290)) {
3362 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
3363 if (rt2x00_get_field32(reg, WLAN_EN) == 1) {
3364 rt2x00_set_field32(&reg, PCIE_APP0_CLK_REQ, 1);
3365 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
3366 }
3367
3368 rt2800_register_read(rt2x00dev, CMB_CTRL, &reg);
3369 if (!(rt2x00_get_field32(reg, LDO0_EN) == 1)) {
3370 rt2x00_set_field32(&reg, LDO0_EN, 1);
3371 rt2x00_set_field32(&reg, LDO_BGSEL, 3);
3372 rt2800_register_write(rt2x00dev, CMB_CTRL, reg);
3373 }
3374
3375 rt2800_register_read(rt2x00dev, OSC_CTRL, &reg);
3376 rt2x00_set_field32(&reg, OSC_ROSC_EN, 1);
3377 rt2x00_set_field32(&reg, OSC_CAL_REQ, 1);
3378 rt2x00_set_field32(&reg, OSC_REF_CYCLE, 0x27);
3379 rt2800_register_write(rt2x00dev, OSC_CTRL, reg);
3380
3381 rt2800_register_read(rt2x00dev, COEX_CFG0, &reg);
3382 rt2x00_set_field32(&reg, COEX_CFG_ANT, 0x5e);
3383 rt2800_register_write(rt2x00dev, COEX_CFG0, reg);
3384
3385 rt2800_register_read(rt2x00dev, COEX_CFG2, &reg);
3386 rt2x00_set_field32(&reg, BT_COEX_CFG1, 0x00);
3387 rt2x00_set_field32(&reg, BT_COEX_CFG0, 0x17);
3388 rt2x00_set_field32(&reg, WL_COEX_CFG1, 0x93);
3389 rt2x00_set_field32(&reg, WL_COEX_CFG0, 0x7f);
3390 rt2800_register_write(rt2x00dev, COEX_CFG2, reg);
3391
3392 rt2800_register_read(rt2x00dev, PLL_CTRL, &reg);
3393 rt2x00_set_field32(&reg, PLL_CONTROL, 1);
3394 rt2800_register_write(rt2x00dev, PLL_CTRL, reg);
3395 }
3396
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003397 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003398 rt2x00_rt(rt2x00dev, RT3090) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003399 rt2x00_rt(rt2x00dev, RT3290) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003400 rt2x00_rt(rt2x00dev, RT3390)) {
Woody Hunga89534e2012-06-13 15:01:16 +08003401
3402 if (rt2x00_rt(rt2x00dev, RT3290))
3403 rt2800_register_write(rt2x00dev, TX_SW_CFG0,
3404 0x00000404);
3405 else
3406 rt2800_register_write(rt2x00dev, TX_SW_CFG0,
3407 0x00000400);
3408
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003409 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003410 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003411 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
3412 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003413 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
3414 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003415 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
3416 0x0000002c);
3417 else
3418 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
3419 0x0000000f);
3420 } else {
3421 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
3422 }
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003423 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003424 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003425
3426 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
3427 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
3428 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
3429 } else {
3430 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
3431 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
3432 }
Helmut Schaac295a812010-06-03 10:52:13 +02003433 } else if (rt2800_is_305x_soc(rt2x00dev)) {
3434 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
3435 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Helmut Schaa961636b2011-04-18 15:28:27 +02003436 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000030);
Daniel Golle03839952012-09-09 14:24:39 +03003437 } else if (rt2x00_rt(rt2x00dev, RT3352)) {
3438 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000402);
3439 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
3440 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003441 } else if (rt2x00_rt(rt2x00dev, RT3572)) {
3442 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
3443 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
John Li2ed71882012-02-17 17:33:06 +08003444 } else if (rt2x00_rt(rt2x00dev, RT5390) ||
Stanislaw Gruszka76413282013-03-16 19:19:33 +01003445 rt2x00_rt(rt2x00dev, RT5392) ||
3446 rt2x00_rt(rt2x00dev, RT5592)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01003447 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000404);
3448 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
3449 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003450 } else {
3451 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
3452 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
3453 }
3454
3455 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
3456 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
3457 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
3458 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
3459 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
3460 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
3461 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
3462 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
3463 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
3464 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
3465
3466 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
3467 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003468 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003469 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
3470 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
3471
3472 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
3473 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02003474 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01003475 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02003476 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003477 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
3478 else
3479 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
3480 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
3481 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
3482 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
3483
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003484 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
3485 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
3486 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
3487 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
3488 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
3489 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
3490 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
3491 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
3492 rt2800_register_write(rt2x00dev, LED_CFG, reg);
3493
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003494 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
3495
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003496 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
3497 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
3498 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
3499 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
3500 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
3501 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
3502 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
3503 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
3504
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003505 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
3506 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003507 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003508 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
3509 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003510 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003511 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
3512 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
3513 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
3514
3515 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003516 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003517 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003518 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003519 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3520 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3521 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003522 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003523 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003524 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
3525 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003526 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
3527
3528 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003529 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003530 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003531 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003532 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3533 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3534 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003535 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003536 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003537 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
3538 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003539 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
3540
3541 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
3542 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
3543 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003544 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003545 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3546 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3547 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
3548 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
3549 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
3550 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003551 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003552 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
3553
3554 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
3555 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Helmut Schaad13a97f2010-10-02 11:29:08 +02003556 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003557 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003558 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3559 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3560 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
3561 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
3562 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
3563 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003564 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003565 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
3566
3567 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
3568 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
3569 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003570 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003571 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3572 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3573 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
3574 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
3575 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
3576 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003577 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003578 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
3579
3580 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
3581 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
3582 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01003583 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003584 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
3585 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
3586 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
3587 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
3588 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
3589 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003590 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003591 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
3592
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01003593 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003594 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
3595
3596 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
3597 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
3598 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
3599 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
3600 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
3601 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
3602 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
3603 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
3604 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
3605 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
3606 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
3607 }
3608
Helmut Schaa961621a2010-11-04 20:36:59 +01003609 /*
3610 * The legacy driver also sets TXOP_CTRL_CFG_RESERVED_TRUN_EN to 1
3611 * although it is reserved.
3612 */
3613 rt2800_register_read(rt2x00dev, TXOP_CTRL_CFG, &reg);
3614 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TIMEOUT_TRUN_EN, 1);
3615 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_AC_TRUN_EN, 1);
3616 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TXRATEGRP_TRUN_EN, 1);
3617 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_USER_MODE_TRUN_EN, 1);
3618 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_MIMO_PS_TRUN_EN, 1);
3619 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_RESERVED_TRUN_EN, 1);
3620 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_LSIG_TXOP_EN, 0);
3621 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_EN, 0);
3622 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_DLY, 88);
3623 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CWMIN, 0);
3624 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, reg);
3625
Stanislaw Gruszka76413282013-03-16 19:19:33 +01003626 reg = rt2x00_rt(rt2x00dev, RT5592) ? 0x00000082 : 0x00000002;
3627 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003628
3629 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
3630 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
3631 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
3632 IEEE80211_MAX_RTS_THRESHOLD);
3633 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
3634 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
3635
3636 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003637
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02003638 /*
3639 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
3640 * time should be set to 16. However, the original Ralink driver uses
3641 * 16 for both and indeed using a value of 10 for CCK SIFS results in
3642 * connection problems with 11g + CTS protection. Hence, use the same
3643 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
3644 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003645 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02003646 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
3647 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003648 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
3649 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
3650 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
3651 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
3652
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003653 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
3654
3655 /*
3656 * ASIC will keep garbage value after boot, clear encryption keys.
3657 */
3658 for (i = 0; i < 4; i++)
3659 rt2800_register_write(rt2x00dev,
3660 SHARED_KEY_MODE_ENTRY(i), 0);
3661
3662 for (i = 0; i < 256; i++) {
Helmut Schaad7d259d2011-09-08 14:39:04 +02003663 rt2800_config_wcid(rt2x00dev, NULL, i);
3664 rt2800_delete_wcid_attr(rt2x00dev, i);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003665 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
3666 }
3667
3668 /*
3669 * Clear all beacons
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003670 */
Helmut Schaa69cf36a2011-01-30 13:16:03 +01003671 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE0);
3672 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE1);
3673 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE2);
3674 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE3);
3675 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE4);
3676 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE5);
3677 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE6);
3678 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE7);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003679
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01003680 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02003681 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
3682 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
3683 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
RA-Jay Hungc6fcc0e2011-01-30 13:21:22 +01003684 } else if (rt2x00_is_pcie(rt2x00dev)) {
3685 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
3686 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 125);
3687 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003688 }
3689
3690 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
3691 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
3692 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
3693 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
3694 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
3695 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
3696 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
3697 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
3698 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
3699 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
3700
3701 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
3702 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
3703 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
3704 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
3705 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
3706 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
3707 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
3708 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
3709 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
3710 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
3711
3712 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
3713 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
3714 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
3715 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
3716 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
3717 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
3718 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
3719 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
3720 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
3721 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
3722
3723 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
3724 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
3725 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
3726 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
3727 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
3728 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
3729
3730 /*
Helmut Schaa47ee3eb2010-09-08 20:56:04 +02003731 * Do not force the BA window size, we use the TXWI to set it
3732 */
3733 rt2800_register_read(rt2x00dev, AMPDU_BA_WINSIZE, &reg);
3734 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE_ENABLE, 0);
3735 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE, 0);
3736 rt2800_register_write(rt2x00dev, AMPDU_BA_WINSIZE, reg);
3737
3738 /*
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003739 * We must clear the error counters.
3740 * These registers are cleared on read,
3741 * so we may pass a useless variable to store the value.
3742 */
3743 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
3744 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
3745 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
3746 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
3747 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
3748 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
3749
Helmut Schaa9f926fb2010-07-11 12:28:23 +02003750 /*
3751 * Setup leadtime for pre tbtt interrupt to 6ms
3752 */
3753 rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
3754 rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
3755 rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
3756
Helmut Schaa977206d2010-12-13 12:31:58 +01003757 /*
3758 * Set up channel statistics timer
3759 */
3760 rt2800_register_read(rt2x00dev, CH_TIME_CFG, &reg);
3761 rt2x00_set_field32(&reg, CH_TIME_CFG_EIFS_BUSY, 1);
3762 rt2x00_set_field32(&reg, CH_TIME_CFG_NAV_BUSY, 1);
3763 rt2x00_set_field32(&reg, CH_TIME_CFG_RX_BUSY, 1);
3764 rt2x00_set_field32(&reg, CH_TIME_CFG_TX_BUSY, 1);
3765 rt2x00_set_field32(&reg, CH_TIME_CFG_TMR_EN, 1);
3766 rt2800_register_write(rt2x00dev, CH_TIME_CFG, reg);
3767
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003768 return 0;
3769}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003770
3771static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
3772{
3773 unsigned int i;
3774 u32 reg;
3775
3776 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
3777 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
3778 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
3779 return 0;
3780
3781 udelay(REGISTER_BUSY_DELAY);
3782 }
3783
3784 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
3785 return -EACCES;
3786}
3787
3788static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
3789{
3790 unsigned int i;
3791 u8 value;
3792
3793 /*
3794 * BBP was enabled after firmware was loaded,
3795 * but we need to reactivate it now.
3796 */
3797 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
3798 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
3799 msleep(1);
3800
3801 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
3802 rt2800_bbp_read(rt2x00dev, 0, &value);
3803 if ((value != 0xff) && (value != 0x00))
3804 return 0;
3805 udelay(REGISTER_BUSY_DELAY);
3806 }
3807
3808 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
3809 return -EACCES;
3810}
3811
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01003812static void rt2800_bbp4_mac_if_ctrl(struct rt2x00_dev *rt2x00dev)
3813{
3814 u8 value;
3815
3816 rt2800_bbp_read(rt2x00dev, 4, &value);
3817 rt2x00_set_field8(&value, BBP4_MAC_IF_CTRL, 1);
3818 rt2800_bbp_write(rt2x00dev, 4, value);
3819}
3820
Stanislaw Gruszkac2675482013-03-16 19:19:41 +01003821static void rt2800_init_freq_calibration(struct rt2x00_dev *rt2x00dev)
3822{
3823 rt2800_bbp_write(rt2x00dev, 142, 1);
3824 rt2800_bbp_write(rt2x00dev, 143, 57);
3825}
3826
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01003827static void rt2800_init_bbp_5592_glrt(struct rt2x00_dev *rt2x00dev)
3828{
3829 const u8 glrt_table[] = {
3830 0xE0, 0x1F, 0X38, 0x32, 0x08, 0x28, 0x19, 0x0A, 0xFF, 0x00, /* 128 ~ 137 */
3831 0x16, 0x10, 0x10, 0x0B, 0x36, 0x2C, 0x26, 0x24, 0x42, 0x36, /* 138 ~ 147 */
3832 0x30, 0x2D, 0x4C, 0x46, 0x3D, 0x40, 0x3E, 0x42, 0x3D, 0x40, /* 148 ~ 157 */
3833 0X3C, 0x34, 0x2C, 0x2F, 0x3C, 0x35, 0x2E, 0x2A, 0x49, 0x41, /* 158 ~ 167 */
3834 0x36, 0x31, 0x30, 0x30, 0x0E, 0x0D, 0x28, 0x21, 0x1C, 0x16, /* 168 ~ 177 */
3835 0x50, 0x4A, 0x43, 0x40, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, /* 178 ~ 187 */
3836 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 188 ~ 197 */
3837 0x00, 0x00, 0x7D, 0x14, 0x32, 0x2C, 0x36, 0x4C, 0x43, 0x2C, /* 198 ~ 207 */
3838 0x2E, 0x36, 0x30, 0x6E, /* 208 ~ 211 */
3839 };
3840 int i;
3841
3842 for (i = 0; i < ARRAY_SIZE(glrt_table); i++) {
3843 rt2800_bbp_write(rt2x00dev, 195, 128 + i);
3844 rt2800_bbp_write(rt2x00dev, 196, glrt_table[i]);
3845 }
3846};
3847
Stanislaw Gruszkaa4969d02013-03-16 19:19:35 +01003848static void rt2800_init_bbb_early(struct rt2x00_dev *rt2x00dev)
3849{
3850 rt2800_bbp_write(rt2x00dev, 65, 0x2C);
3851 rt2800_bbp_write(rt2x00dev, 66, 0x38);
3852 rt2800_bbp_write(rt2x00dev, 68, 0x0B);
3853 rt2800_bbp_write(rt2x00dev, 69, 0x12);
3854 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
3855 rt2800_bbp_write(rt2x00dev, 73, 0x10);
3856 rt2800_bbp_write(rt2x00dev, 81, 0x37);
3857 rt2800_bbp_write(rt2x00dev, 82, 0x62);
3858 rt2800_bbp_write(rt2x00dev, 83, 0x6A);
3859 rt2800_bbp_write(rt2x00dev, 84, 0x99);
3860 rt2800_bbp_write(rt2x00dev, 86, 0x00);
3861 rt2800_bbp_write(rt2x00dev, 91, 0x04);
3862 rt2800_bbp_write(rt2x00dev, 92, 0x00);
3863 rt2800_bbp_write(rt2x00dev, 103, 0x00);
3864 rt2800_bbp_write(rt2x00dev, 105, 0x05);
3865 rt2800_bbp_write(rt2x00dev, 106, 0x35);
3866}
3867
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01003868static void rt2800_init_bbp_5592(struct rt2x00_dev *rt2x00dev)
3869{
3870 int ant, div_mode;
3871 u16 eeprom;
3872 u8 value;
3873
Stanislaw Gruszkaa4969d02013-03-16 19:19:35 +01003874 rt2800_init_bbb_early(rt2x00dev);
3875
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01003876 rt2800_bbp_read(rt2x00dev, 105, &value);
3877 rt2x00_set_field8(&value, BBP105_MLD,
3878 rt2x00dev->default_ant.rx_chain_num == 2);
3879 rt2800_bbp_write(rt2x00dev, 105, value);
3880
3881 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
3882
3883 rt2800_bbp_write(rt2x00dev, 20, 0x06);
3884 rt2800_bbp_write(rt2x00dev, 31, 0x08);
3885 rt2800_bbp_write(rt2x00dev, 65, 0x2C);
3886 rt2800_bbp_write(rt2x00dev, 68, 0xDD);
3887 rt2800_bbp_write(rt2x00dev, 69, 0x1A);
3888 rt2800_bbp_write(rt2x00dev, 70, 0x05);
3889 rt2800_bbp_write(rt2x00dev, 73, 0x13);
3890 rt2800_bbp_write(rt2x00dev, 74, 0x0F);
3891 rt2800_bbp_write(rt2x00dev, 75, 0x4F);
3892 rt2800_bbp_write(rt2x00dev, 76, 0x28);
3893 rt2800_bbp_write(rt2x00dev, 77, 0x59);
3894 rt2800_bbp_write(rt2x00dev, 84, 0x9A);
3895 rt2800_bbp_write(rt2x00dev, 86, 0x38);
3896 rt2800_bbp_write(rt2x00dev, 88, 0x90);
3897 rt2800_bbp_write(rt2x00dev, 91, 0x04);
3898 rt2800_bbp_write(rt2x00dev, 92, 0x02);
3899 rt2800_bbp_write(rt2x00dev, 95, 0x9a);
3900 rt2800_bbp_write(rt2x00dev, 98, 0x12);
3901 rt2800_bbp_write(rt2x00dev, 103, 0xC0);
3902 rt2800_bbp_write(rt2x00dev, 104, 0x92);
3903 /* FIXME BBP105 owerwrite */
3904 rt2800_bbp_write(rt2x00dev, 105, 0x3C);
3905 rt2800_bbp_write(rt2x00dev, 106, 0x35);
3906 rt2800_bbp_write(rt2x00dev, 128, 0x12);
3907 rt2800_bbp_write(rt2x00dev, 134, 0xD0);
3908 rt2800_bbp_write(rt2x00dev, 135, 0xF6);
3909 rt2800_bbp_write(rt2x00dev, 137, 0x0F);
3910
3911 /* Initialize GLRT (Generalized Likehood Radio Test) */
3912 rt2800_init_bbp_5592_glrt(rt2x00dev);
3913
3914 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
3915
3916 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
3917 div_mode = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_ANT_DIVERSITY);
3918 ant = (div_mode == 3) ? 1 : 0;
3919 rt2800_bbp_read(rt2x00dev, 152, &value);
3920 if (ant == 0) {
3921 /* Main antenna */
3922 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
3923 } else {
3924 /* Auxiliary antenna */
3925 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
3926 }
3927 rt2800_bbp_write(rt2x00dev, 152, value);
3928
3929 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C)) {
3930 rt2800_bbp_read(rt2x00dev, 254, &value);
3931 rt2x00_set_field8(&value, BBP254_BIT7, 1);
3932 rt2800_bbp_write(rt2x00dev, 254, value);
3933 }
3934
Stanislaw Gruszkac2675482013-03-16 19:19:41 +01003935 rt2800_init_freq_calibration(rt2x00dev);
3936
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01003937 rt2800_bbp_write(rt2x00dev, 84, 0x19);
Stanislaw Gruszka6e04f252013-03-16 19:19:38 +01003938 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C))
3939 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01003940}
3941
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003942static int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003943{
3944 unsigned int i;
3945 u16 eeprom;
3946 u8 reg_id;
3947 u8 value;
3948
3949 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
3950 rt2800_wait_bbp_ready(rt2x00dev)))
3951 return -EACCES;
3952
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01003953 if (rt2x00_rt(rt2x00dev, RT5592)) {
3954 rt2800_init_bbp_5592(rt2x00dev);
3955 return 0;
3956 }
3957
Daniel Golle03839952012-09-09 14:24:39 +03003958 if (rt2x00_rt(rt2x00dev, RT3352)) {
3959 rt2800_bbp_write(rt2x00dev, 3, 0x00);
3960 rt2800_bbp_write(rt2x00dev, 4, 0x50);
3961 }
3962
Woody Hunga89534e2012-06-13 15:01:16 +08003963 if (rt2x00_rt(rt2x00dev, RT3290) ||
3964 rt2x00_rt(rt2x00dev, RT5390) ||
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01003965 rt2x00_rt(rt2x00dev, RT5392))
3966 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003967
Gabor Juhosadde5882011-03-03 11:46:45 +01003968 if (rt2800_is_305x_soc(rt2x00dev) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003969 rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003970 rt2x00_rt(rt2x00dev, RT3352) ||
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003971 rt2x00_rt(rt2x00dev, RT3572) ||
John Li2ed71882012-02-17 17:33:06 +08003972 rt2x00_rt(rt2x00dev, RT5390) ||
3973 rt2x00_rt(rt2x00dev, RT5392))
Helmut Schaabaff8002010-04-28 09:58:59 +02003974 rt2800_bbp_write(rt2x00dev, 31, 0x08);
3975
Daniel Golle03839952012-09-09 14:24:39 +03003976 if (rt2x00_rt(rt2x00dev, RT3352))
3977 rt2800_bbp_write(rt2x00dev, 47, 0x48);
3978
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003979 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
3980 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003981
Woody Hunga89534e2012-06-13 15:01:16 +08003982 if (rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003983 rt2x00_rt(rt2x00dev, RT3352) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003984 rt2x00_rt(rt2x00dev, RT5390) ||
3985 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01003986 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003987
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02003988 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
3989 rt2800_bbp_write(rt2x00dev, 69, 0x16);
3990 rt2800_bbp_write(rt2x00dev, 73, 0x12);
Woody Hunga89534e2012-06-13 15:01:16 +08003991 } else if (rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003992 rt2x00_rt(rt2x00dev, RT3352) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003993 rt2x00_rt(rt2x00dev, RT5390) ||
3994 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01003995 rt2800_bbp_write(rt2x00dev, 69, 0x12);
3996 rt2800_bbp_write(rt2x00dev, 73, 0x13);
3997 rt2800_bbp_write(rt2x00dev, 75, 0x46);
3998 rt2800_bbp_write(rt2x00dev, 76, 0x28);
Woody Hunga89534e2012-06-13 15:01:16 +08003999
4000 if (rt2x00_rt(rt2x00dev, RT3290))
4001 rt2800_bbp_write(rt2x00dev, 77, 0x58);
4002 else
4003 rt2800_bbp_write(rt2x00dev, 77, 0x59);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004004 } else {
4005 rt2800_bbp_write(rt2x00dev, 69, 0x12);
4006 rt2800_bbp_write(rt2x00dev, 73, 0x10);
4007 }
4008
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004009 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004010
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004011 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004012 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004013 rt2x00_rt(rt2x00dev, RT3090) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01004014 rt2x00_rt(rt2x00dev, RT3390) ||
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004015 rt2x00_rt(rt2x00dev, RT3572) ||
John Li2ed71882012-02-17 17:33:06 +08004016 rt2x00_rt(rt2x00dev, RT5390) ||
4017 rt2x00_rt(rt2x00dev, RT5392)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004018 rt2800_bbp_write(rt2x00dev, 79, 0x13);
4019 rt2800_bbp_write(rt2x00dev, 80, 0x05);
4020 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Helmut Schaabaff8002010-04-28 09:58:59 +02004021 } else if (rt2800_is_305x_soc(rt2x00dev)) {
4022 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
4023 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Gertjan van Wingerde59d12872012-09-16 22:29:51 +02004024 } else if (rt2x00_rt(rt2x00dev, RT3290)) {
4025 rt2800_bbp_write(rt2x00dev, 74, 0x0b);
4026 rt2800_bbp_write(rt2x00dev, 79, 0x18);
4027 rt2800_bbp_write(rt2x00dev, 80, 0x09);
4028 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Daniel Golle03839952012-09-09 14:24:39 +03004029 } else if (rt2x00_rt(rt2x00dev, RT3352)) {
4030 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
4031 rt2800_bbp_write(rt2x00dev, 80, 0x08);
4032 rt2800_bbp_write(rt2x00dev, 81, 0x37);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004033 } else {
4034 rt2800_bbp_write(rt2x00dev, 81, 0x37);
4035 }
4036
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004037 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Woody Hunga89534e2012-06-13 15:01:16 +08004038 if (rt2x00_rt(rt2x00dev, RT3290) ||
4039 rt2x00_rt(rt2x00dev, RT5390) ||
4040 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01004041 rt2800_bbp_write(rt2x00dev, 83, 0x7a);
4042 else
4043 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004044
Gertjan van Wingerde5ed8f452010-06-03 10:51:57 +02004045 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004046 rt2800_bbp_write(rt2x00dev, 84, 0x19);
Woody Hunga89534e2012-06-13 15:01:16 +08004047 else if (rt2x00_rt(rt2x00dev, RT3290) ||
Gabor Juhose6d227b2012-12-02 15:53:28 +01004048 rt2x00_rt(rt2x00dev, RT5390) ||
4049 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01004050 rt2800_bbp_write(rt2x00dev, 84, 0x9a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004051 else
4052 rt2800_bbp_write(rt2x00dev, 84, 0x99);
4053
Woody Hunga89534e2012-06-13 15:01:16 +08004054 if (rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03004055 rt2x00_rt(rt2x00dev, RT3352) ||
Woody Hunga89534e2012-06-13 15:01:16 +08004056 rt2x00_rt(rt2x00dev, RT5390) ||
4057 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01004058 rt2800_bbp_write(rt2x00dev, 86, 0x38);
4059 else
4060 rt2800_bbp_write(rt2x00dev, 86, 0x00);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004061
Daniel Golle03839952012-09-09 14:24:39 +03004062 if (rt2x00_rt(rt2x00dev, RT3352) ||
4063 rt2x00_rt(rt2x00dev, RT5392))
John Li2ed71882012-02-17 17:33:06 +08004064 rt2800_bbp_write(rt2x00dev, 88, 0x90);
4065
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004066 rt2800_bbp_write(rt2x00dev, 91, 0x04);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004067
Woody Hunga89534e2012-06-13 15:01:16 +08004068 if (rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03004069 rt2x00_rt(rt2x00dev, RT3352) ||
Woody Hunga89534e2012-06-13 15:01:16 +08004070 rt2x00_rt(rt2x00dev, RT5390) ||
4071 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01004072 rt2800_bbp_write(rt2x00dev, 92, 0x02);
4073 else
4074 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004075
John Li2ed71882012-02-17 17:33:06 +08004076 if (rt2x00_rt(rt2x00dev, RT5392)) {
4077 rt2800_bbp_write(rt2x00dev, 95, 0x9a);
4078 rt2800_bbp_write(rt2x00dev, 98, 0x12);
4079 }
4080
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004081 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004082 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004083 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02004084 rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
Woody Hunga89534e2012-06-13 15:01:16 +08004085 rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03004086 rt2x00_rt(rt2x00dev, RT3352) ||
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004087 rt2x00_rt(rt2x00dev, RT3572) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01004088 rt2x00_rt(rt2x00dev, RT5390) ||
John Li2ed71882012-02-17 17:33:06 +08004089 rt2x00_rt(rt2x00dev, RT5392) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02004090 rt2800_is_305x_soc(rt2x00dev))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004091 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
4092 else
4093 rt2800_bbp_write(rt2x00dev, 103, 0x00);
4094
Woody Hunga89534e2012-06-13 15:01:16 +08004095 if (rt2x00_rt(rt2x00dev, RT3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03004096 rt2x00_rt(rt2x00dev, RT3352) ||
Woody Hunga89534e2012-06-13 15:01:16 +08004097 rt2x00_rt(rt2x00dev, RT5390) ||
4098 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01004099 rt2800_bbp_write(rt2x00dev, 104, 0x92);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004100
Helmut Schaabaff8002010-04-28 09:58:59 +02004101 if (rt2800_is_305x_soc(rt2x00dev))
4102 rt2800_bbp_write(rt2x00dev, 105, 0x01);
Woody Hunga89534e2012-06-13 15:01:16 +08004103 else if (rt2x00_rt(rt2x00dev, RT3290))
4104 rt2800_bbp_write(rt2x00dev, 105, 0x1c);
Daniel Golle03839952012-09-09 14:24:39 +03004105 else if (rt2x00_rt(rt2x00dev, RT3352))
4106 rt2800_bbp_write(rt2x00dev, 105, 0x34);
John Li2ed71882012-02-17 17:33:06 +08004107 else if (rt2x00_rt(rt2x00dev, RT5390) ||
Gabor Juhose6d227b2012-12-02 15:53:28 +01004108 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01004109 rt2800_bbp_write(rt2x00dev, 105, 0x3c);
Helmut Schaabaff8002010-04-28 09:58:59 +02004110 else
4111 rt2800_bbp_write(rt2x00dev, 105, 0x05);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004112
Woody Hunga89534e2012-06-13 15:01:16 +08004113 if (rt2x00_rt(rt2x00dev, RT3290) ||
4114 rt2x00_rt(rt2x00dev, RT5390))
Gabor Juhosadde5882011-03-03 11:46:45 +01004115 rt2800_bbp_write(rt2x00dev, 106, 0x03);
Daniel Golle03839952012-09-09 14:24:39 +03004116 else if (rt2x00_rt(rt2x00dev, RT3352))
4117 rt2800_bbp_write(rt2x00dev, 106, 0x05);
John Li2ed71882012-02-17 17:33:06 +08004118 else if (rt2x00_rt(rt2x00dev, RT5392))
4119 rt2800_bbp_write(rt2x00dev, 106, 0x12);
Gabor Juhosadde5882011-03-03 11:46:45 +01004120 else
4121 rt2800_bbp_write(rt2x00dev, 106, 0x35);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004122
Daniel Golle03839952012-09-09 14:24:39 +03004123 if (rt2x00_rt(rt2x00dev, RT3352))
4124 rt2800_bbp_write(rt2x00dev, 120, 0x50);
4125
Woody Hunga89534e2012-06-13 15:01:16 +08004126 if (rt2x00_rt(rt2x00dev, RT3290) ||
4127 rt2x00_rt(rt2x00dev, RT5390) ||
4128 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhosadde5882011-03-03 11:46:45 +01004129 rt2800_bbp_write(rt2x00dev, 128, 0x12);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004130
John Li2ed71882012-02-17 17:33:06 +08004131 if (rt2x00_rt(rt2x00dev, RT5392)) {
4132 rt2800_bbp_write(rt2x00dev, 134, 0xd0);
4133 rt2800_bbp_write(rt2x00dev, 135, 0xf6);
4134 }
4135
Daniel Golle03839952012-09-09 14:24:39 +03004136 if (rt2x00_rt(rt2x00dev, RT3352))
4137 rt2800_bbp_write(rt2x00dev, 137, 0x0f);
4138
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004139 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004140 rt2x00_rt(rt2x00dev, RT3090) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01004141 rt2x00_rt(rt2x00dev, RT3390) ||
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004142 rt2x00_rt(rt2x00dev, RT3572) ||
John Li2ed71882012-02-17 17:33:06 +08004143 rt2x00_rt(rt2x00dev, RT5390) ||
4144 rt2x00_rt(rt2x00dev, RT5392)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004145 rt2800_bbp_read(rt2x00dev, 138, &value);
4146
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004147 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
4148 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004149 value |= 0x20;
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004150 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004151 value &= ~0x02;
4152
4153 rt2800_bbp_write(rt2x00dev, 138, value);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004154 }
4155
Woody Hunga89534e2012-06-13 15:01:16 +08004156 if (rt2x00_rt(rt2x00dev, RT3290)) {
4157 rt2800_bbp_write(rt2x00dev, 67, 0x24);
4158 rt2800_bbp_write(rt2x00dev, 143, 0x04);
4159 rt2800_bbp_write(rt2x00dev, 142, 0x99);
4160 rt2800_bbp_write(rt2x00dev, 150, 0x30);
4161 rt2800_bbp_write(rt2x00dev, 151, 0x2e);
4162 rt2800_bbp_write(rt2x00dev, 152, 0x20);
4163 rt2800_bbp_write(rt2x00dev, 153, 0x34);
4164 rt2800_bbp_write(rt2x00dev, 154, 0x40);
4165 rt2800_bbp_write(rt2x00dev, 155, 0x3b);
4166 rt2800_bbp_write(rt2x00dev, 253, 0x04);
4167
4168 rt2800_bbp_read(rt2x00dev, 47, &value);
4169 rt2x00_set_field8(&value, BBP47_TSSI_ADC6, 1);
4170 rt2800_bbp_write(rt2x00dev, 47, value);
4171
4172 /* Use 5-bit ADC for Acquisition and 8-bit ADC for data */
4173 rt2800_bbp_read(rt2x00dev, 3, &value);
4174 rt2x00_set_field8(&value, BBP3_ADC_MODE_SWITCH, 1);
4175 rt2x00_set_field8(&value, BBP3_ADC_INIT_MODE, 1);
4176 rt2800_bbp_write(rt2x00dev, 3, value);
4177 }
4178
Daniel Golle03839952012-09-09 14:24:39 +03004179 if (rt2x00_rt(rt2x00dev, RT3352)) {
4180 rt2800_bbp_write(rt2x00dev, 163, 0xbd);
4181 /* Set ITxBF timeout to 0x9c40=1000msec */
4182 rt2800_bbp_write(rt2x00dev, 179, 0x02);
4183 rt2800_bbp_write(rt2x00dev, 180, 0x00);
4184 rt2800_bbp_write(rt2x00dev, 182, 0x40);
4185 rt2800_bbp_write(rt2x00dev, 180, 0x01);
4186 rt2800_bbp_write(rt2x00dev, 182, 0x9c);
4187 rt2800_bbp_write(rt2x00dev, 179, 0x00);
4188 /* Reprogram the inband interface to put right values in RXWI */
4189 rt2800_bbp_write(rt2x00dev, 142, 0x04);
4190 rt2800_bbp_write(rt2x00dev, 143, 0x3b);
4191 rt2800_bbp_write(rt2x00dev, 142, 0x06);
4192 rt2800_bbp_write(rt2x00dev, 143, 0xa0);
4193 rt2800_bbp_write(rt2x00dev, 142, 0x07);
4194 rt2800_bbp_write(rt2x00dev, 143, 0xa1);
4195 rt2800_bbp_write(rt2x00dev, 142, 0x08);
4196 rt2800_bbp_write(rt2x00dev, 143, 0xa2);
4197
4198 rt2800_bbp_write(rt2x00dev, 148, 0xc8);
4199 }
4200
John Li2ed71882012-02-17 17:33:06 +08004201 if (rt2x00_rt(rt2x00dev, RT5390) ||
Gabor Juhose6d227b2012-12-02 15:53:28 +01004202 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01004203 int ant, div_mode;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004204
Gabor Juhosadde5882011-03-03 11:46:45 +01004205 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
4206 div_mode = rt2x00_get_field16(eeprom,
4207 EEPROM_NIC_CONF1_ANT_DIVERSITY);
4208 ant = (div_mode == 3) ? 1 : 0;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004209
Gabor Juhosadde5882011-03-03 11:46:45 +01004210 /* check if this is a Bluetooth combo card */
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02004211 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01004212 u32 reg;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004213
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02004214 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
4215 rt2x00_set_field32(&reg, GPIO_CTRL_DIR3, 0);
4216 rt2x00_set_field32(&reg, GPIO_CTRL_DIR6, 0);
4217 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, 0);
4218 rt2x00_set_field32(&reg, GPIO_CTRL_VAL6, 0);
Gabor Juhosadde5882011-03-03 11:46:45 +01004219 if (ant == 0)
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02004220 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, 1);
Gabor Juhosadde5882011-03-03 11:46:45 +01004221 else if (ant == 1)
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02004222 rt2x00_set_field32(&reg, GPIO_CTRL_VAL6, 1);
4223 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
Gabor Juhosadde5882011-03-03 11:46:45 +01004224 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004225
Anisse Astier0586a112012-04-23 12:33:11 +02004226 /* This chip has hardware antenna diversity*/
4227 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390R)) {
4228 rt2800_bbp_write(rt2x00dev, 150, 0); /* Disable Antenna Software OFDM */
4229 rt2800_bbp_write(rt2x00dev, 151, 0); /* Disable Antenna Software CCK */
4230 rt2800_bbp_write(rt2x00dev, 154, 0); /* Clear previously selected antenna */
4231 }
4232
Gabor Juhosadde5882011-03-03 11:46:45 +01004233 rt2800_bbp_read(rt2x00dev, 152, &value);
4234 if (ant == 0)
4235 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
4236 else
4237 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
4238 rt2800_bbp_write(rt2x00dev, 152, value);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004239
Stanislaw Gruszkac2675482013-03-16 19:19:41 +01004240 rt2800_init_freq_calibration(rt2x00dev);
Gabor Juhosadde5882011-03-03 11:46:45 +01004241 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004242
4243 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
4244 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
4245
4246 if (eeprom != 0xffff && eeprom != 0x0000) {
4247 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
4248 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
4249 rt2800_bbp_write(rt2x00dev, reg_id, value);
4250 }
4251 }
4252
4253 return 0;
4254}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004255
4256static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
4257 bool bw40, u8 rfcsr24, u8 filter_target)
4258{
4259 unsigned int i;
4260 u8 bbp;
4261 u8 rfcsr;
4262 u8 passband;
4263 u8 stopband;
4264 u8 overtuned = 0;
4265
4266 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
4267
4268 rt2800_bbp_read(rt2x00dev, 4, &bbp);
4269 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
4270 rt2800_bbp_write(rt2x00dev, 4, bbp);
4271
RA-Jay Hung80d184e2011-01-10 11:28:10 +01004272 rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
4273 rt2x00_set_field8(&rfcsr, RFCSR31_RX_H20M, bw40);
4274 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
4275
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004276 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
4277 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
4278 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
4279
4280 /*
4281 * Set power & frequency of passband test tone
4282 */
4283 rt2800_bbp_write(rt2x00dev, 24, 0);
4284
4285 for (i = 0; i < 100; i++) {
4286 rt2800_bbp_write(rt2x00dev, 25, 0x90);
4287 msleep(1);
4288
4289 rt2800_bbp_read(rt2x00dev, 55, &passband);
4290 if (passband)
4291 break;
4292 }
4293
4294 /*
4295 * Set power & frequency of stopband test tone
4296 */
4297 rt2800_bbp_write(rt2x00dev, 24, 0x06);
4298
4299 for (i = 0; i < 100; i++) {
4300 rt2800_bbp_write(rt2x00dev, 25, 0x90);
4301 msleep(1);
4302
4303 rt2800_bbp_read(rt2x00dev, 55, &stopband);
4304
4305 if ((passband - stopband) <= filter_target) {
4306 rfcsr24++;
4307 overtuned += ((passband - stopband) == filter_target);
4308 } else
4309 break;
4310
4311 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
4312 }
4313
4314 rfcsr24 -= !!overtuned;
4315
4316 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
4317 return rfcsr24;
4318}
4319
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01004320static void rt2800_init_rfcsr_305x_soc(struct rt2x00_dev *rt2x00dev)
4321{
4322 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
4323 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
4324 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
4325 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
4326 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
4327 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
4328 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
4329 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
4330 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
4331 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
4332 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
4333 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
4334 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
4335 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
4336 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
4337 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
4338 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
4339 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
4340 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
4341 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
4342 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
4343 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
4344 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
4345 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
4346 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
4347 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
4348 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
4349 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
4350 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
4351 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
4352 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
4353 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
4354}
4355
4356static void rt2800_init_rfcsr_30xx(struct rt2x00_dev *rt2x00dev)
4357{
4358 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
4359 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
4360 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
4361 rt2800_rfcsr_write(rt2x00dev, 7, 0x60);
4362 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
4363 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
4364 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
4365 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
4366 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
4367 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
4368 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
4369 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
4370 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
4371 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
4372 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
4373 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
4374 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
4375 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
4376 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
4377}
4378
4379static void rt2800_init_rfcsr_3290(struct rt2x00_dev *rt2x00dev)
4380{
4381 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
4382 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
4383 rt2800_rfcsr_write(rt2x00dev, 3, 0x08);
4384 rt2800_rfcsr_write(rt2x00dev, 4, 0x00);
4385 rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
4386 rt2800_rfcsr_write(rt2x00dev, 8, 0xf3);
4387 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
4388 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
4389 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
4390 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
4391 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
4392 rt2800_rfcsr_write(rt2x00dev, 18, 0x02);
4393 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
4394 rt2800_rfcsr_write(rt2x00dev, 25, 0x83);
4395 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
4396 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
4397 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
4398 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
4399 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
4400 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
4401 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
4402 rt2800_rfcsr_write(rt2x00dev, 34, 0x05);
4403 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
4404 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
4405 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
4406 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
4407 rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
4408 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
4409 rt2800_rfcsr_write(rt2x00dev, 42, 0xd5);
4410 rt2800_rfcsr_write(rt2x00dev, 43, 0x7b);
4411 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
4412 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
4413 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
4414 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
4415 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
4416 rt2800_rfcsr_write(rt2x00dev, 49, 0x98);
4417 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
4418 rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
4419 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
4420 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
4421 rt2800_rfcsr_write(rt2x00dev, 56, 0x02);
4422 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
4423 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
4424 rt2800_rfcsr_write(rt2x00dev, 59, 0x09);
4425 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
4426 rt2800_rfcsr_write(rt2x00dev, 61, 0xc1);
4427}
4428
4429static void rt2800_init_rfcsr_3352(struct rt2x00_dev *rt2x00dev)
4430{
4431 rt2800_rfcsr_write(rt2x00dev, 0, 0xf0);
4432 rt2800_rfcsr_write(rt2x00dev, 1, 0x23);
4433 rt2800_rfcsr_write(rt2x00dev, 2, 0x50);
4434 rt2800_rfcsr_write(rt2x00dev, 3, 0x18);
4435 rt2800_rfcsr_write(rt2x00dev, 4, 0x00);
4436 rt2800_rfcsr_write(rt2x00dev, 5, 0x00);
4437 rt2800_rfcsr_write(rt2x00dev, 6, 0x33);
4438 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
4439 rt2800_rfcsr_write(rt2x00dev, 8, 0xf1);
4440 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
4441 rt2800_rfcsr_write(rt2x00dev, 10, 0xd2);
4442 rt2800_rfcsr_write(rt2x00dev, 11, 0x42);
4443 rt2800_rfcsr_write(rt2x00dev, 12, 0x1c);
4444 rt2800_rfcsr_write(rt2x00dev, 13, 0x00);
4445 rt2800_rfcsr_write(rt2x00dev, 14, 0x5a);
4446 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
4447 rt2800_rfcsr_write(rt2x00dev, 16, 0x01);
4448 rt2800_rfcsr_write(rt2x00dev, 18, 0x45);
4449 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
4450 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
4451 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
4452 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
4453 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
4454 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
4455 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
4456 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
4457 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
4458 rt2800_rfcsr_write(rt2x00dev, 28, 0x03);
4459 rt2800_rfcsr_write(rt2x00dev, 29, 0x00);
4460 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
4461 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
4462 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
4463 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
4464 rt2800_rfcsr_write(rt2x00dev, 34, 0x01);
4465 rt2800_rfcsr_write(rt2x00dev, 35, 0x03);
4466 rt2800_rfcsr_write(rt2x00dev, 36, 0xbd);
4467 rt2800_rfcsr_write(rt2x00dev, 37, 0x3c);
4468 rt2800_rfcsr_write(rt2x00dev, 38, 0x5f);
4469 rt2800_rfcsr_write(rt2x00dev, 39, 0xc5);
4470 rt2800_rfcsr_write(rt2x00dev, 40, 0x33);
4471 rt2800_rfcsr_write(rt2x00dev, 41, 0x5b);
4472 rt2800_rfcsr_write(rt2x00dev, 42, 0x5b);
4473 rt2800_rfcsr_write(rt2x00dev, 43, 0xdb);
4474 rt2800_rfcsr_write(rt2x00dev, 44, 0xdb);
4475 rt2800_rfcsr_write(rt2x00dev, 45, 0xdb);
4476 rt2800_rfcsr_write(rt2x00dev, 46, 0xdd);
4477 rt2800_rfcsr_write(rt2x00dev, 47, 0x0d);
4478 rt2800_rfcsr_write(rt2x00dev, 48, 0x14);
4479 rt2800_rfcsr_write(rt2x00dev, 49, 0x00);
4480 rt2800_rfcsr_write(rt2x00dev, 50, 0x2d);
4481 rt2800_rfcsr_write(rt2x00dev, 51, 0x7f);
4482 rt2800_rfcsr_write(rt2x00dev, 52, 0x00);
4483 rt2800_rfcsr_write(rt2x00dev, 53, 0x52);
4484 rt2800_rfcsr_write(rt2x00dev, 54, 0x1b);
4485 rt2800_rfcsr_write(rt2x00dev, 55, 0x7f);
4486 rt2800_rfcsr_write(rt2x00dev, 56, 0x00);
4487 rt2800_rfcsr_write(rt2x00dev, 57, 0x52);
4488 rt2800_rfcsr_write(rt2x00dev, 58, 0x1b);
4489 rt2800_rfcsr_write(rt2x00dev, 59, 0x00);
4490 rt2800_rfcsr_write(rt2x00dev, 60, 0x00);
4491 rt2800_rfcsr_write(rt2x00dev, 61, 0x00);
4492 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
4493 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
4494}
4495
4496static void rt2800_init_rfcsr_3390(struct rt2x00_dev *rt2x00dev)
4497{
4498 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
4499 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
4500 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
4501 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
4502 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
4503 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
4504 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
4505 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
4506 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
4507 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
4508 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
4509 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
4510 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
4511 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
4512 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
4513 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
4514 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
4515 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
4516 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
4517 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
4518 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
4519 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
4520 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
4521 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
4522 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
4523 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
4524 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
4525 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
4526 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
4527 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
4528 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
4529 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
4530}
4531
4532static void rt2800_init_rfcsr_3572(struct rt2x00_dev *rt2x00dev)
4533{
4534 rt2800_rfcsr_write(rt2x00dev, 0, 0x70);
4535 rt2800_rfcsr_write(rt2x00dev, 1, 0x81);
4536 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
4537 rt2800_rfcsr_write(rt2x00dev, 3, 0x02);
4538 rt2800_rfcsr_write(rt2x00dev, 4, 0x4c);
4539 rt2800_rfcsr_write(rt2x00dev, 5, 0x05);
4540 rt2800_rfcsr_write(rt2x00dev, 6, 0x4a);
4541 rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
4542 rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
4543 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
4544 rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
4545 rt2800_rfcsr_write(rt2x00dev, 12, 0x70);
4546 rt2800_rfcsr_write(rt2x00dev, 13, 0x65);
4547 rt2800_rfcsr_write(rt2x00dev, 14, 0xa0);
4548 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
4549 rt2800_rfcsr_write(rt2x00dev, 16, 0x4c);
4550 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
4551 rt2800_rfcsr_write(rt2x00dev, 18, 0xac);
4552 rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
4553 rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
4554 rt2800_rfcsr_write(rt2x00dev, 21, 0xd0);
4555 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
4556 rt2800_rfcsr_write(rt2x00dev, 23, 0x3c);
4557 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
4558 rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
4559 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
4560 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
4561 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
4562 rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
4563 rt2800_rfcsr_write(rt2x00dev, 30, 0x09);
4564 rt2800_rfcsr_write(rt2x00dev, 31, 0x10);
4565}
4566
4567static void rt2800_init_rfcsr_5390(struct rt2x00_dev *rt2x00dev)
4568{
4569 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
4570 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
4571 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
4572 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
4573 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4574 rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
4575 else
4576 rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
4577 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
4578 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
4579 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
4580 rt2800_rfcsr_write(rt2x00dev, 12, 0xc6);
4581 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
4582 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
4583 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
4584 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
4585 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
4586 rt2800_rfcsr_write(rt2x00dev, 19, 0x00);
4587
4588 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
4589 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
4590 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
4591 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
4592 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
4593 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4594 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
4595 else
4596 rt2800_rfcsr_write(rt2x00dev, 25, 0xc0);
4597 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
4598 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
4599 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
4600 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
4601
4602 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
4603 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
4604 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
4605 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
4606 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
4607 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
4608 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
4609 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
4610 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
4611 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
4612
4613 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4614 rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
4615 else
4616 rt2800_rfcsr_write(rt2x00dev, 40, 0x4b);
4617 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
4618 rt2800_rfcsr_write(rt2x00dev, 42, 0xd2);
4619 rt2800_rfcsr_write(rt2x00dev, 43, 0x9a);
4620 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
4621 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
4622 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4623 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
4624 else
4625 rt2800_rfcsr_write(rt2x00dev, 46, 0x7b);
4626 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
4627 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
4628 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
4629
4630 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
4631 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4632 rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
4633 else
4634 rt2800_rfcsr_write(rt2x00dev, 53, 0x84);
4635 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
4636 rt2800_rfcsr_write(rt2x00dev, 55, 0x44);
4637 rt2800_rfcsr_write(rt2x00dev, 56, 0x22);
4638 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
4639 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
4640 rt2800_rfcsr_write(rt2x00dev, 59, 0x63);
4641
4642 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
4643 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
4644 rt2800_rfcsr_write(rt2x00dev, 61, 0xd1);
4645 else
4646 rt2800_rfcsr_write(rt2x00dev, 61, 0xdd);
4647 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
4648 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
4649}
4650
4651static void rt2800_init_rfcsr_5392(struct rt2x00_dev *rt2x00dev)
4652{
4653 rt2800_rfcsr_write(rt2x00dev, 1, 0x17);
4654 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
4655 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
4656 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
4657 rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
4658 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
4659 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
4660 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
4661 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
4662 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
4663 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
4664 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
4665 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
4666 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
4667 rt2800_rfcsr_write(rt2x00dev, 19, 0x4d);
4668 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
4669 rt2800_rfcsr_write(rt2x00dev, 21, 0x8d);
4670 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
4671 rt2800_rfcsr_write(rt2x00dev, 23, 0x0b);
4672 rt2800_rfcsr_write(rt2x00dev, 24, 0x44);
4673 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
4674 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
4675 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
4676 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
4677 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
4678 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
4679 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
4680 rt2800_rfcsr_write(rt2x00dev, 32, 0x20);
4681 rt2800_rfcsr_write(rt2x00dev, 33, 0xC0);
4682 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
4683 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
4684 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
4685 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
4686 rt2800_rfcsr_write(rt2x00dev, 38, 0x89);
4687 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
4688 rt2800_rfcsr_write(rt2x00dev, 40, 0x0f);
4689 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
4690 rt2800_rfcsr_write(rt2x00dev, 42, 0xd5);
4691 rt2800_rfcsr_write(rt2x00dev, 43, 0x9b);
4692 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
4693 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
4694 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
4695 rt2800_rfcsr_write(rt2x00dev, 47, 0x0c);
4696 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
4697 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
4698 rt2800_rfcsr_write(rt2x00dev, 50, 0x94);
4699 rt2800_rfcsr_write(rt2x00dev, 51, 0x3a);
4700 rt2800_rfcsr_write(rt2x00dev, 52, 0x48);
4701 rt2800_rfcsr_write(rt2x00dev, 53, 0x44);
4702 rt2800_rfcsr_write(rt2x00dev, 54, 0x38);
4703 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
4704 rt2800_rfcsr_write(rt2x00dev, 56, 0xa1);
4705 rt2800_rfcsr_write(rt2x00dev, 57, 0x00);
4706 rt2800_rfcsr_write(rt2x00dev, 58, 0x39);
4707 rt2800_rfcsr_write(rt2x00dev, 59, 0x07);
4708 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
4709 rt2800_rfcsr_write(rt2x00dev, 61, 0x91);
4710 rt2800_rfcsr_write(rt2x00dev, 62, 0x39);
4711 rt2800_rfcsr_write(rt2x00dev, 63, 0x07);
4712}
4713
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01004714static void rt2800_init_rfcsr_5592(struct rt2x00_dev *rt2x00dev)
4715{
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01004716 u8 reg;
4717 u16 eeprom;
4718
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01004719 rt2800_rfcsr_write(rt2x00dev, 1, 0x3F);
4720 rt2800_rfcsr_write(rt2x00dev, 3, 0x08);
4721 rt2800_rfcsr_write(rt2x00dev, 3, 0x08);
4722 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
4723 rt2800_rfcsr_write(rt2x00dev, 6, 0xE4);
4724 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
4725 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
4726 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
4727 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
4728 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
4729 rt2800_rfcsr_write(rt2x00dev, 19, 0x4D);
4730 rt2800_rfcsr_write(rt2x00dev, 20, 0x10);
4731 rt2800_rfcsr_write(rt2x00dev, 21, 0x8D);
4732 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
4733 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
4734 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
4735 rt2800_rfcsr_write(rt2x00dev, 33, 0xC0);
4736 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
4737 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
4738 rt2800_rfcsr_write(rt2x00dev, 47, 0x0C);
4739 rt2800_rfcsr_write(rt2x00dev, 53, 0x22);
4740 rt2800_rfcsr_write(rt2x00dev, 63, 0x07);
4741
4742 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
4743 msleep(1);
4744
4745 rt2800_adjust_freq_offset(rt2x00dev);
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01004746
4747 rt2800_bbp_read(rt2x00dev, 138, &reg);
4748
4749 /* Turn off unused DAC1 and ADC1 to reduce power consumption */
4750 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
4751 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
4752 rt2x00_set_field8(&reg, BBP138_RX_ADC1, 0);
4753 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
4754 rt2x00_set_field8(&reg, BBP138_TX_DAC1, 1);
4755
4756 rt2800_bbp_write(rt2x00dev, 138, reg);
4757
4758 /* Enable DC filter */
4759 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C))
4760 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
4761
4762 rt2800_rfcsr_read(rt2x00dev, 38, &reg);
4763 rt2x00_set_field8(&reg, RFCSR38_RX_LO1_EN, 0);
4764 rt2800_rfcsr_write(rt2x00dev, 38, reg);
4765
4766 rt2800_rfcsr_read(rt2x00dev, 39, &reg);
4767 rt2x00_set_field8(&reg, RFCSR39_RX_LO2_EN, 0);
4768 rt2800_rfcsr_write(rt2x00dev, 39, reg);
4769
4770 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
4771
4772 rt2800_rfcsr_read(rt2x00dev, 30, &reg);
4773 rt2x00_set_field8(&reg, RFCSR30_RX_VCM, 2);
4774 rt2800_rfcsr_write(rt2x00dev, 30, reg);
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01004775}
4776
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02004777static int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004778{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01004779 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004780 u8 rfcsr;
4781 u8 bbp;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004782 u32 reg;
4783 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004784
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004785 if (!rt2x00_rt(rt2x00dev, RT3070) &&
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004786 !rt2x00_rt(rt2x00dev, RT3071) &&
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004787 !rt2x00_rt(rt2x00dev, RT3090) &&
Woody Hunga89534e2012-06-13 15:01:16 +08004788 !rt2x00_rt(rt2x00dev, RT3290) &&
Daniel Golle03839952012-09-09 14:24:39 +03004789 !rt2x00_rt(rt2x00dev, RT3352) &&
Helmut Schaa23812382010-04-26 13:48:45 +02004790 !rt2x00_rt(rt2x00dev, RT3390) &&
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004791 !rt2x00_rt(rt2x00dev, RT3572) &&
Gabor Juhosadde5882011-03-03 11:46:45 +01004792 !rt2x00_rt(rt2x00dev, RT5390) &&
John Li2ed71882012-02-17 17:33:06 +08004793 !rt2x00_rt(rt2x00dev, RT5392) &&
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01004794 !rt2x00_rt(rt2x00dev, RT5392) &&
Stanislaw Gruszka4bc618f2013-03-16 19:19:43 +01004795 !rt2x00_rt(rt2x00dev, RT5592) &&
Helmut Schaabaff8002010-04-28 09:58:59 +02004796 !rt2800_is_305x_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004797 return 0;
4798
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004799 /*
4800 * Init RF calibration.
4801 */
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01004802
Woody Hunga89534e2012-06-13 15:01:16 +08004803 if (rt2x00_rt(rt2x00dev, RT3290) ||
4804 rt2x00_rt(rt2x00dev, RT5390) ||
4805 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01004806 rt2800_rfcsr_read(rt2x00dev, 2, &rfcsr);
4807 rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 1);
4808 rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
4809 msleep(1);
4810 rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 0);
4811 rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
4812 } else {
4813 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
4814 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
4815 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
4816 msleep(1);
4817 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
4818 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
4819 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004820
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01004821 if (rt2800_is_305x_soc(rt2x00dev)) {
4822 rt2800_init_rfcsr_305x_soc(rt2x00dev);
Helmut Schaabaff8002010-04-28 09:58:59 +02004823 return 0;
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01004824 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01004825
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01004826 switch (rt2x00dev->chip.rt) {
4827 case RT3070:
4828 case RT3071:
4829 case RT3090:
4830 rt2800_init_rfcsr_30xx(rt2x00dev);
4831 break;
4832 case RT3290:
4833 rt2800_init_rfcsr_3290(rt2x00dev);
4834 break;
4835 case RT3352:
4836 rt2800_init_rfcsr_3352(rt2x00dev);
4837 break;
4838 case RT3390:
4839 rt2800_init_rfcsr_3390(rt2x00dev);
4840 break;
4841 case RT3572:
4842 rt2800_init_rfcsr_3572(rt2x00dev);
4843 break;
4844 case RT5390:
4845 rt2800_init_rfcsr_5390(rt2x00dev);
4846 break;
4847 case RT5392:
4848 rt2800_init_rfcsr_5392(rt2x00dev);
4849 break;
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01004850 case RT5592:
4851 rt2800_init_rfcsr_5592(rt2x00dev);
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01004852 return 0;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004853 }
4854
4855 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
4856 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
4857 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
4858 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
4859 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004860 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
4861 rt2x00_rt(rt2x00dev, RT3090)) {
RA-Jay Hung80d184e2011-01-10 11:28:10 +01004862 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
4863
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004864 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
4865 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
4866 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
4867
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004868 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
4869 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004870 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
4871 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004872 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
4873 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004874 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
4875 else
4876 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
4877 }
4878 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
RA-Jay Hung80d184e2011-01-10 11:28:10 +01004879
4880 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
4881 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
4882 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004883 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
4884 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
4885 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
4886 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004887 } else if (rt2x00_rt(rt2x00dev, RT3572)) {
4888 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
4889 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
4890 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
4891
4892 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
4893 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
4894 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
4895 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
4896 msleep(1);
4897 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
Marc Kleine-Budded0f21fe2012-08-27 00:26:37 +02004898 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004899 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
4900 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004901 }
4902
4903 /*
4904 * Set RX Filter calibration for 20MHz and 40MHz
4905 */
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004906 if (rt2x00_rt(rt2x00dev, RT3070)) {
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01004907 drv_data->calibration_bw20 =
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004908 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01004909 drv_data->calibration_bw40 =
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004910 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004911 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004912 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03004913 rt2x00_rt(rt2x00dev, RT3352) ||
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004914 rt2x00_rt(rt2x00dev, RT3390) ||
4915 rt2x00_rt(rt2x00dev, RT3572)) {
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01004916 drv_data->calibration_bw20 =
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004917 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01004918 drv_data->calibration_bw40 =
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004919 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004920 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004921
Gertjan van Wingerde5d137df2012-02-06 23:45:09 +01004922 /*
4923 * Save BBP 25 & 26 values for later use in channel switching
4924 */
4925 rt2800_bbp_read(rt2x00dev, 25, &drv_data->bbp25);
4926 rt2800_bbp_read(rt2x00dev, 26, &drv_data->bbp26);
4927
John Li2ed71882012-02-17 17:33:06 +08004928 if (!rt2x00_rt(rt2x00dev, RT5390) &&
Gabor Juhose6d227b2012-12-02 15:53:28 +01004929 !rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01004930 /*
4931 * Set back to initial state
4932 */
4933 rt2800_bbp_write(rt2x00dev, 24, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004934
Gabor Juhosadde5882011-03-03 11:46:45 +01004935 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
4936 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
4937 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004938
Gabor Juhosadde5882011-03-03 11:46:45 +01004939 /*
4940 * Set BBP back to BW20
4941 */
4942 rt2800_bbp_read(rt2x00dev, 4, &bbp);
4943 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
4944 rt2800_bbp_write(rt2x00dev, 4, bbp);
4945 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004946
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004947 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004948 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004949 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
Stanislaw Gruszkad8bbf902013-03-16 19:19:37 +01004950 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E) ||
4951 rt2x00_rt_rev_lt(rt2x00dev, RT5592, REV_RT5592C))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004952 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
4953
4954 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
4955 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
4956 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
4957
John Li2ed71882012-02-17 17:33:06 +08004958 if (!rt2x00_rt(rt2x00dev, RT5390) &&
Gabor Juhose6d227b2012-12-02 15:53:28 +01004959 !rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01004960 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
4961 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
4962 if (rt2x00_rt(rt2x00dev, RT3070) ||
4963 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
4964 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
4965 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02004966 if (!test_bit(CAPABILITY_EXTERNAL_LNA_BG,
4967 &rt2x00dev->cap_flags))
Gabor Juhosadde5882011-03-03 11:46:45 +01004968 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
4969 }
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01004970 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
4971 drv_data->txmixer_gain_24g);
Gabor Juhosadde5882011-03-03 11:46:45 +01004972 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
4973 }
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004974
Stanislaw Gruszkaa630afe2013-03-16 19:19:39 +01004975 if (rt2x00_rt(rt2x00dev, RT3090) ||
4976 rt2x00_rt(rt2x00dev, RT5592)) {
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004977 rt2800_bbp_read(rt2x00dev, 138, &bbp);
4978
RA-Jay Hung80d184e2011-01-10 11:28:10 +01004979 /* Turn off unused DAC1 and ADC1 to reduce power consumption */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004980 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
4981 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004982 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004983 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004984 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
4985
4986 rt2800_bbp_write(rt2x00dev, 138, bbp);
4987 }
4988
4989 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004990 rt2x00_rt(rt2x00dev, RT3090) ||
4991 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004992 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
4993 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
4994 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
4995 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
4996 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
4997 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
4998 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
4999
5000 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
5001 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
5002 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
5003
5004 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
5005 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
5006 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
5007
5008 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
5009 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
5010 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
5011 }
5012
RA-Jay Hung80d184e2011-01-10 11:28:10 +01005013 if (rt2x00_rt(rt2x00dev, RT3070)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02005014 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
RA-Jay Hung80d184e2011-01-10 11:28:10 +01005015 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02005016 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
5017 else
5018 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
5019 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
5020 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
5021 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
5022 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
5023 }
5024
Woody Hunga89534e2012-06-13 15:01:16 +08005025 if (rt2x00_rt(rt2x00dev, RT3290)) {
5026 rt2800_rfcsr_read(rt2x00dev, 29, &rfcsr);
5027 rt2x00_set_field8(&rfcsr, RFCSR29_RSSI_GAIN, 3);
5028 rt2800_rfcsr_write(rt2x00dev, 29, rfcsr);
5029 }
5030
John Li2ed71882012-02-17 17:33:06 +08005031 if (rt2x00_rt(rt2x00dev, RT5390) ||
Stanislaw Gruszkacf084c62013-03-16 19:19:40 +01005032 rt2x00_rt(rt2x00dev, RT5392) ||
5033 rt2x00_rt(rt2x00dev, RT5592)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01005034 rt2800_rfcsr_read(rt2x00dev, 38, &rfcsr);
5035 rt2x00_set_field8(&rfcsr, RFCSR38_RX_LO1_EN, 0);
5036 rt2800_rfcsr_write(rt2x00dev, 38, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01005037
Gabor Juhosadde5882011-03-03 11:46:45 +01005038 rt2800_rfcsr_read(rt2x00dev, 39, &rfcsr);
5039 rt2x00_set_field8(&rfcsr, RFCSR39_RX_LO2_EN, 0);
5040 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01005041
Gabor Juhosadde5882011-03-03 11:46:45 +01005042 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
5043 rt2x00_set_field8(&rfcsr, RFCSR30_RX_VCM, 2);
5044 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
5045 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01005046
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005047 return 0;
5048}
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02005049
5050int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
5051{
5052 u32 reg;
5053 u16 word;
5054
5055 /*
5056 * Initialize all registers.
5057 */
5058 if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01005059 rt2800_init_registers(rt2x00dev)))
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02005060 return -EIO;
5061
5062 /*
5063 * Send signal to firmware during boot time.
5064 */
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01005065 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
5066 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
5067 if (rt2x00_is_usb(rt2x00dev)) {
5068 rt2800_register_write(rt2x00dev, H2M_INT_SRC, 0);
5069 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
5070 }
5071 msleep(1);
5072
5073 if (unlikely(rt2800_init_bbp(rt2x00dev) ||
5074 rt2800_init_rfcsr(rt2x00dev)))
5075 return -EIO;
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02005076
5077 if (rt2x00_is_usb(rt2x00dev) &&
5078 (rt2x00_rt(rt2x00dev, RT3070) ||
5079 rt2x00_rt(rt2x00dev, RT3071) ||
5080 rt2x00_rt(rt2x00dev, RT3572))) {
5081 udelay(200);
5082 rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
5083 udelay(10);
5084 }
5085
5086 /*
5087 * Enable RX.
5088 */
5089 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
5090 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
5091 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
5092 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
5093
5094 udelay(50);
5095
5096 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
5097 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
5098 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
5099 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
5100 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
5101 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
5102
5103 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
5104 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
5105 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
5106 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
5107
5108 /*
5109 * Initialize LED control
5110 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005111 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_AG_CONF, &word);
5112 rt2800_mcu_request(rt2x00dev, MCU_LED_AG_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02005113 word & 0xff, (word >> 8) & 0xff);
5114
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005115 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_ACT_CONF, &word);
5116 rt2800_mcu_request(rt2x00dev, MCU_LED_ACT_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02005117 word & 0xff, (word >> 8) & 0xff);
5118
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005119 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_POLARITY, &word);
5120 rt2800_mcu_request(rt2x00dev, MCU_LED_LED_POLARITY, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02005121 word & 0xff, (word >> 8) & 0xff);
5122
5123 return 0;
5124}
5125EXPORT_SYMBOL_GPL(rt2800_enable_radio);
5126
5127void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
5128{
5129 u32 reg;
5130
Jakub Kicinskif7b395e2012-04-03 03:40:47 +02005131 rt2800_disable_wpdma(rt2x00dev);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02005132
5133 /* Wait for DMA, ignore error */
5134 rt2800_wait_wpdma_ready(rt2x00dev);
5135
5136 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
5137 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
5138 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
5139 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02005140}
5141EXPORT_SYMBOL_GPL(rt2800_disable_radio);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005142
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01005143int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
5144{
5145 u32 reg;
Woody Hunga89534e2012-06-13 15:01:16 +08005146 u16 efuse_ctrl_reg;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01005147
Woody Hunga89534e2012-06-13 15:01:16 +08005148 if (rt2x00_rt(rt2x00dev, RT3290))
5149 efuse_ctrl_reg = EFUSE_CTRL_3290;
5150 else
5151 efuse_ctrl_reg = EFUSE_CTRL;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01005152
Woody Hunga89534e2012-06-13 15:01:16 +08005153 rt2800_register_read(rt2x00dev, efuse_ctrl_reg, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01005154 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
5155}
5156EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
5157
5158static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
5159{
5160 u32 reg;
Woody Hunga89534e2012-06-13 15:01:16 +08005161 u16 efuse_ctrl_reg;
5162 u16 efuse_data0_reg;
5163 u16 efuse_data1_reg;
5164 u16 efuse_data2_reg;
5165 u16 efuse_data3_reg;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01005166
Woody Hunga89534e2012-06-13 15:01:16 +08005167 if (rt2x00_rt(rt2x00dev, RT3290)) {
5168 efuse_ctrl_reg = EFUSE_CTRL_3290;
5169 efuse_data0_reg = EFUSE_DATA0_3290;
5170 efuse_data1_reg = EFUSE_DATA1_3290;
5171 efuse_data2_reg = EFUSE_DATA2_3290;
5172 efuse_data3_reg = EFUSE_DATA3_3290;
5173 } else {
5174 efuse_ctrl_reg = EFUSE_CTRL;
5175 efuse_data0_reg = EFUSE_DATA0;
5176 efuse_data1_reg = EFUSE_DATA1;
5177 efuse_data2_reg = EFUSE_DATA2;
5178 efuse_data3_reg = EFUSE_DATA3;
5179 }
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01005180 mutex_lock(&rt2x00dev->csr_mutex);
5181
Woody Hunga89534e2012-06-13 15:01:16 +08005182 rt2800_register_read_lock(rt2x00dev, efuse_ctrl_reg, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01005183 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
5184 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
5185 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Woody Hunga89534e2012-06-13 15:01:16 +08005186 rt2800_register_write_lock(rt2x00dev, efuse_ctrl_reg, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01005187
5188 /* Wait until the EEPROM has been loaded */
Woody Hunga89534e2012-06-13 15:01:16 +08005189 rt2800_regbusy_read(rt2x00dev, efuse_ctrl_reg, EFUSE_CTRL_KICK, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01005190 /* Apparently the data is read from end to start */
Woody Hunga89534e2012-06-13 15:01:16 +08005191 rt2800_register_read_lock(rt2x00dev, efuse_data3_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05005192 /* The returned value is in CPU order, but eeprom is le */
Gertjan van Wingerde68fa64e2011-11-16 23:16:15 +01005193 *(u32 *)&rt2x00dev->eeprom[i] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08005194 rt2800_register_read_lock(rt2x00dev, efuse_data2_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05005195 *(u32 *)&rt2x00dev->eeprom[i + 2] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08005196 rt2800_register_read_lock(rt2x00dev, efuse_data1_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05005197 *(u32 *)&rt2x00dev->eeprom[i + 4] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08005198 rt2800_register_read_lock(rt2x00dev, efuse_data0_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05005199 *(u32 *)&rt2x00dev->eeprom[i + 6] = cpu_to_le32(reg);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01005200
5201 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01005202}
5203
Gabor Juhosa02308e2012-12-29 14:51:51 +01005204int rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01005205{
5206 unsigned int i;
5207
5208 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
5209 rt2800_efuse_read(rt2x00dev, i);
Gabor Juhosa02308e2012-12-29 14:51:51 +01005210
5211 return 0;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01005212}
5213EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
5214
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02005215static int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005216{
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01005217 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005218 u16 word;
5219 u8 *mac;
5220 u8 default_lna_gain;
Gabor Juhosa02308e2012-12-29 14:51:51 +01005221 int retval;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005222
5223 /*
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02005224 * Read the EEPROM.
5225 */
Gabor Juhosa02308e2012-12-29 14:51:51 +01005226 retval = rt2800_read_eeprom(rt2x00dev);
5227 if (retval)
5228 return retval;
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02005229
5230 /*
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005231 * Start validation of the data that has been read.
5232 */
5233 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
5234 if (!is_valid_ether_addr(mac)) {
Joe Perchesf4f7f4142012-07-12 19:33:08 +00005235 eth_random_addr(mac);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005236 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
5237 }
5238
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005239 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005240 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005241 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
5242 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_TXPATH, 1);
5243 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RF_TYPE, RF2820);
5244 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005245 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01005246 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02005247 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005248 /*
5249 * There is a max of 2 RX streams for RT28x0 series
5250 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005251 if (rt2x00_get_field16(word, EEPROM_NIC_CONF0_RXPATH) > 2)
5252 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
5253 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005254 }
5255
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005256 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005257 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005258 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_HW_RADIO, 0);
5259 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC, 0);
5260 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G, 0);
5261 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G, 0);
5262 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_CARDBUS_ACCEL, 0);
5263 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_2G, 0);
5264 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_5G, 0);
5265 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_WPS_PBC, 0);
5266 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_2G, 0);
5267 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_5G, 0);
5268 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BROADBAND_EXT_LNA, 0);
5269 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_ANT_DIVERSITY, 0);
5270 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_INTERNAL_TX_ALC, 0);
5271 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BT_COEXIST, 0);
5272 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_DAC_TEST, 0);
5273 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF1, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005274 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
5275 }
5276
5277 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
5278 if ((word & 0x00ff) == 0x00ff) {
5279 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02005280 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
5281 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
5282 }
5283 if ((word & 0xff00) == 0xff00) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005284 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
5285 LED_MODE_TXRX_ACTIVITY);
5286 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
5287 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005288 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_AG_CONF, 0x5555);
5289 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_ACT_CONF, 0x2221);
5290 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_POLARITY, 0xa9f8);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02005291 EEPROM(rt2x00dev, "Led Mode: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005292 }
5293
5294 /*
5295 * During the LNA validation we are going to use
5296 * lna0 as correct value. Note that EEPROM_LNA
5297 * is never validated.
5298 */
5299 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
5300 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
5301
5302 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
5303 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
5304 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
5305 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
5306 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
5307 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
5308
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01005309 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &word);
5310 if ((word & 0x00ff) != 0x00ff) {
5311 drv_data->txmixer_gain_24g =
5312 rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_BG_VAL);
5313 } else {
5314 drv_data->txmixer_gain_24g = 0;
5315 }
5316
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005317 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
5318 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
5319 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
5320 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
5321 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
5322 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
5323 default_lna_gain);
5324 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
5325
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01005326 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_A, &word);
5327 if ((word & 0x00ff) != 0x00ff) {
5328 drv_data->txmixer_gain_5g =
5329 rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_A_VAL);
5330 } else {
5331 drv_data->txmixer_gain_5g = 0;
5332 }
5333
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005334 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
5335 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
5336 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
5337 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
5338 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
5339 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
5340
5341 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
5342 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
5343 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
5344 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
5345 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
5346 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
5347 default_lna_gain);
5348 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
5349
5350 return 0;
5351}
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005352
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02005353static int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005354{
5355 u32 reg;
5356 u16 value;
5357 u16 eeprom;
5358
5359 /*
5360 * Read EEPROM word for configuration.
5361 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005362 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005363
5364 /*
Gabor Juhosadde5882011-03-03 11:46:45 +01005365 * Identify RF chipset by EEPROM value
5366 * RT28xx/RT30xx: defined in "EEPROM_NIC_CONF0_RF_TYPE" field
5367 * RT53xx: defined in "EEPROM_CHIP_ID" field
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005368 */
Woody Hunga89534e2012-06-13 15:01:16 +08005369 if (rt2x00_rt(rt2x00dev, RT3290))
5370 rt2800_register_read(rt2x00dev, MAC_CSR0_3290, &reg);
5371 else
5372 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
5373
5374 if (rt2x00_get_field32(reg, MAC_CSR0_CHIPSET) == RT3290 ||
5375 rt2x00_get_field32(reg, MAC_CSR0_CHIPSET) == RT5390 ||
5376 rt2x00_get_field32(reg, MAC_CSR0_CHIPSET) == RT5392)
Gabor Juhosadde5882011-03-03 11:46:45 +01005377 rt2x00_eeprom_read(rt2x00dev, EEPROM_CHIP_ID, &value);
5378 else
5379 value = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RF_TYPE);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005380
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01005381 rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
5382 value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
Gertjan van Wingerde714fa662010-02-13 20:55:48 +01005383
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01005384 switch (rt2x00dev->chip.rt) {
5385 case RT2860:
5386 case RT2872:
5387 case RT2883:
5388 case RT3070:
5389 case RT3071:
5390 case RT3090:
Woody Hunga89534e2012-06-13 15:01:16 +08005391 case RT3290:
Daniel Golle03839952012-09-09 14:24:39 +03005392 case RT3352:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01005393 case RT3390:
5394 case RT3572:
5395 case RT5390:
John Li2ed71882012-02-17 17:33:06 +08005396 case RT5392:
Stanislaw Gruszkab8863f82013-03-16 19:19:30 +01005397 case RT5592:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01005398 break;
5399 default:
John Lib6df7f12012-02-08 21:25:24 +08005400 ERROR(rt2x00dev, "Invalid RT chipset 0x%04x detected.\n", rt2x00dev->chip.rt);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01005401 return -ENODEV;
5402 }
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005403
Larry Fingerd331eb52011-09-14 16:50:22 -05005404 switch (rt2x00dev->chip.rf) {
5405 case RF2820:
5406 case RF2850:
5407 case RF2720:
5408 case RF2750:
5409 case RF3020:
5410 case RF2020:
5411 case RF3021:
5412 case RF3022:
5413 case RF3052:
Woody Hunga89534e2012-06-13 15:01:16 +08005414 case RF3290:
Larry Fingerd331eb52011-09-14 16:50:22 -05005415 case RF3320:
Daniel Golle03839952012-09-09 14:24:39 +03005416 case RF3322:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02005417 case RF5360:
Larry Fingerd331eb52011-09-14 16:50:22 -05005418 case RF5370:
John Li2ed71882012-02-17 17:33:06 +08005419 case RF5372:
Larry Fingerd331eb52011-09-14 16:50:22 -05005420 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08005421 case RF5392:
Stanislaw Gruszkab8863f82013-03-16 19:19:30 +01005422 case RF5592:
Larry Fingerd331eb52011-09-14 16:50:22 -05005423 break;
5424 default:
John Lib6df7f12012-02-08 21:25:24 +08005425 ERROR(rt2x00dev, "Invalid RF chipset 0x%04x detected.\n",
Larry Fingerd331eb52011-09-14 16:50:22 -05005426 rt2x00dev->chip.rf);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005427 return -ENODEV;
5428 }
5429
5430 /*
5431 * Identify default antenna configuration.
5432 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01005433 rt2x00dev->default_ant.tx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005434 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01005435 rt2x00dev->default_ant.rx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005436 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005437
RA-Jay Hungd96aa642011-02-20 13:54:52 +01005438 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
5439
5440 if (rt2x00_rt(rt2x00dev, RT3070) ||
5441 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03005442 rt2x00_rt(rt2x00dev, RT3352) ||
RA-Jay Hungd96aa642011-02-20 13:54:52 +01005443 rt2x00_rt(rt2x00dev, RT3390)) {
5444 value = rt2x00_get_field16(eeprom,
5445 EEPROM_NIC_CONF1_ANT_DIVERSITY);
5446 switch (value) {
5447 case 0:
5448 case 1:
5449 case 2:
5450 rt2x00dev->default_ant.tx = ANTENNA_A;
5451 rt2x00dev->default_ant.rx = ANTENNA_A;
5452 break;
5453 case 3:
5454 rt2x00dev->default_ant.tx = ANTENNA_A;
5455 rt2x00dev->default_ant.rx = ANTENNA_B;
5456 break;
5457 }
5458 } else {
5459 rt2x00dev->default_ant.tx = ANTENNA_A;
5460 rt2x00dev->default_ant.rx = ANTENNA_A;
5461 }
5462
Anisse Astier0586a112012-04-23 12:33:11 +02005463 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390R)) {
5464 rt2x00dev->default_ant.tx = ANTENNA_HW_DIVERSITY; /* Unused */
5465 rt2x00dev->default_ant.rx = ANTENNA_HW_DIVERSITY; /* Unused */
5466 }
5467
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005468 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02005469 * Determine external LNA informations.
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005470 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005471 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02005472 __set_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005473 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02005474 __set_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005475
5476 /*
5477 * Detect if this device has an hardware controlled radio.
5478 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005479 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_HW_RADIO))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02005480 __set_bit(CAPABILITY_HW_BUTTON, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005481
5482 /*
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02005483 * Detect if this device has Bluetooth co-existence.
5484 */
5485 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_BT_COEXIST))
5486 __set_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags);
5487
5488 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02005489 * Read frequency offset and RF programming sequence.
5490 */
5491 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
5492 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
5493
5494 /*
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005495 * Store led settings, for correct led behaviour.
5496 */
5497#ifdef CONFIG_RT2X00_LIB_LEDS
5498 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
5499 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
5500 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
5501
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02005502 rt2x00dev->led_mcu_reg = eeprom;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005503#endif /* CONFIG_RT2X00_LIB_LEDS */
5504
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01005505 /*
5506 * Check if support EIRP tx power limit feature.
5507 */
5508 rt2x00_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER, &eeprom);
5509
5510 if (rt2x00_get_field16(eeprom, EEPROM_EIRP_MAX_TX_POWER_2GHZ) <
5511 EIRP_MAX_TX_POWER_LIMIT)
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02005512 __set_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01005513
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005514 return 0;
5515}
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01005516
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01005517/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02005518 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005519 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
5520 */
5521static const struct rf_channel rf_vals[] = {
5522 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
5523 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
5524 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
5525 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
5526 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
5527 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
5528 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
5529 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
5530 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
5531 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
5532 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
5533 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
5534 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
5535 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
5536
5537 /* 802.11 UNI / HyperLan 2 */
5538 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
5539 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
5540 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
5541 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
5542 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
5543 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
5544 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
5545 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
5546 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
5547 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
5548 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
5549 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
5550
5551 /* 802.11 HyperLan 2 */
5552 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
5553 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
5554 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
5555 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
5556 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
5557 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
5558 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
5559 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
5560 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
5561 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
5562 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
5563 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
5564 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
5565 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
5566 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
5567 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
5568
5569 /* 802.11 UNII */
5570 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
5571 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
5572 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
5573 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
5574 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
5575 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
5576 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
5577 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
5578 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
5579 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
5580 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
5581
5582 /* 802.11 Japan */
5583 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
5584 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
5585 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
5586 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
5587 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
5588 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
5589 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
5590};
5591
5592/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02005593 * RF value list for rt3xxx
5594 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005595 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02005596static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005597 {1, 241, 2, 2 },
5598 {2, 241, 2, 7 },
5599 {3, 242, 2, 2 },
5600 {4, 242, 2, 7 },
5601 {5, 243, 2, 2 },
5602 {6, 243, 2, 7 },
5603 {7, 244, 2, 2 },
5604 {8, 244, 2, 7 },
5605 {9, 245, 2, 2 },
5606 {10, 245, 2, 7 },
5607 {11, 246, 2, 2 },
5608 {12, 246, 2, 7 },
5609 {13, 247, 2, 2 },
5610 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02005611
5612 /* 802.11 UNI / HyperLan 2 */
5613 {36, 0x56, 0, 4},
5614 {38, 0x56, 0, 6},
5615 {40, 0x56, 0, 8},
5616 {44, 0x57, 0, 0},
5617 {46, 0x57, 0, 2},
5618 {48, 0x57, 0, 4},
5619 {52, 0x57, 0, 8},
5620 {54, 0x57, 0, 10},
5621 {56, 0x58, 0, 0},
5622 {60, 0x58, 0, 4},
5623 {62, 0x58, 0, 6},
5624 {64, 0x58, 0, 8},
5625
5626 /* 802.11 HyperLan 2 */
5627 {100, 0x5b, 0, 8},
5628 {102, 0x5b, 0, 10},
5629 {104, 0x5c, 0, 0},
5630 {108, 0x5c, 0, 4},
5631 {110, 0x5c, 0, 6},
5632 {112, 0x5c, 0, 8},
5633 {116, 0x5d, 0, 0},
5634 {118, 0x5d, 0, 2},
5635 {120, 0x5d, 0, 4},
5636 {124, 0x5d, 0, 8},
5637 {126, 0x5d, 0, 10},
5638 {128, 0x5e, 0, 0},
5639 {132, 0x5e, 0, 4},
5640 {134, 0x5e, 0, 6},
5641 {136, 0x5e, 0, 8},
5642 {140, 0x5f, 0, 0},
5643
5644 /* 802.11 UNII */
5645 {149, 0x5f, 0, 9},
5646 {151, 0x5f, 0, 11},
5647 {153, 0x60, 0, 1},
5648 {157, 0x60, 0, 5},
5649 {159, 0x60, 0, 7},
5650 {161, 0x60, 0, 9},
5651 {165, 0x61, 0, 1},
5652 {167, 0x61, 0, 3},
5653 {169, 0x61, 0, 5},
5654 {171, 0x61, 0, 7},
5655 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005656};
5657
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01005658static const struct rf_channel rf_vals_5592_xtal20[] = {
5659 /* Channel, N, K, mod, R */
5660 {1, 482, 4, 10, 3},
5661 {2, 483, 4, 10, 3},
5662 {3, 484, 4, 10, 3},
5663 {4, 485, 4, 10, 3},
5664 {5, 486, 4, 10, 3},
5665 {6, 487, 4, 10, 3},
5666 {7, 488, 4, 10, 3},
5667 {8, 489, 4, 10, 3},
5668 {9, 490, 4, 10, 3},
5669 {10, 491, 4, 10, 3},
5670 {11, 492, 4, 10, 3},
5671 {12, 493, 4, 10, 3},
5672 {13, 494, 4, 10, 3},
5673 {14, 496, 8, 10, 3},
5674 {36, 172, 8, 12, 1},
5675 {38, 173, 0, 12, 1},
5676 {40, 173, 4, 12, 1},
5677 {42, 173, 8, 12, 1},
5678 {44, 174, 0, 12, 1},
5679 {46, 174, 4, 12, 1},
5680 {48, 174, 8, 12, 1},
5681 {50, 175, 0, 12, 1},
5682 {52, 175, 4, 12, 1},
5683 {54, 175, 8, 12, 1},
5684 {56, 176, 0, 12, 1},
5685 {58, 176, 4, 12, 1},
5686 {60, 176, 8, 12, 1},
5687 {62, 177, 0, 12, 1},
5688 {64, 177, 4, 12, 1},
5689 {100, 183, 4, 12, 1},
5690 {102, 183, 8, 12, 1},
5691 {104, 184, 0, 12, 1},
5692 {106, 184, 4, 12, 1},
5693 {108, 184, 8, 12, 1},
5694 {110, 185, 0, 12, 1},
5695 {112, 185, 4, 12, 1},
5696 {114, 185, 8, 12, 1},
5697 {116, 186, 0, 12, 1},
5698 {118, 186, 4, 12, 1},
5699 {120, 186, 8, 12, 1},
5700 {122, 187, 0, 12, 1},
5701 {124, 187, 4, 12, 1},
5702 {126, 187, 8, 12, 1},
5703 {128, 188, 0, 12, 1},
5704 {130, 188, 4, 12, 1},
5705 {132, 188, 8, 12, 1},
5706 {134, 189, 0, 12, 1},
5707 {136, 189, 4, 12, 1},
5708 {138, 189, 8, 12, 1},
5709 {140, 190, 0, 12, 1},
5710 {149, 191, 6, 12, 1},
5711 {151, 191, 10, 12, 1},
5712 {153, 192, 2, 12, 1},
5713 {155, 192, 6, 12, 1},
5714 {157, 192, 10, 12, 1},
5715 {159, 193, 2, 12, 1},
5716 {161, 193, 6, 12, 1},
5717 {165, 194, 2, 12, 1},
5718 {184, 164, 0, 12, 1},
5719 {188, 164, 4, 12, 1},
5720 {192, 165, 8, 12, 1},
5721 {196, 166, 0, 12, 1},
5722};
5723
5724static const struct rf_channel rf_vals_5592_xtal40[] = {
5725 /* Channel, N, K, mod, R */
5726 {1, 241, 2, 10, 3},
5727 {2, 241, 7, 10, 3},
5728 {3, 242, 2, 10, 3},
5729 {4, 242, 7, 10, 3},
5730 {5, 243, 2, 10, 3},
5731 {6, 243, 7, 10, 3},
5732 {7, 244, 2, 10, 3},
5733 {8, 244, 7, 10, 3},
5734 {9, 245, 2, 10, 3},
5735 {10, 245, 7, 10, 3},
5736 {11, 246, 2, 10, 3},
5737 {12, 246, 7, 10, 3},
5738 {13, 247, 2, 10, 3},
5739 {14, 248, 4, 10, 3},
5740 {36, 86, 4, 12, 1},
5741 {38, 86, 6, 12, 1},
5742 {40, 86, 8, 12, 1},
5743 {42, 86, 10, 12, 1},
5744 {44, 87, 0, 12, 1},
5745 {46, 87, 2, 12, 1},
5746 {48, 87, 4, 12, 1},
5747 {50, 87, 6, 12, 1},
5748 {52, 87, 8, 12, 1},
5749 {54, 87, 10, 12, 1},
5750 {56, 88, 0, 12, 1},
5751 {58, 88, 2, 12, 1},
5752 {60, 88, 4, 12, 1},
5753 {62, 88, 6, 12, 1},
5754 {64, 88, 8, 12, 1},
5755 {100, 91, 8, 12, 1},
5756 {102, 91, 10, 12, 1},
5757 {104, 92, 0, 12, 1},
5758 {106, 92, 2, 12, 1},
5759 {108, 92, 4, 12, 1},
5760 {110, 92, 6, 12, 1},
5761 {112, 92, 8, 12, 1},
5762 {114, 92, 10, 12, 1},
5763 {116, 93, 0, 12, 1},
5764 {118, 93, 2, 12, 1},
5765 {120, 93, 4, 12, 1},
5766 {122, 93, 6, 12, 1},
5767 {124, 93, 8, 12, 1},
5768 {126, 93, 10, 12, 1},
5769 {128, 94, 0, 12, 1},
5770 {130, 94, 2, 12, 1},
5771 {132, 94, 4, 12, 1},
5772 {134, 94, 6, 12, 1},
5773 {136, 94, 8, 12, 1},
5774 {138, 94, 10, 12, 1},
5775 {140, 95, 0, 12, 1},
5776 {149, 95, 9, 12, 1},
5777 {151, 95, 11, 12, 1},
5778 {153, 96, 1, 12, 1},
5779 {155, 96, 3, 12, 1},
5780 {157, 96, 5, 12, 1},
5781 {159, 96, 7, 12, 1},
5782 {161, 96, 9, 12, 1},
5783 {165, 97, 1, 12, 1},
5784 {184, 82, 0, 12, 1},
5785 {188, 82, 4, 12, 1},
5786 {192, 82, 8, 12, 1},
5787 {196, 83, 0, 12, 1},
5788};
5789
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02005790static int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005791{
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005792 struct hw_mode_spec *spec = &rt2x00dev->spec;
5793 struct channel_info *info;
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02005794 char *default_power1;
5795 char *default_power2;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005796 unsigned int i;
5797 u16 eeprom;
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01005798 u32 reg;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005799
5800 /*
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01005801 * Disable powersaving as default on PCI devices.
5802 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01005803 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01005804 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
5805
5806 /*
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005807 * Initialize all hw fields.
5808 */
5809 rt2x00dev->hw->flags =
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005810 IEEE80211_HW_SIGNAL_DBM |
5811 IEEE80211_HW_SUPPORTS_PS |
Helmut Schaa1df90802010-06-29 21:38:12 +02005812 IEEE80211_HW_PS_NULLFUNC_STACK |
Helmut Schaa9d4f09b2012-03-14 08:56:47 +01005813 IEEE80211_HW_AMPDU_AGGREGATION |
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +01005814 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
Helmut Schaa9d4f09b2012-03-14 08:56:47 +01005815
Helmut Schaa5a5b6ed2010-10-02 11:31:33 +02005816 /*
5817 * Don't set IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING for USB devices
5818 * unless we are capable of sending the buffered frames out after the
5819 * DTIM transmission using rt2x00lib_beacondone. This will send out
5820 * multicast and broadcast traffic immediately instead of buffering it
5821 * infinitly and thus dropping it after some time.
5822 */
5823 if (!rt2x00_is_usb(rt2x00dev))
5824 rt2x00dev->hw->flags |=
5825 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005826
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005827 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
5828 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
5829 rt2x00_eeprom_addr(rt2x00dev,
5830 EEPROM_MAC_ADDR_0));
5831
Helmut Schaa3f2bee22010-06-14 22:12:01 +02005832 /*
5833 * As rt2800 has a global fallback table we cannot specify
5834 * more then one tx rate per frame but since the hw will
5835 * try several rates (based on the fallback table) we should
Helmut Schaaba3b9e52010-10-02 11:32:16 +02005836 * initialize max_report_rates to the maximum number of rates
Helmut Schaa3f2bee22010-06-14 22:12:01 +02005837 * we are going to try. Otherwise mac80211 will truncate our
5838 * reported tx rates and the rc algortihm will end up with
5839 * incorrect data.
5840 */
Helmut Schaaba3b9e52010-10-02 11:32:16 +02005841 rt2x00dev->hw->max_rates = 1;
5842 rt2x00dev->hw->max_report_rates = 7;
Helmut Schaa3f2bee22010-06-14 22:12:01 +02005843 rt2x00dev->hw->max_rate_tries = 1;
5844
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005845 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005846
5847 /*
5848 * Initialize hw_mode information.
5849 */
5850 spec->supported_bands = SUPPORT_BAND_2GHZ;
5851 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
5852
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01005853 if (rt2x00_rf(rt2x00dev, RF2820) ||
Ivo van Doorn55f93212010-05-06 14:45:46 +02005854 rt2x00_rf(rt2x00dev, RF2720)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005855 spec->num_channels = 14;
5856 spec->channels = rf_vals;
Ivo van Doorn55f93212010-05-06 14:45:46 +02005857 } else if (rt2x00_rf(rt2x00dev, RF2850) ||
5858 rt2x00_rf(rt2x00dev, RF2750)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005859 spec->supported_bands |= SUPPORT_BAND_5GHZ;
5860 spec->num_channels = ARRAY_SIZE(rf_vals);
5861 spec->channels = rf_vals;
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01005862 } else if (rt2x00_rf(rt2x00dev, RF3020) ||
5863 rt2x00_rf(rt2x00dev, RF2020) ||
5864 rt2x00_rf(rt2x00dev, RF3021) ||
Gertjan van Wingerdef93bc9b2010-11-13 19:09:50 +01005865 rt2x00_rf(rt2x00dev, RF3022) ||
Woody Hunga89534e2012-06-13 15:01:16 +08005866 rt2x00_rf(rt2x00dev, RF3290) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01005867 rt2x00_rf(rt2x00dev, RF3320) ||
Daniel Golle03839952012-09-09 14:24:39 +03005868 rt2x00_rf(rt2x00dev, RF3322) ||
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02005869 rt2x00_rf(rt2x00dev, RF5360) ||
Gertjan van Wingerdeaca355b2011-05-04 21:41:36 +02005870 rt2x00_rf(rt2x00dev, RF5370) ||
John Li2ed71882012-02-17 17:33:06 +08005871 rt2x00_rf(rt2x00dev, RF5372) ||
Zero.Lincff3d1f2012-05-29 16:11:09 +08005872 rt2x00_rf(rt2x00dev, RF5390) ||
5873 rt2x00_rf(rt2x00dev, RF5392)) {
Ivo van Doorn55f93212010-05-06 14:45:46 +02005874 spec->num_channels = 14;
5875 spec->channels = rf_vals_3x;
5876 } else if (rt2x00_rf(rt2x00dev, RF3052)) {
5877 spec->supported_bands |= SUPPORT_BAND_5GHZ;
5878 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
5879 spec->channels = rf_vals_3x;
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01005880 } else if (rt2x00_rf(rt2x00dev, RF5592)) {
5881 spec->supported_bands |= SUPPORT_BAND_5GHZ;
5882
5883 rt2800_register_read(rt2x00dev, MAC_DEBUG_INDEX, &reg);
5884 if (rt2x00_get_field32(reg, MAC_DEBUG_INDEX_XTAL)) {
5885 spec->num_channels = ARRAY_SIZE(rf_vals_5592_xtal40);
5886 spec->channels = rf_vals_5592_xtal40;
5887 } else {
5888 spec->num_channels = ARRAY_SIZE(rf_vals_5592_xtal20);
5889 spec->channels = rf_vals_5592_xtal20;
5890 }
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005891 }
5892
Stanislaw Gruszka53216d62013-03-16 19:19:29 +01005893 if (WARN_ON_ONCE(!spec->channels))
5894 return -ENODEV;
5895
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005896 /*
5897 * Initialize HT information.
5898 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01005899 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01005900 spec->ht.ht_supported = true;
5901 else
5902 spec->ht.ht_supported = false;
5903
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005904 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02005905 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005906 IEEE80211_HT_CAP_GRN_FLD |
5907 IEEE80211_HT_CAP_SGI_20 |
Ivo van Doornaa674632010-06-29 21:48:37 +02005908 IEEE80211_HT_CAP_SGI_40;
Helmut Schaa22cabaa2010-06-03 10:52:10 +02005909
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005910 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) >= 2)
Helmut Schaa22cabaa2010-06-03 10:52:10 +02005911 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
5912
Ivo van Doornaa674632010-06-29 21:48:37 +02005913 spec->ht.cap |=
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005914 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) <<
Ivo van Doornaa674632010-06-29 21:48:37 +02005915 IEEE80211_HT_CAP_RX_STBC_SHIFT;
5916
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005917 spec->ht.ampdu_factor = 3;
5918 spec->ht.ampdu_density = 4;
5919 spec->ht.mcs.tx_params =
5920 IEEE80211_HT_MCS_TX_DEFINED |
5921 IEEE80211_HT_MCS_TX_RX_DIFF |
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005922 ((rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) - 1) <<
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005923 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
5924
RA-Jay Hung38c8a562010-12-13 12:31:27 +01005925 switch (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005926 case 3:
5927 spec->ht.mcs.rx_mask[2] = 0xff;
5928 case 2:
5929 spec->ht.mcs.rx_mask[1] = 0xff;
5930 case 1:
5931 spec->ht.mcs.rx_mask[0] = 0xff;
5932 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
5933 break;
5934 }
5935
5936 /*
5937 * Create channel information array
5938 */
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00005939 info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005940 if (!info)
5941 return -ENOMEM;
5942
5943 spec->channels_info = info;
5944
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02005945 default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
5946 default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005947
5948 for (i = 0; i < 14; i++) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01005949 info[i].default_power1 = default_power1[i];
5950 info[i].default_power2 = default_power2[i];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005951 }
5952
5953 if (spec->num_channels > 14) {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02005954 default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
5955 default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005956
5957 for (i = 14; i < spec->num_channels; i++) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01005958 info[i].default_power1 = default_power1[i];
5959 info[i].default_power2 = default_power2[i];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005960 }
5961 }
5962
John Li2e9c43d2012-02-16 21:40:57 +08005963 switch (rt2x00dev->chip.rf) {
5964 case RF2020:
5965 case RF3020:
5966 case RF3021:
5967 case RF3022:
5968 case RF3320:
5969 case RF3052:
Woody Hunga89534e2012-06-13 15:01:16 +08005970 case RF3290:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02005971 case RF5360:
John Li2e9c43d2012-02-16 21:40:57 +08005972 case RF5370:
5973 case RF5372:
5974 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08005975 case RF5392:
John Li2e9c43d2012-02-16 21:40:57 +08005976 __set_bit(CAPABILITY_VCO_RECALIBRATION, &rt2x00dev->cap_flags);
5977 break;
5978 }
5979
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01005980 return 0;
5981}
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02005982
5983int rt2800_probe_hw(struct rt2x00_dev *rt2x00dev)
5984{
5985 int retval;
5986 u32 reg;
5987
5988 /*
5989 * Allocate eeprom data.
5990 */
5991 retval = rt2800_validate_eeprom(rt2x00dev);
5992 if (retval)
5993 return retval;
5994
5995 retval = rt2800_init_eeprom(rt2x00dev);
5996 if (retval)
5997 return retval;
5998
5999 /*
6000 * Enable rfkill polling by setting GPIO direction of the
6001 * rfkill switch GPIO pin correctly.
6002 */
6003 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
6004 rt2x00_set_field32(&reg, GPIO_CTRL_DIR2, 1);
6005 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
6006
6007 /*
6008 * Initialize hw specifications.
6009 */
6010 retval = rt2800_probe_hw_mode(rt2x00dev);
6011 if (retval)
6012 return retval;
6013
6014 /*
6015 * Set device capabilities.
6016 */
6017 __set_bit(CAPABILITY_CONTROL_FILTERS, &rt2x00dev->cap_flags);
6018 __set_bit(CAPABILITY_CONTROL_FILTER_PSPOLL, &rt2x00dev->cap_flags);
6019 if (!rt2x00_is_usb(rt2x00dev))
6020 __set_bit(CAPABILITY_PRE_TBTT_INTERRUPT, &rt2x00dev->cap_flags);
6021
6022 /*
6023 * Set device requirements.
6024 */
6025 if (!rt2x00_is_soc(rt2x00dev))
6026 __set_bit(REQUIRE_FIRMWARE, &rt2x00dev->cap_flags);
6027 __set_bit(REQUIRE_L2PAD, &rt2x00dev->cap_flags);
6028 __set_bit(REQUIRE_TXSTATUS_FIFO, &rt2x00dev->cap_flags);
6029 if (!rt2800_hwcrypt_disabled(rt2x00dev))
6030 __set_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags);
6031 __set_bit(CAPABILITY_LINK_TUNING, &rt2x00dev->cap_flags);
6032 __set_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags);
6033 if (rt2x00_is_usb(rt2x00dev))
6034 __set_bit(REQUIRE_PS_AUTOWAKE, &rt2x00dev->cap_flags);
6035 else {
6036 __set_bit(REQUIRE_DMA, &rt2x00dev->cap_flags);
6037 __set_bit(REQUIRE_TASKLET_CONTEXT, &rt2x00dev->cap_flags);
6038 }
6039
6040 /*
6041 * Set the rssi offset.
6042 */
6043 rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
6044
6045 return 0;
6046}
6047EXPORT_SYMBOL_GPL(rt2800_probe_hw);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006048
6049/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006050 * IEEE80211 stack callback functions.
6051 */
Helmut Schaae7836192010-07-11 12:28:54 +02006052void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx, u32 *iv32,
6053 u16 *iv16)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006054{
6055 struct rt2x00_dev *rt2x00dev = hw->priv;
6056 struct mac_iveiv_entry iveiv_entry;
6057 u32 offset;
6058
6059 offset = MAC_IVEIV_ENTRY(hw_key_idx);
6060 rt2800_register_multiread(rt2x00dev, offset,
6061 &iveiv_entry, sizeof(iveiv_entry));
6062
Julia Lawall855da5e2009-12-13 17:07:45 +01006063 memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
6064 memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006065}
Helmut Schaae7836192010-07-11 12:28:54 +02006066EXPORT_SYMBOL_GPL(rt2800_get_tkip_seq);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006067
Helmut Schaae7836192010-07-11 12:28:54 +02006068int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006069{
6070 struct rt2x00_dev *rt2x00dev = hw->priv;
6071 u32 reg;
6072 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
6073
6074 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
6075 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
6076 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
6077
6078 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
6079 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
6080 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
6081
6082 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
6083 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
6084 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
6085
6086 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
6087 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
6088 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
6089
6090 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
6091 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
6092 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
6093
6094 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
6095 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
6096 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
6097
6098 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
6099 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
6100 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
6101
6102 return 0;
6103}
Helmut Schaae7836192010-07-11 12:28:54 +02006104EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006105
Eliad Peller8a3a3c82011-10-02 10:15:52 +02006106int rt2800_conf_tx(struct ieee80211_hw *hw,
6107 struct ieee80211_vif *vif, u16 queue_idx,
Helmut Schaae7836192010-07-11 12:28:54 +02006108 const struct ieee80211_tx_queue_params *params)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006109{
6110 struct rt2x00_dev *rt2x00dev = hw->priv;
6111 struct data_queue *queue;
6112 struct rt2x00_field32 field;
6113 int retval;
6114 u32 reg;
6115 u32 offset;
6116
6117 /*
6118 * First pass the configuration through rt2x00lib, that will
6119 * update the queue settings and validate the input. After that
6120 * we are free to update the registers based on the value
6121 * in the queue parameter.
6122 */
Eliad Peller8a3a3c82011-10-02 10:15:52 +02006123 retval = rt2x00mac_conf_tx(hw, vif, queue_idx, params);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006124 if (retval)
6125 return retval;
6126
6127 /*
6128 * We only need to perform additional register initialization
6129 * for WMM queues/
6130 */
6131 if (queue_idx >= 4)
6132 return 0;
6133
Helmut Schaa11f818e2011-03-03 19:38:55 +01006134 queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006135
6136 /* Update WMM TXOP register */
6137 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
6138 field.bit_offset = (queue_idx & 1) * 16;
6139 field.bit_mask = 0xffff << field.bit_offset;
6140
6141 rt2800_register_read(rt2x00dev, offset, &reg);
6142 rt2x00_set_field32(&reg, field, queue->txop);
6143 rt2800_register_write(rt2x00dev, offset, reg);
6144
6145 /* Update WMM registers */
6146 field.bit_offset = queue_idx * 4;
6147 field.bit_mask = 0xf << field.bit_offset;
6148
6149 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
6150 rt2x00_set_field32(&reg, field, queue->aifs);
6151 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
6152
6153 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
6154 rt2x00_set_field32(&reg, field, queue->cw_min);
6155 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
6156
6157 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
6158 rt2x00_set_field32(&reg, field, queue->cw_max);
6159 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
6160
6161 /* Update EDCA registers */
6162 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
6163
6164 rt2800_register_read(rt2x00dev, offset, &reg);
6165 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
6166 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
6167 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
6168 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
6169 rt2800_register_write(rt2x00dev, offset, reg);
6170
6171 return 0;
6172}
Helmut Schaae7836192010-07-11 12:28:54 +02006173EXPORT_SYMBOL_GPL(rt2800_conf_tx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006174
Eliad Peller37a41b42011-09-21 14:06:11 +03006175u64 rt2800_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006176{
6177 struct rt2x00_dev *rt2x00dev = hw->priv;
6178 u64 tsf;
6179 u32 reg;
6180
6181 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
6182 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
6183 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
6184 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
6185
6186 return tsf;
6187}
Helmut Schaae7836192010-07-11 12:28:54 +02006188EXPORT_SYMBOL_GPL(rt2800_get_tsf);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006189
Helmut Schaae7836192010-07-11 12:28:54 +02006190int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6191 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01006192 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
6193 u8 buf_size)
Helmut Schaa1df90802010-06-29 21:38:12 +02006194{
Helmut Schaaaf353232011-09-08 14:38:36 +02006195 struct rt2x00_sta *sta_priv = (struct rt2x00_sta *)sta->drv_priv;
Helmut Schaa1df90802010-06-29 21:38:12 +02006196 int ret = 0;
6197
Helmut Schaaaf353232011-09-08 14:38:36 +02006198 /*
6199 * Don't allow aggregation for stations the hardware isn't aware
6200 * of because tx status reports for frames to an unknown station
6201 * always contain wcid=255 and thus we can't distinguish between
6202 * multiple stations which leads to unwanted situations when the
6203 * hw reorders frames due to aggregation.
6204 */
6205 if (sta_priv->wcid < 0)
6206 return 1;
6207
Helmut Schaa1df90802010-06-29 21:38:12 +02006208 switch (action) {
6209 case IEEE80211_AMPDU_RX_START:
6210 case IEEE80211_AMPDU_RX_STOP:
Helmut Schaa58ed8262010-10-02 11:33:17 +02006211 /*
6212 * The hw itself takes care of setting up BlockAck mechanisms.
6213 * So, we only have to allow mac80211 to nagotiate a BlockAck
6214 * agreement. Once that is done, the hw will BlockAck incoming
6215 * AMPDUs without further setup.
6216 */
Helmut Schaa1df90802010-06-29 21:38:12 +02006217 break;
6218 case IEEE80211_AMPDU_TX_START:
6219 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
6220 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02006221 case IEEE80211_AMPDU_TX_STOP_CONT:
6222 case IEEE80211_AMPDU_TX_STOP_FLUSH:
6223 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Helmut Schaa1df90802010-06-29 21:38:12 +02006224 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
6225 break;
6226 case IEEE80211_AMPDU_TX_OPERATIONAL:
6227 break;
6228 default:
Ivo van Doorn4e9e58c2010-06-29 21:49:50 +02006229 WARNING((struct rt2x00_dev *)hw->priv, "Unknown AMPDU action\n");
Helmut Schaa1df90802010-06-29 21:38:12 +02006230 }
6231
6232 return ret;
6233}
Helmut Schaae7836192010-07-11 12:28:54 +02006234EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02006235
Helmut Schaa977206d2010-12-13 12:31:58 +01006236int rt2800_get_survey(struct ieee80211_hw *hw, int idx,
6237 struct survey_info *survey)
6238{
6239 struct rt2x00_dev *rt2x00dev = hw->priv;
6240 struct ieee80211_conf *conf = &hw->conf;
6241 u32 idle, busy, busy_ext;
6242
6243 if (idx != 0)
6244 return -ENOENT;
6245
6246 survey->channel = conf->channel;
6247
6248 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &idle);
6249 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &busy);
6250 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &busy_ext);
6251
6252 if (idle || busy) {
6253 survey->filled = SURVEY_INFO_CHANNEL_TIME |
6254 SURVEY_INFO_CHANNEL_TIME_BUSY |
6255 SURVEY_INFO_CHANNEL_TIME_EXT_BUSY;
6256
6257 survey->channel_time = (idle + busy) / 1000;
6258 survey->channel_time_busy = busy / 1000;
6259 survey->channel_time_ext_busy = busy_ext / 1000;
6260 }
6261
Helmut Schaa9931df22011-12-22 09:36:29 +01006262 if (!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
6263 survey->filled |= SURVEY_INFO_IN_USE;
6264
Helmut Schaa977206d2010-12-13 12:31:58 +01006265 return 0;
6266
6267}
6268EXPORT_SYMBOL_GPL(rt2800_get_survey);
6269
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02006270MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
6271MODULE_VERSION(DRV_VERSION);
6272MODULE_DESCRIPTION("Ralink RT2800 library");
6273MODULE_LICENSE("GPL");