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