blob: d88857ccdf851f71273ff062bd07338b5009c522 [file] [log] [blame]
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +01001/*
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01002 Copyright (C) 2009 Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
Gertjan van Wingerdecce5fc42009-11-10 22:42:40 +01003 Copyright (C) 2009 Gertjan van Wingerde <gwingerde@gmail.com>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +01004
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01005 Based on the original rt2800pci.c and rt2800usb.c.
6 Copyright (C) 2009 Ivo van Doorn <IvDoorn@gmail.com>
7 Copyright (C) 2009 Alban Browaeys <prahal@yahoo.com>
8 Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
9 Copyright (C) 2009 Luis Correia <luis.f.correia@gmail.com>
10 Copyright (C) 2009 Mattias Nissler <mattias.nissler@gmx.de>
11 Copyright (C) 2009 Mark Asselstine <asselsm@gmail.com>
12 Copyright (C) 2009 Xose Vazquez Perez <xose.vazquez@gmail.com>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010013 <http://rt2x00.serialmonkey.com>
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the
27 Free Software Foundation, Inc.,
28 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30
31/*
32 Module: rt2800lib
33 Abstract: rt2800 generic device routines.
34 */
35
36#include <linux/kernel.h>
37#include <linux/module.h>
38
39#include "rt2x00.h"
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010040#ifdef CONFIG_RT2800USB
41#include "rt2x00usb.h"
42#endif
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010043#include "rt2800lib.h"
44#include "rt2800.h"
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010045#include "rt2800usb.h"
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010046
47MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
48MODULE_DESCRIPTION("rt2800 library");
49MODULE_LICENSE("GPL");
50
51/*
52 * Register access.
53 * All access to the CSR registers will go through the methods
54 * rt2800_register_read and rt2800_register_write.
55 * BBP and RF register require indirect register access,
56 * and use the CSR registers BBPCSR and RFCSR to achieve this.
57 * These indirect registers work with busy bits,
58 * and we will try maximal REGISTER_BUSY_COUNT times to access
59 * the register while taking a REGISTER_BUSY_DELAY us delay
60 * between each attampt. When the busy bit is still set at that time,
61 * the access attempt is considered to have failed,
62 * and we will print an error.
63 * The _lock versions must be used if you already hold the csr_mutex
64 */
65#define WAIT_FOR_BBP(__dev, __reg) \
66 rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
67#define WAIT_FOR_RFCSR(__dev, __reg) \
68 rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
69#define WAIT_FOR_RF(__dev, __reg) \
70 rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
71#define WAIT_FOR_MCU(__dev, __reg) \
72 rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
73 H2M_MAILBOX_CSR_OWNER, (__reg))
74
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010075static void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
76 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010077{
78 u32 reg;
79
80 mutex_lock(&rt2x00dev->csr_mutex);
81
82 /*
83 * Wait until the BBP becomes available, afterwards we
84 * can safely write the new data into the register.
85 */
86 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
87 reg = 0;
88 rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
89 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
90 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
91 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
92 if (rt2x00_intf_is_pci(rt2x00dev))
93 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
94
95 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
96 }
97
98 mutex_unlock(&rt2x00dev->csr_mutex);
99}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100100
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100101static void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
102 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100103{
104 u32 reg;
105
106 mutex_lock(&rt2x00dev->csr_mutex);
107
108 /*
109 * Wait until the BBP becomes available, afterwards we
110 * can safely write the read request into the register.
111 * After the data has been written, we wait until hardware
112 * returns the correct value, if at any time the register
113 * doesn't become available in time, reg will be 0xffffffff
114 * which means we return 0xff to the caller.
115 */
116 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
117 reg = 0;
118 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
119 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
120 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
121 if (rt2x00_intf_is_pci(rt2x00dev))
122 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
123
124 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
125
126 WAIT_FOR_BBP(rt2x00dev, &reg);
127 }
128
129 *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
130
131 mutex_unlock(&rt2x00dev->csr_mutex);
132}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100133
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100134static void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
135 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100136{
137 u32 reg;
138
139 mutex_lock(&rt2x00dev->csr_mutex);
140
141 /*
142 * Wait until the RFCSR becomes available, afterwards we
143 * can safely write the new data into the register.
144 */
145 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
146 reg = 0;
147 rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
148 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
149 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
150 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
151
152 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
153 }
154
155 mutex_unlock(&rt2x00dev->csr_mutex);
156}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100157
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100158static void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
159 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100160{
161 u32 reg;
162
163 mutex_lock(&rt2x00dev->csr_mutex);
164
165 /*
166 * Wait until the RFCSR becomes available, afterwards we
167 * can safely write the read request into the register.
168 * After the data has been written, we wait until hardware
169 * returns the correct value, if at any time the register
170 * doesn't become available in time, reg will be 0xffffffff
171 * which means we return 0xff to the caller.
172 */
173 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
174 reg = 0;
175 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
176 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
177 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
178
179 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
180
181 WAIT_FOR_RFCSR(rt2x00dev, &reg);
182 }
183
184 *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
185
186 mutex_unlock(&rt2x00dev->csr_mutex);
187}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100188
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100189static void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
190 const unsigned int word, const u32 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100191{
192 u32 reg;
193
194 mutex_lock(&rt2x00dev->csr_mutex);
195
196 /*
197 * Wait until the RF becomes available, afterwards we
198 * can safely write the new data into the register.
199 */
200 if (WAIT_FOR_RF(rt2x00dev, &reg)) {
201 reg = 0;
202 rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
203 rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
204 rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
205 rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
206
207 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
208 rt2x00_rf_write(rt2x00dev, word, value);
209 }
210
211 mutex_unlock(&rt2x00dev->csr_mutex);
212}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100213
214void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
215 const u8 command, const u8 token,
216 const u8 arg0, const u8 arg1)
217{
218 u32 reg;
219
220 if (rt2x00_intf_is_pci(rt2x00dev)) {
221 /*
222 * RT2880 and RT3052 don't support MCU requests.
223 */
224 if (rt2x00_rt(&rt2x00dev->chip, RT2880) ||
225 rt2x00_rt(&rt2x00dev->chip, RT3052))
226 return;
227 }
228
229 mutex_lock(&rt2x00dev->csr_mutex);
230
231 /*
232 * Wait until the MCU becomes available, afterwards we
233 * can safely write the new data into the register.
234 */
235 if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
236 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
237 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
238 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
239 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
240 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
241
242 reg = 0;
243 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
244 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
245 }
246
247 mutex_unlock(&rt2x00dev->csr_mutex);
248}
249EXPORT_SYMBOL_GPL(rt2800_mcu_request);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100250
251#ifdef CONFIG_RT2X00_LIB_DEBUGFS
252const struct rt2x00debug rt2800_rt2x00debug = {
253 .owner = THIS_MODULE,
254 .csr = {
255 .read = rt2800_register_read,
256 .write = rt2800_register_write,
257 .flags = RT2X00DEBUGFS_OFFSET,
258 .word_base = CSR_REG_BASE,
259 .word_size = sizeof(u32),
260 .word_count = CSR_REG_SIZE / sizeof(u32),
261 },
262 .eeprom = {
263 .read = rt2x00_eeprom_read,
264 .write = rt2x00_eeprom_write,
265 .word_base = EEPROM_BASE,
266 .word_size = sizeof(u16),
267 .word_count = EEPROM_SIZE / sizeof(u16),
268 },
269 .bbp = {
270 .read = rt2800_bbp_read,
271 .write = rt2800_bbp_write,
272 .word_base = BBP_BASE,
273 .word_size = sizeof(u8),
274 .word_count = BBP_SIZE / sizeof(u8),
275 },
276 .rf = {
277 .read = rt2x00_rf_read,
278 .write = rt2800_rf_write,
279 .word_base = RF_BASE,
280 .word_size = sizeof(u32),
281 .word_count = RF_SIZE / sizeof(u32),
282 },
283};
284EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
285#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
286
287int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
288{
289 u32 reg;
290
291 rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
292 return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
293}
294EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
295
296#ifdef CONFIG_RT2X00_LIB_LEDS
297static void rt2800_brightness_set(struct led_classdev *led_cdev,
298 enum led_brightness brightness)
299{
300 struct rt2x00_led *led =
301 container_of(led_cdev, struct rt2x00_led, led_dev);
302 unsigned int enabled = brightness != LED_OFF;
303 unsigned int bg_mode =
304 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
305 unsigned int polarity =
306 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
307 EEPROM_FREQ_LED_POLARITY);
308 unsigned int ledmode =
309 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
310 EEPROM_FREQ_LED_MODE);
311
312 if (led->type == LED_TYPE_RADIO) {
313 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
314 enabled ? 0x20 : 0);
315 } else if (led->type == LED_TYPE_ASSOC) {
316 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
317 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
318 } else if (led->type == LED_TYPE_QUALITY) {
319 /*
320 * The brightness is divided into 6 levels (0 - 5),
321 * The specs tell us the following levels:
322 * 0, 1 ,3, 7, 15, 31
323 * to determine the level in a simple way we can simply
324 * work with bitshifting:
325 * (1 << level) - 1
326 */
327 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
328 (1 << brightness / (LED_FULL / 6)) - 1,
329 polarity);
330 }
331}
332
333static int rt2800_blink_set(struct led_classdev *led_cdev,
334 unsigned long *delay_on, unsigned long *delay_off)
335{
336 struct rt2x00_led *led =
337 container_of(led_cdev, struct rt2x00_led, led_dev);
338 u32 reg;
339
340 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
341 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
342 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
343 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
344 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
345 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 12);
346 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
347 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
348 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
349
350 return 0;
351}
352
353void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
354 struct rt2x00_led *led, enum led_type type)
355{
356 led->rt2x00dev = rt2x00dev;
357 led->type = type;
358 led->led_dev.brightness_set = rt2800_brightness_set;
359 led->led_dev.blink_set = rt2800_blink_set;
360 led->flags = LED_INITIALIZED;
361}
362EXPORT_SYMBOL_GPL(rt2800_init_led);
363#endif /* CONFIG_RT2X00_LIB_LEDS */
364
365/*
366 * Configuration handlers.
367 */
368static void rt2800_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
369 struct rt2x00lib_crypto *crypto,
370 struct ieee80211_key_conf *key)
371{
372 struct mac_wcid_entry wcid_entry;
373 struct mac_iveiv_entry iveiv_entry;
374 u32 offset;
375 u32 reg;
376
377 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
378
379 rt2800_register_read(rt2x00dev, offset, &reg);
380 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
381 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
382 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
383 (crypto->cmd == SET_KEY) * crypto->cipher);
384 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
385 (crypto->cmd == SET_KEY) * crypto->bssidx);
386 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
387 rt2800_register_write(rt2x00dev, offset, reg);
388
389 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
390
391 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
392 if ((crypto->cipher == CIPHER_TKIP) ||
393 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
394 (crypto->cipher == CIPHER_AES))
395 iveiv_entry.iv[3] |= 0x20;
396 iveiv_entry.iv[3] |= key->keyidx << 6;
397 rt2800_register_multiwrite(rt2x00dev, offset,
398 &iveiv_entry, sizeof(iveiv_entry));
399
400 offset = MAC_WCID_ENTRY(key->hw_key_idx);
401
402 memset(&wcid_entry, 0, sizeof(wcid_entry));
403 if (crypto->cmd == SET_KEY)
404 memcpy(&wcid_entry, crypto->address, ETH_ALEN);
405 rt2800_register_multiwrite(rt2x00dev, offset,
406 &wcid_entry, sizeof(wcid_entry));
407}
408
409int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
410 struct rt2x00lib_crypto *crypto,
411 struct ieee80211_key_conf *key)
412{
413 struct hw_key_entry key_entry;
414 struct rt2x00_field32 field;
415 u32 offset;
416 u32 reg;
417
418 if (crypto->cmd == SET_KEY) {
419 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
420
421 memcpy(key_entry.key, crypto->key,
422 sizeof(key_entry.key));
423 memcpy(key_entry.tx_mic, crypto->tx_mic,
424 sizeof(key_entry.tx_mic));
425 memcpy(key_entry.rx_mic, crypto->rx_mic,
426 sizeof(key_entry.rx_mic));
427
428 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
429 rt2800_register_multiwrite(rt2x00dev, offset,
430 &key_entry, sizeof(key_entry));
431 }
432
433 /*
434 * The cipher types are stored over multiple registers
435 * starting with SHARED_KEY_MODE_BASE each word will have
436 * 32 bits and contains the cipher types for 2 bssidx each.
437 * Using the correct defines correctly will cause overhead,
438 * so just calculate the correct offset.
439 */
440 field.bit_offset = 4 * (key->hw_key_idx % 8);
441 field.bit_mask = 0x7 << field.bit_offset;
442
443 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
444
445 rt2800_register_read(rt2x00dev, offset, &reg);
446 rt2x00_set_field32(&reg, field,
447 (crypto->cmd == SET_KEY) * crypto->cipher);
448 rt2800_register_write(rt2x00dev, offset, reg);
449
450 /*
451 * Update WCID information
452 */
453 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
454
455 return 0;
456}
457EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
458
459int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
460 struct rt2x00lib_crypto *crypto,
461 struct ieee80211_key_conf *key)
462{
463 struct hw_key_entry key_entry;
464 u32 offset;
465
466 if (crypto->cmd == SET_KEY) {
467 /*
468 * 1 pairwise key is possible per AID, this means that the AID
469 * equals our hw_key_idx. Make sure the WCID starts _after_ the
470 * last possible shared key entry.
471 */
472 if (crypto->aid > (256 - 32))
473 return -ENOSPC;
474
475 key->hw_key_idx = 32 + crypto->aid;
476
477 memcpy(key_entry.key, crypto->key,
478 sizeof(key_entry.key));
479 memcpy(key_entry.tx_mic, crypto->tx_mic,
480 sizeof(key_entry.tx_mic));
481 memcpy(key_entry.rx_mic, crypto->rx_mic,
482 sizeof(key_entry.rx_mic));
483
484 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
485 rt2800_register_multiwrite(rt2x00dev, offset,
486 &key_entry, sizeof(key_entry));
487 }
488
489 /*
490 * Update WCID information
491 */
492 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
493
494 return 0;
495}
496EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
497
498void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
499 const unsigned int filter_flags)
500{
501 u32 reg;
502
503 /*
504 * Start configuration steps.
505 * Note that the version error will always be dropped
506 * and broadcast frames will always be accepted since
507 * there is no filter for it at this time.
508 */
509 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
510 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
511 !(filter_flags & FIF_FCSFAIL));
512 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
513 !(filter_flags & FIF_PLCPFAIL));
514 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
515 !(filter_flags & FIF_PROMISC_IN_BSS));
516 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
517 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
518 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
519 !(filter_flags & FIF_ALLMULTI));
520 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
521 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
522 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
523 !(filter_flags & FIF_CONTROL));
524 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
525 !(filter_flags & FIF_CONTROL));
526 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
527 !(filter_flags & FIF_CONTROL));
528 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
529 !(filter_flags & FIF_CONTROL));
530 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
531 !(filter_flags & FIF_CONTROL));
532 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
533 !(filter_flags & FIF_PSPOLL));
534 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
535 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
536 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
537 !(filter_flags & FIF_CONTROL));
538 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
539}
540EXPORT_SYMBOL_GPL(rt2800_config_filter);
541
542void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
543 struct rt2x00intf_conf *conf, const unsigned int flags)
544{
545 unsigned int beacon_base;
546 u32 reg;
547
548 if (flags & CONFIG_UPDATE_TYPE) {
549 /*
550 * Clear current synchronisation setup.
551 * For the Beacon base registers we only need to clear
552 * the first byte since that byte contains the VALID and OWNER
553 * bits which (when set to 0) will invalidate the entire beacon.
554 */
555 beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
556 rt2800_register_write(rt2x00dev, beacon_base, 0);
557
558 /*
559 * Enable synchronisation.
560 */
561 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
562 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
563 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
564 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
565 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
566 }
567
568 if (flags & CONFIG_UPDATE_MAC) {
569 reg = le32_to_cpu(conf->mac[1]);
570 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
571 conf->mac[1] = cpu_to_le32(reg);
572
573 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
574 conf->mac, sizeof(conf->mac));
575 }
576
577 if (flags & CONFIG_UPDATE_BSSID) {
578 reg = le32_to_cpu(conf->bssid[1]);
579 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 0);
580 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 0);
581 conf->bssid[1] = cpu_to_le32(reg);
582
583 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
584 conf->bssid, sizeof(conf->bssid));
585 }
586}
587EXPORT_SYMBOL_GPL(rt2800_config_intf);
588
589void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp)
590{
591 u32 reg;
592
593 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
594 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 0x20);
595 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
596
597 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
598 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
599 !!erp->short_preamble);
600 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
601 !!erp->short_preamble);
602 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
603
604 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
605 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
606 erp->cts_protection ? 2 : 0);
607 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
608
609 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
610 erp->basic_rates);
611 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
612
613 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
614 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, erp->slot_time);
615 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
616 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
617
618 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
619 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, erp->sifs);
620 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, erp->sifs);
621 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
622 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
623 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
624 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
625
626 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
627 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
628 erp->beacon_int * 16);
629 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
630}
631EXPORT_SYMBOL_GPL(rt2800_config_erp);
632
633void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
634{
635 u8 r1;
636 u8 r3;
637
638 rt2800_bbp_read(rt2x00dev, 1, &r1);
639 rt2800_bbp_read(rt2x00dev, 3, &r3);
640
641 /*
642 * Configure the TX antenna.
643 */
644 switch ((int)ant->tx) {
645 case 1:
646 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
647 if (rt2x00_intf_is_pci(rt2x00dev))
648 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
649 break;
650 case 2:
651 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
652 break;
653 case 3:
654 /* Do nothing */
655 break;
656 }
657
658 /*
659 * Configure the RX antenna.
660 */
661 switch ((int)ant->rx) {
662 case 1:
663 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
664 break;
665 case 2:
666 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
667 break;
668 case 3:
669 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
670 break;
671 }
672
673 rt2800_bbp_write(rt2x00dev, 3, r3);
674 rt2800_bbp_write(rt2x00dev, 1, r1);
675}
676EXPORT_SYMBOL_GPL(rt2800_config_ant);
677
678static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
679 struct rt2x00lib_conf *libconf)
680{
681 u16 eeprom;
682 short lna_gain;
683
684 if (libconf->rf.channel <= 14) {
685 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
686 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
687 } else if (libconf->rf.channel <= 64) {
688 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
689 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
690 } else if (libconf->rf.channel <= 128) {
691 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
692 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
693 } else {
694 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
695 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
696 }
697
698 rt2x00dev->lna_gain = lna_gain;
699}
700
701static void rt2800_config_channel_rt2x(struct rt2x00_dev *rt2x00dev,
702 struct ieee80211_conf *conf,
703 struct rf_channel *rf,
704 struct channel_info *info)
705{
706 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
707
708 if (rt2x00dev->default_ant.tx == 1)
709 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
710
711 if (rt2x00dev->default_ant.rx == 1) {
712 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
713 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
714 } else if (rt2x00dev->default_ant.rx == 2)
715 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
716
717 if (rf->channel > 14) {
718 /*
719 * When TX power is below 0, we should increase it by 7 to
720 * make it a positive value (Minumum value is -7).
721 * However this means that values between 0 and 7 have
722 * double meaning, and we should set a 7DBm boost flag.
723 */
724 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
725 (info->tx_power1 >= 0));
726
727 if (info->tx_power1 < 0)
728 info->tx_power1 += 7;
729
730 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A,
731 TXPOWER_A_TO_DEV(info->tx_power1));
732
733 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
734 (info->tx_power2 >= 0));
735
736 if (info->tx_power2 < 0)
737 info->tx_power2 += 7;
738
739 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A,
740 TXPOWER_A_TO_DEV(info->tx_power2));
741 } else {
742 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G,
743 TXPOWER_G_TO_DEV(info->tx_power1));
744 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G,
745 TXPOWER_G_TO_DEV(info->tx_power2));
746 }
747
748 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
749
750 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
751 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
752 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
753 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
754
755 udelay(200);
756
757 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
758 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
759 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
760 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
761
762 udelay(200);
763
764 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
765 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
766 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
767 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
768}
769
770static void rt2800_config_channel_rt3x(struct rt2x00_dev *rt2x00dev,
771 struct ieee80211_conf *conf,
772 struct rf_channel *rf,
773 struct channel_info *info)
774{
775 u8 rfcsr;
776
777 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Gertjan van Wingerde41a26172009-11-09 22:59:04 +0100778 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100779
780 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
781 rt2x00_set_field8(&rfcsr, RFCSR6_R, rf->rf2);
782 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
783
784 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
785 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
786 TXPOWER_G_TO_DEV(info->tx_power1));
787 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
788
789 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
790 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
791 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
792
793 rt2800_rfcsr_write(rt2x00dev, 24,
794 rt2x00dev->calibration[conf_is_ht40(conf)]);
795
796 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
797 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
798 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
799}
800
801static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
802 struct ieee80211_conf *conf,
803 struct rf_channel *rf,
804 struct channel_info *info)
805{
806 u32 reg;
807 unsigned int tx_pin;
808 u8 bbp;
809
Gertjan van Wingerdecce5fc42009-11-10 22:42:40 +0100810 if ((rt2x00_rt(&rt2x00dev->chip, RT3070) ||
811 rt2x00_rt(&rt2x00dev->chip, RT3090)) &&
Gertjan van Wingerdefa6f6322009-11-09 22:59:58 +0100812 (rt2x00_rf(&rt2x00dev->chip, RF2020) ||
813 rt2x00_rf(&rt2x00dev->chip, RF3020) ||
814 rt2x00_rf(&rt2x00dev->chip, RF3021) ||
815 rt2x00_rf(&rt2x00dev->chip, RF3022)))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100816 rt2800_config_channel_rt3x(rt2x00dev, conf, rf, info);
Gertjan van Wingerdefa6f6322009-11-09 22:59:58 +0100817 else
818 rt2800_config_channel_rt2x(rt2x00dev, conf, rf, info);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100819
820 /*
821 * Change BBP settings
822 */
823 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
824 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
825 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
826 rt2800_bbp_write(rt2x00dev, 86, 0);
827
828 if (rf->channel <= 14) {
829 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
830 rt2800_bbp_write(rt2x00dev, 82, 0x62);
831 rt2800_bbp_write(rt2x00dev, 75, 0x46);
832 } else {
833 rt2800_bbp_write(rt2x00dev, 82, 0x84);
834 rt2800_bbp_write(rt2x00dev, 75, 0x50);
835 }
836 } else {
837 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
838
839 if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
840 rt2800_bbp_write(rt2x00dev, 75, 0x46);
841 else
842 rt2800_bbp_write(rt2x00dev, 75, 0x50);
843 }
844
845 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
846 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_PLUS, conf_is_ht40_plus(conf));
847 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
848 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
849 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
850
851 tx_pin = 0;
852
853 /* Turn on unused PA or LNA when not using 1T or 1R */
854 if (rt2x00dev->default_ant.tx != 1) {
855 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
856 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
857 }
858
859 /* Turn on unused PA or LNA when not using 1T or 1R */
860 if (rt2x00dev->default_ant.rx != 1) {
861 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
862 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
863 }
864
865 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
866 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
867 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
868 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
869 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
870 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
871
872 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
873
874 rt2800_bbp_read(rt2x00dev, 4, &bbp);
875 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
876 rt2800_bbp_write(rt2x00dev, 4, bbp);
877
878 rt2800_bbp_read(rt2x00dev, 3, &bbp);
879 rt2x00_set_field8(&bbp, BBP3_HT40_PLUS, conf_is_ht40_plus(conf));
880 rt2800_bbp_write(rt2x00dev, 3, bbp);
881
882 if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION) {
883 if (conf_is_ht40(conf)) {
884 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
885 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
886 rt2800_bbp_write(rt2x00dev, 73, 0x16);
887 } else {
888 rt2800_bbp_write(rt2x00dev, 69, 0x16);
889 rt2800_bbp_write(rt2x00dev, 70, 0x08);
890 rt2800_bbp_write(rt2x00dev, 73, 0x11);
891 }
892 }
893
894 msleep(1);
895}
896
897static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
898 const int txpower)
899{
900 u32 reg;
901 u32 value = TXPOWER_G_TO_DEV(txpower);
902 u8 r1;
903
904 rt2800_bbp_read(rt2x00dev, 1, &r1);
905 rt2x00_set_field8(&reg, BBP1_TX_POWER, 0);
906 rt2800_bbp_write(rt2x00dev, 1, r1);
907
908 rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
909 rt2x00_set_field32(&reg, TX_PWR_CFG_0_1MBS, value);
910 rt2x00_set_field32(&reg, TX_PWR_CFG_0_2MBS, value);
911 rt2x00_set_field32(&reg, TX_PWR_CFG_0_55MBS, value);
912 rt2x00_set_field32(&reg, TX_PWR_CFG_0_11MBS, value);
913 rt2x00_set_field32(&reg, TX_PWR_CFG_0_6MBS, value);
914 rt2x00_set_field32(&reg, TX_PWR_CFG_0_9MBS, value);
915 rt2x00_set_field32(&reg, TX_PWR_CFG_0_12MBS, value);
916 rt2x00_set_field32(&reg, TX_PWR_CFG_0_18MBS, value);
917 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0, reg);
918
919 rt2800_register_read(rt2x00dev, TX_PWR_CFG_1, &reg);
920 rt2x00_set_field32(&reg, TX_PWR_CFG_1_24MBS, value);
921 rt2x00_set_field32(&reg, TX_PWR_CFG_1_36MBS, value);
922 rt2x00_set_field32(&reg, TX_PWR_CFG_1_48MBS, value);
923 rt2x00_set_field32(&reg, TX_PWR_CFG_1_54MBS, value);
924 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS0, value);
925 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS1, value);
926 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS2, value);
927 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS3, value);
928 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1, reg);
929
930 rt2800_register_read(rt2x00dev, TX_PWR_CFG_2, &reg);
931 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS4, value);
932 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS5, value);
933 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS6, value);
934 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS7, value);
935 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS8, value);
936 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS9, value);
937 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS10, value);
938 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS11, value);
939 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2, reg);
940
941 rt2800_register_read(rt2x00dev, TX_PWR_CFG_3, &reg);
942 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS12, value);
943 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS13, value);
944 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS14, value);
945 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS15, value);
946 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN1, value);
947 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN2, value);
948 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN3, value);
949 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN4, value);
950 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3, reg);
951
952 rt2800_register_read(rt2x00dev, TX_PWR_CFG_4, &reg);
953 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN5, value);
954 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN6, value);
955 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN7, value);
956 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN8, value);
957 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4, reg);
958}
959
960static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
961 struct rt2x00lib_conf *libconf)
962{
963 u32 reg;
964
965 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
966 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
967 libconf->conf->short_frame_max_tx_count);
968 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
969 libconf->conf->long_frame_max_tx_count);
970 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
971 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
972 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
973 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
974 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
975}
976
977static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
978 struct rt2x00lib_conf *libconf)
979{
980 enum dev_state state =
981 (libconf->conf->flags & IEEE80211_CONF_PS) ?
982 STATE_SLEEP : STATE_AWAKE;
983 u32 reg;
984
985 if (state == STATE_SLEEP) {
986 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
987
988 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
989 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
990 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
991 libconf->conf->listen_interval - 1);
992 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
993 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
994
995 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
996 } else {
997 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
998
999 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1000 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
1001 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
1002 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
1003 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
1004 }
1005}
1006
1007void rt2800_config(struct rt2x00_dev *rt2x00dev,
1008 struct rt2x00lib_conf *libconf,
1009 const unsigned int flags)
1010{
1011 /* Always recalculate LNA gain before changing configuration */
1012 rt2800_config_lna_gain(rt2x00dev, libconf);
1013
1014 if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
1015 rt2800_config_channel(rt2x00dev, libconf->conf,
1016 &libconf->rf, &libconf->channel);
1017 if (flags & IEEE80211_CONF_CHANGE_POWER)
1018 rt2800_config_txpower(rt2x00dev, libconf->conf->power_level);
1019 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
1020 rt2800_config_retry_limit(rt2x00dev, libconf);
1021 if (flags & IEEE80211_CONF_CHANGE_PS)
1022 rt2800_config_ps(rt2x00dev, libconf);
1023}
1024EXPORT_SYMBOL_GPL(rt2800_config);
1025
1026/*
1027 * Link tuning
1028 */
1029void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1030{
1031 u32 reg;
1032
1033 /*
1034 * Update FCS error count from register.
1035 */
1036 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1037 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
1038}
1039EXPORT_SYMBOL_GPL(rt2800_link_stats);
1040
1041static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
1042{
1043 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
1044 if (rt2x00_intf_is_usb(rt2x00dev) &&
1045 rt2x00_rev(&rt2x00dev->chip) == RT3070_VERSION)
1046 return 0x1c + (2 * rt2x00dev->lna_gain);
1047 else
1048 return 0x2e + rt2x00dev->lna_gain;
1049 }
1050
1051 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
1052 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
1053 else
1054 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
1055}
1056
1057static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
1058 struct link_qual *qual, u8 vgc_level)
1059{
1060 if (qual->vgc_level != vgc_level) {
1061 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
1062 qual->vgc_level = vgc_level;
1063 qual->vgc_level_reg = vgc_level;
1064 }
1065}
1066
1067void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
1068{
1069 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
1070}
1071EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
1072
1073void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
1074 const u32 count)
1075{
1076 if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION)
1077 return;
1078
1079 /*
1080 * When RSSI is better then -80 increase VGC level with 0x10
1081 */
1082 rt2800_set_vgc(rt2x00dev, qual,
1083 rt2800_get_default_vgc(rt2x00dev) +
1084 ((qual->rssi > -80) * 0x10));
1085}
1086EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001087
1088/*
1089 * Initialization functions.
1090 */
1091int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
1092{
1093 u32 reg;
1094 unsigned int i;
1095
1096 if (rt2x00_intf_is_usb(rt2x00dev)) {
1097 /*
Thadeu Lima de Souza Cascardo235faf92009-11-12 20:04:52 +01001098 * Wait until BBP and RF are ready.
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01001099 */
1100 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1101 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
1102 if (reg && reg != ~0)
1103 break;
1104 msleep(1);
1105 }
1106
1107 if (i == REGISTER_BUSY_COUNT) {
1108 ERROR(rt2x00dev, "Unstable hardware.\n");
1109 return -EBUSY;
1110 }
1111
1112 rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
1113 rt2800_register_write(rt2x00dev, PBF_SYS_CTRL,
1114 reg & ~0x00002000);
1115 } else if (rt2x00_intf_is_pci(rt2x00dev))
1116 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
1117
1118 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
1119 rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_CSR, 1);
1120 rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_BBP, 1);
1121 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
1122
1123 if (rt2x00_intf_is_usb(rt2x00dev)) {
1124 rt2800_register_write(rt2x00dev, USB_DMA_CFG, 0x00000000);
1125#ifdef CONFIG_RT2800USB
1126 rt2x00usb_vendor_request_sw(rt2x00dev, USB_DEVICE_MODE, 0,
1127 USB_MODE_RESET, REGISTER_TIMEOUT);
1128#endif
1129 }
1130
1131 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1132
1133 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
1134 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
1135 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
1136 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
1137 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
1138 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
1139
1140 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
1141 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
1142 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
1143 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
1144 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
1145 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
1146
1147 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
1148 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1149
1150 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1151
1152 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1153 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 0);
1154 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
1155 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
1156 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
1157 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1158 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
1159 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1160
1161 if (rt2x00_intf_is_usb(rt2x00dev) &&
1162 rt2x00_rev(&rt2x00dev->chip) == RT3070_VERSION) {
1163 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1164 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1165 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1166 } else {
1167 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
1168 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1169 }
1170
1171 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
1172 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
1173 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
1174 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
1175 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
1176 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
1177 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
1178 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
1179 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
1180 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
1181
1182 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
1183 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
1184 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
1185 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
1186
1187 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
1188 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
1189 if (rt2x00_rev(&rt2x00dev->chip) >= RT2880E_VERSION &&
1190 rt2x00_rev(&rt2x00dev->chip) < RT3070_VERSION)
1191 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
1192 else
1193 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
1194 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
1195 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
1196 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
1197
1198 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
1199
1200 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1201 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
1202 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
1203 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
1204 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
1205 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
1206 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1207
1208 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
1209 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 8);
1210 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
1211 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
1212 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1213 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1214 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1215 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1216 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1217 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 1);
1218 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
1219
1220 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
1221 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 8);
1222 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
1223 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
1224 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1225 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1226 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1227 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1228 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1229 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 1);
1230 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1231
1232 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1233 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
1234 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
1235 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
1236 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1237 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1238 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1239 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1240 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1241 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1242 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1243
1244 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1245 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
1246 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
1247 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
1248 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1249 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1250 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1251 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1252 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1253 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
1254 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1255
1256 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1257 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
1258 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
1259 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
1260 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1261 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1262 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1263 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1264 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1265 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1266 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1267
1268 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1269 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
1270 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
1271 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
1272 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1273 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1274 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1275 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1276 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1277 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
1278 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1279
1280 if (rt2x00_intf_is_usb(rt2x00dev)) {
1281 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
1282
1283 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1284 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1285 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1286 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1287 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1288 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
1289 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
1290 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
1291 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
1292 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
1293 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1294 }
1295
1296 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, 0x0000583f);
1297 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
1298
1299 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
1300 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
1301 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
1302 IEEE80211_MAX_RTS_THRESHOLD);
1303 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
1304 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
1305
1306 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
1307 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
1308
1309 /*
1310 * ASIC will keep garbage value after boot, clear encryption keys.
1311 */
1312 for (i = 0; i < 4; i++)
1313 rt2800_register_write(rt2x00dev,
1314 SHARED_KEY_MODE_ENTRY(i), 0);
1315
1316 for (i = 0; i < 256; i++) {
1317 u32 wcid[2] = { 0xffffffff, 0x00ffffff };
1318 rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
1319 wcid, sizeof(wcid));
1320
1321 rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
1322 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
1323 }
1324
1325 /*
1326 * Clear all beacons
1327 * For the Beacon base registers we only need to clear
1328 * the first byte since that byte contains the VALID and OWNER
1329 * bits which (when set to 0) will invalidate the entire beacon.
1330 */
1331 rt2800_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
1332 rt2800_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
1333 rt2800_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
1334 rt2800_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
1335 rt2800_register_write(rt2x00dev, HW_BEACON_BASE4, 0);
1336 rt2800_register_write(rt2x00dev, HW_BEACON_BASE5, 0);
1337 rt2800_register_write(rt2x00dev, HW_BEACON_BASE6, 0);
1338 rt2800_register_write(rt2x00dev, HW_BEACON_BASE7, 0);
1339
1340 if (rt2x00_intf_is_usb(rt2x00dev)) {
1341 rt2800_register_read(rt2x00dev, USB_CYC_CFG, &reg);
1342 rt2x00_set_field32(&reg, USB_CYC_CFG_CLOCK_CYCLE, 30);
1343 rt2800_register_write(rt2x00dev, USB_CYC_CFG, reg);
1344 }
1345
1346 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
1347 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
1348 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
1349 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
1350 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
1351 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
1352 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
1353 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
1354 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
1355 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
1356
1357 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
1358 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
1359 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
1360 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
1361 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
1362 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
1363 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
1364 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
1365 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
1366 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
1367
1368 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
1369 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
1370 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
1371 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
1372 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
1373 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
1374 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
1375 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
1376 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
1377 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
1378
1379 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
1380 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
1381 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
1382 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
1383 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
1384 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
1385
1386 /*
1387 * We must clear the error counters.
1388 * These registers are cleared on read,
1389 * so we may pass a useless variable to store the value.
1390 */
1391 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1392 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
1393 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
1394 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
1395 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
1396 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
1397
1398 return 0;
1399}
1400EXPORT_SYMBOL_GPL(rt2800_init_registers);
1401
1402static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
1403{
1404 unsigned int i;
1405 u32 reg;
1406
1407 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1408 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
1409 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
1410 return 0;
1411
1412 udelay(REGISTER_BUSY_DELAY);
1413 }
1414
1415 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
1416 return -EACCES;
1417}
1418
1419static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
1420{
1421 unsigned int i;
1422 u8 value;
1423
1424 /*
1425 * BBP was enabled after firmware was loaded,
1426 * but we need to reactivate it now.
1427 */
1428 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
1429 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
1430 msleep(1);
1431
1432 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1433 rt2800_bbp_read(rt2x00dev, 0, &value);
1434 if ((value != 0xff) && (value != 0x00))
1435 return 0;
1436 udelay(REGISTER_BUSY_DELAY);
1437 }
1438
1439 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
1440 return -EACCES;
1441}
1442
1443int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
1444{
1445 unsigned int i;
1446 u16 eeprom;
1447 u8 reg_id;
1448 u8 value;
1449
1450 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
1451 rt2800_wait_bbp_ready(rt2x00dev)))
1452 return -EACCES;
1453
1454 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
1455 rt2800_bbp_write(rt2x00dev, 66, 0x38);
1456 rt2800_bbp_write(rt2x00dev, 69, 0x12);
1457 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1458 rt2800_bbp_write(rt2x00dev, 73, 0x10);
1459 rt2800_bbp_write(rt2x00dev, 81, 0x37);
1460 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1461 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
1462 rt2800_bbp_write(rt2x00dev, 84, 0x99);
1463 rt2800_bbp_write(rt2x00dev, 86, 0x00);
1464 rt2800_bbp_write(rt2x00dev, 91, 0x04);
1465 rt2800_bbp_write(rt2x00dev, 92, 0x00);
1466 rt2800_bbp_write(rt2x00dev, 103, 0x00);
1467 rt2800_bbp_write(rt2x00dev, 105, 0x05);
1468
1469 if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION) {
1470 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1471 rt2800_bbp_write(rt2x00dev, 73, 0x12);
1472 }
1473
1474 if (rt2x00_rev(&rt2x00dev->chip) > RT2860D_VERSION)
1475 rt2800_bbp_write(rt2x00dev, 84, 0x19);
1476
1477 if (rt2x00_intf_is_usb(rt2x00dev) &&
1478 rt2x00_rev(&rt2x00dev->chip) == RT3070_VERSION) {
1479 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1480 rt2800_bbp_write(rt2x00dev, 84, 0x99);
1481 rt2800_bbp_write(rt2x00dev, 105, 0x05);
1482 }
1483
1484 if (rt2x00_intf_is_pci(rt2x00dev) &&
1485 rt2x00_rt(&rt2x00dev->chip, RT3052)) {
1486 rt2800_bbp_write(rt2x00dev, 31, 0x08);
1487 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
1488 rt2800_bbp_write(rt2x00dev, 80, 0x08);
1489 }
1490
1491 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
1492 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
1493
1494 if (eeprom != 0xffff && eeprom != 0x0000) {
1495 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
1496 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
1497 rt2800_bbp_write(rt2x00dev, reg_id, value);
1498 }
1499 }
1500
1501 return 0;
1502}
1503EXPORT_SYMBOL_GPL(rt2800_init_bbp);
1504
1505static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
1506 bool bw40, u8 rfcsr24, u8 filter_target)
1507{
1508 unsigned int i;
1509 u8 bbp;
1510 u8 rfcsr;
1511 u8 passband;
1512 u8 stopband;
1513 u8 overtuned = 0;
1514
1515 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1516
1517 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1518 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
1519 rt2800_bbp_write(rt2x00dev, 4, bbp);
1520
1521 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
1522 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
1523 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
1524
1525 /*
1526 * Set power & frequency of passband test tone
1527 */
1528 rt2800_bbp_write(rt2x00dev, 24, 0);
1529
1530 for (i = 0; i < 100; i++) {
1531 rt2800_bbp_write(rt2x00dev, 25, 0x90);
1532 msleep(1);
1533
1534 rt2800_bbp_read(rt2x00dev, 55, &passband);
1535 if (passband)
1536 break;
1537 }
1538
1539 /*
1540 * Set power & frequency of stopband test tone
1541 */
1542 rt2800_bbp_write(rt2x00dev, 24, 0x06);
1543
1544 for (i = 0; i < 100; i++) {
1545 rt2800_bbp_write(rt2x00dev, 25, 0x90);
1546 msleep(1);
1547
1548 rt2800_bbp_read(rt2x00dev, 55, &stopband);
1549
1550 if ((passband - stopband) <= filter_target) {
1551 rfcsr24++;
1552 overtuned += ((passband - stopband) == filter_target);
1553 } else
1554 break;
1555
1556 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1557 }
1558
1559 rfcsr24 -= !!overtuned;
1560
1561 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
1562 return rfcsr24;
1563}
1564
1565int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
1566{
1567 u8 rfcsr;
1568 u8 bbp;
1569
1570 if (rt2x00_intf_is_usb(rt2x00dev) &&
1571 rt2x00_rev(&rt2x00dev->chip) != RT3070_VERSION)
1572 return 0;
1573
1574 if (rt2x00_intf_is_pci(rt2x00dev)) {
1575 if (!rt2x00_rf(&rt2x00dev->chip, RF3020) &&
1576 !rt2x00_rf(&rt2x00dev->chip, RF3021) &&
1577 !rt2x00_rf(&rt2x00dev->chip, RF3022))
1578 return 0;
1579 }
1580
1581 /*
1582 * Init RF calibration.
1583 */
1584 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1585 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1586 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1587 msleep(1);
1588 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1589 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1590
1591 if (rt2x00_intf_is_usb(rt2x00dev)) {
1592 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
1593 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
1594 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
1595 rt2800_rfcsr_write(rt2x00dev, 7, 0x70);
1596 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
1597 rt2800_rfcsr_write(rt2x00dev, 10, 0x71);
1598 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
1599 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
1600 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
1601 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
1602 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
1603 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
1604 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
1605 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
1606 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
1607 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
1608 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
1609 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
1610 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
1611 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
1612 } else if (rt2x00_intf_is_pci(rt2x00dev)) {
1613 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
1614 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
1615 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
1616 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
1617 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
1618 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
1619 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
1620 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
1621 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
1622 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
1623 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
1624 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
1625 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
1626 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
1627 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
1628 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
1629 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
1630 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
1631 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
1632 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
1633 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
1634 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
1635 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
1636 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
1637 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
1638 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
1639 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
1640 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
1641 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
1642 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
1643 }
1644
1645 /*
1646 * Set RX Filter calibration for 20MHz and 40MHz
1647 */
1648 rt2x00dev->calibration[0] =
1649 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
1650 rt2x00dev->calibration[1] =
1651 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
1652
1653 /*
1654 * Set back to initial state
1655 */
1656 rt2800_bbp_write(rt2x00dev, 24, 0);
1657
1658 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
1659 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
1660 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
1661
1662 /*
1663 * set BBP back to BW20
1664 */
1665 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1666 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
1667 rt2800_bbp_write(rt2x00dev, 4, bbp);
1668
1669 return 0;
1670}
1671EXPORT_SYMBOL_GPL(rt2800_init_rfcsr);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01001672
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01001673int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
1674{
1675 u32 reg;
1676
1677 rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
1678
1679 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
1680}
1681EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
1682
1683static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
1684{
1685 u32 reg;
1686
1687 rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
1688 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
1689 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
1690 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
1691 rt2800_register_write(rt2x00dev, EFUSE_CTRL, reg);
1692
1693 /* Wait until the EEPROM has been loaded */
1694 rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
1695
1696 /* Apparently the data is read from end to start */
1697 rt2800_register_read(rt2x00dev, EFUSE_DATA3,
1698 (u32 *)&rt2x00dev->eeprom[i]);
1699 rt2800_register_read(rt2x00dev, EFUSE_DATA2,
1700 (u32 *)&rt2x00dev->eeprom[i + 2]);
1701 rt2800_register_read(rt2x00dev, EFUSE_DATA1,
1702 (u32 *)&rt2x00dev->eeprom[i + 4]);
1703 rt2800_register_read(rt2x00dev, EFUSE_DATA0,
1704 (u32 *)&rt2x00dev->eeprom[i + 6]);
1705}
1706
1707void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
1708{
1709 unsigned int i;
1710
1711 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
1712 rt2800_efuse_read(rt2x00dev, i);
1713}
1714EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
1715
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01001716int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
1717{
1718 u16 word;
1719 u8 *mac;
1720 u8 default_lna_gain;
1721
1722 /*
1723 * Start validation of the data that has been read.
1724 */
1725 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
1726 if (!is_valid_ether_addr(mac)) {
1727 random_ether_addr(mac);
1728 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
1729 }
1730
1731 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
1732 if (word == 0xffff) {
1733 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
1734 rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
1735 rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
1736 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
1737 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
1738 } else if (rt2x00_rev(&rt2x00dev->chip) < RT2883_VERSION) {
1739 /*
1740 * There is a max of 2 RX streams for RT28x0 series
1741 */
1742 if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
1743 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
1744 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
1745 }
1746
1747 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
1748 if (word == 0xffff) {
1749 rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
1750 rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
1751 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
1752 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
1753 rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
1754 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
1755 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
1756 rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
1757 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
1758 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
1759 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
1760 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
1761 }
1762
1763 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
1764 if ((word & 0x00ff) == 0x00ff) {
1765 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
1766 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
1767 LED_MODE_TXRX_ACTIVITY);
1768 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
1769 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
1770 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
1771 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
1772 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
1773 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
1774 }
1775
1776 /*
1777 * During the LNA validation we are going to use
1778 * lna0 as correct value. Note that EEPROM_LNA
1779 * is never validated.
1780 */
1781 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
1782 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
1783
1784 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
1785 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
1786 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
1787 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
1788 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
1789 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
1790
1791 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
1792 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
1793 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
1794 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
1795 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
1796 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
1797 default_lna_gain);
1798 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
1799
1800 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
1801 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
1802 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
1803 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
1804 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
1805 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
1806
1807 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
1808 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
1809 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
1810 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
1811 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
1812 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
1813 default_lna_gain);
1814 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
1815
1816 return 0;
1817}
1818EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
1819
1820int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
1821{
1822 u32 reg;
1823 u16 value;
1824 u16 eeprom;
1825
1826 /*
1827 * Read EEPROM word for configuration.
1828 */
1829 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
1830
1831 /*
1832 * Identify RF chipset.
1833 */
1834 value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
1835 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
1836
Gertjan van Wingerdef273fe52009-11-10 22:41:51 +01001837 rt2x00_set_chip_rf(rt2x00dev, value, reg);
1838
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01001839 if (rt2x00_intf_is_usb(rt2x00dev)) {
1840 struct rt2x00_chip *chip = &rt2x00dev->chip;
1841
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01001842 /*
1843 * The check for rt2860 is not a typo, some rt2870 hardware
1844 * identifies itself as rt2860 in the CSR register.
1845 */
Gertjan van Wingerdef273fe52009-11-10 22:41:51 +01001846 if (rt2x00_check_rev(chip, 0xfff00000, 0x28600000) ||
1847 rt2x00_check_rev(chip, 0xfff00000, 0x28700000) ||
1848 rt2x00_check_rev(chip, 0xfff00000, 0x28800000)) {
1849 rt2x00_set_chip_rt(rt2x00dev, RT2870);
1850 } else if (rt2x00_check_rev(chip, 0xffff0000, 0x30700000)) {
1851 rt2x00_set_chip_rt(rt2x00dev, RT3070);
1852 } else {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01001853 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
1854 return -ENODEV;
1855 }
Gertjan van Wingerdef273fe52009-11-10 22:41:51 +01001856 }
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01001857
1858 if (!rt2x00_rf(&rt2x00dev->chip, RF2820) &&
1859 !rt2x00_rf(&rt2x00dev->chip, RF2850) &&
1860 !rt2x00_rf(&rt2x00dev->chip, RF2720) &&
1861 !rt2x00_rf(&rt2x00dev->chip, RF2750) &&
1862 !rt2x00_rf(&rt2x00dev->chip, RF3020) &&
1863 !rt2x00_rf(&rt2x00dev->chip, RF2020) &&
Gertjan van Wingerdef273fe52009-11-10 22:41:51 +01001864 !rt2x00_rf(&rt2x00dev->chip, RF3021) &&
1865 !rt2x00_rf(&rt2x00dev->chip, RF3022)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01001866 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
1867 return -ENODEV;
1868 }
1869
1870 /*
1871 * Identify default antenna configuration.
1872 */
1873 rt2x00dev->default_ant.tx =
1874 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH);
1875 rt2x00dev->default_ant.rx =
1876 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH);
1877
1878 /*
1879 * Read frequency offset and RF programming sequence.
1880 */
1881 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
1882 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
1883
1884 /*
1885 * Read external LNA informations.
1886 */
1887 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1888
1889 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
1890 __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
1891 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
1892 __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
1893
1894 /*
1895 * Detect if this device has an hardware controlled radio.
1896 */
1897 if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
1898 __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
1899
1900 /*
1901 * Store led settings, for correct led behaviour.
1902 */
1903#ifdef CONFIG_RT2X00_LIB_LEDS
1904 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
1905 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
1906 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
1907
1908 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &rt2x00dev->led_mcu_reg);
1909#endif /* CONFIG_RT2X00_LIB_LEDS */
1910
1911 return 0;
1912}
1913EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
1914
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01001915/*
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01001916 * RF value list for rt28x0
1917 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
1918 */
1919static const struct rf_channel rf_vals[] = {
1920 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
1921 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
1922 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
1923 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
1924 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
1925 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
1926 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
1927 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
1928 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
1929 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
1930 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
1931 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
1932 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
1933 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
1934
1935 /* 802.11 UNI / HyperLan 2 */
1936 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
1937 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
1938 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
1939 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
1940 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
1941 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
1942 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
1943 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
1944 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
1945 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
1946 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
1947 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
1948
1949 /* 802.11 HyperLan 2 */
1950 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
1951 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
1952 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
1953 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
1954 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
1955 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
1956 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
1957 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
1958 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
1959 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
1960 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
1961 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
1962 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
1963 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
1964 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
1965 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
1966
1967 /* 802.11 UNII */
1968 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
1969 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
1970 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
1971 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
1972 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
1973 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
1974 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
1975 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
1976 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
1977 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
1978 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
1979
1980 /* 802.11 Japan */
1981 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
1982 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
1983 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
1984 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
1985 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
1986 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
1987 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
1988};
1989
1990/*
1991 * RF value list for rt3070
1992 * Supports: 2.4 GHz
1993 */
Gertjan van Wingerdecce5fc42009-11-10 22:42:40 +01001994static const struct rf_channel rf_vals_302x[] = {
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01001995 {1, 241, 2, 2 },
1996 {2, 241, 2, 7 },
1997 {3, 242, 2, 2 },
1998 {4, 242, 2, 7 },
1999 {5, 243, 2, 2 },
2000 {6, 243, 2, 7 },
2001 {7, 244, 2, 2 },
2002 {8, 244, 2, 7 },
2003 {9, 245, 2, 2 },
2004 {10, 245, 2, 7 },
2005 {11, 246, 2, 2 },
2006 {12, 246, 2, 7 },
2007 {13, 247, 2, 2 },
2008 {14, 248, 2, 4 },
2009};
2010
2011int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
2012{
2013 struct rt2x00_chip *chip = &rt2x00dev->chip;
2014 struct hw_mode_spec *spec = &rt2x00dev->spec;
2015 struct channel_info *info;
2016 char *tx_power1;
2017 char *tx_power2;
2018 unsigned int i;
2019 u16 eeprom;
2020
2021 /*
2022 * Initialize all hw fields.
2023 */
2024 rt2x00dev->hw->flags =
2025 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2026 IEEE80211_HW_SIGNAL_DBM |
2027 IEEE80211_HW_SUPPORTS_PS |
2028 IEEE80211_HW_PS_NULLFUNC_STACK;
2029
2030 if (rt2x00_intf_is_usb(rt2x00dev))
2031 rt2x00dev->hw->extra_tx_headroom =
2032 TXINFO_DESC_SIZE + TXWI_DESC_SIZE;
2033 else if (rt2x00_intf_is_pci(rt2x00dev))
2034 rt2x00dev->hw->extra_tx_headroom = TXWI_DESC_SIZE;
2035
2036 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
2037 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
2038 rt2x00_eeprom_addr(rt2x00dev,
2039 EEPROM_MAC_ADDR_0));
2040
2041 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2042
2043 /*
2044 * Initialize hw_mode information.
2045 */
2046 spec->supported_bands = SUPPORT_BAND_2GHZ;
2047 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
2048
2049 if (rt2x00_rf(chip, RF2820) ||
2050 rt2x00_rf(chip, RF2720) ||
Gertjan van Wingerdecce5fc42009-11-10 22:42:40 +01002051 (rt2x00_intf_is_pci(rt2x00dev) && rt2x00_rf(chip, RF3052))) {
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002052 spec->num_channels = 14;
2053 spec->channels = rf_vals;
Gertjan van Wingerdecce5fc42009-11-10 22:42:40 +01002054 } else if (rt2x00_rf(chip, RF2850) || rt2x00_rf(chip, RF2750)) {
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002055 spec->supported_bands |= SUPPORT_BAND_5GHZ;
2056 spec->num_channels = ARRAY_SIZE(rf_vals);
2057 spec->channels = rf_vals;
Gertjan van Wingerdecce5fc42009-11-10 22:42:40 +01002058 } else if (rt2x00_rf(chip, RF3020) ||
2059 rt2x00_rf(chip, RF2020) ||
2060 rt2x00_rf(chip, RF3021) ||
2061 rt2x00_rf(chip, RF3022)) {
2062 spec->num_channels = ARRAY_SIZE(rf_vals_302x);
2063 spec->channels = rf_vals_302x;
Bartlomiej Zolnierkiewicz4da2933f2009-11-08 14:39:32 +01002064 }
2065
2066 /*
2067 * Initialize HT information.
2068 */
2069 spec->ht.ht_supported = true;
2070 spec->ht.cap =
2071 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2072 IEEE80211_HT_CAP_GRN_FLD |
2073 IEEE80211_HT_CAP_SGI_20 |
2074 IEEE80211_HT_CAP_SGI_40 |
2075 IEEE80211_HT_CAP_TX_STBC |
2076 IEEE80211_HT_CAP_RX_STBC |
2077 IEEE80211_HT_CAP_PSMP_SUPPORT;
2078 spec->ht.ampdu_factor = 3;
2079 spec->ht.ampdu_density = 4;
2080 spec->ht.mcs.tx_params =
2081 IEEE80211_HT_MCS_TX_DEFINED |
2082 IEEE80211_HT_MCS_TX_RX_DIFF |
2083 ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
2084 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
2085
2086 switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
2087 case 3:
2088 spec->ht.mcs.rx_mask[2] = 0xff;
2089 case 2:
2090 spec->ht.mcs.rx_mask[1] = 0xff;
2091 case 1:
2092 spec->ht.mcs.rx_mask[0] = 0xff;
2093 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
2094 break;
2095 }
2096
2097 /*
2098 * Create channel information array
2099 */
2100 info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
2101 if (!info)
2102 return -ENOMEM;
2103
2104 spec->channels_info = info;
2105
2106 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
2107 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
2108
2109 for (i = 0; i < 14; i++) {
2110 info[i].tx_power1 = TXPOWER_G_FROM_DEV(tx_power1[i]);
2111 info[i].tx_power2 = TXPOWER_G_FROM_DEV(tx_power2[i]);
2112 }
2113
2114 if (spec->num_channels > 14) {
2115 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
2116 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
2117
2118 for (i = 14; i < spec->num_channels; i++) {
2119 info[i].tx_power1 = TXPOWER_A_FROM_DEV(tx_power1[i]);
2120 info[i].tx_power2 = TXPOWER_A_FROM_DEV(tx_power2[i]);
2121 }
2122 }
2123
2124 return 0;
2125}
2126EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
2127
2128/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01002129 * IEEE80211 stack callback functions.
2130 */
2131static void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx,
2132 u32 *iv32, u16 *iv16)
2133{
2134 struct rt2x00_dev *rt2x00dev = hw->priv;
2135 struct mac_iveiv_entry iveiv_entry;
2136 u32 offset;
2137
2138 offset = MAC_IVEIV_ENTRY(hw_key_idx);
2139 rt2800_register_multiread(rt2x00dev, offset,
2140 &iveiv_entry, sizeof(iveiv_entry));
2141
2142 memcpy(&iveiv_entry.iv[0], iv16, sizeof(iv16));
2143 memcpy(&iveiv_entry.iv[4], iv32, sizeof(iv32));
2144}
2145
2146static int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2147{
2148 struct rt2x00_dev *rt2x00dev = hw->priv;
2149 u32 reg;
2150 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
2151
2152 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
2153 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
2154 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
2155
2156 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
2157 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
2158 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
2159
2160 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
2161 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
2162 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
2163
2164 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
2165 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
2166 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
2167
2168 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
2169 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
2170 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
2171
2172 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
2173 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
2174 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
2175
2176 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
2177 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
2178 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
2179
2180 return 0;
2181}
2182
2183static int rt2800_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
2184 const struct ieee80211_tx_queue_params *params)
2185{
2186 struct rt2x00_dev *rt2x00dev = hw->priv;
2187 struct data_queue *queue;
2188 struct rt2x00_field32 field;
2189 int retval;
2190 u32 reg;
2191 u32 offset;
2192
2193 /*
2194 * First pass the configuration through rt2x00lib, that will
2195 * update the queue settings and validate the input. After that
2196 * we are free to update the registers based on the value
2197 * in the queue parameter.
2198 */
2199 retval = rt2x00mac_conf_tx(hw, queue_idx, params);
2200 if (retval)
2201 return retval;
2202
2203 /*
2204 * We only need to perform additional register initialization
2205 * for WMM queues/
2206 */
2207 if (queue_idx >= 4)
2208 return 0;
2209
2210 queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
2211
2212 /* Update WMM TXOP register */
2213 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
2214 field.bit_offset = (queue_idx & 1) * 16;
2215 field.bit_mask = 0xffff << field.bit_offset;
2216
2217 rt2800_register_read(rt2x00dev, offset, &reg);
2218 rt2x00_set_field32(&reg, field, queue->txop);
2219 rt2800_register_write(rt2x00dev, offset, reg);
2220
2221 /* Update WMM registers */
2222 field.bit_offset = queue_idx * 4;
2223 field.bit_mask = 0xf << field.bit_offset;
2224
2225 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
2226 rt2x00_set_field32(&reg, field, queue->aifs);
2227 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
2228
2229 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
2230 rt2x00_set_field32(&reg, field, queue->cw_min);
2231 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
2232
2233 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
2234 rt2x00_set_field32(&reg, field, queue->cw_max);
2235 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
2236
2237 /* Update EDCA registers */
2238 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
2239
2240 rt2800_register_read(rt2x00dev, offset, &reg);
2241 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
2242 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
2243 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
2244 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
2245 rt2800_register_write(rt2x00dev, offset, reg);
2246
2247 return 0;
2248}
2249
2250static u64 rt2800_get_tsf(struct ieee80211_hw *hw)
2251{
2252 struct rt2x00_dev *rt2x00dev = hw->priv;
2253 u64 tsf;
2254 u32 reg;
2255
2256 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
2257 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
2258 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
2259 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
2260
2261 return tsf;
2262}
2263
2264const struct ieee80211_ops rt2800_mac80211_ops = {
2265 .tx = rt2x00mac_tx,
2266 .start = rt2x00mac_start,
2267 .stop = rt2x00mac_stop,
2268 .add_interface = rt2x00mac_add_interface,
2269 .remove_interface = rt2x00mac_remove_interface,
2270 .config = rt2x00mac_config,
2271 .configure_filter = rt2x00mac_configure_filter,
2272 .set_tim = rt2x00mac_set_tim,
2273 .set_key = rt2x00mac_set_key,
2274 .get_stats = rt2x00mac_get_stats,
2275 .get_tkip_seq = rt2800_get_tkip_seq,
2276 .set_rts_threshold = rt2800_set_rts_threshold,
2277 .bss_info_changed = rt2x00mac_bss_info_changed,
2278 .conf_tx = rt2800_conf_tx,
2279 .get_tx_stats = rt2x00mac_get_tx_stats,
2280 .get_tsf = rt2800_get_tsf,
2281 .rfkill_poll = rt2x00mac_rfkill_poll,
2282};
2283EXPORT_SYMBOL_GPL(rt2800_mac80211_ops);