198686cd2SShayne Chen // SPDX-License-Identifier: ISC
298686cd2SShayne Chen /*
398686cd2SShayne Chen * Copyright (C) 2022 MediaTek Inc.
498686cd2SShayne Chen */
598686cd2SShayne Chen
698686cd2SShayne Chen #include <linux/kernel.h>
798686cd2SShayne Chen #include <linux/module.h>
898686cd2SShayne Chen #include <linux/pci.h>
998686cd2SShayne Chen
1098686cd2SShayne Chen #include "mt7996.h"
1198686cd2SShayne Chen #include "mac.h"
1298686cd2SShayne Chen #include "../trace.h"
1398686cd2SShayne Chen
1498686cd2SShayne Chen static LIST_HEAD(hif_list);
1598686cd2SShayne Chen static DEFINE_SPINLOCK(hif_lock);
1698686cd2SShayne Chen static u32 hif_idx;
1798686cd2SShayne Chen
1898686cd2SShayne Chen static const struct pci_device_id mt7996_pci_device_table[] = {
1998686cd2SShayne Chen { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7990) },
203d3f117aSStanleyYP Wang { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7992) },
2198686cd2SShayne Chen { },
2298686cd2SShayne Chen };
2398686cd2SShayne Chen
2498686cd2SShayne Chen static const struct pci_device_id mt7996_hif_device_table[] = {
2598686cd2SShayne Chen { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7991) },
263d3f117aSStanleyYP Wang { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x799a) },
2798686cd2SShayne Chen { },
2898686cd2SShayne Chen };
2998686cd2SShayne Chen
mt7996_pci_get_hif2(u32 idx)3098686cd2SShayne Chen static struct mt7996_hif *mt7996_pci_get_hif2(u32 idx)
3198686cd2SShayne Chen {
3298686cd2SShayne Chen struct mt7996_hif *hif;
3398686cd2SShayne Chen u32 val;
3498686cd2SShayne Chen
3598686cd2SShayne Chen spin_lock_bh(&hif_lock);
3698686cd2SShayne Chen
3798686cd2SShayne Chen list_for_each_entry(hif, &hif_list, list) {
3898686cd2SShayne Chen val = readl(hif->regs + MT_PCIE_RECOG_ID);
3998686cd2SShayne Chen val &= MT_PCIE_RECOG_ID_MASK;
4098686cd2SShayne Chen if (val != idx)
4198686cd2SShayne Chen continue;
4298686cd2SShayne Chen
4398686cd2SShayne Chen get_device(hif->dev);
4498686cd2SShayne Chen goto out;
4598686cd2SShayne Chen }
4698686cd2SShayne Chen hif = NULL;
4798686cd2SShayne Chen
4898686cd2SShayne Chen out:
4998686cd2SShayne Chen spin_unlock_bh(&hif_lock);
5098686cd2SShayne Chen
5198686cd2SShayne Chen return hif;
5298686cd2SShayne Chen }
5398686cd2SShayne Chen
mt7996_put_hif2(struct mt7996_hif * hif)5498686cd2SShayne Chen static void mt7996_put_hif2(struct mt7996_hif *hif)
5598686cd2SShayne Chen {
5698686cd2SShayne Chen if (!hif)
5798686cd2SShayne Chen return;
5898686cd2SShayne Chen
5998686cd2SShayne Chen put_device(hif->dev);
6098686cd2SShayne Chen }
6198686cd2SShayne Chen
mt7996_pci_init_hif2(struct pci_dev * pdev)6298686cd2SShayne Chen static struct mt7996_hif *mt7996_pci_init_hif2(struct pci_dev *pdev)
6398686cd2SShayne Chen {
6498686cd2SShayne Chen hif_idx++;
653d3f117aSStanleyYP Wang
663d3f117aSStanleyYP Wang if (!pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x7991, NULL) &&
673d3f117aSStanleyYP Wang !pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x799a, NULL))
6898686cd2SShayne Chen return NULL;
6998686cd2SShayne Chen
7098686cd2SShayne Chen writel(hif_idx | MT_PCIE_RECOG_ID_SEM,
7198686cd2SShayne Chen pcim_iomap_table(pdev)[0] + MT_PCIE_RECOG_ID);
7298686cd2SShayne Chen
7398686cd2SShayne Chen return mt7996_pci_get_hif2(hif_idx);
7498686cd2SShayne Chen }
7598686cd2SShayne Chen
mt7996_pci_hif2_probe(struct pci_dev * pdev)7698686cd2SShayne Chen static int mt7996_pci_hif2_probe(struct pci_dev *pdev)
7798686cd2SShayne Chen {
7898686cd2SShayne Chen struct mt7996_hif *hif;
7998686cd2SShayne Chen
8098686cd2SShayne Chen hif = devm_kzalloc(&pdev->dev, sizeof(*hif), GFP_KERNEL);
8198686cd2SShayne Chen if (!hif)
8298686cd2SShayne Chen return -ENOMEM;
8398686cd2SShayne Chen
8498686cd2SShayne Chen hif->dev = &pdev->dev;
8598686cd2SShayne Chen hif->regs = pcim_iomap_table(pdev)[0];
8698686cd2SShayne Chen hif->irq = pdev->irq;
8798686cd2SShayne Chen spin_lock_bh(&hif_lock);
8898686cd2SShayne Chen list_add(&hif->list, &hif_list);
8998686cd2SShayne Chen spin_unlock_bh(&hif_lock);
9098686cd2SShayne Chen pci_set_drvdata(pdev, hif);
9198686cd2SShayne Chen
9298686cd2SShayne Chen return 0;
9398686cd2SShayne Chen }
9498686cd2SShayne Chen
mt7996_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)9598686cd2SShayne Chen static int mt7996_pci_probe(struct pci_dev *pdev,
9698686cd2SShayne Chen const struct pci_device_id *id)
9798686cd2SShayne Chen {
9898686cd2SShayne Chen struct pci_dev *hif2_dev;
9998686cd2SShayne Chen struct mt7996_hif *hif2;
10083eafc92SSujuan Chen struct mt7996_dev *dev;
10183eafc92SSujuan Chen int irq, hif2_irq, ret;
10283eafc92SSujuan Chen struct mt76_dev *mdev;
10398686cd2SShayne Chen
10498686cd2SShayne Chen ret = pcim_enable_device(pdev);
10598686cd2SShayne Chen if (ret)
10698686cd2SShayne Chen return ret;
10798686cd2SShayne Chen
10898686cd2SShayne Chen ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
10998686cd2SShayne Chen if (ret)
11098686cd2SShayne Chen return ret;
11198686cd2SShayne Chen
11298686cd2SShayne Chen pci_set_master(pdev);
11398686cd2SShayne Chen
114*4920a3a1SSujuan Chen ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(36));
115*4920a3a1SSujuan Chen if (ret)
116*4920a3a1SSujuan Chen return ret;
117*4920a3a1SSujuan Chen
118*4920a3a1SSujuan Chen ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
11998686cd2SShayne Chen if (ret)
12098686cd2SShayne Chen return ret;
12198686cd2SShayne Chen
12298686cd2SShayne Chen mt76_pci_disable_aspm(pdev);
12398686cd2SShayne Chen
1243d3f117aSStanleyYP Wang if (id->device == 0x7991 || id->device == 0x799a)
12598686cd2SShayne Chen return mt7996_pci_hif2_probe(pdev);
12698686cd2SShayne Chen
12798686cd2SShayne Chen dev = mt7996_mmio_probe(&pdev->dev, pcim_iomap_table(pdev)[0],
12898686cd2SShayne Chen id->device);
12998686cd2SShayne Chen if (IS_ERR(dev))
13098686cd2SShayne Chen return PTR_ERR(dev);
13198686cd2SShayne Chen
13298686cd2SShayne Chen mdev = &dev->mt76;
13398686cd2SShayne Chen mt7996_wfsys_reset(dev);
13498686cd2SShayne Chen hif2 = mt7996_pci_init_hif2(pdev);
13598686cd2SShayne Chen
13683eafc92SSujuan Chen ret = mt7996_mmio_wed_init(dev, pdev, false, &irq);
13783eafc92SSujuan Chen if (ret < 0)
13883eafc92SSujuan Chen goto free_wed_or_irq_vector;
13983eafc92SSujuan Chen
14083eafc92SSujuan Chen if (!ret) {
14198686cd2SShayne Chen ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
14298686cd2SShayne Chen if (ret < 0)
14398686cd2SShayne Chen goto free_device;
14498686cd2SShayne Chen
14598686cd2SShayne Chen irq = pdev->irq;
14683eafc92SSujuan Chen }
14783eafc92SSujuan Chen
14898686cd2SShayne Chen ret = devm_request_irq(mdev->dev, irq, mt7996_irq_handler,
14998686cd2SShayne Chen IRQF_SHARED, KBUILD_MODNAME, dev);
15098686cd2SShayne Chen if (ret)
15183eafc92SSujuan Chen goto free_wed_or_irq_vector;
15298686cd2SShayne Chen
15398686cd2SShayne Chen mt76_wr(dev, MT_INT_MASK_CSR, 0);
15498686cd2SShayne Chen /* master switch of PCIe tnterrupt enable */
15598686cd2SShayne Chen mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff);
15698686cd2SShayne Chen
15798686cd2SShayne Chen if (hif2) {
15898686cd2SShayne Chen hif2_dev = container_of(hif2->dev, struct pci_dev, dev);
15998686cd2SShayne Chen dev->hif2 = hif2;
16098686cd2SShayne Chen
16183eafc92SSujuan Chen ret = mt7996_mmio_wed_init(dev, hif2_dev, true, &hif2_irq);
16283eafc92SSujuan Chen if (ret < 0)
16383eafc92SSujuan Chen goto free_hif2_wed_irq_vector;
16483eafc92SSujuan Chen
16583eafc92SSujuan Chen if (!ret) {
16683eafc92SSujuan Chen ret = pci_alloc_irq_vectors(hif2_dev, 1, 1,
16783eafc92SSujuan Chen PCI_IRQ_ALL_TYPES);
16898686cd2SShayne Chen if (ret < 0)
16998686cd2SShayne Chen goto free_hif2;
17098686cd2SShayne Chen
17198686cd2SShayne Chen dev->hif2->irq = hif2_dev->irq;
17283eafc92SSujuan Chen hif2_irq = dev->hif2->irq;
17383eafc92SSujuan Chen }
17483eafc92SSujuan Chen
17583eafc92SSujuan Chen ret = devm_request_irq(mdev->dev, hif2_irq, mt7996_irq_handler,
17683eafc92SSujuan Chen IRQF_SHARED, KBUILD_MODNAME "-hif",
17783eafc92SSujuan Chen dev);
17898686cd2SShayne Chen if (ret)
17983eafc92SSujuan Chen goto free_hif2_wed_irq_vector;
18098686cd2SShayne Chen
18198686cd2SShayne Chen mt76_wr(dev, MT_INT1_MASK_CSR, 0);
18298686cd2SShayne Chen /* master switch of PCIe tnterrupt enable */
18398686cd2SShayne Chen mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0xff);
18498686cd2SShayne Chen }
18598686cd2SShayne Chen
18698686cd2SShayne Chen ret = mt7996_register_device(dev);
18798686cd2SShayne Chen if (ret)
18898686cd2SShayne Chen goto free_hif2_irq;
18998686cd2SShayne Chen
19098686cd2SShayne Chen return 0;
19198686cd2SShayne Chen
19298686cd2SShayne Chen free_hif2_irq:
19398686cd2SShayne Chen if (dev->hif2)
19483eafc92SSujuan Chen devm_free_irq(mdev->dev, hif2_irq, dev);
19583eafc92SSujuan Chen free_hif2_wed_irq_vector:
19683eafc92SSujuan Chen if (dev->hif2) {
19783eafc92SSujuan Chen if (mtk_wed_device_active(&dev->mt76.mmio.wed_hif2))
19883eafc92SSujuan Chen mtk_wed_device_detach(&dev->mt76.mmio.wed_hif2);
19983eafc92SSujuan Chen else
20098686cd2SShayne Chen pci_free_irq_vectors(hif2_dev);
20183eafc92SSujuan Chen }
20298686cd2SShayne Chen free_hif2:
20398686cd2SShayne Chen if (dev->hif2)
20498686cd2SShayne Chen put_device(dev->hif2->dev);
20598686cd2SShayne Chen devm_free_irq(mdev->dev, irq, dev);
20683eafc92SSujuan Chen free_wed_or_irq_vector:
20783eafc92SSujuan Chen if (mtk_wed_device_active(&dev->mt76.mmio.wed))
20883eafc92SSujuan Chen mtk_wed_device_detach(&dev->mt76.mmio.wed);
20983eafc92SSujuan Chen else
21098686cd2SShayne Chen pci_free_irq_vectors(pdev);
21198686cd2SShayne Chen free_device:
21298686cd2SShayne Chen mt76_free_device(&dev->mt76);
21398686cd2SShayne Chen
21498686cd2SShayne Chen return ret;
21598686cd2SShayne Chen }
21698686cd2SShayne Chen
mt7996_hif_remove(struct pci_dev * pdev)21798686cd2SShayne Chen static void mt7996_hif_remove(struct pci_dev *pdev)
21898686cd2SShayne Chen {
21998686cd2SShayne Chen struct mt7996_hif *hif = pci_get_drvdata(pdev);
22098686cd2SShayne Chen
22198686cd2SShayne Chen list_del(&hif->list);
22298686cd2SShayne Chen }
22398686cd2SShayne Chen
mt7996_pci_remove(struct pci_dev * pdev)22498686cd2SShayne Chen static void mt7996_pci_remove(struct pci_dev *pdev)
22598686cd2SShayne Chen {
22698686cd2SShayne Chen struct mt76_dev *mdev;
22798686cd2SShayne Chen struct mt7996_dev *dev;
22898686cd2SShayne Chen
22998686cd2SShayne Chen mdev = pci_get_drvdata(pdev);
23098686cd2SShayne Chen dev = container_of(mdev, struct mt7996_dev, mt76);
23198686cd2SShayne Chen mt7996_put_hif2(dev->hif2);
23298686cd2SShayne Chen mt7996_unregister_device(dev);
23398686cd2SShayne Chen }
23498686cd2SShayne Chen
23598686cd2SShayne Chen struct pci_driver mt7996_hif_driver = {
23698686cd2SShayne Chen .name = KBUILD_MODNAME "_hif",
23798686cd2SShayne Chen .id_table = mt7996_hif_device_table,
23898686cd2SShayne Chen .probe = mt7996_pci_probe,
23998686cd2SShayne Chen .remove = mt7996_hif_remove,
24098686cd2SShayne Chen };
24198686cd2SShayne Chen
24298686cd2SShayne Chen struct pci_driver mt7996_pci_driver = {
24398686cd2SShayne Chen .name = KBUILD_MODNAME,
24498686cd2SShayne Chen .id_table = mt7996_pci_device_table,
24598686cd2SShayne Chen .probe = mt7996_pci_probe,
24698686cd2SShayne Chen .remove = mt7996_pci_remove,
24798686cd2SShayne Chen };
24898686cd2SShayne Chen
24998686cd2SShayne Chen MODULE_DEVICE_TABLE(pci, mt7996_pci_device_table);
25098686cd2SShayne Chen MODULE_DEVICE_TABLE(pci, mt7996_hif_device_table);
25198686cd2SShayne Chen MODULE_FIRMWARE(MT7996_FIRMWARE_WA);
25298686cd2SShayne Chen MODULE_FIRMWARE(MT7996_FIRMWARE_WM);
253a32f063dSPeter Chiu MODULE_FIRMWARE(MT7996_FIRMWARE_DSP);
25498686cd2SShayne Chen MODULE_FIRMWARE(MT7996_ROM_PATCH);
255a63b75aaSStanleyYP Wang MODULE_FIRMWARE(MT7992_FIRMWARE_WA);
256a63b75aaSStanleyYP Wang MODULE_FIRMWARE(MT7992_FIRMWARE_WM);
257a63b75aaSStanleyYP Wang MODULE_FIRMWARE(MT7992_FIRMWARE_DSP);
258a63b75aaSStanleyYP Wang MODULE_FIRMWARE(MT7992_ROM_PATCH);
259