blob: 1f327325df52bad894569406820424fe23149fa8 [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
Joe Perchesec9c4982013-04-19 08:33:40 -070083 rt2x00_warn(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
Gabor Juhos379448f2013-07-08 11:25:55 +0200224static const unsigned int rt2800_eeprom_map[EEPROM_WORD_COUNT] = {
225 [EEPROM_CHIP_ID] = 0x0000,
226 [EEPROM_VERSION] = 0x0001,
227 [EEPROM_MAC_ADDR_0] = 0x0002,
228 [EEPROM_MAC_ADDR_1] = 0x0003,
229 [EEPROM_MAC_ADDR_2] = 0x0004,
230 [EEPROM_NIC_CONF0] = 0x001a,
231 [EEPROM_NIC_CONF1] = 0x001b,
232 [EEPROM_FREQ] = 0x001d,
233 [EEPROM_LED_AG_CONF] = 0x001e,
234 [EEPROM_LED_ACT_CONF] = 0x001f,
235 [EEPROM_LED_POLARITY] = 0x0020,
236 [EEPROM_NIC_CONF2] = 0x0021,
237 [EEPROM_LNA] = 0x0022,
238 [EEPROM_RSSI_BG] = 0x0023,
239 [EEPROM_RSSI_BG2] = 0x0024,
240 [EEPROM_TXMIXER_GAIN_BG] = 0x0024, /* overlaps with RSSI_BG2 */
241 [EEPROM_RSSI_A] = 0x0025,
242 [EEPROM_RSSI_A2] = 0x0026,
243 [EEPROM_TXMIXER_GAIN_A] = 0x0026, /* overlaps with RSSI_A2 */
244 [EEPROM_EIRP_MAX_TX_POWER] = 0x0027,
245 [EEPROM_TXPOWER_DELTA] = 0x0028,
246 [EEPROM_TXPOWER_BG1] = 0x0029,
247 [EEPROM_TXPOWER_BG2] = 0x0030,
248 [EEPROM_TSSI_BOUND_BG1] = 0x0037,
249 [EEPROM_TSSI_BOUND_BG2] = 0x0038,
250 [EEPROM_TSSI_BOUND_BG3] = 0x0039,
251 [EEPROM_TSSI_BOUND_BG4] = 0x003a,
252 [EEPROM_TSSI_BOUND_BG5] = 0x003b,
253 [EEPROM_TXPOWER_A1] = 0x003c,
254 [EEPROM_TXPOWER_A2] = 0x0053,
255 [EEPROM_TSSI_BOUND_A1] = 0x006a,
256 [EEPROM_TSSI_BOUND_A2] = 0x006b,
257 [EEPROM_TSSI_BOUND_A3] = 0x006c,
258 [EEPROM_TSSI_BOUND_A4] = 0x006d,
259 [EEPROM_TSSI_BOUND_A5] = 0x006e,
260 [EEPROM_TXPOWER_BYRATE] = 0x006f,
261 [EEPROM_BBP_START] = 0x0078,
262};
263
Gabor Juhosfa31d152013-07-08 11:25:56 +0200264static const unsigned int rt2800_eeprom_map_ext[EEPROM_WORD_COUNT] = {
265 [EEPROM_CHIP_ID] = 0x0000,
266 [EEPROM_VERSION] = 0x0001,
267 [EEPROM_MAC_ADDR_0] = 0x0002,
268 [EEPROM_MAC_ADDR_1] = 0x0003,
269 [EEPROM_MAC_ADDR_2] = 0x0004,
270 [EEPROM_NIC_CONF0] = 0x001a,
271 [EEPROM_NIC_CONF1] = 0x001b,
272 [EEPROM_NIC_CONF2] = 0x001c,
273 [EEPROM_EIRP_MAX_TX_POWER] = 0x0020,
274 [EEPROM_FREQ] = 0x0022,
275 [EEPROM_LED_AG_CONF] = 0x0023,
276 [EEPROM_LED_ACT_CONF] = 0x0024,
277 [EEPROM_LED_POLARITY] = 0x0025,
278 [EEPROM_LNA] = 0x0026,
279 [EEPROM_EXT_LNA2] = 0x0027,
280 [EEPROM_RSSI_BG] = 0x0028,
281 [EEPROM_TXPOWER_DELTA] = 0x0028, /* Overlaps with RSSI_BG */
282 [EEPROM_RSSI_BG2] = 0x0029,
283 [EEPROM_TXMIXER_GAIN_BG] = 0x0029, /* Overlaps with RSSI_BG2 */
284 [EEPROM_RSSI_A] = 0x002a,
285 [EEPROM_RSSI_A2] = 0x002b,
286 [EEPROM_TXMIXER_GAIN_A] = 0x002b, /* Overlaps with RSSI_A2 */
287 [EEPROM_TXPOWER_BG1] = 0x0030,
288 [EEPROM_TXPOWER_BG2] = 0x0037,
289 [EEPROM_EXT_TXPOWER_BG3] = 0x003e,
290 [EEPROM_TSSI_BOUND_BG1] = 0x0045,
291 [EEPROM_TSSI_BOUND_BG2] = 0x0046,
292 [EEPROM_TSSI_BOUND_BG3] = 0x0047,
293 [EEPROM_TSSI_BOUND_BG4] = 0x0048,
294 [EEPROM_TSSI_BOUND_BG5] = 0x0049,
295 [EEPROM_TXPOWER_A1] = 0x004b,
296 [EEPROM_TXPOWER_A2] = 0x0065,
297 [EEPROM_EXT_TXPOWER_A3] = 0x007f,
298 [EEPROM_TSSI_BOUND_A1] = 0x009a,
299 [EEPROM_TSSI_BOUND_A2] = 0x009b,
300 [EEPROM_TSSI_BOUND_A3] = 0x009c,
301 [EEPROM_TSSI_BOUND_A4] = 0x009d,
302 [EEPROM_TSSI_BOUND_A5] = 0x009e,
303 [EEPROM_TXPOWER_BYRATE] = 0x00a0,
304};
305
Gabor Juhos379448f2013-07-08 11:25:55 +0200306static unsigned int rt2800_eeprom_word_index(struct rt2x00_dev *rt2x00dev,
307 const enum rt2800_eeprom_word word)
308{
309 const unsigned int *map;
310 unsigned int index;
311
312 if (WARN_ONCE(word >= EEPROM_WORD_COUNT,
313 "%s: invalid EEPROM word %d\n",
314 wiphy_name(rt2x00dev->hw->wiphy), word))
315 return 0;
316
Gabor Juhosfa31d152013-07-08 11:25:56 +0200317 if (rt2x00_rt(rt2x00dev, RT3593))
318 map = rt2800_eeprom_map_ext;
319 else
320 map = rt2800_eeprom_map;
321
Gabor Juhos379448f2013-07-08 11:25:55 +0200322 index = map[word];
323
324 /* Index 0 is valid only for EEPROM_CHIP_ID.
325 * Otherwise it means that the offset of the
326 * given word is not initialized in the map,
327 * or that the field is not usable on the
328 * actual chipset.
329 */
330 WARN_ONCE(word != EEPROM_CHIP_ID && index == 0,
331 "%s: invalid access of EEPROM word %d\n",
332 wiphy_name(rt2x00dev->hw->wiphy), word);
333
334 return index;
335}
336
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200337static void *rt2800_eeprom_addr(struct rt2x00_dev *rt2x00dev,
338 const enum rt2800_eeprom_word word)
339{
Gabor Juhos379448f2013-07-08 11:25:55 +0200340 unsigned int index;
341
342 index = rt2800_eeprom_word_index(rt2x00dev, word);
343 return rt2x00_eeprom_addr(rt2x00dev, index);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200344}
345
346static void rt2800_eeprom_read(struct rt2x00_dev *rt2x00dev,
347 const enum rt2800_eeprom_word word, u16 *data)
348{
Gabor Juhos379448f2013-07-08 11:25:55 +0200349 unsigned int index;
350
351 index = rt2800_eeprom_word_index(rt2x00dev, word);
352 rt2x00_eeprom_read(rt2x00dev, index, data);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200353}
354
355static void rt2800_eeprom_write(struct rt2x00_dev *rt2x00dev,
356 const enum rt2800_eeprom_word word, u16 data)
357{
Gabor Juhos379448f2013-07-08 11:25:55 +0200358 unsigned int index;
359
360 index = rt2800_eeprom_word_index(rt2x00dev, word);
361 rt2x00_eeprom_write(rt2x00dev, index, data);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200362}
363
Gabor Juhos022138c2013-07-08 11:25:54 +0200364static void rt2800_eeprom_read_from_array(struct rt2x00_dev *rt2x00dev,
365 const enum rt2800_eeprom_word array,
366 unsigned int offset,
367 u16 *data)
368{
Gabor Juhos379448f2013-07-08 11:25:55 +0200369 unsigned int index;
370
371 index = rt2800_eeprom_word_index(rt2x00dev, array);
372 rt2x00_eeprom_read(rt2x00dev, index + offset, data);
Gabor Juhos022138c2013-07-08 11:25:54 +0200373}
374
Woody Hung16ebd602012-07-31 21:53:33 +0800375static int rt2800_enable_wlan_rt3290(struct rt2x00_dev *rt2x00dev)
376{
377 u32 reg;
378 int i, count;
379
380 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
381 if (rt2x00_get_field32(reg, WLAN_EN))
382 return 0;
383
384 rt2x00_set_field32(&reg, WLAN_GPIO_OUT_OE_BIT_ALL, 0xff);
385 rt2x00_set_field32(&reg, FRC_WL_ANT_SET, 1);
386 rt2x00_set_field32(&reg, WLAN_CLK_EN, 0);
387 rt2x00_set_field32(&reg, WLAN_EN, 1);
388 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
389
390 udelay(REGISTER_BUSY_DELAY);
391
392 count = 0;
393 do {
394 /*
395 * Check PLL_LD & XTAL_RDY.
396 */
397 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
398 rt2800_register_read(rt2x00dev, CMB_CTRL, &reg);
399 if (rt2x00_get_field32(reg, PLL_LD) &&
400 rt2x00_get_field32(reg, XTAL_RDY))
401 break;
402 udelay(REGISTER_BUSY_DELAY);
403 }
404
405 if (i >= REGISTER_BUSY_COUNT) {
406
407 if (count >= 10)
408 return -EIO;
409
410 rt2800_register_write(rt2x00dev, 0x58, 0x018);
411 udelay(REGISTER_BUSY_DELAY);
412 rt2800_register_write(rt2x00dev, 0x58, 0x418);
413 udelay(REGISTER_BUSY_DELAY);
414 rt2800_register_write(rt2x00dev, 0x58, 0x618);
415 udelay(REGISTER_BUSY_DELAY);
416 count++;
417 } else {
418 count = 0;
419 }
420
421 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
422 rt2x00_set_field32(&reg, PCIE_APP0_CLK_REQ, 0);
423 rt2x00_set_field32(&reg, WLAN_CLK_EN, 1);
424 rt2x00_set_field32(&reg, WLAN_RESET, 1);
425 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
426 udelay(10);
427 rt2x00_set_field32(&reg, WLAN_RESET, 0);
428 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
429 udelay(10);
430 rt2800_register_write(rt2x00dev, INT_SOURCE_CSR, 0x7fffffff);
431 } while (count != 0);
432
433 return 0;
434}
435
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100436void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
437 const u8 command, const u8 token,
438 const u8 arg0, const u8 arg1)
439{
440 u32 reg;
441
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100442 /*
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100443 * SOC devices don't support MCU requests.
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100444 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100445 if (rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100446 return;
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100447
448 mutex_lock(&rt2x00dev->csr_mutex);
449
450 /*
451 * Wait until the MCU becomes available, afterwards we
452 * can safely write the new data into the register.
453 */
454 if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
455 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
456 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
457 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
458 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
459 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
460
461 reg = 0;
462 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
463 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
464 }
465
466 mutex_unlock(&rt2x00dev->csr_mutex);
467}
468EXPORT_SYMBOL_GPL(rt2800_mcu_request);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100469
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200470int rt2800_wait_csr_ready(struct rt2x00_dev *rt2x00dev)
471{
472 unsigned int i = 0;
473 u32 reg;
474
475 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
476 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
477 if (reg && reg != ~0)
478 return 0;
479 msleep(1);
480 }
481
Joe Perchesec9c4982013-04-19 08:33:40 -0700482 rt2x00_err(rt2x00dev, "Unstable hardware\n");
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200483 return -EBUSY;
484}
485EXPORT_SYMBOL_GPL(rt2800_wait_csr_ready);
486
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100487int rt2800_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
488{
489 unsigned int i;
490 u32 reg;
491
Helmut Schaa08e53102010-11-04 20:37:47 +0100492 /*
493 * Some devices are really slow to respond here. Wait a whole second
494 * before timing out.
495 */
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100496 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
497 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
498 if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
499 !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
500 return 0;
501
Helmut Schaa08e53102010-11-04 20:37:47 +0100502 msleep(10);
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100503 }
504
Joe Perchesec9c4982013-04-19 08:33:40 -0700505 rt2x00_err(rt2x00dev, "WPDMA TX/RX busy [0x%08x]\n", reg);
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100506 return -EACCES;
507}
508EXPORT_SYMBOL_GPL(rt2800_wait_wpdma_ready);
509
Jakub Kicinskif7b395e2012-04-03 03:40:47 +0200510void rt2800_disable_wpdma(struct rt2x00_dev *rt2x00dev)
511{
512 u32 reg;
513
514 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
515 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
516 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
517 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
518 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
519 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
520 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
521}
522EXPORT_SYMBOL_GPL(rt2800_disable_wpdma);
523
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200524static bool rt2800_check_firmware_crc(const u8 *data, const size_t len)
525{
526 u16 fw_crc;
527 u16 crc;
528
529 /*
530 * The last 2 bytes in the firmware array are the crc checksum itself,
531 * this means that we should never pass those 2 bytes to the crc
532 * algorithm.
533 */
534 fw_crc = (data[len - 2] << 8 | data[len - 1]);
535
536 /*
537 * Use the crc ccitt algorithm.
538 * This will return the same value as the legacy driver which
539 * used bit ordering reversion on the both the firmware bytes
540 * before input input as well as on the final output.
541 * Obviously using crc ccitt directly is much more efficient.
542 */
543 crc = crc_ccitt(~0, data, len - 2);
544
545 /*
546 * There is a small difference between the crc-itu-t + bitrev and
547 * the crc-ccitt crc calculation. In the latter method the 2 bytes
548 * will be swapped, use swab16 to convert the crc to the correct
549 * value.
550 */
551 crc = swab16(crc);
552
553 return fw_crc == crc;
554}
555
556int rt2800_check_firmware(struct rt2x00_dev *rt2x00dev,
557 const u8 *data, const size_t len)
558{
559 size_t offset = 0;
560 size_t fw_len;
561 bool multiple;
562
563 /*
564 * PCI(e) & SOC devices require firmware with a length
565 * of 8kb. USB devices require firmware files with a length
566 * of 4kb. Certain USB chipsets however require different firmware,
567 * which Ralink only provides attached to the original firmware
568 * file. Thus for USB devices, firmware files have a length
Woody Hunga89534e2012-06-13 15:01:16 +0800569 * which is a multiple of 4kb. The firmware for rt3290 chip also
570 * have a length which is a multiple of 4kb.
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200571 */
Woody Hunga89534e2012-06-13 15:01:16 +0800572 if (rt2x00_is_usb(rt2x00dev) || rt2x00_rt(rt2x00dev, RT3290))
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200573 fw_len = 4096;
Woody Hunga89534e2012-06-13 15:01:16 +0800574 else
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200575 fw_len = 8192;
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200576
Woody Hunga89534e2012-06-13 15:01:16 +0800577 multiple = true;
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200578 /*
579 * Validate the firmware length
580 */
581 if (len != fw_len && (!multiple || (len % fw_len) != 0))
582 return FW_BAD_LENGTH;
583
584 /*
585 * Check if the chipset requires one of the upper parts
586 * of the firmware.
587 */
588 if (rt2x00_is_usb(rt2x00dev) &&
589 !rt2x00_rt(rt2x00dev, RT2860) &&
590 !rt2x00_rt(rt2x00dev, RT2872) &&
591 !rt2x00_rt(rt2x00dev, RT3070) &&
592 ((len / fw_len) == 1))
593 return FW_BAD_VERSION;
594
595 /*
596 * 8kb firmware files must be checked as if it were
597 * 2 separate firmware files.
598 */
599 while (offset < len) {
600 if (!rt2800_check_firmware_crc(data + offset, fw_len))
601 return FW_BAD_CRC;
602
603 offset += fw_len;
604 }
605
606 return FW_OK;
607}
608EXPORT_SYMBOL_GPL(rt2800_check_firmware);
609
610int rt2800_load_firmware(struct rt2x00_dev *rt2x00dev,
611 const u8 *data, const size_t len)
612{
613 unsigned int i;
614 u32 reg;
Woody Hung16ebd602012-07-31 21:53:33 +0800615 int retval;
616
617 if (rt2x00_rt(rt2x00dev, RT3290)) {
618 retval = rt2800_enable_wlan_rt3290(rt2x00dev);
619 if (retval)
620 return -EBUSY;
621 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200622
623 /*
Ivo van Doornb9eca242010-08-30 21:13:54 +0200624 * If driver doesn't wake up firmware here,
625 * rt2800_load_firmware will hang forever when interface is up again.
626 */
627 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0x00000000);
628
629 /*
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200630 * Wait for stable hardware.
631 */
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200632 if (rt2800_wait_csr_ready(rt2x00dev))
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200633 return -EBUSY;
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200634
Gabor Juhosadde5882011-03-03 11:46:45 +0100635 if (rt2x00_is_pci(rt2x00dev)) {
Woody Hunga89534e2012-06-13 15:01:16 +0800636 if (rt2x00_rt(rt2x00dev, RT3290) ||
637 rt2x00_rt(rt2x00dev, RT3572) ||
John Li2ed71882012-02-17 17:33:06 +0800638 rt2x00_rt(rt2x00dev, RT5390) ||
639 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +0100640 rt2800_register_read(rt2x00dev, AUX_CTRL, &reg);
641 rt2x00_set_field32(&reg, AUX_CTRL_FORCE_PCIE_CLK, 1);
642 rt2x00_set_field32(&reg, AUX_CTRL_WAKE_PCIE_EN, 1);
643 rt2800_register_write(rt2x00dev, AUX_CTRL, reg);
644 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200645 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
Gabor Juhosadde5882011-03-03 11:46:45 +0100646 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200647
Jakub Kicinskib7e1d222012-04-03 03:40:48 +0200648 rt2800_disable_wpdma(rt2x00dev);
649
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200650 /*
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200651 * Write firmware to the device.
652 */
653 rt2800_drv_write_firmware(rt2x00dev, data, len);
654
655 /*
656 * Wait for device to stabilize.
657 */
658 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
659 rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
660 if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
661 break;
662 msleep(1);
663 }
664
665 if (i == REGISTER_BUSY_COUNT) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700666 rt2x00_err(rt2x00dev, "PBF system register not ready\n");
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200667 return -EBUSY;
668 }
669
670 /*
Stanislaw Gruszka4ed1dd22012-01-24 14:09:07 +0100671 * Disable DMA, will be reenabled later when enabling
672 * the radio.
673 */
Jakub Kicinskif7b395e2012-04-03 03:40:47 +0200674 rt2800_disable_wpdma(rt2x00dev);
Stanislaw Gruszka4ed1dd22012-01-24 14:09:07 +0100675
676 /*
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200677 * Initialize firmware.
678 */
679 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
680 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
Stanislaw Gruszka87561302013-03-16 19:19:45 +0100681 if (rt2x00_is_usb(rt2x00dev)) {
Stanislaw Gruszka0c17cf92012-01-24 14:09:06 +0100682 rt2800_register_write(rt2x00dev, H2M_INT_SRC, 0);
Stanislaw Gruszka87561302013-03-16 19:19:45 +0100683 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
684 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200685 msleep(1);
686
687 return 0;
688}
689EXPORT_SYMBOL_GPL(rt2800_load_firmware);
690
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200691void rt2800_write_tx_data(struct queue_entry *entry,
692 struct txentry_desc *txdesc)
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200693{
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200694 __le32 *txwi = rt2800_drv_get_txwi(entry);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200695 u32 word;
Stanislaw Gruszka557985a2013-04-17 14:30:48 +0200696 int i;
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200697
698 /*
699 * Initialize TX Info descriptor
700 */
701 rt2x00_desc_read(txwi, 0, &word);
702 rt2x00_set_field32(&word, TXWI_W0_FRAG,
703 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
Ivo van Doorn84804cd2010-08-06 20:46:19 +0200704 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS,
705 test_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags));
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200706 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
707 rt2x00_set_field32(&word, TXWI_W0_TS,
708 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
709 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
710 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100711 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY,
712 txdesc->u.ht.mpdu_density);
713 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->u.ht.txop);
714 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->u.ht.mcs);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200715 rt2x00_set_field32(&word, TXWI_W0_BW,
716 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
717 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
718 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100719 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->u.ht.stbc);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200720 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
721 rt2x00_desc_write(txwi, 0, word);
722
723 rt2x00_desc_read(txwi, 1, &word);
724 rt2x00_set_field32(&word, TXWI_W1_ACK,
725 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
726 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
727 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100728 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->u.ht.ba_size);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200729 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
730 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
Helmut Schaaa2b13282011-09-08 14:38:01 +0200731 txdesc->key_idx : txdesc->u.ht.wcid);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200732 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
733 txdesc->length);
Helmut Schaa2b23cda2010-11-04 20:38:15 +0100734 rt2x00_set_field32(&word, TXWI_W1_PACKETID_QUEUE, entry->queue->qid);
Ivo van Doornbc8a9792010-10-02 11:32:43 +0200735 rt2x00_set_field32(&word, TXWI_W1_PACKETID_ENTRY, (entry->entry_idx % 3) + 1);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200736 rt2x00_desc_write(txwi, 1, word);
737
738 /*
Stanislaw Gruszka557985a2013-04-17 14:30:48 +0200739 * Always write 0 to IV/EIV fields (word 2 and 3), hardware will insert
740 * the IV from the IVEIV register when TXD_W3_WIV is set to 0.
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200741 * When TXD_W3_WIV is set to 1 it will use the IV data
742 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
743 * crypto entry in the registers should be used to encrypt the frame.
Stanislaw Gruszka557985a2013-04-17 14:30:48 +0200744 *
745 * Nulify all remaining words as well, we don't know how to program them.
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200746 */
Stanislaw Gruszka557985a2013-04-17 14:30:48 +0200747 for (i = 2; i < entry->queue->winfo_size / sizeof(__le32); i++)
748 _rt2x00_desc_write(txwi, i, 0);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200749}
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200750EXPORT_SYMBOL_GPL(rt2800_write_tx_data);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200751
Helmut Schaaff6133b2010-10-09 13:34:11 +0200752static int rt2800_agc_to_rssi(struct rt2x00_dev *rt2x00dev, u32 rxwi_w2)
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200753{
Luigi Tarenga7fc41752012-01-31 18:51:23 +0100754 s8 rssi0 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI0);
755 s8 rssi1 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI1);
756 s8 rssi2 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI2);
Ivo van Doorn74861922010-07-11 12:23:50 +0200757 u16 eeprom;
758 u8 offset0;
759 u8 offset1;
760 u8 offset2;
761
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200762 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200763 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &eeprom);
Ivo van Doorn74861922010-07-11 12:23:50 +0200764 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET0);
765 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET1);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200766 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
Ivo van Doorn74861922010-07-11 12:23:50 +0200767 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_OFFSET2);
768 } else {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200769 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &eeprom);
Ivo van Doorn74861922010-07-11 12:23:50 +0200770 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET0);
771 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET1);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200772 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
Ivo van Doorn74861922010-07-11 12:23:50 +0200773 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_OFFSET2);
774 }
775
776 /*
777 * Convert the value from the descriptor into the RSSI value
778 * If the value in the descriptor is 0, it is considered invalid
779 * and the default (extremely low) rssi value is assumed
780 */
781 rssi0 = (rssi0) ? (-12 - offset0 - rt2x00dev->lna_gain - rssi0) : -128;
782 rssi1 = (rssi1) ? (-12 - offset1 - rt2x00dev->lna_gain - rssi1) : -128;
783 rssi2 = (rssi2) ? (-12 - offset2 - rt2x00dev->lna_gain - rssi2) : -128;
784
785 /*
786 * mac80211 only accepts a single RSSI value. Calculating the
787 * average doesn't deliver a fair answer either since -60:-60 would
788 * be considered equally good as -50:-70 while the second is the one
789 * which gives less energy...
790 */
791 rssi0 = max(rssi0, rssi1);
Luigi Tarenga7fc41752012-01-31 18:51:23 +0100792 return (int)max(rssi0, rssi2);
Ivo van Doorn74861922010-07-11 12:23:50 +0200793}
794
795void rt2800_process_rxwi(struct queue_entry *entry,
796 struct rxdone_entry_desc *rxdesc)
797{
798 __le32 *rxwi = (__le32 *) entry->skb->data;
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200799 u32 word;
800
801 rt2x00_desc_read(rxwi, 0, &word);
802
803 rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
804 rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
805
806 rt2x00_desc_read(rxwi, 1, &word);
807
808 if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
809 rxdesc->flags |= RX_FLAG_SHORT_GI;
810
811 if (rt2x00_get_field32(word, RXWI_W1_BW))
812 rxdesc->flags |= RX_FLAG_40MHZ;
813
814 /*
815 * Detect RX rate, always use MCS as signal type.
816 */
817 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
818 rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
819 rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
820
821 /*
822 * Mask of 0x8 bit to remove the short preamble flag.
823 */
824 if (rxdesc->rate_mode == RATE_MODE_CCK)
825 rxdesc->signal &= ~0x8;
826
827 rt2x00_desc_read(rxwi, 2, &word);
828
Ivo van Doorn74861922010-07-11 12:23:50 +0200829 /*
830 * Convert descriptor AGC value to RSSI value.
831 */
832 rxdesc->rssi = rt2800_agc_to_rssi(entry->queue->rt2x00dev, word);
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +0200833 /*
834 * Remove RXWI descriptor from start of the buffer.
835 */
836 skb_pull(entry->skb, entry->queue->winfo_size);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200837}
838EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
839
Helmut Schaa31937c42011-09-07 20:10:02 +0200840void rt2800_txdone_entry(struct queue_entry *entry, u32 status, __le32 *txwi)
Helmut Schaa14433332010-10-02 11:27:03 +0200841{
842 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Helmut Schaab34793e2010-10-02 11:34:56 +0200843 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Helmut Schaa14433332010-10-02 11:27:03 +0200844 struct txdone_entry_desc txdesc;
845 u32 word;
846 u16 mcs, real_mcs;
Helmut Schaab34793e2010-10-02 11:34:56 +0200847 int aggr, ampdu;
Helmut Schaa14433332010-10-02 11:27:03 +0200848
849 /*
850 * Obtain the status about this packet.
851 */
852 txdesc.flags = 0;
Helmut Schaa14433332010-10-02 11:27:03 +0200853 rt2x00_desc_read(txwi, 0, &word);
Helmut Schaab34793e2010-10-02 11:34:56 +0200854
Helmut Schaa14433332010-10-02 11:27:03 +0200855 mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200856 ampdu = rt2x00_get_field32(word, TXWI_W0_AMPDU);
857
Helmut Schaa14433332010-10-02 11:27:03 +0200858 real_mcs = rt2x00_get_field32(status, TX_STA_FIFO_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200859 aggr = rt2x00_get_field32(status, TX_STA_FIFO_TX_AGGRE);
860
861 /*
862 * If a frame was meant to be sent as a single non-aggregated MPDU
863 * but ended up in an aggregate the used tx rate doesn't correlate
864 * with the one specified in the TXWI as the whole aggregate is sent
865 * with the same rate.
866 *
867 * For example: two frames are sent to rt2x00, the first one sets
868 * AMPDU=1 and requests MCS7 whereas the second frame sets AMDPU=0
869 * and requests MCS15. If the hw aggregates both frames into one
870 * AMDPU the tx status for both frames will contain MCS7 although
871 * the frame was sent successfully.
872 *
873 * Hence, replace the requested rate with the real tx rate to not
874 * confuse the rate control algortihm by providing clearly wrong
875 * data.
876 */
Helmut Schaa5356d962011-03-03 19:40:33 +0100877 if (unlikely(aggr == 1 && ampdu == 0 && real_mcs != mcs)) {
Helmut Schaab34793e2010-10-02 11:34:56 +0200878 skbdesc->tx_rate_idx = real_mcs;
879 mcs = real_mcs;
880 }
Helmut Schaa14433332010-10-02 11:27:03 +0200881
Helmut Schaaf16d2db2011-03-28 13:35:21 +0200882 if (aggr == 1 || ampdu == 1)
883 __set_bit(TXDONE_AMPDU, &txdesc.flags);
884
Helmut Schaa14433332010-10-02 11:27:03 +0200885 /*
886 * Ralink has a retry mechanism using a global fallback
887 * table. We setup this fallback table to try the immediate
888 * lower rate for all rates. In the TX_STA_FIFO, the MCS field
889 * always contains the MCS used for the last transmission, be
890 * it successful or not.
891 */
892 if (rt2x00_get_field32(status, TX_STA_FIFO_TX_SUCCESS)) {
893 /*
894 * Transmission succeeded. The number of retries is
895 * mcs - real_mcs
896 */
897 __set_bit(TXDONE_SUCCESS, &txdesc.flags);
898 txdesc.retry = ((mcs > real_mcs) ? mcs - real_mcs : 0);
899 } else {
900 /*
901 * Transmission failed. The number of retries is
902 * always 7 in this case (for a total number of 8
903 * frames sent).
904 */
905 __set_bit(TXDONE_FAILURE, &txdesc.flags);
906 txdesc.retry = rt2x00dev->long_retry;
907 }
908
909 /*
910 * the frame was retried at least once
911 * -> hw used fallback rates
912 */
913 if (txdesc.retry)
914 __set_bit(TXDONE_FALLBACK, &txdesc.flags);
915
916 rt2x00lib_txdone(entry, &txdesc);
917}
918EXPORT_SYMBOL_GPL(rt2800_txdone_entry);
919
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200920void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
921{
922 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
923 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
924 unsigned int beacon_base;
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100925 unsigned int padding_len;
Seth Forsheed76dfc62011-02-14 08:52:25 -0600926 u32 orig_reg, reg;
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +0200927 const int txwi_desc_size = entry->queue->winfo_size;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200928
929 /*
930 * Disable beaconing while we are reloading the beacon data,
931 * otherwise we might be sending out invalid data.
932 */
933 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Seth Forsheed76dfc62011-02-14 08:52:25 -0600934 orig_reg = reg;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200935 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
936 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
937
938 /*
939 * Add space for the TXWI in front of the skb.
940 */
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +0200941 memset(skb_push(entry->skb, txwi_desc_size), 0, txwi_desc_size);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200942
943 /*
944 * Register descriptor details in skb frame descriptor.
945 */
946 skbdesc->flags |= SKBDESC_DESC_IN_SKB;
947 skbdesc->desc = entry->skb->data;
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +0200948 skbdesc->desc_len = txwi_desc_size;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200949
950 /*
951 * Add the TXWI for the beacon to the skb.
952 */
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200953 rt2800_write_tx_data(entry, txdesc);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200954
955 /*
956 * Dump beacon to userspace through debugfs.
957 */
958 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
959
960 /*
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100961 * Write entire beacon with TXWI and padding to register.
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200962 */
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100963 padding_len = roundup(entry->skb->len, 4) - entry->skb->len;
Seth Forsheed76dfc62011-02-14 08:52:25 -0600964 if (padding_len && skb_pad(entry->skb, padding_len)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700965 rt2x00_err(rt2x00dev, "Failure padding beacon, aborting\n");
Seth Forsheed76dfc62011-02-14 08:52:25 -0600966 /* skb freed by skb_pad() on failure */
967 entry->skb = NULL;
968 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, orig_reg);
969 return;
970 }
971
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200972 beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100973 rt2800_register_multiwrite(rt2x00dev, beacon_base, entry->skb->data,
974 entry->skb->len + padding_len);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200975
976 /*
977 * Enable beaconing again.
978 */
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200979 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
980 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
981
982 /*
983 * Clean up beacon skb.
984 */
985 dev_kfree_skb_any(entry->skb);
986 entry->skb = NULL;
987}
Ivo van Doorn50e888e2010-07-11 12:26:12 +0200988EXPORT_SYMBOL_GPL(rt2800_write_beacon);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200989
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100990static inline void rt2800_clear_beacon_register(struct rt2x00_dev *rt2x00dev,
991 unsigned int beacon_base)
Helmut Schaafdb87252010-06-29 21:48:06 +0200992{
993 int i;
Gabor Juhos0879f872013-05-01 17:17:33 +0200994 const int txwi_desc_size = rt2x00dev->bcn->winfo_size;
Helmut Schaafdb87252010-06-29 21:48:06 +0200995
996 /*
997 * For the Beacon base registers we only need to clear
998 * the whole TXWI which (when set to 0) will invalidate
999 * the entire beacon.
1000 */
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +02001001 for (i = 0; i < txwi_desc_size; i += sizeof(__le32))
Helmut Schaafdb87252010-06-29 21:48:06 +02001002 rt2800_register_write(rt2x00dev, beacon_base + i, 0);
1003}
1004
Helmut Schaa69cf36a2011-01-30 13:16:03 +01001005void rt2800_clear_beacon(struct queue_entry *entry)
1006{
1007 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
1008 u32 reg;
1009
1010 /*
1011 * Disable beaconing while we are reloading the beacon data,
1012 * otherwise we might be sending out invalid data.
1013 */
1014 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1015 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1016 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1017
1018 /*
1019 * Clear beacon.
1020 */
1021 rt2800_clear_beacon_register(rt2x00dev,
1022 HW_BEACON_OFFSET(entry->entry_idx));
1023
1024 /*
1025 * Enabled beaconing again.
1026 */
1027 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
1028 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1029}
1030EXPORT_SYMBOL_GPL(rt2800_clear_beacon);
1031
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001032#ifdef CONFIG_RT2X00_LIB_DEBUGFS
1033const struct rt2x00debug rt2800_rt2x00debug = {
1034 .owner = THIS_MODULE,
1035 .csr = {
1036 .read = rt2800_register_read,
1037 .write = rt2800_register_write,
1038 .flags = RT2X00DEBUGFS_OFFSET,
1039 .word_base = CSR_REG_BASE,
1040 .word_size = sizeof(u32),
1041 .word_count = CSR_REG_SIZE / sizeof(u32),
1042 },
1043 .eeprom = {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001044 /* NOTE: The local EEPROM access functions can't
1045 * be used here, use the generic versions instead.
1046 */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001047 .read = rt2x00_eeprom_read,
1048 .write = rt2x00_eeprom_write,
1049 .word_base = EEPROM_BASE,
1050 .word_size = sizeof(u16),
1051 .word_count = EEPROM_SIZE / sizeof(u16),
1052 },
1053 .bbp = {
1054 .read = rt2800_bbp_read,
1055 .write = rt2800_bbp_write,
1056 .word_base = BBP_BASE,
1057 .word_size = sizeof(u8),
1058 .word_count = BBP_SIZE / sizeof(u8),
1059 },
1060 .rf = {
1061 .read = rt2x00_rf_read,
1062 .write = rt2800_rf_write,
1063 .word_base = RF_BASE,
1064 .word_size = sizeof(u32),
1065 .word_count = RF_SIZE / sizeof(u32),
1066 },
Anisse Astierf2bd7f12012-04-19 15:53:10 +02001067 .rfcsr = {
1068 .read = rt2800_rfcsr_read,
1069 .write = rt2800_rfcsr_write,
1070 .word_base = RFCSR_BASE,
1071 .word_size = sizeof(u8),
1072 .word_count = RFCSR_SIZE / sizeof(u8),
1073 },
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001074};
1075EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
1076#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
1077
1078int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
1079{
1080 u32 reg;
1081
Woody Hunga89534e2012-06-13 15:01:16 +08001082 if (rt2x00_rt(rt2x00dev, RT3290)) {
1083 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
1084 return rt2x00_get_field32(reg, WLAN_GPIO_IN_BIT0);
1085 } else {
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02001086 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
1087 return rt2x00_get_field32(reg, GPIO_CTRL_VAL2);
Woody Hunga89534e2012-06-13 15:01:16 +08001088 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001089}
1090EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
1091
1092#ifdef CONFIG_RT2X00_LIB_LEDS
1093static void rt2800_brightness_set(struct led_classdev *led_cdev,
1094 enum led_brightness brightness)
1095{
1096 struct rt2x00_led *led =
1097 container_of(led_cdev, struct rt2x00_led, led_dev);
1098 unsigned int enabled = brightness != LED_OFF;
1099 unsigned int bg_mode =
1100 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
1101 unsigned int polarity =
1102 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
1103 EEPROM_FREQ_LED_POLARITY);
1104 unsigned int ledmode =
1105 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
1106 EEPROM_FREQ_LED_MODE);
Layne Edwards44704e52011-04-18 15:26:00 +02001107 u32 reg;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001108
Layne Edwards44704e52011-04-18 15:26:00 +02001109 /* Check for SoC (SOC devices don't support MCU requests) */
1110 if (rt2x00_is_soc(led->rt2x00dev)) {
1111 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
1112
1113 /* Set LED Polarity */
1114 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, polarity);
1115
1116 /* Set LED Mode */
1117 if (led->type == LED_TYPE_RADIO) {
1118 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE,
1119 enabled ? 3 : 0);
1120 } else if (led->type == LED_TYPE_ASSOC) {
1121 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE,
1122 enabled ? 3 : 0);
1123 } else if (led->type == LED_TYPE_QUALITY) {
1124 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE,
1125 enabled ? 3 : 0);
1126 }
1127
1128 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
1129
1130 } else {
1131 if (led->type == LED_TYPE_RADIO) {
1132 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
1133 enabled ? 0x20 : 0);
1134 } else if (led->type == LED_TYPE_ASSOC) {
1135 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
1136 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
1137 } else if (led->type == LED_TYPE_QUALITY) {
1138 /*
1139 * The brightness is divided into 6 levels (0 - 5),
1140 * The specs tell us the following levels:
1141 * 0, 1 ,3, 7, 15, 31
1142 * to determine the level in a simple way we can simply
1143 * work with bitshifting:
1144 * (1 << level) - 1
1145 */
1146 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
1147 (1 << brightness / (LED_FULL / 6)) - 1,
1148 polarity);
1149 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001150 }
1151}
1152
Gertjan van Wingerdeb3579d62009-12-30 11:36:34 +01001153static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001154 struct rt2x00_led *led, enum led_type type)
1155{
1156 led->rt2x00dev = rt2x00dev;
1157 led->type = type;
1158 led->led_dev.brightness_set = rt2800_brightness_set;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001159 led->flags = LED_INITIALIZED;
1160}
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001161#endif /* CONFIG_RT2X00_LIB_LEDS */
1162
1163/*
1164 * Configuration handlers.
1165 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001166static void rt2800_config_wcid(struct rt2x00_dev *rt2x00dev,
1167 const u8 *address,
1168 int wcid)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001169{
1170 struct mac_wcid_entry wcid_entry;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001171 u32 offset;
1172
1173 offset = MAC_WCID_ENTRY(wcid);
1174
1175 memset(&wcid_entry, 0xff, sizeof(wcid_entry));
1176 if (address)
1177 memcpy(wcid_entry.mac, address, ETH_ALEN);
1178
1179 rt2800_register_multiwrite(rt2x00dev, offset,
1180 &wcid_entry, sizeof(wcid_entry));
1181}
1182
1183static void rt2800_delete_wcid_attr(struct rt2x00_dev *rt2x00dev, int wcid)
1184{
1185 u32 offset;
1186 offset = MAC_WCID_ATTR_ENTRY(wcid);
1187 rt2800_register_write(rt2x00dev, offset, 0);
1188}
1189
1190static void rt2800_config_wcid_attr_bssidx(struct rt2x00_dev *rt2x00dev,
1191 int wcid, u32 bssidx)
1192{
1193 u32 offset = MAC_WCID_ATTR_ENTRY(wcid);
1194 u32 reg;
1195
1196 /*
1197 * The BSS Idx numbers is split in a main value of 3 bits,
1198 * and a extended field for adding one additional bit to the value.
1199 */
1200 rt2800_register_read(rt2x00dev, offset, &reg);
1201 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX, (bssidx & 0x7));
1202 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
1203 (bssidx & 0x8) >> 3);
1204 rt2800_register_write(rt2x00dev, offset, reg);
1205}
1206
1207static void rt2800_config_wcid_attr_cipher(struct rt2x00_dev *rt2x00dev,
1208 struct rt2x00lib_crypto *crypto,
1209 struct ieee80211_key_conf *key)
1210{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001211 struct mac_iveiv_entry iveiv_entry;
1212 u32 offset;
1213 u32 reg;
1214
1215 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
1216
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001217 if (crypto->cmd == SET_KEY) {
1218 rt2800_register_read(rt2x00dev, offset, &reg);
1219 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
1220 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
1221 /*
1222 * Both the cipher as the BSS Idx numbers are split in a main
1223 * value of 3 bits, and a extended field for adding one additional
1224 * bit to the value.
1225 */
1226 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
1227 (crypto->cipher & 0x7));
1228 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
1229 (crypto->cipher & 0x8) >> 3);
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001230 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
1231 rt2800_register_write(rt2x00dev, offset, reg);
1232 } else {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001233 /* Delete the cipher without touching the bssidx */
1234 rt2800_register_read(rt2x00dev, offset, &reg);
1235 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB, 0);
1236 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER, 0);
1237 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT, 0);
1238 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, 0);
1239 rt2800_register_write(rt2x00dev, offset, reg);
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001240 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001241
1242 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
1243
1244 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
1245 if ((crypto->cipher == CIPHER_TKIP) ||
1246 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
1247 (crypto->cipher == CIPHER_AES))
1248 iveiv_entry.iv[3] |= 0x20;
1249 iveiv_entry.iv[3] |= key->keyidx << 6;
1250 rt2800_register_multiwrite(rt2x00dev, offset,
1251 &iveiv_entry, sizeof(iveiv_entry));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001252}
1253
1254int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
1255 struct rt2x00lib_crypto *crypto,
1256 struct ieee80211_key_conf *key)
1257{
1258 struct hw_key_entry key_entry;
1259 struct rt2x00_field32 field;
1260 u32 offset;
1261 u32 reg;
1262
1263 if (crypto->cmd == SET_KEY) {
1264 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
1265
1266 memcpy(key_entry.key, crypto->key,
1267 sizeof(key_entry.key));
1268 memcpy(key_entry.tx_mic, crypto->tx_mic,
1269 sizeof(key_entry.tx_mic));
1270 memcpy(key_entry.rx_mic, crypto->rx_mic,
1271 sizeof(key_entry.rx_mic));
1272
1273 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
1274 rt2800_register_multiwrite(rt2x00dev, offset,
1275 &key_entry, sizeof(key_entry));
1276 }
1277
1278 /*
1279 * The cipher types are stored over multiple registers
1280 * starting with SHARED_KEY_MODE_BASE each word will have
1281 * 32 bits and contains the cipher types for 2 bssidx each.
1282 * Using the correct defines correctly will cause overhead,
1283 * so just calculate the correct offset.
1284 */
1285 field.bit_offset = 4 * (key->hw_key_idx % 8);
1286 field.bit_mask = 0x7 << field.bit_offset;
1287
1288 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
1289
1290 rt2800_register_read(rt2x00dev, offset, &reg);
1291 rt2x00_set_field32(&reg, field,
1292 (crypto->cmd == SET_KEY) * crypto->cipher);
1293 rt2800_register_write(rt2x00dev, offset, reg);
1294
1295 /*
1296 * Update WCID information
1297 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001298 rt2800_config_wcid(rt2x00dev, crypto->address, key->hw_key_idx);
1299 rt2800_config_wcid_attr_bssidx(rt2x00dev, key->hw_key_idx,
1300 crypto->bssidx);
1301 rt2800_config_wcid_attr_cipher(rt2x00dev, crypto, key);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001302
1303 return 0;
1304}
1305EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
1306
Helmut Schaaa2b13282011-09-08 14:38:01 +02001307static inline int rt2800_find_wcid(struct rt2x00_dev *rt2x00dev)
Helmut Schaa1ed38112011-03-03 19:44:33 +01001308{
Helmut Schaaa2b13282011-09-08 14:38:01 +02001309 struct mac_wcid_entry wcid_entry;
Helmut Schaa1ed38112011-03-03 19:44:33 +01001310 int idx;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001311 u32 offset;
Helmut Schaa1ed38112011-03-03 19:44:33 +01001312
1313 /*
Helmut Schaaa2b13282011-09-08 14:38:01 +02001314 * Search for the first free WCID entry and return the corresponding
1315 * index.
Helmut Schaa1ed38112011-03-03 19:44:33 +01001316 *
1317 * Make sure the WCID starts _after_ the last possible shared key
1318 * entry (>32).
1319 *
1320 * Since parts of the pairwise key table might be shared with
1321 * the beacon frame buffers 6 & 7 we should only write into the
1322 * first 222 entries.
1323 */
1324 for (idx = 33; idx <= 222; idx++) {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001325 offset = MAC_WCID_ENTRY(idx);
1326 rt2800_register_multiread(rt2x00dev, offset, &wcid_entry,
1327 sizeof(wcid_entry));
1328 if (is_broadcast_ether_addr(wcid_entry.mac))
Helmut Schaa1ed38112011-03-03 19:44:33 +01001329 return idx;
1330 }
Helmut Schaaa2b13282011-09-08 14:38:01 +02001331
1332 /*
1333 * Use -1 to indicate that we don't have any more space in the WCID
1334 * table.
1335 */
Helmut Schaa1ed38112011-03-03 19:44:33 +01001336 return -1;
1337}
1338
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001339int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
1340 struct rt2x00lib_crypto *crypto,
1341 struct ieee80211_key_conf *key)
1342{
1343 struct hw_key_entry key_entry;
1344 u32 offset;
1345
1346 if (crypto->cmd == SET_KEY) {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001347 /*
1348 * Allow key configuration only for STAs that are
1349 * known by the hw.
1350 */
1351 if (crypto->wcid < 0)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001352 return -ENOSPC;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001353 key->hw_key_idx = crypto->wcid;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001354
1355 memcpy(key_entry.key, crypto->key,
1356 sizeof(key_entry.key));
1357 memcpy(key_entry.tx_mic, crypto->tx_mic,
1358 sizeof(key_entry.tx_mic));
1359 memcpy(key_entry.rx_mic, crypto->rx_mic,
1360 sizeof(key_entry.rx_mic));
1361
1362 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
1363 rt2800_register_multiwrite(rt2x00dev, offset,
1364 &key_entry, sizeof(key_entry));
1365 }
1366
1367 /*
1368 * Update WCID information
1369 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001370 rt2800_config_wcid_attr_cipher(rt2x00dev, crypto, key);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001371
1372 return 0;
1373}
1374EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
1375
Helmut Schaaa2b13282011-09-08 14:38:01 +02001376int rt2800_sta_add(struct rt2x00_dev *rt2x00dev, struct ieee80211_vif *vif,
1377 struct ieee80211_sta *sta)
1378{
1379 int wcid;
1380 struct rt2x00_sta *sta_priv = sta_to_rt2x00_sta(sta);
1381
1382 /*
1383 * Find next free WCID.
1384 */
1385 wcid = rt2800_find_wcid(rt2x00dev);
1386
1387 /*
1388 * Store selected wcid even if it is invalid so that we can
1389 * later decide if the STA is uploaded into the hw.
1390 */
1391 sta_priv->wcid = wcid;
1392
1393 /*
1394 * No space left in the device, however, we can still communicate
1395 * with the STA -> No error.
1396 */
1397 if (wcid < 0)
1398 return 0;
1399
1400 /*
1401 * Clean up WCID attributes and write STA address to the device.
1402 */
1403 rt2800_delete_wcid_attr(rt2x00dev, wcid);
1404 rt2800_config_wcid(rt2x00dev, sta->addr, wcid);
1405 rt2800_config_wcid_attr_bssidx(rt2x00dev, wcid,
1406 rt2x00lib_get_bssidx(rt2x00dev, vif));
1407 return 0;
1408}
1409EXPORT_SYMBOL_GPL(rt2800_sta_add);
1410
1411int rt2800_sta_remove(struct rt2x00_dev *rt2x00dev, int wcid)
1412{
1413 /*
1414 * Remove WCID entry, no need to clean the attributes as they will
1415 * get renewed when the WCID is reused.
1416 */
1417 rt2800_config_wcid(rt2x00dev, NULL, wcid);
1418
1419 return 0;
1420}
1421EXPORT_SYMBOL_GPL(rt2800_sta_remove);
1422
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001423void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
1424 const unsigned int filter_flags)
1425{
1426 u32 reg;
1427
1428 /*
1429 * Start configuration steps.
1430 * Note that the version error will always be dropped
1431 * and broadcast frames will always be accepted since
1432 * there is no filter for it at this time.
1433 */
1434 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
1435 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
1436 !(filter_flags & FIF_FCSFAIL));
1437 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
1438 !(filter_flags & FIF_PLCPFAIL));
1439 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
1440 !(filter_flags & FIF_PROMISC_IN_BSS));
1441 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
1442 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
1443 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
1444 !(filter_flags & FIF_ALLMULTI));
1445 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
1446 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
1447 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
1448 !(filter_flags & FIF_CONTROL));
1449 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
1450 !(filter_flags & FIF_CONTROL));
1451 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
1452 !(filter_flags & FIF_CONTROL));
1453 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
1454 !(filter_flags & FIF_CONTROL));
1455 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
1456 !(filter_flags & FIF_CONTROL));
1457 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
1458 !(filter_flags & FIF_PSPOLL));
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +01001459 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 0);
Helmut Schaa48839932011-11-24 09:13:26 +01001460 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR,
1461 !(filter_flags & FIF_CONTROL));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001462 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
1463 !(filter_flags & FIF_CONTROL));
1464 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
1465}
1466EXPORT_SYMBOL_GPL(rt2800_config_filter);
1467
1468void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
1469 struct rt2x00intf_conf *conf, const unsigned int flags)
1470{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001471 u32 reg;
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001472 bool update_bssid = false;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001473
1474 if (flags & CONFIG_UPDATE_TYPE) {
1475 /*
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001476 * Enable synchronisation.
1477 */
1478 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001479 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001480 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Helmut Schaa15a533c2011-04-18 15:28:04 +02001481
1482 if (conf->sync == TSF_SYNC_AP_NONE) {
1483 /*
1484 * Tune beacon queue transmit parameters for AP mode
1485 */
1486 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1487 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 0);
1488 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 1);
1489 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1490 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 0);
1491 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1492 } else {
1493 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1494 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 4);
1495 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 2);
1496 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1497 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 16);
1498 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1499 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001500 }
1501
1502 if (flags & CONFIG_UPDATE_MAC) {
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001503 if (flags & CONFIG_UPDATE_TYPE &&
1504 conf->sync == TSF_SYNC_AP_NONE) {
1505 /*
1506 * The BSSID register has to be set to our own mac
1507 * address in AP mode.
1508 */
1509 memcpy(conf->bssid, conf->mac, sizeof(conf->mac));
1510 update_bssid = true;
1511 }
1512
Ivo van Doornc600c822010-08-30 21:14:15 +02001513 if (!is_zero_ether_addr((const u8 *)conf->mac)) {
1514 reg = le32_to_cpu(conf->mac[1]);
1515 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
1516 conf->mac[1] = cpu_to_le32(reg);
1517 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001518
1519 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
1520 conf->mac, sizeof(conf->mac));
1521 }
1522
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001523 if ((flags & CONFIG_UPDATE_BSSID) || update_bssid) {
Ivo van Doornc600c822010-08-30 21:14:15 +02001524 if (!is_zero_ether_addr((const u8 *)conf->bssid)) {
1525 reg = le32_to_cpu(conf->bssid[1]);
1526 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
1527 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 7);
1528 conf->bssid[1] = cpu_to_le32(reg);
1529 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001530
1531 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
1532 conf->bssid, sizeof(conf->bssid));
1533 }
1534}
1535EXPORT_SYMBOL_GPL(rt2800_config_intf);
1536
Helmut Schaa87c19152010-10-02 11:28:34 +02001537static void rt2800_config_ht_opmode(struct rt2x00_dev *rt2x00dev,
1538 struct rt2x00lib_erp *erp)
1539{
1540 bool any_sta_nongf = !!(erp->ht_opmode &
1541 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1542 u8 protection = erp->ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION;
1543 u8 mm20_mode, mm40_mode, gf20_mode, gf40_mode;
1544 u16 mm20_rate, mm40_rate, gf20_rate, gf40_rate;
1545 u32 reg;
1546
1547 /* default protection rate for HT20: OFDM 24M */
1548 mm20_rate = gf20_rate = 0x4004;
1549
1550 /* default protection rate for HT40: duplicate OFDM 24M */
1551 mm40_rate = gf40_rate = 0x4084;
1552
1553 switch (protection) {
1554 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
1555 /*
1556 * All STAs in this BSS are HT20/40 but there might be
1557 * STAs not supporting greenfield mode.
1558 * => Disable protection for HT transmissions.
1559 */
1560 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 0;
1561
1562 break;
1563 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
1564 /*
1565 * All STAs in this BSS are HT20 or HT20/40 but there
1566 * might be STAs not supporting greenfield mode.
1567 * => Protect all HT40 transmissions.
1568 */
1569 mm20_mode = gf20_mode = 0;
1570 mm40_mode = gf40_mode = 2;
1571
1572 break;
1573 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
1574 /*
1575 * Nonmember protection:
1576 * According to 802.11n we _should_ protect all
1577 * HT transmissions (but we don't have to).
1578 *
1579 * But if cts_protection is enabled we _shall_ protect
1580 * all HT transmissions using a CCK rate.
1581 *
1582 * And if any station is non GF we _shall_ protect
1583 * GF transmissions.
1584 *
1585 * We decide to protect everything
1586 * -> fall through to mixed mode.
1587 */
1588 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
1589 /*
1590 * Legacy STAs are present
1591 * => Protect all HT transmissions.
1592 */
1593 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 2;
1594
1595 /*
1596 * If erp protection is needed we have to protect HT
1597 * transmissions with CCK 11M long preamble.
1598 */
1599 if (erp->cts_protection) {
1600 /* don't duplicate RTS/CTS in CCK mode */
1601 mm20_rate = mm40_rate = 0x0003;
1602 gf20_rate = gf40_rate = 0x0003;
1603 }
1604 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001605 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001606
1607 /* check for STAs not supporting greenfield mode */
1608 if (any_sta_nongf)
1609 gf20_mode = gf40_mode = 2;
1610
1611 /* Update HT protection config */
1612 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1613 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, mm20_rate);
1614 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, mm20_mode);
1615 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1616
1617 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1618 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, mm40_rate);
1619 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, mm40_mode);
1620 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1621
1622 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1623 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, gf20_rate);
1624 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, gf20_mode);
1625 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1626
1627 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1628 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, gf40_rate);
1629 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, gf40_mode);
1630 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1631}
1632
Helmut Schaa02044642010-09-08 20:56:32 +02001633void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp,
1634 u32 changed)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001635{
1636 u32 reg;
1637
Helmut Schaa02044642010-09-08 20:56:32 +02001638 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1639 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1640 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
1641 !!erp->short_preamble);
1642 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
1643 !!erp->short_preamble);
1644 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1645 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001646
Helmut Schaa02044642010-09-08 20:56:32 +02001647 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1648 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
1649 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
1650 erp->cts_protection ? 2 : 0);
1651 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1652 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001653
Helmut Schaa02044642010-09-08 20:56:32 +02001654 if (changed & BSS_CHANGED_BASIC_RATES) {
1655 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
1656 erp->basic_rates);
1657 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1658 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001659
Helmut Schaa02044642010-09-08 20:56:32 +02001660 if (changed & BSS_CHANGED_ERP_SLOT) {
1661 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1662 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME,
1663 erp->slot_time);
1664 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001665
Helmut Schaa02044642010-09-08 20:56:32 +02001666 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
1667 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
1668 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1669 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001670
Helmut Schaa02044642010-09-08 20:56:32 +02001671 if (changed & BSS_CHANGED_BEACON_INT) {
1672 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1673 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
1674 erp->beacon_int * 16);
1675 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1676 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001677
1678 if (changed & BSS_CHANGED_HT)
1679 rt2800_config_ht_opmode(rt2x00dev, erp);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001680}
1681EXPORT_SYMBOL_GPL(rt2800_config_erp);
1682
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001683static void rt2800_config_3572bt_ant(struct rt2x00_dev *rt2x00dev)
1684{
1685 u32 reg;
1686 u16 eeprom;
1687 u8 led_ctrl, led_g_mode, led_r_mode;
1688
1689 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
1690 if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
1691 rt2x00_set_field32(&reg, GPIO_SWITCH_0, 1);
1692 rt2x00_set_field32(&reg, GPIO_SWITCH_1, 1);
1693 } else {
1694 rt2x00_set_field32(&reg, GPIO_SWITCH_0, 0);
1695 rt2x00_set_field32(&reg, GPIO_SWITCH_1, 0);
1696 }
1697 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
1698
1699 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1700 led_g_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 3 : 0;
1701 led_r_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 0 : 3;
1702 if (led_g_mode != rt2x00_get_field32(reg, LED_CFG_G_LED_MODE) ||
1703 led_r_mode != rt2x00_get_field32(reg, LED_CFG_R_LED_MODE)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001704 rt2800_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001705 led_ctrl = rt2x00_get_field16(eeprom, EEPROM_FREQ_LED_MODE);
1706 if (led_ctrl == 0 || led_ctrl > 0x40) {
1707 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, led_g_mode);
1708 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, led_r_mode);
1709 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1710 } else {
1711 rt2800_mcu_request(rt2x00dev, MCU_BAND_SELECT, 0xff,
1712 (led_g_mode << 2) | led_r_mode, 1);
1713 }
1714 }
1715}
1716
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001717static void rt2800_set_ant_diversity(struct rt2x00_dev *rt2x00dev,
1718 enum antenna ant)
1719{
1720 u32 reg;
1721 u8 eesk_pin = (ant == ANTENNA_A) ? 1 : 0;
1722 u8 gpio_bit3 = (ant == ANTENNA_A) ? 0 : 1;
1723
1724 if (rt2x00_is_pci(rt2x00dev)) {
1725 rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
1726 rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK, eesk_pin);
1727 rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
1728 } else if (rt2x00_is_usb(rt2x00dev))
1729 rt2800_mcu_request(rt2x00dev, MCU_ANT_SELECT, 0xff,
1730 eesk_pin, 0);
1731
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02001732 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
1733 rt2x00_set_field32(&reg, GPIO_CTRL_DIR3, 0);
1734 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, gpio_bit3);
1735 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001736}
1737
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001738void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
1739{
1740 u8 r1;
1741 u8 r3;
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001742 u16 eeprom;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001743
1744 rt2800_bbp_read(rt2x00dev, 1, &r1);
1745 rt2800_bbp_read(rt2x00dev, 3, &r3);
1746
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001747 if (rt2x00_rt(rt2x00dev, RT3572) &&
1748 test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
1749 rt2800_config_3572bt_ant(rt2x00dev);
1750
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001751 /*
1752 * Configure the TX antenna.
1753 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001754 switch (ant->tx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001755 case 1:
1756 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001757 break;
1758 case 2:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001759 if (rt2x00_rt(rt2x00dev, RT3572) &&
1760 test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
1761 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 1);
1762 else
1763 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001764 break;
1765 case 3:
Gabor Juhos4788ac12013-07-08 16:08:21 +02001766 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001767 break;
1768 }
1769
1770 /*
1771 * Configure the RX antenna.
1772 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001773 switch (ant->rx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001774 case 1:
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001775 if (rt2x00_rt(rt2x00dev, RT3070) ||
1776 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03001777 rt2x00_rt(rt2x00dev, RT3352) ||
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001778 rt2x00_rt(rt2x00dev, RT3390)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001779 rt2800_eeprom_read(rt2x00dev,
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001780 EEPROM_NIC_CONF1, &eeprom);
1781 if (rt2x00_get_field16(eeprom,
1782 EEPROM_NIC_CONF1_ANT_DIVERSITY))
1783 rt2800_set_ant_diversity(rt2x00dev,
1784 rt2x00dev->default_ant.rx);
1785 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001786 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
1787 break;
1788 case 2:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001789 if (rt2x00_rt(rt2x00dev, RT3572) &&
1790 test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
1791 rt2x00_set_field8(&r3, BBP3_RX_ADC, 1);
1792 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA,
1793 rt2x00dev->curr_band == IEEE80211_BAND_5GHZ);
1794 rt2800_set_ant_diversity(rt2x00dev, ANTENNA_B);
1795 } else {
1796 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
1797 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001798 break;
1799 case 3:
1800 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
1801 break;
1802 }
1803
1804 rt2800_bbp_write(rt2x00dev, 3, r3);
1805 rt2800_bbp_write(rt2x00dev, 1, r1);
Gabor Juhos5cddb3c2013-07-08 16:08:22 +02001806
1807 if (rt2x00_rt(rt2x00dev, RT3593)) {
1808 if (ant->rx_chain_num == 1)
1809 rt2800_bbp_write(rt2x00dev, 86, 0x00);
1810 else
1811 rt2800_bbp_write(rt2x00dev, 86, 0x46);
1812 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001813}
1814EXPORT_SYMBOL_GPL(rt2800_config_ant);
1815
1816static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
1817 struct rt2x00lib_conf *libconf)
1818{
1819 u16 eeprom;
1820 short lna_gain;
1821
1822 if (libconf->rf.channel <= 14) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001823 rt2800_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001824 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
1825 } else if (libconf->rf.channel <= 64) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001826 rt2800_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001827 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
1828 } else if (libconf->rf.channel <= 128) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001829 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001830 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
1831 } else {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001832 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001833 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
1834 }
1835
1836 rt2x00dev->lna_gain = lna_gain;
1837}
1838
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001839static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
1840 struct ieee80211_conf *conf,
1841 struct rf_channel *rf,
1842 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001843{
1844 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
1845
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001846 if (rt2x00dev->default_ant.tx_chain_num == 1)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001847 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
1848
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001849 if (rt2x00dev->default_ant.rx_chain_num == 1) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001850 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
1851 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001852 } else if (rt2x00dev->default_ant.rx_chain_num == 2)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001853 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1854
1855 if (rf->channel > 14) {
1856 /*
1857 * When TX power is below 0, we should increase it by 7 to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001858 * make it a positive value (Minimum value is -7).
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001859 * However this means that values between 0 and 7 have
1860 * double meaning, and we should set a 7DBm boost flag.
1861 */
1862 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001863 (info->default_power1 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001864
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001865 if (info->default_power1 < 0)
1866 info->default_power1 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001867
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001868 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001869
1870 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001871 (info->default_power2 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001872
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001873 if (info->default_power2 < 0)
1874 info->default_power2 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001875
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001876 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001877 } else {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001878 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G, info->default_power1);
1879 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001880 }
1881
1882 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
1883
1884 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1885 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1886 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1887 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1888
1889 udelay(200);
1890
1891 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1892 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1893 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
1894 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1895
1896 udelay(200);
1897
1898 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1899 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1900 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1901 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1902}
1903
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001904static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
1905 struct ieee80211_conf *conf,
1906 struct rf_channel *rf,
1907 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001908{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01001909 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01001910 u8 rfcsr, calib_tx, calib_rx;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001911
1912 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Stanislaw Gruszka7f4666a2012-01-30 16:17:56 +01001913
1914 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
1915 rt2x00_set_field8(&rfcsr, RFCSR3_K, rf->rf3);
1916 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001917
1918 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +02001919 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001920 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1921
1922 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001923 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001924 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
1925
Helmut Schaa5a673962010-04-23 15:54:43 +02001926 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001927 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER, info->default_power2);
Helmut Schaa5a673962010-04-23 15:54:43 +02001928 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
1929
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01001930 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
1931 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
Gertjan van Wingerde7ad63032012-09-16 22:29:53 +02001932 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD,
1933 rt2x00dev->default_ant.rx_chain_num <= 1);
1934 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD,
1935 rt2x00dev->default_ant.rx_chain_num <= 2);
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01001936 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
Gertjan van Wingerde7ad63032012-09-16 22:29:53 +02001937 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD,
1938 rt2x00dev->default_ant.tx_chain_num <= 1);
1939 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD,
1940 rt2x00dev->default_ant.tx_chain_num <= 2);
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01001941 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
1942
Stanislaw Gruszka3e0c7642012-01-30 16:17:58 +01001943 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1944 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1945 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1946 msleep(1);
1947 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1948 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1949
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001950 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
1951 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
1952 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
1953
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01001954 if (rt2x00_rt(rt2x00dev, RT3390)) {
1955 calib_tx = conf_is_ht40(conf) ? 0x68 : 0x4f;
1956 calib_rx = conf_is_ht40(conf) ? 0x6f : 0x4f;
1957 } else {
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01001958 if (conf_is_ht40(conf)) {
1959 calib_tx = drv_data->calibration_bw40;
1960 calib_rx = drv_data->calibration_bw40;
1961 } else {
1962 calib_tx = drv_data->calibration_bw20;
1963 calib_rx = drv_data->calibration_bw20;
1964 }
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01001965 }
1966
1967 rt2800_rfcsr_read(rt2x00dev, 24, &rfcsr);
1968 rt2x00_set_field8(&rfcsr, RFCSR24_TX_CALIB, calib_tx);
1969 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr);
1970
1971 rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
1972 rt2x00_set_field8(&rfcsr, RFCSR31_RX_CALIB, calib_rx);
1973 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001974
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001975 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001976 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001977 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Stanislaw Gruszka3e0c7642012-01-30 16:17:58 +01001978
1979 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1980 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1981 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1982 msleep(1);
1983 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1984 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001985}
1986
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001987static void rt2800_config_channel_rf3052(struct rt2x00_dev *rt2x00dev,
1988 struct ieee80211_conf *conf,
1989 struct rf_channel *rf,
1990 struct channel_info *info)
1991{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01001992 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001993 u8 rfcsr;
1994 u32 reg;
1995
1996 if (rf->channel <= 14) {
Gertjan van Wingerde5d137df2012-02-06 23:45:09 +01001997 rt2800_bbp_write(rt2x00dev, 25, drv_data->bbp25);
1998 rt2800_bbp_write(rt2x00dev, 26, drv_data->bbp26);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001999 } else {
2000 rt2800_bbp_write(rt2x00dev, 25, 0x09);
2001 rt2800_bbp_write(rt2x00dev, 26, 0xff);
2002 }
2003
2004 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
2005 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
2006
2007 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
2008 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
2009 if (rf->channel <= 14)
2010 rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 2);
2011 else
2012 rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 1);
2013 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
2014
2015 rt2800_rfcsr_read(rt2x00dev, 5, &rfcsr);
2016 if (rf->channel <= 14)
2017 rt2x00_set_field8(&rfcsr, RFCSR5_R1, 1);
2018 else
2019 rt2x00_set_field8(&rfcsr, RFCSR5_R1, 2);
2020 rt2800_rfcsr_write(rt2x00dev, 5, rfcsr);
2021
2022 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
2023 if (rf->channel <= 14) {
2024 rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 3);
2025 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
Gertjan van Wingerde569ffa52012-02-06 23:45:10 +01002026 info->default_power1);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002027 } else {
2028 rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 7);
2029 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
2030 (info->default_power1 & 0x3) |
2031 ((info->default_power1 & 0xC) << 1));
2032 }
2033 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
2034
2035 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
2036 if (rf->channel <= 14) {
2037 rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 3);
2038 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
Gertjan van Wingerde569ffa52012-02-06 23:45:10 +01002039 info->default_power2);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002040 } else {
2041 rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 7);
2042 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
2043 (info->default_power2 & 0x3) |
2044 ((info->default_power2 & 0xC) << 1));
2045 }
2046 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
2047
2048 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002049 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2050 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2051 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
2052 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
Gertjan van Wingerde0cd461e2012-02-06 23:45:11 +01002053 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2054 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002055 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
2056 if (rf->channel <= 14) {
2057 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2058 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2059 }
2060 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
2061 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
2062 } else {
2063 switch (rt2x00dev->default_ant.tx_chain_num) {
2064 case 1:
2065 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2066 case 2:
2067 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
2068 break;
2069 }
2070
2071 switch (rt2x00dev->default_ant.rx_chain_num) {
2072 case 1:
2073 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2074 case 2:
2075 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
2076 break;
2077 }
2078 }
2079 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2080
2081 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
2082 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
2083 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
2084
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01002085 if (conf_is_ht40(conf)) {
2086 rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw40);
2087 rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw40);
2088 } else {
2089 rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw20);
2090 rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw20);
2091 }
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002092
2093 if (rf->channel <= 14) {
2094 rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
2095 rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
2096 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
2097 rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
2098 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01002099 rfcsr = 0x4c;
2100 rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
2101 drv_data->txmixer_gain_24g);
2102 rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002103 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
2104 rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
2105 rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
2106 rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
2107 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
2108 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
2109 rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
2110 } else {
Gertjan van Wingerde58b8ae12012-02-06 23:45:12 +01002111 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
2112 rt2x00_set_field8(&rfcsr, RFCSR7_BIT2, 1);
2113 rt2x00_set_field8(&rfcsr, RFCSR7_BIT3, 0);
2114 rt2x00_set_field8(&rfcsr, RFCSR7_BIT4, 1);
2115 rt2x00_set_field8(&rfcsr, RFCSR7_BITS67, 0);
2116 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002117 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
2118 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
2119 rt2800_rfcsr_write(rt2x00dev, 11, 0x00);
2120 rt2800_rfcsr_write(rt2x00dev, 15, 0x43);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01002121 rfcsr = 0x7a;
2122 rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
2123 drv_data->txmixer_gain_5g);
2124 rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002125 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
2126 if (rf->channel <= 64) {
2127 rt2800_rfcsr_write(rt2x00dev, 19, 0xb7);
2128 rt2800_rfcsr_write(rt2x00dev, 20, 0xf6);
2129 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
2130 } else if (rf->channel <= 128) {
2131 rt2800_rfcsr_write(rt2x00dev, 19, 0x74);
2132 rt2800_rfcsr_write(rt2x00dev, 20, 0xf4);
2133 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
2134 } else {
2135 rt2800_rfcsr_write(rt2x00dev, 19, 0x72);
2136 rt2800_rfcsr_write(rt2x00dev, 20, 0xf3);
2137 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
2138 }
2139 rt2800_rfcsr_write(rt2x00dev, 26, 0x87);
2140 rt2800_rfcsr_write(rt2x00dev, 27, 0x01);
2141 rt2800_rfcsr_write(rt2x00dev, 29, 0x9f);
2142 }
2143
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02002144 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
2145 rt2x00_set_field32(&reg, GPIO_CTRL_DIR7, 0);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002146 if (rf->channel <= 14)
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02002147 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 1);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002148 else
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02002149 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 0);
2150 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002151
2152 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
2153 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
2154 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
2155}
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002156
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002157#define POWER_BOUND 0x27
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002158#define POWER_BOUND_5G 0x2b
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002159#define FREQ_OFFSET_BOUND 0x5f
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002160
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002161static void rt2800_adjust_freq_offset(struct rt2x00_dev *rt2x00dev)
2162{
2163 u8 rfcsr;
2164
2165 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
2166 if (rt2x00dev->freq_offset > FREQ_OFFSET_BOUND)
2167 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, FREQ_OFFSET_BOUND);
2168 else
2169 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, rt2x00dev->freq_offset);
2170 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
2171}
2172
Woody Hunga89534e2012-06-13 15:01:16 +08002173static void rt2800_config_channel_rf3290(struct rt2x00_dev *rt2x00dev,
2174 struct ieee80211_conf *conf,
2175 struct rf_channel *rf,
2176 struct channel_info *info)
2177{
2178 u8 rfcsr;
2179
2180 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2181 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2182 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2183 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
2184 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2185
2186 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002187 if (info->default_power1 > POWER_BOUND)
2188 rt2x00_set_field8(&rfcsr, RFCSR49_TX, POWER_BOUND);
Woody Hunga89534e2012-06-13 15:01:16 +08002189 else
2190 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2191 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
2192
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002193 rt2800_adjust_freq_offset(rt2x00dev);
Woody Hunga89534e2012-06-13 15:01:16 +08002194
2195 if (rf->channel <= 14) {
2196 if (rf->channel == 6)
2197 rt2800_bbp_write(rt2x00dev, 68, 0x0c);
2198 else
2199 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
2200
2201 if (rf->channel >= 1 && rf->channel <= 6)
2202 rt2800_bbp_write(rt2x00dev, 59, 0x0f);
2203 else if (rf->channel >= 7 && rf->channel <= 11)
2204 rt2800_bbp_write(rt2x00dev, 59, 0x0e);
2205 else if (rf->channel >= 12 && rf->channel <= 14)
2206 rt2800_bbp_write(rt2x00dev, 59, 0x0d);
2207 }
2208}
2209
Daniel Golle03839952012-09-09 14:24:39 +03002210static void rt2800_config_channel_rf3322(struct rt2x00_dev *rt2x00dev,
2211 struct ieee80211_conf *conf,
2212 struct rf_channel *rf,
2213 struct channel_info *info)
2214{
2215 u8 rfcsr;
2216
2217 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2218 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2219
2220 rt2800_rfcsr_write(rt2x00dev, 11, 0x42);
2221 rt2800_rfcsr_write(rt2x00dev, 12, 0x1c);
2222 rt2800_rfcsr_write(rt2x00dev, 13, 0x00);
2223
2224 if (info->default_power1 > POWER_BOUND)
2225 rt2800_rfcsr_write(rt2x00dev, 47, POWER_BOUND);
2226 else
2227 rt2800_rfcsr_write(rt2x00dev, 47, info->default_power1);
2228
2229 if (info->default_power2 > POWER_BOUND)
2230 rt2800_rfcsr_write(rt2x00dev, 48, POWER_BOUND);
2231 else
2232 rt2800_rfcsr_write(rt2x00dev, 48, info->default_power2);
2233
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002234 rt2800_adjust_freq_offset(rt2x00dev);
Daniel Golle03839952012-09-09 14:24:39 +03002235
2236 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2237 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2238 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2239
2240 if ( rt2x00dev->default_ant.tx_chain_num == 2 )
2241 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2242 else
2243 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
2244
2245 if ( rt2x00dev->default_ant.rx_chain_num == 2 )
2246 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2247 else
2248 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
2249
2250 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2251 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
2252
2253 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2254
2255 rt2800_rfcsr_write(rt2x00dev, 31, 80);
2256}
2257
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002258static void rt2800_config_channel_rf53xx(struct rt2x00_dev *rt2x00dev,
Gabor Juhosadde5882011-03-03 11:46:45 +01002259 struct ieee80211_conf *conf,
2260 struct rf_channel *rf,
2261 struct channel_info *info)
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002262{
Gabor Juhosadde5882011-03-03 11:46:45 +01002263 u8 rfcsr;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002264
Gabor Juhosadde5882011-03-03 11:46:45 +01002265 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2266 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2267 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2268 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
2269 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002270
Gabor Juhosadde5882011-03-03 11:46:45 +01002271 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002272 if (info->default_power1 > POWER_BOUND)
2273 rt2x00_set_field8(&rfcsr, RFCSR49_TX, POWER_BOUND);
Gabor Juhosadde5882011-03-03 11:46:45 +01002274 else
2275 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2276 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002277
Zero.Lincff3d1f2012-05-29 16:11:09 +08002278 if (rt2x00_rt(rt2x00dev, RT5392)) {
2279 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002280 if (info->default_power1 > POWER_BOUND)
2281 rt2x00_set_field8(&rfcsr, RFCSR50_TX, POWER_BOUND);
Zero.Lincff3d1f2012-05-29 16:11:09 +08002282 else
2283 rt2x00_set_field8(&rfcsr, RFCSR50_TX,
2284 info->default_power2);
2285 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
2286 }
2287
Gabor Juhosadde5882011-03-03 11:46:45 +01002288 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
Zero.Lincff3d1f2012-05-29 16:11:09 +08002289 if (rt2x00_rt(rt2x00dev, RT5392)) {
2290 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2291 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2292 }
Gabor Juhosadde5882011-03-03 11:46:45 +01002293 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2294 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
2295 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2296 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2297 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002298
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002299 rt2800_adjust_freq_offset(rt2x00dev);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002300
Gabor Juhosadde5882011-03-03 11:46:45 +01002301 if (rf->channel <= 14) {
2302 int idx = rf->channel-1;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002303
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02002304 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002305 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
2306 /* r55/r59 value array of channel 1~14 */
2307 static const char r55_bt_rev[] = {0x83, 0x83,
2308 0x83, 0x73, 0x73, 0x63, 0x53, 0x53,
2309 0x53, 0x43, 0x43, 0x43, 0x43, 0x43};
2310 static const char r59_bt_rev[] = {0x0e, 0x0e,
2311 0x0e, 0x0e, 0x0e, 0x0b, 0x0a, 0x09,
2312 0x07, 0x07, 0x07, 0x07, 0x07, 0x07};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002313
Gabor Juhosadde5882011-03-03 11:46:45 +01002314 rt2800_rfcsr_write(rt2x00dev, 55,
2315 r55_bt_rev[idx]);
2316 rt2800_rfcsr_write(rt2x00dev, 59,
2317 r59_bt_rev[idx]);
2318 } else {
2319 static const char r59_bt[] = {0x8b, 0x8b, 0x8b,
2320 0x8b, 0x8b, 0x8b, 0x8b, 0x8a, 0x89,
2321 0x88, 0x88, 0x86, 0x85, 0x84};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002322
Gabor Juhosadde5882011-03-03 11:46:45 +01002323 rt2800_rfcsr_write(rt2x00dev, 59, r59_bt[idx]);
2324 }
2325 } else {
2326 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
2327 static const char r55_nonbt_rev[] = {0x23, 0x23,
2328 0x23, 0x23, 0x13, 0x13, 0x03, 0x03,
2329 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
2330 static const char r59_nonbt_rev[] = {0x07, 0x07,
2331 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
2332 0x07, 0x07, 0x06, 0x05, 0x04, 0x04};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002333
Gabor Juhosadde5882011-03-03 11:46:45 +01002334 rt2800_rfcsr_write(rt2x00dev, 55,
2335 r55_nonbt_rev[idx]);
2336 rt2800_rfcsr_write(rt2x00dev, 59,
2337 r59_nonbt_rev[idx]);
John Li2ed71882012-02-17 17:33:06 +08002338 } else if (rt2x00_rt(rt2x00dev, RT5390) ||
Gabor Juhose6d227b2012-12-02 15:53:28 +01002339 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002340 static const char r59_non_bt[] = {0x8f, 0x8f,
2341 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8d,
2342 0x8a, 0x88, 0x88, 0x87, 0x87, 0x86};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002343
Gabor Juhosadde5882011-03-03 11:46:45 +01002344 rt2800_rfcsr_write(rt2x00dev, 59,
2345 r59_non_bt[idx]);
2346 }
2347 }
2348 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002349}
2350
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002351static void rt2800_config_channel_rf55xx(struct rt2x00_dev *rt2x00dev,
2352 struct ieee80211_conf *conf,
2353 struct rf_channel *rf,
2354 struct channel_info *info)
2355{
2356 u8 rfcsr, ep_reg;
Stanislaw Gruszkad5ae7a62013-03-16 19:19:42 +01002357 u32 reg;
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002358 int power_bound;
2359
2360 /* TODO */
2361 const bool is_11b = false;
2362 const bool is_type_ep = false;
2363
Stanislaw Gruszkad5ae7a62013-03-16 19:19:42 +01002364 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
2365 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL,
2366 (rf->channel > 14 || conf_is_ht40(conf)) ? 5 : 0);
2367 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002368
2369 /* Order of values on rf_channel entry: N, K, mod, R */
2370 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1 & 0xff);
2371
2372 rt2800_rfcsr_read(rt2x00dev, 9, &rfcsr);
2373 rt2x00_set_field8(&rfcsr, RFCSR9_K, rf->rf2 & 0xf);
2374 rt2x00_set_field8(&rfcsr, RFCSR9_N, (rf->rf1 & 0x100) >> 8);
2375 rt2x00_set_field8(&rfcsr, RFCSR9_MOD, ((rf->rf3 - 8) & 0x4) >> 2);
2376 rt2800_rfcsr_write(rt2x00dev, 9, rfcsr);
2377
2378 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2379 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf4 - 1);
2380 rt2x00_set_field8(&rfcsr, RFCSR11_MOD, (rf->rf3 - 8) & 0x3);
2381 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2382
2383 if (rf->channel <= 14) {
2384 rt2800_rfcsr_write(rt2x00dev, 10, 0x90);
2385 /* FIXME: RF11 owerwrite ? */
2386 rt2800_rfcsr_write(rt2x00dev, 11, 0x4A);
2387 rt2800_rfcsr_write(rt2x00dev, 12, 0x52);
2388 rt2800_rfcsr_write(rt2x00dev, 13, 0x42);
2389 rt2800_rfcsr_write(rt2x00dev, 22, 0x40);
2390 rt2800_rfcsr_write(rt2x00dev, 24, 0x4A);
2391 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
2392 rt2800_rfcsr_write(rt2x00dev, 27, 0x42);
2393 rt2800_rfcsr_write(rt2x00dev, 36, 0x80);
2394 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
2395 rt2800_rfcsr_write(rt2x00dev, 38, 0x89);
2396 rt2800_rfcsr_write(rt2x00dev, 39, 0x1B);
2397 rt2800_rfcsr_write(rt2x00dev, 40, 0x0D);
2398 rt2800_rfcsr_write(rt2x00dev, 41, 0x9B);
2399 rt2800_rfcsr_write(rt2x00dev, 42, 0xD5);
2400 rt2800_rfcsr_write(rt2x00dev, 43, 0x72);
2401 rt2800_rfcsr_write(rt2x00dev, 44, 0x0E);
2402 rt2800_rfcsr_write(rt2x00dev, 45, 0xA2);
2403 rt2800_rfcsr_write(rt2x00dev, 46, 0x6B);
2404 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
2405 rt2800_rfcsr_write(rt2x00dev, 51, 0x3E);
2406 rt2800_rfcsr_write(rt2x00dev, 52, 0x48);
2407 rt2800_rfcsr_write(rt2x00dev, 54, 0x38);
2408 rt2800_rfcsr_write(rt2x00dev, 56, 0xA1);
2409 rt2800_rfcsr_write(rt2x00dev, 57, 0x00);
2410 rt2800_rfcsr_write(rt2x00dev, 58, 0x39);
2411 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
2412 rt2800_rfcsr_write(rt2x00dev, 61, 0x91);
2413 rt2800_rfcsr_write(rt2x00dev, 62, 0x39);
2414
2415 /* TODO RF27 <- tssi */
2416
2417 rfcsr = rf->channel <= 10 ? 0x07 : 0x06;
2418 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
2419 rt2800_rfcsr_write(rt2x00dev, 59, rfcsr);
2420
2421 if (is_11b) {
2422 /* CCK */
2423 rt2800_rfcsr_write(rt2x00dev, 31, 0xF8);
2424 rt2800_rfcsr_write(rt2x00dev, 32, 0xC0);
2425 if (is_type_ep)
2426 rt2800_rfcsr_write(rt2x00dev, 55, 0x06);
2427 else
2428 rt2800_rfcsr_write(rt2x00dev, 55, 0x47);
2429 } else {
2430 /* OFDM */
2431 if (is_type_ep)
2432 rt2800_rfcsr_write(rt2x00dev, 55, 0x03);
2433 else
2434 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
2435 }
2436
2437 power_bound = POWER_BOUND;
2438 ep_reg = 0x2;
2439 } else {
2440 rt2800_rfcsr_write(rt2x00dev, 10, 0x97);
2441 /* FIMXE: RF11 overwrite */
2442 rt2800_rfcsr_write(rt2x00dev, 11, 0x40);
2443 rt2800_rfcsr_write(rt2x00dev, 25, 0xBF);
2444 rt2800_rfcsr_write(rt2x00dev, 27, 0x42);
2445 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
2446 rt2800_rfcsr_write(rt2x00dev, 37, 0x04);
2447 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
2448 rt2800_rfcsr_write(rt2x00dev, 40, 0x42);
2449 rt2800_rfcsr_write(rt2x00dev, 41, 0xBB);
2450 rt2800_rfcsr_write(rt2x00dev, 42, 0xD7);
2451 rt2800_rfcsr_write(rt2x00dev, 45, 0x41);
2452 rt2800_rfcsr_write(rt2x00dev, 48, 0x00);
2453 rt2800_rfcsr_write(rt2x00dev, 57, 0x77);
2454 rt2800_rfcsr_write(rt2x00dev, 60, 0x05);
2455 rt2800_rfcsr_write(rt2x00dev, 61, 0x01);
2456
2457 /* TODO RF27 <- tssi */
2458
2459 if (rf->channel >= 36 && rf->channel <= 64) {
2460
2461 rt2800_rfcsr_write(rt2x00dev, 12, 0x2E);
2462 rt2800_rfcsr_write(rt2x00dev, 13, 0x22);
2463 rt2800_rfcsr_write(rt2x00dev, 22, 0x60);
2464 rt2800_rfcsr_write(rt2x00dev, 23, 0x7F);
2465 if (rf->channel <= 50)
2466 rt2800_rfcsr_write(rt2x00dev, 24, 0x09);
2467 else if (rf->channel >= 52)
2468 rt2800_rfcsr_write(rt2x00dev, 24, 0x07);
2469 rt2800_rfcsr_write(rt2x00dev, 39, 0x1C);
2470 rt2800_rfcsr_write(rt2x00dev, 43, 0x5B);
2471 rt2800_rfcsr_write(rt2x00dev, 44, 0X40);
2472 rt2800_rfcsr_write(rt2x00dev, 46, 0X00);
2473 rt2800_rfcsr_write(rt2x00dev, 51, 0xFE);
2474 rt2800_rfcsr_write(rt2x00dev, 52, 0x0C);
2475 rt2800_rfcsr_write(rt2x00dev, 54, 0xF8);
2476 if (rf->channel <= 50) {
2477 rt2800_rfcsr_write(rt2x00dev, 55, 0x06),
2478 rt2800_rfcsr_write(rt2x00dev, 56, 0xD3);
2479 } else if (rf->channel >= 52) {
2480 rt2800_rfcsr_write(rt2x00dev, 55, 0x04);
2481 rt2800_rfcsr_write(rt2x00dev, 56, 0xBB);
2482 }
2483
2484 rt2800_rfcsr_write(rt2x00dev, 58, 0x15);
2485 rt2800_rfcsr_write(rt2x00dev, 59, 0x7F);
2486 rt2800_rfcsr_write(rt2x00dev, 62, 0x15);
2487
2488 } else if (rf->channel >= 100 && rf->channel <= 165) {
2489
2490 rt2800_rfcsr_write(rt2x00dev, 12, 0x0E);
2491 rt2800_rfcsr_write(rt2x00dev, 13, 0x42);
2492 rt2800_rfcsr_write(rt2x00dev, 22, 0x40);
2493 if (rf->channel <= 153) {
2494 rt2800_rfcsr_write(rt2x00dev, 23, 0x3C);
2495 rt2800_rfcsr_write(rt2x00dev, 24, 0x06);
2496 } else if (rf->channel >= 155) {
2497 rt2800_rfcsr_write(rt2x00dev, 23, 0x38);
2498 rt2800_rfcsr_write(rt2x00dev, 24, 0x05);
2499 }
2500 if (rf->channel <= 138) {
2501 rt2800_rfcsr_write(rt2x00dev, 39, 0x1A);
2502 rt2800_rfcsr_write(rt2x00dev, 43, 0x3B);
2503 rt2800_rfcsr_write(rt2x00dev, 44, 0x20);
2504 rt2800_rfcsr_write(rt2x00dev, 46, 0x18);
2505 } else if (rf->channel >= 140) {
2506 rt2800_rfcsr_write(rt2x00dev, 39, 0x18);
2507 rt2800_rfcsr_write(rt2x00dev, 43, 0x1B);
2508 rt2800_rfcsr_write(rt2x00dev, 44, 0x10);
2509 rt2800_rfcsr_write(rt2x00dev, 46, 0X08);
2510 }
2511 if (rf->channel <= 124)
2512 rt2800_rfcsr_write(rt2x00dev, 51, 0xFC);
2513 else if (rf->channel >= 126)
2514 rt2800_rfcsr_write(rt2x00dev, 51, 0xEC);
2515 if (rf->channel <= 138)
2516 rt2800_rfcsr_write(rt2x00dev, 52, 0x06);
2517 else if (rf->channel >= 140)
2518 rt2800_rfcsr_write(rt2x00dev, 52, 0x06);
2519 rt2800_rfcsr_write(rt2x00dev, 54, 0xEB);
2520 if (rf->channel <= 138)
2521 rt2800_rfcsr_write(rt2x00dev, 55, 0x01);
2522 else if (rf->channel >= 140)
2523 rt2800_rfcsr_write(rt2x00dev, 55, 0x00);
2524 if (rf->channel <= 128)
2525 rt2800_rfcsr_write(rt2x00dev, 56, 0xBB);
2526 else if (rf->channel >= 130)
2527 rt2800_rfcsr_write(rt2x00dev, 56, 0xAB);
2528 if (rf->channel <= 116)
2529 rt2800_rfcsr_write(rt2x00dev, 58, 0x1D);
2530 else if (rf->channel >= 118)
2531 rt2800_rfcsr_write(rt2x00dev, 58, 0x15);
2532 if (rf->channel <= 138)
2533 rt2800_rfcsr_write(rt2x00dev, 59, 0x3F);
2534 else if (rf->channel >= 140)
2535 rt2800_rfcsr_write(rt2x00dev, 59, 0x7C);
2536 if (rf->channel <= 116)
2537 rt2800_rfcsr_write(rt2x00dev, 62, 0x1D);
2538 else if (rf->channel >= 118)
2539 rt2800_rfcsr_write(rt2x00dev, 62, 0x15);
2540 }
2541
2542 power_bound = POWER_BOUND_5G;
2543 ep_reg = 0x3;
2544 }
2545
2546 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
2547 if (info->default_power1 > power_bound)
2548 rt2x00_set_field8(&rfcsr, RFCSR49_TX, power_bound);
2549 else
2550 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2551 if (is_type_ep)
2552 rt2x00_set_field8(&rfcsr, RFCSR49_EP, ep_reg);
2553 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
2554
2555 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
Gabor Juhos0847beb2013-06-25 22:57:29 +02002556 if (info->default_power2 > power_bound)
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002557 rt2x00_set_field8(&rfcsr, RFCSR50_TX, power_bound);
2558 else
2559 rt2x00_set_field8(&rfcsr, RFCSR50_TX, info->default_power2);
2560 if (is_type_ep)
2561 rt2x00_set_field8(&rfcsr, RFCSR50_EP, ep_reg);
2562 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
2563
2564 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2565 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2566 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
2567
2568 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD,
2569 rt2x00dev->default_ant.tx_chain_num >= 1);
2570 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD,
2571 rt2x00dev->default_ant.tx_chain_num == 2);
2572 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
2573
2574 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD,
2575 rt2x00dev->default_ant.rx_chain_num >= 1);
2576 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD,
2577 rt2x00dev->default_ant.rx_chain_num == 2);
2578 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2579
2580 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2581 rt2800_rfcsr_write(rt2x00dev, 6, 0xe4);
2582
2583 if (conf_is_ht40(conf))
2584 rt2800_rfcsr_write(rt2x00dev, 30, 0x16);
2585 else
2586 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
2587
2588 if (!is_11b) {
2589 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
2590 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
2591 }
2592
2593 /* TODO proper frequency adjustment */
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002594 rt2800_adjust_freq_offset(rt2x00dev);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002595
2596 /* TODO merge with others */
2597 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
2598 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
2599 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
Stanislaw Gruszka68031412013-03-16 19:19:44 +01002600
2601 /* BBP settings */
2602 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
2603 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
2604 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
2605
2606 rt2800_bbp_write(rt2x00dev, 79, (rf->channel <= 14) ? 0x1C : 0x18);
2607 rt2800_bbp_write(rt2x00dev, 80, (rf->channel <= 14) ? 0x0E : 0x08);
2608 rt2800_bbp_write(rt2x00dev, 81, (rf->channel <= 14) ? 0x3A : 0x38);
2609 rt2800_bbp_write(rt2x00dev, 82, (rf->channel <= 14) ? 0x62 : 0x92);
2610
2611 /* GLRT band configuration */
2612 rt2800_bbp_write(rt2x00dev, 195, 128);
2613 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0xE0 : 0xF0);
2614 rt2800_bbp_write(rt2x00dev, 195, 129);
2615 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x1F : 0x1E);
2616 rt2800_bbp_write(rt2x00dev, 195, 130);
2617 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x38 : 0x28);
2618 rt2800_bbp_write(rt2x00dev, 195, 131);
2619 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x32 : 0x20);
2620 rt2800_bbp_write(rt2x00dev, 195, 133);
2621 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x28 : 0x7F);
2622 rt2800_bbp_write(rt2x00dev, 195, 124);
2623 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x19 : 0x7F);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002624}
2625
Stanislaw Gruszka5bc2dd02013-03-16 19:19:47 +01002626static void rt2800_bbp_write_with_rx_chain(struct rt2x00_dev *rt2x00dev,
2627 const unsigned int word,
2628 const u8 value)
2629{
2630 u8 chain, reg;
2631
2632 for (chain = 0; chain < rt2x00dev->default_ant.rx_chain_num; chain++) {
2633 rt2800_bbp_read(rt2x00dev, 27, &reg);
2634 rt2x00_set_field8(&reg, BBP27_RX_CHAIN_SEL, chain);
2635 rt2800_bbp_write(rt2x00dev, 27, reg);
2636
2637 rt2800_bbp_write(rt2x00dev, word, value);
2638 }
2639}
2640
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002641static void rt2800_iq_calibrate(struct rt2x00_dev *rt2x00dev, int channel)
2642{
2643 u8 cal;
2644
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01002645 /* TX0 IQ Gain */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002646 rt2800_bbp_write(rt2x00dev, 158, 0x2c);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01002647 if (channel <= 14)
2648 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_GAIN_CAL_TX0_2G);
2649 else if (channel >= 36 && channel <= 64)
2650 cal = rt2x00_eeprom_byte(rt2x00dev,
2651 EEPROM_IQ_GAIN_CAL_TX0_CH36_TO_CH64_5G);
2652 else if (channel >= 100 && channel <= 138)
2653 cal = rt2x00_eeprom_byte(rt2x00dev,
2654 EEPROM_IQ_GAIN_CAL_TX0_CH100_TO_CH138_5G);
2655 else if (channel >= 140 && channel <= 165)
2656 cal = rt2x00_eeprom_byte(rt2x00dev,
2657 EEPROM_IQ_GAIN_CAL_TX0_CH140_TO_CH165_5G);
2658 else
2659 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002660 rt2800_bbp_write(rt2x00dev, 159, cal);
2661
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01002662 /* TX0 IQ Phase */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002663 rt2800_bbp_write(rt2x00dev, 158, 0x2d);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01002664 if (channel <= 14)
2665 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_PHASE_CAL_TX0_2G);
2666 else if (channel >= 36 && channel <= 64)
2667 cal = rt2x00_eeprom_byte(rt2x00dev,
2668 EEPROM_IQ_PHASE_CAL_TX0_CH36_TO_CH64_5G);
2669 else if (channel >= 100 && channel <= 138)
2670 cal = rt2x00_eeprom_byte(rt2x00dev,
2671 EEPROM_IQ_PHASE_CAL_TX0_CH100_TO_CH138_5G);
2672 else if (channel >= 140 && channel <= 165)
2673 cal = rt2x00_eeprom_byte(rt2x00dev,
2674 EEPROM_IQ_PHASE_CAL_TX0_CH140_TO_CH165_5G);
2675 else
2676 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002677 rt2800_bbp_write(rt2x00dev, 159, cal);
2678
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01002679 /* TX1 IQ Gain */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002680 rt2800_bbp_write(rt2x00dev, 158, 0x4a);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01002681 if (channel <= 14)
2682 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_GAIN_CAL_TX1_2G);
2683 else if (channel >= 36 && channel <= 64)
2684 cal = rt2x00_eeprom_byte(rt2x00dev,
2685 EEPROM_IQ_GAIN_CAL_TX1_CH36_TO_CH64_5G);
2686 else if (channel >= 100 && channel <= 138)
2687 cal = rt2x00_eeprom_byte(rt2x00dev,
2688 EEPROM_IQ_GAIN_CAL_TX1_CH100_TO_CH138_5G);
2689 else if (channel >= 140 && channel <= 165)
2690 cal = rt2x00_eeprom_byte(rt2x00dev,
2691 EEPROM_IQ_GAIN_CAL_TX1_CH140_TO_CH165_5G);
2692 else
2693 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002694 rt2800_bbp_write(rt2x00dev, 159, cal);
2695
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01002696 /* TX1 IQ Phase */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002697 rt2800_bbp_write(rt2x00dev, 158, 0x4b);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01002698 if (channel <= 14)
2699 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_PHASE_CAL_TX1_2G);
2700 else if (channel >= 36 && channel <= 64)
2701 cal = rt2x00_eeprom_byte(rt2x00dev,
2702 EEPROM_IQ_PHASE_CAL_TX1_CH36_TO_CH64_5G);
2703 else if (channel >= 100 && channel <= 138)
2704 cal = rt2x00_eeprom_byte(rt2x00dev,
2705 EEPROM_IQ_PHASE_CAL_TX1_CH100_TO_CH138_5G);
2706 else if (channel >= 140 && channel <= 165)
2707 cal = rt2x00_eeprom_byte(rt2x00dev,
2708 EEPROM_IQ_PHASE_CAL_TX1_CH140_TO_CH165_5G);
2709 else
2710 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002711 rt2800_bbp_write(rt2x00dev, 159, cal);
2712
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01002713 /* FIXME: possible RX0, RX1 callibration ? */
2714
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002715 /* RF IQ compensation control */
2716 rt2800_bbp_write(rt2x00dev, 158, 0x04);
2717 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_RF_IQ_COMPENSATION_CONTROL);
2718 rt2800_bbp_write(rt2x00dev, 159, cal != 0xff ? cal : 0);
2719
2720 /* RF IQ imbalance compensation control */
2721 rt2800_bbp_write(rt2x00dev, 158, 0x03);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01002722 cal = rt2x00_eeprom_byte(rt2x00dev,
2723 EEPROM_RF_IQ_IMBALANCE_COMPENSATION_CONTROL);
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002724 rt2800_bbp_write(rt2x00dev, 159, cal != 0xff ? cal : 0);
2725}
2726
Gabor Juhos97aa03f2013-07-08 16:08:23 +02002727static char rt2800_txpower_to_dev(struct rt2x00_dev *rt2x00dev,
2728 unsigned int channel,
2729 char txpower)
2730{
Gabor Juhosfc739cf2013-07-08 16:08:24 +02002731 if (rt2x00_rt(rt2x00dev, RT3593))
2732 txpower = rt2x00_get_field8(txpower, EEPROM_TXPOWER_ALC);
2733
Gabor Juhos97aa03f2013-07-08 16:08:23 +02002734 if (channel <= 14)
2735 return clamp_t(char, txpower, MIN_G_TXPOWER, MAX_G_TXPOWER);
Gabor Juhosfc739cf2013-07-08 16:08:24 +02002736
2737 if (rt2x00_rt(rt2x00dev, RT3593))
2738 return clamp_t(char, txpower, MIN_A_TXPOWER_3593,
2739 MAX_A_TXPOWER_3593);
Gabor Juhos97aa03f2013-07-08 16:08:23 +02002740 else
2741 return clamp_t(char, txpower, MIN_A_TXPOWER, MAX_A_TXPOWER);
2742}
2743
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002744static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
2745 struct ieee80211_conf *conf,
2746 struct rf_channel *rf,
2747 struct channel_info *info)
2748{
2749 u32 reg;
2750 unsigned int tx_pin;
Woody Hunga89534e2012-06-13 15:01:16 +08002751 u8 bbp, rfcsr;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002752
Gabor Juhos97aa03f2013-07-08 16:08:23 +02002753 info->default_power1 = rt2800_txpower_to_dev(rt2x00dev, rf->channel,
2754 info->default_power1);
2755 info->default_power2 = rt2800_txpower_to_dev(rt2x00dev, rf->channel,
2756 info->default_power2);
Ivo van Doorn46323e12010-08-23 19:55:43 +02002757
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002758 switch (rt2x00dev->chip.rf) {
2759 case RF2020:
2760 case RF3020:
2761 case RF3021:
2762 case RF3022:
2763 case RF3320:
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02002764 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002765 break;
2766 case RF3052:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002767 rt2800_config_channel_rf3052(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002768 break;
Woody Hunga89534e2012-06-13 15:01:16 +08002769 case RF3290:
2770 rt2800_config_channel_rf3290(rt2x00dev, conf, rf, info);
2771 break;
Daniel Golle03839952012-09-09 14:24:39 +03002772 case RF3322:
2773 rt2800_config_channel_rf3322(rt2x00dev, conf, rf, info);
2774 break;
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02002775 case RF5360:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002776 case RF5370:
John Li2ed71882012-02-17 17:33:06 +08002777 case RF5372:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002778 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08002779 case RF5392:
Gabor Juhosadde5882011-03-03 11:46:45 +01002780 rt2800_config_channel_rf53xx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002781 break;
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002782 case RF5592:
2783 rt2800_config_channel_rf55xx(rt2x00dev, conf, rf, info);
2784 break;
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002785 default:
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02002786 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01002787 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002788
Woody Hunga89534e2012-06-13 15:01:16 +08002789 if (rt2x00_rf(rt2x00dev, RF3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03002790 rt2x00_rf(rt2x00dev, RF3322) ||
Woody Hunga89534e2012-06-13 15:01:16 +08002791 rt2x00_rf(rt2x00dev, RF5360) ||
2792 rt2x00_rf(rt2x00dev, RF5370) ||
2793 rt2x00_rf(rt2x00dev, RF5372) ||
2794 rt2x00_rf(rt2x00dev, RF5390) ||
2795 rt2x00_rf(rt2x00dev, RF5392)) {
2796 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2797 rt2x00_set_field8(&rfcsr, RFCSR30_TX_H20M, 0);
2798 rt2x00_set_field8(&rfcsr, RFCSR30_RX_H20M, 0);
2799 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2800
2801 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
Gabor Juhosd6d82022012-12-02 18:34:47 +01002802 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
Woody Hunga89534e2012-06-13 15:01:16 +08002803 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
2804 }
2805
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002806 /*
2807 * Change BBP settings
2808 */
Daniel Golle03839952012-09-09 14:24:39 +03002809 if (rt2x00_rt(rt2x00dev, RT3352)) {
2810 rt2800_bbp_write(rt2x00dev, 27, 0x0);
Daniel Gollecf193f62012-10-04 01:20:41 +02002811 rt2800_bbp_write(rt2x00dev, 66, 0x26 + rt2x00dev->lna_gain);
Daniel Golle03839952012-09-09 14:24:39 +03002812 rt2800_bbp_write(rt2x00dev, 27, 0x20);
Daniel Gollecf193f62012-10-04 01:20:41 +02002813 rt2800_bbp_write(rt2x00dev, 66, 0x26 + rt2x00dev->lna_gain);
Daniel Golle03839952012-09-09 14:24:39 +03002814 } else {
2815 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
2816 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
2817 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
2818 rt2800_bbp_write(rt2x00dev, 86, 0);
2819 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002820
2821 if (rf->channel <= 14) {
John Li2ed71882012-02-17 17:33:06 +08002822 if (!rt2x00_rt(rt2x00dev, RT5390) &&
Gabor Juhose6d227b2012-12-02 15:53:28 +01002823 !rt2x00_rt(rt2x00dev, RT5392)) {
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02002824 if (test_bit(CAPABILITY_EXTERNAL_LNA_BG,
2825 &rt2x00dev->cap_flags)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002826 rt2800_bbp_write(rt2x00dev, 82, 0x62);
2827 rt2800_bbp_write(rt2x00dev, 75, 0x46);
2828 } else {
2829 rt2800_bbp_write(rt2x00dev, 82, 0x84);
2830 rt2800_bbp_write(rt2x00dev, 75, 0x50);
2831 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002832 }
2833 } else {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002834 if (rt2x00_rt(rt2x00dev, RT3572))
2835 rt2800_bbp_write(rt2x00dev, 82, 0x94);
2836 else
2837 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002838
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02002839 if (test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002840 rt2800_bbp_write(rt2x00dev, 75, 0x46);
2841 else
2842 rt2800_bbp_write(rt2x00dev, 75, 0x50);
2843 }
2844
2845 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02002846 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002847 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
2848 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
2849 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
2850
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002851 if (rt2x00_rt(rt2x00dev, RT3572))
2852 rt2800_rfcsr_write(rt2x00dev, 8, 0);
2853
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002854 tx_pin = 0;
2855
Gabor Juhosbb16d482013-06-24 23:03:24 +02002856 switch (rt2x00dev->default_ant.tx_chain_num) {
2857 case 3:
2858 /* Turn on tertiary PAs */
2859 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A2_EN,
2860 rf->channel > 14);
2861 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G2_EN,
2862 rf->channel <= 14);
2863 /* fall-through */
2864 case 2:
2865 /* Turn on secondary PAs */
Gertjan van Wingerde65f31b52011-05-18 20:25:05 +02002866 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN,
2867 rf->channel > 14);
2868 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN,
2869 rf->channel <= 14);
Gabor Juhosbb16d482013-06-24 23:03:24 +02002870 /* fall-through */
2871 case 1:
2872 /* Turn on primary PAs */
2873 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN,
2874 rf->channel > 14);
2875 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
2876 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
2877 else
2878 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN,
2879 rf->channel <= 14);
2880 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002881 }
2882
Gabor Juhosbb16d482013-06-24 23:03:24 +02002883 switch (rt2x00dev->default_ant.rx_chain_num) {
2884 case 3:
2885 /* Turn on tertiary LNAs */
2886 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A2_EN, 1);
2887 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G2_EN, 1);
2888 /* fall-through */
2889 case 2:
2890 /* Turn on secondary LNAs */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002891 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
2892 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
Gabor Juhosbb16d482013-06-24 23:03:24 +02002893 /* fall-through */
2894 case 1:
2895 /* Turn on primary LNAs */
2896 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
2897 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
2898 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002899 }
2900
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002901 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
2902 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002903
2904 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
2905
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002906 if (rt2x00_rt(rt2x00dev, RT3572))
2907 rt2800_rfcsr_write(rt2x00dev, 8, 0x80);
2908
Stanislaw Gruszka68031412013-03-16 19:19:44 +01002909 if (rt2x00_rt(rt2x00dev, RT5592)) {
2910 rt2800_bbp_write(rt2x00dev, 195, 141);
2911 rt2800_bbp_write(rt2x00dev, 196, conf_is_ht40(conf) ? 0x10 : 0x1a);
2912
Stanislaw Gruszka8ba0ebf2013-03-16 19:19:48 +01002913 /* AGC init */
2914 reg = (rf->channel <= 14 ? 0x1c : 0x24) + 2 * rt2x00dev->lna_gain;
2915 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66, reg);
2916
Stanislaw Gruszka87561302013-03-16 19:19:45 +01002917 rt2800_iq_calibrate(rt2x00dev, rf->channel);
Stanislaw Gruszka68031412013-03-16 19:19:44 +01002918 }
2919
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002920 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2921 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
2922 rt2800_bbp_write(rt2x00dev, 4, bbp);
2923
2924 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02002925 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002926 rt2800_bbp_write(rt2x00dev, 3, bbp);
2927
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02002928 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002929 if (conf_is_ht40(conf)) {
2930 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
2931 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
2932 rt2800_bbp_write(rt2x00dev, 73, 0x16);
2933 } else {
2934 rt2800_bbp_write(rt2x00dev, 69, 0x16);
2935 rt2800_bbp_write(rt2x00dev, 70, 0x08);
2936 rt2800_bbp_write(rt2x00dev, 73, 0x11);
2937 }
2938 }
2939
2940 msleep(1);
Helmut Schaa977206d2010-12-13 12:31:58 +01002941
2942 /*
2943 * Clear channel statistic counters
2944 */
2945 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &reg);
2946 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &reg);
2947 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &reg);
Daniel Golle03839952012-09-09 14:24:39 +03002948
2949 /*
2950 * Clear update flag
2951 */
2952 if (rt2x00_rt(rt2x00dev, RT3352)) {
2953 rt2800_bbp_read(rt2x00dev, 49, &bbp);
2954 rt2x00_set_field8(&bbp, BBP49_UPDATE_FLAG, 0);
2955 rt2800_bbp_write(rt2x00dev, 49, bbp);
2956 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002957}
2958
Helmut Schaa9e33a352011-03-28 13:33:40 +02002959static int rt2800_get_gain_calibration_delta(struct rt2x00_dev *rt2x00dev)
2960{
2961 u8 tssi_bounds[9];
2962 u8 current_tssi;
2963 u16 eeprom;
2964 u8 step;
2965 int i;
2966
2967 /*
2968 * Read TSSI boundaries for temperature compensation from
2969 * the EEPROM.
2970 *
2971 * Array idx 0 1 2 3 4 5 6 7 8
2972 * Matching Delta value -4 -3 -2 -1 0 +1 +2 +3 +4
2973 * Example TSSI bounds 0xF0 0xD0 0xB5 0xA0 0x88 0x45 0x25 0x15 0x00
2974 */
2975 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02002976 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG1, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02002977 tssi_bounds[0] = rt2x00_get_field16(eeprom,
2978 EEPROM_TSSI_BOUND_BG1_MINUS4);
2979 tssi_bounds[1] = rt2x00_get_field16(eeprom,
2980 EEPROM_TSSI_BOUND_BG1_MINUS3);
2981
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02002982 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG2, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02002983 tssi_bounds[2] = rt2x00_get_field16(eeprom,
2984 EEPROM_TSSI_BOUND_BG2_MINUS2);
2985 tssi_bounds[3] = rt2x00_get_field16(eeprom,
2986 EEPROM_TSSI_BOUND_BG2_MINUS1);
2987
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02002988 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG3, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02002989 tssi_bounds[4] = rt2x00_get_field16(eeprom,
2990 EEPROM_TSSI_BOUND_BG3_REF);
2991 tssi_bounds[5] = rt2x00_get_field16(eeprom,
2992 EEPROM_TSSI_BOUND_BG3_PLUS1);
2993
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02002994 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG4, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02002995 tssi_bounds[6] = rt2x00_get_field16(eeprom,
2996 EEPROM_TSSI_BOUND_BG4_PLUS2);
2997 tssi_bounds[7] = rt2x00_get_field16(eeprom,
2998 EEPROM_TSSI_BOUND_BG4_PLUS3);
2999
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003000 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG5, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003001 tssi_bounds[8] = rt2x00_get_field16(eeprom,
3002 EEPROM_TSSI_BOUND_BG5_PLUS4);
3003
3004 step = rt2x00_get_field16(eeprom,
3005 EEPROM_TSSI_BOUND_BG5_AGC_STEP);
3006 } else {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003007 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A1, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003008 tssi_bounds[0] = rt2x00_get_field16(eeprom,
3009 EEPROM_TSSI_BOUND_A1_MINUS4);
3010 tssi_bounds[1] = rt2x00_get_field16(eeprom,
3011 EEPROM_TSSI_BOUND_A1_MINUS3);
3012
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003013 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A2, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003014 tssi_bounds[2] = rt2x00_get_field16(eeprom,
3015 EEPROM_TSSI_BOUND_A2_MINUS2);
3016 tssi_bounds[3] = rt2x00_get_field16(eeprom,
3017 EEPROM_TSSI_BOUND_A2_MINUS1);
3018
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003019 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A3, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003020 tssi_bounds[4] = rt2x00_get_field16(eeprom,
3021 EEPROM_TSSI_BOUND_A3_REF);
3022 tssi_bounds[5] = rt2x00_get_field16(eeprom,
3023 EEPROM_TSSI_BOUND_A3_PLUS1);
3024
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003025 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A4, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003026 tssi_bounds[6] = rt2x00_get_field16(eeprom,
3027 EEPROM_TSSI_BOUND_A4_PLUS2);
3028 tssi_bounds[7] = rt2x00_get_field16(eeprom,
3029 EEPROM_TSSI_BOUND_A4_PLUS3);
3030
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003031 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A5, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003032 tssi_bounds[8] = rt2x00_get_field16(eeprom,
3033 EEPROM_TSSI_BOUND_A5_PLUS4);
3034
3035 step = rt2x00_get_field16(eeprom,
3036 EEPROM_TSSI_BOUND_A5_AGC_STEP);
3037 }
3038
3039 /*
3040 * Check if temperature compensation is supported.
3041 */
Stanislaw Gruszkabf7e1ab2012-10-25 09:51:39 +02003042 if (tssi_bounds[4] == 0xff || step == 0xff)
Helmut Schaa9e33a352011-03-28 13:33:40 +02003043 return 0;
3044
3045 /*
3046 * Read current TSSI (BBP 49).
3047 */
3048 rt2800_bbp_read(rt2x00dev, 49, &current_tssi);
3049
3050 /*
3051 * Compare TSSI value (BBP49) with the compensation boundaries
3052 * from the EEPROM and increase or decrease tx power.
3053 */
3054 for (i = 0; i <= 3; i++) {
3055 if (current_tssi > tssi_bounds[i])
3056 break;
3057 }
3058
3059 if (i == 4) {
3060 for (i = 8; i >= 5; i--) {
3061 if (current_tssi < tssi_bounds[i])
3062 break;
3063 }
3064 }
3065
3066 return (i - 4) * step;
3067}
3068
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003069static int rt2800_get_txpower_bw_comp(struct rt2x00_dev *rt2x00dev,
3070 enum ieee80211_band band)
3071{
3072 u16 eeprom;
3073 u8 comp_en;
3074 u8 comp_type;
Helmut Schaa75faae82011-03-28 13:31:30 +02003075 int comp_value = 0;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003076
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003077 rt2800_eeprom_read(rt2x00dev, EEPROM_TXPOWER_DELTA, &eeprom);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003078
Helmut Schaa75faae82011-03-28 13:31:30 +02003079 /*
3080 * HT40 compensation not required.
3081 */
3082 if (eeprom == 0xffff ||
3083 !test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003084 return 0;
3085
3086 if (band == IEEE80211_BAND_2GHZ) {
3087 comp_en = rt2x00_get_field16(eeprom,
3088 EEPROM_TXPOWER_DELTA_ENABLE_2G);
3089 if (comp_en) {
3090 comp_type = rt2x00_get_field16(eeprom,
3091 EEPROM_TXPOWER_DELTA_TYPE_2G);
3092 comp_value = rt2x00_get_field16(eeprom,
3093 EEPROM_TXPOWER_DELTA_VALUE_2G);
3094 if (!comp_type)
3095 comp_value = -comp_value;
3096 }
3097 } else {
3098 comp_en = rt2x00_get_field16(eeprom,
3099 EEPROM_TXPOWER_DELTA_ENABLE_5G);
3100 if (comp_en) {
3101 comp_type = rt2x00_get_field16(eeprom,
3102 EEPROM_TXPOWER_DELTA_TYPE_5G);
3103 comp_value = rt2x00_get_field16(eeprom,
3104 EEPROM_TXPOWER_DELTA_VALUE_5G);
3105 if (!comp_type)
3106 comp_value = -comp_value;
3107 }
3108 }
3109
3110 return comp_value;
3111}
3112
Stanislaw Gruszka1e4cf242012-10-05 13:44:14 +02003113static int rt2800_get_txpower_reg_delta(struct rt2x00_dev *rt2x00dev,
3114 int power_level, int max_power)
3115{
3116 int delta;
3117
3118 if (test_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags))
3119 return 0;
3120
3121 /*
3122 * XXX: We don't know the maximum transmit power of our hardware since
3123 * the EEPROM doesn't expose it. We only know that we are calibrated
3124 * to 100% tx power.
3125 *
3126 * Hence, we assume the regulatory limit that cfg80211 calulated for
3127 * the current channel is our maximum and if we are requested to lower
3128 * the value we just reduce our tx power accordingly.
3129 */
3130 delta = power_level - max_power;
3131 return min(delta, 0);
3132}
3133
Helmut Schaafa71a162011-03-28 13:32:32 +02003134static u8 rt2800_compensate_txpower(struct rt2x00_dev *rt2x00dev, int is_rate_b,
3135 enum ieee80211_band band, int power_level,
3136 u8 txpower, int delta)
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003137{
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003138 u16 eeprom;
3139 u8 criterion;
3140 u8 eirp_txpower;
3141 u8 eirp_txpower_criterion;
3142 u8 reg_limit;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003143
Gabor Juhos34542ff2013-07-08 16:08:20 +02003144 if (rt2x00_rt(rt2x00dev, RT3593))
3145 return min_t(u8, txpower, 0xc);
3146
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02003147 if (test_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags)) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003148 /*
3149 * Check if eirp txpower exceed txpower_limit.
3150 * We use OFDM 6M as criterion and its eirp txpower
3151 * is stored at EEPROM_EIRP_MAX_TX_POWER.
3152 * .11b data rate need add additional 4dbm
3153 * when calculating eirp txpower.
3154 */
Gabor Juhos022138c2013-07-08 11:25:54 +02003155 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3156 1, &eeprom);
Stanislaw Gruszkad9bceae2012-10-05 13:44:12 +02003157 criterion = rt2x00_get_field16(eeprom,
3158 EEPROM_TXPOWER_BYRATE_RATE0);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003159
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003160 rt2800_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER,
Stanislaw Gruszkad9bceae2012-10-05 13:44:12 +02003161 &eeprom);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003162
3163 if (band == IEEE80211_BAND_2GHZ)
3164 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
3165 EEPROM_EIRP_MAX_TX_POWER_2GHZ);
3166 else
3167 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
3168 EEPROM_EIRP_MAX_TX_POWER_5GHZ);
3169
3170 eirp_txpower = eirp_txpower_criterion + (txpower - criterion) +
Helmut Schaa2af242e2011-03-28 13:32:01 +02003171 (is_rate_b ? 4 : 0) + delta;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003172
3173 reg_limit = (eirp_txpower > power_level) ?
3174 (eirp_txpower - power_level) : 0;
3175 } else
3176 reg_limit = 0;
3177
Stanislaw Gruszka19f3fa22012-10-05 13:44:10 +02003178 txpower = max(0, txpower + delta - reg_limit);
3179 return min_t(u8, txpower, 0xc);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003180}
3181
Gabor Juhos34542ff2013-07-08 16:08:20 +02003182
3183enum {
3184 TX_PWR_CFG_0_IDX,
3185 TX_PWR_CFG_1_IDX,
3186 TX_PWR_CFG_2_IDX,
3187 TX_PWR_CFG_3_IDX,
3188 TX_PWR_CFG_4_IDX,
3189 TX_PWR_CFG_5_IDX,
3190 TX_PWR_CFG_6_IDX,
3191 TX_PWR_CFG_7_IDX,
3192 TX_PWR_CFG_8_IDX,
3193 TX_PWR_CFG_9_IDX,
3194 TX_PWR_CFG_0_EXT_IDX,
3195 TX_PWR_CFG_1_EXT_IDX,
3196 TX_PWR_CFG_2_EXT_IDX,
3197 TX_PWR_CFG_3_EXT_IDX,
3198 TX_PWR_CFG_4_EXT_IDX,
3199 TX_PWR_CFG_IDX_COUNT,
3200};
3201
3202static void rt2800_config_txpower_rt3593(struct rt2x00_dev *rt2x00dev,
3203 struct ieee80211_channel *chan,
3204 int power_level)
3205{
3206 u8 txpower;
3207 u16 eeprom;
3208 u32 regs[TX_PWR_CFG_IDX_COUNT];
3209 unsigned int offset;
3210 enum ieee80211_band band = chan->band;
3211 int delta;
3212 int i;
3213
3214 memset(regs, '\0', sizeof(regs));
3215
3216 /* TODO: adapt TX power reduction from the rt28xx code */
3217
3218 /* calculate temperature compensation delta */
3219 delta = rt2800_get_gain_calibration_delta(rt2x00dev);
3220
3221 if (band == IEEE80211_BAND_5GHZ)
3222 offset = 16;
3223 else
3224 offset = 0;
3225
3226 if (test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
3227 offset += 8;
3228
3229 /* read the next four txpower values */
3230 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3231 offset, &eeprom);
3232
3233 /* CCK 1MBS,2MBS */
3234 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3235 txpower = rt2800_compensate_txpower(rt2x00dev, 1, band, power_level,
3236 txpower, delta);
3237 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3238 TX_PWR_CFG_0_CCK1_CH0, txpower);
3239 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3240 TX_PWR_CFG_0_CCK1_CH1, txpower);
3241 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3242 TX_PWR_CFG_0_EXT_CCK1_CH2, txpower);
3243
3244 /* CCK 5.5MBS,11MBS */
3245 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3246 txpower = rt2800_compensate_txpower(rt2x00dev, 1, band, power_level,
3247 txpower, delta);
3248 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3249 TX_PWR_CFG_0_CCK5_CH0, txpower);
3250 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3251 TX_PWR_CFG_0_CCK5_CH1, txpower);
3252 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3253 TX_PWR_CFG_0_EXT_CCK5_CH2, txpower);
3254
3255 /* OFDM 6MBS,9MBS */
3256 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3257 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3258 txpower, delta);
3259 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3260 TX_PWR_CFG_0_OFDM6_CH0, txpower);
3261 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3262 TX_PWR_CFG_0_OFDM6_CH1, txpower);
3263 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3264 TX_PWR_CFG_0_EXT_OFDM6_CH2, txpower);
3265
3266 /* OFDM 12MBS,18MBS */
3267 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3268 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3269 txpower, delta);
3270 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3271 TX_PWR_CFG_0_OFDM12_CH0, txpower);
3272 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3273 TX_PWR_CFG_0_OFDM12_CH1, txpower);
3274 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3275 TX_PWR_CFG_0_EXT_OFDM12_CH2, txpower);
3276
3277 /* read the next four txpower values */
3278 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3279 offset + 1, &eeprom);
3280
3281 /* OFDM 24MBS,36MBS */
3282 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3283 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3284 txpower, delta);
3285 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3286 TX_PWR_CFG_1_OFDM24_CH0, txpower);
3287 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3288 TX_PWR_CFG_1_OFDM24_CH1, txpower);
3289 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3290 TX_PWR_CFG_1_EXT_OFDM24_CH2, txpower);
3291
3292 /* OFDM 48MBS */
3293 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3294 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3295 txpower, delta);
3296 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3297 TX_PWR_CFG_1_OFDM48_CH0, txpower);
3298 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3299 TX_PWR_CFG_1_OFDM48_CH1, txpower);
3300 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3301 TX_PWR_CFG_1_EXT_OFDM48_CH2, txpower);
3302
3303 /* OFDM 54MBS */
3304 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3305 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3306 txpower, delta);
3307 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3308 TX_PWR_CFG_7_OFDM54_CH0, txpower);
3309 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3310 TX_PWR_CFG_7_OFDM54_CH1, txpower);
3311 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3312 TX_PWR_CFG_7_OFDM54_CH2, txpower);
3313
3314 /* read the next four txpower values */
3315 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3316 offset + 2, &eeprom);
3317
3318 /* MCS 0,1 */
3319 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3320 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3321 txpower, delta);
3322 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3323 TX_PWR_CFG_1_MCS0_CH0, txpower);
3324 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3325 TX_PWR_CFG_1_MCS0_CH1, txpower);
3326 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3327 TX_PWR_CFG_1_EXT_MCS0_CH2, txpower);
3328
3329 /* MCS 2,3 */
3330 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3331 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3332 txpower, delta);
3333 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3334 TX_PWR_CFG_1_MCS2_CH0, txpower);
3335 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3336 TX_PWR_CFG_1_MCS2_CH1, txpower);
3337 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3338 TX_PWR_CFG_1_EXT_MCS2_CH2, txpower);
3339
3340 /* MCS 4,5 */
3341 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3342 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3343 txpower, delta);
3344 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3345 TX_PWR_CFG_2_MCS4_CH0, txpower);
3346 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3347 TX_PWR_CFG_2_MCS4_CH1, txpower);
3348 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3349 TX_PWR_CFG_2_EXT_MCS4_CH2, txpower);
3350
3351 /* MCS 6 */
3352 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3353 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3354 txpower, delta);
3355 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3356 TX_PWR_CFG_2_MCS6_CH0, txpower);
3357 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3358 TX_PWR_CFG_2_MCS6_CH1, txpower);
3359 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3360 TX_PWR_CFG_2_EXT_MCS6_CH2, txpower);
3361
3362 /* read the next four txpower values */
3363 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3364 offset + 3, &eeprom);
3365
3366 /* MCS 7 */
3367 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3368 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3369 txpower, delta);
3370 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3371 TX_PWR_CFG_7_MCS7_CH0, txpower);
3372 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3373 TX_PWR_CFG_7_MCS7_CH1, txpower);
3374 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3375 TX_PWR_CFG_7_MCS7_CH2, txpower);
3376
3377 /* MCS 8,9 */
3378 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3379 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3380 txpower, delta);
3381 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3382 TX_PWR_CFG_2_MCS8_CH0, txpower);
3383 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3384 TX_PWR_CFG_2_MCS8_CH1, txpower);
3385 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3386 TX_PWR_CFG_2_EXT_MCS8_CH2, txpower);
3387
3388 /* MCS 10,11 */
3389 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3390 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3391 txpower, delta);
3392 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3393 TX_PWR_CFG_2_MCS10_CH0, txpower);
3394 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3395 TX_PWR_CFG_2_MCS10_CH1, txpower);
3396 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3397 TX_PWR_CFG_2_EXT_MCS10_CH2, txpower);
3398
3399 /* MCS 12,13 */
3400 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3401 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3402 txpower, delta);
3403 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3404 TX_PWR_CFG_3_MCS12_CH0, txpower);
3405 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3406 TX_PWR_CFG_3_MCS12_CH1, txpower);
3407 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
3408 TX_PWR_CFG_3_EXT_MCS12_CH2, txpower);
3409
3410 /* read the next four txpower values */
3411 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3412 offset + 4, &eeprom);
3413
3414 /* MCS 14 */
3415 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3416 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3417 txpower, delta);
3418 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3419 TX_PWR_CFG_3_MCS14_CH0, txpower);
3420 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3421 TX_PWR_CFG_3_MCS14_CH1, txpower);
3422 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
3423 TX_PWR_CFG_3_EXT_MCS14_CH2, txpower);
3424
3425 /* MCS 15 */
3426 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3427 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3428 txpower, delta);
3429 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3430 TX_PWR_CFG_8_MCS15_CH0, txpower);
3431 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3432 TX_PWR_CFG_8_MCS15_CH1, txpower);
3433 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3434 TX_PWR_CFG_8_MCS15_CH2, txpower);
3435
3436 /* MCS 16,17 */
3437 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3438 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3439 txpower, delta);
3440 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3441 TX_PWR_CFG_5_MCS16_CH0, txpower);
3442 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3443 TX_PWR_CFG_5_MCS16_CH1, txpower);
3444 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3445 TX_PWR_CFG_5_MCS16_CH2, txpower);
3446
3447 /* MCS 18,19 */
3448 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3449 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3450 txpower, delta);
3451 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3452 TX_PWR_CFG_5_MCS18_CH0, txpower);
3453 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3454 TX_PWR_CFG_5_MCS18_CH1, txpower);
3455 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3456 TX_PWR_CFG_5_MCS18_CH2, txpower);
3457
3458 /* read the next four txpower values */
3459 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3460 offset + 5, &eeprom);
3461
3462 /* MCS 20,21 */
3463 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3464 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3465 txpower, delta);
3466 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3467 TX_PWR_CFG_6_MCS20_CH0, txpower);
3468 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3469 TX_PWR_CFG_6_MCS20_CH1, txpower);
3470 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3471 TX_PWR_CFG_6_MCS20_CH2, txpower);
3472
3473 /* MCS 22 */
3474 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3475 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3476 txpower, delta);
3477 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3478 TX_PWR_CFG_6_MCS22_CH0, txpower);
3479 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3480 TX_PWR_CFG_6_MCS22_CH1, txpower);
3481 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3482 TX_PWR_CFG_6_MCS22_CH2, txpower);
3483
3484 /* MCS 23 */
3485 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3486 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3487 txpower, delta);
3488 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3489 TX_PWR_CFG_8_MCS23_CH0, txpower);
3490 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3491 TX_PWR_CFG_8_MCS23_CH1, txpower);
3492 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3493 TX_PWR_CFG_8_MCS23_CH2, txpower);
3494
3495 /* read the next four txpower values */
3496 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3497 offset + 6, &eeprom);
3498
3499 /* STBC, MCS 0,1 */
3500 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3501 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3502 txpower, delta);
3503 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3504 TX_PWR_CFG_3_STBC0_CH0, txpower);
3505 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3506 TX_PWR_CFG_3_STBC0_CH1, txpower);
3507 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
3508 TX_PWR_CFG_3_EXT_STBC0_CH2, txpower);
3509
3510 /* STBC, MCS 2,3 */
3511 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3512 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3513 txpower, delta);
3514 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3515 TX_PWR_CFG_3_STBC2_CH0, txpower);
3516 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3517 TX_PWR_CFG_3_STBC2_CH1, txpower);
3518 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
3519 TX_PWR_CFG_3_EXT_STBC2_CH2, txpower);
3520
3521 /* STBC, MCS 4,5 */
3522 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3523 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3524 txpower, delta);
3525 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE0, txpower);
3526 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE1, txpower);
3527 rt2x00_set_field32(&regs[TX_PWR_CFG_4_EXT_IDX], TX_PWR_CFG_RATE0,
3528 txpower);
3529
3530 /* STBC, MCS 6 */
3531 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3532 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3533 txpower, delta);
3534 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE2, txpower);
3535 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE3, txpower);
3536 rt2x00_set_field32(&regs[TX_PWR_CFG_4_EXT_IDX], TX_PWR_CFG_RATE2,
3537 txpower);
3538
3539 /* read the next four txpower values */
3540 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3541 offset + 7, &eeprom);
3542
3543 /* STBC, MCS 7 */
3544 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3545 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3546 txpower, delta);
3547 rt2x00_set_field32(&regs[TX_PWR_CFG_9_IDX],
3548 TX_PWR_CFG_9_STBC7_CH0, txpower);
3549 rt2x00_set_field32(&regs[TX_PWR_CFG_9_IDX],
3550 TX_PWR_CFG_9_STBC7_CH1, txpower);
3551 rt2x00_set_field32(&regs[TX_PWR_CFG_9_IDX],
3552 TX_PWR_CFG_9_STBC7_CH2, txpower);
3553
3554 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0, regs[TX_PWR_CFG_0_IDX]);
3555 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1, regs[TX_PWR_CFG_1_IDX]);
3556 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2, regs[TX_PWR_CFG_2_IDX]);
3557 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3, regs[TX_PWR_CFG_3_IDX]);
3558 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4, regs[TX_PWR_CFG_4_IDX]);
3559 rt2800_register_write(rt2x00dev, TX_PWR_CFG_5, regs[TX_PWR_CFG_5_IDX]);
3560 rt2800_register_write(rt2x00dev, TX_PWR_CFG_6, regs[TX_PWR_CFG_6_IDX]);
3561 rt2800_register_write(rt2x00dev, TX_PWR_CFG_7, regs[TX_PWR_CFG_7_IDX]);
3562 rt2800_register_write(rt2x00dev, TX_PWR_CFG_8, regs[TX_PWR_CFG_8_IDX]);
3563 rt2800_register_write(rt2x00dev, TX_PWR_CFG_9, regs[TX_PWR_CFG_9_IDX]);
3564
3565 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0_EXT,
3566 regs[TX_PWR_CFG_0_EXT_IDX]);
3567 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1_EXT,
3568 regs[TX_PWR_CFG_1_EXT_IDX]);
3569 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2_EXT,
3570 regs[TX_PWR_CFG_2_EXT_IDX]);
3571 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3_EXT,
3572 regs[TX_PWR_CFG_3_EXT_IDX]);
3573 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4_EXT,
3574 regs[TX_PWR_CFG_4_EXT_IDX]);
3575
3576 for (i = 0; i < TX_PWR_CFG_IDX_COUNT; i++)
3577 rt2x00_dbg(rt2x00dev,
3578 "band:%cGHz, BW:%c0MHz, TX_PWR_CFG_%d%s = %08lx\n",
3579 (band == IEEE80211_BAND_5GHZ) ? '5' : '2',
3580 (test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags)) ?
3581 '4' : '2',
3582 (i > TX_PWR_CFG_9_IDX) ?
3583 (i - TX_PWR_CFG_9_IDX - 1) : i,
3584 (i > TX_PWR_CFG_9_IDX) ? "_EXT" : "",
3585 (unsigned long) regs[i]);
3586}
3587
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02003588/*
3589 * We configure transmit power using MAC TX_PWR_CFG_{0,...,N} registers and
3590 * BBP R1 register. TX_PWR_CFG_X allow to configure per rate TX power values,
3591 * 4 bits for each rate (tune from 0 to 15 dBm). BBP_R1 controls transmit power
3592 * for all rates, but allow to set only 4 discrete values: -12, -6, 0 and 6 dBm.
3593 * Reference per rate transmit power values are located in the EEPROM at
3594 * EEPROM_TXPOWER_BYRATE offset. We adjust them and BBP R1 settings according to
3595 * current conditions (i.e. band, bandwidth, temperature, user settings).
3596 */
Gabor Juhos34542ff2013-07-08 16:08:20 +02003597static void rt2800_config_txpower_rt28xx(struct rt2x00_dev *rt2x00dev,
3598 struct ieee80211_channel *chan,
3599 int power_level)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003600{
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02003601 u8 txpower, r1;
Helmut Schaa5e846002010-07-11 12:23:09 +02003602 u16 eeprom;
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02003603 u32 reg, offset;
3604 int i, is_rate_b, delta, power_ctrl;
Stanislaw Gruszka146c3b02012-10-05 13:44:13 +02003605 enum ieee80211_band band = chan->band;
Helmut Schaa2af242e2011-03-28 13:32:01 +02003606
3607 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02003608 * Calculate HT40 compensation. For 40MHz we need to add or subtract
3609 * value read from EEPROM (different for 2GHz and for 5GHz).
Helmut Schaa2af242e2011-03-28 13:32:01 +02003610 */
3611 delta = rt2800_get_txpower_bw_comp(rt2x00dev, band);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003612
Helmut Schaa5e846002010-07-11 12:23:09 +02003613 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02003614 * Calculate temperature compensation. Depends on measurement of current
3615 * TSSI (Transmitter Signal Strength Indication) we know TX power (due
3616 * to temperature or maybe other factors) is smaller or bigger than
3617 * expected. We adjust it, based on TSSI reference and boundaries values
3618 * provided in EEPROM.
Helmut Schaa9e33a352011-03-28 13:33:40 +02003619 */
3620 delta += rt2800_get_gain_calibration_delta(rt2x00dev);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003621
Helmut Schaa5e846002010-07-11 12:23:09 +02003622 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02003623 * Decrease power according to user settings, on devices with unknown
3624 * maximum tx power. For other devices we take user power_level into
3625 * consideration on rt2800_compensate_txpower().
Stanislaw Gruszka1e4cf242012-10-05 13:44:14 +02003626 */
3627 delta += rt2800_get_txpower_reg_delta(rt2x00dev, power_level,
3628 chan->max_power);
3629
3630 /*
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02003631 * BBP_R1 controls TX power for all rates, it allow to set the following
3632 * gains -12, -6, 0, +6 dBm by setting values 2, 1, 0, 3 respectively.
3633 *
3634 * TODO: we do not use +6 dBm option to do not increase power beyond
3635 * regulatory limit, however this could be utilized for devices with
3636 * CAPABILITY_POWER_LIMIT.
Stanislaw Gruszka8c8d20172013-06-11 18:48:53 +02003637 *
3638 * TODO: add different temperature compensation code for RT3290 & RT5390
3639 * to allow to use BBP_R1 for those chips.
Helmut Schaa5e846002010-07-11 12:23:09 +02003640 */
Stanislaw Gruszka8c8d20172013-06-11 18:48:53 +02003641 if (!rt2x00_rt(rt2x00dev, RT3290) &&
3642 !rt2x00_rt(rt2x00dev, RT5390)) {
3643 rt2800_bbp_read(rt2x00dev, 1, &r1);
3644 if (delta <= -12) {
3645 power_ctrl = 2;
3646 delta += 12;
3647 } else if (delta <= -6) {
3648 power_ctrl = 1;
3649 delta += 6;
3650 } else {
3651 power_ctrl = 0;
3652 }
3653 rt2x00_set_field8(&r1, BBP1_TX_POWER_CTRL, power_ctrl);
3654 rt2800_bbp_write(rt2x00dev, 1, r1);
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02003655 }
Stanislaw Gruszka8c8d20172013-06-11 18:48:53 +02003656
Helmut Schaa5e846002010-07-11 12:23:09 +02003657 offset = TX_PWR_CFG_0;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003658
Helmut Schaa5e846002010-07-11 12:23:09 +02003659 for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
3660 /* just to be safe */
3661 if (offset > TX_PWR_CFG_4)
3662 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003663
Helmut Schaa5e846002010-07-11 12:23:09 +02003664 rt2800_register_read(rt2x00dev, offset, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003665
Helmut Schaa5e846002010-07-11 12:23:09 +02003666 /* read the next four txpower values */
Gabor Juhos022138c2013-07-08 11:25:54 +02003667 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3668 i, &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003669
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003670 is_rate_b = i ? 0 : 1;
3671 /*
3672 * TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02003673 * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003674 * TX_PWR_CFG_4: unknown
3675 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003676 txpower = rt2x00_get_field16(eeprom,
3677 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02003678 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003679 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003680 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003681
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003682 /*
3683 * TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02003684 * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003685 * TX_PWR_CFG_4: unknown
3686 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003687 txpower = rt2x00_get_field16(eeprom,
3688 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02003689 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003690 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003691 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003692
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003693 /*
3694 * TX_PWR_CFG_0: 5.5MBS, TX_PWR_CFG_1: 48MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02003695 * TX_PWR_CFG_2: MCS6, TX_PWR_CFG_3: MCS14,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003696 * TX_PWR_CFG_4: unknown
3697 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003698 txpower = rt2x00_get_field16(eeprom,
3699 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02003700 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003701 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003702 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003703
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003704 /*
3705 * TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02003706 * TX_PWR_CFG_2: MCS7, TX_PWR_CFG_3: MCS15,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003707 * TX_PWR_CFG_4: unknown
3708 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003709 txpower = rt2x00_get_field16(eeprom,
3710 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02003711 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003712 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003713 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003714
3715 /* read the next four txpower values */
Gabor Juhos022138c2013-07-08 11:25:54 +02003716 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3717 i + 1, &eeprom);
Helmut Schaa5e846002010-07-11 12:23:09 +02003718
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003719 is_rate_b = 0;
3720 /*
3721 * TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
Helmut Schaa5e846002010-07-11 12:23:09 +02003722 * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003723 * TX_PWR_CFG_4: unknown
3724 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003725 txpower = rt2x00_get_field16(eeprom,
3726 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02003727 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003728 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003729 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003730
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003731 /*
3732 * TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
Helmut Schaa5e846002010-07-11 12:23:09 +02003733 * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003734 * TX_PWR_CFG_4: unknown
3735 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003736 txpower = rt2x00_get_field16(eeprom,
3737 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02003738 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003739 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003740 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003741
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003742 /*
3743 * TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
Helmut Schaa5e846002010-07-11 12:23:09 +02003744 * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003745 * TX_PWR_CFG_4: unknown
3746 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003747 txpower = rt2x00_get_field16(eeprom,
3748 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02003749 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003750 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003751 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003752
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003753 /*
3754 * TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
Helmut Schaa5e846002010-07-11 12:23:09 +02003755 * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003756 * TX_PWR_CFG_4: unknown
3757 */
Helmut Schaa5e846002010-07-11 12:23:09 +02003758 txpower = rt2x00_get_field16(eeprom,
3759 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02003760 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02003761 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003762 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02003763
3764 rt2800_register_write(rt2x00dev, offset, reg);
3765
3766 /* next TX_PWR_CFG register */
3767 offset += 4;
3768 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003769}
3770
Gabor Juhos34542ff2013-07-08 16:08:20 +02003771static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
3772 struct ieee80211_channel *chan,
3773 int power_level)
3774{
3775 if (rt2x00_rt(rt2x00dev, RT3593))
3776 rt2800_config_txpower_rt3593(rt2x00dev, chan, power_level);
3777 else
3778 rt2800_config_txpower_rt28xx(rt2x00dev, chan, power_level);
3779}
3780
Helmut Schaa9e33a352011-03-28 13:33:40 +02003781void rt2800_gain_calibration(struct rt2x00_dev *rt2x00dev)
3782{
Karl Beldan675a0b02013-03-25 16:26:57 +01003783 rt2800_config_txpower(rt2x00dev, rt2x00dev->hw->conf.chandef.chan,
Helmut Schaa9e33a352011-03-28 13:33:40 +02003784 rt2x00dev->tx_power);
3785}
3786EXPORT_SYMBOL_GPL(rt2800_gain_calibration);
3787
John Li2e9c43d2012-02-16 21:40:57 +08003788void rt2800_vco_calibration(struct rt2x00_dev *rt2x00dev)
3789{
3790 u32 tx_pin;
3791 u8 rfcsr;
3792
3793 /*
3794 * A voltage-controlled oscillator(VCO) is an electronic oscillator
3795 * designed to be controlled in oscillation frequency by a voltage
3796 * input. Maybe the temperature will affect the frequency of
3797 * oscillation to be shifted. The VCO calibration will be called
3798 * periodically to adjust the frequency to be precision.
3799 */
3800
3801 rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
3802 tx_pin &= TX_PIN_CFG_PA_PE_DISABLE;
3803 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
3804
3805 switch (rt2x00dev->chip.rf) {
3806 case RF2020:
3807 case RF3020:
3808 case RF3021:
3809 case RF3022:
3810 case RF3320:
3811 case RF3052:
3812 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
3813 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
3814 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
3815 break;
Woody Hunga89534e2012-06-13 15:01:16 +08003816 case RF3290:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02003817 case RF5360:
John Li2e9c43d2012-02-16 21:40:57 +08003818 case RF5370:
3819 case RF5372:
3820 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08003821 case RF5392:
John Li2e9c43d2012-02-16 21:40:57 +08003822 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
Gabor Juhosd6d82022012-12-02 18:34:47 +01003823 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
John Li2e9c43d2012-02-16 21:40:57 +08003824 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
3825 break;
3826 default:
3827 return;
3828 }
3829
3830 mdelay(1);
3831
3832 rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
3833 if (rt2x00dev->rf_channel <= 14) {
3834 switch (rt2x00dev->default_ant.tx_chain_num) {
3835 case 3:
3836 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G2_EN, 1);
3837 /* fall through */
3838 case 2:
3839 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
3840 /* fall through */
3841 case 1:
3842 default:
3843 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
3844 break;
3845 }
3846 } else {
3847 switch (rt2x00dev->default_ant.tx_chain_num) {
3848 case 3:
3849 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A2_EN, 1);
3850 /* fall through */
3851 case 2:
3852 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
3853 /* fall through */
3854 case 1:
3855 default:
3856 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, 1);
3857 break;
3858 }
3859 }
3860 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
3861
3862}
3863EXPORT_SYMBOL_GPL(rt2800_vco_calibration);
3864
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003865static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
3866 struct rt2x00lib_conf *libconf)
3867{
3868 u32 reg;
3869
3870 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
3871 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
3872 libconf->conf->short_frame_max_tx_count);
3873 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
3874 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003875 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
3876}
3877
3878static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
3879 struct rt2x00lib_conf *libconf)
3880{
3881 enum dev_state state =
3882 (libconf->conf->flags & IEEE80211_CONF_PS) ?
3883 STATE_SLEEP : STATE_AWAKE;
3884 u32 reg;
3885
3886 if (state == STATE_SLEEP) {
3887 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
3888
3889 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
3890 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
3891 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
3892 libconf->conf->listen_interval - 1);
3893 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
3894 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
3895
3896 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
3897 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003898 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
3899 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
3900 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
3901 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
3902 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02003903
3904 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003905 }
3906}
3907
3908void rt2800_config(struct rt2x00_dev *rt2x00dev,
3909 struct rt2x00lib_conf *libconf,
3910 const unsigned int flags)
3911{
3912 /* Always recalculate LNA gain before changing configuration */
3913 rt2800_config_lna_gain(rt2x00dev, libconf);
3914
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003915 if (flags & IEEE80211_CONF_CHANGE_CHANNEL) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003916 rt2800_config_channel(rt2x00dev, libconf->conf,
3917 &libconf->rf, &libconf->channel);
Karl Beldan675a0b02013-03-25 16:26:57 +01003918 rt2800_config_txpower(rt2x00dev, libconf->conf->chandef.chan,
Helmut Schaa9e33a352011-03-28 13:33:40 +02003919 libconf->conf->power_level);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003920 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003921 if (flags & IEEE80211_CONF_CHANGE_POWER)
Karl Beldan675a0b02013-03-25 16:26:57 +01003922 rt2800_config_txpower(rt2x00dev, libconf->conf->chandef.chan,
Helmut Schaa9e33a352011-03-28 13:33:40 +02003923 libconf->conf->power_level);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003924 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
3925 rt2800_config_retry_limit(rt2x00dev, libconf);
3926 if (flags & IEEE80211_CONF_CHANGE_PS)
3927 rt2800_config_ps(rt2x00dev, libconf);
3928}
3929EXPORT_SYMBOL_GPL(rt2800_config);
3930
3931/*
3932 * Link tuning
3933 */
3934void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
3935{
3936 u32 reg;
3937
3938 /*
3939 * Update FCS error count from register.
3940 */
3941 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
3942 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
3943}
3944EXPORT_SYMBOL_GPL(rt2800_link_stats);
3945
3946static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
3947{
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02003948 u8 vgc;
3949
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003950 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003951 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003952 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003953 rt2x00_rt(rt2x00dev, RT3090) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003954 rt2x00_rt(rt2x00dev, RT3290) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01003955 rt2x00_rt(rt2x00dev, RT3390) ||
Gertjan van Wingerded961e442012-09-16 22:29:50 +02003956 rt2x00_rt(rt2x00dev, RT3572) ||
John Li2ed71882012-02-17 17:33:06 +08003957 rt2x00_rt(rt2x00dev, RT5390) ||
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01003958 rt2x00_rt(rt2x00dev, RT5392) ||
3959 rt2x00_rt(rt2x00dev, RT5592))
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02003960 vgc = 0x1c + (2 * rt2x00dev->lna_gain);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003961 else
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02003962 vgc = 0x2e + rt2x00dev->lna_gain;
3963 } else { /* 5GHZ band */
Gertjan van Wingerded961e442012-09-16 22:29:50 +02003964 if (rt2x00_rt(rt2x00dev, RT3572))
3965 vgc = 0x22 + (rt2x00dev->lna_gain * 5) / 3;
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01003966 else if (rt2x00_rt(rt2x00dev, RT5592))
3967 vgc = 0x24 + (2 * rt2x00dev->lna_gain);
Gertjan van Wingerded961e442012-09-16 22:29:50 +02003968 else {
3969 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
3970 vgc = 0x32 + (rt2x00dev->lna_gain * 5) / 3;
3971 else
3972 vgc = 0x3a + (rt2x00dev->lna_gain * 5) / 3;
3973 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003974 }
3975
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02003976 return vgc;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003977}
3978
3979static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
3980 struct link_qual *qual, u8 vgc_level)
3981{
3982 if (qual->vgc_level != vgc_level) {
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01003983 if (rt2x00_rt(rt2x00dev, RT5592)) {
3984 rt2800_bbp_write(rt2x00dev, 83, qual->rssi > -65 ? 0x4a : 0x7a);
3985 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66, vgc_level);
3986 } else
3987 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003988 qual->vgc_level = vgc_level;
3989 qual->vgc_level_reg = vgc_level;
3990 }
3991}
3992
3993void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
3994{
3995 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
3996}
3997EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
3998
3999void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
4000 const u32 count)
4001{
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004002 u8 vgc;
4003
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02004004 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004005 return;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004006 /*
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004007 * When RSSI is better then -80 increase VGC level with 0x10, except
4008 * for rt5592 chip.
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004009 */
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004010
4011 vgc = rt2800_get_default_vgc(rt2x00dev);
4012
4013 if (rt2x00_rt(rt2x00dev, RT5592) && qual->rssi > -65)
4014 vgc += 0x20;
4015 else if (qual->rssi > -80)
4016 vgc += 0x10;
4017
4018 rt2800_set_vgc(rt2x00dev, qual, vgc);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004019}
4020EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004021
4022/*
4023 * Initialization functions.
4024 */
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02004025static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004026{
4027 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004028 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004029 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02004030 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004031
Jakub Kicinskif7b395e2012-04-03 03:40:47 +02004032 rt2800_disable_wpdma(rt2x00dev);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004033
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02004034 ret = rt2800_drv_init_registers(rt2x00dev);
4035 if (ret)
4036 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004037
4038 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
4039 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
4040 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
4041 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
4042 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
4043 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
4044
4045 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
4046 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
4047 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
4048 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
4049 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
4050 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
4051
4052 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
4053 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
4054
4055 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
4056
4057 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Helmut Schaa8544df32010-07-11 12:29:49 +02004058 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004059 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
4060 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
4061 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
4062 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
4063 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
4064 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
4065
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004066 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
4067
4068 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
4069 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
4070 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
4071 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
4072
Woody Hunga89534e2012-06-13 15:01:16 +08004073 if (rt2x00_rt(rt2x00dev, RT3290)) {
4074 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
4075 if (rt2x00_get_field32(reg, WLAN_EN) == 1) {
4076 rt2x00_set_field32(&reg, PCIE_APP0_CLK_REQ, 1);
4077 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
4078 }
4079
4080 rt2800_register_read(rt2x00dev, CMB_CTRL, &reg);
4081 if (!(rt2x00_get_field32(reg, LDO0_EN) == 1)) {
4082 rt2x00_set_field32(&reg, LDO0_EN, 1);
4083 rt2x00_set_field32(&reg, LDO_BGSEL, 3);
4084 rt2800_register_write(rt2x00dev, CMB_CTRL, reg);
4085 }
4086
4087 rt2800_register_read(rt2x00dev, OSC_CTRL, &reg);
4088 rt2x00_set_field32(&reg, OSC_ROSC_EN, 1);
4089 rt2x00_set_field32(&reg, OSC_CAL_REQ, 1);
4090 rt2x00_set_field32(&reg, OSC_REF_CYCLE, 0x27);
4091 rt2800_register_write(rt2x00dev, OSC_CTRL, reg);
4092
4093 rt2800_register_read(rt2x00dev, COEX_CFG0, &reg);
4094 rt2x00_set_field32(&reg, COEX_CFG_ANT, 0x5e);
4095 rt2800_register_write(rt2x00dev, COEX_CFG0, reg);
4096
4097 rt2800_register_read(rt2x00dev, COEX_CFG2, &reg);
4098 rt2x00_set_field32(&reg, BT_COEX_CFG1, 0x00);
4099 rt2x00_set_field32(&reg, BT_COEX_CFG0, 0x17);
4100 rt2x00_set_field32(&reg, WL_COEX_CFG1, 0x93);
4101 rt2x00_set_field32(&reg, WL_COEX_CFG0, 0x7f);
4102 rt2800_register_write(rt2x00dev, COEX_CFG2, reg);
4103
4104 rt2800_register_read(rt2x00dev, PLL_CTRL, &reg);
4105 rt2x00_set_field32(&reg, PLL_CONTROL, 1);
4106 rt2800_register_write(rt2x00dev, PLL_CTRL, reg);
4107 }
4108
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004109 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004110 rt2x00_rt(rt2x00dev, RT3090) ||
Woody Hunga89534e2012-06-13 15:01:16 +08004111 rt2x00_rt(rt2x00dev, RT3290) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004112 rt2x00_rt(rt2x00dev, RT3390)) {
Woody Hunga89534e2012-06-13 15:01:16 +08004113
4114 if (rt2x00_rt(rt2x00dev, RT3290))
4115 rt2800_register_write(rt2x00dev, TX_SW_CFG0,
4116 0x00000404);
4117 else
4118 rt2800_register_write(rt2x00dev, TX_SW_CFG0,
4119 0x00000400);
4120
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004121 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004122 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004123 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
4124 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02004125 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1,
4126 &eeprom);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004127 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004128 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4129 0x0000002c);
4130 else
4131 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4132 0x0000000f);
4133 } else {
4134 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
4135 }
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004136 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004137 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004138
4139 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
4140 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
4141 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
4142 } else {
4143 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4144 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
4145 }
Helmut Schaac295a812010-06-03 10:52:13 +02004146 } else if (rt2800_is_305x_soc(rt2x00dev)) {
4147 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
4148 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Helmut Schaa961636b2011-04-18 15:28:27 +02004149 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000030);
Daniel Golle03839952012-09-09 14:24:39 +03004150 } else if (rt2x00_rt(rt2x00dev, RT3352)) {
4151 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000402);
4152 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4153 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004154 } else if (rt2x00_rt(rt2x00dev, RT3572)) {
4155 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
4156 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
Gabor Juhos1706d152013-07-08 16:08:16 +02004157 } else if (rt2x00_rt(rt2x00dev, RT3593)) {
4158 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000402);
4159 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
4160 if (rt2x00_rt_rev_lt(rt2x00dev, RT3593, REV_RT3593E)) {
4161 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1,
4162 &eeprom);
4163 if (rt2x00_get_field16(eeprom,
4164 EEPROM_NIC_CONF1_DAC_TEST))
4165 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4166 0x0000001f);
4167 else
4168 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4169 0x0000000f);
4170 } else {
4171 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4172 0x00000000);
4173 }
John Li2ed71882012-02-17 17:33:06 +08004174 } else if (rt2x00_rt(rt2x00dev, RT5390) ||
Stanislaw Gruszka76413282013-03-16 19:19:33 +01004175 rt2x00_rt(rt2x00dev, RT5392) ||
4176 rt2x00_rt(rt2x00dev, RT5592)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01004177 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000404);
4178 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4179 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004180 } else {
4181 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
4182 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4183 }
4184
4185 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
4186 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
4187 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
4188 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
4189 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
4190 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
4191 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
4192 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
4193 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
4194 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
4195
4196 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
4197 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004198 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004199 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
4200 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
4201
4202 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
4203 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02004204 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01004205 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02004206 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004207 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
4208 else
4209 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
4210 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
4211 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
4212 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
4213
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004214 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
4215 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
4216 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
4217 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
4218 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
4219 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
4220 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
4221 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
4222 rt2800_register_write(rt2x00dev, LED_CFG, reg);
4223
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004224 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
4225
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004226 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
4227 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
4228 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
4229 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
4230 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
4231 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
4232 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
4233 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
4234
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004235 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
4236 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004237 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004238 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
4239 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004240 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004241 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
4242 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
4243 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
4244
4245 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004246 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004247 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004248 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004249 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4250 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4251 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004252 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004253 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004254 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
4255 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004256 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
4257
4258 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004259 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004260 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004261 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004262 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4263 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4264 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004265 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004266 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004267 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
4268 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004269 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
4270
4271 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
4272 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
4273 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004274 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004275 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4276 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4277 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4278 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
4279 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4280 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004281 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004282 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
4283
4284 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
4285 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Helmut Schaad13a97f2010-10-02 11:29:08 +02004286 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004287 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004288 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4289 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4290 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4291 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
4292 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4293 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004294 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004295 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
4296
4297 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
4298 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
4299 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004300 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004301 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4302 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4303 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4304 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
4305 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4306 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004307 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004308 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
4309
4310 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
4311 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
4312 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004313 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004314 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4315 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4316 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4317 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
4318 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4319 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004320 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004321 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
4322
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01004323 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004324 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
4325
4326 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
4327 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
4328 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
4329 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
4330 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
4331 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
4332 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
4333 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
4334 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
4335 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
4336 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
4337 }
4338
Helmut Schaa961621a2010-11-04 20:36:59 +01004339 /*
4340 * The legacy driver also sets TXOP_CTRL_CFG_RESERVED_TRUN_EN to 1
4341 * although it is reserved.
4342 */
4343 rt2800_register_read(rt2x00dev, TXOP_CTRL_CFG, &reg);
4344 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TIMEOUT_TRUN_EN, 1);
4345 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_AC_TRUN_EN, 1);
4346 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TXRATEGRP_TRUN_EN, 1);
4347 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_USER_MODE_TRUN_EN, 1);
4348 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_MIMO_PS_TRUN_EN, 1);
4349 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_RESERVED_TRUN_EN, 1);
4350 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_LSIG_TXOP_EN, 0);
4351 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_EN, 0);
4352 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_DLY, 88);
4353 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CWMIN, 0);
4354 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, reg);
4355
Stanislaw Gruszka76413282013-03-16 19:19:33 +01004356 reg = rt2x00_rt(rt2x00dev, RT5592) ? 0x00000082 : 0x00000002;
4357 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004358
4359 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
4360 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
4361 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
4362 IEEE80211_MAX_RTS_THRESHOLD);
4363 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
4364 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
4365
4366 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004367
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02004368 /*
4369 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
4370 * time should be set to 16. However, the original Ralink driver uses
4371 * 16 for both and indeed using a value of 10 for CCK SIFS results in
4372 * connection problems with 11g + CTS protection. Hence, use the same
4373 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
4374 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004375 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02004376 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
4377 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004378 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
4379 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
4380 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
4381 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
4382
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004383 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
4384
4385 /*
4386 * ASIC will keep garbage value after boot, clear encryption keys.
4387 */
4388 for (i = 0; i < 4; i++)
4389 rt2800_register_write(rt2x00dev,
4390 SHARED_KEY_MODE_ENTRY(i), 0);
4391
4392 for (i = 0; i < 256; i++) {
Helmut Schaad7d259d2011-09-08 14:39:04 +02004393 rt2800_config_wcid(rt2x00dev, NULL, i);
4394 rt2800_delete_wcid_attr(rt2x00dev, i);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004395 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
4396 }
4397
4398 /*
4399 * Clear all beacons
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004400 */
Helmut Schaa69cf36a2011-01-30 13:16:03 +01004401 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE0);
4402 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE1);
4403 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE2);
4404 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE3);
4405 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE4);
4406 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE5);
4407 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE6);
4408 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE7);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004409
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01004410 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02004411 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
4412 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
4413 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
RA-Jay Hungc6fcc0e2011-01-30 13:21:22 +01004414 } else if (rt2x00_is_pcie(rt2x00dev)) {
4415 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
4416 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 125);
4417 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004418 }
4419
4420 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
4421 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
4422 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
4423 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
4424 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
4425 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
4426 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
4427 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
4428 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
4429 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
4430
4431 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
4432 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
4433 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
4434 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
4435 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
4436 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
4437 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
4438 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
4439 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
4440 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
4441
4442 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
4443 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
4444 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
4445 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
4446 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
4447 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
4448 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
4449 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
4450 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
4451 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
4452
4453 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
4454 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
4455 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
4456 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
4457 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
4458 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
4459
4460 /*
Helmut Schaa47ee3eb2010-09-08 20:56:04 +02004461 * Do not force the BA window size, we use the TXWI to set it
4462 */
4463 rt2800_register_read(rt2x00dev, AMPDU_BA_WINSIZE, &reg);
4464 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE_ENABLE, 0);
4465 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE, 0);
4466 rt2800_register_write(rt2x00dev, AMPDU_BA_WINSIZE, reg);
4467
4468 /*
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004469 * We must clear the error counters.
4470 * These registers are cleared on read,
4471 * so we may pass a useless variable to store the value.
4472 */
4473 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
4474 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
4475 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
4476 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
4477 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
4478 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
4479
Helmut Schaa9f926fb2010-07-11 12:28:23 +02004480 /*
4481 * Setup leadtime for pre tbtt interrupt to 6ms
4482 */
4483 rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
4484 rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
4485 rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
4486
Helmut Schaa977206d2010-12-13 12:31:58 +01004487 /*
4488 * Set up channel statistics timer
4489 */
4490 rt2800_register_read(rt2x00dev, CH_TIME_CFG, &reg);
4491 rt2x00_set_field32(&reg, CH_TIME_CFG_EIFS_BUSY, 1);
4492 rt2x00_set_field32(&reg, CH_TIME_CFG_NAV_BUSY, 1);
4493 rt2x00_set_field32(&reg, CH_TIME_CFG_RX_BUSY, 1);
4494 rt2x00_set_field32(&reg, CH_TIME_CFG_TX_BUSY, 1);
4495 rt2x00_set_field32(&reg, CH_TIME_CFG_TMR_EN, 1);
4496 rt2800_register_write(rt2x00dev, CH_TIME_CFG, reg);
4497
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004498 return 0;
4499}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004500
4501static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
4502{
4503 unsigned int i;
4504 u32 reg;
4505
4506 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
4507 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
4508 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
4509 return 0;
4510
4511 udelay(REGISTER_BUSY_DELAY);
4512 }
4513
Joe Perchesec9c4982013-04-19 08:33:40 -07004514 rt2x00_err(rt2x00dev, "BBP/RF register access failed, aborting\n");
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004515 return -EACCES;
4516}
4517
4518static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
4519{
4520 unsigned int i;
4521 u8 value;
4522
4523 /*
4524 * BBP was enabled after firmware was loaded,
4525 * but we need to reactivate it now.
4526 */
4527 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
4528 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
4529 msleep(1);
4530
4531 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
4532 rt2800_bbp_read(rt2x00dev, 0, &value);
4533 if ((value != 0xff) && (value != 0x00))
4534 return 0;
4535 udelay(REGISTER_BUSY_DELAY);
4536 }
4537
Joe Perchesec9c4982013-04-19 08:33:40 -07004538 rt2x00_err(rt2x00dev, "BBP register access failed, aborting\n");
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004539 return -EACCES;
4540}
4541
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01004542static void rt2800_bbp4_mac_if_ctrl(struct rt2x00_dev *rt2x00dev)
4543{
4544 u8 value;
4545
4546 rt2800_bbp_read(rt2x00dev, 4, &value);
4547 rt2x00_set_field8(&value, BBP4_MAC_IF_CTRL, 1);
4548 rt2800_bbp_write(rt2x00dev, 4, value);
4549}
4550
Stanislaw Gruszkac2675482013-03-16 19:19:41 +01004551static void rt2800_init_freq_calibration(struct rt2x00_dev *rt2x00dev)
4552{
4553 rt2800_bbp_write(rt2x00dev, 142, 1);
4554 rt2800_bbp_write(rt2x00dev, 143, 57);
4555}
4556
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01004557static void rt2800_init_bbp_5592_glrt(struct rt2x00_dev *rt2x00dev)
4558{
4559 const u8 glrt_table[] = {
4560 0xE0, 0x1F, 0X38, 0x32, 0x08, 0x28, 0x19, 0x0A, 0xFF, 0x00, /* 128 ~ 137 */
4561 0x16, 0x10, 0x10, 0x0B, 0x36, 0x2C, 0x26, 0x24, 0x42, 0x36, /* 138 ~ 147 */
4562 0x30, 0x2D, 0x4C, 0x46, 0x3D, 0x40, 0x3E, 0x42, 0x3D, 0x40, /* 148 ~ 157 */
4563 0X3C, 0x34, 0x2C, 0x2F, 0x3C, 0x35, 0x2E, 0x2A, 0x49, 0x41, /* 158 ~ 167 */
4564 0x36, 0x31, 0x30, 0x30, 0x0E, 0x0D, 0x28, 0x21, 0x1C, 0x16, /* 168 ~ 177 */
4565 0x50, 0x4A, 0x43, 0x40, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, /* 178 ~ 187 */
4566 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 188 ~ 197 */
4567 0x00, 0x00, 0x7D, 0x14, 0x32, 0x2C, 0x36, 0x4C, 0x43, 0x2C, /* 198 ~ 207 */
4568 0x2E, 0x36, 0x30, 0x6E, /* 208 ~ 211 */
4569 };
4570 int i;
4571
4572 for (i = 0; i < ARRAY_SIZE(glrt_table); i++) {
4573 rt2800_bbp_write(rt2x00dev, 195, 128 + i);
4574 rt2800_bbp_write(rt2x00dev, 196, glrt_table[i]);
4575 }
4576};
4577
Gabor Juhos624708b2013-04-19 10:13:52 +02004578static void rt2800_init_bbp_early(struct rt2x00_dev *rt2x00dev)
Stanislaw Gruszkaa4969d02013-03-16 19:19:35 +01004579{
4580 rt2800_bbp_write(rt2x00dev, 65, 0x2C);
4581 rt2800_bbp_write(rt2x00dev, 66, 0x38);
4582 rt2800_bbp_write(rt2x00dev, 68, 0x0B);
4583 rt2800_bbp_write(rt2x00dev, 69, 0x12);
4584 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
4585 rt2800_bbp_write(rt2x00dev, 73, 0x10);
4586 rt2800_bbp_write(rt2x00dev, 81, 0x37);
4587 rt2800_bbp_write(rt2x00dev, 82, 0x62);
4588 rt2800_bbp_write(rt2x00dev, 83, 0x6A);
4589 rt2800_bbp_write(rt2x00dev, 84, 0x99);
4590 rt2800_bbp_write(rt2x00dev, 86, 0x00);
4591 rt2800_bbp_write(rt2x00dev, 91, 0x04);
4592 rt2800_bbp_write(rt2x00dev, 92, 0x00);
4593 rt2800_bbp_write(rt2x00dev, 103, 0x00);
4594 rt2800_bbp_write(rt2x00dev, 105, 0x05);
4595 rt2800_bbp_write(rt2x00dev, 106, 0x35);
4596}
4597
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02004598static void rt2800_disable_unused_dac_adc(struct rt2x00_dev *rt2x00dev)
4599{
4600 u16 eeprom;
4601 u8 value;
4602
4603 rt2800_bbp_read(rt2x00dev, 138, &value);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02004604 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02004605 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
4606 value |= 0x20;
4607 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
4608 value &= ~0x02;
4609 rt2800_bbp_write(rt2x00dev, 138, value);
4610}
4611
Stanislaw Gruszkadae62952013-05-18 14:03:26 +02004612static void rt2800_init_bbp_305x_soc(struct rt2x00_dev *rt2x00dev)
4613{
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02004614 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02004615
4616 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
4617 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02004618
4619 rt2800_bbp_write(rt2x00dev, 69, 0x12);
4620 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02004621
4622 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02004623
4624 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
4625 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02004626
4627 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02004628
4629 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02004630
4631 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02004632
4633 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02004634
4635 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02004636
4637 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02004638
4639 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02004640
4641 rt2800_bbp_write(rt2x00dev, 105, 0x01);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02004642
4643 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszkadae62952013-05-18 14:03:26 +02004644}
4645
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02004646static void rt2800_init_bbp_28xx(struct rt2x00_dev *rt2x00dev)
4647{
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02004648 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
4649 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02004650
4651 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
4652 rt2800_bbp_write(rt2x00dev, 69, 0x16);
4653 rt2800_bbp_write(rt2x00dev, 73, 0x12);
4654 } else {
4655 rt2800_bbp_write(rt2x00dev, 69, 0x12);
4656 rt2800_bbp_write(rt2x00dev, 73, 0x10);
4657 }
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02004658
4659 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02004660
4661 rt2800_bbp_write(rt2x00dev, 81, 0x37);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02004662
4663 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02004664
4665 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02004666
4667 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
4668 rt2800_bbp_write(rt2x00dev, 84, 0x19);
4669 else
4670 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02004671
4672 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02004673
4674 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02004675
4676 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02004677
4678 rt2800_bbp_write(rt2x00dev, 103, 0x00);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02004679
4680 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02004681
4682 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02004683}
4684
4685static void rt2800_init_bbp_30xx(struct rt2x00_dev *rt2x00dev)
4686{
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02004687 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
4688 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02004689
4690 rt2800_bbp_write(rt2x00dev, 69, 0x12);
4691 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02004692
4693 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02004694
4695 rt2800_bbp_write(rt2x00dev, 79, 0x13);
4696 rt2800_bbp_write(rt2x00dev, 80, 0x05);
4697 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02004698
4699 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02004700
4701 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02004702
4703 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02004704
4705 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02004706
4707 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02004708
4709 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02004710
4711 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
4712 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
4713 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E))
4714 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
4715 else
4716 rt2800_bbp_write(rt2x00dev, 103, 0x00);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02004717
4718 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02004719
4720 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02004721
4722 if (rt2x00_rt(rt2x00dev, RT3071) ||
4723 rt2x00_rt(rt2x00dev, RT3090))
4724 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02004725}
4726
4727static void rt2800_init_bbp_3290(struct rt2x00_dev *rt2x00dev)
4728{
Stanislaw Gruszka6addb242013-05-18 14:03:54 +02004729 u8 value;
4730
Stanislaw Gruszkac3223572013-05-18 14:03:28 +02004731 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02004732
4733 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02004734
4735 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
4736 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka59dcabb2013-05-18 14:03:32 +02004737
4738 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02004739
4740 rt2800_bbp_write(rt2x00dev, 69, 0x12);
4741 rt2800_bbp_write(rt2x00dev, 73, 0x13);
4742 rt2800_bbp_write(rt2x00dev, 75, 0x46);
4743 rt2800_bbp_write(rt2x00dev, 76, 0x28);
4744
4745 rt2800_bbp_write(rt2x00dev, 77, 0x58);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02004746
4747 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02004748
4749 rt2800_bbp_write(rt2x00dev, 74, 0x0b);
4750 rt2800_bbp_write(rt2x00dev, 79, 0x18);
4751 rt2800_bbp_write(rt2x00dev, 80, 0x09);
4752 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02004753
4754 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02004755
4756 rt2800_bbp_write(rt2x00dev, 83, 0x7a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02004757
4758 rt2800_bbp_write(rt2x00dev, 84, 0x9a);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02004759
4760 rt2800_bbp_write(rt2x00dev, 86, 0x38);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02004761
4762 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02004763
4764 rt2800_bbp_write(rt2x00dev, 92, 0x02);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02004765
4766 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka1ad44082013-05-18 14:03:45 +02004767
4768 rt2800_bbp_write(rt2x00dev, 104, 0x92);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02004769
4770 rt2800_bbp_write(rt2x00dev, 105, 0x1c);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02004771
4772 rt2800_bbp_write(rt2x00dev, 106, 0x03);
Stanislaw Gruszkaf2b67772013-05-18 14:03:49 +02004773
4774 rt2800_bbp_write(rt2x00dev, 128, 0x12);
Stanislaw Gruszka6addb242013-05-18 14:03:54 +02004775
4776 rt2800_bbp_write(rt2x00dev, 67, 0x24);
4777 rt2800_bbp_write(rt2x00dev, 143, 0x04);
4778 rt2800_bbp_write(rt2x00dev, 142, 0x99);
4779 rt2800_bbp_write(rt2x00dev, 150, 0x30);
4780 rt2800_bbp_write(rt2x00dev, 151, 0x2e);
4781 rt2800_bbp_write(rt2x00dev, 152, 0x20);
4782 rt2800_bbp_write(rt2x00dev, 153, 0x34);
4783 rt2800_bbp_write(rt2x00dev, 154, 0x40);
4784 rt2800_bbp_write(rt2x00dev, 155, 0x3b);
4785 rt2800_bbp_write(rt2x00dev, 253, 0x04);
4786
4787 rt2800_bbp_read(rt2x00dev, 47, &value);
4788 rt2x00_set_field8(&value, BBP47_TSSI_ADC6, 1);
4789 rt2800_bbp_write(rt2x00dev, 47, value);
4790
4791 /* Use 5-bit ADC for Acquisition and 8-bit ADC for data */
4792 rt2800_bbp_read(rt2x00dev, 3, &value);
4793 rt2x00_set_field8(&value, BBP3_ADC_MODE_SWITCH, 1);
4794 rt2x00_set_field8(&value, BBP3_ADC_INIT_MODE, 1);
4795 rt2800_bbp_write(rt2x00dev, 3, value);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02004796}
4797
4798static void rt2800_init_bbp_3352(struct rt2x00_dev *rt2x00dev)
4799{
Stanislaw Gruszka29f3a582013-05-18 14:03:27 +02004800 rt2800_bbp_write(rt2x00dev, 3, 0x00);
4801 rt2800_bbp_write(rt2x00dev, 4, 0x50);
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02004802
4803 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszka3420f792013-05-18 14:03:30 +02004804
4805 rt2800_bbp_write(rt2x00dev, 47, 0x48);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02004806
4807 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
4808 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka59dcabb2013-05-18 14:03:32 +02004809
4810 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02004811
4812 rt2800_bbp_write(rt2x00dev, 69, 0x12);
4813 rt2800_bbp_write(rt2x00dev, 73, 0x13);
4814 rt2800_bbp_write(rt2x00dev, 75, 0x46);
4815 rt2800_bbp_write(rt2x00dev, 76, 0x28);
4816
4817 rt2800_bbp_write(rt2x00dev, 77, 0x59);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02004818
4819 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02004820
4821 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
4822 rt2800_bbp_write(rt2x00dev, 80, 0x08);
4823 rt2800_bbp_write(rt2x00dev, 81, 0x37);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02004824
4825 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02004826
4827 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02004828
4829 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02004830
4831 rt2800_bbp_write(rt2x00dev, 86, 0x38);
Stanislaw Gruszka9400fa82013-05-18 14:03:40 +02004832
4833 rt2800_bbp_write(rt2x00dev, 88, 0x90);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02004834
4835 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02004836
4837 rt2800_bbp_write(rt2x00dev, 92, 0x02);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02004838
4839 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka1ad44082013-05-18 14:03:45 +02004840
4841 rt2800_bbp_write(rt2x00dev, 104, 0x92);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02004842
4843 rt2800_bbp_write(rt2x00dev, 105, 0x34);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02004844
4845 rt2800_bbp_write(rt2x00dev, 106, 0x05);
Stanislaw Gruszka46b90d32013-05-18 14:03:48 +02004846
4847 rt2800_bbp_write(rt2x00dev, 120, 0x50);
Stanislaw Gruszkab7feb9b2013-05-18 14:03:51 +02004848
4849 rt2800_bbp_write(rt2x00dev, 137, 0x0f);
Stanislaw Gruszkac2da5272013-05-18 14:03:53 +02004850
4851 rt2800_bbp_write(rt2x00dev, 163, 0xbd);
4852 /* Set ITxBF timeout to 0x9c40=1000msec */
4853 rt2800_bbp_write(rt2x00dev, 179, 0x02);
4854 rt2800_bbp_write(rt2x00dev, 180, 0x00);
4855 rt2800_bbp_write(rt2x00dev, 182, 0x40);
4856 rt2800_bbp_write(rt2x00dev, 180, 0x01);
4857 rt2800_bbp_write(rt2x00dev, 182, 0x9c);
4858 rt2800_bbp_write(rt2x00dev, 179, 0x00);
4859 /* Reprogram the inband interface to put right values in RXWI */
4860 rt2800_bbp_write(rt2x00dev, 142, 0x04);
4861 rt2800_bbp_write(rt2x00dev, 143, 0x3b);
4862 rt2800_bbp_write(rt2x00dev, 142, 0x06);
4863 rt2800_bbp_write(rt2x00dev, 143, 0xa0);
4864 rt2800_bbp_write(rt2x00dev, 142, 0x07);
4865 rt2800_bbp_write(rt2x00dev, 143, 0xa1);
4866 rt2800_bbp_write(rt2x00dev, 142, 0x08);
4867 rt2800_bbp_write(rt2x00dev, 143, 0xa2);
4868
4869 rt2800_bbp_write(rt2x00dev, 148, 0xc8);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02004870}
4871
4872static void rt2800_init_bbp_3390(struct rt2x00_dev *rt2x00dev)
4873{
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02004874 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
4875 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02004876
4877 rt2800_bbp_write(rt2x00dev, 69, 0x12);
4878 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02004879
4880 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02004881
4882 rt2800_bbp_write(rt2x00dev, 79, 0x13);
4883 rt2800_bbp_write(rt2x00dev, 80, 0x05);
4884 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02004885
4886 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02004887
4888 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02004889
4890 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02004891
4892 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02004893
4894 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02004895
4896 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02004897
4898 if (rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E))
4899 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
4900 else
4901 rt2800_bbp_write(rt2x00dev, 103, 0x00);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02004902
4903 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02004904
4905 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02004906
4907 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02004908}
4909
4910static void rt2800_init_bbp_3572(struct rt2x00_dev *rt2x00dev)
4911{
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02004912 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02004913
4914 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
4915 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02004916
4917 rt2800_bbp_write(rt2x00dev, 69, 0x12);
4918 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02004919
4920 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02004921
4922 rt2800_bbp_write(rt2x00dev, 79, 0x13);
4923 rt2800_bbp_write(rt2x00dev, 80, 0x05);
4924 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02004925
4926 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02004927
4928 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02004929
4930 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02004931
4932 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02004933
4934 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02004935
4936 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02004937
4938 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02004939
4940 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02004941
4942 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02004943
4944 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02004945}
4946
Gabor Juhosb189a182013-07-08 16:08:17 +02004947static void rt2800_init_bbp_3593(struct rt2x00_dev *rt2x00dev)
4948{
4949 rt2800_init_bbp_early(rt2x00dev);
4950
4951 rt2800_bbp_write(rt2x00dev, 79, 0x13);
4952 rt2800_bbp_write(rt2x00dev, 80, 0x05);
4953 rt2800_bbp_write(rt2x00dev, 81, 0x33);
4954 rt2800_bbp_write(rt2x00dev, 137, 0x0f);
4955
4956 rt2800_bbp_write(rt2x00dev, 84, 0x19);
4957
4958 /* Enable DC filter */
4959 if (rt2x00_rt_rev_gte(rt2x00dev, RT3593, REV_RT3593E))
4960 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
4961}
4962
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02004963static void rt2800_init_bbp_53xx(struct rt2x00_dev *rt2x00dev)
4964{
Stanislaw Gruszka32ef8f42013-05-18 14:03:55 +02004965 int ant, div_mode;
4966 u16 eeprom;
4967 u8 value;
4968
Stanislaw Gruszkac3223572013-05-18 14:03:28 +02004969 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02004970
4971 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02004972
4973 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
4974 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka59dcabb2013-05-18 14:03:32 +02004975
4976 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02004977
4978 rt2800_bbp_write(rt2x00dev, 69, 0x12);
4979 rt2800_bbp_write(rt2x00dev, 73, 0x13);
4980 rt2800_bbp_write(rt2x00dev, 75, 0x46);
4981 rt2800_bbp_write(rt2x00dev, 76, 0x28);
4982
4983 rt2800_bbp_write(rt2x00dev, 77, 0x59);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02004984
4985 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02004986
4987 rt2800_bbp_write(rt2x00dev, 79, 0x13);
4988 rt2800_bbp_write(rt2x00dev, 80, 0x05);
4989 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02004990
4991 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02004992
4993 rt2800_bbp_write(rt2x00dev, 83, 0x7a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02004994
4995 rt2800_bbp_write(rt2x00dev, 84, 0x9a);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02004996
4997 rt2800_bbp_write(rt2x00dev, 86, 0x38);
Stanislaw Gruszka9400fa82013-05-18 14:03:40 +02004998
4999 if (rt2x00_rt(rt2x00dev, RT5392))
5000 rt2800_bbp_write(rt2x00dev, 88, 0x90);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005001
5002 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005003
5004 rt2800_bbp_write(rt2x00dev, 92, 0x02);
Stanislaw Gruszka90fed532013-05-18 14:03:43 +02005005
5006 if (rt2x00_rt(rt2x00dev, RT5392)) {
5007 rt2800_bbp_write(rt2x00dev, 95, 0x9a);
5008 rt2800_bbp_write(rt2x00dev, 98, 0x12);
5009 }
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005010
5011 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka1ad44082013-05-18 14:03:45 +02005012
5013 rt2800_bbp_write(rt2x00dev, 104, 0x92);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005014
5015 rt2800_bbp_write(rt2x00dev, 105, 0x3c);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005016
5017 if (rt2x00_rt(rt2x00dev, RT5390))
5018 rt2800_bbp_write(rt2x00dev, 106, 0x03);
5019 else if (rt2x00_rt(rt2x00dev, RT5392))
5020 rt2800_bbp_write(rt2x00dev, 106, 0x12);
5021 else
5022 WARN_ON(1);
Stanislaw Gruszkaf2b67772013-05-18 14:03:49 +02005023
5024 rt2800_bbp_write(rt2x00dev, 128, 0x12);
Stanislaw Gruszka72917142013-05-18 14:03:50 +02005025
5026 if (rt2x00_rt(rt2x00dev, RT5392)) {
5027 rt2800_bbp_write(rt2x00dev, 134, 0xd0);
5028 rt2800_bbp_write(rt2x00dev, 135, 0xf6);
5029 }
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005030
5031 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka32ef8f42013-05-18 14:03:55 +02005032
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005033 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
Stanislaw Gruszka32ef8f42013-05-18 14:03:55 +02005034 div_mode = rt2x00_get_field16(eeprom,
5035 EEPROM_NIC_CONF1_ANT_DIVERSITY);
5036 ant = (div_mode == 3) ? 1 : 0;
5037
5038 /* check if this is a Bluetooth combo card */
5039 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
5040 u32 reg;
5041
5042 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
5043 rt2x00_set_field32(&reg, GPIO_CTRL_DIR3, 0);
5044 rt2x00_set_field32(&reg, GPIO_CTRL_DIR6, 0);
5045 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, 0);
5046 rt2x00_set_field32(&reg, GPIO_CTRL_VAL6, 0);
5047 if (ant == 0)
5048 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, 1);
5049 else if (ant == 1)
5050 rt2x00_set_field32(&reg, GPIO_CTRL_VAL6, 1);
5051 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
5052 }
5053
5054 /* This chip has hardware antenna diversity*/
5055 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390R)) {
5056 rt2800_bbp_write(rt2x00dev, 150, 0); /* Disable Antenna Software OFDM */
5057 rt2800_bbp_write(rt2x00dev, 151, 0); /* Disable Antenna Software CCK */
5058 rt2800_bbp_write(rt2x00dev, 154, 0); /* Clear previously selected antenna */
5059 }
5060
5061 rt2800_bbp_read(rt2x00dev, 152, &value);
5062 if (ant == 0)
5063 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
5064 else
5065 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
5066 rt2800_bbp_write(rt2x00dev, 152, value);
5067
5068 rt2800_init_freq_calibration(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005069}
5070
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005071static void rt2800_init_bbp_5592(struct rt2x00_dev *rt2x00dev)
5072{
5073 int ant, div_mode;
5074 u16 eeprom;
5075 u8 value;
5076
Gabor Juhos624708b2013-04-19 10:13:52 +02005077 rt2800_init_bbp_early(rt2x00dev);
Stanislaw Gruszkaa4969d02013-03-16 19:19:35 +01005078
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005079 rt2800_bbp_read(rt2x00dev, 105, &value);
5080 rt2x00_set_field8(&value, BBP105_MLD,
5081 rt2x00dev->default_ant.rx_chain_num == 2);
5082 rt2800_bbp_write(rt2x00dev, 105, value);
5083
5084 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
5085
5086 rt2800_bbp_write(rt2x00dev, 20, 0x06);
5087 rt2800_bbp_write(rt2x00dev, 31, 0x08);
5088 rt2800_bbp_write(rt2x00dev, 65, 0x2C);
5089 rt2800_bbp_write(rt2x00dev, 68, 0xDD);
5090 rt2800_bbp_write(rt2x00dev, 69, 0x1A);
5091 rt2800_bbp_write(rt2x00dev, 70, 0x05);
5092 rt2800_bbp_write(rt2x00dev, 73, 0x13);
5093 rt2800_bbp_write(rt2x00dev, 74, 0x0F);
5094 rt2800_bbp_write(rt2x00dev, 75, 0x4F);
5095 rt2800_bbp_write(rt2x00dev, 76, 0x28);
5096 rt2800_bbp_write(rt2x00dev, 77, 0x59);
5097 rt2800_bbp_write(rt2x00dev, 84, 0x9A);
5098 rt2800_bbp_write(rt2x00dev, 86, 0x38);
5099 rt2800_bbp_write(rt2x00dev, 88, 0x90);
5100 rt2800_bbp_write(rt2x00dev, 91, 0x04);
5101 rt2800_bbp_write(rt2x00dev, 92, 0x02);
5102 rt2800_bbp_write(rt2x00dev, 95, 0x9a);
5103 rt2800_bbp_write(rt2x00dev, 98, 0x12);
5104 rt2800_bbp_write(rt2x00dev, 103, 0xC0);
5105 rt2800_bbp_write(rt2x00dev, 104, 0x92);
5106 /* FIXME BBP105 owerwrite */
5107 rt2800_bbp_write(rt2x00dev, 105, 0x3C);
5108 rt2800_bbp_write(rt2x00dev, 106, 0x35);
5109 rt2800_bbp_write(rt2x00dev, 128, 0x12);
5110 rt2800_bbp_write(rt2x00dev, 134, 0xD0);
5111 rt2800_bbp_write(rt2x00dev, 135, 0xF6);
5112 rt2800_bbp_write(rt2x00dev, 137, 0x0F);
5113
5114 /* Initialize GLRT (Generalized Likehood Radio Test) */
5115 rt2800_init_bbp_5592_glrt(rt2x00dev);
5116
5117 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
5118
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005119 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005120 div_mode = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_ANT_DIVERSITY);
5121 ant = (div_mode == 3) ? 1 : 0;
5122 rt2800_bbp_read(rt2x00dev, 152, &value);
5123 if (ant == 0) {
5124 /* Main antenna */
5125 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
5126 } else {
5127 /* Auxiliary antenna */
5128 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
5129 }
5130 rt2800_bbp_write(rt2x00dev, 152, value);
5131
5132 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C)) {
5133 rt2800_bbp_read(rt2x00dev, 254, &value);
5134 rt2x00_set_field8(&value, BBP254_BIT7, 1);
5135 rt2800_bbp_write(rt2x00dev, 254, value);
5136 }
5137
Stanislaw Gruszkac2675482013-03-16 19:19:41 +01005138 rt2800_init_freq_calibration(rt2x00dev);
5139
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005140 rt2800_bbp_write(rt2x00dev, 84, 0x19);
Stanislaw Gruszka6e04f252013-03-16 19:19:38 +01005141 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C))
5142 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005143}
5144
Stanislaw Gruszkaa1ef50392013-05-18 14:03:24 +02005145static void rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005146{
5147 unsigned int i;
5148 u16 eeprom;
5149 u8 reg_id;
5150 u8 value;
5151
Stanislaw Gruszkadae62952013-05-18 14:03:26 +02005152 if (rt2800_is_305x_soc(rt2x00dev))
5153 rt2800_init_bbp_305x_soc(rt2x00dev);
5154
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005155 switch (rt2x00dev->chip.rt) {
5156 case RT2860:
5157 case RT2872:
5158 case RT2883:
5159 rt2800_init_bbp_28xx(rt2x00dev);
5160 break;
5161 case RT3070:
5162 case RT3071:
5163 case RT3090:
5164 rt2800_init_bbp_30xx(rt2x00dev);
5165 break;
5166 case RT3290:
5167 rt2800_init_bbp_3290(rt2x00dev);
5168 break;
5169 case RT3352:
5170 rt2800_init_bbp_3352(rt2x00dev);
5171 break;
5172 case RT3390:
5173 rt2800_init_bbp_3390(rt2x00dev);
5174 break;
5175 case RT3572:
5176 rt2800_init_bbp_3572(rt2x00dev);
5177 break;
Gabor Juhosb189a182013-07-08 16:08:17 +02005178 case RT3593:
5179 rt2800_init_bbp_3593(rt2x00dev);
5180 return;
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005181 case RT5390:
5182 case RT5392:
5183 rt2800_init_bbp_53xx(rt2x00dev);
5184 break;
5185 case RT5592:
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005186 rt2800_init_bbp_5592(rt2x00dev);
Stanislaw Gruszkaa1ef50392013-05-18 14:03:24 +02005187 return;
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005188 }
5189
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005190 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
Gabor Juhos022138c2013-07-08 11:25:54 +02005191 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_BBP_START, i,
5192 &eeprom);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005193
5194 if (eeprom != 0xffff && eeprom != 0x0000) {
5195 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
5196 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
5197 rt2800_bbp_write(rt2x00dev, reg_id, value);
5198 }
5199 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005200}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005201
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02005202static void rt2800_led_open_drain_enable(struct rt2x00_dev *rt2x00dev)
5203{
5204 u32 reg;
5205
5206 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
5207 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
5208 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
5209}
5210
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005211static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev, bool bw40,
5212 u8 filter_target)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005213{
5214 unsigned int i;
5215 u8 bbp;
5216 u8 rfcsr;
5217 u8 passband;
5218 u8 stopband;
5219 u8 overtuned = 0;
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005220 u8 rfcsr24 = (bw40) ? 0x27 : 0x07;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005221
5222 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
5223
5224 rt2800_bbp_read(rt2x00dev, 4, &bbp);
5225 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
5226 rt2800_bbp_write(rt2x00dev, 4, bbp);
5227
RA-Jay Hung80d184e2011-01-10 11:28:10 +01005228 rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
5229 rt2x00_set_field8(&rfcsr, RFCSR31_RX_H20M, bw40);
5230 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
5231
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005232 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
5233 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
5234 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
5235
5236 /*
5237 * Set power & frequency of passband test tone
5238 */
5239 rt2800_bbp_write(rt2x00dev, 24, 0);
5240
5241 for (i = 0; i < 100; i++) {
5242 rt2800_bbp_write(rt2x00dev, 25, 0x90);
5243 msleep(1);
5244
5245 rt2800_bbp_read(rt2x00dev, 55, &passband);
5246 if (passband)
5247 break;
5248 }
5249
5250 /*
5251 * Set power & frequency of stopband test tone
5252 */
5253 rt2800_bbp_write(rt2x00dev, 24, 0x06);
5254
5255 for (i = 0; i < 100; i++) {
5256 rt2800_bbp_write(rt2x00dev, 25, 0x90);
5257 msleep(1);
5258
5259 rt2800_bbp_read(rt2x00dev, 55, &stopband);
5260
5261 if ((passband - stopband) <= filter_target) {
5262 rfcsr24++;
5263 overtuned += ((passband - stopband) == filter_target);
5264 } else
5265 break;
5266
5267 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
5268 }
5269
5270 rfcsr24 -= !!overtuned;
5271
5272 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
5273 return rfcsr24;
5274}
5275
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005276static void rt2800_rf_init_calibration(struct rt2x00_dev *rt2x00dev,
5277 const unsigned int rf_reg)
5278{
5279 u8 rfcsr;
5280
5281 rt2800_rfcsr_read(rt2x00dev, rf_reg, &rfcsr);
5282 rt2x00_set_field8(&rfcsr, FIELD8(0x80), 1);
5283 rt2800_rfcsr_write(rt2x00dev, rf_reg, rfcsr);
5284 msleep(1);
5285 rt2x00_set_field8(&rfcsr, FIELD8(0x80), 0);
5286 rt2800_rfcsr_write(rt2x00dev, rf_reg, rfcsr);
5287}
5288
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005289static void rt2800_rx_filter_calibration(struct rt2x00_dev *rt2x00dev)
5290{
5291 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
5292 u8 filter_tgt_bw20;
5293 u8 filter_tgt_bw40;
5294 u8 rfcsr, bbp;
5295
5296 /*
5297 * TODO: sync filter_tgt values with vendor driver
5298 */
5299 if (rt2x00_rt(rt2x00dev, RT3070)) {
5300 filter_tgt_bw20 = 0x16;
5301 filter_tgt_bw40 = 0x19;
5302 } else {
5303 filter_tgt_bw20 = 0x13;
5304 filter_tgt_bw40 = 0x15;
5305 }
5306
5307 drv_data->calibration_bw20 =
5308 rt2800_init_rx_filter(rt2x00dev, false, filter_tgt_bw20);
5309 drv_data->calibration_bw40 =
5310 rt2800_init_rx_filter(rt2x00dev, true, filter_tgt_bw40);
5311
5312 /*
5313 * Save BBP 25 & 26 values for later use in channel switching (for 3052)
5314 */
5315 rt2800_bbp_read(rt2x00dev, 25, &drv_data->bbp25);
5316 rt2800_bbp_read(rt2x00dev, 26, &drv_data->bbp26);
5317
5318 /*
5319 * Set back to initial state
5320 */
5321 rt2800_bbp_write(rt2x00dev, 24, 0);
5322
5323 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
5324 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
5325 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
5326
5327 /*
5328 * Set BBP back to BW20
5329 */
5330 rt2800_bbp_read(rt2x00dev, 4, &bbp);
5331 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
5332 rt2800_bbp_write(rt2x00dev, 4, bbp);
5333}
5334
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005335static void rt2800_normal_mode_setup_3xxx(struct rt2x00_dev *rt2x00dev)
5336{
5337 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
5338 u8 min_gain, rfcsr, bbp;
5339 u16 eeprom;
5340
5341 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
5342
5343 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
5344 if (rt2x00_rt(rt2x00dev, RT3070) ||
5345 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
5346 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
5347 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
5348 if (!test_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags))
5349 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
5350 }
5351
5352 min_gain = rt2x00_rt(rt2x00dev, RT3070) ? 1 : 2;
5353 if (drv_data->txmixer_gain_24g >= min_gain) {
5354 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
5355 drv_data->txmixer_gain_24g);
5356 }
5357
5358 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
5359
5360 if (rt2x00_rt(rt2x00dev, RT3090)) {
5361 /* Turn off unused DAC1 and ADC1 to reduce power consumption */
5362 rt2800_bbp_read(rt2x00dev, 138, &bbp);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005363 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005364 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
5365 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
5366 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
5367 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
5368 rt2800_bbp_write(rt2x00dev, 138, bbp);
5369 }
5370
5371 if (rt2x00_rt(rt2x00dev, RT3070)) {
5372 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
5373 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F))
5374 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
5375 else
5376 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
5377 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
5378 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
5379 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
5380 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
5381 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
5382 rt2x00_rt(rt2x00dev, RT3090) ||
5383 rt2x00_rt(rt2x00dev, RT3390)) {
5384 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
5385 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
5386 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
5387 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
5388 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
5389 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
5390 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
5391
5392 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
5393 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
5394 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
5395
5396 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
5397 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
5398 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
5399
5400 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
5401 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
5402 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
5403 }
5404}
5405
Gabor Juhosab7078a2013-07-08 16:08:18 +02005406static void rt2800_normal_mode_setup_3593(struct rt2x00_dev *rt2x00dev)
5407{
5408 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
5409 u8 rfcsr;
5410 u8 tx_gain;
5411
5412 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
5413 rt2x00_set_field8(&rfcsr, RFCSR50_TX_LO2_EN, 0);
5414 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
5415
5416 rt2800_rfcsr_read(rt2x00dev, 51, &rfcsr);
5417 tx_gain = rt2x00_get_field8(drv_data->txmixer_gain_24g,
5418 RFCSR17_TXMIXER_GAIN);
5419 rt2x00_set_field8(&rfcsr, RFCSR51_BITS24, tx_gain);
5420 rt2800_rfcsr_write(rt2x00dev, 51, rfcsr);
5421
5422 rt2800_rfcsr_read(rt2x00dev, 38, &rfcsr);
5423 rt2x00_set_field8(&rfcsr, RFCSR38_RX_LO1_EN, 0);
5424 rt2800_rfcsr_write(rt2x00dev, 38, rfcsr);
5425
5426 rt2800_rfcsr_read(rt2x00dev, 39, &rfcsr);
5427 rt2x00_set_field8(&rfcsr, RFCSR39_RX_LO2_EN, 0);
5428 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
5429
5430 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
5431 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
5432 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
5433 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
5434
5435 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
5436 rt2x00_set_field8(&rfcsr, RFCSR30_RX_VCM, 2);
5437 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
5438
5439 /* TODO: enable stream mode */
5440}
5441
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02005442static void rt2800_normal_mode_setup_5xxx(struct rt2x00_dev *rt2x00dev)
5443{
5444 u8 reg;
5445 u16 eeprom;
5446
5447 /* Turn off unused DAC1 and ADC1 to reduce power consumption */
5448 rt2800_bbp_read(rt2x00dev, 138, &reg);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005449 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02005450 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
5451 rt2x00_set_field8(&reg, BBP138_RX_ADC1, 0);
5452 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
5453 rt2x00_set_field8(&reg, BBP138_TX_DAC1, 1);
5454 rt2800_bbp_write(rt2x00dev, 138, reg);
5455
5456 rt2800_rfcsr_read(rt2x00dev, 38, &reg);
5457 rt2x00_set_field8(&reg, RFCSR38_RX_LO1_EN, 0);
5458 rt2800_rfcsr_write(rt2x00dev, 38, reg);
5459
5460 rt2800_rfcsr_read(rt2x00dev, 39, &reg);
5461 rt2x00_set_field8(&reg, RFCSR39_RX_LO2_EN, 0);
5462 rt2800_rfcsr_write(rt2x00dev, 39, reg);
5463
5464 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
5465
5466 rt2800_rfcsr_read(rt2x00dev, 30, &reg);
5467 rt2x00_set_field8(&reg, RFCSR30_RX_VCM, 2);
5468 rt2800_rfcsr_write(rt2x00dev, 30, reg);
5469}
5470
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005471static void rt2800_init_rfcsr_305x_soc(struct rt2x00_dev *rt2x00dev)
5472{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005473 rt2800_rf_init_calibration(rt2x00dev, 30);
5474
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005475 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
5476 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
5477 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
5478 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
5479 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
5480 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
5481 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
5482 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
5483 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
5484 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
5485 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
5486 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
5487 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
5488 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
5489 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
5490 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
5491 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
5492 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
5493 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
5494 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
5495 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
5496 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
5497 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
5498 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
5499 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
5500 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
5501 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
5502 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
5503 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
5504 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
5505 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
5506 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
5507}
5508
5509static void rt2800_init_rfcsr_30xx(struct rt2x00_dev *rt2x00dev)
5510{
Stanislaw Gruszkac9a221b2013-04-17 14:08:13 +02005511 u8 rfcsr;
5512 u16 eeprom;
5513 u32 reg;
5514
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005515 /* XXX vendor driver do this only for 3070 */
5516 rt2800_rf_init_calibration(rt2x00dev, 30);
5517
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005518 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
5519 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
5520 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
5521 rt2800_rfcsr_write(rt2x00dev, 7, 0x60);
5522 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
5523 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
5524 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
5525 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
5526 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
5527 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
5528 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
5529 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
5530 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
5531 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
5532 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
5533 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
5534 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
5535 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
5536 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Stanislaw Gruszkac9a221b2013-04-17 14:08:13 +02005537
5538 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
5539 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
5540 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
5541 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
5542 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
5543 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
5544 rt2x00_rt(rt2x00dev, RT3090)) {
5545 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
5546
5547 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
5548 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
5549 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
5550
5551 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
5552 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
5553 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
5554 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005555 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1,
5556 &eeprom);
Stanislaw Gruszkac9a221b2013-04-17 14:08:13 +02005557 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
5558 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
5559 else
5560 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
5561 }
5562 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
5563
5564 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
5565 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
5566 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
5567 }
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005568
5569 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszka5de5a1f2013-04-17 14:08:17 +02005570
5571 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
5572 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
5573 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E))
5574 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02005575
5576 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005577 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005578}
5579
5580static void rt2800_init_rfcsr_3290(struct rt2x00_dev *rt2x00dev)
5581{
Stanislaw Gruszkaf9cdcbb2013-04-17 14:08:12 +02005582 u8 rfcsr;
5583
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005584 rt2800_rf_init_calibration(rt2x00dev, 2);
5585
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005586 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
5587 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
5588 rt2800_rfcsr_write(rt2x00dev, 3, 0x08);
5589 rt2800_rfcsr_write(rt2x00dev, 4, 0x00);
5590 rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
5591 rt2800_rfcsr_write(rt2x00dev, 8, 0xf3);
5592 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
5593 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
5594 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
5595 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
5596 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
5597 rt2800_rfcsr_write(rt2x00dev, 18, 0x02);
5598 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
5599 rt2800_rfcsr_write(rt2x00dev, 25, 0x83);
5600 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
5601 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
5602 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
5603 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
5604 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
5605 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
5606 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
5607 rt2800_rfcsr_write(rt2x00dev, 34, 0x05);
5608 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
5609 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
5610 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
5611 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
5612 rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
5613 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
5614 rt2800_rfcsr_write(rt2x00dev, 42, 0xd5);
5615 rt2800_rfcsr_write(rt2x00dev, 43, 0x7b);
5616 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
5617 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
5618 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
5619 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
5620 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
5621 rt2800_rfcsr_write(rt2x00dev, 49, 0x98);
5622 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
5623 rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
5624 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
5625 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
5626 rt2800_rfcsr_write(rt2x00dev, 56, 0x02);
5627 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
5628 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
5629 rt2800_rfcsr_write(rt2x00dev, 59, 0x09);
5630 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
5631 rt2800_rfcsr_write(rt2x00dev, 61, 0xc1);
Stanislaw Gruszkaf9cdcbb2013-04-17 14:08:12 +02005632
5633 rt2800_rfcsr_read(rt2x00dev, 29, &rfcsr);
5634 rt2x00_set_field8(&rfcsr, RFCSR29_RSSI_GAIN, 3);
5635 rt2800_rfcsr_write(rt2x00dev, 29, rfcsr);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02005636
5637 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005638 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005639}
5640
5641static void rt2800_init_rfcsr_3352(struct rt2x00_dev *rt2x00dev)
5642{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005643 rt2800_rf_init_calibration(rt2x00dev, 30);
5644
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005645 rt2800_rfcsr_write(rt2x00dev, 0, 0xf0);
5646 rt2800_rfcsr_write(rt2x00dev, 1, 0x23);
5647 rt2800_rfcsr_write(rt2x00dev, 2, 0x50);
5648 rt2800_rfcsr_write(rt2x00dev, 3, 0x18);
5649 rt2800_rfcsr_write(rt2x00dev, 4, 0x00);
5650 rt2800_rfcsr_write(rt2x00dev, 5, 0x00);
5651 rt2800_rfcsr_write(rt2x00dev, 6, 0x33);
5652 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
5653 rt2800_rfcsr_write(rt2x00dev, 8, 0xf1);
5654 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
5655 rt2800_rfcsr_write(rt2x00dev, 10, 0xd2);
5656 rt2800_rfcsr_write(rt2x00dev, 11, 0x42);
5657 rt2800_rfcsr_write(rt2x00dev, 12, 0x1c);
5658 rt2800_rfcsr_write(rt2x00dev, 13, 0x00);
5659 rt2800_rfcsr_write(rt2x00dev, 14, 0x5a);
5660 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
5661 rt2800_rfcsr_write(rt2x00dev, 16, 0x01);
5662 rt2800_rfcsr_write(rt2x00dev, 18, 0x45);
5663 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
5664 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
5665 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
5666 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
5667 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
5668 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
5669 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
5670 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
5671 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
5672 rt2800_rfcsr_write(rt2x00dev, 28, 0x03);
5673 rt2800_rfcsr_write(rt2x00dev, 29, 0x00);
5674 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
5675 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
5676 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
5677 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
5678 rt2800_rfcsr_write(rt2x00dev, 34, 0x01);
5679 rt2800_rfcsr_write(rt2x00dev, 35, 0x03);
5680 rt2800_rfcsr_write(rt2x00dev, 36, 0xbd);
5681 rt2800_rfcsr_write(rt2x00dev, 37, 0x3c);
5682 rt2800_rfcsr_write(rt2x00dev, 38, 0x5f);
5683 rt2800_rfcsr_write(rt2x00dev, 39, 0xc5);
5684 rt2800_rfcsr_write(rt2x00dev, 40, 0x33);
5685 rt2800_rfcsr_write(rt2x00dev, 41, 0x5b);
5686 rt2800_rfcsr_write(rt2x00dev, 42, 0x5b);
5687 rt2800_rfcsr_write(rt2x00dev, 43, 0xdb);
5688 rt2800_rfcsr_write(rt2x00dev, 44, 0xdb);
5689 rt2800_rfcsr_write(rt2x00dev, 45, 0xdb);
5690 rt2800_rfcsr_write(rt2x00dev, 46, 0xdd);
5691 rt2800_rfcsr_write(rt2x00dev, 47, 0x0d);
5692 rt2800_rfcsr_write(rt2x00dev, 48, 0x14);
5693 rt2800_rfcsr_write(rt2x00dev, 49, 0x00);
5694 rt2800_rfcsr_write(rt2x00dev, 50, 0x2d);
5695 rt2800_rfcsr_write(rt2x00dev, 51, 0x7f);
5696 rt2800_rfcsr_write(rt2x00dev, 52, 0x00);
5697 rt2800_rfcsr_write(rt2x00dev, 53, 0x52);
5698 rt2800_rfcsr_write(rt2x00dev, 54, 0x1b);
5699 rt2800_rfcsr_write(rt2x00dev, 55, 0x7f);
5700 rt2800_rfcsr_write(rt2x00dev, 56, 0x00);
5701 rt2800_rfcsr_write(rt2x00dev, 57, 0x52);
5702 rt2800_rfcsr_write(rt2x00dev, 58, 0x1b);
5703 rt2800_rfcsr_write(rt2x00dev, 59, 0x00);
5704 rt2800_rfcsr_write(rt2x00dev, 60, 0x00);
5705 rt2800_rfcsr_write(rt2x00dev, 61, 0x00);
5706 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
5707 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005708
5709 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02005710 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005711 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005712}
5713
5714static void rt2800_init_rfcsr_3390(struct rt2x00_dev *rt2x00dev)
5715{
Stanislaw Gruszka2971e662013-04-17 14:08:14 +02005716 u32 reg;
5717
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005718 rt2800_rf_init_calibration(rt2x00dev, 30);
5719
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005720 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
5721 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
5722 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
5723 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
5724 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
5725 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
5726 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
5727 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
5728 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
5729 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
5730 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
5731 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
5732 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
5733 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
5734 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
5735 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
5736 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
5737 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
5738 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
5739 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
5740 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
5741 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
5742 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
5743 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
5744 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
5745 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
5746 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
5747 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
5748 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
5749 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
5750 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
5751 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Stanislaw Gruszka2971e662013-04-17 14:08:14 +02005752
5753 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
5754 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
5755 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005756
5757 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszka5de5a1f2013-04-17 14:08:17 +02005758
5759 if (rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
5760 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02005761
5762 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005763 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005764}
5765
5766static void rt2800_init_rfcsr_3572(struct rt2x00_dev *rt2x00dev)
5767{
Stanislaw Gruszka87d91db2013-04-17 14:08:15 +02005768 u8 rfcsr;
5769 u32 reg;
5770
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005771 rt2800_rf_init_calibration(rt2x00dev, 30);
5772
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005773 rt2800_rfcsr_write(rt2x00dev, 0, 0x70);
5774 rt2800_rfcsr_write(rt2x00dev, 1, 0x81);
5775 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
5776 rt2800_rfcsr_write(rt2x00dev, 3, 0x02);
5777 rt2800_rfcsr_write(rt2x00dev, 4, 0x4c);
5778 rt2800_rfcsr_write(rt2x00dev, 5, 0x05);
5779 rt2800_rfcsr_write(rt2x00dev, 6, 0x4a);
5780 rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
5781 rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
5782 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
5783 rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
5784 rt2800_rfcsr_write(rt2x00dev, 12, 0x70);
5785 rt2800_rfcsr_write(rt2x00dev, 13, 0x65);
5786 rt2800_rfcsr_write(rt2x00dev, 14, 0xa0);
5787 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
5788 rt2800_rfcsr_write(rt2x00dev, 16, 0x4c);
5789 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
5790 rt2800_rfcsr_write(rt2x00dev, 18, 0xac);
5791 rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
5792 rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
5793 rt2800_rfcsr_write(rt2x00dev, 21, 0xd0);
5794 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
5795 rt2800_rfcsr_write(rt2x00dev, 23, 0x3c);
5796 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
5797 rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
5798 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
5799 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
5800 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
5801 rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
5802 rt2800_rfcsr_write(rt2x00dev, 30, 0x09);
5803 rt2800_rfcsr_write(rt2x00dev, 31, 0x10);
Stanislaw Gruszka87d91db2013-04-17 14:08:15 +02005804
5805 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
5806 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
5807 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
5808
5809 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
5810 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
5811 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
5812 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
5813 msleep(1);
5814 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
5815 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
5816 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
5817 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005818
5819 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02005820 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005821 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005822}
5823
Gabor Juhosd63f7e82013-07-08 16:08:19 +02005824static void rt3593_post_bbp_init(struct rt2x00_dev *rt2x00dev)
5825{
5826 u8 bbp;
5827 bool txbf_enabled = false; /* FIXME */
5828
5829 rt2800_bbp_read(rt2x00dev, 105, &bbp);
5830 if (rt2x00dev->default_ant.rx_chain_num == 1)
5831 rt2x00_set_field8(&bbp, BBP105_MLD, 0);
5832 else
5833 rt2x00_set_field8(&bbp, BBP105_MLD, 1);
5834 rt2800_bbp_write(rt2x00dev, 105, bbp);
5835
5836 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
5837
5838 rt2800_bbp_write(rt2x00dev, 92, 0x02);
5839 rt2800_bbp_write(rt2x00dev, 82, 0x82);
5840 rt2800_bbp_write(rt2x00dev, 106, 0x05);
5841 rt2800_bbp_write(rt2x00dev, 104, 0x92);
5842 rt2800_bbp_write(rt2x00dev, 88, 0x90);
5843 rt2800_bbp_write(rt2x00dev, 148, 0xc8);
5844 rt2800_bbp_write(rt2x00dev, 47, 0x48);
5845 rt2800_bbp_write(rt2x00dev, 120, 0x50);
5846
5847 if (txbf_enabled)
5848 rt2800_bbp_write(rt2x00dev, 163, 0xbd);
5849 else
5850 rt2800_bbp_write(rt2x00dev, 163, 0x9d);
5851
5852 /* SNR mapping */
5853 rt2800_bbp_write(rt2x00dev, 142, 6);
5854 rt2800_bbp_write(rt2x00dev, 143, 160);
5855 rt2800_bbp_write(rt2x00dev, 142, 7);
5856 rt2800_bbp_write(rt2x00dev, 143, 161);
5857 rt2800_bbp_write(rt2x00dev, 142, 8);
5858 rt2800_bbp_write(rt2x00dev, 143, 162);
5859
5860 /* ADC/DAC control */
5861 rt2800_bbp_write(rt2x00dev, 31, 0x08);
5862
5863 /* RX AGC energy lower bound in log2 */
5864 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
5865
5866 /* FIXME: BBP 105 owerwrite? */
5867 rt2800_bbp_write(rt2x00dev, 105, 0x04);
5868}
5869
Gabor Juhosab7078a2013-07-08 16:08:18 +02005870static void rt2800_init_rfcsr_3593(struct rt2x00_dev *rt2x00dev)
5871{
5872 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
5873 u32 reg;
5874 u8 rfcsr;
5875
5876 /* Disable GPIO #4 and #7 function for LAN PE control */
5877 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
5878 rt2x00_set_field32(&reg, GPIO_SWITCH_4, 0);
5879 rt2x00_set_field32(&reg, GPIO_SWITCH_7, 0);
5880 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
5881
5882 /* Initialize default register values */
5883 rt2800_rfcsr_write(rt2x00dev, 1, 0x03);
5884 rt2800_rfcsr_write(rt2x00dev, 3, 0x80);
5885 rt2800_rfcsr_write(rt2x00dev, 5, 0x00);
5886 rt2800_rfcsr_write(rt2x00dev, 6, 0x40);
5887 rt2800_rfcsr_write(rt2x00dev, 8, 0xf1);
5888 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
5889 rt2800_rfcsr_write(rt2x00dev, 10, 0xd3);
5890 rt2800_rfcsr_write(rt2x00dev, 11, 0x40);
5891 rt2800_rfcsr_write(rt2x00dev, 12, 0x4e);
5892 rt2800_rfcsr_write(rt2x00dev, 13, 0x12);
5893 rt2800_rfcsr_write(rt2x00dev, 18, 0x40);
5894 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
5895 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
5896 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
5897 rt2800_rfcsr_write(rt2x00dev, 32, 0x78);
5898 rt2800_rfcsr_write(rt2x00dev, 33, 0x3b);
5899 rt2800_rfcsr_write(rt2x00dev, 34, 0x3c);
5900 rt2800_rfcsr_write(rt2x00dev, 35, 0xe0);
5901 rt2800_rfcsr_write(rt2x00dev, 38, 0x86);
5902 rt2800_rfcsr_write(rt2x00dev, 39, 0x23);
5903 rt2800_rfcsr_write(rt2x00dev, 44, 0xd3);
5904 rt2800_rfcsr_write(rt2x00dev, 45, 0xbb);
5905 rt2800_rfcsr_write(rt2x00dev, 46, 0x60);
5906 rt2800_rfcsr_write(rt2x00dev, 49, 0x8e);
5907 rt2800_rfcsr_write(rt2x00dev, 50, 0x86);
5908 rt2800_rfcsr_write(rt2x00dev, 51, 0x75);
5909 rt2800_rfcsr_write(rt2x00dev, 52, 0x45);
5910 rt2800_rfcsr_write(rt2x00dev, 53, 0x18);
5911 rt2800_rfcsr_write(rt2x00dev, 54, 0x18);
5912 rt2800_rfcsr_write(rt2x00dev, 55, 0x18);
5913 rt2800_rfcsr_write(rt2x00dev, 56, 0xdb);
5914 rt2800_rfcsr_write(rt2x00dev, 57, 0x6e);
5915
5916 /* Initiate calibration */
5917 /* TODO: use rt2800_rf_init_calibration ? */
5918 rt2800_rfcsr_read(rt2x00dev, 2, &rfcsr);
5919 rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 1);
5920 rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
5921
5922 rt2800_adjust_freq_offset(rt2x00dev);
5923
5924 rt2800_rfcsr_read(rt2x00dev, 18, &rfcsr);
5925 rt2x00_set_field8(&rfcsr, RFCSR18_XO_TUNE_BYPASS, 1);
5926 rt2800_rfcsr_write(rt2x00dev, 18, rfcsr);
5927
5928 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
5929 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
5930 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
5931 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
5932 usleep_range(1000, 1500);
5933 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
5934 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
5935 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
5936
5937 /* Set initial values for RX filter calibration */
5938 drv_data->calibration_bw20 = 0x1f;
5939 drv_data->calibration_bw40 = 0x2f;
5940
5941 /* Save BBP 25 & 26 values for later use in channel switching */
5942 rt2800_bbp_read(rt2x00dev, 25, &drv_data->bbp25);
5943 rt2800_bbp_read(rt2x00dev, 26, &drv_data->bbp26);
5944
5945 rt2800_led_open_drain_enable(rt2x00dev);
5946 rt2800_normal_mode_setup_3593(rt2x00dev);
5947
Gabor Juhosd63f7e82013-07-08 16:08:19 +02005948 rt3593_post_bbp_init(rt2x00dev);
Gabor Juhosab7078a2013-07-08 16:08:18 +02005949
5950 /* TODO: enable stream mode support */
5951}
5952
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005953static void rt2800_init_rfcsr_5390(struct rt2x00_dev *rt2x00dev)
5954{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005955 rt2800_rf_init_calibration(rt2x00dev, 2);
5956
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005957 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
5958 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
5959 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
5960 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
5961 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
5962 rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
5963 else
5964 rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
5965 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
5966 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
5967 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
5968 rt2800_rfcsr_write(rt2x00dev, 12, 0xc6);
5969 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
5970 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
5971 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
5972 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
5973 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
5974 rt2800_rfcsr_write(rt2x00dev, 19, 0x00);
5975
5976 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
5977 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
5978 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
5979 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
5980 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
5981 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
5982 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
5983 else
5984 rt2800_rfcsr_write(rt2x00dev, 25, 0xc0);
5985 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
5986 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
5987 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
5988 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
5989
5990 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
5991 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
5992 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
5993 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
5994 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
5995 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
5996 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
5997 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
5998 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
5999 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
6000
6001 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6002 rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
6003 else
6004 rt2800_rfcsr_write(rt2x00dev, 40, 0x4b);
6005 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
6006 rt2800_rfcsr_write(rt2x00dev, 42, 0xd2);
6007 rt2800_rfcsr_write(rt2x00dev, 43, 0x9a);
6008 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
6009 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
6010 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6011 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
6012 else
6013 rt2800_rfcsr_write(rt2x00dev, 46, 0x7b);
6014 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
6015 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
6016 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
6017
6018 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
6019 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6020 rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
6021 else
6022 rt2800_rfcsr_write(rt2x00dev, 53, 0x84);
6023 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
6024 rt2800_rfcsr_write(rt2x00dev, 55, 0x44);
6025 rt2800_rfcsr_write(rt2x00dev, 56, 0x22);
6026 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
6027 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
6028 rt2800_rfcsr_write(rt2x00dev, 59, 0x63);
6029
6030 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
6031 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6032 rt2800_rfcsr_write(rt2x00dev, 61, 0xd1);
6033 else
6034 rt2800_rfcsr_write(rt2x00dev, 61, 0xdd);
6035 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
6036 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02006037
6038 rt2800_normal_mode_setup_5xxx(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006039
6040 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006041}
6042
6043static void rt2800_init_rfcsr_5392(struct rt2x00_dev *rt2x00dev)
6044{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006045 rt2800_rf_init_calibration(rt2x00dev, 2);
6046
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006047 rt2800_rfcsr_write(rt2x00dev, 1, 0x17);
6048 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
6049 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
6050 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
6051 rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
6052 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
6053 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
6054 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
6055 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
6056 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
6057 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
6058 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
6059 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
6060 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
6061 rt2800_rfcsr_write(rt2x00dev, 19, 0x4d);
6062 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
6063 rt2800_rfcsr_write(rt2x00dev, 21, 0x8d);
6064 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
6065 rt2800_rfcsr_write(rt2x00dev, 23, 0x0b);
6066 rt2800_rfcsr_write(rt2x00dev, 24, 0x44);
6067 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
6068 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
6069 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
6070 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
6071 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
6072 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
6073 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
6074 rt2800_rfcsr_write(rt2x00dev, 32, 0x20);
6075 rt2800_rfcsr_write(rt2x00dev, 33, 0xC0);
6076 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
6077 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
6078 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
6079 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
6080 rt2800_rfcsr_write(rt2x00dev, 38, 0x89);
6081 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
6082 rt2800_rfcsr_write(rt2x00dev, 40, 0x0f);
6083 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
6084 rt2800_rfcsr_write(rt2x00dev, 42, 0xd5);
6085 rt2800_rfcsr_write(rt2x00dev, 43, 0x9b);
6086 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
6087 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
6088 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
6089 rt2800_rfcsr_write(rt2x00dev, 47, 0x0c);
6090 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
6091 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
6092 rt2800_rfcsr_write(rt2x00dev, 50, 0x94);
6093 rt2800_rfcsr_write(rt2x00dev, 51, 0x3a);
6094 rt2800_rfcsr_write(rt2x00dev, 52, 0x48);
6095 rt2800_rfcsr_write(rt2x00dev, 53, 0x44);
6096 rt2800_rfcsr_write(rt2x00dev, 54, 0x38);
6097 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
6098 rt2800_rfcsr_write(rt2x00dev, 56, 0xa1);
6099 rt2800_rfcsr_write(rt2x00dev, 57, 0x00);
6100 rt2800_rfcsr_write(rt2x00dev, 58, 0x39);
6101 rt2800_rfcsr_write(rt2x00dev, 59, 0x07);
6102 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
6103 rt2800_rfcsr_write(rt2x00dev, 61, 0x91);
6104 rt2800_rfcsr_write(rt2x00dev, 62, 0x39);
6105 rt2800_rfcsr_write(rt2x00dev, 63, 0x07);
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02006106
6107 rt2800_normal_mode_setup_5xxx(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006108
6109 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006110}
6111
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006112static void rt2800_init_rfcsr_5592(struct rt2x00_dev *rt2x00dev)
6113{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006114 rt2800_rf_init_calibration(rt2x00dev, 30);
6115
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006116 rt2800_rfcsr_write(rt2x00dev, 1, 0x3F);
6117 rt2800_rfcsr_write(rt2x00dev, 3, 0x08);
6118 rt2800_rfcsr_write(rt2x00dev, 3, 0x08);
6119 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
6120 rt2800_rfcsr_write(rt2x00dev, 6, 0xE4);
6121 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
6122 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
6123 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
6124 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
6125 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
6126 rt2800_rfcsr_write(rt2x00dev, 19, 0x4D);
6127 rt2800_rfcsr_write(rt2x00dev, 20, 0x10);
6128 rt2800_rfcsr_write(rt2x00dev, 21, 0x8D);
6129 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
6130 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
6131 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
6132 rt2800_rfcsr_write(rt2x00dev, 33, 0xC0);
6133 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
6134 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
6135 rt2800_rfcsr_write(rt2x00dev, 47, 0x0C);
6136 rt2800_rfcsr_write(rt2x00dev, 53, 0x22);
6137 rt2800_rfcsr_write(rt2x00dev, 63, 0x07);
6138
6139 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
6140 msleep(1);
6141
6142 rt2800_adjust_freq_offset(rt2x00dev);
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006143
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006144 /* Enable DC filter */
6145 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C))
6146 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
6147
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02006148 rt2800_normal_mode_setup_5xxx(rt2x00dev);
Stanislaw Gruszka5de5a1f2013-04-17 14:08:17 +02006149
6150 if (rt2x00_rt_rev_lt(rt2x00dev, RT5592, REV_RT5592C))
6151 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006152
6153 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006154}
6155
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006156static void rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01006157{
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006158 if (rt2800_is_305x_soc(rt2x00dev)) {
6159 rt2800_init_rfcsr_305x_soc(rt2x00dev);
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006160 return;
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006161 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01006162
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006163 switch (rt2x00dev->chip.rt) {
6164 case RT3070:
6165 case RT3071:
6166 case RT3090:
6167 rt2800_init_rfcsr_30xx(rt2x00dev);
6168 break;
6169 case RT3290:
6170 rt2800_init_rfcsr_3290(rt2x00dev);
6171 break;
6172 case RT3352:
6173 rt2800_init_rfcsr_3352(rt2x00dev);
6174 break;
6175 case RT3390:
6176 rt2800_init_rfcsr_3390(rt2x00dev);
6177 break;
6178 case RT3572:
6179 rt2800_init_rfcsr_3572(rt2x00dev);
6180 break;
Gabor Juhosab7078a2013-07-08 16:08:18 +02006181 case RT3593:
6182 rt2800_init_rfcsr_3593(rt2x00dev);
6183 break;
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006184 case RT5390:
6185 rt2800_init_rfcsr_5390(rt2x00dev);
6186 break;
6187 case RT5392:
6188 rt2800_init_rfcsr_5392(rt2x00dev);
6189 break;
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006190 case RT5592:
6191 rt2800_init_rfcsr_5592(rt2x00dev);
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006192 break;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02006193 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01006194}
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006195
6196int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
6197{
6198 u32 reg;
6199 u16 word;
6200
6201 /*
6202 * Initialize all registers.
6203 */
6204 if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006205 rt2800_init_registers(rt2x00dev)))
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006206 return -EIO;
6207
6208 /*
6209 * Send signal to firmware during boot time.
6210 */
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006211 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
6212 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
6213 if (rt2x00_is_usb(rt2x00dev)) {
6214 rt2800_register_write(rt2x00dev, H2M_INT_SRC, 0);
6215 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
6216 }
6217 msleep(1);
6218
Stanislaw Gruszkaa1ef50392013-05-18 14:03:24 +02006219 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
6220 rt2800_wait_bbp_ready(rt2x00dev)))
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006221 return -EIO;
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006222
Stanislaw Gruszkaa1ef50392013-05-18 14:03:24 +02006223 rt2800_init_bbp(rt2x00dev);
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006224 rt2800_init_rfcsr(rt2x00dev);
6225
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006226 if (rt2x00_is_usb(rt2x00dev) &&
6227 (rt2x00_rt(rt2x00dev, RT3070) ||
6228 rt2x00_rt(rt2x00dev, RT3071) ||
6229 rt2x00_rt(rt2x00dev, RT3572))) {
6230 udelay(200);
6231 rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
6232 udelay(10);
6233 }
6234
6235 /*
6236 * Enable RX.
6237 */
6238 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
6239 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
6240 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
6241 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
6242
6243 udelay(50);
6244
6245 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
6246 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
6247 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
6248 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
6249 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
6250 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
6251
6252 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
6253 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
6254 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
6255 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
6256
6257 /*
6258 * Initialize LED control
6259 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006260 rt2800_eeprom_read(rt2x00dev, EEPROM_LED_AG_CONF, &word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006261 rt2800_mcu_request(rt2x00dev, MCU_LED_AG_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006262 word & 0xff, (word >> 8) & 0xff);
6263
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006264 rt2800_eeprom_read(rt2x00dev, EEPROM_LED_ACT_CONF, &word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006265 rt2800_mcu_request(rt2x00dev, MCU_LED_ACT_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006266 word & 0xff, (word >> 8) & 0xff);
6267
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006268 rt2800_eeprom_read(rt2x00dev, EEPROM_LED_POLARITY, &word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006269 rt2800_mcu_request(rt2x00dev, MCU_LED_LED_POLARITY, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006270 word & 0xff, (word >> 8) & 0xff);
6271
6272 return 0;
6273}
6274EXPORT_SYMBOL_GPL(rt2800_enable_radio);
6275
6276void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
6277{
6278 u32 reg;
6279
Jakub Kicinskif7b395e2012-04-03 03:40:47 +02006280 rt2800_disable_wpdma(rt2x00dev);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006281
6282 /* Wait for DMA, ignore error */
6283 rt2800_wait_wpdma_ready(rt2x00dev);
6284
6285 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
6286 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
6287 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
6288 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006289}
6290EXPORT_SYMBOL_GPL(rt2800_disable_radio);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006291
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006292int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
6293{
6294 u32 reg;
Woody Hunga89534e2012-06-13 15:01:16 +08006295 u16 efuse_ctrl_reg;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006296
Woody Hunga89534e2012-06-13 15:01:16 +08006297 if (rt2x00_rt(rt2x00dev, RT3290))
6298 efuse_ctrl_reg = EFUSE_CTRL_3290;
6299 else
6300 efuse_ctrl_reg = EFUSE_CTRL;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006301
Woody Hunga89534e2012-06-13 15:01:16 +08006302 rt2800_register_read(rt2x00dev, efuse_ctrl_reg, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006303 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
6304}
6305EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
6306
6307static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
6308{
6309 u32 reg;
Woody Hunga89534e2012-06-13 15:01:16 +08006310 u16 efuse_ctrl_reg;
6311 u16 efuse_data0_reg;
6312 u16 efuse_data1_reg;
6313 u16 efuse_data2_reg;
6314 u16 efuse_data3_reg;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006315
Woody Hunga89534e2012-06-13 15:01:16 +08006316 if (rt2x00_rt(rt2x00dev, RT3290)) {
6317 efuse_ctrl_reg = EFUSE_CTRL_3290;
6318 efuse_data0_reg = EFUSE_DATA0_3290;
6319 efuse_data1_reg = EFUSE_DATA1_3290;
6320 efuse_data2_reg = EFUSE_DATA2_3290;
6321 efuse_data3_reg = EFUSE_DATA3_3290;
6322 } else {
6323 efuse_ctrl_reg = EFUSE_CTRL;
6324 efuse_data0_reg = EFUSE_DATA0;
6325 efuse_data1_reg = EFUSE_DATA1;
6326 efuse_data2_reg = EFUSE_DATA2;
6327 efuse_data3_reg = EFUSE_DATA3;
6328 }
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01006329 mutex_lock(&rt2x00dev->csr_mutex);
6330
Woody Hunga89534e2012-06-13 15:01:16 +08006331 rt2800_register_read_lock(rt2x00dev, efuse_ctrl_reg, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006332 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
6333 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
6334 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Woody Hunga89534e2012-06-13 15:01:16 +08006335 rt2800_register_write_lock(rt2x00dev, efuse_ctrl_reg, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006336
6337 /* Wait until the EEPROM has been loaded */
Woody Hunga89534e2012-06-13 15:01:16 +08006338 rt2800_regbusy_read(rt2x00dev, efuse_ctrl_reg, EFUSE_CTRL_KICK, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006339 /* Apparently the data is read from end to start */
Woody Hunga89534e2012-06-13 15:01:16 +08006340 rt2800_register_read_lock(rt2x00dev, efuse_data3_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006341 /* The returned value is in CPU order, but eeprom is le */
Gertjan van Wingerde68fa64e2011-11-16 23:16:15 +01006342 *(u32 *)&rt2x00dev->eeprom[i] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08006343 rt2800_register_read_lock(rt2x00dev, efuse_data2_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006344 *(u32 *)&rt2x00dev->eeprom[i + 2] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08006345 rt2800_register_read_lock(rt2x00dev, efuse_data1_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006346 *(u32 *)&rt2x00dev->eeprom[i + 4] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08006347 rt2800_register_read_lock(rt2x00dev, efuse_data0_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006348 *(u32 *)&rt2x00dev->eeprom[i + 6] = cpu_to_le32(reg);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01006349
6350 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006351}
6352
Gabor Juhosa02308e2012-12-29 14:51:51 +01006353int rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006354{
6355 unsigned int i;
6356
6357 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
6358 rt2800_efuse_read(rt2x00dev, i);
Gabor Juhosa02308e2012-12-29 14:51:51 +01006359
6360 return 0;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006361}
6362EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
6363
Gabor Juhosa3f16252013-07-08 16:08:25 +02006364static u8 rt2800_get_txmixer_gain_24g(struct rt2x00_dev *rt2x00dev)
6365{
6366 u16 word;
6367
6368 rt2800_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &word);
6369 if ((word & 0x00ff) != 0x00ff)
6370 return rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_BG_VAL);
6371
6372 return 0;
6373}
6374
6375static u8 rt2800_get_txmixer_gain_5g(struct rt2x00_dev *rt2x00dev)
6376{
6377 u16 word;
6378
6379 rt2800_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_A, &word);
6380 if ((word & 0x00ff) != 0x00ff)
6381 return rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_A_VAL);
6382
6383 return 0;
6384}
6385
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02006386static int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006387{
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01006388 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006389 u16 word;
6390 u8 *mac;
6391 u8 default_lna_gain;
Gabor Juhosa02308e2012-12-29 14:51:51 +01006392 int retval;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006393
6394 /*
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02006395 * Read the EEPROM.
6396 */
Gabor Juhosa02308e2012-12-29 14:51:51 +01006397 retval = rt2800_read_eeprom(rt2x00dev);
6398 if (retval)
6399 return retval;
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02006400
6401 /*
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006402 * Start validation of the data that has been read.
6403 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006404 mac = rt2800_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006405 if (!is_valid_ether_addr(mac)) {
Joe Perchesf4f7f4142012-07-12 19:33:08 +00006406 eth_random_addr(mac);
Joe Perchesec9c4982013-04-19 08:33:40 -07006407 rt2x00_eeprom_dbg(rt2x00dev, "MAC: %pM\n", mac);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006408 }
6409
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006410 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006411 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006412 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
6413 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_TXPATH, 1);
6414 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RF_TYPE, RF2820);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006415 rt2800_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Joe Perchesec9c4982013-04-19 08:33:40 -07006416 rt2x00_eeprom_dbg(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01006417 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02006418 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006419 /*
6420 * There is a max of 2 RX streams for RT28x0 series
6421 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006422 if (rt2x00_get_field16(word, EEPROM_NIC_CONF0_RXPATH) > 2)
6423 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006424 rt2800_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006425 }
6426
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006427 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006428 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006429 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_HW_RADIO, 0);
6430 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC, 0);
6431 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G, 0);
6432 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G, 0);
6433 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_CARDBUS_ACCEL, 0);
6434 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_2G, 0);
6435 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_5G, 0);
6436 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_WPS_PBC, 0);
6437 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_2G, 0);
6438 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_5G, 0);
6439 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BROADBAND_EXT_LNA, 0);
6440 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_ANT_DIVERSITY, 0);
6441 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_INTERNAL_TX_ALC, 0);
6442 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BT_COEXIST, 0);
6443 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_DAC_TEST, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006444 rt2800_eeprom_write(rt2x00dev, EEPROM_NIC_CONF1, word);
Joe Perchesec9c4982013-04-19 08:33:40 -07006445 rt2x00_eeprom_dbg(rt2x00dev, "NIC: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006446 }
6447
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006448 rt2800_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006449 if ((word & 0x00ff) == 0x00ff) {
6450 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006451 rt2800_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
Joe Perchesec9c4982013-04-19 08:33:40 -07006452 rt2x00_eeprom_dbg(rt2x00dev, "Freq: 0x%04x\n", word);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02006453 }
6454 if ((word & 0xff00) == 0xff00) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006455 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
6456 LED_MODE_TXRX_ACTIVITY);
6457 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006458 rt2800_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
6459 rt2800_eeprom_write(rt2x00dev, EEPROM_LED_AG_CONF, 0x5555);
6460 rt2800_eeprom_write(rt2x00dev, EEPROM_LED_ACT_CONF, 0x2221);
6461 rt2800_eeprom_write(rt2x00dev, EEPROM_LED_POLARITY, 0xa9f8);
Joe Perchesec9c4982013-04-19 08:33:40 -07006462 rt2x00_eeprom_dbg(rt2x00dev, "Led Mode: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006463 }
6464
6465 /*
6466 * During the LNA validation we are going to use
6467 * lna0 as correct value. Note that EEPROM_LNA
6468 * is never validated.
6469 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006470 rt2800_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006471 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
6472
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006473 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006474 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
6475 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
6476 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
6477 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006478 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006479
Gabor Juhosa3f16252013-07-08 16:08:25 +02006480 drv_data->txmixer_gain_24g = rt2800_get_txmixer_gain_24g(rt2x00dev);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01006481
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006482 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006483 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
6484 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
6485 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
6486 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
6487 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
6488 default_lna_gain);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006489 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006490
Gabor Juhosa3f16252013-07-08 16:08:25 +02006491 drv_data->txmixer_gain_5g = rt2800_get_txmixer_gain_5g(rt2x00dev);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01006492
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006493 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006494 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
6495 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
6496 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
6497 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006498 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006499
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006500 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006501 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
6502 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
6503 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
6504 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
6505 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
6506 default_lna_gain);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006507 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006508
6509 return 0;
6510}
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006511
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02006512static int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006513{
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006514 u16 value;
6515 u16 eeprom;
Gabor Juhos86868b22013-03-30 14:53:09 +01006516 u16 rf;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006517
Gabor Juhos86868b22013-03-30 14:53:09 +01006518 /*
6519 * Read EEPROM word for configuration.
6520 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006521 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Gabor Juhos86868b22013-03-30 14:53:09 +01006522
6523 /*
6524 * Identify RF chipset by EEPROM value
6525 * RT28xx/RT30xx: defined in "EEPROM_NIC_CONF0_RF_TYPE" field
6526 * RT53xx: defined in "EEPROM_CHIP_ID" field
6527 */
6528 if (rt2x00_rt(rt2x00dev, RT3290) ||
6529 rt2x00_rt(rt2x00dev, RT5390) ||
6530 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006531 rt2800_eeprom_read(rt2x00dev, EEPROM_CHIP_ID, &rf);
Gabor Juhos86868b22013-03-30 14:53:09 +01006532 else
6533 rf = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RF_TYPE);
6534
6535 switch (rf) {
Larry Fingerd331eb52011-09-14 16:50:22 -05006536 case RF2820:
6537 case RF2850:
6538 case RF2720:
6539 case RF2750:
6540 case RF3020:
6541 case RF2020:
6542 case RF3021:
6543 case RF3022:
6544 case RF3052:
Woody Hunga89534e2012-06-13 15:01:16 +08006545 case RF3290:
Larry Fingerd331eb52011-09-14 16:50:22 -05006546 case RF3320:
Daniel Golle03839952012-09-09 14:24:39 +03006547 case RF3322:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02006548 case RF5360:
Larry Fingerd331eb52011-09-14 16:50:22 -05006549 case RF5370:
John Li2ed71882012-02-17 17:33:06 +08006550 case RF5372:
Larry Fingerd331eb52011-09-14 16:50:22 -05006551 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08006552 case RF5392:
Stanislaw Gruszkab8863f82013-03-16 19:19:30 +01006553 case RF5592:
Larry Fingerd331eb52011-09-14 16:50:22 -05006554 break;
6555 default:
Joe Perchesec9c4982013-04-19 08:33:40 -07006556 rt2x00_err(rt2x00dev, "Invalid RF chipset 0x%04x detected\n",
6557 rf);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006558 return -ENODEV;
6559 }
6560
Gabor Juhos86868b22013-03-30 14:53:09 +01006561 rt2x00_set_rf(rt2x00dev, rf);
6562
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006563 /*
6564 * Identify default antenna configuration.
6565 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01006566 rt2x00dev->default_ant.tx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006567 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01006568 rt2x00dev->default_ant.rx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006569 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006570
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006571 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01006572
6573 if (rt2x00_rt(rt2x00dev, RT3070) ||
6574 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03006575 rt2x00_rt(rt2x00dev, RT3352) ||
RA-Jay Hungd96aa642011-02-20 13:54:52 +01006576 rt2x00_rt(rt2x00dev, RT3390)) {
6577 value = rt2x00_get_field16(eeprom,
6578 EEPROM_NIC_CONF1_ANT_DIVERSITY);
6579 switch (value) {
6580 case 0:
6581 case 1:
6582 case 2:
6583 rt2x00dev->default_ant.tx = ANTENNA_A;
6584 rt2x00dev->default_ant.rx = ANTENNA_A;
6585 break;
6586 case 3:
6587 rt2x00dev->default_ant.tx = ANTENNA_A;
6588 rt2x00dev->default_ant.rx = ANTENNA_B;
6589 break;
6590 }
6591 } else {
6592 rt2x00dev->default_ant.tx = ANTENNA_A;
6593 rt2x00dev->default_ant.rx = ANTENNA_A;
6594 }
6595
Anisse Astier0586a112012-04-23 12:33:11 +02006596 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390R)) {
6597 rt2x00dev->default_ant.tx = ANTENNA_HW_DIVERSITY; /* Unused */
6598 rt2x00dev->default_ant.rx = ANTENNA_HW_DIVERSITY; /* Unused */
6599 }
6600
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006601 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02006602 * Determine external LNA informations.
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006603 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006604 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02006605 __set_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006606 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02006607 __set_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006608
6609 /*
6610 * Detect if this device has an hardware controlled radio.
6611 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006612 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_HW_RADIO))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02006613 __set_bit(CAPABILITY_HW_BUTTON, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006614
6615 /*
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02006616 * Detect if this device has Bluetooth co-existence.
6617 */
6618 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_BT_COEXIST))
6619 __set_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags);
6620
6621 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02006622 * Read frequency offset and RF programming sequence.
6623 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006624 rt2800_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02006625 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
6626
6627 /*
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006628 * Store led settings, for correct led behaviour.
6629 */
6630#ifdef CONFIG_RT2X00_LIB_LEDS
6631 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
6632 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
6633 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
6634
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02006635 rt2x00dev->led_mcu_reg = eeprom;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006636#endif /* CONFIG_RT2X00_LIB_LEDS */
6637
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01006638 /*
6639 * Check if support EIRP tx power limit feature.
6640 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006641 rt2800_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER, &eeprom);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01006642
6643 if (rt2x00_get_field16(eeprom, EEPROM_EIRP_MAX_TX_POWER_2GHZ) <
6644 EIRP_MAX_TX_POWER_LIMIT)
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02006645 __set_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01006646
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006647 return 0;
6648}
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006649
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006650/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02006651 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006652 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
6653 */
6654static const struct rf_channel rf_vals[] = {
6655 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
6656 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
6657 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
6658 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
6659 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
6660 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
6661 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
6662 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
6663 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
6664 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
6665 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
6666 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
6667 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
6668 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
6669
6670 /* 802.11 UNI / HyperLan 2 */
6671 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
6672 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
6673 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
6674 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
6675 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
6676 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
6677 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
6678 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
6679 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
6680 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
6681 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
6682 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
6683
6684 /* 802.11 HyperLan 2 */
6685 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
6686 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
6687 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
6688 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
6689 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
6690 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
6691 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
6692 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
6693 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
6694 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
6695 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
6696 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
6697 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
6698 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
6699 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
6700 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
6701
6702 /* 802.11 UNII */
6703 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
6704 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
6705 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
6706 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
6707 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
6708 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
6709 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
6710 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
6711 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
6712 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
6713 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
6714
6715 /* 802.11 Japan */
6716 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
6717 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
6718 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
6719 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
6720 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
6721 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
6722 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
6723};
6724
6725/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02006726 * RF value list for rt3xxx
6727 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006728 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02006729static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006730 {1, 241, 2, 2 },
6731 {2, 241, 2, 7 },
6732 {3, 242, 2, 2 },
6733 {4, 242, 2, 7 },
6734 {5, 243, 2, 2 },
6735 {6, 243, 2, 7 },
6736 {7, 244, 2, 2 },
6737 {8, 244, 2, 7 },
6738 {9, 245, 2, 2 },
6739 {10, 245, 2, 7 },
6740 {11, 246, 2, 2 },
6741 {12, 246, 2, 7 },
6742 {13, 247, 2, 2 },
6743 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02006744
6745 /* 802.11 UNI / HyperLan 2 */
6746 {36, 0x56, 0, 4},
6747 {38, 0x56, 0, 6},
6748 {40, 0x56, 0, 8},
6749 {44, 0x57, 0, 0},
6750 {46, 0x57, 0, 2},
6751 {48, 0x57, 0, 4},
6752 {52, 0x57, 0, 8},
6753 {54, 0x57, 0, 10},
6754 {56, 0x58, 0, 0},
6755 {60, 0x58, 0, 4},
6756 {62, 0x58, 0, 6},
6757 {64, 0x58, 0, 8},
6758
6759 /* 802.11 HyperLan 2 */
6760 {100, 0x5b, 0, 8},
6761 {102, 0x5b, 0, 10},
6762 {104, 0x5c, 0, 0},
6763 {108, 0x5c, 0, 4},
6764 {110, 0x5c, 0, 6},
6765 {112, 0x5c, 0, 8},
6766 {116, 0x5d, 0, 0},
6767 {118, 0x5d, 0, 2},
6768 {120, 0x5d, 0, 4},
6769 {124, 0x5d, 0, 8},
6770 {126, 0x5d, 0, 10},
6771 {128, 0x5e, 0, 0},
6772 {132, 0x5e, 0, 4},
6773 {134, 0x5e, 0, 6},
6774 {136, 0x5e, 0, 8},
6775 {140, 0x5f, 0, 0},
6776
6777 /* 802.11 UNII */
6778 {149, 0x5f, 0, 9},
6779 {151, 0x5f, 0, 11},
6780 {153, 0x60, 0, 1},
6781 {157, 0x60, 0, 5},
6782 {159, 0x60, 0, 7},
6783 {161, 0x60, 0, 9},
6784 {165, 0x61, 0, 1},
6785 {167, 0x61, 0, 3},
6786 {169, 0x61, 0, 5},
6787 {171, 0x61, 0, 7},
6788 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006789};
6790
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01006791static const struct rf_channel rf_vals_5592_xtal20[] = {
6792 /* Channel, N, K, mod, R */
6793 {1, 482, 4, 10, 3},
6794 {2, 483, 4, 10, 3},
6795 {3, 484, 4, 10, 3},
6796 {4, 485, 4, 10, 3},
6797 {5, 486, 4, 10, 3},
6798 {6, 487, 4, 10, 3},
6799 {7, 488, 4, 10, 3},
6800 {8, 489, 4, 10, 3},
6801 {9, 490, 4, 10, 3},
6802 {10, 491, 4, 10, 3},
6803 {11, 492, 4, 10, 3},
6804 {12, 493, 4, 10, 3},
6805 {13, 494, 4, 10, 3},
6806 {14, 496, 8, 10, 3},
6807 {36, 172, 8, 12, 1},
6808 {38, 173, 0, 12, 1},
6809 {40, 173, 4, 12, 1},
6810 {42, 173, 8, 12, 1},
6811 {44, 174, 0, 12, 1},
6812 {46, 174, 4, 12, 1},
6813 {48, 174, 8, 12, 1},
6814 {50, 175, 0, 12, 1},
6815 {52, 175, 4, 12, 1},
6816 {54, 175, 8, 12, 1},
6817 {56, 176, 0, 12, 1},
6818 {58, 176, 4, 12, 1},
6819 {60, 176, 8, 12, 1},
6820 {62, 177, 0, 12, 1},
6821 {64, 177, 4, 12, 1},
6822 {100, 183, 4, 12, 1},
6823 {102, 183, 8, 12, 1},
6824 {104, 184, 0, 12, 1},
6825 {106, 184, 4, 12, 1},
6826 {108, 184, 8, 12, 1},
6827 {110, 185, 0, 12, 1},
6828 {112, 185, 4, 12, 1},
6829 {114, 185, 8, 12, 1},
6830 {116, 186, 0, 12, 1},
6831 {118, 186, 4, 12, 1},
6832 {120, 186, 8, 12, 1},
6833 {122, 187, 0, 12, 1},
6834 {124, 187, 4, 12, 1},
6835 {126, 187, 8, 12, 1},
6836 {128, 188, 0, 12, 1},
6837 {130, 188, 4, 12, 1},
6838 {132, 188, 8, 12, 1},
6839 {134, 189, 0, 12, 1},
6840 {136, 189, 4, 12, 1},
6841 {138, 189, 8, 12, 1},
6842 {140, 190, 0, 12, 1},
6843 {149, 191, 6, 12, 1},
6844 {151, 191, 10, 12, 1},
6845 {153, 192, 2, 12, 1},
6846 {155, 192, 6, 12, 1},
6847 {157, 192, 10, 12, 1},
6848 {159, 193, 2, 12, 1},
6849 {161, 193, 6, 12, 1},
6850 {165, 194, 2, 12, 1},
6851 {184, 164, 0, 12, 1},
6852 {188, 164, 4, 12, 1},
6853 {192, 165, 8, 12, 1},
6854 {196, 166, 0, 12, 1},
6855};
6856
6857static const struct rf_channel rf_vals_5592_xtal40[] = {
6858 /* Channel, N, K, mod, R */
6859 {1, 241, 2, 10, 3},
6860 {2, 241, 7, 10, 3},
6861 {3, 242, 2, 10, 3},
6862 {4, 242, 7, 10, 3},
6863 {5, 243, 2, 10, 3},
6864 {6, 243, 7, 10, 3},
6865 {7, 244, 2, 10, 3},
6866 {8, 244, 7, 10, 3},
6867 {9, 245, 2, 10, 3},
6868 {10, 245, 7, 10, 3},
6869 {11, 246, 2, 10, 3},
6870 {12, 246, 7, 10, 3},
6871 {13, 247, 2, 10, 3},
6872 {14, 248, 4, 10, 3},
6873 {36, 86, 4, 12, 1},
6874 {38, 86, 6, 12, 1},
6875 {40, 86, 8, 12, 1},
6876 {42, 86, 10, 12, 1},
6877 {44, 87, 0, 12, 1},
6878 {46, 87, 2, 12, 1},
6879 {48, 87, 4, 12, 1},
6880 {50, 87, 6, 12, 1},
6881 {52, 87, 8, 12, 1},
6882 {54, 87, 10, 12, 1},
6883 {56, 88, 0, 12, 1},
6884 {58, 88, 2, 12, 1},
6885 {60, 88, 4, 12, 1},
6886 {62, 88, 6, 12, 1},
6887 {64, 88, 8, 12, 1},
6888 {100, 91, 8, 12, 1},
6889 {102, 91, 10, 12, 1},
6890 {104, 92, 0, 12, 1},
6891 {106, 92, 2, 12, 1},
6892 {108, 92, 4, 12, 1},
6893 {110, 92, 6, 12, 1},
6894 {112, 92, 8, 12, 1},
6895 {114, 92, 10, 12, 1},
6896 {116, 93, 0, 12, 1},
6897 {118, 93, 2, 12, 1},
6898 {120, 93, 4, 12, 1},
6899 {122, 93, 6, 12, 1},
6900 {124, 93, 8, 12, 1},
6901 {126, 93, 10, 12, 1},
6902 {128, 94, 0, 12, 1},
6903 {130, 94, 2, 12, 1},
6904 {132, 94, 4, 12, 1},
6905 {134, 94, 6, 12, 1},
6906 {136, 94, 8, 12, 1},
6907 {138, 94, 10, 12, 1},
6908 {140, 95, 0, 12, 1},
6909 {149, 95, 9, 12, 1},
6910 {151, 95, 11, 12, 1},
6911 {153, 96, 1, 12, 1},
6912 {155, 96, 3, 12, 1},
6913 {157, 96, 5, 12, 1},
6914 {159, 96, 7, 12, 1},
6915 {161, 96, 9, 12, 1},
6916 {165, 97, 1, 12, 1},
6917 {184, 82, 0, 12, 1},
6918 {188, 82, 4, 12, 1},
6919 {192, 82, 8, 12, 1},
6920 {196, 83, 0, 12, 1},
6921};
6922
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02006923static int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006924{
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006925 struct hw_mode_spec *spec = &rt2x00dev->spec;
6926 struct channel_info *info;
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02006927 char *default_power1;
6928 char *default_power2;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006929 unsigned int i;
6930 u16 eeprom;
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01006931 u32 reg;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006932
6933 /*
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01006934 * Disable powersaving as default on PCI devices.
6935 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01006936 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01006937 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
6938
6939 /*
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006940 * Initialize all hw fields.
6941 */
6942 rt2x00dev->hw->flags =
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006943 IEEE80211_HW_SIGNAL_DBM |
6944 IEEE80211_HW_SUPPORTS_PS |
Helmut Schaa1df90802010-06-29 21:38:12 +02006945 IEEE80211_HW_PS_NULLFUNC_STACK |
Helmut Schaa9d4f09b2012-03-14 08:56:47 +01006946 IEEE80211_HW_AMPDU_AGGREGATION |
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +01006947 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
Helmut Schaa9d4f09b2012-03-14 08:56:47 +01006948
Helmut Schaa5a5b6ed2010-10-02 11:31:33 +02006949 /*
6950 * Don't set IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING for USB devices
6951 * unless we are capable of sending the buffered frames out after the
6952 * DTIM transmission using rt2x00lib_beacondone. This will send out
6953 * multicast and broadcast traffic immediately instead of buffering it
6954 * infinitly and thus dropping it after some time.
6955 */
6956 if (!rt2x00_is_usb(rt2x00dev))
6957 rt2x00dev->hw->flags |=
6958 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006959
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006960 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
6961 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006962 rt2800_eeprom_addr(rt2x00dev,
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006963 EEPROM_MAC_ADDR_0));
6964
Helmut Schaa3f2bee22010-06-14 22:12:01 +02006965 /*
6966 * As rt2800 has a global fallback table we cannot specify
6967 * more then one tx rate per frame but since the hw will
6968 * try several rates (based on the fallback table) we should
Helmut Schaaba3b9e52010-10-02 11:32:16 +02006969 * initialize max_report_rates to the maximum number of rates
Helmut Schaa3f2bee22010-06-14 22:12:01 +02006970 * we are going to try. Otherwise mac80211 will truncate our
6971 * reported tx rates and the rc algortihm will end up with
6972 * incorrect data.
6973 */
Helmut Schaaba3b9e52010-10-02 11:32:16 +02006974 rt2x00dev->hw->max_rates = 1;
6975 rt2x00dev->hw->max_report_rates = 7;
Helmut Schaa3f2bee22010-06-14 22:12:01 +02006976 rt2x00dev->hw->max_rate_tries = 1;
6977
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006978 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006979
6980 /*
6981 * Initialize hw_mode information.
6982 */
6983 spec->supported_bands = SUPPORT_BAND_2GHZ;
6984 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
6985
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01006986 if (rt2x00_rf(rt2x00dev, RF2820) ||
Ivo van Doorn55f93212010-05-06 14:45:46 +02006987 rt2x00_rf(rt2x00dev, RF2720)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006988 spec->num_channels = 14;
6989 spec->channels = rf_vals;
Ivo van Doorn55f93212010-05-06 14:45:46 +02006990 } else if (rt2x00_rf(rt2x00dev, RF2850) ||
6991 rt2x00_rf(rt2x00dev, RF2750)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01006992 spec->supported_bands |= SUPPORT_BAND_5GHZ;
6993 spec->num_channels = ARRAY_SIZE(rf_vals);
6994 spec->channels = rf_vals;
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01006995 } else if (rt2x00_rf(rt2x00dev, RF3020) ||
6996 rt2x00_rf(rt2x00dev, RF2020) ||
6997 rt2x00_rf(rt2x00dev, RF3021) ||
Gertjan van Wingerdef93bc9b2010-11-13 19:09:50 +01006998 rt2x00_rf(rt2x00dev, RF3022) ||
Woody Hunga89534e2012-06-13 15:01:16 +08006999 rt2x00_rf(rt2x00dev, RF3290) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01007000 rt2x00_rf(rt2x00dev, RF3320) ||
Daniel Golle03839952012-09-09 14:24:39 +03007001 rt2x00_rf(rt2x00dev, RF3322) ||
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02007002 rt2x00_rf(rt2x00dev, RF5360) ||
Gertjan van Wingerdeaca355b2011-05-04 21:41:36 +02007003 rt2x00_rf(rt2x00dev, RF5370) ||
John Li2ed71882012-02-17 17:33:06 +08007004 rt2x00_rf(rt2x00dev, RF5372) ||
Zero.Lincff3d1f2012-05-29 16:11:09 +08007005 rt2x00_rf(rt2x00dev, RF5390) ||
7006 rt2x00_rf(rt2x00dev, RF5392)) {
Ivo van Doorn55f93212010-05-06 14:45:46 +02007007 spec->num_channels = 14;
7008 spec->channels = rf_vals_3x;
7009 } else if (rt2x00_rf(rt2x00dev, RF3052)) {
7010 spec->supported_bands |= SUPPORT_BAND_5GHZ;
7011 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
7012 spec->channels = rf_vals_3x;
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01007013 } else if (rt2x00_rf(rt2x00dev, RF5592)) {
7014 spec->supported_bands |= SUPPORT_BAND_5GHZ;
7015
7016 rt2800_register_read(rt2x00dev, MAC_DEBUG_INDEX, &reg);
7017 if (rt2x00_get_field32(reg, MAC_DEBUG_INDEX_XTAL)) {
7018 spec->num_channels = ARRAY_SIZE(rf_vals_5592_xtal40);
7019 spec->channels = rf_vals_5592_xtal40;
7020 } else {
7021 spec->num_channels = ARRAY_SIZE(rf_vals_5592_xtal20);
7022 spec->channels = rf_vals_5592_xtal20;
7023 }
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007024 }
7025
Stanislaw Gruszka53216d62013-03-16 19:19:29 +01007026 if (WARN_ON_ONCE(!spec->channels))
7027 return -ENODEV;
7028
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007029 /*
7030 * Initialize HT information.
7031 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01007032 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01007033 spec->ht.ht_supported = true;
7034 else
7035 spec->ht.ht_supported = false;
7036
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007037 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02007038 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007039 IEEE80211_HT_CAP_GRN_FLD |
7040 IEEE80211_HT_CAP_SGI_20 |
Ivo van Doornaa674632010-06-29 21:48:37 +02007041 IEEE80211_HT_CAP_SGI_40;
Helmut Schaa22cabaa2010-06-03 10:52:10 +02007042
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007043 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) >= 2)
Helmut Schaa22cabaa2010-06-03 10:52:10 +02007044 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
7045
Ivo van Doornaa674632010-06-29 21:48:37 +02007046 spec->ht.cap |=
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007047 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) <<
Ivo van Doornaa674632010-06-29 21:48:37 +02007048 IEEE80211_HT_CAP_RX_STBC_SHIFT;
7049
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007050 spec->ht.ampdu_factor = 3;
7051 spec->ht.ampdu_density = 4;
7052 spec->ht.mcs.tx_params =
7053 IEEE80211_HT_MCS_TX_DEFINED |
7054 IEEE80211_HT_MCS_TX_RX_DIFF |
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007055 ((rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) - 1) <<
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007056 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
7057
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007058 switch (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007059 case 3:
7060 spec->ht.mcs.rx_mask[2] = 0xff;
7061 case 2:
7062 spec->ht.mcs.rx_mask[1] = 0xff;
7063 case 1:
7064 spec->ht.mcs.rx_mask[0] = 0xff;
7065 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
7066 break;
7067 }
7068
7069 /*
7070 * Create channel information array
7071 */
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00007072 info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007073 if (!info)
7074 return -ENOMEM;
7075
7076 spec->channels_info = info;
7077
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007078 default_power1 = rt2800_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
7079 default_power2 = rt2800_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007080
7081 for (i = 0; i < 14; i++) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01007082 info[i].default_power1 = default_power1[i];
7083 info[i].default_power2 = default_power2[i];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007084 }
7085
7086 if (spec->num_channels > 14) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007087 default_power1 = rt2800_eeprom_addr(rt2x00dev,
7088 EEPROM_TXPOWER_A1);
7089 default_power2 = rt2800_eeprom_addr(rt2x00dev,
7090 EEPROM_TXPOWER_A2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007091
7092 for (i = 14; i < spec->num_channels; i++) {
Gabor Juhos0a6f3a82013-06-22 13:13:25 +02007093 info[i].default_power1 = default_power1[i - 14];
7094 info[i].default_power2 = default_power2[i - 14];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007095 }
7096 }
7097
John Li2e9c43d2012-02-16 21:40:57 +08007098 switch (rt2x00dev->chip.rf) {
7099 case RF2020:
7100 case RF3020:
7101 case RF3021:
7102 case RF3022:
7103 case RF3320:
7104 case RF3052:
Woody Hunga89534e2012-06-13 15:01:16 +08007105 case RF3290:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02007106 case RF5360:
John Li2e9c43d2012-02-16 21:40:57 +08007107 case RF5370:
7108 case RF5372:
7109 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08007110 case RF5392:
John Li2e9c43d2012-02-16 21:40:57 +08007111 __set_bit(CAPABILITY_VCO_RECALIBRATION, &rt2x00dev->cap_flags);
7112 break;
7113 }
7114
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007115 return 0;
7116}
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007117
Gabor Juhoscbafb602013-03-30 14:53:10 +01007118static int rt2800_probe_rt(struct rt2x00_dev *rt2x00dev)
7119{
7120 u32 reg;
7121 u32 rt;
7122 u32 rev;
7123
7124 if (rt2x00_rt(rt2x00dev, RT3290))
7125 rt2800_register_read(rt2x00dev, MAC_CSR0_3290, &reg);
7126 else
7127 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
7128
7129 rt = rt2x00_get_field32(reg, MAC_CSR0_CHIPSET);
7130 rev = rt2x00_get_field32(reg, MAC_CSR0_REVISION);
7131
7132 switch (rt) {
7133 case RT2860:
7134 case RT2872:
7135 case RT2883:
7136 case RT3070:
7137 case RT3071:
7138 case RT3090:
7139 case RT3290:
7140 case RT3352:
7141 case RT3390:
7142 case RT3572:
7143 case RT5390:
7144 case RT5392:
7145 case RT5592:
7146 break;
7147 default:
Joe Perchesec9c4982013-04-19 08:33:40 -07007148 rt2x00_err(rt2x00dev, "Invalid RT chipset 0x%04x, rev %04x detected\n",
7149 rt, rev);
Gabor Juhoscbafb602013-03-30 14:53:10 +01007150 return -ENODEV;
7151 }
7152
7153 rt2x00_set_rt(rt2x00dev, rt, rev);
7154
7155 return 0;
7156}
7157
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007158int rt2800_probe_hw(struct rt2x00_dev *rt2x00dev)
7159{
7160 int retval;
7161 u32 reg;
7162
Gabor Juhoscbafb602013-03-30 14:53:10 +01007163 retval = rt2800_probe_rt(rt2x00dev);
7164 if (retval)
7165 return retval;
7166
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007167 /*
7168 * Allocate eeprom data.
7169 */
7170 retval = rt2800_validate_eeprom(rt2x00dev);
7171 if (retval)
7172 return retval;
7173
7174 retval = rt2800_init_eeprom(rt2x00dev);
7175 if (retval)
7176 return retval;
7177
7178 /*
7179 * Enable rfkill polling by setting GPIO direction of the
7180 * rfkill switch GPIO pin correctly.
7181 */
7182 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
7183 rt2x00_set_field32(&reg, GPIO_CTRL_DIR2, 1);
7184 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
7185
7186 /*
7187 * Initialize hw specifications.
7188 */
7189 retval = rt2800_probe_hw_mode(rt2x00dev);
7190 if (retval)
7191 return retval;
7192
7193 /*
7194 * Set device capabilities.
7195 */
7196 __set_bit(CAPABILITY_CONTROL_FILTERS, &rt2x00dev->cap_flags);
7197 __set_bit(CAPABILITY_CONTROL_FILTER_PSPOLL, &rt2x00dev->cap_flags);
7198 if (!rt2x00_is_usb(rt2x00dev))
7199 __set_bit(CAPABILITY_PRE_TBTT_INTERRUPT, &rt2x00dev->cap_flags);
7200
7201 /*
7202 * Set device requirements.
7203 */
7204 if (!rt2x00_is_soc(rt2x00dev))
7205 __set_bit(REQUIRE_FIRMWARE, &rt2x00dev->cap_flags);
7206 __set_bit(REQUIRE_L2PAD, &rt2x00dev->cap_flags);
7207 __set_bit(REQUIRE_TXSTATUS_FIFO, &rt2x00dev->cap_flags);
7208 if (!rt2800_hwcrypt_disabled(rt2x00dev))
7209 __set_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags);
7210 __set_bit(CAPABILITY_LINK_TUNING, &rt2x00dev->cap_flags);
7211 __set_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags);
7212 if (rt2x00_is_usb(rt2x00dev))
7213 __set_bit(REQUIRE_PS_AUTOWAKE, &rt2x00dev->cap_flags);
7214 else {
7215 __set_bit(REQUIRE_DMA, &rt2x00dev->cap_flags);
7216 __set_bit(REQUIRE_TASKLET_CONTEXT, &rt2x00dev->cap_flags);
7217 }
7218
7219 /*
7220 * Set the rssi offset.
7221 */
7222 rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
7223
7224 return 0;
7225}
7226EXPORT_SYMBOL_GPL(rt2800_probe_hw);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007227
7228/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007229 * IEEE80211 stack callback functions.
7230 */
Helmut Schaae7836192010-07-11 12:28:54 +02007231void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx, u32 *iv32,
7232 u16 *iv16)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007233{
7234 struct rt2x00_dev *rt2x00dev = hw->priv;
7235 struct mac_iveiv_entry iveiv_entry;
7236 u32 offset;
7237
7238 offset = MAC_IVEIV_ENTRY(hw_key_idx);
7239 rt2800_register_multiread(rt2x00dev, offset,
7240 &iveiv_entry, sizeof(iveiv_entry));
7241
Julia Lawall855da5e2009-12-13 17:07:45 +01007242 memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
7243 memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007244}
Helmut Schaae7836192010-07-11 12:28:54 +02007245EXPORT_SYMBOL_GPL(rt2800_get_tkip_seq);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007246
Helmut Schaae7836192010-07-11 12:28:54 +02007247int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007248{
7249 struct rt2x00_dev *rt2x00dev = hw->priv;
7250 u32 reg;
7251 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
7252
7253 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
7254 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
7255 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
7256
7257 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
7258 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
7259 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
7260
7261 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
7262 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
7263 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
7264
7265 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
7266 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
7267 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
7268
7269 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
7270 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
7271 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
7272
7273 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
7274 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
7275 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
7276
7277 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
7278 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
7279 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
7280
7281 return 0;
7282}
Helmut Schaae7836192010-07-11 12:28:54 +02007283EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007284
Eliad Peller8a3a3c82011-10-02 10:15:52 +02007285int rt2800_conf_tx(struct ieee80211_hw *hw,
7286 struct ieee80211_vif *vif, u16 queue_idx,
Helmut Schaae7836192010-07-11 12:28:54 +02007287 const struct ieee80211_tx_queue_params *params)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007288{
7289 struct rt2x00_dev *rt2x00dev = hw->priv;
7290 struct data_queue *queue;
7291 struct rt2x00_field32 field;
7292 int retval;
7293 u32 reg;
7294 u32 offset;
7295
7296 /*
7297 * First pass the configuration through rt2x00lib, that will
7298 * update the queue settings and validate the input. After that
7299 * we are free to update the registers based on the value
7300 * in the queue parameter.
7301 */
Eliad Peller8a3a3c82011-10-02 10:15:52 +02007302 retval = rt2x00mac_conf_tx(hw, vif, queue_idx, params);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007303 if (retval)
7304 return retval;
7305
7306 /*
7307 * We only need to perform additional register initialization
7308 * for WMM queues/
7309 */
7310 if (queue_idx >= 4)
7311 return 0;
7312
Helmut Schaa11f818e2011-03-03 19:38:55 +01007313 queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007314
7315 /* Update WMM TXOP register */
7316 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
7317 field.bit_offset = (queue_idx & 1) * 16;
7318 field.bit_mask = 0xffff << field.bit_offset;
7319
7320 rt2800_register_read(rt2x00dev, offset, &reg);
7321 rt2x00_set_field32(&reg, field, queue->txop);
7322 rt2800_register_write(rt2x00dev, offset, reg);
7323
7324 /* Update WMM registers */
7325 field.bit_offset = queue_idx * 4;
7326 field.bit_mask = 0xf << field.bit_offset;
7327
7328 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
7329 rt2x00_set_field32(&reg, field, queue->aifs);
7330 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
7331
7332 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
7333 rt2x00_set_field32(&reg, field, queue->cw_min);
7334 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
7335
7336 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
7337 rt2x00_set_field32(&reg, field, queue->cw_max);
7338 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
7339
7340 /* Update EDCA registers */
7341 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
7342
7343 rt2800_register_read(rt2x00dev, offset, &reg);
7344 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
7345 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
7346 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
7347 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
7348 rt2800_register_write(rt2x00dev, offset, reg);
7349
7350 return 0;
7351}
Helmut Schaae7836192010-07-11 12:28:54 +02007352EXPORT_SYMBOL_GPL(rt2800_conf_tx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007353
Eliad Peller37a41b42011-09-21 14:06:11 +03007354u64 rt2800_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007355{
7356 struct rt2x00_dev *rt2x00dev = hw->priv;
7357 u64 tsf;
7358 u32 reg;
7359
7360 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
7361 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
7362 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
7363 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
7364
7365 return tsf;
7366}
Helmut Schaae7836192010-07-11 12:28:54 +02007367EXPORT_SYMBOL_GPL(rt2800_get_tsf);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007368
Helmut Schaae7836192010-07-11 12:28:54 +02007369int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7370 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01007371 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
7372 u8 buf_size)
Helmut Schaa1df90802010-06-29 21:38:12 +02007373{
Helmut Schaaaf353232011-09-08 14:38:36 +02007374 struct rt2x00_sta *sta_priv = (struct rt2x00_sta *)sta->drv_priv;
Helmut Schaa1df90802010-06-29 21:38:12 +02007375 int ret = 0;
7376
Helmut Schaaaf353232011-09-08 14:38:36 +02007377 /*
7378 * Don't allow aggregation for stations the hardware isn't aware
7379 * of because tx status reports for frames to an unknown station
7380 * always contain wcid=255 and thus we can't distinguish between
7381 * multiple stations which leads to unwanted situations when the
7382 * hw reorders frames due to aggregation.
7383 */
7384 if (sta_priv->wcid < 0)
7385 return 1;
7386
Helmut Schaa1df90802010-06-29 21:38:12 +02007387 switch (action) {
7388 case IEEE80211_AMPDU_RX_START:
7389 case IEEE80211_AMPDU_RX_STOP:
Helmut Schaa58ed8262010-10-02 11:33:17 +02007390 /*
7391 * The hw itself takes care of setting up BlockAck mechanisms.
7392 * So, we only have to allow mac80211 to nagotiate a BlockAck
7393 * agreement. Once that is done, the hw will BlockAck incoming
7394 * AMPDUs without further setup.
7395 */
Helmut Schaa1df90802010-06-29 21:38:12 +02007396 break;
7397 case IEEE80211_AMPDU_TX_START:
7398 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
7399 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02007400 case IEEE80211_AMPDU_TX_STOP_CONT:
7401 case IEEE80211_AMPDU_TX_STOP_FLUSH:
7402 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Helmut Schaa1df90802010-06-29 21:38:12 +02007403 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
7404 break;
7405 case IEEE80211_AMPDU_TX_OPERATIONAL:
7406 break;
7407 default:
Joe Perchesec9c4982013-04-19 08:33:40 -07007408 rt2x00_warn((struct rt2x00_dev *)hw->priv,
7409 "Unknown AMPDU action\n");
Helmut Schaa1df90802010-06-29 21:38:12 +02007410 }
7411
7412 return ret;
7413}
Helmut Schaae7836192010-07-11 12:28:54 +02007414EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02007415
Helmut Schaa977206d2010-12-13 12:31:58 +01007416int rt2800_get_survey(struct ieee80211_hw *hw, int idx,
7417 struct survey_info *survey)
7418{
7419 struct rt2x00_dev *rt2x00dev = hw->priv;
7420 struct ieee80211_conf *conf = &hw->conf;
7421 u32 idle, busy, busy_ext;
7422
7423 if (idx != 0)
7424 return -ENOENT;
7425
Karl Beldan675a0b02013-03-25 16:26:57 +01007426 survey->channel = conf->chandef.chan;
Helmut Schaa977206d2010-12-13 12:31:58 +01007427
7428 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &idle);
7429 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &busy);
7430 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &busy_ext);
7431
7432 if (idle || busy) {
7433 survey->filled = SURVEY_INFO_CHANNEL_TIME |
7434 SURVEY_INFO_CHANNEL_TIME_BUSY |
7435 SURVEY_INFO_CHANNEL_TIME_EXT_BUSY;
7436
7437 survey->channel_time = (idle + busy) / 1000;
7438 survey->channel_time_busy = busy / 1000;
7439 survey->channel_time_ext_busy = busy_ext / 1000;
7440 }
7441
Helmut Schaa9931df22011-12-22 09:36:29 +01007442 if (!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
7443 survey->filled |= SURVEY_INFO_IN_USE;
7444
Helmut Schaa977206d2010-12-13 12:31:58 +01007445 return 0;
7446
7447}
7448EXPORT_SYMBOL_GPL(rt2800_get_survey);
7449
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02007450MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
7451MODULE_VERSION(DRV_VERSION);
7452MODULE_DESCRIPTION("Ralink RT2800 library");
7453MODULE_LICENSE("GPL");