Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: ISC
0002 /*
0003  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
0004  */
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/module.h>
0008 #include <linux/pci.h>
0009 
0010 #include "mt76x2.h"
0011 
0012 static const struct pci_device_id mt76x2e_device_table[] = {
0013     { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7662) },
0014     { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7612) },
0015     { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7602) },
0016     { },
0017 };
0018 
0019 static int
0020 mt76x2e_probe(struct pci_dev *pdev, const struct pci_device_id *id)
0021 {
0022     static const struct mt76_driver_ops drv_ops = {
0023         .txwi_size = sizeof(struct mt76x02_txwi),
0024         .drv_flags = MT_DRV_TX_ALIGNED4_SKBS |
0025                  MT_DRV_SW_RX_AIRTIME,
0026         .survey_flags = SURVEY_INFO_TIME_TX,
0027         .update_survey = mt76x02_update_channel,
0028         .tx_prepare_skb = mt76x02_tx_prepare_skb,
0029         .tx_complete_skb = mt76x02_tx_complete_skb,
0030         .rx_skb = mt76x02_queue_rx_skb,
0031         .rx_poll_complete = mt76x02_rx_poll_complete,
0032         .sta_ps = mt76x02_sta_ps,
0033         .sta_add = mt76x02_sta_add,
0034         .sta_remove = mt76x02_sta_remove,
0035     };
0036     struct mt76x02_dev *dev;
0037     struct mt76_dev *mdev;
0038     int ret;
0039 
0040     ret = pcim_enable_device(pdev);
0041     if (ret)
0042         return ret;
0043 
0044     ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
0045     if (ret)
0046         return ret;
0047 
0048     pci_set_master(pdev);
0049 
0050     ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
0051     if (ret)
0052         return ret;
0053 
0054     mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt76x2_ops,
0055                  &drv_ops);
0056     if (!mdev)
0057         return -ENOMEM;
0058 
0059     dev = container_of(mdev, struct mt76x02_dev, mt76);
0060     mt76_mmio_init(mdev, pcim_iomap_table(pdev)[0]);
0061     mt76x2_reset_wlan(dev, false);
0062 
0063     mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
0064     dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
0065 
0066     mt76_wr(dev, MT_INT_MASK_CSR, 0);
0067 
0068     ret = devm_request_irq(mdev->dev, pdev->irq, mt76x02_irq_handler,
0069                    IRQF_SHARED, KBUILD_MODNAME, dev);
0070     if (ret)
0071         goto error;
0072 
0073     ret = mt76x2_register_device(dev);
0074     if (ret)
0075         goto error;
0076 
0077     /* Fix up ASPM configuration */
0078 
0079     /* RG_SSUSB_G1_CDR_BIR_LTR = 0x9 */
0080     mt76_rmw_field(dev, 0x15a10, 0x1f << 16, 0x9);
0081 
0082     /* RG_SSUSB_G1_CDR_BIC_LTR = 0xf */
0083     mt76_rmw_field(dev, 0x15a0c, 0xfU << 28, 0xf);
0084 
0085     /* RG_SSUSB_CDR_BR_PE1D = 0x3 */
0086     mt76_rmw_field(dev, 0x15c58, 0x3 << 6, 0x3);
0087 
0088     mt76_pci_disable_aspm(pdev);
0089 
0090     return 0;
0091 
0092 error:
0093     mt76_free_device(&dev->mt76);
0094 
0095     return ret;
0096 }
0097 
0098 static void
0099 mt76x2e_remove(struct pci_dev *pdev)
0100 {
0101     struct mt76_dev *mdev = pci_get_drvdata(pdev);
0102     struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
0103 
0104     mt76_unregister_device(mdev);
0105     mt76x2_cleanup(dev);
0106     mt76_free_device(mdev);
0107 }
0108 
0109 static int __maybe_unused
0110 mt76x2e_suspend(struct pci_dev *pdev, pm_message_t state)
0111 {
0112     struct mt76_dev *mdev = pci_get_drvdata(pdev);
0113     int i, err;
0114 
0115     napi_disable(&mdev->tx_napi);
0116     tasklet_kill(&mdev->pre_tbtt_tasklet);
0117     mt76_worker_disable(&mdev->tx_worker);
0118 
0119     mt76_for_each_q_rx(mdev, i)
0120         napi_disable(&mdev->napi[i]);
0121 
0122     pci_enable_wake(pdev, pci_choose_state(pdev, state), true);
0123     pci_save_state(pdev);
0124     err = pci_set_power_state(pdev, pci_choose_state(pdev, state));
0125     if (err)
0126         goto restore;
0127 
0128     return 0;
0129 
0130 restore:
0131     mt76_for_each_q_rx(mdev, i)
0132         napi_enable(&mdev->napi[i]);
0133     napi_enable(&mdev->tx_napi);
0134 
0135     return err;
0136 }
0137 
0138 static int __maybe_unused
0139 mt76x2e_resume(struct pci_dev *pdev)
0140 {
0141     struct mt76_dev *mdev = pci_get_drvdata(pdev);
0142     struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
0143     int i, err;
0144 
0145     err = pci_set_power_state(pdev, PCI_D0);
0146     if (err)
0147         return err;
0148 
0149     pci_restore_state(pdev);
0150 
0151     mt76_worker_enable(&mdev->tx_worker);
0152 
0153     local_bh_disable();
0154     mt76_for_each_q_rx(mdev, i) {
0155         napi_enable(&mdev->napi[i]);
0156         napi_schedule(&mdev->napi[i]);
0157     }
0158     napi_enable(&mdev->tx_napi);
0159     napi_schedule(&mdev->tx_napi);
0160     local_bh_enable();
0161 
0162     return mt76x2_resume_device(dev);
0163 }
0164 
0165 MODULE_DEVICE_TABLE(pci, mt76x2e_device_table);
0166 MODULE_FIRMWARE(MT7662_FIRMWARE);
0167 MODULE_FIRMWARE(MT7662_ROM_PATCH);
0168 MODULE_LICENSE("Dual BSD/GPL");
0169 
0170 static struct pci_driver mt76pci_driver = {
0171     .name       = KBUILD_MODNAME,
0172     .id_table   = mt76x2e_device_table,
0173     .probe      = mt76x2e_probe,
0174     .remove     = mt76x2e_remove,
0175 #ifdef CONFIG_PM
0176     .suspend    = mt76x2e_suspend,
0177     .resume     = mt76x2e_resume,
0178 #endif /* CONFIG_PM */
0179 };
0180 
0181 module_pci_driver(mt76pci_driver);