blob: 3258301aa29cfb3d6e90c39ab74084cdf21f9c15 [file] [log] [blame]
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +01001/*
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01002 Copyright (C) 2009 Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
Gertjan van Wingerdecce5fc42009-11-10 22:42:40 +01003 Copyright (C) 2009 Gertjan van Wingerde <gwingerde@gmail.com>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +01004
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01005 Based on the original rt2800pci.c and rt2800usb.c.
6 Copyright (C) 2009 Ivo van Doorn <IvDoorn@gmail.com>
7 Copyright (C) 2009 Alban Browaeys <prahal@yahoo.com>
8 Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
9 Copyright (C) 2009 Luis Correia <luis.f.correia@gmail.com>
10 Copyright (C) 2009 Mattias Nissler <mattias.nissler@gmx.de>
11 Copyright (C) 2009 Mark Asselstine <asselsm@gmail.com>
12 Copyright (C) 2009 Xose Vazquez Perez <xose.vazquez@gmail.com>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010013 <http://rt2x00.serialmonkey.com>
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the
27 Free Software Foundation, Inc.,
28 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30
31/*
32 Module: rt2800lib
33 Abstract: rt2800 generic device routines.
34 */
35
36#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"
Gertjan van Wingerdeac394912009-12-23 00:03:23 +010041#if defined(CONFIG_RT2X00_LIB_USB) || defined(CONFIG_RT2X00_LIB_USB_MODULE)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010042#include "rt2x00usb.h"
43#endif
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010044#include "rt2800lib.h"
45#include "rt2800.h"
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010046#include "rt2800usb.h"
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010047
48MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
49MODULE_DESCRIPTION("rt2800 library");
50MODULE_LICENSE("GPL");
51
52/*
53 * Register access.
54 * All access to the CSR registers will go through the methods
55 * rt2800_register_read and rt2800_register_write.
56 * BBP and RF register require indirect register access,
57 * and use the CSR registers BBPCSR and RFCSR to achieve this.
58 * These indirect registers work with busy bits,
59 * and we will try maximal REGISTER_BUSY_COUNT times to access
60 * the register while taking a REGISTER_BUSY_DELAY us delay
61 * between each attampt. When the busy bit is still set at that time,
62 * the access attempt is considered to have failed,
63 * and we will print an error.
64 * The _lock versions must be used if you already hold the csr_mutex
65 */
66#define WAIT_FOR_BBP(__dev, __reg) \
67 rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
68#define WAIT_FOR_RFCSR(__dev, __reg) \
69 rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
70#define WAIT_FOR_RF(__dev, __reg) \
71 rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
72#define WAIT_FOR_MCU(__dev, __reg) \
73 rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
74 H2M_MAILBOX_CSR_OWNER, (__reg))
75
Helmut Schaabaff8002010-04-28 09:58:59 +020076static inline bool rt2800_is_305x_soc(struct rt2x00_dev *rt2x00dev)
77{
78 /* check for rt2872 on SoC */
79 if (!rt2x00_is_soc(rt2x00dev) ||
80 !rt2x00_rt(rt2x00dev, RT2872))
81 return false;
82
83 /* we know for sure that these rf chipsets are used on rt305x boards */
84 if (rt2x00_rf(rt2x00dev, RF3020) ||
85 rt2x00_rf(rt2x00dev, RF3021) ||
86 rt2x00_rf(rt2x00dev, RF3022))
87 return true;
88
89 NOTICE(rt2x00dev, "Unknown RF chipset on rt305x\n");
90 return false;
91}
92
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010093static void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
94 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010095{
96 u32 reg;
97
98 mutex_lock(&rt2x00dev->csr_mutex);
99
100 /*
101 * Wait until the BBP becomes available, afterwards we
102 * can safely write the new data into the register.
103 */
104 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
105 reg = 0;
106 rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
107 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
108 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
109 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100110 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100111 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
112
113 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
114 }
115
116 mutex_unlock(&rt2x00dev->csr_mutex);
117}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100118
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100119static void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
120 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100121{
122 u32 reg;
123
124 mutex_lock(&rt2x00dev->csr_mutex);
125
126 /*
127 * Wait until the BBP becomes available, afterwards we
128 * can safely write the read request into the register.
129 * After the data has been written, we wait until hardware
130 * returns the correct value, if at any time the register
131 * doesn't become available in time, reg will be 0xffffffff
132 * which means we return 0xff to the caller.
133 */
134 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
135 reg = 0;
136 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
137 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
138 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100139 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100140 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
141
142 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
143
144 WAIT_FOR_BBP(rt2x00dev, &reg);
145 }
146
147 *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
148
149 mutex_unlock(&rt2x00dev->csr_mutex);
150}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100151
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100152static void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
153 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100154{
155 u32 reg;
156
157 mutex_lock(&rt2x00dev->csr_mutex);
158
159 /*
160 * Wait until the RFCSR becomes available, afterwards we
161 * can safely write the new data into the register.
162 */
163 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
164 reg = 0;
165 rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
166 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
167 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
168 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
169
170 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
171 }
172
173 mutex_unlock(&rt2x00dev->csr_mutex);
174}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100175
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100176static void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
177 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100178{
179 u32 reg;
180
181 mutex_lock(&rt2x00dev->csr_mutex);
182
183 /*
184 * Wait until the RFCSR becomes available, afterwards we
185 * can safely write the read request into the register.
186 * After the data has been written, we wait until hardware
187 * returns the correct value, if at any time the register
188 * doesn't become available in time, reg will be 0xffffffff
189 * which means we return 0xff to the caller.
190 */
191 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
192 reg = 0;
193 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
194 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
195 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
196
197 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
198
199 WAIT_FOR_RFCSR(rt2x00dev, &reg);
200 }
201
202 *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
203
204 mutex_unlock(&rt2x00dev->csr_mutex);
205}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100206
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100207static void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
208 const unsigned int word, const u32 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100209{
210 u32 reg;
211
212 mutex_lock(&rt2x00dev->csr_mutex);
213
214 /*
215 * Wait until the RF becomes available, afterwards we
216 * can safely write the new data into the register.
217 */
218 if (WAIT_FOR_RF(rt2x00dev, &reg)) {
219 reg = 0;
220 rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
221 rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
222 rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
223 rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
224
225 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
226 rt2x00_rf_write(rt2x00dev, word, value);
227 }
228
229 mutex_unlock(&rt2x00dev->csr_mutex);
230}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100231
232void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
233 const u8 command, const u8 token,
234 const u8 arg0, const u8 arg1)
235{
236 u32 reg;
237
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100238 /*
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100239 * SOC devices don't support MCU requests.
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100240 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100241 if (rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100242 return;
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100243
244 mutex_lock(&rt2x00dev->csr_mutex);
245
246 /*
247 * Wait until the MCU becomes available, afterwards we
248 * can safely write the new data into the register.
249 */
250 if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
251 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
252 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
253 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
254 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
255 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
256
257 reg = 0;
258 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
259 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
260 }
261
262 mutex_unlock(&rt2x00dev->csr_mutex);
263}
264EXPORT_SYMBOL_GPL(rt2800_mcu_request);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100265
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100266int rt2800_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
267{
268 unsigned int i;
269 u32 reg;
270
271 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
272 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
273 if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
274 !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
275 return 0;
276
277 msleep(1);
278 }
279
280 ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
281 return -EACCES;
282}
283EXPORT_SYMBOL_GPL(rt2800_wait_wpdma_ready);
284
Gertjan van Wingerde0b8004a2010-06-03 10:51:45 +0200285void rt2800_write_txwi(__le32 *txwi, struct txentry_desc *txdesc)
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200286{
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200287 u32 word;
288
289 /*
290 * Initialize TX Info descriptor
291 */
292 rt2x00_desc_read(txwi, 0, &word);
293 rt2x00_set_field32(&word, TXWI_W0_FRAG,
294 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
295 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS, 0);
296 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
297 rt2x00_set_field32(&word, TXWI_W0_TS,
298 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
299 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
300 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
301 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
302 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->txop);
303 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
304 rt2x00_set_field32(&word, TXWI_W0_BW,
305 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
306 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
307 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
308 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
309 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
310 rt2x00_desc_write(txwi, 0, word);
311
312 rt2x00_desc_read(txwi, 1, &word);
313 rt2x00_set_field32(&word, TXWI_W1_ACK,
314 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
315 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
316 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
317 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
318 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
319 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
320 txdesc->key_idx : 0xff);
321 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
322 txdesc->length);
323 rt2x00_set_field32(&word, TXWI_W1_PACKETID, txdesc->queue + 1);
324 rt2x00_desc_write(txwi, 1, word);
325
326 /*
327 * Always write 0 to IV/EIV fields, hardware will insert the IV
328 * from the IVEIV register when TXD_W3_WIV is set to 0.
329 * When TXD_W3_WIV is set to 1 it will use the IV data
330 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
331 * crypto entry in the registers should be used to encrypt the frame.
332 */
333 _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
334 _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
335}
336EXPORT_SYMBOL_GPL(rt2800_write_txwi);
337
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200338void rt2800_process_rxwi(struct sk_buff *skb, struct rxdone_entry_desc *rxdesc)
339{
340 __le32 *rxwi = (__le32 *) skb->data;
341 u32 word;
342
343 rt2x00_desc_read(rxwi, 0, &word);
344
345 rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
346 rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
347
348 rt2x00_desc_read(rxwi, 1, &word);
349
350 if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
351 rxdesc->flags |= RX_FLAG_SHORT_GI;
352
353 if (rt2x00_get_field32(word, RXWI_W1_BW))
354 rxdesc->flags |= RX_FLAG_40MHZ;
355
356 /*
357 * Detect RX rate, always use MCS as signal type.
358 */
359 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
360 rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
361 rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
362
363 /*
364 * Mask of 0x8 bit to remove the short preamble flag.
365 */
366 if (rxdesc->rate_mode == RATE_MODE_CCK)
367 rxdesc->signal &= ~0x8;
368
369 rt2x00_desc_read(rxwi, 2, &word);
370
371 rxdesc->rssi =
372 (rt2x00_get_field32(word, RXWI_W2_RSSI0) +
373 rt2x00_get_field32(word, RXWI_W2_RSSI1)) / 2;
374
375 /*
376 * Remove RXWI descriptor from start of buffer.
377 */
378 skb_pull(skb, RXWI_DESC_SIZE);
379}
380EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
381
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100382#ifdef CONFIG_RT2X00_LIB_DEBUGFS
383const struct rt2x00debug rt2800_rt2x00debug = {
384 .owner = THIS_MODULE,
385 .csr = {
386 .read = rt2800_register_read,
387 .write = rt2800_register_write,
388 .flags = RT2X00DEBUGFS_OFFSET,
389 .word_base = CSR_REG_BASE,
390 .word_size = sizeof(u32),
391 .word_count = CSR_REG_SIZE / sizeof(u32),
392 },
393 .eeprom = {
394 .read = rt2x00_eeprom_read,
395 .write = rt2x00_eeprom_write,
396 .word_base = EEPROM_BASE,
397 .word_size = sizeof(u16),
398 .word_count = EEPROM_SIZE / sizeof(u16),
399 },
400 .bbp = {
401 .read = rt2800_bbp_read,
402 .write = rt2800_bbp_write,
403 .word_base = BBP_BASE,
404 .word_size = sizeof(u8),
405 .word_count = BBP_SIZE / sizeof(u8),
406 },
407 .rf = {
408 .read = rt2x00_rf_read,
409 .write = rt2800_rf_write,
410 .word_base = RF_BASE,
411 .word_size = sizeof(u32),
412 .word_count = RF_SIZE / sizeof(u32),
413 },
414};
415EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
416#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
417
418int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
419{
420 u32 reg;
421
422 rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
423 return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
424}
425EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
426
427#ifdef CONFIG_RT2X00_LIB_LEDS
428static void rt2800_brightness_set(struct led_classdev *led_cdev,
429 enum led_brightness brightness)
430{
431 struct rt2x00_led *led =
432 container_of(led_cdev, struct rt2x00_led, led_dev);
433 unsigned int enabled = brightness != LED_OFF;
434 unsigned int bg_mode =
435 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
436 unsigned int polarity =
437 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
438 EEPROM_FREQ_LED_POLARITY);
439 unsigned int ledmode =
440 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
441 EEPROM_FREQ_LED_MODE);
442
443 if (led->type == LED_TYPE_RADIO) {
444 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
445 enabled ? 0x20 : 0);
446 } else if (led->type == LED_TYPE_ASSOC) {
447 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
448 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
449 } else if (led->type == LED_TYPE_QUALITY) {
450 /*
451 * The brightness is divided into 6 levels (0 - 5),
452 * The specs tell us the following levels:
453 * 0, 1 ,3, 7, 15, 31
454 * to determine the level in a simple way we can simply
455 * work with bitshifting:
456 * (1 << level) - 1
457 */
458 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
459 (1 << brightness / (LED_FULL / 6)) - 1,
460 polarity);
461 }
462}
463
464static int rt2800_blink_set(struct led_classdev *led_cdev,
465 unsigned long *delay_on, unsigned long *delay_off)
466{
467 struct rt2x00_led *led =
468 container_of(led_cdev, struct rt2x00_led, led_dev);
469 u32 reg;
470
471 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
472 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
473 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100474 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
475
476 return 0;
477}
478
Gertjan van Wingerdeb3579d62009-12-30 11:36:34 +0100479static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100480 struct rt2x00_led *led, enum led_type type)
481{
482 led->rt2x00dev = rt2x00dev;
483 led->type = type;
484 led->led_dev.brightness_set = rt2800_brightness_set;
485 led->led_dev.blink_set = rt2800_blink_set;
486 led->flags = LED_INITIALIZED;
487}
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100488#endif /* CONFIG_RT2X00_LIB_LEDS */
489
490/*
491 * Configuration handlers.
492 */
493static void rt2800_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
494 struct rt2x00lib_crypto *crypto,
495 struct ieee80211_key_conf *key)
496{
497 struct mac_wcid_entry wcid_entry;
498 struct mac_iveiv_entry iveiv_entry;
499 u32 offset;
500 u32 reg;
501
502 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
503
504 rt2800_register_read(rt2x00dev, offset, &reg);
505 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
506 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
507 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
508 (crypto->cmd == SET_KEY) * crypto->cipher);
509 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
510 (crypto->cmd == SET_KEY) * crypto->bssidx);
511 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
512 rt2800_register_write(rt2x00dev, offset, reg);
513
514 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
515
516 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
517 if ((crypto->cipher == CIPHER_TKIP) ||
518 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
519 (crypto->cipher == CIPHER_AES))
520 iveiv_entry.iv[3] |= 0x20;
521 iveiv_entry.iv[3] |= key->keyidx << 6;
522 rt2800_register_multiwrite(rt2x00dev, offset,
523 &iveiv_entry, sizeof(iveiv_entry));
524
525 offset = MAC_WCID_ENTRY(key->hw_key_idx);
526
527 memset(&wcid_entry, 0, sizeof(wcid_entry));
528 if (crypto->cmd == SET_KEY)
529 memcpy(&wcid_entry, crypto->address, ETH_ALEN);
530 rt2800_register_multiwrite(rt2x00dev, offset,
531 &wcid_entry, sizeof(wcid_entry));
532}
533
534int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
535 struct rt2x00lib_crypto *crypto,
536 struct ieee80211_key_conf *key)
537{
538 struct hw_key_entry key_entry;
539 struct rt2x00_field32 field;
540 u32 offset;
541 u32 reg;
542
543 if (crypto->cmd == SET_KEY) {
544 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
545
546 memcpy(key_entry.key, crypto->key,
547 sizeof(key_entry.key));
548 memcpy(key_entry.tx_mic, crypto->tx_mic,
549 sizeof(key_entry.tx_mic));
550 memcpy(key_entry.rx_mic, crypto->rx_mic,
551 sizeof(key_entry.rx_mic));
552
553 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
554 rt2800_register_multiwrite(rt2x00dev, offset,
555 &key_entry, sizeof(key_entry));
556 }
557
558 /*
559 * The cipher types are stored over multiple registers
560 * starting with SHARED_KEY_MODE_BASE each word will have
561 * 32 bits and contains the cipher types for 2 bssidx each.
562 * Using the correct defines correctly will cause overhead,
563 * so just calculate the correct offset.
564 */
565 field.bit_offset = 4 * (key->hw_key_idx % 8);
566 field.bit_mask = 0x7 << field.bit_offset;
567
568 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
569
570 rt2800_register_read(rt2x00dev, offset, &reg);
571 rt2x00_set_field32(&reg, field,
572 (crypto->cmd == SET_KEY) * crypto->cipher);
573 rt2800_register_write(rt2x00dev, offset, reg);
574
575 /*
576 * Update WCID information
577 */
578 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
579
580 return 0;
581}
582EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
583
584int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
585 struct rt2x00lib_crypto *crypto,
586 struct ieee80211_key_conf *key)
587{
588 struct hw_key_entry key_entry;
589 u32 offset;
590
591 if (crypto->cmd == SET_KEY) {
592 /*
593 * 1 pairwise key is possible per AID, this means that the AID
594 * equals our hw_key_idx. Make sure the WCID starts _after_ the
595 * last possible shared key entry.
596 */
597 if (crypto->aid > (256 - 32))
598 return -ENOSPC;
599
600 key->hw_key_idx = 32 + crypto->aid;
601
602 memcpy(key_entry.key, crypto->key,
603 sizeof(key_entry.key));
604 memcpy(key_entry.tx_mic, crypto->tx_mic,
605 sizeof(key_entry.tx_mic));
606 memcpy(key_entry.rx_mic, crypto->rx_mic,
607 sizeof(key_entry.rx_mic));
608
609 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
610 rt2800_register_multiwrite(rt2x00dev, offset,
611 &key_entry, sizeof(key_entry));
612 }
613
614 /*
615 * Update WCID information
616 */
617 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
618
619 return 0;
620}
621EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
622
623void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
624 const unsigned int filter_flags)
625{
626 u32 reg;
627
628 /*
629 * Start configuration steps.
630 * Note that the version error will always be dropped
631 * and broadcast frames will always be accepted since
632 * there is no filter for it at this time.
633 */
634 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
635 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
636 !(filter_flags & FIF_FCSFAIL));
637 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
638 !(filter_flags & FIF_PLCPFAIL));
639 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
640 !(filter_flags & FIF_PROMISC_IN_BSS));
641 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
642 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
643 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
644 !(filter_flags & FIF_ALLMULTI));
645 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
646 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
647 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
648 !(filter_flags & FIF_CONTROL));
649 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
650 !(filter_flags & FIF_CONTROL));
651 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
652 !(filter_flags & FIF_CONTROL));
653 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
654 !(filter_flags & FIF_CONTROL));
655 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
656 !(filter_flags & FIF_CONTROL));
657 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
658 !(filter_flags & FIF_PSPOLL));
659 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
660 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
661 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
662 !(filter_flags & FIF_CONTROL));
663 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
664}
665EXPORT_SYMBOL_GPL(rt2800_config_filter);
666
667void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
668 struct rt2x00intf_conf *conf, const unsigned int flags)
669{
670 unsigned int beacon_base;
671 u32 reg;
672
673 if (flags & CONFIG_UPDATE_TYPE) {
674 /*
675 * Clear current synchronisation setup.
676 * For the Beacon base registers we only need to clear
677 * the first byte since that byte contains the VALID and OWNER
678 * bits which (when set to 0) will invalidate the entire beacon.
679 */
680 beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
681 rt2800_register_write(rt2x00dev, beacon_base, 0);
682
683 /*
684 * Enable synchronisation.
685 */
686 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
687 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
688 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Josef Bacik6a62e5e2009-11-15 21:33:18 -0500689 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE,
690 (conf->sync == TSF_SYNC_BEACON));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100691 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
692 }
693
694 if (flags & CONFIG_UPDATE_MAC) {
695 reg = le32_to_cpu(conf->mac[1]);
696 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
697 conf->mac[1] = cpu_to_le32(reg);
698
699 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
700 conf->mac, sizeof(conf->mac));
701 }
702
703 if (flags & CONFIG_UPDATE_BSSID) {
704 reg = le32_to_cpu(conf->bssid[1]);
705 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 0);
706 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 0);
707 conf->bssid[1] = cpu_to_le32(reg);
708
709 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
710 conf->bssid, sizeof(conf->bssid));
711 }
712}
713EXPORT_SYMBOL_GPL(rt2800_config_intf);
714
715void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp)
716{
717 u32 reg;
718
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100719 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
720 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
721 !!erp->short_preamble);
722 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
723 !!erp->short_preamble);
724 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
725
726 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
727 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
728 erp->cts_protection ? 2 : 0);
729 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
730
731 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
732 erp->basic_rates);
733 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
734
735 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
736 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, erp->slot_time);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100737 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
738
739 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100740 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100741 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
742
743 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
744 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
745 erp->beacon_int * 16);
746 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
747}
748EXPORT_SYMBOL_GPL(rt2800_config_erp);
749
750void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
751{
752 u8 r1;
753 u8 r3;
754
755 rt2800_bbp_read(rt2x00dev, 1, &r1);
756 rt2800_bbp_read(rt2x00dev, 3, &r3);
757
758 /*
759 * Configure the TX antenna.
760 */
761 switch ((int)ant->tx) {
762 case 1:
763 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100764 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100765 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
766 break;
767 case 2:
768 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
769 break;
770 case 3:
771 /* Do nothing */
772 break;
773 }
774
775 /*
776 * Configure the RX antenna.
777 */
778 switch ((int)ant->rx) {
779 case 1:
780 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
781 break;
782 case 2:
783 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
784 break;
785 case 3:
786 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
787 break;
788 }
789
790 rt2800_bbp_write(rt2x00dev, 3, r3);
791 rt2800_bbp_write(rt2x00dev, 1, r1);
792}
793EXPORT_SYMBOL_GPL(rt2800_config_ant);
794
795static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
796 struct rt2x00lib_conf *libconf)
797{
798 u16 eeprom;
799 short lna_gain;
800
801 if (libconf->rf.channel <= 14) {
802 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
803 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
804 } else if (libconf->rf.channel <= 64) {
805 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
806 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
807 } else if (libconf->rf.channel <= 128) {
808 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
809 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
810 } else {
811 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
812 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
813 }
814
815 rt2x00dev->lna_gain = lna_gain;
816}
817
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200818static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
819 struct ieee80211_conf *conf,
820 struct rf_channel *rf,
821 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100822{
823 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
824
825 if (rt2x00dev->default_ant.tx == 1)
826 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
827
828 if (rt2x00dev->default_ant.rx == 1) {
829 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
830 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
831 } else if (rt2x00dev->default_ant.rx == 2)
832 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
833
834 if (rf->channel > 14) {
835 /*
836 * When TX power is below 0, we should increase it by 7 to
837 * make it a positive value (Minumum value is -7).
838 * However this means that values between 0 and 7 have
839 * double meaning, and we should set a 7DBm boost flag.
840 */
841 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
842 (info->tx_power1 >= 0));
843
844 if (info->tx_power1 < 0)
845 info->tx_power1 += 7;
846
847 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A,
848 TXPOWER_A_TO_DEV(info->tx_power1));
849
850 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
851 (info->tx_power2 >= 0));
852
853 if (info->tx_power2 < 0)
854 info->tx_power2 += 7;
855
856 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A,
857 TXPOWER_A_TO_DEV(info->tx_power2));
858 } else {
859 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G,
860 TXPOWER_G_TO_DEV(info->tx_power1));
861 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G,
862 TXPOWER_G_TO_DEV(info->tx_power2));
863 }
864
865 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
866
867 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
868 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
869 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
870 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
871
872 udelay(200);
873
874 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
875 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
876 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
877 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
878
879 udelay(200);
880
881 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
882 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
883 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
884 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
885}
886
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200887static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
888 struct ieee80211_conf *conf,
889 struct rf_channel *rf,
890 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100891{
892 u8 rfcsr;
893
894 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Gertjan van Wingerde41a26172009-11-09 22:59:04 +0100895 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100896
897 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +0200898 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100899 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
900
901 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
902 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
903 TXPOWER_G_TO_DEV(info->tx_power1));
904 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
905
Helmut Schaa5a673962010-04-23 15:54:43 +0200906 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
907 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
908 TXPOWER_G_TO_DEV(info->tx_power2));
909 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
910
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100911 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
912 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
913 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
914
915 rt2800_rfcsr_write(rt2x00dev, 24,
916 rt2x00dev->calibration[conf_is_ht40(conf)]);
917
Gertjan van Wingerde71976902010-03-24 21:42:36 +0100918 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100919 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +0100920 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100921}
922
923static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
924 struct ieee80211_conf *conf,
925 struct rf_channel *rf,
926 struct channel_info *info)
927{
928 u32 reg;
929 unsigned int tx_pin;
930 u8 bbp;
931
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200932 if (rt2x00_rf(rt2x00dev, RF2020) ||
933 rt2x00_rf(rt2x00dev, RF3020) ||
934 rt2x00_rf(rt2x00dev, RF3021) ||
935 rt2x00_rf(rt2x00dev, RF3022))
936 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerdefa6f6322009-11-09 22:59:58 +0100937 else
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200938 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100939
940 /*
941 * Change BBP settings
942 */
943 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
944 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
945 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
946 rt2800_bbp_write(rt2x00dev, 86, 0);
947
948 if (rf->channel <= 14) {
949 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
950 rt2800_bbp_write(rt2x00dev, 82, 0x62);
951 rt2800_bbp_write(rt2x00dev, 75, 0x46);
952 } else {
953 rt2800_bbp_write(rt2x00dev, 82, 0x84);
954 rt2800_bbp_write(rt2x00dev, 75, 0x50);
955 }
956 } else {
957 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
958
959 if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
960 rt2800_bbp_write(rt2x00dev, 75, 0x46);
961 else
962 rt2800_bbp_write(rt2x00dev, 75, 0x50);
963 }
964
965 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +0200966 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100967 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
968 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
969 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
970
971 tx_pin = 0;
972
973 /* Turn on unused PA or LNA when not using 1T or 1R */
974 if (rt2x00dev->default_ant.tx != 1) {
975 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
976 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
977 }
978
979 /* Turn on unused PA or LNA when not using 1T or 1R */
980 if (rt2x00dev->default_ant.rx != 1) {
981 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
982 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
983 }
984
985 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
986 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
987 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
988 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
989 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
990 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
991
992 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
993
994 rt2800_bbp_read(rt2x00dev, 4, &bbp);
995 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
996 rt2800_bbp_write(rt2x00dev, 4, bbp);
997
998 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +0200999 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001000 rt2800_bbp_write(rt2x00dev, 3, bbp);
1001
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001002 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001003 if (conf_is_ht40(conf)) {
1004 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
1005 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1006 rt2800_bbp_write(rt2x00dev, 73, 0x16);
1007 } else {
1008 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1009 rt2800_bbp_write(rt2x00dev, 70, 0x08);
1010 rt2800_bbp_write(rt2x00dev, 73, 0x11);
1011 }
1012 }
1013
1014 msleep(1);
1015}
1016
1017static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
1018 const int txpower)
1019{
1020 u32 reg;
1021 u32 value = TXPOWER_G_TO_DEV(txpower);
1022 u8 r1;
1023
1024 rt2800_bbp_read(rt2x00dev, 1, &r1);
1025 rt2x00_set_field8(&reg, BBP1_TX_POWER, 0);
1026 rt2800_bbp_write(rt2x00dev, 1, r1);
1027
1028 rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
1029 rt2x00_set_field32(&reg, TX_PWR_CFG_0_1MBS, value);
1030 rt2x00_set_field32(&reg, TX_PWR_CFG_0_2MBS, value);
1031 rt2x00_set_field32(&reg, TX_PWR_CFG_0_55MBS, value);
1032 rt2x00_set_field32(&reg, TX_PWR_CFG_0_11MBS, value);
1033 rt2x00_set_field32(&reg, TX_PWR_CFG_0_6MBS, value);
1034 rt2x00_set_field32(&reg, TX_PWR_CFG_0_9MBS, value);
1035 rt2x00_set_field32(&reg, TX_PWR_CFG_0_12MBS, value);
1036 rt2x00_set_field32(&reg, TX_PWR_CFG_0_18MBS, value);
1037 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0, reg);
1038
1039 rt2800_register_read(rt2x00dev, TX_PWR_CFG_1, &reg);
1040 rt2x00_set_field32(&reg, TX_PWR_CFG_1_24MBS, value);
1041 rt2x00_set_field32(&reg, TX_PWR_CFG_1_36MBS, value);
1042 rt2x00_set_field32(&reg, TX_PWR_CFG_1_48MBS, value);
1043 rt2x00_set_field32(&reg, TX_PWR_CFG_1_54MBS, value);
1044 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS0, value);
1045 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS1, value);
1046 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS2, value);
1047 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS3, value);
1048 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1, reg);
1049
1050 rt2800_register_read(rt2x00dev, TX_PWR_CFG_2, &reg);
1051 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS4, value);
1052 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS5, value);
1053 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS6, value);
1054 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS7, value);
1055 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS8, value);
1056 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS9, value);
1057 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS10, value);
1058 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS11, value);
1059 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2, reg);
1060
1061 rt2800_register_read(rt2x00dev, TX_PWR_CFG_3, &reg);
1062 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS12, value);
1063 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS13, value);
1064 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS14, value);
1065 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS15, value);
1066 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN1, value);
1067 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN2, value);
1068 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN3, value);
1069 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN4, value);
1070 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3, reg);
1071
1072 rt2800_register_read(rt2x00dev, TX_PWR_CFG_4, &reg);
1073 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN5, value);
1074 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN6, value);
1075 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN7, value);
1076 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN8, value);
1077 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4, reg);
1078}
1079
1080static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
1081 struct rt2x00lib_conf *libconf)
1082{
1083 u32 reg;
1084
1085 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1086 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
1087 libconf->conf->short_frame_max_tx_count);
1088 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
1089 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001090 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1091}
1092
1093static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
1094 struct rt2x00lib_conf *libconf)
1095{
1096 enum dev_state state =
1097 (libconf->conf->flags & IEEE80211_CONF_PS) ?
1098 STATE_SLEEP : STATE_AWAKE;
1099 u32 reg;
1100
1101 if (state == STATE_SLEEP) {
1102 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
1103
1104 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1105 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
1106 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
1107 libconf->conf->listen_interval - 1);
1108 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
1109 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
1110
1111 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
1112 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001113 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1114 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
1115 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
1116 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
1117 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02001118
1119 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001120 }
1121}
1122
1123void rt2800_config(struct rt2x00_dev *rt2x00dev,
1124 struct rt2x00lib_conf *libconf,
1125 const unsigned int flags)
1126{
1127 /* Always recalculate LNA gain before changing configuration */
1128 rt2800_config_lna_gain(rt2x00dev, libconf);
1129
1130 if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
1131 rt2800_config_channel(rt2x00dev, libconf->conf,
1132 &libconf->rf, &libconf->channel);
1133 if (flags & IEEE80211_CONF_CHANGE_POWER)
1134 rt2800_config_txpower(rt2x00dev, libconf->conf->power_level);
1135 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
1136 rt2800_config_retry_limit(rt2x00dev, libconf);
1137 if (flags & IEEE80211_CONF_CHANGE_PS)
1138 rt2800_config_ps(rt2x00dev, libconf);
1139}
1140EXPORT_SYMBOL_GPL(rt2800_config);
1141
1142/*
1143 * Link tuning
1144 */
1145void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1146{
1147 u32 reg;
1148
1149 /*
1150 * Update FCS error count from register.
1151 */
1152 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1153 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
1154}
1155EXPORT_SYMBOL_GPL(rt2800_link_stats);
1156
1157static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
1158{
1159 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001160 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001161 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001162 rt2x00_rt(rt2x00dev, RT3090) ||
1163 rt2x00_rt(rt2x00dev, RT3390))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001164 return 0x1c + (2 * rt2x00dev->lna_gain);
1165 else
1166 return 0x2e + rt2x00dev->lna_gain;
1167 }
1168
1169 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
1170 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
1171 else
1172 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
1173}
1174
1175static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
1176 struct link_qual *qual, u8 vgc_level)
1177{
1178 if (qual->vgc_level != vgc_level) {
1179 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
1180 qual->vgc_level = vgc_level;
1181 qual->vgc_level_reg = vgc_level;
1182 }
1183}
1184
1185void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1186{
1187 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
1188}
1189EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
1190
1191void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
1192 const u32 count)
1193{
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001194 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001195 return;
1196
1197 /*
1198 * When RSSI is better then -80 increase VGC level with 0x10
1199 */
1200 rt2800_set_vgc(rt2x00dev, qual,
1201 rt2800_get_default_vgc(rt2x00dev) +
1202 ((qual->rssi > -80) * 0x10));
1203}
1204EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001205
1206/*
1207 * Initialization functions.
1208 */
1209int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
1210{
1211 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001212 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001213 unsigned int i;
1214
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001215 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1216 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1217 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1218 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1219 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1220 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
1221 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1222
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001223 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001224 /*
Thadeu Lima de Souza Cascardo235faf92009-11-12 20:04:52 +01001225 * Wait until BBP and RF are ready.
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001226 */
1227 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1228 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
1229 if (reg && reg != ~0)
1230 break;
1231 msleep(1);
1232 }
1233
1234 if (i == REGISTER_BUSY_COUNT) {
1235 ERROR(rt2x00dev, "Unstable hardware.\n");
1236 return -EBUSY;
1237 }
1238
1239 rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
1240 rt2800_register_write(rt2x00dev, PBF_SYS_CTRL,
1241 reg & ~0x00002000);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001242 } else if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev)) {
1243 /*
1244 * Reset DMA indexes
1245 */
1246 rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
1247 rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, 1);
1248 rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, 1);
1249 rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, 1);
1250 rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, 1);
1251 rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX4, 1);
1252 rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX5, 1);
1253 rt2x00_set_field32(&reg, WPDMA_RST_IDX_DRX_IDX0, 1);
1254 rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
1255
1256 rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e1f);
1257 rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e00);
1258
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001259 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001260 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001261
1262 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
1263 rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_CSR, 1);
1264 rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_BBP, 1);
1265 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
1266
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001267 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001268 rt2800_register_write(rt2x00dev, USB_DMA_CFG, 0x00000000);
Gertjan van Wingerdeac394912009-12-23 00:03:23 +01001269#if defined(CONFIG_RT2X00_LIB_USB) || defined(CONFIG_RT2X00_LIB_USB_MODULE)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001270 rt2x00usb_vendor_request_sw(rt2x00dev, USB_DEVICE_MODE, 0,
1271 USB_MODE_RESET, REGISTER_TIMEOUT);
1272#endif
1273 }
1274
1275 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1276
1277 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
1278 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
1279 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
1280 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
1281 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
1282 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
1283
1284 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
1285 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
1286 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
1287 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
1288 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
1289 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
1290
1291 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
1292 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1293
1294 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1295
1296 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1297 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 0);
1298 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
1299 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
1300 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
1301 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1302 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
1303 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1304
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001305 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
1306
1307 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1308 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
1309 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
1310 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
1311
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001312 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001313 rt2x00_rt(rt2x00dev, RT3090) ||
1314 rt2x00_rt(rt2x00dev, RT3390)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001315 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1316 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001317 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001318 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
1319 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001320 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1321 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
1322 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1323 0x0000002c);
1324 else
1325 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1326 0x0000000f);
1327 } else {
1328 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1329 }
1330 rt2800_register_write(rt2x00dev, TX_SW_CFG2, reg);
1331 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001332 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001333
1334 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
1335 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1336 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
1337 } else {
1338 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1339 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1340 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001341 } else {
1342 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
1343 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1344 }
1345
1346 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
1347 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
1348 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
1349 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
1350 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
1351 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
1352 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
1353 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
1354 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
1355 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
1356
1357 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
1358 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001359 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001360 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
1361 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
1362
1363 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
1364 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001365 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01001366 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001367 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001368 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
1369 else
1370 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
1371 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
1372 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
1373 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
1374
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001375 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1376 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
1377 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
1378 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
1379 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
1380 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
1381 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
1382 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
1383 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1384
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001385 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
1386
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001387 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1388 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
1389 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
1390 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
1391 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
1392 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
1393 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
1394 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1395
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001396 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1397 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001398 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001399 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
1400 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001401 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001402 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
1403 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
1404 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1405
1406 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001407 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001408 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
1409 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
1410 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1411 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1412 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001413 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001414 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001415 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1416 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001417 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
1418
1419 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001420 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001421 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
1422 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
1423 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1424 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1425 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001426 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001427 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001428 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1429 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001430 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1431
1432 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1433 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
1434 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
1435 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
1436 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1437 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1438 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1439 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1440 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1441 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001442 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001443 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1444
1445 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1446 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001447 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL,
1448 !rt2x00_is_usb(rt2x00dev));
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001449 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
1450 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1451 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1452 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1453 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1454 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1455 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001456 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001457 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1458
1459 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1460 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
1461 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
1462 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
1463 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1464 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1465 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1466 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1467 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1468 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001469 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001470 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1471
1472 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1473 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
1474 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
1475 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
1476 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1477 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1478 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1479 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1480 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1481 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001482 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001483 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1484
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001485 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001486 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
1487
1488 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1489 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1490 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1491 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1492 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1493 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
1494 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
1495 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
1496 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
1497 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
1498 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1499 }
1500
1501 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, 0x0000583f);
1502 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
1503
1504 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
1505 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
1506 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
1507 IEEE80211_MAX_RTS_THRESHOLD);
1508 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
1509 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
1510
1511 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001512
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001513 /*
1514 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
1515 * time should be set to 16. However, the original Ralink driver uses
1516 * 16 for both and indeed using a value of 10 for CCK SIFS results in
1517 * connection problems with 11g + CTS protection. Hence, use the same
1518 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
1519 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001520 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001521 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
1522 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001523 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
1524 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
1525 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
1526 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1527
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001528 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
1529
1530 /*
1531 * ASIC will keep garbage value after boot, clear encryption keys.
1532 */
1533 for (i = 0; i < 4; i++)
1534 rt2800_register_write(rt2x00dev,
1535 SHARED_KEY_MODE_ENTRY(i), 0);
1536
1537 for (i = 0; i < 256; i++) {
1538 u32 wcid[2] = { 0xffffffff, 0x00ffffff };
1539 rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
1540 wcid, sizeof(wcid));
1541
1542 rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
1543 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
1544 }
1545
1546 /*
1547 * Clear all beacons
1548 * For the Beacon base registers we only need to clear
1549 * the first byte since that byte contains the VALID and OWNER
1550 * bits which (when set to 0) will invalidate the entire beacon.
1551 */
1552 rt2800_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
1553 rt2800_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
1554 rt2800_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
1555 rt2800_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
1556 rt2800_register_write(rt2x00dev, HW_BEACON_BASE4, 0);
1557 rt2800_register_write(rt2x00dev, HW_BEACON_BASE5, 0);
1558 rt2800_register_write(rt2x00dev, HW_BEACON_BASE6, 0);
1559 rt2800_register_write(rt2x00dev, HW_BEACON_BASE7, 0);
1560
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001561 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001562 rt2800_register_read(rt2x00dev, USB_CYC_CFG, &reg);
1563 rt2x00_set_field32(&reg, USB_CYC_CFG_CLOCK_CYCLE, 30);
1564 rt2800_register_write(rt2x00dev, USB_CYC_CFG, reg);
1565 }
1566
1567 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
1568 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
1569 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
1570 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
1571 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
1572 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
1573 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
1574 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
1575 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
1576 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
1577
1578 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
1579 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
1580 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
1581 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
1582 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
1583 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
1584 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
1585 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
1586 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
1587 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
1588
1589 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
1590 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
1591 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
1592 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
1593 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
1594 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
1595 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
1596 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
1597 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
1598 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
1599
1600 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
1601 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
1602 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
1603 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
1604 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
1605 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
1606
1607 /*
1608 * We must clear the error counters.
1609 * These registers are cleared on read,
1610 * so we may pass a useless variable to store the value.
1611 */
1612 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1613 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
1614 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
1615 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
1616 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
1617 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
1618
1619 return 0;
1620}
1621EXPORT_SYMBOL_GPL(rt2800_init_registers);
1622
1623static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
1624{
1625 unsigned int i;
1626 u32 reg;
1627
1628 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1629 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
1630 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
1631 return 0;
1632
1633 udelay(REGISTER_BUSY_DELAY);
1634 }
1635
1636 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
1637 return -EACCES;
1638}
1639
1640static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
1641{
1642 unsigned int i;
1643 u8 value;
1644
1645 /*
1646 * BBP was enabled after firmware was loaded,
1647 * but we need to reactivate it now.
1648 */
1649 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
1650 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
1651 msleep(1);
1652
1653 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1654 rt2800_bbp_read(rt2x00dev, 0, &value);
1655 if ((value != 0xff) && (value != 0x00))
1656 return 0;
1657 udelay(REGISTER_BUSY_DELAY);
1658 }
1659
1660 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
1661 return -EACCES;
1662}
1663
1664int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
1665{
1666 unsigned int i;
1667 u16 eeprom;
1668 u8 reg_id;
1669 u8 value;
1670
1671 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
1672 rt2800_wait_bbp_ready(rt2x00dev)))
1673 return -EACCES;
1674
Helmut Schaabaff8002010-04-28 09:58:59 +02001675 if (rt2800_is_305x_soc(rt2x00dev))
1676 rt2800_bbp_write(rt2x00dev, 31, 0x08);
1677
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001678 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
1679 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001680
1681 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
1682 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1683 rt2800_bbp_write(rt2x00dev, 73, 0x12);
1684 } else {
1685 rt2800_bbp_write(rt2x00dev, 69, 0x12);
1686 rt2800_bbp_write(rt2x00dev, 73, 0x10);
1687 }
1688
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001689 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001690
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001691 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001692 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001693 rt2x00_rt(rt2x00dev, RT3090) ||
1694 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001695 rt2800_bbp_write(rt2x00dev, 79, 0x13);
1696 rt2800_bbp_write(rt2x00dev, 80, 0x05);
1697 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Helmut Schaabaff8002010-04-28 09:58:59 +02001698 } else if (rt2800_is_305x_soc(rt2x00dev)) {
1699 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
1700 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001701 } else {
1702 rt2800_bbp_write(rt2x00dev, 81, 0x37);
1703 }
1704
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001705 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1706 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001707
1708 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D) ||
1709 rt2x00_rt_rev(rt2x00dev, RT2870, REV_RT2870D))
1710 rt2800_bbp_write(rt2x00dev, 84, 0x19);
1711 else
1712 rt2800_bbp_write(rt2x00dev, 84, 0x99);
1713
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001714 rt2800_bbp_write(rt2x00dev, 86, 0x00);
1715 rt2800_bbp_write(rt2x00dev, 91, 0x04);
1716 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001717
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001718 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001719 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001720 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02001721 rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
1722 rt2800_is_305x_soc(rt2x00dev))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001723 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
1724 else
1725 rt2800_bbp_write(rt2x00dev, 103, 0x00);
1726
Helmut Schaabaff8002010-04-28 09:58:59 +02001727 if (rt2800_is_305x_soc(rt2x00dev))
1728 rt2800_bbp_write(rt2x00dev, 105, 0x01);
1729 else
1730 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001731 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001732
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001733 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001734 rt2x00_rt(rt2x00dev, RT3090) ||
1735 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001736 rt2800_bbp_read(rt2x00dev, 138, &value);
1737
1738 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
1739 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
1740 value |= 0x20;
1741 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
1742 value &= ~0x02;
1743
1744 rt2800_bbp_write(rt2x00dev, 138, value);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001745 }
1746
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001747
1748 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
1749 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
1750
1751 if (eeprom != 0xffff && eeprom != 0x0000) {
1752 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
1753 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
1754 rt2800_bbp_write(rt2x00dev, reg_id, value);
1755 }
1756 }
1757
1758 return 0;
1759}
1760EXPORT_SYMBOL_GPL(rt2800_init_bbp);
1761
1762static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
1763 bool bw40, u8 rfcsr24, u8 filter_target)
1764{
1765 unsigned int i;
1766 u8 bbp;
1767 u8 rfcsr;
1768 u8 passband;
1769 u8 stopband;
1770 u8 overtuned = 0;
1771
1772 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1773
1774 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1775 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
1776 rt2800_bbp_write(rt2x00dev, 4, bbp);
1777
1778 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
1779 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
1780 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
1781
1782 /*
1783 * Set power & frequency of passband test tone
1784 */
1785 rt2800_bbp_write(rt2x00dev, 24, 0);
1786
1787 for (i = 0; i < 100; i++) {
1788 rt2800_bbp_write(rt2x00dev, 25, 0x90);
1789 msleep(1);
1790
1791 rt2800_bbp_read(rt2x00dev, 55, &passband);
1792 if (passband)
1793 break;
1794 }
1795
1796 /*
1797 * Set power & frequency of stopband test tone
1798 */
1799 rt2800_bbp_write(rt2x00dev, 24, 0x06);
1800
1801 for (i = 0; i < 100; i++) {
1802 rt2800_bbp_write(rt2x00dev, 25, 0x90);
1803 msleep(1);
1804
1805 rt2800_bbp_read(rt2x00dev, 55, &stopband);
1806
1807 if ((passband - stopband) <= filter_target) {
1808 rfcsr24++;
1809 overtuned += ((passband - stopband) == filter_target);
1810 } else
1811 break;
1812
1813 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1814 }
1815
1816 rfcsr24 -= !!overtuned;
1817
1818 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1819 return rfcsr24;
1820}
1821
1822int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
1823{
1824 u8 rfcsr;
1825 u8 bbp;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001826 u32 reg;
1827 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001828
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001829 if (!rt2x00_rt(rt2x00dev, RT3070) &&
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001830 !rt2x00_rt(rt2x00dev, RT3071) &&
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001831 !rt2x00_rt(rt2x00dev, RT3090) &&
Helmut Schaa23812382010-04-26 13:48:45 +02001832 !rt2x00_rt(rt2x00dev, RT3390) &&
Helmut Schaabaff8002010-04-28 09:58:59 +02001833 !rt2800_is_305x_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001834 return 0;
1835
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001836 /*
1837 * Init RF calibration.
1838 */
1839 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1840 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1841 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1842 msleep(1);
1843 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1844 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1845
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001846 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001847 rt2x00_rt(rt2x00dev, RT3071) ||
1848 rt2x00_rt(rt2x00dev, RT3090)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001849 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
1850 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
1851 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
1852 rt2800_rfcsr_write(rt2x00dev, 7, 0x70);
1853 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001854 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001855 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
1856 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
1857 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
1858 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
1859 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
1860 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
1861 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
1862 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
1863 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
1864 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
1865 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
1866 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001867 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001868 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
1869 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
1870 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
1871 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
1872 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001873 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001874 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
1875 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
1876 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
1877 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
1878 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
1879 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001880 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001881 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
1882 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001883 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001884 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
1885 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
1886 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
1887 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
1888 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
1889 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
1890 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001891 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001892 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001893 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001894 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
1895 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
1896 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
1897 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
1898 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
1899 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
1900 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Helmut Schaabaff8002010-04-28 09:58:59 +02001901 } else if (rt2800_is_305x_soc(rt2x00dev)) {
Helmut Schaa23812382010-04-26 13:48:45 +02001902 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
1903 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
1904 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
1905 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
1906 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
1907 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
1908 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
1909 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
1910 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
1911 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
1912 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
1913 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
1914 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
1915 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
1916 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
1917 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
1918 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
1919 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
1920 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
1921 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
1922 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
1923 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
1924 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
1925 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
1926 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
1927 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
1928 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
1929 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
1930 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
1931 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
Helmut Schaabaff8002010-04-28 09:58:59 +02001932 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
1933 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
1934 return 0;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001935 }
1936
1937 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
1938 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
1939 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
1940 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
1941 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001942 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
1943 rt2x00_rt(rt2x00dev, RT3090)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001944 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
1945 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
1946 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1947
1948 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
1949
1950 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
1951 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001952 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
1953 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001954 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1955 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
1956 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
1957 else
1958 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
1959 }
1960 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001961 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
1962 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
1963 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
1964 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001965 }
1966
1967 /*
1968 * Set RX Filter calibration for 20MHz and 40MHz
1969 */
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001970 if (rt2x00_rt(rt2x00dev, RT3070)) {
1971 rt2x00dev->calibration[0] =
1972 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
1973 rt2x00dev->calibration[1] =
1974 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001975 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001976 rt2x00_rt(rt2x00dev, RT3090) ||
1977 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001978 rt2x00dev->calibration[0] =
1979 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
1980 rt2x00dev->calibration[1] =
1981 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001982 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001983
1984 /*
1985 * Set back to initial state
1986 */
1987 rt2800_bbp_write(rt2x00dev, 24, 0);
1988
1989 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
1990 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
1991 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
1992
1993 /*
1994 * set BBP back to BW20
1995 */
1996 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1997 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
1998 rt2800_bbp_write(rt2x00dev, 4, bbp);
1999
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002000 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002001 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002002 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2003 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002004 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
2005
2006 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
2007 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
2008 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
2009
2010 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
2011 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002012 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002013 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2014 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002015 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2016 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
2017 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
2018 }
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002019 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &eeprom);
2020 if (rt2x00_get_field16(eeprom, EEPROM_TXMIXER_GAIN_BG_VAL) >= 1)
2021 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
2022 rt2x00_get_field16(eeprom,
2023 EEPROM_TXMIXER_GAIN_BG_VAL));
2024 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
2025
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002026 if (rt2x00_rt(rt2x00dev, RT3090)) {
2027 rt2800_bbp_read(rt2x00dev, 138, &bbp);
2028
2029 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2030 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
2031 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
2032 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
2033 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
2034
2035 rt2800_bbp_write(rt2x00dev, 138, bbp);
2036 }
2037
2038 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002039 rt2x00_rt(rt2x00dev, RT3090) ||
2040 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002041 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2042 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2043 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2044 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2045 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2046 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2047 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2048
2049 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
2050 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
2051 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
2052
2053 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
2054 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
2055 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
2056
2057 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
2058 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
2059 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
2060 }
2061
2062 if (rt2x00_rt(rt2x00dev, RT3070) || rt2x00_rt(rt2x00dev, RT3071)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002063 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002064 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
2065 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002066 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
2067 else
2068 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
2069 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
2070 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
2071 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
2072 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
2073 }
2074
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002075 return 0;
2076}
2077EXPORT_SYMBOL_GPL(rt2800_init_rfcsr);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002078
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002079int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
2080{
2081 u32 reg;
2082
2083 rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
2084
2085 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
2086}
2087EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
2088
2089static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
2090{
2091 u32 reg;
2092
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002093 mutex_lock(&rt2x00dev->csr_mutex);
2094
2095 rt2800_register_read_lock(rt2x00dev, EFUSE_CTRL, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002096 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
2097 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
2098 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002099 rt2800_register_write_lock(rt2x00dev, EFUSE_CTRL, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002100
2101 /* Wait until the EEPROM has been loaded */
2102 rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
2103
2104 /* Apparently the data is read from end to start */
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002105 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA3,
2106 (u32 *)&rt2x00dev->eeprom[i]);
2107 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA2,
2108 (u32 *)&rt2x00dev->eeprom[i + 2]);
2109 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA1,
2110 (u32 *)&rt2x00dev->eeprom[i + 4]);
2111 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA0,
2112 (u32 *)&rt2x00dev->eeprom[i + 6]);
2113
2114 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002115}
2116
2117void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
2118{
2119 unsigned int i;
2120
2121 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
2122 rt2800_efuse_read(rt2x00dev, i);
2123}
2124EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
2125
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002126int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
2127{
2128 u16 word;
2129 u8 *mac;
2130 u8 default_lna_gain;
2131
2132 /*
2133 * Start validation of the data that has been read.
2134 */
2135 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
2136 if (!is_valid_ether_addr(mac)) {
2137 random_ether_addr(mac);
2138 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
2139 }
2140
2141 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
2142 if (word == 0xffff) {
2143 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2144 rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
2145 rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
2146 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2147 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002148 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
2149 rt2x00_rt(rt2x00dev, RT2870) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02002150 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002151 /*
2152 * There is a max of 2 RX streams for RT28x0 series
2153 */
2154 if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
2155 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2156 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2157 }
2158
2159 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
2160 if (word == 0xffff) {
2161 rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
2162 rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
2163 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
2164 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
2165 rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
2166 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
2167 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
2168 rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
2169 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
2170 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
2171 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
2172 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
2173 }
2174
2175 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
2176 if ((word & 0x00ff) == 0x00ff) {
2177 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
2178 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
2179 LED_MODE_TXRX_ACTIVITY);
2180 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
2181 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2182 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
2183 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
2184 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
2185 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
2186 }
2187
2188 /*
2189 * During the LNA validation we are going to use
2190 * lna0 as correct value. Note that EEPROM_LNA
2191 * is never validated.
2192 */
2193 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
2194 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
2195
2196 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
2197 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
2198 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
2199 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
2200 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
2201 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
2202
2203 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
2204 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
2205 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
2206 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
2207 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
2208 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
2209 default_lna_gain);
2210 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
2211
2212 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
2213 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
2214 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
2215 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
2216 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
2217 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
2218
2219 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
2220 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
2221 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
2222 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
2223 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
2224 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
2225 default_lna_gain);
2226 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
2227
2228 return 0;
2229}
2230EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
2231
2232int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
2233{
2234 u32 reg;
2235 u16 value;
2236 u16 eeprom;
2237
2238 /*
2239 * Read EEPROM word for configuration.
2240 */
2241 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2242
2243 /*
2244 * Identify RF chipset.
2245 */
2246 value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
2247 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
2248
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002249 rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
2250 value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
Gertjan van Wingerde714fa6632010-02-13 20:55:48 +01002251
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002252 if (!rt2x00_rt(rt2x00dev, RT2860) &&
2253 !rt2x00_rt(rt2x00dev, RT2870) &&
2254 !rt2x00_rt(rt2x00dev, RT2872) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002255 !rt2x00_rt(rt2x00dev, RT2883) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002256 !rt2x00_rt(rt2x00dev, RT3070) &&
2257 !rt2x00_rt(rt2x00dev, RT3071) &&
2258 !rt2x00_rt(rt2x00dev, RT3090) &&
2259 !rt2x00_rt(rt2x00dev, RT3390) &&
2260 !rt2x00_rt(rt2x00dev, RT3572)) {
2261 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
2262 return -ENODEV;
2263 }
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002264
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002265 if (!rt2x00_rf(rt2x00dev, RF2820) &&
2266 !rt2x00_rf(rt2x00dev, RF2850) &&
2267 !rt2x00_rf(rt2x00dev, RF2720) &&
2268 !rt2x00_rf(rt2x00dev, RF2750) &&
2269 !rt2x00_rf(rt2x00dev, RF3020) &&
2270 !rt2x00_rf(rt2x00dev, RF2020) &&
2271 !rt2x00_rf(rt2x00dev, RF3021) &&
Gertjan van Wingerde6c0fe262009-12-30 11:36:31 +01002272 !rt2x00_rf(rt2x00dev, RF3022) &&
2273 !rt2x00_rf(rt2x00dev, RF3052)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002274 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
2275 return -ENODEV;
2276 }
2277
2278 /*
2279 * Identify default antenna configuration.
2280 */
2281 rt2x00dev->default_ant.tx =
2282 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH);
2283 rt2x00dev->default_ant.rx =
2284 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH);
2285
2286 /*
2287 * Read frequency offset and RF programming sequence.
2288 */
2289 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
2290 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
2291
2292 /*
2293 * Read external LNA informations.
2294 */
2295 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2296
2297 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
2298 __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
2299 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
2300 __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
2301
2302 /*
2303 * Detect if this device has an hardware controlled radio.
2304 */
2305 if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
2306 __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
2307
2308 /*
2309 * Store led settings, for correct led behaviour.
2310 */
2311#ifdef CONFIG_RT2X00_LIB_LEDS
2312 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
2313 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
2314 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
2315
2316 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &rt2x00dev->led_mcu_reg);
2317#endif /* CONFIG_RT2X00_LIB_LEDS */
2318
2319 return 0;
2320}
2321EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
2322
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002323/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002324 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002325 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
2326 */
2327static const struct rf_channel rf_vals[] = {
2328 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
2329 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
2330 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
2331 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
2332 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
2333 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
2334 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
2335 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
2336 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
2337 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
2338 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
2339 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
2340 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
2341 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
2342
2343 /* 802.11 UNI / HyperLan 2 */
2344 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
2345 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
2346 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
2347 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
2348 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
2349 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
2350 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
2351 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
2352 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
2353 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
2354 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
2355 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
2356
2357 /* 802.11 HyperLan 2 */
2358 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
2359 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
2360 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
2361 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
2362 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
2363 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
2364 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
2365 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
2366 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
2367 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
2368 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
2369 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
2370 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
2371 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
2372 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
2373 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
2374
2375 /* 802.11 UNII */
2376 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
2377 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
2378 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
2379 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
2380 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
2381 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
2382 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
2383 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
2384 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
2385 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
2386 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
2387
2388 /* 802.11 Japan */
2389 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
2390 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
2391 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
2392 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
2393 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
2394 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
2395 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
2396};
2397
2398/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002399 * RF value list for rt3xxx
2400 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002401 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02002402static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002403 {1, 241, 2, 2 },
2404 {2, 241, 2, 7 },
2405 {3, 242, 2, 2 },
2406 {4, 242, 2, 7 },
2407 {5, 243, 2, 2 },
2408 {6, 243, 2, 7 },
2409 {7, 244, 2, 2 },
2410 {8, 244, 2, 7 },
2411 {9, 245, 2, 2 },
2412 {10, 245, 2, 7 },
2413 {11, 246, 2, 2 },
2414 {12, 246, 2, 7 },
2415 {13, 247, 2, 2 },
2416 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02002417
2418 /* 802.11 UNI / HyperLan 2 */
2419 {36, 0x56, 0, 4},
2420 {38, 0x56, 0, 6},
2421 {40, 0x56, 0, 8},
2422 {44, 0x57, 0, 0},
2423 {46, 0x57, 0, 2},
2424 {48, 0x57, 0, 4},
2425 {52, 0x57, 0, 8},
2426 {54, 0x57, 0, 10},
2427 {56, 0x58, 0, 0},
2428 {60, 0x58, 0, 4},
2429 {62, 0x58, 0, 6},
2430 {64, 0x58, 0, 8},
2431
2432 /* 802.11 HyperLan 2 */
2433 {100, 0x5b, 0, 8},
2434 {102, 0x5b, 0, 10},
2435 {104, 0x5c, 0, 0},
2436 {108, 0x5c, 0, 4},
2437 {110, 0x5c, 0, 6},
2438 {112, 0x5c, 0, 8},
2439 {116, 0x5d, 0, 0},
2440 {118, 0x5d, 0, 2},
2441 {120, 0x5d, 0, 4},
2442 {124, 0x5d, 0, 8},
2443 {126, 0x5d, 0, 10},
2444 {128, 0x5e, 0, 0},
2445 {132, 0x5e, 0, 4},
2446 {134, 0x5e, 0, 6},
2447 {136, 0x5e, 0, 8},
2448 {140, 0x5f, 0, 0},
2449
2450 /* 802.11 UNII */
2451 {149, 0x5f, 0, 9},
2452 {151, 0x5f, 0, 11},
2453 {153, 0x60, 0, 1},
2454 {157, 0x60, 0, 5},
2455 {159, 0x60, 0, 7},
2456 {161, 0x60, 0, 9},
2457 {165, 0x61, 0, 1},
2458 {167, 0x61, 0, 3},
2459 {169, 0x61, 0, 5},
2460 {171, 0x61, 0, 7},
2461 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002462};
2463
2464int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
2465{
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002466 struct hw_mode_spec *spec = &rt2x00dev->spec;
2467 struct channel_info *info;
2468 char *tx_power1;
2469 char *tx_power2;
2470 unsigned int i;
2471 u16 eeprom;
2472
2473 /*
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002474 * Disable powersaving as default on PCI devices.
2475 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01002476 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002477 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
2478
2479 /*
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002480 * Initialize all hw fields.
2481 */
2482 rt2x00dev->hw->flags =
2483 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2484 IEEE80211_HW_SIGNAL_DBM |
2485 IEEE80211_HW_SUPPORTS_PS |
2486 IEEE80211_HW_PS_NULLFUNC_STACK;
2487
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002488 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
2489 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
2490 rt2x00_eeprom_addr(rt2x00dev,
2491 EEPROM_MAC_ADDR_0));
2492
2493 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2494
2495 /*
2496 * Initialize hw_mode information.
2497 */
2498 spec->supported_bands = SUPPORT_BAND_2GHZ;
2499 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
2500
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002501 if (rt2x00_rf(rt2x00dev, RF2820) ||
Ivo van Doorn55f93212010-05-06 14:45:46 +02002502 rt2x00_rf(rt2x00dev, RF2720)) {
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002503 spec->num_channels = 14;
2504 spec->channels = rf_vals;
Ivo van Doorn55f93212010-05-06 14:45:46 +02002505 } else if (rt2x00_rf(rt2x00dev, RF2850) ||
2506 rt2x00_rf(rt2x00dev, RF2750)) {
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002507 spec->supported_bands |= SUPPORT_BAND_5GHZ;
2508 spec->num_channels = ARRAY_SIZE(rf_vals);
2509 spec->channels = rf_vals;
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002510 } else if (rt2x00_rf(rt2x00dev, RF3020) ||
2511 rt2x00_rf(rt2x00dev, RF2020) ||
2512 rt2x00_rf(rt2x00dev, RF3021) ||
2513 rt2x00_rf(rt2x00dev, RF3022)) {
Ivo van Doorn55f93212010-05-06 14:45:46 +02002514 spec->num_channels = 14;
2515 spec->channels = rf_vals_3x;
2516 } else if (rt2x00_rf(rt2x00dev, RF3052)) {
2517 spec->supported_bands |= SUPPORT_BAND_5GHZ;
2518 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
2519 spec->channels = rf_vals_3x;
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002520 }
2521
2522 /*
2523 * Initialize HT information.
2524 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002525 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01002526 spec->ht.ht_supported = true;
2527 else
2528 spec->ht.ht_supported = false;
2529
Helmut Schaa2caaa5d2010-04-23 15:05:29 +02002530 /*
2531 * Don't set IEEE80211_HT_CAP_SUP_WIDTH_20_40 for now as it causes
2532 * reception problems with HT40 capable 11n APs
2533 */
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002534 spec->ht.cap =
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002535 IEEE80211_HT_CAP_GRN_FLD |
2536 IEEE80211_HT_CAP_SGI_20 |
2537 IEEE80211_HT_CAP_SGI_40 |
2538 IEEE80211_HT_CAP_TX_STBC |
Johannes Berg9a418af2009-12-17 13:55:48 +01002539 IEEE80211_HT_CAP_RX_STBC;
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002540 spec->ht.ampdu_factor = 3;
2541 spec->ht.ampdu_density = 4;
2542 spec->ht.mcs.tx_params =
2543 IEEE80211_HT_MCS_TX_DEFINED |
2544 IEEE80211_HT_MCS_TX_RX_DIFF |
2545 ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
2546 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
2547
2548 switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
2549 case 3:
2550 spec->ht.mcs.rx_mask[2] = 0xff;
2551 case 2:
2552 spec->ht.mcs.rx_mask[1] = 0xff;
2553 case 1:
2554 spec->ht.mcs.rx_mask[0] = 0xff;
2555 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
2556 break;
2557 }
2558
2559 /*
2560 * Create channel information array
2561 */
2562 info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
2563 if (!info)
2564 return -ENOMEM;
2565
2566 spec->channels_info = info;
2567
2568 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
2569 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
2570
2571 for (i = 0; i < 14; i++) {
2572 info[i].tx_power1 = TXPOWER_G_FROM_DEV(tx_power1[i]);
2573 info[i].tx_power2 = TXPOWER_G_FROM_DEV(tx_power2[i]);
2574 }
2575
2576 if (spec->num_channels > 14) {
2577 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
2578 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
2579
2580 for (i = 14; i < spec->num_channels; i++) {
2581 info[i].tx_power1 = TXPOWER_A_FROM_DEV(tx_power1[i]);
2582 info[i].tx_power2 = TXPOWER_A_FROM_DEV(tx_power2[i]);
2583 }
2584 }
2585
2586 return 0;
2587}
2588EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
2589
2590/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002591 * IEEE80211 stack callback functions.
2592 */
2593static void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx,
2594 u32 *iv32, u16 *iv16)
2595{
2596 struct rt2x00_dev *rt2x00dev = hw->priv;
2597 struct mac_iveiv_entry iveiv_entry;
2598 u32 offset;
2599
2600 offset = MAC_IVEIV_ENTRY(hw_key_idx);
2601 rt2800_register_multiread(rt2x00dev, offset,
2602 &iveiv_entry, sizeof(iveiv_entry));
2603
Julia Lawall855da5e2009-12-13 17:07:45 +01002604 memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
2605 memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002606}
2607
2608static int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2609{
2610 struct rt2x00_dev *rt2x00dev = hw->priv;
2611 u32 reg;
2612 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
2613
2614 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
2615 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
2616 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
2617
2618 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
2619 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
2620 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
2621
2622 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
2623 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
2624 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
2625
2626 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
2627 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
2628 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
2629
2630 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
2631 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
2632 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
2633
2634 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
2635 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
2636 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
2637
2638 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
2639 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
2640 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
2641
2642 return 0;
2643}
2644
2645static int rt2800_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
2646 const struct ieee80211_tx_queue_params *params)
2647{
2648 struct rt2x00_dev *rt2x00dev = hw->priv;
2649 struct data_queue *queue;
2650 struct rt2x00_field32 field;
2651 int retval;
2652 u32 reg;
2653 u32 offset;
2654
2655 /*
2656 * First pass the configuration through rt2x00lib, that will
2657 * update the queue settings and validate the input. After that
2658 * we are free to update the registers based on the value
2659 * in the queue parameter.
2660 */
2661 retval = rt2x00mac_conf_tx(hw, queue_idx, params);
2662 if (retval)
2663 return retval;
2664
2665 /*
2666 * We only need to perform additional register initialization
2667 * for WMM queues/
2668 */
2669 if (queue_idx >= 4)
2670 return 0;
2671
2672 queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
2673
2674 /* Update WMM TXOP register */
2675 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
2676 field.bit_offset = (queue_idx & 1) * 16;
2677 field.bit_mask = 0xffff << field.bit_offset;
2678
2679 rt2800_register_read(rt2x00dev, offset, &reg);
2680 rt2x00_set_field32(&reg, field, queue->txop);
2681 rt2800_register_write(rt2x00dev, offset, reg);
2682
2683 /* Update WMM registers */
2684 field.bit_offset = queue_idx * 4;
2685 field.bit_mask = 0xf << field.bit_offset;
2686
2687 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
2688 rt2x00_set_field32(&reg, field, queue->aifs);
2689 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
2690
2691 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
2692 rt2x00_set_field32(&reg, field, queue->cw_min);
2693 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
2694
2695 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
2696 rt2x00_set_field32(&reg, field, queue->cw_max);
2697 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
2698
2699 /* Update EDCA registers */
2700 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
2701
2702 rt2800_register_read(rt2x00dev, offset, &reg);
2703 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
2704 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
2705 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
2706 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
2707 rt2800_register_write(rt2x00dev, offset, reg);
2708
2709 return 0;
2710}
2711
2712static u64 rt2800_get_tsf(struct ieee80211_hw *hw)
2713{
2714 struct rt2x00_dev *rt2x00dev = hw->priv;
2715 u64 tsf;
2716 u32 reg;
2717
2718 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
2719 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
2720 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
2721 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
2722
2723 return tsf;
2724}
2725
2726const struct ieee80211_ops rt2800_mac80211_ops = {
2727 .tx = rt2x00mac_tx,
2728 .start = rt2x00mac_start,
2729 .stop = rt2x00mac_stop,
2730 .add_interface = rt2x00mac_add_interface,
2731 .remove_interface = rt2x00mac_remove_interface,
2732 .config = rt2x00mac_config,
2733 .configure_filter = rt2x00mac_configure_filter,
2734 .set_tim = rt2x00mac_set_tim,
2735 .set_key = rt2x00mac_set_key,
2736 .get_stats = rt2x00mac_get_stats,
2737 .get_tkip_seq = rt2800_get_tkip_seq,
2738 .set_rts_threshold = rt2800_set_rts_threshold,
2739 .bss_info_changed = rt2x00mac_bss_info_changed,
2740 .conf_tx = rt2800_conf_tx,
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002741 .get_tsf = rt2800_get_tsf,
2742 .rfkill_poll = rt2x00mac_rfkill_poll,
2743};
2744EXPORT_SYMBOL_GPL(rt2800_mac80211_ops);