blob: bebbda5ffaab54b2e22c7b66e010bc4ca3e8e01d [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"
41#include "rt2800lib.h"
42#include "rt2800.h"
43
44MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
45MODULE_DESCRIPTION("rt2800 library");
46MODULE_LICENSE("GPL");
47
48/*
49 * Register access.
50 * All access to the CSR registers will go through the methods
51 * rt2800_register_read and rt2800_register_write.
52 * BBP and RF register require indirect register access,
53 * and use the CSR registers BBPCSR and RFCSR to achieve this.
54 * These indirect registers work with busy bits,
55 * and we will try maximal REGISTER_BUSY_COUNT times to access
56 * the register while taking a REGISTER_BUSY_DELAY us delay
57 * between each attampt. When the busy bit is still set at that time,
58 * the access attempt is considered to have failed,
59 * and we will print an error.
60 * The _lock versions must be used if you already hold the csr_mutex
61 */
62#define WAIT_FOR_BBP(__dev, __reg) \
63 rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
64#define WAIT_FOR_RFCSR(__dev, __reg) \
65 rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
66#define WAIT_FOR_RF(__dev, __reg) \
67 rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
68#define WAIT_FOR_MCU(__dev, __reg) \
69 rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
70 H2M_MAILBOX_CSR_OWNER, (__reg))
71
Helmut Schaabaff8002010-04-28 09:58:59 +020072static inline bool rt2800_is_305x_soc(struct rt2x00_dev *rt2x00dev)
73{
74 /* check for rt2872 on SoC */
75 if (!rt2x00_is_soc(rt2x00dev) ||
76 !rt2x00_rt(rt2x00dev, RT2872))
77 return false;
78
79 /* we know for sure that these rf chipsets are used on rt305x boards */
80 if (rt2x00_rf(rt2x00dev, RF3020) ||
81 rt2x00_rf(rt2x00dev, RF3021) ||
82 rt2x00_rf(rt2x00dev, RF3022))
83 return true;
84
85 NOTICE(rt2x00dev, "Unknown RF chipset on rt305x\n");
86 return false;
87}
88
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010089static void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
90 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010091{
92 u32 reg;
93
94 mutex_lock(&rt2x00dev->csr_mutex);
95
96 /*
97 * Wait until the BBP becomes available, afterwards we
98 * can safely write the new data into the register.
99 */
100 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
101 reg = 0;
102 rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
103 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
104 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
105 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100106 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100107 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
108
109 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
110 }
111
112 mutex_unlock(&rt2x00dev->csr_mutex);
113}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100114
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100115static void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
116 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100117{
118 u32 reg;
119
120 mutex_lock(&rt2x00dev->csr_mutex);
121
122 /*
123 * Wait until the BBP becomes available, afterwards we
124 * can safely write the read request into the register.
125 * After the data has been written, we wait until hardware
126 * returns the correct value, if at any time the register
127 * doesn't become available in time, reg will be 0xffffffff
128 * which means we return 0xff to the caller.
129 */
130 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
131 reg = 0;
132 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
133 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
134 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100135 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100136 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
137
138 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
139
140 WAIT_FOR_BBP(rt2x00dev, &reg);
141 }
142
143 *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
144
145 mutex_unlock(&rt2x00dev->csr_mutex);
146}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100147
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100148static void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
149 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100150{
151 u32 reg;
152
153 mutex_lock(&rt2x00dev->csr_mutex);
154
155 /*
156 * Wait until the RFCSR becomes available, afterwards we
157 * can safely write the new data into the register.
158 */
159 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
160 reg = 0;
161 rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
162 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
163 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
164 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
165
166 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
167 }
168
169 mutex_unlock(&rt2x00dev->csr_mutex);
170}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100171
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100172static void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
173 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100174{
175 u32 reg;
176
177 mutex_lock(&rt2x00dev->csr_mutex);
178
179 /*
180 * Wait until the RFCSR becomes available, afterwards we
181 * can safely write the read request into the register.
182 * After the data has been written, we wait until hardware
183 * returns the correct value, if at any time the register
184 * doesn't become available in time, reg will be 0xffffffff
185 * which means we return 0xff to the caller.
186 */
187 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
188 reg = 0;
189 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
190 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
191 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
192
193 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
194
195 WAIT_FOR_RFCSR(rt2x00dev, &reg);
196 }
197
198 *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
199
200 mutex_unlock(&rt2x00dev->csr_mutex);
201}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100202
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100203static void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
204 const unsigned int word, const u32 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100205{
206 u32 reg;
207
208 mutex_lock(&rt2x00dev->csr_mutex);
209
210 /*
211 * Wait until the RF becomes available, afterwards we
212 * can safely write the new data into the register.
213 */
214 if (WAIT_FOR_RF(rt2x00dev, &reg)) {
215 reg = 0;
216 rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
217 rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
218 rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
219 rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
220
221 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
222 rt2x00_rf_write(rt2x00dev, word, value);
223 }
224
225 mutex_unlock(&rt2x00dev->csr_mutex);
226}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100227
228void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
229 const u8 command, const u8 token,
230 const u8 arg0, const u8 arg1)
231{
232 u32 reg;
233
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100234 /*
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100235 * SOC devices don't support MCU requests.
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100236 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100237 if (rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100238 return;
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100239
240 mutex_lock(&rt2x00dev->csr_mutex);
241
242 /*
243 * Wait until the MCU becomes available, afterwards we
244 * can safely write the new data into the register.
245 */
246 if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
247 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
248 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
249 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
250 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
251 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
252
253 reg = 0;
254 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
255 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
256 }
257
258 mutex_unlock(&rt2x00dev->csr_mutex);
259}
260EXPORT_SYMBOL_GPL(rt2800_mcu_request);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100261
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100262int rt2800_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
263{
264 unsigned int i;
265 u32 reg;
266
267 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
268 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
269 if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
270 !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
271 return 0;
272
273 msleep(1);
274 }
275
276 ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
277 return -EACCES;
278}
279EXPORT_SYMBOL_GPL(rt2800_wait_wpdma_ready);
280
Gertjan van Wingerde0b8004a2010-06-03 10:51:45 +0200281void rt2800_write_txwi(__le32 *txwi, struct txentry_desc *txdesc)
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200282{
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200283 u32 word;
284
285 /*
286 * Initialize TX Info descriptor
287 */
288 rt2x00_desc_read(txwi, 0, &word);
289 rt2x00_set_field32(&word, TXWI_W0_FRAG,
290 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
291 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS, 0);
292 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
293 rt2x00_set_field32(&word, TXWI_W0_TS,
294 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
295 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
296 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
297 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
298 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->txop);
299 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
300 rt2x00_set_field32(&word, TXWI_W0_BW,
301 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
302 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
303 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
304 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
305 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
306 rt2x00_desc_write(txwi, 0, word);
307
308 rt2x00_desc_read(txwi, 1, &word);
309 rt2x00_set_field32(&word, TXWI_W1_ACK,
310 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
311 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
312 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
313 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
314 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
315 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
316 txdesc->key_idx : 0xff);
317 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
318 txdesc->length);
319 rt2x00_set_field32(&word, TXWI_W1_PACKETID, txdesc->queue + 1);
320 rt2x00_desc_write(txwi, 1, word);
321
322 /*
323 * Always write 0 to IV/EIV fields, hardware will insert the IV
324 * from the IVEIV register when TXD_W3_WIV is set to 0.
325 * When TXD_W3_WIV is set to 1 it will use the IV data
326 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
327 * crypto entry in the registers should be used to encrypt the frame.
328 */
329 _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
330 _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
331}
332EXPORT_SYMBOL_GPL(rt2800_write_txwi);
333
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200334void rt2800_process_rxwi(struct sk_buff *skb, struct rxdone_entry_desc *rxdesc)
335{
336 __le32 *rxwi = (__le32 *) skb->data;
337 u32 word;
338
339 rt2x00_desc_read(rxwi, 0, &word);
340
341 rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
342 rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
343
344 rt2x00_desc_read(rxwi, 1, &word);
345
346 if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
347 rxdesc->flags |= RX_FLAG_SHORT_GI;
348
349 if (rt2x00_get_field32(word, RXWI_W1_BW))
350 rxdesc->flags |= RX_FLAG_40MHZ;
351
352 /*
353 * Detect RX rate, always use MCS as signal type.
354 */
355 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
356 rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
357 rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
358
359 /*
360 * Mask of 0x8 bit to remove the short preamble flag.
361 */
362 if (rxdesc->rate_mode == RATE_MODE_CCK)
363 rxdesc->signal &= ~0x8;
364
365 rt2x00_desc_read(rxwi, 2, &word);
366
367 rxdesc->rssi =
368 (rt2x00_get_field32(word, RXWI_W2_RSSI0) +
369 rt2x00_get_field32(word, RXWI_W2_RSSI1)) / 2;
370
371 /*
372 * Remove RXWI descriptor from start of buffer.
373 */
374 skb_pull(skb, RXWI_DESC_SIZE);
375}
376EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
377
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200378void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
379{
380 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
381 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
382 unsigned int beacon_base;
383 u32 reg;
384
385 /*
386 * Disable beaconing while we are reloading the beacon data,
387 * otherwise we might be sending out invalid data.
388 */
389 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
390 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
391 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
392
393 /*
394 * Add space for the TXWI in front of the skb.
395 */
396 skb_push(entry->skb, TXWI_DESC_SIZE);
397 memset(entry->skb, 0, TXWI_DESC_SIZE);
398
399 /*
400 * Register descriptor details in skb frame descriptor.
401 */
402 skbdesc->flags |= SKBDESC_DESC_IN_SKB;
403 skbdesc->desc = entry->skb->data;
404 skbdesc->desc_len = TXWI_DESC_SIZE;
405
406 /*
407 * Add the TXWI for the beacon to the skb.
408 */
409 rt2800_write_txwi((__le32 *)entry->skb->data, txdesc);
410
411 /*
412 * Dump beacon to userspace through debugfs.
413 */
414 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
415
416 /*
417 * Write entire beacon with TXWI to register.
418 */
419 beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
420 rt2800_register_multiwrite(rt2x00dev, beacon_base,
421 entry->skb->data, entry->skb->len);
422
423 /*
424 * Enable beaconing again.
425 */
426 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
427 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
428 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
429 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
430
431 /*
432 * Clean up beacon skb.
433 */
434 dev_kfree_skb_any(entry->skb);
435 entry->skb = NULL;
436}
437EXPORT_SYMBOL(rt2800_write_beacon);
438
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100439#ifdef CONFIG_RT2X00_LIB_DEBUGFS
440const struct rt2x00debug rt2800_rt2x00debug = {
441 .owner = THIS_MODULE,
442 .csr = {
443 .read = rt2800_register_read,
444 .write = rt2800_register_write,
445 .flags = RT2X00DEBUGFS_OFFSET,
446 .word_base = CSR_REG_BASE,
447 .word_size = sizeof(u32),
448 .word_count = CSR_REG_SIZE / sizeof(u32),
449 },
450 .eeprom = {
451 .read = rt2x00_eeprom_read,
452 .write = rt2x00_eeprom_write,
453 .word_base = EEPROM_BASE,
454 .word_size = sizeof(u16),
455 .word_count = EEPROM_SIZE / sizeof(u16),
456 },
457 .bbp = {
458 .read = rt2800_bbp_read,
459 .write = rt2800_bbp_write,
460 .word_base = BBP_BASE,
461 .word_size = sizeof(u8),
462 .word_count = BBP_SIZE / sizeof(u8),
463 },
464 .rf = {
465 .read = rt2x00_rf_read,
466 .write = rt2800_rf_write,
467 .word_base = RF_BASE,
468 .word_size = sizeof(u32),
469 .word_count = RF_SIZE / sizeof(u32),
470 },
471};
472EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
473#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
474
475int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
476{
477 u32 reg;
478
479 rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
480 return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
481}
482EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
483
484#ifdef CONFIG_RT2X00_LIB_LEDS
485static void rt2800_brightness_set(struct led_classdev *led_cdev,
486 enum led_brightness brightness)
487{
488 struct rt2x00_led *led =
489 container_of(led_cdev, struct rt2x00_led, led_dev);
490 unsigned int enabled = brightness != LED_OFF;
491 unsigned int bg_mode =
492 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
493 unsigned int polarity =
494 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
495 EEPROM_FREQ_LED_POLARITY);
496 unsigned int ledmode =
497 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
498 EEPROM_FREQ_LED_MODE);
499
500 if (led->type == LED_TYPE_RADIO) {
501 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
502 enabled ? 0x20 : 0);
503 } else if (led->type == LED_TYPE_ASSOC) {
504 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
505 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
506 } else if (led->type == LED_TYPE_QUALITY) {
507 /*
508 * The brightness is divided into 6 levels (0 - 5),
509 * The specs tell us the following levels:
510 * 0, 1 ,3, 7, 15, 31
511 * to determine the level in a simple way we can simply
512 * work with bitshifting:
513 * (1 << level) - 1
514 */
515 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
516 (1 << brightness / (LED_FULL / 6)) - 1,
517 polarity);
518 }
519}
520
521static int rt2800_blink_set(struct led_classdev *led_cdev,
522 unsigned long *delay_on, unsigned long *delay_off)
523{
524 struct rt2x00_led *led =
525 container_of(led_cdev, struct rt2x00_led, led_dev);
526 u32 reg;
527
528 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
529 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
530 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100531 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
532
533 return 0;
534}
535
Gertjan van Wingerdeb3579d62009-12-30 11:36:34 +0100536static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100537 struct rt2x00_led *led, enum led_type type)
538{
539 led->rt2x00dev = rt2x00dev;
540 led->type = type;
541 led->led_dev.brightness_set = rt2800_brightness_set;
542 led->led_dev.blink_set = rt2800_blink_set;
543 led->flags = LED_INITIALIZED;
544}
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100545#endif /* CONFIG_RT2X00_LIB_LEDS */
546
547/*
548 * Configuration handlers.
549 */
550static void rt2800_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
551 struct rt2x00lib_crypto *crypto,
552 struct ieee80211_key_conf *key)
553{
554 struct mac_wcid_entry wcid_entry;
555 struct mac_iveiv_entry iveiv_entry;
556 u32 offset;
557 u32 reg;
558
559 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
560
561 rt2800_register_read(rt2x00dev, offset, &reg);
562 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
563 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
564 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
565 (crypto->cmd == SET_KEY) * crypto->cipher);
566 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
567 (crypto->cmd == SET_KEY) * crypto->bssidx);
568 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
569 rt2800_register_write(rt2x00dev, offset, reg);
570
571 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
572
573 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
574 if ((crypto->cipher == CIPHER_TKIP) ||
575 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
576 (crypto->cipher == CIPHER_AES))
577 iveiv_entry.iv[3] |= 0x20;
578 iveiv_entry.iv[3] |= key->keyidx << 6;
579 rt2800_register_multiwrite(rt2x00dev, offset,
580 &iveiv_entry, sizeof(iveiv_entry));
581
582 offset = MAC_WCID_ENTRY(key->hw_key_idx);
583
584 memset(&wcid_entry, 0, sizeof(wcid_entry));
585 if (crypto->cmd == SET_KEY)
586 memcpy(&wcid_entry, crypto->address, ETH_ALEN);
587 rt2800_register_multiwrite(rt2x00dev, offset,
588 &wcid_entry, sizeof(wcid_entry));
589}
590
591int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
592 struct rt2x00lib_crypto *crypto,
593 struct ieee80211_key_conf *key)
594{
595 struct hw_key_entry key_entry;
596 struct rt2x00_field32 field;
597 u32 offset;
598 u32 reg;
599
600 if (crypto->cmd == SET_KEY) {
601 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
602
603 memcpy(key_entry.key, crypto->key,
604 sizeof(key_entry.key));
605 memcpy(key_entry.tx_mic, crypto->tx_mic,
606 sizeof(key_entry.tx_mic));
607 memcpy(key_entry.rx_mic, crypto->rx_mic,
608 sizeof(key_entry.rx_mic));
609
610 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
611 rt2800_register_multiwrite(rt2x00dev, offset,
612 &key_entry, sizeof(key_entry));
613 }
614
615 /*
616 * The cipher types are stored over multiple registers
617 * starting with SHARED_KEY_MODE_BASE each word will have
618 * 32 bits and contains the cipher types for 2 bssidx each.
619 * Using the correct defines correctly will cause overhead,
620 * so just calculate the correct offset.
621 */
622 field.bit_offset = 4 * (key->hw_key_idx % 8);
623 field.bit_mask = 0x7 << field.bit_offset;
624
625 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
626
627 rt2800_register_read(rt2x00dev, offset, &reg);
628 rt2x00_set_field32(&reg, field,
629 (crypto->cmd == SET_KEY) * crypto->cipher);
630 rt2800_register_write(rt2x00dev, offset, reg);
631
632 /*
633 * Update WCID information
634 */
635 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
636
637 return 0;
638}
639EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
640
641int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
642 struct rt2x00lib_crypto *crypto,
643 struct ieee80211_key_conf *key)
644{
645 struct hw_key_entry key_entry;
646 u32 offset;
647
648 if (crypto->cmd == SET_KEY) {
649 /*
650 * 1 pairwise key is possible per AID, this means that the AID
651 * equals our hw_key_idx. Make sure the WCID starts _after_ the
652 * last possible shared key entry.
653 */
654 if (crypto->aid > (256 - 32))
655 return -ENOSPC;
656
657 key->hw_key_idx = 32 + crypto->aid;
658
659 memcpy(key_entry.key, crypto->key,
660 sizeof(key_entry.key));
661 memcpy(key_entry.tx_mic, crypto->tx_mic,
662 sizeof(key_entry.tx_mic));
663 memcpy(key_entry.rx_mic, crypto->rx_mic,
664 sizeof(key_entry.rx_mic));
665
666 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
667 rt2800_register_multiwrite(rt2x00dev, offset,
668 &key_entry, sizeof(key_entry));
669 }
670
671 /*
672 * Update WCID information
673 */
674 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
675
676 return 0;
677}
678EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
679
680void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
681 const unsigned int filter_flags)
682{
683 u32 reg;
684
685 /*
686 * Start configuration steps.
687 * Note that the version error will always be dropped
688 * and broadcast frames will always be accepted since
689 * there is no filter for it at this time.
690 */
691 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
692 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
693 !(filter_flags & FIF_FCSFAIL));
694 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
695 !(filter_flags & FIF_PLCPFAIL));
696 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
697 !(filter_flags & FIF_PROMISC_IN_BSS));
698 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
699 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
700 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
701 !(filter_flags & FIF_ALLMULTI));
702 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
703 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
704 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
705 !(filter_flags & FIF_CONTROL));
706 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
707 !(filter_flags & FIF_CONTROL));
708 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
709 !(filter_flags & FIF_CONTROL));
710 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
711 !(filter_flags & FIF_CONTROL));
712 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
713 !(filter_flags & FIF_CONTROL));
714 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
715 !(filter_flags & FIF_PSPOLL));
716 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
717 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
718 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
719 !(filter_flags & FIF_CONTROL));
720 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
721}
722EXPORT_SYMBOL_GPL(rt2800_config_filter);
723
724void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
725 struct rt2x00intf_conf *conf, const unsigned int flags)
726{
727 unsigned int beacon_base;
728 u32 reg;
729
730 if (flags & CONFIG_UPDATE_TYPE) {
731 /*
732 * Clear current synchronisation setup.
733 * For the Beacon base registers we only need to clear
734 * the first byte since that byte contains the VALID and OWNER
735 * bits which (when set to 0) will invalidate the entire beacon.
736 */
737 beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
738 rt2800_register_write(rt2x00dev, beacon_base, 0);
739
740 /*
741 * Enable synchronisation.
742 */
743 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
744 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
745 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Josef Bacik6a62e5ef2009-11-15 21:33:18 -0500746 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE,
747 (conf->sync == TSF_SYNC_BEACON));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100748 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
749 }
750
751 if (flags & CONFIG_UPDATE_MAC) {
752 reg = le32_to_cpu(conf->mac[1]);
753 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
754 conf->mac[1] = cpu_to_le32(reg);
755
756 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
757 conf->mac, sizeof(conf->mac));
758 }
759
760 if (flags & CONFIG_UPDATE_BSSID) {
761 reg = le32_to_cpu(conf->bssid[1]);
762 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 0);
763 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 0);
764 conf->bssid[1] = cpu_to_le32(reg);
765
766 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
767 conf->bssid, sizeof(conf->bssid));
768 }
769}
770EXPORT_SYMBOL_GPL(rt2800_config_intf);
771
772void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp)
773{
774 u32 reg;
775
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100776 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
777 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
778 !!erp->short_preamble);
779 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
780 !!erp->short_preamble);
781 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
782
783 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
784 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
785 erp->cts_protection ? 2 : 0);
786 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
787
788 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
789 erp->basic_rates);
790 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
791
792 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
793 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, erp->slot_time);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100794 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
795
796 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100797 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100798 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
799
800 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
801 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
802 erp->beacon_int * 16);
803 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
804}
805EXPORT_SYMBOL_GPL(rt2800_config_erp);
806
807void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
808{
809 u8 r1;
810 u8 r3;
811
812 rt2800_bbp_read(rt2x00dev, 1, &r1);
813 rt2800_bbp_read(rt2x00dev, 3, &r3);
814
815 /*
816 * Configure the TX antenna.
817 */
818 switch ((int)ant->tx) {
819 case 1:
820 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100821 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100822 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
823 break;
824 case 2:
825 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
826 break;
827 case 3:
828 /* Do nothing */
829 break;
830 }
831
832 /*
833 * Configure the RX antenna.
834 */
835 switch ((int)ant->rx) {
836 case 1:
837 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
838 break;
839 case 2:
840 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
841 break;
842 case 3:
843 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
844 break;
845 }
846
847 rt2800_bbp_write(rt2x00dev, 3, r3);
848 rt2800_bbp_write(rt2x00dev, 1, r1);
849}
850EXPORT_SYMBOL_GPL(rt2800_config_ant);
851
852static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
853 struct rt2x00lib_conf *libconf)
854{
855 u16 eeprom;
856 short lna_gain;
857
858 if (libconf->rf.channel <= 14) {
859 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
860 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
861 } else if (libconf->rf.channel <= 64) {
862 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
863 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
864 } else if (libconf->rf.channel <= 128) {
865 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
866 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
867 } else {
868 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
869 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
870 }
871
872 rt2x00dev->lna_gain = lna_gain;
873}
874
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200875static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
876 struct ieee80211_conf *conf,
877 struct rf_channel *rf,
878 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100879{
880 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
881
882 if (rt2x00dev->default_ant.tx == 1)
883 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
884
885 if (rt2x00dev->default_ant.rx == 1) {
886 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
887 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
888 } else if (rt2x00dev->default_ant.rx == 2)
889 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
890
891 if (rf->channel > 14) {
892 /*
893 * When TX power is below 0, we should increase it by 7 to
894 * make it a positive value (Minumum value is -7).
895 * However this means that values between 0 and 7 have
896 * double meaning, and we should set a 7DBm boost flag.
897 */
898 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
899 (info->tx_power1 >= 0));
900
901 if (info->tx_power1 < 0)
902 info->tx_power1 += 7;
903
904 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A,
905 TXPOWER_A_TO_DEV(info->tx_power1));
906
907 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
908 (info->tx_power2 >= 0));
909
910 if (info->tx_power2 < 0)
911 info->tx_power2 += 7;
912
913 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A,
914 TXPOWER_A_TO_DEV(info->tx_power2));
915 } else {
916 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G,
917 TXPOWER_G_TO_DEV(info->tx_power1));
918 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G,
919 TXPOWER_G_TO_DEV(info->tx_power2));
920 }
921
922 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
923
924 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
925 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
926 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
927 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
928
929 udelay(200);
930
931 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
932 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
933 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
934 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
935
936 udelay(200);
937
938 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
939 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
940 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
941 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
942}
943
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200944static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
945 struct ieee80211_conf *conf,
946 struct rf_channel *rf,
947 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100948{
949 u8 rfcsr;
950
951 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Gertjan van Wingerde41a26172009-11-09 22:59:04 +0100952 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100953
954 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +0200955 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100956 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
957
958 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
959 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
960 TXPOWER_G_TO_DEV(info->tx_power1));
961 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
962
Helmut Schaa5a673962010-04-23 15:54:43 +0200963 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
964 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
965 TXPOWER_G_TO_DEV(info->tx_power2));
966 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
967
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100968 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
969 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
970 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
971
972 rt2800_rfcsr_write(rt2x00dev, 24,
973 rt2x00dev->calibration[conf_is_ht40(conf)]);
974
Gertjan van Wingerde71976902010-03-24 21:42:36 +0100975 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100976 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +0100977 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100978}
979
980static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
981 struct ieee80211_conf *conf,
982 struct rf_channel *rf,
983 struct channel_info *info)
984{
985 u32 reg;
986 unsigned int tx_pin;
987 u8 bbp;
988
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200989 if (rt2x00_rf(rt2x00dev, RF2020) ||
990 rt2x00_rf(rt2x00dev, RF3020) ||
991 rt2x00_rf(rt2x00dev, RF3021) ||
992 rt2x00_rf(rt2x00dev, RF3022))
993 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerdefa6f6322009-11-09 22:59:58 +0100994 else
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200995 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100996
997 /*
998 * Change BBP settings
999 */
1000 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
1001 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
1002 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
1003 rt2800_bbp_write(rt2x00dev, 86, 0);
1004
1005 if (rf->channel <= 14) {
1006 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
1007 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1008 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1009 } else {
1010 rt2800_bbp_write(rt2x00dev, 82, 0x84);
1011 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1012 }
1013 } else {
1014 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
1015
1016 if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
1017 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1018 else
1019 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1020 }
1021
1022 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001023 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001024 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
1025 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
1026 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
1027
1028 tx_pin = 0;
1029
1030 /* Turn on unused PA or LNA when not using 1T or 1R */
1031 if (rt2x00dev->default_ant.tx != 1) {
1032 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
1033 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
1034 }
1035
1036 /* Turn on unused PA or LNA when not using 1T or 1R */
1037 if (rt2x00dev->default_ant.rx != 1) {
1038 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
1039 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
1040 }
1041
1042 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
1043 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
1044 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
1045 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
1046 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
1047 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
1048
1049 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
1050
1051 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1052 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
1053 rt2800_bbp_write(rt2x00dev, 4, bbp);
1054
1055 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001056 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001057 rt2800_bbp_write(rt2x00dev, 3, bbp);
1058
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001059 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001060 if (conf_is_ht40(conf)) {
1061 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
1062 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1063 rt2800_bbp_write(rt2x00dev, 73, 0x16);
1064 } else {
1065 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1066 rt2800_bbp_write(rt2x00dev, 70, 0x08);
1067 rt2800_bbp_write(rt2x00dev, 73, 0x11);
1068 }
1069 }
1070
1071 msleep(1);
1072}
1073
1074static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
1075 const int txpower)
1076{
1077 u32 reg;
1078 u32 value = TXPOWER_G_TO_DEV(txpower);
1079 u8 r1;
1080
1081 rt2800_bbp_read(rt2x00dev, 1, &r1);
1082 rt2x00_set_field8(&reg, BBP1_TX_POWER, 0);
1083 rt2800_bbp_write(rt2x00dev, 1, r1);
1084
1085 rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
1086 rt2x00_set_field32(&reg, TX_PWR_CFG_0_1MBS, value);
1087 rt2x00_set_field32(&reg, TX_PWR_CFG_0_2MBS, value);
1088 rt2x00_set_field32(&reg, TX_PWR_CFG_0_55MBS, value);
1089 rt2x00_set_field32(&reg, TX_PWR_CFG_0_11MBS, value);
1090 rt2x00_set_field32(&reg, TX_PWR_CFG_0_6MBS, value);
1091 rt2x00_set_field32(&reg, TX_PWR_CFG_0_9MBS, value);
1092 rt2x00_set_field32(&reg, TX_PWR_CFG_0_12MBS, value);
1093 rt2x00_set_field32(&reg, TX_PWR_CFG_0_18MBS, value);
1094 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0, reg);
1095
1096 rt2800_register_read(rt2x00dev, TX_PWR_CFG_1, &reg);
1097 rt2x00_set_field32(&reg, TX_PWR_CFG_1_24MBS, value);
1098 rt2x00_set_field32(&reg, TX_PWR_CFG_1_36MBS, value);
1099 rt2x00_set_field32(&reg, TX_PWR_CFG_1_48MBS, value);
1100 rt2x00_set_field32(&reg, TX_PWR_CFG_1_54MBS, value);
1101 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS0, value);
1102 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS1, value);
1103 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS2, value);
1104 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS3, value);
1105 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1, reg);
1106
1107 rt2800_register_read(rt2x00dev, TX_PWR_CFG_2, &reg);
1108 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS4, value);
1109 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS5, value);
1110 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS6, value);
1111 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS7, value);
1112 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS8, value);
1113 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS9, value);
1114 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS10, value);
1115 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS11, value);
1116 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2, reg);
1117
1118 rt2800_register_read(rt2x00dev, TX_PWR_CFG_3, &reg);
1119 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS12, value);
1120 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS13, value);
1121 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS14, value);
1122 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS15, value);
1123 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN1, value);
1124 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN2, value);
1125 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN3, value);
1126 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN4, value);
1127 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3, reg);
1128
1129 rt2800_register_read(rt2x00dev, TX_PWR_CFG_4, &reg);
1130 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN5, value);
1131 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN6, value);
1132 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN7, value);
1133 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN8, value);
1134 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4, reg);
1135}
1136
1137static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
1138 struct rt2x00lib_conf *libconf)
1139{
1140 u32 reg;
1141
1142 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1143 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
1144 libconf->conf->short_frame_max_tx_count);
1145 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
1146 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001147 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1148}
1149
1150static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
1151 struct rt2x00lib_conf *libconf)
1152{
1153 enum dev_state state =
1154 (libconf->conf->flags & IEEE80211_CONF_PS) ?
1155 STATE_SLEEP : STATE_AWAKE;
1156 u32 reg;
1157
1158 if (state == STATE_SLEEP) {
1159 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
1160
1161 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1162 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
1163 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
1164 libconf->conf->listen_interval - 1);
1165 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
1166 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
1167
1168 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
1169 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001170 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1171 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
1172 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
1173 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
1174 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02001175
1176 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001177 }
1178}
1179
1180void rt2800_config(struct rt2x00_dev *rt2x00dev,
1181 struct rt2x00lib_conf *libconf,
1182 const unsigned int flags)
1183{
1184 /* Always recalculate LNA gain before changing configuration */
1185 rt2800_config_lna_gain(rt2x00dev, libconf);
1186
1187 if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
1188 rt2800_config_channel(rt2x00dev, libconf->conf,
1189 &libconf->rf, &libconf->channel);
1190 if (flags & IEEE80211_CONF_CHANGE_POWER)
1191 rt2800_config_txpower(rt2x00dev, libconf->conf->power_level);
1192 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
1193 rt2800_config_retry_limit(rt2x00dev, libconf);
1194 if (flags & IEEE80211_CONF_CHANGE_PS)
1195 rt2800_config_ps(rt2x00dev, libconf);
1196}
1197EXPORT_SYMBOL_GPL(rt2800_config);
1198
1199/*
1200 * Link tuning
1201 */
1202void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1203{
1204 u32 reg;
1205
1206 /*
1207 * Update FCS error count from register.
1208 */
1209 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1210 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
1211}
1212EXPORT_SYMBOL_GPL(rt2800_link_stats);
1213
1214static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
1215{
1216 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001217 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001218 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001219 rt2x00_rt(rt2x00dev, RT3090) ||
1220 rt2x00_rt(rt2x00dev, RT3390))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001221 return 0x1c + (2 * rt2x00dev->lna_gain);
1222 else
1223 return 0x2e + rt2x00dev->lna_gain;
1224 }
1225
1226 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
1227 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
1228 else
1229 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
1230}
1231
1232static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
1233 struct link_qual *qual, u8 vgc_level)
1234{
1235 if (qual->vgc_level != vgc_level) {
1236 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
1237 qual->vgc_level = vgc_level;
1238 qual->vgc_level_reg = vgc_level;
1239 }
1240}
1241
1242void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1243{
1244 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
1245}
1246EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
1247
1248void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
1249 const u32 count)
1250{
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001251 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001252 return;
1253
1254 /*
1255 * When RSSI is better then -80 increase VGC level with 0x10
1256 */
1257 rt2800_set_vgc(rt2x00dev, qual,
1258 rt2800_get_default_vgc(rt2x00dev) +
1259 ((qual->rssi > -80) * 0x10));
1260}
1261EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001262
1263/*
1264 * Initialization functions.
1265 */
1266int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
1267{
1268 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001269 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001270 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001271 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001272
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001273 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1274 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1275 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1276 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1277 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1278 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
1279 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1280
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001281 ret = rt2800_drv_init_registers(rt2x00dev);
1282 if (ret)
1283 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001284
1285 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
1286 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
1287 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
1288 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
1289 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
1290 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
1291
1292 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
1293 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
1294 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
1295 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
1296 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
1297 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
1298
1299 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
1300 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1301
1302 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1303
1304 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1305 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 0);
1306 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
1307 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
1308 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
1309 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1310 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
1311 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1312
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001313 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
1314
1315 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1316 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
1317 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
1318 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
1319
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001320 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001321 rt2x00_rt(rt2x00dev, RT3090) ||
1322 rt2x00_rt(rt2x00dev, RT3390)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001323 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1324 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001325 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001326 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
1327 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001328 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1329 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
1330 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1331 0x0000002c);
1332 else
1333 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1334 0x0000000f);
1335 } else {
1336 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1337 }
1338 rt2800_register_write(rt2x00dev, TX_SW_CFG2, reg);
1339 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001340 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001341
1342 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
1343 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1344 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
1345 } else {
1346 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1347 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1348 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001349 } else {
1350 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
1351 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1352 }
1353
1354 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
1355 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
1356 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
1357 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
1358 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
1359 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
1360 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
1361 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
1362 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
1363 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
1364
1365 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
1366 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001367 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001368 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
1369 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
1370
1371 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
1372 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001373 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01001374 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001375 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001376 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
1377 else
1378 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
1379 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
1380 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
1381 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
1382
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001383 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1384 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
1385 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
1386 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
1387 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
1388 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
1389 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
1390 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
1391 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1392
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001393 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
1394
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001395 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1396 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
1397 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
1398 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
1399 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
1400 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
1401 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
1402 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1403
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001404 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1405 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001406 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001407 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
1408 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001409 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001410 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
1411 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
1412 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1413
1414 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001415 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001416 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
1417 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
1418 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1419 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1420 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001421 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001422 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001423 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1424 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001425 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
1426
1427 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001428 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001429 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
1430 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
1431 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1432 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1433 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001434 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001435 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001436 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1437 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001438 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1439
1440 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1441 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
1442 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
1443 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
1444 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1445 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1446 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1447 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1448 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1449 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001450 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001451 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1452
1453 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1454 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001455 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL,
1456 !rt2x00_is_usb(rt2x00dev));
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001457 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
1458 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1459 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1460 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1461 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1462 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1463 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001464 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001465 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1466
1467 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1468 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
1469 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
1470 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
1471 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1472 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1473 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1474 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1475 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1476 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001477 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001478 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1479
1480 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1481 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
1482 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
1483 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
1484 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1485 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1486 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1487 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1488 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1489 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001490 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001491 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1492
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001493 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001494 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
1495
1496 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1497 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1498 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1499 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1500 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1501 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
1502 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
1503 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
1504 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
1505 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
1506 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1507 }
1508
1509 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, 0x0000583f);
1510 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
1511
1512 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
1513 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
1514 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
1515 IEEE80211_MAX_RTS_THRESHOLD);
1516 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
1517 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
1518
1519 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001520
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001521 /*
1522 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
1523 * time should be set to 16. However, the original Ralink driver uses
1524 * 16 for both and indeed using a value of 10 for CCK SIFS results in
1525 * connection problems with 11g + CTS protection. Hence, use the same
1526 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
1527 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001528 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001529 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
1530 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001531 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
1532 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
1533 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
1534 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1535
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001536 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
1537
1538 /*
1539 * ASIC will keep garbage value after boot, clear encryption keys.
1540 */
1541 for (i = 0; i < 4; i++)
1542 rt2800_register_write(rt2x00dev,
1543 SHARED_KEY_MODE_ENTRY(i), 0);
1544
1545 for (i = 0; i < 256; i++) {
1546 u32 wcid[2] = { 0xffffffff, 0x00ffffff };
1547 rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
1548 wcid, sizeof(wcid));
1549
1550 rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
1551 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
1552 }
1553
1554 /*
1555 * Clear all beacons
1556 * For the Beacon base registers we only need to clear
1557 * the first byte since that byte contains the VALID and OWNER
1558 * bits which (when set to 0) will invalidate the entire beacon.
1559 */
1560 rt2800_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
1561 rt2800_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
1562 rt2800_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
1563 rt2800_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
1564 rt2800_register_write(rt2x00dev, HW_BEACON_BASE4, 0);
1565 rt2800_register_write(rt2x00dev, HW_BEACON_BASE5, 0);
1566 rt2800_register_write(rt2x00dev, HW_BEACON_BASE6, 0);
1567 rt2800_register_write(rt2x00dev, HW_BEACON_BASE7, 0);
1568
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001569 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02001570 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
1571 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
1572 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001573 }
1574
1575 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
1576 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
1577 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
1578 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
1579 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
1580 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
1581 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
1582 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
1583 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
1584 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
1585
1586 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
1587 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
1588 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
1589 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
1590 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
1591 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
1592 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
1593 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
1594 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
1595 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
1596
1597 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
1598 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
1599 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
1600 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
1601 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
1602 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
1603 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
1604 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
1605 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
1606 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
1607
1608 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
1609 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
1610 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
1611 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
1612 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
1613 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
1614
1615 /*
1616 * We must clear the error counters.
1617 * These registers are cleared on read,
1618 * so we may pass a useless variable to store the value.
1619 */
1620 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1621 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
1622 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
1623 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
1624 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
1625 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
1626
1627 return 0;
1628}
1629EXPORT_SYMBOL_GPL(rt2800_init_registers);
1630
1631static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
1632{
1633 unsigned int i;
1634 u32 reg;
1635
1636 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1637 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
1638 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
1639 return 0;
1640
1641 udelay(REGISTER_BUSY_DELAY);
1642 }
1643
1644 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
1645 return -EACCES;
1646}
1647
1648static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
1649{
1650 unsigned int i;
1651 u8 value;
1652
1653 /*
1654 * BBP was enabled after firmware was loaded,
1655 * but we need to reactivate it now.
1656 */
1657 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
1658 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
1659 msleep(1);
1660
1661 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1662 rt2800_bbp_read(rt2x00dev, 0, &value);
1663 if ((value != 0xff) && (value != 0x00))
1664 return 0;
1665 udelay(REGISTER_BUSY_DELAY);
1666 }
1667
1668 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
1669 return -EACCES;
1670}
1671
1672int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
1673{
1674 unsigned int i;
1675 u16 eeprom;
1676 u8 reg_id;
1677 u8 value;
1678
1679 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
1680 rt2800_wait_bbp_ready(rt2x00dev)))
1681 return -EACCES;
1682
Helmut Schaabaff8002010-04-28 09:58:59 +02001683 if (rt2800_is_305x_soc(rt2x00dev))
1684 rt2800_bbp_write(rt2x00dev, 31, 0x08);
1685
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001686 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
1687 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001688
1689 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
1690 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1691 rt2800_bbp_write(rt2x00dev, 73, 0x12);
1692 } else {
1693 rt2800_bbp_write(rt2x00dev, 69, 0x12);
1694 rt2800_bbp_write(rt2x00dev, 73, 0x10);
1695 }
1696
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001697 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001698
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001699 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001700 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001701 rt2x00_rt(rt2x00dev, RT3090) ||
1702 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001703 rt2800_bbp_write(rt2x00dev, 79, 0x13);
1704 rt2800_bbp_write(rt2x00dev, 80, 0x05);
1705 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Helmut Schaabaff8002010-04-28 09:58:59 +02001706 } else if (rt2800_is_305x_soc(rt2x00dev)) {
1707 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
1708 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001709 } else {
1710 rt2800_bbp_write(rt2x00dev, 81, 0x37);
1711 }
1712
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001713 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1714 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001715
Gertjan van Wingerde5ed8f452010-06-03 10:51:57 +02001716 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001717 rt2800_bbp_write(rt2x00dev, 84, 0x19);
1718 else
1719 rt2800_bbp_write(rt2x00dev, 84, 0x99);
1720
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001721 rt2800_bbp_write(rt2x00dev, 86, 0x00);
1722 rt2800_bbp_write(rt2x00dev, 91, 0x04);
1723 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001724
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001725 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001726 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001727 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02001728 rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
1729 rt2800_is_305x_soc(rt2x00dev))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001730 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
1731 else
1732 rt2800_bbp_write(rt2x00dev, 103, 0x00);
1733
Helmut Schaabaff8002010-04-28 09:58:59 +02001734 if (rt2800_is_305x_soc(rt2x00dev))
1735 rt2800_bbp_write(rt2x00dev, 105, 0x01);
1736 else
1737 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001738 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001739
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001740 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001741 rt2x00_rt(rt2x00dev, RT3090) ||
1742 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001743 rt2800_bbp_read(rt2x00dev, 138, &value);
1744
1745 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
1746 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
1747 value |= 0x20;
1748 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
1749 value &= ~0x02;
1750
1751 rt2800_bbp_write(rt2x00dev, 138, value);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001752 }
1753
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001754
1755 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
1756 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
1757
1758 if (eeprom != 0xffff && eeprom != 0x0000) {
1759 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
1760 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
1761 rt2800_bbp_write(rt2x00dev, reg_id, value);
1762 }
1763 }
1764
1765 return 0;
1766}
1767EXPORT_SYMBOL_GPL(rt2800_init_bbp);
1768
1769static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
1770 bool bw40, u8 rfcsr24, u8 filter_target)
1771{
1772 unsigned int i;
1773 u8 bbp;
1774 u8 rfcsr;
1775 u8 passband;
1776 u8 stopband;
1777 u8 overtuned = 0;
1778
1779 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1780
1781 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1782 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
1783 rt2800_bbp_write(rt2x00dev, 4, bbp);
1784
1785 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
1786 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
1787 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
1788
1789 /*
1790 * Set power & frequency of passband test tone
1791 */
1792 rt2800_bbp_write(rt2x00dev, 24, 0);
1793
1794 for (i = 0; i < 100; i++) {
1795 rt2800_bbp_write(rt2x00dev, 25, 0x90);
1796 msleep(1);
1797
1798 rt2800_bbp_read(rt2x00dev, 55, &passband);
1799 if (passband)
1800 break;
1801 }
1802
1803 /*
1804 * Set power & frequency of stopband test tone
1805 */
1806 rt2800_bbp_write(rt2x00dev, 24, 0x06);
1807
1808 for (i = 0; i < 100; i++) {
1809 rt2800_bbp_write(rt2x00dev, 25, 0x90);
1810 msleep(1);
1811
1812 rt2800_bbp_read(rt2x00dev, 55, &stopband);
1813
1814 if ((passband - stopband) <= filter_target) {
1815 rfcsr24++;
1816 overtuned += ((passband - stopband) == filter_target);
1817 } else
1818 break;
1819
1820 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1821 }
1822
1823 rfcsr24 -= !!overtuned;
1824
1825 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1826 return rfcsr24;
1827}
1828
1829int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
1830{
1831 u8 rfcsr;
1832 u8 bbp;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001833 u32 reg;
1834 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001835
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001836 if (!rt2x00_rt(rt2x00dev, RT3070) &&
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001837 !rt2x00_rt(rt2x00dev, RT3071) &&
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001838 !rt2x00_rt(rt2x00dev, RT3090) &&
Helmut Schaa23812382010-04-26 13:48:45 +02001839 !rt2x00_rt(rt2x00dev, RT3390) &&
Helmut Schaabaff8002010-04-28 09:58:59 +02001840 !rt2800_is_305x_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001841 return 0;
1842
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001843 /*
1844 * Init RF calibration.
1845 */
1846 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1847 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1848 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1849 msleep(1);
1850 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1851 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1852
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001853 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001854 rt2x00_rt(rt2x00dev, RT3071) ||
1855 rt2x00_rt(rt2x00dev, RT3090)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001856 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
1857 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
1858 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
1859 rt2800_rfcsr_write(rt2x00dev, 7, 0x70);
1860 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001861 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001862 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
1863 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
1864 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
1865 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
1866 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
1867 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
1868 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
1869 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
1870 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
1871 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
1872 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
1873 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001874 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001875 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
1876 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
1877 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
1878 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
1879 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001880 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001881 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
1882 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
1883 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
1884 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
1885 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
1886 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001887 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001888 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
1889 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001890 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001891 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
1892 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
1893 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
1894 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
1895 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
1896 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
1897 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001898 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001899 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001900 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001901 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
1902 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
1903 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
1904 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
1905 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
1906 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
1907 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Helmut Schaabaff8002010-04-28 09:58:59 +02001908 } else if (rt2800_is_305x_soc(rt2x00dev)) {
Helmut Schaa23812382010-04-26 13:48:45 +02001909 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
1910 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
1911 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
1912 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
1913 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
1914 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
1915 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
1916 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
1917 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
1918 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
1919 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
1920 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
1921 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
1922 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
1923 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
1924 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
1925 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
1926 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
1927 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
1928 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
1929 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
1930 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
1931 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
1932 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
1933 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
1934 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
1935 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
1936 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
1937 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
1938 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
Helmut Schaabaff8002010-04-28 09:58:59 +02001939 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
1940 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
1941 return 0;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001942 }
1943
1944 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
1945 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
1946 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
1947 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
1948 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001949 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
1950 rt2x00_rt(rt2x00dev, RT3090)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001951 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
1952 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
1953 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1954
1955 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
1956
1957 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
1958 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001959 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
1960 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001961 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1962 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
1963 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
1964 else
1965 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
1966 }
1967 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001968 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
1969 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
1970 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
1971 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001972 }
1973
1974 /*
1975 * Set RX Filter calibration for 20MHz and 40MHz
1976 */
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001977 if (rt2x00_rt(rt2x00dev, RT3070)) {
1978 rt2x00dev->calibration[0] =
1979 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
1980 rt2x00dev->calibration[1] =
1981 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001982 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001983 rt2x00_rt(rt2x00dev, RT3090) ||
1984 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001985 rt2x00dev->calibration[0] =
1986 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
1987 rt2x00dev->calibration[1] =
1988 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001989 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001990
1991 /*
1992 * Set back to initial state
1993 */
1994 rt2800_bbp_write(rt2x00dev, 24, 0);
1995
1996 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
1997 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
1998 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
1999
2000 /*
2001 * set BBP back to BW20
2002 */
2003 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2004 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
2005 rt2800_bbp_write(rt2x00dev, 4, bbp);
2006
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002007 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002008 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002009 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2010 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002011 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
2012
2013 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
2014 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
2015 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
2016
2017 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
2018 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002019 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002020 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2021 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerde8440c292010-06-03 10:52:02 +02002022 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002023 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
2024 }
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002025 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &eeprom);
2026 if (rt2x00_get_field16(eeprom, EEPROM_TXMIXER_GAIN_BG_VAL) >= 1)
2027 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
2028 rt2x00_get_field16(eeprom,
2029 EEPROM_TXMIXER_GAIN_BG_VAL));
2030 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
2031
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002032 if (rt2x00_rt(rt2x00dev, RT3090)) {
2033 rt2800_bbp_read(rt2x00dev, 138, &bbp);
2034
2035 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2036 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
2037 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
2038 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
2039 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
2040
2041 rt2800_bbp_write(rt2x00dev, 138, bbp);
2042 }
2043
2044 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002045 rt2x00_rt(rt2x00dev, RT3090) ||
2046 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002047 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2048 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2049 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2050 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2051 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2052 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2053 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2054
2055 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
2056 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
2057 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
2058
2059 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
2060 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
2061 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
2062
2063 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
2064 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
2065 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
2066 }
2067
2068 if (rt2x00_rt(rt2x00dev, RT3070) || rt2x00_rt(rt2x00dev, RT3071)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002069 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002070 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
2071 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002072 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
2073 else
2074 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
2075 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
2076 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
2077 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
2078 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
2079 }
2080
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002081 return 0;
2082}
2083EXPORT_SYMBOL_GPL(rt2800_init_rfcsr);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002084
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002085int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
2086{
2087 u32 reg;
2088
2089 rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
2090
2091 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
2092}
2093EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
2094
2095static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
2096{
2097 u32 reg;
2098
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002099 mutex_lock(&rt2x00dev->csr_mutex);
2100
2101 rt2800_register_read_lock(rt2x00dev, EFUSE_CTRL, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002102 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
2103 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
2104 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002105 rt2800_register_write_lock(rt2x00dev, EFUSE_CTRL, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002106
2107 /* Wait until the EEPROM has been loaded */
2108 rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
2109
2110 /* Apparently the data is read from end to start */
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002111 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA3,
2112 (u32 *)&rt2x00dev->eeprom[i]);
2113 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA2,
2114 (u32 *)&rt2x00dev->eeprom[i + 2]);
2115 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA1,
2116 (u32 *)&rt2x00dev->eeprom[i + 4]);
2117 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA0,
2118 (u32 *)&rt2x00dev->eeprom[i + 6]);
2119
2120 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002121}
2122
2123void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
2124{
2125 unsigned int i;
2126
2127 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
2128 rt2800_efuse_read(rt2x00dev, i);
2129}
2130EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
2131
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002132int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
2133{
2134 u16 word;
2135 u8 *mac;
2136 u8 default_lna_gain;
2137
2138 /*
2139 * Start validation of the data that has been read.
2140 */
2141 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
2142 if (!is_valid_ether_addr(mac)) {
2143 random_ether_addr(mac);
2144 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
2145 }
2146
2147 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
2148 if (word == 0xffff) {
2149 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2150 rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
2151 rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
2152 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2153 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002154 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02002155 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002156 /*
2157 * There is a max of 2 RX streams for RT28x0 series
2158 */
2159 if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
2160 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2161 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2162 }
2163
2164 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
2165 if (word == 0xffff) {
2166 rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
2167 rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
2168 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
2169 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
2170 rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
2171 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
2172 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
2173 rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
2174 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
2175 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
2176 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
2177 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
2178 }
2179
2180 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
2181 if ((word & 0x00ff) == 0x00ff) {
2182 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
2183 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
2184 LED_MODE_TXRX_ACTIVITY);
2185 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
2186 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2187 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
2188 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
2189 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
2190 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
2191 }
2192
2193 /*
2194 * During the LNA validation we are going to use
2195 * lna0 as correct value. Note that EEPROM_LNA
2196 * is never validated.
2197 */
2198 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
2199 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
2200
2201 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
2202 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
2203 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
2204 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
2205 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
2206 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
2207
2208 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
2209 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
2210 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
2211 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
2212 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
2213 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
2214 default_lna_gain);
2215 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
2216
2217 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
2218 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
2219 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
2220 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
2221 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
2222 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
2223
2224 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
2225 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
2226 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
2227 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
2228 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
2229 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
2230 default_lna_gain);
2231 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
2232
2233 return 0;
2234}
2235EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
2236
2237int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
2238{
2239 u32 reg;
2240 u16 value;
2241 u16 eeprom;
2242
2243 /*
2244 * Read EEPROM word for configuration.
2245 */
2246 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2247
2248 /*
2249 * Identify RF chipset.
2250 */
2251 value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
2252 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
2253
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002254 rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
2255 value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
Gertjan van Wingerde714fa662010-02-13 20:55:48 +01002256
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002257 if (!rt2x00_rt(rt2x00dev, RT2860) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002258 !rt2x00_rt(rt2x00dev, RT2872) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002259 !rt2x00_rt(rt2x00dev, RT2883) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002260 !rt2x00_rt(rt2x00dev, RT3070) &&
2261 !rt2x00_rt(rt2x00dev, RT3071) &&
2262 !rt2x00_rt(rt2x00dev, RT3090) &&
2263 !rt2x00_rt(rt2x00dev, RT3390) &&
2264 !rt2x00_rt(rt2x00dev, RT3572)) {
2265 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
2266 return -ENODEV;
2267 }
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002268
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002269 if (!rt2x00_rf(rt2x00dev, RF2820) &&
2270 !rt2x00_rf(rt2x00dev, RF2850) &&
2271 !rt2x00_rf(rt2x00dev, RF2720) &&
2272 !rt2x00_rf(rt2x00dev, RF2750) &&
2273 !rt2x00_rf(rt2x00dev, RF3020) &&
2274 !rt2x00_rf(rt2x00dev, RF2020) &&
2275 !rt2x00_rf(rt2x00dev, RF3021) &&
Gertjan van Wingerde6c0fe262009-12-30 11:36:31 +01002276 !rt2x00_rf(rt2x00dev, RF3022) &&
2277 !rt2x00_rf(rt2x00dev, RF3052)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002278 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
2279 return -ENODEV;
2280 }
2281
2282 /*
2283 * Identify default antenna configuration.
2284 */
2285 rt2x00dev->default_ant.tx =
2286 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH);
2287 rt2x00dev->default_ant.rx =
2288 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH);
2289
2290 /*
2291 * Read frequency offset and RF programming sequence.
2292 */
2293 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
2294 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
2295
2296 /*
2297 * Read external LNA informations.
2298 */
2299 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2300
2301 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
2302 __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
2303 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
2304 __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
2305
2306 /*
2307 * Detect if this device has an hardware controlled radio.
2308 */
2309 if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
2310 __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
2311
2312 /*
2313 * Store led settings, for correct led behaviour.
2314 */
2315#ifdef CONFIG_RT2X00_LIB_LEDS
2316 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
2317 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
2318 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
2319
2320 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &rt2x00dev->led_mcu_reg);
2321#endif /* CONFIG_RT2X00_LIB_LEDS */
2322
2323 return 0;
2324}
2325EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
2326
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002327/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002328 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002329 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
2330 */
2331static const struct rf_channel rf_vals[] = {
2332 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
2333 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
2334 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
2335 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
2336 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
2337 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
2338 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
2339 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
2340 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
2341 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
2342 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
2343 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
2344 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
2345 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
2346
2347 /* 802.11 UNI / HyperLan 2 */
2348 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
2349 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
2350 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
2351 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
2352 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
2353 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
2354 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
2355 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
2356 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
2357 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
2358 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
2359 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
2360
2361 /* 802.11 HyperLan 2 */
2362 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
2363 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
2364 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
2365 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
2366 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
2367 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
2368 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
2369 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
2370 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
2371 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
2372 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
2373 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
2374 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
2375 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
2376 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
2377 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
2378
2379 /* 802.11 UNII */
2380 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
2381 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
2382 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
2383 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
2384 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
2385 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
2386 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
2387 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
2388 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
2389 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
2390 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
2391
2392 /* 802.11 Japan */
2393 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
2394 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
2395 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
2396 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
2397 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
2398 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
2399 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
2400};
2401
2402/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002403 * RF value list for rt3xxx
2404 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002405 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02002406static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002407 {1, 241, 2, 2 },
2408 {2, 241, 2, 7 },
2409 {3, 242, 2, 2 },
2410 {4, 242, 2, 7 },
2411 {5, 243, 2, 2 },
2412 {6, 243, 2, 7 },
2413 {7, 244, 2, 2 },
2414 {8, 244, 2, 7 },
2415 {9, 245, 2, 2 },
2416 {10, 245, 2, 7 },
2417 {11, 246, 2, 2 },
2418 {12, 246, 2, 7 },
2419 {13, 247, 2, 2 },
2420 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02002421
2422 /* 802.11 UNI / HyperLan 2 */
2423 {36, 0x56, 0, 4},
2424 {38, 0x56, 0, 6},
2425 {40, 0x56, 0, 8},
2426 {44, 0x57, 0, 0},
2427 {46, 0x57, 0, 2},
2428 {48, 0x57, 0, 4},
2429 {52, 0x57, 0, 8},
2430 {54, 0x57, 0, 10},
2431 {56, 0x58, 0, 0},
2432 {60, 0x58, 0, 4},
2433 {62, 0x58, 0, 6},
2434 {64, 0x58, 0, 8},
2435
2436 /* 802.11 HyperLan 2 */
2437 {100, 0x5b, 0, 8},
2438 {102, 0x5b, 0, 10},
2439 {104, 0x5c, 0, 0},
2440 {108, 0x5c, 0, 4},
2441 {110, 0x5c, 0, 6},
2442 {112, 0x5c, 0, 8},
2443 {116, 0x5d, 0, 0},
2444 {118, 0x5d, 0, 2},
2445 {120, 0x5d, 0, 4},
2446 {124, 0x5d, 0, 8},
2447 {126, 0x5d, 0, 10},
2448 {128, 0x5e, 0, 0},
2449 {132, 0x5e, 0, 4},
2450 {134, 0x5e, 0, 6},
2451 {136, 0x5e, 0, 8},
2452 {140, 0x5f, 0, 0},
2453
2454 /* 802.11 UNII */
2455 {149, 0x5f, 0, 9},
2456 {151, 0x5f, 0, 11},
2457 {153, 0x60, 0, 1},
2458 {157, 0x60, 0, 5},
2459 {159, 0x60, 0, 7},
2460 {161, 0x60, 0, 9},
2461 {165, 0x61, 0, 1},
2462 {167, 0x61, 0, 3},
2463 {169, 0x61, 0, 5},
2464 {171, 0x61, 0, 7},
2465 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002466};
2467
2468int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
2469{
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002470 struct hw_mode_spec *spec = &rt2x00dev->spec;
2471 struct channel_info *info;
2472 char *tx_power1;
2473 char *tx_power2;
2474 unsigned int i;
2475 u16 eeprom;
2476
2477 /*
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002478 * Disable powersaving as default on PCI devices.
2479 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01002480 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002481 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
2482
2483 /*
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002484 * Initialize all hw fields.
2485 */
2486 rt2x00dev->hw->flags =
2487 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2488 IEEE80211_HW_SIGNAL_DBM |
2489 IEEE80211_HW_SUPPORTS_PS |
2490 IEEE80211_HW_PS_NULLFUNC_STACK;
2491
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002492 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
2493 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
2494 rt2x00_eeprom_addr(rt2x00dev,
2495 EEPROM_MAC_ADDR_0));
2496
2497 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2498
2499 /*
2500 * Initialize hw_mode information.
2501 */
2502 spec->supported_bands = SUPPORT_BAND_2GHZ;
2503 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
2504
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002505 if (rt2x00_rf(rt2x00dev, RF2820) ||
Ivo van Doorn55f93212010-05-06 14:45:46 +02002506 rt2x00_rf(rt2x00dev, RF2720)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002507 spec->num_channels = 14;
2508 spec->channels = rf_vals;
Ivo van Doorn55f93212010-05-06 14:45:46 +02002509 } else if (rt2x00_rf(rt2x00dev, RF2850) ||
2510 rt2x00_rf(rt2x00dev, RF2750)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002511 spec->supported_bands |= SUPPORT_BAND_5GHZ;
2512 spec->num_channels = ARRAY_SIZE(rf_vals);
2513 spec->channels = rf_vals;
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002514 } else if (rt2x00_rf(rt2x00dev, RF3020) ||
2515 rt2x00_rf(rt2x00dev, RF2020) ||
2516 rt2x00_rf(rt2x00dev, RF3021) ||
2517 rt2x00_rf(rt2x00dev, RF3022)) {
Ivo van Doorn55f93212010-05-06 14:45:46 +02002518 spec->num_channels = 14;
2519 spec->channels = rf_vals_3x;
2520 } else if (rt2x00_rf(rt2x00dev, RF3052)) {
2521 spec->supported_bands |= SUPPORT_BAND_5GHZ;
2522 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
2523 spec->channels = rf_vals_3x;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002524 }
2525
2526 /*
2527 * Initialize HT information.
2528 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002529 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01002530 spec->ht.ht_supported = true;
2531 else
2532 spec->ht.ht_supported = false;
2533
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002534 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02002535 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002536 IEEE80211_HT_CAP_GRN_FLD |
2537 IEEE80211_HT_CAP_SGI_20 |
2538 IEEE80211_HT_CAP_SGI_40 |
2539 IEEE80211_HT_CAP_TX_STBC |
Johannes Berg9a418af2009-12-17 13:55:48 +01002540 IEEE80211_HT_CAP_RX_STBC;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002541 spec->ht.ampdu_factor = 3;
2542 spec->ht.ampdu_density = 4;
2543 spec->ht.mcs.tx_params =
2544 IEEE80211_HT_MCS_TX_DEFINED |
2545 IEEE80211_HT_MCS_TX_RX_DIFF |
2546 ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
2547 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
2548
2549 switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
2550 case 3:
2551 spec->ht.mcs.rx_mask[2] = 0xff;
2552 case 2:
2553 spec->ht.mcs.rx_mask[1] = 0xff;
2554 case 1:
2555 spec->ht.mcs.rx_mask[0] = 0xff;
2556 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
2557 break;
2558 }
2559
2560 /*
2561 * Create channel information array
2562 */
2563 info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
2564 if (!info)
2565 return -ENOMEM;
2566
2567 spec->channels_info = info;
2568
2569 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
2570 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
2571
2572 for (i = 0; i < 14; i++) {
2573 info[i].tx_power1 = TXPOWER_G_FROM_DEV(tx_power1[i]);
2574 info[i].tx_power2 = TXPOWER_G_FROM_DEV(tx_power2[i]);
2575 }
2576
2577 if (spec->num_channels > 14) {
2578 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
2579 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
2580
2581 for (i = 14; i < spec->num_channels; i++) {
2582 info[i].tx_power1 = TXPOWER_A_FROM_DEV(tx_power1[i]);
2583 info[i].tx_power2 = TXPOWER_A_FROM_DEV(tx_power2[i]);
2584 }
2585 }
2586
2587 return 0;
2588}
2589EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
2590
2591/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002592 * IEEE80211 stack callback functions.
2593 */
2594static void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx,
2595 u32 *iv32, u16 *iv16)
2596{
2597 struct rt2x00_dev *rt2x00dev = hw->priv;
2598 struct mac_iveiv_entry iveiv_entry;
2599 u32 offset;
2600
2601 offset = MAC_IVEIV_ENTRY(hw_key_idx);
2602 rt2800_register_multiread(rt2x00dev, offset,
2603 &iveiv_entry, sizeof(iveiv_entry));
2604
Julia Lawall855da5e2009-12-13 17:07:45 +01002605 memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
2606 memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002607}
2608
2609static int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2610{
2611 struct rt2x00_dev *rt2x00dev = hw->priv;
2612 u32 reg;
2613 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
2614
2615 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
2616 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
2617 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
2618
2619 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
2620 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
2621 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
2622
2623 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
2624 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
2625 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
2626
2627 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
2628 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
2629 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
2630
2631 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
2632 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
2633 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
2634
2635 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
2636 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
2637 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
2638
2639 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
2640 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
2641 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
2642
2643 return 0;
2644}
2645
2646static int rt2800_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
2647 const struct ieee80211_tx_queue_params *params)
2648{
2649 struct rt2x00_dev *rt2x00dev = hw->priv;
2650 struct data_queue *queue;
2651 struct rt2x00_field32 field;
2652 int retval;
2653 u32 reg;
2654 u32 offset;
2655
2656 /*
2657 * First pass the configuration through rt2x00lib, that will
2658 * update the queue settings and validate the input. After that
2659 * we are free to update the registers based on the value
2660 * in the queue parameter.
2661 */
2662 retval = rt2x00mac_conf_tx(hw, queue_idx, params);
2663 if (retval)
2664 return retval;
2665
2666 /*
2667 * We only need to perform additional register initialization
2668 * for WMM queues/
2669 */
2670 if (queue_idx >= 4)
2671 return 0;
2672
2673 queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
2674
2675 /* Update WMM TXOP register */
2676 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
2677 field.bit_offset = (queue_idx & 1) * 16;
2678 field.bit_mask = 0xffff << field.bit_offset;
2679
2680 rt2800_register_read(rt2x00dev, offset, &reg);
2681 rt2x00_set_field32(&reg, field, queue->txop);
2682 rt2800_register_write(rt2x00dev, offset, reg);
2683
2684 /* Update WMM registers */
2685 field.bit_offset = queue_idx * 4;
2686 field.bit_mask = 0xf << field.bit_offset;
2687
2688 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
2689 rt2x00_set_field32(&reg, field, queue->aifs);
2690 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
2691
2692 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
2693 rt2x00_set_field32(&reg, field, queue->cw_min);
2694 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
2695
2696 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
2697 rt2x00_set_field32(&reg, field, queue->cw_max);
2698 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
2699
2700 /* Update EDCA registers */
2701 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
2702
2703 rt2800_register_read(rt2x00dev, offset, &reg);
2704 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
2705 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
2706 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
2707 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
2708 rt2800_register_write(rt2x00dev, offset, reg);
2709
2710 return 0;
2711}
2712
2713static u64 rt2800_get_tsf(struct ieee80211_hw *hw)
2714{
2715 struct rt2x00_dev *rt2x00dev = hw->priv;
2716 u64 tsf;
2717 u32 reg;
2718
2719 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
2720 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
2721 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
2722 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
2723
2724 return tsf;
2725}
2726
2727const struct ieee80211_ops rt2800_mac80211_ops = {
2728 .tx = rt2x00mac_tx,
2729 .start = rt2x00mac_start,
2730 .stop = rt2x00mac_stop,
2731 .add_interface = rt2x00mac_add_interface,
2732 .remove_interface = rt2x00mac_remove_interface,
2733 .config = rt2x00mac_config,
2734 .configure_filter = rt2x00mac_configure_filter,
2735 .set_tim = rt2x00mac_set_tim,
2736 .set_key = rt2x00mac_set_key,
2737 .get_stats = rt2x00mac_get_stats,
2738 .get_tkip_seq = rt2800_get_tkip_seq,
2739 .set_rts_threshold = rt2800_set_rts_threshold,
2740 .bss_info_changed = rt2x00mac_bss_info_changed,
2741 .conf_tx = rt2800_conf_tx,
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002742 .get_tsf = rt2800_get_tsf,
2743 .rfkill_poll = rt2x00mac_rfkill_poll,
2744};
2745EXPORT_SYMBOL_GPL(rt2800_mac80211_ops);