1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc.
4 * Author: Ryder Lee <ryder.lee@mediatek.com>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
15 static bool wed_enable = false;
16 module_param(wed_enable, bool, 0644);
18 static LIST_HEAD(hif_list);
19 static DEFINE_SPINLOCK(hif_lock);
22 static const struct pci_device_id mt7915_pci_device_table[] = {
23 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7915) },
24 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7906) },
28 static const struct pci_device_id mt7915_hif_device_table[] = {
29 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7916) },
30 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x790a) },
34 static struct mt7915_hif *mt7915_pci_get_hif2(u32 idx)
36 struct mt7915_hif *hif;
39 spin_lock_bh(&hif_lock);
41 list_for_each_entry(hif, &hif_list, list) {
42 val = readl(hif->regs + MT_PCIE_RECOG_ID);
43 val &= MT_PCIE_RECOG_ID_MASK;
53 spin_unlock_bh(&hif_lock);
58 static void mt7915_put_hif2(struct mt7915_hif *hif)
66 static struct mt7915_hif *mt7915_pci_init_hif2(struct pci_dev *pdev)
69 if (!pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x7916, NULL) &&
70 !pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x790a, NULL))
73 writel(hif_idx | MT_PCIE_RECOG_ID_SEM,
74 pcim_iomap_table(pdev)[0] + MT_PCIE_RECOG_ID);
76 return mt7915_pci_get_hif2(hif_idx);
79 static int mt7915_pci_hif2_probe(struct pci_dev *pdev)
81 struct mt7915_hif *hif;
83 hif = devm_kzalloc(&pdev->dev, sizeof(*hif), GFP_KERNEL);
87 hif->dev = &pdev->dev;
88 hif->regs = pcim_iomap_table(pdev)[0];
90 spin_lock_bh(&hif_lock);
91 list_add(&hif->list, &hif_list);
92 spin_unlock_bh(&hif_lock);
93 pci_set_drvdata(pdev, hif);
98 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
99 static int mt7915_wed_offload_enable(struct mtk_wed_device *wed)
101 struct mt7915_dev *dev;
104 dev = container_of(wed, struct mt7915_dev, mt76.mmio.wed);
106 spin_lock_bh(&dev->mt76.token_lock);
107 dev->mt76.token_size = wed->wlan.token_start;
108 spin_unlock_bh(&dev->mt76.token_lock);
110 ret = wait_event_timeout(dev->mt76.tx_wait,
111 !dev->mt76.wed_token_count, HZ);
118 static void mt7915_wed_offload_disable(struct mtk_wed_device *wed)
120 struct mt7915_dev *dev;
122 dev = container_of(wed, struct mt7915_dev, mt76.mmio.wed);
124 spin_lock_bh(&dev->mt76.token_lock);
125 dev->mt76.token_size = MT7915_TOKEN_SIZE;
126 spin_unlock_bh(&dev->mt76.token_lock);
131 mt7915_pci_wed_init(struct mt7915_dev *dev, struct pci_dev *pdev, int *irq)
133 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
134 struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
140 wed->wlan.pci_dev = pdev;
141 wed->wlan.wpdma_phys = pci_resource_start(pdev, 0) +
142 MT_WFDMA_EXT_CSR_BASE;
143 wed->wlan.nbuf = 4096;
144 wed->wlan.token_start = MT7915_TOKEN_SIZE - wed->wlan.nbuf;
145 wed->wlan.init_buf = mt7915_wed_init_buf;
146 wed->wlan.offload_enable = mt7915_wed_offload_enable;
147 wed->wlan.offload_disable = mt7915_wed_offload_disable;
149 if (mtk_wed_device_attach(wed) != 0)
153 dev->mt76.dma_dev = wed->dev;
155 ret = dma_set_mask(wed->dev, DMA_BIT_MASK(32));
165 static int mt7915_pci_probe(struct pci_dev *pdev,
166 const struct pci_device_id *id)
168 struct mt7915_hif *hif2 = NULL;
169 struct mt7915_dev *dev;
170 struct mt76_dev *mdev;
174 ret = pcim_enable_device(pdev);
178 ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
182 pci_set_master(pdev);
184 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
188 mt76_pci_disable_aspm(pdev);
190 if (id->device == 0x7916 || id->device == 0x790a)
191 return mt7915_pci_hif2_probe(pdev);
193 dev = mt7915_mmio_probe(&pdev->dev, pcim_iomap_table(pdev)[0],
199 mt7915_wfsys_reset(dev);
200 hif2 = mt7915_pci_init_hif2(pdev);
202 ret = mt7915_pci_wed_init(dev, pdev, &irq);
204 goto free_wed_or_irq_vector;
207 hif2 = mt7915_pci_init_hif2(pdev);
209 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
216 ret = devm_request_irq(mdev->dev, irq, mt7915_irq_handler,
217 IRQF_SHARED, KBUILD_MODNAME, dev);
219 goto free_wed_or_irq_vector;
221 /* master switch of PCIe tnterrupt enable */
222 mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff);
227 mt76_wr(dev, MT_INT1_MASK_CSR, 0);
228 /* master switch of PCIe tnterrupt enable */
230 mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0xff);
232 mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE_MT7916, 0xff);
234 ret = devm_request_irq(mdev->dev, dev->hif2->irq,
235 mt7915_irq_handler, IRQF_SHARED,
236 KBUILD_MODNAME "-hif", dev);
241 ret = mt7915_register_device(dev);
249 devm_free_irq(mdev->dev, dev->hif2->irq, dev);
252 put_device(dev->hif2->dev);
253 devm_free_irq(mdev->dev, irq, dev);
254 free_wed_or_irq_vector:
255 if (mtk_wed_device_active(&mdev->mmio.wed))
256 mtk_wed_device_detach(&mdev->mmio.wed);
258 pci_free_irq_vectors(pdev);
260 mt76_free_device(&dev->mt76);
265 static void mt7915_hif_remove(struct pci_dev *pdev)
267 struct mt7915_hif *hif = pci_get_drvdata(pdev);
269 list_del(&hif->list);
272 static void mt7915_pci_remove(struct pci_dev *pdev)
274 struct mt76_dev *mdev;
275 struct mt7915_dev *dev;
277 mdev = pci_get_drvdata(pdev);
278 dev = container_of(mdev, struct mt7915_dev, mt76);
279 mt7915_put_hif2(dev->hif2);
280 mt7915_unregister_device(dev);
283 struct pci_driver mt7915_hif_driver = {
284 .name = KBUILD_MODNAME "_hif",
285 .id_table = mt7915_hif_device_table,
286 .probe = mt7915_pci_probe,
287 .remove = mt7915_hif_remove,
290 struct pci_driver mt7915_pci_driver = {
291 .name = KBUILD_MODNAME,
292 .id_table = mt7915_pci_device_table,
293 .probe = mt7915_pci_probe,
294 .remove = mt7915_pci_remove,
297 MODULE_DEVICE_TABLE(pci, mt7915_pci_device_table);
298 MODULE_DEVICE_TABLE(pci, mt7915_hif_device_table);