blob: b2f9b04134ebbaea55c1e7ac17cb02bab28d23d1 [file] [log] [blame]
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +01001/*
Ivo van Doorn96481b22010-08-06 20:47:57 +02002 Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02003 Copyright (C) 2010 Ivo van Doorn <IvDoorn@gmail.com>
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01004 Copyright (C) 2009 Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
Gertjan van Wingerdecce5fc42009-11-10 22:42:40 +01005 Copyright (C) 2009 Gertjan van Wingerde <gwingerde@gmail.com>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +01006
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01007 Based on the original rt2800pci.c and rt2800usb.c.
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01008 Copyright (C) 2009 Alban Browaeys <prahal@yahoo.com>
9 Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
10 Copyright (C) 2009 Luis Correia <luis.f.correia@gmail.com>
11 Copyright (C) 2009 Mattias Nissler <mattias.nissler@gmx.de>
12 Copyright (C) 2009 Mark Asselstine <asselsm@gmail.com>
13 Copyright (C) 2009 Xose Vazquez Perez <xose.vazquez@gmail.com>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010014 <http://rt2x00.serialmonkey.com>
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the
28 Free Software Foundation, Inc.,
29 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 */
31
32/*
33 Module: rt2800lib
34 Abstract: rt2800 generic device routines.
35 */
36
Ivo van Doornf31c9a82010-07-11 12:30:37 +020037#include <linux/crc-ccitt.h>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010038#include <linux/kernel.h>
39#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010041
42#include "rt2x00.h"
43#include "rt2800lib.h"
44#include "rt2800.h"
45
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010046/*
47 * Register access.
48 * All access to the CSR registers will go through the methods
49 * rt2800_register_read and rt2800_register_write.
50 * BBP and RF register require indirect register access,
51 * and use the CSR registers BBPCSR and RFCSR to achieve this.
52 * These indirect registers work with busy bits,
53 * and we will try maximal REGISTER_BUSY_COUNT times to access
54 * the register while taking a REGISTER_BUSY_DELAY us delay
55 * between each attampt. When the busy bit is still set at that time,
56 * the access attempt is considered to have failed,
57 * and we will print an error.
58 * The _lock versions must be used if you already hold the csr_mutex
59 */
60#define WAIT_FOR_BBP(__dev, __reg) \
61 rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
62#define WAIT_FOR_RFCSR(__dev, __reg) \
63 rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
64#define WAIT_FOR_RF(__dev, __reg) \
65 rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
66#define WAIT_FOR_MCU(__dev, __reg) \
67 rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
68 H2M_MAILBOX_CSR_OWNER, (__reg))
69
Helmut Schaabaff8002010-04-28 09:58:59 +020070static inline bool rt2800_is_305x_soc(struct rt2x00_dev *rt2x00dev)
71{
72 /* check for rt2872 on SoC */
73 if (!rt2x00_is_soc(rt2x00dev) ||
74 !rt2x00_rt(rt2x00dev, RT2872))
75 return false;
76
77 /* we know for sure that these rf chipsets are used on rt305x boards */
78 if (rt2x00_rf(rt2x00dev, RF3020) ||
79 rt2x00_rf(rt2x00dev, RF3021) ||
80 rt2x00_rf(rt2x00dev, RF3022))
81 return true;
82
83 NOTICE(rt2x00dev, "Unknown RF chipset on rt305x\n");
84 return false;
85}
86
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010087static void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
88 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010089{
90 u32 reg;
91
92 mutex_lock(&rt2x00dev->csr_mutex);
93
94 /*
95 * Wait until the BBP becomes available, afterwards we
96 * can safely write the new data into the register.
97 */
98 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
99 reg = 0;
100 rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
101 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
102 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
103 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
Ivo van Doornefc7d362010-06-29 21:49:26 +0200104 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100105
106 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
107 }
108
109 mutex_unlock(&rt2x00dev->csr_mutex);
110}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100111
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100112static void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
113 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100114{
115 u32 reg;
116
117 mutex_lock(&rt2x00dev->csr_mutex);
118
119 /*
120 * Wait until the BBP becomes available, afterwards we
121 * can safely write the read request into the register.
122 * After the data has been written, we wait until hardware
123 * returns the correct value, if at any time the register
124 * doesn't become available in time, reg will be 0xffffffff
125 * which means we return 0xff to the caller.
126 */
127 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
128 reg = 0;
129 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
130 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
131 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
Ivo van Doornefc7d362010-06-29 21:49:26 +0200132 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100133
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
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200277static bool rt2800_check_firmware_crc(const u8 *data, const size_t len)
278{
279 u16 fw_crc;
280 u16 crc;
281
282 /*
283 * The last 2 bytes in the firmware array are the crc checksum itself,
284 * this means that we should never pass those 2 bytes to the crc
285 * algorithm.
286 */
287 fw_crc = (data[len - 2] << 8 | data[len - 1]);
288
289 /*
290 * Use the crc ccitt algorithm.
291 * This will return the same value as the legacy driver which
292 * used bit ordering reversion on the both the firmware bytes
293 * before input input as well as on the final output.
294 * Obviously using crc ccitt directly is much more efficient.
295 */
296 crc = crc_ccitt(~0, data, len - 2);
297
298 /*
299 * There is a small difference between the crc-itu-t + bitrev and
300 * the crc-ccitt crc calculation. In the latter method the 2 bytes
301 * will be swapped, use swab16 to convert the crc to the correct
302 * value.
303 */
304 crc = swab16(crc);
305
306 return fw_crc == crc;
307}
308
309int rt2800_check_firmware(struct rt2x00_dev *rt2x00dev,
310 const u8 *data, const size_t len)
311{
312 size_t offset = 0;
313 size_t fw_len;
314 bool multiple;
315
316 /*
317 * PCI(e) & SOC devices require firmware with a length
318 * of 8kb. USB devices require firmware files with a length
319 * of 4kb. Certain USB chipsets however require different firmware,
320 * which Ralink only provides attached to the original firmware
321 * file. Thus for USB devices, firmware files have a length
322 * which is a multiple of 4kb.
323 */
324 if (rt2x00_is_usb(rt2x00dev)) {
325 fw_len = 4096;
326 multiple = true;
327 } else {
328 fw_len = 8192;
329 multiple = true;
330 }
331
332 /*
333 * Validate the firmware length
334 */
335 if (len != fw_len && (!multiple || (len % fw_len) != 0))
336 return FW_BAD_LENGTH;
337
338 /*
339 * Check if the chipset requires one of the upper parts
340 * of the firmware.
341 */
342 if (rt2x00_is_usb(rt2x00dev) &&
343 !rt2x00_rt(rt2x00dev, RT2860) &&
344 !rt2x00_rt(rt2x00dev, RT2872) &&
345 !rt2x00_rt(rt2x00dev, RT3070) &&
346 ((len / fw_len) == 1))
347 return FW_BAD_VERSION;
348
349 /*
350 * 8kb firmware files must be checked as if it were
351 * 2 separate firmware files.
352 */
353 while (offset < len) {
354 if (!rt2800_check_firmware_crc(data + offset, fw_len))
355 return FW_BAD_CRC;
356
357 offset += fw_len;
358 }
359
360 return FW_OK;
361}
362EXPORT_SYMBOL_GPL(rt2800_check_firmware);
363
364int rt2800_load_firmware(struct rt2x00_dev *rt2x00dev,
365 const u8 *data, const size_t len)
366{
367 unsigned int i;
368 u32 reg;
369
370 /*
371 * Wait for stable hardware.
372 */
373 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
374 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
375 if (reg && reg != ~0)
376 break;
377 msleep(1);
378 }
379
380 if (i == REGISTER_BUSY_COUNT) {
381 ERROR(rt2x00dev, "Unstable hardware.\n");
382 return -EBUSY;
383 }
384
385 if (rt2x00_is_pci(rt2x00dev))
386 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
387
388 /*
389 * Disable DMA, will be reenabled later when enabling
390 * the radio.
391 */
392 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
393 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
394 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
395 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
396 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
397 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
398 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
399
400 /*
401 * Write firmware to the device.
402 */
403 rt2800_drv_write_firmware(rt2x00dev, data, len);
404
405 /*
406 * Wait for device to stabilize.
407 */
408 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
409 rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
410 if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
411 break;
412 msleep(1);
413 }
414
415 if (i == REGISTER_BUSY_COUNT) {
416 ERROR(rt2x00dev, "PBF system register not ready.\n");
417 return -EBUSY;
418 }
419
420 /*
421 * Initialize firmware.
422 */
423 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
424 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
425 msleep(1);
426
427 return 0;
428}
429EXPORT_SYMBOL_GPL(rt2800_load_firmware);
430
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200431void rt2800_write_tx_data(struct queue_entry *entry,
432 struct txentry_desc *txdesc)
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200433{
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200434 __le32 *txwi = rt2800_drv_get_txwi(entry);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200435 u32 word;
436
437 /*
438 * Initialize TX Info descriptor
439 */
440 rt2x00_desc_read(txwi, 0, &word);
441 rt2x00_set_field32(&word, TXWI_W0_FRAG,
442 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
Ivo van Doorn84804cd2010-08-06 20:46:19 +0200443 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS,
444 test_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags));
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200445 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
446 rt2x00_set_field32(&word, TXWI_W0_TS,
447 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
448 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
449 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
450 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
451 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->txop);
452 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
453 rt2x00_set_field32(&word, TXWI_W0_BW,
454 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
455 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
456 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
457 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
458 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
459 rt2x00_desc_write(txwi, 0, word);
460
461 rt2x00_desc_read(txwi, 1, &word);
462 rt2x00_set_field32(&word, TXWI_W1_ACK,
463 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
464 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
465 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
466 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
467 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
468 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
469 txdesc->key_idx : 0xff);
470 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
471 txdesc->length);
472 rt2x00_set_field32(&word, TXWI_W1_PACKETID, txdesc->queue + 1);
473 rt2x00_desc_write(txwi, 1, word);
474
475 /*
476 * Always write 0 to IV/EIV fields, hardware will insert the IV
477 * from the IVEIV register when TXD_W3_WIV is set to 0.
478 * When TXD_W3_WIV is set to 1 it will use the IV data
479 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
480 * crypto entry in the registers should be used to encrypt the frame.
481 */
482 _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
483 _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
484}
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200485EXPORT_SYMBOL_GPL(rt2800_write_tx_data);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200486
Ivo van Doorn74861922010-07-11 12:23:50 +0200487static int rt2800_agc_to_rssi(struct rt2x00_dev *rt2x00dev, int rxwi_w2)
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200488{
Ivo van Doorn74861922010-07-11 12:23:50 +0200489 int rssi0 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI0);
490 int rssi1 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI1);
491 int rssi2 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI2);
492 u16 eeprom;
493 u8 offset0;
494 u8 offset1;
495 u8 offset2;
496
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200497 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Ivo van Doorn74861922010-07-11 12:23:50 +0200498 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &eeprom);
499 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET0);
500 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET1);
501 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
502 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_OFFSET2);
503 } else {
504 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &eeprom);
505 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET0);
506 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET1);
507 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
508 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_OFFSET2);
509 }
510
511 /*
512 * Convert the value from the descriptor into the RSSI value
513 * If the value in the descriptor is 0, it is considered invalid
514 * and the default (extremely low) rssi value is assumed
515 */
516 rssi0 = (rssi0) ? (-12 - offset0 - rt2x00dev->lna_gain - rssi0) : -128;
517 rssi1 = (rssi1) ? (-12 - offset1 - rt2x00dev->lna_gain - rssi1) : -128;
518 rssi2 = (rssi2) ? (-12 - offset2 - rt2x00dev->lna_gain - rssi2) : -128;
519
520 /*
521 * mac80211 only accepts a single RSSI value. Calculating the
522 * average doesn't deliver a fair answer either since -60:-60 would
523 * be considered equally good as -50:-70 while the second is the one
524 * which gives less energy...
525 */
526 rssi0 = max(rssi0, rssi1);
527 return max(rssi0, rssi2);
528}
529
530void rt2800_process_rxwi(struct queue_entry *entry,
531 struct rxdone_entry_desc *rxdesc)
532{
533 __le32 *rxwi = (__le32 *) entry->skb->data;
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200534 u32 word;
535
536 rt2x00_desc_read(rxwi, 0, &word);
537
538 rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
539 rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
540
541 rt2x00_desc_read(rxwi, 1, &word);
542
543 if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
544 rxdesc->flags |= RX_FLAG_SHORT_GI;
545
546 if (rt2x00_get_field32(word, RXWI_W1_BW))
547 rxdesc->flags |= RX_FLAG_40MHZ;
548
549 /*
550 * Detect RX rate, always use MCS as signal type.
551 */
552 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
553 rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
554 rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
555
556 /*
557 * Mask of 0x8 bit to remove the short preamble flag.
558 */
559 if (rxdesc->rate_mode == RATE_MODE_CCK)
560 rxdesc->signal &= ~0x8;
561
562 rt2x00_desc_read(rxwi, 2, &word);
563
Ivo van Doorn74861922010-07-11 12:23:50 +0200564 /*
565 * Convert descriptor AGC value to RSSI value.
566 */
567 rxdesc->rssi = rt2800_agc_to_rssi(entry->queue->rt2x00dev, word);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200568
569 /*
570 * Remove RXWI descriptor from start of buffer.
571 */
Ivo van Doorn74861922010-07-11 12:23:50 +0200572 skb_pull(entry->skb, RXWI_DESC_SIZE);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200573}
574EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
575
Ivo van Doorn96481b22010-08-06 20:47:57 +0200576void rt2800_txdone(struct rt2x00_dev *rt2x00dev)
577{
578 struct data_queue *queue;
579 struct queue_entry *entry;
580 __le32 *txwi;
581 struct txdone_entry_desc txdesc;
582 u32 word;
583 u32 reg;
584 int wcid, ack, pid, tx_wcid, tx_ack, tx_pid;
585 u16 mcs, real_mcs;
586 int i;
587
588 /*
589 * TX_STA_FIFO is a stack of X entries, hence read TX_STA_FIFO
590 * at most X times and also stop processing once the TX_STA_FIFO_VALID
591 * flag is not set anymore.
592 *
593 * The legacy drivers use X=TX_RING_SIZE but state in a comment
594 * that the TX_STA_FIFO stack has a size of 16. We stick to our
595 * tx ring size for now.
596 */
597 for (i = 0; i < TX_ENTRIES; i++) {
598 rt2800_register_read(rt2x00dev, TX_STA_FIFO, &reg);
599 if (!rt2x00_get_field32(reg, TX_STA_FIFO_VALID))
600 break;
601
602 wcid = rt2x00_get_field32(reg, TX_STA_FIFO_WCID);
603 ack = rt2x00_get_field32(reg, TX_STA_FIFO_TX_ACK_REQUIRED);
604 pid = rt2x00_get_field32(reg, TX_STA_FIFO_PID_TYPE);
605
606 /*
607 * Skip this entry when it contains an invalid
608 * queue identication number.
609 */
610 if (pid <= 0 || pid > QID_RX)
611 continue;
612
613 queue = rt2x00queue_get_queue(rt2x00dev, pid - 1);
614 if (unlikely(!queue))
615 continue;
616
617 /*
618 * Inside each queue, we process each entry in a chronological
619 * order. We first check that the queue is not empty.
620 */
621 entry = NULL;
622 while (!rt2x00queue_empty(queue)) {
623 entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
624 if (!test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
625 break;
626
627 rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
628 }
629
630 if (!entry || rt2x00queue_empty(queue))
631 break;
632
633 /*
634 * Check if we got a match by looking at WCID/ACK/PID
635 * fields
636 */
637 txwi = rt2800_drv_get_txwi(entry);
638
639 rt2x00_desc_read(txwi, 1, &word);
640 tx_wcid = rt2x00_get_field32(word, TXWI_W1_WIRELESS_CLI_ID);
641 tx_ack = rt2x00_get_field32(word, TXWI_W1_ACK);
642 tx_pid = rt2x00_get_field32(word, TXWI_W1_PACKETID);
643
644 if ((wcid != tx_wcid) || (ack != tx_ack) || (pid != tx_pid))
645 WARNING(rt2x00dev, "invalid TX_STA_FIFO content");
646
647 /*
648 * Obtain the status about this packet.
649 */
650 txdesc.flags = 0;
651 rt2x00_desc_read(txwi, 0, &word);
652 mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
Ivo van Doorn96481b22010-08-06 20:47:57 +0200653 real_mcs = rt2x00_get_field32(reg, TX_STA_FIFO_MCS);
654
655 /*
656 * Ralink has a retry mechanism using a global fallback
657 * table. We setup this fallback table to try the immediate
658 * lower rate for all rates. In the TX_STA_FIFO, the MCS field
659 * always contains the MCS used for the last transmission, be
660 * it successful or not.
661 */
662 if (rt2x00_get_field32(reg, TX_STA_FIFO_TX_SUCCESS)) {
663 /*
664 * Transmission succeeded. The number of retries is
665 * mcs - real_mcs
666 */
667 __set_bit(TXDONE_SUCCESS, &txdesc.flags);
668 txdesc.retry = ((mcs > real_mcs) ? mcs - real_mcs : 0);
669 } else {
670 /*
671 * Transmission failed. The number of retries is
672 * always 7 in this case (for a total number of 8
673 * frames sent).
674 */
675 __set_bit(TXDONE_FAILURE, &txdesc.flags);
676 txdesc.retry = rt2x00dev->long_retry;
677 }
678
679 /*
680 * the frame was retried at least once
681 * -> hw used fallback rates
682 */
683 if (txdesc.retry)
684 __set_bit(TXDONE_FALLBACK, &txdesc.flags);
685
686 rt2x00lib_txdone(entry, &txdesc);
687 }
688}
689EXPORT_SYMBOL_GPL(rt2800_txdone);
690
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200691void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
692{
693 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
694 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
695 unsigned int beacon_base;
696 u32 reg;
697
698 /*
699 * Disable beaconing while we are reloading the beacon data,
700 * otherwise we might be sending out invalid data.
701 */
702 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
703 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
704 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
705
706 /*
707 * Add space for the TXWI in front of the skb.
708 */
709 skb_push(entry->skb, TXWI_DESC_SIZE);
710 memset(entry->skb, 0, TXWI_DESC_SIZE);
711
712 /*
713 * Register descriptor details in skb frame descriptor.
714 */
715 skbdesc->flags |= SKBDESC_DESC_IN_SKB;
716 skbdesc->desc = entry->skb->data;
717 skbdesc->desc_len = TXWI_DESC_SIZE;
718
719 /*
720 * Add the TXWI for the beacon to the skb.
721 */
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200722 rt2800_write_tx_data(entry, txdesc);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200723
724 /*
725 * Dump beacon to userspace through debugfs.
726 */
727 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
728
729 /*
730 * Write entire beacon with TXWI to register.
731 */
732 beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
733 rt2800_register_multiwrite(rt2x00dev, beacon_base,
734 entry->skb->data, entry->skb->len);
735
736 /*
737 * Enable beaconing again.
738 */
739 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
740 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
741 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
742 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
743
744 /*
745 * Clean up beacon skb.
746 */
747 dev_kfree_skb_any(entry->skb);
748 entry->skb = NULL;
749}
Ivo van Doorn50e888e2010-07-11 12:26:12 +0200750EXPORT_SYMBOL_GPL(rt2800_write_beacon);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200751
Helmut Schaafdb87252010-06-29 21:48:06 +0200752static void inline rt2800_clear_beacon(struct rt2x00_dev *rt2x00dev,
753 unsigned int beacon_base)
754{
755 int i;
756
757 /*
758 * For the Beacon base registers we only need to clear
759 * the whole TXWI which (when set to 0) will invalidate
760 * the entire beacon.
761 */
762 for (i = 0; i < TXWI_DESC_SIZE; i += sizeof(__le32))
763 rt2800_register_write(rt2x00dev, beacon_base + i, 0);
764}
765
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100766#ifdef CONFIG_RT2X00_LIB_DEBUGFS
767const struct rt2x00debug rt2800_rt2x00debug = {
768 .owner = THIS_MODULE,
769 .csr = {
770 .read = rt2800_register_read,
771 .write = rt2800_register_write,
772 .flags = RT2X00DEBUGFS_OFFSET,
773 .word_base = CSR_REG_BASE,
774 .word_size = sizeof(u32),
775 .word_count = CSR_REG_SIZE / sizeof(u32),
776 },
777 .eeprom = {
778 .read = rt2x00_eeprom_read,
779 .write = rt2x00_eeprom_write,
780 .word_base = EEPROM_BASE,
781 .word_size = sizeof(u16),
782 .word_count = EEPROM_SIZE / sizeof(u16),
783 },
784 .bbp = {
785 .read = rt2800_bbp_read,
786 .write = rt2800_bbp_write,
787 .word_base = BBP_BASE,
788 .word_size = sizeof(u8),
789 .word_count = BBP_SIZE / sizeof(u8),
790 },
791 .rf = {
792 .read = rt2x00_rf_read,
793 .write = rt2800_rf_write,
794 .word_base = RF_BASE,
795 .word_size = sizeof(u32),
796 .word_count = RF_SIZE / sizeof(u32),
797 },
798};
799EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
800#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
801
802int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
803{
804 u32 reg;
805
806 rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
807 return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
808}
809EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
810
811#ifdef CONFIG_RT2X00_LIB_LEDS
812static void rt2800_brightness_set(struct led_classdev *led_cdev,
813 enum led_brightness brightness)
814{
815 struct rt2x00_led *led =
816 container_of(led_cdev, struct rt2x00_led, led_dev);
817 unsigned int enabled = brightness != LED_OFF;
818 unsigned int bg_mode =
819 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
820 unsigned int polarity =
821 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
822 EEPROM_FREQ_LED_POLARITY);
823 unsigned int ledmode =
824 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
825 EEPROM_FREQ_LED_MODE);
826
827 if (led->type == LED_TYPE_RADIO) {
828 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
829 enabled ? 0x20 : 0);
830 } else if (led->type == LED_TYPE_ASSOC) {
831 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
832 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
833 } else if (led->type == LED_TYPE_QUALITY) {
834 /*
835 * The brightness is divided into 6 levels (0 - 5),
836 * The specs tell us the following levels:
837 * 0, 1 ,3, 7, 15, 31
838 * to determine the level in a simple way we can simply
839 * work with bitshifting:
840 * (1 << level) - 1
841 */
842 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
843 (1 << brightness / (LED_FULL / 6)) - 1,
844 polarity);
845 }
846}
847
848static int rt2800_blink_set(struct led_classdev *led_cdev,
849 unsigned long *delay_on, unsigned long *delay_off)
850{
851 struct rt2x00_led *led =
852 container_of(led_cdev, struct rt2x00_led, led_dev);
853 u32 reg;
854
855 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
856 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
857 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100858 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
859
860 return 0;
861}
862
Gertjan van Wingerdeb3579d62009-12-30 11:36:34 +0100863static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100864 struct rt2x00_led *led, enum led_type type)
865{
866 led->rt2x00dev = rt2x00dev;
867 led->type = type;
868 led->led_dev.brightness_set = rt2800_brightness_set;
869 led->led_dev.blink_set = rt2800_blink_set;
870 led->flags = LED_INITIALIZED;
871}
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100872#endif /* CONFIG_RT2X00_LIB_LEDS */
873
874/*
875 * Configuration handlers.
876 */
877static void rt2800_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
878 struct rt2x00lib_crypto *crypto,
879 struct ieee80211_key_conf *key)
880{
881 struct mac_wcid_entry wcid_entry;
882 struct mac_iveiv_entry iveiv_entry;
883 u32 offset;
884 u32 reg;
885
886 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
887
Ivo van Doorne4a0ab32010-06-14 22:14:19 +0200888 if (crypto->cmd == SET_KEY) {
889 rt2800_register_read(rt2x00dev, offset, &reg);
890 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
891 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
892 /*
893 * Both the cipher as the BSS Idx numbers are split in a main
894 * value of 3 bits, and a extended field for adding one additional
895 * bit to the value.
896 */
897 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
898 (crypto->cipher & 0x7));
899 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
900 (crypto->cipher & 0x8) >> 3);
901 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
902 (crypto->bssidx & 0x7));
903 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
904 (crypto->bssidx & 0x8) >> 3);
905 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
906 rt2800_register_write(rt2x00dev, offset, reg);
907 } else {
908 rt2800_register_write(rt2x00dev, offset, 0);
909 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100910
911 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
912
913 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
914 if ((crypto->cipher == CIPHER_TKIP) ||
915 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
916 (crypto->cipher == CIPHER_AES))
917 iveiv_entry.iv[3] |= 0x20;
918 iveiv_entry.iv[3] |= key->keyidx << 6;
919 rt2800_register_multiwrite(rt2x00dev, offset,
920 &iveiv_entry, sizeof(iveiv_entry));
921
922 offset = MAC_WCID_ENTRY(key->hw_key_idx);
923
924 memset(&wcid_entry, 0, sizeof(wcid_entry));
925 if (crypto->cmd == SET_KEY)
926 memcpy(&wcid_entry, crypto->address, ETH_ALEN);
927 rt2800_register_multiwrite(rt2x00dev, offset,
928 &wcid_entry, sizeof(wcid_entry));
929}
930
931int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
932 struct rt2x00lib_crypto *crypto,
933 struct ieee80211_key_conf *key)
934{
935 struct hw_key_entry key_entry;
936 struct rt2x00_field32 field;
937 u32 offset;
938 u32 reg;
939
940 if (crypto->cmd == SET_KEY) {
941 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
942
943 memcpy(key_entry.key, crypto->key,
944 sizeof(key_entry.key));
945 memcpy(key_entry.tx_mic, crypto->tx_mic,
946 sizeof(key_entry.tx_mic));
947 memcpy(key_entry.rx_mic, crypto->rx_mic,
948 sizeof(key_entry.rx_mic));
949
950 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
951 rt2800_register_multiwrite(rt2x00dev, offset,
952 &key_entry, sizeof(key_entry));
953 }
954
955 /*
956 * The cipher types are stored over multiple registers
957 * starting with SHARED_KEY_MODE_BASE each word will have
958 * 32 bits and contains the cipher types for 2 bssidx each.
959 * Using the correct defines correctly will cause overhead,
960 * so just calculate the correct offset.
961 */
962 field.bit_offset = 4 * (key->hw_key_idx % 8);
963 field.bit_mask = 0x7 << field.bit_offset;
964
965 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
966
967 rt2800_register_read(rt2x00dev, offset, &reg);
968 rt2x00_set_field32(&reg, field,
969 (crypto->cmd == SET_KEY) * crypto->cipher);
970 rt2800_register_write(rt2x00dev, offset, reg);
971
972 /*
973 * Update WCID information
974 */
975 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
976
977 return 0;
978}
979EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
980
981int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
982 struct rt2x00lib_crypto *crypto,
983 struct ieee80211_key_conf *key)
984{
985 struct hw_key_entry key_entry;
986 u32 offset;
987
988 if (crypto->cmd == SET_KEY) {
989 /*
990 * 1 pairwise key is possible per AID, this means that the AID
991 * equals our hw_key_idx. Make sure the WCID starts _after_ the
992 * last possible shared key entry.
993 */
994 if (crypto->aid > (256 - 32))
995 return -ENOSPC;
996
997 key->hw_key_idx = 32 + crypto->aid;
998
999 memcpy(key_entry.key, crypto->key,
1000 sizeof(key_entry.key));
1001 memcpy(key_entry.tx_mic, crypto->tx_mic,
1002 sizeof(key_entry.tx_mic));
1003 memcpy(key_entry.rx_mic, crypto->rx_mic,
1004 sizeof(key_entry.rx_mic));
1005
1006 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
1007 rt2800_register_multiwrite(rt2x00dev, offset,
1008 &key_entry, sizeof(key_entry));
1009 }
1010
1011 /*
1012 * Update WCID information
1013 */
1014 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
1015
1016 return 0;
1017}
1018EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
1019
1020void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
1021 const unsigned int filter_flags)
1022{
1023 u32 reg;
1024
1025 /*
1026 * Start configuration steps.
1027 * Note that the version error will always be dropped
1028 * and broadcast frames will always be accepted since
1029 * there is no filter for it at this time.
1030 */
1031 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
1032 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
1033 !(filter_flags & FIF_FCSFAIL));
1034 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
1035 !(filter_flags & FIF_PLCPFAIL));
1036 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
1037 !(filter_flags & FIF_PROMISC_IN_BSS));
1038 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
1039 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
1040 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
1041 !(filter_flags & FIF_ALLMULTI));
1042 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
1043 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
1044 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
1045 !(filter_flags & FIF_CONTROL));
1046 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
1047 !(filter_flags & FIF_CONTROL));
1048 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
1049 !(filter_flags & FIF_CONTROL));
1050 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
1051 !(filter_flags & FIF_CONTROL));
1052 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
1053 !(filter_flags & FIF_CONTROL));
1054 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
1055 !(filter_flags & FIF_PSPOLL));
1056 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
1057 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
1058 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
1059 !(filter_flags & FIF_CONTROL));
1060 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
1061}
1062EXPORT_SYMBOL_GPL(rt2800_config_filter);
1063
1064void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
1065 struct rt2x00intf_conf *conf, const unsigned int flags)
1066{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001067 u32 reg;
1068
1069 if (flags & CONFIG_UPDATE_TYPE) {
1070 /*
1071 * Clear current synchronisation setup.
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001072 */
Helmut Schaafdb87252010-06-29 21:48:06 +02001073 rt2800_clear_beacon(rt2x00dev,
1074 HW_BEACON_OFFSET(intf->beacon->entry_idx));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001075 /*
1076 * Enable synchronisation.
1077 */
1078 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1079 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
1080 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Josef Bacik6a62e5ef2009-11-15 21:33:18 -05001081 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE,
Helmut Schaaab8966d2010-07-11 12:30:13 +02001082 (conf->sync == TSF_SYNC_ADHOC ||
1083 conf->sync == TSF_SYNC_AP_NONE));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001084 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Helmut Schaa9f926fb2010-07-11 12:28:23 +02001085
1086 /*
1087 * Enable pre tbtt interrupt for beaconing modes
1088 */
1089 rt2800_register_read(rt2x00dev, INT_TIMER_EN, &reg);
1090 rt2x00_set_field32(&reg, INT_TIMER_EN_PRE_TBTT_TIMER,
Helmut Schaaab8966d2010-07-11 12:30:13 +02001091 (conf->sync == TSF_SYNC_AP_NONE));
Helmut Schaa9f926fb2010-07-11 12:28:23 +02001092 rt2800_register_write(rt2x00dev, INT_TIMER_EN, reg);
1093
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001094 }
1095
1096 if (flags & CONFIG_UPDATE_MAC) {
1097 reg = le32_to_cpu(conf->mac[1]);
1098 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
1099 conf->mac[1] = cpu_to_le32(reg);
1100
1101 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
1102 conf->mac, sizeof(conf->mac));
1103 }
1104
1105 if (flags & CONFIG_UPDATE_BSSID) {
1106 reg = le32_to_cpu(conf->bssid[1]);
Ivo van Doornd440cb92010-06-29 21:45:31 +02001107 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
1108 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 7);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001109 conf->bssid[1] = cpu_to_le32(reg);
1110
1111 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
1112 conf->bssid, sizeof(conf->bssid));
1113 }
1114}
1115EXPORT_SYMBOL_GPL(rt2800_config_intf);
1116
1117void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp)
1118{
1119 u32 reg;
1120
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001121 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1122 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
1123 !!erp->short_preamble);
1124 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
1125 !!erp->short_preamble);
1126 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1127
1128 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
1129 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
1130 erp->cts_protection ? 2 : 0);
1131 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1132
1133 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
1134 erp->basic_rates);
1135 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1136
1137 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1138 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, erp->slot_time);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001139 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
1140
1141 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001142 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001143 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1144
1145 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1146 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
1147 erp->beacon_int * 16);
1148 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1149}
1150EXPORT_SYMBOL_GPL(rt2800_config_erp);
1151
1152void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
1153{
1154 u8 r1;
1155 u8 r3;
1156
1157 rt2800_bbp_read(rt2x00dev, 1, &r1);
1158 rt2800_bbp_read(rt2x00dev, 3, &r3);
1159
1160 /*
1161 * Configure the TX antenna.
1162 */
1163 switch ((int)ant->tx) {
1164 case 1:
1165 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001166 break;
1167 case 2:
1168 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
1169 break;
1170 case 3:
Ivo van Doorne22557f2010-06-29 21:49:05 +02001171 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001172 break;
1173 }
1174
1175 /*
1176 * Configure the RX antenna.
1177 */
1178 switch ((int)ant->rx) {
1179 case 1:
1180 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
1181 break;
1182 case 2:
1183 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
1184 break;
1185 case 3:
1186 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
1187 break;
1188 }
1189
1190 rt2800_bbp_write(rt2x00dev, 3, r3);
1191 rt2800_bbp_write(rt2x00dev, 1, r1);
1192}
1193EXPORT_SYMBOL_GPL(rt2800_config_ant);
1194
1195static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
1196 struct rt2x00lib_conf *libconf)
1197{
1198 u16 eeprom;
1199 short lna_gain;
1200
1201 if (libconf->rf.channel <= 14) {
1202 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1203 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
1204 } else if (libconf->rf.channel <= 64) {
1205 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1206 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
1207 } else if (libconf->rf.channel <= 128) {
1208 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
1209 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
1210 } else {
1211 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
1212 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
1213 }
1214
1215 rt2x00dev->lna_gain = lna_gain;
1216}
1217
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001218static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
1219 struct ieee80211_conf *conf,
1220 struct rf_channel *rf,
1221 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001222{
1223 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
1224
1225 if (rt2x00dev->default_ant.tx == 1)
1226 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
1227
1228 if (rt2x00dev->default_ant.rx == 1) {
1229 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
1230 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1231 } else if (rt2x00dev->default_ant.rx == 2)
1232 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1233
1234 if (rf->channel > 14) {
1235 /*
1236 * When TX power is below 0, we should increase it by 7 to
1237 * make it a positive value (Minumum value is -7).
1238 * However this means that values between 0 and 7 have
1239 * double meaning, and we should set a 7DBm boost flag.
1240 */
1241 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
1242 (info->tx_power1 >= 0));
1243
1244 if (info->tx_power1 < 0)
1245 info->tx_power1 += 7;
1246
1247 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A,
1248 TXPOWER_A_TO_DEV(info->tx_power1));
1249
1250 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
1251 (info->tx_power2 >= 0));
1252
1253 if (info->tx_power2 < 0)
1254 info->tx_power2 += 7;
1255
1256 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A,
1257 TXPOWER_A_TO_DEV(info->tx_power2));
1258 } else {
1259 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G,
1260 TXPOWER_G_TO_DEV(info->tx_power1));
1261 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G,
1262 TXPOWER_G_TO_DEV(info->tx_power2));
1263 }
1264
1265 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
1266
1267 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1268 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1269 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1270 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1271
1272 udelay(200);
1273
1274 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1275 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1276 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
1277 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1278
1279 udelay(200);
1280
1281 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1282 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1283 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1284 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1285}
1286
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001287static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
1288 struct ieee80211_conf *conf,
1289 struct rf_channel *rf,
1290 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001291{
1292 u8 rfcsr;
1293
1294 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Gertjan van Wingerde41a26172009-11-09 22:59:04 +01001295 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001296
1297 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +02001298 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001299 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1300
1301 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
1302 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
1303 TXPOWER_G_TO_DEV(info->tx_power1));
1304 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
1305
Helmut Schaa5a673962010-04-23 15:54:43 +02001306 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
1307 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
1308 TXPOWER_G_TO_DEV(info->tx_power2));
1309 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
1310
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001311 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
1312 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
1313 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
1314
1315 rt2800_rfcsr_write(rt2x00dev, 24,
1316 rt2x00dev->calibration[conf_is_ht40(conf)]);
1317
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001318 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001319 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001320 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001321}
1322
1323static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
1324 struct ieee80211_conf *conf,
1325 struct rf_channel *rf,
1326 struct channel_info *info)
1327{
1328 u32 reg;
1329 unsigned int tx_pin;
1330 u8 bbp;
1331
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001332 if (rt2x00_rf(rt2x00dev, RF2020) ||
1333 rt2x00_rf(rt2x00dev, RF3020) ||
1334 rt2x00_rf(rt2x00dev, RF3021) ||
1335 rt2x00_rf(rt2x00dev, RF3022))
1336 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerdefa6f6322009-11-09 22:59:58 +01001337 else
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001338 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001339
1340 /*
1341 * Change BBP settings
1342 */
1343 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
1344 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
1345 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
1346 rt2800_bbp_write(rt2x00dev, 86, 0);
1347
1348 if (rf->channel <= 14) {
1349 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
1350 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1351 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1352 } else {
1353 rt2800_bbp_write(rt2x00dev, 82, 0x84);
1354 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1355 }
1356 } else {
1357 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
1358
1359 if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
1360 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1361 else
1362 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1363 }
1364
1365 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001366 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001367 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
1368 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
1369 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
1370
1371 tx_pin = 0;
1372
1373 /* Turn on unused PA or LNA when not using 1T or 1R */
1374 if (rt2x00dev->default_ant.tx != 1) {
1375 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
1376 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
1377 }
1378
1379 /* Turn on unused PA or LNA when not using 1T or 1R */
1380 if (rt2x00dev->default_ant.rx != 1) {
1381 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
1382 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
1383 }
1384
1385 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
1386 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
1387 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
1388 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
1389 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
1390 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
1391
1392 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
1393
1394 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1395 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
1396 rt2800_bbp_write(rt2x00dev, 4, bbp);
1397
1398 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001399 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001400 rt2800_bbp_write(rt2x00dev, 3, bbp);
1401
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001402 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001403 if (conf_is_ht40(conf)) {
1404 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
1405 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1406 rt2800_bbp_write(rt2x00dev, 73, 0x16);
1407 } else {
1408 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1409 rt2800_bbp_write(rt2x00dev, 70, 0x08);
1410 rt2800_bbp_write(rt2x00dev, 73, 0x11);
1411 }
1412 }
1413
1414 msleep(1);
1415}
1416
1417static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
Helmut Schaa5e846002010-07-11 12:23:09 +02001418 const int max_txpower)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001419{
Helmut Schaa5e846002010-07-11 12:23:09 +02001420 u8 txpower;
1421 u8 max_value = (u8)max_txpower;
1422 u16 eeprom;
1423 int i;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001424 u32 reg;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001425 u8 r1;
Helmut Schaa5e846002010-07-11 12:23:09 +02001426 u32 offset;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001427
Helmut Schaa5e846002010-07-11 12:23:09 +02001428 /*
1429 * set to normal tx power mode: +/- 0dBm
1430 */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001431 rt2800_bbp_read(rt2x00dev, 1, &r1);
Helmut Schaaa3f84ca2010-06-14 22:11:32 +02001432 rt2x00_set_field8(&r1, BBP1_TX_POWER, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001433 rt2800_bbp_write(rt2x00dev, 1, r1);
1434
Helmut Schaa5e846002010-07-11 12:23:09 +02001435 /*
1436 * The eeprom contains the tx power values for each rate. These
1437 * values map to 100% tx power. Each 16bit word contains four tx
1438 * power values and the order is the same as used in the TX_PWR_CFG
1439 * registers.
1440 */
1441 offset = TX_PWR_CFG_0;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001442
Helmut Schaa5e846002010-07-11 12:23:09 +02001443 for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
1444 /* just to be safe */
1445 if (offset > TX_PWR_CFG_4)
1446 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001447
Helmut Schaa5e846002010-07-11 12:23:09 +02001448 rt2800_register_read(rt2x00dev, offset, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001449
Helmut Schaa5e846002010-07-11 12:23:09 +02001450 /* read the next four txpower values */
1451 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i,
1452 &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001453
Helmut Schaa5e846002010-07-11 12:23:09 +02001454 /* TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
1455 * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
1456 * TX_PWR_CFG_4: unknown */
1457 txpower = rt2x00_get_field16(eeprom,
1458 EEPROM_TXPOWER_BYRATE_RATE0);
1459 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0,
1460 min(txpower, max_value));
1461
1462 /* TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
1463 * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
1464 * TX_PWR_CFG_4: unknown */
1465 txpower = rt2x00_get_field16(eeprom,
1466 EEPROM_TXPOWER_BYRATE_RATE1);
1467 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1,
1468 min(txpower, max_value));
1469
1470 /* TX_PWR_CFG_0: 55MBS, TX_PWR_CFG_1: 48MBS,
1471 * TX_PWR_CFG_2: MCS6, TX_PWR_CFG_3: MCS14,
1472 * TX_PWR_CFG_4: unknown */
1473 txpower = rt2x00_get_field16(eeprom,
1474 EEPROM_TXPOWER_BYRATE_RATE2);
1475 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2,
1476 min(txpower, max_value));
1477
1478 /* TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
1479 * TX_PWR_CFG_2: MCS7, TX_PWR_CFG_3: MCS15,
1480 * TX_PWR_CFG_4: unknown */
1481 txpower = rt2x00_get_field16(eeprom,
1482 EEPROM_TXPOWER_BYRATE_RATE3);
1483 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3,
1484 min(txpower, max_value));
1485
1486 /* read the next four txpower values */
1487 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i + 1,
1488 &eeprom);
1489
1490 /* TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
1491 * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
1492 * TX_PWR_CFG_4: unknown */
1493 txpower = rt2x00_get_field16(eeprom,
1494 EEPROM_TXPOWER_BYRATE_RATE0);
1495 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4,
1496 min(txpower, max_value));
1497
1498 /* TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
1499 * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
1500 * TX_PWR_CFG_4: unknown */
1501 txpower = rt2x00_get_field16(eeprom,
1502 EEPROM_TXPOWER_BYRATE_RATE1);
1503 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5,
1504 min(txpower, max_value));
1505
1506 /* TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
1507 * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
1508 * TX_PWR_CFG_4: unknown */
1509 txpower = rt2x00_get_field16(eeprom,
1510 EEPROM_TXPOWER_BYRATE_RATE2);
1511 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6,
1512 min(txpower, max_value));
1513
1514 /* TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
1515 * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
1516 * TX_PWR_CFG_4: unknown */
1517 txpower = rt2x00_get_field16(eeprom,
1518 EEPROM_TXPOWER_BYRATE_RATE3);
1519 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7,
1520 min(txpower, max_value));
1521
1522 rt2800_register_write(rt2x00dev, offset, reg);
1523
1524 /* next TX_PWR_CFG register */
1525 offset += 4;
1526 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001527}
1528
1529static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
1530 struct rt2x00lib_conf *libconf)
1531{
1532 u32 reg;
1533
1534 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1535 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
1536 libconf->conf->short_frame_max_tx_count);
1537 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
1538 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001539 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1540}
1541
1542static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
1543 struct rt2x00lib_conf *libconf)
1544{
1545 enum dev_state state =
1546 (libconf->conf->flags & IEEE80211_CONF_PS) ?
1547 STATE_SLEEP : STATE_AWAKE;
1548 u32 reg;
1549
1550 if (state == STATE_SLEEP) {
1551 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
1552
1553 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1554 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
1555 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
1556 libconf->conf->listen_interval - 1);
1557 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
1558 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
1559
1560 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
1561 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001562 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1563 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
1564 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
1565 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
1566 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02001567
1568 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001569 }
1570}
1571
1572void rt2800_config(struct rt2x00_dev *rt2x00dev,
1573 struct rt2x00lib_conf *libconf,
1574 const unsigned int flags)
1575{
1576 /* Always recalculate LNA gain before changing configuration */
1577 rt2800_config_lna_gain(rt2x00dev, libconf);
1578
1579 if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
1580 rt2800_config_channel(rt2x00dev, libconf->conf,
1581 &libconf->rf, &libconf->channel);
1582 if (flags & IEEE80211_CONF_CHANGE_POWER)
1583 rt2800_config_txpower(rt2x00dev, libconf->conf->power_level);
1584 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
1585 rt2800_config_retry_limit(rt2x00dev, libconf);
1586 if (flags & IEEE80211_CONF_CHANGE_PS)
1587 rt2800_config_ps(rt2x00dev, libconf);
1588}
1589EXPORT_SYMBOL_GPL(rt2800_config);
1590
1591/*
1592 * Link tuning
1593 */
1594void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1595{
1596 u32 reg;
1597
1598 /*
1599 * Update FCS error count from register.
1600 */
1601 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1602 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
1603}
1604EXPORT_SYMBOL_GPL(rt2800_link_stats);
1605
1606static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
1607{
1608 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001609 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001610 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001611 rt2x00_rt(rt2x00dev, RT3090) ||
1612 rt2x00_rt(rt2x00dev, RT3390))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001613 return 0x1c + (2 * rt2x00dev->lna_gain);
1614 else
1615 return 0x2e + rt2x00dev->lna_gain;
1616 }
1617
1618 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
1619 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
1620 else
1621 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
1622}
1623
1624static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
1625 struct link_qual *qual, u8 vgc_level)
1626{
1627 if (qual->vgc_level != vgc_level) {
1628 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
1629 qual->vgc_level = vgc_level;
1630 qual->vgc_level_reg = vgc_level;
1631 }
1632}
1633
1634void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1635{
1636 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
1637}
1638EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
1639
1640void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
1641 const u32 count)
1642{
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001643 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001644 return;
1645
1646 /*
1647 * When RSSI is better then -80 increase VGC level with 0x10
1648 */
1649 rt2800_set_vgc(rt2x00dev, qual,
1650 rt2800_get_default_vgc(rt2x00dev) +
1651 ((qual->rssi > -80) * 0x10));
1652}
1653EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001654
1655/*
1656 * Initialization functions.
1657 */
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02001658static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001659{
1660 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001661 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001662 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001663 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001664
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001665 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1666 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1667 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1668 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1669 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1670 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
1671 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1672
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001673 ret = rt2800_drv_init_registers(rt2x00dev);
1674 if (ret)
1675 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001676
1677 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
1678 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
1679 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
1680 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
1681 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
1682 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
1683
1684 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
1685 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
1686 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
1687 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
1688 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
1689 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
1690
1691 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
1692 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1693
1694 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1695
1696 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Helmut Schaa8544df32010-07-11 12:29:49 +02001697 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001698 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
1699 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
1700 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
1701 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1702 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
1703 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1704
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001705 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
1706
1707 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1708 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
1709 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
1710 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
1711
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001712 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001713 rt2x00_rt(rt2x00dev, RT3090) ||
1714 rt2x00_rt(rt2x00dev, RT3390)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001715 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1716 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001717 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001718 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
1719 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001720 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1721 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
1722 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1723 0x0000002c);
1724 else
1725 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1726 0x0000000f);
1727 } else {
1728 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1729 }
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001730 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001731 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001732
1733 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
1734 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1735 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
1736 } else {
1737 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1738 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1739 }
Helmut Schaac295a812010-06-03 10:52:13 +02001740 } else if (rt2800_is_305x_soc(rt2x00dev)) {
1741 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1742 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1743 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000001f);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001744 } else {
1745 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
1746 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1747 }
1748
1749 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
1750 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
1751 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
1752 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
1753 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
1754 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
1755 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
1756 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
1757 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
1758 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
1759
1760 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
1761 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001762 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001763 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
1764 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
1765
1766 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
1767 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001768 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01001769 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001770 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001771 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
1772 else
1773 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
1774 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
1775 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
1776 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
1777
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001778 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1779 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
1780 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
1781 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
1782 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
1783 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
1784 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
1785 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
1786 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1787
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001788 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
1789
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001790 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1791 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
1792 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
1793 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
1794 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
1795 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
1796 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
1797 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1798
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001799 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1800 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001801 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001802 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
1803 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001804 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001805 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
1806 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
1807 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1808
1809 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001810 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001811 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
1812 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
1813 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1814 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1815 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001816 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001817 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001818 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1819 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001820 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
1821
1822 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001823 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001824 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
1825 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
1826 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1827 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1828 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001829 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001830 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001831 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1832 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001833 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1834
1835 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1836 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
1837 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
1838 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
1839 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1840 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1841 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1842 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1843 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1844 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001845 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001846 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1847
1848 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1849 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001850 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL,
1851 !rt2x00_is_usb(rt2x00dev));
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001852 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
1853 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1854 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1855 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1856 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1857 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1858 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001859 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001860 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1861
1862 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1863 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
1864 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
1865 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
1866 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1867 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1868 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1869 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1870 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1871 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001872 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001873 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1874
1875 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1876 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
1877 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
1878 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
1879 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1880 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1881 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1882 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1883 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1884 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001885 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001886 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1887
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001888 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001889 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
1890
1891 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1892 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1893 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1894 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1895 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1896 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
1897 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
1898 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
1899 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
1900 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
1901 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1902 }
1903
1904 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, 0x0000583f);
1905 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
1906
1907 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
1908 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
1909 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
1910 IEEE80211_MAX_RTS_THRESHOLD);
1911 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
1912 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
1913
1914 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001915
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001916 /*
1917 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
1918 * time should be set to 16. However, the original Ralink driver uses
1919 * 16 for both and indeed using a value of 10 for CCK SIFS results in
1920 * connection problems with 11g + CTS protection. Hence, use the same
1921 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
1922 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001923 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001924 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
1925 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001926 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
1927 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
1928 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
1929 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1930
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001931 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
1932
1933 /*
1934 * ASIC will keep garbage value after boot, clear encryption keys.
1935 */
1936 for (i = 0; i < 4; i++)
1937 rt2800_register_write(rt2x00dev,
1938 SHARED_KEY_MODE_ENTRY(i), 0);
1939
1940 for (i = 0; i < 256; i++) {
1941 u32 wcid[2] = { 0xffffffff, 0x00ffffff };
1942 rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
1943 wcid, sizeof(wcid));
1944
1945 rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
1946 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
1947 }
1948
1949 /*
1950 * Clear all beacons
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001951 */
Helmut Schaafdb87252010-06-29 21:48:06 +02001952 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE0);
1953 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE1);
1954 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE2);
1955 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE3);
1956 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE4);
1957 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE5);
1958 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE6);
1959 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE7);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001960
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001961 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02001962 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
1963 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
1964 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001965 }
1966
1967 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
1968 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
1969 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
1970 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
1971 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
1972 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
1973 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
1974 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
1975 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
1976 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
1977
1978 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
1979 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
1980 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
1981 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
1982 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
1983 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
1984 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
1985 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
1986 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
1987 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
1988
1989 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
1990 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
1991 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
1992 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
1993 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
1994 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
1995 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
1996 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
1997 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
1998 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
1999
2000 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
2001 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
2002 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
2003 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
2004 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
2005 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
2006
2007 /*
2008 * We must clear the error counters.
2009 * These registers are cleared on read,
2010 * so we may pass a useless variable to store the value.
2011 */
2012 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
2013 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
2014 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
2015 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
2016 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
2017 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
2018
Helmut Schaa9f926fb2010-07-11 12:28:23 +02002019 /*
2020 * Setup leadtime for pre tbtt interrupt to 6ms
2021 */
2022 rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
2023 rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
2024 rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
2025
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002026 return 0;
2027}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002028
2029static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
2030{
2031 unsigned int i;
2032 u32 reg;
2033
2034 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
2035 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
2036 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
2037 return 0;
2038
2039 udelay(REGISTER_BUSY_DELAY);
2040 }
2041
2042 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
2043 return -EACCES;
2044}
2045
2046static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
2047{
2048 unsigned int i;
2049 u8 value;
2050
2051 /*
2052 * BBP was enabled after firmware was loaded,
2053 * but we need to reactivate it now.
2054 */
2055 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
2056 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
2057 msleep(1);
2058
2059 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
2060 rt2800_bbp_read(rt2x00dev, 0, &value);
2061 if ((value != 0xff) && (value != 0x00))
2062 return 0;
2063 udelay(REGISTER_BUSY_DELAY);
2064 }
2065
2066 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
2067 return -EACCES;
2068}
2069
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002070static int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002071{
2072 unsigned int i;
2073 u16 eeprom;
2074 u8 reg_id;
2075 u8 value;
2076
2077 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
2078 rt2800_wait_bbp_ready(rt2x00dev)))
2079 return -EACCES;
2080
Helmut Schaabaff8002010-04-28 09:58:59 +02002081 if (rt2800_is_305x_soc(rt2x00dev))
2082 rt2800_bbp_write(rt2x00dev, 31, 0x08);
2083
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002084 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
2085 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002086
2087 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
2088 rt2800_bbp_write(rt2x00dev, 69, 0x16);
2089 rt2800_bbp_write(rt2x00dev, 73, 0x12);
2090 } else {
2091 rt2800_bbp_write(rt2x00dev, 69, 0x12);
2092 rt2800_bbp_write(rt2x00dev, 73, 0x10);
2093 }
2094
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002095 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002096
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002097 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002098 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002099 rt2x00_rt(rt2x00dev, RT3090) ||
2100 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002101 rt2800_bbp_write(rt2x00dev, 79, 0x13);
2102 rt2800_bbp_write(rt2x00dev, 80, 0x05);
2103 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Helmut Schaabaff8002010-04-28 09:58:59 +02002104 } else if (rt2800_is_305x_soc(rt2x00dev)) {
2105 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
2106 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002107 } else {
2108 rt2800_bbp_write(rt2x00dev, 81, 0x37);
2109 }
2110
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002111 rt2800_bbp_write(rt2x00dev, 82, 0x62);
2112 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002113
Gertjan van Wingerde5ed8f452010-06-03 10:51:57 +02002114 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002115 rt2800_bbp_write(rt2x00dev, 84, 0x19);
2116 else
2117 rt2800_bbp_write(rt2x00dev, 84, 0x99);
2118
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002119 rt2800_bbp_write(rt2x00dev, 86, 0x00);
2120 rt2800_bbp_write(rt2x00dev, 91, 0x04);
2121 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002122
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002123 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002124 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002125 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02002126 rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
2127 rt2800_is_305x_soc(rt2x00dev))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002128 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
2129 else
2130 rt2800_bbp_write(rt2x00dev, 103, 0x00);
2131
Helmut Schaabaff8002010-04-28 09:58:59 +02002132 if (rt2800_is_305x_soc(rt2x00dev))
2133 rt2800_bbp_write(rt2x00dev, 105, 0x01);
2134 else
2135 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002136 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002137
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002138 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002139 rt2x00_rt(rt2x00dev, RT3090) ||
2140 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002141 rt2800_bbp_read(rt2x00dev, 138, &value);
2142
2143 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2144 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
2145 value |= 0x20;
2146 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
2147 value &= ~0x02;
2148
2149 rt2800_bbp_write(rt2x00dev, 138, value);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002150 }
2151
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002152
2153 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
2154 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
2155
2156 if (eeprom != 0xffff && eeprom != 0x0000) {
2157 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
2158 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
2159 rt2800_bbp_write(rt2x00dev, reg_id, value);
2160 }
2161 }
2162
2163 return 0;
2164}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002165
2166static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
2167 bool bw40, u8 rfcsr24, u8 filter_target)
2168{
2169 unsigned int i;
2170 u8 bbp;
2171 u8 rfcsr;
2172 u8 passband;
2173 u8 stopband;
2174 u8 overtuned = 0;
2175
2176 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
2177
2178 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2179 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
2180 rt2800_bbp_write(rt2x00dev, 4, bbp);
2181
2182 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
2183 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
2184 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
2185
2186 /*
2187 * Set power & frequency of passband test tone
2188 */
2189 rt2800_bbp_write(rt2x00dev, 24, 0);
2190
2191 for (i = 0; i < 100; i++) {
2192 rt2800_bbp_write(rt2x00dev, 25, 0x90);
2193 msleep(1);
2194
2195 rt2800_bbp_read(rt2x00dev, 55, &passband);
2196 if (passband)
2197 break;
2198 }
2199
2200 /*
2201 * Set power & frequency of stopband test tone
2202 */
2203 rt2800_bbp_write(rt2x00dev, 24, 0x06);
2204
2205 for (i = 0; i < 100; i++) {
2206 rt2800_bbp_write(rt2x00dev, 25, 0x90);
2207 msleep(1);
2208
2209 rt2800_bbp_read(rt2x00dev, 55, &stopband);
2210
2211 if ((passband - stopband) <= filter_target) {
2212 rfcsr24++;
2213 overtuned += ((passband - stopband) == filter_target);
2214 } else
2215 break;
2216
2217 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
2218 }
2219
2220 rfcsr24 -= !!overtuned;
2221
2222 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
2223 return rfcsr24;
2224}
2225
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002226static int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002227{
2228 u8 rfcsr;
2229 u8 bbp;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002230 u32 reg;
2231 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002232
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002233 if (!rt2x00_rt(rt2x00dev, RT3070) &&
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002234 !rt2x00_rt(rt2x00dev, RT3071) &&
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002235 !rt2x00_rt(rt2x00dev, RT3090) &&
Helmut Schaa23812382010-04-26 13:48:45 +02002236 !rt2x00_rt(rt2x00dev, RT3390) &&
Helmut Schaabaff8002010-04-28 09:58:59 +02002237 !rt2800_is_305x_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002238 return 0;
2239
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002240 /*
2241 * Init RF calibration.
2242 */
2243 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2244 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
2245 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2246 msleep(1);
2247 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
2248 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2249
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002250 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002251 rt2x00_rt(rt2x00dev, RT3071) ||
2252 rt2x00_rt(rt2x00dev, RT3090)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002253 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
2254 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
2255 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
2256 rt2800_rfcsr_write(rt2x00dev, 7, 0x70);
2257 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002258 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002259 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
2260 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
2261 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
2262 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
2263 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
2264 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
2265 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
2266 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
2267 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
2268 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
2269 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
2270 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002271 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002272 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
2273 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
2274 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
2275 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
2276 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002277 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002278 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
2279 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
2280 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
2281 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
2282 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
2283 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002284 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002285 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
2286 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002287 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002288 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
2289 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
2290 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
2291 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
2292 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
2293 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
2294 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002295 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002296 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002297 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002298 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
2299 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
2300 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
2301 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
2302 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
2303 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
2304 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Helmut Schaabaff8002010-04-28 09:58:59 +02002305 } else if (rt2800_is_305x_soc(rt2x00dev)) {
Helmut Schaa23812382010-04-26 13:48:45 +02002306 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
2307 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
2308 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
2309 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
2310 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
2311 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
2312 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
2313 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
2314 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
2315 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
2316 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
2317 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
2318 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
2319 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
2320 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
2321 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
2322 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
2323 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
2324 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
2325 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
2326 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
2327 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
2328 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
2329 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
2330 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
2331 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
2332 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
2333 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
2334 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
2335 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
Helmut Schaabaff8002010-04-28 09:58:59 +02002336 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
2337 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
2338 return 0;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002339 }
2340
2341 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
2342 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
2343 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
2344 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
2345 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002346 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
2347 rt2x00_rt(rt2x00dev, RT3090)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002348 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
2349 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
2350 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
2351
2352 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
2353
2354 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
2355 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002356 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
2357 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002358 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2359 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
2360 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
2361 else
2362 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
2363 }
2364 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002365 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
2366 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
2367 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
2368 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002369 }
2370
2371 /*
2372 * Set RX Filter calibration for 20MHz and 40MHz
2373 */
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002374 if (rt2x00_rt(rt2x00dev, RT3070)) {
2375 rt2x00dev->calibration[0] =
2376 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
2377 rt2x00dev->calibration[1] =
2378 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002379 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002380 rt2x00_rt(rt2x00dev, RT3090) ||
2381 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002382 rt2x00dev->calibration[0] =
2383 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
2384 rt2x00dev->calibration[1] =
2385 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002386 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002387
2388 /*
2389 * Set back to initial state
2390 */
2391 rt2800_bbp_write(rt2x00dev, 24, 0);
2392
2393 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
2394 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
2395 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
2396
2397 /*
2398 * set BBP back to BW20
2399 */
2400 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2401 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
2402 rt2800_bbp_write(rt2x00dev, 4, bbp);
2403
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002404 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002405 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002406 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2407 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002408 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
2409
2410 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
2411 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
2412 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
2413
2414 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
2415 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002416 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002417 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2418 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerde8440c292010-06-03 10:52:02 +02002419 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002420 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
2421 }
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002422 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &eeprom);
2423 if (rt2x00_get_field16(eeprom, EEPROM_TXMIXER_GAIN_BG_VAL) >= 1)
2424 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
2425 rt2x00_get_field16(eeprom,
2426 EEPROM_TXMIXER_GAIN_BG_VAL));
2427 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
2428
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002429 if (rt2x00_rt(rt2x00dev, RT3090)) {
2430 rt2800_bbp_read(rt2x00dev, 138, &bbp);
2431
2432 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2433 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
2434 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
2435 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
2436 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
2437
2438 rt2800_bbp_write(rt2x00dev, 138, bbp);
2439 }
2440
2441 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002442 rt2x00_rt(rt2x00dev, RT3090) ||
2443 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002444 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2445 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2446 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2447 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2448 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2449 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2450 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2451
2452 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
2453 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
2454 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
2455
2456 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
2457 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
2458 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
2459
2460 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
2461 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
2462 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
2463 }
2464
2465 if (rt2x00_rt(rt2x00dev, RT3070) || rt2x00_rt(rt2x00dev, RT3071)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002466 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002467 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
2468 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002469 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
2470 else
2471 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
2472 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
2473 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
2474 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
2475 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
2476 }
2477
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002478 return 0;
2479}
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002480
2481int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
2482{
2483 u32 reg;
2484 u16 word;
2485
2486 /*
2487 * Initialize all registers.
2488 */
2489 if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
2490 rt2800_init_registers(rt2x00dev) ||
2491 rt2800_init_bbp(rt2x00dev) ||
2492 rt2800_init_rfcsr(rt2x00dev)))
2493 return -EIO;
2494
2495 /*
2496 * Send signal to firmware during boot time.
2497 */
2498 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
2499
2500 if (rt2x00_is_usb(rt2x00dev) &&
2501 (rt2x00_rt(rt2x00dev, RT3070) ||
2502 rt2x00_rt(rt2x00dev, RT3071) ||
2503 rt2x00_rt(rt2x00dev, RT3572))) {
2504 udelay(200);
2505 rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
2506 udelay(10);
2507 }
2508
2509 /*
2510 * Enable RX.
2511 */
2512 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
2513 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
2514 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
2515 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
2516
2517 udelay(50);
2518
2519 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
2520 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
2521 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
2522 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
2523 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
2524 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
2525
2526 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
2527 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
2528 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
2529 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
2530
2531 /*
2532 * Initialize LED control
2533 */
2534 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED1, &word);
2535 rt2800_mcu_request(rt2x00dev, MCU_LED_1, 0xff,
2536 word & 0xff, (word >> 8) & 0xff);
2537
2538 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED2, &word);
2539 rt2800_mcu_request(rt2x00dev, MCU_LED_2, 0xff,
2540 word & 0xff, (word >> 8) & 0xff);
2541
2542 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED3, &word);
2543 rt2800_mcu_request(rt2x00dev, MCU_LED_3, 0xff,
2544 word & 0xff, (word >> 8) & 0xff);
2545
2546 return 0;
2547}
2548EXPORT_SYMBOL_GPL(rt2800_enable_radio);
2549
2550void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
2551{
2552 u32 reg;
2553
2554 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
2555 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
2556 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
2557 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
2558 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
2559 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
2560 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
2561
2562 /* Wait for DMA, ignore error */
2563 rt2800_wait_wpdma_ready(rt2x00dev);
2564
2565 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
2566 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
2567 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
2568 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
2569
2570 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0);
2571 rt2800_register_write(rt2x00dev, TX_PIN_CFG, 0);
2572}
2573EXPORT_SYMBOL_GPL(rt2800_disable_radio);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002574
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002575int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
2576{
2577 u32 reg;
2578
2579 rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
2580
2581 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
2582}
2583EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
2584
2585static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
2586{
2587 u32 reg;
2588
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002589 mutex_lock(&rt2x00dev->csr_mutex);
2590
2591 rt2800_register_read_lock(rt2x00dev, EFUSE_CTRL, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002592 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
2593 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
2594 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002595 rt2800_register_write_lock(rt2x00dev, EFUSE_CTRL, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002596
2597 /* Wait until the EEPROM has been loaded */
2598 rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
2599
2600 /* Apparently the data is read from end to start */
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002601 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA3,
2602 (u32 *)&rt2x00dev->eeprom[i]);
2603 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA2,
2604 (u32 *)&rt2x00dev->eeprom[i + 2]);
2605 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA1,
2606 (u32 *)&rt2x00dev->eeprom[i + 4]);
2607 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA0,
2608 (u32 *)&rt2x00dev->eeprom[i + 6]);
2609
2610 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002611}
2612
2613void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
2614{
2615 unsigned int i;
2616
2617 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
2618 rt2800_efuse_read(rt2x00dev, i);
2619}
2620EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
2621
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002622int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
2623{
2624 u16 word;
2625 u8 *mac;
2626 u8 default_lna_gain;
2627
2628 /*
2629 * Start validation of the data that has been read.
2630 */
2631 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
2632 if (!is_valid_ether_addr(mac)) {
2633 random_ether_addr(mac);
2634 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
2635 }
2636
2637 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
2638 if (word == 0xffff) {
2639 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2640 rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
2641 rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
2642 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2643 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002644 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02002645 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002646 /*
2647 * There is a max of 2 RX streams for RT28x0 series
2648 */
2649 if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
2650 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2651 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2652 }
2653
2654 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
2655 if (word == 0xffff) {
2656 rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
2657 rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
2658 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
2659 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
2660 rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
2661 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
2662 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
2663 rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
2664 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
2665 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002666 rt2x00_set_field16(&word, EEPROM_NIC_ANT_DIVERSITY, 0);
2667 rt2x00_set_field16(&word, EEPROM_NIC_DAC_TEST, 0);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002668 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
2669 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
2670 }
2671
2672 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
2673 if ((word & 0x00ff) == 0x00ff) {
2674 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002675 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2676 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
2677 }
2678 if ((word & 0xff00) == 0xff00) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002679 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
2680 LED_MODE_TXRX_ACTIVITY);
2681 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
2682 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2683 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
2684 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
2685 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002686 EEPROM(rt2x00dev, "Led Mode: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002687 }
2688
2689 /*
2690 * During the LNA validation we are going to use
2691 * lna0 as correct value. Note that EEPROM_LNA
2692 * is never validated.
2693 */
2694 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
2695 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
2696
2697 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
2698 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
2699 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
2700 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
2701 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
2702 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
2703
2704 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
2705 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
2706 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
2707 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
2708 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
2709 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
2710 default_lna_gain);
2711 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
2712
2713 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
2714 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
2715 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
2716 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
2717 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
2718 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
2719
2720 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
2721 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
2722 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
2723 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
2724 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
2725 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
2726 default_lna_gain);
2727 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
2728
2729 return 0;
2730}
2731EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
2732
2733int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
2734{
2735 u32 reg;
2736 u16 value;
2737 u16 eeprom;
2738
2739 /*
2740 * Read EEPROM word for configuration.
2741 */
2742 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2743
2744 /*
2745 * Identify RF chipset.
2746 */
2747 value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
2748 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
2749
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002750 rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
2751 value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
Gertjan van Wingerde714fa662010-02-13 20:55:48 +01002752
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002753 if (!rt2x00_rt(rt2x00dev, RT2860) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002754 !rt2x00_rt(rt2x00dev, RT2872) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002755 !rt2x00_rt(rt2x00dev, RT2883) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002756 !rt2x00_rt(rt2x00dev, RT3070) &&
2757 !rt2x00_rt(rt2x00dev, RT3071) &&
2758 !rt2x00_rt(rt2x00dev, RT3090) &&
2759 !rt2x00_rt(rt2x00dev, RT3390) &&
2760 !rt2x00_rt(rt2x00dev, RT3572)) {
2761 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
2762 return -ENODEV;
2763 }
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002764
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002765 if (!rt2x00_rf(rt2x00dev, RF2820) &&
2766 !rt2x00_rf(rt2x00dev, RF2850) &&
2767 !rt2x00_rf(rt2x00dev, RF2720) &&
2768 !rt2x00_rf(rt2x00dev, RF2750) &&
2769 !rt2x00_rf(rt2x00dev, RF3020) &&
2770 !rt2x00_rf(rt2x00dev, RF2020) &&
2771 !rt2x00_rf(rt2x00dev, RF3021) &&
Gertjan van Wingerde6c0fe262009-12-30 11:36:31 +01002772 !rt2x00_rf(rt2x00dev, RF3022) &&
2773 !rt2x00_rf(rt2x00dev, RF3052)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002774 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
2775 return -ENODEV;
2776 }
2777
2778 /*
2779 * Identify default antenna configuration.
2780 */
2781 rt2x00dev->default_ant.tx =
2782 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH);
2783 rt2x00dev->default_ant.rx =
2784 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH);
2785
2786 /*
2787 * Read frequency offset and RF programming sequence.
2788 */
2789 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
2790 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
2791
2792 /*
2793 * Read external LNA informations.
2794 */
2795 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2796
2797 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
2798 __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
2799 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
2800 __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
2801
2802 /*
2803 * Detect if this device has an hardware controlled radio.
2804 */
2805 if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
2806 __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
2807
2808 /*
2809 * Store led settings, for correct led behaviour.
2810 */
2811#ifdef CONFIG_RT2X00_LIB_LEDS
2812 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
2813 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
2814 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
2815
2816 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &rt2x00dev->led_mcu_reg);
2817#endif /* CONFIG_RT2X00_LIB_LEDS */
2818
2819 return 0;
2820}
2821EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
2822
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002823/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002824 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002825 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
2826 */
2827static const struct rf_channel rf_vals[] = {
2828 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
2829 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
2830 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
2831 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
2832 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
2833 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
2834 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
2835 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
2836 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
2837 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
2838 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
2839 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
2840 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
2841 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
2842
2843 /* 802.11 UNI / HyperLan 2 */
2844 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
2845 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
2846 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
2847 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
2848 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
2849 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
2850 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
2851 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
2852 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
2853 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
2854 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
2855 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
2856
2857 /* 802.11 HyperLan 2 */
2858 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
2859 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
2860 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
2861 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
2862 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
2863 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
2864 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
2865 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
2866 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
2867 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
2868 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
2869 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
2870 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
2871 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
2872 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
2873 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
2874
2875 /* 802.11 UNII */
2876 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
2877 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
2878 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
2879 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
2880 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
2881 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
2882 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
2883 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
2884 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
2885 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
2886 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
2887
2888 /* 802.11 Japan */
2889 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
2890 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
2891 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
2892 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
2893 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
2894 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
2895 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
2896};
2897
2898/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002899 * RF value list for rt3xxx
2900 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002901 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02002902static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002903 {1, 241, 2, 2 },
2904 {2, 241, 2, 7 },
2905 {3, 242, 2, 2 },
2906 {4, 242, 2, 7 },
2907 {5, 243, 2, 2 },
2908 {6, 243, 2, 7 },
2909 {7, 244, 2, 2 },
2910 {8, 244, 2, 7 },
2911 {9, 245, 2, 2 },
2912 {10, 245, 2, 7 },
2913 {11, 246, 2, 2 },
2914 {12, 246, 2, 7 },
2915 {13, 247, 2, 2 },
2916 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02002917
2918 /* 802.11 UNI / HyperLan 2 */
2919 {36, 0x56, 0, 4},
2920 {38, 0x56, 0, 6},
2921 {40, 0x56, 0, 8},
2922 {44, 0x57, 0, 0},
2923 {46, 0x57, 0, 2},
2924 {48, 0x57, 0, 4},
2925 {52, 0x57, 0, 8},
2926 {54, 0x57, 0, 10},
2927 {56, 0x58, 0, 0},
2928 {60, 0x58, 0, 4},
2929 {62, 0x58, 0, 6},
2930 {64, 0x58, 0, 8},
2931
2932 /* 802.11 HyperLan 2 */
2933 {100, 0x5b, 0, 8},
2934 {102, 0x5b, 0, 10},
2935 {104, 0x5c, 0, 0},
2936 {108, 0x5c, 0, 4},
2937 {110, 0x5c, 0, 6},
2938 {112, 0x5c, 0, 8},
2939 {116, 0x5d, 0, 0},
2940 {118, 0x5d, 0, 2},
2941 {120, 0x5d, 0, 4},
2942 {124, 0x5d, 0, 8},
2943 {126, 0x5d, 0, 10},
2944 {128, 0x5e, 0, 0},
2945 {132, 0x5e, 0, 4},
2946 {134, 0x5e, 0, 6},
2947 {136, 0x5e, 0, 8},
2948 {140, 0x5f, 0, 0},
2949
2950 /* 802.11 UNII */
2951 {149, 0x5f, 0, 9},
2952 {151, 0x5f, 0, 11},
2953 {153, 0x60, 0, 1},
2954 {157, 0x60, 0, 5},
2955 {159, 0x60, 0, 7},
2956 {161, 0x60, 0, 9},
2957 {165, 0x61, 0, 1},
2958 {167, 0x61, 0, 3},
2959 {169, 0x61, 0, 5},
2960 {171, 0x61, 0, 7},
2961 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002962};
2963
2964int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
2965{
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002966 struct hw_mode_spec *spec = &rt2x00dev->spec;
2967 struct channel_info *info;
2968 char *tx_power1;
2969 char *tx_power2;
2970 unsigned int i;
2971 u16 eeprom;
2972
2973 /*
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002974 * Disable powersaving as default on PCI devices.
2975 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01002976 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002977 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
2978
2979 /*
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002980 * Initialize all hw fields.
2981 */
2982 rt2x00dev->hw->flags =
2983 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2984 IEEE80211_HW_SIGNAL_DBM |
2985 IEEE80211_HW_SUPPORTS_PS |
Helmut Schaa1df90802010-06-29 21:38:12 +02002986 IEEE80211_HW_PS_NULLFUNC_STACK |
2987 IEEE80211_HW_AMPDU_AGGREGATION;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002988
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01002989 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
2990 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
2991 rt2x00_eeprom_addr(rt2x00dev,
2992 EEPROM_MAC_ADDR_0));
2993
Helmut Schaa3f2bee22010-06-14 22:12:01 +02002994 /*
2995 * As rt2800 has a global fallback table we cannot specify
2996 * more then one tx rate per frame but since the hw will
2997 * try several rates (based on the fallback table) we should
2998 * still initialize max_rates to the maximum number of rates
2999 * we are going to try. Otherwise mac80211 will truncate our
3000 * reported tx rates and the rc algortihm will end up with
3001 * incorrect data.
3002 */
3003 rt2x00dev->hw->max_rates = 7;
3004 rt2x00dev->hw->max_rate_tries = 1;
3005
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003006 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
3007
3008 /*
3009 * Initialize hw_mode information.
3010 */
3011 spec->supported_bands = SUPPORT_BAND_2GHZ;
3012 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
3013
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003014 if (rt2x00_rf(rt2x00dev, RF2820) ||
Ivo van Doorn55f93212010-05-06 14:45:46 +02003015 rt2x00_rf(rt2x00dev, RF2720)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003016 spec->num_channels = 14;
3017 spec->channels = rf_vals;
Ivo van Doorn55f93212010-05-06 14:45:46 +02003018 } else if (rt2x00_rf(rt2x00dev, RF2850) ||
3019 rt2x00_rf(rt2x00dev, RF2750)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003020 spec->supported_bands |= SUPPORT_BAND_5GHZ;
3021 spec->num_channels = ARRAY_SIZE(rf_vals);
3022 spec->channels = rf_vals;
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003023 } else if (rt2x00_rf(rt2x00dev, RF3020) ||
3024 rt2x00_rf(rt2x00dev, RF2020) ||
3025 rt2x00_rf(rt2x00dev, RF3021) ||
3026 rt2x00_rf(rt2x00dev, RF3022)) {
Ivo van Doorn55f93212010-05-06 14:45:46 +02003027 spec->num_channels = 14;
3028 spec->channels = rf_vals_3x;
3029 } else if (rt2x00_rf(rt2x00dev, RF3052)) {
3030 spec->supported_bands |= SUPPORT_BAND_5GHZ;
3031 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
3032 spec->channels = rf_vals_3x;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003033 }
3034
3035 /*
3036 * Initialize HT information.
3037 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003038 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01003039 spec->ht.ht_supported = true;
3040 else
3041 spec->ht.ht_supported = false;
3042
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003043 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02003044 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003045 IEEE80211_HT_CAP_GRN_FLD |
3046 IEEE80211_HT_CAP_SGI_20 |
Ivo van Doornaa674632010-06-29 21:48:37 +02003047 IEEE80211_HT_CAP_SGI_40;
Helmut Schaa22cabaa2010-06-03 10:52:10 +02003048
3049 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) >= 2)
3050 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
3051
Ivo van Doornaa674632010-06-29 21:48:37 +02003052 spec->ht.cap |=
3053 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) <<
3054 IEEE80211_HT_CAP_RX_STBC_SHIFT;
3055
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003056 spec->ht.ampdu_factor = 3;
3057 spec->ht.ampdu_density = 4;
3058 spec->ht.mcs.tx_params =
3059 IEEE80211_HT_MCS_TX_DEFINED |
3060 IEEE80211_HT_MCS_TX_RX_DIFF |
3061 ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
3062 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
3063
3064 switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
3065 case 3:
3066 spec->ht.mcs.rx_mask[2] = 0xff;
3067 case 2:
3068 spec->ht.mcs.rx_mask[1] = 0xff;
3069 case 1:
3070 spec->ht.mcs.rx_mask[0] = 0xff;
3071 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
3072 break;
3073 }
3074
3075 /*
3076 * Create channel information array
3077 */
3078 info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
3079 if (!info)
3080 return -ENOMEM;
3081
3082 spec->channels_info = info;
3083
3084 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
3085 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
3086
3087 for (i = 0; i < 14; i++) {
3088 info[i].tx_power1 = TXPOWER_G_FROM_DEV(tx_power1[i]);
3089 info[i].tx_power2 = TXPOWER_G_FROM_DEV(tx_power2[i]);
3090 }
3091
3092 if (spec->num_channels > 14) {
3093 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
3094 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
3095
3096 for (i = 14; i < spec->num_channels; i++) {
3097 info[i].tx_power1 = TXPOWER_A_FROM_DEV(tx_power1[i]);
3098 info[i].tx_power2 = TXPOWER_A_FROM_DEV(tx_power2[i]);
3099 }
3100 }
3101
3102 return 0;
3103}
3104EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
3105
3106/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003107 * IEEE80211 stack callback functions.
3108 */
Helmut Schaae7836192010-07-11 12:28:54 +02003109void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx, u32 *iv32,
3110 u16 *iv16)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003111{
3112 struct rt2x00_dev *rt2x00dev = hw->priv;
3113 struct mac_iveiv_entry iveiv_entry;
3114 u32 offset;
3115
3116 offset = MAC_IVEIV_ENTRY(hw_key_idx);
3117 rt2800_register_multiread(rt2x00dev, offset,
3118 &iveiv_entry, sizeof(iveiv_entry));
3119
Julia Lawall855da5e2009-12-13 17:07:45 +01003120 memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
3121 memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003122}
Helmut Schaae7836192010-07-11 12:28:54 +02003123EXPORT_SYMBOL_GPL(rt2800_get_tkip_seq);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003124
Helmut Schaae7836192010-07-11 12:28:54 +02003125int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003126{
3127 struct rt2x00_dev *rt2x00dev = hw->priv;
3128 u32 reg;
3129 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
3130
3131 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
3132 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
3133 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
3134
3135 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
3136 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
3137 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
3138
3139 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
3140 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
3141 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
3142
3143 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
3144 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
3145 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
3146
3147 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
3148 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
3149 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
3150
3151 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
3152 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
3153 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
3154
3155 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
3156 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
3157 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
3158
3159 return 0;
3160}
Helmut Schaae7836192010-07-11 12:28:54 +02003161EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003162
Helmut Schaae7836192010-07-11 12:28:54 +02003163int rt2800_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
3164 const struct ieee80211_tx_queue_params *params)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003165{
3166 struct rt2x00_dev *rt2x00dev = hw->priv;
3167 struct data_queue *queue;
3168 struct rt2x00_field32 field;
3169 int retval;
3170 u32 reg;
3171 u32 offset;
3172
3173 /*
3174 * First pass the configuration through rt2x00lib, that will
3175 * update the queue settings and validate the input. After that
3176 * we are free to update the registers based on the value
3177 * in the queue parameter.
3178 */
3179 retval = rt2x00mac_conf_tx(hw, queue_idx, params);
3180 if (retval)
3181 return retval;
3182
3183 /*
3184 * We only need to perform additional register initialization
3185 * for WMM queues/
3186 */
3187 if (queue_idx >= 4)
3188 return 0;
3189
3190 queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
3191
3192 /* Update WMM TXOP register */
3193 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
3194 field.bit_offset = (queue_idx & 1) * 16;
3195 field.bit_mask = 0xffff << field.bit_offset;
3196
3197 rt2800_register_read(rt2x00dev, offset, &reg);
3198 rt2x00_set_field32(&reg, field, queue->txop);
3199 rt2800_register_write(rt2x00dev, offset, reg);
3200
3201 /* Update WMM registers */
3202 field.bit_offset = queue_idx * 4;
3203 field.bit_mask = 0xf << field.bit_offset;
3204
3205 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
3206 rt2x00_set_field32(&reg, field, queue->aifs);
3207 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
3208
3209 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
3210 rt2x00_set_field32(&reg, field, queue->cw_min);
3211 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
3212
3213 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
3214 rt2x00_set_field32(&reg, field, queue->cw_max);
3215 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
3216
3217 /* Update EDCA registers */
3218 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
3219
3220 rt2800_register_read(rt2x00dev, offset, &reg);
3221 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
3222 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
3223 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
3224 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
3225 rt2800_register_write(rt2x00dev, offset, reg);
3226
3227 return 0;
3228}
Helmut Schaae7836192010-07-11 12:28:54 +02003229EXPORT_SYMBOL_GPL(rt2800_conf_tx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003230
Helmut Schaae7836192010-07-11 12:28:54 +02003231u64 rt2800_get_tsf(struct ieee80211_hw *hw)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003232{
3233 struct rt2x00_dev *rt2x00dev = hw->priv;
3234 u64 tsf;
3235 u32 reg;
3236
3237 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
3238 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
3239 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
3240 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
3241
3242 return tsf;
3243}
Helmut Schaae7836192010-07-11 12:28:54 +02003244EXPORT_SYMBOL_GPL(rt2800_get_tsf);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003245
Helmut Schaae7836192010-07-11 12:28:54 +02003246int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3247 enum ieee80211_ampdu_mlme_action action,
3248 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
Helmut Schaa1df90802010-06-29 21:38:12 +02003249{
Helmut Schaa1df90802010-06-29 21:38:12 +02003250 int ret = 0;
3251
3252 switch (action) {
3253 case IEEE80211_AMPDU_RX_START:
3254 case IEEE80211_AMPDU_RX_STOP:
3255 /* we don't support RX aggregation yet */
3256 ret = -ENOTSUPP;
3257 break;
3258 case IEEE80211_AMPDU_TX_START:
3259 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3260 break;
3261 case IEEE80211_AMPDU_TX_STOP:
3262 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3263 break;
3264 case IEEE80211_AMPDU_TX_OPERATIONAL:
3265 break;
3266 default:
Ivo van Doorn4e9e58c2010-06-29 21:49:50 +02003267 WARNING((struct rt2x00_dev *)hw->priv, "Unknown AMPDU action\n");
Helmut Schaa1df90802010-06-29 21:38:12 +02003268 }
3269
3270 return ret;
3271}
Helmut Schaae7836192010-07-11 12:28:54 +02003272EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02003273
3274MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
3275MODULE_VERSION(DRV_VERSION);
3276MODULE_DESCRIPTION("Ralink RT2800 library");
3277MODULE_LICENSE("GPL");