16c92544dSBjoern A. Zeeb // SPDX-License-Identifier: ISC
26c92544dSBjoern A. Zeeb /* Copyright (C) 2020 MediaTek Inc.
36c92544dSBjoern A. Zeeb *
46c92544dSBjoern A. Zeeb * Author: Ryder Lee <ryder.lee@mediatek.com>
56c92544dSBjoern A. Zeeb */
66c92544dSBjoern A. Zeeb
76c92544dSBjoern A. Zeeb #if defined(__FreeBSD__)
86c92544dSBjoern A. Zeeb #define LINUXKPI_PARAM_PREFIX mt7915_pci_
96c92544dSBjoern A. Zeeb #endif
106c92544dSBjoern A. Zeeb
116c92544dSBjoern A. Zeeb #include <linux/kernel.h>
126c92544dSBjoern A. Zeeb #include <linux/module.h>
136c92544dSBjoern A. Zeeb #include <linux/pci.h>
146c92544dSBjoern A. Zeeb
156c92544dSBjoern A. Zeeb #include "mt7915.h"
166c92544dSBjoern A. Zeeb #include "mac.h"
176c92544dSBjoern A. Zeeb #include "../trace.h"
186c92544dSBjoern A. Zeeb
196c92544dSBjoern A. Zeeb #if defined(__linux__)
206c92544dSBjoern A. Zeeb static LIST_HEAD(hif_list);
216c92544dSBjoern A. Zeeb #elif defined(__FreeBSD__)
226c92544dSBjoern A. Zeeb static LINUX_LIST_HEAD(hif_list);
236c92544dSBjoern A. Zeeb #endif
246c92544dSBjoern A. Zeeb static DEFINE_SPINLOCK(hif_lock);
256c92544dSBjoern A. Zeeb static u32 hif_idx;
266c92544dSBjoern A. Zeeb
276c92544dSBjoern A. Zeeb static const struct pci_device_id mt7915_pci_device_table[] = {
286c92544dSBjoern A. Zeeb { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7915) },
296c92544dSBjoern A. Zeeb { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7906) },
306c92544dSBjoern A. Zeeb { },
316c92544dSBjoern A. Zeeb };
326c92544dSBjoern A. Zeeb
336c92544dSBjoern A. Zeeb static const struct pci_device_id mt7915_hif_device_table[] = {
346c92544dSBjoern A. Zeeb { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7916) },
356c92544dSBjoern A. Zeeb { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x790a) },
366c92544dSBjoern A. Zeeb { },
376c92544dSBjoern A. Zeeb };
386c92544dSBjoern A. Zeeb
mt7915_pci_get_hif2(u32 idx)396c92544dSBjoern A. Zeeb static struct mt7915_hif *mt7915_pci_get_hif2(u32 idx)
406c92544dSBjoern A. Zeeb {
416c92544dSBjoern A. Zeeb struct mt7915_hif *hif;
426c92544dSBjoern A. Zeeb u32 val;
436c92544dSBjoern A. Zeeb
446c92544dSBjoern A. Zeeb spin_lock_bh(&hif_lock);
456c92544dSBjoern A. Zeeb
466c92544dSBjoern A. Zeeb list_for_each_entry(hif, &hif_list, list) {
476c92544dSBjoern A. Zeeb #if defined(__linux__)
486c92544dSBjoern A. Zeeb val = readl(hif->regs + MT_PCIE_RECOG_ID);
496c92544dSBjoern A. Zeeb #elif defined(__FreeBSD__)
506c92544dSBjoern A. Zeeb val = readl((u8 *)hif->regs + MT_PCIE_RECOG_ID);
516c92544dSBjoern A. Zeeb #endif
526c92544dSBjoern A. Zeeb val &= MT_PCIE_RECOG_ID_MASK;
536c92544dSBjoern A. Zeeb if (val != idx)
546c92544dSBjoern A. Zeeb continue;
556c92544dSBjoern A. Zeeb
566c92544dSBjoern A. Zeeb get_device(hif->dev);
576c92544dSBjoern A. Zeeb goto out;
586c92544dSBjoern A. Zeeb }
596c92544dSBjoern A. Zeeb hif = NULL;
606c92544dSBjoern A. Zeeb
616c92544dSBjoern A. Zeeb out:
626c92544dSBjoern A. Zeeb spin_unlock_bh(&hif_lock);
636c92544dSBjoern A. Zeeb
646c92544dSBjoern A. Zeeb return hif;
656c92544dSBjoern A. Zeeb }
666c92544dSBjoern A. Zeeb
mt7915_put_hif2(struct mt7915_hif * hif)676c92544dSBjoern A. Zeeb static void mt7915_put_hif2(struct mt7915_hif *hif)
686c92544dSBjoern A. Zeeb {
696c92544dSBjoern A. Zeeb if (!hif)
706c92544dSBjoern A. Zeeb return;
716c92544dSBjoern A. Zeeb
726c92544dSBjoern A. Zeeb put_device(hif->dev);
736c92544dSBjoern A. Zeeb }
746c92544dSBjoern A. Zeeb
mt7915_pci_init_hif2(struct pci_dev * pdev)756c92544dSBjoern A. Zeeb static struct mt7915_hif *mt7915_pci_init_hif2(struct pci_dev *pdev)
766c92544dSBjoern A. Zeeb {
77*cbb3ec25SBjoern A. Zeeb struct pci_dev *tmp_pdev;
78*cbb3ec25SBjoern A. Zeeb
796c92544dSBjoern A. Zeeb hif_idx++;
80*cbb3ec25SBjoern A. Zeeb
816c92544dSBjoern A. Zeeb #if defined(__linux__)
82*cbb3ec25SBjoern A. Zeeb tmp_pdev = pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x7916, NULL);
836c92544dSBjoern A. Zeeb #elif defined(__FreeBSD__)
84*cbb3ec25SBjoern A. Zeeb tmp_pdev = linuxkpi_pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x7916, NULL);
856c92544dSBjoern A. Zeeb #endif
86*cbb3ec25SBjoern A. Zeeb if (!tmp_pdev) {
87*cbb3ec25SBjoern A. Zeeb #if defined(__linux__)
88*cbb3ec25SBjoern A. Zeeb tmp_pdev = pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x790a, NULL);
89*cbb3ec25SBjoern A. Zeeb #elif defined(__FreeBSD__)
90*cbb3ec25SBjoern A. Zeeb tmp_pdev = linuxkpi_pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x790a, NULL);
91*cbb3ec25SBjoern A. Zeeb #endif
92*cbb3ec25SBjoern A. Zeeb if (!tmp_pdev)
936c92544dSBjoern A. Zeeb return NULL;
94*cbb3ec25SBjoern A. Zeeb }
95*cbb3ec25SBjoern A. Zeeb pci_dev_put(tmp_pdev);
966c92544dSBjoern A. Zeeb
976c92544dSBjoern A. Zeeb writel(hif_idx | MT_PCIE_RECOG_ID_SEM,
986c92544dSBjoern A. Zeeb #if defined(__linux__)
996c92544dSBjoern A. Zeeb pcim_iomap_table(pdev)[0] + MT_PCIE_RECOG_ID);
1006c92544dSBjoern A. Zeeb #elif defined(__FreeBSD__)
1016c92544dSBjoern A. Zeeb (u8 *)(pcim_iomap_table(pdev)[0]) + MT_PCIE_RECOG_ID);
1026c92544dSBjoern A. Zeeb #endif
1036c92544dSBjoern A. Zeeb
1046c92544dSBjoern A. Zeeb return mt7915_pci_get_hif2(hif_idx);
1056c92544dSBjoern A. Zeeb }
1066c92544dSBjoern A. Zeeb
mt7915_pci_hif2_probe(struct pci_dev * pdev)1076c92544dSBjoern A. Zeeb static int mt7915_pci_hif2_probe(struct pci_dev *pdev)
1086c92544dSBjoern A. Zeeb {
1096c92544dSBjoern A. Zeeb struct mt7915_hif *hif;
1106c92544dSBjoern A. Zeeb
1116c92544dSBjoern A. Zeeb hif = devm_kzalloc(&pdev->dev, sizeof(*hif), GFP_KERNEL);
1126c92544dSBjoern A. Zeeb if (!hif)
1136c92544dSBjoern A. Zeeb return -ENOMEM;
1146c92544dSBjoern A. Zeeb
1156c92544dSBjoern A. Zeeb hif->dev = &pdev->dev;
1166c92544dSBjoern A. Zeeb hif->regs = pcim_iomap_table(pdev)[0];
1176c92544dSBjoern A. Zeeb hif->irq = pdev->irq;
1186c92544dSBjoern A. Zeeb spin_lock_bh(&hif_lock);
1196c92544dSBjoern A. Zeeb list_add(&hif->list, &hif_list);
1206c92544dSBjoern A. Zeeb spin_unlock_bh(&hif_lock);
1216c92544dSBjoern A. Zeeb pci_set_drvdata(pdev, hif);
1226c92544dSBjoern A. Zeeb
1236c92544dSBjoern A. Zeeb return 0;
1246c92544dSBjoern A. Zeeb }
1256c92544dSBjoern A. Zeeb
mt7915_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)1266c92544dSBjoern A. Zeeb static int mt7915_pci_probe(struct pci_dev *pdev,
1276c92544dSBjoern A. Zeeb const struct pci_device_id *id)
1286c92544dSBjoern A. Zeeb {
1296c92544dSBjoern A. Zeeb struct mt7915_hif *hif2 = NULL;
1306c92544dSBjoern A. Zeeb struct mt7915_dev *dev;
1316c92544dSBjoern A. Zeeb struct mt76_dev *mdev;
1326c92544dSBjoern A. Zeeb int irq;
1336c92544dSBjoern A. Zeeb int ret;
1346c92544dSBjoern A. Zeeb
1356c92544dSBjoern A. Zeeb ret = pcim_enable_device(pdev);
1366c92544dSBjoern A. Zeeb if (ret)
1376c92544dSBjoern A. Zeeb return ret;
1386c92544dSBjoern A. Zeeb
1396c92544dSBjoern A. Zeeb ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
1406c92544dSBjoern A. Zeeb if (ret)
1416c92544dSBjoern A. Zeeb return ret;
1426c92544dSBjoern A. Zeeb
1436c92544dSBjoern A. Zeeb pci_set_master(pdev);
1446c92544dSBjoern A. Zeeb
1456c92544dSBjoern A. Zeeb ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1466c92544dSBjoern A. Zeeb if (ret)
1476c92544dSBjoern A. Zeeb return ret;
1486c92544dSBjoern A. Zeeb
1496c92544dSBjoern A. Zeeb mt76_pci_disable_aspm(pdev);
1506c92544dSBjoern A. Zeeb
1516c92544dSBjoern A. Zeeb if (id->device == 0x7916 || id->device == 0x790a)
1526c92544dSBjoern A. Zeeb return mt7915_pci_hif2_probe(pdev);
1536c92544dSBjoern A. Zeeb
1546c92544dSBjoern A. Zeeb dev = mt7915_mmio_probe(&pdev->dev, pcim_iomap_table(pdev)[0],
1556c92544dSBjoern A. Zeeb id->device);
1566c92544dSBjoern A. Zeeb if (IS_ERR(dev))
1576c92544dSBjoern A. Zeeb return PTR_ERR(dev);
1586c92544dSBjoern A. Zeeb
1596c92544dSBjoern A. Zeeb mdev = &dev->mt76;
1606c92544dSBjoern A. Zeeb mt7915_wfsys_reset(dev);
1616c92544dSBjoern A. Zeeb hif2 = mt7915_pci_init_hif2(pdev);
1626c92544dSBjoern A. Zeeb
163*cbb3ec25SBjoern A. Zeeb ret = mt7915_mmio_wed_init(dev, pdev, true, &irq);
1646c92544dSBjoern A. Zeeb if (ret < 0)
1656c92544dSBjoern A. Zeeb goto free_wed_or_irq_vector;
1666c92544dSBjoern A. Zeeb
1676c92544dSBjoern A. Zeeb if (!ret) {
1686c92544dSBjoern A. Zeeb hif2 = mt7915_pci_init_hif2(pdev);
1696c92544dSBjoern A. Zeeb
1706c92544dSBjoern A. Zeeb ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
1716c92544dSBjoern A. Zeeb if (ret < 0)
1726c92544dSBjoern A. Zeeb goto free_device;
1736c92544dSBjoern A. Zeeb
1746c92544dSBjoern A. Zeeb irq = pdev->irq;
1756c92544dSBjoern A. Zeeb }
1766c92544dSBjoern A. Zeeb
1776c92544dSBjoern A. Zeeb ret = devm_request_irq(mdev->dev, irq, mt7915_irq_handler,
1786c92544dSBjoern A. Zeeb IRQF_SHARED, KBUILD_MODNAME, dev);
1796c92544dSBjoern A. Zeeb if (ret)
1806c92544dSBjoern A. Zeeb goto free_wed_or_irq_vector;
1816c92544dSBjoern A. Zeeb
1826c92544dSBjoern A. Zeeb /* master switch of PCIe tnterrupt enable */
1836c92544dSBjoern A. Zeeb mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff);
1846c92544dSBjoern A. Zeeb
1856c92544dSBjoern A. Zeeb if (hif2) {
1866c92544dSBjoern A. Zeeb dev->hif2 = hif2;
1876c92544dSBjoern A. Zeeb
1886c92544dSBjoern A. Zeeb mt76_wr(dev, MT_INT1_MASK_CSR, 0);
1896c92544dSBjoern A. Zeeb /* master switch of PCIe tnterrupt enable */
1906c92544dSBjoern A. Zeeb if (is_mt7915(mdev))
1916c92544dSBjoern A. Zeeb mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0xff);
1926c92544dSBjoern A. Zeeb else
1936c92544dSBjoern A. Zeeb mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE_MT7916, 0xff);
1946c92544dSBjoern A. Zeeb
1956c92544dSBjoern A. Zeeb ret = devm_request_irq(mdev->dev, dev->hif2->irq,
1966c92544dSBjoern A. Zeeb mt7915_irq_handler, IRQF_SHARED,
1976c92544dSBjoern A. Zeeb KBUILD_MODNAME "-hif", dev);
1986c92544dSBjoern A. Zeeb if (ret)
1996c92544dSBjoern A. Zeeb goto free_hif2;
2006c92544dSBjoern A. Zeeb }
2016c92544dSBjoern A. Zeeb
2026c92544dSBjoern A. Zeeb ret = mt7915_register_device(dev);
2036c92544dSBjoern A. Zeeb if (ret)
2046c92544dSBjoern A. Zeeb goto free_hif2_irq;
2056c92544dSBjoern A. Zeeb
2066c92544dSBjoern A. Zeeb return 0;
2076c92544dSBjoern A. Zeeb
2086c92544dSBjoern A. Zeeb free_hif2_irq:
2096c92544dSBjoern A. Zeeb if (dev->hif2)
2106c92544dSBjoern A. Zeeb devm_free_irq(mdev->dev, dev->hif2->irq, dev);
2116c92544dSBjoern A. Zeeb free_hif2:
2126c92544dSBjoern A. Zeeb if (dev->hif2)
2136c92544dSBjoern A. Zeeb put_device(dev->hif2->dev);
2146c92544dSBjoern A. Zeeb devm_free_irq(mdev->dev, irq, dev);
2156c92544dSBjoern A. Zeeb free_wed_or_irq_vector:
2166c92544dSBjoern A. Zeeb if (mtk_wed_device_active(&mdev->mmio.wed))
2176c92544dSBjoern A. Zeeb mtk_wed_device_detach(&mdev->mmio.wed);
2186c92544dSBjoern A. Zeeb else
2196c92544dSBjoern A. Zeeb pci_free_irq_vectors(pdev);
2206c92544dSBjoern A. Zeeb free_device:
2216c92544dSBjoern A. Zeeb mt76_free_device(&dev->mt76);
2226c92544dSBjoern A. Zeeb
2236c92544dSBjoern A. Zeeb return ret;
2246c92544dSBjoern A. Zeeb }
2256c92544dSBjoern A. Zeeb
mt7915_hif_remove(struct pci_dev * pdev)2266c92544dSBjoern A. Zeeb static void mt7915_hif_remove(struct pci_dev *pdev)
2276c92544dSBjoern A. Zeeb {
2286c92544dSBjoern A. Zeeb struct mt7915_hif *hif = pci_get_drvdata(pdev);
2296c92544dSBjoern A. Zeeb
2306c92544dSBjoern A. Zeeb list_del(&hif->list);
2316c92544dSBjoern A. Zeeb }
2326c92544dSBjoern A. Zeeb
mt7915_pci_remove(struct pci_dev * pdev)2336c92544dSBjoern A. Zeeb static void mt7915_pci_remove(struct pci_dev *pdev)
2346c92544dSBjoern A. Zeeb {
2356c92544dSBjoern A. Zeeb struct mt76_dev *mdev;
2366c92544dSBjoern A. Zeeb struct mt7915_dev *dev;
2376c92544dSBjoern A. Zeeb
2386c92544dSBjoern A. Zeeb mdev = pci_get_drvdata(pdev);
2396c92544dSBjoern A. Zeeb dev = container_of(mdev, struct mt7915_dev, mt76);
2406c92544dSBjoern A. Zeeb mt7915_put_hif2(dev->hif2);
2416c92544dSBjoern A. Zeeb mt7915_unregister_device(dev);
2426c92544dSBjoern A. Zeeb }
2436c92544dSBjoern A. Zeeb
2446c92544dSBjoern A. Zeeb struct pci_driver mt7915_hif_driver = {
2456c92544dSBjoern A. Zeeb .name = KBUILD_MODNAME "_hif",
2466c92544dSBjoern A. Zeeb .id_table = mt7915_hif_device_table,
2476c92544dSBjoern A. Zeeb .probe = mt7915_pci_probe,
2486c92544dSBjoern A. Zeeb .remove = mt7915_hif_remove,
2496c92544dSBjoern A. Zeeb };
2506c92544dSBjoern A. Zeeb
2516c92544dSBjoern A. Zeeb struct pci_driver mt7915_pci_driver = {
2526c92544dSBjoern A. Zeeb .name = KBUILD_MODNAME,
2536c92544dSBjoern A. Zeeb .id_table = mt7915_pci_device_table,
2546c92544dSBjoern A. Zeeb .probe = mt7915_pci_probe,
2556c92544dSBjoern A. Zeeb .remove = mt7915_pci_remove,
2566c92544dSBjoern A. Zeeb };
2576c92544dSBjoern A. Zeeb
2586c92544dSBjoern A. Zeeb MODULE_DEVICE_TABLE(pci, mt7915_pci_device_table);
2596c92544dSBjoern A. Zeeb MODULE_DEVICE_TABLE(pci, mt7915_hif_device_table);
2606c92544dSBjoern A. Zeeb MODULE_FIRMWARE(MT7915_FIRMWARE_WA);
2616c92544dSBjoern A. Zeeb MODULE_FIRMWARE(MT7915_FIRMWARE_WM);
2626c92544dSBjoern A. Zeeb MODULE_FIRMWARE(MT7915_ROM_PATCH);
2636c92544dSBjoern A. Zeeb MODULE_FIRMWARE(MT7916_FIRMWARE_WA);
2646c92544dSBjoern A. Zeeb MODULE_FIRMWARE(MT7916_FIRMWARE_WM);
2656c92544dSBjoern A. Zeeb MODULE_FIRMWARE(MT7916_ROM_PATCH);
2666c92544dSBjoern A. Zeeb #if defined(__FreeBSD__)
2676c92544dSBjoern A. Zeeb MODULE_VERSION(mt7915_pci, 1);
2686c92544dSBjoern A. Zeeb MODULE_DEPEND(mt7915_pci, linuxkpi, 1, 1, 1);
2696c92544dSBjoern A. Zeeb MODULE_DEPEND(mt7915_pci, linuxkpi_wlan, 1, 1, 1);
2706c92544dSBjoern A. Zeeb MODULE_DEPEND(mt7915_pci, mt76_core, 1, 1, 1);
2716c92544dSBjoern A. Zeeb #endif
272