1 // 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
15 static bool wed_enable = false;
16 module_param(wed_enable, bool, 0644);
17
18 static LIST_HEAD(hif_list);
19 static DEFINE_SPINLOCK(hif_lock);
20 static u32 hif_idx;
21
22 static const struct pci_device_id mt7915_pci_device_table[] = {
23 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7915) },
24 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7906) },
25 { },
26 };
27
28 static const struct pci_device_id mt7915_hif_device_table[] = {
29 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7916) },
30 { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x790a) },
31 { },
32 };
33
mt7915_pci_get_hif2(u32 idx)34 static struct mt7915_hif *mt7915_pci_get_hif2(u32 idx)
35 {
36 struct mt7915_hif *hif;
37 u32 val;
38
39 spin_lock_bh(&hif_lock);
40
41 list_for_each_entry(hif, &hif_list, list) {
42 val = readl(hif->regs + MT_PCIE_RECOG_ID);
43 val &= MT_PCIE_RECOG_ID_MASK;
44 if (val != idx)
45 continue;
46
47 get_device(hif->dev);
48 goto out;
49 }
50 hif = NULL;
51
52 out:
53 spin_unlock_bh(&hif_lock);
54
55 return hif;
56 }
57
mt7915_put_hif2(struct mt7915_hif * hif)58 static void mt7915_put_hif2(struct mt7915_hif *hif)
59 {
60 if (!hif)
61 return;
62
63 put_device(hif->dev);
64 }
65
mt7915_pci_init_hif2(struct pci_dev * pdev)66 static struct mt7915_hif *mt7915_pci_init_hif2(struct pci_dev *pdev)
67 {
68 hif_idx++;
69 if (!pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x7916, NULL) &&
70 !pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x790a, 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 mt7915_pci_get_hif2(hif_idx);
77 }
78
mt7915_pci_hif2_probe(struct pci_dev * pdev)79 static int mt7915_pci_hif2_probe(struct pci_dev *pdev)
80 {
81 struct mt7915_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 spin_lock_bh(&hif_lock);
91 list_add(&hif->list, &hif_list);
92 spin_unlock_bh(&hif_lock);
93 pci_set_drvdata(pdev, hif);
94
95 return 0;
96 }
97
98 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
mt7915_wed_offload_enable(struct mtk_wed_device * wed)99 static int mt7915_wed_offload_enable(struct mtk_wed_device *wed)
100 {
101 struct mt7915_dev *dev;
102 struct mt7915_phy *phy;
103 int ret;
104
105 dev = container_of(wed, struct mt7915_dev, mt76.mmio.wed);
106
107 spin_lock_bh(&dev->mt76.token_lock);
108 dev->mt76.token_size = wed->wlan.token_start;
109 spin_unlock_bh(&dev->mt76.token_lock);
110
111 ret = wait_event_timeout(dev->mt76.tx_wait,
112 !dev->mt76.wed_token_count, HZ);
113 if (!ret)
114 return -EAGAIN;
115
116 phy = &dev->phy;
117 mt76_set(dev, MT_AGG_ACR4(phy->band_idx), MT_AGG_ACR_PPDU_TXS2H);
118
119 phy = dev->mt76.phys[MT_BAND1] ? dev->mt76.phys[MT_BAND1]->priv : NULL;
120 if (phy)
121 mt76_set(dev, MT_AGG_ACR4(phy->band_idx),
122 MT_AGG_ACR_PPDU_TXS2H);
123
124 return 0;
125 }
126
mt7915_wed_offload_disable(struct mtk_wed_device * wed)127 static void mt7915_wed_offload_disable(struct mtk_wed_device *wed)
128 {
129 struct mt7915_dev *dev;
130 struct mt7915_phy *phy;
131
132 dev = container_of(wed, struct mt7915_dev, mt76.mmio.wed);
133
134 spin_lock_bh(&dev->mt76.token_lock);
135 dev->mt76.token_size = MT7915_TOKEN_SIZE;
136 spin_unlock_bh(&dev->mt76.token_lock);
137
138 /* MT_TXD5_TX_STATUS_HOST (MPDU format) has higher priority than
139 * MT_AGG_ACR_PPDU_TXS2H (PPDU format) even though ACR bit is set.
140 */
141 phy = &dev->phy;
142 mt76_clear(dev, MT_AGG_ACR4(phy->band_idx), MT_AGG_ACR_PPDU_TXS2H);
143
144 phy = dev->mt76.phys[MT_BAND1] ? dev->mt76.phys[MT_BAND1]->priv : NULL;
145 if (phy)
146 mt76_clear(dev, MT_AGG_ACR4(phy->band_idx),
147 MT_AGG_ACR_PPDU_TXS2H);
148 }
149 #endif
150
151 static int
mt7915_pci_wed_init(struct mt7915_dev * dev,struct pci_dev * pdev,int * irq)152 mt7915_pci_wed_init(struct mt7915_dev *dev, struct pci_dev *pdev, int *irq)
153 {
154 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
155 struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
156 int ret;
157
158 if (!wed_enable)
159 return 0;
160
161 wed->wlan.pci_dev = pdev;
162 wed->wlan.wpdma_phys = pci_resource_start(pdev, 0) +
163 MT_WFDMA_EXT_CSR_BASE;
164 wed->wlan.nbuf = 4096;
165 wed->wlan.token_start = MT7915_TOKEN_SIZE - wed->wlan.nbuf;
166 wed->wlan.init_buf = mt7915_wed_init_buf;
167 wed->wlan.offload_enable = mt7915_wed_offload_enable;
168 wed->wlan.offload_disable = mt7915_wed_offload_disable;
169
170 if (mtk_wed_device_attach(wed) != 0)
171 return 0;
172
173 *irq = wed->irq;
174 dev->mt76.dma_dev = wed->dev;
175
176 ret = dma_set_mask(wed->dev, DMA_BIT_MASK(32));
177 if (ret)
178 return ret;
179
180 return 1;
181 #else
182 return 0;
183 #endif
184 }
185
mt7915_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)186 static int mt7915_pci_probe(struct pci_dev *pdev,
187 const struct pci_device_id *id)
188 {
189 struct mt7915_hif *hif2 = NULL;
190 struct mt7915_dev *dev;
191 struct mt76_dev *mdev;
192 int irq;
193 int ret;
194
195 ret = pcim_enable_device(pdev);
196 if (ret)
197 return ret;
198
199 ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
200 if (ret)
201 return ret;
202
203 pci_set_master(pdev);
204
205 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
206 if (ret)
207 return ret;
208
209 mt76_pci_disable_aspm(pdev);
210
211 if (id->device == 0x7916 || id->device == 0x790a)
212 return mt7915_pci_hif2_probe(pdev);
213
214 dev = mt7915_mmio_probe(&pdev->dev, pcim_iomap_table(pdev)[0],
215 id->device);
216 if (IS_ERR(dev))
217 return PTR_ERR(dev);
218
219 mdev = &dev->mt76;
220 mt7915_wfsys_reset(dev);
221 hif2 = mt7915_pci_init_hif2(pdev);
222
223 ret = mt7915_pci_wed_init(dev, pdev, &irq);
224 if (ret < 0)
225 goto free_wed_or_irq_vector;
226
227 if (!ret) {
228 hif2 = mt7915_pci_init_hif2(pdev);
229
230 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
231 if (ret < 0)
232 goto free_device;
233
234 irq = pdev->irq;
235 }
236
237 ret = devm_request_irq(mdev->dev, irq, mt7915_irq_handler,
238 IRQF_SHARED, KBUILD_MODNAME, dev);
239 if (ret)
240 goto free_wed_or_irq_vector;
241
242 /* master switch of PCIe tnterrupt enable */
243 mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff);
244
245 if (hif2) {
246 dev->hif2 = hif2;
247
248 mt76_wr(dev, MT_INT1_MASK_CSR, 0);
249 /* master switch of PCIe tnterrupt enable */
250 if (is_mt7915(mdev))
251 mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0xff);
252 else
253 mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE_MT7916, 0xff);
254
255 ret = devm_request_irq(mdev->dev, dev->hif2->irq,
256 mt7915_irq_handler, IRQF_SHARED,
257 KBUILD_MODNAME "-hif", dev);
258 if (ret)
259 goto free_hif2;
260 }
261
262 ret = mt7915_register_device(dev);
263 if (ret)
264 goto free_hif2_irq;
265
266 return 0;
267
268 free_hif2_irq:
269 if (dev->hif2)
270 devm_free_irq(mdev->dev, dev->hif2->irq, dev);
271 free_hif2:
272 if (dev->hif2)
273 put_device(dev->hif2->dev);
274 devm_free_irq(mdev->dev, irq, dev);
275 free_wed_or_irq_vector:
276 if (mtk_wed_device_active(&mdev->mmio.wed))
277 mtk_wed_device_detach(&mdev->mmio.wed);
278 else
279 pci_free_irq_vectors(pdev);
280 free_device:
281 mt76_free_device(&dev->mt76);
282
283 return ret;
284 }
285
mt7915_hif_remove(struct pci_dev * pdev)286 static void mt7915_hif_remove(struct pci_dev *pdev)
287 {
288 struct mt7915_hif *hif = pci_get_drvdata(pdev);
289
290 list_del(&hif->list);
291 }
292
mt7915_pci_remove(struct pci_dev * pdev)293 static void mt7915_pci_remove(struct pci_dev *pdev)
294 {
295 struct mt76_dev *mdev;
296 struct mt7915_dev *dev;
297
298 mdev = pci_get_drvdata(pdev);
299 dev = container_of(mdev, struct mt7915_dev, mt76);
300 mt7915_put_hif2(dev->hif2);
301 mt7915_unregister_device(dev);
302 }
303
304 struct pci_driver mt7915_hif_driver = {
305 .name = KBUILD_MODNAME "_hif",
306 .id_table = mt7915_hif_device_table,
307 .probe = mt7915_pci_probe,
308 .remove = mt7915_hif_remove,
309 };
310
311 struct pci_driver mt7915_pci_driver = {
312 .name = KBUILD_MODNAME,
313 .id_table = mt7915_pci_device_table,
314 .probe = mt7915_pci_probe,
315 .remove = mt7915_pci_remove,
316 };
317
318 MODULE_DEVICE_TABLE(pci, mt7915_pci_device_table);
319 MODULE_DEVICE_TABLE(pci, mt7915_hif_device_table);
320 MODULE_FIRMWARE(MT7915_FIRMWARE_WA);
321 MODULE_FIRMWARE(MT7915_FIRMWARE_WM);
322 MODULE_FIRMWARE(MT7915_ROM_PATCH);
323 MODULE_FIRMWARE(MT7916_FIRMWARE_WA);
324 MODULE_FIRMWARE(MT7916_FIRMWARE_WM);
325 MODULE_FIRMWARE(MT7916_ROM_PATCH);
326