blob: a53536dc882d76a65c6b312ae92a3d0567b645a9 [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
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200258int rt2800_wait_csr_ready(struct rt2x00_dev *rt2x00dev)
259{
260 unsigned int i = 0;
261 u32 reg;
262
263 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
264 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
265 if (reg && reg != ~0)
266 return 0;
267 msleep(1);
268 }
269
270 ERROR(rt2x00dev, "Unstable hardware.\n");
271 return -EBUSY;
272}
273EXPORT_SYMBOL_GPL(rt2800_wait_csr_ready);
274
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100275int rt2800_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
276{
277 unsigned int i;
278 u32 reg;
279
Helmut Schaa08e53102010-11-04 20:37:47 +0100280 /*
281 * Some devices are really slow to respond here. Wait a whole second
282 * before timing out.
283 */
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100284 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
285 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
286 if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
287 !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
288 return 0;
289
Helmut Schaa08e53102010-11-04 20:37:47 +0100290 msleep(10);
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100291 }
292
293 ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
294 return -EACCES;
295}
296EXPORT_SYMBOL_GPL(rt2800_wait_wpdma_ready);
297
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200298static bool rt2800_check_firmware_crc(const u8 *data, const size_t len)
299{
300 u16 fw_crc;
301 u16 crc;
302
303 /*
304 * The last 2 bytes in the firmware array are the crc checksum itself,
305 * this means that we should never pass those 2 bytes to the crc
306 * algorithm.
307 */
308 fw_crc = (data[len - 2] << 8 | data[len - 1]);
309
310 /*
311 * Use the crc ccitt algorithm.
312 * This will return the same value as the legacy driver which
313 * used bit ordering reversion on the both the firmware bytes
314 * before input input as well as on the final output.
315 * Obviously using crc ccitt directly is much more efficient.
316 */
317 crc = crc_ccitt(~0, data, len - 2);
318
319 /*
320 * There is a small difference between the crc-itu-t + bitrev and
321 * the crc-ccitt crc calculation. In the latter method the 2 bytes
322 * will be swapped, use swab16 to convert the crc to the correct
323 * value.
324 */
325 crc = swab16(crc);
326
327 return fw_crc == crc;
328}
329
330int rt2800_check_firmware(struct rt2x00_dev *rt2x00dev,
331 const u8 *data, const size_t len)
332{
333 size_t offset = 0;
334 size_t fw_len;
335 bool multiple;
336
337 /*
338 * PCI(e) & SOC devices require firmware with a length
339 * of 8kb. USB devices require firmware files with a length
340 * of 4kb. Certain USB chipsets however require different firmware,
341 * which Ralink only provides attached to the original firmware
342 * file. Thus for USB devices, firmware files have a length
343 * which is a multiple of 4kb.
344 */
345 if (rt2x00_is_usb(rt2x00dev)) {
346 fw_len = 4096;
347 multiple = true;
348 } else {
349 fw_len = 8192;
350 multiple = true;
351 }
352
353 /*
354 * Validate the firmware length
355 */
356 if (len != fw_len && (!multiple || (len % fw_len) != 0))
357 return FW_BAD_LENGTH;
358
359 /*
360 * Check if the chipset requires one of the upper parts
361 * of the firmware.
362 */
363 if (rt2x00_is_usb(rt2x00dev) &&
364 !rt2x00_rt(rt2x00dev, RT2860) &&
365 !rt2x00_rt(rt2x00dev, RT2872) &&
366 !rt2x00_rt(rt2x00dev, RT3070) &&
367 ((len / fw_len) == 1))
368 return FW_BAD_VERSION;
369
370 /*
371 * 8kb firmware files must be checked as if it were
372 * 2 separate firmware files.
373 */
374 while (offset < len) {
375 if (!rt2800_check_firmware_crc(data + offset, fw_len))
376 return FW_BAD_CRC;
377
378 offset += fw_len;
379 }
380
381 return FW_OK;
382}
383EXPORT_SYMBOL_GPL(rt2800_check_firmware);
384
385int rt2800_load_firmware(struct rt2x00_dev *rt2x00dev,
386 const u8 *data, const size_t len)
387{
388 unsigned int i;
389 u32 reg;
390
391 /*
Ivo van Doornb9eca242010-08-30 21:13:54 +0200392 * If driver doesn't wake up firmware here,
393 * rt2800_load_firmware will hang forever when interface is up again.
394 */
395 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0x00000000);
396
397 /*
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200398 * Wait for stable hardware.
399 */
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200400 if (rt2800_wait_csr_ready(rt2x00dev))
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200401 return -EBUSY;
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200402
403 if (rt2x00_is_pci(rt2x00dev))
404 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
405
406 /*
407 * Disable DMA, will be reenabled later when enabling
408 * the radio.
409 */
410 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
411 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
412 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
413 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
414 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
415 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
416 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
417
418 /*
419 * Write firmware to the device.
420 */
421 rt2800_drv_write_firmware(rt2x00dev, data, len);
422
423 /*
424 * Wait for device to stabilize.
425 */
426 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
427 rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
428 if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
429 break;
430 msleep(1);
431 }
432
433 if (i == REGISTER_BUSY_COUNT) {
434 ERROR(rt2x00dev, "PBF system register not ready.\n");
435 return -EBUSY;
436 }
437
438 /*
439 * Initialize firmware.
440 */
441 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
442 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
443 msleep(1);
444
445 return 0;
446}
447EXPORT_SYMBOL_GPL(rt2800_load_firmware);
448
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200449void rt2800_write_tx_data(struct queue_entry *entry,
450 struct txentry_desc *txdesc)
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200451{
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200452 __le32 *txwi = rt2800_drv_get_txwi(entry);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200453 u32 word;
454
455 /*
456 * Initialize TX Info descriptor
457 */
458 rt2x00_desc_read(txwi, 0, &word);
459 rt2x00_set_field32(&word, TXWI_W0_FRAG,
460 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
Ivo van Doorn84804cd2010-08-06 20:46:19 +0200461 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS,
462 test_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags));
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200463 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
464 rt2x00_set_field32(&word, TXWI_W0_TS,
465 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
466 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
467 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
468 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
469 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->txop);
470 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
471 rt2x00_set_field32(&word, TXWI_W0_BW,
472 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
473 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
474 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
475 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
476 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
477 rt2x00_desc_write(txwi, 0, word);
478
479 rt2x00_desc_read(txwi, 1, &word);
480 rt2x00_set_field32(&word, TXWI_W1_ACK,
481 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
482 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
483 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
484 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
485 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
486 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
487 txdesc->key_idx : 0xff);
488 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
489 txdesc->length);
Helmut Schaa2b23cda2010-11-04 20:38:15 +0100490 rt2x00_set_field32(&word, TXWI_W1_PACKETID_QUEUE, entry->queue->qid);
Ivo van Doornbc8a9792010-10-02 11:32:43 +0200491 rt2x00_set_field32(&word, TXWI_W1_PACKETID_ENTRY, (entry->entry_idx % 3) + 1);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200492 rt2x00_desc_write(txwi, 1, word);
493
494 /*
495 * Always write 0 to IV/EIV fields, hardware will insert the IV
496 * from the IVEIV register when TXD_W3_WIV is set to 0.
497 * When TXD_W3_WIV is set to 1 it will use the IV data
498 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
499 * crypto entry in the registers should be used to encrypt the frame.
500 */
501 _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
502 _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
503}
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200504EXPORT_SYMBOL_GPL(rt2800_write_tx_data);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200505
Helmut Schaaff6133b2010-10-09 13:34:11 +0200506static int rt2800_agc_to_rssi(struct rt2x00_dev *rt2x00dev, u32 rxwi_w2)
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200507{
Ivo van Doorn74861922010-07-11 12:23:50 +0200508 int rssi0 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI0);
509 int rssi1 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI1);
510 int rssi2 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI2);
511 u16 eeprom;
512 u8 offset0;
513 u8 offset1;
514 u8 offset2;
515
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200516 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Ivo van Doorn74861922010-07-11 12:23:50 +0200517 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &eeprom);
518 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET0);
519 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET1);
520 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
521 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_OFFSET2);
522 } else {
523 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &eeprom);
524 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET0);
525 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET1);
526 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
527 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_OFFSET2);
528 }
529
530 /*
531 * Convert the value from the descriptor into the RSSI value
532 * If the value in the descriptor is 0, it is considered invalid
533 * and the default (extremely low) rssi value is assumed
534 */
535 rssi0 = (rssi0) ? (-12 - offset0 - rt2x00dev->lna_gain - rssi0) : -128;
536 rssi1 = (rssi1) ? (-12 - offset1 - rt2x00dev->lna_gain - rssi1) : -128;
537 rssi2 = (rssi2) ? (-12 - offset2 - rt2x00dev->lna_gain - rssi2) : -128;
538
539 /*
540 * mac80211 only accepts a single RSSI value. Calculating the
541 * average doesn't deliver a fair answer either since -60:-60 would
542 * be considered equally good as -50:-70 while the second is the one
543 * which gives less energy...
544 */
545 rssi0 = max(rssi0, rssi1);
546 return max(rssi0, rssi2);
547}
548
549void rt2800_process_rxwi(struct queue_entry *entry,
550 struct rxdone_entry_desc *rxdesc)
551{
552 __le32 *rxwi = (__le32 *) entry->skb->data;
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200553 u32 word;
554
555 rt2x00_desc_read(rxwi, 0, &word);
556
557 rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
558 rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
559
560 rt2x00_desc_read(rxwi, 1, &word);
561
562 if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
563 rxdesc->flags |= RX_FLAG_SHORT_GI;
564
565 if (rt2x00_get_field32(word, RXWI_W1_BW))
566 rxdesc->flags |= RX_FLAG_40MHZ;
567
568 /*
569 * Detect RX rate, always use MCS as signal type.
570 */
571 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
572 rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
573 rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
574
575 /*
576 * Mask of 0x8 bit to remove the short preamble flag.
577 */
578 if (rxdesc->rate_mode == RATE_MODE_CCK)
579 rxdesc->signal &= ~0x8;
580
581 rt2x00_desc_read(rxwi, 2, &word);
582
Ivo van Doorn74861922010-07-11 12:23:50 +0200583 /*
584 * Convert descriptor AGC value to RSSI value.
585 */
586 rxdesc->rssi = rt2800_agc_to_rssi(entry->queue->rt2x00dev, word);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200587
588 /*
589 * Remove RXWI descriptor from start of buffer.
590 */
Ivo van Doorn74861922010-07-11 12:23:50 +0200591 skb_pull(entry->skb, RXWI_DESC_SIZE);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200592}
593EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
594
Ivo van Doorn36138842010-08-30 21:13:30 +0200595static bool rt2800_txdone_entry_check(struct queue_entry *entry, u32 reg)
596{
597 __le32 *txwi;
598 u32 word;
599 int wcid, ack, pid;
600 int tx_wcid, tx_ack, tx_pid;
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 * This frames has returned with an IO error,
608 * so the status report is not intended for this
609 * frame.
610 */
611 if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags)) {
612 rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
613 return false;
614 }
615
616 /*
617 * Validate if this TX status report is intended for
618 * this entry by comparing the WCID/ACK/PID fields.
619 */
620 txwi = rt2800_drv_get_txwi(entry);
621
622 rt2x00_desc_read(txwi, 1, &word);
623 tx_wcid = rt2x00_get_field32(word, TXWI_W1_WIRELESS_CLI_ID);
624 tx_ack = rt2x00_get_field32(word, TXWI_W1_ACK);
625 tx_pid = rt2x00_get_field32(word, TXWI_W1_PACKETID);
626
627 if ((wcid != tx_wcid) || (ack != tx_ack) || (pid != tx_pid)) {
628 WARNING(entry->queue->rt2x00dev,
629 "TX status report missed for queue %d entry %d\n",
630 entry->queue->qid, entry->entry_idx);
631 rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
632 return false;
633 }
634
635 return true;
636}
637
Helmut Schaa14433332010-10-02 11:27:03 +0200638void rt2800_txdone_entry(struct queue_entry *entry, u32 status)
639{
640 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Helmut Schaab34793e2010-10-02 11:34:56 +0200641 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Helmut Schaa14433332010-10-02 11:27:03 +0200642 struct txdone_entry_desc txdesc;
643 u32 word;
644 u16 mcs, real_mcs;
Helmut Schaab34793e2010-10-02 11:34:56 +0200645 int aggr, ampdu;
Helmut Schaa14433332010-10-02 11:27:03 +0200646 __le32 *txwi;
647
648 /*
649 * Obtain the status about this packet.
650 */
651 txdesc.flags = 0;
652 txwi = rt2800_drv_get_txwi(entry);
653 rt2x00_desc_read(txwi, 0, &word);
Helmut Schaab34793e2010-10-02 11:34:56 +0200654
Helmut Schaa14433332010-10-02 11:27:03 +0200655 mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200656 ampdu = rt2x00_get_field32(word, TXWI_W0_AMPDU);
657
Helmut Schaa14433332010-10-02 11:27:03 +0200658 real_mcs = rt2x00_get_field32(status, TX_STA_FIFO_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200659 aggr = rt2x00_get_field32(status, TX_STA_FIFO_TX_AGGRE);
660
661 /*
662 * If a frame was meant to be sent as a single non-aggregated MPDU
663 * but ended up in an aggregate the used tx rate doesn't correlate
664 * with the one specified in the TXWI as the whole aggregate is sent
665 * with the same rate.
666 *
667 * For example: two frames are sent to rt2x00, the first one sets
668 * AMPDU=1 and requests MCS7 whereas the second frame sets AMDPU=0
669 * and requests MCS15. If the hw aggregates both frames into one
670 * AMDPU the tx status for both frames will contain MCS7 although
671 * the frame was sent successfully.
672 *
673 * Hence, replace the requested rate with the real tx rate to not
674 * confuse the rate control algortihm by providing clearly wrong
675 * data.
676 */
677 if (aggr == 1 && ampdu == 0 && real_mcs != mcs) {
678 skbdesc->tx_rate_idx = real_mcs;
679 mcs = real_mcs;
680 }
Helmut Schaa14433332010-10-02 11:27:03 +0200681
682 /*
683 * Ralink has a retry mechanism using a global fallback
684 * table. We setup this fallback table to try the immediate
685 * lower rate for all rates. In the TX_STA_FIFO, the MCS field
686 * always contains the MCS used for the last transmission, be
687 * it successful or not.
688 */
689 if (rt2x00_get_field32(status, TX_STA_FIFO_TX_SUCCESS)) {
690 /*
691 * Transmission succeeded. The number of retries is
692 * mcs - real_mcs
693 */
694 __set_bit(TXDONE_SUCCESS, &txdesc.flags);
695 txdesc.retry = ((mcs > real_mcs) ? mcs - real_mcs : 0);
696 } else {
697 /*
698 * Transmission failed. The number of retries is
699 * always 7 in this case (for a total number of 8
700 * frames sent).
701 */
702 __set_bit(TXDONE_FAILURE, &txdesc.flags);
703 txdesc.retry = rt2x00dev->long_retry;
704 }
705
706 /*
707 * the frame was retried at least once
708 * -> hw used fallback rates
709 */
710 if (txdesc.retry)
711 __set_bit(TXDONE_FALLBACK, &txdesc.flags);
712
713 rt2x00lib_txdone(entry, &txdesc);
714}
715EXPORT_SYMBOL_GPL(rt2800_txdone_entry);
716
Ivo van Doorn96481b22010-08-06 20:47:57 +0200717void rt2800_txdone(struct rt2x00_dev *rt2x00dev)
718{
719 struct data_queue *queue;
720 struct queue_entry *entry;
Ivo van Doorn96481b22010-08-06 20:47:57 +0200721 u32 reg;
Ivo van Doorn36138842010-08-30 21:13:30 +0200722 u8 pid;
Ivo van Doorn96481b22010-08-06 20:47:57 +0200723 int i;
724
725 /*
726 * TX_STA_FIFO is a stack of X entries, hence read TX_STA_FIFO
727 * at most X times and also stop processing once the TX_STA_FIFO_VALID
728 * flag is not set anymore.
729 *
730 * The legacy drivers use X=TX_RING_SIZE but state in a comment
731 * that the TX_STA_FIFO stack has a size of 16. We stick to our
732 * tx ring size for now.
733 */
Helmut Schaaefd2f272010-11-04 20:37:22 +0100734 for (i = 0; i < rt2x00dev->ops->tx->entry_num; i++) {
Ivo van Doorn96481b22010-08-06 20:47:57 +0200735 rt2800_register_read(rt2x00dev, TX_STA_FIFO, &reg);
736 if (!rt2x00_get_field32(reg, TX_STA_FIFO_VALID))
737 break;
738
Ivo van Doorn96481b22010-08-06 20:47:57 +0200739 /*
740 * Skip this entry when it contains an invalid
741 * queue identication number.
742 */
Ivo van Doornbc8a9792010-10-02 11:32:43 +0200743 pid = rt2x00_get_field32(reg, TX_STA_FIFO_PID_QUEUE);
Ivo van Doorn36138842010-08-30 21:13:30 +0200744 if (pid >= QID_RX)
Ivo van Doorn96481b22010-08-06 20:47:57 +0200745 continue;
746
Ivo van Doorn36138842010-08-30 21:13:30 +0200747 queue = rt2x00queue_get_queue(rt2x00dev, pid);
Ivo van Doorn96481b22010-08-06 20:47:57 +0200748 if (unlikely(!queue))
749 continue;
750
751 /*
752 * Inside each queue, we process each entry in a chronological
753 * order. We first check that the queue is not empty.
754 */
755 entry = NULL;
756 while (!rt2x00queue_empty(queue)) {
757 entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
Ivo van Doorn36138842010-08-30 21:13:30 +0200758 if (rt2800_txdone_entry_check(entry, reg))
Ivo van Doorn96481b22010-08-06 20:47:57 +0200759 break;
Ivo van Doorn96481b22010-08-06 20:47:57 +0200760 }
761
762 if (!entry || rt2x00queue_empty(queue))
763 break;
764
Helmut Schaa14433332010-10-02 11:27:03 +0200765 rt2800_txdone_entry(entry, reg);
Ivo van Doorn96481b22010-08-06 20:47:57 +0200766 }
767}
768EXPORT_SYMBOL_GPL(rt2800_txdone);
769
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200770void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
771{
772 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
773 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
774 unsigned int beacon_base;
775 u32 reg;
776
777 /*
778 * Disable beaconing while we are reloading the beacon data,
779 * otherwise we might be sending out invalid data.
780 */
781 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
782 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
783 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
784
785 /*
786 * Add space for the TXWI in front of the skb.
787 */
788 skb_push(entry->skb, TXWI_DESC_SIZE);
789 memset(entry->skb, 0, TXWI_DESC_SIZE);
790
791 /*
792 * Register descriptor details in skb frame descriptor.
793 */
794 skbdesc->flags |= SKBDESC_DESC_IN_SKB;
795 skbdesc->desc = entry->skb->data;
796 skbdesc->desc_len = TXWI_DESC_SIZE;
797
798 /*
799 * Add the TXWI for the beacon to the skb.
800 */
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200801 rt2800_write_tx_data(entry, txdesc);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200802
803 /*
804 * Dump beacon to userspace through debugfs.
805 */
806 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
807
808 /*
809 * Write entire beacon with TXWI to register.
810 */
811 beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
812 rt2800_register_multiwrite(rt2x00dev, beacon_base,
813 entry->skb->data, entry->skb->len);
814
815 /*
816 * Enable beaconing again.
817 */
818 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
819 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
820 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
821 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
822
823 /*
824 * Clean up beacon skb.
825 */
826 dev_kfree_skb_any(entry->skb);
827 entry->skb = NULL;
828}
Ivo van Doorn50e888e2010-07-11 12:26:12 +0200829EXPORT_SYMBOL_GPL(rt2800_write_beacon);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200830
Helmut Schaafdb87252010-06-29 21:48:06 +0200831static void inline rt2800_clear_beacon(struct rt2x00_dev *rt2x00dev,
832 unsigned int beacon_base)
833{
834 int i;
835
836 /*
837 * For the Beacon base registers we only need to clear
838 * the whole TXWI which (when set to 0) will invalidate
839 * the entire beacon.
840 */
841 for (i = 0; i < TXWI_DESC_SIZE; i += sizeof(__le32))
842 rt2800_register_write(rt2x00dev, beacon_base + i, 0);
843}
844
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100845#ifdef CONFIG_RT2X00_LIB_DEBUGFS
846const struct rt2x00debug rt2800_rt2x00debug = {
847 .owner = THIS_MODULE,
848 .csr = {
849 .read = rt2800_register_read,
850 .write = rt2800_register_write,
851 .flags = RT2X00DEBUGFS_OFFSET,
852 .word_base = CSR_REG_BASE,
853 .word_size = sizeof(u32),
854 .word_count = CSR_REG_SIZE / sizeof(u32),
855 },
856 .eeprom = {
857 .read = rt2x00_eeprom_read,
858 .write = rt2x00_eeprom_write,
859 .word_base = EEPROM_BASE,
860 .word_size = sizeof(u16),
861 .word_count = EEPROM_SIZE / sizeof(u16),
862 },
863 .bbp = {
864 .read = rt2800_bbp_read,
865 .write = rt2800_bbp_write,
866 .word_base = BBP_BASE,
867 .word_size = sizeof(u8),
868 .word_count = BBP_SIZE / sizeof(u8),
869 },
870 .rf = {
871 .read = rt2x00_rf_read,
872 .write = rt2800_rf_write,
873 .word_base = RF_BASE,
874 .word_size = sizeof(u32),
875 .word_count = RF_SIZE / sizeof(u32),
876 },
877};
878EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
879#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
880
881int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
882{
883 u32 reg;
884
885 rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
886 return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
887}
888EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
889
890#ifdef CONFIG_RT2X00_LIB_LEDS
891static void rt2800_brightness_set(struct led_classdev *led_cdev,
892 enum led_brightness brightness)
893{
894 struct rt2x00_led *led =
895 container_of(led_cdev, struct rt2x00_led, led_dev);
896 unsigned int enabled = brightness != LED_OFF;
897 unsigned int bg_mode =
898 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
899 unsigned int polarity =
900 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
901 EEPROM_FREQ_LED_POLARITY);
902 unsigned int ledmode =
903 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
904 EEPROM_FREQ_LED_MODE);
905
906 if (led->type == LED_TYPE_RADIO) {
907 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
908 enabled ? 0x20 : 0);
909 } else if (led->type == LED_TYPE_ASSOC) {
910 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
911 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
912 } else if (led->type == LED_TYPE_QUALITY) {
913 /*
914 * The brightness is divided into 6 levels (0 - 5),
915 * The specs tell us the following levels:
916 * 0, 1 ,3, 7, 15, 31
917 * to determine the level in a simple way we can simply
918 * work with bitshifting:
919 * (1 << level) - 1
920 */
921 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
922 (1 << brightness / (LED_FULL / 6)) - 1,
923 polarity);
924 }
925}
926
927static int rt2800_blink_set(struct led_classdev *led_cdev,
928 unsigned long *delay_on, unsigned long *delay_off)
929{
930 struct rt2x00_led *led =
931 container_of(led_cdev, struct rt2x00_led, led_dev);
932 u32 reg;
933
934 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
935 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
936 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100937 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
938
939 return 0;
940}
941
Gertjan van Wingerdeb3579d62009-12-30 11:36:34 +0100942static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100943 struct rt2x00_led *led, enum led_type type)
944{
945 led->rt2x00dev = rt2x00dev;
946 led->type = type;
947 led->led_dev.brightness_set = rt2800_brightness_set;
948 led->led_dev.blink_set = rt2800_blink_set;
949 led->flags = LED_INITIALIZED;
950}
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100951#endif /* CONFIG_RT2X00_LIB_LEDS */
952
953/*
954 * Configuration handlers.
955 */
956static void rt2800_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
957 struct rt2x00lib_crypto *crypto,
958 struct ieee80211_key_conf *key)
959{
960 struct mac_wcid_entry wcid_entry;
961 struct mac_iveiv_entry iveiv_entry;
962 u32 offset;
963 u32 reg;
964
965 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
966
Ivo van Doorne4a0ab32010-06-14 22:14:19 +0200967 if (crypto->cmd == SET_KEY) {
968 rt2800_register_read(rt2x00dev, offset, &reg);
969 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
970 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
971 /*
972 * Both the cipher as the BSS Idx numbers are split in a main
973 * value of 3 bits, and a extended field for adding one additional
974 * bit to the value.
975 */
976 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
977 (crypto->cipher & 0x7));
978 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
979 (crypto->cipher & 0x8) >> 3);
980 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
981 (crypto->bssidx & 0x7));
982 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
983 (crypto->bssidx & 0x8) >> 3);
984 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
985 rt2800_register_write(rt2x00dev, offset, reg);
986 } else {
987 rt2800_register_write(rt2x00dev, offset, 0);
988 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100989
990 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
991
992 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
993 if ((crypto->cipher == CIPHER_TKIP) ||
994 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
995 (crypto->cipher == CIPHER_AES))
996 iveiv_entry.iv[3] |= 0x20;
997 iveiv_entry.iv[3] |= key->keyidx << 6;
998 rt2800_register_multiwrite(rt2x00dev, offset,
999 &iveiv_entry, sizeof(iveiv_entry));
1000
1001 offset = MAC_WCID_ENTRY(key->hw_key_idx);
1002
1003 memset(&wcid_entry, 0, sizeof(wcid_entry));
1004 if (crypto->cmd == SET_KEY)
1005 memcpy(&wcid_entry, crypto->address, ETH_ALEN);
1006 rt2800_register_multiwrite(rt2x00dev, offset,
1007 &wcid_entry, sizeof(wcid_entry));
1008}
1009
1010int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
1011 struct rt2x00lib_crypto *crypto,
1012 struct ieee80211_key_conf *key)
1013{
1014 struct hw_key_entry key_entry;
1015 struct rt2x00_field32 field;
1016 u32 offset;
1017 u32 reg;
1018
1019 if (crypto->cmd == SET_KEY) {
1020 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
1021
1022 memcpy(key_entry.key, crypto->key,
1023 sizeof(key_entry.key));
1024 memcpy(key_entry.tx_mic, crypto->tx_mic,
1025 sizeof(key_entry.tx_mic));
1026 memcpy(key_entry.rx_mic, crypto->rx_mic,
1027 sizeof(key_entry.rx_mic));
1028
1029 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
1030 rt2800_register_multiwrite(rt2x00dev, offset,
1031 &key_entry, sizeof(key_entry));
1032 }
1033
1034 /*
1035 * The cipher types are stored over multiple registers
1036 * starting with SHARED_KEY_MODE_BASE each word will have
1037 * 32 bits and contains the cipher types for 2 bssidx each.
1038 * Using the correct defines correctly will cause overhead,
1039 * so just calculate the correct offset.
1040 */
1041 field.bit_offset = 4 * (key->hw_key_idx % 8);
1042 field.bit_mask = 0x7 << field.bit_offset;
1043
1044 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
1045
1046 rt2800_register_read(rt2x00dev, offset, &reg);
1047 rt2x00_set_field32(&reg, field,
1048 (crypto->cmd == SET_KEY) * crypto->cipher);
1049 rt2800_register_write(rt2x00dev, offset, reg);
1050
1051 /*
1052 * Update WCID information
1053 */
1054 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
1055
1056 return 0;
1057}
1058EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
1059
1060int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
1061 struct rt2x00lib_crypto *crypto,
1062 struct ieee80211_key_conf *key)
1063{
1064 struct hw_key_entry key_entry;
1065 u32 offset;
1066
1067 if (crypto->cmd == SET_KEY) {
1068 /*
1069 * 1 pairwise key is possible per AID, this means that the AID
1070 * equals our hw_key_idx. Make sure the WCID starts _after_ the
1071 * last possible shared key entry.
Helmut Schaa2a0cfeb2010-10-02 11:26:17 +02001072 *
1073 * Since parts of the pairwise key table might be shared with
1074 * the beacon frame buffers 6 & 7 we should only write into the
1075 * first 222 entries.
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001076 */
Helmut Schaa2a0cfeb2010-10-02 11:26:17 +02001077 if (crypto->aid > (222 - 32))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001078 return -ENOSPC;
1079
1080 key->hw_key_idx = 32 + crypto->aid;
1081
1082 memcpy(key_entry.key, crypto->key,
1083 sizeof(key_entry.key));
1084 memcpy(key_entry.tx_mic, crypto->tx_mic,
1085 sizeof(key_entry.tx_mic));
1086 memcpy(key_entry.rx_mic, crypto->rx_mic,
1087 sizeof(key_entry.rx_mic));
1088
1089 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
1090 rt2800_register_multiwrite(rt2x00dev, offset,
1091 &key_entry, sizeof(key_entry));
1092 }
1093
1094 /*
1095 * Update WCID information
1096 */
1097 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
1098
1099 return 0;
1100}
1101EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
1102
1103void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
1104 const unsigned int filter_flags)
1105{
1106 u32 reg;
1107
1108 /*
1109 * Start configuration steps.
1110 * Note that the version error will always be dropped
1111 * and broadcast frames will always be accepted since
1112 * there is no filter for it at this time.
1113 */
1114 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
1115 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
1116 !(filter_flags & FIF_FCSFAIL));
1117 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
1118 !(filter_flags & FIF_PLCPFAIL));
1119 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
1120 !(filter_flags & FIF_PROMISC_IN_BSS));
1121 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
1122 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
1123 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
1124 !(filter_flags & FIF_ALLMULTI));
1125 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
1126 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
1127 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
1128 !(filter_flags & FIF_CONTROL));
1129 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
1130 !(filter_flags & FIF_CONTROL));
1131 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
1132 !(filter_flags & FIF_CONTROL));
1133 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
1134 !(filter_flags & FIF_CONTROL));
1135 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
1136 !(filter_flags & FIF_CONTROL));
1137 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
1138 !(filter_flags & FIF_PSPOLL));
1139 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
1140 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
1141 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
1142 !(filter_flags & FIF_CONTROL));
1143 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
1144}
1145EXPORT_SYMBOL_GPL(rt2800_config_filter);
1146
1147void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
1148 struct rt2x00intf_conf *conf, const unsigned int flags)
1149{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001150 u32 reg;
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001151 bool update_bssid = false;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001152
1153 if (flags & CONFIG_UPDATE_TYPE) {
1154 /*
1155 * Clear current synchronisation setup.
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001156 */
Helmut Schaafdb87252010-06-29 21:48:06 +02001157 rt2800_clear_beacon(rt2x00dev,
1158 HW_BEACON_OFFSET(intf->beacon->entry_idx));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001159 /*
1160 * Enable synchronisation.
1161 */
1162 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1163 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
1164 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Josef Bacik6a62e5ef2009-11-15 21:33:18 -05001165 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE,
Helmut Schaaab8966d2010-07-11 12:30:13 +02001166 (conf->sync == TSF_SYNC_ADHOC ||
1167 conf->sync == TSF_SYNC_AP_NONE));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001168 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Helmut Schaa9f926fb2010-07-11 12:28:23 +02001169
1170 /*
1171 * Enable pre tbtt interrupt for beaconing modes
1172 */
1173 rt2800_register_read(rt2x00dev, INT_TIMER_EN, &reg);
1174 rt2x00_set_field32(&reg, INT_TIMER_EN_PRE_TBTT_TIMER,
Helmut Schaaab8966d2010-07-11 12:30:13 +02001175 (conf->sync == TSF_SYNC_AP_NONE));
Helmut Schaa9f926fb2010-07-11 12:28:23 +02001176 rt2800_register_write(rt2x00dev, INT_TIMER_EN, reg);
1177
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001178 }
1179
1180 if (flags & CONFIG_UPDATE_MAC) {
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001181 if (flags & CONFIG_UPDATE_TYPE &&
1182 conf->sync == TSF_SYNC_AP_NONE) {
1183 /*
1184 * The BSSID register has to be set to our own mac
1185 * address in AP mode.
1186 */
1187 memcpy(conf->bssid, conf->mac, sizeof(conf->mac));
1188 update_bssid = true;
1189 }
1190
Ivo van Doornc600c822010-08-30 21:14:15 +02001191 if (!is_zero_ether_addr((const u8 *)conf->mac)) {
1192 reg = le32_to_cpu(conf->mac[1]);
1193 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
1194 conf->mac[1] = cpu_to_le32(reg);
1195 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001196
1197 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
1198 conf->mac, sizeof(conf->mac));
1199 }
1200
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001201 if ((flags & CONFIG_UPDATE_BSSID) || update_bssid) {
Ivo van Doornc600c822010-08-30 21:14:15 +02001202 if (!is_zero_ether_addr((const u8 *)conf->bssid)) {
1203 reg = le32_to_cpu(conf->bssid[1]);
1204 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
1205 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 7);
1206 conf->bssid[1] = cpu_to_le32(reg);
1207 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001208
1209 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
1210 conf->bssid, sizeof(conf->bssid));
1211 }
1212}
1213EXPORT_SYMBOL_GPL(rt2800_config_intf);
1214
Helmut Schaa87c19152010-10-02 11:28:34 +02001215static void rt2800_config_ht_opmode(struct rt2x00_dev *rt2x00dev,
1216 struct rt2x00lib_erp *erp)
1217{
1218 bool any_sta_nongf = !!(erp->ht_opmode &
1219 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1220 u8 protection = erp->ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION;
1221 u8 mm20_mode, mm40_mode, gf20_mode, gf40_mode;
1222 u16 mm20_rate, mm40_rate, gf20_rate, gf40_rate;
1223 u32 reg;
1224
1225 /* default protection rate for HT20: OFDM 24M */
1226 mm20_rate = gf20_rate = 0x4004;
1227
1228 /* default protection rate for HT40: duplicate OFDM 24M */
1229 mm40_rate = gf40_rate = 0x4084;
1230
1231 switch (protection) {
1232 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
1233 /*
1234 * All STAs in this BSS are HT20/40 but there might be
1235 * STAs not supporting greenfield mode.
1236 * => Disable protection for HT transmissions.
1237 */
1238 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 0;
1239
1240 break;
1241 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
1242 /*
1243 * All STAs in this BSS are HT20 or HT20/40 but there
1244 * might be STAs not supporting greenfield mode.
1245 * => Protect all HT40 transmissions.
1246 */
1247 mm20_mode = gf20_mode = 0;
1248 mm40_mode = gf40_mode = 2;
1249
1250 break;
1251 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
1252 /*
1253 * Nonmember protection:
1254 * According to 802.11n we _should_ protect all
1255 * HT transmissions (but we don't have to).
1256 *
1257 * But if cts_protection is enabled we _shall_ protect
1258 * all HT transmissions using a CCK rate.
1259 *
1260 * And if any station is non GF we _shall_ protect
1261 * GF transmissions.
1262 *
1263 * We decide to protect everything
1264 * -> fall through to mixed mode.
1265 */
1266 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
1267 /*
1268 * Legacy STAs are present
1269 * => Protect all HT transmissions.
1270 */
1271 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 2;
1272
1273 /*
1274 * If erp protection is needed we have to protect HT
1275 * transmissions with CCK 11M long preamble.
1276 */
1277 if (erp->cts_protection) {
1278 /* don't duplicate RTS/CTS in CCK mode */
1279 mm20_rate = mm40_rate = 0x0003;
1280 gf20_rate = gf40_rate = 0x0003;
1281 }
1282 break;
1283 };
1284
1285 /* check for STAs not supporting greenfield mode */
1286 if (any_sta_nongf)
1287 gf20_mode = gf40_mode = 2;
1288
1289 /* Update HT protection config */
1290 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1291 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, mm20_rate);
1292 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, mm20_mode);
1293 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1294
1295 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1296 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, mm40_rate);
1297 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, mm40_mode);
1298 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1299
1300 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1301 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, gf20_rate);
1302 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, gf20_mode);
1303 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1304
1305 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1306 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, gf40_rate);
1307 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, gf40_mode);
1308 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1309}
1310
Helmut Schaa02044642010-09-08 20:56:32 +02001311void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp,
1312 u32 changed)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001313{
1314 u32 reg;
1315
Helmut Schaa02044642010-09-08 20:56:32 +02001316 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1317 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1318 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
1319 !!erp->short_preamble);
1320 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
1321 !!erp->short_preamble);
1322 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1323 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001324
Helmut Schaa02044642010-09-08 20:56:32 +02001325 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1326 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
1327 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
1328 erp->cts_protection ? 2 : 0);
1329 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1330 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001331
Helmut Schaa02044642010-09-08 20:56:32 +02001332 if (changed & BSS_CHANGED_BASIC_RATES) {
1333 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
1334 erp->basic_rates);
1335 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1336 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001337
Helmut Schaa02044642010-09-08 20:56:32 +02001338 if (changed & BSS_CHANGED_ERP_SLOT) {
1339 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1340 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME,
1341 erp->slot_time);
1342 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001343
Helmut Schaa02044642010-09-08 20:56:32 +02001344 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
1345 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
1346 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1347 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001348
Helmut Schaa02044642010-09-08 20:56:32 +02001349 if (changed & BSS_CHANGED_BEACON_INT) {
1350 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1351 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
1352 erp->beacon_int * 16);
1353 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1354 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001355
1356 if (changed & BSS_CHANGED_HT)
1357 rt2800_config_ht_opmode(rt2x00dev, erp);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001358}
1359EXPORT_SYMBOL_GPL(rt2800_config_erp);
1360
1361void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
1362{
1363 u8 r1;
1364 u8 r3;
1365
1366 rt2800_bbp_read(rt2x00dev, 1, &r1);
1367 rt2800_bbp_read(rt2x00dev, 3, &r3);
1368
1369 /*
1370 * Configure the TX antenna.
1371 */
1372 switch ((int)ant->tx) {
1373 case 1:
1374 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001375 break;
1376 case 2:
1377 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
1378 break;
1379 case 3:
Ivo van Doorne22557f2010-06-29 21:49:05 +02001380 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001381 break;
1382 }
1383
1384 /*
1385 * Configure the RX antenna.
1386 */
1387 switch ((int)ant->rx) {
1388 case 1:
1389 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
1390 break;
1391 case 2:
1392 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
1393 break;
1394 case 3:
1395 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
1396 break;
1397 }
1398
1399 rt2800_bbp_write(rt2x00dev, 3, r3);
1400 rt2800_bbp_write(rt2x00dev, 1, r1);
1401}
1402EXPORT_SYMBOL_GPL(rt2800_config_ant);
1403
1404static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
1405 struct rt2x00lib_conf *libconf)
1406{
1407 u16 eeprom;
1408 short lna_gain;
1409
1410 if (libconf->rf.channel <= 14) {
1411 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1412 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
1413 } else if (libconf->rf.channel <= 64) {
1414 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1415 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
1416 } else if (libconf->rf.channel <= 128) {
1417 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
1418 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
1419 } else {
1420 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
1421 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
1422 }
1423
1424 rt2x00dev->lna_gain = lna_gain;
1425}
1426
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001427static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
1428 struct ieee80211_conf *conf,
1429 struct rf_channel *rf,
1430 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001431{
1432 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
1433
1434 if (rt2x00dev->default_ant.tx == 1)
1435 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
1436
1437 if (rt2x00dev->default_ant.rx == 1) {
1438 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
1439 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1440 } else if (rt2x00dev->default_ant.rx == 2)
1441 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1442
1443 if (rf->channel > 14) {
1444 /*
1445 * When TX power is below 0, we should increase it by 7 to
1446 * make it a positive value (Minumum value is -7).
1447 * However this means that values between 0 and 7 have
1448 * double meaning, and we should set a 7DBm boost flag.
1449 */
1450 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001451 (info->default_power1 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001452
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001453 if (info->default_power1 < 0)
1454 info->default_power1 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001455
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001456 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001457
1458 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001459 (info->default_power2 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001460
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001461 if (info->default_power2 < 0)
1462 info->default_power2 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001463
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001464 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001465 } else {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001466 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G, info->default_power1);
1467 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001468 }
1469
1470 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
1471
1472 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1473 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1474 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1475 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1476
1477 udelay(200);
1478
1479 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1480 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1481 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
1482 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1483
1484 udelay(200);
1485
1486 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1487 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1488 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1489 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1490}
1491
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001492static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
1493 struct ieee80211_conf *conf,
1494 struct rf_channel *rf,
1495 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001496{
1497 u8 rfcsr;
1498
1499 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Gertjan van Wingerde41a26172009-11-09 22:59:04 +01001500 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001501
1502 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +02001503 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001504 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1505
1506 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001507 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001508 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
1509
Helmut Schaa5a673962010-04-23 15:54:43 +02001510 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001511 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER, info->default_power2);
Helmut Schaa5a673962010-04-23 15:54:43 +02001512 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
1513
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001514 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
1515 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
1516 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
1517
1518 rt2800_rfcsr_write(rt2x00dev, 24,
1519 rt2x00dev->calibration[conf_is_ht40(conf)]);
1520
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001521 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001522 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001523 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001524}
1525
1526static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
1527 struct ieee80211_conf *conf,
1528 struct rf_channel *rf,
1529 struct channel_info *info)
1530{
1531 u32 reg;
1532 unsigned int tx_pin;
1533 u8 bbp;
1534
Ivo van Doorn46323e12010-08-23 19:55:43 +02001535 if (rf->channel <= 14) {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001536 info->default_power1 = TXPOWER_G_TO_DEV(info->default_power1);
1537 info->default_power2 = TXPOWER_G_TO_DEV(info->default_power2);
Ivo van Doorn46323e12010-08-23 19:55:43 +02001538 } else {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001539 info->default_power1 = TXPOWER_A_TO_DEV(info->default_power1);
1540 info->default_power2 = TXPOWER_A_TO_DEV(info->default_power2);
Ivo van Doorn46323e12010-08-23 19:55:43 +02001541 }
1542
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001543 if (rt2x00_rf(rt2x00dev, RF2020) ||
1544 rt2x00_rf(rt2x00dev, RF3020) ||
1545 rt2x00_rf(rt2x00dev, RF3021) ||
Ivo van Doorn46323e12010-08-23 19:55:43 +02001546 rt2x00_rf(rt2x00dev, RF3022) ||
1547 rt2x00_rf(rt2x00dev, RF3052))
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001548 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerdefa6f6322009-11-09 22:59:58 +01001549 else
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001550 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001551
1552 /*
1553 * Change BBP settings
1554 */
1555 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
1556 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
1557 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
1558 rt2800_bbp_write(rt2x00dev, 86, 0);
1559
1560 if (rf->channel <= 14) {
1561 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
1562 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1563 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1564 } else {
1565 rt2800_bbp_write(rt2x00dev, 82, 0x84);
1566 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1567 }
1568 } else {
1569 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
1570
1571 if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
1572 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1573 else
1574 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1575 }
1576
1577 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001578 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001579 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
1580 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
1581 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
1582
1583 tx_pin = 0;
1584
1585 /* Turn on unused PA or LNA when not using 1T or 1R */
1586 if (rt2x00dev->default_ant.tx != 1) {
1587 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
1588 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
1589 }
1590
1591 /* Turn on unused PA or LNA when not using 1T or 1R */
1592 if (rt2x00dev->default_ant.rx != 1) {
1593 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
1594 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
1595 }
1596
1597 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
1598 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
1599 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
1600 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
1601 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
1602 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
1603
1604 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
1605
1606 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1607 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
1608 rt2800_bbp_write(rt2x00dev, 4, bbp);
1609
1610 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001611 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001612 rt2800_bbp_write(rt2x00dev, 3, bbp);
1613
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001614 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001615 if (conf_is_ht40(conf)) {
1616 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
1617 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1618 rt2800_bbp_write(rt2x00dev, 73, 0x16);
1619 } else {
1620 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1621 rt2800_bbp_write(rt2x00dev, 70, 0x08);
1622 rt2800_bbp_write(rt2x00dev, 73, 0x11);
1623 }
1624 }
1625
1626 msleep(1);
1627}
1628
1629static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
Helmut Schaa5e846002010-07-11 12:23:09 +02001630 const int max_txpower)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001631{
Helmut Schaa5e846002010-07-11 12:23:09 +02001632 u8 txpower;
1633 u8 max_value = (u8)max_txpower;
1634 u16 eeprom;
1635 int i;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001636 u32 reg;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001637 u8 r1;
Helmut Schaa5e846002010-07-11 12:23:09 +02001638 u32 offset;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001639
Helmut Schaa5e846002010-07-11 12:23:09 +02001640 /*
1641 * set to normal tx power mode: +/- 0dBm
1642 */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001643 rt2800_bbp_read(rt2x00dev, 1, &r1);
Helmut Schaaa3f84ca2010-06-14 22:11:32 +02001644 rt2x00_set_field8(&r1, BBP1_TX_POWER, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001645 rt2800_bbp_write(rt2x00dev, 1, r1);
1646
Helmut Schaa5e846002010-07-11 12:23:09 +02001647 /*
1648 * The eeprom contains the tx power values for each rate. These
1649 * values map to 100% tx power. Each 16bit word contains four tx
1650 * power values and the order is the same as used in the TX_PWR_CFG
1651 * registers.
1652 */
1653 offset = TX_PWR_CFG_0;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001654
Helmut Schaa5e846002010-07-11 12:23:09 +02001655 for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
1656 /* just to be safe */
1657 if (offset > TX_PWR_CFG_4)
1658 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001659
Helmut Schaa5e846002010-07-11 12:23:09 +02001660 rt2800_register_read(rt2x00dev, offset, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001661
Helmut Schaa5e846002010-07-11 12:23:09 +02001662 /* read the next four txpower values */
1663 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i,
1664 &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001665
Helmut Schaa5e846002010-07-11 12:23:09 +02001666 /* TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
1667 * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
1668 * TX_PWR_CFG_4: unknown */
1669 txpower = rt2x00_get_field16(eeprom,
1670 EEPROM_TXPOWER_BYRATE_RATE0);
1671 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0,
1672 min(txpower, max_value));
1673
1674 /* TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
1675 * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
1676 * TX_PWR_CFG_4: unknown */
1677 txpower = rt2x00_get_field16(eeprom,
1678 EEPROM_TXPOWER_BYRATE_RATE1);
1679 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1,
1680 min(txpower, max_value));
1681
1682 /* TX_PWR_CFG_0: 55MBS, TX_PWR_CFG_1: 48MBS,
1683 * TX_PWR_CFG_2: MCS6, TX_PWR_CFG_3: MCS14,
1684 * TX_PWR_CFG_4: unknown */
1685 txpower = rt2x00_get_field16(eeprom,
1686 EEPROM_TXPOWER_BYRATE_RATE2);
1687 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2,
1688 min(txpower, max_value));
1689
1690 /* TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
1691 * TX_PWR_CFG_2: MCS7, TX_PWR_CFG_3: MCS15,
1692 * TX_PWR_CFG_4: unknown */
1693 txpower = rt2x00_get_field16(eeprom,
1694 EEPROM_TXPOWER_BYRATE_RATE3);
1695 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3,
1696 min(txpower, max_value));
1697
1698 /* read the next four txpower values */
1699 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i + 1,
1700 &eeprom);
1701
1702 /* TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
1703 * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
1704 * TX_PWR_CFG_4: unknown */
1705 txpower = rt2x00_get_field16(eeprom,
1706 EEPROM_TXPOWER_BYRATE_RATE0);
1707 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4,
1708 min(txpower, max_value));
1709
1710 /* TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
1711 * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
1712 * TX_PWR_CFG_4: unknown */
1713 txpower = rt2x00_get_field16(eeprom,
1714 EEPROM_TXPOWER_BYRATE_RATE1);
1715 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5,
1716 min(txpower, max_value));
1717
1718 /* TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
1719 * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
1720 * TX_PWR_CFG_4: unknown */
1721 txpower = rt2x00_get_field16(eeprom,
1722 EEPROM_TXPOWER_BYRATE_RATE2);
1723 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6,
1724 min(txpower, max_value));
1725
1726 /* TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
1727 * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
1728 * TX_PWR_CFG_4: unknown */
1729 txpower = rt2x00_get_field16(eeprom,
1730 EEPROM_TXPOWER_BYRATE_RATE3);
1731 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7,
1732 min(txpower, max_value));
1733
1734 rt2800_register_write(rt2x00dev, offset, reg);
1735
1736 /* next TX_PWR_CFG register */
1737 offset += 4;
1738 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001739}
1740
1741static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
1742 struct rt2x00lib_conf *libconf)
1743{
1744 u32 reg;
1745
1746 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1747 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
1748 libconf->conf->short_frame_max_tx_count);
1749 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
1750 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001751 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
1752}
1753
1754static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
1755 struct rt2x00lib_conf *libconf)
1756{
1757 enum dev_state state =
1758 (libconf->conf->flags & IEEE80211_CONF_PS) ?
1759 STATE_SLEEP : STATE_AWAKE;
1760 u32 reg;
1761
1762 if (state == STATE_SLEEP) {
1763 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
1764
1765 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1766 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
1767 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
1768 libconf->conf->listen_interval - 1);
1769 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
1770 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
1771
1772 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
1773 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001774 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1775 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
1776 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
1777 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
1778 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02001779
1780 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001781 }
1782}
1783
1784void rt2800_config(struct rt2x00_dev *rt2x00dev,
1785 struct rt2x00lib_conf *libconf,
1786 const unsigned int flags)
1787{
1788 /* Always recalculate LNA gain before changing configuration */
1789 rt2800_config_lna_gain(rt2x00dev, libconf);
1790
1791 if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
1792 rt2800_config_channel(rt2x00dev, libconf->conf,
1793 &libconf->rf, &libconf->channel);
1794 if (flags & IEEE80211_CONF_CHANGE_POWER)
1795 rt2800_config_txpower(rt2x00dev, libconf->conf->power_level);
1796 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
1797 rt2800_config_retry_limit(rt2x00dev, libconf);
1798 if (flags & IEEE80211_CONF_CHANGE_PS)
1799 rt2800_config_ps(rt2x00dev, libconf);
1800}
1801EXPORT_SYMBOL_GPL(rt2800_config);
1802
1803/*
1804 * Link tuning
1805 */
1806void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1807{
1808 u32 reg;
1809
1810 /*
1811 * Update FCS error count from register.
1812 */
1813 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1814 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
1815}
1816EXPORT_SYMBOL_GPL(rt2800_link_stats);
1817
1818static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
1819{
1820 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001821 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001822 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001823 rt2x00_rt(rt2x00dev, RT3090) ||
1824 rt2x00_rt(rt2x00dev, RT3390))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001825 return 0x1c + (2 * rt2x00dev->lna_gain);
1826 else
1827 return 0x2e + rt2x00dev->lna_gain;
1828 }
1829
1830 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
1831 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
1832 else
1833 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
1834}
1835
1836static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
1837 struct link_qual *qual, u8 vgc_level)
1838{
1839 if (qual->vgc_level != vgc_level) {
1840 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
1841 qual->vgc_level = vgc_level;
1842 qual->vgc_level_reg = vgc_level;
1843 }
1844}
1845
1846void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1847{
1848 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
1849}
1850EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
1851
1852void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
1853 const u32 count)
1854{
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001855 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001856 return;
1857
1858 /*
1859 * When RSSI is better then -80 increase VGC level with 0x10
1860 */
1861 rt2800_set_vgc(rt2x00dev, qual,
1862 rt2800_get_default_vgc(rt2x00dev) +
1863 ((qual->rssi > -80) * 0x10));
1864}
1865EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001866
1867/*
1868 * Initialization functions.
1869 */
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02001870static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001871{
1872 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001873 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001874 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001875 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001876
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001877 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1878 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1879 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1880 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1881 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1882 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
1883 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1884
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02001885 ret = rt2800_drv_init_registers(rt2x00dev);
1886 if (ret)
1887 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001888
1889 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
1890 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
1891 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
1892 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
1893 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
1894 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
1895
1896 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
1897 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
1898 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
1899 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
1900 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
1901 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
1902
1903 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
1904 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1905
1906 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1907
1908 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Helmut Schaa8544df32010-07-11 12:29:49 +02001909 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001910 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
1911 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
1912 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
1913 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1914 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
1915 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1916
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001917 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
1918
1919 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1920 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
1921 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
1922 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
1923
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001924 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001925 rt2x00_rt(rt2x00dev, RT3090) ||
1926 rt2x00_rt(rt2x00dev, RT3390)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001927 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1928 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02001929 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02001930 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
1931 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001932 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1933 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
1934 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1935 0x0000002c);
1936 else
1937 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
1938 0x0000000f);
1939 } else {
1940 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1941 }
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02001942 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001943 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02001944
1945 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
1946 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1947 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
1948 } else {
1949 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1950 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1951 }
Helmut Schaac295a812010-06-03 10:52:13 +02001952 } else if (rt2800_is_305x_soc(rt2x00dev)) {
1953 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1954 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1955 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000001f);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001956 } else {
1957 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
1958 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1959 }
1960
1961 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
1962 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
1963 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
1964 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
1965 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
1966 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
1967 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
1968 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
1969 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
1970 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
1971
1972 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
1973 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001974 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001975 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
1976 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
1977
1978 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
1979 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001980 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01001981 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001982 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001983 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
1984 else
1985 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
1986 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
1987 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
1988 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
1989
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02001990 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
1991 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
1992 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
1993 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
1994 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
1995 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
1996 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
1997 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
1998 rt2800_register_write(rt2x00dev, LED_CFG, reg);
1999
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002000 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
2001
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002002 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
2003 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
2004 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
2005 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
2006 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
2007 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
2008 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
2009 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
2010
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002011 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
2012 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002013 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002014 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
2015 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002016 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002017 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
2018 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
2019 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
2020
2021 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002022 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002023 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
2024 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
2025 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2026 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2027 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002028 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002029 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002030 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
2031 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002032 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
2033
2034 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002035 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002036 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
2037 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
2038 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2039 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2040 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002041 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002042 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002043 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
2044 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002045 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
2046
2047 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
2048 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
2049 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
2050 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
2051 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2052 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2053 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2054 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
2055 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2056 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002057 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002058 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
2059
2060 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
2061 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Helmut Schaad13a97f2010-10-02 11:29:08 +02002062 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002063 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
2064 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2065 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2066 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2067 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
2068 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2069 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002070 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002071 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
2072
2073 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
2074 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
2075 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
2076 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
2077 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2078 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2079 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2080 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
2081 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2082 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002083 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002084 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
2085
2086 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
2087 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
2088 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
2089 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
2090 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2091 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2092 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2093 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
2094 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2095 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002096 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002097 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
2098
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01002099 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002100 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
2101
2102 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
2103 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
2104 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
2105 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
2106 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
2107 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
2108 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
2109 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
2110 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
2111 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
2112 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
2113 }
2114
Helmut Schaa961621a2010-11-04 20:36:59 +01002115 /*
2116 * The legacy driver also sets TXOP_CTRL_CFG_RESERVED_TRUN_EN to 1
2117 * although it is reserved.
2118 */
2119 rt2800_register_read(rt2x00dev, TXOP_CTRL_CFG, &reg);
2120 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TIMEOUT_TRUN_EN, 1);
2121 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_AC_TRUN_EN, 1);
2122 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TXRATEGRP_TRUN_EN, 1);
2123 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_USER_MODE_TRUN_EN, 1);
2124 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_MIMO_PS_TRUN_EN, 1);
2125 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_RESERVED_TRUN_EN, 1);
2126 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_LSIG_TXOP_EN, 0);
2127 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_EN, 0);
2128 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_DLY, 88);
2129 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CWMIN, 0);
2130 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, reg);
2131
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002132 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
2133
2134 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
2135 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
2136 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
2137 IEEE80211_MAX_RTS_THRESHOLD);
2138 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
2139 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
2140
2141 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002142
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02002143 /*
2144 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
2145 * time should be set to 16. However, the original Ralink driver uses
2146 * 16 for both and indeed using a value of 10 for CCK SIFS results in
2147 * connection problems with 11g + CTS protection. Hence, use the same
2148 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
2149 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002150 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02002151 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
2152 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002153 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
2154 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
2155 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
2156 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
2157
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002158 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
2159
2160 /*
2161 * ASIC will keep garbage value after boot, clear encryption keys.
2162 */
2163 for (i = 0; i < 4; i++)
2164 rt2800_register_write(rt2x00dev,
2165 SHARED_KEY_MODE_ENTRY(i), 0);
2166
2167 for (i = 0; i < 256; i++) {
2168 u32 wcid[2] = { 0xffffffff, 0x00ffffff };
2169 rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
2170 wcid, sizeof(wcid));
2171
2172 rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
2173 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
2174 }
2175
2176 /*
2177 * Clear all beacons
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002178 */
Helmut Schaafdb87252010-06-29 21:48:06 +02002179 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE0);
2180 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE1);
2181 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE2);
2182 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE3);
2183 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE4);
2184 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE5);
2185 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE6);
2186 rt2800_clear_beacon(rt2x00dev, HW_BEACON_BASE7);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002187
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01002188 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02002189 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
2190 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
2191 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002192 }
2193
2194 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
2195 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
2196 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
2197 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
2198 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
2199 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
2200 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
2201 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
2202 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
2203 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
2204
2205 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
2206 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
2207 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
2208 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
2209 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
2210 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
2211 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
2212 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
2213 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
2214 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
2215
2216 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
2217 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
2218 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
2219 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
2220 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
2221 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
2222 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
2223 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
2224 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
2225 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
2226
2227 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
2228 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
2229 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
2230 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
2231 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
2232 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
2233
2234 /*
Helmut Schaa47ee3eb2010-09-08 20:56:04 +02002235 * Do not force the BA window size, we use the TXWI to set it
2236 */
2237 rt2800_register_read(rt2x00dev, AMPDU_BA_WINSIZE, &reg);
2238 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE_ENABLE, 0);
2239 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE, 0);
2240 rt2800_register_write(rt2x00dev, AMPDU_BA_WINSIZE, reg);
2241
2242 /*
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002243 * We must clear the error counters.
2244 * These registers are cleared on read,
2245 * so we may pass a useless variable to store the value.
2246 */
2247 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
2248 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
2249 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
2250 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
2251 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
2252 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
2253
Helmut Schaa9f926fb2010-07-11 12:28:23 +02002254 /*
2255 * Setup leadtime for pre tbtt interrupt to 6ms
2256 */
2257 rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
2258 rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
2259 rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
2260
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002261 return 0;
2262}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002263
2264static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
2265{
2266 unsigned int i;
2267 u32 reg;
2268
2269 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
2270 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
2271 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
2272 return 0;
2273
2274 udelay(REGISTER_BUSY_DELAY);
2275 }
2276
2277 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
2278 return -EACCES;
2279}
2280
2281static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
2282{
2283 unsigned int i;
2284 u8 value;
2285
2286 /*
2287 * BBP was enabled after firmware was loaded,
2288 * but we need to reactivate it now.
2289 */
2290 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
2291 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
2292 msleep(1);
2293
2294 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
2295 rt2800_bbp_read(rt2x00dev, 0, &value);
2296 if ((value != 0xff) && (value != 0x00))
2297 return 0;
2298 udelay(REGISTER_BUSY_DELAY);
2299 }
2300
2301 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
2302 return -EACCES;
2303}
2304
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002305static int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002306{
2307 unsigned int i;
2308 u16 eeprom;
2309 u8 reg_id;
2310 u8 value;
2311
2312 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
2313 rt2800_wait_bbp_ready(rt2x00dev)))
2314 return -EACCES;
2315
Helmut Schaabaff8002010-04-28 09:58:59 +02002316 if (rt2800_is_305x_soc(rt2x00dev))
2317 rt2800_bbp_write(rt2x00dev, 31, 0x08);
2318
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002319 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
2320 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002321
2322 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
2323 rt2800_bbp_write(rt2x00dev, 69, 0x16);
2324 rt2800_bbp_write(rt2x00dev, 73, 0x12);
2325 } else {
2326 rt2800_bbp_write(rt2x00dev, 69, 0x12);
2327 rt2800_bbp_write(rt2x00dev, 73, 0x10);
2328 }
2329
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002330 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002331
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002332 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002333 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002334 rt2x00_rt(rt2x00dev, RT3090) ||
2335 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002336 rt2800_bbp_write(rt2x00dev, 79, 0x13);
2337 rt2800_bbp_write(rt2x00dev, 80, 0x05);
2338 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Helmut Schaabaff8002010-04-28 09:58:59 +02002339 } else if (rt2800_is_305x_soc(rt2x00dev)) {
2340 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
2341 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002342 } else {
2343 rt2800_bbp_write(rt2x00dev, 81, 0x37);
2344 }
2345
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002346 rt2800_bbp_write(rt2x00dev, 82, 0x62);
2347 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002348
Gertjan van Wingerde5ed8f452010-06-03 10:51:57 +02002349 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002350 rt2800_bbp_write(rt2x00dev, 84, 0x19);
2351 else
2352 rt2800_bbp_write(rt2x00dev, 84, 0x99);
2353
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002354 rt2800_bbp_write(rt2x00dev, 86, 0x00);
2355 rt2800_bbp_write(rt2x00dev, 91, 0x04);
2356 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002357
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002358 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002359 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002360 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02002361 rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
2362 rt2800_is_305x_soc(rt2x00dev))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002363 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
2364 else
2365 rt2800_bbp_write(rt2x00dev, 103, 0x00);
2366
Helmut Schaabaff8002010-04-28 09:58:59 +02002367 if (rt2800_is_305x_soc(rt2x00dev))
2368 rt2800_bbp_write(rt2x00dev, 105, 0x01);
2369 else
2370 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002371 rt2800_bbp_write(rt2x00dev, 106, 0x35);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002372
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002373 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002374 rt2x00_rt(rt2x00dev, RT3090) ||
2375 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002376 rt2800_bbp_read(rt2x00dev, 138, &value);
2377
2378 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2379 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
2380 value |= 0x20;
2381 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
2382 value &= ~0x02;
2383
2384 rt2800_bbp_write(rt2x00dev, 138, value);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002385 }
2386
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002387
2388 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
2389 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
2390
2391 if (eeprom != 0xffff && eeprom != 0x0000) {
2392 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
2393 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
2394 rt2800_bbp_write(rt2x00dev, reg_id, value);
2395 }
2396 }
2397
2398 return 0;
2399}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002400
2401static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
2402 bool bw40, u8 rfcsr24, u8 filter_target)
2403{
2404 unsigned int i;
2405 u8 bbp;
2406 u8 rfcsr;
2407 u8 passband;
2408 u8 stopband;
2409 u8 overtuned = 0;
2410
2411 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
2412
2413 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2414 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
2415 rt2800_bbp_write(rt2x00dev, 4, bbp);
2416
2417 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
2418 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
2419 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
2420
2421 /*
2422 * Set power & frequency of passband test tone
2423 */
2424 rt2800_bbp_write(rt2x00dev, 24, 0);
2425
2426 for (i = 0; i < 100; i++) {
2427 rt2800_bbp_write(rt2x00dev, 25, 0x90);
2428 msleep(1);
2429
2430 rt2800_bbp_read(rt2x00dev, 55, &passband);
2431 if (passband)
2432 break;
2433 }
2434
2435 /*
2436 * Set power & frequency of stopband test tone
2437 */
2438 rt2800_bbp_write(rt2x00dev, 24, 0x06);
2439
2440 for (i = 0; i < 100; i++) {
2441 rt2800_bbp_write(rt2x00dev, 25, 0x90);
2442 msleep(1);
2443
2444 rt2800_bbp_read(rt2x00dev, 55, &stopband);
2445
2446 if ((passband - stopband) <= filter_target) {
2447 rfcsr24++;
2448 overtuned += ((passband - stopband) == filter_target);
2449 } else
2450 break;
2451
2452 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
2453 }
2454
2455 rfcsr24 -= !!overtuned;
2456
2457 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
2458 return rfcsr24;
2459}
2460
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002461static int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002462{
2463 u8 rfcsr;
2464 u8 bbp;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002465 u32 reg;
2466 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002467
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002468 if (!rt2x00_rt(rt2x00dev, RT3070) &&
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002469 !rt2x00_rt(rt2x00dev, RT3071) &&
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002470 !rt2x00_rt(rt2x00dev, RT3090) &&
Helmut Schaa23812382010-04-26 13:48:45 +02002471 !rt2x00_rt(rt2x00dev, RT3390) &&
Helmut Schaabaff8002010-04-28 09:58:59 +02002472 !rt2800_is_305x_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002473 return 0;
2474
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002475 /*
2476 * Init RF calibration.
2477 */
2478 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
2479 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
2480 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2481 msleep(1);
2482 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
2483 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
2484
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002485 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002486 rt2x00_rt(rt2x00dev, RT3071) ||
2487 rt2x00_rt(rt2x00dev, RT3090)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002488 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
2489 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
2490 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
2491 rt2800_rfcsr_write(rt2x00dev, 7, 0x70);
2492 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002493 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002494 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
2495 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
2496 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
2497 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
2498 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
2499 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
2500 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
2501 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
2502 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
2503 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
2504 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
2505 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002506 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002507 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
2508 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
2509 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
2510 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
2511 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002512 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002513 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
2514 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
2515 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
2516 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
2517 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
2518 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002519 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002520 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
2521 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002522 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002523 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
2524 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
2525 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
2526 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
2527 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
2528 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
2529 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002530 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002531 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002532 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002533 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
2534 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
2535 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
2536 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
2537 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
2538 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
2539 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Helmut Schaabaff8002010-04-28 09:58:59 +02002540 } else if (rt2800_is_305x_soc(rt2x00dev)) {
Helmut Schaa23812382010-04-26 13:48:45 +02002541 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
2542 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
2543 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
2544 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
2545 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
2546 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
2547 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
2548 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
2549 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
2550 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
2551 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
2552 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
2553 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
2554 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
2555 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
2556 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
2557 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
2558 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
2559 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
2560 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
2561 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
2562 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
2563 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
2564 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
2565 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
2566 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
2567 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
2568 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
2569 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
2570 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
Helmut Schaabaff8002010-04-28 09:58:59 +02002571 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
2572 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
2573 return 0;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002574 }
2575
2576 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
2577 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
2578 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
2579 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
2580 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002581 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
2582 rt2x00_rt(rt2x00dev, RT3090)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002583 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
2584 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
2585 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
2586
2587 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
2588
2589 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
2590 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002591 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
2592 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002593 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2594 if (rt2x00_get_field16(eeprom, EEPROM_NIC_DAC_TEST))
2595 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
2596 else
2597 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
2598 }
2599 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002600 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
2601 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
2602 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
2603 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002604 }
2605
2606 /*
2607 * Set RX Filter calibration for 20MHz and 40MHz
2608 */
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002609 if (rt2x00_rt(rt2x00dev, RT3070)) {
2610 rt2x00dev->calibration[0] =
2611 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
2612 rt2x00dev->calibration[1] =
2613 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002614 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002615 rt2x00_rt(rt2x00dev, RT3090) ||
2616 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002617 rt2x00dev->calibration[0] =
2618 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
2619 rt2x00dev->calibration[1] =
2620 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002621 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002622
2623 /*
2624 * Set back to initial state
2625 */
2626 rt2800_bbp_write(rt2x00dev, 24, 0);
2627
2628 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
2629 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
2630 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
2631
2632 /*
2633 * set BBP back to BW20
2634 */
2635 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2636 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
2637 rt2800_bbp_write(rt2x00dev, 4, bbp);
2638
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002639 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002640 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002641 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2642 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002643 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
2644
2645 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
2646 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
2647 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
2648
2649 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
2650 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002651 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002652 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2653 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Gertjan van Wingerde8440c292010-06-03 10:52:02 +02002654 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002655 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
2656 }
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002657 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &eeprom);
2658 if (rt2x00_get_field16(eeprom, EEPROM_TXMIXER_GAIN_BG_VAL) >= 1)
2659 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
2660 rt2x00_get_field16(eeprom,
2661 EEPROM_TXMIXER_GAIN_BG_VAL));
2662 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
2663
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002664 if (rt2x00_rt(rt2x00dev, RT3090)) {
2665 rt2800_bbp_read(rt2x00dev, 138, &bbp);
2666
2667 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2668 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1)
2669 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
2670 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
2671 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
2672
2673 rt2800_bbp_write(rt2x00dev, 138, bbp);
2674 }
2675
2676 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002677 rt2x00_rt(rt2x00dev, RT3090) ||
2678 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002679 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
2680 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
2681 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
2682 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
2683 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
2684 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
2685 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
2686
2687 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
2688 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
2689 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
2690
2691 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
2692 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
2693 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
2694
2695 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
2696 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
2697 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
2698 }
2699
2700 if (rt2x00_rt(rt2x00dev, RT3070) || rt2x00_rt(rt2x00dev, RT3071)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002701 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002702 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
2703 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002704 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
2705 else
2706 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
2707 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
2708 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
2709 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
2710 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
2711 }
2712
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002713 return 0;
2714}
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002715
2716int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
2717{
2718 u32 reg;
2719 u16 word;
2720
2721 /*
2722 * Initialize all registers.
2723 */
2724 if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
2725 rt2800_init_registers(rt2x00dev) ||
2726 rt2800_init_bbp(rt2x00dev) ||
2727 rt2800_init_rfcsr(rt2x00dev)))
2728 return -EIO;
2729
2730 /*
2731 * Send signal to firmware during boot time.
2732 */
2733 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
2734
2735 if (rt2x00_is_usb(rt2x00dev) &&
2736 (rt2x00_rt(rt2x00dev, RT3070) ||
2737 rt2x00_rt(rt2x00dev, RT3071) ||
2738 rt2x00_rt(rt2x00dev, RT3572))) {
2739 udelay(200);
2740 rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
2741 udelay(10);
2742 }
2743
2744 /*
2745 * Enable RX.
2746 */
2747 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
2748 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
2749 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
2750 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
2751
2752 udelay(50);
2753
2754 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
2755 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
2756 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
2757 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
2758 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
2759 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
2760
2761 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
2762 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
2763 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
2764 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
2765
2766 /*
2767 * Initialize LED control
2768 */
2769 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED1, &word);
2770 rt2800_mcu_request(rt2x00dev, MCU_LED_1, 0xff,
2771 word & 0xff, (word >> 8) & 0xff);
2772
2773 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED2, &word);
2774 rt2800_mcu_request(rt2x00dev, MCU_LED_2, 0xff,
2775 word & 0xff, (word >> 8) & 0xff);
2776
2777 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED3, &word);
2778 rt2800_mcu_request(rt2x00dev, MCU_LED_3, 0xff,
2779 word & 0xff, (word >> 8) & 0xff);
2780
2781 return 0;
2782}
2783EXPORT_SYMBOL_GPL(rt2800_enable_radio);
2784
2785void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
2786{
2787 u32 reg;
2788
2789 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
2790 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
2791 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
2792 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
2793 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
2794 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
2795 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
2796
2797 /* Wait for DMA, ignore error */
2798 rt2800_wait_wpdma_ready(rt2x00dev);
2799
2800 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
2801 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
2802 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
2803 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
2804
2805 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0);
2806 rt2800_register_write(rt2x00dev, TX_PIN_CFG, 0);
2807}
2808EXPORT_SYMBOL_GPL(rt2800_disable_radio);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002809
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002810int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
2811{
2812 u32 reg;
2813
2814 rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
2815
2816 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
2817}
2818EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
2819
2820static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
2821{
2822 u32 reg;
2823
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002824 mutex_lock(&rt2x00dev->csr_mutex);
2825
2826 rt2800_register_read_lock(rt2x00dev, EFUSE_CTRL, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002827 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
2828 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
2829 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002830 rt2800_register_write_lock(rt2x00dev, EFUSE_CTRL, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002831
2832 /* Wait until the EEPROM has been loaded */
2833 rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
2834
2835 /* Apparently the data is read from end to start */
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01002836 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA3,
2837 (u32 *)&rt2x00dev->eeprom[i]);
2838 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA2,
2839 (u32 *)&rt2x00dev->eeprom[i + 2]);
2840 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA1,
2841 (u32 *)&rt2x00dev->eeprom[i + 4]);
2842 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA0,
2843 (u32 *)&rt2x00dev->eeprom[i + 6]);
2844
2845 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01002846}
2847
2848void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
2849{
2850 unsigned int i;
2851
2852 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
2853 rt2800_efuse_read(rt2x00dev, i);
2854}
2855EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
2856
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002857int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
2858{
2859 u16 word;
2860 u8 *mac;
2861 u8 default_lna_gain;
2862
2863 /*
2864 * Start validation of the data that has been read.
2865 */
2866 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
2867 if (!is_valid_ether_addr(mac)) {
2868 random_ether_addr(mac);
2869 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
2870 }
2871
2872 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
2873 if (word == 0xffff) {
2874 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2875 rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
2876 rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
2877 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2878 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002879 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02002880 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002881 /*
2882 * There is a max of 2 RX streams for RT28x0 series
2883 */
2884 if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
2885 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2886 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2887 }
2888
2889 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
2890 if (word == 0xffff) {
2891 rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
2892 rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
2893 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
2894 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
2895 rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
2896 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
2897 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
2898 rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
2899 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
2900 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002901 rt2x00_set_field16(&word, EEPROM_NIC_ANT_DIVERSITY, 0);
2902 rt2x00_set_field16(&word, EEPROM_NIC_DAC_TEST, 0);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002903 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
2904 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
2905 }
2906
2907 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
2908 if ((word & 0x00ff) == 0x00ff) {
2909 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002910 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2911 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
2912 }
2913 if ((word & 0xff00) == 0xff00) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002914 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
2915 LED_MODE_TXRX_ACTIVITY);
2916 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
2917 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2918 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
2919 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
2920 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02002921 EEPROM(rt2x00dev, "Led Mode: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002922 }
2923
2924 /*
2925 * During the LNA validation we are going to use
2926 * lna0 as correct value. Note that EEPROM_LNA
2927 * is never validated.
2928 */
2929 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
2930 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
2931
2932 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
2933 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
2934 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
2935 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
2936 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
2937 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
2938
2939 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
2940 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
2941 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
2942 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
2943 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
2944 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
2945 default_lna_gain);
2946 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
2947
2948 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
2949 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
2950 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
2951 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
2952 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
2953 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
2954
2955 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
2956 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
2957 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
2958 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
2959 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
2960 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
2961 default_lna_gain);
2962 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
2963
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02002964 rt2x00_eeprom_read(rt2x00dev, EEPROM_MAX_TX_POWER, &word);
2965 if (rt2x00_get_field16(word, EEPROM_MAX_TX_POWER_24GHZ) == 0xff)
2966 rt2x00_set_field16(&word, EEPROM_MAX_TX_POWER_24GHZ, MAX_G_TXPOWER);
2967 if (rt2x00_get_field16(word, EEPROM_MAX_TX_POWER_5GHZ) == 0xff)
2968 rt2x00_set_field16(&word, EEPROM_MAX_TX_POWER_5GHZ, MAX_A_TXPOWER);
2969 rt2x00_eeprom_write(rt2x00dev, EEPROM_MAX_TX_POWER, word);
2970
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01002971 return 0;
2972}
2973EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
2974
2975int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
2976{
2977 u32 reg;
2978 u16 value;
2979 u16 eeprom;
2980
2981 /*
2982 * Read EEPROM word for configuration.
2983 */
2984 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2985
2986 /*
2987 * Identify RF chipset.
2988 */
2989 value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
2990 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
2991
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002992 rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
2993 value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
Gertjan van Wingerde714fa662010-02-13 20:55:48 +01002994
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002995 if (!rt2x00_rt(rt2x00dev, RT2860) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002996 !rt2x00_rt(rt2x00dev, RT2872) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002997 !rt2x00_rt(rt2x00dev, RT2883) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002998 !rt2x00_rt(rt2x00dev, RT3070) &&
2999 !rt2x00_rt(rt2x00dev, RT3071) &&
3000 !rt2x00_rt(rt2x00dev, RT3090) &&
3001 !rt2x00_rt(rt2x00dev, RT3390) &&
3002 !rt2x00_rt(rt2x00dev, RT3572)) {
3003 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
3004 return -ENODEV;
3005 }
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003006
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003007 if (!rt2x00_rf(rt2x00dev, RF2820) &&
3008 !rt2x00_rf(rt2x00dev, RF2850) &&
3009 !rt2x00_rf(rt2x00dev, RF2720) &&
3010 !rt2x00_rf(rt2x00dev, RF2750) &&
3011 !rt2x00_rf(rt2x00dev, RF3020) &&
3012 !rt2x00_rf(rt2x00dev, RF2020) &&
3013 !rt2x00_rf(rt2x00dev, RF3021) &&
Gertjan van Wingerde6c0fe262009-12-30 11:36:31 +01003014 !rt2x00_rf(rt2x00dev, RF3022) &&
3015 !rt2x00_rf(rt2x00dev, RF3052)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003016 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
3017 return -ENODEV;
3018 }
3019
3020 /*
3021 * Identify default antenna configuration.
3022 */
3023 rt2x00dev->default_ant.tx =
3024 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH);
3025 rt2x00dev->default_ant.rx =
3026 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH);
3027
3028 /*
3029 * Read frequency offset and RF programming sequence.
3030 */
3031 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
3032 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
3033
3034 /*
3035 * Read external LNA informations.
3036 */
3037 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
3038
3039 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
3040 __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
3041 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
3042 __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
3043
3044 /*
3045 * Detect if this device has an hardware controlled radio.
3046 */
3047 if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
3048 __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
3049
3050 /*
3051 * Store led settings, for correct led behaviour.
3052 */
3053#ifdef CONFIG_RT2X00_LIB_LEDS
3054 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
3055 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
3056 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
3057
3058 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &rt2x00dev->led_mcu_reg);
3059#endif /* CONFIG_RT2X00_LIB_LEDS */
3060
3061 return 0;
3062}
3063EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
3064
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003065/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02003066 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003067 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
3068 */
3069static const struct rf_channel rf_vals[] = {
3070 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
3071 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
3072 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
3073 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
3074 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
3075 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
3076 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
3077 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
3078 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
3079 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
3080 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
3081 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
3082 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
3083 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
3084
3085 /* 802.11 UNI / HyperLan 2 */
3086 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
3087 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
3088 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
3089 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
3090 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
3091 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
3092 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
3093 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
3094 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
3095 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
3096 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
3097 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
3098
3099 /* 802.11 HyperLan 2 */
3100 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
3101 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
3102 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
3103 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
3104 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
3105 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
3106 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
3107 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
3108 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
3109 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
3110 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
3111 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
3112 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
3113 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
3114 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
3115 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
3116
3117 /* 802.11 UNII */
3118 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
3119 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
3120 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
3121 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
3122 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
3123 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
3124 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
3125 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
3126 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
3127 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
3128 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
3129
3130 /* 802.11 Japan */
3131 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
3132 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
3133 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
3134 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
3135 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
3136 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
3137 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
3138};
3139
3140/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02003141 * RF value list for rt3xxx
3142 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003143 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02003144static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003145 {1, 241, 2, 2 },
3146 {2, 241, 2, 7 },
3147 {3, 242, 2, 2 },
3148 {4, 242, 2, 7 },
3149 {5, 243, 2, 2 },
3150 {6, 243, 2, 7 },
3151 {7, 244, 2, 2 },
3152 {8, 244, 2, 7 },
3153 {9, 245, 2, 2 },
3154 {10, 245, 2, 7 },
3155 {11, 246, 2, 2 },
3156 {12, 246, 2, 7 },
3157 {13, 247, 2, 2 },
3158 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02003159
3160 /* 802.11 UNI / HyperLan 2 */
3161 {36, 0x56, 0, 4},
3162 {38, 0x56, 0, 6},
3163 {40, 0x56, 0, 8},
3164 {44, 0x57, 0, 0},
3165 {46, 0x57, 0, 2},
3166 {48, 0x57, 0, 4},
3167 {52, 0x57, 0, 8},
3168 {54, 0x57, 0, 10},
3169 {56, 0x58, 0, 0},
3170 {60, 0x58, 0, 4},
3171 {62, 0x58, 0, 6},
3172 {64, 0x58, 0, 8},
3173
3174 /* 802.11 HyperLan 2 */
3175 {100, 0x5b, 0, 8},
3176 {102, 0x5b, 0, 10},
3177 {104, 0x5c, 0, 0},
3178 {108, 0x5c, 0, 4},
3179 {110, 0x5c, 0, 6},
3180 {112, 0x5c, 0, 8},
3181 {116, 0x5d, 0, 0},
3182 {118, 0x5d, 0, 2},
3183 {120, 0x5d, 0, 4},
3184 {124, 0x5d, 0, 8},
3185 {126, 0x5d, 0, 10},
3186 {128, 0x5e, 0, 0},
3187 {132, 0x5e, 0, 4},
3188 {134, 0x5e, 0, 6},
3189 {136, 0x5e, 0, 8},
3190 {140, 0x5f, 0, 0},
3191
3192 /* 802.11 UNII */
3193 {149, 0x5f, 0, 9},
3194 {151, 0x5f, 0, 11},
3195 {153, 0x60, 0, 1},
3196 {157, 0x60, 0, 5},
3197 {159, 0x60, 0, 7},
3198 {161, 0x60, 0, 9},
3199 {165, 0x61, 0, 1},
3200 {167, 0x61, 0, 3},
3201 {169, 0x61, 0, 5},
3202 {171, 0x61, 0, 7},
3203 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003204};
3205
3206int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
3207{
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003208 struct hw_mode_spec *spec = &rt2x00dev->spec;
3209 struct channel_info *info;
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02003210 char *default_power1;
3211 char *default_power2;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003212 unsigned int i;
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02003213 unsigned short max_power;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003214 u16 eeprom;
3215
3216 /*
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01003217 * Disable powersaving as default on PCI devices.
3218 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01003219 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01003220 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
3221
3222 /*
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003223 * Initialize all hw fields.
3224 */
3225 rt2x00dev->hw->flags =
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003226 IEEE80211_HW_SIGNAL_DBM |
3227 IEEE80211_HW_SUPPORTS_PS |
Helmut Schaa1df90802010-06-29 21:38:12 +02003228 IEEE80211_HW_PS_NULLFUNC_STACK |
3229 IEEE80211_HW_AMPDU_AGGREGATION;
Helmut Schaa5a5b6ed2010-10-02 11:31:33 +02003230 /*
3231 * Don't set IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING for USB devices
3232 * unless we are capable of sending the buffered frames out after the
3233 * DTIM transmission using rt2x00lib_beacondone. This will send out
3234 * multicast and broadcast traffic immediately instead of buffering it
3235 * infinitly and thus dropping it after some time.
3236 */
3237 if (!rt2x00_is_usb(rt2x00dev))
3238 rt2x00dev->hw->flags |=
3239 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003240
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003241 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
3242 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
3243 rt2x00_eeprom_addr(rt2x00dev,
3244 EEPROM_MAC_ADDR_0));
3245
Helmut Schaa3f2bee22010-06-14 22:12:01 +02003246 /*
3247 * As rt2800 has a global fallback table we cannot specify
3248 * more then one tx rate per frame but since the hw will
3249 * try several rates (based on the fallback table) we should
Helmut Schaaba3b9e52010-10-02 11:32:16 +02003250 * initialize max_report_rates to the maximum number of rates
Helmut Schaa3f2bee22010-06-14 22:12:01 +02003251 * we are going to try. Otherwise mac80211 will truncate our
3252 * reported tx rates and the rc algortihm will end up with
3253 * incorrect data.
3254 */
Helmut Schaaba3b9e52010-10-02 11:32:16 +02003255 rt2x00dev->hw->max_rates = 1;
3256 rt2x00dev->hw->max_report_rates = 7;
Helmut Schaa3f2bee22010-06-14 22:12:01 +02003257 rt2x00dev->hw->max_rate_tries = 1;
3258
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003259 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
3260
3261 /*
3262 * Initialize hw_mode information.
3263 */
3264 spec->supported_bands = SUPPORT_BAND_2GHZ;
3265 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
3266
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003267 if (rt2x00_rf(rt2x00dev, RF2820) ||
Ivo van Doorn55f93212010-05-06 14:45:46 +02003268 rt2x00_rf(rt2x00dev, RF2720)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003269 spec->num_channels = 14;
3270 spec->channels = rf_vals;
Ivo van Doorn55f93212010-05-06 14:45:46 +02003271 } else if (rt2x00_rf(rt2x00dev, RF2850) ||
3272 rt2x00_rf(rt2x00dev, RF2750)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003273 spec->supported_bands |= SUPPORT_BAND_5GHZ;
3274 spec->num_channels = ARRAY_SIZE(rf_vals);
3275 spec->channels = rf_vals;
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003276 } else if (rt2x00_rf(rt2x00dev, RF3020) ||
3277 rt2x00_rf(rt2x00dev, RF2020) ||
3278 rt2x00_rf(rt2x00dev, RF3021) ||
3279 rt2x00_rf(rt2x00dev, RF3022)) {
Ivo van Doorn55f93212010-05-06 14:45:46 +02003280 spec->num_channels = 14;
3281 spec->channels = rf_vals_3x;
3282 } else if (rt2x00_rf(rt2x00dev, RF3052)) {
3283 spec->supported_bands |= SUPPORT_BAND_5GHZ;
3284 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
3285 spec->channels = rf_vals_3x;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003286 }
3287
3288 /*
3289 * Initialize HT information.
3290 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003291 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01003292 spec->ht.ht_supported = true;
3293 else
3294 spec->ht.ht_supported = false;
3295
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003296 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02003297 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003298 IEEE80211_HT_CAP_GRN_FLD |
3299 IEEE80211_HT_CAP_SGI_20 |
Ivo van Doornaa674632010-06-29 21:48:37 +02003300 IEEE80211_HT_CAP_SGI_40;
Helmut Schaa22cabaa2010-06-03 10:52:10 +02003301
3302 if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) >= 2)
3303 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
3304
Ivo van Doornaa674632010-06-29 21:48:37 +02003305 spec->ht.cap |=
3306 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) <<
3307 IEEE80211_HT_CAP_RX_STBC_SHIFT;
3308
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003309 spec->ht.ampdu_factor = 3;
3310 spec->ht.ampdu_density = 4;
3311 spec->ht.mcs.tx_params =
3312 IEEE80211_HT_MCS_TX_DEFINED |
3313 IEEE80211_HT_MCS_TX_RX_DIFF |
3314 ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
3315 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
3316
3317 switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
3318 case 3:
3319 spec->ht.mcs.rx_mask[2] = 0xff;
3320 case 2:
3321 spec->ht.mcs.rx_mask[1] = 0xff;
3322 case 1:
3323 spec->ht.mcs.rx_mask[0] = 0xff;
3324 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
3325 break;
3326 }
3327
3328 /*
3329 * Create channel information array
3330 */
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00003331 info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003332 if (!info)
3333 return -ENOMEM;
3334
3335 spec->channels_info = info;
3336
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02003337 rt2x00_eeprom_read(rt2x00dev, EEPROM_MAX_TX_POWER, &eeprom);
3338 max_power = rt2x00_get_field16(eeprom, EEPROM_MAX_TX_POWER_24GHZ);
3339 default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
3340 default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003341
3342 for (i = 0; i < 14; i++) {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02003343 info[i].max_power = max_power;
3344 info[i].default_power1 = TXPOWER_G_FROM_DEV(default_power1[i]);
3345 info[i].default_power2 = TXPOWER_G_FROM_DEV(default_power2[i]);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003346 }
3347
3348 if (spec->num_channels > 14) {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02003349 max_power = rt2x00_get_field16(eeprom, EEPROM_MAX_TX_POWER_5GHZ);
3350 default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
3351 default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003352
3353 for (i = 14; i < spec->num_channels; i++) {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02003354 info[i].max_power = max_power;
3355 info[i].default_power1 = TXPOWER_A_FROM_DEV(default_power1[i]);
3356 info[i].default_power2 = TXPOWER_A_FROM_DEV(default_power2[i]);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003357 }
3358 }
3359
3360 return 0;
3361}
3362EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
3363
3364/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003365 * IEEE80211 stack callback functions.
3366 */
Helmut Schaae7836192010-07-11 12:28:54 +02003367void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx, u32 *iv32,
3368 u16 *iv16)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003369{
3370 struct rt2x00_dev *rt2x00dev = hw->priv;
3371 struct mac_iveiv_entry iveiv_entry;
3372 u32 offset;
3373
3374 offset = MAC_IVEIV_ENTRY(hw_key_idx);
3375 rt2800_register_multiread(rt2x00dev, offset,
3376 &iveiv_entry, sizeof(iveiv_entry));
3377
Julia Lawall855da5e2009-12-13 17:07:45 +01003378 memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
3379 memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003380}
Helmut Schaae7836192010-07-11 12:28:54 +02003381EXPORT_SYMBOL_GPL(rt2800_get_tkip_seq);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003382
Helmut Schaae7836192010-07-11 12:28:54 +02003383int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003384{
3385 struct rt2x00_dev *rt2x00dev = hw->priv;
3386 u32 reg;
3387 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
3388
3389 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
3390 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
3391 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
3392
3393 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
3394 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
3395 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
3396
3397 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
3398 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
3399 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
3400
3401 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
3402 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
3403 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
3404
3405 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
3406 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
3407 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
3408
3409 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
3410 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
3411 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
3412
3413 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
3414 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
3415 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
3416
3417 return 0;
3418}
Helmut Schaae7836192010-07-11 12:28:54 +02003419EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003420
Helmut Schaae7836192010-07-11 12:28:54 +02003421int rt2800_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
3422 const struct ieee80211_tx_queue_params *params)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003423{
3424 struct rt2x00_dev *rt2x00dev = hw->priv;
3425 struct data_queue *queue;
3426 struct rt2x00_field32 field;
3427 int retval;
3428 u32 reg;
3429 u32 offset;
3430
3431 /*
3432 * First pass the configuration through rt2x00lib, that will
3433 * update the queue settings and validate the input. After that
3434 * we are free to update the registers based on the value
3435 * in the queue parameter.
3436 */
3437 retval = rt2x00mac_conf_tx(hw, queue_idx, params);
3438 if (retval)
3439 return retval;
3440
3441 /*
3442 * We only need to perform additional register initialization
3443 * for WMM queues/
3444 */
3445 if (queue_idx >= 4)
3446 return 0;
3447
3448 queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
3449
3450 /* Update WMM TXOP register */
3451 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
3452 field.bit_offset = (queue_idx & 1) * 16;
3453 field.bit_mask = 0xffff << field.bit_offset;
3454
3455 rt2800_register_read(rt2x00dev, offset, &reg);
3456 rt2x00_set_field32(&reg, field, queue->txop);
3457 rt2800_register_write(rt2x00dev, offset, reg);
3458
3459 /* Update WMM registers */
3460 field.bit_offset = queue_idx * 4;
3461 field.bit_mask = 0xf << field.bit_offset;
3462
3463 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
3464 rt2x00_set_field32(&reg, field, queue->aifs);
3465 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
3466
3467 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
3468 rt2x00_set_field32(&reg, field, queue->cw_min);
3469 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
3470
3471 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
3472 rt2x00_set_field32(&reg, field, queue->cw_max);
3473 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
3474
3475 /* Update EDCA registers */
3476 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
3477
3478 rt2800_register_read(rt2x00dev, offset, &reg);
3479 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
3480 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
3481 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
3482 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
3483 rt2800_register_write(rt2x00dev, offset, reg);
3484
3485 return 0;
3486}
Helmut Schaae7836192010-07-11 12:28:54 +02003487EXPORT_SYMBOL_GPL(rt2800_conf_tx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003488
Helmut Schaae7836192010-07-11 12:28:54 +02003489u64 rt2800_get_tsf(struct ieee80211_hw *hw)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003490{
3491 struct rt2x00_dev *rt2x00dev = hw->priv;
3492 u64 tsf;
3493 u32 reg;
3494
3495 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
3496 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
3497 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
3498 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
3499
3500 return tsf;
3501}
Helmut Schaae7836192010-07-11 12:28:54 +02003502EXPORT_SYMBOL_GPL(rt2800_get_tsf);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003503
Helmut Schaae7836192010-07-11 12:28:54 +02003504int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3505 enum ieee80211_ampdu_mlme_action action,
3506 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
Helmut Schaa1df90802010-06-29 21:38:12 +02003507{
Helmut Schaa1df90802010-06-29 21:38:12 +02003508 int ret = 0;
3509
3510 switch (action) {
3511 case IEEE80211_AMPDU_RX_START:
3512 case IEEE80211_AMPDU_RX_STOP:
Helmut Schaa58ed8262010-10-02 11:33:17 +02003513 /*
3514 * The hw itself takes care of setting up BlockAck mechanisms.
3515 * So, we only have to allow mac80211 to nagotiate a BlockAck
3516 * agreement. Once that is done, the hw will BlockAck incoming
3517 * AMPDUs without further setup.
3518 */
Helmut Schaa1df90802010-06-29 21:38:12 +02003519 break;
3520 case IEEE80211_AMPDU_TX_START:
3521 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3522 break;
3523 case IEEE80211_AMPDU_TX_STOP:
3524 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3525 break;
3526 case IEEE80211_AMPDU_TX_OPERATIONAL:
3527 break;
3528 default:
Ivo van Doorn4e9e58c2010-06-29 21:49:50 +02003529 WARNING((struct rt2x00_dev *)hw->priv, "Unknown AMPDU action\n");
Helmut Schaa1df90802010-06-29 21:38:12 +02003530 }
3531
3532 return ret;
3533}
Helmut Schaae7836192010-07-11 12:28:54 +02003534EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02003535
3536MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
3537MODULE_VERSION(DRV_VERSION);
3538MODULE_DESCRIPTION("Ralink RT2800 library");
3539MODULE_LICENSE("GPL");