GNU Linux-libre 4.9.333-gnu1
[releases.git] / drivers / net / wireless / ralink / rt2x00 / rt2800pci.c
1 /*
2         Copyright (C) 2009 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
3         Copyright (C) 2009 Alban Browaeys <prahal@yahoo.com>
4         Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
5         Copyright (C) 2009 Luis Correia <luis.f.correia@gmail.com>
6         Copyright (C) 2009 Mattias Nissler <mattias.nissler@gmx.de>
7         Copyright (C) 2009 Mark Asselstine <asselsm@gmail.com>
8         Copyright (C) 2009 Xose Vazquez Perez <xose.vazquez@gmail.com>
9         Copyright (C) 2009 Bart Zolnierkiewicz <bzolnier@gmail.com>
10         <http://rt2x00.serialmonkey.com>
11
12         This program is free software; you can redistribute it and/or modify
13         it under the terms of the GNU General Public License as published by
14         the Free Software Foundation; either version 2 of the License, or
15         (at your option) any later version.
16
17         This program is distributed in the hope that it will be useful,
18         but WITHOUT ANY WARRANTY; without even the implied warranty of
19         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20         GNU General Public License for more details.
21
22         You should have received a copy of the GNU General Public License
23         along with this program; if not, see <http://www.gnu.org/licenses/>.
24  */
25
26 /*
27         Module: rt2800pci
28         Abstract: rt2800pci device specific routines.
29         Supported chipsets: RT2800E & RT2800ED.
30  */
31
32 #include <linux/delay.h>
33 #include <linux/etherdevice.h>
34 #include <linux/init.h>
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 #include <linux/eeprom_93cx6.h>
39
40 #include "rt2x00.h"
41 #include "rt2x00mmio.h"
42 #include "rt2x00pci.h"
43 #include "rt2800lib.h"
44 #include "rt2800mmio.h"
45 #include "rt2800.h"
46 #include "rt2800pci.h"
47
48 /*
49  * Allow hardware encryption to be disabled.
50  */
51 static bool modparam_nohwcrypt = false;
52 module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
53 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
54
55 static bool rt2800pci_hwcrypt_disabled(struct rt2x00_dev *rt2x00dev)
56 {
57         return modparam_nohwcrypt;
58 }
59
60 static void rt2800pci_mcu_status(struct rt2x00_dev *rt2x00dev, const u8 token)
61 {
62         unsigned int i;
63         u32 reg;
64
65         /*
66          * SOC devices don't support MCU requests.
67          */
68         if (rt2x00_is_soc(rt2x00dev))
69                 return;
70
71         for (i = 0; i < 200; i++) {
72                 rt2x00mmio_register_read(rt2x00dev, H2M_MAILBOX_CID, &reg);
73
74                 if ((rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD0) == token) ||
75                     (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD1) == token) ||
76                     (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD2) == token) ||
77                     (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD3) == token))
78                         break;
79
80                 udelay(REGISTER_BUSY_DELAY);
81         }
82
83         if (i == 200)
84                 rt2x00_err(rt2x00dev, "MCU request failed, no response from hardware\n");
85
86         rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_STATUS, ~0);
87         rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_CID, ~0);
88 }
89
90 static void rt2800pci_eepromregister_read(struct eeprom_93cx6 *eeprom)
91 {
92         struct rt2x00_dev *rt2x00dev = eeprom->data;
93         u32 reg;
94
95         rt2x00mmio_register_read(rt2x00dev, E2PROM_CSR, &reg);
96
97         eeprom->reg_data_in = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_IN);
98         eeprom->reg_data_out = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_OUT);
99         eeprom->reg_data_clock =
100             !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_CLOCK);
101         eeprom->reg_chip_select =
102             !!rt2x00_get_field32(reg, E2PROM_CSR_CHIP_SELECT);
103 }
104
105 static void rt2800pci_eepromregister_write(struct eeprom_93cx6 *eeprom)
106 {
107         struct rt2x00_dev *rt2x00dev = eeprom->data;
108         u32 reg = 0;
109
110         rt2x00_set_field32(&reg, E2PROM_CSR_DATA_IN, !!eeprom->reg_data_in);
111         rt2x00_set_field32(&reg, E2PROM_CSR_DATA_OUT, !!eeprom->reg_data_out);
112         rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK,
113                            !!eeprom->reg_data_clock);
114         rt2x00_set_field32(&reg, E2PROM_CSR_CHIP_SELECT,
115                            !!eeprom->reg_chip_select);
116
117         rt2x00mmio_register_write(rt2x00dev, E2PROM_CSR, reg);
118 }
119
120 static int rt2800pci_read_eeprom_pci(struct rt2x00_dev *rt2x00dev)
121 {
122         struct eeprom_93cx6 eeprom;
123         u32 reg;
124
125         rt2x00mmio_register_read(rt2x00dev, E2PROM_CSR, &reg);
126
127         eeprom.data = rt2x00dev;
128         eeprom.register_read = rt2800pci_eepromregister_read;
129         eeprom.register_write = rt2800pci_eepromregister_write;
130         switch (rt2x00_get_field32(reg, E2PROM_CSR_TYPE))
131         {
132         case 0:
133                 eeprom.width = PCI_EEPROM_WIDTH_93C46;
134                 break;
135         case 1:
136                 eeprom.width = PCI_EEPROM_WIDTH_93C66;
137                 break;
138         default:
139                 eeprom.width = PCI_EEPROM_WIDTH_93C86;
140                 break;
141         }
142         eeprom.reg_data_in = 0;
143         eeprom.reg_data_out = 0;
144         eeprom.reg_data_clock = 0;
145         eeprom.reg_chip_select = 0;
146
147         eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
148                                EEPROM_SIZE / sizeof(u16));
149
150         return 0;
151 }
152
153 static int rt2800pci_efuse_detect(struct rt2x00_dev *rt2x00dev)
154 {
155         return rt2800_efuse_detect(rt2x00dev);
156 }
157
158 static inline int rt2800pci_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
159 {
160         return rt2800_read_eeprom_efuse(rt2x00dev);
161 }
162
163 /*
164  * Firmware functions
165  */
166 static char *rt2800pci_get_firmware_name(struct rt2x00_dev *rt2x00dev)
167 {
168         /*
169          * Chip rt3290 use specific 4KB firmware named /*(DEBLOBBED)*/
170         if (rt2x00_rt(rt2x00dev, RT3290))
171                 return FIRMWARE_RT3290;
172         else
173                 return FIRMWARE_RT2860;
174 }
175
176 static int rt2800pci_write_firmware(struct rt2x00_dev *rt2x00dev,
177                                     const u8 *data, const size_t len)
178 {
179         u32 reg;
180
181         /*
182          * enable Host program ram write selection
183          */
184         reg = 0;
185         rt2x00_set_field32(&reg, PBF_SYS_CTRL_HOST_RAM_WRITE, 1);
186         rt2x00mmio_register_write(rt2x00dev, PBF_SYS_CTRL, reg);
187
188         /*
189          * Write firmware to device.
190          */
191         rt2x00mmio_register_multiwrite(rt2x00dev, FIRMWARE_IMAGE_BASE,
192                                        data, len);
193
194         rt2x00mmio_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000);
195         rt2x00mmio_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00001);
196
197         rt2x00mmio_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
198         rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
199
200         return 0;
201 }
202
203 /*
204  * Device state switch handlers.
205  */
206 static int rt2800pci_enable_radio(struct rt2x00_dev *rt2x00dev)
207 {
208         int retval;
209
210         retval = rt2800mmio_enable_radio(rt2x00dev);
211         if (retval)
212                 return retval;
213
214         /* After resume MCU_BOOT_SIGNAL will trash these. */
215         rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_STATUS, ~0);
216         rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_CID, ~0);
217
218         rt2800_mcu_request(rt2x00dev, MCU_SLEEP, TOKEN_RADIO_OFF, 0xff, 0x02);
219         rt2800pci_mcu_status(rt2x00dev, TOKEN_RADIO_OFF);
220
221         rt2800_mcu_request(rt2x00dev, MCU_WAKEUP, TOKEN_WAKEUP, 0, 0);
222         rt2800pci_mcu_status(rt2x00dev, TOKEN_WAKEUP);
223
224         return retval;
225 }
226
227 static int rt2800pci_set_state(struct rt2x00_dev *rt2x00dev,
228                                enum dev_state state)
229 {
230         if (state == STATE_AWAKE) {
231                 rt2800_mcu_request(rt2x00dev, MCU_WAKEUP, TOKEN_WAKEUP,
232                                    0, 0x02);
233                 rt2800pci_mcu_status(rt2x00dev, TOKEN_WAKEUP);
234         } else if (state == STATE_SLEEP) {
235                 rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_STATUS,
236                                           0xffffffff);
237                 rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_CID,
238                                           0xffffffff);
239                 rt2800_mcu_request(rt2x00dev, MCU_SLEEP, TOKEN_SLEEP,
240                                    0xff, 0x01);
241         }
242
243         return 0;
244 }
245
246 static int rt2800pci_set_device_state(struct rt2x00_dev *rt2x00dev,
247                                       enum dev_state state)
248 {
249         int retval = 0;
250
251         switch (state) {
252         case STATE_RADIO_ON:
253                 retval = rt2800pci_enable_radio(rt2x00dev);
254                 break;
255         case STATE_RADIO_OFF:
256                 /*
257                  * After the radio has been disabled, the device should
258                  * be put to sleep for powersaving.
259                  */
260                 rt2800pci_set_state(rt2x00dev, STATE_SLEEP);
261                 break;
262         case STATE_RADIO_IRQ_ON:
263         case STATE_RADIO_IRQ_OFF:
264                 rt2800mmio_toggle_irq(rt2x00dev, state);
265                 break;
266         case STATE_DEEP_SLEEP:
267         case STATE_SLEEP:
268         case STATE_STANDBY:
269         case STATE_AWAKE:
270                 retval = rt2800pci_set_state(rt2x00dev, state);
271                 break;
272         default:
273                 retval = -ENOTSUPP;
274                 break;
275         }
276
277         if (unlikely(retval))
278                 rt2x00_err(rt2x00dev, "Device failed to enter state %d (%d)\n",
279                            state, retval);
280
281         return retval;
282 }
283
284 /*
285  * Device probe functions.
286  */
287 static int rt2800pci_read_eeprom(struct rt2x00_dev *rt2x00dev)
288 {
289         int retval;
290
291         if (rt2800pci_efuse_detect(rt2x00dev))
292                 retval = rt2800pci_read_eeprom_efuse(rt2x00dev);
293         else
294                 retval = rt2800pci_read_eeprom_pci(rt2x00dev);
295
296         return retval;
297 }
298
299 static const struct ieee80211_ops rt2800pci_mac80211_ops = {
300         .tx                     = rt2x00mac_tx,
301         .start                  = rt2x00mac_start,
302         .stop                   = rt2x00mac_stop,
303         .add_interface          = rt2x00mac_add_interface,
304         .remove_interface       = rt2x00mac_remove_interface,
305         .config                 = rt2x00mac_config,
306         .configure_filter       = rt2x00mac_configure_filter,
307         .set_key                = rt2x00mac_set_key,
308         .sw_scan_start          = rt2x00mac_sw_scan_start,
309         .sw_scan_complete       = rt2x00mac_sw_scan_complete,
310         .get_stats              = rt2x00mac_get_stats,
311         .get_key_seq            = rt2800_get_key_seq,
312         .set_rts_threshold      = rt2800_set_rts_threshold,
313         .sta_add                = rt2x00mac_sta_add,
314         .sta_remove             = rt2x00mac_sta_remove,
315         .bss_info_changed       = rt2x00mac_bss_info_changed,
316         .conf_tx                = rt2800_conf_tx,
317         .get_tsf                = rt2800_get_tsf,
318         .rfkill_poll            = rt2x00mac_rfkill_poll,
319         .ampdu_action           = rt2800_ampdu_action,
320         .flush                  = rt2x00mac_flush,
321         .get_survey             = rt2800_get_survey,
322         .get_ringparam          = rt2x00mac_get_ringparam,
323         .tx_frames_pending      = rt2x00mac_tx_frames_pending,
324 };
325
326 static const struct rt2800_ops rt2800pci_rt2800_ops = {
327         .register_read          = rt2x00mmio_register_read,
328         .register_read_lock     = rt2x00mmio_register_read, /* same for PCI */
329         .register_write         = rt2x00mmio_register_write,
330         .register_write_lock    = rt2x00mmio_register_write, /* same for PCI */
331         .register_multiread     = rt2x00mmio_register_multiread,
332         .register_multiwrite    = rt2x00mmio_register_multiwrite,
333         .regbusy_read           = rt2x00mmio_regbusy_read,
334         .read_eeprom            = rt2800pci_read_eeprom,
335         .hwcrypt_disabled       = rt2800pci_hwcrypt_disabled,
336         .drv_write_firmware     = rt2800pci_write_firmware,
337         .drv_init_registers     = rt2800mmio_init_registers,
338         .drv_get_txwi           = rt2800mmio_get_txwi,
339 };
340
341 static const struct rt2x00lib_ops rt2800pci_rt2x00_ops = {
342         .irq_handler            = rt2800mmio_interrupt,
343         .txstatus_tasklet       = rt2800mmio_txstatus_tasklet,
344         .pretbtt_tasklet        = rt2800mmio_pretbtt_tasklet,
345         .tbtt_tasklet           = rt2800mmio_tbtt_tasklet,
346         .rxdone_tasklet         = rt2800mmio_rxdone_tasklet,
347         .autowake_tasklet       = rt2800mmio_autowake_tasklet,
348         .probe_hw               = rt2800_probe_hw,
349         .get_firmware_name      = rt2800pci_get_firmware_name,
350         .check_firmware         = rt2800_check_firmware,
351         .load_firmware          = rt2800_load_firmware,
352         .initialize             = rt2x00mmio_initialize,
353         .uninitialize           = rt2x00mmio_uninitialize,
354         .get_entry_state        = rt2800mmio_get_entry_state,
355         .clear_entry            = rt2800mmio_clear_entry,
356         .set_device_state       = rt2800pci_set_device_state,
357         .rfkill_poll            = rt2800_rfkill_poll,
358         .link_stats             = rt2800_link_stats,
359         .reset_tuner            = rt2800_reset_tuner,
360         .link_tuner             = rt2800_link_tuner,
361         .gain_calibration       = rt2800_gain_calibration,
362         .vco_calibration        = rt2800_vco_calibration,
363         .start_queue            = rt2800mmio_start_queue,
364         .kick_queue             = rt2800mmio_kick_queue,
365         .stop_queue             = rt2800mmio_stop_queue,
366         .flush_queue            = rt2x00mmio_flush_queue,
367         .write_tx_desc          = rt2800mmio_write_tx_desc,
368         .write_tx_data          = rt2800_write_tx_data,
369         .write_beacon           = rt2800_write_beacon,
370         .clear_beacon           = rt2800_clear_beacon,
371         .fill_rxdone            = rt2800mmio_fill_rxdone,
372         .config_shared_key      = rt2800_config_shared_key,
373         .config_pairwise_key    = rt2800_config_pairwise_key,
374         .config_filter          = rt2800_config_filter,
375         .config_intf            = rt2800_config_intf,
376         .config_erp             = rt2800_config_erp,
377         .config_ant             = rt2800_config_ant,
378         .config                 = rt2800_config,
379         .sta_add                = rt2800_sta_add,
380         .sta_remove             = rt2800_sta_remove,
381 };
382
383 static const struct rt2x00_ops rt2800pci_ops = {
384         .name                   = KBUILD_MODNAME,
385         .drv_data_size          = sizeof(struct rt2800_drv_data),
386         .max_ap_intf            = 8,
387         .eeprom_size            = EEPROM_SIZE,
388         .rf_size                = RF_SIZE,
389         .tx_queues              = NUM_TX_QUEUES,
390         .queue_init             = rt2800mmio_queue_init,
391         .lib                    = &rt2800pci_rt2x00_ops,
392         .drv                    = &rt2800pci_rt2800_ops,
393         .hw                     = &rt2800pci_mac80211_ops,
394 #ifdef CONFIG_RT2X00_LIB_DEBUGFS
395         .debugfs                = &rt2800_rt2x00debug,
396 #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
397 };
398
399 /*
400  * RT2800pci module information.
401  */
402 static const struct pci_device_id rt2800pci_device_table[] = {
403         { PCI_DEVICE(0x1814, 0x0601) },
404         { PCI_DEVICE(0x1814, 0x0681) },
405         { PCI_DEVICE(0x1814, 0x0701) },
406         { PCI_DEVICE(0x1814, 0x0781) },
407         { PCI_DEVICE(0x1814, 0x3090) },
408         { PCI_DEVICE(0x1814, 0x3091) },
409         { PCI_DEVICE(0x1814, 0x3092) },
410         { PCI_DEVICE(0x1432, 0x7708) },
411         { PCI_DEVICE(0x1432, 0x7727) },
412         { PCI_DEVICE(0x1432, 0x7728) },
413         { PCI_DEVICE(0x1432, 0x7738) },
414         { PCI_DEVICE(0x1432, 0x7748) },
415         { PCI_DEVICE(0x1432, 0x7758) },
416         { PCI_DEVICE(0x1432, 0x7768) },
417         { PCI_DEVICE(0x1462, 0x891a) },
418         { PCI_DEVICE(0x1a3b, 0x1059) },
419 #ifdef CONFIG_RT2800PCI_RT3290
420         { PCI_DEVICE(0x1814, 0x3290) },
421 #endif
422 #ifdef CONFIG_RT2800PCI_RT33XX
423         { PCI_DEVICE(0x1814, 0x3390) },
424 #endif
425 #ifdef CONFIG_RT2800PCI_RT35XX
426         { PCI_DEVICE(0x1432, 0x7711) },
427         { PCI_DEVICE(0x1432, 0x7722) },
428         { PCI_DEVICE(0x1814, 0x3060) },
429         { PCI_DEVICE(0x1814, 0x3062) },
430         { PCI_DEVICE(0x1814, 0x3562) },
431         { PCI_DEVICE(0x1814, 0x3592) },
432         { PCI_DEVICE(0x1814, 0x3593) },
433         { PCI_DEVICE(0x1814, 0x359f) },
434 #endif
435 #ifdef CONFIG_RT2800PCI_RT53XX
436         { PCI_DEVICE(0x1814, 0x5360) },
437         { PCI_DEVICE(0x1814, 0x5362) },
438         { PCI_DEVICE(0x1814, 0x5390) },
439         { PCI_DEVICE(0x1814, 0x5392) },
440         { PCI_DEVICE(0x1814, 0x539a) },
441         { PCI_DEVICE(0x1814, 0x539b) },
442         { PCI_DEVICE(0x1814, 0x539f) },
443 #endif
444         { 0, }
445 };
446
447 MODULE_AUTHOR(DRV_PROJECT);
448 MODULE_VERSION(DRV_VERSION);
449 MODULE_DESCRIPTION("Ralink RT2800 PCI & PCMCIA Wireless LAN driver.");
450 MODULE_SUPPORTED_DEVICE("Ralink RT2860 PCI & PCMCIA chipset based cards");
451 /*(DEBLOBBED)*/
452 MODULE_DEVICE_TABLE(pci, rt2800pci_device_table);
453 MODULE_LICENSE("GPL");
454
455 static int rt2800pci_probe(struct pci_dev *pci_dev,
456                            const struct pci_device_id *id)
457 {
458         return rt2x00pci_probe(pci_dev, &rt2800pci_ops);
459 }
460
461 static struct pci_driver rt2800pci_driver = {
462         .name           = KBUILD_MODNAME,
463         .id_table       = rt2800pci_device_table,
464         .probe          = rt2800pci_probe,
465         .remove         = rt2x00pci_remove,
466         .suspend        = rt2x00pci_suspend,
467         .resume         = rt2x00pci_resume,
468 };
469
470 module_pci_driver(rt2800pci_driver);