blob: e81319dbe7fd543d73da85f28ffd4369c576bd89 [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
Jeff Kirshera05b8c52013-12-06 03:32:11 -080027 along with this program; if not, see <http://www.gnu.org/licenses/>.
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010028 */
29
30/*
31 Module: rt2800lib
32 Abstract: rt2800 generic device routines.
33 */
34
Ivo van Doornf31c9a82010-07-11 12:30:37 +020035#include <linux/crc-ccitt.h>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010036#include <linux/kernel.h>
37#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010039
40#include "rt2x00.h"
41#include "rt2800lib.h"
42#include "rt2800.h"
43
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010044/*
45 * Register access.
46 * All access to the CSR registers will go through the methods
47 * rt2800_register_read and rt2800_register_write.
48 * BBP and RF register require indirect register access,
49 * and use the CSR registers BBPCSR and RFCSR to achieve this.
50 * These indirect registers work with busy bits,
51 * and we will try maximal REGISTER_BUSY_COUNT times to access
52 * the register while taking a REGISTER_BUSY_DELAY us delay
53 * between each attampt. When the busy bit is still set at that time,
54 * the access attempt is considered to have failed,
55 * and we will print an error.
56 * The _lock versions must be used if you already hold the csr_mutex
57 */
58#define WAIT_FOR_BBP(__dev, __reg) \
59 rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
60#define WAIT_FOR_RFCSR(__dev, __reg) \
61 rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
62#define WAIT_FOR_RF(__dev, __reg) \
63 rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
64#define WAIT_FOR_MCU(__dev, __reg) \
65 rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
66 H2M_MAILBOX_CSR_OWNER, (__reg))
67
Helmut Schaabaff8002010-04-28 09:58:59 +020068static inline bool rt2800_is_305x_soc(struct rt2x00_dev *rt2x00dev)
69{
70 /* check for rt2872 on SoC */
71 if (!rt2x00_is_soc(rt2x00dev) ||
72 !rt2x00_rt(rt2x00dev, RT2872))
73 return false;
74
75 /* we know for sure that these rf chipsets are used on rt305x boards */
76 if (rt2x00_rf(rt2x00dev, RF3020) ||
77 rt2x00_rf(rt2x00dev, RF3021) ||
78 rt2x00_rf(rt2x00dev, RF3022))
79 return true;
80
Joe Perchesec9c4982013-04-19 08:33:40 -070081 rt2x00_warn(rt2x00dev, "Unknown RF chipset on rt305x\n");
Helmut Schaabaff8002010-04-28 09:58:59 +020082 return false;
83}
84
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010085static void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
86 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010087{
88 u32 reg;
89
90 mutex_lock(&rt2x00dev->csr_mutex);
91
92 /*
93 * Wait until the BBP becomes available, afterwards we
94 * can safely write the new data into the register.
95 */
96 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
97 reg = 0;
98 rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
99 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
100 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
101 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
Ivo van Doornefc7d362010-06-29 21:49:26 +0200102 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100103
104 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
105 }
106
107 mutex_unlock(&rt2x00dev->csr_mutex);
108}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100109
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100110static void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
111 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100112{
113 u32 reg;
114
115 mutex_lock(&rt2x00dev->csr_mutex);
116
117 /*
118 * Wait until the BBP becomes available, afterwards we
119 * can safely write the read request into the register.
120 * After the data has been written, we wait until hardware
121 * returns the correct value, if at any time the register
122 * doesn't become available in time, reg will be 0xffffffff
123 * which means we return 0xff to the caller.
124 */
125 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
126 reg = 0;
127 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
128 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
129 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
Ivo van Doornefc7d362010-06-29 21:49:26 +0200130 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100131
132 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
133
134 WAIT_FOR_BBP(rt2x00dev, &reg);
135 }
136
137 *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
138
139 mutex_unlock(&rt2x00dev->csr_mutex);
140}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100141
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100142static void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
143 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100144{
145 u32 reg;
146
147 mutex_lock(&rt2x00dev->csr_mutex);
148
149 /*
150 * Wait until the RFCSR becomes available, afterwards we
151 * can safely write the new data into the register.
152 */
153 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
154 reg = 0;
155 rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
156 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
157 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
158 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
159
160 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
161 }
162
163 mutex_unlock(&rt2x00dev->csr_mutex);
164}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100165
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100166static void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
167 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100168{
169 u32 reg;
170
171 mutex_lock(&rt2x00dev->csr_mutex);
172
173 /*
174 * Wait until the RFCSR becomes available, afterwards we
175 * can safely write the read request into the register.
176 * After the data has been written, we wait until hardware
177 * returns the correct value, if at any time the register
178 * doesn't become available in time, reg will be 0xffffffff
179 * which means we return 0xff to the caller.
180 */
181 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
182 reg = 0;
183 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
184 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
185 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
186
187 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
188
189 WAIT_FOR_RFCSR(rt2x00dev, &reg);
190 }
191
192 *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
193
194 mutex_unlock(&rt2x00dev->csr_mutex);
195}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100196
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100197static void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
198 const unsigned int word, const u32 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100199{
200 u32 reg;
201
202 mutex_lock(&rt2x00dev->csr_mutex);
203
204 /*
205 * Wait until the RF becomes available, afterwards we
206 * can safely write the new data into the register.
207 */
208 if (WAIT_FOR_RF(rt2x00dev, &reg)) {
209 reg = 0;
210 rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
211 rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
212 rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
213 rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
214
215 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
216 rt2x00_rf_write(rt2x00dev, word, value);
217 }
218
219 mutex_unlock(&rt2x00dev->csr_mutex);
220}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100221
Gabor Juhos379448f2013-07-08 11:25:55 +0200222static const unsigned int rt2800_eeprom_map[EEPROM_WORD_COUNT] = {
223 [EEPROM_CHIP_ID] = 0x0000,
224 [EEPROM_VERSION] = 0x0001,
225 [EEPROM_MAC_ADDR_0] = 0x0002,
226 [EEPROM_MAC_ADDR_1] = 0x0003,
227 [EEPROM_MAC_ADDR_2] = 0x0004,
228 [EEPROM_NIC_CONF0] = 0x001a,
229 [EEPROM_NIC_CONF1] = 0x001b,
230 [EEPROM_FREQ] = 0x001d,
231 [EEPROM_LED_AG_CONF] = 0x001e,
232 [EEPROM_LED_ACT_CONF] = 0x001f,
233 [EEPROM_LED_POLARITY] = 0x0020,
234 [EEPROM_NIC_CONF2] = 0x0021,
235 [EEPROM_LNA] = 0x0022,
236 [EEPROM_RSSI_BG] = 0x0023,
237 [EEPROM_RSSI_BG2] = 0x0024,
238 [EEPROM_TXMIXER_GAIN_BG] = 0x0024, /* overlaps with RSSI_BG2 */
239 [EEPROM_RSSI_A] = 0x0025,
240 [EEPROM_RSSI_A2] = 0x0026,
241 [EEPROM_TXMIXER_GAIN_A] = 0x0026, /* overlaps with RSSI_A2 */
242 [EEPROM_EIRP_MAX_TX_POWER] = 0x0027,
243 [EEPROM_TXPOWER_DELTA] = 0x0028,
244 [EEPROM_TXPOWER_BG1] = 0x0029,
245 [EEPROM_TXPOWER_BG2] = 0x0030,
246 [EEPROM_TSSI_BOUND_BG1] = 0x0037,
247 [EEPROM_TSSI_BOUND_BG2] = 0x0038,
248 [EEPROM_TSSI_BOUND_BG3] = 0x0039,
249 [EEPROM_TSSI_BOUND_BG4] = 0x003a,
250 [EEPROM_TSSI_BOUND_BG5] = 0x003b,
251 [EEPROM_TXPOWER_A1] = 0x003c,
252 [EEPROM_TXPOWER_A2] = 0x0053,
253 [EEPROM_TSSI_BOUND_A1] = 0x006a,
254 [EEPROM_TSSI_BOUND_A2] = 0x006b,
255 [EEPROM_TSSI_BOUND_A3] = 0x006c,
256 [EEPROM_TSSI_BOUND_A4] = 0x006d,
257 [EEPROM_TSSI_BOUND_A5] = 0x006e,
258 [EEPROM_TXPOWER_BYRATE] = 0x006f,
259 [EEPROM_BBP_START] = 0x0078,
260};
261
Gabor Juhosfa31d152013-07-08 11:25:56 +0200262static const unsigned int rt2800_eeprom_map_ext[EEPROM_WORD_COUNT] = {
263 [EEPROM_CHIP_ID] = 0x0000,
264 [EEPROM_VERSION] = 0x0001,
265 [EEPROM_MAC_ADDR_0] = 0x0002,
266 [EEPROM_MAC_ADDR_1] = 0x0003,
267 [EEPROM_MAC_ADDR_2] = 0x0004,
268 [EEPROM_NIC_CONF0] = 0x001a,
269 [EEPROM_NIC_CONF1] = 0x001b,
270 [EEPROM_NIC_CONF2] = 0x001c,
271 [EEPROM_EIRP_MAX_TX_POWER] = 0x0020,
272 [EEPROM_FREQ] = 0x0022,
273 [EEPROM_LED_AG_CONF] = 0x0023,
274 [EEPROM_LED_ACT_CONF] = 0x0024,
275 [EEPROM_LED_POLARITY] = 0x0025,
276 [EEPROM_LNA] = 0x0026,
277 [EEPROM_EXT_LNA2] = 0x0027,
278 [EEPROM_RSSI_BG] = 0x0028,
Gabor Juhosfa31d152013-07-08 11:25:56 +0200279 [EEPROM_RSSI_BG2] = 0x0029,
Gabor Juhosfa31d152013-07-08 11:25:56 +0200280 [EEPROM_RSSI_A] = 0x002a,
281 [EEPROM_RSSI_A2] = 0x002b,
Gabor Juhosfa31d152013-07-08 11:25:56 +0200282 [EEPROM_TXPOWER_BG1] = 0x0030,
283 [EEPROM_TXPOWER_BG2] = 0x0037,
284 [EEPROM_EXT_TXPOWER_BG3] = 0x003e,
285 [EEPROM_TSSI_BOUND_BG1] = 0x0045,
286 [EEPROM_TSSI_BOUND_BG2] = 0x0046,
287 [EEPROM_TSSI_BOUND_BG3] = 0x0047,
288 [EEPROM_TSSI_BOUND_BG4] = 0x0048,
289 [EEPROM_TSSI_BOUND_BG5] = 0x0049,
290 [EEPROM_TXPOWER_A1] = 0x004b,
291 [EEPROM_TXPOWER_A2] = 0x0065,
292 [EEPROM_EXT_TXPOWER_A3] = 0x007f,
293 [EEPROM_TSSI_BOUND_A1] = 0x009a,
294 [EEPROM_TSSI_BOUND_A2] = 0x009b,
295 [EEPROM_TSSI_BOUND_A3] = 0x009c,
296 [EEPROM_TSSI_BOUND_A4] = 0x009d,
297 [EEPROM_TSSI_BOUND_A5] = 0x009e,
298 [EEPROM_TXPOWER_BYRATE] = 0x00a0,
299};
300
Gabor Juhos379448f2013-07-08 11:25:55 +0200301static unsigned int rt2800_eeprom_word_index(struct rt2x00_dev *rt2x00dev,
302 const enum rt2800_eeprom_word word)
303{
304 const unsigned int *map;
305 unsigned int index;
306
307 if (WARN_ONCE(word >= EEPROM_WORD_COUNT,
308 "%s: invalid EEPROM word %d\n",
309 wiphy_name(rt2x00dev->hw->wiphy), word))
310 return 0;
311
Gabor Juhosfa31d152013-07-08 11:25:56 +0200312 if (rt2x00_rt(rt2x00dev, RT3593))
313 map = rt2800_eeprom_map_ext;
314 else
315 map = rt2800_eeprom_map;
316
Gabor Juhos379448f2013-07-08 11:25:55 +0200317 index = map[word];
318
319 /* Index 0 is valid only for EEPROM_CHIP_ID.
320 * Otherwise it means that the offset of the
321 * given word is not initialized in the map,
322 * or that the field is not usable on the
323 * actual chipset.
324 */
325 WARN_ONCE(word != EEPROM_CHIP_ID && index == 0,
326 "%s: invalid access of EEPROM word %d\n",
327 wiphy_name(rt2x00dev->hw->wiphy), word);
328
329 return index;
330}
331
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200332static void *rt2800_eeprom_addr(struct rt2x00_dev *rt2x00dev,
333 const enum rt2800_eeprom_word word)
334{
Gabor Juhos379448f2013-07-08 11:25:55 +0200335 unsigned int index;
336
337 index = rt2800_eeprom_word_index(rt2x00dev, word);
338 return rt2x00_eeprom_addr(rt2x00dev, index);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200339}
340
341static void rt2800_eeprom_read(struct rt2x00_dev *rt2x00dev,
342 const enum rt2800_eeprom_word word, u16 *data)
343{
Gabor Juhos379448f2013-07-08 11:25:55 +0200344 unsigned int index;
345
346 index = rt2800_eeprom_word_index(rt2x00dev, word);
347 rt2x00_eeprom_read(rt2x00dev, index, data);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200348}
349
350static void rt2800_eeprom_write(struct rt2x00_dev *rt2x00dev,
351 const enum rt2800_eeprom_word word, u16 data)
352{
Gabor Juhos379448f2013-07-08 11:25:55 +0200353 unsigned int index;
354
355 index = rt2800_eeprom_word_index(rt2x00dev, word);
356 rt2x00_eeprom_write(rt2x00dev, index, data);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200357}
358
Gabor Juhos022138c2013-07-08 11:25:54 +0200359static void rt2800_eeprom_read_from_array(struct rt2x00_dev *rt2x00dev,
360 const enum rt2800_eeprom_word array,
361 unsigned int offset,
362 u16 *data)
363{
Gabor Juhos379448f2013-07-08 11:25:55 +0200364 unsigned int index;
365
366 index = rt2800_eeprom_word_index(rt2x00dev, array);
367 rt2x00_eeprom_read(rt2x00dev, index + offset, data);
Gabor Juhos022138c2013-07-08 11:25:54 +0200368}
369
Woody Hung16ebd602012-07-31 21:53:33 +0800370static int rt2800_enable_wlan_rt3290(struct rt2x00_dev *rt2x00dev)
371{
372 u32 reg;
373 int i, count;
374
375 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
376 if (rt2x00_get_field32(reg, WLAN_EN))
377 return 0;
378
379 rt2x00_set_field32(&reg, WLAN_GPIO_OUT_OE_BIT_ALL, 0xff);
380 rt2x00_set_field32(&reg, FRC_WL_ANT_SET, 1);
381 rt2x00_set_field32(&reg, WLAN_CLK_EN, 0);
382 rt2x00_set_field32(&reg, WLAN_EN, 1);
383 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
384
385 udelay(REGISTER_BUSY_DELAY);
386
387 count = 0;
388 do {
389 /*
390 * Check PLL_LD & XTAL_RDY.
391 */
392 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
393 rt2800_register_read(rt2x00dev, CMB_CTRL, &reg);
394 if (rt2x00_get_field32(reg, PLL_LD) &&
395 rt2x00_get_field32(reg, XTAL_RDY))
396 break;
397 udelay(REGISTER_BUSY_DELAY);
398 }
399
400 if (i >= REGISTER_BUSY_COUNT) {
401
402 if (count >= 10)
403 return -EIO;
404
405 rt2800_register_write(rt2x00dev, 0x58, 0x018);
406 udelay(REGISTER_BUSY_DELAY);
407 rt2800_register_write(rt2x00dev, 0x58, 0x418);
408 udelay(REGISTER_BUSY_DELAY);
409 rt2800_register_write(rt2x00dev, 0x58, 0x618);
410 udelay(REGISTER_BUSY_DELAY);
411 count++;
412 } else {
413 count = 0;
414 }
415
416 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
417 rt2x00_set_field32(&reg, PCIE_APP0_CLK_REQ, 0);
418 rt2x00_set_field32(&reg, WLAN_CLK_EN, 1);
419 rt2x00_set_field32(&reg, WLAN_RESET, 1);
420 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
421 udelay(10);
422 rt2x00_set_field32(&reg, WLAN_RESET, 0);
423 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
424 udelay(10);
425 rt2800_register_write(rt2x00dev, INT_SOURCE_CSR, 0x7fffffff);
426 } while (count != 0);
427
428 return 0;
429}
430
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100431void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
432 const u8 command, const u8 token,
433 const u8 arg0, const u8 arg1)
434{
435 u32 reg;
436
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100437 /*
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100438 * SOC devices don't support MCU requests.
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100439 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100440 if (rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100441 return;
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100442
443 mutex_lock(&rt2x00dev->csr_mutex);
444
445 /*
446 * Wait until the MCU becomes available, afterwards we
447 * can safely write the new data into the register.
448 */
449 if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
450 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
451 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
452 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
453 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
454 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
455
456 reg = 0;
457 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
458 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
459 }
460
461 mutex_unlock(&rt2x00dev->csr_mutex);
462}
463EXPORT_SYMBOL_GPL(rt2800_mcu_request);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100464
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200465int rt2800_wait_csr_ready(struct rt2x00_dev *rt2x00dev)
466{
467 unsigned int i = 0;
468 u32 reg;
469
470 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
471 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
472 if (reg && reg != ~0)
473 return 0;
474 msleep(1);
475 }
476
Joe Perchesec9c4982013-04-19 08:33:40 -0700477 rt2x00_err(rt2x00dev, "Unstable hardware\n");
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200478 return -EBUSY;
479}
480EXPORT_SYMBOL_GPL(rt2800_wait_csr_ready);
481
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100482int rt2800_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
483{
484 unsigned int i;
485 u32 reg;
486
Helmut Schaa08e53102010-11-04 20:37:47 +0100487 /*
488 * Some devices are really slow to respond here. Wait a whole second
489 * before timing out.
490 */
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100491 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
492 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
493 if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
494 !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
495 return 0;
496
Helmut Schaa08e53102010-11-04 20:37:47 +0100497 msleep(10);
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100498 }
499
Joe Perchesec9c4982013-04-19 08:33:40 -0700500 rt2x00_err(rt2x00dev, "WPDMA TX/RX busy [0x%08x]\n", reg);
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100501 return -EACCES;
502}
503EXPORT_SYMBOL_GPL(rt2800_wait_wpdma_ready);
504
Jakub Kicinskif7b395e2012-04-03 03:40:47 +0200505void rt2800_disable_wpdma(struct rt2x00_dev *rt2x00dev)
506{
507 u32 reg;
508
509 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
510 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
511 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
512 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
513 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
514 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
515 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
516}
517EXPORT_SYMBOL_GPL(rt2800_disable_wpdma);
518
Gabor Juhosae1b1c52013-08-16 10:23:29 +0200519void rt2800_get_txwi_rxwi_size(struct rt2x00_dev *rt2x00dev,
520 unsigned short *txwi_size,
521 unsigned short *rxwi_size)
522{
523 switch (rt2x00dev->chip.rt) {
524 case RT3593:
525 *txwi_size = TXWI_DESC_SIZE_4WORDS;
526 *rxwi_size = RXWI_DESC_SIZE_5WORDS;
527 break;
528
529 case RT5592:
530 *txwi_size = TXWI_DESC_SIZE_5WORDS;
531 *rxwi_size = RXWI_DESC_SIZE_6WORDS;
532 break;
533
534 default:
535 *txwi_size = TXWI_DESC_SIZE_4WORDS;
536 *rxwi_size = RXWI_DESC_SIZE_4WORDS;
537 break;
538 }
539}
540EXPORT_SYMBOL_GPL(rt2800_get_txwi_rxwi_size);
541
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200542static bool rt2800_check_firmware_crc(const u8 *data, const size_t len)
543{
544 u16 fw_crc;
545 u16 crc;
546
547 /*
548 * The last 2 bytes in the firmware array are the crc checksum itself,
549 * this means that we should never pass those 2 bytes to the crc
550 * algorithm.
551 */
552 fw_crc = (data[len - 2] << 8 | data[len - 1]);
553
554 /*
555 * Use the crc ccitt algorithm.
556 * This will return the same value as the legacy driver which
557 * used bit ordering reversion on the both the firmware bytes
558 * before input input as well as on the final output.
559 * Obviously using crc ccitt directly is much more efficient.
560 */
561 crc = crc_ccitt(~0, data, len - 2);
562
563 /*
564 * There is a small difference between the crc-itu-t + bitrev and
565 * the crc-ccitt crc calculation. In the latter method the 2 bytes
566 * will be swapped, use swab16 to convert the crc to the correct
567 * value.
568 */
569 crc = swab16(crc);
570
571 return fw_crc == crc;
572}
573
574int rt2800_check_firmware(struct rt2x00_dev *rt2x00dev,
575 const u8 *data, const size_t len)
576{
577 size_t offset = 0;
578 size_t fw_len;
579 bool multiple;
580
581 /*
582 * PCI(e) & SOC devices require firmware with a length
583 * of 8kb. USB devices require firmware files with a length
584 * of 4kb. Certain USB chipsets however require different firmware,
585 * which Ralink only provides attached to the original firmware
586 * file. Thus for USB devices, firmware files have a length
Woody Hunga89534e2012-06-13 15:01:16 +0800587 * which is a multiple of 4kb. The firmware for rt3290 chip also
588 * have a length which is a multiple of 4kb.
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200589 */
Woody Hunga89534e2012-06-13 15:01:16 +0800590 if (rt2x00_is_usb(rt2x00dev) || rt2x00_rt(rt2x00dev, RT3290))
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200591 fw_len = 4096;
Woody Hunga89534e2012-06-13 15:01:16 +0800592 else
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200593 fw_len = 8192;
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200594
Woody Hunga89534e2012-06-13 15:01:16 +0800595 multiple = true;
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200596 /*
597 * Validate the firmware length
598 */
599 if (len != fw_len && (!multiple || (len % fw_len) != 0))
600 return FW_BAD_LENGTH;
601
602 /*
603 * Check if the chipset requires one of the upper parts
604 * of the firmware.
605 */
606 if (rt2x00_is_usb(rt2x00dev) &&
607 !rt2x00_rt(rt2x00dev, RT2860) &&
608 !rt2x00_rt(rt2x00dev, RT2872) &&
609 !rt2x00_rt(rt2x00dev, RT3070) &&
610 ((len / fw_len) == 1))
611 return FW_BAD_VERSION;
612
613 /*
614 * 8kb firmware files must be checked as if it were
615 * 2 separate firmware files.
616 */
617 while (offset < len) {
618 if (!rt2800_check_firmware_crc(data + offset, fw_len))
619 return FW_BAD_CRC;
620
621 offset += fw_len;
622 }
623
624 return FW_OK;
625}
626EXPORT_SYMBOL_GPL(rt2800_check_firmware);
627
628int rt2800_load_firmware(struct rt2x00_dev *rt2x00dev,
629 const u8 *data, const size_t len)
630{
631 unsigned int i;
632 u32 reg;
Woody Hung16ebd602012-07-31 21:53:33 +0800633 int retval;
634
635 if (rt2x00_rt(rt2x00dev, RT3290)) {
636 retval = rt2800_enable_wlan_rt3290(rt2x00dev);
637 if (retval)
638 return -EBUSY;
639 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200640
641 /*
Ivo van Doornb9eca242010-08-30 21:13:54 +0200642 * If driver doesn't wake up firmware here,
643 * rt2800_load_firmware will hang forever when interface is up again.
644 */
645 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0x00000000);
646
647 /*
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200648 * Wait for stable hardware.
649 */
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200650 if (rt2800_wait_csr_ready(rt2x00dev))
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200651 return -EBUSY;
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200652
Gabor Juhosadde5882011-03-03 11:46:45 +0100653 if (rt2x00_is_pci(rt2x00dev)) {
Woody Hunga89534e2012-06-13 15:01:16 +0800654 if (rt2x00_rt(rt2x00dev, RT3290) ||
655 rt2x00_rt(rt2x00dev, RT3572) ||
John Li2ed71882012-02-17 17:33:06 +0800656 rt2x00_rt(rt2x00dev, RT5390) ||
657 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +0100658 rt2800_register_read(rt2x00dev, AUX_CTRL, &reg);
659 rt2x00_set_field32(&reg, AUX_CTRL_FORCE_PCIE_CLK, 1);
660 rt2x00_set_field32(&reg, AUX_CTRL_WAKE_PCIE_EN, 1);
661 rt2800_register_write(rt2x00dev, AUX_CTRL, reg);
662 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200663 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
Gabor Juhosadde5882011-03-03 11:46:45 +0100664 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200665
Jakub Kicinskib7e1d222012-04-03 03:40:48 +0200666 rt2800_disable_wpdma(rt2x00dev);
667
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200668 /*
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200669 * Write firmware to the device.
670 */
671 rt2800_drv_write_firmware(rt2x00dev, data, len);
672
673 /*
674 * Wait for device to stabilize.
675 */
676 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
677 rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
678 if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
679 break;
680 msleep(1);
681 }
682
683 if (i == REGISTER_BUSY_COUNT) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700684 rt2x00_err(rt2x00dev, "PBF system register not ready\n");
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200685 return -EBUSY;
686 }
687
688 /*
Stanislaw Gruszka4ed1dd22012-01-24 14:09:07 +0100689 * Disable DMA, will be reenabled later when enabling
690 * the radio.
691 */
Jakub Kicinskif7b395e2012-04-03 03:40:47 +0200692 rt2800_disable_wpdma(rt2x00dev);
Stanislaw Gruszka4ed1dd22012-01-24 14:09:07 +0100693
694 /*
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200695 * Initialize firmware.
696 */
697 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
698 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
Stanislaw Gruszka87561302013-03-16 19:19:45 +0100699 if (rt2x00_is_usb(rt2x00dev)) {
Stanislaw Gruszka0c17cf92012-01-24 14:09:06 +0100700 rt2800_register_write(rt2x00dev, H2M_INT_SRC, 0);
Stanislaw Gruszka87561302013-03-16 19:19:45 +0100701 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
702 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200703 msleep(1);
704
705 return 0;
706}
707EXPORT_SYMBOL_GPL(rt2800_load_firmware);
708
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200709void rt2800_write_tx_data(struct queue_entry *entry,
710 struct txentry_desc *txdesc)
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200711{
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200712 __le32 *txwi = rt2800_drv_get_txwi(entry);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200713 u32 word;
Stanislaw Gruszka557985a2013-04-17 14:30:48 +0200714 int i;
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200715
716 /*
717 * Initialize TX Info descriptor
718 */
719 rt2x00_desc_read(txwi, 0, &word);
720 rt2x00_set_field32(&word, TXWI_W0_FRAG,
721 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
Ivo van Doorn84804cd2010-08-06 20:46:19 +0200722 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS,
723 test_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags));
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200724 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
725 rt2x00_set_field32(&word, TXWI_W0_TS,
726 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
727 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
728 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100729 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY,
730 txdesc->u.ht.mpdu_density);
731 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->u.ht.txop);
732 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->u.ht.mcs);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200733 rt2x00_set_field32(&word, TXWI_W0_BW,
734 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
735 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
736 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100737 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->u.ht.stbc);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200738 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
739 rt2x00_desc_write(txwi, 0, word);
740
741 rt2x00_desc_read(txwi, 1, &word);
742 rt2x00_set_field32(&word, TXWI_W1_ACK,
743 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
744 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
745 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100746 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->u.ht.ba_size);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200747 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
748 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
Helmut Schaaa2b13282011-09-08 14:38:01 +0200749 txdesc->key_idx : txdesc->u.ht.wcid);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200750 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
751 txdesc->length);
Helmut Schaa2b23cda2010-11-04 20:38:15 +0100752 rt2x00_set_field32(&word, TXWI_W1_PACKETID_QUEUE, entry->queue->qid);
Ivo van Doornbc8a9792010-10-02 11:32:43 +0200753 rt2x00_set_field32(&word, TXWI_W1_PACKETID_ENTRY, (entry->entry_idx % 3) + 1);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200754 rt2x00_desc_write(txwi, 1, word);
755
756 /*
Stanislaw Gruszka557985a2013-04-17 14:30:48 +0200757 * Always write 0 to IV/EIV fields (word 2 and 3), hardware will insert
758 * the IV from the IVEIV register when TXD_W3_WIV is set to 0.
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200759 * When TXD_W3_WIV is set to 1 it will use the IV data
760 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
761 * crypto entry in the registers should be used to encrypt the frame.
Stanislaw Gruszka557985a2013-04-17 14:30:48 +0200762 *
763 * Nulify all remaining words as well, we don't know how to program them.
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200764 */
Stanislaw Gruszka557985a2013-04-17 14:30:48 +0200765 for (i = 2; i < entry->queue->winfo_size / sizeof(__le32); i++)
766 _rt2x00_desc_write(txwi, i, 0);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200767}
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200768EXPORT_SYMBOL_GPL(rt2800_write_tx_data);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200769
Helmut Schaaff6133b2010-10-09 13:34:11 +0200770static int rt2800_agc_to_rssi(struct rt2x00_dev *rt2x00dev, u32 rxwi_w2)
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200771{
Luigi Tarenga7fc41752012-01-31 18:51:23 +0100772 s8 rssi0 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI0);
773 s8 rssi1 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI1);
774 s8 rssi2 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI2);
Ivo van Doorn74861922010-07-11 12:23:50 +0200775 u16 eeprom;
776 u8 offset0;
777 u8 offset1;
778 u8 offset2;
779
Johannes Berg57fbcce2016-04-12 15:56:15 +0200780 if (rt2x00dev->curr_band == NL80211_BAND_2GHZ) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200781 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &eeprom);
Ivo van Doorn74861922010-07-11 12:23:50 +0200782 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET0);
783 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET1);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200784 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
Ivo van Doorn74861922010-07-11 12:23:50 +0200785 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_OFFSET2);
786 } else {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200787 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &eeprom);
Ivo van Doorn74861922010-07-11 12:23:50 +0200788 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET0);
789 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET1);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +0200790 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
Ivo van Doorn74861922010-07-11 12:23:50 +0200791 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_OFFSET2);
792 }
793
794 /*
795 * Convert the value from the descriptor into the RSSI value
796 * If the value in the descriptor is 0, it is considered invalid
797 * and the default (extremely low) rssi value is assumed
798 */
799 rssi0 = (rssi0) ? (-12 - offset0 - rt2x00dev->lna_gain - rssi0) : -128;
800 rssi1 = (rssi1) ? (-12 - offset1 - rt2x00dev->lna_gain - rssi1) : -128;
801 rssi2 = (rssi2) ? (-12 - offset2 - rt2x00dev->lna_gain - rssi2) : -128;
802
803 /*
804 * mac80211 only accepts a single RSSI value. Calculating the
805 * average doesn't deliver a fair answer either since -60:-60 would
806 * be considered equally good as -50:-70 while the second is the one
807 * which gives less energy...
808 */
809 rssi0 = max(rssi0, rssi1);
Luigi Tarenga7fc41752012-01-31 18:51:23 +0100810 return (int)max(rssi0, rssi2);
Ivo van Doorn74861922010-07-11 12:23:50 +0200811}
812
813void rt2800_process_rxwi(struct queue_entry *entry,
814 struct rxdone_entry_desc *rxdesc)
815{
816 __le32 *rxwi = (__le32 *) entry->skb->data;
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200817 u32 word;
818
819 rt2x00_desc_read(rxwi, 0, &word);
820
821 rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
822 rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
823
824 rt2x00_desc_read(rxwi, 1, &word);
825
826 if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
827 rxdesc->flags |= RX_FLAG_SHORT_GI;
828
829 if (rt2x00_get_field32(word, RXWI_W1_BW))
830 rxdesc->flags |= RX_FLAG_40MHZ;
831
832 /*
833 * Detect RX rate, always use MCS as signal type.
834 */
835 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
836 rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
837 rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
838
839 /*
840 * Mask of 0x8 bit to remove the short preamble flag.
841 */
842 if (rxdesc->rate_mode == RATE_MODE_CCK)
843 rxdesc->signal &= ~0x8;
844
845 rt2x00_desc_read(rxwi, 2, &word);
846
Ivo van Doorn74861922010-07-11 12:23:50 +0200847 /*
848 * Convert descriptor AGC value to RSSI value.
849 */
850 rxdesc->rssi = rt2800_agc_to_rssi(entry->queue->rt2x00dev, word);
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +0200851 /*
852 * Remove RXWI descriptor from start of the buffer.
853 */
854 skb_pull(entry->skb, entry->queue->winfo_size);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200855}
856EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
857
Helmut Schaa31937c42011-09-07 20:10:02 +0200858void rt2800_txdone_entry(struct queue_entry *entry, u32 status, __le32 *txwi)
Helmut Schaa14433332010-10-02 11:27:03 +0200859{
860 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Helmut Schaab34793e2010-10-02 11:34:56 +0200861 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Helmut Schaa14433332010-10-02 11:27:03 +0200862 struct txdone_entry_desc txdesc;
863 u32 word;
864 u16 mcs, real_mcs;
Helmut Schaab34793e2010-10-02 11:34:56 +0200865 int aggr, ampdu;
Helmut Schaa14433332010-10-02 11:27:03 +0200866
867 /*
868 * Obtain the status about this packet.
869 */
870 txdesc.flags = 0;
Helmut Schaa14433332010-10-02 11:27:03 +0200871 rt2x00_desc_read(txwi, 0, &word);
Helmut Schaab34793e2010-10-02 11:34:56 +0200872
Helmut Schaa14433332010-10-02 11:27:03 +0200873 mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200874 ampdu = rt2x00_get_field32(word, TXWI_W0_AMPDU);
875
Helmut Schaa14433332010-10-02 11:27:03 +0200876 real_mcs = rt2x00_get_field32(status, TX_STA_FIFO_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200877 aggr = rt2x00_get_field32(status, TX_STA_FIFO_TX_AGGRE);
878
879 /*
880 * If a frame was meant to be sent as a single non-aggregated MPDU
881 * but ended up in an aggregate the used tx rate doesn't correlate
882 * with the one specified in the TXWI as the whole aggregate is sent
883 * with the same rate.
884 *
885 * For example: two frames are sent to rt2x00, the first one sets
886 * AMPDU=1 and requests MCS7 whereas the second frame sets AMDPU=0
887 * and requests MCS15. If the hw aggregates both frames into one
888 * AMDPU the tx status for both frames will contain MCS7 although
889 * the frame was sent successfully.
890 *
891 * Hence, replace the requested rate with the real tx rate to not
892 * confuse the rate control algortihm by providing clearly wrong
893 * data.
894 */
Helmut Schaa5356d962011-03-03 19:40:33 +0100895 if (unlikely(aggr == 1 && ampdu == 0 && real_mcs != mcs)) {
Helmut Schaab34793e2010-10-02 11:34:56 +0200896 skbdesc->tx_rate_idx = real_mcs;
897 mcs = real_mcs;
898 }
Helmut Schaa14433332010-10-02 11:27:03 +0200899
Helmut Schaaf16d2db2011-03-28 13:35:21 +0200900 if (aggr == 1 || ampdu == 1)
901 __set_bit(TXDONE_AMPDU, &txdesc.flags);
902
Helmut Schaa14433332010-10-02 11:27:03 +0200903 /*
904 * Ralink has a retry mechanism using a global fallback
905 * table. We setup this fallback table to try the immediate
906 * lower rate for all rates. In the TX_STA_FIFO, the MCS field
907 * always contains the MCS used for the last transmission, be
908 * it successful or not.
909 */
910 if (rt2x00_get_field32(status, TX_STA_FIFO_TX_SUCCESS)) {
911 /*
912 * Transmission succeeded. The number of retries is
913 * mcs - real_mcs
914 */
915 __set_bit(TXDONE_SUCCESS, &txdesc.flags);
916 txdesc.retry = ((mcs > real_mcs) ? mcs - real_mcs : 0);
917 } else {
918 /*
919 * Transmission failed. The number of retries is
920 * always 7 in this case (for a total number of 8
921 * frames sent).
922 */
923 __set_bit(TXDONE_FAILURE, &txdesc.flags);
924 txdesc.retry = rt2x00dev->long_retry;
925 }
926
927 /*
928 * the frame was retried at least once
929 * -> hw used fallback rates
930 */
931 if (txdesc.retry)
932 __set_bit(TXDONE_FALLBACK, &txdesc.flags);
933
934 rt2x00lib_txdone(entry, &txdesc);
935}
936EXPORT_SYMBOL_GPL(rt2800_txdone_entry);
937
Gabor Juhos21c6af62013-08-22 20:53:21 +0200938static unsigned int rt2800_hw_beacon_base(struct rt2x00_dev *rt2x00dev,
939 unsigned int index)
940{
941 return HW_BEACON_BASE(index);
942}
943
Gabor Juhos634b8052013-08-22 20:53:22 +0200944static inline u8 rt2800_get_beacon_offset(struct rt2x00_dev *rt2x00dev,
945 unsigned int index)
946{
947 return BEACON_BASE_TO_OFFSET(rt2800_hw_beacon_base(rt2x00dev, index));
948}
949
Stanislaw Gruszkaba089102014-06-05 13:52:24 +0200950static void rt2800_update_beacons_setup(struct rt2x00_dev *rt2x00dev)
951{
952 struct data_queue *queue = rt2x00dev->bcn;
953 struct queue_entry *entry;
954 int i, bcn_num = 0;
955 u64 off, reg = 0;
956 u32 bssid_dw1;
957
958 /*
959 * Setup offsets of all active beacons in BCN_OFFSET{0,1} registers.
960 */
961 for (i = 0; i < queue->limit; i++) {
962 entry = &queue->entries[i];
963 if (!test_bit(ENTRY_BCN_ENABLED, &entry->flags))
964 continue;
965 off = rt2800_get_beacon_offset(rt2x00dev, entry->entry_idx);
966 reg |= off << (8 * bcn_num);
967 bcn_num++;
968 }
969
970 WARN_ON_ONCE(bcn_num != rt2x00dev->intf_beaconing);
971
972 rt2800_register_write(rt2x00dev, BCN_OFFSET0, (u32) reg);
973 rt2800_register_write(rt2x00dev, BCN_OFFSET1, (u32) (reg >> 32));
974
975 /*
976 * H/W sends up to MAC_BSSID_DW1_BSS_BCN_NUM + 1 consecutive beacons.
977 */
978 rt2800_register_read(rt2x00dev, MAC_BSSID_DW1, &bssid_dw1);
979 rt2x00_set_field32(&bssid_dw1, MAC_BSSID_DW1_BSS_BCN_NUM,
980 bcn_num > 0 ? bcn_num - 1 : 0);
981 rt2800_register_write(rt2x00dev, MAC_BSSID_DW1, bssid_dw1);
982}
983
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200984void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
985{
986 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
987 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
988 unsigned int beacon_base;
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100989 unsigned int padding_len;
Seth Forsheed76dfc62011-02-14 08:52:25 -0600990 u32 orig_reg, reg;
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +0200991 const int txwi_desc_size = entry->queue->winfo_size;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200992
993 /*
994 * Disable beaconing while we are reloading the beacon data,
995 * otherwise we might be sending out invalid data.
996 */
997 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Seth Forsheed76dfc62011-02-14 08:52:25 -0600998 orig_reg = reg;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200999 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1000 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1001
1002 /*
1003 * Add space for the TXWI in front of the skb.
1004 */
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +02001005 memset(skb_push(entry->skb, txwi_desc_size), 0, txwi_desc_size);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +02001006
1007 /*
1008 * Register descriptor details in skb frame descriptor.
1009 */
1010 skbdesc->flags |= SKBDESC_DESC_IN_SKB;
1011 skbdesc->desc = entry->skb->data;
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +02001012 skbdesc->desc_len = txwi_desc_size;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +02001013
1014 /*
1015 * Add the TXWI for the beacon to the skb.
1016 */
Ivo van Doorn0c5879b2010-08-06 20:47:20 +02001017 rt2800_write_tx_data(entry, txdesc);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +02001018
1019 /*
1020 * Dump beacon to userspace through debugfs.
1021 */
1022 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
1023
1024 /*
Wolfgang Kufner739fd942010-12-13 12:39:12 +01001025 * Write entire beacon with TXWI and padding to register.
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +02001026 */
Wolfgang Kufner739fd942010-12-13 12:39:12 +01001027 padding_len = roundup(entry->skb->len, 4) - entry->skb->len;
Seth Forsheed76dfc62011-02-14 08:52:25 -06001028 if (padding_len && skb_pad(entry->skb, padding_len)) {
Joe Perchesec9c4982013-04-19 08:33:40 -07001029 rt2x00_err(rt2x00dev, "Failure padding beacon, aborting\n");
Seth Forsheed76dfc62011-02-14 08:52:25 -06001030 /* skb freed by skb_pad() on failure */
1031 entry->skb = NULL;
1032 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, orig_reg);
1033 return;
1034 }
1035
Gabor Juhos21c6af62013-08-22 20:53:21 +02001036 beacon_base = rt2800_hw_beacon_base(rt2x00dev, entry->entry_idx);
1037
Wolfgang Kufner739fd942010-12-13 12:39:12 +01001038 rt2800_register_multiwrite(rt2x00dev, beacon_base, entry->skb->data,
1039 entry->skb->len + padding_len);
Stanislaw Gruszkaba089102014-06-05 13:52:24 +02001040 __set_bit(ENTRY_BCN_ENABLED, &entry->flags);
1041
1042 /*
1043 * Change global beacons settings.
1044 */
1045 rt2800_update_beacons_setup(rt2x00dev);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +02001046
1047 /*
Stanislaw Gruszkabc0df75a2014-04-17 11:08:48 +02001048 * Restore beaconing state.
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +02001049 */
Stanislaw Gruszkabc0df75a2014-04-17 11:08:48 +02001050 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, orig_reg);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +02001051
1052 /*
1053 * Clean up beacon skb.
1054 */
1055 dev_kfree_skb_any(entry->skb);
1056 entry->skb = NULL;
1057}
Ivo van Doorn50e888e2010-07-11 12:26:12 +02001058EXPORT_SYMBOL_GPL(rt2800_write_beacon);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +02001059
Helmut Schaa69cf36a2011-01-30 13:16:03 +01001060static inline void rt2800_clear_beacon_register(struct rt2x00_dev *rt2x00dev,
Gabor Juhos77f7c0f2013-08-17 00:15:50 +02001061 unsigned int index)
Helmut Schaafdb87252010-06-29 21:48:06 +02001062{
1063 int i;
Gabor Juhos0879f872013-05-01 17:17:33 +02001064 const int txwi_desc_size = rt2x00dev->bcn->winfo_size;
Gabor Juhos77f7c0f2013-08-17 00:15:50 +02001065 unsigned int beacon_base;
1066
Gabor Juhos21c6af62013-08-22 20:53:21 +02001067 beacon_base = rt2800_hw_beacon_base(rt2x00dev, index);
Helmut Schaafdb87252010-06-29 21:48:06 +02001068
1069 /*
1070 * For the Beacon base registers we only need to clear
1071 * the whole TXWI which (when set to 0) will invalidate
1072 * the entire beacon.
1073 */
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +02001074 for (i = 0; i < txwi_desc_size; i += sizeof(__le32))
Helmut Schaafdb87252010-06-29 21:48:06 +02001075 rt2800_register_write(rt2x00dev, beacon_base + i, 0);
1076}
1077
Helmut Schaa69cf36a2011-01-30 13:16:03 +01001078void rt2800_clear_beacon(struct queue_entry *entry)
1079{
1080 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Stanislaw Gruszkabc0df75a2014-04-17 11:08:48 +02001081 u32 orig_reg, reg;
Helmut Schaa69cf36a2011-01-30 13:16:03 +01001082
1083 /*
1084 * Disable beaconing while we are reloading the beacon data,
1085 * otherwise we might be sending out invalid data.
1086 */
Stanislaw Gruszkabc0df75a2014-04-17 11:08:48 +02001087 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &orig_reg);
1088 reg = orig_reg;
Helmut Schaa69cf36a2011-01-30 13:16:03 +01001089 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1090 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1091
1092 /*
1093 * Clear beacon.
1094 */
Gabor Juhos77f7c0f2013-08-17 00:15:50 +02001095 rt2800_clear_beacon_register(rt2x00dev, entry->entry_idx);
Stanislaw Gruszkaba089102014-06-05 13:52:24 +02001096 __clear_bit(ENTRY_BCN_ENABLED, &entry->flags);
Helmut Schaa69cf36a2011-01-30 13:16:03 +01001097
1098 /*
Stanislaw Gruszkaba089102014-06-05 13:52:24 +02001099 * Change global beacons settings.
1100 */
1101 rt2800_update_beacons_setup(rt2x00dev);
1102 /*
Stanislaw Gruszkabc0df75a2014-04-17 11:08:48 +02001103 * Restore beaconing state.
Helmut Schaa69cf36a2011-01-30 13:16:03 +01001104 */
Stanislaw Gruszkabc0df75a2014-04-17 11:08:48 +02001105 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, orig_reg);
Helmut Schaa69cf36a2011-01-30 13:16:03 +01001106}
1107EXPORT_SYMBOL_GPL(rt2800_clear_beacon);
1108
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001109#ifdef CONFIG_RT2X00_LIB_DEBUGFS
1110const struct rt2x00debug rt2800_rt2x00debug = {
1111 .owner = THIS_MODULE,
1112 .csr = {
1113 .read = rt2800_register_read,
1114 .write = rt2800_register_write,
1115 .flags = RT2X00DEBUGFS_OFFSET,
1116 .word_base = CSR_REG_BASE,
1117 .word_size = sizeof(u32),
1118 .word_count = CSR_REG_SIZE / sizeof(u32),
1119 },
1120 .eeprom = {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001121 /* NOTE: The local EEPROM access functions can't
1122 * be used here, use the generic versions instead.
1123 */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001124 .read = rt2x00_eeprom_read,
1125 .write = rt2x00_eeprom_write,
1126 .word_base = EEPROM_BASE,
1127 .word_size = sizeof(u16),
1128 .word_count = EEPROM_SIZE / sizeof(u16),
1129 },
1130 .bbp = {
1131 .read = rt2800_bbp_read,
1132 .write = rt2800_bbp_write,
1133 .word_base = BBP_BASE,
1134 .word_size = sizeof(u8),
1135 .word_count = BBP_SIZE / sizeof(u8),
1136 },
1137 .rf = {
1138 .read = rt2x00_rf_read,
1139 .write = rt2800_rf_write,
1140 .word_base = RF_BASE,
1141 .word_size = sizeof(u32),
1142 .word_count = RF_SIZE / sizeof(u32),
1143 },
Anisse Astierf2bd7f12012-04-19 15:53:10 +02001144 .rfcsr = {
1145 .read = rt2800_rfcsr_read,
1146 .write = rt2800_rfcsr_write,
1147 .word_base = RFCSR_BASE,
1148 .word_size = sizeof(u8),
1149 .word_count = RFCSR_SIZE / sizeof(u8),
1150 },
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001151};
1152EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
1153#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
1154
1155int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
1156{
1157 u32 reg;
1158
Woody Hunga89534e2012-06-13 15:01:16 +08001159 if (rt2x00_rt(rt2x00dev, RT3290)) {
1160 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
1161 return rt2x00_get_field32(reg, WLAN_GPIO_IN_BIT0);
1162 } else {
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02001163 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
1164 return rt2x00_get_field32(reg, GPIO_CTRL_VAL2);
Woody Hunga89534e2012-06-13 15:01:16 +08001165 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001166}
1167EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
1168
1169#ifdef CONFIG_RT2X00_LIB_LEDS
1170static void rt2800_brightness_set(struct led_classdev *led_cdev,
1171 enum led_brightness brightness)
1172{
1173 struct rt2x00_led *led =
1174 container_of(led_cdev, struct rt2x00_led, led_dev);
1175 unsigned int enabled = brightness != LED_OFF;
1176 unsigned int bg_mode =
Johannes Berg57fbcce2016-04-12 15:56:15 +02001177 (enabled && led->rt2x00dev->curr_band == NL80211_BAND_2GHZ);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001178 unsigned int polarity =
1179 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
1180 EEPROM_FREQ_LED_POLARITY);
1181 unsigned int ledmode =
1182 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
1183 EEPROM_FREQ_LED_MODE);
Layne Edwards44704e52011-04-18 15:26:00 +02001184 u32 reg;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001185
Layne Edwards44704e52011-04-18 15:26:00 +02001186 /* Check for SoC (SOC devices don't support MCU requests) */
1187 if (rt2x00_is_soc(led->rt2x00dev)) {
1188 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
1189
1190 /* Set LED Polarity */
1191 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, polarity);
1192
1193 /* Set LED Mode */
1194 if (led->type == LED_TYPE_RADIO) {
1195 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE,
1196 enabled ? 3 : 0);
1197 } else if (led->type == LED_TYPE_ASSOC) {
1198 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE,
1199 enabled ? 3 : 0);
1200 } else if (led->type == LED_TYPE_QUALITY) {
1201 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE,
1202 enabled ? 3 : 0);
1203 }
1204
1205 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
1206
1207 } else {
1208 if (led->type == LED_TYPE_RADIO) {
1209 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
1210 enabled ? 0x20 : 0);
1211 } else if (led->type == LED_TYPE_ASSOC) {
1212 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
1213 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
1214 } else if (led->type == LED_TYPE_QUALITY) {
1215 /*
1216 * The brightness is divided into 6 levels (0 - 5),
1217 * The specs tell us the following levels:
1218 * 0, 1 ,3, 7, 15, 31
1219 * to determine the level in a simple way we can simply
1220 * work with bitshifting:
1221 * (1 << level) - 1
1222 */
1223 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
1224 (1 << brightness / (LED_FULL / 6)) - 1,
1225 polarity);
1226 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001227 }
1228}
1229
Gertjan van Wingerdeb3579d62009-12-30 11:36:34 +01001230static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001231 struct rt2x00_led *led, enum led_type type)
1232{
1233 led->rt2x00dev = rt2x00dev;
1234 led->type = type;
1235 led->led_dev.brightness_set = rt2800_brightness_set;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001236 led->flags = LED_INITIALIZED;
1237}
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001238#endif /* CONFIG_RT2X00_LIB_LEDS */
1239
1240/*
1241 * Configuration handlers.
1242 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001243static void rt2800_config_wcid(struct rt2x00_dev *rt2x00dev,
1244 const u8 *address,
1245 int wcid)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001246{
1247 struct mac_wcid_entry wcid_entry;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001248 u32 offset;
1249
1250 offset = MAC_WCID_ENTRY(wcid);
1251
1252 memset(&wcid_entry, 0xff, sizeof(wcid_entry));
1253 if (address)
1254 memcpy(wcid_entry.mac, address, ETH_ALEN);
1255
1256 rt2800_register_multiwrite(rt2x00dev, offset,
1257 &wcid_entry, sizeof(wcid_entry));
1258}
1259
1260static void rt2800_delete_wcid_attr(struct rt2x00_dev *rt2x00dev, int wcid)
1261{
1262 u32 offset;
1263 offset = MAC_WCID_ATTR_ENTRY(wcid);
1264 rt2800_register_write(rt2x00dev, offset, 0);
1265}
1266
1267static void rt2800_config_wcid_attr_bssidx(struct rt2x00_dev *rt2x00dev,
1268 int wcid, u32 bssidx)
1269{
1270 u32 offset = MAC_WCID_ATTR_ENTRY(wcid);
1271 u32 reg;
1272
1273 /*
1274 * The BSS Idx numbers is split in a main value of 3 bits,
1275 * and a extended field for adding one additional bit to the value.
1276 */
1277 rt2800_register_read(rt2x00dev, offset, &reg);
1278 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX, (bssidx & 0x7));
1279 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
1280 (bssidx & 0x8) >> 3);
1281 rt2800_register_write(rt2x00dev, offset, reg);
1282}
1283
1284static void rt2800_config_wcid_attr_cipher(struct rt2x00_dev *rt2x00dev,
1285 struct rt2x00lib_crypto *crypto,
1286 struct ieee80211_key_conf *key)
1287{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001288 struct mac_iveiv_entry iveiv_entry;
1289 u32 offset;
1290 u32 reg;
1291
1292 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
1293
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001294 if (crypto->cmd == SET_KEY) {
1295 rt2800_register_read(rt2x00dev, offset, &reg);
1296 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
1297 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
1298 /*
1299 * Both the cipher as the BSS Idx numbers are split in a main
1300 * value of 3 bits, and a extended field for adding one additional
1301 * bit to the value.
1302 */
1303 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
1304 (crypto->cipher & 0x7));
1305 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
1306 (crypto->cipher & 0x8) >> 3);
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001307 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
1308 rt2800_register_write(rt2x00dev, offset, reg);
1309 } else {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001310 /* Delete the cipher without touching the bssidx */
1311 rt2800_register_read(rt2x00dev, offset, &reg);
1312 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB, 0);
1313 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER, 0);
1314 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT, 0);
1315 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, 0);
1316 rt2800_register_write(rt2x00dev, offset, reg);
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001317 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001318
1319 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
1320
1321 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
1322 if ((crypto->cipher == CIPHER_TKIP) ||
1323 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
1324 (crypto->cipher == CIPHER_AES))
1325 iveiv_entry.iv[3] |= 0x20;
1326 iveiv_entry.iv[3] |= key->keyidx << 6;
1327 rt2800_register_multiwrite(rt2x00dev, offset,
1328 &iveiv_entry, sizeof(iveiv_entry));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001329}
1330
1331int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
1332 struct rt2x00lib_crypto *crypto,
1333 struct ieee80211_key_conf *key)
1334{
1335 struct hw_key_entry key_entry;
1336 struct rt2x00_field32 field;
1337 u32 offset;
1338 u32 reg;
1339
1340 if (crypto->cmd == SET_KEY) {
1341 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
1342
1343 memcpy(key_entry.key, crypto->key,
1344 sizeof(key_entry.key));
1345 memcpy(key_entry.tx_mic, crypto->tx_mic,
1346 sizeof(key_entry.tx_mic));
1347 memcpy(key_entry.rx_mic, crypto->rx_mic,
1348 sizeof(key_entry.rx_mic));
1349
1350 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
1351 rt2800_register_multiwrite(rt2x00dev, offset,
1352 &key_entry, sizeof(key_entry));
1353 }
1354
1355 /*
1356 * The cipher types are stored over multiple registers
1357 * starting with SHARED_KEY_MODE_BASE each word will have
1358 * 32 bits and contains the cipher types for 2 bssidx each.
1359 * Using the correct defines correctly will cause overhead,
1360 * so just calculate the correct offset.
1361 */
1362 field.bit_offset = 4 * (key->hw_key_idx % 8);
1363 field.bit_mask = 0x7 << field.bit_offset;
1364
1365 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
1366
1367 rt2800_register_read(rt2x00dev, offset, &reg);
1368 rt2x00_set_field32(&reg, field,
1369 (crypto->cmd == SET_KEY) * crypto->cipher);
1370 rt2800_register_write(rt2x00dev, offset, reg);
1371
1372 /*
1373 * Update WCID information
1374 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001375 rt2800_config_wcid(rt2x00dev, crypto->address, key->hw_key_idx);
1376 rt2800_config_wcid_attr_bssidx(rt2x00dev, key->hw_key_idx,
1377 crypto->bssidx);
1378 rt2800_config_wcid_attr_cipher(rt2x00dev, crypto, key);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001379
1380 return 0;
1381}
1382EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
1383
1384int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
1385 struct rt2x00lib_crypto *crypto,
1386 struct ieee80211_key_conf *key)
1387{
1388 struct hw_key_entry key_entry;
1389 u32 offset;
1390
1391 if (crypto->cmd == SET_KEY) {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001392 /*
1393 * Allow key configuration only for STAs that are
1394 * known by the hw.
1395 */
Stanislaw Gruszkaed8e0ed2015-06-11 12:53:45 +02001396 if (crypto->wcid > WCID_END)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001397 return -ENOSPC;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001398 key->hw_key_idx = crypto->wcid;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001399
1400 memcpy(key_entry.key, crypto->key,
1401 sizeof(key_entry.key));
1402 memcpy(key_entry.tx_mic, crypto->tx_mic,
1403 sizeof(key_entry.tx_mic));
1404 memcpy(key_entry.rx_mic, crypto->rx_mic,
1405 sizeof(key_entry.rx_mic));
1406
1407 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
1408 rt2800_register_multiwrite(rt2x00dev, offset,
1409 &key_entry, sizeof(key_entry));
1410 }
1411
1412 /*
1413 * Update WCID information
1414 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001415 rt2800_config_wcid_attr_cipher(rt2x00dev, crypto, key);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001416
1417 return 0;
1418}
1419EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
1420
Helmut Schaaa2b13282011-09-08 14:38:01 +02001421int rt2800_sta_add(struct rt2x00_dev *rt2x00dev, struct ieee80211_vif *vif,
1422 struct ieee80211_sta *sta)
1423{
1424 int wcid;
1425 struct rt2x00_sta *sta_priv = sta_to_rt2x00_sta(sta);
Stanislaw Gruszkaed8e0ed2015-06-11 12:53:45 +02001426 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001427
1428 /*
Stanislaw Gruszkaed8e0ed2015-06-11 12:53:45 +02001429 * Search for the first free WCID entry and return the corresponding
1430 * index.
Helmut Schaaa2b13282011-09-08 14:38:01 +02001431 */
Stanislaw Gruszkaed8e0ed2015-06-11 12:53:45 +02001432 wcid = find_first_zero_bit(drv_data->sta_ids, STA_IDS_SIZE) + WCID_START;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001433
1434 /*
1435 * Store selected wcid even if it is invalid so that we can
1436 * later decide if the STA is uploaded into the hw.
1437 */
1438 sta_priv->wcid = wcid;
1439
1440 /*
1441 * No space left in the device, however, we can still communicate
1442 * with the STA -> No error.
1443 */
Stanislaw Gruszkaed8e0ed2015-06-11 12:53:45 +02001444 if (wcid > WCID_END)
Helmut Schaaa2b13282011-09-08 14:38:01 +02001445 return 0;
1446
Stanislaw Gruszkaed8e0ed2015-06-11 12:53:45 +02001447 __set_bit(wcid - WCID_START, drv_data->sta_ids);
1448
Helmut Schaaa2b13282011-09-08 14:38:01 +02001449 /*
1450 * Clean up WCID attributes and write STA address to the device.
1451 */
1452 rt2800_delete_wcid_attr(rt2x00dev, wcid);
1453 rt2800_config_wcid(rt2x00dev, sta->addr, wcid);
1454 rt2800_config_wcid_attr_bssidx(rt2x00dev, wcid,
1455 rt2x00lib_get_bssidx(rt2x00dev, vif));
1456 return 0;
1457}
1458EXPORT_SYMBOL_GPL(rt2800_sta_add);
1459
1460int rt2800_sta_remove(struct rt2x00_dev *rt2x00dev, int wcid)
1461{
Stanislaw Gruszkaed8e0ed2015-06-11 12:53:45 +02001462 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
1463
1464 if (wcid > WCID_END)
1465 return 0;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001466 /*
1467 * Remove WCID entry, no need to clean the attributes as they will
1468 * get renewed when the WCID is reused.
1469 */
1470 rt2800_config_wcid(rt2x00dev, NULL, wcid);
Stanislaw Gruszkaed8e0ed2015-06-11 12:53:45 +02001471 __clear_bit(wcid - WCID_START, drv_data->sta_ids);
Helmut Schaaa2b13282011-09-08 14:38:01 +02001472
1473 return 0;
1474}
1475EXPORT_SYMBOL_GPL(rt2800_sta_remove);
1476
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001477void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
1478 const unsigned int filter_flags)
1479{
1480 u32 reg;
1481
1482 /*
1483 * Start configuration steps.
1484 * Note that the version error will always be dropped
1485 * and broadcast frames will always be accepted since
1486 * there is no filter for it at this time.
1487 */
1488 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
1489 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
1490 !(filter_flags & FIF_FCSFAIL));
1491 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
1492 !(filter_flags & FIF_PLCPFAIL));
Eli Cooper262c7412016-01-18 19:30:19 +08001493 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
1494 !test_bit(CONFIG_MONITORING, &rt2x00dev->flags));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001495 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
1496 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
1497 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
1498 !(filter_flags & FIF_ALLMULTI));
1499 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
1500 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
1501 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
1502 !(filter_flags & FIF_CONTROL));
1503 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
1504 !(filter_flags & FIF_CONTROL));
1505 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
1506 !(filter_flags & FIF_CONTROL));
1507 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
1508 !(filter_flags & FIF_CONTROL));
1509 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
1510 !(filter_flags & FIF_CONTROL));
1511 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
1512 !(filter_flags & FIF_PSPOLL));
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +01001513 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 0);
Helmut Schaa48839932011-11-24 09:13:26 +01001514 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR,
1515 !(filter_flags & FIF_CONTROL));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001516 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
1517 !(filter_flags & FIF_CONTROL));
1518 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
1519}
1520EXPORT_SYMBOL_GPL(rt2800_config_filter);
1521
1522void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
1523 struct rt2x00intf_conf *conf, const unsigned int flags)
1524{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001525 u32 reg;
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001526 bool update_bssid = false;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001527
1528 if (flags & CONFIG_UPDATE_TYPE) {
1529 /*
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001530 * Enable synchronisation.
1531 */
1532 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001533 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001534 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Helmut Schaa15a533c2011-04-18 15:28:04 +02001535
1536 if (conf->sync == TSF_SYNC_AP_NONE) {
1537 /*
1538 * Tune beacon queue transmit parameters for AP mode
1539 */
1540 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1541 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 0);
1542 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 1);
1543 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1544 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 0);
1545 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1546 } else {
1547 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1548 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 4);
1549 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 2);
1550 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1551 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 16);
1552 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1553 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001554 }
1555
1556 if (flags & CONFIG_UPDATE_MAC) {
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001557 if (flags & CONFIG_UPDATE_TYPE &&
1558 conf->sync == TSF_SYNC_AP_NONE) {
1559 /*
1560 * The BSSID register has to be set to our own mac
1561 * address in AP mode.
1562 */
1563 memcpy(conf->bssid, conf->mac, sizeof(conf->mac));
1564 update_bssid = true;
1565 }
1566
Ivo van Doornc600c822010-08-30 21:14:15 +02001567 if (!is_zero_ether_addr((const u8 *)conf->mac)) {
1568 reg = le32_to_cpu(conf->mac[1]);
1569 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
1570 conf->mac[1] = cpu_to_le32(reg);
1571 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001572
1573 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
1574 conf->mac, sizeof(conf->mac));
1575 }
1576
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001577 if ((flags & CONFIG_UPDATE_BSSID) || update_bssid) {
Ivo van Doornc600c822010-08-30 21:14:15 +02001578 if (!is_zero_ether_addr((const u8 *)conf->bssid)) {
1579 reg = le32_to_cpu(conf->bssid[1]);
1580 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
Stanislaw Gruszka88ff2f42014-06-05 13:52:25 +02001581 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 0);
Ivo van Doornc600c822010-08-30 21:14:15 +02001582 conf->bssid[1] = cpu_to_le32(reg);
1583 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001584
1585 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
1586 conf->bssid, sizeof(conf->bssid));
1587 }
1588}
1589EXPORT_SYMBOL_GPL(rt2800_config_intf);
1590
Helmut Schaa87c19152010-10-02 11:28:34 +02001591static void rt2800_config_ht_opmode(struct rt2x00_dev *rt2x00dev,
1592 struct rt2x00lib_erp *erp)
1593{
1594 bool any_sta_nongf = !!(erp->ht_opmode &
1595 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1596 u8 protection = erp->ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION;
1597 u8 mm20_mode, mm40_mode, gf20_mode, gf40_mode;
1598 u16 mm20_rate, mm40_rate, gf20_rate, gf40_rate;
1599 u32 reg;
1600
1601 /* default protection rate for HT20: OFDM 24M */
1602 mm20_rate = gf20_rate = 0x4004;
1603
1604 /* default protection rate for HT40: duplicate OFDM 24M */
1605 mm40_rate = gf40_rate = 0x4084;
1606
1607 switch (protection) {
1608 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
1609 /*
1610 * All STAs in this BSS are HT20/40 but there might be
1611 * STAs not supporting greenfield mode.
1612 * => Disable protection for HT transmissions.
1613 */
1614 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 0;
1615
1616 break;
1617 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
1618 /*
1619 * All STAs in this BSS are HT20 or HT20/40 but there
1620 * might be STAs not supporting greenfield mode.
1621 * => Protect all HT40 transmissions.
1622 */
1623 mm20_mode = gf20_mode = 0;
1624 mm40_mode = gf40_mode = 2;
1625
1626 break;
1627 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
1628 /*
1629 * Nonmember protection:
1630 * According to 802.11n we _should_ protect all
1631 * HT transmissions (but we don't have to).
1632 *
1633 * But if cts_protection is enabled we _shall_ protect
1634 * all HT transmissions using a CCK rate.
1635 *
1636 * And if any station is non GF we _shall_ protect
1637 * GF transmissions.
1638 *
1639 * We decide to protect everything
1640 * -> fall through to mixed mode.
1641 */
1642 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
1643 /*
1644 * Legacy STAs are present
1645 * => Protect all HT transmissions.
1646 */
1647 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 2;
1648
1649 /*
1650 * If erp protection is needed we have to protect HT
1651 * transmissions with CCK 11M long preamble.
1652 */
1653 if (erp->cts_protection) {
1654 /* don't duplicate RTS/CTS in CCK mode */
1655 mm20_rate = mm40_rate = 0x0003;
1656 gf20_rate = gf40_rate = 0x0003;
1657 }
1658 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001659 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001660
1661 /* check for STAs not supporting greenfield mode */
1662 if (any_sta_nongf)
1663 gf20_mode = gf40_mode = 2;
1664
1665 /* Update HT protection config */
1666 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1667 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, mm20_rate);
1668 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, mm20_mode);
1669 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1670
1671 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1672 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, mm40_rate);
1673 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, mm40_mode);
1674 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1675
1676 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1677 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, gf20_rate);
1678 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, gf20_mode);
1679 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1680
1681 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1682 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, gf40_rate);
1683 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, gf40_mode);
1684 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1685}
1686
Helmut Schaa02044642010-09-08 20:56:32 +02001687void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp,
1688 u32 changed)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001689{
1690 u32 reg;
1691
Helmut Schaa02044642010-09-08 20:56:32 +02001692 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1693 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
Helmut Schaa02044642010-09-08 20:56:32 +02001694 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
1695 !!erp->short_preamble);
1696 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1697 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001698
Helmut Schaa02044642010-09-08 20:56:32 +02001699 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1700 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
1701 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
1702 erp->cts_protection ? 2 : 0);
1703 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1704 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001705
Helmut Schaa02044642010-09-08 20:56:32 +02001706 if (changed & BSS_CHANGED_BASIC_RATES) {
1707 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
Stanislaw Gruszka770e4b72016-11-18 10:43:55 +01001708 0xff0 | erp->basic_rates);
Helmut Schaa02044642010-09-08 20:56:32 +02001709 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1710 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001711
Helmut Schaa02044642010-09-08 20:56:32 +02001712 if (changed & BSS_CHANGED_ERP_SLOT) {
1713 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1714 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME,
1715 erp->slot_time);
1716 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001717
Helmut Schaa02044642010-09-08 20:56:32 +02001718 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
1719 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
1720 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1721 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001722
Helmut Schaa02044642010-09-08 20:56:32 +02001723 if (changed & BSS_CHANGED_BEACON_INT) {
1724 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1725 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
1726 erp->beacon_int * 16);
1727 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1728 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001729
1730 if (changed & BSS_CHANGED_HT)
1731 rt2800_config_ht_opmode(rt2x00dev, erp);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001732}
1733EXPORT_SYMBOL_GPL(rt2800_config_erp);
1734
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001735static void rt2800_config_3572bt_ant(struct rt2x00_dev *rt2x00dev)
1736{
1737 u32 reg;
1738 u16 eeprom;
1739 u8 led_ctrl, led_g_mode, led_r_mode;
1740
1741 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
Johannes Berg57fbcce2016-04-12 15:56:15 +02001742 if (rt2x00dev->curr_band == NL80211_BAND_5GHZ) {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001743 rt2x00_set_field32(&reg, GPIO_SWITCH_0, 1);
1744 rt2x00_set_field32(&reg, GPIO_SWITCH_1, 1);
1745 } else {
1746 rt2x00_set_field32(&reg, GPIO_SWITCH_0, 0);
1747 rt2x00_set_field32(&reg, GPIO_SWITCH_1, 0);
1748 }
1749 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
1750
1751 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1752 led_g_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 3 : 0;
1753 led_r_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 0 : 3;
1754 if (led_g_mode != rt2x00_get_field32(reg, LED_CFG_G_LED_MODE) ||
1755 led_r_mode != rt2x00_get_field32(reg, LED_CFG_R_LED_MODE)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001756 rt2800_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001757 led_ctrl = rt2x00_get_field16(eeprom, EEPROM_FREQ_LED_MODE);
1758 if (led_ctrl == 0 || led_ctrl > 0x40) {
1759 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, led_g_mode);
1760 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, led_r_mode);
1761 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1762 } else {
1763 rt2800_mcu_request(rt2x00dev, MCU_BAND_SELECT, 0xff,
1764 (led_g_mode << 2) | led_r_mode, 1);
1765 }
1766 }
1767}
1768
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001769static void rt2800_set_ant_diversity(struct rt2x00_dev *rt2x00dev,
1770 enum antenna ant)
1771{
1772 u32 reg;
1773 u8 eesk_pin = (ant == ANTENNA_A) ? 1 : 0;
1774 u8 gpio_bit3 = (ant == ANTENNA_A) ? 0 : 1;
1775
1776 if (rt2x00_is_pci(rt2x00dev)) {
1777 rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
1778 rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK, eesk_pin);
1779 rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
1780 } else if (rt2x00_is_usb(rt2x00dev))
1781 rt2800_mcu_request(rt2x00dev, MCU_ANT_SELECT, 0xff,
1782 eesk_pin, 0);
1783
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02001784 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
1785 rt2x00_set_field32(&reg, GPIO_CTRL_DIR3, 0);
1786 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, gpio_bit3);
1787 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001788}
1789
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001790void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
1791{
1792 u8 r1;
1793 u8 r3;
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001794 u16 eeprom;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001795
1796 rt2800_bbp_read(rt2x00dev, 1, &r1);
1797 rt2800_bbp_read(rt2x00dev, 3, &r3);
1798
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001799 if (rt2x00_rt(rt2x00dev, RT3572) &&
Gabor Juhosc429dfe2013-10-11 13:18:42 +02001800 rt2x00_has_cap_bt_coexist(rt2x00dev))
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001801 rt2800_config_3572bt_ant(rt2x00dev);
1802
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001803 /*
1804 * Configure the TX antenna.
1805 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001806 switch (ant->tx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001807 case 1:
1808 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001809 break;
1810 case 2:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001811 if (rt2x00_rt(rt2x00dev, RT3572) &&
Gabor Juhosc429dfe2013-10-11 13:18:42 +02001812 rt2x00_has_cap_bt_coexist(rt2x00dev))
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001813 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 1);
1814 else
1815 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001816 break;
1817 case 3:
Gabor Juhos4788ac12013-07-08 16:08:21 +02001818 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001819 break;
1820 }
1821
1822 /*
1823 * Configure the RX antenna.
1824 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001825 switch (ant->rx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001826 case 1:
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001827 if (rt2x00_rt(rt2x00dev, RT3070) ||
1828 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03001829 rt2x00_rt(rt2x00dev, RT3352) ||
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001830 rt2x00_rt(rt2x00dev, RT3390)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001831 rt2800_eeprom_read(rt2x00dev,
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001832 EEPROM_NIC_CONF1, &eeprom);
1833 if (rt2x00_get_field16(eeprom,
1834 EEPROM_NIC_CONF1_ANT_DIVERSITY))
1835 rt2800_set_ant_diversity(rt2x00dev,
1836 rt2x00dev->default_ant.rx);
1837 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001838 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
1839 break;
1840 case 2:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001841 if (rt2x00_rt(rt2x00dev, RT3572) &&
Gabor Juhosc429dfe2013-10-11 13:18:42 +02001842 rt2x00_has_cap_bt_coexist(rt2x00dev)) {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001843 rt2x00_set_field8(&r3, BBP3_RX_ADC, 1);
1844 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA,
Johannes Berg57fbcce2016-04-12 15:56:15 +02001845 rt2x00dev->curr_band == NL80211_BAND_5GHZ);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001846 rt2800_set_ant_diversity(rt2x00dev, ANTENNA_B);
1847 } else {
1848 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
1849 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001850 break;
1851 case 3:
1852 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
1853 break;
1854 }
1855
1856 rt2800_bbp_write(rt2x00dev, 3, r3);
1857 rt2800_bbp_write(rt2x00dev, 1, r1);
Gabor Juhos5cddb3c2013-07-08 16:08:22 +02001858
1859 if (rt2x00_rt(rt2x00dev, RT3593)) {
1860 if (ant->rx_chain_num == 1)
1861 rt2800_bbp_write(rt2x00dev, 86, 0x00);
1862 else
1863 rt2800_bbp_write(rt2x00dev, 86, 0x46);
1864 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001865}
1866EXPORT_SYMBOL_GPL(rt2800_config_ant);
1867
1868static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
1869 struct rt2x00lib_conf *libconf)
1870{
1871 u16 eeprom;
1872 short lna_gain;
1873
1874 if (libconf->rf.channel <= 14) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001875 rt2800_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001876 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
1877 } else if (libconf->rf.channel <= 64) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001878 rt2800_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001879 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
1880 } else if (libconf->rf.channel <= 128) {
Gabor Juhosf36bb0c2013-07-08 16:08:27 +02001881 if (rt2x00_rt(rt2x00dev, RT3593)) {
1882 rt2800_eeprom_read(rt2x00dev, EEPROM_EXT_LNA2, &eeprom);
1883 lna_gain = rt2x00_get_field16(eeprom,
1884 EEPROM_EXT_LNA2_A1);
1885 } else {
1886 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
1887 lna_gain = rt2x00_get_field16(eeprom,
1888 EEPROM_RSSI_BG2_LNA_A1);
1889 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001890 } else {
Gabor Juhosf36bb0c2013-07-08 16:08:27 +02001891 if (rt2x00_rt(rt2x00dev, RT3593)) {
1892 rt2800_eeprom_read(rt2x00dev, EEPROM_EXT_LNA2, &eeprom);
1893 lna_gain = rt2x00_get_field16(eeprom,
1894 EEPROM_EXT_LNA2_A2);
1895 } else {
1896 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
1897 lna_gain = rt2x00_get_field16(eeprom,
1898 EEPROM_RSSI_A2_LNA_A2);
1899 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001900 }
1901
1902 rt2x00dev->lna_gain = lna_gain;
1903}
1904
Gabor Juhos3f1b8732013-08-17 14:09:32 +02001905#define FREQ_OFFSET_BOUND 0x5f
1906
1907static void rt2800_adjust_freq_offset(struct rt2x00_dev *rt2x00dev)
1908{
1909 u8 freq_offset, prev_freq_offset;
1910 u8 rfcsr, prev_rfcsr;
1911
1912 freq_offset = rt2x00_get_field8(rt2x00dev->freq_offset, RFCSR17_CODE);
1913 freq_offset = min_t(u8, freq_offset, FREQ_OFFSET_BOUND);
1914
1915 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
1916 prev_rfcsr = rfcsr;
1917
1918 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, freq_offset);
1919 if (rfcsr == prev_rfcsr)
1920 return;
1921
1922 if (rt2x00_is_usb(rt2x00dev)) {
1923 rt2800_mcu_request(rt2x00dev, MCU_FREQ_OFFSET, 0xff,
1924 freq_offset, prev_rfcsr);
1925 return;
1926 }
1927
1928 prev_freq_offset = rt2x00_get_field8(prev_rfcsr, RFCSR17_CODE);
1929 while (prev_freq_offset != freq_offset) {
1930 if (prev_freq_offset < freq_offset)
1931 prev_freq_offset++;
1932 else
1933 prev_freq_offset--;
1934
1935 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, prev_freq_offset);
1936 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
1937
1938 usleep_range(1000, 1500);
1939 }
1940}
1941
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001942static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
1943 struct ieee80211_conf *conf,
1944 struct rf_channel *rf,
1945 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001946{
1947 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
1948
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001949 if (rt2x00dev->default_ant.tx_chain_num == 1)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001950 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
1951
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001952 if (rt2x00dev->default_ant.rx_chain_num == 1) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001953 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
1954 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001955 } else if (rt2x00dev->default_ant.rx_chain_num == 2)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001956 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1957
1958 if (rf->channel > 14) {
1959 /*
1960 * When TX power is below 0, we should increase it by 7 to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001961 * make it a positive value (Minimum value is -7).
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001962 * However this means that values between 0 and 7 have
1963 * double meaning, and we should set a 7DBm boost flag.
1964 */
1965 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001966 (info->default_power1 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001967
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001968 if (info->default_power1 < 0)
1969 info->default_power1 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001970
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001971 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001972
1973 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001974 (info->default_power2 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001975
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001976 if (info->default_power2 < 0)
1977 info->default_power2 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001978
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001979 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001980 } else {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001981 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G, info->default_power1);
1982 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001983 }
1984
1985 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
1986
1987 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1988 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1989 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1990 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1991
1992 udelay(200);
1993
1994 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1995 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1996 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
1997 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1998
1999 udelay(200);
2000
2001 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
2002 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
2003 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
2004 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
2005}
2006
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02002007static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
2008 struct ieee80211_conf *conf,
2009 struct rf_channel *rf,
2010 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002011{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01002012 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01002013 u8 rfcsr, calib_tx, calib_rx;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002014
2015 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Stanislaw Gruszka7f4666a2012-01-30 16:17:56 +01002016
2017 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
2018 rt2x00_set_field8(&rfcsr, RFCSR3_K, rf->rf3);
2019 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002020
2021 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +02002022 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002023 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
2024
2025 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002026 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002027 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
2028
Helmut Schaa5a673962010-04-23 15:54:43 +02002029 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002030 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER, info->default_power2);
Helmut Schaa5a673962010-04-23 15:54:43 +02002031 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
2032
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01002033 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2034 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
Gertjan van Wingerde7ad63032012-09-16 22:29:53 +02002035 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD,
2036 rt2x00dev->default_ant.rx_chain_num <= 1);
2037 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD,
2038 rt2x00dev->default_ant.rx_chain_num <= 2);
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01002039 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
Gertjan van Wingerde7ad63032012-09-16 22:29:53 +02002040 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD,
2041 rt2x00dev->default_ant.tx_chain_num <= 1);
2042 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD,
2043 rt2x00dev->default_ant.tx_chain_num <= 2);
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01002044 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2045
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002046 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
2047 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
2048 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
2049
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01002050 if (rt2x00_rt(rt2x00dev, RT3390)) {
2051 calib_tx = conf_is_ht40(conf) ? 0x68 : 0x4f;
2052 calib_rx = conf_is_ht40(conf) ? 0x6f : 0x4f;
2053 } else {
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01002054 if (conf_is_ht40(conf)) {
2055 calib_tx = drv_data->calibration_bw40;
2056 calib_rx = drv_data->calibration_bw40;
2057 } else {
2058 calib_tx = drv_data->calibration_bw20;
2059 calib_rx = drv_data->calibration_bw20;
2060 }
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01002061 }
2062
2063 rt2800_rfcsr_read(rt2x00dev, 24, &rfcsr);
2064 rt2x00_set_field8(&rfcsr, RFCSR24_TX_CALIB, calib_tx);
2065 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr);
2066
2067 rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
2068 rt2x00_set_field8(&rfcsr, RFCSR31_RX_CALIB, calib_rx);
2069 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002070
Gertjan van Wingerde71976902010-03-24 21:42:36 +01002071 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002072 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +01002073 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Stanislaw Gruszka3e0c7642012-01-30 16:17:58 +01002074
2075 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2076 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
2077 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2078 msleep(1);
2079 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
2080 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002081}
2082
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002083static void rt2800_config_channel_rf3052(struct rt2x00_dev *rt2x00dev,
2084 struct ieee80211_conf *conf,
2085 struct rf_channel *rf,
2086 struct channel_info *info)
2087{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01002088 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002089 u8 rfcsr;
2090 u32 reg;
2091
2092 if (rf->channel <= 14) {
Gertjan van Wingerde5d137df2012-02-06 23:45:09 +01002093 rt2800_bbp_write(rt2x00dev, 25, drv_data->bbp25);
2094 rt2800_bbp_write(rt2x00dev, 26, drv_data->bbp26);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002095 } else {
2096 rt2800_bbp_write(rt2x00dev, 25, 0x09);
2097 rt2800_bbp_write(rt2x00dev, 26, 0xff);
2098 }
2099
2100 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
2101 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
2102
2103 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
2104 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
2105 if (rf->channel <= 14)
2106 rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 2);
2107 else
2108 rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 1);
2109 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
2110
2111 rt2800_rfcsr_read(rt2x00dev, 5, &rfcsr);
2112 if (rf->channel <= 14)
2113 rt2x00_set_field8(&rfcsr, RFCSR5_R1, 1);
2114 else
2115 rt2x00_set_field8(&rfcsr, RFCSR5_R1, 2);
2116 rt2800_rfcsr_write(rt2x00dev, 5, rfcsr);
2117
2118 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
2119 if (rf->channel <= 14) {
2120 rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 3);
2121 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
Gertjan van Wingerde569ffa52012-02-06 23:45:10 +01002122 info->default_power1);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002123 } else {
2124 rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 7);
2125 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
2126 (info->default_power1 & 0x3) |
2127 ((info->default_power1 & 0xC) << 1));
2128 }
2129 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
2130
2131 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
2132 if (rf->channel <= 14) {
2133 rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 3);
2134 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
Gertjan van Wingerde569ffa52012-02-06 23:45:10 +01002135 info->default_power2);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002136 } else {
2137 rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 7);
2138 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
2139 (info->default_power2 & 0x3) |
2140 ((info->default_power2 & 0xC) << 1));
2141 }
2142 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
2143
2144 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002145 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2146 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2147 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
2148 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
Gertjan van Wingerde0cd461e2012-02-06 23:45:11 +01002149 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2150 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
Gabor Juhosc429dfe2013-10-11 13:18:42 +02002151 if (rt2x00_has_cap_bt_coexist(rt2x00dev)) {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002152 if (rf->channel <= 14) {
2153 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2154 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2155 }
2156 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
2157 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
2158 } else {
2159 switch (rt2x00dev->default_ant.tx_chain_num) {
2160 case 1:
2161 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2162 case 2:
2163 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
2164 break;
2165 }
2166
2167 switch (rt2x00dev->default_ant.rx_chain_num) {
2168 case 1:
2169 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2170 case 2:
2171 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
2172 break;
2173 }
2174 }
2175 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2176
2177 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
2178 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
2179 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
2180
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01002181 if (conf_is_ht40(conf)) {
2182 rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw40);
2183 rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw40);
2184 } else {
2185 rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw20);
2186 rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw20);
2187 }
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002188
2189 if (rf->channel <= 14) {
2190 rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
2191 rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
2192 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
2193 rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
2194 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01002195 rfcsr = 0x4c;
2196 rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
2197 drv_data->txmixer_gain_24g);
2198 rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002199 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
2200 rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
2201 rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
2202 rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
2203 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
2204 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
2205 rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
2206 } else {
Gertjan van Wingerde58b8ae12012-02-06 23:45:12 +01002207 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
2208 rt2x00_set_field8(&rfcsr, RFCSR7_BIT2, 1);
2209 rt2x00_set_field8(&rfcsr, RFCSR7_BIT3, 0);
2210 rt2x00_set_field8(&rfcsr, RFCSR7_BIT4, 1);
2211 rt2x00_set_field8(&rfcsr, RFCSR7_BITS67, 0);
2212 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002213 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
2214 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
2215 rt2800_rfcsr_write(rt2x00dev, 11, 0x00);
2216 rt2800_rfcsr_write(rt2x00dev, 15, 0x43);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01002217 rfcsr = 0x7a;
2218 rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
2219 drv_data->txmixer_gain_5g);
2220 rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002221 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
2222 if (rf->channel <= 64) {
2223 rt2800_rfcsr_write(rt2x00dev, 19, 0xb7);
2224 rt2800_rfcsr_write(rt2x00dev, 20, 0xf6);
2225 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
2226 } else if (rf->channel <= 128) {
2227 rt2800_rfcsr_write(rt2x00dev, 19, 0x74);
2228 rt2800_rfcsr_write(rt2x00dev, 20, 0xf4);
2229 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
2230 } else {
2231 rt2800_rfcsr_write(rt2x00dev, 19, 0x72);
2232 rt2800_rfcsr_write(rt2x00dev, 20, 0xf3);
2233 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
2234 }
2235 rt2800_rfcsr_write(rt2x00dev, 26, 0x87);
2236 rt2800_rfcsr_write(rt2x00dev, 27, 0x01);
2237 rt2800_rfcsr_write(rt2x00dev, 29, 0x9f);
2238 }
2239
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02002240 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
2241 rt2x00_set_field32(&reg, GPIO_CTRL_DIR7, 0);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002242 if (rf->channel <= 14)
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02002243 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 1);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002244 else
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02002245 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 0);
2246 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002247
2248 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
2249 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
2250 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
2251}
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002252
Gabor Juhosf42b0462013-07-08 16:08:30 +02002253static void rt2800_config_channel_rf3053(struct rt2x00_dev *rt2x00dev,
2254 struct ieee80211_conf *conf,
2255 struct rf_channel *rf,
2256 struct channel_info *info)
2257{
2258 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
2259 u8 txrx_agc_fc;
2260 u8 txrx_h20m;
2261 u8 rfcsr;
2262 u8 bbp;
2263 const bool txbf_enabled = false; /* TODO */
2264
2265 /* TODO: use TX{0,1,2}FinePowerControl values from EEPROM */
2266 rt2800_bbp_read(rt2x00dev, 109, &bbp);
2267 rt2x00_set_field8(&bbp, BBP109_TX0_POWER, 0);
2268 rt2x00_set_field8(&bbp, BBP109_TX1_POWER, 0);
2269 rt2800_bbp_write(rt2x00dev, 109, bbp);
2270
2271 rt2800_bbp_read(rt2x00dev, 110, &bbp);
2272 rt2x00_set_field8(&bbp, BBP110_TX2_POWER, 0);
2273 rt2800_bbp_write(rt2x00dev, 110, bbp);
2274
2275 if (rf->channel <= 14) {
2276 /* Restore BBP 25 & 26 for 2.4 GHz */
2277 rt2800_bbp_write(rt2x00dev, 25, drv_data->bbp25);
2278 rt2800_bbp_write(rt2x00dev, 26, drv_data->bbp26);
2279 } else {
2280 /* Hard code BBP 25 & 26 for 5GHz */
2281
2282 /* Enable IQ Phase correction */
2283 rt2800_bbp_write(rt2x00dev, 25, 0x09);
2284 /* Setup IQ Phase correction value */
2285 rt2800_bbp_write(rt2x00dev, 26, 0xff);
2286 }
2287
2288 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2289 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3 & 0xf);
2290
2291 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2292 rt2x00_set_field8(&rfcsr, RFCSR11_R, (rf->rf2 & 0x3));
2293 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2294
2295 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2296 rt2x00_set_field8(&rfcsr, RFCSR11_PLL_IDOH, 1);
2297 if (rf->channel <= 14)
2298 rt2x00_set_field8(&rfcsr, RFCSR11_PLL_MOD, 1);
2299 else
2300 rt2x00_set_field8(&rfcsr, RFCSR11_PLL_MOD, 2);
2301 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2302
2303 rt2800_rfcsr_read(rt2x00dev, 53, &rfcsr);
2304 if (rf->channel <= 14) {
2305 rfcsr = 0;
2306 rt2x00_set_field8(&rfcsr, RFCSR53_TX_POWER,
2307 info->default_power1 & 0x1f);
2308 } else {
2309 if (rt2x00_is_usb(rt2x00dev))
2310 rfcsr = 0x40;
2311
2312 rt2x00_set_field8(&rfcsr, RFCSR53_TX_POWER,
2313 ((info->default_power1 & 0x18) << 1) |
2314 (info->default_power1 & 7));
2315 }
2316 rt2800_rfcsr_write(rt2x00dev, 53, rfcsr);
2317
2318 rt2800_rfcsr_read(rt2x00dev, 55, &rfcsr);
2319 if (rf->channel <= 14) {
2320 rfcsr = 0;
2321 rt2x00_set_field8(&rfcsr, RFCSR55_TX_POWER,
2322 info->default_power2 & 0x1f);
2323 } else {
2324 if (rt2x00_is_usb(rt2x00dev))
2325 rfcsr = 0x40;
2326
2327 rt2x00_set_field8(&rfcsr, RFCSR55_TX_POWER,
2328 ((info->default_power2 & 0x18) << 1) |
2329 (info->default_power2 & 7));
2330 }
2331 rt2800_rfcsr_write(rt2x00dev, 55, rfcsr);
2332
2333 rt2800_rfcsr_read(rt2x00dev, 54, &rfcsr);
2334 if (rf->channel <= 14) {
2335 rfcsr = 0;
2336 rt2x00_set_field8(&rfcsr, RFCSR54_TX_POWER,
2337 info->default_power3 & 0x1f);
2338 } else {
2339 if (rt2x00_is_usb(rt2x00dev))
2340 rfcsr = 0x40;
2341
2342 rt2x00_set_field8(&rfcsr, RFCSR54_TX_POWER,
2343 ((info->default_power3 & 0x18) << 1) |
2344 (info->default_power3 & 7));
2345 }
2346 rt2800_rfcsr_write(rt2x00dev, 54, rfcsr);
2347
2348 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2349 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2350 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2351 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
2352 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
2353 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2354 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
2355 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2356 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
2357
2358 switch (rt2x00dev->default_ant.tx_chain_num) {
2359 case 3:
2360 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
2361 /* fallthrough */
2362 case 2:
2363 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2364 /* fallthrough */
2365 case 1:
2366 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2367 break;
2368 }
2369
2370 switch (rt2x00dev->default_ant.rx_chain_num) {
2371 case 3:
2372 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
2373 /* fallthrough */
2374 case 2:
2375 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2376 /* fallthrough */
2377 case 1:
2378 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2379 break;
2380 }
2381 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2382
Gabor Juhose979a8a2013-08-17 14:09:33 +02002383 rt2800_adjust_freq_offset(rt2x00dev);
Gabor Juhosf42b0462013-07-08 16:08:30 +02002384
2385 if (conf_is_ht40(conf)) {
2386 txrx_agc_fc = rt2x00_get_field8(drv_data->calibration_bw40,
2387 RFCSR24_TX_AGC_FC);
2388 txrx_h20m = rt2x00_get_field8(drv_data->calibration_bw40,
2389 RFCSR24_TX_H20M);
2390 } else {
2391 txrx_agc_fc = rt2x00_get_field8(drv_data->calibration_bw20,
2392 RFCSR24_TX_AGC_FC);
2393 txrx_h20m = rt2x00_get_field8(drv_data->calibration_bw20,
2394 RFCSR24_TX_H20M);
2395 }
2396
2397 /* NOTE: the reference driver does not writes the new value
2398 * back to RFCSR 32
2399 */
2400 rt2800_rfcsr_read(rt2x00dev, 32, &rfcsr);
2401 rt2x00_set_field8(&rfcsr, RFCSR32_TX_AGC_FC, txrx_agc_fc);
2402
2403 if (rf->channel <= 14)
2404 rfcsr = 0xa0;
2405 else
2406 rfcsr = 0x80;
2407 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
2408
2409 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2410 rt2x00_set_field8(&rfcsr, RFCSR30_TX_H20M, txrx_h20m);
2411 rt2x00_set_field8(&rfcsr, RFCSR30_RX_H20M, txrx_h20m);
2412 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2413
2414 /* Band selection */
2415 rt2800_rfcsr_read(rt2x00dev, 36, &rfcsr);
2416 if (rf->channel <= 14)
2417 rt2x00_set_field8(&rfcsr, RFCSR36_RF_BS, 1);
2418 else
2419 rt2x00_set_field8(&rfcsr, RFCSR36_RF_BS, 0);
2420 rt2800_rfcsr_write(rt2x00dev, 36, rfcsr);
2421
2422 rt2800_rfcsr_read(rt2x00dev, 34, &rfcsr);
2423 if (rf->channel <= 14)
2424 rfcsr = 0x3c;
2425 else
2426 rfcsr = 0x20;
2427 rt2800_rfcsr_write(rt2x00dev, 34, rfcsr);
2428
2429 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
2430 if (rf->channel <= 14)
2431 rfcsr = 0x1a;
2432 else
2433 rfcsr = 0x12;
2434 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
2435
2436 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
2437 if (rf->channel >= 1 && rf->channel <= 14)
2438 rt2x00_set_field8(&rfcsr, RFCSR6_VCO_IC, 1);
2439 else if (rf->channel >= 36 && rf->channel <= 64)
2440 rt2x00_set_field8(&rfcsr, RFCSR6_VCO_IC, 2);
2441 else if (rf->channel >= 100 && rf->channel <= 128)
2442 rt2x00_set_field8(&rfcsr, RFCSR6_VCO_IC, 2);
2443 else
2444 rt2x00_set_field8(&rfcsr, RFCSR6_VCO_IC, 1);
2445 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
2446
2447 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2448 rt2x00_set_field8(&rfcsr, RFCSR30_RX_VCM, 2);
2449 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2450
2451 rt2800_rfcsr_write(rt2x00dev, 46, 0x60);
2452
2453 if (rf->channel <= 14) {
2454 rt2800_rfcsr_write(rt2x00dev, 10, 0xd3);
2455 rt2800_rfcsr_write(rt2x00dev, 13, 0x12);
2456 } else {
2457 rt2800_rfcsr_write(rt2x00dev, 10, 0xd8);
2458 rt2800_rfcsr_write(rt2x00dev, 13, 0x23);
2459 }
2460
2461 rt2800_rfcsr_read(rt2x00dev, 51, &rfcsr);
2462 rt2x00_set_field8(&rfcsr, RFCSR51_BITS01, 1);
2463 rt2800_rfcsr_write(rt2x00dev, 51, rfcsr);
2464
2465 rt2800_rfcsr_read(rt2x00dev, 51, &rfcsr);
2466 if (rf->channel <= 14) {
2467 rt2x00_set_field8(&rfcsr, RFCSR51_BITS24, 5);
2468 rt2x00_set_field8(&rfcsr, RFCSR51_BITS57, 3);
2469 } else {
2470 rt2x00_set_field8(&rfcsr, RFCSR51_BITS24, 4);
2471 rt2x00_set_field8(&rfcsr, RFCSR51_BITS57, 2);
2472 }
2473 rt2800_rfcsr_write(rt2x00dev, 51, rfcsr);
2474
2475 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
2476 if (rf->channel <= 14)
2477 rt2x00_set_field8(&rfcsr, RFCSR49_TX_LO1_IC, 3);
2478 else
2479 rt2x00_set_field8(&rfcsr, RFCSR49_TX_LO1_IC, 2);
2480
2481 if (txbf_enabled)
2482 rt2x00_set_field8(&rfcsr, RFCSR49_TX_DIV, 1);
2483
2484 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
2485
2486 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
2487 rt2x00_set_field8(&rfcsr, RFCSR50_TX_LO1_EN, 0);
2488 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
2489
2490 rt2800_rfcsr_read(rt2x00dev, 57, &rfcsr);
2491 if (rf->channel <= 14)
2492 rt2x00_set_field8(&rfcsr, RFCSR57_DRV_CC, 0x1b);
2493 else
2494 rt2x00_set_field8(&rfcsr, RFCSR57_DRV_CC, 0x0f);
2495 rt2800_rfcsr_write(rt2x00dev, 57, rfcsr);
2496
2497 if (rf->channel <= 14) {
2498 rt2800_rfcsr_write(rt2x00dev, 44, 0x93);
2499 rt2800_rfcsr_write(rt2x00dev, 52, 0x45);
2500 } else {
2501 rt2800_rfcsr_write(rt2x00dev, 44, 0x9b);
2502 rt2800_rfcsr_write(rt2x00dev, 52, 0x05);
2503 }
2504
2505 /* Initiate VCO calibration */
2506 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
2507 if (rf->channel <= 14) {
2508 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
2509 } else {
2510 rt2x00_set_field8(&rfcsr, RFCSR3_BIT1, 1);
2511 rt2x00_set_field8(&rfcsr, RFCSR3_BIT2, 1);
2512 rt2x00_set_field8(&rfcsr, RFCSR3_BIT3, 1);
2513 rt2x00_set_field8(&rfcsr, RFCSR3_BIT4, 1);
2514 rt2x00_set_field8(&rfcsr, RFCSR3_BIT5, 1);
2515 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
2516 }
2517 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
2518
2519 if (rf->channel >= 1 && rf->channel <= 14) {
2520 rfcsr = 0x23;
2521 if (txbf_enabled)
2522 rt2x00_set_field8(&rfcsr, RFCSR39_RX_DIV, 1);
2523 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
2524
2525 rt2800_rfcsr_write(rt2x00dev, 45, 0xbb);
2526 } else if (rf->channel >= 36 && rf->channel <= 64) {
2527 rfcsr = 0x36;
2528 if (txbf_enabled)
2529 rt2x00_set_field8(&rfcsr, RFCSR39_RX_DIV, 1);
2530 rt2800_rfcsr_write(rt2x00dev, 39, 0x36);
2531
2532 rt2800_rfcsr_write(rt2x00dev, 45, 0xeb);
2533 } else if (rf->channel >= 100 && rf->channel <= 128) {
2534 rfcsr = 0x32;
2535 if (txbf_enabled)
2536 rt2x00_set_field8(&rfcsr, RFCSR39_RX_DIV, 1);
2537 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
2538
2539 rt2800_rfcsr_write(rt2x00dev, 45, 0xb3);
2540 } else {
2541 rfcsr = 0x30;
2542 if (txbf_enabled)
2543 rt2x00_set_field8(&rfcsr, RFCSR39_RX_DIV, 1);
2544 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
2545
2546 rt2800_rfcsr_write(rt2x00dev, 45, 0x9b);
2547 }
2548}
2549
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002550#define POWER_BOUND 0x27
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002551#define POWER_BOUND_5G 0x2b
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002552
Woody Hunga89534e2012-06-13 15:01:16 +08002553static void rt2800_config_channel_rf3290(struct rt2x00_dev *rt2x00dev,
2554 struct ieee80211_conf *conf,
2555 struct rf_channel *rf,
2556 struct channel_info *info)
2557{
2558 u8 rfcsr;
2559
2560 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2561 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2562 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2563 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
2564 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2565
2566 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002567 if (info->default_power1 > POWER_BOUND)
2568 rt2x00_set_field8(&rfcsr, RFCSR49_TX, POWER_BOUND);
Woody Hunga89534e2012-06-13 15:01:16 +08002569 else
2570 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2571 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
2572
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002573 rt2800_adjust_freq_offset(rt2x00dev);
Woody Hunga89534e2012-06-13 15:01:16 +08002574
2575 if (rf->channel <= 14) {
2576 if (rf->channel == 6)
2577 rt2800_bbp_write(rt2x00dev, 68, 0x0c);
2578 else
2579 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
2580
2581 if (rf->channel >= 1 && rf->channel <= 6)
2582 rt2800_bbp_write(rt2x00dev, 59, 0x0f);
2583 else if (rf->channel >= 7 && rf->channel <= 11)
2584 rt2800_bbp_write(rt2x00dev, 59, 0x0e);
2585 else if (rf->channel >= 12 && rf->channel <= 14)
2586 rt2800_bbp_write(rt2x00dev, 59, 0x0d);
2587 }
2588}
2589
Daniel Golle03839952012-09-09 14:24:39 +03002590static void rt2800_config_channel_rf3322(struct rt2x00_dev *rt2x00dev,
2591 struct ieee80211_conf *conf,
2592 struct rf_channel *rf,
2593 struct channel_info *info)
2594{
2595 u8 rfcsr;
2596
2597 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2598 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2599
2600 rt2800_rfcsr_write(rt2x00dev, 11, 0x42);
2601 rt2800_rfcsr_write(rt2x00dev, 12, 0x1c);
2602 rt2800_rfcsr_write(rt2x00dev, 13, 0x00);
2603
2604 if (info->default_power1 > POWER_BOUND)
2605 rt2800_rfcsr_write(rt2x00dev, 47, POWER_BOUND);
2606 else
2607 rt2800_rfcsr_write(rt2x00dev, 47, info->default_power1);
2608
2609 if (info->default_power2 > POWER_BOUND)
2610 rt2800_rfcsr_write(rt2x00dev, 48, POWER_BOUND);
2611 else
2612 rt2800_rfcsr_write(rt2x00dev, 48, info->default_power2);
2613
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002614 rt2800_adjust_freq_offset(rt2x00dev);
Daniel Golle03839952012-09-09 14:24:39 +03002615
2616 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2617 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2618 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2619
2620 if ( rt2x00dev->default_ant.tx_chain_num == 2 )
2621 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2622 else
2623 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
2624
2625 if ( rt2x00dev->default_ant.rx_chain_num == 2 )
2626 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2627 else
2628 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
2629
2630 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2631 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
2632
2633 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2634
2635 rt2800_rfcsr_write(rt2x00dev, 31, 80);
2636}
2637
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002638static void rt2800_config_channel_rf53xx(struct rt2x00_dev *rt2x00dev,
Gabor Juhosadde5882011-03-03 11:46:45 +01002639 struct ieee80211_conf *conf,
2640 struct rf_channel *rf,
2641 struct channel_info *info)
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002642{
Gabor Juhosadde5882011-03-03 11:46:45 +01002643 u8 rfcsr;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002644
Gabor Juhosadde5882011-03-03 11:46:45 +01002645 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2646 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2647 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2648 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
2649 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002650
Gabor Juhosadde5882011-03-03 11:46:45 +01002651 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002652 if (info->default_power1 > POWER_BOUND)
2653 rt2x00_set_field8(&rfcsr, RFCSR49_TX, POWER_BOUND);
Gabor Juhosadde5882011-03-03 11:46:45 +01002654 else
2655 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2656 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002657
Zero.Lincff3d1f2012-05-29 16:11:09 +08002658 if (rt2x00_rt(rt2x00dev, RT5392)) {
2659 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
Felipe Pena62649952013-10-18 21:20:42 -03002660 if (info->default_power2 > POWER_BOUND)
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002661 rt2x00_set_field8(&rfcsr, RFCSR50_TX, POWER_BOUND);
Zero.Lincff3d1f2012-05-29 16:11:09 +08002662 else
2663 rt2x00_set_field8(&rfcsr, RFCSR50_TX,
2664 info->default_power2);
2665 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
2666 }
2667
Gabor Juhosadde5882011-03-03 11:46:45 +01002668 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
Zero.Lincff3d1f2012-05-29 16:11:09 +08002669 if (rt2x00_rt(rt2x00dev, RT5392)) {
2670 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2671 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2672 }
Gabor Juhosadde5882011-03-03 11:46:45 +01002673 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2674 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
2675 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2676 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2677 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002678
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002679 rt2800_adjust_freq_offset(rt2x00dev);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002680
Gabor Juhosadde5882011-03-03 11:46:45 +01002681 if (rf->channel <= 14) {
2682 int idx = rf->channel-1;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002683
Gabor Juhosc429dfe2013-10-11 13:18:42 +02002684 if (rt2x00_has_cap_bt_coexist(rt2x00dev)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002685 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
2686 /* r55/r59 value array of channel 1~14 */
2687 static const char r55_bt_rev[] = {0x83, 0x83,
2688 0x83, 0x73, 0x73, 0x63, 0x53, 0x53,
2689 0x53, 0x43, 0x43, 0x43, 0x43, 0x43};
2690 static const char r59_bt_rev[] = {0x0e, 0x0e,
2691 0x0e, 0x0e, 0x0e, 0x0b, 0x0a, 0x09,
2692 0x07, 0x07, 0x07, 0x07, 0x07, 0x07};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002693
Gabor Juhosadde5882011-03-03 11:46:45 +01002694 rt2800_rfcsr_write(rt2x00dev, 55,
2695 r55_bt_rev[idx]);
2696 rt2800_rfcsr_write(rt2x00dev, 59,
2697 r59_bt_rev[idx]);
2698 } else {
2699 static const char r59_bt[] = {0x8b, 0x8b, 0x8b,
2700 0x8b, 0x8b, 0x8b, 0x8b, 0x8a, 0x89,
2701 0x88, 0x88, 0x86, 0x85, 0x84};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002702
Gabor Juhosadde5882011-03-03 11:46:45 +01002703 rt2800_rfcsr_write(rt2x00dev, 59, r59_bt[idx]);
2704 }
2705 } else {
2706 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
2707 static const char r55_nonbt_rev[] = {0x23, 0x23,
2708 0x23, 0x23, 0x13, 0x13, 0x03, 0x03,
2709 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
2710 static const char r59_nonbt_rev[] = {0x07, 0x07,
2711 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
2712 0x07, 0x07, 0x06, 0x05, 0x04, 0x04};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002713
Gabor Juhosadde5882011-03-03 11:46:45 +01002714 rt2800_rfcsr_write(rt2x00dev, 55,
2715 r55_nonbt_rev[idx]);
2716 rt2800_rfcsr_write(rt2x00dev, 59,
2717 r59_nonbt_rev[idx]);
John Li2ed71882012-02-17 17:33:06 +08002718 } else if (rt2x00_rt(rt2x00dev, RT5390) ||
Gabor Juhose6d227b2012-12-02 15:53:28 +01002719 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002720 static const char r59_non_bt[] = {0x8f, 0x8f,
2721 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8d,
2722 0x8a, 0x88, 0x88, 0x87, 0x87, 0x86};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002723
Gabor Juhosadde5882011-03-03 11:46:45 +01002724 rt2800_rfcsr_write(rt2x00dev, 59,
2725 r59_non_bt[idx]);
2726 }
2727 }
2728 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002729}
2730
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002731static void rt2800_config_channel_rf55xx(struct rt2x00_dev *rt2x00dev,
2732 struct ieee80211_conf *conf,
2733 struct rf_channel *rf,
2734 struct channel_info *info)
2735{
2736 u8 rfcsr, ep_reg;
Stanislaw Gruszkad5ae7a62013-03-16 19:19:42 +01002737 u32 reg;
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002738 int power_bound;
2739
2740 /* TODO */
2741 const bool is_11b = false;
2742 const bool is_type_ep = false;
2743
Stanislaw Gruszkad5ae7a62013-03-16 19:19:42 +01002744 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
2745 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL,
2746 (rf->channel > 14 || conf_is_ht40(conf)) ? 5 : 0);
2747 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002748
2749 /* Order of values on rf_channel entry: N, K, mod, R */
2750 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1 & 0xff);
2751
2752 rt2800_rfcsr_read(rt2x00dev, 9, &rfcsr);
2753 rt2x00_set_field8(&rfcsr, RFCSR9_K, rf->rf2 & 0xf);
2754 rt2x00_set_field8(&rfcsr, RFCSR9_N, (rf->rf1 & 0x100) >> 8);
2755 rt2x00_set_field8(&rfcsr, RFCSR9_MOD, ((rf->rf3 - 8) & 0x4) >> 2);
2756 rt2800_rfcsr_write(rt2x00dev, 9, rfcsr);
2757
2758 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2759 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf4 - 1);
2760 rt2x00_set_field8(&rfcsr, RFCSR11_MOD, (rf->rf3 - 8) & 0x3);
2761 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2762
2763 if (rf->channel <= 14) {
2764 rt2800_rfcsr_write(rt2x00dev, 10, 0x90);
2765 /* FIXME: RF11 owerwrite ? */
2766 rt2800_rfcsr_write(rt2x00dev, 11, 0x4A);
2767 rt2800_rfcsr_write(rt2x00dev, 12, 0x52);
2768 rt2800_rfcsr_write(rt2x00dev, 13, 0x42);
2769 rt2800_rfcsr_write(rt2x00dev, 22, 0x40);
2770 rt2800_rfcsr_write(rt2x00dev, 24, 0x4A);
2771 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
2772 rt2800_rfcsr_write(rt2x00dev, 27, 0x42);
2773 rt2800_rfcsr_write(rt2x00dev, 36, 0x80);
2774 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
2775 rt2800_rfcsr_write(rt2x00dev, 38, 0x89);
2776 rt2800_rfcsr_write(rt2x00dev, 39, 0x1B);
2777 rt2800_rfcsr_write(rt2x00dev, 40, 0x0D);
2778 rt2800_rfcsr_write(rt2x00dev, 41, 0x9B);
2779 rt2800_rfcsr_write(rt2x00dev, 42, 0xD5);
2780 rt2800_rfcsr_write(rt2x00dev, 43, 0x72);
2781 rt2800_rfcsr_write(rt2x00dev, 44, 0x0E);
2782 rt2800_rfcsr_write(rt2x00dev, 45, 0xA2);
2783 rt2800_rfcsr_write(rt2x00dev, 46, 0x6B);
2784 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
2785 rt2800_rfcsr_write(rt2x00dev, 51, 0x3E);
2786 rt2800_rfcsr_write(rt2x00dev, 52, 0x48);
2787 rt2800_rfcsr_write(rt2x00dev, 54, 0x38);
2788 rt2800_rfcsr_write(rt2x00dev, 56, 0xA1);
2789 rt2800_rfcsr_write(rt2x00dev, 57, 0x00);
2790 rt2800_rfcsr_write(rt2x00dev, 58, 0x39);
2791 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
2792 rt2800_rfcsr_write(rt2x00dev, 61, 0x91);
2793 rt2800_rfcsr_write(rt2x00dev, 62, 0x39);
2794
2795 /* TODO RF27 <- tssi */
2796
2797 rfcsr = rf->channel <= 10 ? 0x07 : 0x06;
2798 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
2799 rt2800_rfcsr_write(rt2x00dev, 59, rfcsr);
2800
2801 if (is_11b) {
2802 /* CCK */
2803 rt2800_rfcsr_write(rt2x00dev, 31, 0xF8);
2804 rt2800_rfcsr_write(rt2x00dev, 32, 0xC0);
2805 if (is_type_ep)
2806 rt2800_rfcsr_write(rt2x00dev, 55, 0x06);
2807 else
2808 rt2800_rfcsr_write(rt2x00dev, 55, 0x47);
2809 } else {
2810 /* OFDM */
2811 if (is_type_ep)
2812 rt2800_rfcsr_write(rt2x00dev, 55, 0x03);
2813 else
2814 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
2815 }
2816
2817 power_bound = POWER_BOUND;
2818 ep_reg = 0x2;
2819 } else {
2820 rt2800_rfcsr_write(rt2x00dev, 10, 0x97);
2821 /* FIMXE: RF11 overwrite */
2822 rt2800_rfcsr_write(rt2x00dev, 11, 0x40);
2823 rt2800_rfcsr_write(rt2x00dev, 25, 0xBF);
2824 rt2800_rfcsr_write(rt2x00dev, 27, 0x42);
2825 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
2826 rt2800_rfcsr_write(rt2x00dev, 37, 0x04);
2827 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
2828 rt2800_rfcsr_write(rt2x00dev, 40, 0x42);
2829 rt2800_rfcsr_write(rt2x00dev, 41, 0xBB);
2830 rt2800_rfcsr_write(rt2x00dev, 42, 0xD7);
2831 rt2800_rfcsr_write(rt2x00dev, 45, 0x41);
2832 rt2800_rfcsr_write(rt2x00dev, 48, 0x00);
2833 rt2800_rfcsr_write(rt2x00dev, 57, 0x77);
2834 rt2800_rfcsr_write(rt2x00dev, 60, 0x05);
2835 rt2800_rfcsr_write(rt2x00dev, 61, 0x01);
2836
2837 /* TODO RF27 <- tssi */
2838
2839 if (rf->channel >= 36 && rf->channel <= 64) {
2840
2841 rt2800_rfcsr_write(rt2x00dev, 12, 0x2E);
2842 rt2800_rfcsr_write(rt2x00dev, 13, 0x22);
2843 rt2800_rfcsr_write(rt2x00dev, 22, 0x60);
2844 rt2800_rfcsr_write(rt2x00dev, 23, 0x7F);
2845 if (rf->channel <= 50)
2846 rt2800_rfcsr_write(rt2x00dev, 24, 0x09);
2847 else if (rf->channel >= 52)
2848 rt2800_rfcsr_write(rt2x00dev, 24, 0x07);
2849 rt2800_rfcsr_write(rt2x00dev, 39, 0x1C);
2850 rt2800_rfcsr_write(rt2x00dev, 43, 0x5B);
2851 rt2800_rfcsr_write(rt2x00dev, 44, 0X40);
2852 rt2800_rfcsr_write(rt2x00dev, 46, 0X00);
2853 rt2800_rfcsr_write(rt2x00dev, 51, 0xFE);
2854 rt2800_rfcsr_write(rt2x00dev, 52, 0x0C);
2855 rt2800_rfcsr_write(rt2x00dev, 54, 0xF8);
2856 if (rf->channel <= 50) {
2857 rt2800_rfcsr_write(rt2x00dev, 55, 0x06),
2858 rt2800_rfcsr_write(rt2x00dev, 56, 0xD3);
2859 } else if (rf->channel >= 52) {
2860 rt2800_rfcsr_write(rt2x00dev, 55, 0x04);
2861 rt2800_rfcsr_write(rt2x00dev, 56, 0xBB);
2862 }
2863
2864 rt2800_rfcsr_write(rt2x00dev, 58, 0x15);
2865 rt2800_rfcsr_write(rt2x00dev, 59, 0x7F);
2866 rt2800_rfcsr_write(rt2x00dev, 62, 0x15);
2867
2868 } else if (rf->channel >= 100 && rf->channel <= 165) {
2869
2870 rt2800_rfcsr_write(rt2x00dev, 12, 0x0E);
2871 rt2800_rfcsr_write(rt2x00dev, 13, 0x42);
2872 rt2800_rfcsr_write(rt2x00dev, 22, 0x40);
2873 if (rf->channel <= 153) {
2874 rt2800_rfcsr_write(rt2x00dev, 23, 0x3C);
2875 rt2800_rfcsr_write(rt2x00dev, 24, 0x06);
2876 } else if (rf->channel >= 155) {
2877 rt2800_rfcsr_write(rt2x00dev, 23, 0x38);
2878 rt2800_rfcsr_write(rt2x00dev, 24, 0x05);
2879 }
2880 if (rf->channel <= 138) {
2881 rt2800_rfcsr_write(rt2x00dev, 39, 0x1A);
2882 rt2800_rfcsr_write(rt2x00dev, 43, 0x3B);
2883 rt2800_rfcsr_write(rt2x00dev, 44, 0x20);
2884 rt2800_rfcsr_write(rt2x00dev, 46, 0x18);
2885 } else if (rf->channel >= 140) {
2886 rt2800_rfcsr_write(rt2x00dev, 39, 0x18);
2887 rt2800_rfcsr_write(rt2x00dev, 43, 0x1B);
2888 rt2800_rfcsr_write(rt2x00dev, 44, 0x10);
2889 rt2800_rfcsr_write(rt2x00dev, 46, 0X08);
2890 }
2891 if (rf->channel <= 124)
2892 rt2800_rfcsr_write(rt2x00dev, 51, 0xFC);
2893 else if (rf->channel >= 126)
2894 rt2800_rfcsr_write(rt2x00dev, 51, 0xEC);
2895 if (rf->channel <= 138)
2896 rt2800_rfcsr_write(rt2x00dev, 52, 0x06);
2897 else if (rf->channel >= 140)
2898 rt2800_rfcsr_write(rt2x00dev, 52, 0x06);
2899 rt2800_rfcsr_write(rt2x00dev, 54, 0xEB);
2900 if (rf->channel <= 138)
2901 rt2800_rfcsr_write(rt2x00dev, 55, 0x01);
2902 else if (rf->channel >= 140)
2903 rt2800_rfcsr_write(rt2x00dev, 55, 0x00);
2904 if (rf->channel <= 128)
2905 rt2800_rfcsr_write(rt2x00dev, 56, 0xBB);
2906 else if (rf->channel >= 130)
2907 rt2800_rfcsr_write(rt2x00dev, 56, 0xAB);
2908 if (rf->channel <= 116)
2909 rt2800_rfcsr_write(rt2x00dev, 58, 0x1D);
2910 else if (rf->channel >= 118)
2911 rt2800_rfcsr_write(rt2x00dev, 58, 0x15);
2912 if (rf->channel <= 138)
2913 rt2800_rfcsr_write(rt2x00dev, 59, 0x3F);
2914 else if (rf->channel >= 140)
2915 rt2800_rfcsr_write(rt2x00dev, 59, 0x7C);
2916 if (rf->channel <= 116)
2917 rt2800_rfcsr_write(rt2x00dev, 62, 0x1D);
2918 else if (rf->channel >= 118)
2919 rt2800_rfcsr_write(rt2x00dev, 62, 0x15);
2920 }
2921
2922 power_bound = POWER_BOUND_5G;
2923 ep_reg = 0x3;
2924 }
2925
2926 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
2927 if (info->default_power1 > power_bound)
2928 rt2x00_set_field8(&rfcsr, RFCSR49_TX, power_bound);
2929 else
2930 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2931 if (is_type_ep)
2932 rt2x00_set_field8(&rfcsr, RFCSR49_EP, ep_reg);
2933 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
2934
2935 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
Gabor Juhos0847beb2013-06-25 22:57:29 +02002936 if (info->default_power2 > power_bound)
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002937 rt2x00_set_field8(&rfcsr, RFCSR50_TX, power_bound);
2938 else
2939 rt2x00_set_field8(&rfcsr, RFCSR50_TX, info->default_power2);
2940 if (is_type_ep)
2941 rt2x00_set_field8(&rfcsr, RFCSR50_EP, ep_reg);
2942 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
2943
2944 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2945 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2946 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
2947
2948 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD,
2949 rt2x00dev->default_ant.tx_chain_num >= 1);
2950 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD,
2951 rt2x00dev->default_ant.tx_chain_num == 2);
2952 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
2953
2954 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD,
2955 rt2x00dev->default_ant.rx_chain_num >= 1);
2956 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD,
2957 rt2x00dev->default_ant.rx_chain_num == 2);
2958 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2959
2960 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2961 rt2800_rfcsr_write(rt2x00dev, 6, 0xe4);
2962
2963 if (conf_is_ht40(conf))
2964 rt2800_rfcsr_write(rt2x00dev, 30, 0x16);
2965 else
2966 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
2967
2968 if (!is_11b) {
2969 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
2970 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
2971 }
2972
2973 /* TODO proper frequency adjustment */
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002974 rt2800_adjust_freq_offset(rt2x00dev);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002975
2976 /* TODO merge with others */
2977 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
2978 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
2979 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
Stanislaw Gruszka68031412013-03-16 19:19:44 +01002980
2981 /* BBP settings */
2982 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
2983 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
2984 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
2985
2986 rt2800_bbp_write(rt2x00dev, 79, (rf->channel <= 14) ? 0x1C : 0x18);
2987 rt2800_bbp_write(rt2x00dev, 80, (rf->channel <= 14) ? 0x0E : 0x08);
2988 rt2800_bbp_write(rt2x00dev, 81, (rf->channel <= 14) ? 0x3A : 0x38);
2989 rt2800_bbp_write(rt2x00dev, 82, (rf->channel <= 14) ? 0x62 : 0x92);
2990
2991 /* GLRT band configuration */
2992 rt2800_bbp_write(rt2x00dev, 195, 128);
2993 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0xE0 : 0xF0);
2994 rt2800_bbp_write(rt2x00dev, 195, 129);
2995 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x1F : 0x1E);
2996 rt2800_bbp_write(rt2x00dev, 195, 130);
2997 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x38 : 0x28);
2998 rt2800_bbp_write(rt2x00dev, 195, 131);
2999 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x32 : 0x20);
3000 rt2800_bbp_write(rt2x00dev, 195, 133);
3001 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x28 : 0x7F);
3002 rt2800_bbp_write(rt2x00dev, 195, 124);
3003 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x19 : 0x7F);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01003004}
3005
Stanislaw Gruszka5bc2dd02013-03-16 19:19:47 +01003006static void rt2800_bbp_write_with_rx_chain(struct rt2x00_dev *rt2x00dev,
3007 const unsigned int word,
3008 const u8 value)
3009{
3010 u8 chain, reg;
3011
3012 for (chain = 0; chain < rt2x00dev->default_ant.rx_chain_num; chain++) {
3013 rt2800_bbp_read(rt2x00dev, 27, &reg);
3014 rt2x00_set_field8(&reg, BBP27_RX_CHAIN_SEL, chain);
3015 rt2800_bbp_write(rt2x00dev, 27, reg);
3016
3017 rt2800_bbp_write(rt2x00dev, word, value);
3018 }
3019}
3020
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003021static void rt2800_iq_calibrate(struct rt2x00_dev *rt2x00dev, int channel)
3022{
3023 u8 cal;
3024
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003025 /* TX0 IQ Gain */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003026 rt2800_bbp_write(rt2x00dev, 158, 0x2c);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003027 if (channel <= 14)
3028 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_GAIN_CAL_TX0_2G);
3029 else if (channel >= 36 && channel <= 64)
3030 cal = rt2x00_eeprom_byte(rt2x00dev,
3031 EEPROM_IQ_GAIN_CAL_TX0_CH36_TO_CH64_5G);
3032 else if (channel >= 100 && channel <= 138)
3033 cal = rt2x00_eeprom_byte(rt2x00dev,
3034 EEPROM_IQ_GAIN_CAL_TX0_CH100_TO_CH138_5G);
3035 else if (channel >= 140 && channel <= 165)
3036 cal = rt2x00_eeprom_byte(rt2x00dev,
3037 EEPROM_IQ_GAIN_CAL_TX0_CH140_TO_CH165_5G);
3038 else
3039 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003040 rt2800_bbp_write(rt2x00dev, 159, cal);
3041
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003042 /* TX0 IQ Phase */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003043 rt2800_bbp_write(rt2x00dev, 158, 0x2d);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003044 if (channel <= 14)
3045 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_PHASE_CAL_TX0_2G);
3046 else if (channel >= 36 && channel <= 64)
3047 cal = rt2x00_eeprom_byte(rt2x00dev,
3048 EEPROM_IQ_PHASE_CAL_TX0_CH36_TO_CH64_5G);
3049 else if (channel >= 100 && channel <= 138)
3050 cal = rt2x00_eeprom_byte(rt2x00dev,
3051 EEPROM_IQ_PHASE_CAL_TX0_CH100_TO_CH138_5G);
3052 else if (channel >= 140 && channel <= 165)
3053 cal = rt2x00_eeprom_byte(rt2x00dev,
3054 EEPROM_IQ_PHASE_CAL_TX0_CH140_TO_CH165_5G);
3055 else
3056 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003057 rt2800_bbp_write(rt2x00dev, 159, cal);
3058
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003059 /* TX1 IQ Gain */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003060 rt2800_bbp_write(rt2x00dev, 158, 0x4a);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003061 if (channel <= 14)
3062 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_GAIN_CAL_TX1_2G);
3063 else if (channel >= 36 && channel <= 64)
3064 cal = rt2x00_eeprom_byte(rt2x00dev,
3065 EEPROM_IQ_GAIN_CAL_TX1_CH36_TO_CH64_5G);
3066 else if (channel >= 100 && channel <= 138)
3067 cal = rt2x00_eeprom_byte(rt2x00dev,
3068 EEPROM_IQ_GAIN_CAL_TX1_CH100_TO_CH138_5G);
3069 else if (channel >= 140 && channel <= 165)
3070 cal = rt2x00_eeprom_byte(rt2x00dev,
3071 EEPROM_IQ_GAIN_CAL_TX1_CH140_TO_CH165_5G);
3072 else
3073 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003074 rt2800_bbp_write(rt2x00dev, 159, cal);
3075
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003076 /* TX1 IQ Phase */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003077 rt2800_bbp_write(rt2x00dev, 158, 0x4b);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003078 if (channel <= 14)
3079 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_PHASE_CAL_TX1_2G);
3080 else if (channel >= 36 && channel <= 64)
3081 cal = rt2x00_eeprom_byte(rt2x00dev,
3082 EEPROM_IQ_PHASE_CAL_TX1_CH36_TO_CH64_5G);
3083 else if (channel >= 100 && channel <= 138)
3084 cal = rt2x00_eeprom_byte(rt2x00dev,
3085 EEPROM_IQ_PHASE_CAL_TX1_CH100_TO_CH138_5G);
3086 else if (channel >= 140 && channel <= 165)
3087 cal = rt2x00_eeprom_byte(rt2x00dev,
3088 EEPROM_IQ_PHASE_CAL_TX1_CH140_TO_CH165_5G);
3089 else
3090 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003091 rt2800_bbp_write(rt2x00dev, 159, cal);
3092
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003093 /* FIXME: possible RX0, RX1 callibration ? */
3094
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003095 /* RF IQ compensation control */
3096 rt2800_bbp_write(rt2x00dev, 158, 0x04);
3097 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_RF_IQ_COMPENSATION_CONTROL);
3098 rt2800_bbp_write(rt2x00dev, 159, cal != 0xff ? cal : 0);
3099
3100 /* RF IQ imbalance compensation control */
3101 rt2800_bbp_write(rt2x00dev, 158, 0x03);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003102 cal = rt2x00_eeprom_byte(rt2x00dev,
3103 EEPROM_RF_IQ_IMBALANCE_COMPENSATION_CONTROL);
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003104 rt2800_bbp_write(rt2x00dev, 159, cal != 0xff ? cal : 0);
3105}
3106
Gabor Juhos97aa03f2013-07-08 16:08:23 +02003107static char rt2800_txpower_to_dev(struct rt2x00_dev *rt2x00dev,
3108 unsigned int channel,
3109 char txpower)
3110{
Gabor Juhosfc739cf2013-07-08 16:08:24 +02003111 if (rt2x00_rt(rt2x00dev, RT3593))
3112 txpower = rt2x00_get_field8(txpower, EEPROM_TXPOWER_ALC);
3113
Gabor Juhos97aa03f2013-07-08 16:08:23 +02003114 if (channel <= 14)
3115 return clamp_t(char, txpower, MIN_G_TXPOWER, MAX_G_TXPOWER);
Gabor Juhosfc739cf2013-07-08 16:08:24 +02003116
3117 if (rt2x00_rt(rt2x00dev, RT3593))
3118 return clamp_t(char, txpower, MIN_A_TXPOWER_3593,
3119 MAX_A_TXPOWER_3593);
Gabor Juhos97aa03f2013-07-08 16:08:23 +02003120 else
3121 return clamp_t(char, txpower, MIN_A_TXPOWER, MAX_A_TXPOWER);
3122}
3123
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003124static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
3125 struct ieee80211_conf *conf,
3126 struct rf_channel *rf,
3127 struct channel_info *info)
3128{
3129 u32 reg;
3130 unsigned int tx_pin;
Woody Hunga89534e2012-06-13 15:01:16 +08003131 u8 bbp, rfcsr;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003132
Gabor Juhos97aa03f2013-07-08 16:08:23 +02003133 info->default_power1 = rt2800_txpower_to_dev(rt2x00dev, rf->channel,
3134 info->default_power1);
3135 info->default_power2 = rt2800_txpower_to_dev(rt2x00dev, rf->channel,
3136 info->default_power2);
Gabor Juhosc0a14362013-07-08 16:08:28 +02003137 if (rt2x00dev->default_ant.tx_chain_num > 2)
3138 info->default_power3 =
3139 rt2800_txpower_to_dev(rt2x00dev, rf->channel,
3140 info->default_power3);
Ivo van Doorn46323e12010-08-23 19:55:43 +02003141
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003142 switch (rt2x00dev->chip.rf) {
3143 case RF2020:
3144 case RF3020:
3145 case RF3021:
3146 case RF3022:
3147 case RF3320:
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02003148 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003149 break;
3150 case RF3052:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003151 rt2800_config_channel_rf3052(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003152 break;
Gabor Juhosf42b0462013-07-08 16:08:30 +02003153 case RF3053:
3154 rt2800_config_channel_rf3053(rt2x00dev, conf, rf, info);
3155 break;
Woody Hunga89534e2012-06-13 15:01:16 +08003156 case RF3290:
3157 rt2800_config_channel_rf3290(rt2x00dev, conf, rf, info);
3158 break;
Daniel Golle03839952012-09-09 14:24:39 +03003159 case RF3322:
3160 rt2800_config_channel_rf3322(rt2x00dev, conf, rf, info);
3161 break;
Stanislaw Gruszka3b9b74b2013-09-25 15:34:55 +02003162 case RF3070:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02003163 case RF5360:
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05003164 case RF5362:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003165 case RF5370:
John Li2ed71882012-02-17 17:33:06 +08003166 case RF5372:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003167 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08003168 case RF5392:
Gabor Juhosadde5882011-03-03 11:46:45 +01003169 rt2800_config_channel_rf53xx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003170 break;
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01003171 case RF5592:
3172 rt2800_config_channel_rf55xx(rt2x00dev, conf, rf, info);
3173 break;
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003174 default:
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02003175 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003176 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003177
Stanislaw Gruszka3b9b74b2013-09-25 15:34:55 +02003178 if (rt2x00_rf(rt2x00dev, RF3070) ||
3179 rt2x00_rf(rt2x00dev, RF3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003180 rt2x00_rf(rt2x00dev, RF3322) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003181 rt2x00_rf(rt2x00dev, RF5360) ||
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05003182 rt2x00_rf(rt2x00dev, RF5362) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003183 rt2x00_rf(rt2x00dev, RF5370) ||
3184 rt2x00_rf(rt2x00dev, RF5372) ||
3185 rt2x00_rf(rt2x00dev, RF5390) ||
3186 rt2x00_rf(rt2x00dev, RF5392)) {
3187 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
3188 rt2x00_set_field8(&rfcsr, RFCSR30_TX_H20M, 0);
3189 rt2x00_set_field8(&rfcsr, RFCSR30_RX_H20M, 0);
3190 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
3191
3192 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
Gabor Juhosd6d82022012-12-02 18:34:47 +01003193 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
Woody Hunga89534e2012-06-13 15:01:16 +08003194 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
3195 }
3196
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003197 /*
3198 * Change BBP settings
3199 */
Daniel Golle03839952012-09-09 14:24:39 +03003200 if (rt2x00_rt(rt2x00dev, RT3352)) {
3201 rt2800_bbp_write(rt2x00dev, 27, 0x0);
Daniel Gollecf193f62012-10-04 01:20:41 +02003202 rt2800_bbp_write(rt2x00dev, 66, 0x26 + rt2x00dev->lna_gain);
Daniel Golle03839952012-09-09 14:24:39 +03003203 rt2800_bbp_write(rt2x00dev, 27, 0x20);
Daniel Gollecf193f62012-10-04 01:20:41 +02003204 rt2800_bbp_write(rt2x00dev, 66, 0x26 + rt2x00dev->lna_gain);
Gabor Juhosf42b0462013-07-08 16:08:30 +02003205 } else if (rt2x00_rt(rt2x00dev, RT3593)) {
3206 if (rf->channel > 14) {
3207 /* Disable CCK Packet detection on 5GHz */
3208 rt2800_bbp_write(rt2x00dev, 70, 0x00);
3209 } else {
3210 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
3211 }
3212
3213 if (conf_is_ht40(conf))
3214 rt2800_bbp_write(rt2x00dev, 105, 0x04);
3215 else
3216 rt2800_bbp_write(rt2x00dev, 105, 0x34);
3217
3218 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
3219 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
3220 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
3221 rt2800_bbp_write(rt2x00dev, 77, 0x98);
Daniel Golle03839952012-09-09 14:24:39 +03003222 } else {
3223 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
3224 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
3225 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
3226 rt2800_bbp_write(rt2x00dev, 86, 0);
3227 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003228
3229 if (rf->channel <= 14) {
John Li2ed71882012-02-17 17:33:06 +08003230 if (!rt2x00_rt(rt2x00dev, RT5390) &&
Gabor Juhose6d227b2012-12-02 15:53:28 +01003231 !rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosc429dfe2013-10-11 13:18:42 +02003232 if (rt2x00_has_cap_external_lna_bg(rt2x00dev)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01003233 rt2800_bbp_write(rt2x00dev, 82, 0x62);
3234 rt2800_bbp_write(rt2x00dev, 75, 0x46);
3235 } else {
Gabor Juhosf42b0462013-07-08 16:08:30 +02003236 if (rt2x00_rt(rt2x00dev, RT3593))
3237 rt2800_bbp_write(rt2x00dev, 82, 0x62);
3238 else
3239 rt2800_bbp_write(rt2x00dev, 82, 0x84);
Gabor Juhosadde5882011-03-03 11:46:45 +01003240 rt2800_bbp_write(rt2x00dev, 75, 0x50);
3241 }
Gabor Juhosf42b0462013-07-08 16:08:30 +02003242 if (rt2x00_rt(rt2x00dev, RT3593))
3243 rt2800_bbp_write(rt2x00dev, 83, 0x8a);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003244 }
Gabor Juhosf42b0462013-07-08 16:08:30 +02003245
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003246 } else {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003247 if (rt2x00_rt(rt2x00dev, RT3572))
3248 rt2800_bbp_write(rt2x00dev, 82, 0x94);
Gabor Juhosf42b0462013-07-08 16:08:30 +02003249 else if (rt2x00_rt(rt2x00dev, RT3593))
3250 rt2800_bbp_write(rt2x00dev, 82, 0x82);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003251 else
3252 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003253
Gabor Juhosf42b0462013-07-08 16:08:30 +02003254 if (rt2x00_rt(rt2x00dev, RT3593))
3255 rt2800_bbp_write(rt2x00dev, 83, 0x9a);
3256
Gabor Juhosc429dfe2013-10-11 13:18:42 +02003257 if (rt2x00_has_cap_external_lna_a(rt2x00dev))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003258 rt2800_bbp_write(rt2x00dev, 75, 0x46);
3259 else
3260 rt2800_bbp_write(rt2x00dev, 75, 0x50);
3261 }
3262
3263 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02003264 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003265 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
3266 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
3267 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
3268
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003269 if (rt2x00_rt(rt2x00dev, RT3572))
3270 rt2800_rfcsr_write(rt2x00dev, 8, 0);
3271
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003272 tx_pin = 0;
3273
Gabor Juhosbb16d482013-06-24 23:03:24 +02003274 switch (rt2x00dev->default_ant.tx_chain_num) {
3275 case 3:
3276 /* Turn on tertiary PAs */
3277 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A2_EN,
3278 rf->channel > 14);
3279 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G2_EN,
3280 rf->channel <= 14);
3281 /* fall-through */
3282 case 2:
3283 /* Turn on secondary PAs */
Gertjan van Wingerde65f31b52011-05-18 20:25:05 +02003284 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN,
3285 rf->channel > 14);
3286 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN,
3287 rf->channel <= 14);
Gabor Juhosbb16d482013-06-24 23:03:24 +02003288 /* fall-through */
3289 case 1:
3290 /* Turn on primary PAs */
3291 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN,
3292 rf->channel > 14);
Gabor Juhosc429dfe2013-10-11 13:18:42 +02003293 if (rt2x00_has_cap_bt_coexist(rt2x00dev))
Gabor Juhosbb16d482013-06-24 23:03:24 +02003294 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
3295 else
3296 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN,
3297 rf->channel <= 14);
3298 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003299 }
3300
Gabor Juhosbb16d482013-06-24 23:03:24 +02003301 switch (rt2x00dev->default_ant.rx_chain_num) {
3302 case 3:
3303 /* Turn on tertiary LNAs */
3304 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A2_EN, 1);
3305 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G2_EN, 1);
3306 /* fall-through */
3307 case 2:
3308 /* Turn on secondary LNAs */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003309 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
3310 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
Gabor Juhosbb16d482013-06-24 23:03:24 +02003311 /* fall-through */
3312 case 1:
3313 /* Turn on primary LNAs */
3314 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
3315 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
3316 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003317 }
3318
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003319 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
3320 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003321
3322 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
3323
Gabor Juhos733aec62013-10-04 22:07:09 +02003324 if (rt2x00_rt(rt2x00dev, RT3572)) {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003325 rt2800_rfcsr_write(rt2x00dev, 8, 0x80);
3326
Gabor Juhos733aec62013-10-04 22:07:09 +02003327 /* AGC init */
3328 if (rf->channel <= 14)
3329 reg = 0x1c + (2 * rt2x00dev->lna_gain);
3330 else
3331 reg = 0x22 + ((rt2x00dev->lna_gain * 5) / 3);
3332
3333 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66, reg);
3334 }
3335
Gabor Juhosf42b0462013-07-08 16:08:30 +02003336 if (rt2x00_rt(rt2x00dev, RT3593)) {
Gabor Juhos60751002013-09-11 19:56:45 +02003337 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
Gabor Juhosf42b0462013-07-08 16:08:30 +02003338
Gabor Juhos60751002013-09-11 19:56:45 +02003339 /* Band selection */
3340 if (rt2x00_is_usb(rt2x00dev) ||
3341 rt2x00_is_pcie(rt2x00dev)) {
3342 /* GPIO #8 controls all paths */
Gabor Juhosf42b0462013-07-08 16:08:30 +02003343 rt2x00_set_field32(&reg, GPIO_CTRL_DIR8, 0);
3344 if (rf->channel <= 14)
3345 rt2x00_set_field32(&reg, GPIO_CTRL_VAL8, 1);
3346 else
3347 rt2x00_set_field32(&reg, GPIO_CTRL_VAL8, 0);
Gabor Juhos60751002013-09-11 19:56:45 +02003348 }
Gabor Juhosf42b0462013-07-08 16:08:30 +02003349
Gabor Juhos60751002013-09-11 19:56:45 +02003350 /* LNA PE control. */
3351 if (rt2x00_is_usb(rt2x00dev)) {
3352 /* GPIO #4 controls PE0 and PE1,
3353 * GPIO #7 controls PE2
3354 */
Gabor Juhosf42b0462013-07-08 16:08:30 +02003355 rt2x00_set_field32(&reg, GPIO_CTRL_DIR4, 0);
3356 rt2x00_set_field32(&reg, GPIO_CTRL_DIR7, 0);
3357
Gabor Juhosf42b0462013-07-08 16:08:30 +02003358 rt2x00_set_field32(&reg, GPIO_CTRL_VAL4, 1);
3359 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 1);
Gabor Juhos60751002013-09-11 19:56:45 +02003360 } else if (rt2x00_is_pcie(rt2x00dev)) {
3361 /* GPIO #4 controls PE0, PE1 and PE2 */
3362 rt2x00_set_field32(&reg, GPIO_CTRL_DIR4, 0);
3363 rt2x00_set_field32(&reg, GPIO_CTRL_VAL4, 1);
Gabor Juhosf42b0462013-07-08 16:08:30 +02003364 }
3365
Gabor Juhos60751002013-09-11 19:56:45 +02003366 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
3367
Gabor Juhosf42b0462013-07-08 16:08:30 +02003368 /* AGC init */
3369 if (rf->channel <= 14)
3370 reg = 0x1c + 2 * rt2x00dev->lna_gain;
3371 else
3372 reg = 0x22 + ((rt2x00dev->lna_gain * 5) / 3);
3373
3374 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66, reg);
3375
3376 usleep_range(1000, 1500);
3377 }
3378
Stanislaw Gruszka68031412013-03-16 19:19:44 +01003379 if (rt2x00_rt(rt2x00dev, RT5592)) {
3380 rt2800_bbp_write(rt2x00dev, 195, 141);
3381 rt2800_bbp_write(rt2x00dev, 196, conf_is_ht40(conf) ? 0x10 : 0x1a);
3382
Stanislaw Gruszka8ba0ebf2013-03-16 19:19:48 +01003383 /* AGC init */
3384 reg = (rf->channel <= 14 ? 0x1c : 0x24) + 2 * rt2x00dev->lna_gain;
3385 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66, reg);
3386
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003387 rt2800_iq_calibrate(rt2x00dev, rf->channel);
Stanislaw Gruszka68031412013-03-16 19:19:44 +01003388 }
3389
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003390 rt2800_bbp_read(rt2x00dev, 4, &bbp);
3391 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
3392 rt2800_bbp_write(rt2x00dev, 4, bbp);
3393
3394 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02003395 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003396 rt2800_bbp_write(rt2x00dev, 3, bbp);
3397
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02003398 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003399 if (conf_is_ht40(conf)) {
3400 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
3401 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
3402 rt2800_bbp_write(rt2x00dev, 73, 0x16);
3403 } else {
3404 rt2800_bbp_write(rt2x00dev, 69, 0x16);
3405 rt2800_bbp_write(rt2x00dev, 70, 0x08);
3406 rt2800_bbp_write(rt2x00dev, 73, 0x11);
3407 }
3408 }
3409
3410 msleep(1);
Helmut Schaa977206d2010-12-13 12:31:58 +01003411
3412 /*
3413 * Clear channel statistic counters
3414 */
3415 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &reg);
3416 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &reg);
3417 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &reg);
Daniel Golle03839952012-09-09 14:24:39 +03003418
3419 /*
3420 * Clear update flag
3421 */
3422 if (rt2x00_rt(rt2x00dev, RT3352)) {
3423 rt2800_bbp_read(rt2x00dev, 49, &bbp);
3424 rt2x00_set_field8(&bbp, BBP49_UPDATE_FLAG, 0);
3425 rt2800_bbp_write(rt2x00dev, 49, bbp);
3426 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003427}
3428
Helmut Schaa9e33a352011-03-28 13:33:40 +02003429static int rt2800_get_gain_calibration_delta(struct rt2x00_dev *rt2x00dev)
3430{
3431 u8 tssi_bounds[9];
3432 u8 current_tssi;
3433 u16 eeprom;
3434 u8 step;
3435 int i;
3436
3437 /*
Stanislaw Gruszka6e956da2013-08-26 15:18:53 +02003438 * First check if temperature compensation is supported.
3439 */
3440 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
3441 if (!rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC))
3442 return 0;
3443
3444 /*
Helmut Schaa9e33a352011-03-28 13:33:40 +02003445 * Read TSSI boundaries for temperature compensation from
3446 * the EEPROM.
3447 *
3448 * Array idx 0 1 2 3 4 5 6 7 8
3449 * Matching Delta value -4 -3 -2 -1 0 +1 +2 +3 +4
3450 * Example TSSI bounds 0xF0 0xD0 0xB5 0xA0 0x88 0x45 0x25 0x15 0x00
3451 */
Johannes Berg57fbcce2016-04-12 15:56:15 +02003452 if (rt2x00dev->curr_band == NL80211_BAND_2GHZ) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003453 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG1, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003454 tssi_bounds[0] = rt2x00_get_field16(eeprom,
3455 EEPROM_TSSI_BOUND_BG1_MINUS4);
3456 tssi_bounds[1] = rt2x00_get_field16(eeprom,
3457 EEPROM_TSSI_BOUND_BG1_MINUS3);
3458
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003459 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG2, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003460 tssi_bounds[2] = rt2x00_get_field16(eeprom,
3461 EEPROM_TSSI_BOUND_BG2_MINUS2);
3462 tssi_bounds[3] = rt2x00_get_field16(eeprom,
3463 EEPROM_TSSI_BOUND_BG2_MINUS1);
3464
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003465 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG3, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003466 tssi_bounds[4] = rt2x00_get_field16(eeprom,
3467 EEPROM_TSSI_BOUND_BG3_REF);
3468 tssi_bounds[5] = rt2x00_get_field16(eeprom,
3469 EEPROM_TSSI_BOUND_BG3_PLUS1);
3470
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003471 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG4, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003472 tssi_bounds[6] = rt2x00_get_field16(eeprom,
3473 EEPROM_TSSI_BOUND_BG4_PLUS2);
3474 tssi_bounds[7] = rt2x00_get_field16(eeprom,
3475 EEPROM_TSSI_BOUND_BG4_PLUS3);
3476
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003477 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG5, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003478 tssi_bounds[8] = rt2x00_get_field16(eeprom,
3479 EEPROM_TSSI_BOUND_BG5_PLUS4);
3480
3481 step = rt2x00_get_field16(eeprom,
3482 EEPROM_TSSI_BOUND_BG5_AGC_STEP);
3483 } else {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003484 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A1, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003485 tssi_bounds[0] = rt2x00_get_field16(eeprom,
3486 EEPROM_TSSI_BOUND_A1_MINUS4);
3487 tssi_bounds[1] = rt2x00_get_field16(eeprom,
3488 EEPROM_TSSI_BOUND_A1_MINUS3);
3489
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003490 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A2, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003491 tssi_bounds[2] = rt2x00_get_field16(eeprom,
3492 EEPROM_TSSI_BOUND_A2_MINUS2);
3493 tssi_bounds[3] = rt2x00_get_field16(eeprom,
3494 EEPROM_TSSI_BOUND_A2_MINUS1);
3495
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003496 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A3, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003497 tssi_bounds[4] = rt2x00_get_field16(eeprom,
3498 EEPROM_TSSI_BOUND_A3_REF);
3499 tssi_bounds[5] = rt2x00_get_field16(eeprom,
3500 EEPROM_TSSI_BOUND_A3_PLUS1);
3501
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003502 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A4, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003503 tssi_bounds[6] = rt2x00_get_field16(eeprom,
3504 EEPROM_TSSI_BOUND_A4_PLUS2);
3505 tssi_bounds[7] = rt2x00_get_field16(eeprom,
3506 EEPROM_TSSI_BOUND_A4_PLUS3);
3507
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003508 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A5, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003509 tssi_bounds[8] = rt2x00_get_field16(eeprom,
3510 EEPROM_TSSI_BOUND_A5_PLUS4);
3511
3512 step = rt2x00_get_field16(eeprom,
3513 EEPROM_TSSI_BOUND_A5_AGC_STEP);
3514 }
3515
3516 /*
3517 * Check if temperature compensation is supported.
3518 */
Stanislaw Gruszkabf7e1ab2012-10-25 09:51:39 +02003519 if (tssi_bounds[4] == 0xff || step == 0xff)
Helmut Schaa9e33a352011-03-28 13:33:40 +02003520 return 0;
3521
3522 /*
3523 * Read current TSSI (BBP 49).
3524 */
3525 rt2800_bbp_read(rt2x00dev, 49, &current_tssi);
3526
3527 /*
3528 * Compare TSSI value (BBP49) with the compensation boundaries
3529 * from the EEPROM and increase or decrease tx power.
3530 */
3531 for (i = 0; i <= 3; i++) {
3532 if (current_tssi > tssi_bounds[i])
3533 break;
3534 }
3535
3536 if (i == 4) {
3537 for (i = 8; i >= 5; i--) {
3538 if (current_tssi < tssi_bounds[i])
3539 break;
3540 }
3541 }
3542
3543 return (i - 4) * step;
3544}
3545
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003546static int rt2800_get_txpower_bw_comp(struct rt2x00_dev *rt2x00dev,
Johannes Berg57fbcce2016-04-12 15:56:15 +02003547 enum nl80211_band band)
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003548{
3549 u16 eeprom;
3550 u8 comp_en;
3551 u8 comp_type;
Helmut Schaa75faae82011-03-28 13:31:30 +02003552 int comp_value = 0;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003553
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003554 rt2800_eeprom_read(rt2x00dev, EEPROM_TXPOWER_DELTA, &eeprom);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003555
Helmut Schaa75faae82011-03-28 13:31:30 +02003556 /*
3557 * HT40 compensation not required.
3558 */
3559 if (eeprom == 0xffff ||
3560 !test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003561 return 0;
3562
Johannes Berg57fbcce2016-04-12 15:56:15 +02003563 if (band == NL80211_BAND_2GHZ) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003564 comp_en = rt2x00_get_field16(eeprom,
3565 EEPROM_TXPOWER_DELTA_ENABLE_2G);
3566 if (comp_en) {
3567 comp_type = rt2x00_get_field16(eeprom,
3568 EEPROM_TXPOWER_DELTA_TYPE_2G);
3569 comp_value = rt2x00_get_field16(eeprom,
3570 EEPROM_TXPOWER_DELTA_VALUE_2G);
3571 if (!comp_type)
3572 comp_value = -comp_value;
3573 }
3574 } else {
3575 comp_en = rt2x00_get_field16(eeprom,
3576 EEPROM_TXPOWER_DELTA_ENABLE_5G);
3577 if (comp_en) {
3578 comp_type = rt2x00_get_field16(eeprom,
3579 EEPROM_TXPOWER_DELTA_TYPE_5G);
3580 comp_value = rt2x00_get_field16(eeprom,
3581 EEPROM_TXPOWER_DELTA_VALUE_5G);
3582 if (!comp_type)
3583 comp_value = -comp_value;
3584 }
3585 }
3586
3587 return comp_value;
3588}
3589
Stanislaw Gruszka1e4cf242012-10-05 13:44:14 +02003590static int rt2800_get_txpower_reg_delta(struct rt2x00_dev *rt2x00dev,
3591 int power_level, int max_power)
3592{
3593 int delta;
3594
Gabor Juhosc429dfe2013-10-11 13:18:42 +02003595 if (rt2x00_has_cap_power_limit(rt2x00dev))
Stanislaw Gruszka1e4cf242012-10-05 13:44:14 +02003596 return 0;
3597
3598 /*
3599 * XXX: We don't know the maximum transmit power of our hardware since
3600 * the EEPROM doesn't expose it. We only know that we are calibrated
3601 * to 100% tx power.
3602 *
3603 * Hence, we assume the regulatory limit that cfg80211 calulated for
3604 * the current channel is our maximum and if we are requested to lower
3605 * the value we just reduce our tx power accordingly.
3606 */
3607 delta = power_level - max_power;
3608 return min(delta, 0);
3609}
3610
Helmut Schaafa71a162011-03-28 13:32:32 +02003611static u8 rt2800_compensate_txpower(struct rt2x00_dev *rt2x00dev, int is_rate_b,
Johannes Berg57fbcce2016-04-12 15:56:15 +02003612 enum nl80211_band band, int power_level,
Helmut Schaafa71a162011-03-28 13:32:32 +02003613 u8 txpower, int delta)
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003614{
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003615 u16 eeprom;
3616 u8 criterion;
3617 u8 eirp_txpower;
3618 u8 eirp_txpower_criterion;
3619 u8 reg_limit;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003620
Gabor Juhos34542ff2013-07-08 16:08:20 +02003621 if (rt2x00_rt(rt2x00dev, RT3593))
3622 return min_t(u8, txpower, 0xc);
3623
Gabor Juhosc429dfe2013-10-11 13:18:42 +02003624 if (rt2x00_has_cap_power_limit(rt2x00dev)) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003625 /*
3626 * Check if eirp txpower exceed txpower_limit.
3627 * We use OFDM 6M as criterion and its eirp txpower
3628 * is stored at EEPROM_EIRP_MAX_TX_POWER.
3629 * .11b data rate need add additional 4dbm
3630 * when calculating eirp txpower.
3631 */
Gabor Juhos022138c2013-07-08 11:25:54 +02003632 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3633 1, &eeprom);
Stanislaw Gruszkad9bceae2012-10-05 13:44:12 +02003634 criterion = rt2x00_get_field16(eeprom,
3635 EEPROM_TXPOWER_BYRATE_RATE0);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003636
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003637 rt2800_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER,
Stanislaw Gruszkad9bceae2012-10-05 13:44:12 +02003638 &eeprom);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003639
Johannes Berg57fbcce2016-04-12 15:56:15 +02003640 if (band == NL80211_BAND_2GHZ)
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003641 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
3642 EEPROM_EIRP_MAX_TX_POWER_2GHZ);
3643 else
3644 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
3645 EEPROM_EIRP_MAX_TX_POWER_5GHZ);
3646
3647 eirp_txpower = eirp_txpower_criterion + (txpower - criterion) +
Helmut Schaa2af242e2011-03-28 13:32:01 +02003648 (is_rate_b ? 4 : 0) + delta;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003649
3650 reg_limit = (eirp_txpower > power_level) ?
3651 (eirp_txpower - power_level) : 0;
3652 } else
3653 reg_limit = 0;
3654
Stanislaw Gruszka19f3fa22012-10-05 13:44:10 +02003655 txpower = max(0, txpower + delta - reg_limit);
3656 return min_t(u8, txpower, 0xc);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003657}
3658
Gabor Juhos34542ff2013-07-08 16:08:20 +02003659
3660enum {
3661 TX_PWR_CFG_0_IDX,
3662 TX_PWR_CFG_1_IDX,
3663 TX_PWR_CFG_2_IDX,
3664 TX_PWR_CFG_3_IDX,
3665 TX_PWR_CFG_4_IDX,
3666 TX_PWR_CFG_5_IDX,
3667 TX_PWR_CFG_6_IDX,
3668 TX_PWR_CFG_7_IDX,
3669 TX_PWR_CFG_8_IDX,
3670 TX_PWR_CFG_9_IDX,
3671 TX_PWR_CFG_0_EXT_IDX,
3672 TX_PWR_CFG_1_EXT_IDX,
3673 TX_PWR_CFG_2_EXT_IDX,
3674 TX_PWR_CFG_3_EXT_IDX,
3675 TX_PWR_CFG_4_EXT_IDX,
3676 TX_PWR_CFG_IDX_COUNT,
3677};
3678
3679static void rt2800_config_txpower_rt3593(struct rt2x00_dev *rt2x00dev,
3680 struct ieee80211_channel *chan,
3681 int power_level)
3682{
3683 u8 txpower;
3684 u16 eeprom;
3685 u32 regs[TX_PWR_CFG_IDX_COUNT];
3686 unsigned int offset;
Johannes Berg57fbcce2016-04-12 15:56:15 +02003687 enum nl80211_band band = chan->band;
Gabor Juhos34542ff2013-07-08 16:08:20 +02003688 int delta;
3689 int i;
3690
3691 memset(regs, '\0', sizeof(regs));
3692
3693 /* TODO: adapt TX power reduction from the rt28xx code */
3694
3695 /* calculate temperature compensation delta */
3696 delta = rt2800_get_gain_calibration_delta(rt2x00dev);
3697
Johannes Berg57fbcce2016-04-12 15:56:15 +02003698 if (band == NL80211_BAND_5GHZ)
Gabor Juhos34542ff2013-07-08 16:08:20 +02003699 offset = 16;
3700 else
3701 offset = 0;
3702
3703 if (test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
3704 offset += 8;
3705
3706 /* read the next four txpower values */
3707 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3708 offset, &eeprom);
3709
3710 /* CCK 1MBS,2MBS */
3711 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3712 txpower = rt2800_compensate_txpower(rt2x00dev, 1, band, power_level,
3713 txpower, delta);
3714 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3715 TX_PWR_CFG_0_CCK1_CH0, txpower);
3716 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3717 TX_PWR_CFG_0_CCK1_CH1, txpower);
3718 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3719 TX_PWR_CFG_0_EXT_CCK1_CH2, txpower);
3720
3721 /* CCK 5.5MBS,11MBS */
3722 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3723 txpower = rt2800_compensate_txpower(rt2x00dev, 1, band, power_level,
3724 txpower, delta);
3725 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3726 TX_PWR_CFG_0_CCK5_CH0, txpower);
3727 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3728 TX_PWR_CFG_0_CCK5_CH1, txpower);
3729 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3730 TX_PWR_CFG_0_EXT_CCK5_CH2, txpower);
3731
3732 /* OFDM 6MBS,9MBS */
3733 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3734 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3735 txpower, delta);
3736 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3737 TX_PWR_CFG_0_OFDM6_CH0, txpower);
3738 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3739 TX_PWR_CFG_0_OFDM6_CH1, txpower);
3740 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3741 TX_PWR_CFG_0_EXT_OFDM6_CH2, txpower);
3742
3743 /* OFDM 12MBS,18MBS */
3744 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3745 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3746 txpower, delta);
3747 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3748 TX_PWR_CFG_0_OFDM12_CH0, txpower);
3749 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3750 TX_PWR_CFG_0_OFDM12_CH1, txpower);
3751 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3752 TX_PWR_CFG_0_EXT_OFDM12_CH2, txpower);
3753
3754 /* read the next four txpower values */
3755 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3756 offset + 1, &eeprom);
3757
3758 /* OFDM 24MBS,36MBS */
3759 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3760 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3761 txpower, delta);
3762 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3763 TX_PWR_CFG_1_OFDM24_CH0, txpower);
3764 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3765 TX_PWR_CFG_1_OFDM24_CH1, txpower);
3766 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3767 TX_PWR_CFG_1_EXT_OFDM24_CH2, txpower);
3768
3769 /* OFDM 48MBS */
3770 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3771 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3772 txpower, delta);
3773 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3774 TX_PWR_CFG_1_OFDM48_CH0, txpower);
3775 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3776 TX_PWR_CFG_1_OFDM48_CH1, txpower);
3777 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3778 TX_PWR_CFG_1_EXT_OFDM48_CH2, txpower);
3779
3780 /* OFDM 54MBS */
3781 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3782 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3783 txpower, delta);
3784 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3785 TX_PWR_CFG_7_OFDM54_CH0, txpower);
3786 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3787 TX_PWR_CFG_7_OFDM54_CH1, txpower);
3788 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3789 TX_PWR_CFG_7_OFDM54_CH2, txpower);
3790
3791 /* read the next four txpower values */
3792 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3793 offset + 2, &eeprom);
3794
3795 /* MCS 0,1 */
3796 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3797 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3798 txpower, delta);
3799 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3800 TX_PWR_CFG_1_MCS0_CH0, txpower);
3801 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3802 TX_PWR_CFG_1_MCS0_CH1, txpower);
3803 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3804 TX_PWR_CFG_1_EXT_MCS0_CH2, txpower);
3805
3806 /* MCS 2,3 */
3807 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3808 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3809 txpower, delta);
3810 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3811 TX_PWR_CFG_1_MCS2_CH0, txpower);
3812 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3813 TX_PWR_CFG_1_MCS2_CH1, txpower);
3814 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3815 TX_PWR_CFG_1_EXT_MCS2_CH2, txpower);
3816
3817 /* MCS 4,5 */
3818 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3819 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3820 txpower, delta);
3821 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3822 TX_PWR_CFG_2_MCS4_CH0, txpower);
3823 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3824 TX_PWR_CFG_2_MCS4_CH1, txpower);
3825 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3826 TX_PWR_CFG_2_EXT_MCS4_CH2, txpower);
3827
3828 /* MCS 6 */
3829 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3830 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3831 txpower, delta);
3832 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3833 TX_PWR_CFG_2_MCS6_CH0, txpower);
3834 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3835 TX_PWR_CFG_2_MCS6_CH1, txpower);
3836 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3837 TX_PWR_CFG_2_EXT_MCS6_CH2, txpower);
3838
3839 /* read the next four txpower values */
3840 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3841 offset + 3, &eeprom);
3842
3843 /* MCS 7 */
3844 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3845 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3846 txpower, delta);
3847 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3848 TX_PWR_CFG_7_MCS7_CH0, txpower);
3849 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3850 TX_PWR_CFG_7_MCS7_CH1, txpower);
3851 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3852 TX_PWR_CFG_7_MCS7_CH2, txpower);
3853
3854 /* MCS 8,9 */
3855 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3856 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3857 txpower, delta);
3858 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3859 TX_PWR_CFG_2_MCS8_CH0, txpower);
3860 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3861 TX_PWR_CFG_2_MCS8_CH1, txpower);
3862 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3863 TX_PWR_CFG_2_EXT_MCS8_CH2, txpower);
3864
3865 /* MCS 10,11 */
3866 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3867 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3868 txpower, delta);
3869 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3870 TX_PWR_CFG_2_MCS10_CH0, txpower);
3871 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3872 TX_PWR_CFG_2_MCS10_CH1, txpower);
3873 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3874 TX_PWR_CFG_2_EXT_MCS10_CH2, txpower);
3875
3876 /* MCS 12,13 */
3877 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3878 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3879 txpower, delta);
3880 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3881 TX_PWR_CFG_3_MCS12_CH0, txpower);
3882 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3883 TX_PWR_CFG_3_MCS12_CH1, txpower);
3884 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
3885 TX_PWR_CFG_3_EXT_MCS12_CH2, txpower);
3886
3887 /* read the next four txpower values */
3888 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3889 offset + 4, &eeprom);
3890
3891 /* MCS 14 */
3892 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3893 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3894 txpower, delta);
3895 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3896 TX_PWR_CFG_3_MCS14_CH0, txpower);
3897 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3898 TX_PWR_CFG_3_MCS14_CH1, txpower);
3899 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
3900 TX_PWR_CFG_3_EXT_MCS14_CH2, txpower);
3901
3902 /* MCS 15 */
3903 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3904 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3905 txpower, delta);
3906 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3907 TX_PWR_CFG_8_MCS15_CH0, txpower);
3908 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3909 TX_PWR_CFG_8_MCS15_CH1, txpower);
3910 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3911 TX_PWR_CFG_8_MCS15_CH2, txpower);
3912
3913 /* MCS 16,17 */
3914 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3915 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3916 txpower, delta);
3917 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3918 TX_PWR_CFG_5_MCS16_CH0, txpower);
3919 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3920 TX_PWR_CFG_5_MCS16_CH1, txpower);
3921 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3922 TX_PWR_CFG_5_MCS16_CH2, txpower);
3923
3924 /* MCS 18,19 */
3925 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3926 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3927 txpower, delta);
3928 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3929 TX_PWR_CFG_5_MCS18_CH0, txpower);
3930 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3931 TX_PWR_CFG_5_MCS18_CH1, txpower);
3932 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3933 TX_PWR_CFG_5_MCS18_CH2, txpower);
3934
3935 /* read the next four txpower values */
3936 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3937 offset + 5, &eeprom);
3938
3939 /* MCS 20,21 */
3940 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3941 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3942 txpower, delta);
3943 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3944 TX_PWR_CFG_6_MCS20_CH0, txpower);
3945 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3946 TX_PWR_CFG_6_MCS20_CH1, txpower);
3947 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3948 TX_PWR_CFG_6_MCS20_CH2, txpower);
3949
3950 /* MCS 22 */
3951 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3952 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3953 txpower, delta);
3954 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3955 TX_PWR_CFG_6_MCS22_CH0, txpower);
3956 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3957 TX_PWR_CFG_6_MCS22_CH1, txpower);
3958 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3959 TX_PWR_CFG_6_MCS22_CH2, txpower);
3960
3961 /* MCS 23 */
3962 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3963 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3964 txpower, delta);
3965 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3966 TX_PWR_CFG_8_MCS23_CH0, txpower);
3967 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3968 TX_PWR_CFG_8_MCS23_CH1, txpower);
3969 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3970 TX_PWR_CFG_8_MCS23_CH2, txpower);
3971
3972 /* read the next four txpower values */
3973 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3974 offset + 6, &eeprom);
3975
3976 /* STBC, MCS 0,1 */
3977 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3978 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3979 txpower, delta);
3980 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3981 TX_PWR_CFG_3_STBC0_CH0, txpower);
3982 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3983 TX_PWR_CFG_3_STBC0_CH1, txpower);
3984 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
3985 TX_PWR_CFG_3_EXT_STBC0_CH2, txpower);
3986
3987 /* STBC, MCS 2,3 */
3988 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3989 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3990 txpower, delta);
3991 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3992 TX_PWR_CFG_3_STBC2_CH0, txpower);
3993 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3994 TX_PWR_CFG_3_STBC2_CH1, txpower);
3995 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
3996 TX_PWR_CFG_3_EXT_STBC2_CH2, txpower);
3997
3998 /* STBC, MCS 4,5 */
3999 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
4000 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
4001 txpower, delta);
4002 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE0, txpower);
4003 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE1, txpower);
4004 rt2x00_set_field32(&regs[TX_PWR_CFG_4_EXT_IDX], TX_PWR_CFG_RATE0,
4005 txpower);
4006
4007 /* STBC, MCS 6 */
4008 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
4009 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
4010 txpower, delta);
4011 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE2, txpower);
4012 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE3, txpower);
4013 rt2x00_set_field32(&regs[TX_PWR_CFG_4_EXT_IDX], TX_PWR_CFG_RATE2,
4014 txpower);
4015
4016 /* read the next four txpower values */
4017 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
4018 offset + 7, &eeprom);
4019
4020 /* STBC, MCS 7 */
4021 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
4022 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
4023 txpower, delta);
4024 rt2x00_set_field32(&regs[TX_PWR_CFG_9_IDX],
4025 TX_PWR_CFG_9_STBC7_CH0, txpower);
4026 rt2x00_set_field32(&regs[TX_PWR_CFG_9_IDX],
4027 TX_PWR_CFG_9_STBC7_CH1, txpower);
4028 rt2x00_set_field32(&regs[TX_PWR_CFG_9_IDX],
4029 TX_PWR_CFG_9_STBC7_CH2, txpower);
4030
4031 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0, regs[TX_PWR_CFG_0_IDX]);
4032 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1, regs[TX_PWR_CFG_1_IDX]);
4033 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2, regs[TX_PWR_CFG_2_IDX]);
4034 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3, regs[TX_PWR_CFG_3_IDX]);
4035 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4, regs[TX_PWR_CFG_4_IDX]);
4036 rt2800_register_write(rt2x00dev, TX_PWR_CFG_5, regs[TX_PWR_CFG_5_IDX]);
4037 rt2800_register_write(rt2x00dev, TX_PWR_CFG_6, regs[TX_PWR_CFG_6_IDX]);
4038 rt2800_register_write(rt2x00dev, TX_PWR_CFG_7, regs[TX_PWR_CFG_7_IDX]);
4039 rt2800_register_write(rt2x00dev, TX_PWR_CFG_8, regs[TX_PWR_CFG_8_IDX]);
4040 rt2800_register_write(rt2x00dev, TX_PWR_CFG_9, regs[TX_PWR_CFG_9_IDX]);
4041
4042 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0_EXT,
4043 regs[TX_PWR_CFG_0_EXT_IDX]);
4044 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1_EXT,
4045 regs[TX_PWR_CFG_1_EXT_IDX]);
4046 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2_EXT,
4047 regs[TX_PWR_CFG_2_EXT_IDX]);
4048 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3_EXT,
4049 regs[TX_PWR_CFG_3_EXT_IDX]);
4050 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4_EXT,
4051 regs[TX_PWR_CFG_4_EXT_IDX]);
4052
4053 for (i = 0; i < TX_PWR_CFG_IDX_COUNT; i++)
4054 rt2x00_dbg(rt2x00dev,
4055 "band:%cGHz, BW:%c0MHz, TX_PWR_CFG_%d%s = %08lx\n",
Johannes Berg57fbcce2016-04-12 15:56:15 +02004056 (band == NL80211_BAND_5GHZ) ? '5' : '2',
Gabor Juhos34542ff2013-07-08 16:08:20 +02004057 (test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags)) ?
4058 '4' : '2',
4059 (i > TX_PWR_CFG_9_IDX) ?
4060 (i - TX_PWR_CFG_9_IDX - 1) : i,
4061 (i > TX_PWR_CFG_9_IDX) ? "_EXT" : "",
4062 (unsigned long) regs[i]);
4063}
4064
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02004065/*
4066 * We configure transmit power using MAC TX_PWR_CFG_{0,...,N} registers and
4067 * BBP R1 register. TX_PWR_CFG_X allow to configure per rate TX power values,
4068 * 4 bits for each rate (tune from 0 to 15 dBm). BBP_R1 controls transmit power
4069 * for all rates, but allow to set only 4 discrete values: -12, -6, 0 and 6 dBm.
4070 * Reference per rate transmit power values are located in the EEPROM at
4071 * EEPROM_TXPOWER_BYRATE offset. We adjust them and BBP R1 settings according to
4072 * current conditions (i.e. band, bandwidth, temperature, user settings).
4073 */
Gabor Juhos34542ff2013-07-08 16:08:20 +02004074static void rt2800_config_txpower_rt28xx(struct rt2x00_dev *rt2x00dev,
4075 struct ieee80211_channel *chan,
4076 int power_level)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004077{
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02004078 u8 txpower, r1;
Helmut Schaa5e846002010-07-11 12:23:09 +02004079 u16 eeprom;
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02004080 u32 reg, offset;
4081 int i, is_rate_b, delta, power_ctrl;
Johannes Berg57fbcce2016-04-12 15:56:15 +02004082 enum nl80211_band band = chan->band;
Helmut Schaa2af242e2011-03-28 13:32:01 +02004083
4084 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02004085 * Calculate HT40 compensation. For 40MHz we need to add or subtract
4086 * value read from EEPROM (different for 2GHz and for 5GHz).
Helmut Schaa2af242e2011-03-28 13:32:01 +02004087 */
4088 delta = rt2800_get_txpower_bw_comp(rt2x00dev, band);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004089
Helmut Schaa5e846002010-07-11 12:23:09 +02004090 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02004091 * Calculate temperature compensation. Depends on measurement of current
4092 * TSSI (Transmitter Signal Strength Indication) we know TX power (due
4093 * to temperature or maybe other factors) is smaller or bigger than
4094 * expected. We adjust it, based on TSSI reference and boundaries values
4095 * provided in EEPROM.
Helmut Schaa9e33a352011-03-28 13:33:40 +02004096 */
Stanislaw Gruszka87dd2d72014-11-25 15:17:29 +01004097 switch (rt2x00dev->chip.rt) {
4098 case RT2860:
4099 case RT2872:
4100 case RT2883:
4101 case RT3070:
4102 case RT3071:
4103 case RT3090:
4104 case RT3572:
4105 delta += rt2800_get_gain_calibration_delta(rt2x00dev);
4106 break;
4107 default:
4108 /* TODO: temperature compensation code for other chips. */
4109 break;
4110 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004111
Helmut Schaa5e846002010-07-11 12:23:09 +02004112 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02004113 * Decrease power according to user settings, on devices with unknown
4114 * maximum tx power. For other devices we take user power_level into
4115 * consideration on rt2800_compensate_txpower().
Stanislaw Gruszka1e4cf242012-10-05 13:44:14 +02004116 */
4117 delta += rt2800_get_txpower_reg_delta(rt2x00dev, power_level,
4118 chan->max_power);
4119
4120 /*
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02004121 * BBP_R1 controls TX power for all rates, it allow to set the following
4122 * gains -12, -6, 0, +6 dBm by setting values 2, 1, 0, 3 respectively.
4123 *
4124 * TODO: we do not use +6 dBm option to do not increase power beyond
4125 * regulatory limit, however this could be utilized for devices with
4126 * CAPABILITY_POWER_LIMIT.
Helmut Schaa5e846002010-07-11 12:23:09 +02004127 */
Stanislaw Gruszka87dd2d72014-11-25 15:17:29 +01004128 if (delta <= -12) {
4129 power_ctrl = 2;
4130 delta += 12;
4131 } else if (delta <= -6) {
4132 power_ctrl = 1;
4133 delta += 6;
4134 } else {
4135 power_ctrl = 0;
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02004136 }
Stanislaw Gruszka87dd2d72014-11-25 15:17:29 +01004137 rt2800_bbp_read(rt2x00dev, 1, &r1);
4138 rt2x00_set_field8(&r1, BBP1_TX_POWER_CTRL, power_ctrl);
4139 rt2800_bbp_write(rt2x00dev, 1, r1);
Stanislaw Gruszka8c8d20172013-06-11 18:48:53 +02004140
Helmut Schaa5e846002010-07-11 12:23:09 +02004141 offset = TX_PWR_CFG_0;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004142
Helmut Schaa5e846002010-07-11 12:23:09 +02004143 for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
4144 /* just to be safe */
4145 if (offset > TX_PWR_CFG_4)
4146 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004147
Helmut Schaa5e846002010-07-11 12:23:09 +02004148 rt2800_register_read(rt2x00dev, offset, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004149
Helmut Schaa5e846002010-07-11 12:23:09 +02004150 /* read the next four txpower values */
Gabor Juhos022138c2013-07-08 11:25:54 +02004151 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
4152 i, &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004153
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004154 is_rate_b = i ? 0 : 1;
4155 /*
4156 * TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02004157 * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004158 * TX_PWR_CFG_4: unknown
4159 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004160 txpower = rt2x00_get_field16(eeprom,
4161 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02004162 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004163 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004164 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004165
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004166 /*
4167 * TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02004168 * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004169 * TX_PWR_CFG_4: unknown
4170 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004171 txpower = rt2x00_get_field16(eeprom,
4172 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02004173 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004174 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004175 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004176
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004177 /*
4178 * TX_PWR_CFG_0: 5.5MBS, TX_PWR_CFG_1: 48MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02004179 * TX_PWR_CFG_2: MCS6, TX_PWR_CFG_3: MCS14,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004180 * TX_PWR_CFG_4: unknown
4181 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004182 txpower = rt2x00_get_field16(eeprom,
4183 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02004184 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004185 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004186 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004187
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004188 /*
4189 * TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02004190 * TX_PWR_CFG_2: MCS7, TX_PWR_CFG_3: MCS15,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004191 * TX_PWR_CFG_4: unknown
4192 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004193 txpower = rt2x00_get_field16(eeprom,
4194 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02004195 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004196 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004197 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004198
4199 /* read the next four txpower values */
Gabor Juhos022138c2013-07-08 11:25:54 +02004200 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
4201 i + 1, &eeprom);
Helmut Schaa5e846002010-07-11 12:23:09 +02004202
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004203 is_rate_b = 0;
4204 /*
4205 * TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
Helmut Schaa5e846002010-07-11 12:23:09 +02004206 * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004207 * TX_PWR_CFG_4: unknown
4208 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004209 txpower = rt2x00_get_field16(eeprom,
4210 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02004211 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004212 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004213 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004214
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004215 /*
4216 * TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
Helmut Schaa5e846002010-07-11 12:23:09 +02004217 * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004218 * TX_PWR_CFG_4: unknown
4219 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004220 txpower = rt2x00_get_field16(eeprom,
4221 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02004222 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004223 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004224 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004225
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004226 /*
4227 * TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
Helmut Schaa5e846002010-07-11 12:23:09 +02004228 * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004229 * TX_PWR_CFG_4: unknown
4230 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004231 txpower = rt2x00_get_field16(eeprom,
4232 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02004233 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004234 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004235 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004236
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004237 /*
4238 * TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
Helmut Schaa5e846002010-07-11 12:23:09 +02004239 * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004240 * TX_PWR_CFG_4: unknown
4241 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004242 txpower = rt2x00_get_field16(eeprom,
4243 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02004244 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004245 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004246 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004247
4248 rt2800_register_write(rt2x00dev, offset, reg);
4249
4250 /* next TX_PWR_CFG register */
4251 offset += 4;
4252 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004253}
4254
Gabor Juhos34542ff2013-07-08 16:08:20 +02004255static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
4256 struct ieee80211_channel *chan,
4257 int power_level)
4258{
4259 if (rt2x00_rt(rt2x00dev, RT3593))
4260 rt2800_config_txpower_rt3593(rt2x00dev, chan, power_level);
4261 else
4262 rt2800_config_txpower_rt28xx(rt2x00dev, chan, power_level);
4263}
4264
Helmut Schaa9e33a352011-03-28 13:33:40 +02004265void rt2800_gain_calibration(struct rt2x00_dev *rt2x00dev)
4266{
Karl Beldan675a0b02013-03-25 16:26:57 +01004267 rt2800_config_txpower(rt2x00dev, rt2x00dev->hw->conf.chandef.chan,
Helmut Schaa9e33a352011-03-28 13:33:40 +02004268 rt2x00dev->tx_power);
4269}
4270EXPORT_SYMBOL_GPL(rt2800_gain_calibration);
4271
John Li2e9c43d2012-02-16 21:40:57 +08004272void rt2800_vco_calibration(struct rt2x00_dev *rt2x00dev)
4273{
4274 u32 tx_pin;
4275 u8 rfcsr;
4276
4277 /*
4278 * A voltage-controlled oscillator(VCO) is an electronic oscillator
4279 * designed to be controlled in oscillation frequency by a voltage
4280 * input. Maybe the temperature will affect the frequency of
4281 * oscillation to be shifted. The VCO calibration will be called
4282 * periodically to adjust the frequency to be precision.
4283 */
4284
4285 rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
4286 tx_pin &= TX_PIN_CFG_PA_PE_DISABLE;
4287 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
4288
4289 switch (rt2x00dev->chip.rf) {
4290 case RF2020:
4291 case RF3020:
4292 case RF3021:
4293 case RF3022:
4294 case RF3320:
4295 case RF3052:
4296 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
4297 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
4298 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
4299 break;
Gabor Juhos1095df02013-07-08 16:08:31 +02004300 case RF3053:
Stanislaw Gruszka3b9b74b2013-09-25 15:34:55 +02004301 case RF3070:
Woody Hunga89534e2012-06-13 15:01:16 +08004302 case RF3290:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02004303 case RF5360:
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05004304 case RF5362:
John Li2e9c43d2012-02-16 21:40:57 +08004305 case RF5370:
4306 case RF5372:
4307 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08004308 case RF5392:
John Li2e9c43d2012-02-16 21:40:57 +08004309 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
Gabor Juhosd6d82022012-12-02 18:34:47 +01004310 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
John Li2e9c43d2012-02-16 21:40:57 +08004311 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
4312 break;
4313 default:
4314 return;
4315 }
4316
4317 mdelay(1);
4318
4319 rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
4320 if (rt2x00dev->rf_channel <= 14) {
4321 switch (rt2x00dev->default_ant.tx_chain_num) {
4322 case 3:
4323 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G2_EN, 1);
4324 /* fall through */
4325 case 2:
4326 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
4327 /* fall through */
4328 case 1:
4329 default:
4330 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
4331 break;
4332 }
4333 } else {
4334 switch (rt2x00dev->default_ant.tx_chain_num) {
4335 case 3:
4336 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A2_EN, 1);
4337 /* fall through */
4338 case 2:
4339 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
4340 /* fall through */
4341 case 1:
4342 default:
4343 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, 1);
4344 break;
4345 }
4346 }
4347 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
4348
4349}
4350EXPORT_SYMBOL_GPL(rt2800_vco_calibration);
4351
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004352static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
4353 struct rt2x00lib_conf *libconf)
4354{
4355 u32 reg;
4356
4357 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
4358 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
4359 libconf->conf->short_frame_max_tx_count);
4360 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
4361 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004362 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
4363}
4364
4365static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
4366 struct rt2x00lib_conf *libconf)
4367{
4368 enum dev_state state =
4369 (libconf->conf->flags & IEEE80211_CONF_PS) ?
4370 STATE_SLEEP : STATE_AWAKE;
4371 u32 reg;
4372
4373 if (state == STATE_SLEEP) {
4374 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
4375
4376 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
4377 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
4378 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
4379 libconf->conf->listen_interval - 1);
4380 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
4381 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
4382
4383 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
4384 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004385 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
4386 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
4387 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
4388 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
4389 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02004390
4391 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004392 }
4393}
4394
4395void rt2800_config(struct rt2x00_dev *rt2x00dev,
4396 struct rt2x00lib_conf *libconf,
4397 const unsigned int flags)
4398{
4399 /* Always recalculate LNA gain before changing configuration */
4400 rt2800_config_lna_gain(rt2x00dev, libconf);
4401
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004402 if (flags & IEEE80211_CONF_CHANGE_CHANNEL) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004403 rt2800_config_channel(rt2x00dev, libconf->conf,
4404 &libconf->rf, &libconf->channel);
Karl Beldan675a0b02013-03-25 16:26:57 +01004405 rt2800_config_txpower(rt2x00dev, libconf->conf->chandef.chan,
Helmut Schaa9e33a352011-03-28 13:33:40 +02004406 libconf->conf->power_level);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004407 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004408 if (flags & IEEE80211_CONF_CHANGE_POWER)
Karl Beldan675a0b02013-03-25 16:26:57 +01004409 rt2800_config_txpower(rt2x00dev, libconf->conf->chandef.chan,
Helmut Schaa9e33a352011-03-28 13:33:40 +02004410 libconf->conf->power_level);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004411 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
4412 rt2800_config_retry_limit(rt2x00dev, libconf);
4413 if (flags & IEEE80211_CONF_CHANGE_PS)
4414 rt2800_config_ps(rt2x00dev, libconf);
4415}
4416EXPORT_SYMBOL_GPL(rt2800_config);
4417
4418/*
4419 * Link tuning
4420 */
4421void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
4422{
4423 u32 reg;
4424
4425 /*
4426 * Update FCS error count from register.
4427 */
4428 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
4429 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
4430}
4431EXPORT_SYMBOL_GPL(rt2800_link_stats);
4432
4433static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
4434{
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02004435 u8 vgc;
4436
Johannes Berg57fbcce2016-04-12 15:56:15 +02004437 if (rt2x00dev->curr_band == NL80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004438 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004439 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004440 rt2x00_rt(rt2x00dev, RT3090) ||
Woody Hunga89534e2012-06-13 15:01:16 +08004441 rt2x00_rt(rt2x00dev, RT3290) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01004442 rt2x00_rt(rt2x00dev, RT3390) ||
Gertjan van Wingerded961e442012-09-16 22:29:50 +02004443 rt2x00_rt(rt2x00dev, RT3572) ||
Gabor Juhos0ffd2a92013-10-03 20:00:42 +02004444 rt2x00_rt(rt2x00dev, RT3593) ||
John Li2ed71882012-02-17 17:33:06 +08004445 rt2x00_rt(rt2x00dev, RT5390) ||
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004446 rt2x00_rt(rt2x00dev, RT5392) ||
4447 rt2x00_rt(rt2x00dev, RT5592))
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02004448 vgc = 0x1c + (2 * rt2x00dev->lna_gain);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004449 else
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02004450 vgc = 0x2e + rt2x00dev->lna_gain;
4451 } else { /* 5GHZ band */
Gabor Juhos733aec62013-10-04 22:07:09 +02004452 if (rt2x00_rt(rt2x00dev, RT3593))
Gabor Juhos0ffd2a92013-10-03 20:00:42 +02004453 vgc = 0x20 + (rt2x00dev->lna_gain * 5) / 3;
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004454 else if (rt2x00_rt(rt2x00dev, RT5592))
4455 vgc = 0x24 + (2 * rt2x00dev->lna_gain);
Gertjan van Wingerded961e442012-09-16 22:29:50 +02004456 else {
4457 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
4458 vgc = 0x32 + (rt2x00dev->lna_gain * 5) / 3;
4459 else
4460 vgc = 0x3a + (rt2x00dev->lna_gain * 5) / 3;
4461 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004462 }
4463
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02004464 return vgc;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004465}
4466
4467static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
4468 struct link_qual *qual, u8 vgc_level)
4469{
4470 if (qual->vgc_level != vgc_level) {
Gabor Juhos271f1a42013-10-03 20:00:43 +02004471 if (rt2x00_rt(rt2x00dev, RT3572) ||
4472 rt2x00_rt(rt2x00dev, RT3593)) {
4473 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66,
4474 vgc_level);
4475 } else if (rt2x00_rt(rt2x00dev, RT5592)) {
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004476 rt2800_bbp_write(rt2x00dev, 83, qual->rssi > -65 ? 0x4a : 0x7a);
4477 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66, vgc_level);
Gabor Juhos271f1a42013-10-03 20:00:43 +02004478 } else {
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004479 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
Gabor Juhos271f1a42013-10-03 20:00:43 +02004480 }
4481
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004482 qual->vgc_level = vgc_level;
4483 qual->vgc_level_reg = vgc_level;
4484 }
4485}
4486
4487void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
4488{
4489 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
4490}
4491EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
4492
4493void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
4494 const u32 count)
4495{
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004496 u8 vgc;
4497
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02004498 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004499 return;
Gabor Juhose25aa822013-10-03 20:00:41 +02004500
4501 /* When RSSI is better than a certain threshold, increase VGC
4502 * with a chip specific value in order to improve the balance
4503 * between sensibility and noise isolation.
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004504 */
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004505
4506 vgc = rt2800_get_default_vgc(rt2x00dev);
4507
Gabor Juhose25aa822013-10-03 20:00:41 +02004508 switch (rt2x00dev->chip.rt) {
4509 case RT3572:
4510 case RT3593:
4511 if (qual->rssi > -65) {
Johannes Berg57fbcce2016-04-12 15:56:15 +02004512 if (rt2x00dev->curr_band == NL80211_BAND_2GHZ)
Gabor Juhose25aa822013-10-03 20:00:41 +02004513 vgc += 0x20;
4514 else
4515 vgc += 0x10;
4516 }
4517 break;
4518
4519 case RT5592:
Gabor Juhos0beb1bb2013-10-03 20:00:40 +02004520 if (qual->rssi > -65)
4521 vgc += 0x20;
Gabor Juhose25aa822013-10-03 20:00:41 +02004522 break;
4523
4524 default:
Gabor Juhos0beb1bb2013-10-03 20:00:40 +02004525 if (qual->rssi > -80)
4526 vgc += 0x10;
Gabor Juhose25aa822013-10-03 20:00:41 +02004527 break;
Gabor Juhos0beb1bb2013-10-03 20:00:40 +02004528 }
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004529
4530 rt2800_set_vgc(rt2x00dev, qual, vgc);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004531}
4532EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004533
4534/*
4535 * Initialization functions.
4536 */
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02004537static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004538{
4539 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004540 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004541 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02004542 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004543
Jakub Kicinskif7b395e2012-04-03 03:40:47 +02004544 rt2800_disable_wpdma(rt2x00dev);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004545
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02004546 ret = rt2800_drv_init_registers(rt2x00dev);
4547 if (ret)
4548 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004549
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004550 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
4551 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
4552
4553 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
4554
4555 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Helmut Schaa8544df32010-07-11 12:29:49 +02004556 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004557 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
4558 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
4559 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
4560 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
4561 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
4562 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
4563
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004564 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
4565
4566 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
4567 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
4568 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
4569 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
4570
Woody Hunga89534e2012-06-13 15:01:16 +08004571 if (rt2x00_rt(rt2x00dev, RT3290)) {
4572 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
4573 if (rt2x00_get_field32(reg, WLAN_EN) == 1) {
4574 rt2x00_set_field32(&reg, PCIE_APP0_CLK_REQ, 1);
4575 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
4576 }
4577
4578 rt2800_register_read(rt2x00dev, CMB_CTRL, &reg);
4579 if (!(rt2x00_get_field32(reg, LDO0_EN) == 1)) {
4580 rt2x00_set_field32(&reg, LDO0_EN, 1);
4581 rt2x00_set_field32(&reg, LDO_BGSEL, 3);
4582 rt2800_register_write(rt2x00dev, CMB_CTRL, reg);
4583 }
4584
4585 rt2800_register_read(rt2x00dev, OSC_CTRL, &reg);
4586 rt2x00_set_field32(&reg, OSC_ROSC_EN, 1);
4587 rt2x00_set_field32(&reg, OSC_CAL_REQ, 1);
4588 rt2x00_set_field32(&reg, OSC_REF_CYCLE, 0x27);
4589 rt2800_register_write(rt2x00dev, OSC_CTRL, reg);
4590
4591 rt2800_register_read(rt2x00dev, COEX_CFG0, &reg);
4592 rt2x00_set_field32(&reg, COEX_CFG_ANT, 0x5e);
4593 rt2800_register_write(rt2x00dev, COEX_CFG0, reg);
4594
4595 rt2800_register_read(rt2x00dev, COEX_CFG2, &reg);
4596 rt2x00_set_field32(&reg, BT_COEX_CFG1, 0x00);
4597 rt2x00_set_field32(&reg, BT_COEX_CFG0, 0x17);
4598 rt2x00_set_field32(&reg, WL_COEX_CFG1, 0x93);
4599 rt2x00_set_field32(&reg, WL_COEX_CFG0, 0x7f);
4600 rt2800_register_write(rt2x00dev, COEX_CFG2, reg);
4601
4602 rt2800_register_read(rt2x00dev, PLL_CTRL, &reg);
4603 rt2x00_set_field32(&reg, PLL_CONTROL, 1);
4604 rt2800_register_write(rt2x00dev, PLL_CTRL, reg);
4605 }
4606
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004607 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004608 rt2x00_rt(rt2x00dev, RT3090) ||
Woody Hunga89534e2012-06-13 15:01:16 +08004609 rt2x00_rt(rt2x00dev, RT3290) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004610 rt2x00_rt(rt2x00dev, RT3390)) {
Woody Hunga89534e2012-06-13 15:01:16 +08004611
4612 if (rt2x00_rt(rt2x00dev, RT3290))
4613 rt2800_register_write(rt2x00dev, TX_SW_CFG0,
4614 0x00000404);
4615 else
4616 rt2800_register_write(rt2x00dev, TX_SW_CFG0,
4617 0x00000400);
4618
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004619 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004620 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004621 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
4622 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02004623 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1,
4624 &eeprom);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004625 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004626 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4627 0x0000002c);
4628 else
4629 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4630 0x0000000f);
4631 } else {
4632 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
4633 }
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004634 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004635 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004636
4637 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
4638 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
4639 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
4640 } else {
4641 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4642 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
4643 }
Helmut Schaac295a812010-06-03 10:52:13 +02004644 } else if (rt2800_is_305x_soc(rt2x00dev)) {
4645 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
4646 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Helmut Schaa961636b2011-04-18 15:28:27 +02004647 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000030);
Daniel Golle03839952012-09-09 14:24:39 +03004648 } else if (rt2x00_rt(rt2x00dev, RT3352)) {
4649 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000402);
4650 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4651 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004652 } else if (rt2x00_rt(rt2x00dev, RT3572)) {
4653 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
4654 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
Gabor Juhos1706d152013-07-08 16:08:16 +02004655 } else if (rt2x00_rt(rt2x00dev, RT3593)) {
4656 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000402);
4657 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
4658 if (rt2x00_rt_rev_lt(rt2x00dev, RT3593, REV_RT3593E)) {
4659 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1,
4660 &eeprom);
4661 if (rt2x00_get_field16(eeprom,
4662 EEPROM_NIC_CONF1_DAC_TEST))
4663 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4664 0x0000001f);
4665 else
4666 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4667 0x0000000f);
4668 } else {
4669 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4670 0x00000000);
4671 }
John Li2ed71882012-02-17 17:33:06 +08004672 } else if (rt2x00_rt(rt2x00dev, RT5390) ||
Stanislaw Gruszka76413282013-03-16 19:19:33 +01004673 rt2x00_rt(rt2x00dev, RT5392) ||
4674 rt2x00_rt(rt2x00dev, RT5592)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01004675 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000404);
4676 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4677 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004678 } else {
4679 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
4680 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4681 }
4682
4683 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
4684 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
4685 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
4686 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
4687 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
4688 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
4689 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
4690 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
4691 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
4692 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
4693
4694 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
4695 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004696 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004697 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
4698 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
4699
4700 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
4701 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02004702 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01004703 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02004704 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004705 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
4706 else
4707 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
4708 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
4709 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
4710 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
4711
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004712 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
4713 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
4714 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
4715 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
4716 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
4717 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
4718 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
4719 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
4720 rt2800_register_write(rt2x00dev, LED_CFG, reg);
4721
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004722 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
4723
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004724 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
4725 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
4726 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
4727 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
4728 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
4729 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
4730 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
4731 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
4732
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004733 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
4734 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004735 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Stanislaw Gruszkabe82de92016-11-18 10:43:57 +01004736 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004737 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Stanislaw Gruszkabe82de92016-11-18 10:43:57 +01004738 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004739 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
4740 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
4741 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
4742
4743 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004744 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004745 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004746 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004747 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4748 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4749 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004750 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004751 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004752 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
4753 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004754 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
4755
4756 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004757 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004758 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004759 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004760 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4761 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4762 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004763 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004764 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004765 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
4766 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004767 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
4768
4769 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
4770 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
4771 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004772 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004773 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4774 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4775 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4776 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
4777 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4778 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004779 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004780 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
4781
4782 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
4783 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Helmut Schaad13a97f2010-10-02 11:29:08 +02004784 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004785 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004786 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4787 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4788 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4789 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
4790 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4791 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004792 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004793 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
4794
4795 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
4796 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
4797 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004798 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004799 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4800 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4801 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4802 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
4803 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4804 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004805 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004806 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
4807
4808 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
4809 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
4810 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004811 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004812 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4813 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4814 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4815 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
4816 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4817 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004818 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004819 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
4820
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01004821 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004822 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
4823
4824 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
4825 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
4826 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
4827 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
4828 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
4829 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
4830 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
4831 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
4832 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
4833 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
4834 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
4835 }
4836
Helmut Schaa961621a2010-11-04 20:36:59 +01004837 /*
4838 * The legacy driver also sets TXOP_CTRL_CFG_RESERVED_TRUN_EN to 1
4839 * although it is reserved.
4840 */
4841 rt2800_register_read(rt2x00dev, TXOP_CTRL_CFG, &reg);
4842 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TIMEOUT_TRUN_EN, 1);
4843 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_AC_TRUN_EN, 1);
4844 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TXRATEGRP_TRUN_EN, 1);
4845 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_USER_MODE_TRUN_EN, 1);
4846 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_MIMO_PS_TRUN_EN, 1);
4847 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_RESERVED_TRUN_EN, 1);
4848 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_LSIG_TXOP_EN, 0);
4849 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_EN, 0);
4850 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_DLY, 88);
4851 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CWMIN, 0);
4852 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, reg);
4853
Stanislaw Gruszka76413282013-03-16 19:19:33 +01004854 reg = rt2x00_rt(rt2x00dev, RT5592) ? 0x00000082 : 0x00000002;
4855 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004856
4857 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
4858 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
4859 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
4860 IEEE80211_MAX_RTS_THRESHOLD);
4861 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
4862 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
4863
4864 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004865
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02004866 /*
4867 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
4868 * time should be set to 16. However, the original Ralink driver uses
4869 * 16 for both and indeed using a value of 10 for CCK SIFS results in
4870 * connection problems with 11g + CTS protection. Hence, use the same
4871 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
4872 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004873 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02004874 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
4875 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004876 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
4877 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
4878 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
4879 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
4880
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004881 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
4882
4883 /*
4884 * ASIC will keep garbage value after boot, clear encryption keys.
4885 */
4886 for (i = 0; i < 4; i++)
4887 rt2800_register_write(rt2x00dev,
4888 SHARED_KEY_MODE_ENTRY(i), 0);
4889
4890 for (i = 0; i < 256; i++) {
Helmut Schaad7d259d2011-09-08 14:39:04 +02004891 rt2800_config_wcid(rt2x00dev, NULL, i);
4892 rt2800_delete_wcid_attr(rt2x00dev, i);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004893 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
4894 }
4895
4896 /*
4897 * Clear all beacons
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004898 */
Gabor Juhos77f7c0f2013-08-17 00:15:50 +02004899 for (i = 0; i < 8; i++)
4900 rt2800_clear_beacon_register(rt2x00dev, i);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004901
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01004902 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02004903 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
4904 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
4905 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
RA-Jay Hungc6fcc0e2011-01-30 13:21:22 +01004906 } else if (rt2x00_is_pcie(rt2x00dev)) {
4907 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
4908 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 125);
4909 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004910 }
4911
4912 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
4913 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
4914 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
4915 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
4916 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
4917 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
4918 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
4919 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
4920 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
4921 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
4922
4923 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
4924 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
4925 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
4926 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
4927 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
4928 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
4929 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
4930 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
4931 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
4932 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
4933
4934 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
4935 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
4936 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
4937 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
4938 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
4939 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
4940 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
4941 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
4942 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
4943 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
4944
4945 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
4946 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
4947 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
4948 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
4949 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
4950 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
4951
4952 /*
Helmut Schaa47ee3eb2010-09-08 20:56:04 +02004953 * Do not force the BA window size, we use the TXWI to set it
4954 */
4955 rt2800_register_read(rt2x00dev, AMPDU_BA_WINSIZE, &reg);
4956 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE_ENABLE, 0);
4957 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE, 0);
4958 rt2800_register_write(rt2x00dev, AMPDU_BA_WINSIZE, reg);
4959
4960 /*
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004961 * We must clear the error counters.
4962 * These registers are cleared on read,
4963 * so we may pass a useless variable to store the value.
4964 */
4965 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
4966 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
4967 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
4968 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
4969 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
4970 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
4971
Helmut Schaa9f926fb2010-07-11 12:28:23 +02004972 /*
4973 * Setup leadtime for pre tbtt interrupt to 6ms
4974 */
4975 rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
4976 rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
4977 rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
4978
Helmut Schaa977206d2010-12-13 12:31:58 +01004979 /*
4980 * Set up channel statistics timer
4981 */
4982 rt2800_register_read(rt2x00dev, CH_TIME_CFG, &reg);
4983 rt2x00_set_field32(&reg, CH_TIME_CFG_EIFS_BUSY, 1);
4984 rt2x00_set_field32(&reg, CH_TIME_CFG_NAV_BUSY, 1);
4985 rt2x00_set_field32(&reg, CH_TIME_CFG_RX_BUSY, 1);
4986 rt2x00_set_field32(&reg, CH_TIME_CFG_TX_BUSY, 1);
4987 rt2x00_set_field32(&reg, CH_TIME_CFG_TMR_EN, 1);
4988 rt2800_register_write(rt2x00dev, CH_TIME_CFG, reg);
4989
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004990 return 0;
4991}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004992
4993static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
4994{
4995 unsigned int i;
4996 u32 reg;
4997
4998 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
4999 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
5000 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
5001 return 0;
5002
5003 udelay(REGISTER_BUSY_DELAY);
5004 }
5005
Joe Perchesec9c4982013-04-19 08:33:40 -07005006 rt2x00_err(rt2x00dev, "BBP/RF register access failed, aborting\n");
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005007 return -EACCES;
5008}
5009
5010static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
5011{
5012 unsigned int i;
5013 u8 value;
5014
5015 /*
5016 * BBP was enabled after firmware was loaded,
5017 * but we need to reactivate it now.
5018 */
5019 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
5020 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
5021 msleep(1);
5022
5023 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
5024 rt2800_bbp_read(rt2x00dev, 0, &value);
5025 if ((value != 0xff) && (value != 0x00))
5026 return 0;
5027 udelay(REGISTER_BUSY_DELAY);
5028 }
5029
Joe Perchesec9c4982013-04-19 08:33:40 -07005030 rt2x00_err(rt2x00dev, "BBP register access failed, aborting\n");
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005031 return -EACCES;
5032}
5033
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005034static void rt2800_bbp4_mac_if_ctrl(struct rt2x00_dev *rt2x00dev)
5035{
5036 u8 value;
5037
5038 rt2800_bbp_read(rt2x00dev, 4, &value);
5039 rt2x00_set_field8(&value, BBP4_MAC_IF_CTRL, 1);
5040 rt2800_bbp_write(rt2x00dev, 4, value);
5041}
5042
Stanislaw Gruszkac2675482013-03-16 19:19:41 +01005043static void rt2800_init_freq_calibration(struct rt2x00_dev *rt2x00dev)
5044{
5045 rt2800_bbp_write(rt2x00dev, 142, 1);
5046 rt2800_bbp_write(rt2x00dev, 143, 57);
5047}
5048
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005049static void rt2800_init_bbp_5592_glrt(struct rt2x00_dev *rt2x00dev)
5050{
5051 const u8 glrt_table[] = {
5052 0xE0, 0x1F, 0X38, 0x32, 0x08, 0x28, 0x19, 0x0A, 0xFF, 0x00, /* 128 ~ 137 */
5053 0x16, 0x10, 0x10, 0x0B, 0x36, 0x2C, 0x26, 0x24, 0x42, 0x36, /* 138 ~ 147 */
5054 0x30, 0x2D, 0x4C, 0x46, 0x3D, 0x40, 0x3E, 0x42, 0x3D, 0x40, /* 148 ~ 157 */
5055 0X3C, 0x34, 0x2C, 0x2F, 0x3C, 0x35, 0x2E, 0x2A, 0x49, 0x41, /* 158 ~ 167 */
5056 0x36, 0x31, 0x30, 0x30, 0x0E, 0x0D, 0x28, 0x21, 0x1C, 0x16, /* 168 ~ 177 */
5057 0x50, 0x4A, 0x43, 0x40, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, /* 178 ~ 187 */
5058 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 188 ~ 197 */
5059 0x00, 0x00, 0x7D, 0x14, 0x32, 0x2C, 0x36, 0x4C, 0x43, 0x2C, /* 198 ~ 207 */
5060 0x2E, 0x36, 0x30, 0x6E, /* 208 ~ 211 */
5061 };
5062 int i;
5063
5064 for (i = 0; i < ARRAY_SIZE(glrt_table); i++) {
5065 rt2800_bbp_write(rt2x00dev, 195, 128 + i);
5066 rt2800_bbp_write(rt2x00dev, 196, glrt_table[i]);
5067 }
5068};
5069
Gabor Juhos624708b2013-04-19 10:13:52 +02005070static void rt2800_init_bbp_early(struct rt2x00_dev *rt2x00dev)
Stanislaw Gruszkaa4969d02013-03-16 19:19:35 +01005071{
5072 rt2800_bbp_write(rt2x00dev, 65, 0x2C);
5073 rt2800_bbp_write(rt2x00dev, 66, 0x38);
5074 rt2800_bbp_write(rt2x00dev, 68, 0x0B);
5075 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5076 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
5077 rt2800_bbp_write(rt2x00dev, 73, 0x10);
5078 rt2800_bbp_write(rt2x00dev, 81, 0x37);
5079 rt2800_bbp_write(rt2x00dev, 82, 0x62);
5080 rt2800_bbp_write(rt2x00dev, 83, 0x6A);
5081 rt2800_bbp_write(rt2x00dev, 84, 0x99);
5082 rt2800_bbp_write(rt2x00dev, 86, 0x00);
5083 rt2800_bbp_write(rt2x00dev, 91, 0x04);
5084 rt2800_bbp_write(rt2x00dev, 92, 0x00);
5085 rt2800_bbp_write(rt2x00dev, 103, 0x00);
5086 rt2800_bbp_write(rt2x00dev, 105, 0x05);
5087 rt2800_bbp_write(rt2x00dev, 106, 0x35);
5088}
5089
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005090static void rt2800_disable_unused_dac_adc(struct rt2x00_dev *rt2x00dev)
5091{
5092 u16 eeprom;
5093 u8 value;
5094
5095 rt2800_bbp_read(rt2x00dev, 138, &value);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005096 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005097 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
5098 value |= 0x20;
5099 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
5100 value &= ~0x02;
5101 rt2800_bbp_write(rt2x00dev, 138, value);
5102}
5103
Stanislaw Gruszkadae62952013-05-18 14:03:26 +02005104static void rt2800_init_bbp_305x_soc(struct rt2x00_dev *rt2x00dev)
5105{
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02005106 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005107
5108 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5109 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005110
5111 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5112 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005113
5114 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005115
5116 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
5117 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005118
5119 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005120
5121 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005122
5123 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005124
5125 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005126
5127 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005128
5129 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005130
5131 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005132
5133 rt2800_bbp_write(rt2x00dev, 105, 0x01);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005134
5135 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszkadae62952013-05-18 14:03:26 +02005136}
5137
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005138static void rt2800_init_bbp_28xx(struct rt2x00_dev *rt2x00dev)
5139{
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005140 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5141 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005142
5143 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
5144 rt2800_bbp_write(rt2x00dev, 69, 0x16);
5145 rt2800_bbp_write(rt2x00dev, 73, 0x12);
5146 } else {
5147 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5148 rt2800_bbp_write(rt2x00dev, 73, 0x10);
5149 }
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005150
5151 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005152
5153 rt2800_bbp_write(rt2x00dev, 81, 0x37);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005154
5155 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005156
5157 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005158
5159 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
5160 rt2800_bbp_write(rt2x00dev, 84, 0x19);
5161 else
5162 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005163
5164 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005165
5166 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005167
5168 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005169
5170 rt2800_bbp_write(rt2x00dev, 103, 0x00);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005171
5172 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005173
5174 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005175}
5176
5177static void rt2800_init_bbp_30xx(struct rt2x00_dev *rt2x00dev)
5178{
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005179 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5180 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005181
5182 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5183 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005184
5185 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005186
5187 rt2800_bbp_write(rt2x00dev, 79, 0x13);
5188 rt2800_bbp_write(rt2x00dev, 80, 0x05);
5189 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005190
5191 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005192
5193 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005194
5195 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005196
5197 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005198
5199 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005200
5201 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005202
5203 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
5204 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
5205 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E))
5206 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
5207 else
5208 rt2800_bbp_write(rt2x00dev, 103, 0x00);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005209
5210 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005211
5212 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005213
5214 if (rt2x00_rt(rt2x00dev, RT3071) ||
5215 rt2x00_rt(rt2x00dev, RT3090))
5216 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005217}
5218
5219static void rt2800_init_bbp_3290(struct rt2x00_dev *rt2x00dev)
5220{
Stanislaw Gruszka6addb242013-05-18 14:03:54 +02005221 u8 value;
5222
Stanislaw Gruszkac3223572013-05-18 14:03:28 +02005223 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02005224
5225 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005226
5227 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5228 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka59dcabb2013-05-18 14:03:32 +02005229
5230 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005231
5232 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5233 rt2800_bbp_write(rt2x00dev, 73, 0x13);
5234 rt2800_bbp_write(rt2x00dev, 75, 0x46);
5235 rt2800_bbp_write(rt2x00dev, 76, 0x28);
5236
5237 rt2800_bbp_write(rt2x00dev, 77, 0x58);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005238
5239 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005240
5241 rt2800_bbp_write(rt2x00dev, 74, 0x0b);
5242 rt2800_bbp_write(rt2x00dev, 79, 0x18);
5243 rt2800_bbp_write(rt2x00dev, 80, 0x09);
5244 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005245
5246 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005247
5248 rt2800_bbp_write(rt2x00dev, 83, 0x7a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005249
5250 rt2800_bbp_write(rt2x00dev, 84, 0x9a);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005251
5252 rt2800_bbp_write(rt2x00dev, 86, 0x38);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005253
5254 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005255
5256 rt2800_bbp_write(rt2x00dev, 92, 0x02);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005257
5258 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka1ad44082013-05-18 14:03:45 +02005259
5260 rt2800_bbp_write(rt2x00dev, 104, 0x92);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005261
5262 rt2800_bbp_write(rt2x00dev, 105, 0x1c);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005263
5264 rt2800_bbp_write(rt2x00dev, 106, 0x03);
Stanislaw Gruszkaf2b67772013-05-18 14:03:49 +02005265
5266 rt2800_bbp_write(rt2x00dev, 128, 0x12);
Stanislaw Gruszka6addb242013-05-18 14:03:54 +02005267
5268 rt2800_bbp_write(rt2x00dev, 67, 0x24);
5269 rt2800_bbp_write(rt2x00dev, 143, 0x04);
5270 rt2800_bbp_write(rt2x00dev, 142, 0x99);
5271 rt2800_bbp_write(rt2x00dev, 150, 0x30);
5272 rt2800_bbp_write(rt2x00dev, 151, 0x2e);
5273 rt2800_bbp_write(rt2x00dev, 152, 0x20);
5274 rt2800_bbp_write(rt2x00dev, 153, 0x34);
5275 rt2800_bbp_write(rt2x00dev, 154, 0x40);
5276 rt2800_bbp_write(rt2x00dev, 155, 0x3b);
5277 rt2800_bbp_write(rt2x00dev, 253, 0x04);
5278
5279 rt2800_bbp_read(rt2x00dev, 47, &value);
5280 rt2x00_set_field8(&value, BBP47_TSSI_ADC6, 1);
5281 rt2800_bbp_write(rt2x00dev, 47, value);
5282
5283 /* Use 5-bit ADC for Acquisition and 8-bit ADC for data */
5284 rt2800_bbp_read(rt2x00dev, 3, &value);
5285 rt2x00_set_field8(&value, BBP3_ADC_MODE_SWITCH, 1);
5286 rt2x00_set_field8(&value, BBP3_ADC_INIT_MODE, 1);
5287 rt2800_bbp_write(rt2x00dev, 3, value);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005288}
5289
5290static void rt2800_init_bbp_3352(struct rt2x00_dev *rt2x00dev)
5291{
Stanislaw Gruszka29f3a582013-05-18 14:03:27 +02005292 rt2800_bbp_write(rt2x00dev, 3, 0x00);
5293 rt2800_bbp_write(rt2x00dev, 4, 0x50);
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02005294
5295 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszka3420f792013-05-18 14:03:30 +02005296
5297 rt2800_bbp_write(rt2x00dev, 47, 0x48);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005298
5299 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5300 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka59dcabb2013-05-18 14:03:32 +02005301
5302 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005303
5304 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5305 rt2800_bbp_write(rt2x00dev, 73, 0x13);
5306 rt2800_bbp_write(rt2x00dev, 75, 0x46);
5307 rt2800_bbp_write(rt2x00dev, 76, 0x28);
5308
5309 rt2800_bbp_write(rt2x00dev, 77, 0x59);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005310
5311 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005312
5313 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
5314 rt2800_bbp_write(rt2x00dev, 80, 0x08);
5315 rt2800_bbp_write(rt2x00dev, 81, 0x37);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005316
5317 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005318
5319 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005320
5321 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005322
5323 rt2800_bbp_write(rt2x00dev, 86, 0x38);
Stanislaw Gruszka9400fa82013-05-18 14:03:40 +02005324
5325 rt2800_bbp_write(rt2x00dev, 88, 0x90);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005326
5327 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005328
5329 rt2800_bbp_write(rt2x00dev, 92, 0x02);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005330
5331 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka1ad44082013-05-18 14:03:45 +02005332
5333 rt2800_bbp_write(rt2x00dev, 104, 0x92);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005334
5335 rt2800_bbp_write(rt2x00dev, 105, 0x34);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005336
5337 rt2800_bbp_write(rt2x00dev, 106, 0x05);
Stanislaw Gruszka46b90d32013-05-18 14:03:48 +02005338
5339 rt2800_bbp_write(rt2x00dev, 120, 0x50);
Stanislaw Gruszkab7feb9b2013-05-18 14:03:51 +02005340
5341 rt2800_bbp_write(rt2x00dev, 137, 0x0f);
Stanislaw Gruszkac2da5272013-05-18 14:03:53 +02005342
5343 rt2800_bbp_write(rt2x00dev, 163, 0xbd);
5344 /* Set ITxBF timeout to 0x9c40=1000msec */
5345 rt2800_bbp_write(rt2x00dev, 179, 0x02);
5346 rt2800_bbp_write(rt2x00dev, 180, 0x00);
5347 rt2800_bbp_write(rt2x00dev, 182, 0x40);
5348 rt2800_bbp_write(rt2x00dev, 180, 0x01);
5349 rt2800_bbp_write(rt2x00dev, 182, 0x9c);
5350 rt2800_bbp_write(rt2x00dev, 179, 0x00);
5351 /* Reprogram the inband interface to put right values in RXWI */
5352 rt2800_bbp_write(rt2x00dev, 142, 0x04);
5353 rt2800_bbp_write(rt2x00dev, 143, 0x3b);
5354 rt2800_bbp_write(rt2x00dev, 142, 0x06);
5355 rt2800_bbp_write(rt2x00dev, 143, 0xa0);
5356 rt2800_bbp_write(rt2x00dev, 142, 0x07);
5357 rt2800_bbp_write(rt2x00dev, 143, 0xa1);
5358 rt2800_bbp_write(rt2x00dev, 142, 0x08);
5359 rt2800_bbp_write(rt2x00dev, 143, 0xa2);
5360
5361 rt2800_bbp_write(rt2x00dev, 148, 0xc8);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005362}
5363
5364static void rt2800_init_bbp_3390(struct rt2x00_dev *rt2x00dev)
5365{
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005366 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5367 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005368
5369 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5370 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005371
5372 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005373
5374 rt2800_bbp_write(rt2x00dev, 79, 0x13);
5375 rt2800_bbp_write(rt2x00dev, 80, 0x05);
5376 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005377
5378 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005379
5380 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005381
5382 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005383
5384 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005385
5386 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005387
5388 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005389
5390 if (rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E))
5391 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
5392 else
5393 rt2800_bbp_write(rt2x00dev, 103, 0x00);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005394
5395 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005396
5397 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005398
5399 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005400}
5401
5402static void rt2800_init_bbp_3572(struct rt2x00_dev *rt2x00dev)
5403{
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02005404 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005405
5406 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5407 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005408
5409 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5410 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005411
5412 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005413
5414 rt2800_bbp_write(rt2x00dev, 79, 0x13);
5415 rt2800_bbp_write(rt2x00dev, 80, 0x05);
5416 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005417
5418 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005419
5420 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005421
5422 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005423
5424 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005425
5426 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005427
5428 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005429
5430 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005431
5432 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005433
5434 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005435
5436 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005437}
5438
Gabor Juhosb189a182013-07-08 16:08:17 +02005439static void rt2800_init_bbp_3593(struct rt2x00_dev *rt2x00dev)
5440{
5441 rt2800_init_bbp_early(rt2x00dev);
5442
5443 rt2800_bbp_write(rt2x00dev, 79, 0x13);
5444 rt2800_bbp_write(rt2x00dev, 80, 0x05);
5445 rt2800_bbp_write(rt2x00dev, 81, 0x33);
5446 rt2800_bbp_write(rt2x00dev, 137, 0x0f);
5447
5448 rt2800_bbp_write(rt2x00dev, 84, 0x19);
5449
5450 /* Enable DC filter */
5451 if (rt2x00_rt_rev_gte(rt2x00dev, RT3593, REV_RT3593E))
5452 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
5453}
5454
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005455static void rt2800_init_bbp_53xx(struct rt2x00_dev *rt2x00dev)
5456{
Stanislaw Gruszka32ef8f42013-05-18 14:03:55 +02005457 int ant, div_mode;
5458 u16 eeprom;
5459 u8 value;
5460
Stanislaw Gruszkac3223572013-05-18 14:03:28 +02005461 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02005462
5463 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005464
5465 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5466 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka59dcabb2013-05-18 14:03:32 +02005467
5468 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005469
Stanislaw Gruszka58422192014-03-12 15:14:04 +01005470 rt2800_bbp_write(rt2x00dev, 69, 0x12);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005471 rt2800_bbp_write(rt2x00dev, 73, 0x13);
5472 rt2800_bbp_write(rt2x00dev, 75, 0x46);
5473 rt2800_bbp_write(rt2x00dev, 76, 0x28);
5474
5475 rt2800_bbp_write(rt2x00dev, 77, 0x59);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005476
Stanislaw Gruszka58422192014-03-12 15:14:04 +01005477 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
5478
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005479 rt2800_bbp_write(rt2x00dev, 79, 0x13);
5480 rt2800_bbp_write(rt2x00dev, 80, 0x05);
5481 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005482
5483 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005484
5485 rt2800_bbp_write(rt2x00dev, 83, 0x7a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005486
5487 rt2800_bbp_write(rt2x00dev, 84, 0x9a);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005488
5489 rt2800_bbp_write(rt2x00dev, 86, 0x38);
Stanislaw Gruszka9400fa82013-05-18 14:03:40 +02005490
5491 if (rt2x00_rt(rt2x00dev, RT5392))
5492 rt2800_bbp_write(rt2x00dev, 88, 0x90);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005493
5494 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005495
5496 rt2800_bbp_write(rt2x00dev, 92, 0x02);
Stanislaw Gruszka90fed532013-05-18 14:03:43 +02005497
5498 if (rt2x00_rt(rt2x00dev, RT5392)) {
5499 rt2800_bbp_write(rt2x00dev, 95, 0x9a);
5500 rt2800_bbp_write(rt2x00dev, 98, 0x12);
5501 }
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005502
5503 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka1ad44082013-05-18 14:03:45 +02005504
5505 rt2800_bbp_write(rt2x00dev, 104, 0x92);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005506
5507 rt2800_bbp_write(rt2x00dev, 105, 0x3c);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005508
5509 if (rt2x00_rt(rt2x00dev, RT5390))
5510 rt2800_bbp_write(rt2x00dev, 106, 0x03);
5511 else if (rt2x00_rt(rt2x00dev, RT5392))
5512 rt2800_bbp_write(rt2x00dev, 106, 0x12);
5513 else
5514 WARN_ON(1);
Stanislaw Gruszkaf2b67772013-05-18 14:03:49 +02005515
5516 rt2800_bbp_write(rt2x00dev, 128, 0x12);
Stanislaw Gruszka72917142013-05-18 14:03:50 +02005517
5518 if (rt2x00_rt(rt2x00dev, RT5392)) {
5519 rt2800_bbp_write(rt2x00dev, 134, 0xd0);
5520 rt2800_bbp_write(rt2x00dev, 135, 0xf6);
5521 }
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005522
5523 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka32ef8f42013-05-18 14:03:55 +02005524
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005525 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
Stanislaw Gruszka32ef8f42013-05-18 14:03:55 +02005526 div_mode = rt2x00_get_field16(eeprom,
5527 EEPROM_NIC_CONF1_ANT_DIVERSITY);
5528 ant = (div_mode == 3) ? 1 : 0;
5529
5530 /* check if this is a Bluetooth combo card */
Gabor Juhosc429dfe2013-10-11 13:18:42 +02005531 if (rt2x00_has_cap_bt_coexist(rt2x00dev)) {
Stanislaw Gruszka32ef8f42013-05-18 14:03:55 +02005532 u32 reg;
5533
5534 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
5535 rt2x00_set_field32(&reg, GPIO_CTRL_DIR3, 0);
5536 rt2x00_set_field32(&reg, GPIO_CTRL_DIR6, 0);
5537 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, 0);
5538 rt2x00_set_field32(&reg, GPIO_CTRL_VAL6, 0);
5539 if (ant == 0)
5540 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, 1);
5541 else if (ant == 1)
5542 rt2x00_set_field32(&reg, GPIO_CTRL_VAL6, 1);
5543 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
5544 }
5545
5546 /* This chip has hardware antenna diversity*/
5547 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390R)) {
5548 rt2800_bbp_write(rt2x00dev, 150, 0); /* Disable Antenna Software OFDM */
5549 rt2800_bbp_write(rt2x00dev, 151, 0); /* Disable Antenna Software CCK */
5550 rt2800_bbp_write(rt2x00dev, 154, 0); /* Clear previously selected antenna */
5551 }
5552
5553 rt2800_bbp_read(rt2x00dev, 152, &value);
5554 if (ant == 0)
5555 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
5556 else
5557 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
5558 rt2800_bbp_write(rt2x00dev, 152, value);
5559
5560 rt2800_init_freq_calibration(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005561}
5562
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005563static void rt2800_init_bbp_5592(struct rt2x00_dev *rt2x00dev)
5564{
5565 int ant, div_mode;
5566 u16 eeprom;
5567 u8 value;
5568
Gabor Juhos624708b2013-04-19 10:13:52 +02005569 rt2800_init_bbp_early(rt2x00dev);
Stanislaw Gruszkaa4969d02013-03-16 19:19:35 +01005570
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005571 rt2800_bbp_read(rt2x00dev, 105, &value);
5572 rt2x00_set_field8(&value, BBP105_MLD,
5573 rt2x00dev->default_ant.rx_chain_num == 2);
5574 rt2800_bbp_write(rt2x00dev, 105, value);
5575
5576 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
5577
5578 rt2800_bbp_write(rt2x00dev, 20, 0x06);
5579 rt2800_bbp_write(rt2x00dev, 31, 0x08);
5580 rt2800_bbp_write(rt2x00dev, 65, 0x2C);
5581 rt2800_bbp_write(rt2x00dev, 68, 0xDD);
5582 rt2800_bbp_write(rt2x00dev, 69, 0x1A);
5583 rt2800_bbp_write(rt2x00dev, 70, 0x05);
5584 rt2800_bbp_write(rt2x00dev, 73, 0x13);
5585 rt2800_bbp_write(rt2x00dev, 74, 0x0F);
5586 rt2800_bbp_write(rt2x00dev, 75, 0x4F);
5587 rt2800_bbp_write(rt2x00dev, 76, 0x28);
5588 rt2800_bbp_write(rt2x00dev, 77, 0x59);
5589 rt2800_bbp_write(rt2x00dev, 84, 0x9A);
5590 rt2800_bbp_write(rt2x00dev, 86, 0x38);
5591 rt2800_bbp_write(rt2x00dev, 88, 0x90);
5592 rt2800_bbp_write(rt2x00dev, 91, 0x04);
5593 rt2800_bbp_write(rt2x00dev, 92, 0x02);
5594 rt2800_bbp_write(rt2x00dev, 95, 0x9a);
5595 rt2800_bbp_write(rt2x00dev, 98, 0x12);
5596 rt2800_bbp_write(rt2x00dev, 103, 0xC0);
5597 rt2800_bbp_write(rt2x00dev, 104, 0x92);
5598 /* FIXME BBP105 owerwrite */
5599 rt2800_bbp_write(rt2x00dev, 105, 0x3C);
5600 rt2800_bbp_write(rt2x00dev, 106, 0x35);
5601 rt2800_bbp_write(rt2x00dev, 128, 0x12);
5602 rt2800_bbp_write(rt2x00dev, 134, 0xD0);
5603 rt2800_bbp_write(rt2x00dev, 135, 0xF6);
5604 rt2800_bbp_write(rt2x00dev, 137, 0x0F);
5605
5606 /* Initialize GLRT (Generalized Likehood Radio Test) */
5607 rt2800_init_bbp_5592_glrt(rt2x00dev);
5608
5609 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
5610
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005611 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005612 div_mode = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_ANT_DIVERSITY);
5613 ant = (div_mode == 3) ? 1 : 0;
5614 rt2800_bbp_read(rt2x00dev, 152, &value);
5615 if (ant == 0) {
5616 /* Main antenna */
5617 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
5618 } else {
5619 /* Auxiliary antenna */
5620 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
5621 }
5622 rt2800_bbp_write(rt2x00dev, 152, value);
5623
5624 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C)) {
5625 rt2800_bbp_read(rt2x00dev, 254, &value);
5626 rt2x00_set_field8(&value, BBP254_BIT7, 1);
5627 rt2800_bbp_write(rt2x00dev, 254, value);
5628 }
5629
Stanislaw Gruszkac2675482013-03-16 19:19:41 +01005630 rt2800_init_freq_calibration(rt2x00dev);
5631
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005632 rt2800_bbp_write(rt2x00dev, 84, 0x19);
Stanislaw Gruszka6e04f252013-03-16 19:19:38 +01005633 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C))
5634 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005635}
5636
Stanislaw Gruszkaa1ef50392013-05-18 14:03:24 +02005637static void rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005638{
5639 unsigned int i;
5640 u16 eeprom;
5641 u8 reg_id;
5642 u8 value;
5643
Stanislaw Gruszkadae62952013-05-18 14:03:26 +02005644 if (rt2800_is_305x_soc(rt2x00dev))
5645 rt2800_init_bbp_305x_soc(rt2x00dev);
5646
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005647 switch (rt2x00dev->chip.rt) {
5648 case RT2860:
5649 case RT2872:
5650 case RT2883:
5651 rt2800_init_bbp_28xx(rt2x00dev);
5652 break;
5653 case RT3070:
5654 case RT3071:
5655 case RT3090:
5656 rt2800_init_bbp_30xx(rt2x00dev);
5657 break;
5658 case RT3290:
5659 rt2800_init_bbp_3290(rt2x00dev);
5660 break;
5661 case RT3352:
5662 rt2800_init_bbp_3352(rt2x00dev);
5663 break;
5664 case RT3390:
5665 rt2800_init_bbp_3390(rt2x00dev);
5666 break;
5667 case RT3572:
5668 rt2800_init_bbp_3572(rt2x00dev);
5669 break;
Gabor Juhosb189a182013-07-08 16:08:17 +02005670 case RT3593:
5671 rt2800_init_bbp_3593(rt2x00dev);
5672 return;
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005673 case RT5390:
5674 case RT5392:
5675 rt2800_init_bbp_53xx(rt2x00dev);
5676 break;
5677 case RT5592:
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005678 rt2800_init_bbp_5592(rt2x00dev);
Stanislaw Gruszkaa1ef50392013-05-18 14:03:24 +02005679 return;
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005680 }
5681
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005682 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
Gabor Juhos022138c2013-07-08 11:25:54 +02005683 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_BBP_START, i,
5684 &eeprom);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005685
5686 if (eeprom != 0xffff && eeprom != 0x0000) {
5687 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
5688 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
5689 rt2800_bbp_write(rt2x00dev, reg_id, value);
5690 }
5691 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005692}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005693
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02005694static void rt2800_led_open_drain_enable(struct rt2x00_dev *rt2x00dev)
5695{
5696 u32 reg;
5697
5698 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
5699 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
5700 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
5701}
5702
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005703static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev, bool bw40,
5704 u8 filter_target)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005705{
5706 unsigned int i;
5707 u8 bbp;
5708 u8 rfcsr;
5709 u8 passband;
5710 u8 stopband;
5711 u8 overtuned = 0;
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005712 u8 rfcsr24 = (bw40) ? 0x27 : 0x07;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005713
5714 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
5715
5716 rt2800_bbp_read(rt2x00dev, 4, &bbp);
5717 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
5718 rt2800_bbp_write(rt2x00dev, 4, bbp);
5719
RA-Jay Hung80d184e2011-01-10 11:28:10 +01005720 rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
5721 rt2x00_set_field8(&rfcsr, RFCSR31_RX_H20M, bw40);
5722 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
5723
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005724 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
5725 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
5726 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
5727
5728 /*
5729 * Set power & frequency of passband test tone
5730 */
5731 rt2800_bbp_write(rt2x00dev, 24, 0);
5732
5733 for (i = 0; i < 100; i++) {
5734 rt2800_bbp_write(rt2x00dev, 25, 0x90);
5735 msleep(1);
5736
5737 rt2800_bbp_read(rt2x00dev, 55, &passband);
5738 if (passband)
5739 break;
5740 }
5741
5742 /*
5743 * Set power & frequency of stopband test tone
5744 */
5745 rt2800_bbp_write(rt2x00dev, 24, 0x06);
5746
5747 for (i = 0; i < 100; i++) {
5748 rt2800_bbp_write(rt2x00dev, 25, 0x90);
5749 msleep(1);
5750
5751 rt2800_bbp_read(rt2x00dev, 55, &stopband);
5752
5753 if ((passband - stopband) <= filter_target) {
5754 rfcsr24++;
5755 overtuned += ((passband - stopband) == filter_target);
5756 } else
5757 break;
5758
5759 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
5760 }
5761
5762 rfcsr24 -= !!overtuned;
5763
5764 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
5765 return rfcsr24;
5766}
5767
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005768static void rt2800_rf_init_calibration(struct rt2x00_dev *rt2x00dev,
5769 const unsigned int rf_reg)
5770{
5771 u8 rfcsr;
5772
5773 rt2800_rfcsr_read(rt2x00dev, rf_reg, &rfcsr);
5774 rt2x00_set_field8(&rfcsr, FIELD8(0x80), 1);
5775 rt2800_rfcsr_write(rt2x00dev, rf_reg, rfcsr);
5776 msleep(1);
5777 rt2x00_set_field8(&rfcsr, FIELD8(0x80), 0);
5778 rt2800_rfcsr_write(rt2x00dev, rf_reg, rfcsr);
5779}
5780
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005781static void rt2800_rx_filter_calibration(struct rt2x00_dev *rt2x00dev)
5782{
5783 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
5784 u8 filter_tgt_bw20;
5785 u8 filter_tgt_bw40;
5786 u8 rfcsr, bbp;
5787
5788 /*
5789 * TODO: sync filter_tgt values with vendor driver
5790 */
5791 if (rt2x00_rt(rt2x00dev, RT3070)) {
5792 filter_tgt_bw20 = 0x16;
5793 filter_tgt_bw40 = 0x19;
5794 } else {
5795 filter_tgt_bw20 = 0x13;
5796 filter_tgt_bw40 = 0x15;
5797 }
5798
5799 drv_data->calibration_bw20 =
5800 rt2800_init_rx_filter(rt2x00dev, false, filter_tgt_bw20);
5801 drv_data->calibration_bw40 =
5802 rt2800_init_rx_filter(rt2x00dev, true, filter_tgt_bw40);
5803
5804 /*
5805 * Save BBP 25 & 26 values for later use in channel switching (for 3052)
5806 */
5807 rt2800_bbp_read(rt2x00dev, 25, &drv_data->bbp25);
5808 rt2800_bbp_read(rt2x00dev, 26, &drv_data->bbp26);
5809
5810 /*
5811 * Set back to initial state
5812 */
5813 rt2800_bbp_write(rt2x00dev, 24, 0);
5814
5815 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
5816 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
5817 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
5818
5819 /*
5820 * Set BBP back to BW20
5821 */
5822 rt2800_bbp_read(rt2x00dev, 4, &bbp);
5823 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
5824 rt2800_bbp_write(rt2x00dev, 4, bbp);
5825}
5826
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005827static void rt2800_normal_mode_setup_3xxx(struct rt2x00_dev *rt2x00dev)
5828{
5829 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
5830 u8 min_gain, rfcsr, bbp;
5831 u16 eeprom;
5832
5833 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
5834
5835 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
5836 if (rt2x00_rt(rt2x00dev, RT3070) ||
5837 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
5838 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
5839 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gabor Juhosc429dfe2013-10-11 13:18:42 +02005840 if (!rt2x00_has_cap_external_lna_bg(rt2x00dev))
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005841 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
5842 }
5843
5844 min_gain = rt2x00_rt(rt2x00dev, RT3070) ? 1 : 2;
5845 if (drv_data->txmixer_gain_24g >= min_gain) {
5846 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
5847 drv_data->txmixer_gain_24g);
5848 }
5849
5850 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
5851
5852 if (rt2x00_rt(rt2x00dev, RT3090)) {
5853 /* Turn off unused DAC1 and ADC1 to reduce power consumption */
5854 rt2800_bbp_read(rt2x00dev, 138, &bbp);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005855 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005856 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
5857 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
5858 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
5859 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
5860 rt2800_bbp_write(rt2x00dev, 138, bbp);
5861 }
5862
5863 if (rt2x00_rt(rt2x00dev, RT3070)) {
5864 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
5865 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F))
5866 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
5867 else
5868 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
5869 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
5870 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
5871 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
5872 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
5873 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
5874 rt2x00_rt(rt2x00dev, RT3090) ||
5875 rt2x00_rt(rt2x00dev, RT3390)) {
5876 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
5877 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
5878 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
5879 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
5880 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
5881 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
5882 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
5883
5884 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
5885 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
5886 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
5887
5888 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
5889 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
5890 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
5891
5892 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
5893 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
5894 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
5895 }
5896}
5897
Gabor Juhosab7078a2013-07-08 16:08:18 +02005898static void rt2800_normal_mode_setup_3593(struct rt2x00_dev *rt2x00dev)
5899{
5900 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
5901 u8 rfcsr;
5902 u8 tx_gain;
5903
5904 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
5905 rt2x00_set_field8(&rfcsr, RFCSR50_TX_LO2_EN, 0);
5906 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
5907
5908 rt2800_rfcsr_read(rt2x00dev, 51, &rfcsr);
5909 tx_gain = rt2x00_get_field8(drv_data->txmixer_gain_24g,
5910 RFCSR17_TXMIXER_GAIN);
5911 rt2x00_set_field8(&rfcsr, RFCSR51_BITS24, tx_gain);
5912 rt2800_rfcsr_write(rt2x00dev, 51, rfcsr);
5913
5914 rt2800_rfcsr_read(rt2x00dev, 38, &rfcsr);
5915 rt2x00_set_field8(&rfcsr, RFCSR38_RX_LO1_EN, 0);
5916 rt2800_rfcsr_write(rt2x00dev, 38, rfcsr);
5917
5918 rt2800_rfcsr_read(rt2x00dev, 39, &rfcsr);
5919 rt2x00_set_field8(&rfcsr, RFCSR39_RX_LO2_EN, 0);
5920 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
5921
5922 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
5923 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
5924 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
5925 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
5926
5927 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
5928 rt2x00_set_field8(&rfcsr, RFCSR30_RX_VCM, 2);
5929 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
5930
5931 /* TODO: enable stream mode */
5932}
5933
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02005934static void rt2800_normal_mode_setup_5xxx(struct rt2x00_dev *rt2x00dev)
5935{
5936 u8 reg;
5937 u16 eeprom;
5938
5939 /* Turn off unused DAC1 and ADC1 to reduce power consumption */
5940 rt2800_bbp_read(rt2x00dev, 138, &reg);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005941 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02005942 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
5943 rt2x00_set_field8(&reg, BBP138_RX_ADC1, 0);
5944 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
5945 rt2x00_set_field8(&reg, BBP138_TX_DAC1, 1);
5946 rt2800_bbp_write(rt2x00dev, 138, reg);
5947
5948 rt2800_rfcsr_read(rt2x00dev, 38, &reg);
5949 rt2x00_set_field8(&reg, RFCSR38_RX_LO1_EN, 0);
5950 rt2800_rfcsr_write(rt2x00dev, 38, reg);
5951
5952 rt2800_rfcsr_read(rt2x00dev, 39, &reg);
5953 rt2x00_set_field8(&reg, RFCSR39_RX_LO2_EN, 0);
5954 rt2800_rfcsr_write(rt2x00dev, 39, reg);
5955
5956 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
5957
5958 rt2800_rfcsr_read(rt2x00dev, 30, &reg);
5959 rt2x00_set_field8(&reg, RFCSR30_RX_VCM, 2);
5960 rt2800_rfcsr_write(rt2x00dev, 30, reg);
5961}
5962
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005963static void rt2800_init_rfcsr_305x_soc(struct rt2x00_dev *rt2x00dev)
5964{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005965 rt2800_rf_init_calibration(rt2x00dev, 30);
5966
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005967 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
5968 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
5969 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
5970 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
5971 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
5972 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
5973 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
5974 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
5975 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
5976 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
5977 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
5978 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
5979 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
5980 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
5981 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
5982 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
5983 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
5984 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
5985 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
5986 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
5987 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
5988 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
5989 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
5990 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
5991 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
5992 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
5993 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
5994 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
5995 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
5996 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
5997 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
5998 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
5999}
6000
6001static void rt2800_init_rfcsr_30xx(struct rt2x00_dev *rt2x00dev)
6002{
Stanislaw Gruszkac9a221b2013-04-17 14:08:13 +02006003 u8 rfcsr;
6004 u16 eeprom;
6005 u32 reg;
6006
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006007 /* XXX vendor driver do this only for 3070 */
6008 rt2800_rf_init_calibration(rt2x00dev, 30);
6009
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006010 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
6011 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
6012 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
6013 rt2800_rfcsr_write(rt2x00dev, 7, 0x60);
6014 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
6015 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
6016 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
6017 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
6018 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
6019 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
6020 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
6021 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
6022 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
6023 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
6024 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
6025 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
6026 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
Kevin Lo772eb432013-09-18 16:22:44 +08006027 rt2800_rfcsr_write(rt2x00dev, 25, 0x03);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006028 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Stanislaw Gruszkac9a221b2013-04-17 14:08:13 +02006029
6030 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
6031 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6032 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
6033 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
6034 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
6035 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
6036 rt2x00_rt(rt2x00dev, RT3090)) {
6037 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
6038
6039 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
6040 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
6041 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
6042
6043 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6044 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
6045 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
6046 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006047 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1,
6048 &eeprom);
Stanislaw Gruszkac9a221b2013-04-17 14:08:13 +02006049 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
6050 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
6051 else
6052 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
6053 }
6054 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
6055
6056 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
6057 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
6058 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
6059 }
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02006060
6061 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszka5de5a1f2013-04-17 14:08:17 +02006062
6063 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
6064 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
6065 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E))
6066 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006067
6068 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02006069 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006070}
6071
6072static void rt2800_init_rfcsr_3290(struct rt2x00_dev *rt2x00dev)
6073{
Stanislaw Gruszkaf9cdcbb2013-04-17 14:08:12 +02006074 u8 rfcsr;
6075
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006076 rt2800_rf_init_calibration(rt2x00dev, 2);
6077
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006078 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
6079 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
6080 rt2800_rfcsr_write(rt2x00dev, 3, 0x08);
6081 rt2800_rfcsr_write(rt2x00dev, 4, 0x00);
6082 rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
6083 rt2800_rfcsr_write(rt2x00dev, 8, 0xf3);
6084 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
6085 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
6086 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
6087 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
6088 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
6089 rt2800_rfcsr_write(rt2x00dev, 18, 0x02);
6090 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
6091 rt2800_rfcsr_write(rt2x00dev, 25, 0x83);
6092 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
6093 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
6094 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
6095 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
6096 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
6097 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
6098 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
6099 rt2800_rfcsr_write(rt2x00dev, 34, 0x05);
6100 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
6101 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
6102 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
6103 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
6104 rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
6105 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
6106 rt2800_rfcsr_write(rt2x00dev, 42, 0xd5);
6107 rt2800_rfcsr_write(rt2x00dev, 43, 0x7b);
6108 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
6109 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
6110 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
6111 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
6112 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
6113 rt2800_rfcsr_write(rt2x00dev, 49, 0x98);
6114 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
6115 rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
6116 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
6117 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
6118 rt2800_rfcsr_write(rt2x00dev, 56, 0x02);
6119 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
6120 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
6121 rt2800_rfcsr_write(rt2x00dev, 59, 0x09);
6122 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
6123 rt2800_rfcsr_write(rt2x00dev, 61, 0xc1);
Stanislaw Gruszkaf9cdcbb2013-04-17 14:08:12 +02006124
6125 rt2800_rfcsr_read(rt2x00dev, 29, &rfcsr);
6126 rt2x00_set_field8(&rfcsr, RFCSR29_RSSI_GAIN, 3);
6127 rt2800_rfcsr_write(rt2x00dev, 29, rfcsr);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006128
6129 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02006130 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006131}
6132
6133static void rt2800_init_rfcsr_3352(struct rt2x00_dev *rt2x00dev)
6134{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006135 rt2800_rf_init_calibration(rt2x00dev, 30);
6136
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006137 rt2800_rfcsr_write(rt2x00dev, 0, 0xf0);
6138 rt2800_rfcsr_write(rt2x00dev, 1, 0x23);
6139 rt2800_rfcsr_write(rt2x00dev, 2, 0x50);
6140 rt2800_rfcsr_write(rt2x00dev, 3, 0x18);
6141 rt2800_rfcsr_write(rt2x00dev, 4, 0x00);
6142 rt2800_rfcsr_write(rt2x00dev, 5, 0x00);
6143 rt2800_rfcsr_write(rt2x00dev, 6, 0x33);
6144 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
6145 rt2800_rfcsr_write(rt2x00dev, 8, 0xf1);
6146 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
6147 rt2800_rfcsr_write(rt2x00dev, 10, 0xd2);
6148 rt2800_rfcsr_write(rt2x00dev, 11, 0x42);
6149 rt2800_rfcsr_write(rt2x00dev, 12, 0x1c);
6150 rt2800_rfcsr_write(rt2x00dev, 13, 0x00);
6151 rt2800_rfcsr_write(rt2x00dev, 14, 0x5a);
6152 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
6153 rt2800_rfcsr_write(rt2x00dev, 16, 0x01);
6154 rt2800_rfcsr_write(rt2x00dev, 18, 0x45);
6155 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
6156 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
6157 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
6158 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
6159 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
6160 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
6161 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
6162 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
6163 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
6164 rt2800_rfcsr_write(rt2x00dev, 28, 0x03);
6165 rt2800_rfcsr_write(rt2x00dev, 29, 0x00);
6166 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
6167 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
6168 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
6169 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
6170 rt2800_rfcsr_write(rt2x00dev, 34, 0x01);
6171 rt2800_rfcsr_write(rt2x00dev, 35, 0x03);
6172 rt2800_rfcsr_write(rt2x00dev, 36, 0xbd);
6173 rt2800_rfcsr_write(rt2x00dev, 37, 0x3c);
6174 rt2800_rfcsr_write(rt2x00dev, 38, 0x5f);
6175 rt2800_rfcsr_write(rt2x00dev, 39, 0xc5);
6176 rt2800_rfcsr_write(rt2x00dev, 40, 0x33);
6177 rt2800_rfcsr_write(rt2x00dev, 41, 0x5b);
6178 rt2800_rfcsr_write(rt2x00dev, 42, 0x5b);
6179 rt2800_rfcsr_write(rt2x00dev, 43, 0xdb);
6180 rt2800_rfcsr_write(rt2x00dev, 44, 0xdb);
6181 rt2800_rfcsr_write(rt2x00dev, 45, 0xdb);
6182 rt2800_rfcsr_write(rt2x00dev, 46, 0xdd);
6183 rt2800_rfcsr_write(rt2x00dev, 47, 0x0d);
6184 rt2800_rfcsr_write(rt2x00dev, 48, 0x14);
6185 rt2800_rfcsr_write(rt2x00dev, 49, 0x00);
6186 rt2800_rfcsr_write(rt2x00dev, 50, 0x2d);
6187 rt2800_rfcsr_write(rt2x00dev, 51, 0x7f);
6188 rt2800_rfcsr_write(rt2x00dev, 52, 0x00);
6189 rt2800_rfcsr_write(rt2x00dev, 53, 0x52);
6190 rt2800_rfcsr_write(rt2x00dev, 54, 0x1b);
6191 rt2800_rfcsr_write(rt2x00dev, 55, 0x7f);
6192 rt2800_rfcsr_write(rt2x00dev, 56, 0x00);
6193 rt2800_rfcsr_write(rt2x00dev, 57, 0x52);
6194 rt2800_rfcsr_write(rt2x00dev, 58, 0x1b);
6195 rt2800_rfcsr_write(rt2x00dev, 59, 0x00);
6196 rt2800_rfcsr_write(rt2x00dev, 60, 0x00);
6197 rt2800_rfcsr_write(rt2x00dev, 61, 0x00);
6198 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
6199 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02006200
6201 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006202 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02006203 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006204}
6205
6206static void rt2800_init_rfcsr_3390(struct rt2x00_dev *rt2x00dev)
6207{
Stanislaw Gruszka2971e662013-04-17 14:08:14 +02006208 u32 reg;
6209
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006210 rt2800_rf_init_calibration(rt2x00dev, 30);
6211
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006212 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
6213 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
6214 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
6215 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
6216 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
6217 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
6218 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
6219 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
6220 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
6221 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
6222 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
6223 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
6224 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
6225 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
6226 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
6227 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
6228 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
6229 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
6230 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
6231 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
6232 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
6233 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
6234 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
6235 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
6236 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
6237 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
6238 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
6239 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
6240 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
6241 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
6242 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
6243 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Stanislaw Gruszka2971e662013-04-17 14:08:14 +02006244
6245 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
6246 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
6247 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02006248
6249 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszka5de5a1f2013-04-17 14:08:17 +02006250
6251 if (rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
6252 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006253
6254 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02006255 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006256}
6257
6258static void rt2800_init_rfcsr_3572(struct rt2x00_dev *rt2x00dev)
6259{
Stanislaw Gruszka87d91db2013-04-17 14:08:15 +02006260 u8 rfcsr;
6261 u32 reg;
6262
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006263 rt2800_rf_init_calibration(rt2x00dev, 30);
6264
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006265 rt2800_rfcsr_write(rt2x00dev, 0, 0x70);
6266 rt2800_rfcsr_write(rt2x00dev, 1, 0x81);
6267 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
6268 rt2800_rfcsr_write(rt2x00dev, 3, 0x02);
6269 rt2800_rfcsr_write(rt2x00dev, 4, 0x4c);
6270 rt2800_rfcsr_write(rt2x00dev, 5, 0x05);
6271 rt2800_rfcsr_write(rt2x00dev, 6, 0x4a);
6272 rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
6273 rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
6274 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
6275 rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
6276 rt2800_rfcsr_write(rt2x00dev, 12, 0x70);
6277 rt2800_rfcsr_write(rt2x00dev, 13, 0x65);
6278 rt2800_rfcsr_write(rt2x00dev, 14, 0xa0);
6279 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
6280 rt2800_rfcsr_write(rt2x00dev, 16, 0x4c);
6281 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
6282 rt2800_rfcsr_write(rt2x00dev, 18, 0xac);
6283 rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
6284 rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
6285 rt2800_rfcsr_write(rt2x00dev, 21, 0xd0);
6286 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
6287 rt2800_rfcsr_write(rt2x00dev, 23, 0x3c);
6288 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
6289 rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
6290 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
6291 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
6292 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
6293 rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
6294 rt2800_rfcsr_write(rt2x00dev, 30, 0x09);
6295 rt2800_rfcsr_write(rt2x00dev, 31, 0x10);
Stanislaw Gruszka87d91db2013-04-17 14:08:15 +02006296
6297 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
6298 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
6299 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
6300
6301 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6302 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
6303 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
6304 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
6305 msleep(1);
6306 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6307 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
6308 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
6309 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02006310
6311 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006312 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02006313 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006314}
6315
Gabor Juhosd63f7e82013-07-08 16:08:19 +02006316static void rt3593_post_bbp_init(struct rt2x00_dev *rt2x00dev)
6317{
6318 u8 bbp;
6319 bool txbf_enabled = false; /* FIXME */
6320
6321 rt2800_bbp_read(rt2x00dev, 105, &bbp);
6322 if (rt2x00dev->default_ant.rx_chain_num == 1)
6323 rt2x00_set_field8(&bbp, BBP105_MLD, 0);
6324 else
6325 rt2x00_set_field8(&bbp, BBP105_MLD, 1);
6326 rt2800_bbp_write(rt2x00dev, 105, bbp);
6327
6328 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
6329
6330 rt2800_bbp_write(rt2x00dev, 92, 0x02);
6331 rt2800_bbp_write(rt2x00dev, 82, 0x82);
6332 rt2800_bbp_write(rt2x00dev, 106, 0x05);
6333 rt2800_bbp_write(rt2x00dev, 104, 0x92);
6334 rt2800_bbp_write(rt2x00dev, 88, 0x90);
6335 rt2800_bbp_write(rt2x00dev, 148, 0xc8);
6336 rt2800_bbp_write(rt2x00dev, 47, 0x48);
6337 rt2800_bbp_write(rt2x00dev, 120, 0x50);
6338
6339 if (txbf_enabled)
6340 rt2800_bbp_write(rt2x00dev, 163, 0xbd);
6341 else
6342 rt2800_bbp_write(rt2x00dev, 163, 0x9d);
6343
6344 /* SNR mapping */
6345 rt2800_bbp_write(rt2x00dev, 142, 6);
6346 rt2800_bbp_write(rt2x00dev, 143, 160);
6347 rt2800_bbp_write(rt2x00dev, 142, 7);
6348 rt2800_bbp_write(rt2x00dev, 143, 161);
6349 rt2800_bbp_write(rt2x00dev, 142, 8);
6350 rt2800_bbp_write(rt2x00dev, 143, 162);
6351
6352 /* ADC/DAC control */
6353 rt2800_bbp_write(rt2x00dev, 31, 0x08);
6354
6355 /* RX AGC energy lower bound in log2 */
6356 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
6357
6358 /* FIXME: BBP 105 owerwrite? */
6359 rt2800_bbp_write(rt2x00dev, 105, 0x04);
Gabor Juhosf42b0462013-07-08 16:08:30 +02006360
Gabor Juhosd63f7e82013-07-08 16:08:19 +02006361}
6362
Gabor Juhosab7078a2013-07-08 16:08:18 +02006363static void rt2800_init_rfcsr_3593(struct rt2x00_dev *rt2x00dev)
6364{
6365 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
6366 u32 reg;
6367 u8 rfcsr;
6368
6369 /* Disable GPIO #4 and #7 function for LAN PE control */
6370 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
6371 rt2x00_set_field32(&reg, GPIO_SWITCH_4, 0);
6372 rt2x00_set_field32(&reg, GPIO_SWITCH_7, 0);
6373 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
6374
6375 /* Initialize default register values */
6376 rt2800_rfcsr_write(rt2x00dev, 1, 0x03);
6377 rt2800_rfcsr_write(rt2x00dev, 3, 0x80);
6378 rt2800_rfcsr_write(rt2x00dev, 5, 0x00);
6379 rt2800_rfcsr_write(rt2x00dev, 6, 0x40);
6380 rt2800_rfcsr_write(rt2x00dev, 8, 0xf1);
6381 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
6382 rt2800_rfcsr_write(rt2x00dev, 10, 0xd3);
6383 rt2800_rfcsr_write(rt2x00dev, 11, 0x40);
6384 rt2800_rfcsr_write(rt2x00dev, 12, 0x4e);
6385 rt2800_rfcsr_write(rt2x00dev, 13, 0x12);
6386 rt2800_rfcsr_write(rt2x00dev, 18, 0x40);
6387 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
6388 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
6389 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
6390 rt2800_rfcsr_write(rt2x00dev, 32, 0x78);
6391 rt2800_rfcsr_write(rt2x00dev, 33, 0x3b);
6392 rt2800_rfcsr_write(rt2x00dev, 34, 0x3c);
6393 rt2800_rfcsr_write(rt2x00dev, 35, 0xe0);
6394 rt2800_rfcsr_write(rt2x00dev, 38, 0x86);
6395 rt2800_rfcsr_write(rt2x00dev, 39, 0x23);
6396 rt2800_rfcsr_write(rt2x00dev, 44, 0xd3);
6397 rt2800_rfcsr_write(rt2x00dev, 45, 0xbb);
6398 rt2800_rfcsr_write(rt2x00dev, 46, 0x60);
6399 rt2800_rfcsr_write(rt2x00dev, 49, 0x8e);
6400 rt2800_rfcsr_write(rt2x00dev, 50, 0x86);
6401 rt2800_rfcsr_write(rt2x00dev, 51, 0x75);
6402 rt2800_rfcsr_write(rt2x00dev, 52, 0x45);
6403 rt2800_rfcsr_write(rt2x00dev, 53, 0x18);
6404 rt2800_rfcsr_write(rt2x00dev, 54, 0x18);
6405 rt2800_rfcsr_write(rt2x00dev, 55, 0x18);
6406 rt2800_rfcsr_write(rt2x00dev, 56, 0xdb);
6407 rt2800_rfcsr_write(rt2x00dev, 57, 0x6e);
6408
6409 /* Initiate calibration */
6410 /* TODO: use rt2800_rf_init_calibration ? */
6411 rt2800_rfcsr_read(rt2x00dev, 2, &rfcsr);
6412 rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 1);
6413 rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
6414
6415 rt2800_adjust_freq_offset(rt2x00dev);
6416
6417 rt2800_rfcsr_read(rt2x00dev, 18, &rfcsr);
6418 rt2x00_set_field8(&rfcsr, RFCSR18_XO_TUNE_BYPASS, 1);
6419 rt2800_rfcsr_write(rt2x00dev, 18, rfcsr);
6420
6421 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6422 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
6423 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
6424 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
6425 usleep_range(1000, 1500);
6426 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6427 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
6428 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
6429
6430 /* Set initial values for RX filter calibration */
6431 drv_data->calibration_bw20 = 0x1f;
6432 drv_data->calibration_bw40 = 0x2f;
6433
6434 /* Save BBP 25 & 26 values for later use in channel switching */
6435 rt2800_bbp_read(rt2x00dev, 25, &drv_data->bbp25);
6436 rt2800_bbp_read(rt2x00dev, 26, &drv_data->bbp26);
6437
6438 rt2800_led_open_drain_enable(rt2x00dev);
6439 rt2800_normal_mode_setup_3593(rt2x00dev);
6440
Gabor Juhosd63f7e82013-07-08 16:08:19 +02006441 rt3593_post_bbp_init(rt2x00dev);
Gabor Juhosab7078a2013-07-08 16:08:18 +02006442
6443 /* TODO: enable stream mode support */
6444}
6445
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006446static void rt2800_init_rfcsr_5390(struct rt2x00_dev *rt2x00dev)
6447{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006448 rt2800_rf_init_calibration(rt2x00dev, 2);
6449
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006450 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
6451 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
6452 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
6453 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
6454 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6455 rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
6456 else
6457 rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
6458 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
6459 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
6460 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
Kevin Loc8520bc2013-10-24 13:24:08 +08006461 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006462 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
6463 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
6464 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
6465 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
6466 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
6467 rt2800_rfcsr_write(rt2x00dev, 19, 0x00);
6468
6469 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
6470 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
6471 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
6472 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
6473 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
Kevin Loc8520bc2013-10-24 13:24:08 +08006474 if (rt2x00_is_usb(rt2x00dev) &&
6475 rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006476 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
6477 else
6478 rt2800_rfcsr_write(rt2x00dev, 25, 0xc0);
6479 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
6480 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
6481 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
6482 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
6483
Kevin Lo7122e662013-10-12 23:25:23 +08006484 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006485 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
6486 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
6487 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
6488 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
6489 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
6490 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
6491 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
6492 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
6493 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
6494
Kevin Loc8520bc2013-10-24 13:24:08 +08006495 rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006496 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
6497 rt2800_rfcsr_write(rt2x00dev, 42, 0xd2);
6498 rt2800_rfcsr_write(rt2x00dev, 43, 0x9a);
6499 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
6500 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
6501 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6502 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
6503 else
6504 rt2800_rfcsr_write(rt2x00dev, 46, 0x7b);
6505 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
6506 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
6507 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
6508
6509 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
6510 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6511 rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
6512 else
6513 rt2800_rfcsr_write(rt2x00dev, 53, 0x84);
6514 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
6515 rt2800_rfcsr_write(rt2x00dev, 55, 0x44);
Kevin Loc8520bc2013-10-24 13:24:08 +08006516 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6517 rt2800_rfcsr_write(rt2x00dev, 56, 0x42);
6518 else
6519 rt2800_rfcsr_write(rt2x00dev, 56, 0x22);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006520 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
6521 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
Kevin Lo7122e662013-10-12 23:25:23 +08006522 rt2800_rfcsr_write(rt2x00dev, 59, 0x8f);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006523
6524 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
Kevin Loc8520bc2013-10-24 13:24:08 +08006525 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
6526 if (rt2x00_is_usb(rt2x00dev))
6527 rt2800_rfcsr_write(rt2x00dev, 61, 0xd1);
6528 else
6529 rt2800_rfcsr_write(rt2x00dev, 61, 0xd5);
6530 } else {
6531 if (rt2x00_is_usb(rt2x00dev))
6532 rt2800_rfcsr_write(rt2x00dev, 61, 0xdd);
6533 else
6534 rt2800_rfcsr_write(rt2x00dev, 61, 0xb5);
6535 }
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006536 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
6537 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02006538
6539 rt2800_normal_mode_setup_5xxx(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006540
6541 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006542}
6543
6544static void rt2800_init_rfcsr_5392(struct rt2x00_dev *rt2x00dev)
6545{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006546 rt2800_rf_init_calibration(rt2x00dev, 2);
6547
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006548 rt2800_rfcsr_write(rt2x00dev, 1, 0x17);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006549 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
6550 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
6551 rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
6552 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
6553 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
6554 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
6555 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
6556 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
6557 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
6558 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
6559 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
6560 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
6561 rt2800_rfcsr_write(rt2x00dev, 19, 0x4d);
6562 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
6563 rt2800_rfcsr_write(rt2x00dev, 21, 0x8d);
6564 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
6565 rt2800_rfcsr_write(rt2x00dev, 23, 0x0b);
6566 rt2800_rfcsr_write(rt2x00dev, 24, 0x44);
6567 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
6568 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
6569 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
6570 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
6571 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
6572 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
6573 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
6574 rt2800_rfcsr_write(rt2x00dev, 32, 0x20);
6575 rt2800_rfcsr_write(rt2x00dev, 33, 0xC0);
6576 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
6577 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
6578 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
6579 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
6580 rt2800_rfcsr_write(rt2x00dev, 38, 0x89);
6581 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
6582 rt2800_rfcsr_write(rt2x00dev, 40, 0x0f);
6583 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
6584 rt2800_rfcsr_write(rt2x00dev, 42, 0xd5);
6585 rt2800_rfcsr_write(rt2x00dev, 43, 0x9b);
6586 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
6587 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
6588 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
6589 rt2800_rfcsr_write(rt2x00dev, 47, 0x0c);
6590 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
6591 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
6592 rt2800_rfcsr_write(rt2x00dev, 50, 0x94);
6593 rt2800_rfcsr_write(rt2x00dev, 51, 0x3a);
6594 rt2800_rfcsr_write(rt2x00dev, 52, 0x48);
6595 rt2800_rfcsr_write(rt2x00dev, 53, 0x44);
6596 rt2800_rfcsr_write(rt2x00dev, 54, 0x38);
6597 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
6598 rt2800_rfcsr_write(rt2x00dev, 56, 0xa1);
6599 rt2800_rfcsr_write(rt2x00dev, 57, 0x00);
6600 rt2800_rfcsr_write(rt2x00dev, 58, 0x39);
6601 rt2800_rfcsr_write(rt2x00dev, 59, 0x07);
6602 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
6603 rt2800_rfcsr_write(rt2x00dev, 61, 0x91);
6604 rt2800_rfcsr_write(rt2x00dev, 62, 0x39);
6605 rt2800_rfcsr_write(rt2x00dev, 63, 0x07);
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02006606
6607 rt2800_normal_mode_setup_5xxx(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006608
6609 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006610}
6611
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006612static void rt2800_init_rfcsr_5592(struct rt2x00_dev *rt2x00dev)
6613{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006614 rt2800_rf_init_calibration(rt2x00dev, 30);
6615
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006616 rt2800_rfcsr_write(rt2x00dev, 1, 0x3F);
6617 rt2800_rfcsr_write(rt2x00dev, 3, 0x08);
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006618 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
6619 rt2800_rfcsr_write(rt2x00dev, 6, 0xE4);
6620 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
6621 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
6622 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
6623 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
6624 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
6625 rt2800_rfcsr_write(rt2x00dev, 19, 0x4D);
6626 rt2800_rfcsr_write(rt2x00dev, 20, 0x10);
6627 rt2800_rfcsr_write(rt2x00dev, 21, 0x8D);
6628 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
6629 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
6630 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
6631 rt2800_rfcsr_write(rt2x00dev, 33, 0xC0);
6632 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
6633 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
6634 rt2800_rfcsr_write(rt2x00dev, 47, 0x0C);
6635 rt2800_rfcsr_write(rt2x00dev, 53, 0x22);
6636 rt2800_rfcsr_write(rt2x00dev, 63, 0x07);
6637
6638 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
6639 msleep(1);
6640
6641 rt2800_adjust_freq_offset(rt2x00dev);
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006642
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006643 /* Enable DC filter */
6644 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C))
6645 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
6646
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02006647 rt2800_normal_mode_setup_5xxx(rt2x00dev);
Stanislaw Gruszka5de5a1f2013-04-17 14:08:17 +02006648
6649 if (rt2x00_rt_rev_lt(rt2x00dev, RT5592, REV_RT5592C))
6650 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006651
6652 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006653}
6654
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006655static void rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01006656{
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006657 if (rt2800_is_305x_soc(rt2x00dev)) {
6658 rt2800_init_rfcsr_305x_soc(rt2x00dev);
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006659 return;
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006660 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01006661
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006662 switch (rt2x00dev->chip.rt) {
6663 case RT3070:
6664 case RT3071:
6665 case RT3090:
6666 rt2800_init_rfcsr_30xx(rt2x00dev);
6667 break;
6668 case RT3290:
6669 rt2800_init_rfcsr_3290(rt2x00dev);
6670 break;
6671 case RT3352:
6672 rt2800_init_rfcsr_3352(rt2x00dev);
6673 break;
6674 case RT3390:
6675 rt2800_init_rfcsr_3390(rt2x00dev);
6676 break;
6677 case RT3572:
6678 rt2800_init_rfcsr_3572(rt2x00dev);
6679 break;
Gabor Juhosab7078a2013-07-08 16:08:18 +02006680 case RT3593:
6681 rt2800_init_rfcsr_3593(rt2x00dev);
6682 break;
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006683 case RT5390:
6684 rt2800_init_rfcsr_5390(rt2x00dev);
6685 break;
6686 case RT5392:
6687 rt2800_init_rfcsr_5392(rt2x00dev);
6688 break;
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006689 case RT5592:
6690 rt2800_init_rfcsr_5592(rt2x00dev);
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006691 break;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02006692 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01006693}
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006694
6695int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
6696{
6697 u32 reg;
6698 u16 word;
6699
6700 /*
Stanislaw Gruszka61edc7f2013-09-09 12:37:38 +02006701 * Initialize MAC registers.
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006702 */
6703 if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006704 rt2800_init_registers(rt2x00dev)))
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006705 return -EIO;
6706
Stanislaw Gruszka61edc7f2013-09-09 12:37:38 +02006707 /*
6708 * Wait BBP/RF to wake up.
6709 */
Stanislaw Gruszkaf4e1a4d2013-09-09 12:37:37 +02006710 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev)))
6711 return -EIO;
6712
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006713 /*
Stanislaw Gruszka61edc7f2013-09-09 12:37:38 +02006714 * Send signal during boot time to initialize firmware.
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006715 */
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006716 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
6717 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
Stanislaw Gruszkaf4e1a4d2013-09-09 12:37:37 +02006718 if (rt2x00_is_usb(rt2x00dev))
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006719 rt2800_register_write(rt2x00dev, H2M_INT_SRC, 0);
Stanislaw Gruszkaf4e1a4d2013-09-09 12:37:37 +02006720 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006721 msleep(1);
6722
Stanislaw Gruszka61edc7f2013-09-09 12:37:38 +02006723 /*
6724 * Make sure BBP is up and running.
6725 */
Stanislaw Gruszkaf4e1a4d2013-09-09 12:37:37 +02006726 if (unlikely(rt2800_wait_bbp_ready(rt2x00dev)))
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006727 return -EIO;
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006728
Stanislaw Gruszka61edc7f2013-09-09 12:37:38 +02006729 /*
6730 * Initialize BBP/RF registers.
6731 */
Stanislaw Gruszkaa1ef50392013-05-18 14:03:24 +02006732 rt2800_init_bbp(rt2x00dev);
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006733 rt2800_init_rfcsr(rt2x00dev);
6734
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006735 if (rt2x00_is_usb(rt2x00dev) &&
6736 (rt2x00_rt(rt2x00dev, RT3070) ||
6737 rt2x00_rt(rt2x00dev, RT3071) ||
6738 rt2x00_rt(rt2x00dev, RT3572))) {
6739 udelay(200);
6740 rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
6741 udelay(10);
6742 }
6743
6744 /*
6745 * Enable RX.
6746 */
6747 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
6748 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
6749 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
6750 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
6751
6752 udelay(50);
6753
6754 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
6755 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
6756 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006757 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
6758 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
6759
6760 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
6761 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
6762 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
6763 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
6764
6765 /*
6766 * Initialize LED control
6767 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006768 rt2800_eeprom_read(rt2x00dev, EEPROM_LED_AG_CONF, &word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006769 rt2800_mcu_request(rt2x00dev, MCU_LED_AG_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006770 word & 0xff, (word >> 8) & 0xff);
6771
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006772 rt2800_eeprom_read(rt2x00dev, EEPROM_LED_ACT_CONF, &word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006773 rt2800_mcu_request(rt2x00dev, MCU_LED_ACT_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006774 word & 0xff, (word >> 8) & 0xff);
6775
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006776 rt2800_eeprom_read(rt2x00dev, EEPROM_LED_POLARITY, &word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006777 rt2800_mcu_request(rt2x00dev, MCU_LED_LED_POLARITY, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006778 word & 0xff, (word >> 8) & 0xff);
6779
6780 return 0;
6781}
6782EXPORT_SYMBOL_GPL(rt2800_enable_radio);
6783
6784void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
6785{
6786 u32 reg;
6787
Jakub Kicinskif7b395e2012-04-03 03:40:47 +02006788 rt2800_disable_wpdma(rt2x00dev);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006789
6790 /* Wait for DMA, ignore error */
6791 rt2800_wait_wpdma_ready(rt2x00dev);
6792
6793 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
6794 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
6795 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
6796 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006797}
6798EXPORT_SYMBOL_GPL(rt2800_disable_radio);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006799
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006800int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
6801{
6802 u32 reg;
Woody Hunga89534e2012-06-13 15:01:16 +08006803 u16 efuse_ctrl_reg;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006804
Woody Hunga89534e2012-06-13 15:01:16 +08006805 if (rt2x00_rt(rt2x00dev, RT3290))
6806 efuse_ctrl_reg = EFUSE_CTRL_3290;
6807 else
6808 efuse_ctrl_reg = EFUSE_CTRL;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006809
Woody Hunga89534e2012-06-13 15:01:16 +08006810 rt2800_register_read(rt2x00dev, efuse_ctrl_reg, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006811 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
6812}
6813EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
6814
6815static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
6816{
6817 u32 reg;
Woody Hunga89534e2012-06-13 15:01:16 +08006818 u16 efuse_ctrl_reg;
6819 u16 efuse_data0_reg;
6820 u16 efuse_data1_reg;
6821 u16 efuse_data2_reg;
6822 u16 efuse_data3_reg;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006823
Woody Hunga89534e2012-06-13 15:01:16 +08006824 if (rt2x00_rt(rt2x00dev, RT3290)) {
6825 efuse_ctrl_reg = EFUSE_CTRL_3290;
6826 efuse_data0_reg = EFUSE_DATA0_3290;
6827 efuse_data1_reg = EFUSE_DATA1_3290;
6828 efuse_data2_reg = EFUSE_DATA2_3290;
6829 efuse_data3_reg = EFUSE_DATA3_3290;
6830 } else {
6831 efuse_ctrl_reg = EFUSE_CTRL;
6832 efuse_data0_reg = EFUSE_DATA0;
6833 efuse_data1_reg = EFUSE_DATA1;
6834 efuse_data2_reg = EFUSE_DATA2;
6835 efuse_data3_reg = EFUSE_DATA3;
6836 }
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01006837 mutex_lock(&rt2x00dev->csr_mutex);
6838
Woody Hunga89534e2012-06-13 15:01:16 +08006839 rt2800_register_read_lock(rt2x00dev, efuse_ctrl_reg, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006840 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
6841 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
6842 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Woody Hunga89534e2012-06-13 15:01:16 +08006843 rt2800_register_write_lock(rt2x00dev, efuse_ctrl_reg, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006844
6845 /* Wait until the EEPROM has been loaded */
Woody Hunga89534e2012-06-13 15:01:16 +08006846 rt2800_regbusy_read(rt2x00dev, efuse_ctrl_reg, EFUSE_CTRL_KICK, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006847 /* Apparently the data is read from end to start */
Woody Hunga89534e2012-06-13 15:01:16 +08006848 rt2800_register_read_lock(rt2x00dev, efuse_data3_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006849 /* The returned value is in CPU order, but eeprom is le */
Gertjan van Wingerde68fa64e2011-11-16 23:16:15 +01006850 *(u32 *)&rt2x00dev->eeprom[i] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08006851 rt2800_register_read_lock(rt2x00dev, efuse_data2_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006852 *(u32 *)&rt2x00dev->eeprom[i + 2] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08006853 rt2800_register_read_lock(rt2x00dev, efuse_data1_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006854 *(u32 *)&rt2x00dev->eeprom[i + 4] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08006855 rt2800_register_read_lock(rt2x00dev, efuse_data0_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006856 *(u32 *)&rt2x00dev->eeprom[i + 6] = cpu_to_le32(reg);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01006857
6858 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006859}
6860
Gabor Juhosa02308e2012-12-29 14:51:51 +01006861int rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006862{
6863 unsigned int i;
6864
6865 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
6866 rt2800_efuse_read(rt2x00dev, i);
Gabor Juhosa02308e2012-12-29 14:51:51 +01006867
6868 return 0;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006869}
6870EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
6871
Gabor Juhosa3f16252013-07-08 16:08:25 +02006872static u8 rt2800_get_txmixer_gain_24g(struct rt2x00_dev *rt2x00dev)
6873{
6874 u16 word;
6875
Gabor Juhos6316c782013-07-08 16:08:26 +02006876 if (rt2x00_rt(rt2x00dev, RT3593))
6877 return 0;
6878
Gabor Juhosa3f16252013-07-08 16:08:25 +02006879 rt2800_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &word);
6880 if ((word & 0x00ff) != 0x00ff)
6881 return rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_BG_VAL);
6882
6883 return 0;
6884}
6885
6886static u8 rt2800_get_txmixer_gain_5g(struct rt2x00_dev *rt2x00dev)
6887{
6888 u16 word;
6889
Gabor Juhos6316c782013-07-08 16:08:26 +02006890 if (rt2x00_rt(rt2x00dev, RT3593))
6891 return 0;
6892
Gabor Juhosa3f16252013-07-08 16:08:25 +02006893 rt2800_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_A, &word);
6894 if ((word & 0x00ff) != 0x00ff)
6895 return rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_A_VAL);
6896
6897 return 0;
6898}
6899
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02006900static int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006901{
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01006902 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006903 u16 word;
6904 u8 *mac;
6905 u8 default_lna_gain;
Gabor Juhosa02308e2012-12-29 14:51:51 +01006906 int retval;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006907
6908 /*
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02006909 * Read the EEPROM.
6910 */
Gabor Juhosa02308e2012-12-29 14:51:51 +01006911 retval = rt2800_read_eeprom(rt2x00dev);
6912 if (retval)
6913 return retval;
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02006914
6915 /*
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006916 * Start validation of the data that has been read.
6917 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006918 mac = rt2800_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
Mathias Kresin9766cb72016-08-26 09:16:53 +02006919 rt2x00lib_set_mac_address(rt2x00dev, mac);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006920
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006921 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006922 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006923 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
6924 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_TXPATH, 1);
6925 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RF_TYPE, RF2820);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006926 rt2800_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Joe Perchesec9c4982013-04-19 08:33:40 -07006927 rt2x00_eeprom_dbg(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01006928 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02006929 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006930 /*
6931 * There is a max of 2 RX streams for RT28x0 series
6932 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006933 if (rt2x00_get_field16(word, EEPROM_NIC_CONF0_RXPATH) > 2)
6934 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006935 rt2800_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006936 }
6937
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006938 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006939 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006940 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_HW_RADIO, 0);
6941 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC, 0);
6942 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G, 0);
6943 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G, 0);
6944 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_CARDBUS_ACCEL, 0);
6945 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_2G, 0);
6946 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_5G, 0);
6947 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_WPS_PBC, 0);
6948 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_2G, 0);
6949 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_5G, 0);
6950 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BROADBAND_EXT_LNA, 0);
6951 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_ANT_DIVERSITY, 0);
6952 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_INTERNAL_TX_ALC, 0);
6953 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BT_COEXIST, 0);
6954 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_DAC_TEST, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006955 rt2800_eeprom_write(rt2x00dev, EEPROM_NIC_CONF1, word);
Joe Perchesec9c4982013-04-19 08:33:40 -07006956 rt2x00_eeprom_dbg(rt2x00dev, "NIC: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006957 }
6958
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006959 rt2800_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006960 if ((word & 0x00ff) == 0x00ff) {
6961 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006962 rt2800_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
Joe Perchesec9c4982013-04-19 08:33:40 -07006963 rt2x00_eeprom_dbg(rt2x00dev, "Freq: 0x%04x\n", word);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02006964 }
6965 if ((word & 0xff00) == 0xff00) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006966 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
6967 LED_MODE_TXRX_ACTIVITY);
6968 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006969 rt2800_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
6970 rt2800_eeprom_write(rt2x00dev, EEPROM_LED_AG_CONF, 0x5555);
6971 rt2800_eeprom_write(rt2x00dev, EEPROM_LED_ACT_CONF, 0x2221);
6972 rt2800_eeprom_write(rt2x00dev, EEPROM_LED_POLARITY, 0xa9f8);
Joe Perchesec9c4982013-04-19 08:33:40 -07006973 rt2x00_eeprom_dbg(rt2x00dev, "Led Mode: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006974 }
6975
6976 /*
6977 * During the LNA validation we are going to use
6978 * lna0 as correct value. Note that EEPROM_LNA
6979 * is never validated.
6980 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006981 rt2800_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006982 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
6983
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006984 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006985 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
6986 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
6987 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
6988 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006989 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006990
Gabor Juhosa3f16252013-07-08 16:08:25 +02006991 drv_data->txmixer_gain_24g = rt2800_get_txmixer_gain_24g(rt2x00dev);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01006992
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006993 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006994 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
6995 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
Gabor Juhosf36bb0c2013-07-08 16:08:27 +02006996 if (!rt2x00_rt(rt2x00dev, RT3593)) {
6997 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
6998 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
6999 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
7000 default_lna_gain);
7001 }
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007002 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007003
Gabor Juhosa3f16252013-07-08 16:08:25 +02007004 drv_data->txmixer_gain_5g = rt2800_get_txmixer_gain_5g(rt2x00dev);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01007005
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007006 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007007 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
7008 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
7009 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
7010 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007011 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007012
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007013 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007014 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
7015 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
Gabor Juhosf36bb0c2013-07-08 16:08:27 +02007016 if (!rt2x00_rt(rt2x00dev, RT3593)) {
7017 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
7018 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
7019 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
7020 default_lna_gain);
7021 }
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007022 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007023
Gabor Juhosf36bb0c2013-07-08 16:08:27 +02007024 if (rt2x00_rt(rt2x00dev, RT3593)) {
7025 rt2800_eeprom_read(rt2x00dev, EEPROM_EXT_LNA2, &word);
7026 if (rt2x00_get_field16(word, EEPROM_EXT_LNA2_A1) == 0x00 ||
7027 rt2x00_get_field16(word, EEPROM_EXT_LNA2_A1) == 0xff)
7028 rt2x00_set_field16(&word, EEPROM_EXT_LNA2_A1,
7029 default_lna_gain);
7030 if (rt2x00_get_field16(word, EEPROM_EXT_LNA2_A2) == 0x00 ||
7031 rt2x00_get_field16(word, EEPROM_EXT_LNA2_A2) == 0xff)
7032 rt2x00_set_field16(&word, EEPROM_EXT_LNA2_A1,
7033 default_lna_gain);
7034 rt2800_eeprom_write(rt2x00dev, EEPROM_EXT_LNA2, word);
7035 }
7036
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007037 return 0;
7038}
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007039
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007040static int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007041{
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007042 u16 value;
7043 u16 eeprom;
Gabor Juhos86868b22013-03-30 14:53:09 +01007044 u16 rf;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007045
Gabor Juhos86868b22013-03-30 14:53:09 +01007046 /*
7047 * Read EEPROM word for configuration.
7048 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007049 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Gabor Juhos86868b22013-03-30 14:53:09 +01007050
7051 /*
7052 * Identify RF chipset by EEPROM value
7053 * RT28xx/RT30xx: defined in "EEPROM_NIC_CONF0_RF_TYPE" field
7054 * RT53xx: defined in "EEPROM_CHIP_ID" field
7055 */
7056 if (rt2x00_rt(rt2x00dev, RT3290) ||
7057 rt2x00_rt(rt2x00dev, RT5390) ||
7058 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007059 rt2800_eeprom_read(rt2x00dev, EEPROM_CHIP_ID, &rf);
Gabor Juhos86868b22013-03-30 14:53:09 +01007060 else
7061 rf = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RF_TYPE);
7062
7063 switch (rf) {
Larry Fingerd331eb52011-09-14 16:50:22 -05007064 case RF2820:
7065 case RF2850:
7066 case RF2720:
7067 case RF2750:
7068 case RF3020:
7069 case RF2020:
7070 case RF3021:
7071 case RF3022:
7072 case RF3052:
Gabor Juhos0f5af262013-07-08 16:08:32 +02007073 case RF3053:
Stanislaw Gruszka3b9b74b2013-09-25 15:34:55 +02007074 case RF3070:
Woody Hunga89534e2012-06-13 15:01:16 +08007075 case RF3290:
Larry Fingerd331eb52011-09-14 16:50:22 -05007076 case RF3320:
Daniel Golle03839952012-09-09 14:24:39 +03007077 case RF3322:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02007078 case RF5360:
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05007079 case RF5362:
Larry Fingerd331eb52011-09-14 16:50:22 -05007080 case RF5370:
John Li2ed71882012-02-17 17:33:06 +08007081 case RF5372:
Larry Fingerd331eb52011-09-14 16:50:22 -05007082 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08007083 case RF5392:
Stanislaw Gruszkab8863f82013-03-16 19:19:30 +01007084 case RF5592:
Larry Fingerd331eb52011-09-14 16:50:22 -05007085 break;
7086 default:
Joe Perchesec9c4982013-04-19 08:33:40 -07007087 rt2x00_err(rt2x00dev, "Invalid RF chipset 0x%04x detected\n",
7088 rf);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007089 return -ENODEV;
7090 }
7091
Gabor Juhos86868b22013-03-30 14:53:09 +01007092 rt2x00_set_rf(rt2x00dev, rf);
7093
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007094 /*
7095 * Identify default antenna configuration.
7096 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01007097 rt2x00dev->default_ant.tx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007098 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01007099 rt2x00dev->default_ant.rx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007100 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007101
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007102 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01007103
7104 if (rt2x00_rt(rt2x00dev, RT3070) ||
7105 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03007106 rt2x00_rt(rt2x00dev, RT3352) ||
RA-Jay Hungd96aa642011-02-20 13:54:52 +01007107 rt2x00_rt(rt2x00dev, RT3390)) {
7108 value = rt2x00_get_field16(eeprom,
7109 EEPROM_NIC_CONF1_ANT_DIVERSITY);
7110 switch (value) {
7111 case 0:
7112 case 1:
7113 case 2:
7114 rt2x00dev->default_ant.tx = ANTENNA_A;
7115 rt2x00dev->default_ant.rx = ANTENNA_A;
7116 break;
7117 case 3:
7118 rt2x00dev->default_ant.tx = ANTENNA_A;
7119 rt2x00dev->default_ant.rx = ANTENNA_B;
7120 break;
7121 }
7122 } else {
7123 rt2x00dev->default_ant.tx = ANTENNA_A;
7124 rt2x00dev->default_ant.rx = ANTENNA_A;
7125 }
7126
Anisse Astier0586a112012-04-23 12:33:11 +02007127 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390R)) {
7128 rt2x00dev->default_ant.tx = ANTENNA_HW_DIVERSITY; /* Unused */
7129 rt2x00dev->default_ant.rx = ANTENNA_HW_DIVERSITY; /* Unused */
7130 }
7131
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007132 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02007133 * Determine external LNA informations.
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007134 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007135 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02007136 __set_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007137 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02007138 __set_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007139
7140 /*
7141 * Detect if this device has an hardware controlled radio.
7142 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007143 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_HW_RADIO))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02007144 __set_bit(CAPABILITY_HW_BUTTON, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007145
7146 /*
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02007147 * Detect if this device has Bluetooth co-existence.
7148 */
7149 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_BT_COEXIST))
7150 __set_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags);
7151
7152 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02007153 * Read frequency offset and RF programming sequence.
7154 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007155 rt2800_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02007156 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
7157
7158 /*
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007159 * Store led settings, for correct led behaviour.
7160 */
7161#ifdef CONFIG_RT2X00_LIB_LEDS
7162 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
7163 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
7164 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
7165
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02007166 rt2x00dev->led_mcu_reg = eeprom;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007167#endif /* CONFIG_RT2X00_LIB_LEDS */
7168
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01007169 /*
7170 * Check if support EIRP tx power limit feature.
7171 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007172 rt2800_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER, &eeprom);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01007173
7174 if (rt2x00_get_field16(eeprom, EEPROM_EIRP_MAX_TX_POWER_2GHZ) <
7175 EIRP_MAX_TX_POWER_LIMIT)
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02007176 __set_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01007177
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007178 return 0;
7179}
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007180
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007181/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02007182 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007183 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
7184 */
7185static const struct rf_channel rf_vals[] = {
7186 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
7187 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
7188 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
7189 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
7190 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
7191 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
7192 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
7193 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
7194 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
7195 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
7196 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
7197 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
7198 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
7199 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
7200
7201 /* 802.11 UNI / HyperLan 2 */
7202 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
7203 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
7204 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
7205 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
7206 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
7207 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
7208 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
7209 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
7210 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
7211 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
7212 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
7213 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
7214
7215 /* 802.11 HyperLan 2 */
7216 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
7217 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
7218 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
7219 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
7220 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
7221 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
7222 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
7223 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
7224 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
7225 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
7226 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
7227 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
7228 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
7229 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
7230 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
7231 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
7232
7233 /* 802.11 UNII */
7234 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
7235 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
7236 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
7237 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
7238 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
7239 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
7240 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
7241 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
7242 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
7243 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
7244 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
7245
7246 /* 802.11 Japan */
7247 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
7248 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
7249 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
7250 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
7251 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
7252 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
7253 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
7254};
7255
7256/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02007257 * RF value list for rt3xxx
Kevin Lob6b561c2013-10-14 10:05:45 +08007258 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052 & RF3053)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007259 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02007260static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007261 {1, 241, 2, 2 },
7262 {2, 241, 2, 7 },
7263 {3, 242, 2, 2 },
7264 {4, 242, 2, 7 },
7265 {5, 243, 2, 2 },
7266 {6, 243, 2, 7 },
7267 {7, 244, 2, 2 },
7268 {8, 244, 2, 7 },
7269 {9, 245, 2, 2 },
7270 {10, 245, 2, 7 },
7271 {11, 246, 2, 2 },
7272 {12, 246, 2, 7 },
7273 {13, 247, 2, 2 },
7274 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02007275
7276 /* 802.11 UNI / HyperLan 2 */
7277 {36, 0x56, 0, 4},
7278 {38, 0x56, 0, 6},
7279 {40, 0x56, 0, 8},
7280 {44, 0x57, 0, 0},
7281 {46, 0x57, 0, 2},
7282 {48, 0x57, 0, 4},
7283 {52, 0x57, 0, 8},
7284 {54, 0x57, 0, 10},
7285 {56, 0x58, 0, 0},
7286 {60, 0x58, 0, 4},
7287 {62, 0x58, 0, 6},
7288 {64, 0x58, 0, 8},
7289
7290 /* 802.11 HyperLan 2 */
7291 {100, 0x5b, 0, 8},
7292 {102, 0x5b, 0, 10},
7293 {104, 0x5c, 0, 0},
7294 {108, 0x5c, 0, 4},
7295 {110, 0x5c, 0, 6},
7296 {112, 0x5c, 0, 8},
7297 {116, 0x5d, 0, 0},
7298 {118, 0x5d, 0, 2},
7299 {120, 0x5d, 0, 4},
7300 {124, 0x5d, 0, 8},
7301 {126, 0x5d, 0, 10},
7302 {128, 0x5e, 0, 0},
7303 {132, 0x5e, 0, 4},
7304 {134, 0x5e, 0, 6},
7305 {136, 0x5e, 0, 8},
7306 {140, 0x5f, 0, 0},
7307
7308 /* 802.11 UNII */
7309 {149, 0x5f, 0, 9},
7310 {151, 0x5f, 0, 11},
7311 {153, 0x60, 0, 1},
7312 {157, 0x60, 0, 5},
7313 {159, 0x60, 0, 7},
7314 {161, 0x60, 0, 9},
7315 {165, 0x61, 0, 1},
7316 {167, 0x61, 0, 3},
7317 {169, 0x61, 0, 5},
7318 {171, 0x61, 0, 7},
7319 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007320};
7321
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01007322static const struct rf_channel rf_vals_5592_xtal20[] = {
7323 /* Channel, N, K, mod, R */
7324 {1, 482, 4, 10, 3},
7325 {2, 483, 4, 10, 3},
7326 {3, 484, 4, 10, 3},
7327 {4, 485, 4, 10, 3},
7328 {5, 486, 4, 10, 3},
7329 {6, 487, 4, 10, 3},
7330 {7, 488, 4, 10, 3},
7331 {8, 489, 4, 10, 3},
7332 {9, 490, 4, 10, 3},
7333 {10, 491, 4, 10, 3},
7334 {11, 492, 4, 10, 3},
7335 {12, 493, 4, 10, 3},
7336 {13, 494, 4, 10, 3},
7337 {14, 496, 8, 10, 3},
7338 {36, 172, 8, 12, 1},
7339 {38, 173, 0, 12, 1},
7340 {40, 173, 4, 12, 1},
7341 {42, 173, 8, 12, 1},
7342 {44, 174, 0, 12, 1},
7343 {46, 174, 4, 12, 1},
7344 {48, 174, 8, 12, 1},
7345 {50, 175, 0, 12, 1},
7346 {52, 175, 4, 12, 1},
7347 {54, 175, 8, 12, 1},
7348 {56, 176, 0, 12, 1},
7349 {58, 176, 4, 12, 1},
7350 {60, 176, 8, 12, 1},
7351 {62, 177, 0, 12, 1},
7352 {64, 177, 4, 12, 1},
7353 {100, 183, 4, 12, 1},
7354 {102, 183, 8, 12, 1},
7355 {104, 184, 0, 12, 1},
7356 {106, 184, 4, 12, 1},
7357 {108, 184, 8, 12, 1},
7358 {110, 185, 0, 12, 1},
7359 {112, 185, 4, 12, 1},
7360 {114, 185, 8, 12, 1},
7361 {116, 186, 0, 12, 1},
7362 {118, 186, 4, 12, 1},
7363 {120, 186, 8, 12, 1},
7364 {122, 187, 0, 12, 1},
7365 {124, 187, 4, 12, 1},
7366 {126, 187, 8, 12, 1},
7367 {128, 188, 0, 12, 1},
7368 {130, 188, 4, 12, 1},
7369 {132, 188, 8, 12, 1},
7370 {134, 189, 0, 12, 1},
7371 {136, 189, 4, 12, 1},
7372 {138, 189, 8, 12, 1},
7373 {140, 190, 0, 12, 1},
7374 {149, 191, 6, 12, 1},
7375 {151, 191, 10, 12, 1},
7376 {153, 192, 2, 12, 1},
7377 {155, 192, 6, 12, 1},
7378 {157, 192, 10, 12, 1},
7379 {159, 193, 2, 12, 1},
7380 {161, 193, 6, 12, 1},
7381 {165, 194, 2, 12, 1},
7382 {184, 164, 0, 12, 1},
7383 {188, 164, 4, 12, 1},
7384 {192, 165, 8, 12, 1},
7385 {196, 166, 0, 12, 1},
7386};
7387
7388static const struct rf_channel rf_vals_5592_xtal40[] = {
7389 /* Channel, N, K, mod, R */
7390 {1, 241, 2, 10, 3},
7391 {2, 241, 7, 10, 3},
7392 {3, 242, 2, 10, 3},
7393 {4, 242, 7, 10, 3},
7394 {5, 243, 2, 10, 3},
7395 {6, 243, 7, 10, 3},
7396 {7, 244, 2, 10, 3},
7397 {8, 244, 7, 10, 3},
7398 {9, 245, 2, 10, 3},
7399 {10, 245, 7, 10, 3},
7400 {11, 246, 2, 10, 3},
7401 {12, 246, 7, 10, 3},
7402 {13, 247, 2, 10, 3},
7403 {14, 248, 4, 10, 3},
7404 {36, 86, 4, 12, 1},
7405 {38, 86, 6, 12, 1},
7406 {40, 86, 8, 12, 1},
7407 {42, 86, 10, 12, 1},
7408 {44, 87, 0, 12, 1},
7409 {46, 87, 2, 12, 1},
7410 {48, 87, 4, 12, 1},
7411 {50, 87, 6, 12, 1},
7412 {52, 87, 8, 12, 1},
7413 {54, 87, 10, 12, 1},
7414 {56, 88, 0, 12, 1},
7415 {58, 88, 2, 12, 1},
7416 {60, 88, 4, 12, 1},
7417 {62, 88, 6, 12, 1},
7418 {64, 88, 8, 12, 1},
7419 {100, 91, 8, 12, 1},
7420 {102, 91, 10, 12, 1},
7421 {104, 92, 0, 12, 1},
7422 {106, 92, 2, 12, 1},
7423 {108, 92, 4, 12, 1},
7424 {110, 92, 6, 12, 1},
7425 {112, 92, 8, 12, 1},
7426 {114, 92, 10, 12, 1},
7427 {116, 93, 0, 12, 1},
7428 {118, 93, 2, 12, 1},
7429 {120, 93, 4, 12, 1},
7430 {122, 93, 6, 12, 1},
7431 {124, 93, 8, 12, 1},
7432 {126, 93, 10, 12, 1},
7433 {128, 94, 0, 12, 1},
7434 {130, 94, 2, 12, 1},
7435 {132, 94, 4, 12, 1},
7436 {134, 94, 6, 12, 1},
7437 {136, 94, 8, 12, 1},
7438 {138, 94, 10, 12, 1},
7439 {140, 95, 0, 12, 1},
7440 {149, 95, 9, 12, 1},
7441 {151, 95, 11, 12, 1},
7442 {153, 96, 1, 12, 1},
7443 {155, 96, 3, 12, 1},
7444 {157, 96, 5, 12, 1},
7445 {159, 96, 7, 12, 1},
7446 {161, 96, 9, 12, 1},
7447 {165, 97, 1, 12, 1},
7448 {184, 82, 0, 12, 1},
7449 {188, 82, 4, 12, 1},
7450 {192, 82, 8, 12, 1},
7451 {196, 83, 0, 12, 1},
7452};
7453
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007454static int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007455{
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007456 struct hw_mode_spec *spec = &rt2x00dev->spec;
7457 struct channel_info *info;
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02007458 char *default_power1;
7459 char *default_power2;
Gabor Juhosc0a14362013-07-08 16:08:28 +02007460 char *default_power3;
Stanislaw Gruszkacea5b032016-11-18 10:43:53 +01007461 unsigned int i, tx_chains, rx_chains;
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01007462 u32 reg;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007463
7464 /*
Stanislaw Gruszka58e33a22014-01-29 17:42:37 +01007465 * Disable powersaving as default.
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01007466 */
Stanislaw Gruszka58e33a22014-01-29 17:42:37 +01007467 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01007468
7469 /*
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007470 * Initialize all hw fields.
7471 */
Johannes Berg30686bf2015-06-02 21:39:54 +02007472 ieee80211_hw_set(rt2x00dev->hw, SUPPORTS_HT_CCK_RATES);
7473 ieee80211_hw_set(rt2x00dev->hw, REPORTS_TX_ACK_STATUS);
7474 ieee80211_hw_set(rt2x00dev->hw, AMPDU_AGGREGATION);
7475 ieee80211_hw_set(rt2x00dev->hw, PS_NULLFUNC_STACK);
7476 ieee80211_hw_set(rt2x00dev->hw, SIGNAL_DBM);
7477 ieee80211_hw_set(rt2x00dev->hw, SUPPORTS_PS);
Helmut Schaa9d4f09b2012-03-14 08:56:47 +01007478
Helmut Schaa5a5b6ed2010-10-02 11:31:33 +02007479 /*
7480 * Don't set IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING for USB devices
7481 * unless we are capable of sending the buffered frames out after the
7482 * DTIM transmission using rt2x00lib_beacondone. This will send out
7483 * multicast and broadcast traffic immediately instead of buffering it
7484 * infinitly and thus dropping it after some time.
7485 */
7486 if (!rt2x00_is_usb(rt2x00dev))
Johannes Berg30686bf2015-06-02 21:39:54 +02007487 ieee80211_hw_set(rt2x00dev->hw, HOST_BROADCAST_PS_BUFFERING);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007488
Chun-Yeow Yeoh25576542016-04-21 00:41:34 +08007489 /* Set MFP if HW crypto is disabled. */
7490 if (rt2800_hwcrypt_disabled(rt2x00dev))
7491 ieee80211_hw_set(rt2x00dev->hw, MFP_CAPABLE);
7492
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007493 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
7494 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007495 rt2800_eeprom_addr(rt2x00dev,
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007496 EEPROM_MAC_ADDR_0));
7497
Helmut Schaa3f2bee22010-06-14 22:12:01 +02007498 /*
7499 * As rt2800 has a global fallback table we cannot specify
7500 * more then one tx rate per frame but since the hw will
7501 * try several rates (based on the fallback table) we should
Helmut Schaaba3b9e52010-10-02 11:32:16 +02007502 * initialize max_report_rates to the maximum number of rates
Helmut Schaa3f2bee22010-06-14 22:12:01 +02007503 * we are going to try. Otherwise mac80211 will truncate our
7504 * reported tx rates and the rc algortihm will end up with
7505 * incorrect data.
7506 */
Helmut Schaaba3b9e52010-10-02 11:32:16 +02007507 rt2x00dev->hw->max_rates = 1;
7508 rt2x00dev->hw->max_report_rates = 7;
Helmut Schaa3f2bee22010-06-14 22:12:01 +02007509 rt2x00dev->hw->max_rate_tries = 1;
7510
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007511 /*
7512 * Initialize hw_mode information.
7513 */
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007514 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
7515
Gabor Juhos4a32c362013-10-14 21:59:51 +02007516 switch (rt2x00dev->chip.rf) {
7517 case RF2720:
7518 case RF2820:
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007519 spec->num_channels = 14;
7520 spec->channels = rf_vals;
Gabor Juhos4a32c362013-10-14 21:59:51 +02007521 break;
7522
7523 case RF2750:
7524 case RF2850:
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007525 spec->num_channels = ARRAY_SIZE(rf_vals);
7526 spec->channels = rf_vals;
Gabor Juhos4a32c362013-10-14 21:59:51 +02007527 break;
7528
7529 case RF2020:
7530 case RF3020:
7531 case RF3021:
7532 case RF3022:
7533 case RF3070:
7534 case RF3290:
7535 case RF3320:
7536 case RF3322:
7537 case RF5360:
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05007538 case RF5362:
Gabor Juhos4a32c362013-10-14 21:59:51 +02007539 case RF5370:
7540 case RF5372:
7541 case RF5390:
7542 case RF5392:
Ivo van Doorn55f93212010-05-06 14:45:46 +02007543 spec->num_channels = 14;
7544 spec->channels = rf_vals_3x;
Gabor Juhos4a32c362013-10-14 21:59:51 +02007545 break;
7546
7547 case RF3052:
7548 case RF3053:
Ivo van Doorn55f93212010-05-06 14:45:46 +02007549 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
7550 spec->channels = rf_vals_3x;
Gabor Juhos4a32c362013-10-14 21:59:51 +02007551 break;
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01007552
Gabor Juhos4a32c362013-10-14 21:59:51 +02007553 case RF5592:
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01007554 rt2800_register_read(rt2x00dev, MAC_DEBUG_INDEX, &reg);
7555 if (rt2x00_get_field32(reg, MAC_DEBUG_INDEX_XTAL)) {
7556 spec->num_channels = ARRAY_SIZE(rf_vals_5592_xtal40);
7557 spec->channels = rf_vals_5592_xtal40;
7558 } else {
7559 spec->num_channels = ARRAY_SIZE(rf_vals_5592_xtal20);
7560 spec->channels = rf_vals_5592_xtal20;
7561 }
Gabor Juhos4a32c362013-10-14 21:59:51 +02007562 break;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007563 }
7564
Stanislaw Gruszka53216d62013-03-16 19:19:29 +01007565 if (WARN_ON_ONCE(!spec->channels))
7566 return -ENODEV;
7567
Gabor Juhos53c5a092013-10-14 21:59:52 +02007568 spec->supported_bands = SUPPORT_BAND_2GHZ;
7569 if (spec->num_channels > 14)
7570 spec->supported_bands |= SUPPORT_BAND_5GHZ;
7571
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007572 /*
7573 * Initialize HT information.
7574 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01007575 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01007576 spec->ht.ht_supported = true;
7577 else
7578 spec->ht.ht_supported = false;
7579
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007580 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02007581 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007582 IEEE80211_HT_CAP_GRN_FLD |
7583 IEEE80211_HT_CAP_SGI_20 |
Ivo van Doornaa674632010-06-29 21:48:37 +02007584 IEEE80211_HT_CAP_SGI_40;
Helmut Schaa22cabaa2010-06-03 10:52:10 +02007585
Stanislaw Gruszkacea5b032016-11-18 10:43:53 +01007586 tx_chains = rt2x00dev->default_ant.tx_chain_num;
7587 rx_chains = rt2x00dev->default_ant.rx_chain_num;
7588
7589 if (tx_chains >= 2)
Helmut Schaa22cabaa2010-06-03 10:52:10 +02007590 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
7591
Stanislaw Gruszkacea5b032016-11-18 10:43:53 +01007592 spec->ht.cap |= rx_chains << IEEE80211_HT_CAP_RX_STBC_SHIFT;
Ivo van Doornaa674632010-06-29 21:48:37 +02007593
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007594 spec->ht.ampdu_factor = 3;
7595 spec->ht.ampdu_density = 4;
Stanislaw Gruszkacea5b032016-11-18 10:43:53 +01007596 spec->ht.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
7597 if (tx_chains != rx_chains) {
7598 spec->ht.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
7599 spec->ht.mcs.tx_params |=
7600 (tx_chains - 1) << IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
7601 }
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007602
Stanislaw Gruszkacea5b032016-11-18 10:43:53 +01007603 switch (rx_chains) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007604 case 3:
7605 spec->ht.mcs.rx_mask[2] = 0xff;
7606 case 2:
7607 spec->ht.mcs.rx_mask[1] = 0xff;
7608 case 1:
7609 spec->ht.mcs.rx_mask[0] = 0xff;
7610 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
7611 break;
7612 }
7613
7614 /*
7615 * Create channel information array
7616 */
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00007617 info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007618 if (!info)
7619 return -ENOMEM;
7620
7621 spec->channels_info = info;
7622
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007623 default_power1 = rt2800_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
7624 default_power2 = rt2800_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007625
Gabor Juhosc0a14362013-07-08 16:08:28 +02007626 if (rt2x00dev->default_ant.tx_chain_num > 2)
7627 default_power3 = rt2800_eeprom_addr(rt2x00dev,
7628 EEPROM_EXT_TXPOWER_BG3);
7629 else
7630 default_power3 = NULL;
7631
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007632 for (i = 0; i < 14; i++) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01007633 info[i].default_power1 = default_power1[i];
7634 info[i].default_power2 = default_power2[i];
Gabor Juhosc0a14362013-07-08 16:08:28 +02007635 if (default_power3)
7636 info[i].default_power3 = default_power3[i];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007637 }
7638
7639 if (spec->num_channels > 14) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007640 default_power1 = rt2800_eeprom_addr(rt2x00dev,
7641 EEPROM_TXPOWER_A1);
7642 default_power2 = rt2800_eeprom_addr(rt2x00dev,
7643 EEPROM_TXPOWER_A2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007644
Gabor Juhosc0a14362013-07-08 16:08:28 +02007645 if (rt2x00dev->default_ant.tx_chain_num > 2)
7646 default_power3 =
7647 rt2800_eeprom_addr(rt2x00dev,
7648 EEPROM_EXT_TXPOWER_A3);
7649 else
7650 default_power3 = NULL;
7651
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007652 for (i = 14; i < spec->num_channels; i++) {
Gabor Juhos0a6f3a82013-06-22 13:13:25 +02007653 info[i].default_power1 = default_power1[i - 14];
7654 info[i].default_power2 = default_power2[i - 14];
Gabor Juhosc0a14362013-07-08 16:08:28 +02007655 if (default_power3)
7656 info[i].default_power3 = default_power3[i - 14];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007657 }
7658 }
7659
John Li2e9c43d2012-02-16 21:40:57 +08007660 switch (rt2x00dev->chip.rf) {
7661 case RF2020:
7662 case RF3020:
7663 case RF3021:
7664 case RF3022:
7665 case RF3320:
7666 case RF3052:
Gabor Juhos1095df02013-07-08 16:08:31 +02007667 case RF3053:
Stanislaw Gruszka3b9b74b2013-09-25 15:34:55 +02007668 case RF3070:
Woody Hunga89534e2012-06-13 15:01:16 +08007669 case RF3290:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02007670 case RF5360:
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05007671 case RF5362:
John Li2e9c43d2012-02-16 21:40:57 +08007672 case RF5370:
7673 case RF5372:
7674 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08007675 case RF5392:
John Li2e9c43d2012-02-16 21:40:57 +08007676 __set_bit(CAPABILITY_VCO_RECALIBRATION, &rt2x00dev->cap_flags);
7677 break;
7678 }
7679
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007680 return 0;
7681}
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007682
Gabor Juhoscbafb602013-03-30 14:53:10 +01007683static int rt2800_probe_rt(struct rt2x00_dev *rt2x00dev)
7684{
7685 u32 reg;
7686 u32 rt;
7687 u32 rev;
7688
7689 if (rt2x00_rt(rt2x00dev, RT3290))
7690 rt2800_register_read(rt2x00dev, MAC_CSR0_3290, &reg);
7691 else
7692 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
7693
7694 rt = rt2x00_get_field32(reg, MAC_CSR0_CHIPSET);
7695 rev = rt2x00_get_field32(reg, MAC_CSR0_REVISION);
7696
7697 switch (rt) {
7698 case RT2860:
7699 case RT2872:
7700 case RT2883:
7701 case RT3070:
7702 case RT3071:
7703 case RT3090:
7704 case RT3290:
7705 case RT3352:
7706 case RT3390:
7707 case RT3572:
Gabor Juhos2dc2bd22013-07-08 16:08:33 +02007708 case RT3593:
Gabor Juhoscbafb602013-03-30 14:53:10 +01007709 case RT5390:
7710 case RT5392:
7711 case RT5592:
7712 break;
7713 default:
Joe Perchesec9c4982013-04-19 08:33:40 -07007714 rt2x00_err(rt2x00dev, "Invalid RT chipset 0x%04x, rev %04x detected\n",
7715 rt, rev);
Gabor Juhoscbafb602013-03-30 14:53:10 +01007716 return -ENODEV;
7717 }
7718
7719 rt2x00_set_rt(rt2x00dev, rt, rev);
7720
7721 return 0;
7722}
7723
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007724int rt2800_probe_hw(struct rt2x00_dev *rt2x00dev)
7725{
7726 int retval;
7727 u32 reg;
7728
Gabor Juhoscbafb602013-03-30 14:53:10 +01007729 retval = rt2800_probe_rt(rt2x00dev);
7730 if (retval)
7731 return retval;
7732
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007733 /*
7734 * Allocate eeprom data.
7735 */
7736 retval = rt2800_validate_eeprom(rt2x00dev);
7737 if (retval)
7738 return retval;
7739
7740 retval = rt2800_init_eeprom(rt2x00dev);
7741 if (retval)
7742 return retval;
7743
7744 /*
7745 * Enable rfkill polling by setting GPIO direction of the
7746 * rfkill switch GPIO pin correctly.
7747 */
7748 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
7749 rt2x00_set_field32(&reg, GPIO_CTRL_DIR2, 1);
7750 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
7751
7752 /*
7753 * Initialize hw specifications.
7754 */
7755 retval = rt2800_probe_hw_mode(rt2x00dev);
7756 if (retval)
7757 return retval;
7758
7759 /*
7760 * Set device capabilities.
7761 */
7762 __set_bit(CAPABILITY_CONTROL_FILTERS, &rt2x00dev->cap_flags);
7763 __set_bit(CAPABILITY_CONTROL_FILTER_PSPOLL, &rt2x00dev->cap_flags);
7764 if (!rt2x00_is_usb(rt2x00dev))
7765 __set_bit(CAPABILITY_PRE_TBTT_INTERRUPT, &rt2x00dev->cap_flags);
7766
7767 /*
7768 * Set device requirements.
7769 */
7770 if (!rt2x00_is_soc(rt2x00dev))
7771 __set_bit(REQUIRE_FIRMWARE, &rt2x00dev->cap_flags);
7772 __set_bit(REQUIRE_L2PAD, &rt2x00dev->cap_flags);
7773 __set_bit(REQUIRE_TXSTATUS_FIFO, &rt2x00dev->cap_flags);
7774 if (!rt2800_hwcrypt_disabled(rt2x00dev))
7775 __set_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags);
7776 __set_bit(CAPABILITY_LINK_TUNING, &rt2x00dev->cap_flags);
7777 __set_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags);
7778 if (rt2x00_is_usb(rt2x00dev))
7779 __set_bit(REQUIRE_PS_AUTOWAKE, &rt2x00dev->cap_flags);
7780 else {
7781 __set_bit(REQUIRE_DMA, &rt2x00dev->cap_flags);
7782 __set_bit(REQUIRE_TASKLET_CONTEXT, &rt2x00dev->cap_flags);
7783 }
7784
7785 /*
7786 * Set the rssi offset.
7787 */
7788 rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
7789
7790 return 0;
7791}
7792EXPORT_SYMBOL_GPL(rt2800_probe_hw);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007793
7794/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007795 * IEEE80211 stack callback functions.
7796 */
Johannes Berg9352c192015-04-20 18:12:41 +02007797void rt2800_get_key_seq(struct ieee80211_hw *hw,
7798 struct ieee80211_key_conf *key,
7799 struct ieee80211_key_seq *seq)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007800{
7801 struct rt2x00_dev *rt2x00dev = hw->priv;
7802 struct mac_iveiv_entry iveiv_entry;
7803 u32 offset;
7804
Johannes Berg9352c192015-04-20 18:12:41 +02007805 if (key->cipher != WLAN_CIPHER_SUITE_TKIP)
7806 return;
7807
7808 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007809 rt2800_register_multiread(rt2x00dev, offset,
7810 &iveiv_entry, sizeof(iveiv_entry));
7811
Johannes Berg9352c192015-04-20 18:12:41 +02007812 memcpy(&seq->tkip.iv16, &iveiv_entry.iv[0], 2);
7813 memcpy(&seq->tkip.iv32, &iveiv_entry.iv[4], 4);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007814}
Johannes Berg9352c192015-04-20 18:12:41 +02007815EXPORT_SYMBOL_GPL(rt2800_get_key_seq);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007816
Helmut Schaae7836192010-07-11 12:28:54 +02007817int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007818{
7819 struct rt2x00_dev *rt2x00dev = hw->priv;
7820 u32 reg;
7821 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
7822
7823 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
7824 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
7825 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
7826
7827 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
7828 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
7829 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
7830
7831 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
7832 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
7833 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
7834
7835 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
7836 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
7837 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
7838
7839 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
7840 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
7841 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
7842
7843 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
7844 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
7845 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
7846
7847 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
7848 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
7849 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
7850
7851 return 0;
7852}
Helmut Schaae7836192010-07-11 12:28:54 +02007853EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007854
Eliad Peller8a3a3c82011-10-02 10:15:52 +02007855int rt2800_conf_tx(struct ieee80211_hw *hw,
7856 struct ieee80211_vif *vif, u16 queue_idx,
Helmut Schaae7836192010-07-11 12:28:54 +02007857 const struct ieee80211_tx_queue_params *params)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007858{
7859 struct rt2x00_dev *rt2x00dev = hw->priv;
7860 struct data_queue *queue;
7861 struct rt2x00_field32 field;
7862 int retval;
7863 u32 reg;
7864 u32 offset;
7865
7866 /*
7867 * First pass the configuration through rt2x00lib, that will
7868 * update the queue settings and validate the input. After that
7869 * we are free to update the registers based on the value
7870 * in the queue parameter.
7871 */
Eliad Peller8a3a3c82011-10-02 10:15:52 +02007872 retval = rt2x00mac_conf_tx(hw, vif, queue_idx, params);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007873 if (retval)
7874 return retval;
7875
7876 /*
7877 * We only need to perform additional register initialization
7878 * for WMM queues/
7879 */
7880 if (queue_idx >= 4)
7881 return 0;
7882
Helmut Schaa11f818e2011-03-03 19:38:55 +01007883 queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007884
7885 /* Update WMM TXOP register */
7886 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
7887 field.bit_offset = (queue_idx & 1) * 16;
7888 field.bit_mask = 0xffff << field.bit_offset;
7889
7890 rt2800_register_read(rt2x00dev, offset, &reg);
7891 rt2x00_set_field32(&reg, field, queue->txop);
7892 rt2800_register_write(rt2x00dev, offset, reg);
7893
7894 /* Update WMM registers */
7895 field.bit_offset = queue_idx * 4;
7896 field.bit_mask = 0xf << field.bit_offset;
7897
7898 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
7899 rt2x00_set_field32(&reg, field, queue->aifs);
7900 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
7901
7902 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
7903 rt2x00_set_field32(&reg, field, queue->cw_min);
7904 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
7905
7906 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
7907 rt2x00_set_field32(&reg, field, queue->cw_max);
7908 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
7909
7910 /* Update EDCA registers */
7911 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
7912
7913 rt2800_register_read(rt2x00dev, offset, &reg);
7914 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
7915 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
7916 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
7917 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
7918 rt2800_register_write(rt2x00dev, offset, reg);
7919
7920 return 0;
7921}
Helmut Schaae7836192010-07-11 12:28:54 +02007922EXPORT_SYMBOL_GPL(rt2800_conf_tx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007923
Eliad Peller37a41b42011-09-21 14:06:11 +03007924u64 rt2800_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007925{
7926 struct rt2x00_dev *rt2x00dev = hw->priv;
7927 u64 tsf;
7928 u32 reg;
7929
7930 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
7931 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
7932 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
7933 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
7934
7935 return tsf;
7936}
Helmut Schaae7836192010-07-11 12:28:54 +02007937EXPORT_SYMBOL_GPL(rt2800_get_tsf);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007938
Helmut Schaae7836192010-07-11 12:28:54 +02007939int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Sara Sharon50ea05e2015-12-30 16:06:04 +02007940 struct ieee80211_ampdu_params *params)
Helmut Schaa1df90802010-06-29 21:38:12 +02007941{
Sara Sharon50ea05e2015-12-30 16:06:04 +02007942 struct ieee80211_sta *sta = params->sta;
7943 enum ieee80211_ampdu_mlme_action action = params->action;
7944 u16 tid = params->tid;
Helmut Schaaaf353232011-09-08 14:38:36 +02007945 struct rt2x00_sta *sta_priv = (struct rt2x00_sta *)sta->drv_priv;
Helmut Schaa1df90802010-06-29 21:38:12 +02007946 int ret = 0;
7947
Helmut Schaaaf353232011-09-08 14:38:36 +02007948 /*
7949 * Don't allow aggregation for stations the hardware isn't aware
7950 * of because tx status reports for frames to an unknown station
Stanislaw Gruszkaed8e0ed2015-06-11 12:53:45 +02007951 * always contain wcid=WCID_END+1 and thus we can't distinguish
7952 * between multiple stations which leads to unwanted situations
7953 * when the hw reorders frames due to aggregation.
Helmut Schaaaf353232011-09-08 14:38:36 +02007954 */
Stanislaw Gruszkaed8e0ed2015-06-11 12:53:45 +02007955 if (sta_priv->wcid > WCID_END)
Helmut Schaaaf353232011-09-08 14:38:36 +02007956 return 1;
7957
Helmut Schaa1df90802010-06-29 21:38:12 +02007958 switch (action) {
7959 case IEEE80211_AMPDU_RX_START:
7960 case IEEE80211_AMPDU_RX_STOP:
Helmut Schaa58ed8262010-10-02 11:33:17 +02007961 /*
7962 * The hw itself takes care of setting up BlockAck mechanisms.
7963 * So, we only have to allow mac80211 to nagotiate a BlockAck
7964 * agreement. Once that is done, the hw will BlockAck incoming
7965 * AMPDUs without further setup.
7966 */
Helmut Schaa1df90802010-06-29 21:38:12 +02007967 break;
7968 case IEEE80211_AMPDU_TX_START:
7969 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
7970 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02007971 case IEEE80211_AMPDU_TX_STOP_CONT:
7972 case IEEE80211_AMPDU_TX_STOP_FLUSH:
7973 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Helmut Schaa1df90802010-06-29 21:38:12 +02007974 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
7975 break;
7976 case IEEE80211_AMPDU_TX_OPERATIONAL:
7977 break;
7978 default:
Joe Perchesec9c4982013-04-19 08:33:40 -07007979 rt2x00_warn((struct rt2x00_dev *)hw->priv,
7980 "Unknown AMPDU action\n");
Helmut Schaa1df90802010-06-29 21:38:12 +02007981 }
7982
7983 return ret;
7984}
Helmut Schaae7836192010-07-11 12:28:54 +02007985EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02007986
Helmut Schaa977206d2010-12-13 12:31:58 +01007987int rt2800_get_survey(struct ieee80211_hw *hw, int idx,
7988 struct survey_info *survey)
7989{
7990 struct rt2x00_dev *rt2x00dev = hw->priv;
7991 struct ieee80211_conf *conf = &hw->conf;
7992 u32 idle, busy, busy_ext;
7993
7994 if (idx != 0)
7995 return -ENOENT;
7996
Karl Beldan675a0b02013-03-25 16:26:57 +01007997 survey->channel = conf->chandef.chan;
Helmut Schaa977206d2010-12-13 12:31:58 +01007998
7999 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &idle);
8000 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &busy);
8001 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &busy_ext);
8002
8003 if (idle || busy) {
Johannes Berg4ed20be2014-11-14 16:35:34 +01008004 survey->filled = SURVEY_INFO_TIME |
8005 SURVEY_INFO_TIME_BUSY |
8006 SURVEY_INFO_TIME_EXT_BUSY;
Helmut Schaa977206d2010-12-13 12:31:58 +01008007
Johannes Berg4ed20be2014-11-14 16:35:34 +01008008 survey->time = (idle + busy) / 1000;
8009 survey->time_busy = busy / 1000;
8010 survey->time_ext_busy = busy_ext / 1000;
Helmut Schaa977206d2010-12-13 12:31:58 +01008011 }
8012
Helmut Schaa9931df22011-12-22 09:36:29 +01008013 if (!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
8014 survey->filled |= SURVEY_INFO_IN_USE;
8015
Helmut Schaa977206d2010-12-13 12:31:58 +01008016 return 0;
8017
8018}
8019EXPORT_SYMBOL_GPL(rt2800_get_survey);
8020
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02008021MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
8022MODULE_VERSION(DRV_VERSION);
8023MODULE_DESCRIPTION("Ralink RT2800 library");
8024MODULE_LICENSE("GPL");