xref: /freebsd/sys/contrib/dev/mediatek/mt76/mt7996/mmio.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 const struct __base mt7996_reg_base[] = {
15 	[WF_AGG_BASE]		= { { 0x820e2000, 0x820f2000, 0x830e2000 } },
16 	[WF_ARB_BASE]		= { { 0x820e3000, 0x820f3000, 0x830e3000 } },
17 	[WF_TMAC_BASE]		= { { 0x820e4000, 0x820f4000, 0x830e4000 } },
18 	[WF_RMAC_BASE]		= { { 0x820e5000, 0x820f5000, 0x830e5000 } },
19 	[WF_DMA_BASE]		= { { 0x820e7000, 0x820f7000, 0x830e7000 } },
20 	[WF_WTBLOFF_BASE]	= { { 0x820e9000, 0x820f9000, 0x830e9000 } },
21 	[WF_ETBF_BASE]		= { { 0x820ea000, 0x820fa000, 0x830ea000 } },
22 	[WF_LPON_BASE]		= { { 0x820eb000, 0x820fb000, 0x830eb000 } },
23 	[WF_MIB_BASE]		= { { 0x820ed000, 0x820fd000, 0x830ed000 } },
24 	[WF_RATE_BASE]		= { { 0x820ee000, 0x820fe000, 0x830ee000 } },
25 };
26 
27 static const struct __map mt7996_reg_map[] = {
28 	{ 0x54000000, 0x02000, 0x1000 }, /* WFDMA_0 (PCIE0 MCU DMA0) */
29 	{ 0x55000000, 0x03000, 0x1000 }, /* WFDMA_1 (PCIE0 MCU DMA1) */
30 	{ 0x56000000, 0x04000, 0x1000 }, /* WFDMA reserved */
31 	{ 0x57000000, 0x05000, 0x1000 }, /* WFDMA MCU wrap CR */
32 	{ 0x58000000, 0x06000, 0x1000 }, /* WFDMA PCIE1 MCU DMA0 (MEM_DMA) */
33 	{ 0x59000000, 0x07000, 0x1000 }, /* WFDMA PCIE1 MCU DMA1 */
34 	{ 0x820c0000, 0x08000, 0x4000 }, /* WF_UMAC_TOP (PLE) */
35 	{ 0x820c8000, 0x0c000, 0x2000 }, /* WF_UMAC_TOP (PSE) */
36 	{ 0x820cc000, 0x0e000, 0x1000 }, /* WF_UMAC_TOP (PP) */
37 	{ 0x74030000, 0x10000, 0x1000 }, /* PCIe MAC */
38 	{ 0x820e0000, 0x20000, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_CFG) */
39 	{ 0x820e1000, 0x20400, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_TRB) */
40 	{ 0x820e2000, 0x20800, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_AGG) */
41 	{ 0x820e3000, 0x20c00, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_ARB) */
42 	{ 0x820e4000, 0x21000, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_TMAC) */
43 	{ 0x820e5000, 0x21400, 0x0800 }, /* WF_LMAC_TOP BN0 (WF_RMAC) */
44 	{ 0x820ce000, 0x21c00, 0x0200 }, /* WF_LMAC_TOP (WF_SEC) */
45 	{ 0x820e7000, 0x21e00, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_DMA) */
46 	{ 0x820cf000, 0x22000, 0x1000 }, /* WF_LMAC_TOP (WF_PF) */
47 	{ 0x820e9000, 0x23400, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_WTBLOFF) */
48 	{ 0x820ea000, 0x24000, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_ETBF) */
49 	{ 0x820eb000, 0x24200, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_LPON) */
50 	{ 0x820ec000, 0x24600, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_INT) */
51 	{ 0x820ed000, 0x24800, 0x0800 }, /* WF_LMAC_TOP BN0 (WF_MIB) */
52 	{ 0x820ca000, 0x26000, 0x2000 }, /* WF_LMAC_TOP BN0 (WF_MUCOP) */
53 	{ 0x820d0000, 0x30000, 0x10000 }, /* WF_LMAC_TOP (WF_WTBLON) */
54 	{ 0x40000000, 0x70000, 0x10000 }, /* WF_UMAC_SYSRAM */
55 	{ 0x00400000, 0x80000, 0x10000 }, /* WF_MCU_SYSRAM */
56 	{ 0x00410000, 0x90000, 0x10000 }, /* WF_MCU_SYSRAM (configure register) */
57 	{ 0x820f0000, 0xa0000, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_CFG) */
58 	{ 0x820f1000, 0xa0600, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_TRB) */
59 	{ 0x820f2000, 0xa0800, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_AGG) */
60 	{ 0x820f3000, 0xa0c00, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_ARB) */
61 	{ 0x820f4000, 0xa1000, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_TMAC) */
62 	{ 0x820f5000, 0xa1400, 0x0800 }, /* WF_LMAC_TOP BN1 (WF_RMAC) */
63 	{ 0x820f7000, 0xa1e00, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_DMA) */
64 	{ 0x820f9000, 0xa3400, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_WTBLOFF) */
65 	{ 0x820fa000, 0xa4000, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_ETBF) */
66 	{ 0x820fb000, 0xa4200, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_LPON) */
67 	{ 0x820fc000, 0xa4600, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_INT) */
68 	{ 0x820fd000, 0xa4800, 0x0800 }, /* WF_LMAC_TOP BN1 (WF_MIB) */
69 	{ 0x820cc000, 0xa5000, 0x2000 }, /* WF_LMAC_TOP BN1 (WF_MUCOP) */
70 	{ 0x820c4000, 0xa8000, 0x4000 }, /* WF_LMAC_TOP BN1 (WF_MUCOP) */
71 	{ 0x820b0000, 0xae000, 0x1000 }, /* [APB2] WFSYS_ON */
72 	{ 0x80020000, 0xb0000, 0x10000 }, /* WF_TOP_MISC_OFF */
73 	{ 0x81020000, 0xc0000, 0x10000 }, /* WF_TOP_MISC_ON */
74 	{ 0x7c020000, 0xd0000, 0x10000 }, /* CONN_INFRA, wfdma */
75 	{ 0x7c060000, 0xe0000, 0x10000 }, /* CONN_INFRA, conn_host_csr_top */
76 	{ 0x7c000000, 0xf0000, 0x10000 }, /* CONN_INFRA */
77 	{ 0x0, 0x0, 0x0 }, /* imply end of search */
78 };
79 
80 static u32 mt7996_reg_map_l1(struct mt7996_dev *dev, u32 addr)
81 {
82 	u32 offset = FIELD_GET(MT_HIF_REMAP_L1_OFFSET, addr);
83 	u32 base = FIELD_GET(MT_HIF_REMAP_L1_BASE, addr);
84 
85 	dev->reg_l1_backup = dev->bus_ops->rr(&dev->mt76, MT_HIF_REMAP_L1);
86 	dev->bus_ops->rmw(&dev->mt76, MT_HIF_REMAP_L1,
87 			  MT_HIF_REMAP_L1_MASK,
88 			  FIELD_PREP(MT_HIF_REMAP_L1_MASK, base));
89 	/* use read to push write */
90 	dev->bus_ops->rr(&dev->mt76, MT_HIF_REMAP_L1);
91 
92 	return MT_HIF_REMAP_BASE_L1 + offset;
93 }
94 
95 static u32 mt7996_reg_map_l2(struct mt7996_dev *dev, u32 addr)
96 {
97 	u32 offset = FIELD_GET(MT_HIF_REMAP_L2_OFFSET, addr);
98 	u32 base = FIELD_GET(MT_HIF_REMAP_L2_BASE, addr);
99 
100 	dev->reg_l2_backup = dev->bus_ops->rr(&dev->mt76, MT_HIF_REMAP_L2);
101 	dev->bus_ops->rmw(&dev->mt76, MT_HIF_REMAP_L2,
102 			  MT_HIF_REMAP_L2_MASK,
103 			  FIELD_PREP(MT_HIF_REMAP_L2_MASK, base));
104 	/* use read to push write */
105 	dev->bus_ops->rr(&dev->mt76, MT_HIF_REMAP_L2);
106 
107 	return MT_HIF_REMAP_BASE_L2 + offset;
108 }
109 
110 static void mt7996_reg_remap_restore(struct mt7996_dev *dev)
111 {
112 	/* remap to ori status */
113 	if (unlikely(dev->reg_l1_backup)) {
114 		dev->bus_ops->wr(&dev->mt76, MT_HIF_REMAP_L1, dev->reg_l1_backup);
115 		dev->reg_l1_backup = 0;
116 	}
117 
118 	if (dev->reg_l2_backup) {
119 		dev->bus_ops->wr(&dev->mt76, MT_HIF_REMAP_L2, dev->reg_l2_backup);
120 		dev->reg_l2_backup = 0;
121 	}
122 }
123 
124 static u32 __mt7996_reg_addr(struct mt7996_dev *dev, u32 addr)
125 {
126 	int i;
127 
128 	mt7996_reg_remap_restore(dev);
129 
130 	if (addr < 0x100000)
131 		return addr;
132 
133 	for (i = 0; i < dev->reg.map_size; i++) {
134 		u32 ofs;
135 
136 		if (addr < dev->reg.map[i].phys)
137 			continue;
138 
139 		ofs = addr - dev->reg.map[i].phys;
140 		if (ofs > dev->reg.map[i].size)
141 			continue;
142 
143 		return dev->reg.map[i].mapped + ofs;
144 	}
145 
146 	if ((addr >= MT_INFRA_BASE && addr < MT_WFSYS0_PHY_START) ||
147 	    (addr >= MT_WFSYS0_PHY_START && addr < MT_WFSYS1_PHY_START) ||
148 	    (addr >= MT_WFSYS1_PHY_START && addr <= MT_WFSYS1_PHY_END))
149 		return mt7996_reg_map_l1(dev, addr);
150 
151 	if (dev_is_pci(dev->mt76.dev) &&
152 	    ((addr >= MT_CBTOP1_PHY_START && addr <= MT_CBTOP1_PHY_END) ||
153 	    addr >= MT_CBTOP2_PHY_START))
154 		return mt7996_reg_map_l1(dev, addr);
155 
156 	/* CONN_INFRA: covert to phyiscal addr and use layer 1 remap */
157 	if (addr >= MT_INFRA_MCU_START && addr <= MT_INFRA_MCU_END) {
158 		addr = addr - MT_INFRA_MCU_START + MT_INFRA_BASE;
159 		return mt7996_reg_map_l1(dev, addr);
160 	}
161 
162 	return mt7996_reg_map_l2(dev, addr);
163 }
164 
165 void mt7996_memcpy_fromio(struct mt7996_dev *dev, void *buf, u32 offset,
166 			  size_t len)
167 {
168 	u32 addr = __mt7996_reg_addr(dev, offset);
169 
170 #if defined(__linux__)
171 	memcpy_fromio(buf, dev->mt76.mmio.regs + addr, len);
172 #elif defined(__FreeBSD__)
173 	memcpy_fromio(buf, (u8 *)dev->mt76.mmio.regs + addr, len);
174 #endif
175 }
176 
177 static u32 mt7996_rr(struct mt76_dev *mdev, u32 offset)
178 {
179 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
180 
181 	return dev->bus_ops->rr(mdev, __mt7996_reg_addr(dev, offset));
182 }
183 
184 static void mt7996_wr(struct mt76_dev *mdev, u32 offset, u32 val)
185 {
186 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
187 
188 	dev->bus_ops->wr(mdev, __mt7996_reg_addr(dev, offset), val);
189 }
190 
191 static u32 mt7996_rmw(struct mt76_dev *mdev, u32 offset, u32 mask, u32 val)
192 {
193 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
194 
195 	return dev->bus_ops->rmw(mdev, __mt7996_reg_addr(dev, offset), mask, val);
196 }
197 
198 static int mt7996_mmio_init(struct mt76_dev *mdev,
199 			    void __iomem *mem_base,
200 			    u32 device_id)
201 {
202 	struct mt76_bus_ops *bus_ops;
203 	struct mt7996_dev *dev;
204 
205 	dev = container_of(mdev, struct mt7996_dev, mt76);
206 	mt76_mmio_init(&dev->mt76, mem_base);
207 
208 	switch (device_id) {
209 	case 0x7990:
210 		dev->reg.base = mt7996_reg_base;
211 		dev->reg.map = mt7996_reg_map;
212 		dev->reg.map_size = ARRAY_SIZE(mt7996_reg_map);
213 		break;
214 	default:
215 		return -EINVAL;
216 	}
217 
218 	dev->bus_ops = dev->mt76.bus;
219 	bus_ops = devm_kmemdup(dev->mt76.dev, dev->bus_ops, sizeof(*bus_ops),
220 			       GFP_KERNEL);
221 	if (!bus_ops)
222 		return -ENOMEM;
223 
224 	bus_ops->rr = mt7996_rr;
225 	bus_ops->wr = mt7996_wr;
226 	bus_ops->rmw = mt7996_rmw;
227 	dev->mt76.bus = bus_ops;
228 
229 	mdev->rev = (device_id << 16) | (mt76_rr(dev, MT_HW_REV) & 0xff);
230 
231 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
232 
233 	return 0;
234 }
235 
236 void mt7996_dual_hif_set_irq_mask(struct mt7996_dev *dev, bool write_reg,
237 				  u32 clear, u32 set)
238 {
239 	struct mt76_dev *mdev = &dev->mt76;
240 	unsigned long flags;
241 
242 	spin_lock_irqsave(&mdev->mmio.irq_lock, flags);
243 
244 	mdev->mmio.irqmask &= ~clear;
245 	mdev->mmio.irqmask |= set;
246 
247 	if (write_reg) {
248 		mt76_wr(dev, MT_INT_MASK_CSR, mdev->mmio.irqmask);
249 		mt76_wr(dev, MT_INT1_MASK_CSR, mdev->mmio.irqmask);
250 	}
251 
252 	spin_unlock_irqrestore(&mdev->mmio.irq_lock, flags);
253 }
254 
255 static void mt7996_rx_poll_complete(struct mt76_dev *mdev,
256 				    enum mt76_rxq_id q)
257 {
258 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
259 
260 	mt7996_irq_enable(dev, MT_INT_RX(q));
261 }
262 
263 /* TODO: support 2/4/6/8 MSI-X vectors */
264 static void mt7996_irq_tasklet(struct tasklet_struct *t)
265 {
266 	struct mt7996_dev *dev = from_tasklet(dev, t, mt76.irq_tasklet);
267 	u32 i, intr, mask, intr1;
268 
269 	mt76_wr(dev, MT_INT_MASK_CSR, 0);
270 	if (dev->hif2)
271 		mt76_wr(dev, MT_INT1_MASK_CSR, 0);
272 
273 	intr = mt76_rr(dev, MT_INT_SOURCE_CSR);
274 	intr &= dev->mt76.mmio.irqmask;
275 	mt76_wr(dev, MT_INT_SOURCE_CSR, intr);
276 
277 	if (dev->hif2) {
278 		intr1 = mt76_rr(dev, MT_INT1_SOURCE_CSR);
279 		intr1 &= dev->mt76.mmio.irqmask;
280 		mt76_wr(dev, MT_INT1_SOURCE_CSR, intr1);
281 
282 		intr |= intr1;
283 	}
284 
285 	trace_dev_irq(&dev->mt76, intr, dev->mt76.mmio.irqmask);
286 
287 	mask = intr & MT_INT_RX_DONE_ALL;
288 	if (intr & MT_INT_TX_DONE_MCU)
289 		mask |= MT_INT_TX_DONE_MCU;
290 	mt7996_irq_disable(dev, mask);
291 
292 	if (intr & MT_INT_TX_DONE_MCU)
293 		napi_schedule(&dev->mt76.tx_napi);
294 
295 	for (i = 0; i < __MT_RXQ_MAX; i++) {
296 		if ((intr & MT_INT_RX(i)))
297 			napi_schedule(&dev->mt76.napi[i]);
298 	}
299 
300 	if (intr & MT_INT_MCU_CMD) {
301 		u32 val = mt76_rr(dev, MT_MCU_CMD);
302 
303 		mt76_wr(dev, MT_MCU_CMD, val);
304 		if (val & (MT_MCU_CMD_ERROR_MASK | MT_MCU_CMD_WDT_MASK)) {
305 			dev->recovery.state = val;
306 			mt7996_reset(dev);
307 		}
308 	}
309 }
310 
311 irqreturn_t mt7996_irq_handler(int irq, void *dev_instance)
312 {
313 	struct mt7996_dev *dev = dev_instance;
314 
315 	mt76_wr(dev, MT_INT_MASK_CSR, 0);
316 	if (dev->hif2)
317 		mt76_wr(dev, MT_INT1_MASK_CSR, 0);
318 
319 	if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
320 		return IRQ_NONE;
321 
322 	tasklet_schedule(&dev->mt76.irq_tasklet);
323 
324 	return IRQ_HANDLED;
325 }
326 
327 struct mt7996_dev *mt7996_mmio_probe(struct device *pdev,
328 				     void __iomem *mem_base, u32 device_id)
329 {
330 	static const struct mt76_driver_ops drv_ops = {
331 		/* txwi_size = txd size + txp size */
332 		.txwi_size = MT_TXD_SIZE + sizeof(struct mt76_connac_fw_txp),
333 		.drv_flags = MT_DRV_TXWI_NO_FREE |
334 			     MT_DRV_AMSDU_OFFLOAD |
335 			     MT_DRV_HW_MGMT_TXQ,
336 		.survey_flags = SURVEY_INFO_TIME_TX |
337 				SURVEY_INFO_TIME_RX |
338 				SURVEY_INFO_TIME_BSS_RX,
339 		.token_size = MT7996_TOKEN_SIZE,
340 		.tx_prepare_skb = mt7996_tx_prepare_skb,
341 		.tx_complete_skb = mt76_connac_tx_complete_skb,
342 		.rx_skb = mt7996_queue_rx_skb,
343 		.rx_check = mt7996_rx_check,
344 		.rx_poll_complete = mt7996_rx_poll_complete,
345 		.sta_add = mt7996_mac_sta_add,
346 		.sta_remove = mt7996_mac_sta_remove,
347 		.update_survey = mt7996_update_channel,
348 	};
349 	struct mt7996_dev *dev;
350 	struct mt76_dev *mdev;
351 	int ret;
352 
353 	mdev = mt76_alloc_device(pdev, sizeof(*dev), &mt7996_ops, &drv_ops);
354 	if (!mdev)
355 		return ERR_PTR(-ENOMEM);
356 
357 	dev = container_of(mdev, struct mt7996_dev, mt76);
358 
359 	ret = mt7996_mmio_init(mdev, mem_base, device_id);
360 	if (ret)
361 		goto error;
362 
363 	tasklet_setup(&mdev->irq_tasklet, mt7996_irq_tasklet);
364 
365 	mt76_wr(dev, MT_INT_MASK_CSR, 0);
366 
367 	return dev;
368 
369 error:
370 	mt76_free_device(&dev->mt76);
371 
372 	return ERR_PTR(ret);
373 }
374 
375 static int __init mt7996_init(void)
376 {
377 	int ret;
378 
379 	ret = pci_register_driver(&mt7996_hif_driver);
380 	if (ret)
381 		return ret;
382 
383 	ret = pci_register_driver(&mt7996_pci_driver);
384 	if (ret)
385 		pci_unregister_driver(&mt7996_hif_driver);
386 
387 	return ret;
388 }
389 
390 static void __exit mt7996_exit(void)
391 {
392 	pci_unregister_driver(&mt7996_pci_driver);
393 	pci_unregister_driver(&mt7996_hif_driver);
394 }
395 
396 module_init(mt7996_init);
397 module_exit(mt7996_exit);
398 MODULE_LICENSE("Dual BSD/GPL");
399