blob: 1bab1cbb0d89647ee3ac4d71903400213991d6da [file] [log] [blame]
developerb11a5392022-03-31 00:34:47 +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_dev *dev;
99 struct mt76_dev *mdev;
100 struct mt7915_hif *hif2;
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 = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
133 if (ret < 0)
134 goto free_device;
135
136 irq = pdev->irq;
137 ret = devm_request_irq(mdev->dev, irq, mt7915_irq_handler,
138 IRQF_SHARED, KBUILD_MODNAME, dev);
139 if (ret)
140 goto free_irq_vector;
141
142 mt76_wr(dev, MT_INT_MASK_CSR, 0);
143
144 /* master switch of PCIe tnterrupt enable */
145 mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff);
146
147 if (hif2) {
148 dev->hif2 = hif2;
149
150 mt76_wr(dev, MT_INT1_MASK_CSR, 0);
151 /* master switch of PCIe tnterrupt enable */
152 if (is_mt7915(mdev))
153 mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0xff);
154 else
155 mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE_MT7916, 0xff);
156
157 ret = devm_request_irq(mdev->dev, dev->hif2->irq,
158 mt7915_irq_handler, IRQF_SHARED,
159 KBUILD_MODNAME "-hif", dev);
160 if (ret)
161 goto free_hif2;
162 }
163
164 ret = mt7915_register_device(dev);
165 if (ret)
166 goto free_hif2_irq;
167
168 return 0;
169
170free_hif2_irq:
171 if (dev->hif2)
172 devm_free_irq(mdev->dev, dev->hif2->irq, dev);
173free_hif2:
174 if (dev->hif2)
175 put_device(dev->hif2->dev);
176 devm_free_irq(mdev->dev, irq, dev);
177free_irq_vector:
178 pci_free_irq_vectors(pdev);
179free_device:
180 mt76_free_device(&dev->mt76);
181
182 return ret;
183}
184
185static void mt7915_hif_remove(struct pci_dev *pdev)
186{
187 struct mt7915_hif *hif = pci_get_drvdata(pdev);
188
189 list_del(&hif->list);
190}
191
192static void mt7915_pci_remove(struct pci_dev *pdev)
193{
194 struct mt76_dev *mdev;
195 struct mt7915_dev *dev;
196
197 mdev = pci_get_drvdata(pdev);
198 dev = container_of(mdev, struct mt7915_dev, mt76);
199 mt7915_put_hif2(dev->hif2);
200 mt7915_unregister_device(dev);
201}
202
203struct pci_driver mt7915_hif_driver = {
204 .name = KBUILD_MODNAME "_hif",
205 .id_table = mt7915_hif_device_table,
206 .probe = mt7915_pci_probe,
207 .remove = mt7915_hif_remove,
208};
209
210struct pci_driver mt7915_pci_driver = {
211 .name = KBUILD_MODNAME,
212 .id_table = mt7915_pci_device_table,
213 .probe = mt7915_pci_probe,
214 .remove = mt7915_pci_remove,
215};
216
217MODULE_DEVICE_TABLE(pci, mt7915_pci_device_table);
218MODULE_DEVICE_TABLE(pci, mt7915_hif_device_table);
219MODULE_FIRMWARE(MT7915_FIRMWARE_WA);
220MODULE_FIRMWARE(MT7915_FIRMWARE_WM);
221MODULE_FIRMWARE(MT7915_ROM_PATCH);
222MODULE_FIRMWARE(MT7916_FIRMWARE_WA);
223MODULE_FIRMWARE(MT7916_FIRMWARE_WM);
224MODULE_FIRMWARE(MT7916_ROM_PATCH);