blob: cbab2e126167ff475884e6a46391a9ce82a6850c [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 Bacik6a62e5e2009-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
Ivo van Doorn46323e12010-08-23 19:55:43 +02001247 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A, info->tx_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001248
1249 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
1250 (info->tx_power2 >= 0));
1251
1252 if (info->tx_power2 < 0)
1253 info->tx_power2 += 7;
1254
Ivo van Doorn46323e12010-08-23 19:55:43 +02001255 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A, info->tx_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001256 } else {
Ivo van Doorn46323e12010-08-23 19:55:43 +02001257 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G, info->tx_power1);
1258 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G, info->tx_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001259 }
1260
1261 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
1262
1263 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1264 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1265 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1266 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1267
1268 udelay(200);
1269
1270 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1271 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1272 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
1273 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1274
1275 udelay(200);
1276
1277 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1278 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1279 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1280 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1281}
1282
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001283static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
1284 struct ieee80211_conf *conf,
1285 struct rf_channel *rf,
1286 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001287{
1288 u8 rfcsr;
1289
1290 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Gertjan van Wingerde41a26172009-11-09 22:59:04 +01001291 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001292
1293 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +02001294 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001295 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1296
1297 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
Ivo van Doorn46323e12010-08-23 19:55:43 +02001298 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER, info->tx_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001299 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
1300
Helmut Schaa5a673962010-04-23 15:54:43 +02001301 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
Ivo van Doorn46323e12010-08-23 19:55:43 +02001302 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER, info->tx_power2);
Helmut Schaa5a673962010-04-23 15:54:43 +02001303 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
1304
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001305 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
1306 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
1307 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
1308
1309 rt2800_rfcsr_write(rt2x00dev, 24,
1310 rt2x00dev->calibration[conf_is_ht40(conf)]);
1311
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001312 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001313 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001314 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001315}
1316
1317static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
1318 struct ieee80211_conf *conf,
1319 struct rf_channel *rf,
1320 struct channel_info *info)
1321{
1322 u32 reg;
1323 unsigned int tx_pin;
1324 u8 bbp;
1325
Ivo van Doorn46323e12010-08-23 19:55:43 +02001326 if (rf->channel <= 14) {
1327 info->tx_power1 = TXPOWER_G_TO_DEV(info->tx_power1);
1328 info->tx_power2 = TXPOWER_G_TO_DEV(info->tx_power2);
1329 } else {
1330 info->tx_power1 = TXPOWER_A_TO_DEV(info->tx_power1);
1331 info->tx_power2 = TXPOWER_A_TO_DEV(info->tx_power2);
1332 }
1333
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001334 if (rt2x00_rf(rt2x00dev, RF2020) ||
1335 rt2x00_rf(rt2x00dev, RF3020) ||
1336 rt2x00_rf(rt2x00dev, RF3021) ||
Ivo van Doorn46323e12010-08-23 19:55:43 +02001337 rt2x00_rf(rt2x00dev, RF3022) ||
1338 rt2x00_rf(rt2x00dev, RF3052))
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001339 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerdefa6f6322009-11-09 22:59:58 +01001340 else
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001341 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001342
1343 /*
1344 * Change BBP settings
1345 */
1346 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
1347 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
1348 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
1349 rt2800_bbp_write(rt2x00dev, 86, 0);
1350
1351 if (rf->channel <= 14) {
1352 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
1353 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1354 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1355 } else {
1356 rt2800_bbp_write(rt2x00dev, 82, 0x84);
1357 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1358 }
1359 } else {
1360 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
1361
1362 if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
1363 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1364 else
1365 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1366 }
1367
1368 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001369 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001370 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
1371 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
1372 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
1373
1374 tx_pin = 0;
1375
1376 /* Turn on unused PA or LNA when not using 1T or 1R */
1377 if (rt2x00dev->default_ant.tx != 1) {
1378 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
1379 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
1380 }
1381
1382 /* Turn on unused PA or LNA when not using 1T or 1R */
1383 if (rt2x00dev->default_ant.rx != 1) {
1384 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
1385 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
1386 }
1387
1388 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
1389 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
1390 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
1391 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
1392 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
1393 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
1394
1395 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
1396
1397 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1398 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
1399 rt2800_bbp_write(rt2x00dev, 4, bbp);
1400
1401 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001402 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001403 rt2800_bbp_write(rt2x00dev, 3, bbp);
1404
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001405 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001406 if (conf_is_ht40(conf)) {
1407 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
1408 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1409 rt2800_bbp_write(rt2x00dev, 73, 0x16);
1410 } else {
1411 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1412 rt2800_bbp_write(rt2x00dev, 70, 0x08);
1413 rt2800_bbp_write(rt2x00dev, 73, 0x11);
1414 }
1415 }
1416
1417 msleep(1);
1418}
1419
1420static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
Helmut Schaa5e846002010-07-11 12:23:09 +02001421 const int max_txpower)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001422{
Helmut Schaa5e846002010-07-11 12:23:09 +02001423 u8 txpower;
1424 u8 max_value = (u8)max_txpower;
1425 u16 eeprom;
1426 int i;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001427 u32 reg;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001428 u8 r1;
Helmut Schaa5e846002010-07-11 12:23:09 +02001429 u32 offset;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001430
Helmut Schaa5e846002010-07-11 12:23:09 +02001431 /*
1432 * set to normal tx power mode: +/- 0dBm
1433 */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001434 rt2800_bbp_read(rt2x00dev, 1, &r1);
Helmut Schaaa3f84ca2010-06-14 22:11:32 +02001435 rt2x00_set_field8(&r1, BBP1_TX_POWER, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001436 rt2800_bbp_write(rt2x00dev, 1, r1);
1437
Helmut Schaa5e846002010-07-11 12:23:09 +02001438 /*
1439 * The eeprom contains the tx power values for each rate. These
1440 * values map to 100% tx power. Each 16bit word contains four tx
1441 * power values and the order is the same as used in the TX_PWR_CFG
1442 * registers.
1443 */
1444 offset = TX_PWR_CFG_0;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001445
Helmut Schaa5e846002010-07-11 12:23:09 +02001446 for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
1447 /* just to be safe */
1448 if (offset > TX_PWR_CFG_4)
1449 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001450
Helmut Schaa5e846002010-07-11 12:23:09 +02001451 rt2800_register_read(rt2x00dev, offset, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001452
Helmut Schaa5e846002010-07-11 12:23:09 +02001453 /* read the next four txpower values */
1454 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i,
1455 &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001456
Helmut Schaa5e846002010-07-11 12:23:09 +02001457 /* TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
1458 * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
1459 * TX_PWR_CFG_4: unknown */
1460 txpower = rt2x00_get_field16(eeprom,
1461 EEPROM_TXPOWER_BYRATE_RATE0);
1462 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0,
1463 min(txpower, max_value));
1464
1465 /* TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
1466 * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
1467 * TX_PWR_CFG_4: unknown */
1468 txpower = rt2x00_get_field16(eeprom,
1469 EEPROM_TXPOWER_BYRATE_RATE1);
1470 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1,
1471 min(txpower, max_value));
1472
1473 /* TX_PWR_CFG_0: 55MBS, TX_PWR_CFG_1: 48MBS,
1474 * TX_PWR_CFG_2: MCS6, TX_PWR_CFG_3: MCS14,
1475 * TX_PWR_CFG_4: unknown */
1476 txpower = rt2x00_get_field16(eeprom,
1477 EEPROM_TXPOWER_BYRATE_RATE2);
1478 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2,
1479 min(txpower, max_value));
1480
1481 /* TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
1482 * TX_PWR_CFG_2: MCS7, TX_PWR_CFG_3: MCS15,
1483 * TX_PWR_CFG_4: unknown */
1484 txpower = rt2x00_get_field16(eeprom,
1485 EEPROM_TXPOWER_BYRATE_RATE3);
1486 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3,
1487 min(txpower, max_value));
1488
1489 /* read the next four txpower values */
1490 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i + 1,
1491 &eeprom);
1492
1493 /* TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
1494 * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
1495 * TX_PWR_CFG_4: unknown */
1496 txpower = rt2x00_get_field16(eeprom,
1497 EEPROM_TXPOWER_BYRATE_RATE0);
1498 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4,
1499 min(txpower, max_value));
1500
1501 /* TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
1502 * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
1503 * TX_PWR_CFG_4: unknown */
1504 txpower = rt2x00_get_field16(eeprom,
1505 EEPROM_TXPOWER_BYRATE_RATE1);
1506 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5,
1507 min(txpower, max_value));
1508
1509 /* TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
1510 * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
1511 * TX_PWR_CFG_4: unknown */
1512 txpower = rt2x00_get_field16(eeprom,
1513 EEPROM_TXPOWER_BYRATE_RATE2);
1514 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6,
1515 min(txpower, max_value));
1516
1517 /* TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
1518 * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
1519 * TX_PWR_CFG_4: unknown */
1520 txpower = rt2x00_get_field16(eeprom,
1521 EEPROM_TXPOWER_BYRATE_RATE3);
1522 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7,
1523 min(txpower, max_value));
1524
1525 rt2800_register_write(rt2x00dev, offset, reg);
1526
1527 /* next TX_PWR_CFG register */
1528 offset += 4;
1529 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001530}
1531
1532static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
1533 struct rt2x00lib_conf *libconf)
1534{
1535 u32 reg;
1536
1537 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1538 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
1539 libconf->conf->short_frame_max_tx_count);
1540 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
1541 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001542 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1543}
1544
1545static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
1546 struct rt2x00lib_conf *libconf)
1547{
1548 enum dev_state state =
1549 (libconf->conf->flags & IEEE80211_CONF_PS) ?
1550 STATE_SLEEP : STATE_AWAKE;
1551 u32 reg;
1552
1553 if (state == STATE_SLEEP) {
1554 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
1555
1556 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1557 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
1558 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
1559 libconf->conf->listen_interval - 1);
1560 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
1561 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
1562
1563 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
1564 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001565 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1566 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
1567 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
1568 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
1569 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02001570
1571 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001572 }
1573}
1574
1575void rt2800_config(struct rt2x00_dev *rt2x00dev,
1576 struct rt2x00lib_conf *libconf,
1577 const unsigned int flags)
1578{
1579 /* Always recalculate LNA gain before changing configuration */
1580 rt2800_config_lna_gain(rt2x00dev, libconf);
1581
1582 if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
1583 rt2800_config_channel(rt2x00dev, libconf->conf,
1584 &libconf->rf, &libconf->channel);
1585 if (flags & IEEE80211_CONF_CHANGE_POWER)
1586 rt2800_config_txpower(rt2x00dev, libconf->conf->power_level);
1587 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
1588 rt2800_config_retry_limit(rt2x00dev, libconf);
1589 if (flags & IEEE80211_CONF_CHANGE_PS)
1590 rt2800_config_ps(rt2x00dev, libconf);
1591}
1592EXPORT_SYMBOL_GPL(rt2800_config);
1593
1594/*
1595 * Link tuning
1596 */
1597void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1598{
1599 u32 reg;
1600
1601 /*
1602 * Update FCS error count from register.
1603 */
1604 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1605 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
1606}
1607EXPORT_SYMBOL_GPL(rt2800_link_stats);
1608
1609static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
1610{
1611 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001612 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001613 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001614 rt2x00_rt(rt2x00dev, RT3090) ||
1615 rt2x00_rt(rt2x00dev, RT3390))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001616 return 0x1c + (2 * rt2x00dev->lna_gain);
1617 else
1618 return 0x2e + rt2x00dev->lna_gain;
1619 }
1620
1621 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
1622 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
1623 else
1624 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
1625}
1626
1627static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
1628 struct link_qual *qual, u8 vgc_level)
1629{
1630 if (qual->vgc_level != vgc_level) {
1631 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
1632 qual->vgc_level = vgc_level;
1633 qual->vgc_level_reg = vgc_level;
1634 }
1635}
1636
1637void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1638{
1639 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
1640}
1641EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
1642
1643void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
1644 const u32 count)
1645{
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001646 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001647 return;
1648
1649 /*
1650 * When RSSI is better then -80 increase VGC level with 0x10
1651 */
1652 rt2800_set_vgc(rt2x00dev, qual,
1653 rt2800_get_default_vgc(rt2x00dev) +
1654 ((qual->rssi > -80) * 0x10));
1655}
1656EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001657
1658/*
1659 * Initialization functions.
1660 */
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02001661static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001662{
1663 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001664 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001665 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001666 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001667
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001668 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1669 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1670 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1671 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1672 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1673 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
1674 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1675
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001676 ret = rt2800_drv_init_registers(rt2x00dev);
1677 if (ret)
1678 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001679
1680 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
1681 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
1682 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
1683 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
1684 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
1685 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
1686
1687 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
1688 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
1689 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
1690 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
1691 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
1692 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
1693
1694 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
1695 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1696
1697 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1698
1699 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Helmut Schaa8544df32010-07-11 12:29:49 +02001700 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001701 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
1702 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
1703 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
1704 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1705 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
1706 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1707
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001708 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
1709
1710 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1711 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
1712 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
1713 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
1714
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001715 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001716 rt2x00_rt(rt2x00dev, RT3090) ||
1717 rt2x00_rt(rt2x00dev, RT3390)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001718 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1719 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001720 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001721 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
1722 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001723 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1724 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
1725 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1726 0x0000002c);
1727 else
1728 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1729 0x0000000f);
1730 } else {
1731 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1732 }
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001733 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001734 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001735
1736 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
1737 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1738 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
1739 } else {
1740 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1741 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1742 }
Helmut Schaac295a812010-06-03 10:52:13 +02001743 } else if (rt2800_is_305x_soc(rt2x00dev)) {
1744 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1745 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1746 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000001f);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001747 } else {
1748 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
1749 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1750 }
1751
1752 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
1753 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
1754 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
1755 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
1756 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
1757 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
1758 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
1759 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
1760 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
1761 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
1762
1763 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
1764 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001765 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001766 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
1767 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
1768
1769 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
1770 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001771 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01001772 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001773 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001774 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
1775 else
1776 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
1777 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
1778 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
1779 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
1780
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001781 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1782 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
1783 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
1784 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
1785 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
1786 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
1787 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
1788 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
1789 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1790
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001791 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
1792
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001793 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1794 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
1795 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
1796 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
1797 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
1798 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
1799 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
1800 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1801
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001802 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1803 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001804 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001805 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
1806 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001807 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001808 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
1809 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
1810 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1811
1812 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001813 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001814 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
1815 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
1816 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1817 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1818 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001819 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001820 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001821 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1822 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001823 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
1824
1825 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001826 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001827 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
1828 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
1829 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1830 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1831 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001832 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001833 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001834 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1835 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001836 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1837
1838 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1839 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
1840 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
1841 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
1842 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1843 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1844 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1845 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1846 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1847 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001848 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001849 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1850
1851 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1852 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001853 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL,
1854 !rt2x00_is_usb(rt2x00dev));
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001855 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
1856 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1857 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1858 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1859 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1860 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1861 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001862 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001863 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1864
1865 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1866 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
1867 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
1868 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
1869 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1870 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1871 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1872 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1873 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1874 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001875 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001876 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1877
1878 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1879 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
1880 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
1881 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
1882 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1883 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1884 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1885 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1886 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1887 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001888 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001889 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1890
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001891 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001892 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
1893
1894 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1895 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1896 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1897 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1898 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1899 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
1900 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
1901 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
1902 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
1903 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
1904 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1905 }
1906
1907 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, 0x0000583f);
1908 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
1909
1910 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
1911 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
1912 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
1913 IEEE80211_MAX_RTS_THRESHOLD);
1914 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
1915 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
1916
1917 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001918
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001919 /*
1920 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
1921 * time should be set to 16. However, the original Ralink driver uses
1922 * 16 for both and indeed using a value of 10 for CCK SIFS results in
1923 * connection problems with 11g + CTS protection. Hence, use the same
1924 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
1925 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001926 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02001927 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
1928 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001929 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
1930 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
1931 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
1932 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1933
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001934 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
1935
1936 /*
1937 * ASIC will keep garbage value after boot, clear encryption keys.
1938 */
1939 for (i = 0; i < 4; i++)
1940 rt2800_register_write(rt2x00dev,
1941 SHARED_KEY_MODE_ENTRY(i), 0);
1942
1943 for (i = 0; i < 256; i++) {
1944 u32 wcid[2] = { 0xffffffff, 0x00ffffff };
1945 rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
1946 wcid, sizeof(wcid));
1947
1948 rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
1949 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
1950 }
1951
1952 /*
1953 * Clear all beacons
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001954 */
Helmut Schaafdb87252010-06-29 21:48:06 +02001955 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE0);
1956 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE1);
1957 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE2);
1958 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE3);
1959 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE4);
1960 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE5);
1961 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE6);
1962 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE7);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001963
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01001964 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02001965 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
1966 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
1967 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001968 }
1969
1970 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
1971 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
1972 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
1973 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
1974 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
1975 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
1976 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
1977 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
1978 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
1979 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
1980
1981 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
1982 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
1983 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
1984 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
1985 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
1986 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
1987 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
1988 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
1989 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
1990 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
1991
1992 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
1993 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
1994 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
1995 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
1996 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
1997 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
1998 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
1999 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
2000 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
2001 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
2002
2003 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
2004 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
2005 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
2006 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
2007 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
2008 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
2009
2010 /*
2011 * We must clear the error counters.
2012 * These registers are cleared on read,
2013 * so we may pass a useless variable to store the value.
2014 */
2015 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
2016 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
2017 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
2018 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
2019 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
2020 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
2021
Helmut Schaa9f926fb2010-07-11 12:28:23 +02002022 /*
2023 * Setup leadtime for pre tbtt interrupt to 6ms
2024 */
2025 rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
2026 rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
2027 rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
2028
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002029 return 0;
2030}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002031
2032static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
2033{
2034 unsigned int i;
2035 u32 reg;
2036
2037 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
2038 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
2039 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
2040 return 0;
2041
2042 udelay(REGISTER_BUSY_DELAY);
2043 }
2044
2045 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
2046 return -EACCES;
2047}
2048
2049static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
2050{
2051 unsigned int i;
2052 u8 value;
2053
2054 /*
2055 * BBP was enabled after firmware was loaded,
2056 * but we need to reactivate it now.
2057 */
2058 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
2059 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
2060 msleep(1);
2061
2062 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
2063 rt2800_bbp_read(rt2x00dev, 0, &value);
2064 if ((value != 0xff) && (value != 0x00))
2065 return 0;
2066 udelay(REGISTER_BUSY_DELAY);
2067 }
2068
2069 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
2070 return -EACCES;
2071}
2072
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002073static int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002074{
2075 unsigned int i;
2076 u16 eeprom;
2077 u8 reg_id;
2078 u8 value;
2079
2080 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
2081 rt2800_wait_bbp_ready(rt2x00dev)))
2082 return -EACCES;
2083
Helmut Schaabaff8002010-04-28 09:58:59 +02002084 if (rt2800_is_305x_soc(rt2x00dev))
2085 rt2800_bbp_write(rt2x00dev, 31, 0x08);
2086
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002087 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
2088 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002089
2090 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
2091 rt2800_bbp_write(rt2x00dev, 69, 0x16);
2092 rt2800_bbp_write(rt2x00dev, 73, 0x12);
2093 } else {
2094 rt2800_bbp_write(rt2x00dev, 69, 0x12);
2095 rt2800_bbp_write(rt2x00dev, 73, 0x10);
2096 }
2097
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002098 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002099
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002100 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002101 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002102 rt2x00_rt(rt2x00dev, RT3090) ||
2103 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002104 rt2800_bbp_write(rt2x00dev, 79, 0x13);
2105 rt2800_bbp_write(rt2x00dev, 80, 0x05);
2106 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Helmut Schaabaff8002010-04-28 09:58:59 +02002107 } else if (rt2800_is_305x_soc(rt2x00dev)) {
2108 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
2109 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002110 } else {
2111 rt2800_bbp_write(rt2x00dev, 81, 0x37);
2112 }
2113
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002114 rt2800_bbp_write(rt2x00dev, 82, 0x62);
2115 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002116
Gertjan van Wingerde5ed8f452010-06-03 10:51:57 +02002117 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002118 rt2800_bbp_write(rt2x00dev, 84, 0x19);
2119 else
2120 rt2800_bbp_write(rt2x00dev, 84, 0x99);
2121
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002122 rt2800_bbp_write(rt2x00dev, 86, 0x00);
2123 rt2800_bbp_write(rt2x00dev, 91, 0x04);
2124 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002125
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002126 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002127 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002128 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02002129 rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
2130 rt2800_is_305x_soc(rt2x00dev))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002131 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
2132 else
2133 rt2800_bbp_write(rt2x00dev, 103, 0x00);
2134
Helmut Schaabaff8002010-04-28 09:58:59 +02002135 if (rt2800_is_305x_soc(rt2x00dev))
2136 rt2800_bbp_write(rt2x00dev, 105, 0x01);
2137 else
2138 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002139 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002140
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002141 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002142 rt2x00_rt(rt2x00dev, RT3090) ||
2143 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002144 rt2800_bbp_read(rt2x00dev, 138, &value);
2145
2146 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2147 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
2148 value |= 0x20;
2149 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
2150 value &= ~0x02;
2151
2152 rt2800_bbp_write(rt2x00dev, 138, value);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002153 }
2154
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002155
2156 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
2157 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
2158
2159 if (eeprom != 0xffff && eeprom != 0x0000) {
2160 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
2161 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
2162 rt2800_bbp_write(rt2x00dev, reg_id, value);
2163 }
2164 }
2165
2166 return 0;
2167}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002168
2169static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
2170 bool bw40, u8 rfcsr24, u8 filter_target)
2171{
2172 unsigned int i;
2173 u8 bbp;
2174 u8 rfcsr;
2175 u8 passband;
2176 u8 stopband;
2177 u8 overtuned = 0;
2178
2179 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
2180
2181 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2182 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
2183 rt2800_bbp_write(rt2x00dev, 4, bbp);
2184
2185 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
2186 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
2187 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
2188
2189 /*
2190 * Set power & frequency of passband test tone
2191 */
2192 rt2800_bbp_write(rt2x00dev, 24, 0);
2193
2194 for (i = 0; i < 100; i++) {
2195 rt2800_bbp_write(rt2x00dev, 25, 0x90);
2196 msleep(1);
2197
2198 rt2800_bbp_read(rt2x00dev, 55, &passband);
2199 if (passband)
2200 break;
2201 }
2202
2203 /*
2204 * Set power & frequency of stopband test tone
2205 */
2206 rt2800_bbp_write(rt2x00dev, 24, 0x06);
2207
2208 for (i = 0; i < 100; i++) {
2209 rt2800_bbp_write(rt2x00dev, 25, 0x90);
2210 msleep(1);
2211
2212 rt2800_bbp_read(rt2x00dev, 55, &stopband);
2213
2214 if ((passband - stopband) <= filter_target) {
2215 rfcsr24++;
2216 overtuned += ((passband - stopband) == filter_target);
2217 } else
2218 break;
2219
2220 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
2221 }
2222
2223 rfcsr24 -= !!overtuned;
2224
2225 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
2226 return rfcsr24;
2227}
2228
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002229static int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002230{
2231 u8 rfcsr;
2232 u8 bbp;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002233 u32 reg;
2234 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002235
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002236 if (!rt2x00_rt(rt2x00dev, RT3070) &&
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002237 !rt2x00_rt(rt2x00dev, RT3071) &&
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002238 !rt2x00_rt(rt2x00dev, RT3090) &&
Helmut Schaa23812382010-04-26 13:48:45 +02002239 !rt2x00_rt(rt2x00dev, RT3390) &&
Helmut Schaabaff8002010-04-28 09:58:59 +02002240 !rt2800_is_305x_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002241 return 0;
2242
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002243 /*
2244 * Init RF calibration.
2245 */
2246 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2247 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
2248 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2249 msleep(1);
2250 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
2251 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2252
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002253 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002254 rt2x00_rt(rt2x00dev, RT3071) ||
2255 rt2x00_rt(rt2x00dev, RT3090)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002256 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
2257 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
2258 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
2259 rt2800_rfcsr_write(rt2x00dev, 7, 0x70);
2260 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002261 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002262 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
2263 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
2264 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
2265 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
2266 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
2267 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
2268 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
2269 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
2270 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
2271 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
2272 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
2273 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002274 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002275 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
2276 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
2277 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
2278 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
2279 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002280 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002281 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
2282 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
2283 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
2284 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
2285 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
2286 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002287 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002288 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
2289 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002290 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002291 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
2292 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
2293 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
2294 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
2295 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
2296 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
2297 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002298 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002299 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002300 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002301 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
2302 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
2303 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
2304 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
2305 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
2306 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
2307 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Helmut Schaabaff8002010-04-28 09:58:59 +02002308 } else if (rt2800_is_305x_soc(rt2x00dev)) {
Helmut Schaa23812382010-04-26 13:48:45 +02002309 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
2310 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
2311 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
2312 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
2313 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
2314 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
2315 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
2316 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
2317 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
2318 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
2319 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
2320 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
2321 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
2322 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
2323 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
2324 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
2325 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
2326 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
2327 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
2328 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
2329 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
2330 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
2331 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
2332 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
2333 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
2334 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
2335 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
2336 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
2337 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
2338 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
Helmut Schaabaff8002010-04-28 09:58:59 +02002339 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
2340 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
2341 return 0;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002342 }
2343
2344 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
2345 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
2346 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
2347 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
2348 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002349 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
2350 rt2x00_rt(rt2x00dev, RT3090)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002351 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
2352 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
2353 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
2354
2355 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
2356
2357 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
2358 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002359 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
2360 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002361 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2362 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
2363 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
2364 else
2365 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
2366 }
2367 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002368 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
2369 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
2370 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
2371 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002372 }
2373
2374 /*
2375 * Set RX Filter calibration for 20MHz and 40MHz
2376 */
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002377 if (rt2x00_rt(rt2x00dev, RT3070)) {
2378 rt2x00dev->calibration[0] =
2379 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
2380 rt2x00dev->calibration[1] =
2381 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002382 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002383 rt2x00_rt(rt2x00dev, RT3090) ||
2384 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002385 rt2x00dev->calibration[0] =
2386 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
2387 rt2x00dev->calibration[1] =
2388 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002389 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002390
2391 /*
2392 * Set back to initial state
2393 */
2394 rt2800_bbp_write(rt2x00dev, 24, 0);
2395
2396 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
2397 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
2398 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
2399
2400 /*
2401 * set BBP back to BW20
2402 */
2403 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2404 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
2405 rt2800_bbp_write(rt2x00dev, 4, bbp);
2406
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002407 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002408 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002409 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2410 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002411 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
2412
2413 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
2414 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
2415 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
2416
2417 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
2418 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002419 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002420 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2421 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerde8440c292010-06-03 10:52:02 +02002422 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002423 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
2424 }
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002425 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &eeprom);
2426 if (rt2x00_get_field16(eeprom, EEPROM_TXMIXER_GAIN_BG_VAL) >= 1)
2427 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
2428 rt2x00_get_field16(eeprom,
2429 EEPROM_TXMIXER_GAIN_BG_VAL));
2430 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
2431
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002432 if (rt2x00_rt(rt2x00dev, RT3090)) {
2433 rt2800_bbp_read(rt2x00dev, 138, &bbp);
2434
2435 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2436 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
2437 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
2438 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
2439 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
2440
2441 rt2800_bbp_write(rt2x00dev, 138, bbp);
2442 }
2443
2444 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002445 rt2x00_rt(rt2x00dev, RT3090) ||
2446 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002447 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2448 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2449 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2450 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2451 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2452 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2453 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2454
2455 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
2456 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
2457 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
2458
2459 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
2460 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
2461 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
2462
2463 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
2464 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
2465 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
2466 }
2467
2468 if (rt2x00_rt(rt2x00dev, RT3070) || rt2x00_rt(rt2x00dev, RT3071)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002469 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002470 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
2471 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002472 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
2473 else
2474 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
2475 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
2476 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
2477 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
2478 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
2479 }
2480
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002481 return 0;
2482}
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002483
2484int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
2485{
2486 u32 reg;
2487 u16 word;
2488
2489 /*
2490 * Initialize all registers.
2491 */
2492 if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
2493 rt2800_init_registers(rt2x00dev) ||
2494 rt2800_init_bbp(rt2x00dev) ||
2495 rt2800_init_rfcsr(rt2x00dev)))
2496 return -EIO;
2497
2498 /*
2499 * Send signal to firmware during boot time.
2500 */
2501 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
2502
2503 if (rt2x00_is_usb(rt2x00dev) &&
2504 (rt2x00_rt(rt2x00dev, RT3070) ||
2505 rt2x00_rt(rt2x00dev, RT3071) ||
2506 rt2x00_rt(rt2x00dev, RT3572))) {
2507 udelay(200);
2508 rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
2509 udelay(10);
2510 }
2511
2512 /*
2513 * Enable RX.
2514 */
2515 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
2516 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
2517 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
2518 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
2519
2520 udelay(50);
2521
2522 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
2523 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
2524 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
2525 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
2526 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
2527 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
2528
2529 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
2530 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
2531 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
2532 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
2533
2534 /*
2535 * Initialize LED control
2536 */
2537 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED1, &word);
2538 rt2800_mcu_request(rt2x00dev, MCU_LED_1, 0xff,
2539 word & 0xff, (word >> 8) & 0xff);
2540
2541 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED2, &word);
2542 rt2800_mcu_request(rt2x00dev, MCU_LED_2, 0xff,
2543 word & 0xff, (word >> 8) & 0xff);
2544
2545 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED3, &word);
2546 rt2800_mcu_request(rt2x00dev, MCU_LED_3, 0xff,
2547 word & 0xff, (word >> 8) & 0xff);
2548
2549 return 0;
2550}
2551EXPORT_SYMBOL_GPL(rt2800_enable_radio);
2552
2553void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
2554{
2555 u32 reg;
2556
2557 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
2558 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
2559 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
2560 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
2561 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
2562 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
2563 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
2564
2565 /* Wait for DMA, ignore error */
2566 rt2800_wait_wpdma_ready(rt2x00dev);
2567
2568 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
2569 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
2570 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
2571 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
2572
2573 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0);
2574 rt2800_register_write(rt2x00dev, TX_PIN_CFG, 0);
2575}
2576EXPORT_SYMBOL_GPL(rt2800_disable_radio);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002577
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002578int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
2579{
2580 u32 reg;
2581
2582 rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
2583
2584 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
2585}
2586EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
2587
2588static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
2589{
2590 u32 reg;
2591
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002592 mutex_lock(&rt2x00dev->csr_mutex);
2593
2594 rt2800_register_read_lock(rt2x00dev, EFUSE_CTRL, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002595 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
2596 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
2597 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002598 rt2800_register_write_lock(rt2x00dev, EFUSE_CTRL, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002599
2600 /* Wait until the EEPROM has been loaded */
2601 rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
2602
2603 /* Apparently the data is read from end to start */
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002604 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA3,
2605 (u32 *)&rt2x00dev->eeprom[i]);
2606 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA2,
2607 (u32 *)&rt2x00dev->eeprom[i + 2]);
2608 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA1,
2609 (u32 *)&rt2x00dev->eeprom[i + 4]);
2610 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA0,
2611 (u32 *)&rt2x00dev->eeprom[i + 6]);
2612
2613 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002614}
2615
2616void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
2617{
2618 unsigned int i;
2619
2620 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
2621 rt2800_efuse_read(rt2x00dev, i);
2622}
2623EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
2624
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002625int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
2626{
2627 u16 word;
2628 u8 *mac;
2629 u8 default_lna_gain;
2630
2631 /*
2632 * Start validation of the data that has been read.
2633 */
2634 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
2635 if (!is_valid_ether_addr(mac)) {
2636 random_ether_addr(mac);
2637 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
2638 }
2639
2640 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
2641 if (word == 0xffff) {
2642 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2643 rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
2644 rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
2645 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2646 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002647 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02002648 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002649 /*
2650 * There is a max of 2 RX streams for RT28x0 series
2651 */
2652 if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
2653 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2654 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2655 }
2656
2657 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
2658 if (word == 0xffff) {
2659 rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
2660 rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
2661 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
2662 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
2663 rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
2664 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
2665 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
2666 rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
2667 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
2668 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002669 rt2x00_set_field16(&word, EEPROM_NIC_ANT_DIVERSITY, 0);
2670 rt2x00_set_field16(&word, EEPROM_NIC_DAC_TEST, 0);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002671 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
2672 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
2673 }
2674
2675 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
2676 if ((word & 0x00ff) == 0x00ff) {
2677 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002678 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2679 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
2680 }
2681 if ((word & 0xff00) == 0xff00) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002682 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
2683 LED_MODE_TXRX_ACTIVITY);
2684 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
2685 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2686 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
2687 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
2688 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002689 EEPROM(rt2x00dev, "Led Mode: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002690 }
2691
2692 /*
2693 * During the LNA validation we are going to use
2694 * lna0 as correct value. Note that EEPROM_LNA
2695 * is never validated.
2696 */
2697 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
2698 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
2699
2700 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
2701 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
2702 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
2703 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
2704 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
2705 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
2706
2707 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
2708 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
2709 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
2710 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
2711 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
2712 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
2713 default_lna_gain);
2714 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
2715
2716 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
2717 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
2718 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
2719 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
2720 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
2721 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
2722
2723 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
2724 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
2725 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
2726 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
2727 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
2728 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
2729 default_lna_gain);
2730 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
2731
2732 return 0;
2733}
2734EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
2735
2736int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
2737{
2738 u32 reg;
2739 u16 value;
2740 u16 eeprom;
2741
2742 /*
2743 * Read EEPROM word for configuration.
2744 */
2745 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2746
2747 /*
2748 * Identify RF chipset.
2749 */
2750 value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
2751 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
2752
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002753 rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
2754 value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
Gertjan van Wingerde714fa6632010-02-13 20:55:48 +01002755
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002756 if (!rt2x00_rt(rt2x00dev, RT2860) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002757 !rt2x00_rt(rt2x00dev, RT2872) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002758 !rt2x00_rt(rt2x00dev, RT2883) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002759 !rt2x00_rt(rt2x00dev, RT3070) &&
2760 !rt2x00_rt(rt2x00dev, RT3071) &&
2761 !rt2x00_rt(rt2x00dev, RT3090) &&
2762 !rt2x00_rt(rt2x00dev, RT3390) &&
2763 !rt2x00_rt(rt2x00dev, RT3572)) {
2764 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
2765 return -ENODEV;
2766 }
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002767
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01002768 if (!rt2x00_rf(rt2x00dev, RF2820) &&
2769 !rt2x00_rf(rt2x00dev, RF2850) &&
2770 !rt2x00_rf(rt2x00dev, RF2720) &&
2771 !rt2x00_rf(rt2x00dev, RF2750) &&
2772 !rt2x00_rf(rt2x00dev, RF3020) &&
2773 !rt2x00_rf(rt2x00dev, RF2020) &&
2774 !rt2x00_rf(rt2x00dev, RF3021) &&
Gertjan van Wingerde6c0fe262009-12-30 11:36:31 +01002775 !rt2x00_rf(rt2x00dev, RF3022) &&
2776 !rt2x00_rf(rt2x00dev, RF3052)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002777 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
2778 return -ENODEV;
2779 }
2780
2781 /*
2782 * Identify default antenna configuration.
2783 */
2784 rt2x00dev->default_ant.tx =
2785 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH);
2786 rt2x00dev->default_ant.rx =
2787 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH);
2788
2789 /*
2790 * Read frequency offset and RF programming sequence.
2791 */
2792 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
2793 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
2794
2795 /*
2796 * Read external LNA informations.
2797 */
2798 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2799
2800 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
2801 __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
2802 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
2803 __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
2804
2805 /*
2806 * Detect if this device has an hardware controlled radio.
2807 */
2808 if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
2809 __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
2810
2811 /*
2812 * Store led settings, for correct led behaviour.
2813 */
2814#ifdef CONFIG_RT2X00_LIB_LEDS
2815 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
2816 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
2817 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
2818
2819 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &rt2x00dev->led_mcu_reg);
2820#endif /* CONFIG_RT2X00_LIB_LEDS */
2821
2822 return 0;
2823}
2824EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
2825
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002826/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002827 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002828 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
2829 */
2830static const struct rf_channel rf_vals[] = {
2831 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
2832 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
2833 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
2834 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
2835 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
2836 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
2837 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
2838 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
2839 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
2840 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
2841 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
2842 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
2843 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
2844 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
2845
2846 /* 802.11 UNI / HyperLan 2 */
2847 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
2848 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
2849 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
2850 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
2851 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
2852 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
2853 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
2854 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
2855 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
2856 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
2857 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
2858 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
2859
2860 /* 802.11 HyperLan 2 */
2861 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
2862 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
2863 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
2864 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
2865 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
2866 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
2867 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
2868 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
2869 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
2870 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
2871 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
2872 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
2873 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
2874 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
2875 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
2876 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
2877
2878 /* 802.11 UNII */
2879 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
2880 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
2881 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
2882 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
2883 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
2884 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
2885 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
2886 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
2887 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
2888 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
2889 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
2890
2891 /* 802.11 Japan */
2892 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
2893 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
2894 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
2895 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
2896 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
2897 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
2898 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
2899};
2900
2901/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02002902 * RF value list for rt3xxx
2903 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002904 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02002905static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002906 {1, 241, 2, 2 },
2907 {2, 241, 2, 7 },
2908 {3, 242, 2, 2 },
2909 {4, 242, 2, 7 },
2910 {5, 243, 2, 2 },
2911 {6, 243, 2, 7 },
2912 {7, 244, 2, 2 },
2913 {8, 244, 2, 7 },
2914 {9, 245, 2, 2 },
2915 {10, 245, 2, 7 },
2916 {11, 246, 2, 2 },
2917 {12, 246, 2, 7 },
2918 {13, 247, 2, 2 },
2919 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02002920
2921 /* 802.11 UNI / HyperLan 2 */
2922 {36, 0x56, 0, 4},
2923 {38, 0x56, 0, 6},
2924 {40, 0x56, 0, 8},
2925 {44, 0x57, 0, 0},
2926 {46, 0x57, 0, 2},
2927 {48, 0x57, 0, 4},
2928 {52, 0x57, 0, 8},
2929 {54, 0x57, 0, 10},
2930 {56, 0x58, 0, 0},
2931 {60, 0x58, 0, 4},
2932 {62, 0x58, 0, 6},
2933 {64, 0x58, 0, 8},
2934
2935 /* 802.11 HyperLan 2 */
2936 {100, 0x5b, 0, 8},
2937 {102, 0x5b, 0, 10},
2938 {104, 0x5c, 0, 0},
2939 {108, 0x5c, 0, 4},
2940 {110, 0x5c, 0, 6},
2941 {112, 0x5c, 0, 8},
2942 {116, 0x5d, 0, 0},
2943 {118, 0x5d, 0, 2},
2944 {120, 0x5d, 0, 4},
2945 {124, 0x5d, 0, 8},
2946 {126, 0x5d, 0, 10},
2947 {128, 0x5e, 0, 0},
2948 {132, 0x5e, 0, 4},
2949 {134, 0x5e, 0, 6},
2950 {136, 0x5e, 0, 8},
2951 {140, 0x5f, 0, 0},
2952
2953 /* 802.11 UNII */
2954 {149, 0x5f, 0, 9},
2955 {151, 0x5f, 0, 11},
2956 {153, 0x60, 0, 1},
2957 {157, 0x60, 0, 5},
2958 {159, 0x60, 0, 7},
2959 {161, 0x60, 0, 9},
2960 {165, 0x61, 0, 1},
2961 {167, 0x61, 0, 3},
2962 {169, 0x61, 0, 5},
2963 {171, 0x61, 0, 7},
2964 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002965};
2966
2967int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
2968{
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002969 struct hw_mode_spec *spec = &rt2x00dev->spec;
2970 struct channel_info *info;
2971 char *tx_power1;
2972 char *tx_power2;
2973 unsigned int i;
2974 u16 eeprom;
2975
2976 /*
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002977 * Disable powersaving as default on PCI devices.
2978 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01002979 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01002980 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
2981
2982 /*
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002983 * Initialize all hw fields.
2984 */
2985 rt2x00dev->hw->flags =
2986 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2987 IEEE80211_HW_SIGNAL_DBM |
2988 IEEE80211_HW_SUPPORTS_PS |
Helmut Schaa1df90802010-06-29 21:38:12 +02002989 IEEE80211_HW_PS_NULLFUNC_STACK |
2990 IEEE80211_HW_AMPDU_AGGREGATION;
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002991
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002992 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
2993 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
2994 rt2x00_eeprom_addr(rt2x00dev,
2995 EEPROM_MAC_ADDR_0));
2996
Helmut Schaa3f2bee22010-06-14 22:12:01 +02002997 /*
2998 * As rt2800 has a global fallback table we cannot specify
2999 * more then one tx rate per frame but since the hw will
3000 * try several rates (based on the fallback table) we should
3001 * still initialize max_rates to the maximum number of rates
3002 * we are going to try. Otherwise mac80211 will truncate our
3003 * reported tx rates and the rc algortihm will end up with
3004 * incorrect data.
3005 */
3006 rt2x00dev->hw->max_rates = 7;
3007 rt2x00dev->hw->max_rate_tries = 1;
3008
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01003009 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
3010
3011 /*
3012 * Initialize hw_mode information.
3013 */
3014 spec->supported_bands = SUPPORT_BAND_2GHZ;
3015 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
3016
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003017 if (rt2x00_rf(rt2x00dev, RF2820) ||
Ivo van Doorn55f93212010-05-06 14:45:46 +02003018 rt2x00_rf(rt2x00dev, RF2720)) {
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01003019 spec->num_channels = 14;
3020 spec->channels = rf_vals;
Ivo van Doorn55f93212010-05-06 14:45:46 +02003021 } else if (rt2x00_rf(rt2x00dev, RF2850) ||
3022 rt2x00_rf(rt2x00dev, RF2750)) {
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01003023 spec->supported_bands |= SUPPORT_BAND_5GHZ;
3024 spec->num_channels = ARRAY_SIZE(rf_vals);
3025 spec->channels = rf_vals;
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003026 } else if (rt2x00_rf(rt2x00dev, RF3020) ||
3027 rt2x00_rf(rt2x00dev, RF2020) ||
3028 rt2x00_rf(rt2x00dev, RF3021) ||
3029 rt2x00_rf(rt2x00dev, RF3022)) {
Ivo van Doorn55f93212010-05-06 14:45:46 +02003030 spec->num_channels = 14;
3031 spec->channels = rf_vals_3x;
3032 } else if (rt2x00_rf(rt2x00dev, RF3052)) {
3033 spec->supported_bands |= SUPPORT_BAND_5GHZ;
3034 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
3035 spec->channels = rf_vals_3x;
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01003036 }
3037
3038 /*
3039 * Initialize HT information.
3040 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003041 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01003042 spec->ht.ht_supported = true;
3043 else
3044 spec->ht.ht_supported = false;
3045
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01003046 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02003047 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01003048 IEEE80211_HT_CAP_GRN_FLD |
3049 IEEE80211_HT_CAP_SGI_20 |
Ivo van Doornaa674632010-06-29 21:48:37 +02003050 IEEE80211_HT_CAP_SGI_40;
Helmut Schaa22cabaa2010-06-03 10:52:10 +02003051
3052 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) >= 2)
3053 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
3054
Ivo van Doornaa674632010-06-29 21:48:37 +02003055 spec->ht.cap |=
3056 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) <<
3057 IEEE80211_HT_CAP_RX_STBC_SHIFT;
3058
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01003059 spec->ht.ampdu_factor = 3;
3060 spec->ht.ampdu_density = 4;
3061 spec->ht.mcs.tx_params =
3062 IEEE80211_HT_MCS_TX_DEFINED |
3063 IEEE80211_HT_MCS_TX_RX_DIFF |
3064 ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
3065 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
3066
3067 switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
3068 case 3:
3069 spec->ht.mcs.rx_mask[2] = 0xff;
3070 case 2:
3071 spec->ht.mcs.rx_mask[1] = 0xff;
3072 case 1:
3073 spec->ht.mcs.rx_mask[0] = 0xff;
3074 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
3075 break;
3076 }
3077
3078 /*
3079 * Create channel information array
3080 */
3081 info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
3082 if (!info)
3083 return -ENOMEM;
3084
3085 spec->channels_info = info;
3086
3087 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
3088 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
3089
3090 for (i = 0; i < 14; i++) {
3091 info[i].tx_power1 = TXPOWER_G_FROM_DEV(tx_power1[i]);
3092 info[i].tx_power2 = TXPOWER_G_FROM_DEV(tx_power2[i]);
3093 }
3094
3095 if (spec->num_channels > 14) {
3096 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
3097 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
3098
3099 for (i = 14; i < spec->num_channels; i++) {
3100 info[i].tx_power1 = TXPOWER_A_FROM_DEV(tx_power1[i]);
3101 info[i].tx_power2 = TXPOWER_A_FROM_DEV(tx_power2[i]);
3102 }
3103 }
3104
3105 return 0;
3106}
3107EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
3108
3109/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003110 * IEEE80211 stack callback functions.
3111 */
Helmut Schaae7836192010-07-11 12:28:54 +02003112void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx, u32 *iv32,
3113 u16 *iv16)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003114{
3115 struct rt2x00_dev *rt2x00dev = hw->priv;
3116 struct mac_iveiv_entry iveiv_entry;
3117 u32 offset;
3118
3119 offset = MAC_IVEIV_ENTRY(hw_key_idx);
3120 rt2800_register_multiread(rt2x00dev, offset,
3121 &iveiv_entry, sizeof(iveiv_entry));
3122
Julia Lawall855da5e2009-12-13 17:07:45 +01003123 memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
3124 memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003125}
Helmut Schaae7836192010-07-11 12:28:54 +02003126EXPORT_SYMBOL_GPL(rt2800_get_tkip_seq);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003127
Helmut Schaae7836192010-07-11 12:28:54 +02003128int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003129{
3130 struct rt2x00_dev *rt2x00dev = hw->priv;
3131 u32 reg;
3132 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
3133
3134 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
3135 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
3136 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
3137
3138 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
3139 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
3140 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
3141
3142 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
3143 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
3144 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
3145
3146 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
3147 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
3148 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
3149
3150 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
3151 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
3152 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
3153
3154 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
3155 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
3156 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
3157
3158 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
3159 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
3160 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
3161
3162 return 0;
3163}
Helmut Schaae7836192010-07-11 12:28:54 +02003164EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003165
Helmut Schaae7836192010-07-11 12:28:54 +02003166int rt2800_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
3167 const struct ieee80211_tx_queue_params *params)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003168{
3169 struct rt2x00_dev *rt2x00dev = hw->priv;
3170 struct data_queue *queue;
3171 struct rt2x00_field32 field;
3172 int retval;
3173 u32 reg;
3174 u32 offset;
3175
3176 /*
3177 * First pass the configuration through rt2x00lib, that will
3178 * update the queue settings and validate the input. After that
3179 * we are free to update the registers based on the value
3180 * in the queue parameter.
3181 */
3182 retval = rt2x00mac_conf_tx(hw, queue_idx, params);
3183 if (retval)
3184 return retval;
3185
3186 /*
3187 * We only need to perform additional register initialization
3188 * for WMM queues/
3189 */
3190 if (queue_idx >= 4)
3191 return 0;
3192
3193 queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
3194
3195 /* Update WMM TXOP register */
3196 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
3197 field.bit_offset = (queue_idx & 1) * 16;
3198 field.bit_mask = 0xffff << field.bit_offset;
3199
3200 rt2800_register_read(rt2x00dev, offset, &reg);
3201 rt2x00_set_field32(&reg, field, queue->txop);
3202 rt2800_register_write(rt2x00dev, offset, reg);
3203
3204 /* Update WMM registers */
3205 field.bit_offset = queue_idx * 4;
3206 field.bit_mask = 0xf << field.bit_offset;
3207
3208 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
3209 rt2x00_set_field32(&reg, field, queue->aifs);
3210 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
3211
3212 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
3213 rt2x00_set_field32(&reg, field, queue->cw_min);
3214 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
3215
3216 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
3217 rt2x00_set_field32(&reg, field, queue->cw_max);
3218 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
3219
3220 /* Update EDCA registers */
3221 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
3222
3223 rt2800_register_read(rt2x00dev, offset, &reg);
3224 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
3225 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
3226 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
3227 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
3228 rt2800_register_write(rt2x00dev, offset, reg);
3229
3230 return 0;
3231}
Helmut Schaae7836192010-07-11 12:28:54 +02003232EXPORT_SYMBOL_GPL(rt2800_conf_tx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003233
Helmut Schaae7836192010-07-11 12:28:54 +02003234u64 rt2800_get_tsf(struct ieee80211_hw *hw)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003235{
3236 struct rt2x00_dev *rt2x00dev = hw->priv;
3237 u64 tsf;
3238 u32 reg;
3239
3240 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
3241 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
3242 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
3243 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
3244
3245 return tsf;
3246}
Helmut Schaae7836192010-07-11 12:28:54 +02003247EXPORT_SYMBOL_GPL(rt2800_get_tsf);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003248
Helmut Schaae7836192010-07-11 12:28:54 +02003249int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3250 enum ieee80211_ampdu_mlme_action action,
3251 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
Helmut Schaa1df90802010-06-29 21:38:12 +02003252{
Helmut Schaa1df90802010-06-29 21:38:12 +02003253 int ret = 0;
3254
3255 switch (action) {
3256 case IEEE80211_AMPDU_RX_START:
3257 case IEEE80211_AMPDU_RX_STOP:
3258 /* we don't support RX aggregation yet */
3259 ret = -ENOTSUPP;
3260 break;
3261 case IEEE80211_AMPDU_TX_START:
3262 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3263 break;
3264 case IEEE80211_AMPDU_TX_STOP:
3265 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3266 break;
3267 case IEEE80211_AMPDU_TX_OPERATIONAL:
3268 break;
3269 default:
Ivo van Doorn4e9e58c2010-06-29 21:49:50 +02003270 WARNING((struct rt2x00_dev *)hw->priv, "Unknown AMPDU action\n");
Helmut Schaa1df90802010-06-29 21:38:12 +02003271 }
3272
3273 return ret;
3274}
Helmut Schaae7836192010-07-11 12:28:54 +02003275EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02003276
3277MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
3278MODULE_VERSION(DRV_VERSION);
3279MODULE_DESCRIPTION("Ralink RT2800 library");
3280MODULE_LICENSE("GPL");