1e57b7901SRyder Lee // SPDX-License-Identifier: ISC
2e57b7901SRyder Lee /* Copyright (C) 2020 MediaTek Inc.
3e57b7901SRyder Lee *
4e57b7901SRyder Lee * Author: Ryder Lee <ryder.lee@mediatek.com>
5e57b7901SRyder Lee */
6e57b7901SRyder Lee
7e57b7901SRyder Lee #include <linux/kernel.h>
8e57b7901SRyder Lee #include <linux/module.h>
9e57b7901SRyder Lee #include <linux/pci.h>
10e57b7901SRyder Lee
11e57b7901SRyder Lee #include "mt7915.h"
12e57b7901SRyder Lee #include "mac.h"
13e57b7901SRyder Lee #include "../trace.h"
14e57b7901SRyder Lee
159093cfffSFelix Fietkau static LIST_HEAD(hif_list);
169093cfffSFelix Fietkau static DEFINE_SPINLOCK(hif_lock);
179093cfffSFelix Fietkau static u32 hif_idx;
189093cfffSFelix Fietkau
19e57b7901SRyder Lee static const struct pci_device_id mt7915_pci_device_table[] = {
209093cfffSFelix Fietkau { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7915) },
212e30db0dSBo Jiao { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7906) },
22e57b7901SRyder Lee { },
23e57b7901SRyder Lee };
24e57b7901SRyder Lee
259093cfffSFelix Fietkau static const struct pci_device_id mt7915_hif_device_table[] = {
269093cfffSFelix Fietkau { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7916) },
272e30db0dSBo Jiao { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x790a) },
289093cfffSFelix Fietkau { },
299093cfffSFelix Fietkau };
309093cfffSFelix Fietkau
mt7915_pci_get_hif2(u32 idx)31cacdd678SBo Jiao static struct mt7915_hif *mt7915_pci_get_hif2(u32 idx)
329093cfffSFelix Fietkau {
339093cfffSFelix Fietkau struct mt7915_hif *hif;
349093cfffSFelix Fietkau u32 val;
359093cfffSFelix Fietkau
369093cfffSFelix Fietkau spin_lock_bh(&hif_lock);
379093cfffSFelix Fietkau
389093cfffSFelix Fietkau list_for_each_entry(hif, &hif_list, list) {
399093cfffSFelix Fietkau val = readl(hif->regs + MT_PCIE_RECOG_ID);
409093cfffSFelix Fietkau val &= MT_PCIE_RECOG_ID_MASK;
41cacdd678SBo Jiao if (val != idx)
429093cfffSFelix Fietkau continue;
439093cfffSFelix Fietkau
449093cfffSFelix Fietkau get_device(hif->dev);
459093cfffSFelix Fietkau goto out;
469093cfffSFelix Fietkau }
479093cfffSFelix Fietkau hif = NULL;
489093cfffSFelix Fietkau
499093cfffSFelix Fietkau out:
509093cfffSFelix Fietkau spin_unlock_bh(&hif_lock);
519093cfffSFelix Fietkau
529093cfffSFelix Fietkau return hif;
539093cfffSFelix Fietkau }
549093cfffSFelix Fietkau
mt7915_put_hif2(struct mt7915_hif * hif)559093cfffSFelix Fietkau static void mt7915_put_hif2(struct mt7915_hif *hif)
569093cfffSFelix Fietkau {
579093cfffSFelix Fietkau if (!hif)
589093cfffSFelix Fietkau return;
599093cfffSFelix Fietkau
609093cfffSFelix Fietkau put_device(hif->dev);
619093cfffSFelix Fietkau }
629093cfffSFelix Fietkau
mt7915_pci_init_hif2(struct pci_dev * pdev)63cacdd678SBo Jiao static struct mt7915_hif *mt7915_pci_init_hif2(struct pci_dev *pdev)
64e57b7901SRyder Lee {
65*5938196cSXiongfeng Wang struct pci_dev *tmp_pdev;
66*5938196cSXiongfeng Wang
67cacdd678SBo Jiao hif_idx++;
68*5938196cSXiongfeng Wang
69*5938196cSXiongfeng Wang tmp_pdev = pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x7916, NULL);
70*5938196cSXiongfeng Wang if (!tmp_pdev) {
71*5938196cSXiongfeng Wang tmp_pdev = pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x790a, NULL);
72*5938196cSXiongfeng Wang if (!tmp_pdev)
73cacdd678SBo Jiao return NULL;
74*5938196cSXiongfeng Wang }
75*5938196cSXiongfeng Wang pci_dev_put(tmp_pdev);
769093cfffSFelix Fietkau
77cacdd678SBo Jiao writel(hif_idx | MT_PCIE_RECOG_ID_SEM,
78cacdd678SBo Jiao pcim_iomap_table(pdev)[0] + MT_PCIE_RECOG_ID);
799093cfffSFelix Fietkau
80cacdd678SBo Jiao return mt7915_pci_get_hif2(hif_idx);
819093cfffSFelix Fietkau }
829093cfffSFelix Fietkau
mt7915_pci_hif2_probe(struct pci_dev * pdev)839093cfffSFelix Fietkau static int mt7915_pci_hif2_probe(struct pci_dev *pdev)
849093cfffSFelix Fietkau {
859093cfffSFelix Fietkau struct mt7915_hif *hif;
869093cfffSFelix Fietkau
879093cfffSFelix Fietkau hif = devm_kzalloc(&pdev->dev, sizeof(*hif), GFP_KERNEL);
889093cfffSFelix Fietkau if (!hif)
899093cfffSFelix Fietkau return -ENOMEM;
909093cfffSFelix Fietkau
919093cfffSFelix Fietkau hif->dev = &pdev->dev;
929093cfffSFelix Fietkau hif->regs = pcim_iomap_table(pdev)[0];
939093cfffSFelix Fietkau hif->irq = pdev->irq;
949093cfffSFelix Fietkau spin_lock_bh(&hif_lock);
959093cfffSFelix Fietkau list_add(&hif->list, &hif_list);
969093cfffSFelix Fietkau spin_unlock_bh(&hif_lock);
979093cfffSFelix Fietkau pci_set_drvdata(pdev, hif);
989093cfffSFelix Fietkau
999093cfffSFelix Fietkau return 0;
1009093cfffSFelix Fietkau }
1019093cfffSFelix Fietkau
mt7915_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)102e57b7901SRyder Lee static int mt7915_pci_probe(struct pci_dev *pdev,
103e57b7901SRyder Lee const struct pci_device_id *id)
104e57b7901SRyder Lee {
105f68d6762SFelix Fietkau struct mt7915_hif *hif2 = NULL;
106d200d6aeSFelix Fietkau struct mt7915_dev *dev;
107d200d6aeSFelix Fietkau struct mt76_dev *mdev;
108d200d6aeSFelix Fietkau int irq;
109e57b7901SRyder Lee int ret;
110e57b7901SRyder Lee
111e57b7901SRyder Lee ret = pcim_enable_device(pdev);
112e57b7901SRyder Lee if (ret)
113e57b7901SRyder Lee return ret;
114e57b7901SRyder Lee
115e57b7901SRyder Lee ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
116e57b7901SRyder Lee if (ret)
117e57b7901SRyder Lee return ret;
118e57b7901SRyder Lee
119e57b7901SRyder Lee pci_set_master(pdev);
120e57b7901SRyder Lee
121a2e75961SChristophe JAILLET ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
122e57b7901SRyder Lee if (ret)
123e57b7901SRyder Lee return ret;
124e57b7901SRyder Lee
12503b3dedcSFelix Fietkau mt76_pci_disable_aspm(pdev);
12603b3dedcSFelix Fietkau
1272e30db0dSBo Jiao if (id->device == 0x7916 || id->device == 0x790a)
1289093cfffSFelix Fietkau return mt7915_pci_hif2_probe(pdev);
1299093cfffSFelix Fietkau
130d200d6aeSFelix Fietkau dev = mt7915_mmio_probe(&pdev->dev, pcim_iomap_table(pdev)[0],
131d200d6aeSFelix Fietkau id->device);
132d200d6aeSFelix Fietkau if (IS_ERR(dev))
133d200d6aeSFelix Fietkau return PTR_ERR(dev);
134bdac593cSFelix Fietkau
135d200d6aeSFelix Fietkau mdev = &dev->mt76;
1365beadb27SRyder Lee mt7915_wfsys_reset(dev);
137cacdd678SBo Jiao hif2 = mt7915_pci_init_hif2(pdev);
1389df8c539SFelix Fietkau
139eebb7097SLorenzo Bianconi ret = mt7915_mmio_wed_init(dev, pdev, true, &irq);
140f68d6762SFelix Fietkau if (ret < 0)
141f68d6762SFelix Fietkau goto free_wed_or_irq_vector;
142f68d6762SFelix Fietkau
143f68d6762SFelix Fietkau if (!ret) {
144f68d6762SFelix Fietkau hif2 = mt7915_pci_init_hif2(pdev);
145f68d6762SFelix Fietkau
146d200d6aeSFelix Fietkau ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
147d200d6aeSFelix Fietkau if (ret < 0)
148d200d6aeSFelix Fietkau goto free_device;
149d200d6aeSFelix Fietkau
150d200d6aeSFelix Fietkau irq = pdev->irq;
151f68d6762SFelix Fietkau }
152f68d6762SFelix Fietkau
153d200d6aeSFelix Fietkau ret = devm_request_irq(mdev->dev, irq, mt7915_irq_handler,
154d200d6aeSFelix Fietkau IRQF_SHARED, KBUILD_MODNAME, dev);
155d200d6aeSFelix Fietkau if (ret)
156f68d6762SFelix Fietkau goto free_wed_or_irq_vector;
157d200d6aeSFelix Fietkau
158d200d6aeSFelix Fietkau /* master switch of PCIe tnterrupt enable */
159d200d6aeSFelix Fietkau mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff);
160d200d6aeSFelix Fietkau
161d200d6aeSFelix Fietkau if (hif2) {
162d200d6aeSFelix Fietkau dev->hif2 = hif2;
163d200d6aeSFelix Fietkau
164d200d6aeSFelix Fietkau mt76_wr(dev, MT_INT1_MASK_CSR, 0);
165d200d6aeSFelix Fietkau /* master switch of PCIe tnterrupt enable */
166d200d6aeSFelix Fietkau if (is_mt7915(mdev))
167d200d6aeSFelix Fietkau mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0xff);
168d200d6aeSFelix Fietkau else
169d200d6aeSFelix Fietkau mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE_MT7916, 0xff);
170d200d6aeSFelix Fietkau
171d200d6aeSFelix Fietkau ret = devm_request_irq(mdev->dev, dev->hif2->irq,
172d200d6aeSFelix Fietkau mt7915_irq_handler, IRQF_SHARED,
173d200d6aeSFelix Fietkau KBUILD_MODNAME "-hif", dev);
174d200d6aeSFelix Fietkau if (ret)
175d200d6aeSFelix Fietkau goto free_hif2;
176d200d6aeSFelix Fietkau }
177d200d6aeSFelix Fietkau
178d200d6aeSFelix Fietkau ret = mt7915_register_device(dev);
179d200d6aeSFelix Fietkau if (ret)
180d200d6aeSFelix Fietkau goto free_hif2_irq;
181d200d6aeSFelix Fietkau
182e57b7901SRyder Lee return 0;
183cacdd678SBo Jiao
184d200d6aeSFelix Fietkau free_hif2_irq:
185d200d6aeSFelix Fietkau if (dev->hif2)
186d200d6aeSFelix Fietkau devm_free_irq(mdev->dev, dev->hif2->irq, dev);
187d200d6aeSFelix Fietkau free_hif2:
188d200d6aeSFelix Fietkau if (dev->hif2)
189d200d6aeSFelix Fietkau put_device(dev->hif2->dev);
190d200d6aeSFelix Fietkau devm_free_irq(mdev->dev, irq, dev);
191f68d6762SFelix Fietkau free_wed_or_irq_vector:
192f68d6762SFelix Fietkau if (mtk_wed_device_active(&mdev->mmio.wed))
193f68d6762SFelix Fietkau mtk_wed_device_detach(&mdev->mmio.wed);
194f68d6762SFelix Fietkau else
195bdac593cSFelix Fietkau pci_free_irq_vectors(pdev);
196d200d6aeSFelix Fietkau free_device:
197d200d6aeSFelix Fietkau mt76_free_device(&dev->mt76);
198bc348defSLorenzo Bianconi
199e57b7901SRyder Lee return ret;
200e57b7901SRyder Lee }
201e57b7901SRyder Lee
mt7915_hif_remove(struct pci_dev * pdev)2029093cfffSFelix Fietkau static void mt7915_hif_remove(struct pci_dev *pdev)
2039093cfffSFelix Fietkau {
2049093cfffSFelix Fietkau struct mt7915_hif *hif = pci_get_drvdata(pdev);
2059093cfffSFelix Fietkau
2069093cfffSFelix Fietkau list_del(&hif->list);
2079093cfffSFelix Fietkau }
2089093cfffSFelix Fietkau
mt7915_pci_remove(struct pci_dev * pdev)209e57b7901SRyder Lee static void mt7915_pci_remove(struct pci_dev *pdev)
210e57b7901SRyder Lee {
2119093cfffSFelix Fietkau struct mt76_dev *mdev;
2129093cfffSFelix Fietkau struct mt7915_dev *dev;
213e57b7901SRyder Lee
2149093cfffSFelix Fietkau mdev = pci_get_drvdata(pdev);
2159093cfffSFelix Fietkau dev = container_of(mdev, struct mt7915_dev, mt76);
2169093cfffSFelix Fietkau mt7915_put_hif2(dev->hif2);
217e57b7901SRyder Lee mt7915_unregister_device(dev);
218e57b7901SRyder Lee }
219e57b7901SRyder Lee
220cacdd678SBo Jiao struct pci_driver mt7915_hif_driver = {
2219093cfffSFelix Fietkau .name = KBUILD_MODNAME "_hif",
2229093cfffSFelix Fietkau .id_table = mt7915_hif_device_table,
2239093cfffSFelix Fietkau .probe = mt7915_pci_probe,
2249093cfffSFelix Fietkau .remove = mt7915_hif_remove,
2259093cfffSFelix Fietkau };
2269093cfffSFelix Fietkau
227cacdd678SBo Jiao struct pci_driver mt7915_pci_driver = {
228e57b7901SRyder Lee .name = KBUILD_MODNAME,
229e57b7901SRyder Lee .id_table = mt7915_pci_device_table,
230e57b7901SRyder Lee .probe = mt7915_pci_probe,
231e57b7901SRyder Lee .remove = mt7915_pci_remove,
232e57b7901SRyder Lee };
233e57b7901SRyder Lee
234e57b7901SRyder Lee MODULE_DEVICE_TABLE(pci, mt7915_pci_device_table);
2359093cfffSFelix Fietkau MODULE_DEVICE_TABLE(pci, mt7915_hif_device_table);
236e57b7901SRyder Lee MODULE_FIRMWARE(MT7915_FIRMWARE_WA);
237e57b7901SRyder Lee MODULE_FIRMWARE(MT7915_FIRMWARE_WM);
238e57b7901SRyder Lee MODULE_FIRMWARE(MT7915_ROM_PATCH);
2391c7393e6SBo Jiao MODULE_FIRMWARE(MT7916_FIRMWARE_WA);
2401c7393e6SBo Jiao MODULE_FIRMWARE(MT7916_FIRMWARE_WM);
2411c7393e6SBo Jiao MODULE_FIRMWARE(MT7916_ROM_PATCH);
242