blob: 0cf7796cdff52889dc3306bd5c147429e5186d10 [file] [log] [blame]
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +01001/*
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02002 Copyright (C) 2010 Ivo van Doorn <IvDoorn@gmail.com>
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01003 Copyright (C) 2009 Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
Gertjan van Wingerdecce5fc42009-11-10 22:42:40 +01004 Copyright (C) 2009 Gertjan van Wingerde <gwingerde@gmail.com>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +01005
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01006 Based on the original rt2800pci.c and rt2800usb.c.
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01007 Copyright (C) 2009 Alban Browaeys <prahal@yahoo.com>
8 Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
9 Copyright (C) 2009 Luis Correia <luis.f.correia@gmail.com>
10 Copyright (C) 2009 Mattias Nissler <mattias.nissler@gmx.de>
11 Copyright (C) 2009 Mark Asselstine <asselsm@gmail.com>
12 Copyright (C) 2009 Xose Vazquez Perez <xose.vazquez@gmail.com>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010013 <http://rt2x00.serialmonkey.com>
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the
27 Free Software Foundation, Inc.,
28 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30
31/*
32 Module: rt2800lib
33 Abstract: rt2800 generic device routines.
34 */
35
36#include <linux/kernel.h>
37#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010039
40#include "rt2x00.h"
41#include "rt2800lib.h"
42#include "rt2800.h"
43
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010044/*
45 * Register access.
46 * All access to the CSR registers will go through the methods
47 * rt2800_register_read and rt2800_register_write.
48 * BBP and RF register require indirect register access,
49 * and use the CSR registers BBPCSR and RFCSR to achieve this.
50 * These indirect registers work with busy bits,
51 * and we will try maximal REGISTER_BUSY_COUNT times to access
52 * the register while taking a REGISTER_BUSY_DELAY us delay
53 * between each attampt. When the busy bit is still set at that time,
54 * the access attempt is considered to have failed,
55 * and we will print an error.
56 * The _lock versions must be used if you already hold the csr_mutex
57 */
58#define WAIT_FOR_BBP(__dev, __reg) \
59 rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
60#define WAIT_FOR_RFCSR(__dev, __reg) \
61 rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
62#define WAIT_FOR_RF(__dev, __reg) \
63 rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
64#define WAIT_FOR_MCU(__dev, __reg) \
65 rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
66 H2M_MAILBOX_CSR_OWNER, (__reg))
67
Helmut Schaabaff8002010-04-28 09:58:59 +020068static inline bool rt2800_is_305x_soc(struct rt2x00_dev *rt2x00dev)
69{
70 /* check for rt2872 on SoC */
71 if (!rt2x00_is_soc(rt2x00dev) ||
72 !rt2x00_rt(rt2x00dev, RT2872))
73 return false;
74
75 /* we know for sure that these rf chipsets are used on rt305x boards */
76 if (rt2x00_rf(rt2x00dev, RF3020) ||
77 rt2x00_rf(rt2x00dev, RF3021) ||
78 rt2x00_rf(rt2x00dev, RF3022))
79 return true;
80
81 NOTICE(rt2x00dev, "Unknown RF chipset on rt305x\n");
82 return false;
83}
84
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010085static void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
86 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010087{
88 u32 reg;
89
90 mutex_lock(&rt2x00dev->csr_mutex);
91
92 /*
93 * Wait until the BBP becomes available, afterwards we
94 * can safely write the new data into the register.
95 */
96 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
97 reg = 0;
98 rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
99 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
100 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
101 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100102 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100103 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
104
105 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
106 }
107
108 mutex_unlock(&rt2x00dev->csr_mutex);
109}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100110
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100111static void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
112 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100113{
114 u32 reg;
115
116 mutex_lock(&rt2x00dev->csr_mutex);
117
118 /*
119 * Wait until the BBP becomes available, afterwards we
120 * can safely write the read request into the register.
121 * After the data has been written, we wait until hardware
122 * returns the correct value, if at any time the register
123 * doesn't become available in time, reg will be 0xffffffff
124 * which means we return 0xff to the caller.
125 */
126 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
127 reg = 0;
128 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
129 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
130 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100131 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100132 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
133
134 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
135
136 WAIT_FOR_BBP(rt2x00dev, &reg);
137 }
138
139 *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
140
141 mutex_unlock(&rt2x00dev->csr_mutex);
142}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100143
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100144static void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
145 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100146{
147 u32 reg;
148
149 mutex_lock(&rt2x00dev->csr_mutex);
150
151 /*
152 * Wait until the RFCSR becomes available, afterwards we
153 * can safely write the new data into the register.
154 */
155 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
156 reg = 0;
157 rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
158 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
159 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
160 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
161
162 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
163 }
164
165 mutex_unlock(&rt2x00dev->csr_mutex);
166}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100167
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100168static void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
169 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100170{
171 u32 reg;
172
173 mutex_lock(&rt2x00dev->csr_mutex);
174
175 /*
176 * Wait until the RFCSR becomes available, afterwards we
177 * can safely write the read request into the register.
178 * After the data has been written, we wait until hardware
179 * returns the correct value, if at any time the register
180 * doesn't become available in time, reg will be 0xffffffff
181 * which means we return 0xff to the caller.
182 */
183 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
184 reg = 0;
185 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
186 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
187 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
188
189 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
190
191 WAIT_FOR_RFCSR(rt2x00dev, &reg);
192 }
193
194 *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
195
196 mutex_unlock(&rt2x00dev->csr_mutex);
197}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100198
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100199static void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
200 const unsigned int word, const u32 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100201{
202 u32 reg;
203
204 mutex_lock(&rt2x00dev->csr_mutex);
205
206 /*
207 * Wait until the RF becomes available, afterwards we
208 * can safely write the new data into the register.
209 */
210 if (WAIT_FOR_RF(rt2x00dev, &reg)) {
211 reg = 0;
212 rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
213 rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
214 rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
215 rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
216
217 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
218 rt2x00_rf_write(rt2x00dev, word, value);
219 }
220
221 mutex_unlock(&rt2x00dev->csr_mutex);
222}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100223
224void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
225 const u8 command, const u8 token,
226 const u8 arg0, const u8 arg1)
227{
228 u32 reg;
229
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100230 /*
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100231 * SOC devices don't support MCU requests.
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100232 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100233 if (rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100234 return;
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100235
236 mutex_lock(&rt2x00dev->csr_mutex);
237
238 /*
239 * Wait until the MCU becomes available, afterwards we
240 * can safely write the new data into the register.
241 */
242 if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
243 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
244 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
245 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
246 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
247 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
248
249 reg = 0;
250 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
251 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
252 }
253
254 mutex_unlock(&rt2x00dev->csr_mutex);
255}
256EXPORT_SYMBOL_GPL(rt2800_mcu_request);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100257
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100258int rt2800_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
259{
260 unsigned int i;
261 u32 reg;
262
263 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
264 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
265 if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
266 !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
267 return 0;
268
269 msleep(1);
270 }
271
272 ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
273 return -EACCES;
274}
275EXPORT_SYMBOL_GPL(rt2800_wait_wpdma_ready);
276
Gertjan van Wingerde0b8004a2010-06-03 10:51:45 +0200277void rt2800_write_txwi(__le32 *txwi, struct txentry_desc *txdesc)
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200278{
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200279 u32 word;
280
281 /*
282 * Initialize TX Info descriptor
283 */
284 rt2x00_desc_read(txwi, 0, &word);
285 rt2x00_set_field32(&word, TXWI_W0_FRAG,
286 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
287 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS, 0);
288 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
289 rt2x00_set_field32(&word, TXWI_W0_TS,
290 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
291 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
292 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
293 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
294 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->txop);
295 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
296 rt2x00_set_field32(&word, TXWI_W0_BW,
297 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
298 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
299 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
300 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
301 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
302 rt2x00_desc_write(txwi, 0, word);
303
304 rt2x00_desc_read(txwi, 1, &word);
305 rt2x00_set_field32(&word, TXWI_W1_ACK,
306 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
307 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
308 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
309 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
310 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
311 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
312 txdesc->key_idx : 0xff);
313 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
314 txdesc->length);
315 rt2x00_set_field32(&word, TXWI_W1_PACKETID, txdesc->queue + 1);
316 rt2x00_desc_write(txwi, 1, word);
317
318 /*
319 * Always write 0 to IV/EIV fields, hardware will insert the IV
320 * from the IVEIV register when TXD_W3_WIV is set to 0.
321 * When TXD_W3_WIV is set to 1 it will use the IV data
322 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
323 * crypto entry in the registers should be used to encrypt the frame.
324 */
325 _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
326 _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
327}
328EXPORT_SYMBOL_GPL(rt2800_write_txwi);
329
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200330void rt2800_process_rxwi(struct sk_buff *skb, struct rxdone_entry_desc *rxdesc)
331{
332 __le32 *rxwi = (__le32 *) skb->data;
333 u32 word;
334
335 rt2x00_desc_read(rxwi, 0, &word);
336
337 rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
338 rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
339
340 rt2x00_desc_read(rxwi, 1, &word);
341
342 if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
343 rxdesc->flags |= RX_FLAG_SHORT_GI;
344
345 if (rt2x00_get_field32(word, RXWI_W1_BW))
346 rxdesc->flags |= RX_FLAG_40MHZ;
347
348 /*
349 * Detect RX rate, always use MCS as signal type.
350 */
351 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
352 rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
353 rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
354
355 /*
356 * Mask of 0x8 bit to remove the short preamble flag.
357 */
358 if (rxdesc->rate_mode == RATE_MODE_CCK)
359 rxdesc->signal &= ~0x8;
360
361 rt2x00_desc_read(rxwi, 2, &word);
362
363 rxdesc->rssi =
364 (rt2x00_get_field32(word, RXWI_W2_RSSI0) +
365 rt2x00_get_field32(word, RXWI_W2_RSSI1)) / 2;
366
367 /*
368 * Remove RXWI descriptor from start of buffer.
369 */
370 skb_pull(skb, RXWI_DESC_SIZE);
371}
372EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
373
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200374void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
375{
376 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
377 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
378 unsigned int beacon_base;
379 u32 reg;
380
381 /*
382 * Disable beaconing while we are reloading the beacon data,
383 * otherwise we might be sending out invalid data.
384 */
385 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
386 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
387 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
388
389 /*
390 * Add space for the TXWI in front of the skb.
391 */
392 skb_push(entry->skb, TXWI_DESC_SIZE);
393 memset(entry->skb, 0, TXWI_DESC_SIZE);
394
395 /*
396 * Register descriptor details in skb frame descriptor.
397 */
398 skbdesc->flags |= SKBDESC_DESC_IN_SKB;
399 skbdesc->desc = entry->skb->data;
400 skbdesc->desc_len = TXWI_DESC_SIZE;
401
402 /*
403 * Add the TXWI for the beacon to the skb.
404 */
405 rt2800_write_txwi((__le32 *)entry->skb->data, txdesc);
406
407 /*
408 * Dump beacon to userspace through debugfs.
409 */
410 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
411
412 /*
413 * Write entire beacon with TXWI to register.
414 */
415 beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
416 rt2800_register_multiwrite(rt2x00dev, beacon_base,
417 entry->skb->data, entry->skb->len);
418
419 /*
420 * Enable beaconing again.
421 */
422 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
423 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
424 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
425 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
426
427 /*
428 * Clean up beacon skb.
429 */
430 dev_kfree_skb_any(entry->skb);
431 entry->skb = NULL;
432}
433EXPORT_SYMBOL(rt2800_write_beacon);
434
Helmut Schaafdb87252010-06-29 21:48:06 +0200435static void inline rt2800_clear_beacon(struct rt2x00_dev *rt2x00dev,
436 unsigned int beacon_base)
437{
438 int i;
439
440 /*
441 * For the Beacon base registers we only need to clear
442 * the whole TXWI which (when set to 0) will invalidate
443 * the entire beacon.
444 */
445 for (i = 0; i < TXWI_DESC_SIZE; i += sizeof(__le32))
446 rt2800_register_write(rt2x00dev, beacon_base + i, 0);
447}
448
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100449#ifdef CONFIG_RT2X00_LIB_DEBUGFS
450const struct rt2x00debug rt2800_rt2x00debug = {
451 .owner = THIS_MODULE,
452 .csr = {
453 .read = rt2800_register_read,
454 .write = rt2800_register_write,
455 .flags = RT2X00DEBUGFS_OFFSET,
456 .word_base = CSR_REG_BASE,
457 .word_size = sizeof(u32),
458 .word_count = CSR_REG_SIZE / sizeof(u32),
459 },
460 .eeprom = {
461 .read = rt2x00_eeprom_read,
462 .write = rt2x00_eeprom_write,
463 .word_base = EEPROM_BASE,
464 .word_size = sizeof(u16),
465 .word_count = EEPROM_SIZE / sizeof(u16),
466 },
467 .bbp = {
468 .read = rt2800_bbp_read,
469 .write = rt2800_bbp_write,
470 .word_base = BBP_BASE,
471 .word_size = sizeof(u8),
472 .word_count = BBP_SIZE / sizeof(u8),
473 },
474 .rf = {
475 .read = rt2x00_rf_read,
476 .write = rt2800_rf_write,
477 .word_base = RF_BASE,
478 .word_size = sizeof(u32),
479 .word_count = RF_SIZE / sizeof(u32),
480 },
481};
482EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
483#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
484
485int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
486{
487 u32 reg;
488
489 rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
490 return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
491}
492EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
493
494#ifdef CONFIG_RT2X00_LIB_LEDS
495static void rt2800_brightness_set(struct led_classdev *led_cdev,
496 enum led_brightness brightness)
497{
498 struct rt2x00_led *led =
499 container_of(led_cdev, struct rt2x00_led, led_dev);
500 unsigned int enabled = brightness != LED_OFF;
501 unsigned int bg_mode =
502 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
503 unsigned int polarity =
504 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
505 EEPROM_FREQ_LED_POLARITY);
506 unsigned int ledmode =
507 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
508 EEPROM_FREQ_LED_MODE);
509
510 if (led->type == LED_TYPE_RADIO) {
511 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
512 enabled ? 0x20 : 0);
513 } else if (led->type == LED_TYPE_ASSOC) {
514 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
515 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
516 } else if (led->type == LED_TYPE_QUALITY) {
517 /*
518 * The brightness is divided into 6 levels (0 - 5),
519 * The specs tell us the following levels:
520 * 0, 1 ,3, 7, 15, 31
521 * to determine the level in a simple way we can simply
522 * work with bitshifting:
523 * (1 << level) - 1
524 */
525 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
526 (1 << brightness / (LED_FULL / 6)) - 1,
527 polarity);
528 }
529}
530
531static int rt2800_blink_set(struct led_classdev *led_cdev,
532 unsigned long *delay_on, unsigned long *delay_off)
533{
534 struct rt2x00_led *led =
535 container_of(led_cdev, struct rt2x00_led, led_dev);
536 u32 reg;
537
538 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
539 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
540 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100541 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
542
543 return 0;
544}
545
Gertjan van Wingerdeb3579d62009-12-30 11:36:34 +0100546static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100547 struct rt2x00_led *led, enum led_type type)
548{
549 led->rt2x00dev = rt2x00dev;
550 led->type = type;
551 led->led_dev.brightness_set = rt2800_brightness_set;
552 led->led_dev.blink_set = rt2800_blink_set;
553 led->flags = LED_INITIALIZED;
554}
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100555#endif /* CONFIG_RT2X00_LIB_LEDS */
556
557/*
558 * Configuration handlers.
559 */
560static void rt2800_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
561 struct rt2x00lib_crypto *crypto,
562 struct ieee80211_key_conf *key)
563{
564 struct mac_wcid_entry wcid_entry;
565 struct mac_iveiv_entry iveiv_entry;
566 u32 offset;
567 u32 reg;
568
569 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
570
Ivo van Doorne4a0ab32010-06-14 22:14:19 +0200571 if (crypto->cmd == SET_KEY) {
572 rt2800_register_read(rt2x00dev, offset, &reg);
573 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
574 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
575 /*
576 * Both the cipher as the BSS Idx numbers are split in a main
577 * value of 3 bits, and a extended field for adding one additional
578 * bit to the value.
579 */
580 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
581 (crypto->cipher & 0x7));
582 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
583 (crypto->cipher & 0x8) >> 3);
584 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
585 (crypto->bssidx & 0x7));
586 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
587 (crypto->bssidx & 0x8) >> 3);
588 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
589 rt2800_register_write(rt2x00dev, offset, reg);
590 } else {
591 rt2800_register_write(rt2x00dev, offset, 0);
592 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100593
594 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
595
596 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
597 if ((crypto->cipher == CIPHER_TKIP) ||
598 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
599 (crypto->cipher == CIPHER_AES))
600 iveiv_entry.iv[3] |= 0x20;
601 iveiv_entry.iv[3] |= key->keyidx << 6;
602 rt2800_register_multiwrite(rt2x00dev, offset,
603 &iveiv_entry, sizeof(iveiv_entry));
604
605 offset = MAC_WCID_ENTRY(key->hw_key_idx);
606
607 memset(&wcid_entry, 0, sizeof(wcid_entry));
608 if (crypto->cmd == SET_KEY)
609 memcpy(&wcid_entry, crypto->address, ETH_ALEN);
610 rt2800_register_multiwrite(rt2x00dev, offset,
611 &wcid_entry, sizeof(wcid_entry));
612}
613
614int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
615 struct rt2x00lib_crypto *crypto,
616 struct ieee80211_key_conf *key)
617{
618 struct hw_key_entry key_entry;
619 struct rt2x00_field32 field;
620 u32 offset;
621 u32 reg;
622
623 if (crypto->cmd == SET_KEY) {
624 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
625
626 memcpy(key_entry.key, crypto->key,
627 sizeof(key_entry.key));
628 memcpy(key_entry.tx_mic, crypto->tx_mic,
629 sizeof(key_entry.tx_mic));
630 memcpy(key_entry.rx_mic, crypto->rx_mic,
631 sizeof(key_entry.rx_mic));
632
633 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
634 rt2800_register_multiwrite(rt2x00dev, offset,
635 &key_entry, sizeof(key_entry));
636 }
637
638 /*
639 * The cipher types are stored over multiple registers
640 * starting with SHARED_KEY_MODE_BASE each word will have
641 * 32 bits and contains the cipher types for 2 bssidx each.
642 * Using the correct defines correctly will cause overhead,
643 * so just calculate the correct offset.
644 */
645 field.bit_offset = 4 * (key->hw_key_idx % 8);
646 field.bit_mask = 0x7 << field.bit_offset;
647
648 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
649
650 rt2800_register_read(rt2x00dev, offset, &reg);
651 rt2x00_set_field32(&reg, field,
652 (crypto->cmd == SET_KEY) * crypto->cipher);
653 rt2800_register_write(rt2x00dev, offset, reg);
654
655 /*
656 * Update WCID information
657 */
658 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
659
660 return 0;
661}
662EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
663
664int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
665 struct rt2x00lib_crypto *crypto,
666 struct ieee80211_key_conf *key)
667{
668 struct hw_key_entry key_entry;
669 u32 offset;
670
671 if (crypto->cmd == SET_KEY) {
672 /*
673 * 1 pairwise key is possible per AID, this means that the AID
674 * equals our hw_key_idx. Make sure the WCID starts _after_ the
675 * last possible shared key entry.
676 */
677 if (crypto->aid > (256 - 32))
678 return -ENOSPC;
679
680 key->hw_key_idx = 32 + crypto->aid;
681
682 memcpy(key_entry.key, crypto->key,
683 sizeof(key_entry.key));
684 memcpy(key_entry.tx_mic, crypto->tx_mic,
685 sizeof(key_entry.tx_mic));
686 memcpy(key_entry.rx_mic, crypto->rx_mic,
687 sizeof(key_entry.rx_mic));
688
689 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
690 rt2800_register_multiwrite(rt2x00dev, offset,
691 &key_entry, sizeof(key_entry));
692 }
693
694 /*
695 * Update WCID information
696 */
697 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
698
699 return 0;
700}
701EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
702
703void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
704 const unsigned int filter_flags)
705{
706 u32 reg;
707
708 /*
709 * Start configuration steps.
710 * Note that the version error will always be dropped
711 * and broadcast frames will always be accepted since
712 * there is no filter for it at this time.
713 */
714 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
715 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
716 !(filter_flags & FIF_FCSFAIL));
717 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
718 !(filter_flags & FIF_PLCPFAIL));
719 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
720 !(filter_flags & FIF_PROMISC_IN_BSS));
721 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
722 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
723 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
724 !(filter_flags & FIF_ALLMULTI));
725 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
726 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
727 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
728 !(filter_flags & FIF_CONTROL));
729 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
730 !(filter_flags & FIF_CONTROL));
731 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
732 !(filter_flags & FIF_CONTROL));
733 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
734 !(filter_flags & FIF_CONTROL));
735 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
736 !(filter_flags & FIF_CONTROL));
737 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
738 !(filter_flags & FIF_PSPOLL));
739 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
740 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
741 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
742 !(filter_flags & FIF_CONTROL));
743 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
744}
745EXPORT_SYMBOL_GPL(rt2800_config_filter);
746
747void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
748 struct rt2x00intf_conf *conf, const unsigned int flags)
749{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100750 u32 reg;
751
752 if (flags & CONFIG_UPDATE_TYPE) {
753 /*
754 * Clear current synchronisation setup.
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100755 */
Helmut Schaafdb87252010-06-29 21:48:06 +0200756 rt2800_clear_beacon(rt2x00dev,
757 HW_BEACON_OFFSET(intf->beacon->entry_idx));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100758 /*
759 * Enable synchronisation.
760 */
761 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
762 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
763 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Josef Bacik6a62e5ef2009-11-15 21:33:18 -0500764 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE,
765 (conf->sync == TSF_SYNC_BEACON));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100766 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
767 }
768
769 if (flags & CONFIG_UPDATE_MAC) {
770 reg = le32_to_cpu(conf->mac[1]);
771 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
772 conf->mac[1] = cpu_to_le32(reg);
773
774 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
775 conf->mac, sizeof(conf->mac));
776 }
777
778 if (flags & CONFIG_UPDATE_BSSID) {
779 reg = le32_to_cpu(conf->bssid[1]);
Ivo van Doornd440cb92010-06-29 21:45:31 +0200780 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
781 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 7);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100782 conf->bssid[1] = cpu_to_le32(reg);
783
784 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
785 conf->bssid, sizeof(conf->bssid));
786 }
787}
788EXPORT_SYMBOL_GPL(rt2800_config_intf);
789
790void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp)
791{
792 u32 reg;
793
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100794 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
795 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
796 !!erp->short_preamble);
797 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
798 !!erp->short_preamble);
799 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
800
801 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
802 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
803 erp->cts_protection ? 2 : 0);
804 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
805
806 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
807 erp->basic_rates);
808 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
809
810 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
811 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, erp->slot_time);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100812 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
813
814 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100815 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100816 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
817
818 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
819 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
820 erp->beacon_int * 16);
821 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
822}
823EXPORT_SYMBOL_GPL(rt2800_config_erp);
824
825void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
826{
827 u8 r1;
828 u8 r3;
829
830 rt2800_bbp_read(rt2x00dev, 1, &r1);
831 rt2800_bbp_read(rt2x00dev, 3, &r3);
832
833 /*
834 * Configure the TX antenna.
835 */
836 switch ((int)ant->tx) {
837 case 1:
838 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100839 break;
840 case 2:
841 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
842 break;
843 case 3:
Ivo van Doorne22557f2010-06-29 21:49:05 +0200844 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100845 break;
846 }
847
848 /*
849 * Configure the RX antenna.
850 */
851 switch ((int)ant->rx) {
852 case 1:
853 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
854 break;
855 case 2:
856 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
857 break;
858 case 3:
859 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
860 break;
861 }
862
863 rt2800_bbp_write(rt2x00dev, 3, r3);
864 rt2800_bbp_write(rt2x00dev, 1, r1);
865}
866EXPORT_SYMBOL_GPL(rt2800_config_ant);
867
868static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
869 struct rt2x00lib_conf *libconf)
870{
871 u16 eeprom;
872 short lna_gain;
873
874 if (libconf->rf.channel <= 14) {
875 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
876 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
877 } else if (libconf->rf.channel <= 64) {
878 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
879 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
880 } else if (libconf->rf.channel <= 128) {
881 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
882 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
883 } else {
884 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
885 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
886 }
887
888 rt2x00dev->lna_gain = lna_gain;
889}
890
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200891static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
892 struct ieee80211_conf *conf,
893 struct rf_channel *rf,
894 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100895{
896 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
897
898 if (rt2x00dev->default_ant.tx == 1)
899 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
900
901 if (rt2x00dev->default_ant.rx == 1) {
902 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
903 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
904 } else if (rt2x00dev->default_ant.rx == 2)
905 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
906
907 if (rf->channel > 14) {
908 /*
909 * When TX power is below 0, we should increase it by 7 to
910 * make it a positive value (Minumum value is -7).
911 * However this means that values between 0 and 7 have
912 * double meaning, and we should set a 7DBm boost flag.
913 */
914 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
915 (info->tx_power1 >= 0));
916
917 if (info->tx_power1 < 0)
918 info->tx_power1 += 7;
919
920 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A,
921 TXPOWER_A_TO_DEV(info->tx_power1));
922
923 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
924 (info->tx_power2 >= 0));
925
926 if (info->tx_power2 < 0)
927 info->tx_power2 += 7;
928
929 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A,
930 TXPOWER_A_TO_DEV(info->tx_power2));
931 } else {
932 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G,
933 TXPOWER_G_TO_DEV(info->tx_power1));
934 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G,
935 TXPOWER_G_TO_DEV(info->tx_power2));
936 }
937
938 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
939
940 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
941 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
942 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
943 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
944
945 udelay(200);
946
947 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
948 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
949 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
950 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
951
952 udelay(200);
953
954 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
955 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
956 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
957 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
958}
959
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +0200960static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
961 struct ieee80211_conf *conf,
962 struct rf_channel *rf,
963 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100964{
965 u8 rfcsr;
966
967 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Gertjan van Wingerde41a26172009-11-09 22:59:04 +0100968 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100969
970 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +0200971 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100972 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
973
974 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
975 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
976 TXPOWER_G_TO_DEV(info->tx_power1));
977 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
978
Helmut Schaa5a673962010-04-23 15:54:43 +0200979 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
980 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
981 TXPOWER_G_TO_DEV(info->tx_power2));
982 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
983
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100984 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
985 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
986 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
987
988 rt2800_rfcsr_write(rt2x00dev, 24,
989 rt2x00dev->calibration[conf_is_ht40(conf)]);
990
Gertjan van Wingerde71976902010-03-24 21:42:36 +0100991 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100992 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +0100993 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100994}
995
996static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
997 struct ieee80211_conf *conf,
998 struct rf_channel *rf,
999 struct channel_info *info)
1000{
1001 u32 reg;
1002 unsigned int tx_pin;
1003 u8 bbp;
1004
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001005 if (rt2x00_rf(rt2x00dev, RF2020) ||
1006 rt2x00_rf(rt2x00dev, RF3020) ||
1007 rt2x00_rf(rt2x00dev, RF3021) ||
1008 rt2x00_rf(rt2x00dev, RF3022))
1009 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerdefa6f6322009-11-09 22:59:58 +01001010 else
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001011 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001012
1013 /*
1014 * Change BBP settings
1015 */
1016 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
1017 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
1018 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
1019 rt2800_bbp_write(rt2x00dev, 86, 0);
1020
1021 if (rf->channel <= 14) {
1022 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
1023 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1024 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1025 } else {
1026 rt2800_bbp_write(rt2x00dev, 82, 0x84);
1027 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1028 }
1029 } else {
1030 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
1031
1032 if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
1033 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1034 else
1035 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1036 }
1037
1038 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001039 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001040 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
1041 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
1042 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
1043
1044 tx_pin = 0;
1045
1046 /* Turn on unused PA or LNA when not using 1T or 1R */
1047 if (rt2x00dev->default_ant.tx != 1) {
1048 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
1049 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
1050 }
1051
1052 /* Turn on unused PA or LNA when not using 1T or 1R */
1053 if (rt2x00dev->default_ant.rx != 1) {
1054 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
1055 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
1056 }
1057
1058 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
1059 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
1060 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
1061 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
1062 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
1063 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
1064
1065 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
1066
1067 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1068 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
1069 rt2800_bbp_write(rt2x00dev, 4, bbp);
1070
1071 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001072 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001073 rt2800_bbp_write(rt2x00dev, 3, bbp);
1074
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001075 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001076 if (conf_is_ht40(conf)) {
1077 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
1078 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1079 rt2800_bbp_write(rt2x00dev, 73, 0x16);
1080 } else {
1081 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1082 rt2800_bbp_write(rt2x00dev, 70, 0x08);
1083 rt2800_bbp_write(rt2x00dev, 73, 0x11);
1084 }
1085 }
1086
1087 msleep(1);
1088}
1089
1090static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
1091 const int txpower)
1092{
1093 u32 reg;
1094 u32 value = TXPOWER_G_TO_DEV(txpower);
1095 u8 r1;
1096
1097 rt2800_bbp_read(rt2x00dev, 1, &r1);
Helmut Schaaa3f84ca2010-06-14 22:11:32 +02001098 rt2x00_set_field8(&r1, BBP1_TX_POWER, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001099 rt2800_bbp_write(rt2x00dev, 1, r1);
1100
1101 rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
1102 rt2x00_set_field32(&reg, TX_PWR_CFG_0_1MBS, value);
1103 rt2x00_set_field32(&reg, TX_PWR_CFG_0_2MBS, value);
1104 rt2x00_set_field32(&reg, TX_PWR_CFG_0_55MBS, value);
1105 rt2x00_set_field32(&reg, TX_PWR_CFG_0_11MBS, value);
1106 rt2x00_set_field32(&reg, TX_PWR_CFG_0_6MBS, value);
1107 rt2x00_set_field32(&reg, TX_PWR_CFG_0_9MBS, value);
1108 rt2x00_set_field32(&reg, TX_PWR_CFG_0_12MBS, value);
1109 rt2x00_set_field32(&reg, TX_PWR_CFG_0_18MBS, value);
1110 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0, reg);
1111
1112 rt2800_register_read(rt2x00dev, TX_PWR_CFG_1, &reg);
1113 rt2x00_set_field32(&reg, TX_PWR_CFG_1_24MBS, value);
1114 rt2x00_set_field32(&reg, TX_PWR_CFG_1_36MBS, value);
1115 rt2x00_set_field32(&reg, TX_PWR_CFG_1_48MBS, value);
1116 rt2x00_set_field32(&reg, TX_PWR_CFG_1_54MBS, value);
1117 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS0, value);
1118 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS1, value);
1119 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS2, value);
1120 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS3, value);
1121 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1, reg);
1122
1123 rt2800_register_read(rt2x00dev, TX_PWR_CFG_2, &reg);
1124 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS4, value);
1125 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS5, value);
1126 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS6, value);
1127 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS7, value);
1128 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS8, value);
1129 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS9, value);
1130 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS10, value);
1131 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS11, value);
1132 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2, reg);
1133
1134 rt2800_register_read(rt2x00dev, TX_PWR_CFG_3, &reg);
1135 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS12, value);
1136 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS13, value);
1137 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS14, value);
1138 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS15, value);
1139 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN1, value);
1140 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN2, value);
1141 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN3, value);
1142 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN4, value);
1143 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3, reg);
1144
1145 rt2800_register_read(rt2x00dev, TX_PWR_CFG_4, &reg);
1146 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN5, value);
1147 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN6, value);
1148 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN7, value);
1149 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN8, value);
1150 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4, reg);
1151}
1152
1153static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
1154 struct rt2x00lib_conf *libconf)
1155{
1156 u32 reg;
1157
1158 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1159 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
1160 libconf->conf->short_frame_max_tx_count);
1161 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
1162 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001163 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1164}
1165
1166static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
1167 struct rt2x00lib_conf *libconf)
1168{
1169 enum dev_state state =
1170 (libconf->conf->flags & IEEE80211_CONF_PS) ?
1171 STATE_SLEEP : STATE_AWAKE;
1172 u32 reg;
1173
1174 if (state == STATE_SLEEP) {
1175 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
1176
1177 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1178 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
1179 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
1180 libconf->conf->listen_interval - 1);
1181 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
1182 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
1183
1184 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
1185 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001186 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1187 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
1188 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
1189 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
1190 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02001191
1192 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001193 }
1194}
1195
1196void rt2800_config(struct rt2x00_dev *rt2x00dev,
1197 struct rt2x00lib_conf *libconf,
1198 const unsigned int flags)
1199{
1200 /* Always recalculate LNA gain before changing configuration */
1201 rt2800_config_lna_gain(rt2x00dev, libconf);
1202
1203 if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
1204 rt2800_config_channel(rt2x00dev, libconf->conf,
1205 &libconf->rf, &libconf->channel);
1206 if (flags & IEEE80211_CONF_CHANGE_POWER)
1207 rt2800_config_txpower(rt2x00dev, libconf->conf->power_level);
1208 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
1209 rt2800_config_retry_limit(rt2x00dev, libconf);
1210 if (flags & IEEE80211_CONF_CHANGE_PS)
1211 rt2800_config_ps(rt2x00dev, libconf);
1212}
1213EXPORT_SYMBOL_GPL(rt2800_config);
1214
1215/*
1216 * Link tuning
1217 */
1218void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1219{
1220 u32 reg;
1221
1222 /*
1223 * Update FCS error count from register.
1224 */
1225 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1226 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
1227}
1228EXPORT_SYMBOL_GPL(rt2800_link_stats);
1229
1230static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
1231{
1232 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001233 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001234 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001235 rt2x00_rt(rt2x00dev, RT3090) ||
1236 rt2x00_rt(rt2x00dev, RT3390))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001237 return 0x1c + (2 * rt2x00dev->lna_gain);
1238 else
1239 return 0x2e + rt2x00dev->lna_gain;
1240 }
1241
1242 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
1243 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
1244 else
1245 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
1246}
1247
1248static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
1249 struct link_qual *qual, u8 vgc_level)
1250{
1251 if (qual->vgc_level != vgc_level) {
1252 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
1253 qual->vgc_level = vgc_level;
1254 qual->vgc_level_reg = vgc_level;
1255 }
1256}
1257
1258void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1259{
1260 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
1261}
1262EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
1263
1264void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
1265 const u32 count)
1266{
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001267 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001268 return;
1269
1270 /*
1271 * When RSSI is better then -80 increase VGC level with 0x10
1272 */
1273 rt2800_set_vgc(rt2x00dev, qual,
1274 rt2800_get_default_vgc(rt2x00dev) +
1275 ((qual->rssi > -80) * 0x10));
1276}
1277EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001278
1279/*
1280 * Initialization functions.
1281 */
1282int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
1283{
1284 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001285 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001286 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001287 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001288
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001289 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1290 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1291 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1292 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1293 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1294 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
1295 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1296
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001297 ret = rt2800_drv_init_registers(rt2x00dev);
1298 if (ret)
1299 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001300
1301 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
1302 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
1303 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
1304 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
1305 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
1306 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
1307
1308 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
1309 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
1310 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
1311 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
1312 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
1313 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
1314
1315 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
1316 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1317
1318 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1319
1320 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1321 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 0);
1322 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
1323 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
1324 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
1325 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1326 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
1327 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1328
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001329 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
1330
1331 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1332 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
1333 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
1334 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
1335
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001336 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001337 rt2x00_rt(rt2x00dev, RT3090) ||
1338 rt2x00_rt(rt2x00dev, RT3390)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001339 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1340 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001341 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001342 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
1343 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001344 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1345 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
1346 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1347 0x0000002c);
1348 else
1349 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1350 0x0000000f);
1351 } else {
1352 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1353 }
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001354 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001355 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001356
1357 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
1358 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1359 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
1360 } else {
1361 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1362 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1363 }
Helmut Schaac295a812010-06-03 10:52:13 +02001364 } else if (rt2800_is_305x_soc(rt2x00dev)) {
1365 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1366 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1367 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000001f);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001368 } else {
1369 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
1370 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1371 }
1372
1373 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
1374 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
1375 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
1376 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
1377 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
1378 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
1379 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
1380 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
1381 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
1382 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
1383
1384 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
1385 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001386 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001387 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
1388 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
1389
1390 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
1391 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001392 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01001393 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001394 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001395 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
1396 else
1397 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
1398 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
1399 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
1400 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
1401
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001402 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1403 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
1404 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
1405 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
1406 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
1407 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
1408 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
1409 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
1410 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1411
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001412 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
1413
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001414 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1415 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
1416 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
1417 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
1418 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
1419 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
1420 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
1421 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1422
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001423 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1424 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001425 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001426 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
1427 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001428 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001429 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
1430 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
1431 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1432
1433 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001434 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001435 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
1436 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
1437 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1438 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1439 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001440 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001441 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001442 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1443 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001444 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
1445
1446 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001447 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001448 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
1449 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
1450 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1451 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1452 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001453 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001454 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001455 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1456 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001457 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1458
1459 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1460 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
1461 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
1462 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
1463 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1464 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1465 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1466 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1467 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1468 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001469 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001470 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1471
1472 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1473 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001474 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL,
1475 !rt2x00_is_usb(rt2x00dev));
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001476 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
1477 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1478 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1479 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1480 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1481 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1482 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001483 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001484 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1485
1486 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1487 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
1488 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
1489 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
1490 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1491 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1492 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1493 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1494 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1495 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001496 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001497 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1498
1499 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1500 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
1501 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
1502 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
1503 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1504 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1505 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1506 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1507 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1508 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001509 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001510 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1511
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001512 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001513 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
1514
1515 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1516 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1517 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1518 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1519 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1520 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
1521 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
1522 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
1523 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
1524 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
1525 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1526 }
1527
1528 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, 0x0000583f);
1529 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
1530
1531 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
1532 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
1533 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
1534 IEEE80211_MAX_RTS_THRESHOLD);
1535 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
1536 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
1537
1538 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001539
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001540 /*
1541 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
1542 * time should be set to 16. However, the original Ralink driver uses
1543 * 16 for both and indeed using a value of 10 for CCK SIFS results in
1544 * connection problems with 11g + CTS protection. Hence, use the same
1545 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
1546 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001547 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001548 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
1549 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001550 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
1551 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
1552 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
1553 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1554
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001555 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
1556
1557 /*
1558 * ASIC will keep garbage value after boot, clear encryption keys.
1559 */
1560 for (i = 0; i < 4; i++)
1561 rt2800_register_write(rt2x00dev,
1562 SHARED_KEY_MODE_ENTRY(i), 0);
1563
1564 for (i = 0; i < 256; i++) {
1565 u32 wcid[2] = { 0xffffffff, 0x00ffffff };
1566 rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
1567 wcid, sizeof(wcid));
1568
1569 rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
1570 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
1571 }
1572
1573 /*
1574 * Clear all beacons
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001575 */
Helmut Schaafdb87252010-06-29 21:48:06 +02001576 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE0);
1577 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE1);
1578 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE2);
1579 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE3);
1580 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE4);
1581 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE5);
1582 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE6);
1583 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE7);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001584
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001585 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02001586 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
1587 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
1588 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001589 }
1590
1591 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
1592 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
1593 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
1594 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
1595 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
1596 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
1597 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
1598 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
1599 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
1600 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
1601
1602 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
1603 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
1604 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
1605 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
1606 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
1607 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
1608 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
1609 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
1610 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
1611 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
1612
1613 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
1614 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
1615 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
1616 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
1617 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
1618 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
1619 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
1620 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
1621 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
1622 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
1623
1624 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
1625 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
1626 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
1627 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
1628 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
1629 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
1630
1631 /*
1632 * We must clear the error counters.
1633 * These registers are cleared on read,
1634 * so we may pass a useless variable to store the value.
1635 */
1636 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1637 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
1638 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
1639 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
1640 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
1641 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
1642
1643 return 0;
1644}
1645EXPORT_SYMBOL_GPL(rt2800_init_registers);
1646
1647static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
1648{
1649 unsigned int i;
1650 u32 reg;
1651
1652 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1653 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
1654 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
1655 return 0;
1656
1657 udelay(REGISTER_BUSY_DELAY);
1658 }
1659
1660 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
1661 return -EACCES;
1662}
1663
1664static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
1665{
1666 unsigned int i;
1667 u8 value;
1668
1669 /*
1670 * BBP was enabled after firmware was loaded,
1671 * but we need to reactivate it now.
1672 */
1673 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
1674 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
1675 msleep(1);
1676
1677 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1678 rt2800_bbp_read(rt2x00dev, 0, &value);
1679 if ((value != 0xff) && (value != 0x00))
1680 return 0;
1681 udelay(REGISTER_BUSY_DELAY);
1682 }
1683
1684 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
1685 return -EACCES;
1686}
1687
1688int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
1689{
1690 unsigned int i;
1691 u16 eeprom;
1692 u8 reg_id;
1693 u8 value;
1694
1695 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
1696 rt2800_wait_bbp_ready(rt2x00dev)))
1697 return -EACCES;
1698
Helmut Schaabaff8002010-04-28 09:58:59 +02001699 if (rt2800_is_305x_soc(rt2x00dev))
1700 rt2800_bbp_write(rt2x00dev, 31, 0x08);
1701
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001702 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
1703 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001704
1705 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
1706 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1707 rt2800_bbp_write(rt2x00dev, 73, 0x12);
1708 } else {
1709 rt2800_bbp_write(rt2x00dev, 69, 0x12);
1710 rt2800_bbp_write(rt2x00dev, 73, 0x10);
1711 }
1712
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001713 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001714
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001715 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001716 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001717 rt2x00_rt(rt2x00dev, RT3090) ||
1718 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001719 rt2800_bbp_write(rt2x00dev, 79, 0x13);
1720 rt2800_bbp_write(rt2x00dev, 80, 0x05);
1721 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Helmut Schaabaff8002010-04-28 09:58:59 +02001722 } else if (rt2800_is_305x_soc(rt2x00dev)) {
1723 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
1724 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001725 } else {
1726 rt2800_bbp_write(rt2x00dev, 81, 0x37);
1727 }
1728
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001729 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1730 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001731
Gertjan van Wingerde5ed8f452010-06-03 10:51:57 +02001732 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001733 rt2800_bbp_write(rt2x00dev, 84, 0x19);
1734 else
1735 rt2800_bbp_write(rt2x00dev, 84, 0x99);
1736
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001737 rt2800_bbp_write(rt2x00dev, 86, 0x00);
1738 rt2800_bbp_write(rt2x00dev, 91, 0x04);
1739 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001740
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001741 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001742 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001743 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02001744 rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
1745 rt2800_is_305x_soc(rt2x00dev))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001746 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
1747 else
1748 rt2800_bbp_write(rt2x00dev, 103, 0x00);
1749
Helmut Schaabaff8002010-04-28 09:58:59 +02001750 if (rt2800_is_305x_soc(rt2x00dev))
1751 rt2800_bbp_write(rt2x00dev, 105, 0x01);
1752 else
1753 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001754 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001755
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001756 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001757 rt2x00_rt(rt2x00dev, RT3090) ||
1758 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001759 rt2800_bbp_read(rt2x00dev, 138, &value);
1760
1761 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
1762 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
1763 value |= 0x20;
1764 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
1765 value &= ~0x02;
1766
1767 rt2800_bbp_write(rt2x00dev, 138, value);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001768 }
1769
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001770
1771 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
1772 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
1773
1774 if (eeprom != 0xffff && eeprom != 0x0000) {
1775 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
1776 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
1777 rt2800_bbp_write(rt2x00dev, reg_id, value);
1778 }
1779 }
1780
1781 return 0;
1782}
1783EXPORT_SYMBOL_GPL(rt2800_init_bbp);
1784
1785static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
1786 bool bw40, u8 rfcsr24, u8 filter_target)
1787{
1788 unsigned int i;
1789 u8 bbp;
1790 u8 rfcsr;
1791 u8 passband;
1792 u8 stopband;
1793 u8 overtuned = 0;
1794
1795 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1796
1797 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1798 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
1799 rt2800_bbp_write(rt2x00dev, 4, bbp);
1800
1801 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
1802 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
1803 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
1804
1805 /*
1806 * Set power & frequency of passband test tone
1807 */
1808 rt2800_bbp_write(rt2x00dev, 24, 0);
1809
1810 for (i = 0; i < 100; i++) {
1811 rt2800_bbp_write(rt2x00dev, 25, 0x90);
1812 msleep(1);
1813
1814 rt2800_bbp_read(rt2x00dev, 55, &passband);
1815 if (passband)
1816 break;
1817 }
1818
1819 /*
1820 * Set power & frequency of stopband test tone
1821 */
1822 rt2800_bbp_write(rt2x00dev, 24, 0x06);
1823
1824 for (i = 0; i < 100; i++) {
1825 rt2800_bbp_write(rt2x00dev, 25, 0x90);
1826 msleep(1);
1827
1828 rt2800_bbp_read(rt2x00dev, 55, &stopband);
1829
1830 if ((passband - stopband) <= filter_target) {
1831 rfcsr24++;
1832 overtuned += ((passband - stopband) == filter_target);
1833 } else
1834 break;
1835
1836 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1837 }
1838
1839 rfcsr24 -= !!overtuned;
1840
1841 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1842 return rfcsr24;
1843}
1844
1845int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
1846{
1847 u8 rfcsr;
1848 u8 bbp;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001849 u32 reg;
1850 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001851
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001852 if (!rt2x00_rt(rt2x00dev, RT3070) &&
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001853 !rt2x00_rt(rt2x00dev, RT3071) &&
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001854 !rt2x00_rt(rt2x00dev, RT3090) &&
Helmut Schaa23812382010-04-26 13:48:45 +02001855 !rt2x00_rt(rt2x00dev, RT3390) &&
Helmut Schaabaff8002010-04-28 09:58:59 +02001856 !rt2800_is_305x_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001857 return 0;
1858
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001859 /*
1860 * Init RF calibration.
1861 */
1862 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1863 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1864 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1865 msleep(1);
1866 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1867 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1868
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001869 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001870 rt2x00_rt(rt2x00dev, RT3071) ||
1871 rt2x00_rt(rt2x00dev, RT3090)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001872 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
1873 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
1874 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
1875 rt2800_rfcsr_write(rt2x00dev, 7, 0x70);
1876 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001877 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001878 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
1879 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
1880 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
1881 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
1882 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
1883 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
1884 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
1885 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
1886 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
1887 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
1888 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
1889 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001890 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001891 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
1892 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
1893 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
1894 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
1895 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001896 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001897 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
1898 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
1899 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
1900 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
1901 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
1902 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001903 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001904 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
1905 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001906 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001907 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
1908 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
1909 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
1910 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
1911 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
1912 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
1913 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001914 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001915 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001916 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001917 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
1918 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
1919 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
1920 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
1921 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
1922 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
1923 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Helmut Schaabaff8002010-04-28 09:58:59 +02001924 } else if (rt2800_is_305x_soc(rt2x00dev)) {
Helmut Schaa23812382010-04-26 13:48:45 +02001925 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
1926 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
1927 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
1928 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
1929 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
1930 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
1931 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
1932 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
1933 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
1934 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
1935 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
1936 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
1937 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
1938 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
1939 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
1940 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
1941 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
1942 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
1943 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
1944 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
1945 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
1946 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
1947 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
1948 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
1949 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
1950 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
1951 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
1952 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
1953 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
1954 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
Helmut Schaabaff8002010-04-28 09:58:59 +02001955 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
1956 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
1957 return 0;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001958 }
1959
1960 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
1961 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
1962 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
1963 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
1964 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001965 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
1966 rt2x00_rt(rt2x00dev, RT3090)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001967 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
1968 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
1969 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1970
1971 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
1972
1973 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
1974 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001975 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
1976 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001977 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1978 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
1979 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
1980 else
1981 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
1982 }
1983 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001984 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
1985 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
1986 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
1987 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001988 }
1989
1990 /*
1991 * Set RX Filter calibration for 20MHz and 40MHz
1992 */
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001993 if (rt2x00_rt(rt2x00dev, RT3070)) {
1994 rt2x00dev->calibration[0] =
1995 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
1996 rt2x00dev->calibration[1] =
1997 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001998 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001999 rt2x00_rt(rt2x00dev, RT3090) ||
2000 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002001 rt2x00dev->calibration[0] =
2002 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
2003 rt2x00dev->calibration[1] =
2004 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002005 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002006
2007 /*
2008 * Set back to initial state
2009 */
2010 rt2800_bbp_write(rt2x00dev, 24, 0);
2011
2012 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
2013 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
2014 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
2015
2016 /*
2017 * set BBP back to BW20
2018 */
2019 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2020 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
2021 rt2800_bbp_write(rt2x00dev, 4, bbp);
2022
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002023 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002024 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002025 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2026 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002027 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
2028
2029 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
2030 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
2031 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
2032
2033 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
2034 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002035 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002036 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2037 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerde8440c292010-06-03 10:52:02 +02002038 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002039 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
2040 }
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002041 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &eeprom);
2042 if (rt2x00_get_field16(eeprom, EEPROM_TXMIXER_GAIN_BG_VAL) >= 1)
2043 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
2044 rt2x00_get_field16(eeprom,
2045 EEPROM_TXMIXER_GAIN_BG_VAL));
2046 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
2047
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002048 if (rt2x00_rt(rt2x00dev, RT3090)) {
2049 rt2800_bbp_read(rt2x00dev, 138, &bbp);
2050
2051 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2052 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
2053 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
2054 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
2055 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
2056
2057 rt2800_bbp_write(rt2x00dev, 138, bbp);
2058 }
2059
2060 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002061 rt2x00_rt(rt2x00dev, RT3090) ||
2062 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002063 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2064 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2065 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2066 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2067 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2068 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2069 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2070
2071 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
2072 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
2073 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
2074
2075 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
2076 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
2077 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
2078
2079 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
2080 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
2081 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
2082 }
2083
2084 if (rt2x00_rt(rt2x00dev, RT3070) || rt2x00_rt(rt2x00dev, RT3071)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002085 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002086 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
2087 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002088 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
2089 else
2090 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
2091 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
2092 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
2093 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
2094 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
2095 }
2096
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002097 return 0;
2098}
2099EXPORT_SYMBOL_GPL(rt2800_init_rfcsr);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002100
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002101int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
2102{
2103 u32 reg;
2104
2105 rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
2106
2107 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
2108}
2109EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
2110
2111static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
2112{
2113 u32 reg;
2114
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002115 mutex_lock(&rt2x00dev->csr_mutex);
2116
2117 rt2800_register_read_lock(rt2x00dev, EFUSE_CTRL, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002118 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
2119 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
2120 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002121 rt2800_register_write_lock(rt2x00dev, EFUSE_CTRL, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002122
2123 /* Wait until the EEPROM has been loaded */
2124 rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
2125
2126 /* Apparently the data is read from end to start */
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002127 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA3,
2128 (u32 *)&rt2x00dev->eeprom[i]);
2129 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA2,
2130 (u32 *)&rt2x00dev->eeprom[i + 2]);
2131 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA1,
2132 (u32 *)&rt2x00dev->eeprom[i + 4]);
2133 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA0,
2134 (u32 *)&rt2x00dev->eeprom[i + 6]);
2135
2136 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002137}
2138
2139void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
2140{
2141 unsigned int i;
2142
2143 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
2144 rt2800_efuse_read(rt2x00dev, i);
2145}
2146EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
2147
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002148int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
2149{
2150 u16 word;
2151 u8 *mac;
2152 u8 default_lna_gain;
2153
2154 /*
2155 * Start validation of the data that has been read.
2156 */
2157 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
2158 if (!is_valid_ether_addr(mac)) {
2159 random_ether_addr(mac);
2160 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
2161 }
2162
2163 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
2164 if (word == 0xffff) {
2165 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2166 rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
2167 rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
2168 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2169 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002170 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02002171 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002172 /*
2173 * There is a max of 2 RX streams for RT28x0 series
2174 */
2175 if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
2176 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2177 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2178 }
2179
2180 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
2181 if (word == 0xffff) {
2182 rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
2183 rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
2184 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
2185 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
2186 rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
2187 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
2188 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
2189 rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
2190 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
2191 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002192 rt2x00_set_field16(&word, EEPROM_NIC_ANT_DIVERSITY, 0);
2193 rt2x00_set_field16(&word, EEPROM_NIC_DAC_TEST, 0);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002194 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
2195 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
2196 }
2197
2198 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
2199 if ((word & 0x00ff) == 0x00ff) {
2200 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002201 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2202 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
2203 }
2204 if ((word & 0xff00) == 0xff00) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002205 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
2206 LED_MODE_TXRX_ACTIVITY);
2207 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
2208 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2209 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
2210 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
2211 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002212 EEPROM(rt2x00dev, "Led Mode: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002213 }
2214
2215 /*
2216 * During the LNA validation we are going to use
2217 * lna0 as correct value. Note that EEPROM_LNA
2218 * is never validated.
2219 */
2220 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
2221 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
2222
2223 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
2224 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
2225 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
2226 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
2227 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
2228 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
2229
2230 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
2231 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
2232 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
2233 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
2234 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
2235 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
2236 default_lna_gain);
2237 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
2238
2239 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
2240 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
2241 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
2242 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
2243 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
2244 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
2245
2246 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
2247 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
2248 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
2249 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
2250 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
2251 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
2252 default_lna_gain);
2253 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
2254
2255 return 0;
2256}
2257EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
2258
2259int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
2260{
2261 u32 reg;
2262 u16 value;
2263 u16 eeprom;
2264
2265 /*
2266 * Read EEPROM word for configuration.
2267 */
2268 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2269
2270 /*
2271 * Identify RF chipset.
2272 */
2273 value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
2274 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
2275
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002276 rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
2277 value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
Gertjan van Wingerde714fa662010-02-13 20:55:48 +01002278
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002279 if (!rt2x00_rt(rt2x00dev, RT2860) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002280 !rt2x00_rt(rt2x00dev, RT2872) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002281 !rt2x00_rt(rt2x00dev, RT2883) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002282 !rt2x00_rt(rt2x00dev, RT3070) &&
2283 !rt2x00_rt(rt2x00dev, RT3071) &&
2284 !rt2x00_rt(rt2x00dev, RT3090) &&
2285 !rt2x00_rt(rt2x00dev, RT3390) &&
2286 !rt2x00_rt(rt2x00dev, RT3572)) {
2287 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
2288 return -ENODEV;
2289 }
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002290
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002291 if (!rt2x00_rf(rt2x00dev, RF2820) &&
2292 !rt2x00_rf(rt2x00dev, RF2850) &&
2293 !rt2x00_rf(rt2x00dev, RF2720) &&
2294 !rt2x00_rf(rt2x00dev, RF2750) &&
2295 !rt2x00_rf(rt2x00dev, RF3020) &&
2296 !rt2x00_rf(rt2x00dev, RF2020) &&
2297 !rt2x00_rf(rt2x00dev, RF3021) &&
Gertjan van Wingerde6c0fe262009-12-30 11:36:31 +01002298 !rt2x00_rf(rt2x00dev, RF3022) &&
2299 !rt2x00_rf(rt2x00dev, RF3052)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002300 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
2301 return -ENODEV;
2302 }
2303
2304 /*
2305 * Identify default antenna configuration.
2306 */
2307 rt2x00dev->default_ant.tx =
2308 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH);
2309 rt2x00dev->default_ant.rx =
2310 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH);
2311
2312 /*
2313 * Read frequency offset and RF programming sequence.
2314 */
2315 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
2316 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
2317
2318 /*
2319 * Read external LNA informations.
2320 */
2321 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2322
2323 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
2324 __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
2325 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
2326 __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
2327
2328 /*
2329 * Detect if this device has an hardware controlled radio.
2330 */
2331 if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
2332 __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
2333
2334 /*
2335 * Store led settings, for correct led behaviour.
2336 */
2337#ifdef CONFIG_RT2X00_LIB_LEDS
2338 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
2339 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
2340 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
2341
2342 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &rt2x00dev->led_mcu_reg);
2343#endif /* CONFIG_RT2X00_LIB_LEDS */
2344
2345 return 0;
2346}
2347EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
2348
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002349/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002350 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002351 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
2352 */
2353static const struct rf_channel rf_vals[] = {
2354 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
2355 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
2356 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
2357 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
2358 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
2359 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
2360 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
2361 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
2362 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
2363 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
2364 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
2365 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
2366 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
2367 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
2368
2369 /* 802.11 UNI / HyperLan 2 */
2370 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
2371 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
2372 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
2373 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
2374 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
2375 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
2376 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
2377 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
2378 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
2379 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
2380 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
2381 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
2382
2383 /* 802.11 HyperLan 2 */
2384 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
2385 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
2386 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
2387 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
2388 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
2389 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
2390 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
2391 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
2392 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
2393 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
2394 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
2395 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
2396 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
2397 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
2398 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
2399 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
2400
2401 /* 802.11 UNII */
2402 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
2403 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
2404 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
2405 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
2406 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
2407 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
2408 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
2409 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
2410 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
2411 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
2412 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
2413
2414 /* 802.11 Japan */
2415 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
2416 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
2417 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
2418 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
2419 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
2420 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
2421 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
2422};
2423
2424/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002425 * RF value list for rt3xxx
2426 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002427 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02002428static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002429 {1, 241, 2, 2 },
2430 {2, 241, 2, 7 },
2431 {3, 242, 2, 2 },
2432 {4, 242, 2, 7 },
2433 {5, 243, 2, 2 },
2434 {6, 243, 2, 7 },
2435 {7, 244, 2, 2 },
2436 {8, 244, 2, 7 },
2437 {9, 245, 2, 2 },
2438 {10, 245, 2, 7 },
2439 {11, 246, 2, 2 },
2440 {12, 246, 2, 7 },
2441 {13, 247, 2, 2 },
2442 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02002443
2444 /* 802.11 UNI / HyperLan 2 */
2445 {36, 0x56, 0, 4},
2446 {38, 0x56, 0, 6},
2447 {40, 0x56, 0, 8},
2448 {44, 0x57, 0, 0},
2449 {46, 0x57, 0, 2},
2450 {48, 0x57, 0, 4},
2451 {52, 0x57, 0, 8},
2452 {54, 0x57, 0, 10},
2453 {56, 0x58, 0, 0},
2454 {60, 0x58, 0, 4},
2455 {62, 0x58, 0, 6},
2456 {64, 0x58, 0, 8},
2457
2458 /* 802.11 HyperLan 2 */
2459 {100, 0x5b, 0, 8},
2460 {102, 0x5b, 0, 10},
2461 {104, 0x5c, 0, 0},
2462 {108, 0x5c, 0, 4},
2463 {110, 0x5c, 0, 6},
2464 {112, 0x5c, 0, 8},
2465 {116, 0x5d, 0, 0},
2466 {118, 0x5d, 0, 2},
2467 {120, 0x5d, 0, 4},
2468 {124, 0x5d, 0, 8},
2469 {126, 0x5d, 0, 10},
2470 {128, 0x5e, 0, 0},
2471 {132, 0x5e, 0, 4},
2472 {134, 0x5e, 0, 6},
2473 {136, 0x5e, 0, 8},
2474 {140, 0x5f, 0, 0},
2475
2476 /* 802.11 UNII */
2477 {149, 0x5f, 0, 9},
2478 {151, 0x5f, 0, 11},
2479 {153, 0x60, 0, 1},
2480 {157, 0x60, 0, 5},
2481 {159, 0x60, 0, 7},
2482 {161, 0x60, 0, 9},
2483 {165, 0x61, 0, 1},
2484 {167, 0x61, 0, 3},
2485 {169, 0x61, 0, 5},
2486 {171, 0x61, 0, 7},
2487 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002488};
2489
2490int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
2491{
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002492 struct hw_mode_spec *spec = &rt2x00dev->spec;
2493 struct channel_info *info;
2494 char *tx_power1;
2495 char *tx_power2;
2496 unsigned int i;
2497 u16 eeprom;
2498
2499 /*
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002500 * Disable powersaving as default on PCI devices.
2501 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01002502 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002503 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
2504
2505 /*
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002506 * Initialize all hw fields.
2507 */
2508 rt2x00dev->hw->flags =
2509 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2510 IEEE80211_HW_SIGNAL_DBM |
2511 IEEE80211_HW_SUPPORTS_PS |
Helmut Schaa1df90802010-06-29 21:38:12 +02002512 IEEE80211_HW_PS_NULLFUNC_STACK |
2513 IEEE80211_HW_AMPDU_AGGREGATION;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002514
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002515 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
2516 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
2517 rt2x00_eeprom_addr(rt2x00dev,
2518 EEPROM_MAC_ADDR_0));
2519
Helmut Schaa3f2bee22010-06-14 22:12:01 +02002520 /*
2521 * As rt2800 has a global fallback table we cannot specify
2522 * more then one tx rate per frame but since the hw will
2523 * try several rates (based on the fallback table) we should
2524 * still initialize max_rates to the maximum number of rates
2525 * we are going to try. Otherwise mac80211 will truncate our
2526 * reported tx rates and the rc algortihm will end up with
2527 * incorrect data.
2528 */
2529 rt2x00dev->hw->max_rates = 7;
2530 rt2x00dev->hw->max_rate_tries = 1;
2531
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002532 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2533
2534 /*
2535 * Initialize hw_mode information.
2536 */
2537 spec->supported_bands = SUPPORT_BAND_2GHZ;
2538 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
2539
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002540 if (rt2x00_rf(rt2x00dev, RF2820) ||
Ivo van Doorn55f93212010-05-06 14:45:46 +02002541 rt2x00_rf(rt2x00dev, RF2720)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002542 spec->num_channels = 14;
2543 spec->channels = rf_vals;
Ivo van Doorn55f93212010-05-06 14:45:46 +02002544 } else if (rt2x00_rf(rt2x00dev, RF2850) ||
2545 rt2x00_rf(rt2x00dev, RF2750)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002546 spec->supported_bands |= SUPPORT_BAND_5GHZ;
2547 spec->num_channels = ARRAY_SIZE(rf_vals);
2548 spec->channels = rf_vals;
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002549 } else if (rt2x00_rf(rt2x00dev, RF3020) ||
2550 rt2x00_rf(rt2x00dev, RF2020) ||
2551 rt2x00_rf(rt2x00dev, RF3021) ||
2552 rt2x00_rf(rt2x00dev, RF3022)) {
Ivo van Doorn55f93212010-05-06 14:45:46 +02002553 spec->num_channels = 14;
2554 spec->channels = rf_vals_3x;
2555 } else if (rt2x00_rf(rt2x00dev, RF3052)) {
2556 spec->supported_bands |= SUPPORT_BAND_5GHZ;
2557 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
2558 spec->channels = rf_vals_3x;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002559 }
2560
2561 /*
2562 * Initialize HT information.
2563 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002564 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01002565 spec->ht.ht_supported = true;
2566 else
2567 spec->ht.ht_supported = false;
2568
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002569 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02002570 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002571 IEEE80211_HT_CAP_GRN_FLD |
2572 IEEE80211_HT_CAP_SGI_20 |
Ivo van Doornaa674632010-06-29 21:48:37 +02002573 IEEE80211_HT_CAP_SGI_40;
Helmut Schaa22cabaa2010-06-03 10:52:10 +02002574
2575 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) >= 2)
2576 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
2577
Ivo van Doornaa674632010-06-29 21:48:37 +02002578 spec->ht.cap |=
2579 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) <<
2580 IEEE80211_HT_CAP_RX_STBC_SHIFT;
2581
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002582 spec->ht.ampdu_factor = 3;
2583 spec->ht.ampdu_density = 4;
2584 spec->ht.mcs.tx_params =
2585 IEEE80211_HT_MCS_TX_DEFINED |
2586 IEEE80211_HT_MCS_TX_RX_DIFF |
2587 ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
2588 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
2589
2590 switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
2591 case 3:
2592 spec->ht.mcs.rx_mask[2] = 0xff;
2593 case 2:
2594 spec->ht.mcs.rx_mask[1] = 0xff;
2595 case 1:
2596 spec->ht.mcs.rx_mask[0] = 0xff;
2597 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
2598 break;
2599 }
2600
2601 /*
2602 * Create channel information array
2603 */
2604 info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
2605 if (!info)
2606 return -ENOMEM;
2607
2608 spec->channels_info = info;
2609
2610 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
2611 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
2612
2613 for (i = 0; i < 14; i++) {
2614 info[i].tx_power1 = TXPOWER_G_FROM_DEV(tx_power1[i]);
2615 info[i].tx_power2 = TXPOWER_G_FROM_DEV(tx_power2[i]);
2616 }
2617
2618 if (spec->num_channels > 14) {
2619 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
2620 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
2621
2622 for (i = 14; i < spec->num_channels; i++) {
2623 info[i].tx_power1 = TXPOWER_A_FROM_DEV(tx_power1[i]);
2624 info[i].tx_power2 = TXPOWER_A_FROM_DEV(tx_power2[i]);
2625 }
2626 }
2627
2628 return 0;
2629}
2630EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
2631
2632/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002633 * IEEE80211 stack callback functions.
2634 */
2635static void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx,
2636 u32 *iv32, u16 *iv16)
2637{
2638 struct rt2x00_dev *rt2x00dev = hw->priv;
2639 struct mac_iveiv_entry iveiv_entry;
2640 u32 offset;
2641
2642 offset = MAC_IVEIV_ENTRY(hw_key_idx);
2643 rt2800_register_multiread(rt2x00dev, offset,
2644 &iveiv_entry, sizeof(iveiv_entry));
2645
Julia Lawall855da5e2009-12-13 17:07:45 +01002646 memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
2647 memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002648}
2649
2650static int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2651{
2652 struct rt2x00_dev *rt2x00dev = hw->priv;
2653 u32 reg;
2654 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
2655
2656 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
2657 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
2658 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
2659
2660 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
2661 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
2662 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
2663
2664 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
2665 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
2666 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
2667
2668 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
2669 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
2670 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
2671
2672 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
2673 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
2674 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
2675
2676 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
2677 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
2678 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
2679
2680 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
2681 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
2682 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
2683
2684 return 0;
2685}
2686
2687static int rt2800_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
2688 const struct ieee80211_tx_queue_params *params)
2689{
2690 struct rt2x00_dev *rt2x00dev = hw->priv;
2691 struct data_queue *queue;
2692 struct rt2x00_field32 field;
2693 int retval;
2694 u32 reg;
2695 u32 offset;
2696
2697 /*
2698 * First pass the configuration through rt2x00lib, that will
2699 * update the queue settings and validate the input. After that
2700 * we are free to update the registers based on the value
2701 * in the queue parameter.
2702 */
2703 retval = rt2x00mac_conf_tx(hw, queue_idx, params);
2704 if (retval)
2705 return retval;
2706
2707 /*
2708 * We only need to perform additional register initialization
2709 * for WMM queues/
2710 */
2711 if (queue_idx >= 4)
2712 return 0;
2713
2714 queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
2715
2716 /* Update WMM TXOP register */
2717 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
2718 field.bit_offset = (queue_idx & 1) * 16;
2719 field.bit_mask = 0xffff << field.bit_offset;
2720
2721 rt2800_register_read(rt2x00dev, offset, &reg);
2722 rt2x00_set_field32(&reg, field, queue->txop);
2723 rt2800_register_write(rt2x00dev, offset, reg);
2724
2725 /* Update WMM registers */
2726 field.bit_offset = queue_idx * 4;
2727 field.bit_mask = 0xf << field.bit_offset;
2728
2729 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
2730 rt2x00_set_field32(&reg, field, queue->aifs);
2731 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
2732
2733 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
2734 rt2x00_set_field32(&reg, field, queue->cw_min);
2735 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
2736
2737 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
2738 rt2x00_set_field32(&reg, field, queue->cw_max);
2739 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
2740
2741 /* Update EDCA registers */
2742 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
2743
2744 rt2800_register_read(rt2x00dev, offset, &reg);
2745 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
2746 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
2747 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
2748 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
2749 rt2800_register_write(rt2x00dev, offset, reg);
2750
2751 return 0;
2752}
2753
2754static u64 rt2800_get_tsf(struct ieee80211_hw *hw)
2755{
2756 struct rt2x00_dev *rt2x00dev = hw->priv;
2757 u64 tsf;
2758 u32 reg;
2759
2760 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
2761 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
2762 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
2763 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
2764
2765 return tsf;
2766}
2767
Helmut Schaa1df90802010-06-29 21:38:12 +02002768static int rt2800_ampdu_action(struct ieee80211_hw *hw,
2769 struct ieee80211_vif *vif,
2770 enum ieee80211_ampdu_mlme_action action,
2771 struct ieee80211_sta *sta,
2772 u16 tid, u16 *ssn)
2773{
2774 struct rt2x00_dev *rt2x00dev = hw->priv;
2775 int ret = 0;
2776
2777 switch (action) {
2778 case IEEE80211_AMPDU_RX_START:
2779 case IEEE80211_AMPDU_RX_STOP:
2780 /* we don't support RX aggregation yet */
2781 ret = -ENOTSUPP;
2782 break;
2783 case IEEE80211_AMPDU_TX_START:
2784 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2785 break;
2786 case IEEE80211_AMPDU_TX_STOP:
2787 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2788 break;
2789 case IEEE80211_AMPDU_TX_OPERATIONAL:
2790 break;
2791 default:
2792 WARNING(rt2x00dev, "Unknown AMPDU action\n");
2793 }
2794
2795 return ret;
2796}
2797
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002798const struct ieee80211_ops rt2800_mac80211_ops = {
2799 .tx = rt2x00mac_tx,
2800 .start = rt2x00mac_start,
2801 .stop = rt2x00mac_stop,
2802 .add_interface = rt2x00mac_add_interface,
2803 .remove_interface = rt2x00mac_remove_interface,
2804 .config = rt2x00mac_config,
2805 .configure_filter = rt2x00mac_configure_filter,
2806 .set_tim = rt2x00mac_set_tim,
2807 .set_key = rt2x00mac_set_key,
2808 .get_stats = rt2x00mac_get_stats,
2809 .get_tkip_seq = rt2800_get_tkip_seq,
2810 .set_rts_threshold = rt2800_set_rts_threshold,
2811 .bss_info_changed = rt2x00mac_bss_info_changed,
2812 .conf_tx = rt2800_conf_tx,
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002813 .get_tsf = rt2800_get_tsf,
2814 .rfkill_poll = rt2x00mac_rfkill_poll,
Helmut Schaa1df90802010-06-29 21:38:12 +02002815 .ampdu_action = rt2800_ampdu_action,
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002816};
2817EXPORT_SYMBOL_GPL(rt2800_mac80211_ops);
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02002818
2819MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
2820MODULE_VERSION(DRV_VERSION);
2821MODULE_DESCRIPTION("Ralink RT2800 library");
2822MODULE_LICENSE("GPL");