blob: f813b4388b63774a416e5291f519ebebb98e9ab2 [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);
Ivo van Doornefc7d362010-06-29 21:49:26 +0200102 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100103
104 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
105 }
106
107 mutex_unlock(&rt2x00dev->csr_mutex);
108}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100109
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100110static void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
111 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100112{
113 u32 reg;
114
115 mutex_lock(&rt2x00dev->csr_mutex);
116
117 /*
118 * Wait until the BBP becomes available, afterwards we
119 * can safely write the read request into the register.
120 * After the data has been written, we wait until hardware
121 * returns the correct value, if at any time the register
122 * doesn't become available in time, reg will be 0xffffffff
123 * which means we return 0xff to the caller.
124 */
125 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
126 reg = 0;
127 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
128 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
129 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
Ivo van Doornefc7d362010-06-29 21:49:26 +0200130 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100131
132 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
133
134 WAIT_FOR_BBP(rt2x00dev, &reg);
135 }
136
137 *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
138
139 mutex_unlock(&rt2x00dev->csr_mutex);
140}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100141
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100142static void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
143 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100144{
145 u32 reg;
146
147 mutex_lock(&rt2x00dev->csr_mutex);
148
149 /*
150 * Wait until the RFCSR becomes available, afterwards we
151 * can safely write the new data into the register.
152 */
153 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
154 reg = 0;
155 rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
156 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
157 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
158 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
159
160 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
161 }
162
163 mutex_unlock(&rt2x00dev->csr_mutex);
164}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100165
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100166static void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
167 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100168{
169 u32 reg;
170
171 mutex_lock(&rt2x00dev->csr_mutex);
172
173 /*
174 * Wait until the RFCSR becomes available, afterwards we
175 * can safely write the read request into the register.
176 * After the data has been written, we wait until hardware
177 * returns the correct value, if at any time the register
178 * doesn't become available in time, reg will be 0xffffffff
179 * which means we return 0xff to the caller.
180 */
181 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
182 reg = 0;
183 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
184 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
185 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
186
187 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
188
189 WAIT_FOR_RFCSR(rt2x00dev, &reg);
190 }
191
192 *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
193
194 mutex_unlock(&rt2x00dev->csr_mutex);
195}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100196
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100197static void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
198 const unsigned int word, const u32 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100199{
200 u32 reg;
201
202 mutex_lock(&rt2x00dev->csr_mutex);
203
204 /*
205 * Wait until the RF becomes available, afterwards we
206 * can safely write the new data into the register.
207 */
208 if (WAIT_FOR_RF(rt2x00dev, &reg)) {
209 reg = 0;
210 rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
211 rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
212 rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
213 rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
214
215 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
216 rt2x00_rf_write(rt2x00dev, word, value);
217 }
218
219 mutex_unlock(&rt2x00dev->csr_mutex);
220}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100221
222void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
223 const u8 command, const u8 token,
224 const u8 arg0, const u8 arg1)
225{
226 u32 reg;
227
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100228 /*
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100229 * SOC devices don't support MCU requests.
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100230 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100231 if (rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100232 return;
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100233
234 mutex_lock(&rt2x00dev->csr_mutex);
235
236 /*
237 * Wait until the MCU becomes available, afterwards we
238 * can safely write the new data into the register.
239 */
240 if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
241 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
242 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
243 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
244 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
245 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
246
247 reg = 0;
248 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
249 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
250 }
251
252 mutex_unlock(&rt2x00dev->csr_mutex);
253}
254EXPORT_SYMBOL_GPL(rt2800_mcu_request);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100255
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100256int rt2800_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
257{
258 unsigned int i;
259 u32 reg;
260
261 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
262 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
263 if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
264 !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
265 return 0;
266
267 msleep(1);
268 }
269
270 ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
271 return -EACCES;
272}
273EXPORT_SYMBOL_GPL(rt2800_wait_wpdma_ready);
274
Gertjan van Wingerde0b8004a2010-06-03 10:51:45 +0200275void rt2800_write_txwi(__le32 *txwi, struct txentry_desc *txdesc)
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200276{
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200277 u32 word;
278
279 /*
280 * Initialize TX Info descriptor
281 */
282 rt2x00_desc_read(txwi, 0, &word);
283 rt2x00_set_field32(&word, TXWI_W0_FRAG,
284 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
285 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS, 0);
286 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
287 rt2x00_set_field32(&word, TXWI_W0_TS,
288 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
289 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
290 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
291 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
292 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->txop);
293 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
294 rt2x00_set_field32(&word, TXWI_W0_BW,
295 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
296 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
297 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
298 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
299 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
300 rt2x00_desc_write(txwi, 0, word);
301
302 rt2x00_desc_read(txwi, 1, &word);
303 rt2x00_set_field32(&word, TXWI_W1_ACK,
304 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
305 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
306 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
307 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
308 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
309 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
310 txdesc->key_idx : 0xff);
311 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
312 txdesc->length);
313 rt2x00_set_field32(&word, TXWI_W1_PACKETID, txdesc->queue + 1);
314 rt2x00_desc_write(txwi, 1, word);
315
316 /*
317 * Always write 0 to IV/EIV fields, hardware will insert the IV
318 * from the IVEIV register when TXD_W3_WIV is set to 0.
319 * When TXD_W3_WIV is set to 1 it will use the IV data
320 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
321 * crypto entry in the registers should be used to encrypt the frame.
322 */
323 _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
324 _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
325}
326EXPORT_SYMBOL_GPL(rt2800_write_txwi);
327
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200328void rt2800_process_rxwi(struct sk_buff *skb, struct rxdone_entry_desc *rxdesc)
329{
330 __le32 *rxwi = (__le32 *) skb->data;
331 u32 word;
332
333 rt2x00_desc_read(rxwi, 0, &word);
334
335 rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
336 rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
337
338 rt2x00_desc_read(rxwi, 1, &word);
339
340 if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
341 rxdesc->flags |= RX_FLAG_SHORT_GI;
342
343 if (rt2x00_get_field32(word, RXWI_W1_BW))
344 rxdesc->flags |= RX_FLAG_40MHZ;
345
346 /*
347 * Detect RX rate, always use MCS as signal type.
348 */
349 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
350 rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
351 rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
352
353 /*
354 * Mask of 0x8 bit to remove the short preamble flag.
355 */
356 if (rxdesc->rate_mode == RATE_MODE_CCK)
357 rxdesc->signal &= ~0x8;
358
359 rt2x00_desc_read(rxwi, 2, &word);
360
361 rxdesc->rssi =
362 (rt2x00_get_field32(word, RXWI_W2_RSSI0) +
363 rt2x00_get_field32(word, RXWI_W2_RSSI1)) / 2;
364
365 /*
366 * Remove RXWI descriptor from start of buffer.
367 */
368 skb_pull(skb, RXWI_DESC_SIZE);
369}
370EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
371
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200372void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
373{
374 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
375 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
376 unsigned int beacon_base;
377 u32 reg;
378
379 /*
380 * Disable beaconing while we are reloading the beacon data,
381 * otherwise we might be sending out invalid data.
382 */
383 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
384 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
385 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
386
387 /*
388 * Add space for the TXWI in front of the skb.
389 */
390 skb_push(entry->skb, TXWI_DESC_SIZE);
391 memset(entry->skb, 0, TXWI_DESC_SIZE);
392
393 /*
394 * Register descriptor details in skb frame descriptor.
395 */
396 skbdesc->flags |= SKBDESC_DESC_IN_SKB;
397 skbdesc->desc = entry->skb->data;
398 skbdesc->desc_len = TXWI_DESC_SIZE;
399
400 /*
401 * Add the TXWI for the beacon to the skb.
402 */
403 rt2800_write_txwi((__le32 *)entry->skb->data, txdesc);
404
405 /*
406 * Dump beacon to userspace through debugfs.
407 */
408 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
409
410 /*
411 * Write entire beacon with TXWI to register.
412 */
413 beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
414 rt2800_register_multiwrite(rt2x00dev, beacon_base,
415 entry->skb->data, entry->skb->len);
416
417 /*
418 * Enable beaconing again.
419 */
420 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
421 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
422 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
423 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
424
425 /*
426 * Clean up beacon skb.
427 */
428 dev_kfree_skb_any(entry->skb);
429 entry->skb = NULL;
430}
431EXPORT_SYMBOL(rt2800_write_beacon);
432
Helmut Schaafdb87252010-06-29 21:48:06 +0200433static void inline rt2800_clear_beacon(struct rt2x00_dev *rt2x00dev,
434 unsigned int beacon_base)
435{
436 int i;
437
438 /*
439 * For the Beacon base registers we only need to clear
440 * the whole TXWI which (when set to 0) will invalidate
441 * the entire beacon.
442 */
443 for (i = 0; i < TXWI_DESC_SIZE; i += sizeof(__le32))
444 rt2800_register_write(rt2x00dev, beacon_base + i, 0);
445}
446
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100447#ifdef CONFIG_RT2X00_LIB_DEBUGFS
448const struct rt2x00debug rt2800_rt2x00debug = {
449 .owner = THIS_MODULE,
450 .csr = {
451 .read = rt2800_register_read,
452 .write = rt2800_register_write,
453 .flags = RT2X00DEBUGFS_OFFSET,
454 .word_base = CSR_REG_BASE,
455 .word_size = sizeof(u32),
456 .word_count = CSR_REG_SIZE / sizeof(u32),
457 },
458 .eeprom = {
459 .read = rt2x00_eeprom_read,
460 .write = rt2x00_eeprom_write,
461 .word_base = EEPROM_BASE,
462 .word_size = sizeof(u16),
463 .word_count = EEPROM_SIZE / sizeof(u16),
464 },
465 .bbp = {
466 .read = rt2800_bbp_read,
467 .write = rt2800_bbp_write,
468 .word_base = BBP_BASE,
469 .word_size = sizeof(u8),
470 .word_count = BBP_SIZE / sizeof(u8),
471 },
472 .rf = {
473 .read = rt2x00_rf_read,
474 .write = rt2800_rf_write,
475 .word_base = RF_BASE,
476 .word_size = sizeof(u32),
477 .word_count = RF_SIZE / sizeof(u32),
478 },
479};
480EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
481#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
482
483int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
484{
485 u32 reg;
486
487 rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
488 return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
489}
490EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
491
492#ifdef CONFIG_RT2X00_LIB_LEDS
493static void rt2800_brightness_set(struct led_classdev *led_cdev,
494 enum led_brightness brightness)
495{
496 struct rt2x00_led *led =
497 container_of(led_cdev, struct rt2x00_led, led_dev);
498 unsigned int enabled = brightness != LED_OFF;
499 unsigned int bg_mode =
500 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
501 unsigned int polarity =
502 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
503 EEPROM_FREQ_LED_POLARITY);
504 unsigned int ledmode =
505 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
506 EEPROM_FREQ_LED_MODE);
507
508 if (led->type == LED_TYPE_RADIO) {
509 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
510 enabled ? 0x20 : 0);
511 } else if (led->type == LED_TYPE_ASSOC) {
512 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
513 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
514 } else if (led->type == LED_TYPE_QUALITY) {
515 /*
516 * The brightness is divided into 6 levels (0 - 5),
517 * The specs tell us the following levels:
518 * 0, 1 ,3, 7, 15, 31
519 * to determine the level in a simple way we can simply
520 * work with bitshifting:
521 * (1 << level) - 1
522 */
523 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
524 (1 << brightness / (LED_FULL / 6)) - 1,
525 polarity);
526 }
527}
528
529static int rt2800_blink_set(struct led_classdev *led_cdev,
530 unsigned long *delay_on, unsigned long *delay_off)
531{
532 struct rt2x00_led *led =
533 container_of(led_cdev, struct rt2x00_led, led_dev);
534 u32 reg;
535
536 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
537 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
538 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100539 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
540
541 return 0;
542}
543
Gertjan van Wingerdeb3579d62009-12-30 11:36:34 +0100544static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100545 struct rt2x00_led *led, enum led_type type)
546{
547 led->rt2x00dev = rt2x00dev;
548 led->type = type;
549 led->led_dev.brightness_set = rt2800_brightness_set;
550 led->led_dev.blink_set = rt2800_blink_set;
551 led->flags = LED_INITIALIZED;
552}
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100553#endif /* CONFIG_RT2X00_LIB_LEDS */
554
555/*
556 * Configuration handlers.
557 */
558static void rt2800_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
559 struct rt2x00lib_crypto *crypto,
560 struct ieee80211_key_conf *key)
561{
562 struct mac_wcid_entry wcid_entry;
563 struct mac_iveiv_entry iveiv_entry;
564 u32 offset;
565 u32 reg;
566
567 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
568
Ivo van Doorne4a0ab32010-06-14 22:14:19 +0200569 if (crypto->cmd == SET_KEY) {
570 rt2800_register_read(rt2x00dev, offset, &reg);
571 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
572 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
573 /*
574 * Both the cipher as the BSS Idx numbers are split in a main
575 * value of 3 bits, and a extended field for adding one additional
576 * bit to the value.
577 */
578 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
579 (crypto->cipher & 0x7));
580 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
581 (crypto->cipher & 0x8) >> 3);
582 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
583 (crypto->bssidx & 0x7));
584 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
585 (crypto->bssidx & 0x8) >> 3);
586 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
587 rt2800_register_write(rt2x00dev, offset, reg);
588 } else {
589 rt2800_register_write(rt2x00dev, offset, 0);
590 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100591
592 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
593
594 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
595 if ((crypto->cipher == CIPHER_TKIP) ||
596 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
597 (crypto->cipher == CIPHER_AES))
598 iveiv_entry.iv[3] |= 0x20;
599 iveiv_entry.iv[3] |= key->keyidx << 6;
600 rt2800_register_multiwrite(rt2x00dev, offset,
601 &iveiv_entry, sizeof(iveiv_entry));
602
603 offset = MAC_WCID_ENTRY(key->hw_key_idx);
604
605 memset(&wcid_entry, 0, sizeof(wcid_entry));
606 if (crypto->cmd == SET_KEY)
607 memcpy(&wcid_entry, crypto->address, ETH_ALEN);
608 rt2800_register_multiwrite(rt2x00dev, offset,
609 &wcid_entry, sizeof(wcid_entry));
610}
611
612int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
613 struct rt2x00lib_crypto *crypto,
614 struct ieee80211_key_conf *key)
615{
616 struct hw_key_entry key_entry;
617 struct rt2x00_field32 field;
618 u32 offset;
619 u32 reg;
620
621 if (crypto->cmd == SET_KEY) {
622 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
623
624 memcpy(key_entry.key, crypto->key,
625 sizeof(key_entry.key));
626 memcpy(key_entry.tx_mic, crypto->tx_mic,
627 sizeof(key_entry.tx_mic));
628 memcpy(key_entry.rx_mic, crypto->rx_mic,
629 sizeof(key_entry.rx_mic));
630
631 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
632 rt2800_register_multiwrite(rt2x00dev, offset,
633 &key_entry, sizeof(key_entry));
634 }
635
636 /*
637 * The cipher types are stored over multiple registers
638 * starting with SHARED_KEY_MODE_BASE each word will have
639 * 32 bits and contains the cipher types for 2 bssidx each.
640 * Using the correct defines correctly will cause overhead,
641 * so just calculate the correct offset.
642 */
643 field.bit_offset = 4 * (key->hw_key_idx % 8);
644 field.bit_mask = 0x7 << field.bit_offset;
645
646 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
647
648 rt2800_register_read(rt2x00dev, offset, &reg);
649 rt2x00_set_field32(&reg, field,
650 (crypto->cmd == SET_KEY) * crypto->cipher);
651 rt2800_register_write(rt2x00dev, offset, reg);
652
653 /*
654 * Update WCID information
655 */
656 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
657
658 return 0;
659}
660EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
661
662int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
663 struct rt2x00lib_crypto *crypto,
664 struct ieee80211_key_conf *key)
665{
666 struct hw_key_entry key_entry;
667 u32 offset;
668
669 if (crypto->cmd == SET_KEY) {
670 /*
671 * 1 pairwise key is possible per AID, this means that the AID
672 * equals our hw_key_idx. Make sure the WCID starts _after_ the
673 * last possible shared key entry.
674 */
675 if (crypto->aid > (256 - 32))
676 return -ENOSPC;
677
678 key->hw_key_idx = 32 + crypto->aid;
679
680 memcpy(key_entry.key, crypto->key,
681 sizeof(key_entry.key));
682 memcpy(key_entry.tx_mic, crypto->tx_mic,
683 sizeof(key_entry.tx_mic));
684 memcpy(key_entry.rx_mic, crypto->rx_mic,
685 sizeof(key_entry.rx_mic));
686
687 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
688 rt2800_register_multiwrite(rt2x00dev, offset,
689 &key_entry, sizeof(key_entry));
690 }
691
692 /*
693 * Update WCID information
694 */
695 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
696
697 return 0;
698}
699EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
700
701void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
702 const unsigned int filter_flags)
703{
704 u32 reg;
705
706 /*
707 * Start configuration steps.
708 * Note that the version error will always be dropped
709 * and broadcast frames will always be accepted since
710 * there is no filter for it at this time.
711 */
712 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
713 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
714 !(filter_flags & FIF_FCSFAIL));
715 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
716 !(filter_flags & FIF_PLCPFAIL));
717 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
718 !(filter_flags & FIF_PROMISC_IN_BSS));
719 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
720 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
721 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
722 !(filter_flags & FIF_ALLMULTI));
723 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
724 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
725 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
726 !(filter_flags & FIF_CONTROL));
727 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
728 !(filter_flags & FIF_CONTROL));
729 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
730 !(filter_flags & FIF_CONTROL));
731 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
732 !(filter_flags & FIF_CONTROL));
733 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
734 !(filter_flags & FIF_CONTROL));
735 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
736 !(filter_flags & FIF_PSPOLL));
737 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
738 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
739 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
740 !(filter_flags & FIF_CONTROL));
741 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
742}
743EXPORT_SYMBOL_GPL(rt2800_config_filter);
744
745void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
746 struct rt2x00intf_conf *conf, const unsigned int flags)
747{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100748 u32 reg;
749
750 if (flags & CONFIG_UPDATE_TYPE) {
751 /*
752 * Clear current synchronisation setup.
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100753 */
Helmut Schaafdb87252010-06-29 21:48:06 +0200754 rt2800_clear_beacon(rt2x00dev,
755 HW_BEACON_OFFSET(intf->beacon->entry_idx));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100756 /*
757 * Enable synchronisation.
758 */
759 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
760 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
761 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Josef Bacik6a62e5ef2009-11-15 21:33:18 -0500762 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE,
763 (conf->sync == TSF_SYNC_BEACON));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100764 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
765 }
766
767 if (flags & CONFIG_UPDATE_MAC) {
768 reg = le32_to_cpu(conf->mac[1]);
769 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
770 conf->mac[1] = cpu_to_le32(reg);
771
772 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
773 conf->mac, sizeof(conf->mac));
774 }
775
776 if (flags & CONFIG_UPDATE_BSSID) {
777 reg = le32_to_cpu(conf->bssid[1]);
Ivo van Doornd440cb92010-06-29 21:45:31 +0200778 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
779 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 7);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100780 conf->bssid[1] = cpu_to_le32(reg);
781
782 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
783 conf->bssid, sizeof(conf->bssid));
784 }
785}
786EXPORT_SYMBOL_GPL(rt2800_config_intf);
787
788void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp)
789{
790 u32 reg;
791
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100792 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
793 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
794 !!erp->short_preamble);
795 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
796 !!erp->short_preamble);
797 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
798
799 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
800 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
801 erp->cts_protection ? 2 : 0);
802 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
803
804 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
805 erp->basic_rates);
806 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
807
808 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
809 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, erp->slot_time);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100810 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
811
812 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100813 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100814 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
815
816 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
817 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
818 erp->beacon_int * 16);
819 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
820}
821EXPORT_SYMBOL_GPL(rt2800_config_erp);
822
823void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
824{
825 u8 r1;
826 u8 r3;
827
828 rt2800_bbp_read(rt2x00dev, 1, &r1);
829 rt2800_bbp_read(rt2x00dev, 3, &r3);
830
831 /*
832 * Configure the TX antenna.
833 */
834 switch ((int)ant->tx) {
835 case 1:
836 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100837 break;
838 case 2:
839 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
840 break;
841 case 3:
Ivo van Doorne22557f2010-06-29 21:49:05 +0200842 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100843 break;
844 }
845
846 /*
847 * Configure the RX antenna.
848 */
849 switch ((int)ant->rx) {
850 case 1:
851 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
852 break;
853 case 2:
854 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
855 break;
856 case 3:
857 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
858 break;
859 }
860
861 rt2800_bbp_write(rt2x00dev, 3, r3);
862 rt2800_bbp_write(rt2x00dev, 1, r1);
863}
864EXPORT_SYMBOL_GPL(rt2800_config_ant);
865
866static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
867 struct rt2x00lib_conf *libconf)
868{
869 u16 eeprom;
870 short lna_gain;
871
872 if (libconf->rf.channel <= 14) {
873 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
874 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
875 } else if (libconf->rf.channel <= 64) {
876 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
877 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
878 } else if (libconf->rf.channel <= 128) {
879 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
880 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
881 } else {
882 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
883 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
884 }
885
886 rt2x00dev->lna_gain = lna_gain;
887}
888
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200889static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
890 struct ieee80211_conf *conf,
891 struct rf_channel *rf,
892 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100893{
894 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
895
896 if (rt2x00dev->default_ant.tx == 1)
897 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
898
899 if (rt2x00dev->default_ant.rx == 1) {
900 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
901 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
902 } else if (rt2x00dev->default_ant.rx == 2)
903 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
904
905 if (rf->channel > 14) {
906 /*
907 * When TX power is below 0, we should increase it by 7 to
908 * make it a positive value (Minumum value is -7).
909 * However this means that values between 0 and 7 have
910 * double meaning, and we should set a 7DBm boost flag.
911 */
912 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
913 (info->tx_power1 >= 0));
914
915 if (info->tx_power1 < 0)
916 info->tx_power1 += 7;
917
918 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A,
919 TXPOWER_A_TO_DEV(info->tx_power1));
920
921 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
922 (info->tx_power2 >= 0));
923
924 if (info->tx_power2 < 0)
925 info->tx_power2 += 7;
926
927 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A,
928 TXPOWER_A_TO_DEV(info->tx_power2));
929 } else {
930 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G,
931 TXPOWER_G_TO_DEV(info->tx_power1));
932 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G,
933 TXPOWER_G_TO_DEV(info->tx_power2));
934 }
935
936 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
937
938 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
939 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
940 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
941 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
942
943 udelay(200);
944
945 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
946 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
947 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
948 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
949
950 udelay(200);
951
952 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
953 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
954 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
955 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
956}
957
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200958static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
959 struct ieee80211_conf *conf,
960 struct rf_channel *rf,
961 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100962{
963 u8 rfcsr;
964
965 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Gertjan van Wingerde41a26172009-11-09 22:59:04 +0100966 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100967
968 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +0200969 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100970 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
971
972 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
973 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
974 TXPOWER_G_TO_DEV(info->tx_power1));
975 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
976
Helmut Schaa5a673962010-04-23 15:54:43 +0200977 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
978 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
979 TXPOWER_G_TO_DEV(info->tx_power2));
980 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
981
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100982 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
983 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
984 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
985
986 rt2800_rfcsr_write(rt2x00dev, 24,
987 rt2x00dev->calibration[conf_is_ht40(conf)]);
988
Gertjan van Wingerde71976902010-03-24 21:42:36 +0100989 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100990 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +0100991 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100992}
993
994static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
995 struct ieee80211_conf *conf,
996 struct rf_channel *rf,
997 struct channel_info *info)
998{
999 u32 reg;
1000 unsigned int tx_pin;
1001 u8 bbp;
1002
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001003 if (rt2x00_rf(rt2x00dev, RF2020) ||
1004 rt2x00_rf(rt2x00dev, RF3020) ||
1005 rt2x00_rf(rt2x00dev, RF3021) ||
1006 rt2x00_rf(rt2x00dev, RF3022))
1007 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerdefa6f6322009-11-09 22:59:58 +01001008 else
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001009 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001010
1011 /*
1012 * Change BBP settings
1013 */
1014 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
1015 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
1016 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
1017 rt2800_bbp_write(rt2x00dev, 86, 0);
1018
1019 if (rf->channel <= 14) {
1020 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
1021 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1022 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1023 } else {
1024 rt2800_bbp_write(rt2x00dev, 82, 0x84);
1025 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1026 }
1027 } else {
1028 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
1029
1030 if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
1031 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1032 else
1033 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1034 }
1035
1036 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001037 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001038 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
1039 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
1040 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
1041
1042 tx_pin = 0;
1043
1044 /* Turn on unused PA or LNA when not using 1T or 1R */
1045 if (rt2x00dev->default_ant.tx != 1) {
1046 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
1047 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
1048 }
1049
1050 /* Turn on unused PA or LNA when not using 1T or 1R */
1051 if (rt2x00dev->default_ant.rx != 1) {
1052 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
1053 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
1054 }
1055
1056 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
1057 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
1058 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
1059 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
1060 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
1061 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
1062
1063 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
1064
1065 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1066 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
1067 rt2800_bbp_write(rt2x00dev, 4, bbp);
1068
1069 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001070 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001071 rt2800_bbp_write(rt2x00dev, 3, bbp);
1072
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001073 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001074 if (conf_is_ht40(conf)) {
1075 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
1076 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1077 rt2800_bbp_write(rt2x00dev, 73, 0x16);
1078 } else {
1079 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1080 rt2800_bbp_write(rt2x00dev, 70, 0x08);
1081 rt2800_bbp_write(rt2x00dev, 73, 0x11);
1082 }
1083 }
1084
1085 msleep(1);
1086}
1087
1088static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
1089 const int txpower)
1090{
1091 u32 reg;
1092 u32 value = TXPOWER_G_TO_DEV(txpower);
1093 u8 r1;
1094
1095 rt2800_bbp_read(rt2x00dev, 1, &r1);
Helmut Schaaa3f84ca2010-06-14 22:11:32 +02001096 rt2x00_set_field8(&r1, BBP1_TX_POWER, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001097 rt2800_bbp_write(rt2x00dev, 1, r1);
1098
1099 rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
1100 rt2x00_set_field32(&reg, TX_PWR_CFG_0_1MBS, value);
1101 rt2x00_set_field32(&reg, TX_PWR_CFG_0_2MBS, value);
1102 rt2x00_set_field32(&reg, TX_PWR_CFG_0_55MBS, value);
1103 rt2x00_set_field32(&reg, TX_PWR_CFG_0_11MBS, value);
1104 rt2x00_set_field32(&reg, TX_PWR_CFG_0_6MBS, value);
1105 rt2x00_set_field32(&reg, TX_PWR_CFG_0_9MBS, value);
1106 rt2x00_set_field32(&reg, TX_PWR_CFG_0_12MBS, value);
1107 rt2x00_set_field32(&reg, TX_PWR_CFG_0_18MBS, value);
1108 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0, reg);
1109
1110 rt2800_register_read(rt2x00dev, TX_PWR_CFG_1, &reg);
1111 rt2x00_set_field32(&reg, TX_PWR_CFG_1_24MBS, value);
1112 rt2x00_set_field32(&reg, TX_PWR_CFG_1_36MBS, value);
1113 rt2x00_set_field32(&reg, TX_PWR_CFG_1_48MBS, value);
1114 rt2x00_set_field32(&reg, TX_PWR_CFG_1_54MBS, value);
1115 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS0, value);
1116 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS1, value);
1117 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS2, value);
1118 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS3, value);
1119 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1, reg);
1120
1121 rt2800_register_read(rt2x00dev, TX_PWR_CFG_2, &reg);
1122 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS4, value);
1123 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS5, value);
1124 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS6, value);
1125 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS7, value);
1126 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS8, value);
1127 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS9, value);
1128 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS10, value);
1129 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS11, value);
1130 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2, reg);
1131
1132 rt2800_register_read(rt2x00dev, TX_PWR_CFG_3, &reg);
1133 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS12, value);
1134 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS13, value);
1135 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS14, value);
1136 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS15, value);
1137 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN1, value);
1138 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN2, value);
1139 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN3, value);
1140 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN4, value);
1141 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3, reg);
1142
1143 rt2800_register_read(rt2x00dev, TX_PWR_CFG_4, &reg);
1144 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN5, value);
1145 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN6, value);
1146 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN7, value);
1147 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN8, value);
1148 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4, reg);
1149}
1150
1151static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
1152 struct rt2x00lib_conf *libconf)
1153{
1154 u32 reg;
1155
1156 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1157 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
1158 libconf->conf->short_frame_max_tx_count);
1159 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
1160 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001161 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1162}
1163
1164static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
1165 struct rt2x00lib_conf *libconf)
1166{
1167 enum dev_state state =
1168 (libconf->conf->flags & IEEE80211_CONF_PS) ?
1169 STATE_SLEEP : STATE_AWAKE;
1170 u32 reg;
1171
1172 if (state == STATE_SLEEP) {
1173 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
1174
1175 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1176 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
1177 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
1178 libconf->conf->listen_interval - 1);
1179 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
1180 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
1181
1182 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
1183 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001184 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1185 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
1186 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
1187 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
1188 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02001189
1190 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001191 }
1192}
1193
1194void rt2800_config(struct rt2x00_dev *rt2x00dev,
1195 struct rt2x00lib_conf *libconf,
1196 const unsigned int flags)
1197{
1198 /* Always recalculate LNA gain before changing configuration */
1199 rt2800_config_lna_gain(rt2x00dev, libconf);
1200
1201 if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
1202 rt2800_config_channel(rt2x00dev, libconf->conf,
1203 &libconf->rf, &libconf->channel);
1204 if (flags & IEEE80211_CONF_CHANGE_POWER)
1205 rt2800_config_txpower(rt2x00dev, libconf->conf->power_level);
1206 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
1207 rt2800_config_retry_limit(rt2x00dev, libconf);
1208 if (flags & IEEE80211_CONF_CHANGE_PS)
1209 rt2800_config_ps(rt2x00dev, libconf);
1210}
1211EXPORT_SYMBOL_GPL(rt2800_config);
1212
1213/*
1214 * Link tuning
1215 */
1216void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1217{
1218 u32 reg;
1219
1220 /*
1221 * Update FCS error count from register.
1222 */
1223 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1224 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
1225}
1226EXPORT_SYMBOL_GPL(rt2800_link_stats);
1227
1228static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
1229{
1230 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001231 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001232 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001233 rt2x00_rt(rt2x00dev, RT3090) ||
1234 rt2x00_rt(rt2x00dev, RT3390))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001235 return 0x1c + (2 * rt2x00dev->lna_gain);
1236 else
1237 return 0x2e + rt2x00dev->lna_gain;
1238 }
1239
1240 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
1241 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
1242 else
1243 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
1244}
1245
1246static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
1247 struct link_qual *qual, u8 vgc_level)
1248{
1249 if (qual->vgc_level != vgc_level) {
1250 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
1251 qual->vgc_level = vgc_level;
1252 qual->vgc_level_reg = vgc_level;
1253 }
1254}
1255
1256void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1257{
1258 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
1259}
1260EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
1261
1262void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
1263 const u32 count)
1264{
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001265 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001266 return;
1267
1268 /*
1269 * When RSSI is better then -80 increase VGC level with 0x10
1270 */
1271 rt2800_set_vgc(rt2x00dev, qual,
1272 rt2800_get_default_vgc(rt2x00dev) +
1273 ((qual->rssi > -80) * 0x10));
1274}
1275EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001276
1277/*
1278 * Initialization functions.
1279 */
1280int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
1281{
1282 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001283 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001284 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001285 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001286
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001287 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1288 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1289 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1290 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1291 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1292 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
1293 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1294
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001295 ret = rt2800_drv_init_registers(rt2x00dev);
1296 if (ret)
1297 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001298
1299 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
1300 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
1301 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
1302 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
1303 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
1304 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
1305
1306 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
1307 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
1308 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
1309 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
1310 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
1311 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
1312
1313 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
1314 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1315
1316 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1317
1318 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1319 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 0);
1320 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
1321 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
1322 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
1323 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1324 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
1325 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1326
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001327 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
1328
1329 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1330 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
1331 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
1332 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
1333
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001334 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001335 rt2x00_rt(rt2x00dev, RT3090) ||
1336 rt2x00_rt(rt2x00dev, RT3390)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001337 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1338 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001339 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001340 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
1341 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001342 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1343 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
1344 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1345 0x0000002c);
1346 else
1347 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1348 0x0000000f);
1349 } else {
1350 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1351 }
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001352 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001353 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001354
1355 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
1356 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1357 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
1358 } else {
1359 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1360 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1361 }
Helmut Schaac295a812010-06-03 10:52:13 +02001362 } else if (rt2800_is_305x_soc(rt2x00dev)) {
1363 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1364 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1365 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000001f);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001366 } else {
1367 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
1368 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1369 }
1370
1371 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
1372 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
1373 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
1374 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
1375 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
1376 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
1377 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
1378 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
1379 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
1380 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
1381
1382 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
1383 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001384 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001385 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
1386 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
1387
1388 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
1389 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001390 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01001391 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001392 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001393 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
1394 else
1395 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
1396 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
1397 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
1398 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
1399
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001400 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1401 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
1402 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
1403 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
1404 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
1405 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
1406 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
1407 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
1408 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1409
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001410 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
1411
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001412 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1413 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
1414 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
1415 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
1416 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
1417 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
1418 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
1419 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1420
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001421 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1422 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001423 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001424 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
1425 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001426 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001427 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
1428 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
1429 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1430
1431 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001432 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001433 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
1434 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
1435 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1436 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1437 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001438 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001439 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001440 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1441 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001442 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
1443
1444 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001445 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001446 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
1447 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
1448 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1449 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1450 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001451 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001452 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001453 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1454 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001455 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1456
1457 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1458 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
1459 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
1460 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
1461 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1462 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1463 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1464 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1465 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1466 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001467 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001468 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1469
1470 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1471 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001472 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL,
1473 !rt2x00_is_usb(rt2x00dev));
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001474 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
1475 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1476 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1477 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1478 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1479 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1480 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001481 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001482 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1483
1484 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1485 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
1486 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
1487 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
1488 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1489 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1490 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1491 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1492 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1493 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001494 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001495 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1496
1497 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1498 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
1499 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
1500 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
1501 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1502 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1503 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1504 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1505 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1506 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001507 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001508 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1509
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001510 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001511 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
1512
1513 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1514 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1515 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1516 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1517 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1518 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
1519 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
1520 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
1521 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
1522 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
1523 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1524 }
1525
1526 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, 0x0000583f);
1527 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
1528
1529 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
1530 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
1531 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
1532 IEEE80211_MAX_RTS_THRESHOLD);
1533 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
1534 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
1535
1536 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001537
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001538 /*
1539 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
1540 * time should be set to 16. However, the original Ralink driver uses
1541 * 16 for both and indeed using a value of 10 for CCK SIFS results in
1542 * connection problems with 11g + CTS protection. Hence, use the same
1543 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
1544 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001545 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001546 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
1547 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001548 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
1549 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
1550 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
1551 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1552
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001553 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
1554
1555 /*
1556 * ASIC will keep garbage value after boot, clear encryption keys.
1557 */
1558 for (i = 0; i < 4; i++)
1559 rt2800_register_write(rt2x00dev,
1560 SHARED_KEY_MODE_ENTRY(i), 0);
1561
1562 for (i = 0; i < 256; i++) {
1563 u32 wcid[2] = { 0xffffffff, 0x00ffffff };
1564 rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
1565 wcid, sizeof(wcid));
1566
1567 rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
1568 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
1569 }
1570
1571 /*
1572 * Clear all beacons
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001573 */
Helmut Schaafdb87252010-06-29 21:48:06 +02001574 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE0);
1575 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE1);
1576 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE2);
1577 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE3);
1578 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE4);
1579 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE5);
1580 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE6);
1581 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE7);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001582
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001583 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02001584 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
1585 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
1586 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001587 }
1588
1589 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
1590 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
1591 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
1592 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
1593 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
1594 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
1595 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
1596 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
1597 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
1598 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
1599
1600 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
1601 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
1602 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
1603 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
1604 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
1605 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
1606 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
1607 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
1608 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
1609 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
1610
1611 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
1612 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
1613 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
1614 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
1615 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
1616 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
1617 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
1618 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
1619 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
1620 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
1621
1622 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
1623 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
1624 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
1625 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
1626 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
1627 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
1628
1629 /*
1630 * We must clear the error counters.
1631 * These registers are cleared on read,
1632 * so we may pass a useless variable to store the value.
1633 */
1634 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1635 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
1636 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
1637 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
1638 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
1639 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
1640
1641 return 0;
1642}
1643EXPORT_SYMBOL_GPL(rt2800_init_registers);
1644
1645static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
1646{
1647 unsigned int i;
1648 u32 reg;
1649
1650 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1651 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
1652 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
1653 return 0;
1654
1655 udelay(REGISTER_BUSY_DELAY);
1656 }
1657
1658 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
1659 return -EACCES;
1660}
1661
1662static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
1663{
1664 unsigned int i;
1665 u8 value;
1666
1667 /*
1668 * BBP was enabled after firmware was loaded,
1669 * but we need to reactivate it now.
1670 */
1671 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
1672 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
1673 msleep(1);
1674
1675 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1676 rt2800_bbp_read(rt2x00dev, 0, &value);
1677 if ((value != 0xff) && (value != 0x00))
1678 return 0;
1679 udelay(REGISTER_BUSY_DELAY);
1680 }
1681
1682 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
1683 return -EACCES;
1684}
1685
1686int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
1687{
1688 unsigned int i;
1689 u16 eeprom;
1690 u8 reg_id;
1691 u8 value;
1692
1693 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
1694 rt2800_wait_bbp_ready(rt2x00dev)))
1695 return -EACCES;
1696
Helmut Schaabaff8002010-04-28 09:58:59 +02001697 if (rt2800_is_305x_soc(rt2x00dev))
1698 rt2800_bbp_write(rt2x00dev, 31, 0x08);
1699
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001700 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
1701 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001702
1703 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
1704 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1705 rt2800_bbp_write(rt2x00dev, 73, 0x12);
1706 } else {
1707 rt2800_bbp_write(rt2x00dev, 69, 0x12);
1708 rt2800_bbp_write(rt2x00dev, 73, 0x10);
1709 }
1710
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001711 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001712
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001713 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001714 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001715 rt2x00_rt(rt2x00dev, RT3090) ||
1716 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001717 rt2800_bbp_write(rt2x00dev, 79, 0x13);
1718 rt2800_bbp_write(rt2x00dev, 80, 0x05);
1719 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Helmut Schaabaff8002010-04-28 09:58:59 +02001720 } else if (rt2800_is_305x_soc(rt2x00dev)) {
1721 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
1722 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001723 } else {
1724 rt2800_bbp_write(rt2x00dev, 81, 0x37);
1725 }
1726
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001727 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1728 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001729
Gertjan van Wingerde5ed8f452010-06-03 10:51:57 +02001730 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001731 rt2800_bbp_write(rt2x00dev, 84, 0x19);
1732 else
1733 rt2800_bbp_write(rt2x00dev, 84, 0x99);
1734
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001735 rt2800_bbp_write(rt2x00dev, 86, 0x00);
1736 rt2800_bbp_write(rt2x00dev, 91, 0x04);
1737 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001738
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001739 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001740 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001741 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02001742 rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
1743 rt2800_is_305x_soc(rt2x00dev))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001744 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
1745 else
1746 rt2800_bbp_write(rt2x00dev, 103, 0x00);
1747
Helmut Schaabaff8002010-04-28 09:58:59 +02001748 if (rt2800_is_305x_soc(rt2x00dev))
1749 rt2800_bbp_write(rt2x00dev, 105, 0x01);
1750 else
1751 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001752 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001753
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001754 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001755 rt2x00_rt(rt2x00dev, RT3090) ||
1756 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001757 rt2800_bbp_read(rt2x00dev, 138, &value);
1758
1759 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
1760 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
1761 value |= 0x20;
1762 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
1763 value &= ~0x02;
1764
1765 rt2800_bbp_write(rt2x00dev, 138, value);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001766 }
1767
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001768
1769 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
1770 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
1771
1772 if (eeprom != 0xffff && eeprom != 0x0000) {
1773 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
1774 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
1775 rt2800_bbp_write(rt2x00dev, reg_id, value);
1776 }
1777 }
1778
1779 return 0;
1780}
1781EXPORT_SYMBOL_GPL(rt2800_init_bbp);
1782
1783static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
1784 bool bw40, u8 rfcsr24, u8 filter_target)
1785{
1786 unsigned int i;
1787 u8 bbp;
1788 u8 rfcsr;
1789 u8 passband;
1790 u8 stopband;
1791 u8 overtuned = 0;
1792
1793 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1794
1795 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1796 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
1797 rt2800_bbp_write(rt2x00dev, 4, bbp);
1798
1799 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
1800 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
1801 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
1802
1803 /*
1804 * Set power & frequency of passband test tone
1805 */
1806 rt2800_bbp_write(rt2x00dev, 24, 0);
1807
1808 for (i = 0; i < 100; i++) {
1809 rt2800_bbp_write(rt2x00dev, 25, 0x90);
1810 msleep(1);
1811
1812 rt2800_bbp_read(rt2x00dev, 55, &passband);
1813 if (passband)
1814 break;
1815 }
1816
1817 /*
1818 * Set power & frequency of stopband test tone
1819 */
1820 rt2800_bbp_write(rt2x00dev, 24, 0x06);
1821
1822 for (i = 0; i < 100; i++) {
1823 rt2800_bbp_write(rt2x00dev, 25, 0x90);
1824 msleep(1);
1825
1826 rt2800_bbp_read(rt2x00dev, 55, &stopband);
1827
1828 if ((passband - stopband) <= filter_target) {
1829 rfcsr24++;
1830 overtuned += ((passband - stopband) == filter_target);
1831 } else
1832 break;
1833
1834 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1835 }
1836
1837 rfcsr24 -= !!overtuned;
1838
1839 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1840 return rfcsr24;
1841}
1842
1843int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
1844{
1845 u8 rfcsr;
1846 u8 bbp;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001847 u32 reg;
1848 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001849
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001850 if (!rt2x00_rt(rt2x00dev, RT3070) &&
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001851 !rt2x00_rt(rt2x00dev, RT3071) &&
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001852 !rt2x00_rt(rt2x00dev, RT3090) &&
Helmut Schaa23812382010-04-26 13:48:45 +02001853 !rt2x00_rt(rt2x00dev, RT3390) &&
Helmut Schaabaff8002010-04-28 09:58:59 +02001854 !rt2800_is_305x_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001855 return 0;
1856
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001857 /*
1858 * Init RF calibration.
1859 */
1860 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1861 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1862 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1863 msleep(1);
1864 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1865 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1866
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001867 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001868 rt2x00_rt(rt2x00dev, RT3071) ||
1869 rt2x00_rt(rt2x00dev, RT3090)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001870 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
1871 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
1872 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
1873 rt2800_rfcsr_write(rt2x00dev, 7, 0x70);
1874 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001875 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001876 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
1877 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
1878 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
1879 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
1880 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
1881 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
1882 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
1883 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
1884 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
1885 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
1886 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
1887 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001888 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001889 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
1890 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
1891 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
1892 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
1893 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001894 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001895 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
1896 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
1897 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
1898 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
1899 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
1900 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001901 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001902 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
1903 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001904 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001905 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
1906 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
1907 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
1908 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
1909 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
1910 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
1911 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001912 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001913 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001914 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001915 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
1916 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
1917 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
1918 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
1919 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
1920 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
1921 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Helmut Schaabaff8002010-04-28 09:58:59 +02001922 } else if (rt2800_is_305x_soc(rt2x00dev)) {
Helmut Schaa23812382010-04-26 13:48:45 +02001923 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
1924 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
1925 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
1926 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
1927 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
1928 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
1929 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
1930 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
1931 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
1932 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
1933 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
1934 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
1935 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
1936 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
1937 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
1938 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
1939 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
1940 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
1941 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
1942 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
1943 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
1944 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
1945 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
1946 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
1947 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
1948 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
1949 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
1950 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
1951 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
1952 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
Helmut Schaabaff8002010-04-28 09:58:59 +02001953 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
1954 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
1955 return 0;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001956 }
1957
1958 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
1959 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
1960 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
1961 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
1962 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001963 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
1964 rt2x00_rt(rt2x00dev, RT3090)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001965 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
1966 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
1967 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1968
1969 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
1970
1971 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
1972 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001973 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
1974 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001975 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1976 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
1977 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
1978 else
1979 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
1980 }
1981 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001982 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
1983 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
1984 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
1985 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001986 }
1987
1988 /*
1989 * Set RX Filter calibration for 20MHz and 40MHz
1990 */
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001991 if (rt2x00_rt(rt2x00dev, RT3070)) {
1992 rt2x00dev->calibration[0] =
1993 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
1994 rt2x00dev->calibration[1] =
1995 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001996 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001997 rt2x00_rt(rt2x00dev, RT3090) ||
1998 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001999 rt2x00dev->calibration[0] =
2000 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
2001 rt2x00dev->calibration[1] =
2002 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002003 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002004
2005 /*
2006 * Set back to initial state
2007 */
2008 rt2800_bbp_write(rt2x00dev, 24, 0);
2009
2010 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
2011 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
2012 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
2013
2014 /*
2015 * set BBP back to BW20
2016 */
2017 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2018 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
2019 rt2800_bbp_write(rt2x00dev, 4, bbp);
2020
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002021 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002022 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002023 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2024 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002025 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
2026
2027 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
2028 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
2029 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
2030
2031 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
2032 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002033 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002034 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2035 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerde8440c292010-06-03 10:52:02 +02002036 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002037 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
2038 }
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002039 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &eeprom);
2040 if (rt2x00_get_field16(eeprom, EEPROM_TXMIXER_GAIN_BG_VAL) >= 1)
2041 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
2042 rt2x00_get_field16(eeprom,
2043 EEPROM_TXMIXER_GAIN_BG_VAL));
2044 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
2045
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002046 if (rt2x00_rt(rt2x00dev, RT3090)) {
2047 rt2800_bbp_read(rt2x00dev, 138, &bbp);
2048
2049 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2050 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
2051 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
2052 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
2053 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
2054
2055 rt2800_bbp_write(rt2x00dev, 138, bbp);
2056 }
2057
2058 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002059 rt2x00_rt(rt2x00dev, RT3090) ||
2060 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002061 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2062 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2063 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2064 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2065 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2066 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2067 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2068
2069 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
2070 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
2071 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
2072
2073 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
2074 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
2075 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
2076
2077 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
2078 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
2079 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
2080 }
2081
2082 if (rt2x00_rt(rt2x00dev, RT3070) || rt2x00_rt(rt2x00dev, RT3071)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002083 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002084 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
2085 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002086 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
2087 else
2088 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
2089 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
2090 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
2091 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
2092 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
2093 }
2094
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002095 return 0;
2096}
2097EXPORT_SYMBOL_GPL(rt2800_init_rfcsr);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002098
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002099int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
2100{
2101 u32 reg;
2102
2103 rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
2104
2105 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
2106}
2107EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
2108
2109static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
2110{
2111 u32 reg;
2112
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002113 mutex_lock(&rt2x00dev->csr_mutex);
2114
2115 rt2800_register_read_lock(rt2x00dev, EFUSE_CTRL, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002116 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
2117 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
2118 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002119 rt2800_register_write_lock(rt2x00dev, EFUSE_CTRL, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002120
2121 /* Wait until the EEPROM has been loaded */
2122 rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
2123
2124 /* Apparently the data is read from end to start */
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002125 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA3,
2126 (u32 *)&rt2x00dev->eeprom[i]);
2127 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA2,
2128 (u32 *)&rt2x00dev->eeprom[i + 2]);
2129 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA1,
2130 (u32 *)&rt2x00dev->eeprom[i + 4]);
2131 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA0,
2132 (u32 *)&rt2x00dev->eeprom[i + 6]);
2133
2134 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002135}
2136
2137void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
2138{
2139 unsigned int i;
2140
2141 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
2142 rt2800_efuse_read(rt2x00dev, i);
2143}
2144EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
2145
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002146int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
2147{
2148 u16 word;
2149 u8 *mac;
2150 u8 default_lna_gain;
2151
2152 /*
2153 * Start validation of the data that has been read.
2154 */
2155 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
2156 if (!is_valid_ether_addr(mac)) {
2157 random_ether_addr(mac);
2158 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
2159 }
2160
2161 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
2162 if (word == 0xffff) {
2163 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2164 rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
2165 rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
2166 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2167 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002168 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02002169 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002170 /*
2171 * There is a max of 2 RX streams for RT28x0 series
2172 */
2173 if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
2174 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2175 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2176 }
2177
2178 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
2179 if (word == 0xffff) {
2180 rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
2181 rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
2182 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
2183 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
2184 rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
2185 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
2186 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
2187 rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
2188 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
2189 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002190 rt2x00_set_field16(&word, EEPROM_NIC_ANT_DIVERSITY, 0);
2191 rt2x00_set_field16(&word, EEPROM_NIC_DAC_TEST, 0);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002192 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
2193 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
2194 }
2195
2196 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
2197 if ((word & 0x00ff) == 0x00ff) {
2198 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002199 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2200 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
2201 }
2202 if ((word & 0xff00) == 0xff00) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002203 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
2204 LED_MODE_TXRX_ACTIVITY);
2205 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
2206 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2207 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
2208 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
2209 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002210 EEPROM(rt2x00dev, "Led Mode: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002211 }
2212
2213 /*
2214 * During the LNA validation we are going to use
2215 * lna0 as correct value. Note that EEPROM_LNA
2216 * is never validated.
2217 */
2218 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
2219 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
2220
2221 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
2222 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
2223 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
2224 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
2225 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
2226 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
2227
2228 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
2229 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
2230 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
2231 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
2232 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
2233 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
2234 default_lna_gain);
2235 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
2236
2237 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
2238 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
2239 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
2240 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
2241 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
2242 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
2243
2244 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
2245 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
2246 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
2247 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
2248 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
2249 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
2250 default_lna_gain);
2251 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
2252
2253 return 0;
2254}
2255EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
2256
2257int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
2258{
2259 u32 reg;
2260 u16 value;
2261 u16 eeprom;
2262
2263 /*
2264 * Read EEPROM word for configuration.
2265 */
2266 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2267
2268 /*
2269 * Identify RF chipset.
2270 */
2271 value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
2272 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
2273
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002274 rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
2275 value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
Gertjan van Wingerde714fa662010-02-13 20:55:48 +01002276
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002277 if (!rt2x00_rt(rt2x00dev, RT2860) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002278 !rt2x00_rt(rt2x00dev, RT2872) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002279 !rt2x00_rt(rt2x00dev, RT2883) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002280 !rt2x00_rt(rt2x00dev, RT3070) &&
2281 !rt2x00_rt(rt2x00dev, RT3071) &&
2282 !rt2x00_rt(rt2x00dev, RT3090) &&
2283 !rt2x00_rt(rt2x00dev, RT3390) &&
2284 !rt2x00_rt(rt2x00dev, RT3572)) {
2285 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
2286 return -ENODEV;
2287 }
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002288
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002289 if (!rt2x00_rf(rt2x00dev, RF2820) &&
2290 !rt2x00_rf(rt2x00dev, RF2850) &&
2291 !rt2x00_rf(rt2x00dev, RF2720) &&
2292 !rt2x00_rf(rt2x00dev, RF2750) &&
2293 !rt2x00_rf(rt2x00dev, RF3020) &&
2294 !rt2x00_rf(rt2x00dev, RF2020) &&
2295 !rt2x00_rf(rt2x00dev, RF3021) &&
Gertjan van Wingerde6c0fe262009-12-30 11:36:31 +01002296 !rt2x00_rf(rt2x00dev, RF3022) &&
2297 !rt2x00_rf(rt2x00dev, RF3052)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002298 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
2299 return -ENODEV;
2300 }
2301
2302 /*
2303 * Identify default antenna configuration.
2304 */
2305 rt2x00dev->default_ant.tx =
2306 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH);
2307 rt2x00dev->default_ant.rx =
2308 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH);
2309
2310 /*
2311 * Read frequency offset and RF programming sequence.
2312 */
2313 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
2314 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
2315
2316 /*
2317 * Read external LNA informations.
2318 */
2319 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2320
2321 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
2322 __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
2323 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
2324 __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
2325
2326 /*
2327 * Detect if this device has an hardware controlled radio.
2328 */
2329 if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
2330 __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
2331
2332 /*
2333 * Store led settings, for correct led behaviour.
2334 */
2335#ifdef CONFIG_RT2X00_LIB_LEDS
2336 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
2337 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
2338 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
2339
2340 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &rt2x00dev->led_mcu_reg);
2341#endif /* CONFIG_RT2X00_LIB_LEDS */
2342
2343 return 0;
2344}
2345EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
2346
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002347/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002348 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002349 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
2350 */
2351static const struct rf_channel rf_vals[] = {
2352 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
2353 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
2354 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
2355 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
2356 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
2357 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
2358 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
2359 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
2360 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
2361 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
2362 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
2363 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
2364 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
2365 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
2366
2367 /* 802.11 UNI / HyperLan 2 */
2368 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
2369 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
2370 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
2371 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
2372 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
2373 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
2374 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
2375 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
2376 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
2377 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
2378 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
2379 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
2380
2381 /* 802.11 HyperLan 2 */
2382 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
2383 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
2384 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
2385 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
2386 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
2387 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
2388 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
2389 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
2390 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
2391 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
2392 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
2393 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
2394 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
2395 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
2396 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
2397 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
2398
2399 /* 802.11 UNII */
2400 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
2401 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
2402 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
2403 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
2404 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
2405 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
2406 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
2407 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
2408 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
2409 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
2410 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
2411
2412 /* 802.11 Japan */
2413 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
2414 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
2415 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
2416 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
2417 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
2418 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
2419 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
2420};
2421
2422/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002423 * RF value list for rt3xxx
2424 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002425 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02002426static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002427 {1, 241, 2, 2 },
2428 {2, 241, 2, 7 },
2429 {3, 242, 2, 2 },
2430 {4, 242, 2, 7 },
2431 {5, 243, 2, 2 },
2432 {6, 243, 2, 7 },
2433 {7, 244, 2, 2 },
2434 {8, 244, 2, 7 },
2435 {9, 245, 2, 2 },
2436 {10, 245, 2, 7 },
2437 {11, 246, 2, 2 },
2438 {12, 246, 2, 7 },
2439 {13, 247, 2, 2 },
2440 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02002441
2442 /* 802.11 UNI / HyperLan 2 */
2443 {36, 0x56, 0, 4},
2444 {38, 0x56, 0, 6},
2445 {40, 0x56, 0, 8},
2446 {44, 0x57, 0, 0},
2447 {46, 0x57, 0, 2},
2448 {48, 0x57, 0, 4},
2449 {52, 0x57, 0, 8},
2450 {54, 0x57, 0, 10},
2451 {56, 0x58, 0, 0},
2452 {60, 0x58, 0, 4},
2453 {62, 0x58, 0, 6},
2454 {64, 0x58, 0, 8},
2455
2456 /* 802.11 HyperLan 2 */
2457 {100, 0x5b, 0, 8},
2458 {102, 0x5b, 0, 10},
2459 {104, 0x5c, 0, 0},
2460 {108, 0x5c, 0, 4},
2461 {110, 0x5c, 0, 6},
2462 {112, 0x5c, 0, 8},
2463 {116, 0x5d, 0, 0},
2464 {118, 0x5d, 0, 2},
2465 {120, 0x5d, 0, 4},
2466 {124, 0x5d, 0, 8},
2467 {126, 0x5d, 0, 10},
2468 {128, 0x5e, 0, 0},
2469 {132, 0x5e, 0, 4},
2470 {134, 0x5e, 0, 6},
2471 {136, 0x5e, 0, 8},
2472 {140, 0x5f, 0, 0},
2473
2474 /* 802.11 UNII */
2475 {149, 0x5f, 0, 9},
2476 {151, 0x5f, 0, 11},
2477 {153, 0x60, 0, 1},
2478 {157, 0x60, 0, 5},
2479 {159, 0x60, 0, 7},
2480 {161, 0x60, 0, 9},
2481 {165, 0x61, 0, 1},
2482 {167, 0x61, 0, 3},
2483 {169, 0x61, 0, 5},
2484 {171, 0x61, 0, 7},
2485 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002486};
2487
2488int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
2489{
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002490 struct hw_mode_spec *spec = &rt2x00dev->spec;
2491 struct channel_info *info;
2492 char *tx_power1;
2493 char *tx_power2;
2494 unsigned int i;
2495 u16 eeprom;
2496
2497 /*
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002498 * Disable powersaving as default on PCI devices.
2499 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01002500 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002501 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
2502
2503 /*
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002504 * Initialize all hw fields.
2505 */
2506 rt2x00dev->hw->flags =
2507 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2508 IEEE80211_HW_SIGNAL_DBM |
2509 IEEE80211_HW_SUPPORTS_PS |
Helmut Schaa1df90802010-06-29 21:38:12 +02002510 IEEE80211_HW_PS_NULLFUNC_STACK |
2511 IEEE80211_HW_AMPDU_AGGREGATION;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002512
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002513 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
2514 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
2515 rt2x00_eeprom_addr(rt2x00dev,
2516 EEPROM_MAC_ADDR_0));
2517
Helmut Schaa3f2bee22010-06-14 22:12:01 +02002518 /*
2519 * As rt2800 has a global fallback table we cannot specify
2520 * more then one tx rate per frame but since the hw will
2521 * try several rates (based on the fallback table) we should
2522 * still initialize max_rates to the maximum number of rates
2523 * we are going to try. Otherwise mac80211 will truncate our
2524 * reported tx rates and the rc algortihm will end up with
2525 * incorrect data.
2526 */
2527 rt2x00dev->hw->max_rates = 7;
2528 rt2x00dev->hw->max_rate_tries = 1;
2529
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002530 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2531
2532 /*
2533 * Initialize hw_mode information.
2534 */
2535 spec->supported_bands = SUPPORT_BAND_2GHZ;
2536 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
2537
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002538 if (rt2x00_rf(rt2x00dev, RF2820) ||
Ivo van Doorn55f93212010-05-06 14:45:46 +02002539 rt2x00_rf(rt2x00dev, RF2720)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002540 spec->num_channels = 14;
2541 spec->channels = rf_vals;
Ivo van Doorn55f93212010-05-06 14:45:46 +02002542 } else if (rt2x00_rf(rt2x00dev, RF2850) ||
2543 rt2x00_rf(rt2x00dev, RF2750)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002544 spec->supported_bands |= SUPPORT_BAND_5GHZ;
2545 spec->num_channels = ARRAY_SIZE(rf_vals);
2546 spec->channels = rf_vals;
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002547 } else if (rt2x00_rf(rt2x00dev, RF3020) ||
2548 rt2x00_rf(rt2x00dev, RF2020) ||
2549 rt2x00_rf(rt2x00dev, RF3021) ||
2550 rt2x00_rf(rt2x00dev, RF3022)) {
Ivo van Doorn55f93212010-05-06 14:45:46 +02002551 spec->num_channels = 14;
2552 spec->channels = rf_vals_3x;
2553 } else if (rt2x00_rf(rt2x00dev, RF3052)) {
2554 spec->supported_bands |= SUPPORT_BAND_5GHZ;
2555 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
2556 spec->channels = rf_vals_3x;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002557 }
2558
2559 /*
2560 * Initialize HT information.
2561 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002562 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01002563 spec->ht.ht_supported = true;
2564 else
2565 spec->ht.ht_supported = false;
2566
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002567 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02002568 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002569 IEEE80211_HT_CAP_GRN_FLD |
2570 IEEE80211_HT_CAP_SGI_20 |
Ivo van Doornaa674632010-06-29 21:48:37 +02002571 IEEE80211_HT_CAP_SGI_40;
Helmut Schaa22cabaa2010-06-03 10:52:10 +02002572
2573 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) >= 2)
2574 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
2575
Ivo van Doornaa674632010-06-29 21:48:37 +02002576 spec->ht.cap |=
2577 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) <<
2578 IEEE80211_HT_CAP_RX_STBC_SHIFT;
2579
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002580 spec->ht.ampdu_factor = 3;
2581 spec->ht.ampdu_density = 4;
2582 spec->ht.mcs.tx_params =
2583 IEEE80211_HT_MCS_TX_DEFINED |
2584 IEEE80211_HT_MCS_TX_RX_DIFF |
2585 ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
2586 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
2587
2588 switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
2589 case 3:
2590 spec->ht.mcs.rx_mask[2] = 0xff;
2591 case 2:
2592 spec->ht.mcs.rx_mask[1] = 0xff;
2593 case 1:
2594 spec->ht.mcs.rx_mask[0] = 0xff;
2595 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
2596 break;
2597 }
2598
2599 /*
2600 * Create channel information array
2601 */
2602 info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
2603 if (!info)
2604 return -ENOMEM;
2605
2606 spec->channels_info = info;
2607
2608 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
2609 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
2610
2611 for (i = 0; i < 14; i++) {
2612 info[i].tx_power1 = TXPOWER_G_FROM_DEV(tx_power1[i]);
2613 info[i].tx_power2 = TXPOWER_G_FROM_DEV(tx_power2[i]);
2614 }
2615
2616 if (spec->num_channels > 14) {
2617 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
2618 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
2619
2620 for (i = 14; i < spec->num_channels; i++) {
2621 info[i].tx_power1 = TXPOWER_A_FROM_DEV(tx_power1[i]);
2622 info[i].tx_power2 = TXPOWER_A_FROM_DEV(tx_power2[i]);
2623 }
2624 }
2625
2626 return 0;
2627}
2628EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
2629
2630/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002631 * IEEE80211 stack callback functions.
2632 */
2633static void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx,
2634 u32 *iv32, u16 *iv16)
2635{
2636 struct rt2x00_dev *rt2x00dev = hw->priv;
2637 struct mac_iveiv_entry iveiv_entry;
2638 u32 offset;
2639
2640 offset = MAC_IVEIV_ENTRY(hw_key_idx);
2641 rt2800_register_multiread(rt2x00dev, offset,
2642 &iveiv_entry, sizeof(iveiv_entry));
2643
Julia Lawall855da5e2009-12-13 17:07:45 +01002644 memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
2645 memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002646}
2647
2648static int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2649{
2650 struct rt2x00_dev *rt2x00dev = hw->priv;
2651 u32 reg;
2652 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
2653
2654 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
2655 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
2656 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
2657
2658 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
2659 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
2660 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
2661
2662 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
2663 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
2664 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
2665
2666 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
2667 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
2668 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
2669
2670 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
2671 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
2672 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
2673
2674 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
2675 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
2676 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
2677
2678 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
2679 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
2680 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
2681
2682 return 0;
2683}
2684
2685static int rt2800_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
2686 const struct ieee80211_tx_queue_params *params)
2687{
2688 struct rt2x00_dev *rt2x00dev = hw->priv;
2689 struct data_queue *queue;
2690 struct rt2x00_field32 field;
2691 int retval;
2692 u32 reg;
2693 u32 offset;
2694
2695 /*
2696 * First pass the configuration through rt2x00lib, that will
2697 * update the queue settings and validate the input. After that
2698 * we are free to update the registers based on the value
2699 * in the queue parameter.
2700 */
2701 retval = rt2x00mac_conf_tx(hw, queue_idx, params);
2702 if (retval)
2703 return retval;
2704
2705 /*
2706 * We only need to perform additional register initialization
2707 * for WMM queues/
2708 */
2709 if (queue_idx >= 4)
2710 return 0;
2711
2712 queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
2713
2714 /* Update WMM TXOP register */
2715 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
2716 field.bit_offset = (queue_idx & 1) * 16;
2717 field.bit_mask = 0xffff << field.bit_offset;
2718
2719 rt2800_register_read(rt2x00dev, offset, &reg);
2720 rt2x00_set_field32(&reg, field, queue->txop);
2721 rt2800_register_write(rt2x00dev, offset, reg);
2722
2723 /* Update WMM registers */
2724 field.bit_offset = queue_idx * 4;
2725 field.bit_mask = 0xf << field.bit_offset;
2726
2727 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
2728 rt2x00_set_field32(&reg, field, queue->aifs);
2729 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
2730
2731 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
2732 rt2x00_set_field32(&reg, field, queue->cw_min);
2733 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
2734
2735 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
2736 rt2x00_set_field32(&reg, field, queue->cw_max);
2737 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
2738
2739 /* Update EDCA registers */
2740 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
2741
2742 rt2800_register_read(rt2x00dev, offset, &reg);
2743 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
2744 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
2745 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
2746 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
2747 rt2800_register_write(rt2x00dev, offset, reg);
2748
2749 return 0;
2750}
2751
2752static u64 rt2800_get_tsf(struct ieee80211_hw *hw)
2753{
2754 struct rt2x00_dev *rt2x00dev = hw->priv;
2755 u64 tsf;
2756 u32 reg;
2757
2758 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
2759 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
2760 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
2761 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
2762
2763 return tsf;
2764}
2765
Helmut Schaa1df90802010-06-29 21:38:12 +02002766static int rt2800_ampdu_action(struct ieee80211_hw *hw,
2767 struct ieee80211_vif *vif,
2768 enum ieee80211_ampdu_mlme_action action,
2769 struct ieee80211_sta *sta,
2770 u16 tid, u16 *ssn)
2771{
2772 struct rt2x00_dev *rt2x00dev = hw->priv;
2773 int ret = 0;
2774
2775 switch (action) {
2776 case IEEE80211_AMPDU_RX_START:
2777 case IEEE80211_AMPDU_RX_STOP:
2778 /* we don't support RX aggregation yet */
2779 ret = -ENOTSUPP;
2780 break;
2781 case IEEE80211_AMPDU_TX_START:
2782 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2783 break;
2784 case IEEE80211_AMPDU_TX_STOP:
2785 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2786 break;
2787 case IEEE80211_AMPDU_TX_OPERATIONAL:
2788 break;
2789 default:
2790 WARNING(rt2x00dev, "Unknown AMPDU action\n");
2791 }
2792
2793 return ret;
2794}
2795
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002796const struct ieee80211_ops rt2800_mac80211_ops = {
2797 .tx = rt2x00mac_tx,
2798 .start = rt2x00mac_start,
2799 .stop = rt2x00mac_stop,
2800 .add_interface = rt2x00mac_add_interface,
2801 .remove_interface = rt2x00mac_remove_interface,
2802 .config = rt2x00mac_config,
2803 .configure_filter = rt2x00mac_configure_filter,
2804 .set_tim = rt2x00mac_set_tim,
2805 .set_key = rt2x00mac_set_key,
2806 .get_stats = rt2x00mac_get_stats,
2807 .get_tkip_seq = rt2800_get_tkip_seq,
2808 .set_rts_threshold = rt2800_set_rts_threshold,
2809 .bss_info_changed = rt2x00mac_bss_info_changed,
2810 .conf_tx = rt2800_conf_tx,
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002811 .get_tsf = rt2800_get_tsf,
2812 .rfkill_poll = rt2x00mac_rfkill_poll,
Helmut Schaa1df90802010-06-29 21:38:12 +02002813 .ampdu_action = rt2800_ampdu_action,
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002814};
2815EXPORT_SYMBOL_GPL(rt2800_mac80211_ops);
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02002816
2817MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
2818MODULE_VERSION(DRV_VERSION);
2819MODULE_DESCRIPTION("Ralink RT2800 library");
2820MODULE_LICENSE("GPL");