blob: dfeca8355b22f0972c3665906ec5726a9073ec75 [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
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200780 if (rt2x00dev->curr_band == IEEE80211_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 =
1177 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
1178 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
Helmut Schaaa2b13282011-09-08 14:38:01 +02001384static inline int rt2800_find_wcid(struct rt2x00_dev *rt2x00dev)
Helmut Schaa1ed38112011-03-03 19:44:33 +01001385{
Helmut Schaaa2b13282011-09-08 14:38:01 +02001386 struct mac_wcid_entry wcid_entry;
Helmut Schaa1ed38112011-03-03 19:44:33 +01001387 int idx;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001388 u32 offset;
Helmut Schaa1ed38112011-03-03 19:44:33 +01001389
1390 /*
Helmut Schaaa2b13282011-09-08 14:38:01 +02001391 * Search for the first free WCID entry and return the corresponding
1392 * index.
Helmut Schaa1ed38112011-03-03 19:44:33 +01001393 *
1394 * Make sure the WCID starts _after_ the last possible shared key
1395 * entry (>32).
1396 *
1397 * Since parts of the pairwise key table might be shared with
1398 * the beacon frame buffers 6 & 7 we should only write into the
1399 * first 222 entries.
1400 */
1401 for (idx = 33; idx <= 222; idx++) {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001402 offset = MAC_WCID_ENTRY(idx);
1403 rt2800_register_multiread(rt2x00dev, offset, &wcid_entry,
1404 sizeof(wcid_entry));
1405 if (is_broadcast_ether_addr(wcid_entry.mac))
Helmut Schaa1ed38112011-03-03 19:44:33 +01001406 return idx;
1407 }
Helmut Schaaa2b13282011-09-08 14:38:01 +02001408
1409 /*
1410 * Use -1 to indicate that we don't have any more space in the WCID
1411 * table.
1412 */
Helmut Schaa1ed38112011-03-03 19:44:33 +01001413 return -1;
1414}
1415
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001416int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
1417 struct rt2x00lib_crypto *crypto,
1418 struct ieee80211_key_conf *key)
1419{
1420 struct hw_key_entry key_entry;
1421 u32 offset;
1422
1423 if (crypto->cmd == SET_KEY) {
Helmut Schaaa2b13282011-09-08 14:38:01 +02001424 /*
1425 * Allow key configuration only for STAs that are
1426 * known by the hw.
1427 */
1428 if (crypto->wcid < 0)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001429 return -ENOSPC;
Helmut Schaaa2b13282011-09-08 14:38:01 +02001430 key->hw_key_idx = crypto->wcid;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001431
1432 memcpy(key_entry.key, crypto->key,
1433 sizeof(key_entry.key));
1434 memcpy(key_entry.tx_mic, crypto->tx_mic,
1435 sizeof(key_entry.tx_mic));
1436 memcpy(key_entry.rx_mic, crypto->rx_mic,
1437 sizeof(key_entry.rx_mic));
1438
1439 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
1440 rt2800_register_multiwrite(rt2x00dev, offset,
1441 &key_entry, sizeof(key_entry));
1442 }
1443
1444 /*
1445 * Update WCID information
1446 */
Helmut Schaaa2b13282011-09-08 14:38:01 +02001447 rt2800_config_wcid_attr_cipher(rt2x00dev, crypto, key);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001448
1449 return 0;
1450}
1451EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
1452
Helmut Schaaa2b13282011-09-08 14:38:01 +02001453int rt2800_sta_add(struct rt2x00_dev *rt2x00dev, struct ieee80211_vif *vif,
1454 struct ieee80211_sta *sta)
1455{
1456 int wcid;
1457 struct rt2x00_sta *sta_priv = sta_to_rt2x00_sta(sta);
1458
1459 /*
1460 * Find next free WCID.
1461 */
1462 wcid = rt2800_find_wcid(rt2x00dev);
1463
1464 /*
1465 * Store selected wcid even if it is invalid so that we can
1466 * later decide if the STA is uploaded into the hw.
1467 */
1468 sta_priv->wcid = wcid;
1469
1470 /*
1471 * No space left in the device, however, we can still communicate
1472 * with the STA -> No error.
1473 */
1474 if (wcid < 0)
1475 return 0;
1476
1477 /*
1478 * Clean up WCID attributes and write STA address to the device.
1479 */
1480 rt2800_delete_wcid_attr(rt2x00dev, wcid);
1481 rt2800_config_wcid(rt2x00dev, sta->addr, wcid);
1482 rt2800_config_wcid_attr_bssidx(rt2x00dev, wcid,
1483 rt2x00lib_get_bssidx(rt2x00dev, vif));
1484 return 0;
1485}
1486EXPORT_SYMBOL_GPL(rt2800_sta_add);
1487
1488int rt2800_sta_remove(struct rt2x00_dev *rt2x00dev, int wcid)
1489{
1490 /*
1491 * Remove WCID entry, no need to clean the attributes as they will
1492 * get renewed when the WCID is reused.
1493 */
1494 rt2800_config_wcid(rt2x00dev, NULL, wcid);
1495
1496 return 0;
1497}
1498EXPORT_SYMBOL_GPL(rt2800_sta_remove);
1499
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001500void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
1501 const unsigned int filter_flags)
1502{
1503 u32 reg;
1504
1505 /*
1506 * Start configuration steps.
1507 * Note that the version error will always be dropped
1508 * and broadcast frames will always be accepted since
1509 * there is no filter for it at this time.
1510 */
1511 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
1512 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
1513 !(filter_flags & FIF_FCSFAIL));
1514 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
1515 !(filter_flags & FIF_PLCPFAIL));
Johannes Bergdf140462015-04-22 14:40:58 +02001516 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME, 1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001517 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
1518 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
1519 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
1520 !(filter_flags & FIF_ALLMULTI));
1521 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
1522 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
1523 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
1524 !(filter_flags & FIF_CONTROL));
1525 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
1526 !(filter_flags & FIF_CONTROL));
1527 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
1528 !(filter_flags & FIF_CONTROL));
1529 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
1530 !(filter_flags & FIF_CONTROL));
1531 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
1532 !(filter_flags & FIF_CONTROL));
1533 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
1534 !(filter_flags & FIF_PSPOLL));
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +01001535 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 0);
Helmut Schaa48839932011-11-24 09:13:26 +01001536 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR,
1537 !(filter_flags & FIF_CONTROL));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001538 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
1539 !(filter_flags & FIF_CONTROL));
1540 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
1541}
1542EXPORT_SYMBOL_GPL(rt2800_config_filter);
1543
1544void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
1545 struct rt2x00intf_conf *conf, const unsigned int flags)
1546{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001547 u32 reg;
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001548 bool update_bssid = false;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001549
1550 if (flags & CONFIG_UPDATE_TYPE) {
1551 /*
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001552 * Enable synchronisation.
1553 */
1554 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001555 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001556 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Helmut Schaa15a533c2011-04-18 15:28:04 +02001557
1558 if (conf->sync == TSF_SYNC_AP_NONE) {
1559 /*
1560 * Tune beacon queue transmit parameters for AP mode
1561 */
1562 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1563 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 0);
1564 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 1);
1565 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1566 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 0);
1567 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1568 } else {
1569 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1570 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 4);
1571 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 2);
1572 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1573 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 16);
1574 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1575 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001576 }
1577
1578 if (flags & CONFIG_UPDATE_MAC) {
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001579 if (flags & CONFIG_UPDATE_TYPE &&
1580 conf->sync == TSF_SYNC_AP_NONE) {
1581 /*
1582 * The BSSID register has to be set to our own mac
1583 * address in AP mode.
1584 */
1585 memcpy(conf->bssid, conf->mac, sizeof(conf->mac));
1586 update_bssid = true;
1587 }
1588
Ivo van Doornc600c822010-08-30 21:14:15 +02001589 if (!is_zero_ether_addr((const u8 *)conf->mac)) {
1590 reg = le32_to_cpu(conf->mac[1]);
1591 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
1592 conf->mac[1] = cpu_to_le32(reg);
1593 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001594
1595 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
1596 conf->mac, sizeof(conf->mac));
1597 }
1598
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001599 if ((flags & CONFIG_UPDATE_BSSID) || update_bssid) {
Ivo van Doornc600c822010-08-30 21:14:15 +02001600 if (!is_zero_ether_addr((const u8 *)conf->bssid)) {
1601 reg = le32_to_cpu(conf->bssid[1]);
1602 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
Stanislaw Gruszka88ff2f42014-06-05 13:52:25 +02001603 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 0);
Ivo van Doornc600c822010-08-30 21:14:15 +02001604 conf->bssid[1] = cpu_to_le32(reg);
1605 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001606
1607 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
1608 conf->bssid, sizeof(conf->bssid));
1609 }
1610}
1611EXPORT_SYMBOL_GPL(rt2800_config_intf);
1612
Helmut Schaa87c19152010-10-02 11:28:34 +02001613static void rt2800_config_ht_opmode(struct rt2x00_dev *rt2x00dev,
1614 struct rt2x00lib_erp *erp)
1615{
1616 bool any_sta_nongf = !!(erp->ht_opmode &
1617 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1618 u8 protection = erp->ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION;
1619 u8 mm20_mode, mm40_mode, gf20_mode, gf40_mode;
1620 u16 mm20_rate, mm40_rate, gf20_rate, gf40_rate;
1621 u32 reg;
1622
1623 /* default protection rate for HT20: OFDM 24M */
1624 mm20_rate = gf20_rate = 0x4004;
1625
1626 /* default protection rate for HT40: duplicate OFDM 24M */
1627 mm40_rate = gf40_rate = 0x4084;
1628
1629 switch (protection) {
1630 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
1631 /*
1632 * All STAs in this BSS are HT20/40 but there might be
1633 * STAs not supporting greenfield mode.
1634 * => Disable protection for HT transmissions.
1635 */
1636 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 0;
1637
1638 break;
1639 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
1640 /*
1641 * All STAs in this BSS are HT20 or HT20/40 but there
1642 * might be STAs not supporting greenfield mode.
1643 * => Protect all HT40 transmissions.
1644 */
1645 mm20_mode = gf20_mode = 0;
1646 mm40_mode = gf40_mode = 2;
1647
1648 break;
1649 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
1650 /*
1651 * Nonmember protection:
1652 * According to 802.11n we _should_ protect all
1653 * HT transmissions (but we don't have to).
1654 *
1655 * But if cts_protection is enabled we _shall_ protect
1656 * all HT transmissions using a CCK rate.
1657 *
1658 * And if any station is non GF we _shall_ protect
1659 * GF transmissions.
1660 *
1661 * We decide to protect everything
1662 * -> fall through to mixed mode.
1663 */
1664 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
1665 /*
1666 * Legacy STAs are present
1667 * => Protect all HT transmissions.
1668 */
1669 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 2;
1670
1671 /*
1672 * If erp protection is needed we have to protect HT
1673 * transmissions with CCK 11M long preamble.
1674 */
1675 if (erp->cts_protection) {
1676 /* don't duplicate RTS/CTS in CCK mode */
1677 mm20_rate = mm40_rate = 0x0003;
1678 gf20_rate = gf40_rate = 0x0003;
1679 }
1680 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001681 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001682
1683 /* check for STAs not supporting greenfield mode */
1684 if (any_sta_nongf)
1685 gf20_mode = gf40_mode = 2;
1686
1687 /* Update HT protection config */
1688 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1689 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, mm20_rate);
1690 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, mm20_mode);
1691 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1692
1693 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1694 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, mm40_rate);
1695 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, mm40_mode);
1696 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1697
1698 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1699 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, gf20_rate);
1700 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, gf20_mode);
1701 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1702
1703 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1704 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, gf40_rate);
1705 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, gf40_mode);
1706 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1707}
1708
Helmut Schaa02044642010-09-08 20:56:32 +02001709void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp,
1710 u32 changed)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001711{
1712 u32 reg;
1713
Helmut Schaa02044642010-09-08 20:56:32 +02001714 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1715 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1716 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
1717 !!erp->short_preamble);
1718 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
1719 !!erp->short_preamble);
1720 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1721 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001722
Helmut Schaa02044642010-09-08 20:56:32 +02001723 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1724 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
1725 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
1726 erp->cts_protection ? 2 : 0);
1727 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1728 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001729
Helmut Schaa02044642010-09-08 20:56:32 +02001730 if (changed & BSS_CHANGED_BASIC_RATES) {
1731 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
1732 erp->basic_rates);
1733 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1734 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001735
Helmut Schaa02044642010-09-08 20:56:32 +02001736 if (changed & BSS_CHANGED_ERP_SLOT) {
1737 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1738 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME,
1739 erp->slot_time);
1740 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001741
Helmut Schaa02044642010-09-08 20:56:32 +02001742 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
1743 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
1744 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1745 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001746
Helmut Schaa02044642010-09-08 20:56:32 +02001747 if (changed & BSS_CHANGED_BEACON_INT) {
1748 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1749 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
1750 erp->beacon_int * 16);
1751 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1752 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001753
1754 if (changed & BSS_CHANGED_HT)
1755 rt2800_config_ht_opmode(rt2x00dev, erp);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001756}
1757EXPORT_SYMBOL_GPL(rt2800_config_erp);
1758
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001759static void rt2800_config_3572bt_ant(struct rt2x00_dev *rt2x00dev)
1760{
1761 u32 reg;
1762 u16 eeprom;
1763 u8 led_ctrl, led_g_mode, led_r_mode;
1764
1765 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
1766 if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
1767 rt2x00_set_field32(&reg, GPIO_SWITCH_0, 1);
1768 rt2x00_set_field32(&reg, GPIO_SWITCH_1, 1);
1769 } else {
1770 rt2x00_set_field32(&reg, GPIO_SWITCH_0, 0);
1771 rt2x00_set_field32(&reg, GPIO_SWITCH_1, 0);
1772 }
1773 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
1774
1775 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1776 led_g_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 3 : 0;
1777 led_r_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 0 : 3;
1778 if (led_g_mode != rt2x00_get_field32(reg, LED_CFG_G_LED_MODE) ||
1779 led_r_mode != rt2x00_get_field32(reg, LED_CFG_R_LED_MODE)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001780 rt2800_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001781 led_ctrl = rt2x00_get_field16(eeprom, EEPROM_FREQ_LED_MODE);
1782 if (led_ctrl == 0 || led_ctrl > 0x40) {
1783 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, led_g_mode);
1784 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, led_r_mode);
1785 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1786 } else {
1787 rt2800_mcu_request(rt2x00dev, MCU_BAND_SELECT, 0xff,
1788 (led_g_mode << 2) | led_r_mode, 1);
1789 }
1790 }
1791}
1792
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001793static void rt2800_set_ant_diversity(struct rt2x00_dev *rt2x00dev,
1794 enum antenna ant)
1795{
1796 u32 reg;
1797 u8 eesk_pin = (ant == ANTENNA_A) ? 1 : 0;
1798 u8 gpio_bit3 = (ant == ANTENNA_A) ? 0 : 1;
1799
1800 if (rt2x00_is_pci(rt2x00dev)) {
1801 rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
1802 rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK, eesk_pin);
1803 rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
1804 } else if (rt2x00_is_usb(rt2x00dev))
1805 rt2800_mcu_request(rt2x00dev, MCU_ANT_SELECT, 0xff,
1806 eesk_pin, 0);
1807
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02001808 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
1809 rt2x00_set_field32(&reg, GPIO_CTRL_DIR3, 0);
1810 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, gpio_bit3);
1811 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001812}
1813
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001814void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
1815{
1816 u8 r1;
1817 u8 r3;
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001818 u16 eeprom;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001819
1820 rt2800_bbp_read(rt2x00dev, 1, &r1);
1821 rt2800_bbp_read(rt2x00dev, 3, &r3);
1822
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001823 if (rt2x00_rt(rt2x00dev, RT3572) &&
Gabor Juhosc429dfe2013-10-11 13:18:42 +02001824 rt2x00_has_cap_bt_coexist(rt2x00dev))
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001825 rt2800_config_3572bt_ant(rt2x00dev);
1826
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001827 /*
1828 * Configure the TX antenna.
1829 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001830 switch (ant->tx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001831 case 1:
1832 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001833 break;
1834 case 2:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001835 if (rt2x00_rt(rt2x00dev, RT3572) &&
Gabor Juhosc429dfe2013-10-11 13:18:42 +02001836 rt2x00_has_cap_bt_coexist(rt2x00dev))
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001837 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 1);
1838 else
1839 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001840 break;
1841 case 3:
Gabor Juhos4788ac12013-07-08 16:08:21 +02001842 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001843 break;
1844 }
1845
1846 /*
1847 * Configure the RX antenna.
1848 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001849 switch (ant->rx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001850 case 1:
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001851 if (rt2x00_rt(rt2x00dev, RT3070) ||
1852 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03001853 rt2x00_rt(rt2x00dev, RT3352) ||
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001854 rt2x00_rt(rt2x00dev, RT3390)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001855 rt2800_eeprom_read(rt2x00dev,
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001856 EEPROM_NIC_CONF1, &eeprom);
1857 if (rt2x00_get_field16(eeprom,
1858 EEPROM_NIC_CONF1_ANT_DIVERSITY))
1859 rt2800_set_ant_diversity(rt2x00dev,
1860 rt2x00dev->default_ant.rx);
1861 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001862 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
1863 break;
1864 case 2:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001865 if (rt2x00_rt(rt2x00dev, RT3572) &&
Gabor Juhosc429dfe2013-10-11 13:18:42 +02001866 rt2x00_has_cap_bt_coexist(rt2x00dev)) {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02001867 rt2x00_set_field8(&r3, BBP3_RX_ADC, 1);
1868 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA,
1869 rt2x00dev->curr_band == IEEE80211_BAND_5GHZ);
1870 rt2800_set_ant_diversity(rt2x00dev, ANTENNA_B);
1871 } else {
1872 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
1873 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001874 break;
1875 case 3:
1876 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
1877 break;
1878 }
1879
1880 rt2800_bbp_write(rt2x00dev, 3, r3);
1881 rt2800_bbp_write(rt2x00dev, 1, r1);
Gabor Juhos5cddb3c2013-07-08 16:08:22 +02001882
1883 if (rt2x00_rt(rt2x00dev, RT3593)) {
1884 if (ant->rx_chain_num == 1)
1885 rt2800_bbp_write(rt2x00dev, 86, 0x00);
1886 else
1887 rt2800_bbp_write(rt2x00dev, 86, 0x46);
1888 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001889}
1890EXPORT_SYMBOL_GPL(rt2800_config_ant);
1891
1892static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
1893 struct rt2x00lib_conf *libconf)
1894{
1895 u16 eeprom;
1896 short lna_gain;
1897
1898 if (libconf->rf.channel <= 14) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001899 rt2800_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001900 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
1901 } else if (libconf->rf.channel <= 64) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02001902 rt2800_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001903 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
1904 } else if (libconf->rf.channel <= 128) {
Gabor Juhosf36bb0c2013-07-08 16:08:27 +02001905 if (rt2x00_rt(rt2x00dev, RT3593)) {
1906 rt2800_eeprom_read(rt2x00dev, EEPROM_EXT_LNA2, &eeprom);
1907 lna_gain = rt2x00_get_field16(eeprom,
1908 EEPROM_EXT_LNA2_A1);
1909 } else {
1910 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
1911 lna_gain = rt2x00_get_field16(eeprom,
1912 EEPROM_RSSI_BG2_LNA_A1);
1913 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001914 } else {
Gabor Juhosf36bb0c2013-07-08 16:08:27 +02001915 if (rt2x00_rt(rt2x00dev, RT3593)) {
1916 rt2800_eeprom_read(rt2x00dev, EEPROM_EXT_LNA2, &eeprom);
1917 lna_gain = rt2x00_get_field16(eeprom,
1918 EEPROM_EXT_LNA2_A2);
1919 } else {
1920 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
1921 lna_gain = rt2x00_get_field16(eeprom,
1922 EEPROM_RSSI_A2_LNA_A2);
1923 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001924 }
1925
1926 rt2x00dev->lna_gain = lna_gain;
1927}
1928
Gabor Juhos3f1b8732013-08-17 14:09:32 +02001929#define FREQ_OFFSET_BOUND 0x5f
1930
1931static void rt2800_adjust_freq_offset(struct rt2x00_dev *rt2x00dev)
1932{
1933 u8 freq_offset, prev_freq_offset;
1934 u8 rfcsr, prev_rfcsr;
1935
1936 freq_offset = rt2x00_get_field8(rt2x00dev->freq_offset, RFCSR17_CODE);
1937 freq_offset = min_t(u8, freq_offset, FREQ_OFFSET_BOUND);
1938
1939 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
1940 prev_rfcsr = rfcsr;
1941
1942 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, freq_offset);
1943 if (rfcsr == prev_rfcsr)
1944 return;
1945
1946 if (rt2x00_is_usb(rt2x00dev)) {
1947 rt2800_mcu_request(rt2x00dev, MCU_FREQ_OFFSET, 0xff,
1948 freq_offset, prev_rfcsr);
1949 return;
1950 }
1951
1952 prev_freq_offset = rt2x00_get_field8(prev_rfcsr, RFCSR17_CODE);
1953 while (prev_freq_offset != freq_offset) {
1954 if (prev_freq_offset < freq_offset)
1955 prev_freq_offset++;
1956 else
1957 prev_freq_offset--;
1958
1959 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, prev_freq_offset);
1960 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
1961
1962 usleep_range(1000, 1500);
1963 }
1964}
1965
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001966static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
1967 struct ieee80211_conf *conf,
1968 struct rf_channel *rf,
1969 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001970{
1971 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
1972
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001973 if (rt2x00dev->default_ant.tx_chain_num == 1)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001974 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
1975
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001976 if (rt2x00dev->default_ant.rx_chain_num == 1) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001977 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
1978 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001979 } else if (rt2x00dev->default_ant.rx_chain_num == 2)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001980 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1981
1982 if (rf->channel > 14) {
1983 /*
1984 * When TX power is below 0, we should increase it by 7 to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001985 * make it a positive value (Minimum value is -7).
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001986 * However this means that values between 0 and 7 have
1987 * double meaning, and we should set a 7DBm boost flag.
1988 */
1989 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001990 (info->default_power1 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001991
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001992 if (info->default_power1 < 0)
1993 info->default_power1 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001994
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001995 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001996
1997 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001998 (info->default_power2 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001999
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002000 if (info->default_power2 < 0)
2001 info->default_power2 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002002
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002003 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002004 } else {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002005 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G, info->default_power1);
2006 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002007 }
2008
2009 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
2010
2011 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
2012 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
2013 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
2014 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
2015
2016 udelay(200);
2017
2018 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
2019 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
2020 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
2021 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
2022
2023 udelay(200);
2024
2025 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
2026 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
2027 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
2028 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
2029}
2030
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02002031static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
2032 struct ieee80211_conf *conf,
2033 struct rf_channel *rf,
2034 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002035{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01002036 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01002037 u8 rfcsr, calib_tx, calib_rx;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002038
2039 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Stanislaw Gruszka7f4666a2012-01-30 16:17:56 +01002040
2041 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
2042 rt2x00_set_field8(&rfcsr, RFCSR3_K, rf->rf3);
2043 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002044
2045 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +02002046 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002047 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
2048
2049 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002050 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002051 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
2052
Helmut Schaa5a673962010-04-23 15:54:43 +02002053 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002054 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER, info->default_power2);
Helmut Schaa5a673962010-04-23 15:54:43 +02002055 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
2056
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01002057 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2058 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
Gertjan van Wingerde7ad63032012-09-16 22:29:53 +02002059 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD,
2060 rt2x00dev->default_ant.rx_chain_num <= 1);
2061 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD,
2062 rt2x00dev->default_ant.rx_chain_num <= 2);
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01002063 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
Gertjan van Wingerde7ad63032012-09-16 22:29:53 +02002064 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD,
2065 rt2x00dev->default_ant.tx_chain_num <= 1);
2066 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD,
2067 rt2x00dev->default_ant.tx_chain_num <= 2);
Stanislaw Gruszkae3bab192012-01-30 16:17:57 +01002068 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2069
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002070 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
2071 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
2072 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
2073
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01002074 if (rt2x00_rt(rt2x00dev, RT3390)) {
2075 calib_tx = conf_is_ht40(conf) ? 0x68 : 0x4f;
2076 calib_rx = conf_is_ht40(conf) ? 0x6f : 0x4f;
2077 } else {
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01002078 if (conf_is_ht40(conf)) {
2079 calib_tx = drv_data->calibration_bw40;
2080 calib_rx = drv_data->calibration_bw40;
2081 } else {
2082 calib_tx = drv_data->calibration_bw20;
2083 calib_rx = drv_data->calibration_bw20;
2084 }
Stanislaw Gruszkaf1f12f92012-01-30 16:17:59 +01002085 }
2086
2087 rt2800_rfcsr_read(rt2x00dev, 24, &rfcsr);
2088 rt2x00_set_field8(&rfcsr, RFCSR24_TX_CALIB, calib_tx);
2089 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr);
2090
2091 rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
2092 rt2x00_set_field8(&rfcsr, RFCSR31_RX_CALIB, calib_rx);
2093 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002094
Gertjan van Wingerde71976902010-03-24 21:42:36 +01002095 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002096 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +01002097 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Stanislaw Gruszka3e0c7642012-01-30 16:17:58 +01002098
2099 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2100 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
2101 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2102 msleep(1);
2103 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
2104 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002105}
2106
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002107static void rt2800_config_channel_rf3052(struct rt2x00_dev *rt2x00dev,
2108 struct ieee80211_conf *conf,
2109 struct rf_channel *rf,
2110 struct channel_info *info)
2111{
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01002112 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002113 u8 rfcsr;
2114 u32 reg;
2115
2116 if (rf->channel <= 14) {
Gertjan van Wingerde5d137df2012-02-06 23:45:09 +01002117 rt2800_bbp_write(rt2x00dev, 25, drv_data->bbp25);
2118 rt2800_bbp_write(rt2x00dev, 26, drv_data->bbp26);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002119 } else {
2120 rt2800_bbp_write(rt2x00dev, 25, 0x09);
2121 rt2800_bbp_write(rt2x00dev, 26, 0xff);
2122 }
2123
2124 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
2125 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
2126
2127 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
2128 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
2129 if (rf->channel <= 14)
2130 rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 2);
2131 else
2132 rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 1);
2133 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
2134
2135 rt2800_rfcsr_read(rt2x00dev, 5, &rfcsr);
2136 if (rf->channel <= 14)
2137 rt2x00_set_field8(&rfcsr, RFCSR5_R1, 1);
2138 else
2139 rt2x00_set_field8(&rfcsr, RFCSR5_R1, 2);
2140 rt2800_rfcsr_write(rt2x00dev, 5, rfcsr);
2141
2142 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
2143 if (rf->channel <= 14) {
2144 rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 3);
2145 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
Gertjan van Wingerde569ffa52012-02-06 23:45:10 +01002146 info->default_power1);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002147 } else {
2148 rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 7);
2149 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
2150 (info->default_power1 & 0x3) |
2151 ((info->default_power1 & 0xC) << 1));
2152 }
2153 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
2154
2155 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
2156 if (rf->channel <= 14) {
2157 rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 3);
2158 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
Gertjan van Wingerde569ffa52012-02-06 23:45:10 +01002159 info->default_power2);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002160 } else {
2161 rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 7);
2162 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
2163 (info->default_power2 & 0x3) |
2164 ((info->default_power2 & 0xC) << 1));
2165 }
2166 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
2167
2168 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002169 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2170 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2171 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
2172 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
Gertjan van Wingerde0cd461e2012-02-06 23:45:11 +01002173 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2174 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
Gabor Juhosc429dfe2013-10-11 13:18:42 +02002175 if (rt2x00_has_cap_bt_coexist(rt2x00dev)) {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002176 if (rf->channel <= 14) {
2177 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2178 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2179 }
2180 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
2181 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
2182 } else {
2183 switch (rt2x00dev->default_ant.tx_chain_num) {
2184 case 1:
2185 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2186 case 2:
2187 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
2188 break;
2189 }
2190
2191 switch (rt2x00dev->default_ant.rx_chain_num) {
2192 case 1:
2193 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2194 case 2:
2195 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
2196 break;
2197 }
2198 }
2199 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2200
2201 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
2202 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
2203 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
2204
Gertjan van Wingerde3a1c0122012-02-06 23:45:07 +01002205 if (conf_is_ht40(conf)) {
2206 rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw40);
2207 rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw40);
2208 } else {
2209 rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw20);
2210 rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw20);
2211 }
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002212
2213 if (rf->channel <= 14) {
2214 rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
2215 rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
2216 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
2217 rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
2218 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01002219 rfcsr = 0x4c;
2220 rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
2221 drv_data->txmixer_gain_24g);
2222 rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002223 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
2224 rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
2225 rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
2226 rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
2227 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
2228 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
2229 rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
2230 } else {
Gertjan van Wingerde58b8ae12012-02-06 23:45:12 +01002231 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
2232 rt2x00_set_field8(&rfcsr, RFCSR7_BIT2, 1);
2233 rt2x00_set_field8(&rfcsr, RFCSR7_BIT3, 0);
2234 rt2x00_set_field8(&rfcsr, RFCSR7_BIT4, 1);
2235 rt2x00_set_field8(&rfcsr, RFCSR7_BITS67, 0);
2236 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002237 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
2238 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
2239 rt2800_rfcsr_write(rt2x00dev, 11, 0x00);
2240 rt2800_rfcsr_write(rt2x00dev, 15, 0x43);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01002241 rfcsr = 0x7a;
2242 rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
2243 drv_data->txmixer_gain_5g);
2244 rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002245 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
2246 if (rf->channel <= 64) {
2247 rt2800_rfcsr_write(rt2x00dev, 19, 0xb7);
2248 rt2800_rfcsr_write(rt2x00dev, 20, 0xf6);
2249 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
2250 } else if (rf->channel <= 128) {
2251 rt2800_rfcsr_write(rt2x00dev, 19, 0x74);
2252 rt2800_rfcsr_write(rt2x00dev, 20, 0xf4);
2253 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
2254 } else {
2255 rt2800_rfcsr_write(rt2x00dev, 19, 0x72);
2256 rt2800_rfcsr_write(rt2x00dev, 20, 0xf3);
2257 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
2258 }
2259 rt2800_rfcsr_write(rt2x00dev, 26, 0x87);
2260 rt2800_rfcsr_write(rt2x00dev, 27, 0x01);
2261 rt2800_rfcsr_write(rt2x00dev, 29, 0x9f);
2262 }
2263
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02002264 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
2265 rt2x00_set_field32(&reg, GPIO_CTRL_DIR7, 0);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002266 if (rf->channel <= 14)
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02002267 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 1);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002268 else
Gertjan van Wingerde99bdf512012-08-31 19:22:13 +02002269 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 0);
2270 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02002271
2272 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
2273 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
2274 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
2275}
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002276
Gabor Juhosf42b0462013-07-08 16:08:30 +02002277static void rt2800_config_channel_rf3053(struct rt2x00_dev *rt2x00dev,
2278 struct ieee80211_conf *conf,
2279 struct rf_channel *rf,
2280 struct channel_info *info)
2281{
2282 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
2283 u8 txrx_agc_fc;
2284 u8 txrx_h20m;
2285 u8 rfcsr;
2286 u8 bbp;
2287 const bool txbf_enabled = false; /* TODO */
2288
2289 /* TODO: use TX{0,1,2}FinePowerControl values from EEPROM */
2290 rt2800_bbp_read(rt2x00dev, 109, &bbp);
2291 rt2x00_set_field8(&bbp, BBP109_TX0_POWER, 0);
2292 rt2x00_set_field8(&bbp, BBP109_TX1_POWER, 0);
2293 rt2800_bbp_write(rt2x00dev, 109, bbp);
2294
2295 rt2800_bbp_read(rt2x00dev, 110, &bbp);
2296 rt2x00_set_field8(&bbp, BBP110_TX2_POWER, 0);
2297 rt2800_bbp_write(rt2x00dev, 110, bbp);
2298
2299 if (rf->channel <= 14) {
2300 /* Restore BBP 25 & 26 for 2.4 GHz */
2301 rt2800_bbp_write(rt2x00dev, 25, drv_data->bbp25);
2302 rt2800_bbp_write(rt2x00dev, 26, drv_data->bbp26);
2303 } else {
2304 /* Hard code BBP 25 & 26 for 5GHz */
2305
2306 /* Enable IQ Phase correction */
2307 rt2800_bbp_write(rt2x00dev, 25, 0x09);
2308 /* Setup IQ Phase correction value */
2309 rt2800_bbp_write(rt2x00dev, 26, 0xff);
2310 }
2311
2312 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2313 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3 & 0xf);
2314
2315 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2316 rt2x00_set_field8(&rfcsr, RFCSR11_R, (rf->rf2 & 0x3));
2317 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2318
2319 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2320 rt2x00_set_field8(&rfcsr, RFCSR11_PLL_IDOH, 1);
2321 if (rf->channel <= 14)
2322 rt2x00_set_field8(&rfcsr, RFCSR11_PLL_MOD, 1);
2323 else
2324 rt2x00_set_field8(&rfcsr, RFCSR11_PLL_MOD, 2);
2325 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2326
2327 rt2800_rfcsr_read(rt2x00dev, 53, &rfcsr);
2328 if (rf->channel <= 14) {
2329 rfcsr = 0;
2330 rt2x00_set_field8(&rfcsr, RFCSR53_TX_POWER,
2331 info->default_power1 & 0x1f);
2332 } else {
2333 if (rt2x00_is_usb(rt2x00dev))
2334 rfcsr = 0x40;
2335
2336 rt2x00_set_field8(&rfcsr, RFCSR53_TX_POWER,
2337 ((info->default_power1 & 0x18) << 1) |
2338 (info->default_power1 & 7));
2339 }
2340 rt2800_rfcsr_write(rt2x00dev, 53, rfcsr);
2341
2342 rt2800_rfcsr_read(rt2x00dev, 55, &rfcsr);
2343 if (rf->channel <= 14) {
2344 rfcsr = 0;
2345 rt2x00_set_field8(&rfcsr, RFCSR55_TX_POWER,
2346 info->default_power2 & 0x1f);
2347 } else {
2348 if (rt2x00_is_usb(rt2x00dev))
2349 rfcsr = 0x40;
2350
2351 rt2x00_set_field8(&rfcsr, RFCSR55_TX_POWER,
2352 ((info->default_power2 & 0x18) << 1) |
2353 (info->default_power2 & 7));
2354 }
2355 rt2800_rfcsr_write(rt2x00dev, 55, rfcsr);
2356
2357 rt2800_rfcsr_read(rt2x00dev, 54, &rfcsr);
2358 if (rf->channel <= 14) {
2359 rfcsr = 0;
2360 rt2x00_set_field8(&rfcsr, RFCSR54_TX_POWER,
2361 info->default_power3 & 0x1f);
2362 } else {
2363 if (rt2x00_is_usb(rt2x00dev))
2364 rfcsr = 0x40;
2365
2366 rt2x00_set_field8(&rfcsr, RFCSR54_TX_POWER,
2367 ((info->default_power3 & 0x18) << 1) |
2368 (info->default_power3 & 7));
2369 }
2370 rt2800_rfcsr_write(rt2x00dev, 54, rfcsr);
2371
2372 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2373 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2374 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2375 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
2376 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
2377 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2378 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
2379 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2380 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
2381
2382 switch (rt2x00dev->default_ant.tx_chain_num) {
2383 case 3:
2384 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
2385 /* fallthrough */
2386 case 2:
2387 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2388 /* fallthrough */
2389 case 1:
2390 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2391 break;
2392 }
2393
2394 switch (rt2x00dev->default_ant.rx_chain_num) {
2395 case 3:
2396 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
2397 /* fallthrough */
2398 case 2:
2399 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2400 /* fallthrough */
2401 case 1:
2402 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2403 break;
2404 }
2405 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2406
Gabor Juhose979a8a2013-08-17 14:09:33 +02002407 rt2800_adjust_freq_offset(rt2x00dev);
Gabor Juhosf42b0462013-07-08 16:08:30 +02002408
2409 if (conf_is_ht40(conf)) {
2410 txrx_agc_fc = rt2x00_get_field8(drv_data->calibration_bw40,
2411 RFCSR24_TX_AGC_FC);
2412 txrx_h20m = rt2x00_get_field8(drv_data->calibration_bw40,
2413 RFCSR24_TX_H20M);
2414 } else {
2415 txrx_agc_fc = rt2x00_get_field8(drv_data->calibration_bw20,
2416 RFCSR24_TX_AGC_FC);
2417 txrx_h20m = rt2x00_get_field8(drv_data->calibration_bw20,
2418 RFCSR24_TX_H20M);
2419 }
2420
2421 /* NOTE: the reference driver does not writes the new value
2422 * back to RFCSR 32
2423 */
2424 rt2800_rfcsr_read(rt2x00dev, 32, &rfcsr);
2425 rt2x00_set_field8(&rfcsr, RFCSR32_TX_AGC_FC, txrx_agc_fc);
2426
2427 if (rf->channel <= 14)
2428 rfcsr = 0xa0;
2429 else
2430 rfcsr = 0x80;
2431 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
2432
2433 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2434 rt2x00_set_field8(&rfcsr, RFCSR30_TX_H20M, txrx_h20m);
2435 rt2x00_set_field8(&rfcsr, RFCSR30_RX_H20M, txrx_h20m);
2436 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2437
2438 /* Band selection */
2439 rt2800_rfcsr_read(rt2x00dev, 36, &rfcsr);
2440 if (rf->channel <= 14)
2441 rt2x00_set_field8(&rfcsr, RFCSR36_RF_BS, 1);
2442 else
2443 rt2x00_set_field8(&rfcsr, RFCSR36_RF_BS, 0);
2444 rt2800_rfcsr_write(rt2x00dev, 36, rfcsr);
2445
2446 rt2800_rfcsr_read(rt2x00dev, 34, &rfcsr);
2447 if (rf->channel <= 14)
2448 rfcsr = 0x3c;
2449 else
2450 rfcsr = 0x20;
2451 rt2800_rfcsr_write(rt2x00dev, 34, rfcsr);
2452
2453 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
2454 if (rf->channel <= 14)
2455 rfcsr = 0x1a;
2456 else
2457 rfcsr = 0x12;
2458 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
2459
2460 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
2461 if (rf->channel >= 1 && rf->channel <= 14)
2462 rt2x00_set_field8(&rfcsr, RFCSR6_VCO_IC, 1);
2463 else if (rf->channel >= 36 && rf->channel <= 64)
2464 rt2x00_set_field8(&rfcsr, RFCSR6_VCO_IC, 2);
2465 else if (rf->channel >= 100 && rf->channel <= 128)
2466 rt2x00_set_field8(&rfcsr, RFCSR6_VCO_IC, 2);
2467 else
2468 rt2x00_set_field8(&rfcsr, RFCSR6_VCO_IC, 1);
2469 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
2470
2471 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2472 rt2x00_set_field8(&rfcsr, RFCSR30_RX_VCM, 2);
2473 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2474
2475 rt2800_rfcsr_write(rt2x00dev, 46, 0x60);
2476
2477 if (rf->channel <= 14) {
2478 rt2800_rfcsr_write(rt2x00dev, 10, 0xd3);
2479 rt2800_rfcsr_write(rt2x00dev, 13, 0x12);
2480 } else {
2481 rt2800_rfcsr_write(rt2x00dev, 10, 0xd8);
2482 rt2800_rfcsr_write(rt2x00dev, 13, 0x23);
2483 }
2484
2485 rt2800_rfcsr_read(rt2x00dev, 51, &rfcsr);
2486 rt2x00_set_field8(&rfcsr, RFCSR51_BITS01, 1);
2487 rt2800_rfcsr_write(rt2x00dev, 51, rfcsr);
2488
2489 rt2800_rfcsr_read(rt2x00dev, 51, &rfcsr);
2490 if (rf->channel <= 14) {
2491 rt2x00_set_field8(&rfcsr, RFCSR51_BITS24, 5);
2492 rt2x00_set_field8(&rfcsr, RFCSR51_BITS57, 3);
2493 } else {
2494 rt2x00_set_field8(&rfcsr, RFCSR51_BITS24, 4);
2495 rt2x00_set_field8(&rfcsr, RFCSR51_BITS57, 2);
2496 }
2497 rt2800_rfcsr_write(rt2x00dev, 51, rfcsr);
2498
2499 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
2500 if (rf->channel <= 14)
2501 rt2x00_set_field8(&rfcsr, RFCSR49_TX_LO1_IC, 3);
2502 else
2503 rt2x00_set_field8(&rfcsr, RFCSR49_TX_LO1_IC, 2);
2504
2505 if (txbf_enabled)
2506 rt2x00_set_field8(&rfcsr, RFCSR49_TX_DIV, 1);
2507
2508 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
2509
2510 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
2511 rt2x00_set_field8(&rfcsr, RFCSR50_TX_LO1_EN, 0);
2512 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
2513
2514 rt2800_rfcsr_read(rt2x00dev, 57, &rfcsr);
2515 if (rf->channel <= 14)
2516 rt2x00_set_field8(&rfcsr, RFCSR57_DRV_CC, 0x1b);
2517 else
2518 rt2x00_set_field8(&rfcsr, RFCSR57_DRV_CC, 0x0f);
2519 rt2800_rfcsr_write(rt2x00dev, 57, rfcsr);
2520
2521 if (rf->channel <= 14) {
2522 rt2800_rfcsr_write(rt2x00dev, 44, 0x93);
2523 rt2800_rfcsr_write(rt2x00dev, 52, 0x45);
2524 } else {
2525 rt2800_rfcsr_write(rt2x00dev, 44, 0x9b);
2526 rt2800_rfcsr_write(rt2x00dev, 52, 0x05);
2527 }
2528
2529 /* Initiate VCO calibration */
2530 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
2531 if (rf->channel <= 14) {
2532 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
2533 } else {
2534 rt2x00_set_field8(&rfcsr, RFCSR3_BIT1, 1);
2535 rt2x00_set_field8(&rfcsr, RFCSR3_BIT2, 1);
2536 rt2x00_set_field8(&rfcsr, RFCSR3_BIT3, 1);
2537 rt2x00_set_field8(&rfcsr, RFCSR3_BIT4, 1);
2538 rt2x00_set_field8(&rfcsr, RFCSR3_BIT5, 1);
2539 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
2540 }
2541 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
2542
2543 if (rf->channel >= 1 && rf->channel <= 14) {
2544 rfcsr = 0x23;
2545 if (txbf_enabled)
2546 rt2x00_set_field8(&rfcsr, RFCSR39_RX_DIV, 1);
2547 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
2548
2549 rt2800_rfcsr_write(rt2x00dev, 45, 0xbb);
2550 } else if (rf->channel >= 36 && rf->channel <= 64) {
2551 rfcsr = 0x36;
2552 if (txbf_enabled)
2553 rt2x00_set_field8(&rfcsr, RFCSR39_RX_DIV, 1);
2554 rt2800_rfcsr_write(rt2x00dev, 39, 0x36);
2555
2556 rt2800_rfcsr_write(rt2x00dev, 45, 0xeb);
2557 } else if (rf->channel >= 100 && rf->channel <= 128) {
2558 rfcsr = 0x32;
2559 if (txbf_enabled)
2560 rt2x00_set_field8(&rfcsr, RFCSR39_RX_DIV, 1);
2561 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
2562
2563 rt2800_rfcsr_write(rt2x00dev, 45, 0xb3);
2564 } else {
2565 rfcsr = 0x30;
2566 if (txbf_enabled)
2567 rt2x00_set_field8(&rfcsr, RFCSR39_RX_DIV, 1);
2568 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
2569
2570 rt2800_rfcsr_write(rt2x00dev, 45, 0x9b);
2571 }
2572}
2573
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002574#define POWER_BOUND 0x27
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002575#define POWER_BOUND_5G 0x2b
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002576
Woody Hunga89534e2012-06-13 15:01:16 +08002577static void rt2800_config_channel_rf3290(struct rt2x00_dev *rt2x00dev,
2578 struct ieee80211_conf *conf,
2579 struct rf_channel *rf,
2580 struct channel_info *info)
2581{
2582 u8 rfcsr;
2583
2584 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2585 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2586 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2587 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
2588 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2589
2590 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002591 if (info->default_power1 > POWER_BOUND)
2592 rt2x00_set_field8(&rfcsr, RFCSR49_TX, POWER_BOUND);
Woody Hunga89534e2012-06-13 15:01:16 +08002593 else
2594 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2595 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
2596
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002597 rt2800_adjust_freq_offset(rt2x00dev);
Woody Hunga89534e2012-06-13 15:01:16 +08002598
2599 if (rf->channel <= 14) {
2600 if (rf->channel == 6)
2601 rt2800_bbp_write(rt2x00dev, 68, 0x0c);
2602 else
2603 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
2604
2605 if (rf->channel >= 1 && rf->channel <= 6)
2606 rt2800_bbp_write(rt2x00dev, 59, 0x0f);
2607 else if (rf->channel >= 7 && rf->channel <= 11)
2608 rt2800_bbp_write(rt2x00dev, 59, 0x0e);
2609 else if (rf->channel >= 12 && rf->channel <= 14)
2610 rt2800_bbp_write(rt2x00dev, 59, 0x0d);
2611 }
2612}
2613
Daniel Golle03839952012-09-09 14:24:39 +03002614static void rt2800_config_channel_rf3322(struct rt2x00_dev *rt2x00dev,
2615 struct ieee80211_conf *conf,
2616 struct rf_channel *rf,
2617 struct channel_info *info)
2618{
2619 u8 rfcsr;
2620
2621 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2622 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2623
2624 rt2800_rfcsr_write(rt2x00dev, 11, 0x42);
2625 rt2800_rfcsr_write(rt2x00dev, 12, 0x1c);
2626 rt2800_rfcsr_write(rt2x00dev, 13, 0x00);
2627
2628 if (info->default_power1 > POWER_BOUND)
2629 rt2800_rfcsr_write(rt2x00dev, 47, POWER_BOUND);
2630 else
2631 rt2800_rfcsr_write(rt2x00dev, 47, info->default_power1);
2632
2633 if (info->default_power2 > POWER_BOUND)
2634 rt2800_rfcsr_write(rt2x00dev, 48, POWER_BOUND);
2635 else
2636 rt2800_rfcsr_write(rt2x00dev, 48, info->default_power2);
2637
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002638 rt2800_adjust_freq_offset(rt2x00dev);
Daniel Golle03839952012-09-09 14:24:39 +03002639
2640 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2641 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2642 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2643
2644 if ( rt2x00dev->default_ant.tx_chain_num == 2 )
2645 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2646 else
2647 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
2648
2649 if ( rt2x00dev->default_ant.rx_chain_num == 2 )
2650 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2651 else
2652 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
2653
2654 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2655 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
2656
2657 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2658
2659 rt2800_rfcsr_write(rt2x00dev, 31, 80);
2660}
2661
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002662static void rt2800_config_channel_rf53xx(struct rt2x00_dev *rt2x00dev,
Gabor Juhosadde5882011-03-03 11:46:45 +01002663 struct ieee80211_conf *conf,
2664 struct rf_channel *rf,
2665 struct channel_info *info)
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002666{
Gabor Juhosadde5882011-03-03 11:46:45 +01002667 u8 rfcsr;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002668
Gabor Juhosadde5882011-03-03 11:46:45 +01002669 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
2670 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
2671 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2672 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
2673 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002674
Gabor Juhosadde5882011-03-03 11:46:45 +01002675 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002676 if (info->default_power1 > POWER_BOUND)
2677 rt2x00_set_field8(&rfcsr, RFCSR49_TX, POWER_BOUND);
Gabor Juhosadde5882011-03-03 11:46:45 +01002678 else
2679 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2680 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002681
Zero.Lincff3d1f2012-05-29 16:11:09 +08002682 if (rt2x00_rt(rt2x00dev, RT5392)) {
2683 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
Felipe Pena62649952013-10-18 21:20:42 -03002684 if (info->default_power2 > POWER_BOUND)
Stanislaw Gruszka7573cb52012-07-09 14:41:48 +02002685 rt2x00_set_field8(&rfcsr, RFCSR50_TX, POWER_BOUND);
Zero.Lincff3d1f2012-05-29 16:11:09 +08002686 else
2687 rt2x00_set_field8(&rfcsr, RFCSR50_TX,
2688 info->default_power2);
2689 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
2690 }
2691
Gabor Juhosadde5882011-03-03 11:46:45 +01002692 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
Zero.Lincff3d1f2012-05-29 16:11:09 +08002693 if (rt2x00_rt(rt2x00dev, RT5392)) {
2694 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2695 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2696 }
Gabor Juhosadde5882011-03-03 11:46:45 +01002697 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2698 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
2699 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
2700 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
2701 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002702
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002703 rt2800_adjust_freq_offset(rt2x00dev);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002704
Gabor Juhosadde5882011-03-03 11:46:45 +01002705 if (rf->channel <= 14) {
2706 int idx = rf->channel-1;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002707
Gabor Juhosc429dfe2013-10-11 13:18:42 +02002708 if (rt2x00_has_cap_bt_coexist(rt2x00dev)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002709 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
2710 /* r55/r59 value array of channel 1~14 */
2711 static const char r55_bt_rev[] = {0x83, 0x83,
2712 0x83, 0x73, 0x73, 0x63, 0x53, 0x53,
2713 0x53, 0x43, 0x43, 0x43, 0x43, 0x43};
2714 static const char r59_bt_rev[] = {0x0e, 0x0e,
2715 0x0e, 0x0e, 0x0e, 0x0b, 0x0a, 0x09,
2716 0x07, 0x07, 0x07, 0x07, 0x07, 0x07};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002717
Gabor Juhosadde5882011-03-03 11:46:45 +01002718 rt2800_rfcsr_write(rt2x00dev, 55,
2719 r55_bt_rev[idx]);
2720 rt2800_rfcsr_write(rt2x00dev, 59,
2721 r59_bt_rev[idx]);
2722 } else {
2723 static const char r59_bt[] = {0x8b, 0x8b, 0x8b,
2724 0x8b, 0x8b, 0x8b, 0x8b, 0x8a, 0x89,
2725 0x88, 0x88, 0x86, 0x85, 0x84};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002726
Gabor Juhosadde5882011-03-03 11:46:45 +01002727 rt2800_rfcsr_write(rt2x00dev, 59, r59_bt[idx]);
2728 }
2729 } else {
2730 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
2731 static const char r55_nonbt_rev[] = {0x23, 0x23,
2732 0x23, 0x23, 0x13, 0x13, 0x03, 0x03,
2733 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
2734 static const char r59_nonbt_rev[] = {0x07, 0x07,
2735 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
2736 0x07, 0x07, 0x06, 0x05, 0x04, 0x04};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002737
Gabor Juhosadde5882011-03-03 11:46:45 +01002738 rt2800_rfcsr_write(rt2x00dev, 55,
2739 r55_nonbt_rev[idx]);
2740 rt2800_rfcsr_write(rt2x00dev, 59,
2741 r59_nonbt_rev[idx]);
John Li2ed71882012-02-17 17:33:06 +08002742 } else if (rt2x00_rt(rt2x00dev, RT5390) ||
Gabor Juhose6d227b2012-12-02 15:53:28 +01002743 rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002744 static const char r59_non_bt[] = {0x8f, 0x8f,
2745 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8d,
2746 0x8a, 0x88, 0x88, 0x87, 0x87, 0x86};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002747
Gabor Juhosadde5882011-03-03 11:46:45 +01002748 rt2800_rfcsr_write(rt2x00dev, 59,
2749 r59_non_bt[idx]);
2750 }
2751 }
2752 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002753}
2754
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002755static void rt2800_config_channel_rf55xx(struct rt2x00_dev *rt2x00dev,
2756 struct ieee80211_conf *conf,
2757 struct rf_channel *rf,
2758 struct channel_info *info)
2759{
2760 u8 rfcsr, ep_reg;
Stanislaw Gruszkad5ae7a62013-03-16 19:19:42 +01002761 u32 reg;
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002762 int power_bound;
2763
2764 /* TODO */
2765 const bool is_11b = false;
2766 const bool is_type_ep = false;
2767
Stanislaw Gruszkad5ae7a62013-03-16 19:19:42 +01002768 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
2769 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL,
2770 (rf->channel > 14 || conf_is_ht40(conf)) ? 5 : 0);
2771 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002772
2773 /* Order of values on rf_channel entry: N, K, mod, R */
2774 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1 & 0xff);
2775
2776 rt2800_rfcsr_read(rt2x00dev, 9, &rfcsr);
2777 rt2x00_set_field8(&rfcsr, RFCSR9_K, rf->rf2 & 0xf);
2778 rt2x00_set_field8(&rfcsr, RFCSR9_N, (rf->rf1 & 0x100) >> 8);
2779 rt2x00_set_field8(&rfcsr, RFCSR9_MOD, ((rf->rf3 - 8) & 0x4) >> 2);
2780 rt2800_rfcsr_write(rt2x00dev, 9, rfcsr);
2781
2782 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
2783 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf4 - 1);
2784 rt2x00_set_field8(&rfcsr, RFCSR11_MOD, (rf->rf3 - 8) & 0x3);
2785 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
2786
2787 if (rf->channel <= 14) {
2788 rt2800_rfcsr_write(rt2x00dev, 10, 0x90);
2789 /* FIXME: RF11 owerwrite ? */
2790 rt2800_rfcsr_write(rt2x00dev, 11, 0x4A);
2791 rt2800_rfcsr_write(rt2x00dev, 12, 0x52);
2792 rt2800_rfcsr_write(rt2x00dev, 13, 0x42);
2793 rt2800_rfcsr_write(rt2x00dev, 22, 0x40);
2794 rt2800_rfcsr_write(rt2x00dev, 24, 0x4A);
2795 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
2796 rt2800_rfcsr_write(rt2x00dev, 27, 0x42);
2797 rt2800_rfcsr_write(rt2x00dev, 36, 0x80);
2798 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
2799 rt2800_rfcsr_write(rt2x00dev, 38, 0x89);
2800 rt2800_rfcsr_write(rt2x00dev, 39, 0x1B);
2801 rt2800_rfcsr_write(rt2x00dev, 40, 0x0D);
2802 rt2800_rfcsr_write(rt2x00dev, 41, 0x9B);
2803 rt2800_rfcsr_write(rt2x00dev, 42, 0xD5);
2804 rt2800_rfcsr_write(rt2x00dev, 43, 0x72);
2805 rt2800_rfcsr_write(rt2x00dev, 44, 0x0E);
2806 rt2800_rfcsr_write(rt2x00dev, 45, 0xA2);
2807 rt2800_rfcsr_write(rt2x00dev, 46, 0x6B);
2808 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
2809 rt2800_rfcsr_write(rt2x00dev, 51, 0x3E);
2810 rt2800_rfcsr_write(rt2x00dev, 52, 0x48);
2811 rt2800_rfcsr_write(rt2x00dev, 54, 0x38);
2812 rt2800_rfcsr_write(rt2x00dev, 56, 0xA1);
2813 rt2800_rfcsr_write(rt2x00dev, 57, 0x00);
2814 rt2800_rfcsr_write(rt2x00dev, 58, 0x39);
2815 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
2816 rt2800_rfcsr_write(rt2x00dev, 61, 0x91);
2817 rt2800_rfcsr_write(rt2x00dev, 62, 0x39);
2818
2819 /* TODO RF27 <- tssi */
2820
2821 rfcsr = rf->channel <= 10 ? 0x07 : 0x06;
2822 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
2823 rt2800_rfcsr_write(rt2x00dev, 59, rfcsr);
2824
2825 if (is_11b) {
2826 /* CCK */
2827 rt2800_rfcsr_write(rt2x00dev, 31, 0xF8);
2828 rt2800_rfcsr_write(rt2x00dev, 32, 0xC0);
2829 if (is_type_ep)
2830 rt2800_rfcsr_write(rt2x00dev, 55, 0x06);
2831 else
2832 rt2800_rfcsr_write(rt2x00dev, 55, 0x47);
2833 } else {
2834 /* OFDM */
2835 if (is_type_ep)
2836 rt2800_rfcsr_write(rt2x00dev, 55, 0x03);
2837 else
2838 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
2839 }
2840
2841 power_bound = POWER_BOUND;
2842 ep_reg = 0x2;
2843 } else {
2844 rt2800_rfcsr_write(rt2x00dev, 10, 0x97);
2845 /* FIMXE: RF11 overwrite */
2846 rt2800_rfcsr_write(rt2x00dev, 11, 0x40);
2847 rt2800_rfcsr_write(rt2x00dev, 25, 0xBF);
2848 rt2800_rfcsr_write(rt2x00dev, 27, 0x42);
2849 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
2850 rt2800_rfcsr_write(rt2x00dev, 37, 0x04);
2851 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
2852 rt2800_rfcsr_write(rt2x00dev, 40, 0x42);
2853 rt2800_rfcsr_write(rt2x00dev, 41, 0xBB);
2854 rt2800_rfcsr_write(rt2x00dev, 42, 0xD7);
2855 rt2800_rfcsr_write(rt2x00dev, 45, 0x41);
2856 rt2800_rfcsr_write(rt2x00dev, 48, 0x00);
2857 rt2800_rfcsr_write(rt2x00dev, 57, 0x77);
2858 rt2800_rfcsr_write(rt2x00dev, 60, 0x05);
2859 rt2800_rfcsr_write(rt2x00dev, 61, 0x01);
2860
2861 /* TODO RF27 <- tssi */
2862
2863 if (rf->channel >= 36 && rf->channel <= 64) {
2864
2865 rt2800_rfcsr_write(rt2x00dev, 12, 0x2E);
2866 rt2800_rfcsr_write(rt2x00dev, 13, 0x22);
2867 rt2800_rfcsr_write(rt2x00dev, 22, 0x60);
2868 rt2800_rfcsr_write(rt2x00dev, 23, 0x7F);
2869 if (rf->channel <= 50)
2870 rt2800_rfcsr_write(rt2x00dev, 24, 0x09);
2871 else if (rf->channel >= 52)
2872 rt2800_rfcsr_write(rt2x00dev, 24, 0x07);
2873 rt2800_rfcsr_write(rt2x00dev, 39, 0x1C);
2874 rt2800_rfcsr_write(rt2x00dev, 43, 0x5B);
2875 rt2800_rfcsr_write(rt2x00dev, 44, 0X40);
2876 rt2800_rfcsr_write(rt2x00dev, 46, 0X00);
2877 rt2800_rfcsr_write(rt2x00dev, 51, 0xFE);
2878 rt2800_rfcsr_write(rt2x00dev, 52, 0x0C);
2879 rt2800_rfcsr_write(rt2x00dev, 54, 0xF8);
2880 if (rf->channel <= 50) {
2881 rt2800_rfcsr_write(rt2x00dev, 55, 0x06),
2882 rt2800_rfcsr_write(rt2x00dev, 56, 0xD3);
2883 } else if (rf->channel >= 52) {
2884 rt2800_rfcsr_write(rt2x00dev, 55, 0x04);
2885 rt2800_rfcsr_write(rt2x00dev, 56, 0xBB);
2886 }
2887
2888 rt2800_rfcsr_write(rt2x00dev, 58, 0x15);
2889 rt2800_rfcsr_write(rt2x00dev, 59, 0x7F);
2890 rt2800_rfcsr_write(rt2x00dev, 62, 0x15);
2891
2892 } else if (rf->channel >= 100 && rf->channel <= 165) {
2893
2894 rt2800_rfcsr_write(rt2x00dev, 12, 0x0E);
2895 rt2800_rfcsr_write(rt2x00dev, 13, 0x42);
2896 rt2800_rfcsr_write(rt2x00dev, 22, 0x40);
2897 if (rf->channel <= 153) {
2898 rt2800_rfcsr_write(rt2x00dev, 23, 0x3C);
2899 rt2800_rfcsr_write(rt2x00dev, 24, 0x06);
2900 } else if (rf->channel >= 155) {
2901 rt2800_rfcsr_write(rt2x00dev, 23, 0x38);
2902 rt2800_rfcsr_write(rt2x00dev, 24, 0x05);
2903 }
2904 if (rf->channel <= 138) {
2905 rt2800_rfcsr_write(rt2x00dev, 39, 0x1A);
2906 rt2800_rfcsr_write(rt2x00dev, 43, 0x3B);
2907 rt2800_rfcsr_write(rt2x00dev, 44, 0x20);
2908 rt2800_rfcsr_write(rt2x00dev, 46, 0x18);
2909 } else if (rf->channel >= 140) {
2910 rt2800_rfcsr_write(rt2x00dev, 39, 0x18);
2911 rt2800_rfcsr_write(rt2x00dev, 43, 0x1B);
2912 rt2800_rfcsr_write(rt2x00dev, 44, 0x10);
2913 rt2800_rfcsr_write(rt2x00dev, 46, 0X08);
2914 }
2915 if (rf->channel <= 124)
2916 rt2800_rfcsr_write(rt2x00dev, 51, 0xFC);
2917 else if (rf->channel >= 126)
2918 rt2800_rfcsr_write(rt2x00dev, 51, 0xEC);
2919 if (rf->channel <= 138)
2920 rt2800_rfcsr_write(rt2x00dev, 52, 0x06);
2921 else if (rf->channel >= 140)
2922 rt2800_rfcsr_write(rt2x00dev, 52, 0x06);
2923 rt2800_rfcsr_write(rt2x00dev, 54, 0xEB);
2924 if (rf->channel <= 138)
2925 rt2800_rfcsr_write(rt2x00dev, 55, 0x01);
2926 else if (rf->channel >= 140)
2927 rt2800_rfcsr_write(rt2x00dev, 55, 0x00);
2928 if (rf->channel <= 128)
2929 rt2800_rfcsr_write(rt2x00dev, 56, 0xBB);
2930 else if (rf->channel >= 130)
2931 rt2800_rfcsr_write(rt2x00dev, 56, 0xAB);
2932 if (rf->channel <= 116)
2933 rt2800_rfcsr_write(rt2x00dev, 58, 0x1D);
2934 else if (rf->channel >= 118)
2935 rt2800_rfcsr_write(rt2x00dev, 58, 0x15);
2936 if (rf->channel <= 138)
2937 rt2800_rfcsr_write(rt2x00dev, 59, 0x3F);
2938 else if (rf->channel >= 140)
2939 rt2800_rfcsr_write(rt2x00dev, 59, 0x7C);
2940 if (rf->channel <= 116)
2941 rt2800_rfcsr_write(rt2x00dev, 62, 0x1D);
2942 else if (rf->channel >= 118)
2943 rt2800_rfcsr_write(rt2x00dev, 62, 0x15);
2944 }
2945
2946 power_bound = POWER_BOUND_5G;
2947 ep_reg = 0x3;
2948 }
2949
2950 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
2951 if (info->default_power1 > power_bound)
2952 rt2x00_set_field8(&rfcsr, RFCSR49_TX, power_bound);
2953 else
2954 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
2955 if (is_type_ep)
2956 rt2x00_set_field8(&rfcsr, RFCSR49_EP, ep_reg);
2957 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
2958
2959 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
Gabor Juhos0847beb2013-06-25 22:57:29 +02002960 if (info->default_power2 > power_bound)
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002961 rt2x00_set_field8(&rfcsr, RFCSR50_TX, power_bound);
2962 else
2963 rt2x00_set_field8(&rfcsr, RFCSR50_TX, info->default_power2);
2964 if (is_type_ep)
2965 rt2x00_set_field8(&rfcsr, RFCSR50_EP, ep_reg);
2966 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
2967
2968 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2969 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2970 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
2971
2972 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD,
2973 rt2x00dev->default_ant.tx_chain_num >= 1);
2974 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD,
2975 rt2x00dev->default_ant.tx_chain_num == 2);
2976 rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
2977
2978 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD,
2979 rt2x00dev->default_ant.rx_chain_num >= 1);
2980 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD,
2981 rt2x00dev->default_ant.rx_chain_num == 2);
2982 rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
2983
2984 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2985 rt2800_rfcsr_write(rt2x00dev, 6, 0xe4);
2986
2987 if (conf_is_ht40(conf))
2988 rt2800_rfcsr_write(rt2x00dev, 30, 0x16);
2989 else
2990 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
2991
2992 if (!is_11b) {
2993 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
2994 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
2995 }
2996
2997 /* TODO proper frequency adjustment */
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01002998 rt2800_adjust_freq_offset(rt2x00dev);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01002999
3000 /* TODO merge with others */
3001 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
3002 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
3003 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
Stanislaw Gruszka68031412013-03-16 19:19:44 +01003004
3005 /* BBP settings */
3006 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
3007 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
3008 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
3009
3010 rt2800_bbp_write(rt2x00dev, 79, (rf->channel <= 14) ? 0x1C : 0x18);
3011 rt2800_bbp_write(rt2x00dev, 80, (rf->channel <= 14) ? 0x0E : 0x08);
3012 rt2800_bbp_write(rt2x00dev, 81, (rf->channel <= 14) ? 0x3A : 0x38);
3013 rt2800_bbp_write(rt2x00dev, 82, (rf->channel <= 14) ? 0x62 : 0x92);
3014
3015 /* GLRT band configuration */
3016 rt2800_bbp_write(rt2x00dev, 195, 128);
3017 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0xE0 : 0xF0);
3018 rt2800_bbp_write(rt2x00dev, 195, 129);
3019 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x1F : 0x1E);
3020 rt2800_bbp_write(rt2x00dev, 195, 130);
3021 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x38 : 0x28);
3022 rt2800_bbp_write(rt2x00dev, 195, 131);
3023 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x32 : 0x20);
3024 rt2800_bbp_write(rt2x00dev, 195, 133);
3025 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x28 : 0x7F);
3026 rt2800_bbp_write(rt2x00dev, 195, 124);
3027 rt2800_bbp_write(rt2x00dev, 196, (rf->channel <= 14) ? 0x19 : 0x7F);
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01003028}
3029
Stanislaw Gruszka5bc2dd02013-03-16 19:19:47 +01003030static void rt2800_bbp_write_with_rx_chain(struct rt2x00_dev *rt2x00dev,
3031 const unsigned int word,
3032 const u8 value)
3033{
3034 u8 chain, reg;
3035
3036 for (chain = 0; chain < rt2x00dev->default_ant.rx_chain_num; chain++) {
3037 rt2800_bbp_read(rt2x00dev, 27, &reg);
3038 rt2x00_set_field8(&reg, BBP27_RX_CHAIN_SEL, chain);
3039 rt2800_bbp_write(rt2x00dev, 27, reg);
3040
3041 rt2800_bbp_write(rt2x00dev, word, value);
3042 }
3043}
3044
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003045static void rt2800_iq_calibrate(struct rt2x00_dev *rt2x00dev, int channel)
3046{
3047 u8 cal;
3048
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003049 /* TX0 IQ Gain */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003050 rt2800_bbp_write(rt2x00dev, 158, 0x2c);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003051 if (channel <= 14)
3052 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_GAIN_CAL_TX0_2G);
3053 else if (channel >= 36 && channel <= 64)
3054 cal = rt2x00_eeprom_byte(rt2x00dev,
3055 EEPROM_IQ_GAIN_CAL_TX0_CH36_TO_CH64_5G);
3056 else if (channel >= 100 && channel <= 138)
3057 cal = rt2x00_eeprom_byte(rt2x00dev,
3058 EEPROM_IQ_GAIN_CAL_TX0_CH100_TO_CH138_5G);
3059 else if (channel >= 140 && channel <= 165)
3060 cal = rt2x00_eeprom_byte(rt2x00dev,
3061 EEPROM_IQ_GAIN_CAL_TX0_CH140_TO_CH165_5G);
3062 else
3063 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003064 rt2800_bbp_write(rt2x00dev, 159, cal);
3065
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003066 /* TX0 IQ Phase */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003067 rt2800_bbp_write(rt2x00dev, 158, 0x2d);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003068 if (channel <= 14)
3069 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_PHASE_CAL_TX0_2G);
3070 else if (channel >= 36 && channel <= 64)
3071 cal = rt2x00_eeprom_byte(rt2x00dev,
3072 EEPROM_IQ_PHASE_CAL_TX0_CH36_TO_CH64_5G);
3073 else if (channel >= 100 && channel <= 138)
3074 cal = rt2x00_eeprom_byte(rt2x00dev,
3075 EEPROM_IQ_PHASE_CAL_TX0_CH100_TO_CH138_5G);
3076 else if (channel >= 140 && channel <= 165)
3077 cal = rt2x00_eeprom_byte(rt2x00dev,
3078 EEPROM_IQ_PHASE_CAL_TX0_CH140_TO_CH165_5G);
3079 else
3080 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003081 rt2800_bbp_write(rt2x00dev, 159, cal);
3082
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003083 /* TX1 IQ Gain */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003084 rt2800_bbp_write(rt2x00dev, 158, 0x4a);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003085 if (channel <= 14)
3086 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_GAIN_CAL_TX1_2G);
3087 else if (channel >= 36 && channel <= 64)
3088 cal = rt2x00_eeprom_byte(rt2x00dev,
3089 EEPROM_IQ_GAIN_CAL_TX1_CH36_TO_CH64_5G);
3090 else if (channel >= 100 && channel <= 138)
3091 cal = rt2x00_eeprom_byte(rt2x00dev,
3092 EEPROM_IQ_GAIN_CAL_TX1_CH100_TO_CH138_5G);
3093 else if (channel >= 140 && channel <= 165)
3094 cal = rt2x00_eeprom_byte(rt2x00dev,
3095 EEPROM_IQ_GAIN_CAL_TX1_CH140_TO_CH165_5G);
3096 else
3097 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003098 rt2800_bbp_write(rt2x00dev, 159, cal);
3099
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003100 /* TX1 IQ Phase */
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003101 rt2800_bbp_write(rt2x00dev, 158, 0x4b);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003102 if (channel <= 14)
3103 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_IQ_PHASE_CAL_TX1_2G);
3104 else if (channel >= 36 && channel <= 64)
3105 cal = rt2x00_eeprom_byte(rt2x00dev,
3106 EEPROM_IQ_PHASE_CAL_TX1_CH36_TO_CH64_5G);
3107 else if (channel >= 100 && channel <= 138)
3108 cal = rt2x00_eeprom_byte(rt2x00dev,
3109 EEPROM_IQ_PHASE_CAL_TX1_CH100_TO_CH138_5G);
3110 else if (channel >= 140 && channel <= 165)
3111 cal = rt2x00_eeprom_byte(rt2x00dev,
3112 EEPROM_IQ_PHASE_CAL_TX1_CH140_TO_CH165_5G);
3113 else
3114 cal = 0;
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003115 rt2800_bbp_write(rt2x00dev, 159, cal);
3116
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003117 /* FIXME: possible RX0, RX1 callibration ? */
3118
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003119 /* RF IQ compensation control */
3120 rt2800_bbp_write(rt2x00dev, 158, 0x04);
3121 cal = rt2x00_eeprom_byte(rt2x00dev, EEPROM_RF_IQ_COMPENSATION_CONTROL);
3122 rt2800_bbp_write(rt2x00dev, 159, cal != 0xff ? cal : 0);
3123
3124 /* RF IQ imbalance compensation control */
3125 rt2800_bbp_write(rt2x00dev, 158, 0x03);
Stanislaw Gruszka415e3f22013-03-16 19:19:52 +01003126 cal = rt2x00_eeprom_byte(rt2x00dev,
3127 EEPROM_RF_IQ_IMBALANCE_COMPENSATION_CONTROL);
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003128 rt2800_bbp_write(rt2x00dev, 159, cal != 0xff ? cal : 0);
3129}
3130
Gabor Juhos97aa03f2013-07-08 16:08:23 +02003131static char rt2800_txpower_to_dev(struct rt2x00_dev *rt2x00dev,
3132 unsigned int channel,
3133 char txpower)
3134{
Gabor Juhosfc739cf2013-07-08 16:08:24 +02003135 if (rt2x00_rt(rt2x00dev, RT3593))
3136 txpower = rt2x00_get_field8(txpower, EEPROM_TXPOWER_ALC);
3137
Gabor Juhos97aa03f2013-07-08 16:08:23 +02003138 if (channel <= 14)
3139 return clamp_t(char, txpower, MIN_G_TXPOWER, MAX_G_TXPOWER);
Gabor Juhosfc739cf2013-07-08 16:08:24 +02003140
3141 if (rt2x00_rt(rt2x00dev, RT3593))
3142 return clamp_t(char, txpower, MIN_A_TXPOWER_3593,
3143 MAX_A_TXPOWER_3593);
Gabor Juhos97aa03f2013-07-08 16:08:23 +02003144 else
3145 return clamp_t(char, txpower, MIN_A_TXPOWER, MAX_A_TXPOWER);
3146}
3147
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003148static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
3149 struct ieee80211_conf *conf,
3150 struct rf_channel *rf,
3151 struct channel_info *info)
3152{
3153 u32 reg;
3154 unsigned int tx_pin;
Woody Hunga89534e2012-06-13 15:01:16 +08003155 u8 bbp, rfcsr;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003156
Gabor Juhos97aa03f2013-07-08 16:08:23 +02003157 info->default_power1 = rt2800_txpower_to_dev(rt2x00dev, rf->channel,
3158 info->default_power1);
3159 info->default_power2 = rt2800_txpower_to_dev(rt2x00dev, rf->channel,
3160 info->default_power2);
Gabor Juhosc0a14362013-07-08 16:08:28 +02003161 if (rt2x00dev->default_ant.tx_chain_num > 2)
3162 info->default_power3 =
3163 rt2800_txpower_to_dev(rt2x00dev, rf->channel,
3164 info->default_power3);
Ivo van Doorn46323e12010-08-23 19:55:43 +02003165
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003166 switch (rt2x00dev->chip.rf) {
3167 case RF2020:
3168 case RF3020:
3169 case RF3021:
3170 case RF3022:
3171 case RF3320:
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02003172 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003173 break;
3174 case RF3052:
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003175 rt2800_config_channel_rf3052(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003176 break;
Gabor Juhosf42b0462013-07-08 16:08:30 +02003177 case RF3053:
3178 rt2800_config_channel_rf3053(rt2x00dev, conf, rf, info);
3179 break;
Woody Hunga89534e2012-06-13 15:01:16 +08003180 case RF3290:
3181 rt2800_config_channel_rf3290(rt2x00dev, conf, rf, info);
3182 break;
Daniel Golle03839952012-09-09 14:24:39 +03003183 case RF3322:
3184 rt2800_config_channel_rf3322(rt2x00dev, conf, rf, info);
3185 break;
Stanislaw Gruszka3b9b74b2013-09-25 15:34:55 +02003186 case RF3070:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02003187 case RF5360:
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05003188 case RF5362:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003189 case RF5370:
John Li2ed71882012-02-17 17:33:06 +08003190 case RF5372:
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003191 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08003192 case RF5392:
Gabor Juhosadde5882011-03-03 11:46:45 +01003193 rt2800_config_channel_rf53xx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003194 break;
Stanislaw Gruszka8f821092013-03-16 19:19:32 +01003195 case RF5592:
3196 rt2800_config_channel_rf55xx(rt2x00dev, conf, rf, info);
3197 break;
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003198 default:
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02003199 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerde5aa57012011-12-28 01:53:20 +01003200 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003201
Stanislaw Gruszka3b9b74b2013-09-25 15:34:55 +02003202 if (rt2x00_rf(rt2x00dev, RF3070) ||
3203 rt2x00_rf(rt2x00dev, RF3290) ||
Daniel Golle03839952012-09-09 14:24:39 +03003204 rt2x00_rf(rt2x00dev, RF3322) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003205 rt2x00_rf(rt2x00dev, RF5360) ||
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05003206 rt2x00_rf(rt2x00dev, RF5362) ||
Woody Hunga89534e2012-06-13 15:01:16 +08003207 rt2x00_rf(rt2x00dev, RF5370) ||
3208 rt2x00_rf(rt2x00dev, RF5372) ||
3209 rt2x00_rf(rt2x00dev, RF5390) ||
3210 rt2x00_rf(rt2x00dev, RF5392)) {
3211 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
3212 rt2x00_set_field8(&rfcsr, RFCSR30_TX_H20M, 0);
3213 rt2x00_set_field8(&rfcsr, RFCSR30_RX_H20M, 0);
3214 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
3215
3216 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
Gabor Juhosd6d82022012-12-02 18:34:47 +01003217 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
Woody Hunga89534e2012-06-13 15:01:16 +08003218 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
3219 }
3220
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003221 /*
3222 * Change BBP settings
3223 */
Daniel Golle03839952012-09-09 14:24:39 +03003224 if (rt2x00_rt(rt2x00dev, RT3352)) {
3225 rt2800_bbp_write(rt2x00dev, 27, 0x0);
Daniel Gollecf193f62012-10-04 01:20:41 +02003226 rt2800_bbp_write(rt2x00dev, 66, 0x26 + rt2x00dev->lna_gain);
Daniel Golle03839952012-09-09 14:24:39 +03003227 rt2800_bbp_write(rt2x00dev, 27, 0x20);
Daniel Gollecf193f62012-10-04 01:20:41 +02003228 rt2800_bbp_write(rt2x00dev, 66, 0x26 + rt2x00dev->lna_gain);
Gabor Juhosf42b0462013-07-08 16:08:30 +02003229 } else if (rt2x00_rt(rt2x00dev, RT3593)) {
3230 if (rf->channel > 14) {
3231 /* Disable CCK Packet detection on 5GHz */
3232 rt2800_bbp_write(rt2x00dev, 70, 0x00);
3233 } else {
3234 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
3235 }
3236
3237 if (conf_is_ht40(conf))
3238 rt2800_bbp_write(rt2x00dev, 105, 0x04);
3239 else
3240 rt2800_bbp_write(rt2x00dev, 105, 0x34);
3241
3242 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
3243 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
3244 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
3245 rt2800_bbp_write(rt2x00dev, 77, 0x98);
Daniel Golle03839952012-09-09 14:24:39 +03003246 } else {
3247 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
3248 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
3249 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
3250 rt2800_bbp_write(rt2x00dev, 86, 0);
3251 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003252
3253 if (rf->channel <= 14) {
John Li2ed71882012-02-17 17:33:06 +08003254 if (!rt2x00_rt(rt2x00dev, RT5390) &&
Gabor Juhose6d227b2012-12-02 15:53:28 +01003255 !rt2x00_rt(rt2x00dev, RT5392)) {
Gabor Juhosc429dfe2013-10-11 13:18:42 +02003256 if (rt2x00_has_cap_external_lna_bg(rt2x00dev)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01003257 rt2800_bbp_write(rt2x00dev, 82, 0x62);
3258 rt2800_bbp_write(rt2x00dev, 75, 0x46);
3259 } else {
Gabor Juhosf42b0462013-07-08 16:08:30 +02003260 if (rt2x00_rt(rt2x00dev, RT3593))
3261 rt2800_bbp_write(rt2x00dev, 82, 0x62);
3262 else
3263 rt2800_bbp_write(rt2x00dev, 82, 0x84);
Gabor Juhosadde5882011-03-03 11:46:45 +01003264 rt2800_bbp_write(rt2x00dev, 75, 0x50);
3265 }
Gabor Juhosf42b0462013-07-08 16:08:30 +02003266 if (rt2x00_rt(rt2x00dev, RT3593))
3267 rt2800_bbp_write(rt2x00dev, 83, 0x8a);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003268 }
Gabor Juhosf42b0462013-07-08 16:08:30 +02003269
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003270 } else {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003271 if (rt2x00_rt(rt2x00dev, RT3572))
3272 rt2800_bbp_write(rt2x00dev, 82, 0x94);
Gabor Juhosf42b0462013-07-08 16:08:30 +02003273 else if (rt2x00_rt(rt2x00dev, RT3593))
3274 rt2800_bbp_write(rt2x00dev, 82, 0x82);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003275 else
3276 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003277
Gabor Juhosf42b0462013-07-08 16:08:30 +02003278 if (rt2x00_rt(rt2x00dev, RT3593))
3279 rt2800_bbp_write(rt2x00dev, 83, 0x9a);
3280
Gabor Juhosc429dfe2013-10-11 13:18:42 +02003281 if (rt2x00_has_cap_external_lna_a(rt2x00dev))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003282 rt2800_bbp_write(rt2x00dev, 75, 0x46);
3283 else
3284 rt2800_bbp_write(rt2x00dev, 75, 0x50);
3285 }
3286
3287 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02003288 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003289 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
3290 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
3291 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
3292
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003293 if (rt2x00_rt(rt2x00dev, RT3572))
3294 rt2800_rfcsr_write(rt2x00dev, 8, 0);
3295
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003296 tx_pin = 0;
3297
Gabor Juhosbb16d482013-06-24 23:03:24 +02003298 switch (rt2x00dev->default_ant.tx_chain_num) {
3299 case 3:
3300 /* Turn on tertiary PAs */
3301 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A2_EN,
3302 rf->channel > 14);
3303 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G2_EN,
3304 rf->channel <= 14);
3305 /* fall-through */
3306 case 2:
3307 /* Turn on secondary PAs */
Gertjan van Wingerde65f31b52011-05-18 20:25:05 +02003308 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN,
3309 rf->channel > 14);
3310 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN,
3311 rf->channel <= 14);
Gabor Juhosbb16d482013-06-24 23:03:24 +02003312 /* fall-through */
3313 case 1:
3314 /* Turn on primary PAs */
3315 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN,
3316 rf->channel > 14);
Gabor Juhosc429dfe2013-10-11 13:18:42 +02003317 if (rt2x00_has_cap_bt_coexist(rt2x00dev))
Gabor Juhosbb16d482013-06-24 23:03:24 +02003318 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
3319 else
3320 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN,
3321 rf->channel <= 14);
3322 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003323 }
3324
Gabor Juhosbb16d482013-06-24 23:03:24 +02003325 switch (rt2x00dev->default_ant.rx_chain_num) {
3326 case 3:
3327 /* Turn on tertiary LNAs */
3328 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A2_EN, 1);
3329 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G2_EN, 1);
3330 /* fall-through */
3331 case 2:
3332 /* Turn on secondary LNAs */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003333 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
3334 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
Gabor Juhosbb16d482013-06-24 23:03:24 +02003335 /* fall-through */
3336 case 1:
3337 /* Turn on primary LNAs */
3338 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
3339 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
3340 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003341 }
3342
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003343 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
3344 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003345
3346 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
3347
Gabor Juhos733aec62013-10-04 22:07:09 +02003348 if (rt2x00_rt(rt2x00dev, RT3572)) {
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02003349 rt2800_rfcsr_write(rt2x00dev, 8, 0x80);
3350
Gabor Juhos733aec62013-10-04 22:07:09 +02003351 /* AGC init */
3352 if (rf->channel <= 14)
3353 reg = 0x1c + (2 * rt2x00dev->lna_gain);
3354 else
3355 reg = 0x22 + ((rt2x00dev->lna_gain * 5) / 3);
3356
3357 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66, reg);
3358 }
3359
Gabor Juhosf42b0462013-07-08 16:08:30 +02003360 if (rt2x00_rt(rt2x00dev, RT3593)) {
Gabor Juhos60751002013-09-11 19:56:45 +02003361 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
Gabor Juhosf42b0462013-07-08 16:08:30 +02003362
Gabor Juhos60751002013-09-11 19:56:45 +02003363 /* Band selection */
3364 if (rt2x00_is_usb(rt2x00dev) ||
3365 rt2x00_is_pcie(rt2x00dev)) {
3366 /* GPIO #8 controls all paths */
Gabor Juhosf42b0462013-07-08 16:08:30 +02003367 rt2x00_set_field32(&reg, GPIO_CTRL_DIR8, 0);
3368 if (rf->channel <= 14)
3369 rt2x00_set_field32(&reg, GPIO_CTRL_VAL8, 1);
3370 else
3371 rt2x00_set_field32(&reg, GPIO_CTRL_VAL8, 0);
Gabor Juhos60751002013-09-11 19:56:45 +02003372 }
Gabor Juhosf42b0462013-07-08 16:08:30 +02003373
Gabor Juhos60751002013-09-11 19:56:45 +02003374 /* LNA PE control. */
3375 if (rt2x00_is_usb(rt2x00dev)) {
3376 /* GPIO #4 controls PE0 and PE1,
3377 * GPIO #7 controls PE2
3378 */
Gabor Juhosf42b0462013-07-08 16:08:30 +02003379 rt2x00_set_field32(&reg, GPIO_CTRL_DIR4, 0);
3380 rt2x00_set_field32(&reg, GPIO_CTRL_DIR7, 0);
3381
Gabor Juhosf42b0462013-07-08 16:08:30 +02003382 rt2x00_set_field32(&reg, GPIO_CTRL_VAL4, 1);
3383 rt2x00_set_field32(&reg, GPIO_CTRL_VAL7, 1);
Gabor Juhos60751002013-09-11 19:56:45 +02003384 } else if (rt2x00_is_pcie(rt2x00dev)) {
3385 /* GPIO #4 controls PE0, PE1 and PE2 */
3386 rt2x00_set_field32(&reg, GPIO_CTRL_DIR4, 0);
3387 rt2x00_set_field32(&reg, GPIO_CTRL_VAL4, 1);
Gabor Juhosf42b0462013-07-08 16:08:30 +02003388 }
3389
Gabor Juhos60751002013-09-11 19:56:45 +02003390 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
3391
Gabor Juhosf42b0462013-07-08 16:08:30 +02003392 /* AGC init */
3393 if (rf->channel <= 14)
3394 reg = 0x1c + 2 * rt2x00dev->lna_gain;
3395 else
3396 reg = 0x22 + ((rt2x00dev->lna_gain * 5) / 3);
3397
3398 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66, reg);
3399
3400 usleep_range(1000, 1500);
3401 }
3402
Stanislaw Gruszka68031412013-03-16 19:19:44 +01003403 if (rt2x00_rt(rt2x00dev, RT5592)) {
3404 rt2800_bbp_write(rt2x00dev, 195, 141);
3405 rt2800_bbp_write(rt2x00dev, 196, conf_is_ht40(conf) ? 0x10 : 0x1a);
3406
Stanislaw Gruszka8ba0ebf2013-03-16 19:19:48 +01003407 /* AGC init */
3408 reg = (rf->channel <= 14 ? 0x1c : 0x24) + 2 * rt2x00dev->lna_gain;
3409 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66, reg);
3410
Stanislaw Gruszka87561302013-03-16 19:19:45 +01003411 rt2800_iq_calibrate(rt2x00dev, rf->channel);
Stanislaw Gruszka68031412013-03-16 19:19:44 +01003412 }
3413
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003414 rt2800_bbp_read(rt2x00dev, 4, &bbp);
3415 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
3416 rt2800_bbp_write(rt2x00dev, 4, bbp);
3417
3418 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02003419 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003420 rt2800_bbp_write(rt2x00dev, 3, bbp);
3421
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02003422 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003423 if (conf_is_ht40(conf)) {
3424 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
3425 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
3426 rt2800_bbp_write(rt2x00dev, 73, 0x16);
3427 } else {
3428 rt2800_bbp_write(rt2x00dev, 69, 0x16);
3429 rt2800_bbp_write(rt2x00dev, 70, 0x08);
3430 rt2800_bbp_write(rt2x00dev, 73, 0x11);
3431 }
3432 }
3433
3434 msleep(1);
Helmut Schaa977206d2010-12-13 12:31:58 +01003435
3436 /*
3437 * Clear channel statistic counters
3438 */
3439 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &reg);
3440 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &reg);
3441 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &reg);
Daniel Golle03839952012-09-09 14:24:39 +03003442
3443 /*
3444 * Clear update flag
3445 */
3446 if (rt2x00_rt(rt2x00dev, RT3352)) {
3447 rt2800_bbp_read(rt2x00dev, 49, &bbp);
3448 rt2x00_set_field8(&bbp, BBP49_UPDATE_FLAG, 0);
3449 rt2800_bbp_write(rt2x00dev, 49, bbp);
3450 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01003451}
3452
Helmut Schaa9e33a352011-03-28 13:33:40 +02003453static int rt2800_get_gain_calibration_delta(struct rt2x00_dev *rt2x00dev)
3454{
3455 u8 tssi_bounds[9];
3456 u8 current_tssi;
3457 u16 eeprom;
3458 u8 step;
3459 int i;
3460
3461 /*
Stanislaw Gruszka6e956da2013-08-26 15:18:53 +02003462 * First check if temperature compensation is supported.
3463 */
3464 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
3465 if (!rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC))
3466 return 0;
3467
3468 /*
Helmut Schaa9e33a352011-03-28 13:33:40 +02003469 * Read TSSI boundaries for temperature compensation from
3470 * the EEPROM.
3471 *
3472 * Array idx 0 1 2 3 4 5 6 7 8
3473 * Matching Delta value -4 -3 -2 -1 0 +1 +2 +3 +4
3474 * Example TSSI bounds 0xF0 0xD0 0xB5 0xA0 0x88 0x45 0x25 0x15 0x00
3475 */
3476 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003477 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG1, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003478 tssi_bounds[0] = rt2x00_get_field16(eeprom,
3479 EEPROM_TSSI_BOUND_BG1_MINUS4);
3480 tssi_bounds[1] = rt2x00_get_field16(eeprom,
3481 EEPROM_TSSI_BOUND_BG1_MINUS3);
3482
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003483 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG2, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003484 tssi_bounds[2] = rt2x00_get_field16(eeprom,
3485 EEPROM_TSSI_BOUND_BG2_MINUS2);
3486 tssi_bounds[3] = rt2x00_get_field16(eeprom,
3487 EEPROM_TSSI_BOUND_BG2_MINUS1);
3488
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003489 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG3, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003490 tssi_bounds[4] = rt2x00_get_field16(eeprom,
3491 EEPROM_TSSI_BOUND_BG3_REF);
3492 tssi_bounds[5] = rt2x00_get_field16(eeprom,
3493 EEPROM_TSSI_BOUND_BG3_PLUS1);
3494
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003495 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG4, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003496 tssi_bounds[6] = rt2x00_get_field16(eeprom,
3497 EEPROM_TSSI_BOUND_BG4_PLUS2);
3498 tssi_bounds[7] = rt2x00_get_field16(eeprom,
3499 EEPROM_TSSI_BOUND_BG4_PLUS3);
3500
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003501 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG5, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003502 tssi_bounds[8] = rt2x00_get_field16(eeprom,
3503 EEPROM_TSSI_BOUND_BG5_PLUS4);
3504
3505 step = rt2x00_get_field16(eeprom,
3506 EEPROM_TSSI_BOUND_BG5_AGC_STEP);
3507 } else {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003508 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A1, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003509 tssi_bounds[0] = rt2x00_get_field16(eeprom,
3510 EEPROM_TSSI_BOUND_A1_MINUS4);
3511 tssi_bounds[1] = rt2x00_get_field16(eeprom,
3512 EEPROM_TSSI_BOUND_A1_MINUS3);
3513
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003514 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A2, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003515 tssi_bounds[2] = rt2x00_get_field16(eeprom,
3516 EEPROM_TSSI_BOUND_A2_MINUS2);
3517 tssi_bounds[3] = rt2x00_get_field16(eeprom,
3518 EEPROM_TSSI_BOUND_A2_MINUS1);
3519
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003520 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A3, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003521 tssi_bounds[4] = rt2x00_get_field16(eeprom,
3522 EEPROM_TSSI_BOUND_A3_REF);
3523 tssi_bounds[5] = rt2x00_get_field16(eeprom,
3524 EEPROM_TSSI_BOUND_A3_PLUS1);
3525
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003526 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A4, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003527 tssi_bounds[6] = rt2x00_get_field16(eeprom,
3528 EEPROM_TSSI_BOUND_A4_PLUS2);
3529 tssi_bounds[7] = rt2x00_get_field16(eeprom,
3530 EEPROM_TSSI_BOUND_A4_PLUS3);
3531
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003532 rt2800_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A5, &eeprom);
Helmut Schaa9e33a352011-03-28 13:33:40 +02003533 tssi_bounds[8] = rt2x00_get_field16(eeprom,
3534 EEPROM_TSSI_BOUND_A5_PLUS4);
3535
3536 step = rt2x00_get_field16(eeprom,
3537 EEPROM_TSSI_BOUND_A5_AGC_STEP);
3538 }
3539
3540 /*
3541 * Check if temperature compensation is supported.
3542 */
Stanislaw Gruszkabf7e1ab2012-10-25 09:51:39 +02003543 if (tssi_bounds[4] == 0xff || step == 0xff)
Helmut Schaa9e33a352011-03-28 13:33:40 +02003544 return 0;
3545
3546 /*
3547 * Read current TSSI (BBP 49).
3548 */
3549 rt2800_bbp_read(rt2x00dev, 49, &current_tssi);
3550
3551 /*
3552 * Compare TSSI value (BBP49) with the compensation boundaries
3553 * from the EEPROM and increase or decrease tx power.
3554 */
3555 for (i = 0; i <= 3; i++) {
3556 if (current_tssi > tssi_bounds[i])
3557 break;
3558 }
3559
3560 if (i == 4) {
3561 for (i = 8; i >= 5; i--) {
3562 if (current_tssi < tssi_bounds[i])
3563 break;
3564 }
3565 }
3566
3567 return (i - 4) * step;
3568}
3569
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003570static int rt2800_get_txpower_bw_comp(struct rt2x00_dev *rt2x00dev,
3571 enum ieee80211_band band)
3572{
3573 u16 eeprom;
3574 u8 comp_en;
3575 u8 comp_type;
Helmut Schaa75faae82011-03-28 13:31:30 +02003576 int comp_value = 0;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003577
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003578 rt2800_eeprom_read(rt2x00dev, EEPROM_TXPOWER_DELTA, &eeprom);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003579
Helmut Schaa75faae82011-03-28 13:31:30 +02003580 /*
3581 * HT40 compensation not required.
3582 */
3583 if (eeprom == 0xffff ||
3584 !test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003585 return 0;
3586
3587 if (band == IEEE80211_BAND_2GHZ) {
3588 comp_en = rt2x00_get_field16(eeprom,
3589 EEPROM_TXPOWER_DELTA_ENABLE_2G);
3590 if (comp_en) {
3591 comp_type = rt2x00_get_field16(eeprom,
3592 EEPROM_TXPOWER_DELTA_TYPE_2G);
3593 comp_value = rt2x00_get_field16(eeprom,
3594 EEPROM_TXPOWER_DELTA_VALUE_2G);
3595 if (!comp_type)
3596 comp_value = -comp_value;
3597 }
3598 } else {
3599 comp_en = rt2x00_get_field16(eeprom,
3600 EEPROM_TXPOWER_DELTA_ENABLE_5G);
3601 if (comp_en) {
3602 comp_type = rt2x00_get_field16(eeprom,
3603 EEPROM_TXPOWER_DELTA_TYPE_5G);
3604 comp_value = rt2x00_get_field16(eeprom,
3605 EEPROM_TXPOWER_DELTA_VALUE_5G);
3606 if (!comp_type)
3607 comp_value = -comp_value;
3608 }
3609 }
3610
3611 return comp_value;
3612}
3613
Stanislaw Gruszka1e4cf242012-10-05 13:44:14 +02003614static int rt2800_get_txpower_reg_delta(struct rt2x00_dev *rt2x00dev,
3615 int power_level, int max_power)
3616{
3617 int delta;
3618
Gabor Juhosc429dfe2013-10-11 13:18:42 +02003619 if (rt2x00_has_cap_power_limit(rt2x00dev))
Stanislaw Gruszka1e4cf242012-10-05 13:44:14 +02003620 return 0;
3621
3622 /*
3623 * XXX: We don't know the maximum transmit power of our hardware since
3624 * the EEPROM doesn't expose it. We only know that we are calibrated
3625 * to 100% tx power.
3626 *
3627 * Hence, we assume the regulatory limit that cfg80211 calulated for
3628 * the current channel is our maximum and if we are requested to lower
3629 * the value we just reduce our tx power accordingly.
3630 */
3631 delta = power_level - max_power;
3632 return min(delta, 0);
3633}
3634
Helmut Schaafa71a162011-03-28 13:32:32 +02003635static u8 rt2800_compensate_txpower(struct rt2x00_dev *rt2x00dev, int is_rate_b,
3636 enum ieee80211_band band, int power_level,
3637 u8 txpower, int delta)
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003638{
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003639 u16 eeprom;
3640 u8 criterion;
3641 u8 eirp_txpower;
3642 u8 eirp_txpower_criterion;
3643 u8 reg_limit;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003644
Gabor Juhos34542ff2013-07-08 16:08:20 +02003645 if (rt2x00_rt(rt2x00dev, RT3593))
3646 return min_t(u8, txpower, 0xc);
3647
Gabor Juhosc429dfe2013-10-11 13:18:42 +02003648 if (rt2x00_has_cap_power_limit(rt2x00dev)) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003649 /*
3650 * Check if eirp txpower exceed txpower_limit.
3651 * We use OFDM 6M as criterion and its eirp txpower
3652 * is stored at EEPROM_EIRP_MAX_TX_POWER.
3653 * .11b data rate need add additional 4dbm
3654 * when calculating eirp txpower.
3655 */
Gabor Juhos022138c2013-07-08 11:25:54 +02003656 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3657 1, &eeprom);
Stanislaw Gruszkad9bceae2012-10-05 13:44:12 +02003658 criterion = rt2x00_get_field16(eeprom,
3659 EEPROM_TXPOWER_BYRATE_RATE0);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003660
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02003661 rt2800_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER,
Stanislaw Gruszkad9bceae2012-10-05 13:44:12 +02003662 &eeprom);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003663
3664 if (band == IEEE80211_BAND_2GHZ)
3665 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
3666 EEPROM_EIRP_MAX_TX_POWER_2GHZ);
3667 else
3668 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
3669 EEPROM_EIRP_MAX_TX_POWER_5GHZ);
3670
3671 eirp_txpower = eirp_txpower_criterion + (txpower - criterion) +
Helmut Schaa2af242e2011-03-28 13:32:01 +02003672 (is_rate_b ? 4 : 0) + delta;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003673
3674 reg_limit = (eirp_txpower > power_level) ?
3675 (eirp_txpower - power_level) : 0;
3676 } else
3677 reg_limit = 0;
3678
Stanislaw Gruszka19f3fa22012-10-05 13:44:10 +02003679 txpower = max(0, txpower + delta - reg_limit);
3680 return min_t(u8, txpower, 0xc);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003681}
3682
Gabor Juhos34542ff2013-07-08 16:08:20 +02003683
3684enum {
3685 TX_PWR_CFG_0_IDX,
3686 TX_PWR_CFG_1_IDX,
3687 TX_PWR_CFG_2_IDX,
3688 TX_PWR_CFG_3_IDX,
3689 TX_PWR_CFG_4_IDX,
3690 TX_PWR_CFG_5_IDX,
3691 TX_PWR_CFG_6_IDX,
3692 TX_PWR_CFG_7_IDX,
3693 TX_PWR_CFG_8_IDX,
3694 TX_PWR_CFG_9_IDX,
3695 TX_PWR_CFG_0_EXT_IDX,
3696 TX_PWR_CFG_1_EXT_IDX,
3697 TX_PWR_CFG_2_EXT_IDX,
3698 TX_PWR_CFG_3_EXT_IDX,
3699 TX_PWR_CFG_4_EXT_IDX,
3700 TX_PWR_CFG_IDX_COUNT,
3701};
3702
3703static void rt2800_config_txpower_rt3593(struct rt2x00_dev *rt2x00dev,
3704 struct ieee80211_channel *chan,
3705 int power_level)
3706{
3707 u8 txpower;
3708 u16 eeprom;
3709 u32 regs[TX_PWR_CFG_IDX_COUNT];
3710 unsigned int offset;
3711 enum ieee80211_band band = chan->band;
3712 int delta;
3713 int i;
3714
3715 memset(regs, '\0', sizeof(regs));
3716
3717 /* TODO: adapt TX power reduction from the rt28xx code */
3718
3719 /* calculate temperature compensation delta */
3720 delta = rt2800_get_gain_calibration_delta(rt2x00dev);
3721
3722 if (band == IEEE80211_BAND_5GHZ)
3723 offset = 16;
3724 else
3725 offset = 0;
3726
3727 if (test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
3728 offset += 8;
3729
3730 /* read the next four txpower values */
3731 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3732 offset, &eeprom);
3733
3734 /* CCK 1MBS,2MBS */
3735 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3736 txpower = rt2800_compensate_txpower(rt2x00dev, 1, band, power_level,
3737 txpower, delta);
3738 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3739 TX_PWR_CFG_0_CCK1_CH0, txpower);
3740 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3741 TX_PWR_CFG_0_CCK1_CH1, txpower);
3742 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3743 TX_PWR_CFG_0_EXT_CCK1_CH2, txpower);
3744
3745 /* CCK 5.5MBS,11MBS */
3746 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3747 txpower = rt2800_compensate_txpower(rt2x00dev, 1, band, power_level,
3748 txpower, delta);
3749 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3750 TX_PWR_CFG_0_CCK5_CH0, txpower);
3751 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3752 TX_PWR_CFG_0_CCK5_CH1, txpower);
3753 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3754 TX_PWR_CFG_0_EXT_CCK5_CH2, txpower);
3755
3756 /* OFDM 6MBS,9MBS */
3757 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3758 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3759 txpower, delta);
3760 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3761 TX_PWR_CFG_0_OFDM6_CH0, txpower);
3762 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3763 TX_PWR_CFG_0_OFDM6_CH1, txpower);
3764 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3765 TX_PWR_CFG_0_EXT_OFDM6_CH2, txpower);
3766
3767 /* OFDM 12MBS,18MBS */
3768 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3769 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3770 txpower, delta);
3771 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3772 TX_PWR_CFG_0_OFDM12_CH0, txpower);
3773 rt2x00_set_field32(&regs[TX_PWR_CFG_0_IDX],
3774 TX_PWR_CFG_0_OFDM12_CH1, txpower);
3775 rt2x00_set_field32(&regs[TX_PWR_CFG_0_EXT_IDX],
3776 TX_PWR_CFG_0_EXT_OFDM12_CH2, txpower);
3777
3778 /* read the next four txpower values */
3779 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3780 offset + 1, &eeprom);
3781
3782 /* OFDM 24MBS,36MBS */
3783 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3784 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3785 txpower, delta);
3786 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3787 TX_PWR_CFG_1_OFDM24_CH0, txpower);
3788 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3789 TX_PWR_CFG_1_OFDM24_CH1, txpower);
3790 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3791 TX_PWR_CFG_1_EXT_OFDM24_CH2, txpower);
3792
3793 /* OFDM 48MBS */
3794 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3795 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3796 txpower, delta);
3797 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3798 TX_PWR_CFG_1_OFDM48_CH0, txpower);
3799 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3800 TX_PWR_CFG_1_OFDM48_CH1, txpower);
3801 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3802 TX_PWR_CFG_1_EXT_OFDM48_CH2, txpower);
3803
3804 /* OFDM 54MBS */
3805 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3806 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3807 txpower, delta);
3808 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3809 TX_PWR_CFG_7_OFDM54_CH0, txpower);
3810 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3811 TX_PWR_CFG_7_OFDM54_CH1, txpower);
3812 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3813 TX_PWR_CFG_7_OFDM54_CH2, txpower);
3814
3815 /* read the next four txpower values */
3816 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3817 offset + 2, &eeprom);
3818
3819 /* MCS 0,1 */
3820 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3821 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3822 txpower, delta);
3823 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3824 TX_PWR_CFG_1_MCS0_CH0, txpower);
3825 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3826 TX_PWR_CFG_1_MCS0_CH1, txpower);
3827 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3828 TX_PWR_CFG_1_EXT_MCS0_CH2, txpower);
3829
3830 /* MCS 2,3 */
3831 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3832 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3833 txpower, delta);
3834 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3835 TX_PWR_CFG_1_MCS2_CH0, txpower);
3836 rt2x00_set_field32(&regs[TX_PWR_CFG_1_IDX],
3837 TX_PWR_CFG_1_MCS2_CH1, txpower);
3838 rt2x00_set_field32(&regs[TX_PWR_CFG_1_EXT_IDX],
3839 TX_PWR_CFG_1_EXT_MCS2_CH2, txpower);
3840
3841 /* MCS 4,5 */
3842 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3843 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3844 txpower, delta);
3845 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3846 TX_PWR_CFG_2_MCS4_CH0, txpower);
3847 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3848 TX_PWR_CFG_2_MCS4_CH1, txpower);
3849 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3850 TX_PWR_CFG_2_EXT_MCS4_CH2, txpower);
3851
3852 /* MCS 6 */
3853 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3854 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3855 txpower, delta);
3856 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3857 TX_PWR_CFG_2_MCS6_CH0, txpower);
3858 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3859 TX_PWR_CFG_2_MCS6_CH1, txpower);
3860 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3861 TX_PWR_CFG_2_EXT_MCS6_CH2, txpower);
3862
3863 /* read the next four txpower values */
3864 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3865 offset + 3, &eeprom);
3866
3867 /* MCS 7 */
3868 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3869 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3870 txpower, delta);
3871 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3872 TX_PWR_CFG_7_MCS7_CH0, txpower);
3873 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3874 TX_PWR_CFG_7_MCS7_CH1, txpower);
3875 rt2x00_set_field32(&regs[TX_PWR_CFG_7_IDX],
3876 TX_PWR_CFG_7_MCS7_CH2, txpower);
3877
3878 /* MCS 8,9 */
3879 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3880 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3881 txpower, delta);
3882 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3883 TX_PWR_CFG_2_MCS8_CH0, txpower);
3884 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3885 TX_PWR_CFG_2_MCS8_CH1, txpower);
3886 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3887 TX_PWR_CFG_2_EXT_MCS8_CH2, txpower);
3888
3889 /* MCS 10,11 */
3890 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3891 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3892 txpower, delta);
3893 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3894 TX_PWR_CFG_2_MCS10_CH0, txpower);
3895 rt2x00_set_field32(&regs[TX_PWR_CFG_2_IDX],
3896 TX_PWR_CFG_2_MCS10_CH1, txpower);
3897 rt2x00_set_field32(&regs[TX_PWR_CFG_2_EXT_IDX],
3898 TX_PWR_CFG_2_EXT_MCS10_CH2, txpower);
3899
3900 /* MCS 12,13 */
3901 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3902 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3903 txpower, delta);
3904 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3905 TX_PWR_CFG_3_MCS12_CH0, txpower);
3906 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3907 TX_PWR_CFG_3_MCS12_CH1, txpower);
3908 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
3909 TX_PWR_CFG_3_EXT_MCS12_CH2, txpower);
3910
3911 /* read the next four txpower values */
3912 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3913 offset + 4, &eeprom);
3914
3915 /* MCS 14 */
3916 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3917 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3918 txpower, delta);
3919 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3920 TX_PWR_CFG_3_MCS14_CH0, txpower);
3921 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
3922 TX_PWR_CFG_3_MCS14_CH1, txpower);
3923 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
3924 TX_PWR_CFG_3_EXT_MCS14_CH2, txpower);
3925
3926 /* MCS 15 */
3927 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3928 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3929 txpower, delta);
3930 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3931 TX_PWR_CFG_8_MCS15_CH0, txpower);
3932 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3933 TX_PWR_CFG_8_MCS15_CH1, txpower);
3934 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3935 TX_PWR_CFG_8_MCS15_CH2, txpower);
3936
3937 /* MCS 16,17 */
3938 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3939 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3940 txpower, delta);
3941 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3942 TX_PWR_CFG_5_MCS16_CH0, txpower);
3943 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3944 TX_PWR_CFG_5_MCS16_CH1, txpower);
3945 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3946 TX_PWR_CFG_5_MCS16_CH2, txpower);
3947
3948 /* MCS 18,19 */
3949 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
3950 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3951 txpower, delta);
3952 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3953 TX_PWR_CFG_5_MCS18_CH0, txpower);
3954 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3955 TX_PWR_CFG_5_MCS18_CH1, txpower);
3956 rt2x00_set_field32(&regs[TX_PWR_CFG_5_IDX],
3957 TX_PWR_CFG_5_MCS18_CH2, txpower);
3958
3959 /* read the next four txpower values */
3960 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3961 offset + 5, &eeprom);
3962
3963 /* MCS 20,21 */
3964 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
3965 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3966 txpower, delta);
3967 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3968 TX_PWR_CFG_6_MCS20_CH0, txpower);
3969 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3970 TX_PWR_CFG_6_MCS20_CH1, txpower);
3971 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3972 TX_PWR_CFG_6_MCS20_CH2, txpower);
3973
3974 /* MCS 22 */
3975 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
3976 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3977 txpower, delta);
3978 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3979 TX_PWR_CFG_6_MCS22_CH0, txpower);
3980 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3981 TX_PWR_CFG_6_MCS22_CH1, txpower);
3982 rt2x00_set_field32(&regs[TX_PWR_CFG_6_IDX],
3983 TX_PWR_CFG_6_MCS22_CH2, txpower);
3984
3985 /* MCS 23 */
3986 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
3987 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
3988 txpower, delta);
3989 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3990 TX_PWR_CFG_8_MCS23_CH0, txpower);
3991 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3992 TX_PWR_CFG_8_MCS23_CH1, txpower);
3993 rt2x00_set_field32(&regs[TX_PWR_CFG_8_IDX],
3994 TX_PWR_CFG_8_MCS23_CH2, txpower);
3995
3996 /* read the next four txpower values */
3997 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
3998 offset + 6, &eeprom);
3999
4000 /* STBC, MCS 0,1 */
4001 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
4002 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
4003 txpower, delta);
4004 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
4005 TX_PWR_CFG_3_STBC0_CH0, txpower);
4006 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
4007 TX_PWR_CFG_3_STBC0_CH1, txpower);
4008 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
4009 TX_PWR_CFG_3_EXT_STBC0_CH2, txpower);
4010
4011 /* STBC, MCS 2,3 */
4012 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE1);
4013 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
4014 txpower, delta);
4015 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
4016 TX_PWR_CFG_3_STBC2_CH0, txpower);
4017 rt2x00_set_field32(&regs[TX_PWR_CFG_3_IDX],
4018 TX_PWR_CFG_3_STBC2_CH1, txpower);
4019 rt2x00_set_field32(&regs[TX_PWR_CFG_3_EXT_IDX],
4020 TX_PWR_CFG_3_EXT_STBC2_CH2, txpower);
4021
4022 /* STBC, MCS 4,5 */
4023 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE2);
4024 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
4025 txpower, delta);
4026 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE0, txpower);
4027 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE1, txpower);
4028 rt2x00_set_field32(&regs[TX_PWR_CFG_4_EXT_IDX], TX_PWR_CFG_RATE0,
4029 txpower);
4030
4031 /* STBC, MCS 6 */
4032 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE3);
4033 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
4034 txpower, delta);
4035 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE2, txpower);
4036 rt2x00_set_field32(&regs[TX_PWR_CFG_4_IDX], TX_PWR_CFG_RATE3, txpower);
4037 rt2x00_set_field32(&regs[TX_PWR_CFG_4_EXT_IDX], TX_PWR_CFG_RATE2,
4038 txpower);
4039
4040 /* read the next four txpower values */
4041 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
4042 offset + 7, &eeprom);
4043
4044 /* STBC, MCS 7 */
4045 txpower = rt2x00_get_field16(eeprom, EEPROM_TXPOWER_BYRATE_RATE0);
4046 txpower = rt2800_compensate_txpower(rt2x00dev, 0, band, power_level,
4047 txpower, delta);
4048 rt2x00_set_field32(&regs[TX_PWR_CFG_9_IDX],
4049 TX_PWR_CFG_9_STBC7_CH0, txpower);
4050 rt2x00_set_field32(&regs[TX_PWR_CFG_9_IDX],
4051 TX_PWR_CFG_9_STBC7_CH1, txpower);
4052 rt2x00_set_field32(&regs[TX_PWR_CFG_9_IDX],
4053 TX_PWR_CFG_9_STBC7_CH2, txpower);
4054
4055 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0, regs[TX_PWR_CFG_0_IDX]);
4056 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1, regs[TX_PWR_CFG_1_IDX]);
4057 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2, regs[TX_PWR_CFG_2_IDX]);
4058 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3, regs[TX_PWR_CFG_3_IDX]);
4059 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4, regs[TX_PWR_CFG_4_IDX]);
4060 rt2800_register_write(rt2x00dev, TX_PWR_CFG_5, regs[TX_PWR_CFG_5_IDX]);
4061 rt2800_register_write(rt2x00dev, TX_PWR_CFG_6, regs[TX_PWR_CFG_6_IDX]);
4062 rt2800_register_write(rt2x00dev, TX_PWR_CFG_7, regs[TX_PWR_CFG_7_IDX]);
4063 rt2800_register_write(rt2x00dev, TX_PWR_CFG_8, regs[TX_PWR_CFG_8_IDX]);
4064 rt2800_register_write(rt2x00dev, TX_PWR_CFG_9, regs[TX_PWR_CFG_9_IDX]);
4065
4066 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0_EXT,
4067 regs[TX_PWR_CFG_0_EXT_IDX]);
4068 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1_EXT,
4069 regs[TX_PWR_CFG_1_EXT_IDX]);
4070 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2_EXT,
4071 regs[TX_PWR_CFG_2_EXT_IDX]);
4072 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3_EXT,
4073 regs[TX_PWR_CFG_3_EXT_IDX]);
4074 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4_EXT,
4075 regs[TX_PWR_CFG_4_EXT_IDX]);
4076
4077 for (i = 0; i < TX_PWR_CFG_IDX_COUNT; i++)
4078 rt2x00_dbg(rt2x00dev,
4079 "band:%cGHz, BW:%c0MHz, TX_PWR_CFG_%d%s = %08lx\n",
4080 (band == IEEE80211_BAND_5GHZ) ? '5' : '2',
4081 (test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags)) ?
4082 '4' : '2',
4083 (i > TX_PWR_CFG_9_IDX) ?
4084 (i - TX_PWR_CFG_9_IDX - 1) : i,
4085 (i > TX_PWR_CFG_9_IDX) ? "_EXT" : "",
4086 (unsigned long) regs[i]);
4087}
4088
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02004089/*
4090 * We configure transmit power using MAC TX_PWR_CFG_{0,...,N} registers and
4091 * BBP R1 register. TX_PWR_CFG_X allow to configure per rate TX power values,
4092 * 4 bits for each rate (tune from 0 to 15 dBm). BBP_R1 controls transmit power
4093 * for all rates, but allow to set only 4 discrete values: -12, -6, 0 and 6 dBm.
4094 * Reference per rate transmit power values are located in the EEPROM at
4095 * EEPROM_TXPOWER_BYRATE offset. We adjust them and BBP R1 settings according to
4096 * current conditions (i.e. band, bandwidth, temperature, user settings).
4097 */
Gabor Juhos34542ff2013-07-08 16:08:20 +02004098static void rt2800_config_txpower_rt28xx(struct rt2x00_dev *rt2x00dev,
4099 struct ieee80211_channel *chan,
4100 int power_level)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004101{
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02004102 u8 txpower, r1;
Helmut Schaa5e846002010-07-11 12:23:09 +02004103 u16 eeprom;
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02004104 u32 reg, offset;
4105 int i, is_rate_b, delta, power_ctrl;
Stanislaw Gruszka146c3b02012-10-05 13:44:13 +02004106 enum ieee80211_band band = chan->band;
Helmut Schaa2af242e2011-03-28 13:32:01 +02004107
4108 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02004109 * Calculate HT40 compensation. For 40MHz we need to add or subtract
4110 * value read from EEPROM (different for 2GHz and for 5GHz).
Helmut Schaa2af242e2011-03-28 13:32:01 +02004111 */
4112 delta = rt2800_get_txpower_bw_comp(rt2x00dev, band);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004113
Helmut Schaa5e846002010-07-11 12:23:09 +02004114 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02004115 * Calculate temperature compensation. Depends on measurement of current
4116 * TSSI (Transmitter Signal Strength Indication) we know TX power (due
4117 * to temperature or maybe other factors) is smaller or bigger than
4118 * expected. We adjust it, based on TSSI reference and boundaries values
4119 * provided in EEPROM.
Helmut Schaa9e33a352011-03-28 13:33:40 +02004120 */
Stanislaw Gruszka87dd2d72014-11-25 15:17:29 +01004121 switch (rt2x00dev->chip.rt) {
4122 case RT2860:
4123 case RT2872:
4124 case RT2883:
4125 case RT3070:
4126 case RT3071:
4127 case RT3090:
4128 case RT3572:
4129 delta += rt2800_get_gain_calibration_delta(rt2x00dev);
4130 break;
4131 default:
4132 /* TODO: temperature compensation code for other chips. */
4133 break;
4134 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004135
Helmut Schaa5e846002010-07-11 12:23:09 +02004136 /*
Stanislaw Gruszka7a662052012-10-05 13:44:15 +02004137 * Decrease power according to user settings, on devices with unknown
4138 * maximum tx power. For other devices we take user power_level into
4139 * consideration on rt2800_compensate_txpower().
Stanislaw Gruszka1e4cf242012-10-05 13:44:14 +02004140 */
4141 delta += rt2800_get_txpower_reg_delta(rt2x00dev, power_level,
4142 chan->max_power);
4143
4144 /*
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02004145 * BBP_R1 controls TX power for all rates, it allow to set the following
4146 * gains -12, -6, 0, +6 dBm by setting values 2, 1, 0, 3 respectively.
4147 *
4148 * TODO: we do not use +6 dBm option to do not increase power beyond
4149 * regulatory limit, however this could be utilized for devices with
4150 * CAPABILITY_POWER_LIMIT.
Helmut Schaa5e846002010-07-11 12:23:09 +02004151 */
Stanislaw Gruszka87dd2d72014-11-25 15:17:29 +01004152 if (delta <= -12) {
4153 power_ctrl = 2;
4154 delta += 12;
4155 } else if (delta <= -6) {
4156 power_ctrl = 1;
4157 delta += 6;
4158 } else {
4159 power_ctrl = 0;
Stanislaw Gruszkacee2c732012-10-05 13:44:09 +02004160 }
Stanislaw Gruszka87dd2d72014-11-25 15:17:29 +01004161 rt2800_bbp_read(rt2x00dev, 1, &r1);
4162 rt2x00_set_field8(&r1, BBP1_TX_POWER_CTRL, power_ctrl);
4163 rt2800_bbp_write(rt2x00dev, 1, r1);
Stanislaw Gruszka8c8d20172013-06-11 18:48:53 +02004164
Helmut Schaa5e846002010-07-11 12:23:09 +02004165 offset = TX_PWR_CFG_0;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004166
Helmut Schaa5e846002010-07-11 12:23:09 +02004167 for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
4168 /* just to be safe */
4169 if (offset > TX_PWR_CFG_4)
4170 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004171
Helmut Schaa5e846002010-07-11 12:23:09 +02004172 rt2800_register_read(rt2x00dev, offset, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004173
Helmut Schaa5e846002010-07-11 12:23:09 +02004174 /* read the next four txpower values */
Gabor Juhos022138c2013-07-08 11:25:54 +02004175 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
4176 i, &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004177
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004178 is_rate_b = i ? 0 : 1;
4179 /*
4180 * TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02004181 * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004182 * TX_PWR_CFG_4: unknown
4183 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004184 txpower = rt2x00_get_field16(eeprom,
4185 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02004186 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004187 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004188 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004189
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004190 /*
4191 * TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02004192 * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004193 * TX_PWR_CFG_4: unknown
4194 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004195 txpower = rt2x00_get_field16(eeprom,
4196 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02004197 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004198 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004199 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004200
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004201 /*
4202 * TX_PWR_CFG_0: 5.5MBS, TX_PWR_CFG_1: 48MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02004203 * TX_PWR_CFG_2: MCS6, TX_PWR_CFG_3: MCS14,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004204 * TX_PWR_CFG_4: unknown
4205 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004206 txpower = rt2x00_get_field16(eeprom,
4207 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02004208 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004209 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004210 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004211
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004212 /*
4213 * TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02004214 * TX_PWR_CFG_2: MCS7, TX_PWR_CFG_3: MCS15,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004215 * TX_PWR_CFG_4: unknown
4216 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004217 txpower = rt2x00_get_field16(eeprom,
4218 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02004219 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004220 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004221 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004222
4223 /* read the next four txpower values */
Gabor Juhos022138c2013-07-08 11:25:54 +02004224 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_TXPOWER_BYRATE,
4225 i + 1, &eeprom);
Helmut Schaa5e846002010-07-11 12:23:09 +02004226
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004227 is_rate_b = 0;
4228 /*
4229 * TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
Helmut Schaa5e846002010-07-11 12:23:09 +02004230 * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004231 * TX_PWR_CFG_4: unknown
4232 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004233 txpower = rt2x00_get_field16(eeprom,
4234 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02004235 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004236 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004237 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004238
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004239 /*
4240 * TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
Helmut Schaa5e846002010-07-11 12:23:09 +02004241 * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004242 * TX_PWR_CFG_4: unknown
4243 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004244 txpower = rt2x00_get_field16(eeprom,
4245 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02004246 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004247 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004248 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004249
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004250 /*
4251 * TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
Helmut Schaa5e846002010-07-11 12:23:09 +02004252 * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004253 * TX_PWR_CFG_4: unknown
4254 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004255 txpower = rt2x00_get_field16(eeprom,
4256 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02004257 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004258 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004259 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004260
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004261 /*
4262 * TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
Helmut Schaa5e846002010-07-11 12:23:09 +02004263 * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004264 * TX_PWR_CFG_4: unknown
4265 */
Helmut Schaa5e846002010-07-11 12:23:09 +02004266 txpower = rt2x00_get_field16(eeprom,
4267 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02004268 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02004269 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004270 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02004271
4272 rt2800_register_write(rt2x00dev, offset, reg);
4273
4274 /* next TX_PWR_CFG register */
4275 offset += 4;
4276 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004277}
4278
Gabor Juhos34542ff2013-07-08 16:08:20 +02004279static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
4280 struct ieee80211_channel *chan,
4281 int power_level)
4282{
4283 if (rt2x00_rt(rt2x00dev, RT3593))
4284 rt2800_config_txpower_rt3593(rt2x00dev, chan, power_level);
4285 else
4286 rt2800_config_txpower_rt28xx(rt2x00dev, chan, power_level);
4287}
4288
Helmut Schaa9e33a352011-03-28 13:33:40 +02004289void rt2800_gain_calibration(struct rt2x00_dev *rt2x00dev)
4290{
Karl Beldan675a0b02013-03-25 16:26:57 +01004291 rt2800_config_txpower(rt2x00dev, rt2x00dev->hw->conf.chandef.chan,
Helmut Schaa9e33a352011-03-28 13:33:40 +02004292 rt2x00dev->tx_power);
4293}
4294EXPORT_SYMBOL_GPL(rt2800_gain_calibration);
4295
John Li2e9c43d2012-02-16 21:40:57 +08004296void rt2800_vco_calibration(struct rt2x00_dev *rt2x00dev)
4297{
4298 u32 tx_pin;
4299 u8 rfcsr;
4300
4301 /*
4302 * A voltage-controlled oscillator(VCO) is an electronic oscillator
4303 * designed to be controlled in oscillation frequency by a voltage
4304 * input. Maybe the temperature will affect the frequency of
4305 * oscillation to be shifted. The VCO calibration will be called
4306 * periodically to adjust the frequency to be precision.
4307 */
4308
4309 rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
4310 tx_pin &= TX_PIN_CFG_PA_PE_DISABLE;
4311 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
4312
4313 switch (rt2x00dev->chip.rf) {
4314 case RF2020:
4315 case RF3020:
4316 case RF3021:
4317 case RF3022:
4318 case RF3320:
4319 case RF3052:
4320 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
4321 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
4322 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
4323 break;
Gabor Juhos1095df02013-07-08 16:08:31 +02004324 case RF3053:
Stanislaw Gruszka3b9b74b2013-09-25 15:34:55 +02004325 case RF3070:
Woody Hunga89534e2012-06-13 15:01:16 +08004326 case RF3290:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02004327 case RF5360:
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05004328 case RF5362:
John Li2e9c43d2012-02-16 21:40:57 +08004329 case RF5370:
4330 case RF5372:
4331 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08004332 case RF5392:
John Li2e9c43d2012-02-16 21:40:57 +08004333 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
Gabor Juhosd6d82022012-12-02 18:34:47 +01004334 rt2x00_set_field8(&rfcsr, RFCSR3_VCOCAL_EN, 1);
John Li2e9c43d2012-02-16 21:40:57 +08004335 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
4336 break;
4337 default:
4338 return;
4339 }
4340
4341 mdelay(1);
4342
4343 rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
4344 if (rt2x00dev->rf_channel <= 14) {
4345 switch (rt2x00dev->default_ant.tx_chain_num) {
4346 case 3:
4347 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G2_EN, 1);
4348 /* fall through */
4349 case 2:
4350 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
4351 /* fall through */
4352 case 1:
4353 default:
4354 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
4355 break;
4356 }
4357 } else {
4358 switch (rt2x00dev->default_ant.tx_chain_num) {
4359 case 3:
4360 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A2_EN, 1);
4361 /* fall through */
4362 case 2:
4363 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
4364 /* fall through */
4365 case 1:
4366 default:
4367 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, 1);
4368 break;
4369 }
4370 }
4371 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
4372
4373}
4374EXPORT_SYMBOL_GPL(rt2800_vco_calibration);
4375
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004376static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
4377 struct rt2x00lib_conf *libconf)
4378{
4379 u32 reg;
4380
4381 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
4382 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
4383 libconf->conf->short_frame_max_tx_count);
4384 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
4385 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004386 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
4387}
4388
4389static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
4390 struct rt2x00lib_conf *libconf)
4391{
4392 enum dev_state state =
4393 (libconf->conf->flags & IEEE80211_CONF_PS) ?
4394 STATE_SLEEP : STATE_AWAKE;
4395 u32 reg;
4396
4397 if (state == STATE_SLEEP) {
4398 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
4399
4400 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
4401 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
4402 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
4403 libconf->conf->listen_interval - 1);
4404 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
4405 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
4406
4407 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
4408 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004409 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
4410 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
4411 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
4412 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
4413 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02004414
4415 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004416 }
4417}
4418
4419void rt2800_config(struct rt2x00_dev *rt2x00dev,
4420 struct rt2x00lib_conf *libconf,
4421 const unsigned int flags)
4422{
4423 /* Always recalculate LNA gain before changing configuration */
4424 rt2800_config_lna_gain(rt2x00dev, libconf);
4425
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004426 if (flags & IEEE80211_CONF_CHANGE_CHANNEL) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004427 rt2800_config_channel(rt2x00dev, libconf->conf,
4428 &libconf->rf, &libconf->channel);
Karl Beldan675a0b02013-03-25 16:26:57 +01004429 rt2800_config_txpower(rt2x00dev, libconf->conf->chandef.chan,
Helmut Schaa9e33a352011-03-28 13:33:40 +02004430 libconf->conf->power_level);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004431 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004432 if (flags & IEEE80211_CONF_CHANGE_POWER)
Karl Beldan675a0b02013-03-25 16:26:57 +01004433 rt2800_config_txpower(rt2x00dev, libconf->conf->chandef.chan,
Helmut Schaa9e33a352011-03-28 13:33:40 +02004434 libconf->conf->power_level);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004435 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
4436 rt2800_config_retry_limit(rt2x00dev, libconf);
4437 if (flags & IEEE80211_CONF_CHANGE_PS)
4438 rt2800_config_ps(rt2x00dev, libconf);
4439}
4440EXPORT_SYMBOL_GPL(rt2800_config);
4441
4442/*
4443 * Link tuning
4444 */
4445void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
4446{
4447 u32 reg;
4448
4449 /*
4450 * Update FCS error count from register.
4451 */
4452 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
4453 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
4454}
4455EXPORT_SYMBOL_GPL(rt2800_link_stats);
4456
4457static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
4458{
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02004459 u8 vgc;
4460
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004461 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004462 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004463 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004464 rt2x00_rt(rt2x00dev, RT3090) ||
Woody Hunga89534e2012-06-13 15:01:16 +08004465 rt2x00_rt(rt2x00dev, RT3290) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01004466 rt2x00_rt(rt2x00dev, RT3390) ||
Gertjan van Wingerded961e442012-09-16 22:29:50 +02004467 rt2x00_rt(rt2x00dev, RT3572) ||
Gabor Juhos0ffd2a92013-10-03 20:00:42 +02004468 rt2x00_rt(rt2x00dev, RT3593) ||
John Li2ed71882012-02-17 17:33:06 +08004469 rt2x00_rt(rt2x00dev, RT5390) ||
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004470 rt2x00_rt(rt2x00dev, RT5392) ||
4471 rt2x00_rt(rt2x00dev, RT5592))
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02004472 vgc = 0x1c + (2 * rt2x00dev->lna_gain);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004473 else
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02004474 vgc = 0x2e + rt2x00dev->lna_gain;
4475 } else { /* 5GHZ band */
Gabor Juhos733aec62013-10-04 22:07:09 +02004476 if (rt2x00_rt(rt2x00dev, RT3593))
Gabor Juhos0ffd2a92013-10-03 20:00:42 +02004477 vgc = 0x20 + (rt2x00dev->lna_gain * 5) / 3;
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004478 else if (rt2x00_rt(rt2x00dev, RT5592))
4479 vgc = 0x24 + (2 * rt2x00dev->lna_gain);
Gertjan van Wingerded961e442012-09-16 22:29:50 +02004480 else {
4481 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
4482 vgc = 0x32 + (rt2x00dev->lna_gain * 5) / 3;
4483 else
4484 vgc = 0x3a + (rt2x00dev->lna_gain * 5) / 3;
4485 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004486 }
4487
Gertjan van Wingerde8c6728b2012-09-16 22:29:49 +02004488 return vgc;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004489}
4490
4491static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
4492 struct link_qual *qual, u8 vgc_level)
4493{
4494 if (qual->vgc_level != vgc_level) {
Gabor Juhos271f1a42013-10-03 20:00:43 +02004495 if (rt2x00_rt(rt2x00dev, RT3572) ||
4496 rt2x00_rt(rt2x00dev, RT3593)) {
4497 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66,
4498 vgc_level);
4499 } else if (rt2x00_rt(rt2x00dev, RT5592)) {
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004500 rt2800_bbp_write(rt2x00dev, 83, qual->rssi > -65 ? 0x4a : 0x7a);
4501 rt2800_bbp_write_with_rx_chain(rt2x00dev, 66, vgc_level);
Gabor Juhos271f1a42013-10-03 20:00:43 +02004502 } else {
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004503 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
Gabor Juhos271f1a42013-10-03 20:00:43 +02004504 }
4505
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004506 qual->vgc_level = vgc_level;
4507 qual->vgc_level_reg = vgc_level;
4508 }
4509}
4510
4511void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
4512{
4513 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
4514}
4515EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
4516
4517void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
4518 const u32 count)
4519{
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004520 u8 vgc;
4521
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02004522 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004523 return;
Gabor Juhose25aa822013-10-03 20:00:41 +02004524
4525 /* When RSSI is better than a certain threshold, increase VGC
4526 * with a chip specific value in order to improve the balance
4527 * between sensibility and noise isolation.
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004528 */
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004529
4530 vgc = rt2800_get_default_vgc(rt2x00dev);
4531
Gabor Juhose25aa822013-10-03 20:00:41 +02004532 switch (rt2x00dev->chip.rt) {
4533 case RT3572:
4534 case RT3593:
4535 if (qual->rssi > -65) {
4536 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ)
4537 vgc += 0x20;
4538 else
4539 vgc += 0x10;
4540 }
4541 break;
4542
4543 case RT5592:
Gabor Juhos0beb1bb2013-10-03 20:00:40 +02004544 if (qual->rssi > -65)
4545 vgc += 0x20;
Gabor Juhose25aa822013-10-03 20:00:41 +02004546 break;
4547
4548 default:
Gabor Juhos0beb1bb2013-10-03 20:00:40 +02004549 if (qual->rssi > -80)
4550 vgc += 0x10;
Gabor Juhose25aa822013-10-03 20:00:41 +02004551 break;
Gabor Juhos0beb1bb2013-10-03 20:00:40 +02004552 }
Stanislaw Gruszka3d815352013-03-16 19:19:49 +01004553
4554 rt2800_set_vgc(rt2x00dev, qual, vgc);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01004555}
4556EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004557
4558/*
4559 * Initialization functions.
4560 */
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02004561static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004562{
4563 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004564 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004565 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02004566 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004567
Jakub Kicinskif7b395e2012-04-03 03:40:47 +02004568 rt2800_disable_wpdma(rt2x00dev);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004569
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02004570 ret = rt2800_drv_init_registers(rt2x00dev);
4571 if (ret)
4572 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004573
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004574 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
4575 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
4576
4577 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
4578
4579 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Helmut Schaa8544df32010-07-11 12:29:49 +02004580 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004581 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
4582 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
4583 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
4584 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
4585 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
4586 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
4587
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004588 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
4589
4590 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
4591 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
4592 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
4593 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
4594
Woody Hunga89534e2012-06-13 15:01:16 +08004595 if (rt2x00_rt(rt2x00dev, RT3290)) {
4596 rt2800_register_read(rt2x00dev, WLAN_FUN_CTRL, &reg);
4597 if (rt2x00_get_field32(reg, WLAN_EN) == 1) {
4598 rt2x00_set_field32(&reg, PCIE_APP0_CLK_REQ, 1);
4599 rt2800_register_write(rt2x00dev, WLAN_FUN_CTRL, reg);
4600 }
4601
4602 rt2800_register_read(rt2x00dev, CMB_CTRL, &reg);
4603 if (!(rt2x00_get_field32(reg, LDO0_EN) == 1)) {
4604 rt2x00_set_field32(&reg, LDO0_EN, 1);
4605 rt2x00_set_field32(&reg, LDO_BGSEL, 3);
4606 rt2800_register_write(rt2x00dev, CMB_CTRL, reg);
4607 }
4608
4609 rt2800_register_read(rt2x00dev, OSC_CTRL, &reg);
4610 rt2x00_set_field32(&reg, OSC_ROSC_EN, 1);
4611 rt2x00_set_field32(&reg, OSC_CAL_REQ, 1);
4612 rt2x00_set_field32(&reg, OSC_REF_CYCLE, 0x27);
4613 rt2800_register_write(rt2x00dev, OSC_CTRL, reg);
4614
4615 rt2800_register_read(rt2x00dev, COEX_CFG0, &reg);
4616 rt2x00_set_field32(&reg, COEX_CFG_ANT, 0x5e);
4617 rt2800_register_write(rt2x00dev, COEX_CFG0, reg);
4618
4619 rt2800_register_read(rt2x00dev, COEX_CFG2, &reg);
4620 rt2x00_set_field32(&reg, BT_COEX_CFG1, 0x00);
4621 rt2x00_set_field32(&reg, BT_COEX_CFG0, 0x17);
4622 rt2x00_set_field32(&reg, WL_COEX_CFG1, 0x93);
4623 rt2x00_set_field32(&reg, WL_COEX_CFG0, 0x7f);
4624 rt2800_register_write(rt2x00dev, COEX_CFG2, reg);
4625
4626 rt2800_register_read(rt2x00dev, PLL_CTRL, &reg);
4627 rt2x00_set_field32(&reg, PLL_CONTROL, 1);
4628 rt2800_register_write(rt2x00dev, PLL_CTRL, reg);
4629 }
4630
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004631 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004632 rt2x00_rt(rt2x00dev, RT3090) ||
Woody Hunga89534e2012-06-13 15:01:16 +08004633 rt2x00_rt(rt2x00dev, RT3290) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004634 rt2x00_rt(rt2x00dev, RT3390)) {
Woody Hunga89534e2012-06-13 15:01:16 +08004635
4636 if (rt2x00_rt(rt2x00dev, RT3290))
4637 rt2800_register_write(rt2x00dev, TX_SW_CFG0,
4638 0x00000404);
4639 else
4640 rt2800_register_write(rt2x00dev, TX_SW_CFG0,
4641 0x00000400);
4642
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004643 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02004644 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02004645 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
4646 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02004647 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1,
4648 &eeprom);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004649 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004650 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4651 0x0000002c);
4652 else
4653 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4654 0x0000000f);
4655 } else {
4656 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
4657 }
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02004658 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004659 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02004660
4661 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
4662 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
4663 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
4664 } else {
4665 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4666 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
4667 }
Helmut Schaac295a812010-06-03 10:52:13 +02004668 } else if (rt2800_is_305x_soc(rt2x00dev)) {
4669 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
4670 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Helmut Schaa961636b2011-04-18 15:28:27 +02004671 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000030);
Daniel Golle03839952012-09-09 14:24:39 +03004672 } else if (rt2x00_rt(rt2x00dev, RT3352)) {
4673 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000402);
4674 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4675 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Gertjan van Wingerde872834d2011-05-18 20:25:31 +02004676 } else if (rt2x00_rt(rt2x00dev, RT3572)) {
4677 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
4678 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
Gabor Juhos1706d152013-07-08 16:08:16 +02004679 } else if (rt2x00_rt(rt2x00dev, RT3593)) {
4680 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000402);
4681 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
4682 if (rt2x00_rt_rev_lt(rt2x00dev, RT3593, REV_RT3593E)) {
4683 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1,
4684 &eeprom);
4685 if (rt2x00_get_field16(eeprom,
4686 EEPROM_NIC_CONF1_DAC_TEST))
4687 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4688 0x0000001f);
4689 else
4690 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4691 0x0000000f);
4692 } else {
4693 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
4694 0x00000000);
4695 }
John Li2ed71882012-02-17 17:33:06 +08004696 } else if (rt2x00_rt(rt2x00dev, RT5390) ||
Stanislaw Gruszka76413282013-03-16 19:19:33 +01004697 rt2x00_rt(rt2x00dev, RT5392) ||
4698 rt2x00_rt(rt2x00dev, RT5592)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01004699 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000404);
4700 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4701 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004702 } else {
4703 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
4704 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
4705 }
4706
4707 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
4708 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
4709 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
4710 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
4711 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
4712 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
4713 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
4714 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
4715 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
4716 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
4717
4718 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
4719 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004720 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004721 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
4722 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
4723
4724 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
4725 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02004726 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01004727 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02004728 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004729 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
4730 else
4731 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
4732 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
4733 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
4734 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
4735
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004736 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
4737 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
4738 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
4739 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
4740 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
4741 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
4742 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
4743 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
4744 rt2800_register_write(rt2x00dev, LED_CFG, reg);
4745
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004746 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
4747
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004748 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
4749 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
4750 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
4751 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
4752 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
4753 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
4754 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
4755 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
4756
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004757 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
4758 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004759 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004760 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
4761 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004762 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004763 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
4764 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
4765 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
4766
4767 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004768 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004769 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004770 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004771 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4772 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4773 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004774 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004775 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004776 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
4777 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004778 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
4779
4780 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004781 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004782 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004783 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004784 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4785 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4786 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004787 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004788 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004789 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
4790 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004791 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
4792
4793 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
4794 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
4795 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004796 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004797 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4798 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4799 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4800 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
4801 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4802 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004803 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004804 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
4805
4806 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
4807 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Helmut Schaad13a97f2010-10-02 11:29:08 +02004808 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004809 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004810 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4811 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4812 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4813 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
4814 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4815 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004816 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004817 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
4818
4819 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
4820 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
4821 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004822 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004823 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4824 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4825 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4826 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
4827 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4828 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004829 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004830 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
4831
4832 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
4833 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
4834 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01004835 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004836 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
4837 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
4838 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
4839 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
4840 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
4841 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004842 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004843 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
4844
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01004845 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004846 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
4847
4848 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
4849 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
4850 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
4851 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
4852 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
4853 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
4854 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
4855 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
4856 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
4857 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
4858 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
4859 }
4860
Helmut Schaa961621a2010-11-04 20:36:59 +01004861 /*
4862 * The legacy driver also sets TXOP_CTRL_CFG_RESERVED_TRUN_EN to 1
4863 * although it is reserved.
4864 */
4865 rt2800_register_read(rt2x00dev, TXOP_CTRL_CFG, &reg);
4866 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TIMEOUT_TRUN_EN, 1);
4867 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_AC_TRUN_EN, 1);
4868 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TXRATEGRP_TRUN_EN, 1);
4869 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_USER_MODE_TRUN_EN, 1);
4870 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_MIMO_PS_TRUN_EN, 1);
4871 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_RESERVED_TRUN_EN, 1);
4872 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_LSIG_TXOP_EN, 0);
4873 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_EN, 0);
4874 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_DLY, 88);
4875 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CWMIN, 0);
4876 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, reg);
4877
Stanislaw Gruszka76413282013-03-16 19:19:33 +01004878 reg = rt2x00_rt(rt2x00dev, RT5592) ? 0x00000082 : 0x00000002;
4879 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004880
4881 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
4882 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
4883 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
4884 IEEE80211_MAX_RTS_THRESHOLD);
4885 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
4886 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
4887
4888 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004889
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02004890 /*
4891 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
4892 * time should be set to 16. However, the original Ralink driver uses
4893 * 16 for both and indeed using a value of 10 for CCK SIFS results in
4894 * connection problems with 11g + CTS protection. Hence, use the same
4895 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
4896 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004897 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02004898 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
4899 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02004900 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
4901 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
4902 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
4903 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
4904
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004905 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
4906
4907 /*
4908 * ASIC will keep garbage value after boot, clear encryption keys.
4909 */
4910 for (i = 0; i < 4; i++)
4911 rt2800_register_write(rt2x00dev,
4912 SHARED_KEY_MODE_ENTRY(i), 0);
4913
4914 for (i = 0; i < 256; i++) {
Helmut Schaad7d259d2011-09-08 14:39:04 +02004915 rt2800_config_wcid(rt2x00dev, NULL, i);
4916 rt2800_delete_wcid_attr(rt2x00dev, i);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004917 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
4918 }
4919
4920 /*
4921 * Clear all beacons
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004922 */
Gabor Juhos77f7c0f2013-08-17 00:15:50 +02004923 for (i = 0; i < 8; i++)
4924 rt2800_clear_beacon_register(rt2x00dev, i);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004925
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01004926 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02004927 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
4928 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
4929 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
RA-Jay Hungc6fcc0e2011-01-30 13:21:22 +01004930 } else if (rt2x00_is_pcie(rt2x00dev)) {
4931 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
4932 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 125);
4933 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004934 }
4935
4936 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
4937 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
4938 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
4939 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
4940 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
4941 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
4942 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
4943 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
4944 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
4945 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
4946
4947 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
4948 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
4949 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
4950 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
4951 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
4952 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
4953 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
4954 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
4955 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
4956 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
4957
4958 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
4959 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
4960 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
4961 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
4962 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
4963 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
4964 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
4965 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
4966 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
4967 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
4968
4969 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
4970 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
4971 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
4972 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
4973 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
4974 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
4975
4976 /*
Helmut Schaa47ee3eb2010-09-08 20:56:04 +02004977 * Do not force the BA window size, we use the TXWI to set it
4978 */
4979 rt2800_register_read(rt2x00dev, AMPDU_BA_WINSIZE, &reg);
4980 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE_ENABLE, 0);
4981 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE, 0);
4982 rt2800_register_write(rt2x00dev, AMPDU_BA_WINSIZE, reg);
4983
4984 /*
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01004985 * We must clear the error counters.
4986 * These registers are cleared on read,
4987 * so we may pass a useless variable to store the value.
4988 */
4989 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
4990 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
4991 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
4992 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
4993 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
4994 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
4995
Helmut Schaa9f926fb2010-07-11 12:28:23 +02004996 /*
4997 * Setup leadtime for pre tbtt interrupt to 6ms
4998 */
4999 rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
5000 rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
5001 rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
5002
Helmut Schaa977206d2010-12-13 12:31:58 +01005003 /*
5004 * Set up channel statistics timer
5005 */
5006 rt2800_register_read(rt2x00dev, CH_TIME_CFG, &reg);
5007 rt2x00_set_field32(&reg, CH_TIME_CFG_EIFS_BUSY, 1);
5008 rt2x00_set_field32(&reg, CH_TIME_CFG_NAV_BUSY, 1);
5009 rt2x00_set_field32(&reg, CH_TIME_CFG_RX_BUSY, 1);
5010 rt2x00_set_field32(&reg, CH_TIME_CFG_TX_BUSY, 1);
5011 rt2x00_set_field32(&reg, CH_TIME_CFG_TMR_EN, 1);
5012 rt2800_register_write(rt2x00dev, CH_TIME_CFG, reg);
5013
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005014 return 0;
5015}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005016
5017static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
5018{
5019 unsigned int i;
5020 u32 reg;
5021
5022 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
5023 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
5024 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
5025 return 0;
5026
5027 udelay(REGISTER_BUSY_DELAY);
5028 }
5029
Joe Perchesec9c4982013-04-19 08:33:40 -07005030 rt2x00_err(rt2x00dev, "BBP/RF register access failed, aborting\n");
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005031 return -EACCES;
5032}
5033
5034static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
5035{
5036 unsigned int i;
5037 u8 value;
5038
5039 /*
5040 * BBP was enabled after firmware was loaded,
5041 * but we need to reactivate it now.
5042 */
5043 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
5044 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
5045 msleep(1);
5046
5047 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
5048 rt2800_bbp_read(rt2x00dev, 0, &value);
5049 if ((value != 0xff) && (value != 0x00))
5050 return 0;
5051 udelay(REGISTER_BUSY_DELAY);
5052 }
5053
Joe Perchesec9c4982013-04-19 08:33:40 -07005054 rt2x00_err(rt2x00dev, "BBP register access failed, aborting\n");
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005055 return -EACCES;
5056}
5057
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005058static void rt2800_bbp4_mac_if_ctrl(struct rt2x00_dev *rt2x00dev)
5059{
5060 u8 value;
5061
5062 rt2800_bbp_read(rt2x00dev, 4, &value);
5063 rt2x00_set_field8(&value, BBP4_MAC_IF_CTRL, 1);
5064 rt2800_bbp_write(rt2x00dev, 4, value);
5065}
5066
Stanislaw Gruszkac2675482013-03-16 19:19:41 +01005067static void rt2800_init_freq_calibration(struct rt2x00_dev *rt2x00dev)
5068{
5069 rt2800_bbp_write(rt2x00dev, 142, 1);
5070 rt2800_bbp_write(rt2x00dev, 143, 57);
5071}
5072
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005073static void rt2800_init_bbp_5592_glrt(struct rt2x00_dev *rt2x00dev)
5074{
5075 const u8 glrt_table[] = {
5076 0xE0, 0x1F, 0X38, 0x32, 0x08, 0x28, 0x19, 0x0A, 0xFF, 0x00, /* 128 ~ 137 */
5077 0x16, 0x10, 0x10, 0x0B, 0x36, 0x2C, 0x26, 0x24, 0x42, 0x36, /* 138 ~ 147 */
5078 0x30, 0x2D, 0x4C, 0x46, 0x3D, 0x40, 0x3E, 0x42, 0x3D, 0x40, /* 148 ~ 157 */
5079 0X3C, 0x34, 0x2C, 0x2F, 0x3C, 0x35, 0x2E, 0x2A, 0x49, 0x41, /* 158 ~ 167 */
5080 0x36, 0x31, 0x30, 0x30, 0x0E, 0x0D, 0x28, 0x21, 0x1C, 0x16, /* 168 ~ 177 */
5081 0x50, 0x4A, 0x43, 0x40, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, /* 178 ~ 187 */
5082 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 188 ~ 197 */
5083 0x00, 0x00, 0x7D, 0x14, 0x32, 0x2C, 0x36, 0x4C, 0x43, 0x2C, /* 198 ~ 207 */
5084 0x2E, 0x36, 0x30, 0x6E, /* 208 ~ 211 */
5085 };
5086 int i;
5087
5088 for (i = 0; i < ARRAY_SIZE(glrt_table); i++) {
5089 rt2800_bbp_write(rt2x00dev, 195, 128 + i);
5090 rt2800_bbp_write(rt2x00dev, 196, glrt_table[i]);
5091 }
5092};
5093
Gabor Juhos624708b2013-04-19 10:13:52 +02005094static void rt2800_init_bbp_early(struct rt2x00_dev *rt2x00dev)
Stanislaw Gruszkaa4969d02013-03-16 19:19:35 +01005095{
5096 rt2800_bbp_write(rt2x00dev, 65, 0x2C);
5097 rt2800_bbp_write(rt2x00dev, 66, 0x38);
5098 rt2800_bbp_write(rt2x00dev, 68, 0x0B);
5099 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5100 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
5101 rt2800_bbp_write(rt2x00dev, 73, 0x10);
5102 rt2800_bbp_write(rt2x00dev, 81, 0x37);
5103 rt2800_bbp_write(rt2x00dev, 82, 0x62);
5104 rt2800_bbp_write(rt2x00dev, 83, 0x6A);
5105 rt2800_bbp_write(rt2x00dev, 84, 0x99);
5106 rt2800_bbp_write(rt2x00dev, 86, 0x00);
5107 rt2800_bbp_write(rt2x00dev, 91, 0x04);
5108 rt2800_bbp_write(rt2x00dev, 92, 0x00);
5109 rt2800_bbp_write(rt2x00dev, 103, 0x00);
5110 rt2800_bbp_write(rt2x00dev, 105, 0x05);
5111 rt2800_bbp_write(rt2x00dev, 106, 0x35);
5112}
5113
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005114static void rt2800_disable_unused_dac_adc(struct rt2x00_dev *rt2x00dev)
5115{
5116 u16 eeprom;
5117 u8 value;
5118
5119 rt2800_bbp_read(rt2x00dev, 138, &value);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005120 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005121 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
5122 value |= 0x20;
5123 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
5124 value &= ~0x02;
5125 rt2800_bbp_write(rt2x00dev, 138, value);
5126}
5127
Stanislaw Gruszkadae62952013-05-18 14:03:26 +02005128static void rt2800_init_bbp_305x_soc(struct rt2x00_dev *rt2x00dev)
5129{
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02005130 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005131
5132 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5133 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005134
5135 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5136 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005137
5138 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005139
5140 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
5141 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005142
5143 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005144
5145 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005146
5147 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005148
5149 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005150
5151 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005152
5153 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005154
5155 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005156
5157 rt2800_bbp_write(rt2x00dev, 105, 0x01);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005158
5159 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszkadae62952013-05-18 14:03:26 +02005160}
5161
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005162static void rt2800_init_bbp_28xx(struct rt2x00_dev *rt2x00dev)
5163{
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005164 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5165 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005166
5167 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
5168 rt2800_bbp_write(rt2x00dev, 69, 0x16);
5169 rt2800_bbp_write(rt2x00dev, 73, 0x12);
5170 } else {
5171 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5172 rt2800_bbp_write(rt2x00dev, 73, 0x10);
5173 }
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005174
5175 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005176
5177 rt2800_bbp_write(rt2x00dev, 81, 0x37);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005178
5179 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005180
5181 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005182
5183 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
5184 rt2800_bbp_write(rt2x00dev, 84, 0x19);
5185 else
5186 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005187
5188 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005189
5190 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005191
5192 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005193
5194 rt2800_bbp_write(rt2x00dev, 103, 0x00);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005195
5196 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005197
5198 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005199}
5200
5201static void rt2800_init_bbp_30xx(struct rt2x00_dev *rt2x00dev)
5202{
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005203 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5204 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005205
5206 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5207 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005208
5209 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005210
5211 rt2800_bbp_write(rt2x00dev, 79, 0x13);
5212 rt2800_bbp_write(rt2x00dev, 80, 0x05);
5213 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005214
5215 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005216
5217 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005218
5219 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005220
5221 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005222
5223 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005224
5225 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005226
5227 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
5228 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
5229 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E))
5230 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
5231 else
5232 rt2800_bbp_write(rt2x00dev, 103, 0x00);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005233
5234 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005235
5236 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005237
5238 if (rt2x00_rt(rt2x00dev, RT3071) ||
5239 rt2x00_rt(rt2x00dev, RT3090))
5240 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005241}
5242
5243static void rt2800_init_bbp_3290(struct rt2x00_dev *rt2x00dev)
5244{
Stanislaw Gruszka6addb242013-05-18 14:03:54 +02005245 u8 value;
5246
Stanislaw Gruszkac3223572013-05-18 14:03:28 +02005247 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02005248
5249 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005250
5251 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5252 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka59dcabb2013-05-18 14:03:32 +02005253
5254 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005255
5256 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5257 rt2800_bbp_write(rt2x00dev, 73, 0x13);
5258 rt2800_bbp_write(rt2x00dev, 75, 0x46);
5259 rt2800_bbp_write(rt2x00dev, 76, 0x28);
5260
5261 rt2800_bbp_write(rt2x00dev, 77, 0x58);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005262
5263 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005264
5265 rt2800_bbp_write(rt2x00dev, 74, 0x0b);
5266 rt2800_bbp_write(rt2x00dev, 79, 0x18);
5267 rt2800_bbp_write(rt2x00dev, 80, 0x09);
5268 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005269
5270 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005271
5272 rt2800_bbp_write(rt2x00dev, 83, 0x7a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005273
5274 rt2800_bbp_write(rt2x00dev, 84, 0x9a);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005275
5276 rt2800_bbp_write(rt2x00dev, 86, 0x38);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005277
5278 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005279
5280 rt2800_bbp_write(rt2x00dev, 92, 0x02);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005281
5282 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka1ad44082013-05-18 14:03:45 +02005283
5284 rt2800_bbp_write(rt2x00dev, 104, 0x92);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005285
5286 rt2800_bbp_write(rt2x00dev, 105, 0x1c);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005287
5288 rt2800_bbp_write(rt2x00dev, 106, 0x03);
Stanislaw Gruszkaf2b67772013-05-18 14:03:49 +02005289
5290 rt2800_bbp_write(rt2x00dev, 128, 0x12);
Stanislaw Gruszka6addb242013-05-18 14:03:54 +02005291
5292 rt2800_bbp_write(rt2x00dev, 67, 0x24);
5293 rt2800_bbp_write(rt2x00dev, 143, 0x04);
5294 rt2800_bbp_write(rt2x00dev, 142, 0x99);
5295 rt2800_bbp_write(rt2x00dev, 150, 0x30);
5296 rt2800_bbp_write(rt2x00dev, 151, 0x2e);
5297 rt2800_bbp_write(rt2x00dev, 152, 0x20);
5298 rt2800_bbp_write(rt2x00dev, 153, 0x34);
5299 rt2800_bbp_write(rt2x00dev, 154, 0x40);
5300 rt2800_bbp_write(rt2x00dev, 155, 0x3b);
5301 rt2800_bbp_write(rt2x00dev, 253, 0x04);
5302
5303 rt2800_bbp_read(rt2x00dev, 47, &value);
5304 rt2x00_set_field8(&value, BBP47_TSSI_ADC6, 1);
5305 rt2800_bbp_write(rt2x00dev, 47, value);
5306
5307 /* Use 5-bit ADC for Acquisition and 8-bit ADC for data */
5308 rt2800_bbp_read(rt2x00dev, 3, &value);
5309 rt2x00_set_field8(&value, BBP3_ADC_MODE_SWITCH, 1);
5310 rt2x00_set_field8(&value, BBP3_ADC_INIT_MODE, 1);
5311 rt2800_bbp_write(rt2x00dev, 3, value);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005312}
5313
5314static void rt2800_init_bbp_3352(struct rt2x00_dev *rt2x00dev)
5315{
Stanislaw Gruszka29f3a582013-05-18 14:03:27 +02005316 rt2800_bbp_write(rt2x00dev, 3, 0x00);
5317 rt2800_bbp_write(rt2x00dev, 4, 0x50);
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02005318
5319 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszka3420f792013-05-18 14:03:30 +02005320
5321 rt2800_bbp_write(rt2x00dev, 47, 0x48);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005322
5323 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5324 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka59dcabb2013-05-18 14:03:32 +02005325
5326 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005327
5328 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5329 rt2800_bbp_write(rt2x00dev, 73, 0x13);
5330 rt2800_bbp_write(rt2x00dev, 75, 0x46);
5331 rt2800_bbp_write(rt2x00dev, 76, 0x28);
5332
5333 rt2800_bbp_write(rt2x00dev, 77, 0x59);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005334
5335 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005336
5337 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
5338 rt2800_bbp_write(rt2x00dev, 80, 0x08);
5339 rt2800_bbp_write(rt2x00dev, 81, 0x37);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005340
5341 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005342
5343 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005344
5345 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005346
5347 rt2800_bbp_write(rt2x00dev, 86, 0x38);
Stanislaw Gruszka9400fa82013-05-18 14:03:40 +02005348
5349 rt2800_bbp_write(rt2x00dev, 88, 0x90);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005350
5351 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005352
5353 rt2800_bbp_write(rt2x00dev, 92, 0x02);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005354
5355 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka1ad44082013-05-18 14:03:45 +02005356
5357 rt2800_bbp_write(rt2x00dev, 104, 0x92);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005358
5359 rt2800_bbp_write(rt2x00dev, 105, 0x34);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005360
5361 rt2800_bbp_write(rt2x00dev, 106, 0x05);
Stanislaw Gruszka46b90d32013-05-18 14:03:48 +02005362
5363 rt2800_bbp_write(rt2x00dev, 120, 0x50);
Stanislaw Gruszkab7feb9b2013-05-18 14:03:51 +02005364
5365 rt2800_bbp_write(rt2x00dev, 137, 0x0f);
Stanislaw Gruszkac2da5272013-05-18 14:03:53 +02005366
5367 rt2800_bbp_write(rt2x00dev, 163, 0xbd);
5368 /* Set ITxBF timeout to 0x9c40=1000msec */
5369 rt2800_bbp_write(rt2x00dev, 179, 0x02);
5370 rt2800_bbp_write(rt2x00dev, 180, 0x00);
5371 rt2800_bbp_write(rt2x00dev, 182, 0x40);
5372 rt2800_bbp_write(rt2x00dev, 180, 0x01);
5373 rt2800_bbp_write(rt2x00dev, 182, 0x9c);
5374 rt2800_bbp_write(rt2x00dev, 179, 0x00);
5375 /* Reprogram the inband interface to put right values in RXWI */
5376 rt2800_bbp_write(rt2x00dev, 142, 0x04);
5377 rt2800_bbp_write(rt2x00dev, 143, 0x3b);
5378 rt2800_bbp_write(rt2x00dev, 142, 0x06);
5379 rt2800_bbp_write(rt2x00dev, 143, 0xa0);
5380 rt2800_bbp_write(rt2x00dev, 142, 0x07);
5381 rt2800_bbp_write(rt2x00dev, 143, 0xa1);
5382 rt2800_bbp_write(rt2x00dev, 142, 0x08);
5383 rt2800_bbp_write(rt2x00dev, 143, 0xa2);
5384
5385 rt2800_bbp_write(rt2x00dev, 148, 0xc8);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005386}
5387
5388static void rt2800_init_bbp_3390(struct rt2x00_dev *rt2x00dev)
5389{
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005390 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5391 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005392
5393 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5394 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005395
5396 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005397
5398 rt2800_bbp_write(rt2x00dev, 79, 0x13);
5399 rt2800_bbp_write(rt2x00dev, 80, 0x05);
5400 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005401
5402 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005403
5404 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005405
5406 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005407
5408 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005409
5410 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005411
5412 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005413
5414 if (rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E))
5415 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
5416 else
5417 rt2800_bbp_write(rt2x00dev, 103, 0x00);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005418
5419 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005420
5421 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005422
5423 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005424}
5425
5426static void rt2800_init_bbp_3572(struct rt2x00_dev *rt2x00dev)
5427{
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02005428 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005429
5430 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5431 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005432
5433 rt2800_bbp_write(rt2x00dev, 69, 0x12);
5434 rt2800_bbp_write(rt2x00dev, 73, 0x10);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005435
5436 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005437
5438 rt2800_bbp_write(rt2x00dev, 79, 0x13);
5439 rt2800_bbp_write(rt2x00dev, 80, 0x05);
5440 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005441
5442 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005443
5444 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005445
5446 rt2800_bbp_write(rt2x00dev, 84, 0x99);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005447
5448 rt2800_bbp_write(rt2x00dev, 86, 0x00);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005449
5450 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005451
5452 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005453
5454 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005455
5456 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005457
5458 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005459
5460 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005461}
5462
Gabor Juhosb189a182013-07-08 16:08:17 +02005463static void rt2800_init_bbp_3593(struct rt2x00_dev *rt2x00dev)
5464{
5465 rt2800_init_bbp_early(rt2x00dev);
5466
5467 rt2800_bbp_write(rt2x00dev, 79, 0x13);
5468 rt2800_bbp_write(rt2x00dev, 80, 0x05);
5469 rt2800_bbp_write(rt2x00dev, 81, 0x33);
5470 rt2800_bbp_write(rt2x00dev, 137, 0x0f);
5471
5472 rt2800_bbp_write(rt2x00dev, 84, 0x19);
5473
5474 /* Enable DC filter */
5475 if (rt2x00_rt_rev_gte(rt2x00dev, RT3593, REV_RT3593E))
5476 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
5477}
5478
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005479static void rt2800_init_bbp_53xx(struct rt2x00_dev *rt2x00dev)
5480{
Stanislaw Gruszka32ef8f42013-05-18 14:03:55 +02005481 int ant, div_mode;
5482 u16 eeprom;
5483 u8 value;
5484
Stanislaw Gruszkac3223572013-05-18 14:03:28 +02005485 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
Stanislaw Gruszkab2f8e0b2013-05-18 14:03:29 +02005486
5487 rt2800_bbp_write(rt2x00dev, 31, 0x08);
Stanislaw Gruszkae379de12013-05-18 14:03:31 +02005488
5489 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
5490 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Stanislaw Gruszka59dcabb2013-05-18 14:03:32 +02005491
5492 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005493
Stanislaw Gruszka58422192014-03-12 15:14:04 +01005494 rt2800_bbp_write(rt2x00dev, 69, 0x12);
Stanislaw Gruszka72ffe142013-05-18 14:03:33 +02005495 rt2800_bbp_write(rt2x00dev, 73, 0x13);
5496 rt2800_bbp_write(rt2x00dev, 75, 0x46);
5497 rt2800_bbp_write(rt2x00dev, 76, 0x28);
5498
5499 rt2800_bbp_write(rt2x00dev, 77, 0x59);
Stanislaw Gruszka8d97be32013-05-18 14:03:34 +02005500
Stanislaw Gruszka58422192014-03-12 15:14:04 +01005501 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
5502
Stanislaw Gruszka43f535e2013-05-18 14:03:35 +02005503 rt2800_bbp_write(rt2x00dev, 79, 0x13);
5504 rt2800_bbp_write(rt2x00dev, 80, 0x05);
5505 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Stanislaw Gruszkafa1e3422013-05-18 14:03:36 +02005506
5507 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Stanislaw Gruszka885f2412013-05-18 14:03:37 +02005508
5509 rt2800_bbp_write(rt2x00dev, 83, 0x7a);
Stanislaw Gruszka3c20a122013-05-18 14:03:38 +02005510
5511 rt2800_bbp_write(rt2x00dev, 84, 0x9a);
Stanislaw Gruszkaaef9f382013-05-18 14:03:39 +02005512
5513 rt2800_bbp_write(rt2x00dev, 86, 0x38);
Stanislaw Gruszka9400fa82013-05-18 14:03:40 +02005514
5515 if (rt2x00_rt(rt2x00dev, RT5392))
5516 rt2800_bbp_write(rt2x00dev, 88, 0x90);
Stanislaw Gruszka7af98742013-05-18 14:03:41 +02005517
5518 rt2800_bbp_write(rt2x00dev, 91, 0x04);
Stanislaw Gruszkab4e121d2013-05-18 14:03:42 +02005519
5520 rt2800_bbp_write(rt2x00dev, 92, 0x02);
Stanislaw Gruszka90fed532013-05-18 14:03:43 +02005521
5522 if (rt2x00_rt(rt2x00dev, RT5392)) {
5523 rt2800_bbp_write(rt2x00dev, 95, 0x9a);
5524 rt2800_bbp_write(rt2x00dev, 98, 0x12);
5525 }
Stanislaw Gruszka672d1182013-05-18 14:03:44 +02005526
5527 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszka1ad44082013-05-18 14:03:45 +02005528
5529 rt2800_bbp_write(rt2x00dev, 104, 0x92);
Stanislaw Gruszka49d61112013-05-18 14:03:46 +02005530
5531 rt2800_bbp_write(rt2x00dev, 105, 0x3c);
Stanislaw Gruszkaf8670852013-05-18 14:03:47 +02005532
5533 if (rt2x00_rt(rt2x00dev, RT5390))
5534 rt2800_bbp_write(rt2x00dev, 106, 0x03);
5535 else if (rt2x00_rt(rt2x00dev, RT5392))
5536 rt2800_bbp_write(rt2x00dev, 106, 0x12);
5537 else
5538 WARN_ON(1);
Stanislaw Gruszkaf2b67772013-05-18 14:03:49 +02005539
5540 rt2800_bbp_write(rt2x00dev, 128, 0x12);
Stanislaw Gruszka72917142013-05-18 14:03:50 +02005541
5542 if (rt2x00_rt(rt2x00dev, RT5392)) {
5543 rt2800_bbp_write(rt2x00dev, 134, 0xd0);
5544 rt2800_bbp_write(rt2x00dev, 135, 0xf6);
5545 }
Stanislaw Gruszka5df1ff32013-05-18 14:03:52 +02005546
5547 rt2800_disable_unused_dac_adc(rt2x00dev);
Stanislaw Gruszka32ef8f42013-05-18 14:03:55 +02005548
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005549 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
Stanislaw Gruszka32ef8f42013-05-18 14:03:55 +02005550 div_mode = rt2x00_get_field16(eeprom,
5551 EEPROM_NIC_CONF1_ANT_DIVERSITY);
5552 ant = (div_mode == 3) ? 1 : 0;
5553
5554 /* check if this is a Bluetooth combo card */
Gabor Juhosc429dfe2013-10-11 13:18:42 +02005555 if (rt2x00_has_cap_bt_coexist(rt2x00dev)) {
Stanislaw Gruszka32ef8f42013-05-18 14:03:55 +02005556 u32 reg;
5557
5558 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
5559 rt2x00_set_field32(&reg, GPIO_CTRL_DIR3, 0);
5560 rt2x00_set_field32(&reg, GPIO_CTRL_DIR6, 0);
5561 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, 0);
5562 rt2x00_set_field32(&reg, GPIO_CTRL_VAL6, 0);
5563 if (ant == 0)
5564 rt2x00_set_field32(&reg, GPIO_CTRL_VAL3, 1);
5565 else if (ant == 1)
5566 rt2x00_set_field32(&reg, GPIO_CTRL_VAL6, 1);
5567 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
5568 }
5569
5570 /* This chip has hardware antenna diversity*/
5571 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390R)) {
5572 rt2800_bbp_write(rt2x00dev, 150, 0); /* Disable Antenna Software OFDM */
5573 rt2800_bbp_write(rt2x00dev, 151, 0); /* Disable Antenna Software CCK */
5574 rt2800_bbp_write(rt2x00dev, 154, 0); /* Clear previously selected antenna */
5575 }
5576
5577 rt2800_bbp_read(rt2x00dev, 152, &value);
5578 if (ant == 0)
5579 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
5580 else
5581 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
5582 rt2800_bbp_write(rt2x00dev, 152, value);
5583
5584 rt2800_init_freq_calibration(rt2x00dev);
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005585}
5586
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005587static void rt2800_init_bbp_5592(struct rt2x00_dev *rt2x00dev)
5588{
5589 int ant, div_mode;
5590 u16 eeprom;
5591 u8 value;
5592
Gabor Juhos624708b2013-04-19 10:13:52 +02005593 rt2800_init_bbp_early(rt2x00dev);
Stanislaw Gruszkaa4969d02013-03-16 19:19:35 +01005594
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005595 rt2800_bbp_read(rt2x00dev, 105, &value);
5596 rt2x00_set_field8(&value, BBP105_MLD,
5597 rt2x00dev->default_ant.rx_chain_num == 2);
5598 rt2800_bbp_write(rt2x00dev, 105, value);
5599
5600 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
5601
5602 rt2800_bbp_write(rt2x00dev, 20, 0x06);
5603 rt2800_bbp_write(rt2x00dev, 31, 0x08);
5604 rt2800_bbp_write(rt2x00dev, 65, 0x2C);
5605 rt2800_bbp_write(rt2x00dev, 68, 0xDD);
5606 rt2800_bbp_write(rt2x00dev, 69, 0x1A);
5607 rt2800_bbp_write(rt2x00dev, 70, 0x05);
5608 rt2800_bbp_write(rt2x00dev, 73, 0x13);
5609 rt2800_bbp_write(rt2x00dev, 74, 0x0F);
5610 rt2800_bbp_write(rt2x00dev, 75, 0x4F);
5611 rt2800_bbp_write(rt2x00dev, 76, 0x28);
5612 rt2800_bbp_write(rt2x00dev, 77, 0x59);
5613 rt2800_bbp_write(rt2x00dev, 84, 0x9A);
5614 rt2800_bbp_write(rt2x00dev, 86, 0x38);
5615 rt2800_bbp_write(rt2x00dev, 88, 0x90);
5616 rt2800_bbp_write(rt2x00dev, 91, 0x04);
5617 rt2800_bbp_write(rt2x00dev, 92, 0x02);
5618 rt2800_bbp_write(rt2x00dev, 95, 0x9a);
5619 rt2800_bbp_write(rt2x00dev, 98, 0x12);
5620 rt2800_bbp_write(rt2x00dev, 103, 0xC0);
5621 rt2800_bbp_write(rt2x00dev, 104, 0x92);
5622 /* FIXME BBP105 owerwrite */
5623 rt2800_bbp_write(rt2x00dev, 105, 0x3C);
5624 rt2800_bbp_write(rt2x00dev, 106, 0x35);
5625 rt2800_bbp_write(rt2x00dev, 128, 0x12);
5626 rt2800_bbp_write(rt2x00dev, 134, 0xD0);
5627 rt2800_bbp_write(rt2x00dev, 135, 0xF6);
5628 rt2800_bbp_write(rt2x00dev, 137, 0x0F);
5629
5630 /* Initialize GLRT (Generalized Likehood Radio Test) */
5631 rt2800_init_bbp_5592_glrt(rt2x00dev);
5632
5633 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
5634
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005635 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005636 div_mode = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_ANT_DIVERSITY);
5637 ant = (div_mode == 3) ? 1 : 0;
5638 rt2800_bbp_read(rt2x00dev, 152, &value);
5639 if (ant == 0) {
5640 /* Main antenna */
5641 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
5642 } else {
5643 /* Auxiliary antenna */
5644 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
5645 }
5646 rt2800_bbp_write(rt2x00dev, 152, value);
5647
5648 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C)) {
5649 rt2800_bbp_read(rt2x00dev, 254, &value);
5650 rt2x00_set_field8(&value, BBP254_BIT7, 1);
5651 rt2800_bbp_write(rt2x00dev, 254, value);
5652 }
5653
Stanislaw Gruszkac2675482013-03-16 19:19:41 +01005654 rt2800_init_freq_calibration(rt2x00dev);
5655
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005656 rt2800_bbp_write(rt2x00dev, 84, 0x19);
Stanislaw Gruszka6e04f252013-03-16 19:19:38 +01005657 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C))
5658 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005659}
5660
Stanislaw Gruszkaa1ef5032013-05-18 14:03:24 +02005661static void rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005662{
5663 unsigned int i;
5664 u16 eeprom;
5665 u8 reg_id;
5666 u8 value;
5667
Stanislaw Gruszkadae62952013-05-18 14:03:26 +02005668 if (rt2800_is_305x_soc(rt2x00dev))
5669 rt2800_init_bbp_305x_soc(rt2x00dev);
5670
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005671 switch (rt2x00dev->chip.rt) {
5672 case RT2860:
5673 case RT2872:
5674 case RT2883:
5675 rt2800_init_bbp_28xx(rt2x00dev);
5676 break;
5677 case RT3070:
5678 case RT3071:
5679 case RT3090:
5680 rt2800_init_bbp_30xx(rt2x00dev);
5681 break;
5682 case RT3290:
5683 rt2800_init_bbp_3290(rt2x00dev);
5684 break;
5685 case RT3352:
5686 rt2800_init_bbp_3352(rt2x00dev);
5687 break;
5688 case RT3390:
5689 rt2800_init_bbp_3390(rt2x00dev);
5690 break;
5691 case RT3572:
5692 rt2800_init_bbp_3572(rt2x00dev);
5693 break;
Gabor Juhosb189a182013-07-08 16:08:17 +02005694 case RT3593:
5695 rt2800_init_bbp_3593(rt2x00dev);
5696 return;
Stanislaw Gruszka39ab3e82013-05-18 14:03:25 +02005697 case RT5390:
5698 case RT5392:
5699 rt2800_init_bbp_53xx(rt2x00dev);
5700 break;
5701 case RT5592:
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005702 rt2800_init_bbp_5592(rt2x00dev);
Stanislaw Gruszkaa1ef5032013-05-18 14:03:24 +02005703 return;
Stanislaw Gruszkaa7bbbe52013-03-16 19:19:34 +01005704 }
5705
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005706 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
Gabor Juhos022138c2013-07-08 11:25:54 +02005707 rt2800_eeprom_read_from_array(rt2x00dev, EEPROM_BBP_START, i,
5708 &eeprom);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005709
5710 if (eeprom != 0xffff && eeprom != 0x0000) {
5711 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
5712 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
5713 rt2800_bbp_write(rt2x00dev, reg_id, value);
5714 }
5715 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005716}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005717
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02005718static void rt2800_led_open_drain_enable(struct rt2x00_dev *rt2x00dev)
5719{
5720 u32 reg;
5721
5722 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
5723 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
5724 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
5725}
5726
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005727static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev, bool bw40,
5728 u8 filter_target)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005729{
5730 unsigned int i;
5731 u8 bbp;
5732 u8 rfcsr;
5733 u8 passband;
5734 u8 stopband;
5735 u8 overtuned = 0;
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005736 u8 rfcsr24 = (bw40) ? 0x27 : 0x07;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005737
5738 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
5739
5740 rt2800_bbp_read(rt2x00dev, 4, &bbp);
5741 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
5742 rt2800_bbp_write(rt2x00dev, 4, bbp);
5743
RA-Jay Hung80d184e2011-01-10 11:28:10 +01005744 rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
5745 rt2x00_set_field8(&rfcsr, RFCSR31_RX_H20M, bw40);
5746 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
5747
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01005748 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
5749 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
5750 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
5751
5752 /*
5753 * Set power & frequency of passband test tone
5754 */
5755 rt2800_bbp_write(rt2x00dev, 24, 0);
5756
5757 for (i = 0; i < 100; i++) {
5758 rt2800_bbp_write(rt2x00dev, 25, 0x90);
5759 msleep(1);
5760
5761 rt2800_bbp_read(rt2x00dev, 55, &passband);
5762 if (passband)
5763 break;
5764 }
5765
5766 /*
5767 * Set power & frequency of stopband test tone
5768 */
5769 rt2800_bbp_write(rt2x00dev, 24, 0x06);
5770
5771 for (i = 0; i < 100; i++) {
5772 rt2800_bbp_write(rt2x00dev, 25, 0x90);
5773 msleep(1);
5774
5775 rt2800_bbp_read(rt2x00dev, 55, &stopband);
5776
5777 if ((passband - stopband) <= filter_target) {
5778 rfcsr24++;
5779 overtuned += ((passband - stopband) == filter_target);
5780 } else
5781 break;
5782
5783 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
5784 }
5785
5786 rfcsr24 -= !!overtuned;
5787
5788 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
5789 return rfcsr24;
5790}
5791
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005792static void rt2800_rf_init_calibration(struct rt2x00_dev *rt2x00dev,
5793 const unsigned int rf_reg)
5794{
5795 u8 rfcsr;
5796
5797 rt2800_rfcsr_read(rt2x00dev, rf_reg, &rfcsr);
5798 rt2x00_set_field8(&rfcsr, FIELD8(0x80), 1);
5799 rt2800_rfcsr_write(rt2x00dev, rf_reg, rfcsr);
5800 msleep(1);
5801 rt2x00_set_field8(&rfcsr, FIELD8(0x80), 0);
5802 rt2800_rfcsr_write(rt2x00dev, rf_reg, rfcsr);
5803}
5804
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02005805static void rt2800_rx_filter_calibration(struct rt2x00_dev *rt2x00dev)
5806{
5807 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
5808 u8 filter_tgt_bw20;
5809 u8 filter_tgt_bw40;
5810 u8 rfcsr, bbp;
5811
5812 /*
5813 * TODO: sync filter_tgt values with vendor driver
5814 */
5815 if (rt2x00_rt(rt2x00dev, RT3070)) {
5816 filter_tgt_bw20 = 0x16;
5817 filter_tgt_bw40 = 0x19;
5818 } else {
5819 filter_tgt_bw20 = 0x13;
5820 filter_tgt_bw40 = 0x15;
5821 }
5822
5823 drv_data->calibration_bw20 =
5824 rt2800_init_rx_filter(rt2x00dev, false, filter_tgt_bw20);
5825 drv_data->calibration_bw40 =
5826 rt2800_init_rx_filter(rt2x00dev, true, filter_tgt_bw40);
5827
5828 /*
5829 * Save BBP 25 & 26 values for later use in channel switching (for 3052)
5830 */
5831 rt2800_bbp_read(rt2x00dev, 25, &drv_data->bbp25);
5832 rt2800_bbp_read(rt2x00dev, 26, &drv_data->bbp26);
5833
5834 /*
5835 * Set back to initial state
5836 */
5837 rt2800_bbp_write(rt2x00dev, 24, 0);
5838
5839 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
5840 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
5841 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
5842
5843 /*
5844 * Set BBP back to BW20
5845 */
5846 rt2800_bbp_read(rt2x00dev, 4, &bbp);
5847 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
5848 rt2800_bbp_write(rt2x00dev, 4, bbp);
5849}
5850
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005851static void rt2800_normal_mode_setup_3xxx(struct rt2x00_dev *rt2x00dev)
5852{
5853 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
5854 u8 min_gain, rfcsr, bbp;
5855 u16 eeprom;
5856
5857 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
5858
5859 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
5860 if (rt2x00_rt(rt2x00dev, RT3070) ||
5861 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
5862 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
5863 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gabor Juhosc429dfe2013-10-11 13:18:42 +02005864 if (!rt2x00_has_cap_external_lna_bg(rt2x00dev))
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005865 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
5866 }
5867
5868 min_gain = rt2x00_rt(rt2x00dev, RT3070) ? 1 : 2;
5869 if (drv_data->txmixer_gain_24g >= min_gain) {
5870 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
5871 drv_data->txmixer_gain_24g);
5872 }
5873
5874 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
5875
5876 if (rt2x00_rt(rt2x00dev, RT3090)) {
5877 /* Turn off unused DAC1 and ADC1 to reduce power consumption */
5878 rt2800_bbp_read(rt2x00dev, 138, &bbp);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005879 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02005880 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
5881 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
5882 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
5883 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
5884 rt2800_bbp_write(rt2x00dev, 138, bbp);
5885 }
5886
5887 if (rt2x00_rt(rt2x00dev, RT3070)) {
5888 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
5889 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F))
5890 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
5891 else
5892 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
5893 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
5894 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
5895 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
5896 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
5897 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
5898 rt2x00_rt(rt2x00dev, RT3090) ||
5899 rt2x00_rt(rt2x00dev, RT3390)) {
5900 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
5901 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
5902 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
5903 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
5904 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
5905 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
5906 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
5907
5908 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
5909 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
5910 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
5911
5912 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
5913 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
5914 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
5915
5916 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
5917 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
5918 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
5919 }
5920}
5921
Gabor Juhosab7078a2013-07-08 16:08:18 +02005922static void rt2800_normal_mode_setup_3593(struct rt2x00_dev *rt2x00dev)
5923{
5924 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
5925 u8 rfcsr;
5926 u8 tx_gain;
5927
5928 rt2800_rfcsr_read(rt2x00dev, 50, &rfcsr);
5929 rt2x00_set_field8(&rfcsr, RFCSR50_TX_LO2_EN, 0);
5930 rt2800_rfcsr_write(rt2x00dev, 50, rfcsr);
5931
5932 rt2800_rfcsr_read(rt2x00dev, 51, &rfcsr);
5933 tx_gain = rt2x00_get_field8(drv_data->txmixer_gain_24g,
5934 RFCSR17_TXMIXER_GAIN);
5935 rt2x00_set_field8(&rfcsr, RFCSR51_BITS24, tx_gain);
5936 rt2800_rfcsr_write(rt2x00dev, 51, rfcsr);
5937
5938 rt2800_rfcsr_read(rt2x00dev, 38, &rfcsr);
5939 rt2x00_set_field8(&rfcsr, RFCSR38_RX_LO1_EN, 0);
5940 rt2800_rfcsr_write(rt2x00dev, 38, rfcsr);
5941
5942 rt2800_rfcsr_read(rt2x00dev, 39, &rfcsr);
5943 rt2x00_set_field8(&rfcsr, RFCSR39_RX_LO2_EN, 0);
5944 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
5945
5946 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
5947 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
5948 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
5949 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
5950
5951 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
5952 rt2x00_set_field8(&rfcsr, RFCSR30_RX_VCM, 2);
5953 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
5954
5955 /* TODO: enable stream mode */
5956}
5957
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02005958static void rt2800_normal_mode_setup_5xxx(struct rt2x00_dev *rt2x00dev)
5959{
5960 u8 reg;
5961 u16 eeprom;
5962
5963 /* Turn off unused DAC1 and ADC1 to reduce power consumption */
5964 rt2800_bbp_read(rt2x00dev, 138, &reg);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02005965 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02005966 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
5967 rt2x00_set_field8(&reg, BBP138_RX_ADC1, 0);
5968 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
5969 rt2x00_set_field8(&reg, BBP138_TX_DAC1, 1);
5970 rt2800_bbp_write(rt2x00dev, 138, reg);
5971
5972 rt2800_rfcsr_read(rt2x00dev, 38, &reg);
5973 rt2x00_set_field8(&reg, RFCSR38_RX_LO1_EN, 0);
5974 rt2800_rfcsr_write(rt2x00dev, 38, reg);
5975
5976 rt2800_rfcsr_read(rt2x00dev, 39, &reg);
5977 rt2x00_set_field8(&reg, RFCSR39_RX_LO2_EN, 0);
5978 rt2800_rfcsr_write(rt2x00dev, 39, reg);
5979
5980 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
5981
5982 rt2800_rfcsr_read(rt2x00dev, 30, &reg);
5983 rt2x00_set_field8(&reg, RFCSR30_RX_VCM, 2);
5984 rt2800_rfcsr_write(rt2x00dev, 30, reg);
5985}
5986
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005987static void rt2800_init_rfcsr_305x_soc(struct rt2x00_dev *rt2x00dev)
5988{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02005989 rt2800_rf_init_calibration(rt2x00dev, 30);
5990
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01005991 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
5992 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
5993 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
5994 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
5995 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
5996 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
5997 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
5998 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
5999 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
6000 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
6001 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
6002 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
6003 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
6004 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
6005 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
6006 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
6007 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
6008 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
6009 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
6010 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
6011 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
6012 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
6013 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
6014 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
6015 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
6016 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
6017 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
6018 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
6019 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
6020 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
6021 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
6022 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
6023}
6024
6025static void rt2800_init_rfcsr_30xx(struct rt2x00_dev *rt2x00dev)
6026{
Stanislaw Gruszkac9a221b2013-04-17 14:08:13 +02006027 u8 rfcsr;
6028 u16 eeprom;
6029 u32 reg;
6030
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006031 /* XXX vendor driver do this only for 3070 */
6032 rt2800_rf_init_calibration(rt2x00dev, 30);
6033
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006034 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
6035 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
6036 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
6037 rt2800_rfcsr_write(rt2x00dev, 7, 0x60);
6038 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
6039 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
6040 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
6041 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
6042 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
6043 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
6044 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
6045 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
6046 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
6047 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
6048 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
6049 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
6050 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
Kevin Lo772eb432013-09-18 16:22:44 +08006051 rt2800_rfcsr_write(rt2x00dev, 25, 0x03);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006052 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Stanislaw Gruszkac9a221b2013-04-17 14:08:13 +02006053
6054 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
6055 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6056 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
6057 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
6058 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
6059 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
6060 rt2x00_rt(rt2x00dev, RT3090)) {
6061 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
6062
6063 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
6064 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
6065 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
6066
6067 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6068 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
6069 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
6070 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006071 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1,
6072 &eeprom);
Stanislaw Gruszkac9a221b2013-04-17 14:08:13 +02006073 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
6074 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
6075 else
6076 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
6077 }
6078 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
6079
6080 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
6081 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
6082 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
6083 }
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02006084
6085 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszka5de5a1f2013-04-17 14:08:17 +02006086
6087 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
6088 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
6089 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E))
6090 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006091
6092 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02006093 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006094}
6095
6096static void rt2800_init_rfcsr_3290(struct rt2x00_dev *rt2x00dev)
6097{
Stanislaw Gruszkaf9cdcbb2013-04-17 14:08:12 +02006098 u8 rfcsr;
6099
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006100 rt2800_rf_init_calibration(rt2x00dev, 2);
6101
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006102 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
6103 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
6104 rt2800_rfcsr_write(rt2x00dev, 3, 0x08);
6105 rt2800_rfcsr_write(rt2x00dev, 4, 0x00);
6106 rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
6107 rt2800_rfcsr_write(rt2x00dev, 8, 0xf3);
6108 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
6109 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
6110 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
6111 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
6112 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
6113 rt2800_rfcsr_write(rt2x00dev, 18, 0x02);
6114 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
6115 rt2800_rfcsr_write(rt2x00dev, 25, 0x83);
6116 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
6117 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
6118 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
6119 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
6120 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
6121 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
6122 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
6123 rt2800_rfcsr_write(rt2x00dev, 34, 0x05);
6124 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
6125 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
6126 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
6127 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
6128 rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
6129 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
6130 rt2800_rfcsr_write(rt2x00dev, 42, 0xd5);
6131 rt2800_rfcsr_write(rt2x00dev, 43, 0x7b);
6132 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
6133 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
6134 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
6135 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
6136 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
6137 rt2800_rfcsr_write(rt2x00dev, 49, 0x98);
6138 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
6139 rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
6140 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
6141 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
6142 rt2800_rfcsr_write(rt2x00dev, 56, 0x02);
6143 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
6144 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
6145 rt2800_rfcsr_write(rt2x00dev, 59, 0x09);
6146 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
6147 rt2800_rfcsr_write(rt2x00dev, 61, 0xc1);
Stanislaw Gruszkaf9cdcbb2013-04-17 14:08:12 +02006148
6149 rt2800_rfcsr_read(rt2x00dev, 29, &rfcsr);
6150 rt2x00_set_field8(&rfcsr, RFCSR29_RSSI_GAIN, 3);
6151 rt2800_rfcsr_write(rt2x00dev, 29, rfcsr);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006152
6153 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02006154 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006155}
6156
6157static void rt2800_init_rfcsr_3352(struct rt2x00_dev *rt2x00dev)
6158{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006159 rt2800_rf_init_calibration(rt2x00dev, 30);
6160
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006161 rt2800_rfcsr_write(rt2x00dev, 0, 0xf0);
6162 rt2800_rfcsr_write(rt2x00dev, 1, 0x23);
6163 rt2800_rfcsr_write(rt2x00dev, 2, 0x50);
6164 rt2800_rfcsr_write(rt2x00dev, 3, 0x18);
6165 rt2800_rfcsr_write(rt2x00dev, 4, 0x00);
6166 rt2800_rfcsr_write(rt2x00dev, 5, 0x00);
6167 rt2800_rfcsr_write(rt2x00dev, 6, 0x33);
6168 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
6169 rt2800_rfcsr_write(rt2x00dev, 8, 0xf1);
6170 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
6171 rt2800_rfcsr_write(rt2x00dev, 10, 0xd2);
6172 rt2800_rfcsr_write(rt2x00dev, 11, 0x42);
6173 rt2800_rfcsr_write(rt2x00dev, 12, 0x1c);
6174 rt2800_rfcsr_write(rt2x00dev, 13, 0x00);
6175 rt2800_rfcsr_write(rt2x00dev, 14, 0x5a);
6176 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
6177 rt2800_rfcsr_write(rt2x00dev, 16, 0x01);
6178 rt2800_rfcsr_write(rt2x00dev, 18, 0x45);
6179 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
6180 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
6181 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
6182 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
6183 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
6184 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
6185 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
6186 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
6187 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
6188 rt2800_rfcsr_write(rt2x00dev, 28, 0x03);
6189 rt2800_rfcsr_write(rt2x00dev, 29, 0x00);
6190 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
6191 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
6192 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
6193 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
6194 rt2800_rfcsr_write(rt2x00dev, 34, 0x01);
6195 rt2800_rfcsr_write(rt2x00dev, 35, 0x03);
6196 rt2800_rfcsr_write(rt2x00dev, 36, 0xbd);
6197 rt2800_rfcsr_write(rt2x00dev, 37, 0x3c);
6198 rt2800_rfcsr_write(rt2x00dev, 38, 0x5f);
6199 rt2800_rfcsr_write(rt2x00dev, 39, 0xc5);
6200 rt2800_rfcsr_write(rt2x00dev, 40, 0x33);
6201 rt2800_rfcsr_write(rt2x00dev, 41, 0x5b);
6202 rt2800_rfcsr_write(rt2x00dev, 42, 0x5b);
6203 rt2800_rfcsr_write(rt2x00dev, 43, 0xdb);
6204 rt2800_rfcsr_write(rt2x00dev, 44, 0xdb);
6205 rt2800_rfcsr_write(rt2x00dev, 45, 0xdb);
6206 rt2800_rfcsr_write(rt2x00dev, 46, 0xdd);
6207 rt2800_rfcsr_write(rt2x00dev, 47, 0x0d);
6208 rt2800_rfcsr_write(rt2x00dev, 48, 0x14);
6209 rt2800_rfcsr_write(rt2x00dev, 49, 0x00);
6210 rt2800_rfcsr_write(rt2x00dev, 50, 0x2d);
6211 rt2800_rfcsr_write(rt2x00dev, 51, 0x7f);
6212 rt2800_rfcsr_write(rt2x00dev, 52, 0x00);
6213 rt2800_rfcsr_write(rt2x00dev, 53, 0x52);
6214 rt2800_rfcsr_write(rt2x00dev, 54, 0x1b);
6215 rt2800_rfcsr_write(rt2x00dev, 55, 0x7f);
6216 rt2800_rfcsr_write(rt2x00dev, 56, 0x00);
6217 rt2800_rfcsr_write(rt2x00dev, 57, 0x52);
6218 rt2800_rfcsr_write(rt2x00dev, 58, 0x1b);
6219 rt2800_rfcsr_write(rt2x00dev, 59, 0x00);
6220 rt2800_rfcsr_write(rt2x00dev, 60, 0x00);
6221 rt2800_rfcsr_write(rt2x00dev, 61, 0x00);
6222 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
6223 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02006224
6225 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006226 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02006227 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006228}
6229
6230static void rt2800_init_rfcsr_3390(struct rt2x00_dev *rt2x00dev)
6231{
Stanislaw Gruszka2971e662013-04-17 14:08:14 +02006232 u32 reg;
6233
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006234 rt2800_rf_init_calibration(rt2x00dev, 30);
6235
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006236 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
6237 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
6238 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
6239 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
6240 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
6241 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
6242 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
6243 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
6244 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
6245 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
6246 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
6247 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
6248 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
6249 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
6250 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
6251 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
6252 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
6253 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
6254 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
6255 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
6256 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
6257 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
6258 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
6259 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
6260 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
6261 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
6262 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
6263 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
6264 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
6265 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
6266 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
6267 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Stanislaw Gruszka2971e662013-04-17 14:08:14 +02006268
6269 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
6270 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
6271 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02006272
6273 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszka5de5a1f2013-04-17 14:08:17 +02006274
6275 if (rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
6276 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006277
6278 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02006279 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006280}
6281
6282static void rt2800_init_rfcsr_3572(struct rt2x00_dev *rt2x00dev)
6283{
Stanislaw Gruszka87d91db2013-04-17 14:08:15 +02006284 u8 rfcsr;
6285 u32 reg;
6286
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006287 rt2800_rf_init_calibration(rt2x00dev, 30);
6288
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006289 rt2800_rfcsr_write(rt2x00dev, 0, 0x70);
6290 rt2800_rfcsr_write(rt2x00dev, 1, 0x81);
6291 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
6292 rt2800_rfcsr_write(rt2x00dev, 3, 0x02);
6293 rt2800_rfcsr_write(rt2x00dev, 4, 0x4c);
6294 rt2800_rfcsr_write(rt2x00dev, 5, 0x05);
6295 rt2800_rfcsr_write(rt2x00dev, 6, 0x4a);
6296 rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
6297 rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
6298 rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
6299 rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
6300 rt2800_rfcsr_write(rt2x00dev, 12, 0x70);
6301 rt2800_rfcsr_write(rt2x00dev, 13, 0x65);
6302 rt2800_rfcsr_write(rt2x00dev, 14, 0xa0);
6303 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
6304 rt2800_rfcsr_write(rt2x00dev, 16, 0x4c);
6305 rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
6306 rt2800_rfcsr_write(rt2x00dev, 18, 0xac);
6307 rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
6308 rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
6309 rt2800_rfcsr_write(rt2x00dev, 21, 0xd0);
6310 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
6311 rt2800_rfcsr_write(rt2x00dev, 23, 0x3c);
6312 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
6313 rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
6314 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
6315 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
6316 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
6317 rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
6318 rt2800_rfcsr_write(rt2x00dev, 30, 0x09);
6319 rt2800_rfcsr_write(rt2x00dev, 31, 0x10);
Stanislaw Gruszka87d91db2013-04-17 14:08:15 +02006320
6321 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
6322 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
6323 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
6324
6325 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6326 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
6327 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
6328 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
6329 msleep(1);
6330 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6331 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
6332 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
6333 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Stanislaw Gruszkac5b3c352013-04-17 14:08:16 +02006334
6335 rt2800_rx_filter_calibration(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006336 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkada8064c2013-04-17 14:08:19 +02006337 rt2800_normal_mode_setup_3xxx(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006338}
6339
Gabor Juhosd63f7e82013-07-08 16:08:19 +02006340static void rt3593_post_bbp_init(struct rt2x00_dev *rt2x00dev)
6341{
6342 u8 bbp;
6343 bool txbf_enabled = false; /* FIXME */
6344
6345 rt2800_bbp_read(rt2x00dev, 105, &bbp);
6346 if (rt2x00dev->default_ant.rx_chain_num == 1)
6347 rt2x00_set_field8(&bbp, BBP105_MLD, 0);
6348 else
6349 rt2x00_set_field8(&bbp, BBP105_MLD, 1);
6350 rt2800_bbp_write(rt2x00dev, 105, bbp);
6351
6352 rt2800_bbp4_mac_if_ctrl(rt2x00dev);
6353
6354 rt2800_bbp_write(rt2x00dev, 92, 0x02);
6355 rt2800_bbp_write(rt2x00dev, 82, 0x82);
6356 rt2800_bbp_write(rt2x00dev, 106, 0x05);
6357 rt2800_bbp_write(rt2x00dev, 104, 0x92);
6358 rt2800_bbp_write(rt2x00dev, 88, 0x90);
6359 rt2800_bbp_write(rt2x00dev, 148, 0xc8);
6360 rt2800_bbp_write(rt2x00dev, 47, 0x48);
6361 rt2800_bbp_write(rt2x00dev, 120, 0x50);
6362
6363 if (txbf_enabled)
6364 rt2800_bbp_write(rt2x00dev, 163, 0xbd);
6365 else
6366 rt2800_bbp_write(rt2x00dev, 163, 0x9d);
6367
6368 /* SNR mapping */
6369 rt2800_bbp_write(rt2x00dev, 142, 6);
6370 rt2800_bbp_write(rt2x00dev, 143, 160);
6371 rt2800_bbp_write(rt2x00dev, 142, 7);
6372 rt2800_bbp_write(rt2x00dev, 143, 161);
6373 rt2800_bbp_write(rt2x00dev, 142, 8);
6374 rt2800_bbp_write(rt2x00dev, 143, 162);
6375
6376 /* ADC/DAC control */
6377 rt2800_bbp_write(rt2x00dev, 31, 0x08);
6378
6379 /* RX AGC energy lower bound in log2 */
6380 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
6381
6382 /* FIXME: BBP 105 owerwrite? */
6383 rt2800_bbp_write(rt2x00dev, 105, 0x04);
Gabor Juhosf42b0462013-07-08 16:08:30 +02006384
Gabor Juhosd63f7e82013-07-08 16:08:19 +02006385}
6386
Gabor Juhosab7078a2013-07-08 16:08:18 +02006387static void rt2800_init_rfcsr_3593(struct rt2x00_dev *rt2x00dev)
6388{
6389 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
6390 u32 reg;
6391 u8 rfcsr;
6392
6393 /* Disable GPIO #4 and #7 function for LAN PE control */
6394 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
6395 rt2x00_set_field32(&reg, GPIO_SWITCH_4, 0);
6396 rt2x00_set_field32(&reg, GPIO_SWITCH_7, 0);
6397 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
6398
6399 /* Initialize default register values */
6400 rt2800_rfcsr_write(rt2x00dev, 1, 0x03);
6401 rt2800_rfcsr_write(rt2x00dev, 3, 0x80);
6402 rt2800_rfcsr_write(rt2x00dev, 5, 0x00);
6403 rt2800_rfcsr_write(rt2x00dev, 6, 0x40);
6404 rt2800_rfcsr_write(rt2x00dev, 8, 0xf1);
6405 rt2800_rfcsr_write(rt2x00dev, 9, 0x02);
6406 rt2800_rfcsr_write(rt2x00dev, 10, 0xd3);
6407 rt2800_rfcsr_write(rt2x00dev, 11, 0x40);
6408 rt2800_rfcsr_write(rt2x00dev, 12, 0x4e);
6409 rt2800_rfcsr_write(rt2x00dev, 13, 0x12);
6410 rt2800_rfcsr_write(rt2x00dev, 18, 0x40);
6411 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
6412 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
6413 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
6414 rt2800_rfcsr_write(rt2x00dev, 32, 0x78);
6415 rt2800_rfcsr_write(rt2x00dev, 33, 0x3b);
6416 rt2800_rfcsr_write(rt2x00dev, 34, 0x3c);
6417 rt2800_rfcsr_write(rt2x00dev, 35, 0xe0);
6418 rt2800_rfcsr_write(rt2x00dev, 38, 0x86);
6419 rt2800_rfcsr_write(rt2x00dev, 39, 0x23);
6420 rt2800_rfcsr_write(rt2x00dev, 44, 0xd3);
6421 rt2800_rfcsr_write(rt2x00dev, 45, 0xbb);
6422 rt2800_rfcsr_write(rt2x00dev, 46, 0x60);
6423 rt2800_rfcsr_write(rt2x00dev, 49, 0x8e);
6424 rt2800_rfcsr_write(rt2x00dev, 50, 0x86);
6425 rt2800_rfcsr_write(rt2x00dev, 51, 0x75);
6426 rt2800_rfcsr_write(rt2x00dev, 52, 0x45);
6427 rt2800_rfcsr_write(rt2x00dev, 53, 0x18);
6428 rt2800_rfcsr_write(rt2x00dev, 54, 0x18);
6429 rt2800_rfcsr_write(rt2x00dev, 55, 0x18);
6430 rt2800_rfcsr_write(rt2x00dev, 56, 0xdb);
6431 rt2800_rfcsr_write(rt2x00dev, 57, 0x6e);
6432
6433 /* Initiate calibration */
6434 /* TODO: use rt2800_rf_init_calibration ? */
6435 rt2800_rfcsr_read(rt2x00dev, 2, &rfcsr);
6436 rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 1);
6437 rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
6438
6439 rt2800_adjust_freq_offset(rt2x00dev);
6440
6441 rt2800_rfcsr_read(rt2x00dev, 18, &rfcsr);
6442 rt2x00_set_field8(&rfcsr, RFCSR18_XO_TUNE_BYPASS, 1);
6443 rt2800_rfcsr_write(rt2x00dev, 18, rfcsr);
6444
6445 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6446 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
6447 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
6448 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
6449 usleep_range(1000, 1500);
6450 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
6451 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
6452 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
6453
6454 /* Set initial values for RX filter calibration */
6455 drv_data->calibration_bw20 = 0x1f;
6456 drv_data->calibration_bw40 = 0x2f;
6457
6458 /* Save BBP 25 & 26 values for later use in channel switching */
6459 rt2800_bbp_read(rt2x00dev, 25, &drv_data->bbp25);
6460 rt2800_bbp_read(rt2x00dev, 26, &drv_data->bbp26);
6461
6462 rt2800_led_open_drain_enable(rt2x00dev);
6463 rt2800_normal_mode_setup_3593(rt2x00dev);
6464
Gabor Juhosd63f7e82013-07-08 16:08:19 +02006465 rt3593_post_bbp_init(rt2x00dev);
Gabor Juhosab7078a2013-07-08 16:08:18 +02006466
6467 /* TODO: enable stream mode support */
6468}
6469
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006470static void rt2800_init_rfcsr_5390(struct rt2x00_dev *rt2x00dev)
6471{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006472 rt2800_rf_init_calibration(rt2x00dev, 2);
6473
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006474 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
6475 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
6476 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
6477 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
6478 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6479 rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
6480 else
6481 rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
6482 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
6483 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
6484 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
Kevin Loc8520bc2013-10-24 13:24:08 +08006485 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006486 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
6487 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
6488 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
6489 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
6490 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
6491 rt2800_rfcsr_write(rt2x00dev, 19, 0x00);
6492
6493 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
6494 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
6495 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
6496 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
6497 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
Kevin Loc8520bc2013-10-24 13:24:08 +08006498 if (rt2x00_is_usb(rt2x00dev) &&
6499 rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006500 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
6501 else
6502 rt2800_rfcsr_write(rt2x00dev, 25, 0xc0);
6503 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
6504 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
6505 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
6506 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
6507
Kevin Lo7122e662013-10-12 23:25:23 +08006508 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006509 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
6510 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
6511 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
6512 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
6513 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
6514 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
6515 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
6516 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
6517 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
6518
Kevin Loc8520bc2013-10-24 13:24:08 +08006519 rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006520 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
6521 rt2800_rfcsr_write(rt2x00dev, 42, 0xd2);
6522 rt2800_rfcsr_write(rt2x00dev, 43, 0x9a);
6523 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
6524 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
6525 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6526 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
6527 else
6528 rt2800_rfcsr_write(rt2x00dev, 46, 0x7b);
6529 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
6530 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
6531 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
6532
6533 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
6534 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6535 rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
6536 else
6537 rt2800_rfcsr_write(rt2x00dev, 53, 0x84);
6538 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
6539 rt2800_rfcsr_write(rt2x00dev, 55, 0x44);
Kevin Loc8520bc2013-10-24 13:24:08 +08006540 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
6541 rt2800_rfcsr_write(rt2x00dev, 56, 0x42);
6542 else
6543 rt2800_rfcsr_write(rt2x00dev, 56, 0x22);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006544 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
6545 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
Kevin Lo7122e662013-10-12 23:25:23 +08006546 rt2800_rfcsr_write(rt2x00dev, 59, 0x8f);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006547
6548 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
Kevin Loc8520bc2013-10-24 13:24:08 +08006549 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
6550 if (rt2x00_is_usb(rt2x00dev))
6551 rt2800_rfcsr_write(rt2x00dev, 61, 0xd1);
6552 else
6553 rt2800_rfcsr_write(rt2x00dev, 61, 0xd5);
6554 } else {
6555 if (rt2x00_is_usb(rt2x00dev))
6556 rt2800_rfcsr_write(rt2x00dev, 61, 0xdd);
6557 else
6558 rt2800_rfcsr_write(rt2x00dev, 61, 0xb5);
6559 }
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006560 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
6561 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02006562
6563 rt2800_normal_mode_setup_5xxx(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006564
6565 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006566}
6567
6568static void rt2800_init_rfcsr_5392(struct rt2x00_dev *rt2x00dev)
6569{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006570 rt2800_rf_init_calibration(rt2x00dev, 2);
6571
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006572 rt2800_rfcsr_write(rt2x00dev, 1, 0x17);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006573 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
6574 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
6575 rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
6576 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
6577 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
6578 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
6579 rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
6580 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
6581 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
6582 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
6583 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
6584 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
6585 rt2800_rfcsr_write(rt2x00dev, 19, 0x4d);
6586 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
6587 rt2800_rfcsr_write(rt2x00dev, 21, 0x8d);
6588 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
6589 rt2800_rfcsr_write(rt2x00dev, 23, 0x0b);
6590 rt2800_rfcsr_write(rt2x00dev, 24, 0x44);
6591 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
6592 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
6593 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
6594 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
6595 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
6596 rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
6597 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
6598 rt2800_rfcsr_write(rt2x00dev, 32, 0x20);
6599 rt2800_rfcsr_write(rt2x00dev, 33, 0xC0);
6600 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
6601 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
6602 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
6603 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
6604 rt2800_rfcsr_write(rt2x00dev, 38, 0x89);
6605 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
6606 rt2800_rfcsr_write(rt2x00dev, 40, 0x0f);
6607 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
6608 rt2800_rfcsr_write(rt2x00dev, 42, 0xd5);
6609 rt2800_rfcsr_write(rt2x00dev, 43, 0x9b);
6610 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
6611 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
6612 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
6613 rt2800_rfcsr_write(rt2x00dev, 47, 0x0c);
6614 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
6615 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
6616 rt2800_rfcsr_write(rt2x00dev, 50, 0x94);
6617 rt2800_rfcsr_write(rt2x00dev, 51, 0x3a);
6618 rt2800_rfcsr_write(rt2x00dev, 52, 0x48);
6619 rt2800_rfcsr_write(rt2x00dev, 53, 0x44);
6620 rt2800_rfcsr_write(rt2x00dev, 54, 0x38);
6621 rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
6622 rt2800_rfcsr_write(rt2x00dev, 56, 0xa1);
6623 rt2800_rfcsr_write(rt2x00dev, 57, 0x00);
6624 rt2800_rfcsr_write(rt2x00dev, 58, 0x39);
6625 rt2800_rfcsr_write(rt2x00dev, 59, 0x07);
6626 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
6627 rt2800_rfcsr_write(rt2x00dev, 61, 0x91);
6628 rt2800_rfcsr_write(rt2x00dev, 62, 0x39);
6629 rt2800_rfcsr_write(rt2x00dev, 63, 0x07);
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02006630
6631 rt2800_normal_mode_setup_5xxx(rt2x00dev);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006632
6633 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006634}
6635
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006636static void rt2800_init_rfcsr_5592(struct rt2x00_dev *rt2x00dev)
6637{
Stanislaw Gruszkace94ede92013-04-17 14:08:11 +02006638 rt2800_rf_init_calibration(rt2x00dev, 30);
6639
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006640 rt2800_rfcsr_write(rt2x00dev, 1, 0x3F);
6641 rt2800_rfcsr_write(rt2x00dev, 3, 0x08);
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006642 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
6643 rt2800_rfcsr_write(rt2x00dev, 6, 0xE4);
6644 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
6645 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
6646 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
6647 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
6648 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
6649 rt2800_rfcsr_write(rt2x00dev, 19, 0x4D);
6650 rt2800_rfcsr_write(rt2x00dev, 20, 0x10);
6651 rt2800_rfcsr_write(rt2x00dev, 21, 0x8D);
6652 rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
6653 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
6654 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
6655 rt2800_rfcsr_write(rt2x00dev, 33, 0xC0);
6656 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
6657 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
6658 rt2800_rfcsr_write(rt2x00dev, 47, 0x0C);
6659 rt2800_rfcsr_write(rt2x00dev, 53, 0x22);
6660 rt2800_rfcsr_write(rt2x00dev, 63, 0x07);
6661
6662 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
6663 msleep(1);
6664
6665 rt2800_adjust_freq_offset(rt2x00dev);
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006666
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006667 /* Enable DC filter */
6668 if (rt2x00_rt_rev_gte(rt2x00dev, RT5592, REV_RT5592C))
6669 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
6670
Stanislaw Gruszkaf7df8fe2013-04-17 14:08:10 +02006671 rt2800_normal_mode_setup_5xxx(rt2x00dev);
Stanislaw Gruszka5de5a1f2013-04-17 14:08:17 +02006672
6673 if (rt2x00_rt_rev_lt(rt2x00dev, RT5592, REV_RT5592C))
6674 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
Stanislaw Gruszkad9517f22013-04-17 14:08:18 +02006675
6676 rt2800_led_open_drain_enable(rt2x00dev);
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006677}
6678
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006679static void rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01006680{
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006681 if (rt2800_is_305x_soc(rt2x00dev)) {
6682 rt2800_init_rfcsr_305x_soc(rt2x00dev);
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006683 return;
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006684 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01006685
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006686 switch (rt2x00dev->chip.rt) {
6687 case RT3070:
6688 case RT3071:
6689 case RT3090:
6690 rt2800_init_rfcsr_30xx(rt2x00dev);
6691 break;
6692 case RT3290:
6693 rt2800_init_rfcsr_3290(rt2x00dev);
6694 break;
6695 case RT3352:
6696 rt2800_init_rfcsr_3352(rt2x00dev);
6697 break;
6698 case RT3390:
6699 rt2800_init_rfcsr_3390(rt2x00dev);
6700 break;
6701 case RT3572:
6702 rt2800_init_rfcsr_3572(rt2x00dev);
6703 break;
Gabor Juhosab7078a2013-07-08 16:08:18 +02006704 case RT3593:
6705 rt2800_init_rfcsr_3593(rt2x00dev);
6706 break;
Stanislaw Gruszkad5374ef2012-12-12 06:30:55 +01006707 case RT5390:
6708 rt2800_init_rfcsr_5390(rt2x00dev);
6709 break;
6710 case RT5392:
6711 rt2800_init_rfcsr_5392(rt2x00dev);
6712 break;
Stanislaw Gruszka0c9e5fb2013-03-16 19:19:36 +01006713 case RT5592:
6714 rt2800_init_rfcsr_5592(rt2x00dev);
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006715 break;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02006716 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01006717}
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006718
6719int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
6720{
6721 u32 reg;
6722 u16 word;
6723
6724 /*
Stanislaw Gruszka61edc7f2013-09-09 12:37:38 +02006725 * Initialize MAC registers.
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006726 */
6727 if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006728 rt2800_init_registers(rt2x00dev)))
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006729 return -EIO;
6730
Stanislaw Gruszka61edc7f2013-09-09 12:37:38 +02006731 /*
6732 * Wait BBP/RF to wake up.
6733 */
Stanislaw Gruszkaf4e1a4d2013-09-09 12:37:37 +02006734 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev)))
6735 return -EIO;
6736
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006737 /*
Stanislaw Gruszka61edc7f2013-09-09 12:37:38 +02006738 * Send signal during boot time to initialize firmware.
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006739 */
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006740 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
6741 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
Stanislaw Gruszkaf4e1a4d2013-09-09 12:37:37 +02006742 if (rt2x00_is_usb(rt2x00dev))
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006743 rt2800_register_write(rt2x00dev, H2M_INT_SRC, 0);
Stanislaw Gruszkaf4e1a4d2013-09-09 12:37:37 +02006744 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006745 msleep(1);
6746
Stanislaw Gruszka61edc7f2013-09-09 12:37:38 +02006747 /*
6748 * Make sure BBP is up and running.
6749 */
Stanislaw Gruszkaf4e1a4d2013-09-09 12:37:37 +02006750 if (unlikely(rt2800_wait_bbp_ready(rt2x00dev)))
Stanislaw Gruszkac630ccf2013-03-16 19:19:46 +01006751 return -EIO;
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006752
Stanislaw Gruszka61edc7f2013-09-09 12:37:38 +02006753 /*
6754 * Initialize BBP/RF registers.
6755 */
Stanislaw Gruszkaa1ef5032013-05-18 14:03:24 +02006756 rt2800_init_bbp(rt2x00dev);
Stanislaw Gruszka074f2522013-04-17 14:08:20 +02006757 rt2800_init_rfcsr(rt2x00dev);
6758
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006759 if (rt2x00_is_usb(rt2x00dev) &&
6760 (rt2x00_rt(rt2x00dev, RT3070) ||
6761 rt2x00_rt(rt2x00dev, RT3071) ||
6762 rt2x00_rt(rt2x00dev, RT3572))) {
6763 udelay(200);
6764 rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
6765 udelay(10);
6766 }
6767
6768 /*
6769 * Enable RX.
6770 */
6771 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
6772 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
6773 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
6774 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
6775
6776 udelay(50);
6777
6778 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
6779 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
6780 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
6781 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
6782 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
6783 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
6784
6785 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
6786 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
6787 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
6788 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
6789
6790 /*
6791 * Initialize LED control
6792 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006793 rt2800_eeprom_read(rt2x00dev, EEPROM_LED_AG_CONF, &word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006794 rt2800_mcu_request(rt2x00dev, MCU_LED_AG_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006795 word & 0xff, (word >> 8) & 0xff);
6796
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006797 rt2800_eeprom_read(rt2x00dev, EEPROM_LED_ACT_CONF, &word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006798 rt2800_mcu_request(rt2x00dev, MCU_LED_ACT_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006799 word & 0xff, (word >> 8) & 0xff);
6800
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006801 rt2800_eeprom_read(rt2x00dev, EEPROM_LED_POLARITY, &word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006802 rt2800_mcu_request(rt2x00dev, MCU_LED_LED_POLARITY, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006803 word & 0xff, (word >> 8) & 0xff);
6804
6805 return 0;
6806}
6807EXPORT_SYMBOL_GPL(rt2800_enable_radio);
6808
6809void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
6810{
6811 u32 reg;
6812
Jakub Kicinskif7b395e2012-04-03 03:40:47 +02006813 rt2800_disable_wpdma(rt2x00dev);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006814
6815 /* Wait for DMA, ignore error */
6816 rt2800_wait_wpdma_ready(rt2x00dev);
6817
6818 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
6819 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
6820 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
6821 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02006822}
6823EXPORT_SYMBOL_GPL(rt2800_disable_radio);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01006824
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006825int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
6826{
6827 u32 reg;
Woody Hunga89534e2012-06-13 15:01:16 +08006828 u16 efuse_ctrl_reg;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006829
Woody Hunga89534e2012-06-13 15:01:16 +08006830 if (rt2x00_rt(rt2x00dev, RT3290))
6831 efuse_ctrl_reg = EFUSE_CTRL_3290;
6832 else
6833 efuse_ctrl_reg = EFUSE_CTRL;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006834
Woody Hunga89534e2012-06-13 15:01:16 +08006835 rt2800_register_read(rt2x00dev, efuse_ctrl_reg, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006836 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
6837}
6838EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
6839
6840static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
6841{
6842 u32 reg;
Woody Hunga89534e2012-06-13 15:01:16 +08006843 u16 efuse_ctrl_reg;
6844 u16 efuse_data0_reg;
6845 u16 efuse_data1_reg;
6846 u16 efuse_data2_reg;
6847 u16 efuse_data3_reg;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006848
Woody Hunga89534e2012-06-13 15:01:16 +08006849 if (rt2x00_rt(rt2x00dev, RT3290)) {
6850 efuse_ctrl_reg = EFUSE_CTRL_3290;
6851 efuse_data0_reg = EFUSE_DATA0_3290;
6852 efuse_data1_reg = EFUSE_DATA1_3290;
6853 efuse_data2_reg = EFUSE_DATA2_3290;
6854 efuse_data3_reg = EFUSE_DATA3_3290;
6855 } else {
6856 efuse_ctrl_reg = EFUSE_CTRL;
6857 efuse_data0_reg = EFUSE_DATA0;
6858 efuse_data1_reg = EFUSE_DATA1;
6859 efuse_data2_reg = EFUSE_DATA2;
6860 efuse_data3_reg = EFUSE_DATA3;
6861 }
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01006862 mutex_lock(&rt2x00dev->csr_mutex);
6863
Woody Hunga89534e2012-06-13 15:01:16 +08006864 rt2800_register_read_lock(rt2x00dev, efuse_ctrl_reg, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006865 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
6866 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
6867 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Woody Hunga89534e2012-06-13 15:01:16 +08006868 rt2800_register_write_lock(rt2x00dev, efuse_ctrl_reg, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006869
6870 /* Wait until the EEPROM has been loaded */
Woody Hunga89534e2012-06-13 15:01:16 +08006871 rt2800_regbusy_read(rt2x00dev, efuse_ctrl_reg, EFUSE_CTRL_KICK, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006872 /* Apparently the data is read from end to start */
Woody Hunga89534e2012-06-13 15:01:16 +08006873 rt2800_register_read_lock(rt2x00dev, efuse_data3_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006874 /* The returned value is in CPU order, but eeprom is le */
Gertjan van Wingerde68fa64e2011-11-16 23:16:15 +01006875 *(u32 *)&rt2x00dev->eeprom[i] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08006876 rt2800_register_read_lock(rt2x00dev, efuse_data2_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006877 *(u32 *)&rt2x00dev->eeprom[i + 2] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08006878 rt2800_register_read_lock(rt2x00dev, efuse_data1_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006879 *(u32 *)&rt2x00dev->eeprom[i + 4] = cpu_to_le32(reg);
Woody Hunga89534e2012-06-13 15:01:16 +08006880 rt2800_register_read_lock(rt2x00dev, efuse_data0_reg, &reg);
Larry Fingerdaabead2011-09-14 16:50:23 -05006881 *(u32 *)&rt2x00dev->eeprom[i + 6] = cpu_to_le32(reg);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01006882
6883 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006884}
6885
Gabor Juhosa02308e2012-12-29 14:51:51 +01006886int rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006887{
6888 unsigned int i;
6889
6890 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
6891 rt2800_efuse_read(rt2x00dev, i);
Gabor Juhosa02308e2012-12-29 14:51:51 +01006892
6893 return 0;
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01006894}
6895EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
6896
Gabor Juhosa3f16252013-07-08 16:08:25 +02006897static u8 rt2800_get_txmixer_gain_24g(struct rt2x00_dev *rt2x00dev)
6898{
6899 u16 word;
6900
Gabor Juhos6316c782013-07-08 16:08:26 +02006901 if (rt2x00_rt(rt2x00dev, RT3593))
6902 return 0;
6903
Gabor Juhosa3f16252013-07-08 16:08:25 +02006904 rt2800_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &word);
6905 if ((word & 0x00ff) != 0x00ff)
6906 return rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_BG_VAL);
6907
6908 return 0;
6909}
6910
6911static u8 rt2800_get_txmixer_gain_5g(struct rt2x00_dev *rt2x00dev)
6912{
6913 u16 word;
6914
Gabor Juhos6316c782013-07-08 16:08:26 +02006915 if (rt2x00_rt(rt2x00dev, RT3593))
6916 return 0;
6917
Gabor Juhosa3f16252013-07-08 16:08:25 +02006918 rt2800_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_A, &word);
6919 if ((word & 0x00ff) != 0x00ff)
6920 return rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_A_VAL);
6921
6922 return 0;
6923}
6924
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02006925static int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006926{
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01006927 struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006928 u16 word;
6929 u8 *mac;
6930 u8 default_lna_gain;
Gabor Juhosa02308e2012-12-29 14:51:51 +01006931 int retval;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006932
6933 /*
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02006934 * Read the EEPROM.
6935 */
Gabor Juhosa02308e2012-12-29 14:51:51 +01006936 retval = rt2800_read_eeprom(rt2x00dev);
6937 if (retval)
6938 return retval;
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02006939
6940 /*
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006941 * Start validation of the data that has been read.
6942 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006943 mac = rt2800_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006944 if (!is_valid_ether_addr(mac)) {
Joe Perchesf4f7f4142012-07-12 19:33:08 +00006945 eth_random_addr(mac);
Joe Perchesec9c4982013-04-19 08:33:40 -07006946 rt2x00_eeprom_dbg(rt2x00dev, "MAC: %pM\n", mac);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006947 }
6948
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006949 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006950 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006951 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
6952 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_TXPATH, 1);
6953 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RF_TYPE, RF2820);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006954 rt2800_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Joe Perchesec9c4982013-04-19 08:33:40 -07006955 rt2x00_eeprom_dbg(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01006956 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02006957 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006958 /*
6959 * There is a max of 2 RX streams for RT28x0 series
6960 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006961 if (rt2x00_get_field16(word, EEPROM_NIC_CONF0_RXPATH) > 2)
6962 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006963 rt2800_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006964 }
6965
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006966 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006967 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01006968 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_HW_RADIO, 0);
6969 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC, 0);
6970 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G, 0);
6971 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G, 0);
6972 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_CARDBUS_ACCEL, 0);
6973 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_2G, 0);
6974 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_5G, 0);
6975 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_WPS_PBC, 0);
6976 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_2G, 0);
6977 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_5G, 0);
6978 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BROADBAND_EXT_LNA, 0);
6979 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_ANT_DIVERSITY, 0);
6980 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_INTERNAL_TX_ALC, 0);
6981 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BT_COEXIST, 0);
6982 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_DAC_TEST, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006983 rt2800_eeprom_write(rt2x00dev, EEPROM_NIC_CONF1, word);
Joe Perchesec9c4982013-04-19 08:33:40 -07006984 rt2x00_eeprom_dbg(rt2x00dev, "NIC: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006985 }
6986
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006987 rt2800_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006988 if ((word & 0x00ff) == 0x00ff) {
6989 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006990 rt2800_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
Joe Perchesec9c4982013-04-19 08:33:40 -07006991 rt2x00_eeprom_dbg(rt2x00dev, "Freq: 0x%04x\n", word);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02006992 }
6993 if ((word & 0xff00) == 0xff00) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01006994 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
6995 LED_MODE_TXRX_ACTIVITY);
6996 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02006997 rt2800_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
6998 rt2800_eeprom_write(rt2x00dev, EEPROM_LED_AG_CONF, 0x5555);
6999 rt2800_eeprom_write(rt2x00dev, EEPROM_LED_ACT_CONF, 0x2221);
7000 rt2800_eeprom_write(rt2x00dev, EEPROM_LED_POLARITY, 0xa9f8);
Joe Perchesec9c4982013-04-19 08:33:40 -07007001 rt2x00_eeprom_dbg(rt2x00dev, "Led Mode: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007002 }
7003
7004 /*
7005 * During the LNA validation we are going to use
7006 * lna0 as correct value. Note that EEPROM_LNA
7007 * is never validated.
7008 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007009 rt2800_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007010 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
7011
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007012 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007013 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
7014 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
7015 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
7016 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007017 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007018
Gabor Juhosa3f16252013-07-08 16:08:25 +02007019 drv_data->txmixer_gain_24g = rt2800_get_txmixer_gain_24g(rt2x00dev);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01007020
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007021 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007022 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
7023 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
Gabor Juhosf36bb0c2013-07-08 16:08:27 +02007024 if (!rt2x00_rt(rt2x00dev, RT3593)) {
7025 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
7026 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
7027 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
7028 default_lna_gain);
7029 }
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007030 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007031
Gabor Juhosa3f16252013-07-08 16:08:25 +02007032 drv_data->txmixer_gain_5g = rt2800_get_txmixer_gain_5g(rt2x00dev);
Gertjan van Wingerde77c06c22012-02-06 23:45:13 +01007033
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007034 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007035 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
7036 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
7037 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
7038 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007039 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007040
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007041 rt2800_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007042 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
7043 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
Gabor Juhosf36bb0c2013-07-08 16:08:27 +02007044 if (!rt2x00_rt(rt2x00dev, RT3593)) {
7045 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
7046 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
7047 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
7048 default_lna_gain);
7049 }
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007050 rt2800_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007051
Gabor Juhosf36bb0c2013-07-08 16:08:27 +02007052 if (rt2x00_rt(rt2x00dev, RT3593)) {
7053 rt2800_eeprom_read(rt2x00dev, EEPROM_EXT_LNA2, &word);
7054 if (rt2x00_get_field16(word, EEPROM_EXT_LNA2_A1) == 0x00 ||
7055 rt2x00_get_field16(word, EEPROM_EXT_LNA2_A1) == 0xff)
7056 rt2x00_set_field16(&word, EEPROM_EXT_LNA2_A1,
7057 default_lna_gain);
7058 if (rt2x00_get_field16(word, EEPROM_EXT_LNA2_A2) == 0x00 ||
7059 rt2x00_get_field16(word, EEPROM_EXT_LNA2_A2) == 0xff)
7060 rt2x00_set_field16(&word, EEPROM_EXT_LNA2_A1,
7061 default_lna_gain);
7062 rt2800_eeprom_write(rt2x00dev, EEPROM_EXT_LNA2, word);
7063 }
7064
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007065 return 0;
7066}
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007067
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007068static int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007069{
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007070 u16 value;
7071 u16 eeprom;
Gabor Juhos86868b22013-03-30 14:53:09 +01007072 u16 rf;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007073
Gabor Juhos86868b22013-03-30 14:53:09 +01007074 /*
7075 * Read EEPROM word for configuration.
7076 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007077 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Gabor Juhos86868b22013-03-30 14:53:09 +01007078
7079 /*
7080 * Identify RF chipset by EEPROM value
7081 * RT28xx/RT30xx: defined in "EEPROM_NIC_CONF0_RF_TYPE" field
7082 * RT53xx: defined in "EEPROM_CHIP_ID" field
7083 */
7084 if (rt2x00_rt(rt2x00dev, RT3290) ||
7085 rt2x00_rt(rt2x00dev, RT5390) ||
7086 rt2x00_rt(rt2x00dev, RT5392))
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007087 rt2800_eeprom_read(rt2x00dev, EEPROM_CHIP_ID, &rf);
Gabor Juhos86868b22013-03-30 14:53:09 +01007088 else
7089 rf = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RF_TYPE);
7090
7091 switch (rf) {
Larry Fingerd331eb52011-09-14 16:50:22 -05007092 case RF2820:
7093 case RF2850:
7094 case RF2720:
7095 case RF2750:
7096 case RF3020:
7097 case RF2020:
7098 case RF3021:
7099 case RF3022:
7100 case RF3052:
Gabor Juhos0f5af262013-07-08 16:08:32 +02007101 case RF3053:
Stanislaw Gruszka3b9b74b2013-09-25 15:34:55 +02007102 case RF3070:
Woody Hunga89534e2012-06-13 15:01:16 +08007103 case RF3290:
Larry Fingerd331eb52011-09-14 16:50:22 -05007104 case RF3320:
Daniel Golle03839952012-09-09 14:24:39 +03007105 case RF3322:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02007106 case RF5360:
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05007107 case RF5362:
Larry Fingerd331eb52011-09-14 16:50:22 -05007108 case RF5370:
John Li2ed71882012-02-17 17:33:06 +08007109 case RF5372:
Larry Fingerd331eb52011-09-14 16:50:22 -05007110 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08007111 case RF5392:
Stanislaw Gruszkab8863f82013-03-16 19:19:30 +01007112 case RF5592:
Larry Fingerd331eb52011-09-14 16:50:22 -05007113 break;
7114 default:
Joe Perchesec9c4982013-04-19 08:33:40 -07007115 rt2x00_err(rt2x00dev, "Invalid RF chipset 0x%04x detected\n",
7116 rf);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007117 return -ENODEV;
7118 }
7119
Gabor Juhos86868b22013-03-30 14:53:09 +01007120 rt2x00_set_rf(rt2x00dev, rf);
7121
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007122 /*
7123 * Identify default antenna configuration.
7124 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01007125 rt2x00dev->default_ant.tx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007126 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01007127 rt2x00dev->default_ant.rx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007128 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007129
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007130 rt2800_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01007131
7132 if (rt2x00_rt(rt2x00dev, RT3070) ||
7133 rt2x00_rt(rt2x00dev, RT3090) ||
Daniel Golle03839952012-09-09 14:24:39 +03007134 rt2x00_rt(rt2x00dev, RT3352) ||
RA-Jay Hungd96aa642011-02-20 13:54:52 +01007135 rt2x00_rt(rt2x00dev, RT3390)) {
7136 value = rt2x00_get_field16(eeprom,
7137 EEPROM_NIC_CONF1_ANT_DIVERSITY);
7138 switch (value) {
7139 case 0:
7140 case 1:
7141 case 2:
7142 rt2x00dev->default_ant.tx = ANTENNA_A;
7143 rt2x00dev->default_ant.rx = ANTENNA_A;
7144 break;
7145 case 3:
7146 rt2x00dev->default_ant.tx = ANTENNA_A;
7147 rt2x00dev->default_ant.rx = ANTENNA_B;
7148 break;
7149 }
7150 } else {
7151 rt2x00dev->default_ant.tx = ANTENNA_A;
7152 rt2x00dev->default_ant.rx = ANTENNA_A;
7153 }
7154
Anisse Astier0586a112012-04-23 12:33:11 +02007155 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390R)) {
7156 rt2x00dev->default_ant.tx = ANTENNA_HW_DIVERSITY; /* Unused */
7157 rt2x00dev->default_ant.rx = ANTENNA_HW_DIVERSITY; /* Unused */
7158 }
7159
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007160 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02007161 * Determine external LNA informations.
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007162 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007163 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02007164 __set_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007165 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02007166 __set_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007167
7168 /*
7169 * Detect if this device has an hardware controlled radio.
7170 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01007171 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_HW_RADIO))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02007172 __set_bit(CAPABILITY_HW_BUTTON, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007173
7174 /*
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02007175 * Detect if this device has Bluetooth co-existence.
7176 */
7177 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_BT_COEXIST))
7178 __set_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags);
7179
7180 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02007181 * Read frequency offset and RF programming sequence.
7182 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007183 rt2800_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02007184 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
7185
7186 /*
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007187 * Store led settings, for correct led behaviour.
7188 */
7189#ifdef CONFIG_RT2X00_LIB_LEDS
7190 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
7191 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
7192 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
7193
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02007194 rt2x00dev->led_mcu_reg = eeprom;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007195#endif /* CONFIG_RT2X00_LIB_LEDS */
7196
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01007197 /*
7198 * Check if support EIRP tx power limit feature.
7199 */
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007200 rt2800_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER, &eeprom);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01007201
7202 if (rt2x00_get_field16(eeprom, EEPROM_EIRP_MAX_TX_POWER_2GHZ) <
7203 EIRP_MAX_TX_POWER_LIMIT)
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02007204 __set_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01007205
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007206 return 0;
7207}
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01007208
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007209/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02007210 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007211 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
7212 */
7213static const struct rf_channel rf_vals[] = {
7214 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
7215 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
7216 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
7217 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
7218 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
7219 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
7220 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
7221 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
7222 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
7223 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
7224 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
7225 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
7226 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
7227 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
7228
7229 /* 802.11 UNI / HyperLan 2 */
7230 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
7231 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
7232 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
7233 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
7234 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
7235 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
7236 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
7237 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
7238 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
7239 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
7240 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
7241 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
7242
7243 /* 802.11 HyperLan 2 */
7244 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
7245 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
7246 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
7247 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
7248 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
7249 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
7250 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
7251 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
7252 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
7253 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
7254 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
7255 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
7256 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
7257 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
7258 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
7259 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
7260
7261 /* 802.11 UNII */
7262 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
7263 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
7264 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
7265 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
7266 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
7267 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
7268 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
7269 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
7270 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
7271 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
7272 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
7273
7274 /* 802.11 Japan */
7275 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
7276 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
7277 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
7278 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
7279 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
7280 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
7281 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
7282};
7283
7284/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02007285 * RF value list for rt3xxx
Kevin Lob6b561c2013-10-14 10:05:45 +08007286 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052 & RF3053)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007287 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02007288static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007289 {1, 241, 2, 2 },
7290 {2, 241, 2, 7 },
7291 {3, 242, 2, 2 },
7292 {4, 242, 2, 7 },
7293 {5, 243, 2, 2 },
7294 {6, 243, 2, 7 },
7295 {7, 244, 2, 2 },
7296 {8, 244, 2, 7 },
7297 {9, 245, 2, 2 },
7298 {10, 245, 2, 7 },
7299 {11, 246, 2, 2 },
7300 {12, 246, 2, 7 },
7301 {13, 247, 2, 2 },
7302 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02007303
7304 /* 802.11 UNI / HyperLan 2 */
7305 {36, 0x56, 0, 4},
7306 {38, 0x56, 0, 6},
7307 {40, 0x56, 0, 8},
7308 {44, 0x57, 0, 0},
7309 {46, 0x57, 0, 2},
7310 {48, 0x57, 0, 4},
7311 {52, 0x57, 0, 8},
7312 {54, 0x57, 0, 10},
7313 {56, 0x58, 0, 0},
7314 {60, 0x58, 0, 4},
7315 {62, 0x58, 0, 6},
7316 {64, 0x58, 0, 8},
7317
7318 /* 802.11 HyperLan 2 */
7319 {100, 0x5b, 0, 8},
7320 {102, 0x5b, 0, 10},
7321 {104, 0x5c, 0, 0},
7322 {108, 0x5c, 0, 4},
7323 {110, 0x5c, 0, 6},
7324 {112, 0x5c, 0, 8},
7325 {116, 0x5d, 0, 0},
7326 {118, 0x5d, 0, 2},
7327 {120, 0x5d, 0, 4},
7328 {124, 0x5d, 0, 8},
7329 {126, 0x5d, 0, 10},
7330 {128, 0x5e, 0, 0},
7331 {132, 0x5e, 0, 4},
7332 {134, 0x5e, 0, 6},
7333 {136, 0x5e, 0, 8},
7334 {140, 0x5f, 0, 0},
7335
7336 /* 802.11 UNII */
7337 {149, 0x5f, 0, 9},
7338 {151, 0x5f, 0, 11},
7339 {153, 0x60, 0, 1},
7340 {157, 0x60, 0, 5},
7341 {159, 0x60, 0, 7},
7342 {161, 0x60, 0, 9},
7343 {165, 0x61, 0, 1},
7344 {167, 0x61, 0, 3},
7345 {169, 0x61, 0, 5},
7346 {171, 0x61, 0, 7},
7347 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007348};
7349
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01007350static const struct rf_channel rf_vals_5592_xtal20[] = {
7351 /* Channel, N, K, mod, R */
7352 {1, 482, 4, 10, 3},
7353 {2, 483, 4, 10, 3},
7354 {3, 484, 4, 10, 3},
7355 {4, 485, 4, 10, 3},
7356 {5, 486, 4, 10, 3},
7357 {6, 487, 4, 10, 3},
7358 {7, 488, 4, 10, 3},
7359 {8, 489, 4, 10, 3},
7360 {9, 490, 4, 10, 3},
7361 {10, 491, 4, 10, 3},
7362 {11, 492, 4, 10, 3},
7363 {12, 493, 4, 10, 3},
7364 {13, 494, 4, 10, 3},
7365 {14, 496, 8, 10, 3},
7366 {36, 172, 8, 12, 1},
7367 {38, 173, 0, 12, 1},
7368 {40, 173, 4, 12, 1},
7369 {42, 173, 8, 12, 1},
7370 {44, 174, 0, 12, 1},
7371 {46, 174, 4, 12, 1},
7372 {48, 174, 8, 12, 1},
7373 {50, 175, 0, 12, 1},
7374 {52, 175, 4, 12, 1},
7375 {54, 175, 8, 12, 1},
7376 {56, 176, 0, 12, 1},
7377 {58, 176, 4, 12, 1},
7378 {60, 176, 8, 12, 1},
7379 {62, 177, 0, 12, 1},
7380 {64, 177, 4, 12, 1},
7381 {100, 183, 4, 12, 1},
7382 {102, 183, 8, 12, 1},
7383 {104, 184, 0, 12, 1},
7384 {106, 184, 4, 12, 1},
7385 {108, 184, 8, 12, 1},
7386 {110, 185, 0, 12, 1},
7387 {112, 185, 4, 12, 1},
7388 {114, 185, 8, 12, 1},
7389 {116, 186, 0, 12, 1},
7390 {118, 186, 4, 12, 1},
7391 {120, 186, 8, 12, 1},
7392 {122, 187, 0, 12, 1},
7393 {124, 187, 4, 12, 1},
7394 {126, 187, 8, 12, 1},
7395 {128, 188, 0, 12, 1},
7396 {130, 188, 4, 12, 1},
7397 {132, 188, 8, 12, 1},
7398 {134, 189, 0, 12, 1},
7399 {136, 189, 4, 12, 1},
7400 {138, 189, 8, 12, 1},
7401 {140, 190, 0, 12, 1},
7402 {149, 191, 6, 12, 1},
7403 {151, 191, 10, 12, 1},
7404 {153, 192, 2, 12, 1},
7405 {155, 192, 6, 12, 1},
7406 {157, 192, 10, 12, 1},
7407 {159, 193, 2, 12, 1},
7408 {161, 193, 6, 12, 1},
7409 {165, 194, 2, 12, 1},
7410 {184, 164, 0, 12, 1},
7411 {188, 164, 4, 12, 1},
7412 {192, 165, 8, 12, 1},
7413 {196, 166, 0, 12, 1},
7414};
7415
7416static const struct rf_channel rf_vals_5592_xtal40[] = {
7417 /* Channel, N, K, mod, R */
7418 {1, 241, 2, 10, 3},
7419 {2, 241, 7, 10, 3},
7420 {3, 242, 2, 10, 3},
7421 {4, 242, 7, 10, 3},
7422 {5, 243, 2, 10, 3},
7423 {6, 243, 7, 10, 3},
7424 {7, 244, 2, 10, 3},
7425 {8, 244, 7, 10, 3},
7426 {9, 245, 2, 10, 3},
7427 {10, 245, 7, 10, 3},
7428 {11, 246, 2, 10, 3},
7429 {12, 246, 7, 10, 3},
7430 {13, 247, 2, 10, 3},
7431 {14, 248, 4, 10, 3},
7432 {36, 86, 4, 12, 1},
7433 {38, 86, 6, 12, 1},
7434 {40, 86, 8, 12, 1},
7435 {42, 86, 10, 12, 1},
7436 {44, 87, 0, 12, 1},
7437 {46, 87, 2, 12, 1},
7438 {48, 87, 4, 12, 1},
7439 {50, 87, 6, 12, 1},
7440 {52, 87, 8, 12, 1},
7441 {54, 87, 10, 12, 1},
7442 {56, 88, 0, 12, 1},
7443 {58, 88, 2, 12, 1},
7444 {60, 88, 4, 12, 1},
7445 {62, 88, 6, 12, 1},
7446 {64, 88, 8, 12, 1},
7447 {100, 91, 8, 12, 1},
7448 {102, 91, 10, 12, 1},
7449 {104, 92, 0, 12, 1},
7450 {106, 92, 2, 12, 1},
7451 {108, 92, 4, 12, 1},
7452 {110, 92, 6, 12, 1},
7453 {112, 92, 8, 12, 1},
7454 {114, 92, 10, 12, 1},
7455 {116, 93, 0, 12, 1},
7456 {118, 93, 2, 12, 1},
7457 {120, 93, 4, 12, 1},
7458 {122, 93, 6, 12, 1},
7459 {124, 93, 8, 12, 1},
7460 {126, 93, 10, 12, 1},
7461 {128, 94, 0, 12, 1},
7462 {130, 94, 2, 12, 1},
7463 {132, 94, 4, 12, 1},
7464 {134, 94, 6, 12, 1},
7465 {136, 94, 8, 12, 1},
7466 {138, 94, 10, 12, 1},
7467 {140, 95, 0, 12, 1},
7468 {149, 95, 9, 12, 1},
7469 {151, 95, 11, 12, 1},
7470 {153, 96, 1, 12, 1},
7471 {155, 96, 3, 12, 1},
7472 {157, 96, 5, 12, 1},
7473 {159, 96, 7, 12, 1},
7474 {161, 96, 9, 12, 1},
7475 {165, 97, 1, 12, 1},
7476 {184, 82, 0, 12, 1},
7477 {188, 82, 4, 12, 1},
7478 {192, 82, 8, 12, 1},
7479 {196, 83, 0, 12, 1},
7480};
7481
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007482static int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007483{
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007484 struct hw_mode_spec *spec = &rt2x00dev->spec;
7485 struct channel_info *info;
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02007486 char *default_power1;
7487 char *default_power2;
Gabor Juhosc0a14362013-07-08 16:08:28 +02007488 char *default_power3;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007489 unsigned int i;
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01007490 u32 reg;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007491
7492 /*
Stanislaw Gruszka58e33a22014-01-29 17:42:37 +01007493 * Disable powersaving as default.
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01007494 */
Stanislaw Gruszka58e33a22014-01-29 17:42:37 +01007495 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01007496
7497 /*
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007498 * Initialize all hw fields.
7499 */
7500 rt2x00dev->hw->flags =
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007501 IEEE80211_HW_SIGNAL_DBM |
7502 IEEE80211_HW_SUPPORTS_PS |
Helmut Schaa1df90802010-06-29 21:38:12 +02007503 IEEE80211_HW_PS_NULLFUNC_STACK |
Helmut Schaa9d4f09b2012-03-14 08:56:47 +01007504 IEEE80211_HW_AMPDU_AGGREGATION |
Felix Fietkau2dfca312013-08-20 19:43:54 +02007505 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
7506 IEEE80211_HW_SUPPORTS_HT_CCK_RATES;
Helmut Schaa9d4f09b2012-03-14 08:56:47 +01007507
Helmut Schaa5a5b6ed2010-10-02 11:31:33 +02007508 /*
7509 * Don't set IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING for USB devices
7510 * unless we are capable of sending the buffered frames out after the
7511 * DTIM transmission using rt2x00lib_beacondone. This will send out
7512 * multicast and broadcast traffic immediately instead of buffering it
7513 * infinitly and thus dropping it after some time.
7514 */
7515 if (!rt2x00_is_usb(rt2x00dev))
7516 rt2x00dev->hw->flags |=
7517 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007518
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007519 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
7520 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007521 rt2800_eeprom_addr(rt2x00dev,
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007522 EEPROM_MAC_ADDR_0));
7523
Helmut Schaa3f2bee22010-06-14 22:12:01 +02007524 /*
7525 * As rt2800 has a global fallback table we cannot specify
7526 * more then one tx rate per frame but since the hw will
7527 * try several rates (based on the fallback table) we should
Helmut Schaaba3b9e52010-10-02 11:32:16 +02007528 * initialize max_report_rates to the maximum number of rates
Helmut Schaa3f2bee22010-06-14 22:12:01 +02007529 * we are going to try. Otherwise mac80211 will truncate our
7530 * reported tx rates and the rc algortihm will end up with
7531 * incorrect data.
7532 */
Helmut Schaaba3b9e52010-10-02 11:32:16 +02007533 rt2x00dev->hw->max_rates = 1;
7534 rt2x00dev->hw->max_report_rates = 7;
Helmut Schaa3f2bee22010-06-14 22:12:01 +02007535 rt2x00dev->hw->max_rate_tries = 1;
7536
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007537 /*
7538 * Initialize hw_mode information.
7539 */
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007540 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
7541
Gabor Juhos4a32c362013-10-14 21:59:51 +02007542 switch (rt2x00dev->chip.rf) {
7543 case RF2720:
7544 case RF2820:
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007545 spec->num_channels = 14;
7546 spec->channels = rf_vals;
Gabor Juhos4a32c362013-10-14 21:59:51 +02007547 break;
7548
7549 case RF2750:
7550 case RF2850:
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007551 spec->num_channels = ARRAY_SIZE(rf_vals);
7552 spec->channels = rf_vals;
Gabor Juhos4a32c362013-10-14 21:59:51 +02007553 break;
7554
7555 case RF2020:
7556 case RF3020:
7557 case RF3021:
7558 case RF3022:
7559 case RF3070:
7560 case RF3290:
7561 case RF3320:
7562 case RF3322:
7563 case RF5360:
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05007564 case RF5362:
Gabor Juhos4a32c362013-10-14 21:59:51 +02007565 case RF5370:
7566 case RF5372:
7567 case RF5390:
7568 case RF5392:
Ivo van Doorn55f93212010-05-06 14:45:46 +02007569 spec->num_channels = 14;
7570 spec->channels = rf_vals_3x;
Gabor Juhos4a32c362013-10-14 21:59:51 +02007571 break;
7572
7573 case RF3052:
7574 case RF3053:
Ivo van Doorn55f93212010-05-06 14:45:46 +02007575 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
7576 spec->channels = rf_vals_3x;
Gabor Juhos4a32c362013-10-14 21:59:51 +02007577 break;
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01007578
Gabor Juhos4a32c362013-10-14 21:59:51 +02007579 case RF5592:
Stanislaw Gruszka7848b232013-03-16 19:19:31 +01007580 rt2800_register_read(rt2x00dev, MAC_DEBUG_INDEX, &reg);
7581 if (rt2x00_get_field32(reg, MAC_DEBUG_INDEX_XTAL)) {
7582 spec->num_channels = ARRAY_SIZE(rf_vals_5592_xtal40);
7583 spec->channels = rf_vals_5592_xtal40;
7584 } else {
7585 spec->num_channels = ARRAY_SIZE(rf_vals_5592_xtal20);
7586 spec->channels = rf_vals_5592_xtal20;
7587 }
Gabor Juhos4a32c362013-10-14 21:59:51 +02007588 break;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007589 }
7590
Stanislaw Gruszka53216d62013-03-16 19:19:29 +01007591 if (WARN_ON_ONCE(!spec->channels))
7592 return -ENODEV;
7593
Gabor Juhos53c5a092013-10-14 21:59:52 +02007594 spec->supported_bands = SUPPORT_BAND_2GHZ;
7595 if (spec->num_channels > 14)
7596 spec->supported_bands |= SUPPORT_BAND_5GHZ;
7597
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007598 /*
7599 * Initialize HT information.
7600 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01007601 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01007602 spec->ht.ht_supported = true;
7603 else
7604 spec->ht.ht_supported = false;
7605
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007606 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02007607 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007608 IEEE80211_HT_CAP_GRN_FLD |
7609 IEEE80211_HT_CAP_SGI_20 |
Ivo van Doornaa674632010-06-29 21:48:37 +02007610 IEEE80211_HT_CAP_SGI_40;
Helmut Schaa22cabaa2010-06-03 10:52:10 +02007611
Gabor Juhosaa103502013-10-14 21:59:50 +02007612 if (rt2x00dev->default_ant.tx_chain_num >= 2)
Helmut Schaa22cabaa2010-06-03 10:52:10 +02007613 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
7614
Gabor Juhosaa103502013-10-14 21:59:50 +02007615 spec->ht.cap |= rt2x00dev->default_ant.rx_chain_num <<
7616 IEEE80211_HT_CAP_RX_STBC_SHIFT;
Ivo van Doornaa674632010-06-29 21:48:37 +02007617
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007618 spec->ht.ampdu_factor = 3;
7619 spec->ht.ampdu_density = 4;
7620 spec->ht.mcs.tx_params =
7621 IEEE80211_HT_MCS_TX_DEFINED |
7622 IEEE80211_HT_MCS_TX_RX_DIFF |
Gabor Juhosaa103502013-10-14 21:59:50 +02007623 ((rt2x00dev->default_ant.tx_chain_num - 1) <<
7624 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007625
Gabor Juhosaa103502013-10-14 21:59:50 +02007626 switch (rt2x00dev->default_ant.rx_chain_num) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007627 case 3:
7628 spec->ht.mcs.rx_mask[2] = 0xff;
7629 case 2:
7630 spec->ht.mcs.rx_mask[1] = 0xff;
7631 case 1:
7632 spec->ht.mcs.rx_mask[0] = 0xff;
7633 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
7634 break;
7635 }
7636
7637 /*
7638 * Create channel information array
7639 */
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00007640 info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007641 if (!info)
7642 return -ENOMEM;
7643
7644 spec->channels_info = info;
7645
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007646 default_power1 = rt2800_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
7647 default_power2 = rt2800_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007648
Gabor Juhosc0a14362013-07-08 16:08:28 +02007649 if (rt2x00dev->default_ant.tx_chain_num > 2)
7650 default_power3 = rt2800_eeprom_addr(rt2x00dev,
7651 EEPROM_EXT_TXPOWER_BG3);
7652 else
7653 default_power3 = NULL;
7654
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007655 for (i = 0; i < 14; i++) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01007656 info[i].default_power1 = default_power1[i];
7657 info[i].default_power2 = default_power2[i];
Gabor Juhosc0a14362013-07-08 16:08:28 +02007658 if (default_power3)
7659 info[i].default_power3 = default_power3[i];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007660 }
7661
7662 if (spec->num_channels > 14) {
Gabor Juhos3e38d3d2013-07-08 11:25:53 +02007663 default_power1 = rt2800_eeprom_addr(rt2x00dev,
7664 EEPROM_TXPOWER_A1);
7665 default_power2 = rt2800_eeprom_addr(rt2x00dev,
7666 EEPROM_TXPOWER_A2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007667
Gabor Juhosc0a14362013-07-08 16:08:28 +02007668 if (rt2x00dev->default_ant.tx_chain_num > 2)
7669 default_power3 =
7670 rt2800_eeprom_addr(rt2x00dev,
7671 EEPROM_EXT_TXPOWER_A3);
7672 else
7673 default_power3 = NULL;
7674
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007675 for (i = 14; i < spec->num_channels; i++) {
Gabor Juhos0a6f3a82013-06-22 13:13:25 +02007676 info[i].default_power1 = default_power1[i - 14];
7677 info[i].default_power2 = default_power2[i - 14];
Gabor Juhosc0a14362013-07-08 16:08:28 +02007678 if (default_power3)
7679 info[i].default_power3 = default_power3[i - 14];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007680 }
7681 }
7682
John Li2e9c43d2012-02-16 21:40:57 +08007683 switch (rt2x00dev->chip.rf) {
7684 case RF2020:
7685 case RF3020:
7686 case RF3021:
7687 case RF3022:
7688 case RF3320:
7689 case RF3052:
Gabor Juhos1095df02013-07-08 16:08:31 +02007690 case RF3053:
Stanislaw Gruszka3b9b74b2013-09-25 15:34:55 +02007691 case RF3070:
Woody Hunga89534e2012-06-13 15:01:16 +08007692 case RF3290:
villacis@palosanto.comccf91bd2012-05-16 21:07:12 +02007693 case RF5360:
Canek Peláez Valdésac0372a2014-08-24 19:06:11 -05007694 case RF5362:
John Li2e9c43d2012-02-16 21:40:57 +08007695 case RF5370:
7696 case RF5372:
7697 case RF5390:
Zero.Lincff3d1f2012-05-29 16:11:09 +08007698 case RF5392:
John Li2e9c43d2012-02-16 21:40:57 +08007699 __set_bit(CAPABILITY_VCO_RECALIBRATION, &rt2x00dev->cap_flags);
7700 break;
7701 }
7702
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007703 return 0;
7704}
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007705
Gabor Juhoscbafb602013-03-30 14:53:10 +01007706static int rt2800_probe_rt(struct rt2x00_dev *rt2x00dev)
7707{
7708 u32 reg;
7709 u32 rt;
7710 u32 rev;
7711
7712 if (rt2x00_rt(rt2x00dev, RT3290))
7713 rt2800_register_read(rt2x00dev, MAC_CSR0_3290, &reg);
7714 else
7715 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
7716
7717 rt = rt2x00_get_field32(reg, MAC_CSR0_CHIPSET);
7718 rev = rt2x00_get_field32(reg, MAC_CSR0_REVISION);
7719
7720 switch (rt) {
7721 case RT2860:
7722 case RT2872:
7723 case RT2883:
7724 case RT3070:
7725 case RT3071:
7726 case RT3090:
7727 case RT3290:
7728 case RT3352:
7729 case RT3390:
7730 case RT3572:
Gabor Juhos2dc2bd22013-07-08 16:08:33 +02007731 case RT3593:
Gabor Juhoscbafb602013-03-30 14:53:10 +01007732 case RT5390:
7733 case RT5392:
7734 case RT5592:
7735 break;
7736 default:
Joe Perchesec9c4982013-04-19 08:33:40 -07007737 rt2x00_err(rt2x00dev, "Invalid RT chipset 0x%04x, rev %04x detected\n",
7738 rt, rev);
Gabor Juhoscbafb602013-03-30 14:53:10 +01007739 return -ENODEV;
7740 }
7741
7742 rt2x00_set_rt(rt2x00dev, rt, rev);
7743
7744 return 0;
7745}
7746
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007747int rt2800_probe_hw(struct rt2x00_dev *rt2x00dev)
7748{
7749 int retval;
7750 u32 reg;
7751
Gabor Juhoscbafb602013-03-30 14:53:10 +01007752 retval = rt2800_probe_rt(rt2x00dev);
7753 if (retval)
7754 return retval;
7755
Gertjan van Wingerdead417a52012-09-03 03:25:51 +02007756 /*
7757 * Allocate eeprom data.
7758 */
7759 retval = rt2800_validate_eeprom(rt2x00dev);
7760 if (retval)
7761 return retval;
7762
7763 retval = rt2800_init_eeprom(rt2x00dev);
7764 if (retval)
7765 return retval;
7766
7767 /*
7768 * Enable rfkill polling by setting GPIO direction of the
7769 * rfkill switch GPIO pin correctly.
7770 */
7771 rt2800_register_read(rt2x00dev, GPIO_CTRL, &reg);
7772 rt2x00_set_field32(&reg, GPIO_CTRL_DIR2, 1);
7773 rt2800_register_write(rt2x00dev, GPIO_CTRL, reg);
7774
7775 /*
7776 * Initialize hw specifications.
7777 */
7778 retval = rt2800_probe_hw_mode(rt2x00dev);
7779 if (retval)
7780 return retval;
7781
7782 /*
7783 * Set device capabilities.
7784 */
7785 __set_bit(CAPABILITY_CONTROL_FILTERS, &rt2x00dev->cap_flags);
7786 __set_bit(CAPABILITY_CONTROL_FILTER_PSPOLL, &rt2x00dev->cap_flags);
7787 if (!rt2x00_is_usb(rt2x00dev))
7788 __set_bit(CAPABILITY_PRE_TBTT_INTERRUPT, &rt2x00dev->cap_flags);
7789
7790 /*
7791 * Set device requirements.
7792 */
7793 if (!rt2x00_is_soc(rt2x00dev))
7794 __set_bit(REQUIRE_FIRMWARE, &rt2x00dev->cap_flags);
7795 __set_bit(REQUIRE_L2PAD, &rt2x00dev->cap_flags);
7796 __set_bit(REQUIRE_TXSTATUS_FIFO, &rt2x00dev->cap_flags);
7797 if (!rt2800_hwcrypt_disabled(rt2x00dev))
7798 __set_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags);
7799 __set_bit(CAPABILITY_LINK_TUNING, &rt2x00dev->cap_flags);
7800 __set_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags);
7801 if (rt2x00_is_usb(rt2x00dev))
7802 __set_bit(REQUIRE_PS_AUTOWAKE, &rt2x00dev->cap_flags);
7803 else {
7804 __set_bit(REQUIRE_DMA, &rt2x00dev->cap_flags);
7805 __set_bit(REQUIRE_TASKLET_CONTEXT, &rt2x00dev->cap_flags);
7806 }
7807
7808 /*
7809 * Set the rssi offset.
7810 */
7811 rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
7812
7813 return 0;
7814}
7815EXPORT_SYMBOL_GPL(rt2800_probe_hw);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01007816
7817/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007818 * IEEE80211 stack callback functions.
7819 */
Johannes Berg9352c192015-04-20 18:12:41 +02007820void rt2800_get_key_seq(struct ieee80211_hw *hw,
7821 struct ieee80211_key_conf *key,
7822 struct ieee80211_key_seq *seq)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007823{
7824 struct rt2x00_dev *rt2x00dev = hw->priv;
7825 struct mac_iveiv_entry iveiv_entry;
7826 u32 offset;
7827
Johannes Berg9352c192015-04-20 18:12:41 +02007828 if (key->cipher != WLAN_CIPHER_SUITE_TKIP)
7829 return;
7830
7831 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007832 rt2800_register_multiread(rt2x00dev, offset,
7833 &iveiv_entry, sizeof(iveiv_entry));
7834
Johannes Berg9352c192015-04-20 18:12:41 +02007835 memcpy(&seq->tkip.iv16, &iveiv_entry.iv[0], 2);
7836 memcpy(&seq->tkip.iv32, &iveiv_entry.iv[4], 4);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007837}
Johannes Berg9352c192015-04-20 18:12:41 +02007838EXPORT_SYMBOL_GPL(rt2800_get_key_seq);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007839
Helmut Schaae7836192010-07-11 12:28:54 +02007840int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007841{
7842 struct rt2x00_dev *rt2x00dev = hw->priv;
7843 u32 reg;
7844 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
7845
7846 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
7847 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
7848 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
7849
7850 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
7851 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
7852 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
7853
7854 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
7855 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
7856 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
7857
7858 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
7859 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
7860 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
7861
7862 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
7863 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
7864 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
7865
7866 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
7867 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
7868 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
7869
7870 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
7871 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
7872 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
7873
7874 return 0;
7875}
Helmut Schaae7836192010-07-11 12:28:54 +02007876EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007877
Eliad Peller8a3a3c82011-10-02 10:15:52 +02007878int rt2800_conf_tx(struct ieee80211_hw *hw,
7879 struct ieee80211_vif *vif, u16 queue_idx,
Helmut Schaae7836192010-07-11 12:28:54 +02007880 const struct ieee80211_tx_queue_params *params)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007881{
7882 struct rt2x00_dev *rt2x00dev = hw->priv;
7883 struct data_queue *queue;
7884 struct rt2x00_field32 field;
7885 int retval;
7886 u32 reg;
7887 u32 offset;
7888
7889 /*
7890 * First pass the configuration through rt2x00lib, that will
7891 * update the queue settings and validate the input. After that
7892 * we are free to update the registers based on the value
7893 * in the queue parameter.
7894 */
Eliad Peller8a3a3c82011-10-02 10:15:52 +02007895 retval = rt2x00mac_conf_tx(hw, vif, queue_idx, params);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007896 if (retval)
7897 return retval;
7898
7899 /*
7900 * We only need to perform additional register initialization
7901 * for WMM queues/
7902 */
7903 if (queue_idx >= 4)
7904 return 0;
7905
Helmut Schaa11f818e2011-03-03 19:38:55 +01007906 queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007907
7908 /* Update WMM TXOP register */
7909 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
7910 field.bit_offset = (queue_idx & 1) * 16;
7911 field.bit_mask = 0xffff << field.bit_offset;
7912
7913 rt2800_register_read(rt2x00dev, offset, &reg);
7914 rt2x00_set_field32(&reg, field, queue->txop);
7915 rt2800_register_write(rt2x00dev, offset, reg);
7916
7917 /* Update WMM registers */
7918 field.bit_offset = queue_idx * 4;
7919 field.bit_mask = 0xf << field.bit_offset;
7920
7921 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
7922 rt2x00_set_field32(&reg, field, queue->aifs);
7923 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
7924
7925 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
7926 rt2x00_set_field32(&reg, field, queue->cw_min);
7927 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
7928
7929 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
7930 rt2x00_set_field32(&reg, field, queue->cw_max);
7931 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
7932
7933 /* Update EDCA registers */
7934 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
7935
7936 rt2800_register_read(rt2x00dev, offset, &reg);
7937 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
7938 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
7939 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
7940 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
7941 rt2800_register_write(rt2x00dev, offset, reg);
7942
7943 return 0;
7944}
Helmut Schaae7836192010-07-11 12:28:54 +02007945EXPORT_SYMBOL_GPL(rt2800_conf_tx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007946
Eliad Peller37a41b42011-09-21 14:06:11 +03007947u64 rt2800_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007948{
7949 struct rt2x00_dev *rt2x00dev = hw->priv;
7950 u64 tsf;
7951 u32 reg;
7952
7953 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
7954 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
7955 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
7956 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
7957
7958 return tsf;
7959}
Helmut Schaae7836192010-07-11 12:28:54 +02007960EXPORT_SYMBOL_GPL(rt2800_get_tsf);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01007961
Helmut Schaae7836192010-07-11 12:28:54 +02007962int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7963 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01007964 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
7965 u8 buf_size)
Helmut Schaa1df90802010-06-29 21:38:12 +02007966{
Helmut Schaaaf353232011-09-08 14:38:36 +02007967 struct rt2x00_sta *sta_priv = (struct rt2x00_sta *)sta->drv_priv;
Helmut Schaa1df90802010-06-29 21:38:12 +02007968 int ret = 0;
7969
Helmut Schaaaf353232011-09-08 14:38:36 +02007970 /*
7971 * Don't allow aggregation for stations the hardware isn't aware
7972 * of because tx status reports for frames to an unknown station
7973 * always contain wcid=255 and thus we can't distinguish between
7974 * multiple stations which leads to unwanted situations when the
7975 * hw reorders frames due to aggregation.
7976 */
7977 if (sta_priv->wcid < 0)
7978 return 1;
7979
Helmut Schaa1df90802010-06-29 21:38:12 +02007980 switch (action) {
7981 case IEEE80211_AMPDU_RX_START:
7982 case IEEE80211_AMPDU_RX_STOP:
Helmut Schaa58ed8262010-10-02 11:33:17 +02007983 /*
7984 * The hw itself takes care of setting up BlockAck mechanisms.
7985 * So, we only have to allow mac80211 to nagotiate a BlockAck
7986 * agreement. Once that is done, the hw will BlockAck incoming
7987 * AMPDUs without further setup.
7988 */
Helmut Schaa1df90802010-06-29 21:38:12 +02007989 break;
7990 case IEEE80211_AMPDU_TX_START:
7991 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
7992 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02007993 case IEEE80211_AMPDU_TX_STOP_CONT:
7994 case IEEE80211_AMPDU_TX_STOP_FLUSH:
7995 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Helmut Schaa1df90802010-06-29 21:38:12 +02007996 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
7997 break;
7998 case IEEE80211_AMPDU_TX_OPERATIONAL:
7999 break;
8000 default:
Joe Perchesec9c4982013-04-19 08:33:40 -07008001 rt2x00_warn((struct rt2x00_dev *)hw->priv,
8002 "Unknown AMPDU action\n");
Helmut Schaa1df90802010-06-29 21:38:12 +02008003 }
8004
8005 return ret;
8006}
Helmut Schaae7836192010-07-11 12:28:54 +02008007EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02008008
Helmut Schaa977206d2010-12-13 12:31:58 +01008009int rt2800_get_survey(struct ieee80211_hw *hw, int idx,
8010 struct survey_info *survey)
8011{
8012 struct rt2x00_dev *rt2x00dev = hw->priv;
8013 struct ieee80211_conf *conf = &hw->conf;
8014 u32 idle, busy, busy_ext;
8015
8016 if (idx != 0)
8017 return -ENOENT;
8018
Karl Beldan675a0b02013-03-25 16:26:57 +01008019 survey->channel = conf->chandef.chan;
Helmut Schaa977206d2010-12-13 12:31:58 +01008020
8021 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &idle);
8022 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &busy);
8023 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &busy_ext);
8024
8025 if (idle || busy) {
Johannes Berg4ed20be2014-11-14 16:35:34 +01008026 survey->filled = SURVEY_INFO_TIME |
8027 SURVEY_INFO_TIME_BUSY |
8028 SURVEY_INFO_TIME_EXT_BUSY;
Helmut Schaa977206d2010-12-13 12:31:58 +01008029
Johannes Berg4ed20be2014-11-14 16:35:34 +01008030 survey->time = (idle + busy) / 1000;
8031 survey->time_busy = busy / 1000;
8032 survey->time_ext_busy = busy_ext / 1000;
Helmut Schaa977206d2010-12-13 12:31:58 +01008033 }
8034
Helmut Schaa9931df22011-12-22 09:36:29 +01008035 if (!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
8036 survey->filled |= SURVEY_INFO_IN_USE;
8037
Helmut Schaa977206d2010-12-13 12:31:58 +01008038 return 0;
8039
8040}
8041EXPORT_SYMBOL_GPL(rt2800_get_survey);
8042
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02008043MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
8044MODULE_VERSION(DRV_VERSION);
8045MODULE_DESCRIPTION("Ralink RT2800 library");
8046MODULE_LICENSE("GPL");