xref: /linux/drivers/net/wireless/mediatek/mt76/mt7996/pci.c (revision 8a5f956a9fb7d74fff681145082acfad5afa6bb8)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2022 MediaTek Inc.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/pci.h>
9 
10 #include "mt7996.h"
11 #include "mac.h"
12 #include "../trace.h"
13 
14 static LIST_HEAD(hif_list);
15 static DEFINE_SPINLOCK(hif_lock);
16 static u32 hif_idx;
17 
18 static const struct pci_device_id mt7996_pci_device_table[] = {
19 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, MT7996_DEVICE_ID) },
20 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, MT7992_DEVICE_ID) },
21 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, MT7990_DEVICE_ID) },
22 	{ },
23 };
24 
25 static const struct pci_device_id mt7996_hif_device_table[] = {
26 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, MT7996_DEVICE_ID_2) },
27 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, MT7992_DEVICE_ID_2) },
28 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, MT7990_DEVICE_ID_2) },
29 	{ },
30 };
31 
32 static struct mt7996_hif *mt7996_pci_get_hif2(u32 idx)
33 {
34 	struct mt7996_hif *hif;
35 	u32 val;
36 
37 	spin_lock_bh(&hif_lock);
38 
39 	list_for_each_entry(hif, &hif_list, list) {
40 		val = readl(hif->regs + MT_PCIE_RECOG_ID);
41 		val &= MT_PCIE_RECOG_ID_MASK;
42 		if (val != idx)
43 			continue;
44 
45 		get_device(hif->dev);
46 		goto out;
47 	}
48 	hif = NULL;
49 
50 out:
51 	spin_unlock_bh(&hif_lock);
52 
53 	return hif;
54 }
55 
56 static void mt7996_put_hif2(struct mt7996_hif *hif)
57 {
58 	if (!hif)
59 		return;
60 
61 	put_device(hif->dev);
62 }
63 
64 static struct mt7996_hif *mt7996_pci_init_hif2(struct pci_dev *pdev)
65 {
66 	hif_idx++;
67 
68 	if (!pci_get_device(PCI_VENDOR_ID_MEDIATEK, MT7996_DEVICE_ID_2, NULL) &&
69 	    !pci_get_device(PCI_VENDOR_ID_MEDIATEK, MT7992_DEVICE_ID_2, NULL) &&
70 	    !pci_get_device(PCI_VENDOR_ID_MEDIATEK, MT7990_DEVICE_ID_2, NULL))
71 		return NULL;
72 
73 	writel(hif_idx | MT_PCIE_RECOG_ID_SEM,
74 	       pcim_iomap_table(pdev)[0] + MT_PCIE_RECOG_ID);
75 
76 	return mt7996_pci_get_hif2(hif_idx);
77 }
78 
79 static int mt7996_pci_hif2_probe(struct pci_dev *pdev)
80 {
81 	struct mt7996_hif *hif;
82 
83 	hif = devm_kzalloc(&pdev->dev, sizeof(*hif), GFP_KERNEL);
84 	if (!hif)
85 		return -ENOMEM;
86 
87 	hif->dev = &pdev->dev;
88 	hif->regs = pcim_iomap_table(pdev)[0];
89 	hif->irq = pdev->irq;
90 	pcie_bandwidth_available(pdev, NULL, &hif->speed, &hif->width);
91 	spin_lock_bh(&hif_lock);
92 	list_add(&hif->list, &hif_list);
93 	spin_unlock_bh(&hif_lock);
94 	pci_set_drvdata(pdev, hif);
95 
96 	return 0;
97 }
98 
99 static int mt7996_pci_probe(struct pci_dev *pdev,
100 			    const struct pci_device_id *id)
101 {
102 	struct pci_dev *hif2_dev;
103 	struct mt7996_hif *hif2;
104 	struct mt7996_dev *dev;
105 	int irq, hif2_irq, ret;
106 	struct mt76_dev *mdev;
107 
108 	ret = pcim_enable_device(pdev);
109 	if (ret)
110 		return ret;
111 
112 	ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
113 	if (ret)
114 		return ret;
115 
116 	pci_set_master(pdev);
117 
118 	ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(36));
119 	if (ret)
120 		return ret;
121 
122 	ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
123 	if (ret)
124 		return ret;
125 
126 	mt76_pci_disable_aspm(pdev);
127 
128 	if (id->device == MT7996_DEVICE_ID_2 ||
129 	    id->device == MT7992_DEVICE_ID_2 ||
130 	    id->device == MT7990_DEVICE_ID_2)
131 		return mt7996_pci_hif2_probe(pdev);
132 
133 	dev = mt7996_mmio_probe(&pdev->dev, pcim_iomap_table(pdev)[0],
134 				id->device);
135 	if (IS_ERR(dev))
136 		return PTR_ERR(dev);
137 
138 	mdev = &dev->mt76;
139 	mt7996_wfsys_reset(dev);
140 	hif2 = mt7996_pci_init_hif2(pdev);
141 	dev->hif2 = hif2;
142 
143 	ret = mt7996_mmio_wed_init(dev, pdev, false, &irq);
144 	if (ret < 0)
145 		goto free_wed_or_irq_vector;
146 
147 	if (!ret) {
148 		ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
149 		if (ret < 0)
150 			goto free_device;
151 
152 		irq = pdev->irq;
153 	}
154 
155 	ret = devm_request_irq(mdev->dev, irq, mt7996_irq_handler,
156 			       IRQF_SHARED, KBUILD_MODNAME, dev);
157 	if (ret)
158 		goto free_wed_or_irq_vector;
159 
160 	mt76_wr(dev, MT_INT_MASK_CSR, 0);
161 	/* master switch of PCIe tnterrupt enable */
162 	mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff);
163 
164 	if (hif2) {
165 		hif2_dev = container_of(hif2->dev, struct pci_dev, dev);
166 
167 		ret = mt7996_mmio_wed_init(dev, hif2_dev, true, &hif2_irq);
168 		if (ret < 0)
169 			goto free_hif2_wed_irq_vector;
170 
171 		if (!ret) {
172 			ret = pci_alloc_irq_vectors(hif2_dev, 1, 1,
173 						    PCI_IRQ_ALL_TYPES);
174 			if (ret < 0)
175 				goto free_hif2;
176 
177 			dev->hif2->irq = hif2_dev->irq;
178 			hif2_irq = dev->hif2->irq;
179 		}
180 
181 		ret = devm_request_irq(mdev->dev, hif2_irq, mt7996_irq_handler,
182 				       IRQF_SHARED, KBUILD_MODNAME "-hif",
183 				       dev);
184 		if (ret)
185 			goto free_hif2_wed_irq_vector;
186 
187 		mt76_wr(dev, MT_INT1_MASK_CSR, 0);
188 		/* master switch of PCIe tnterrupt enable */
189 		mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0xff);
190 	}
191 
192 	ret = mt7996_register_device(dev);
193 	if (ret)
194 		goto free_hif2_irq;
195 
196 	return 0;
197 
198 free_hif2_irq:
199 	if (dev->hif2)
200 		devm_free_irq(mdev->dev, hif2_irq, dev);
201 free_hif2_wed_irq_vector:
202 	if (dev->hif2) {
203 		if (mtk_wed_device_active(&dev->mt76.mmio.wed_hif2))
204 			mtk_wed_device_detach(&dev->mt76.mmio.wed_hif2);
205 		else
206 			pci_free_irq_vectors(hif2_dev);
207 	}
208 free_hif2:
209 	if (dev->hif2)
210 		put_device(dev->hif2->dev);
211 	devm_free_irq(mdev->dev, irq, dev);
212 free_wed_or_irq_vector:
213 	if (mtk_wed_device_active(&dev->mt76.mmio.wed))
214 		mtk_wed_device_detach(&dev->mt76.mmio.wed);
215 	else
216 		pci_free_irq_vectors(pdev);
217 free_device:
218 	mt76_free_device(&dev->mt76);
219 
220 	return ret;
221 }
222 
223 static void mt7996_hif_remove(struct pci_dev *pdev)
224 {
225 	struct mt7996_hif *hif = pci_get_drvdata(pdev);
226 
227 	list_del(&hif->list);
228 }
229 
230 static void mt7996_pci_remove(struct pci_dev *pdev)
231 {
232 	struct mt76_dev *mdev;
233 	struct mt7996_dev *dev;
234 
235 	mdev = pci_get_drvdata(pdev);
236 	dev = container_of(mdev, struct mt7996_dev, mt76);
237 	mt7996_put_hif2(dev->hif2);
238 	mt7996_unregister_device(dev);
239 }
240 
241 struct pci_driver mt7996_hif_driver = {
242 	.name		= KBUILD_MODNAME "_hif",
243 	.id_table	= mt7996_hif_device_table,
244 	.probe		= mt7996_pci_probe,
245 	.remove		= mt7996_hif_remove,
246 };
247 
248 struct pci_driver mt7996_pci_driver = {
249 	.name		= KBUILD_MODNAME,
250 	.id_table	= mt7996_pci_device_table,
251 	.probe		= mt7996_pci_probe,
252 	.remove		= mt7996_pci_remove,
253 };
254 
255 MODULE_DEVICE_TABLE(pci, mt7996_pci_device_table);
256 MODULE_DEVICE_TABLE(pci, mt7996_hif_device_table);
257 MODULE_FIRMWARE(MT7996_FIRMWARE_WA);
258 MODULE_FIRMWARE(MT7996_FIRMWARE_WM);
259 MODULE_FIRMWARE(MT7996_FIRMWARE_DSP);
260 MODULE_FIRMWARE(MT7996_ROM_PATCH);
261 MODULE_FIRMWARE(MT7992_FIRMWARE_WA);
262 MODULE_FIRMWARE(MT7992_FIRMWARE_WM);
263 MODULE_FIRMWARE(MT7992_FIRMWARE_DSP);
264 MODULE_FIRMWARE(MT7992_ROM_PATCH);
265 MODULE_FIRMWARE(MT7990_FIRMWARE_WM);
266 MODULE_FIRMWARE(MT7990_ROM_PATCH);
267