2
*************************************************************************
4
* 5F., No.36, Taiyuan St., Jhubei City,
8
* (c) Copyright 2002-2007, Ralink Technology, Inc.
10
* This program is free software; you can redistribute it and/or modify *
11
* it under the terms of the GNU General Public License as published by *
12
* the Free Software Foundation; either version 2 of the License, or *
13
* (at your option) any later version. *
15
* This program is distributed in the hope that it will be useful, *
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18
* GNU General Public License for more details. *
20
* You should have received a copy of the GNU General Public License *
21
* along with this program; if not, write to the *
22
* Free Software Foundation, Inc., *
23
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25
*************************************************************************
31
Create and register network interface for PCI based chipsets in Linux platform.
35
Justin P. Mattock 11/07/2010 Fix typos in some comments
36
-------- ---------- ----------------------------------------------
39
#include "rt_config.h"
40
#include <linux/pci.h>
41
#include <linux/slab.h>
43
/* Following information will be show when you run 'modinfo' */
44
/* If you have a solution for a bug in current version of driver, please e-mail me. */
45
/* Otherwise post to forum in ralinktech's web site(www.ralinktech.com) and let all users help you. */
46
MODULE_AUTHOR("Jett Chen <jett_chen@ralinktech.com>");
47
MODULE_DESCRIPTION("RT2860/RT3090 Wireless Lan Linux Driver");
48
MODULE_LICENSE("GPL");
49
MODULE_ALIAS("rt3090sta");
52
/* Function declarations */
54
static void __devexit rt2860_remove_one(struct pci_dev *pci_dev);
55
static int __devinit rt2860_probe(struct pci_dev *pci_dev,
56
const struct pci_device_id *ent);
57
static void __exit rt2860_cleanup_module(void);
58
static int __init rt2860_init_module(void);
60
static void RTMPInitPCIeDevice(IN struct pci_dev *pci_dev,
61
struct rt_rtmp_adapter *pAd);
64
static int rt2860_suspend(struct pci_dev *pci_dev, pm_message_t state);
65
static int rt2860_resume(struct pci_dev *pci_dev);
66
#endif /* CONFIG_PM // */
69
/* Ralink PCI device table, include all supported chipsets */
71
static struct pci_device_id rt2860_pci_tbl[] __devinitdata = {
73
{PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2860_PCI_DEVICE_ID)}, /*RT28602.4G */
74
{PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2860_PCIe_DEVICE_ID)},
75
{PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2760_PCI_DEVICE_ID)},
76
{PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2790_PCIe_DEVICE_ID)},
77
{PCI_DEVICE(VEN_AWT_PCI_VENDOR_ID, VEN_AWT_PCIe_DEVICE_ID)},
78
{PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7708)},
79
{PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7728)},
80
{PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7758)},
81
{PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7727)},
82
{PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7738)},
83
{PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7748)},
84
{PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7768)},
87
{PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC3090_PCIe_DEVICE_ID)},
88
{PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC3091_PCIe_DEVICE_ID)},
89
{PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC3092_PCIe_DEVICE_ID)},
90
#endif /* RT3090 // */
92
{PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC3390_PCIe_DEVICE_ID)},
93
{PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC3391_PCIe_DEVICE_ID)},
94
{PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC3392_PCIe_DEVICE_ID)},
95
#endif /* RT3390 // */
96
{0,} /* terminate list */
99
MODULE_DEVICE_TABLE(pci, rt2860_pci_tbl);
100
#ifdef MODULE_VERSION
101
MODULE_VERSION(STA_DRIVER_VERSION);
105
/* Our PCI driver structure */
107
static struct pci_driver rt2860_driver = {
109
id_table : rt2860_pci_tbl,
110
probe : rt2860_probe,
111
remove : __devexit_p(rt2860_remove_one),
113
suspend : rt2860_suspend,
114
resume : rt2860_resume,
118
/***************************************************************************
120
* PCI device initialization related procedures.
122
***************************************************************************/
125
void RT2860RejectPendingPackets(struct rt_rtmp_adapter *pAd)
127
/* clear PS packets */
128
/* clear TxSw packets */
131
static int rt2860_suspend(struct pci_dev *pci_dev, pm_message_t state)
133
struct net_device *net_dev = pci_get_drvdata(pci_dev);
134
struct rt_rtmp_adapter *pAd = (struct rt_rtmp_adapter *)NULL;
137
DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_suspend()\n"));
139
if (net_dev == NULL) {
140
DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
142
GET_PAD_FROM_NET_DEV(pAd, net_dev);
144
/* we can not use IFF_UP because ra0 down but ra1 up */
145
/* and 1 suspend/resume function for 1 module, not for each interface */
146
/* so Linux will call suspend/resume function once */
147
if (VIRTUAL_IF_NUM(pAd) > 0) {
148
/* avoid users do suspend after interface is down */
151
netif_carrier_off(net_dev);
152
netif_stop_queue(net_dev);
154
/* mark device as removed from system and therefore no longer available */
155
netif_device_detach(net_dev);
158
RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
159
RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
161
/* take down the device */
162
rt28xx_close((struct net_device *)net_dev);
164
RT_MOD_DEC_USE_COUNT();
168
/* reference to http://vovo2000.com/type-lab/linux/kernel-api/linux-kernel-api.html */
169
/* enable device to generate PME# when suspended */
170
/* pci_choose_state(): Choose the power state of a PCI device to be suspended */
171
retval = pci_enable_wake(pci_dev, pci_choose_state(pci_dev, state), 1);
172
/* save the PCI configuration space of a device before suspending */
173
pci_save_state(pci_dev);
174
/* disable PCI device after use */
175
pci_disable_device(pci_dev);
177
retval = pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
179
DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_suspend()\n"));
183
static int rt2860_resume(struct pci_dev *pci_dev)
185
struct net_device *net_dev = pci_get_drvdata(pci_dev);
186
struct rt_rtmp_adapter *pAd = (struct rt_rtmp_adapter *)NULL;
189
/* set the power state of a PCI device */
190
/* PCI has 4 power states, DO (normal) ~ D3(less power) */
191
/* in include/linux/pci.h, you can find that */
192
/* #define PCI_D0 ((pci_power_t __force) 0) */
193
/* #define PCI_D1 ((pci_power_t __force) 1) */
194
/* #define PCI_D2 ((pci_power_t __force) 2) */
195
/* #define PCI_D3hot ((pci_power_t __force) 3) */
196
/* #define PCI_D3cold ((pci_power_t __force) 4) */
197
/* #define PCI_UNKNOWN ((pci_power_t __force) 5) */
198
/* #define PCI_POWER_ERROR ((pci_power_t __force) -1) */
199
retval = pci_set_power_state(pci_dev, PCI_D0);
201
/* restore the saved state of a PCI device */
202
pci_restore_state(pci_dev);
204
/* initialize device before it's used by a driver */
205
if (pci_enable_device(pci_dev)) {
206
printk(KERN_ERR "rt2860: pci enable fail!\n");
210
DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_resume()\n"));
213
DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
215
GET_PAD_FROM_NET_DEV(pAd, net_dev);
218
/* we can not use IFF_UP because ra0 down but ra1 up */
219
/* and 1 suspend/resume function for 1 module, not for each interface */
220
/* so Linux will call suspend/resume function once */
221
if (VIRTUAL_IF_NUM(pAd) > 0) {
222
/* mark device as attached from system and restart if needed */
223
netif_device_attach(net_dev);
225
if (rt28xx_open((struct net_device *)net_dev) != 0) {
227
DBGPRINT(RT_DEBUG_TRACE,
228
("<=== rt2860_resume()\n"));
231
/* increase MODULE use count */
232
RT_MOD_INC_USE_COUNT();
234
RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
235
RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
237
netif_start_queue(net_dev);
238
netif_carrier_on(net_dev);
239
netif_wake_queue(net_dev);
243
DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_resume()\n"));
246
#endif /* CONFIG_PM // */
248
static int __init rt2860_init_module(void)
250
return pci_register_driver(&rt2860_driver);
254
/* Driver module unload function */
256
static void __exit rt2860_cleanup_module(void)
258
pci_unregister_driver(&rt2860_driver);
261
module_init(rt2860_init_module);
262
module_exit(rt2860_cleanup_module);
265
/* PCI device probe & initialization function */
267
static int __devinit rt2860_probe(IN struct pci_dev *pci_dev,
268
IN const struct pci_device_id *pci_id)
270
struct rt_rtmp_adapter *pAd = (struct rt_rtmp_adapter *)NULL;
271
struct net_device *net_dev;
274
unsigned long csr_addr;
276
struct rt_rtmp_os_netdev_op_hook netDevHook;
278
DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_probe\n"));
280
/*PCIDevInit============================================== */
281
/* wake up and enable device */
282
rv = pci_enable_device(pci_dev);
285
DBGPRINT(RT_DEBUG_ERROR,
286
("Enable PCI device failed, errno=%d!\n", rv));
290
print_name = (char *)pci_name(pci_dev);
292
rv = pci_request_regions(pci_dev, print_name);
295
DBGPRINT(RT_DEBUG_ERROR,
296
("Request PCI resource failed, errno=%d!\n", rv));
299
/* map physical address to virtual address for accessing register */
301
(unsigned long)ioremap(pci_resource_start(pci_dev, 0),
302
pci_resource_len(pci_dev, 0));
304
DBGPRINT(RT_DEBUG_ERROR,
305
("ioremap failed for device %s, region 0x%lX @ 0x%lX\n",
306
print_name, (unsigned long)pci_resource_len(pci_dev, 0),
307
(unsigned long)pci_resource_start(pci_dev, 0)));
308
goto err_out_free_res;
310
DBGPRINT(RT_DEBUG_TRACE,
311
("%s: at 0x%lx, VA 0x%lx, IRQ %d. \n", print_name,
312
(unsigned long)pci_resource_start(pci_dev, 0),
313
(unsigned long)csr_addr, pci_dev->irq));
317
pci_set_master(pci_dev);
319
/*RtmpDevInit============================================== */
320
/* Allocate struct rt_rtmp_adapter adapter structure */
321
handle = kmalloc(sizeof(struct os_cookie), GFP_KERNEL);
322
if (handle == NULL) {
323
DBGPRINT(RT_DEBUG_ERROR,
324
("%s(): Allocate memory for os handle failed!\n",
326
goto err_out_iounmap;
329
((struct os_cookie *)handle)->pci_dev = pci_dev;
331
rv = RTMPAllocAdapterBlock(handle, &pAd); /*shiang: we may need the pci_dev for allocate structure of "struct rt_rtmp_adapter" */
332
if (rv != NDIS_STATUS_SUCCESS)
333
goto err_out_iounmap;
334
/* Here are the struct rt_rtmp_adapter structure with pci-bus specific parameters. */
335
pAd->CSRBaseAddress = (u8 *)csr_addr;
336
DBGPRINT(RT_DEBUG_ERROR,
337
("pAd->CSRBaseAddress =0x%lx, csr_addr=0x%lx!\n",
338
(unsigned long)pAd->CSRBaseAddress, csr_addr));
339
RtmpRaDevCtrlInit(pAd, RTMP_DEV_INF_PCI);
341
/*NetDevInit============================================== */
342
net_dev = RtmpPhyNetDevInit(pAd, &netDevHook);
344
goto err_out_free_radev;
346
/* Here are the net_device structure with pci-bus specific parameters. */
347
net_dev->irq = pci_dev->irq; /* Interrupt IRQ number */
348
net_dev->base_addr = csr_addr; /* Save CSR virtual address and irq to device structure */
349
pci_set_drvdata(pci_dev, net_dev); /* Set driver data */
351
/* for supporting Network Manager */
352
/* Set the sysfs physical device reference for the network logical device
353
* if set prior to registration will cause a symlink during initialization.
355
SET_NETDEV_DEV(net_dev, &(pci_dev->dev));
357
/*All done, it's time to register the net device to linux kernel. */
358
/* Register this device */
359
rv = RtmpOSNetDevAttach(net_dev, &netDevHook);
361
goto err_out_free_netdev;
363
pAd->StaCfg.OriDevType = net_dev->type;
364
RTMPInitPCIeDevice(pci_dev, pAd);
366
DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_probe\n"));
368
return 0; /* probe ok */
370
/* --------------------------- ERROR HANDLE --------------------------- */
372
RtmpOSNetDevFree(net_dev);
375
/* free struct rt_rtmp_adapter strcuture and os_cookie */
376
RTMPFreeAdapter(pAd);
379
iounmap((void *)(csr_addr));
380
release_mem_region(pci_resource_start(pci_dev, 0),
381
pci_resource_len(pci_dev, 0));
384
pci_release_regions(pci_dev);
387
pci_disable_device(pci_dev);
389
DBGPRINT(RT_DEBUG_ERROR,
390
("<=== rt2860_probe failed with rv = %d!\n", rv));
392
return -ENODEV; /* probe fail */
395
static void __devexit rt2860_remove_one(IN struct pci_dev *pci_dev)
397
struct net_device *net_dev = pci_get_drvdata(pci_dev);
398
struct rt_rtmp_adapter *pAd = NULL;
399
unsigned long csr_addr = net_dev->base_addr; /* pAd->CSRBaseAddress; */
401
GET_PAD_FROM_NET_DEV(pAd, net_dev);
403
DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_remove_one\n"));
406
/* Unregister/Free all allocated net_device. */
407
RtmpPhyNetDevExit(pAd, net_dev);
409
/* Unmap CSR base address */
410
iounmap((char *)(csr_addr));
412
/* release memory region */
413
release_mem_region(pci_resource_start(pci_dev, 0),
414
pci_resource_len(pci_dev, 0));
416
/* Free struct rt_rtmp_adapter related structures. */
417
RtmpRaDevCtrlExit(pAd);
420
/* Unregister network device */
421
RtmpOSNetDevDetach(net_dev);
423
/* Unmap CSR base address */
424
iounmap((char *)(net_dev->base_addr));
426
/* release memory region */
427
release_mem_region(pci_resource_start(pci_dev, 0),
428
pci_resource_len(pci_dev, 0));
431
/* Free the root net_device */
432
RtmpOSNetDevFree(net_dev);
437
========================================================================
439
Check the chipset vendor/product ID.
442
_dev_p Point to the PCI or USB device
449
========================================================================
451
BOOLEAN RT28XXChipsetCheck(IN void *_dev_p)
457
/***************************************************************************
459
* PCIe device initialization related procedures.
461
***************************************************************************/
462
static void RTMPInitPCIeDevice(struct pci_dev *pci_dev, struct rt_rtmp_adapter *pAd)
465
struct os_cookie *pObj;
467
pObj = (struct os_cookie *)pAd->OS_Cookie;
468
pci_read_config_word(pci_dev, PCI_DEVICE_ID, &device_id);
469
device_id = le2cpu16(device_id);
470
pObj->DeviceID = device_id;
473
(device_id == NIC2860_PCIe_DEVICE_ID) ||
474
(device_id == NIC2790_PCIe_DEVICE_ID) ||
475
(device_id == VEN_AWT_PCIe_DEVICE_ID) ||
478
(device_id == NIC3090_PCIe_DEVICE_ID) ||
479
(device_id == NIC3091_PCIe_DEVICE_ID) ||
480
(device_id == NIC3092_PCIe_DEVICE_ID) ||
481
#endif /* RT3090 // */
483
u32 MacCsr0 = 0, Index = 0;
485
RTMP_IO_READ32(pAd, MAC_CSR0, &MacCsr0);
487
if ((MacCsr0 != 0x00) && (MacCsr0 != 0xFFFFFFFF))
491
} while (Index++ < 100);
493
/* Support advanced power save after 2892/2790. */
494
/* MAC version at offset 0x1000 is 0x2872XXXX/0x2870XXXX(PCIe, USB, SDIO). */
495
if ((MacCsr0 & 0xffff0000) != 0x28600000)
496
OPSTATUS_SET_FLAG(pAd, fOP_STATUS_PCIE_DEVICE);
500
void RTMPInitPCIeLinkCtrlValue(struct rt_rtmp_adapter *pAd)
503
u16 reg16, data2, PCIePowerSaveLevel, Configuration;
505
BOOLEAN bFindIntel = FALSE;
506
struct os_cookie *pObj;
508
pObj = (struct os_cookie *)pAd->OS_Cookie;
510
if (!OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_PCIE_DEVICE))
513
DBGPRINT(RT_DEBUG_TRACE, ("%s.===>\n", __func__));
514
/* Init EEPROM, and save settings */
515
if (!(IS_RT3090(pAd) || IS_RT3572(pAd) || IS_RT3390(pAd))) {
516
RT28xx_EEPROM_READ16(pAd, 0x22, PCIePowerSaveLevel);
517
pAd->PCIePowerSaveLevel = PCIePowerSaveLevel & 0xff;
519
pAd->LnkCtrlBitMask = 0;
520
if ((PCIePowerSaveLevel & 0xff) == 0xff) {
521
OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_PCIE_DEVICE);
522
DBGPRINT(RT_DEBUG_TRACE,
523
("====> PCIePowerSaveLevel = 0x%x.\n",
524
PCIePowerSaveLevel));
527
PCIePowerSaveLevel &= 0x3;
528
RT28xx_EEPROM_READ16(pAd, 0x24, data2);
531
(((data2 & 0xff00) == 0x9200)
532
&& ((data2 & 0x80) != 0))) {
533
if (PCIePowerSaveLevel > 1)
534
PCIePowerSaveLevel = 1;
537
DBGPRINT(RT_DEBUG_TRACE,
538
("====> Write 0x83 = 0x%x.\n",
539
PCIePowerSaveLevel));
540
AsicSendCommandToMcu(pAd, 0x83, 0xff,
541
(u8)PCIePowerSaveLevel, 0x00);
542
RT28xx_EEPROM_READ16(pAd, 0x22, PCIePowerSaveLevel);
543
PCIePowerSaveLevel &= 0xff;
544
PCIePowerSaveLevel = PCIePowerSaveLevel >> 6;
545
switch (PCIePowerSaveLevel) {
546
case 0: /* Only support L0 */
547
pAd->LnkCtrlBitMask = 0;
549
case 1: /* Only enable L0s */
550
pAd->LnkCtrlBitMask = 1;
552
case 2: /* enable L1, L0s */
553
pAd->LnkCtrlBitMask = 3;
555
case 3: /* sync with host clk and enable L1, L0s */
556
pAd->LnkCtrlBitMask = 0x103;
559
RT28xx_EEPROM_READ16(pAd, 0x24, data2);
560
if ((PCIePowerSaveLevel & 0xff) != 0xff) {
561
PCIePowerSaveLevel &= 0x3;
564
(((data2 & 0xff00) == 0x9200)
565
&& ((data2 & 0x80) != 0))) {
566
if (PCIePowerSaveLevel > 1)
567
PCIePowerSaveLevel = 1;
570
DBGPRINT(RT_DEBUG_TRACE,
571
("====> rt28xx Write 0x83 Command = 0x%x.\n",
572
PCIePowerSaveLevel));
574
AsicSendCommandToMcu(pAd, 0x83, 0xff,
575
(u8)PCIePowerSaveLevel,
578
DBGPRINT(RT_DEBUG_TRACE,
579
("====> LnkCtrlBitMask = 0x%x.\n",
580
pAd->LnkCtrlBitMask));
582
} else if (IS_RT3090(pAd) || IS_RT3572(pAd) || IS_RT3390(pAd)) {
583
u8 LinkCtrlSetting = 0;
585
/* Check 3090E special setting chip. */
586
RT28xx_EEPROM_READ16(pAd, 0x24, data2);
587
if ((data2 == 0x9280) && ((pAd->MACVersion & 0xffff) == 0x0211)) {
588
pAd->b3090ESpecialChip = TRUE;
589
DBGPRINT_RAW(RT_DEBUG_ERROR, ("Special 3090E chip \n"));
592
RTMP_IO_READ32(pAd, AUX_CTRL, &MacValue);
593
/*enable WAKE_PCIE function, which forces to enable PCIE clock when mpu interrupt asserting. */
594
/*Force PCIE 125MHz CLK to toggle */
596
RTMP_IO_WRITE32(pAd, AUX_CTRL, MacValue);
597
DBGPRINT_RAW(RT_DEBUG_ERROR,
598
(" AUX_CTRL = 0x%32x\n", MacValue));
600
/* for RT30xx F and after, PCIe interface, and for power solution 3 */
601
if ((IS_VERSION_AFTER_F(pAd))
602
&& (pAd->StaCfg.PSControl.field.rt30xxPowerMode >= 2)
603
&& (pAd->StaCfg.PSControl.field.rt30xxPowerMode <= 3)) {
604
RTMP_IO_READ32(pAd, AUX_CTRL, &MacValue);
605
DBGPRINT_RAW(RT_DEBUG_ERROR,
606
(" Read AUX_CTRL = 0x%x\n", MacValue));
607
/* turn on bit 12. */
608
/*enable 32KHz clock mode for power saving */
610
if (MacValue != 0xffffffff) {
611
RTMP_IO_WRITE32(pAd, AUX_CTRL, MacValue);
612
DBGPRINT_RAW(RT_DEBUG_ERROR,
613
(" Write AUX_CTRL = 0x%x\n",
615
/* 1. if use PCIePowerSetting is 2 or 3, need to program OSC_CTRL to 0x3ff11. */
617
RTMP_IO_WRITE32(pAd, OSC_CTRL, MacValue);
618
DBGPRINT_RAW(RT_DEBUG_ERROR,
619
(" OSC_CTRL = 0x%x\n", MacValue));
620
/* 2. Write PCI register Clk ref bit */
621
RTMPrt3xSetPCIePowerLinkCtrl(pAd);
623
/* Error read Aux_Ctrl value. Force to use solution 1 */
624
DBGPRINT(RT_DEBUG_ERROR,
625
(" Error Value in AUX_CTRL = 0x%x\n",
627
pAd->StaCfg.PSControl.field.rt30xxPowerMode = 1;
628
DBGPRINT(RT_DEBUG_ERROR,
629
(" Force to use power solution1 \n"));
632
/* 1. read setting from inf file. */
635
(u16)pAd->StaCfg.PSControl.field.rt30xxPowerMode;
636
DBGPRINT(RT_DEBUG_ERROR,
637
("====> rt30xx Read PowerLevelMode = 0x%x.\n",
638
PCIePowerSaveLevel));
639
/* 2. Check EnableNewPS. */
640
if (pAd->StaCfg.PSControl.field.EnableNewPS == FALSE)
641
PCIePowerSaveLevel = 1;
643
if (IS_VERSION_BEFORE_F(pAd)
644
&& (pAd->b3090ESpecialChip == FALSE)) {
645
/* Chip Version E only allow 1, So force set 1. */
646
PCIePowerSaveLevel &= 0x1;
647
pAd->PCIePowerSaveLevel = (u16)PCIePowerSaveLevel;
648
DBGPRINT(RT_DEBUG_TRACE,
649
("====> rt30xx E Write 0x83 Command = 0x%x.\n",
650
PCIePowerSaveLevel));
652
AsicSendCommandToMcu(pAd, 0x83, 0xff,
653
(u8)PCIePowerSaveLevel, 0x00);
655
/* Chip Version F and after only allow 1 or 2 or 3. This might be modified after new chip version come out. */
657
((PCIePowerSaveLevel == 1)
658
|| (PCIePowerSaveLevel == 3)))
659
PCIePowerSaveLevel = 1;
660
DBGPRINT(RT_DEBUG_ERROR,
661
("====> rt30xx F Write 0x83 Command = 0x%x.\n",
662
PCIePowerSaveLevel));
663
pAd->PCIePowerSaveLevel = (u16)PCIePowerSaveLevel;
664
/* for 3090F , we need to add high-byte arg for 0x83 command to indicate the link control setting in */
665
/* PCI Configuration Space. Because firmware can't read PCI Configuration Space */
666
if ((pAd->Rt3xxRalinkLinkCtrl & 0x2)
667
&& (pAd->Rt3xxHostLinkCtrl & 0x2)) {
670
DBGPRINT(RT_DEBUG_TRACE,
671
("====> rt30xxF LinkCtrlSetting = 0x%x.\n",
673
AsicSendCommandToMcu(pAd, 0x83, 0xff,
674
(u8)PCIePowerSaveLevel,
678
/* Find Ralink PCIe Device's Express Capability Offset */
679
pos = pci_find_capability(pObj->pci_dev, PCI_CAP_ID_EXP);
682
/* Ralink PCIe Device's Link Control Register Offset */
683
pAd->RLnkCtrlOffset = pos + PCI_EXP_LNKCTL;
684
pci_read_config_word(pObj->pci_dev, pAd->RLnkCtrlOffset,
686
Configuration = le2cpu16(reg16);
687
DBGPRINT(RT_DEBUG_TRACE,
688
("Read (Ralink PCIe Link Control Register) offset 0x%x = 0x%x\n",
689
pAd->RLnkCtrlOffset, Configuration));
690
pAd->RLnkCtrlConfiguration = (Configuration & 0x103);
691
Configuration &= 0xfefc;
692
Configuration |= (0x0);
694
if ((pObj->DeviceID == NIC2860_PCIe_DEVICE_ID)
695
|| (pObj->DeviceID == NIC2790_PCIe_DEVICE_ID)) {
696
reg16 = cpu2le16(Configuration);
697
pci_write_config_word(pObj->pci_dev,
698
pAd->RLnkCtrlOffset, reg16);
699
DBGPRINT(RT_DEBUG_TRACE,
700
("Write (Ralink PCIe Link Control Register) offset 0x%x = 0x%x\n",
701
pos + PCI_EXP_LNKCTL, Configuration));
703
#endif /* RT2860 // */
705
RTMPFindHostPCIDev(pAd);
706
if (pObj->parent_pci_dev) {
709
pci_read_config_word(pObj->parent_pci_dev,
710
PCI_VENDOR_ID, &vendor_id);
711
vendor_id = le2cpu16(vendor_id);
712
if (vendor_id == PCIBUS_INTEL_VENDOR) {
714
RTMP_SET_PSFLAG(pAd, fRTMP_PS_TOGGLE_L1);
716
/* Find PCI-to-PCI Bridge Express Capability Offset */
718
pci_find_capability(pObj->parent_pci_dev,
722
BOOLEAN bChange = FALSE;
723
/* PCI-to-PCI Bridge Link Control Register Offset */
724
pAd->HostLnkCtrlOffset = pos + PCI_EXP_LNKCTL;
725
pci_read_config_word(pObj->parent_pci_dev,
726
pAd->HostLnkCtrlOffset,
728
Configuration = le2cpu16(reg16);
729
DBGPRINT(RT_DEBUG_TRACE,
730
("Read (Host PCI-to-PCI Bridge Link Control Register) offset 0x%x = 0x%x\n",
731
pAd->HostLnkCtrlOffset,
733
pAd->HostLnkCtrlConfiguration =
734
(Configuration & 0x103);
735
Configuration &= 0xfefc;
736
Configuration |= (0x0);
738
switch (pObj->DeviceID) {
740
case NIC2860_PCIe_DEVICE_ID:
741
case NIC2790_PCIe_DEVICE_ID:
744
#endif /* RT2860 // */
746
case NIC3090_PCIe_DEVICE_ID:
747
case NIC3091_PCIe_DEVICE_ID:
748
case NIC3092_PCIe_DEVICE_ID:
749
if (bFindIntel == FALSE)
752
#endif /* RT3090 // */
758
reg16 = cpu2le16(Configuration);
759
pci_write_config_word(pObj->
764
DBGPRINT(RT_DEBUG_TRACE,
765
("Write (Host PCI-to-PCI Bridge Link Control Register) offset 0x%x = 0x%x\n",
766
pAd->HostLnkCtrlOffset,
770
pAd->HostLnkCtrlOffset = 0;
771
DBGPRINT(RT_DEBUG_ERROR,
772
("%s: cannot find PCI-to-PCI Bridge PCI Express Capability!\n",
777
pAd->RLnkCtrlOffset = 0;
778
pAd->HostLnkCtrlOffset = 0;
779
DBGPRINT(RT_DEBUG_ERROR,
780
("%s: cannot find Ralink PCIe Device's PCI Express Capability!\n",
784
if (bFindIntel == FALSE) {
785
DBGPRINT(RT_DEBUG_TRACE,
786
("Doesn't find Intel PCI host controller. \n"));
787
/* Doesn't switch L0, L1, So set PCIePowerSaveLevel to 0xff */
788
pAd->PCIePowerSaveLevel = 0xff;
789
if ((pAd->RLnkCtrlOffset != 0)
791
&& ((pObj->DeviceID == NIC3090_PCIe_DEVICE_ID)
792
|| (pObj->DeviceID == NIC3091_PCIe_DEVICE_ID)
793
|| (pObj->DeviceID == NIC3092_PCIe_DEVICE_ID))
794
#endif /* RT3090 // */
796
pci_read_config_word(pObj->pci_dev, pAd->RLnkCtrlOffset,
798
Configuration = le2cpu16(reg16);
799
DBGPRINT(RT_DEBUG_TRACE,
800
("Read (Ralink 30xx PCIe Link Control Register) offset 0x%x = 0x%x\n",
801
pAd->RLnkCtrlOffset, Configuration));
802
pAd->RLnkCtrlConfiguration = (Configuration & 0x103);
803
Configuration &= 0xfefc;
804
Configuration |= (0x0);
805
reg16 = cpu2le16(Configuration);
806
pci_write_config_word(pObj->pci_dev,
807
pAd->RLnkCtrlOffset, reg16);
808
DBGPRINT(RT_DEBUG_TRACE,
809
("Write (Ralink PCIe Link Control Register) offset 0x%x = 0x%x\n",
810
pos + PCI_EXP_LNKCTL, Configuration));
815
void RTMPFindHostPCIDev(struct rt_rtmp_adapter *pAd)
820
struct pci_dev *pPci_dev;
821
struct os_cookie *pObj;
823
pObj = (struct os_cookie *)pAd->OS_Cookie;
825
if (!OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_PCIE_DEVICE))
828
DBGPRINT(RT_DEBUG_TRACE, ("%s.===>\n", __func__));
830
pObj->parent_pci_dev = NULL;
831
if (pObj->pci_dev->bus->parent) {
832
for (DevFn = 0; DevFn < 255; DevFn++) {
834
pci_get_slot(pObj->pci_dev->bus->parent, DevFn);
836
pci_read_config_word(pPci_dev, PCI_CLASS_DEVICE,
838
reg16 = le2cpu16(reg16);
839
pci_read_config_byte(pPci_dev, PCI_CB_CARD_BUS,
841
if ((reg16 == PCI_CLASS_BRIDGE_PCI)
842
&& (reg8 == pObj->pci_dev->bus->number)) {
843
pObj->parent_pci_dev = pPci_dev;
851
========================================================================
856
Level = RESTORE_HALT : Restore PCI host and Ralink PCIe Link Control field to its default value.
857
Level = Other Value : Restore from dot11 power save or radio off status. And force PCI host Link Control fields to 0x1
859
========================================================================
861
void RTMPPCIeLinkCtrlValueRestore(struct rt_rtmp_adapter *pAd, u8 Level)
863
u16 PCIePowerSaveLevel, reg16;
865
struct os_cookie *pObj;
867
pObj = (struct os_cookie *)pAd->OS_Cookie;
869
if (!OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_PCIE_DEVICE))
873
if (!((pObj->DeviceID == NIC2860_PCIe_DEVICE_ID)
874
|| (pObj->DeviceID == NIC2790_PCIe_DEVICE_ID)))
876
#endif /* RT2860 // */
877
/* Check PSControl Configuration */
878
if (pAd->StaCfg.PSControl.field.EnableNewPS == FALSE)
881
/*3090 will not execute the following codes. */
882
/* Check interface : If not PCIe interface, return. */
885
if ((pObj->DeviceID == NIC3090_PCIe_DEVICE_ID)
886
|| (pObj->DeviceID == NIC3091_PCIe_DEVICE_ID)
887
|| (pObj->DeviceID == NIC3092_PCIe_DEVICE_ID))
889
#endif /* RT3090 // */
891
DBGPRINT(RT_DEBUG_TRACE, ("%s.===>\n", __func__));
892
PCIePowerSaveLevel = pAd->PCIePowerSaveLevel;
893
if ((PCIePowerSaveLevel & 0xff) == 0xff) {
894
DBGPRINT(RT_DEBUG_TRACE, ("return \n"));
898
if (pObj->parent_pci_dev && (pAd->HostLnkCtrlOffset != 0)) {
899
PCI_REG_READ_WORD(pObj->parent_pci_dev, pAd->HostLnkCtrlOffset,
901
if ((Configuration != 0) && (Configuration != 0xFFFF)) {
902
Configuration &= 0xfefc;
903
/* If call from interface down, restore to original setting. */
904
if (Level == RESTORE_CLOSE)
905
Configuration |= pAd->HostLnkCtrlConfiguration;
907
Configuration |= 0x0;
908
PCI_REG_WIRTE_WORD(pObj->parent_pci_dev,
909
pAd->HostLnkCtrlOffset,
911
DBGPRINT(RT_DEBUG_TRACE,
912
("Restore PCI host : offset 0x%x = 0x%x\n",
913
pAd->HostLnkCtrlOffset, Configuration));
915
DBGPRINT(RT_DEBUG_ERROR,
916
("Restore PCI host : PCI_REG_READ_WORD failed (Configuration = 0x%x)\n",
920
if (pObj->pci_dev && (pAd->RLnkCtrlOffset != 0)) {
921
PCI_REG_READ_WORD(pObj->pci_dev, pAd->RLnkCtrlOffset,
923
if ((Configuration != 0) && (Configuration != 0xFFFF)) {
924
Configuration &= 0xfefc;
925
/* If call from interface down, restore to original setting. */
926
if (Level == RESTORE_CLOSE)
927
Configuration |= pAd->RLnkCtrlConfiguration;
929
Configuration |= 0x0;
930
PCI_REG_WIRTE_WORD(pObj->pci_dev, pAd->RLnkCtrlOffset,
932
DBGPRINT(RT_DEBUG_TRACE,
933
("Restore Ralink : offset 0x%x = 0x%x\n",
934
pAd->RLnkCtrlOffset, Configuration));
936
DBGPRINT(RT_DEBUG_ERROR,
937
("Restore Ralink : PCI_REG_READ_WORD failed (Configuration = 0x%x)\n",
941
DBGPRINT(RT_DEBUG_TRACE, ("%s <===\n", __func__));
945
========================================================================
950
Max : limit Host PCI and Ralink PCIe device's LINK CONTROL field's value.
951
Because now frequently set our device to mode 1 or mode 3 will cause problem.
953
========================================================================
955
void RTMPPCIeLinkCtrlSetting(struct rt_rtmp_adapter *pAd, u16 Max)
957
u16 PCIePowerSaveLevel, reg16;
959
struct os_cookie *pObj;
961
pObj = (struct os_cookie *)pAd->OS_Cookie;
963
if (!OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_PCIE_DEVICE))
967
if (!((pObj->DeviceID == NIC2860_PCIe_DEVICE_ID)
968
|| (pObj->DeviceID == NIC2790_PCIe_DEVICE_ID)))
970
#endif /* RT2860 // */
971
/* Check PSControl Configuration */
972
if (pAd->StaCfg.PSControl.field.EnableNewPS == FALSE)
975
/* Check interface : If not PCIe interface, return. */
976
/*Block 3090 to enter the following function */
979
if ((pObj->DeviceID == NIC3090_PCIe_DEVICE_ID)
980
|| (pObj->DeviceID == NIC3091_PCIe_DEVICE_ID)
981
|| (pObj->DeviceID == NIC3092_PCIe_DEVICE_ID))
983
#endif /* RT3090 // */
984
if (!RTMP_TEST_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP)) {
985
DBGPRINT(RT_DEBUG_INFO,
986
("RTMPPCIePowerLinkCtrl return on fRTMP_PS_CAN_GO_SLEEP flag\n"));
990
DBGPRINT(RT_DEBUG_TRACE, ("%s===>\n", __func__));
991
PCIePowerSaveLevel = pAd->PCIePowerSaveLevel;
992
if ((PCIePowerSaveLevel & 0xff) == 0xff) {
993
DBGPRINT(RT_DEBUG_TRACE, ("return \n"));
996
PCIePowerSaveLevel = PCIePowerSaveLevel >> 6;
998
/* Skip non-exist deice right away */
999
if (pObj->parent_pci_dev && (pAd->HostLnkCtrlOffset != 0)) {
1000
PCI_REG_READ_WORD(pObj->parent_pci_dev, pAd->HostLnkCtrlOffset,
1002
switch (PCIePowerSaveLevel) {
1004
/* Set b0 and b1 of LinkControl (both 2892 and PCIe bridge) to 00 */
1005
Configuration &= 0xfefc;
1008
/* Set b0 and b1 of LinkControl (both 2892 and PCIe bridge) to 01 */
1009
Configuration &= 0xfefc;
1010
Configuration |= 0x1;
1013
/* Set b0 and b1 of LinkControl (both 2892 and PCIe bridge) to 11 */
1014
Configuration &= 0xfefc;
1015
Configuration |= 0x3;
1018
/* Set b0 and b1 of LinkControl (both 2892 and PCIe bridge) to 11 and bit 8 of LinkControl of 2892 to 1 */
1019
Configuration &= 0xfefc;
1020
Configuration |= 0x103;
1023
PCI_REG_WIRTE_WORD(pObj->parent_pci_dev, pAd->HostLnkCtrlOffset,
1025
DBGPRINT(RT_DEBUG_TRACE,
1026
("Write PCI host offset 0x%x = 0x%x\n",
1027
pAd->HostLnkCtrlOffset, Configuration));
1030
if (pObj->pci_dev && (pAd->RLnkCtrlOffset != 0)) {
1031
/* first 2892 chip not allow to frequently set mode 3. will cause hang problem. */
1032
if (PCIePowerSaveLevel > Max)
1033
PCIePowerSaveLevel = Max;
1035
PCI_REG_READ_WORD(pObj->pci_dev, pAd->RLnkCtrlOffset,
1037
switch (PCIePowerSaveLevel) {
1039
/* No PCI power safe */
1040
/* Set b0 and b1 of LinkControl (both 2892 and PCIe bridge) to 00 . */
1041
Configuration &= 0xfefc;
1045
/* Set b0 and b1 of LinkControl (both 2892 and PCIe bridge) to 01 . */
1046
Configuration &= 0xfefc;
1047
Configuration |= 0x1;
1051
/* Set b0 and b1 of LinkControl (both 2892 and PCIe bridge) to 11 */
1052
Configuration &= 0xfefc;
1053
Configuration |= 0x3;
1056
/* L0 , L1 and clock management. */
1057
/* Set b0 and b1 of LinkControl (both 2892 and PCIe bridge) to 11 and bit 8 of LinkControl of 2892 to 1 */
1058
Configuration &= 0xfefc;
1059
Configuration |= 0x103;
1060
pAd->bPCIclkOff = TRUE;
1063
PCI_REG_WIRTE_WORD(pObj->pci_dev, pAd->RLnkCtrlOffset,
1065
DBGPRINT(RT_DEBUG_TRACE,
1066
("Write Ralink device : offset 0x%x = 0x%x\n",
1067
pAd->RLnkCtrlOffset, Configuration));
1070
DBGPRINT(RT_DEBUG_TRACE, ("RTMPPCIePowerLinkCtrl <==============\n"));
1074
========================================================================
1076
Routine Description:
1077
1. Write a PCI register for rt30xx power solution 3
1079
========================================================================
1081
void RTMPrt3xSetPCIePowerLinkCtrl(struct rt_rtmp_adapter *pAd)
1084
unsigned long HostConfiguration = 0;
1085
unsigned long Configuration;
1086
struct os_cookie *pObj;
1090
pObj = (struct os_cookie *)pAd->OS_Cookie;
1092
DBGPRINT(RT_DEBUG_INFO,
1093
("RTMPrt3xSetPCIePowerLinkCtrl.===> %lx\n",
1094
pAd->StaCfg.PSControl.word));
1096
/* Check PSControl Configuration */
1097
if (pAd->StaCfg.PSControl.field.EnableNewPS == FALSE)
1099
RTMPFindHostPCIDev(pAd);
1100
if (pObj->parent_pci_dev) {
1101
/* Find PCI-to-PCI Bridge Express Capability Offset */
1102
pos = pci_find_capability(pObj->parent_pci_dev, PCI_CAP_ID_EXP);
1105
pAd->HostLnkCtrlOffset = pos + PCI_EXP_LNKCTL;
1107
/* If configured to turn on L1. */
1108
HostConfiguration = 0;
1109
if (pAd->StaCfg.PSControl.field.rt30xxForceASPMTest == 1) {
1110
DBGPRINT(RT_DEBUG_TRACE, ("Enter,PSM : Force ASPM\n"));
1112
/* Skip non-exist device right away */
1113
if ((pAd->HostLnkCtrlOffset != 0)) {
1114
PCI_REG_READ_WORD(pObj->parent_pci_dev,
1115
pAd->HostLnkCtrlOffset,
1117
/* Prepare Configuration to write to Host */
1118
HostConfiguration |= 0x3;
1119
PCI_REG_WIRTE_WORD(pObj->parent_pci_dev,
1120
pAd->HostLnkCtrlOffset,
1122
pAd->Rt3xxHostLinkCtrl = HostConfiguration;
1123
/* Because in rt30xxForceASPMTest Mode, Force turn on L0s, L1. */
1124
/* Fix HostConfiguration bit0:1 = 0x3 for later use. */
1125
HostConfiguration = 0x3;
1126
DBGPRINT(RT_DEBUG_TRACE,
1127
("PSM : Force ASPM : "
1128
"Host device L1/L0s Value = 0x%lx\n",
1129
HostConfiguration));
1131
} else if (pAd->StaCfg.PSControl.field.rt30xxFollowHostASPM ==
1134
/* Skip non-exist deice right away */
1135
if ((pAd->HostLnkCtrlOffset != 0)) {
1136
PCI_REG_READ_WORD(pObj->parent_pci_dev,
1137
pAd->HostLnkCtrlOffset,
1139
pAd->Rt3xxHostLinkCtrl = HostConfiguration;
1140
HostConfiguration &= 0x3;
1141
DBGPRINT(RT_DEBUG_TRACE,
1142
("PSM : Follow Host ASPM : "
1143
"Host device L1/L0s Value = 0x%lx\n",
1144
HostConfiguration));
1148
/* Prepare to write Ralink setting. */
1149
/* Find Ralink PCIe Device's Express Capability Offset */
1150
pos = pci_find_capability(pObj->pci_dev, PCI_CAP_ID_EXP);
1153
/* Ralink PCIe Device's Link Control Register Offset */
1154
pAd->RLnkCtrlOffset = pos + PCI_EXP_LNKCTL;
1155
pci_read_config_word(pObj->pci_dev, pAd->RLnkCtrlOffset,
1157
Configuration = le2cpu16(reg16);
1158
DBGPRINT(RT_DEBUG_TRACE,
1159
("Read (Ralink PCIe Link Control Register) "
1160
"offset 0x%x = 0x%lx\n",
1161
pAd->RLnkCtrlOffset, Configuration));
1162
Configuration |= 0x100;
1163
if ((pAd->StaCfg.PSControl.field.rt30xxFollowHostASPM == 1)
1164
|| (pAd->StaCfg.PSControl.field.rt30xxForceASPMTest == 1)) {
1165
switch (HostConfiguration) {
1167
Configuration &= 0xffffffc;
1170
Configuration &= 0xffffffc;
1171
Configuration |= 0x1;
1174
Configuration &= 0xffffffc;
1175
Configuration |= 0x2;
1178
Configuration |= 0x3;
1182
reg16 = cpu2le16(Configuration);
1183
pci_write_config_word(pObj->pci_dev, pAd->RLnkCtrlOffset,
1185
pAd->Rt3xxRalinkLinkCtrl = Configuration;
1186
DBGPRINT(RT_DEBUG_TRACE,
1187
("PSM :Write Ralink device L1/L0s Value = 0x%lx\n",
1190
DBGPRINT(RT_DEBUG_INFO,
1191
("PSM :RTMPrt3xSetPCIePowerLinkCtrl <==============\n"));