blob: 743f01f6eae98f38184ece551a153e6e2ea15d32 [file] [log] [blame]
developer0f312e82022-11-01 12:31:52 +08001// SPDX-License-Identifier: ISC
2/* Copyright (C) 2020 MediaTek Inc.
3 *
4 * Author: Ryder Lee <ryder.lee@mediatek.com>
5 */
6
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/pci.h>
10
11#include "mt7915.h"
12#include "mac.h"
13#include "../trace.h"
14
15static LIST_HEAD(hif_list);
16static DEFINE_SPINLOCK(hif_lock);
17static u32 hif_idx;
18
19static const struct pci_device_id mt7915_pci_device_table[] = {
20 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7915) },
21 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7906) },
22 { },
23};
24
25static const struct pci_device_id mt7915_hif_device_table[] = {
26 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7916) },
27 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x790a) },
28 { },
29};
30
31static struct mt7915_hif *mt7915_pci_get_hif2(u32 idx)
32{
33 struct mt7915_hif *hif;
34 u32 val;
35
36 spin_lock_bh(&hif_lock);
37
38 list_for_each_entry(hif, &hif_list, list) {
39 val = readl(hif->regs + MT_PCIE_RECOG_ID);
40 val &= MT_PCIE_RECOG_ID_MASK;
41 if (val != idx)
42 continue;
43
44 get_device(hif->dev);
45 goto out;
46 }
47 hif = NULL;
48
49out:
50 spin_unlock_bh(&hif_lock);
51
52 return hif;
53}
54
55static void mt7915_put_hif2(struct mt7915_hif *hif)
56{
57 if (!hif)
58 return;
59
60 put_device(hif->dev);
61}
62
63static struct mt7915_hif *mt7915_pci_init_hif2(struct pci_dev *pdev)
64{
65 hif_idx++;
66 if (!pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x7916, NULL) &&
67 !pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x790a, NULL))
68 return NULL;
69
70 writel(hif_idx | MT_PCIE_RECOG_ID_SEM,
71 pcim_iomap_table(pdev)[0] + MT_PCIE_RECOG_ID);
72
73 return mt7915_pci_get_hif2(hif_idx);
74}
75
76static int mt7915_pci_hif2_probe(struct pci_dev *pdev)
77{
78 struct mt7915_hif *hif;
79
80 hif = devm_kzalloc(&pdev->dev, sizeof(*hif), GFP_KERNEL);
81 if (!hif)
82 return -ENOMEM;
83
84 hif->dev = &pdev->dev;
85 hif->regs = pcim_iomap_table(pdev)[0];
86 hif->irq = pdev->irq;
87 spin_lock_bh(&hif_lock);
88 list_add(&hif->list, &hif_list);
89 spin_unlock_bh(&hif_lock);
90 pci_set_drvdata(pdev, hif);
91
92 return 0;
93}
94
95static int mt7915_pci_probe(struct pci_dev *pdev,
96 const struct pci_device_id *id)
97{
98 struct mt7915_hif *hif2 = NULL;
99 struct mt7915_dev *dev;
100 struct mt76_dev *mdev;
101 int irq;
102 int ret;
103
104 ret = pcim_enable_device(pdev);
105 if (ret)
106 return ret;
107
108 ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
109 if (ret)
110 return ret;
111
112 pci_set_master(pdev);
113
114 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
115 if (ret)
116 return ret;
117
118 mt76_pci_disable_aspm(pdev);
119
120 if (id->device == 0x7916 || id->device == 0x790a)
121 return mt7915_pci_hif2_probe(pdev);
122
123 dev = mt7915_mmio_probe(&pdev->dev, pcim_iomap_table(pdev)[0],
124 id->device);
125 if (IS_ERR(dev))
126 return PTR_ERR(dev);
127
128 mdev = &dev->mt76;
129 mt7915_wfsys_reset(dev);
130 hif2 = mt7915_pci_init_hif2(pdev);
131
132 ret = mt7915_mmio_wed_init(dev, pdev, true, &irq);
133 if (ret < 0)
134 goto free_wed_or_irq_vector;
135
136 if (!ret) {
137 hif2 = mt7915_pci_init_hif2(pdev);
138
139 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
140 if (ret < 0)
141 goto free_device;
142
143 irq = pdev->irq;
144 }
145
146 ret = devm_request_irq(mdev->dev, irq, mt7915_irq_handler,
147 IRQF_SHARED, KBUILD_MODNAME, dev);
148 if (ret)
149 goto free_wed_or_irq_vector;
150
151 /* master switch of PCIe tnterrupt enable */
152 mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff);
153
154 if (hif2) {
155 dev->hif2 = hif2;
156
157 mt76_wr(dev, MT_INT1_MASK_CSR, 0);
158 /* master switch of PCIe tnterrupt enable */
159 if (is_mt7915(mdev))
160 mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0xff);
161 else
162 mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE_MT7916, 0xff);
163
164 ret = devm_request_irq(mdev->dev, dev->hif2->irq,
165 mt7915_irq_handler, IRQF_SHARED,
166 KBUILD_MODNAME "-hif", dev);
167 if (ret)
168 goto free_hif2;
169 }
170
171 ret = mt7915_register_device(dev);
172 if (ret)
173 goto free_hif2_irq;
174
175 return 0;
176
177free_hif2_irq:
178 if (dev->hif2)
179 devm_free_irq(mdev->dev, dev->hif2->irq, dev);
180free_hif2:
181 if (dev->hif2)
182 put_device(dev->hif2->dev);
183 devm_free_irq(mdev->dev, irq, dev);
184free_wed_or_irq_vector:
185 if (mtk_wed_device_active(&mdev->mmio.wed))
186 mtk_wed_device_detach(&mdev->mmio.wed);
187 else
188 pci_free_irq_vectors(pdev);
189free_device:
190 mt76_free_device(&dev->mt76);
191
192 return ret;
193}
194
195static void mt7915_hif_remove(struct pci_dev *pdev)
196{
197 struct mt7915_hif *hif = pci_get_drvdata(pdev);
198
199 list_del(&hif->list);
200}
201
202static void mt7915_pci_remove(struct pci_dev *pdev)
203{
204 struct mt76_dev *mdev;
205 struct mt7915_dev *dev;
206
207 mdev = pci_get_drvdata(pdev);
208 dev = container_of(mdev, struct mt7915_dev, mt76);
209 mt7915_put_hif2(dev->hif2);
210 mt7915_unregister_device(dev);
211}
212
213struct pci_driver mt7915_hif_driver = {
214 .name = KBUILD_MODNAME "_hif",
215 .id_table = mt7915_hif_device_table,
216 .probe = mt7915_pci_probe,
217 .remove = mt7915_hif_remove,
218};
219
220struct pci_driver mt7915_pci_driver = {
221 .name = KBUILD_MODNAME,
222 .id_table = mt7915_pci_device_table,
223 .probe = mt7915_pci_probe,
224 .remove = mt7915_pci_remove,
225};
226
227MODULE_DEVICE_TABLE(pci, mt7915_pci_device_table);
228MODULE_DEVICE_TABLE(pci, mt7915_hif_device_table);
229MODULE_FIRMWARE(MT7915_FIRMWARE_WA);
230MODULE_FIRMWARE(MT7915_FIRMWARE_WM);
231MODULE_FIRMWARE(MT7915_ROM_PATCH);
232MODULE_FIRMWARE(MT7916_FIRMWARE_WA);
233MODULE_FIRMWARE(MT7916_FIRMWARE_WM);
234MODULE_FIRMWARE(MT7916_ROM_PATCH);