1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4 */
5
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/pci.h>
9
10 #include "mt76x2.h"
11
12 static const struct pci_device_id mt76x2e_device_table[] = {
13 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7662) },
14 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7612) },
15 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7602) },
16 { },
17 };
18
19 static int
mt76x2e_probe(struct pci_dev * pdev,const struct pci_device_id * id)20 mt76x2e_probe(struct pci_dev *pdev, const struct pci_device_id *id)
21 {
22 static const struct mt76_driver_ops drv_ops = {
23 .txwi_size = sizeof(struct mt76x02_txwi),
24 .drv_flags = MT_DRV_TX_ALIGNED4_SKBS |
25 MT_DRV_SW_RX_AIRTIME |
26 MT_DRV_IGNORE_TXS_FAILED,
27 .survey_flags = SURVEY_INFO_TIME_TX,
28 .update_survey = mt76x02_update_channel,
29 .set_channel = mt76x2e_set_channel,
30 .tx_prepare_skb = mt76x02_tx_prepare_skb,
31 .tx_complete_skb = mt76x02_tx_complete_skb,
32 .rx_skb = mt76x02_queue_rx_skb,
33 .rx_poll_complete = mt76x02_rx_poll_complete,
34 .sta_ps = mt76x02_sta_ps,
35 .sta_add = mt76x02_sta_add,
36 .sta_remove = mt76x02_sta_remove,
37 };
38 struct mt76x02_dev *dev;
39 struct mt76_dev *mdev;
40 int ret;
41
42 ret = pcim_enable_device(pdev);
43 if (ret)
44 return ret;
45
46 ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
47 if (ret)
48 return ret;
49
50 pci_set_master(pdev);
51
52 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
53 if (ret)
54 return ret;
55
56 mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt76x2_ops,
57 &drv_ops);
58 if (!mdev)
59 return -ENOMEM;
60
61 dev = container_of(mdev, struct mt76x02_dev, mt76);
62 mt76_mmio_init(mdev, pcim_iomap_table(pdev)[0]);
63 mt76x2_reset_wlan(dev, false);
64
65 mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
66 dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
67
68 mt76_wr(dev, MT_INT_MASK_CSR, 0);
69
70 ret = devm_request_irq(mdev->dev, pdev->irq, mt76x02_irq_handler,
71 IRQF_SHARED, KBUILD_MODNAME, dev);
72 if (ret)
73 goto error;
74
75 ret = mt76x2_register_device(dev);
76 if (ret)
77 goto error;
78
79 /* Fix up ASPM configuration */
80
81 /* RG_SSUSB_G1_CDR_BIR_LTR = 0x9 */
82 mt76_rmw_field(dev, 0x15a10, 0x1f << 16, 0x9);
83
84 /* RG_SSUSB_G1_CDR_BIC_LTR = 0xf */
85 mt76_rmw_field(dev, 0x15a0c, 0xfU << 28, 0xf);
86
87 /* RG_SSUSB_CDR_BR_PE1D = 0x3 */
88 mt76_rmw_field(dev, 0x15c58, 0x3 << 6, 0x3);
89
90 mt76_pci_disable_aspm(pdev);
91
92 return 0;
93
94 error:
95 mt76_free_device(&dev->mt76);
96
97 return ret;
98 }
99
100 static void
mt76x2e_remove(struct pci_dev * pdev)101 mt76x2e_remove(struct pci_dev *pdev)
102 {
103 struct mt76_dev *mdev = pci_get_drvdata(pdev);
104 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
105
106 mt76_unregister_device(mdev);
107 mt76x2_cleanup(dev);
108 mt76_free_device(mdev);
109 }
110
111 static int __maybe_unused
mt76x2e_suspend(struct pci_dev * pdev,pm_message_t state)112 mt76x2e_suspend(struct pci_dev *pdev, pm_message_t state)
113 {
114 struct mt76_dev *mdev = pci_get_drvdata(pdev);
115 int i, err;
116
117 napi_disable(&mdev->tx_napi);
118 tasklet_kill(&mdev->pre_tbtt_tasklet);
119 mt76_worker_disable(&mdev->tx_worker);
120
121 mt76_for_each_q_rx(mdev, i)
122 napi_disable(&mdev->napi[i]);
123
124 pci_enable_wake(pdev, pci_choose_state(pdev, state), true);
125 pci_save_state(pdev);
126 err = pci_set_power_state(pdev, pci_choose_state(pdev, state));
127 if (err)
128 goto restore;
129
130 return 0;
131
132 restore:
133 mt76_for_each_q_rx(mdev, i)
134 napi_enable(&mdev->napi[i]);
135 napi_enable(&mdev->tx_napi);
136
137 return err;
138 }
139
140 static int __maybe_unused
mt76x2e_resume(struct pci_dev * pdev)141 mt76x2e_resume(struct pci_dev *pdev)
142 {
143 struct mt76_dev *mdev = pci_get_drvdata(pdev);
144 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
145 int i, err;
146
147 err = pci_set_power_state(pdev, PCI_D0);
148 if (err)
149 return err;
150
151 pci_restore_state(pdev);
152
153 mt76_worker_enable(&mdev->tx_worker);
154
155 mt76_for_each_q_rx(mdev, i) {
156 napi_enable(&mdev->napi[i]);
157 }
158 napi_enable(&mdev->tx_napi);
159
160 local_bh_disable();
161 mt76_for_each_q_rx(mdev, i) {
162 napi_schedule(&mdev->napi[i]);
163 }
164 napi_schedule(&mdev->tx_napi);
165 local_bh_enable();
166
167 return mt76x2_resume_device(dev);
168 }
169
170 MODULE_DEVICE_TABLE(pci, mt76x2e_device_table);
171 MODULE_FIRMWARE(MT7662_FIRMWARE);
172 MODULE_FIRMWARE(MT7662_ROM_PATCH);
173 MODULE_DESCRIPTION("MediaTek MT76x2E (PCIe) wireless driver");
174 MODULE_LICENSE("Dual BSD/GPL");
175
176 static struct pci_driver mt76pci_driver = {
177 .name = KBUILD_MODNAME,
178 .id_table = mt76x2e_device_table,
179 .probe = mt76x2e_probe,
180 .remove = mt76x2e_remove,
181 #ifdef CONFIG_PM
182 .suspend = mt76x2e_suspend,
183 .resume = mt76x2e_resume,
184 #endif /* CONFIG_PM */
185 };
186
187 module_pci_driver(mt76pci_driver);
188