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