xref: /freebsd/sys/contrib/dev/mediatek/mt76/mt7996/mmio.c (revision 8ba4d145d351db26e07695b8e90697398c5dfec2)
1cbb3ec25SBjoern A. Zeeb // SPDX-License-Identifier: ISC
2cbb3ec25SBjoern A. Zeeb /*
3cbb3ec25SBjoern A. Zeeb  * Copyright (C) 2022 MediaTek Inc.
4cbb3ec25SBjoern A. Zeeb  */
5cbb3ec25SBjoern A. Zeeb 
6cbb3ec25SBjoern A. Zeeb #include <linux/kernel.h>
7cbb3ec25SBjoern A. Zeeb #include <linux/module.h>
8cbb3ec25SBjoern A. Zeeb #include <linux/pci.h>
9*8ba4d145SBjoern A. Zeeb #include <linux/rtnetlink.h>
10cbb3ec25SBjoern A. Zeeb 
11cbb3ec25SBjoern A. Zeeb #include "mt7996.h"
12cbb3ec25SBjoern A. Zeeb #include "mac.h"
13*8ba4d145SBjoern A. Zeeb #include "mcu.h"
14cbb3ec25SBjoern A. Zeeb #include "../trace.h"
15*8ba4d145SBjoern A. Zeeb #include "../dma.h"
16*8ba4d145SBjoern A. Zeeb 
17*8ba4d145SBjoern A. Zeeb static bool wed_enable;
18*8ba4d145SBjoern A. Zeeb module_param(wed_enable, bool, 0644);
19*8ba4d145SBjoern A. Zeeb #if defined(__FreeBSD__)
20*8ba4d145SBjoern A. Zeeb MODULE_PARM_DESC(wed_enable, "Enable Wireless Ethernet Dispatch support");
21*8ba4d145SBjoern A. Zeeb #endif
22cbb3ec25SBjoern A. Zeeb 
23cbb3ec25SBjoern A. Zeeb static const struct __base mt7996_reg_base[] = {
24cbb3ec25SBjoern A. Zeeb 	[WF_AGG_BASE]		= { { 0x820e2000, 0x820f2000, 0x830e2000 } },
25cbb3ec25SBjoern A. Zeeb 	[WF_ARB_BASE]		= { { 0x820e3000, 0x820f3000, 0x830e3000 } },
26cbb3ec25SBjoern A. Zeeb 	[WF_TMAC_BASE]		= { { 0x820e4000, 0x820f4000, 0x830e4000 } },
27cbb3ec25SBjoern A. Zeeb 	[WF_RMAC_BASE]		= { { 0x820e5000, 0x820f5000, 0x830e5000 } },
28cbb3ec25SBjoern A. Zeeb 	[WF_DMA_BASE]		= { { 0x820e7000, 0x820f7000, 0x830e7000 } },
29cbb3ec25SBjoern A. Zeeb 	[WF_WTBLOFF_BASE]	= { { 0x820e9000, 0x820f9000, 0x830e9000 } },
30cbb3ec25SBjoern A. Zeeb 	[WF_ETBF_BASE]		= { { 0x820ea000, 0x820fa000, 0x830ea000 } },
31cbb3ec25SBjoern A. Zeeb 	[WF_LPON_BASE]		= { { 0x820eb000, 0x820fb000, 0x830eb000 } },
32cbb3ec25SBjoern A. Zeeb 	[WF_MIB_BASE]		= { { 0x820ed000, 0x820fd000, 0x830ed000 } },
33cbb3ec25SBjoern A. Zeeb 	[WF_RATE_BASE]		= { { 0x820ee000, 0x820fe000, 0x830ee000 } },
34cbb3ec25SBjoern A. Zeeb };
35cbb3ec25SBjoern A. Zeeb 
36*8ba4d145SBjoern A. Zeeb static const u32 mt7996_offs[] = {
37*8ba4d145SBjoern A. Zeeb 	[MIB_RVSR0]		= 0x720,
38*8ba4d145SBjoern A. Zeeb 	[MIB_RVSR1]		= 0x724,
39*8ba4d145SBjoern A. Zeeb 	[MIB_BTSCR5]		= 0x788,
40*8ba4d145SBjoern A. Zeeb 	[MIB_BTSCR6]		= 0x798,
41*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR1]		= 0x7ac,
42*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR27]		= 0x954,
43*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR28]		= 0x958,
44*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR29]		= 0x95c,
45*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR30]		= 0x960,
46*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR31]		= 0x964,
47*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR33]		= 0x96c,
48*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR35]		= 0x974,
49*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR36]		= 0x978,
50*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR0]		= 0x9cc,
51*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR1]		= 0x9d0,
52*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR2]		= 0x9d4,
53*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR3]		= 0x9d8,
54*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR4]		= 0x9dc,
55*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR5]		= 0x9e0,
56*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR6]		= 0x9e4,
57*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR7]		= 0x9e8,
58*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR17]		= 0xa10,
59*8ba4d145SBjoern A. Zeeb 	[MIB_TRDR1]		= 0xa28,
60*8ba4d145SBjoern A. Zeeb };
61*8ba4d145SBjoern A. Zeeb 
62*8ba4d145SBjoern A. Zeeb static const u32 mt7992_offs[] = {
63*8ba4d145SBjoern A. Zeeb 	[MIB_RVSR0]		= 0x760,
64*8ba4d145SBjoern A. Zeeb 	[MIB_RVSR1]		= 0x764,
65*8ba4d145SBjoern A. Zeeb 	[MIB_BTSCR5]		= 0x7c8,
66*8ba4d145SBjoern A. Zeeb 	[MIB_BTSCR6]		= 0x7d8,
67*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR1]		= 0x7f0,
68*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR27]		= 0x998,
69*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR28]		= 0x99c,
70*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR29]		= 0x9a0,
71*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR30]		= 0x9a4,
72*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR31]		= 0x9a8,
73*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR33]		= 0x9b0,
74*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR35]		= 0x9b8,
75*8ba4d145SBjoern A. Zeeb 	[MIB_RSCR36]		= 0x9bc,
76*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR0]		= 0xac8,
77*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR1]		= 0xacc,
78*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR2]		= 0xad0,
79*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR3]		= 0xad4,
80*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR4]		= 0xad8,
81*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR5]		= 0xadc,
82*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR6]		= 0xae0,
83*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR7]		= 0xae4,
84*8ba4d145SBjoern A. Zeeb 	[MIB_BSCR17]		= 0xb0c,
85*8ba4d145SBjoern A. Zeeb 	[MIB_TRDR1]		= 0xb24,
86*8ba4d145SBjoern A. Zeeb };
87*8ba4d145SBjoern A. Zeeb 
88cbb3ec25SBjoern A. Zeeb static const struct __map mt7996_reg_map[] = {
89cbb3ec25SBjoern A. Zeeb 	{ 0x54000000, 0x02000, 0x1000 }, /* WFDMA_0 (PCIE0 MCU DMA0) */
90cbb3ec25SBjoern A. Zeeb 	{ 0x55000000, 0x03000, 0x1000 }, /* WFDMA_1 (PCIE0 MCU DMA1) */
91cbb3ec25SBjoern A. Zeeb 	{ 0x56000000, 0x04000, 0x1000 }, /* WFDMA reserved */
92cbb3ec25SBjoern A. Zeeb 	{ 0x57000000, 0x05000, 0x1000 }, /* WFDMA MCU wrap CR */
93cbb3ec25SBjoern A. Zeeb 	{ 0x58000000, 0x06000, 0x1000 }, /* WFDMA PCIE1 MCU DMA0 (MEM_DMA) */
94cbb3ec25SBjoern A. Zeeb 	{ 0x59000000, 0x07000, 0x1000 }, /* WFDMA PCIE1 MCU DMA1 */
95cbb3ec25SBjoern A. Zeeb 	{ 0x820c0000, 0x08000, 0x4000 }, /* WF_UMAC_TOP (PLE) */
96cbb3ec25SBjoern A. Zeeb 	{ 0x820c8000, 0x0c000, 0x2000 }, /* WF_UMAC_TOP (PSE) */
97cbb3ec25SBjoern A. Zeeb 	{ 0x820cc000, 0x0e000, 0x1000 }, /* WF_UMAC_TOP (PP) */
98cbb3ec25SBjoern A. Zeeb 	{ 0x74030000, 0x10000, 0x1000 }, /* PCIe MAC */
99cbb3ec25SBjoern A. Zeeb 	{ 0x820e0000, 0x20000, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_CFG) */
100cbb3ec25SBjoern A. Zeeb 	{ 0x820e1000, 0x20400, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_TRB) */
101cbb3ec25SBjoern A. Zeeb 	{ 0x820e2000, 0x20800, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_AGG) */
102cbb3ec25SBjoern A. Zeeb 	{ 0x820e3000, 0x20c00, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_ARB) */
103cbb3ec25SBjoern A. Zeeb 	{ 0x820e4000, 0x21000, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_TMAC) */
104cbb3ec25SBjoern A. Zeeb 	{ 0x820e5000, 0x21400, 0x0800 }, /* WF_LMAC_TOP BN0 (WF_RMAC) */
105cbb3ec25SBjoern A. Zeeb 	{ 0x820ce000, 0x21c00, 0x0200 }, /* WF_LMAC_TOP (WF_SEC) */
106cbb3ec25SBjoern A. Zeeb 	{ 0x820e7000, 0x21e00, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_DMA) */
107cbb3ec25SBjoern A. Zeeb 	{ 0x820cf000, 0x22000, 0x1000 }, /* WF_LMAC_TOP (WF_PF) */
108cbb3ec25SBjoern A. Zeeb 	{ 0x820e9000, 0x23400, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_WTBLOFF) */
109cbb3ec25SBjoern A. Zeeb 	{ 0x820ea000, 0x24000, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_ETBF) */
110cbb3ec25SBjoern A. Zeeb 	{ 0x820eb000, 0x24200, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_LPON) */
111cbb3ec25SBjoern A. Zeeb 	{ 0x820ec000, 0x24600, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_INT) */
112cbb3ec25SBjoern A. Zeeb 	{ 0x820ed000, 0x24800, 0x0800 }, /* WF_LMAC_TOP BN0 (WF_MIB) */
113cbb3ec25SBjoern A. Zeeb 	{ 0x820ca000, 0x26000, 0x2000 }, /* WF_LMAC_TOP BN0 (WF_MUCOP) */
114cbb3ec25SBjoern A. Zeeb 	{ 0x820d0000, 0x30000, 0x10000 }, /* WF_LMAC_TOP (WF_WTBLON) */
115cbb3ec25SBjoern A. Zeeb 	{ 0x40000000, 0x70000, 0x10000 }, /* WF_UMAC_SYSRAM */
116cbb3ec25SBjoern A. Zeeb 	{ 0x00400000, 0x80000, 0x10000 }, /* WF_MCU_SYSRAM */
117cbb3ec25SBjoern A. Zeeb 	{ 0x00410000, 0x90000, 0x10000 }, /* WF_MCU_SYSRAM (configure register) */
118cbb3ec25SBjoern A. Zeeb 	{ 0x820f0000, 0xa0000, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_CFG) */
119cbb3ec25SBjoern A. Zeeb 	{ 0x820f1000, 0xa0600, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_TRB) */
120cbb3ec25SBjoern A. Zeeb 	{ 0x820f2000, 0xa0800, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_AGG) */
121cbb3ec25SBjoern A. Zeeb 	{ 0x820f3000, 0xa0c00, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_ARB) */
122cbb3ec25SBjoern A. Zeeb 	{ 0x820f4000, 0xa1000, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_TMAC) */
123cbb3ec25SBjoern A. Zeeb 	{ 0x820f5000, 0xa1400, 0x0800 }, /* WF_LMAC_TOP BN1 (WF_RMAC) */
124cbb3ec25SBjoern A. Zeeb 	{ 0x820f7000, 0xa1e00, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_DMA) */
125cbb3ec25SBjoern A. Zeeb 	{ 0x820f9000, 0xa3400, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_WTBLOFF) */
126cbb3ec25SBjoern A. Zeeb 	{ 0x820fa000, 0xa4000, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_ETBF) */
127cbb3ec25SBjoern A. Zeeb 	{ 0x820fb000, 0xa4200, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_LPON) */
128cbb3ec25SBjoern A. Zeeb 	{ 0x820fc000, 0xa4600, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_INT) */
129cbb3ec25SBjoern A. Zeeb 	{ 0x820fd000, 0xa4800, 0x0800 }, /* WF_LMAC_TOP BN1 (WF_MIB) */
130cbb3ec25SBjoern A. Zeeb 	{ 0x820cc000, 0xa5000, 0x2000 }, /* WF_LMAC_TOP BN1 (WF_MUCOP) */
131cbb3ec25SBjoern A. Zeeb 	{ 0x820c4000, 0xa8000, 0x4000 }, /* WF_LMAC_TOP BN1 (WF_MUCOP) */
132cbb3ec25SBjoern A. Zeeb 	{ 0x820b0000, 0xae000, 0x1000 }, /* [APB2] WFSYS_ON */
133cbb3ec25SBjoern A. Zeeb 	{ 0x80020000, 0xb0000, 0x10000 }, /* WF_TOP_MISC_OFF */
134cbb3ec25SBjoern A. Zeeb 	{ 0x81020000, 0xc0000, 0x10000 }, /* WF_TOP_MISC_ON */
135cbb3ec25SBjoern A. Zeeb 	{ 0x7c020000, 0xd0000, 0x10000 }, /* CONN_INFRA, wfdma */
136cbb3ec25SBjoern A. Zeeb 	{ 0x7c060000, 0xe0000, 0x10000 }, /* CONN_INFRA, conn_host_csr_top */
137cbb3ec25SBjoern A. Zeeb 	{ 0x7c000000, 0xf0000, 0x10000 }, /* CONN_INFRA */
138cbb3ec25SBjoern A. Zeeb 	{ 0x0, 0x0, 0x0 }, /* imply end of search */
139cbb3ec25SBjoern A. Zeeb };
140cbb3ec25SBjoern A. Zeeb 
mt7996_reg_map_l1(struct mt7996_dev * dev,u32 addr)141cbb3ec25SBjoern A. Zeeb static u32 mt7996_reg_map_l1(struct mt7996_dev *dev, u32 addr)
142cbb3ec25SBjoern A. Zeeb {
143cbb3ec25SBjoern A. Zeeb 	u32 offset = FIELD_GET(MT_HIF_REMAP_L1_OFFSET, addr);
144cbb3ec25SBjoern A. Zeeb 	u32 base = FIELD_GET(MT_HIF_REMAP_L1_BASE, addr);
145cbb3ec25SBjoern A. Zeeb 
146cbb3ec25SBjoern A. Zeeb 	dev->bus_ops->rmw(&dev->mt76, MT_HIF_REMAP_L1,
147cbb3ec25SBjoern A. Zeeb 			  MT_HIF_REMAP_L1_MASK,
148cbb3ec25SBjoern A. Zeeb 			  FIELD_PREP(MT_HIF_REMAP_L1_MASK, base));
149cbb3ec25SBjoern A. Zeeb 	/* use read to push write */
150cbb3ec25SBjoern A. Zeeb 	dev->bus_ops->rr(&dev->mt76, MT_HIF_REMAP_L1);
151cbb3ec25SBjoern A. Zeeb 
152cbb3ec25SBjoern A. Zeeb 	return MT_HIF_REMAP_BASE_L1 + offset;
153cbb3ec25SBjoern A. Zeeb }
154cbb3ec25SBjoern A. Zeeb 
mt7996_reg_map_l2(struct mt7996_dev * dev,u32 addr)155cbb3ec25SBjoern A. Zeeb static u32 mt7996_reg_map_l2(struct mt7996_dev *dev, u32 addr)
156cbb3ec25SBjoern A. Zeeb {
157cbb3ec25SBjoern A. Zeeb 	u32 offset = FIELD_GET(MT_HIF_REMAP_L2_OFFSET, addr);
158cbb3ec25SBjoern A. Zeeb 	u32 base = FIELD_GET(MT_HIF_REMAP_L2_BASE, addr);
159cbb3ec25SBjoern A. Zeeb 
160cbb3ec25SBjoern A. Zeeb 	dev->bus_ops->rmw(&dev->mt76, MT_HIF_REMAP_L2,
161cbb3ec25SBjoern A. Zeeb 			  MT_HIF_REMAP_L2_MASK,
162cbb3ec25SBjoern A. Zeeb 			  FIELD_PREP(MT_HIF_REMAP_L2_MASK, base));
163cbb3ec25SBjoern A. Zeeb 	/* use read to push write */
164cbb3ec25SBjoern A. Zeeb 	dev->bus_ops->rr(&dev->mt76, MT_HIF_REMAP_L2);
165cbb3ec25SBjoern A. Zeeb 
166cbb3ec25SBjoern A. Zeeb 	return MT_HIF_REMAP_BASE_L2 + offset;
167cbb3ec25SBjoern A. Zeeb }
168cbb3ec25SBjoern A. Zeeb 
__mt7996_reg_addr(struct mt7996_dev * dev,u32 addr)169cbb3ec25SBjoern A. Zeeb static u32 __mt7996_reg_addr(struct mt7996_dev *dev, u32 addr)
170cbb3ec25SBjoern A. Zeeb {
171cbb3ec25SBjoern A. Zeeb 	int i;
172cbb3ec25SBjoern A. Zeeb 
173cbb3ec25SBjoern A. Zeeb 	if (addr < 0x100000)
174cbb3ec25SBjoern A. Zeeb 		return addr;
175cbb3ec25SBjoern A. Zeeb 
176cbb3ec25SBjoern A. Zeeb 	for (i = 0; i < dev->reg.map_size; i++) {
177cbb3ec25SBjoern A. Zeeb 		u32 ofs;
178cbb3ec25SBjoern A. Zeeb 
179cbb3ec25SBjoern A. Zeeb 		if (addr < dev->reg.map[i].phys)
180cbb3ec25SBjoern A. Zeeb 			continue;
181cbb3ec25SBjoern A. Zeeb 
182cbb3ec25SBjoern A. Zeeb 		ofs = addr - dev->reg.map[i].phys;
183*8ba4d145SBjoern A. Zeeb 		if (ofs >= dev->reg.map[i].size)
184cbb3ec25SBjoern A. Zeeb 			continue;
185cbb3ec25SBjoern A. Zeeb 
186cbb3ec25SBjoern A. Zeeb 		return dev->reg.map[i].mapped + ofs;
187cbb3ec25SBjoern A. Zeeb 	}
188cbb3ec25SBjoern A. Zeeb 
189*8ba4d145SBjoern A. Zeeb 	return 0;
190*8ba4d145SBjoern A. Zeeb }
191*8ba4d145SBjoern A. Zeeb 
__mt7996_reg_remap_addr(struct mt7996_dev * dev,u32 addr)192*8ba4d145SBjoern A. Zeeb static u32 __mt7996_reg_remap_addr(struct mt7996_dev *dev, u32 addr)
193*8ba4d145SBjoern A. Zeeb {
194cbb3ec25SBjoern A. Zeeb 	if ((addr >= MT_INFRA_BASE && addr < MT_WFSYS0_PHY_START) ||
195cbb3ec25SBjoern A. Zeeb 	    (addr >= MT_WFSYS0_PHY_START && addr < MT_WFSYS1_PHY_START) ||
196cbb3ec25SBjoern A. Zeeb 	    (addr >= MT_WFSYS1_PHY_START && addr <= MT_WFSYS1_PHY_END))
197cbb3ec25SBjoern A. Zeeb 		return mt7996_reg_map_l1(dev, addr);
198cbb3ec25SBjoern A. Zeeb 
199cbb3ec25SBjoern A. Zeeb 	if (dev_is_pci(dev->mt76.dev) &&
200cbb3ec25SBjoern A. Zeeb 	    ((addr >= MT_CBTOP1_PHY_START && addr <= MT_CBTOP1_PHY_END) ||
201cbb3ec25SBjoern A. Zeeb 	    addr >= MT_CBTOP2_PHY_START))
202cbb3ec25SBjoern A. Zeeb 		return mt7996_reg_map_l1(dev, addr);
203cbb3ec25SBjoern A. Zeeb 
204cbb3ec25SBjoern A. Zeeb 	/* CONN_INFRA: covert to phyiscal addr and use layer 1 remap */
205cbb3ec25SBjoern A. Zeeb 	if (addr >= MT_INFRA_MCU_START && addr <= MT_INFRA_MCU_END) {
206cbb3ec25SBjoern A. Zeeb 		addr = addr - MT_INFRA_MCU_START + MT_INFRA_BASE;
207cbb3ec25SBjoern A. Zeeb 		return mt7996_reg_map_l1(dev, addr);
208cbb3ec25SBjoern A. Zeeb 	}
209cbb3ec25SBjoern A. Zeeb 
210cbb3ec25SBjoern A. Zeeb 	return mt7996_reg_map_l2(dev, addr);
211cbb3ec25SBjoern A. Zeeb }
212cbb3ec25SBjoern A. Zeeb 
mt7996_memcpy_fromio(struct mt7996_dev * dev,void * buf,u32 offset,size_t len)213cbb3ec25SBjoern A. Zeeb void mt7996_memcpy_fromio(struct mt7996_dev *dev, void *buf, u32 offset,
214cbb3ec25SBjoern A. Zeeb 			  size_t len)
215cbb3ec25SBjoern A. Zeeb {
216cbb3ec25SBjoern A. Zeeb 	u32 addr = __mt7996_reg_addr(dev, offset);
217cbb3ec25SBjoern A. Zeeb 
218*8ba4d145SBjoern A. Zeeb 	if (addr) {
219cbb3ec25SBjoern A. Zeeb #if defined(__linux__)
220cbb3ec25SBjoern A. Zeeb 		memcpy_fromio(buf, dev->mt76.mmio.regs + addr, len);
221cbb3ec25SBjoern A. Zeeb #elif defined(__FreeBSD__)
222cbb3ec25SBjoern A. Zeeb 		memcpy_fromio(buf, (u8 *)dev->mt76.mmio.regs + addr, len);
223cbb3ec25SBjoern A. Zeeb #endif
224*8ba4d145SBjoern A. Zeeb 		return;
225*8ba4d145SBjoern A. Zeeb 	}
226*8ba4d145SBjoern A. Zeeb 
227*8ba4d145SBjoern A. Zeeb 	spin_lock_bh(&dev->reg_lock);
228*8ba4d145SBjoern A. Zeeb #if defined(__linux__)
229*8ba4d145SBjoern A. Zeeb 	memcpy_fromio(buf, dev->mt76.mmio.regs +
230*8ba4d145SBjoern A. Zeeb #elif defined(__FreeBSD__)
231*8ba4d145SBjoern A. Zeeb 	memcpy_fromio(buf, (u8 *)dev->mt76.mmio.regs +
232*8ba4d145SBjoern A. Zeeb #endif
233*8ba4d145SBjoern A. Zeeb 			   __mt7996_reg_remap_addr(dev, offset), len);
234*8ba4d145SBjoern A. Zeeb 	spin_unlock_bh(&dev->reg_lock);
235cbb3ec25SBjoern A. Zeeb }
236cbb3ec25SBjoern A. Zeeb 
mt7996_rr(struct mt76_dev * mdev,u32 offset)237cbb3ec25SBjoern A. Zeeb static u32 mt7996_rr(struct mt76_dev *mdev, u32 offset)
238cbb3ec25SBjoern A. Zeeb {
239cbb3ec25SBjoern A. Zeeb 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
240*8ba4d145SBjoern A. Zeeb 	u32 addr = __mt7996_reg_addr(dev, offset), val;
241cbb3ec25SBjoern A. Zeeb 
242*8ba4d145SBjoern A. Zeeb 	if (addr)
243*8ba4d145SBjoern A. Zeeb 		return dev->bus_ops->rr(mdev, addr);
244*8ba4d145SBjoern A. Zeeb 
245*8ba4d145SBjoern A. Zeeb 	spin_lock_bh(&dev->reg_lock);
246*8ba4d145SBjoern A. Zeeb 	val = dev->bus_ops->rr(mdev, __mt7996_reg_remap_addr(dev, offset));
247*8ba4d145SBjoern A. Zeeb 	spin_unlock_bh(&dev->reg_lock);
248*8ba4d145SBjoern A. Zeeb 
249*8ba4d145SBjoern A. Zeeb 	return val;
250cbb3ec25SBjoern A. Zeeb }
251cbb3ec25SBjoern A. Zeeb 
mt7996_wr(struct mt76_dev * mdev,u32 offset,u32 val)252cbb3ec25SBjoern A. Zeeb static void mt7996_wr(struct mt76_dev *mdev, u32 offset, u32 val)
253cbb3ec25SBjoern A. Zeeb {
254cbb3ec25SBjoern A. Zeeb 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
255*8ba4d145SBjoern A. Zeeb 	u32 addr = __mt7996_reg_addr(dev, offset);
256cbb3ec25SBjoern A. Zeeb 
257*8ba4d145SBjoern A. Zeeb 	if (addr) {
258*8ba4d145SBjoern A. Zeeb 		dev->bus_ops->wr(mdev, addr, val);
259*8ba4d145SBjoern A. Zeeb 		return;
260*8ba4d145SBjoern A. Zeeb 	}
261*8ba4d145SBjoern A. Zeeb 
262*8ba4d145SBjoern A. Zeeb 	spin_lock_bh(&dev->reg_lock);
263*8ba4d145SBjoern A. Zeeb 	dev->bus_ops->wr(mdev, __mt7996_reg_remap_addr(dev, offset), val);
264*8ba4d145SBjoern A. Zeeb 	spin_unlock_bh(&dev->reg_lock);
265cbb3ec25SBjoern A. Zeeb }
266cbb3ec25SBjoern A. Zeeb 
mt7996_rmw(struct mt76_dev * mdev,u32 offset,u32 mask,u32 val)267cbb3ec25SBjoern A. Zeeb static u32 mt7996_rmw(struct mt76_dev *mdev, u32 offset, u32 mask, u32 val)
268cbb3ec25SBjoern A. Zeeb {
269cbb3ec25SBjoern A. Zeeb 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
270*8ba4d145SBjoern A. Zeeb 	u32 addr = __mt7996_reg_addr(dev, offset);
271cbb3ec25SBjoern A. Zeeb 
272*8ba4d145SBjoern A. Zeeb 	if (addr)
273*8ba4d145SBjoern A. Zeeb 		return dev->bus_ops->rmw(mdev, addr, mask, val);
274*8ba4d145SBjoern A. Zeeb 
275*8ba4d145SBjoern A. Zeeb 	spin_lock_bh(&dev->reg_lock);
276*8ba4d145SBjoern A. Zeeb 	val = dev->bus_ops->rmw(mdev, __mt7996_reg_remap_addr(dev, offset), mask, val);
277*8ba4d145SBjoern A. Zeeb 	spin_unlock_bh(&dev->reg_lock);
278*8ba4d145SBjoern A. Zeeb 
279*8ba4d145SBjoern A. Zeeb 	return val;
280*8ba4d145SBjoern A. Zeeb }
281*8ba4d145SBjoern A. Zeeb 
282*8ba4d145SBjoern A. Zeeb #ifdef CONFIG_NET_MEDIATEK_SOC_WED
mt7996_mmio_wed_reset(struct mtk_wed_device * wed)283*8ba4d145SBjoern A. Zeeb static int mt7996_mmio_wed_reset(struct mtk_wed_device *wed)
284*8ba4d145SBjoern A. Zeeb {
285*8ba4d145SBjoern A. Zeeb 	struct mt76_dev *mdev = container_of(wed, struct mt76_dev, mmio.wed);
286*8ba4d145SBjoern A. Zeeb 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
287*8ba4d145SBjoern A. Zeeb 	struct mt76_phy *mphy = &dev->mphy;
288*8ba4d145SBjoern A. Zeeb 	int ret;
289*8ba4d145SBjoern A. Zeeb 
290*8ba4d145SBjoern A. Zeeb 	ASSERT_RTNL();
291*8ba4d145SBjoern A. Zeeb 
292*8ba4d145SBjoern A. Zeeb 	if (test_and_set_bit(MT76_STATE_WED_RESET, &mphy->state))
293*8ba4d145SBjoern A. Zeeb 		return -EBUSY;
294*8ba4d145SBjoern A. Zeeb 
295*8ba4d145SBjoern A. Zeeb 	ret = mt7996_mcu_set_ser(dev, UNI_CMD_SER_TRIGGER, UNI_CMD_SER_SET_RECOVER_L1,
296*8ba4d145SBjoern A. Zeeb 				 mphy->band_idx);
297*8ba4d145SBjoern A. Zeeb 	if (ret)
298*8ba4d145SBjoern A. Zeeb 		goto out;
299*8ba4d145SBjoern A. Zeeb 
300*8ba4d145SBjoern A. Zeeb 	rtnl_unlock();
301*8ba4d145SBjoern A. Zeeb 	if (!wait_for_completion_timeout(&mdev->mmio.wed_reset, 20 * HZ)) {
302*8ba4d145SBjoern A. Zeeb 		dev_err(mdev->dev, "wed reset timeout\n");
303*8ba4d145SBjoern A. Zeeb 		ret = -ETIMEDOUT;
304*8ba4d145SBjoern A. Zeeb 	}
305*8ba4d145SBjoern A. Zeeb 	rtnl_lock();
306*8ba4d145SBjoern A. Zeeb out:
307*8ba4d145SBjoern A. Zeeb 	clear_bit(MT76_STATE_WED_RESET, &mphy->state);
308*8ba4d145SBjoern A. Zeeb 
309*8ba4d145SBjoern A. Zeeb 	return ret;
310*8ba4d145SBjoern A. Zeeb }
311*8ba4d145SBjoern A. Zeeb #endif
312*8ba4d145SBjoern A. Zeeb 
mt7996_mmio_wed_init(struct mt7996_dev * dev,void * pdev_ptr,bool hif2,int * irq)313*8ba4d145SBjoern A. Zeeb int mt7996_mmio_wed_init(struct mt7996_dev *dev, void *pdev_ptr,
314*8ba4d145SBjoern A. Zeeb 			 bool hif2, int *irq)
315*8ba4d145SBjoern A. Zeeb {
316*8ba4d145SBjoern A. Zeeb #ifdef CONFIG_NET_MEDIATEK_SOC_WED
317*8ba4d145SBjoern A. Zeeb 	struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
318*8ba4d145SBjoern A. Zeeb 	struct pci_dev *pci_dev = pdev_ptr;
319*8ba4d145SBjoern A. Zeeb 	u32 hif1_ofs = 0;
320*8ba4d145SBjoern A. Zeeb 
321*8ba4d145SBjoern A. Zeeb 	if (!wed_enable)
322*8ba4d145SBjoern A. Zeeb 		return 0;
323*8ba4d145SBjoern A. Zeeb 
324*8ba4d145SBjoern A. Zeeb 	dev->has_rro = true;
325*8ba4d145SBjoern A. Zeeb 
326*8ba4d145SBjoern A. Zeeb 	hif1_ofs = MT_WFDMA0_PCIE1(0) - MT_WFDMA0(0);
327*8ba4d145SBjoern A. Zeeb 
328*8ba4d145SBjoern A. Zeeb 	if (hif2)
329*8ba4d145SBjoern A. Zeeb 		wed = &dev->mt76.mmio.wed_hif2;
330*8ba4d145SBjoern A. Zeeb 
331*8ba4d145SBjoern A. Zeeb 	wed->wlan.pci_dev = pci_dev;
332*8ba4d145SBjoern A. Zeeb 	wed->wlan.bus_type = MTK_WED_BUS_PCIE;
333*8ba4d145SBjoern A. Zeeb 
334*8ba4d145SBjoern A. Zeeb 	wed->wlan.base = devm_ioremap(dev->mt76.dev,
335*8ba4d145SBjoern A. Zeeb 				      pci_resource_start(pci_dev, 0),
336*8ba4d145SBjoern A. Zeeb 				      pci_resource_len(pci_dev, 0));
337*8ba4d145SBjoern A. Zeeb 	wed->wlan.phy_base = pci_resource_start(pci_dev, 0);
338*8ba4d145SBjoern A. Zeeb 
339*8ba4d145SBjoern A. Zeeb 	if (hif2) {
340*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_int = wed->wlan.phy_base +
341*8ba4d145SBjoern A. Zeeb 				      MT_INT_PCIE1_SOURCE_CSR_EXT;
342*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_mask = wed->wlan.phy_base +
343*8ba4d145SBjoern A. Zeeb 				       MT_INT_PCIE1_MASK_CSR;
344*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_tx = wed->wlan.phy_base + hif1_ofs +
345*8ba4d145SBjoern A. Zeeb 					     MT_TXQ_RING_BASE(0) +
346*8ba4d145SBjoern A. Zeeb 					     MT7996_TXQ_BAND2 * MT_RING_SIZE;
347*8ba4d145SBjoern A. Zeeb 		if (dev->has_rro) {
348*8ba4d145SBjoern A. Zeeb 			wed->wlan.wpdma_txfree = wed->wlan.phy_base + hif1_ofs +
349*8ba4d145SBjoern A. Zeeb 						 MT_RXQ_RING_BASE(0) +
350*8ba4d145SBjoern A. Zeeb 						 MT7996_RXQ_TXFREE2 * MT_RING_SIZE;
351*8ba4d145SBjoern A. Zeeb 			wed->wlan.txfree_tbit = ffs(MT_INT_RX_TXFREE_EXT) - 1;
352*8ba4d145SBjoern A. Zeeb 		} else {
353*8ba4d145SBjoern A. Zeeb 			wed->wlan.wpdma_txfree = wed->wlan.phy_base + hif1_ofs +
354*8ba4d145SBjoern A. Zeeb 						 MT_RXQ_RING_BASE(0) +
355*8ba4d145SBjoern A. Zeeb 						 MT7996_RXQ_MCU_WA_TRI * MT_RING_SIZE;
356*8ba4d145SBjoern A. Zeeb 			wed->wlan.txfree_tbit = ffs(MT_INT_RX_DONE_WA_TRI) - 1;
357*8ba4d145SBjoern A. Zeeb 		}
358*8ba4d145SBjoern A. Zeeb 
359*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_rx_glo = wed->wlan.phy_base + hif1_ofs + MT_WFDMA0_GLO_CFG;
360*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_rx = wed->wlan.phy_base + hif1_ofs +
361*8ba4d145SBjoern A. Zeeb 				     MT_RXQ_RING_BASE(MT7996_RXQ_BAND0) +
362*8ba4d145SBjoern A. Zeeb 				     MT7996_RXQ_BAND0 * MT_RING_SIZE;
363*8ba4d145SBjoern A. Zeeb 
364*8ba4d145SBjoern A. Zeeb 		wed->wlan.id = 0x7991;
365*8ba4d145SBjoern A. Zeeb 		wed->wlan.tx_tbit[0] = ffs(MT_INT_TX_DONE_BAND2) - 1;
366*8ba4d145SBjoern A. Zeeb 	} else {
367*8ba4d145SBjoern A. Zeeb 		wed->wlan.hw_rro = dev->has_rro; /* default on */
368*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_int = wed->wlan.phy_base + MT_INT_SOURCE_CSR;
369*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_mask = wed->wlan.phy_base + MT_INT_MASK_CSR;
370*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_tx = wed->wlan.phy_base + MT_TXQ_RING_BASE(0) +
371*8ba4d145SBjoern A. Zeeb 				     MT7996_TXQ_BAND0 * MT_RING_SIZE;
372*8ba4d145SBjoern A. Zeeb 
373*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_rx_glo = wed->wlan.phy_base + MT_WFDMA0_GLO_CFG;
374*8ba4d145SBjoern A. Zeeb 
375*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_rx = wed->wlan.phy_base +
376*8ba4d145SBjoern A. Zeeb 				     MT_RXQ_RING_BASE(MT7996_RXQ_BAND0) +
377*8ba4d145SBjoern A. Zeeb 				     MT7996_RXQ_BAND0 * MT_RING_SIZE;
378*8ba4d145SBjoern A. Zeeb 
379*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_rx_rro[0] = wed->wlan.phy_base +
380*8ba4d145SBjoern A. Zeeb 					    MT_RXQ_RING_BASE(MT7996_RXQ_RRO_BAND0) +
381*8ba4d145SBjoern A. Zeeb 					    MT7996_RXQ_RRO_BAND0 * MT_RING_SIZE;
382*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_rx_rro[1] = wed->wlan.phy_base + hif1_ofs +
383*8ba4d145SBjoern A. Zeeb 					    MT_RXQ_RING_BASE(MT7996_RXQ_RRO_BAND2) +
384*8ba4d145SBjoern A. Zeeb 					    MT7996_RXQ_RRO_BAND2 * MT_RING_SIZE;
385*8ba4d145SBjoern A. Zeeb 		wed->wlan.wpdma_rx_pg = wed->wlan.phy_base +
386*8ba4d145SBjoern A. Zeeb 					MT_RXQ_RING_BASE(MT7996_RXQ_MSDU_PG_BAND0) +
387*8ba4d145SBjoern A. Zeeb 					MT7996_RXQ_MSDU_PG_BAND0 * MT_RING_SIZE;
388*8ba4d145SBjoern A. Zeeb 
389*8ba4d145SBjoern A. Zeeb 		wed->wlan.rx_nbuf = 65536;
390*8ba4d145SBjoern A. Zeeb 		wed->wlan.rx_npkt = dev->hif2 ? 32768 : 24576;
391*8ba4d145SBjoern A. Zeeb 		wed->wlan.rx_size = SKB_WITH_OVERHEAD(MT_RX_BUF_SIZE);
392*8ba4d145SBjoern A. Zeeb 
393*8ba4d145SBjoern A. Zeeb 		wed->wlan.rx_tbit[0] = ffs(MT_INT_RX_DONE_BAND0) - 1;
394*8ba4d145SBjoern A. Zeeb 		wed->wlan.rx_tbit[1] = ffs(MT_INT_RX_DONE_BAND2) - 1;
395*8ba4d145SBjoern A. Zeeb 
396*8ba4d145SBjoern A. Zeeb 		wed->wlan.rro_rx_tbit[0] = ffs(MT_INT_RX_DONE_RRO_BAND0) - 1;
397*8ba4d145SBjoern A. Zeeb 		wed->wlan.rro_rx_tbit[1] = ffs(MT_INT_RX_DONE_RRO_BAND2) - 1;
398*8ba4d145SBjoern A. Zeeb 
399*8ba4d145SBjoern A. Zeeb 		wed->wlan.rx_pg_tbit[0] = ffs(MT_INT_RX_DONE_MSDU_PG_BAND0) - 1;
400*8ba4d145SBjoern A. Zeeb 		wed->wlan.rx_pg_tbit[1] = ffs(MT_INT_RX_DONE_MSDU_PG_BAND1) - 1;
401*8ba4d145SBjoern A. Zeeb 		wed->wlan.rx_pg_tbit[2] = ffs(MT_INT_RX_DONE_MSDU_PG_BAND2) - 1;
402*8ba4d145SBjoern A. Zeeb 
403*8ba4d145SBjoern A. Zeeb 		wed->wlan.tx_tbit[0] = ffs(MT_INT_TX_DONE_BAND0) - 1;
404*8ba4d145SBjoern A. Zeeb 		wed->wlan.tx_tbit[1] = ffs(MT_INT_TX_DONE_BAND1) - 1;
405*8ba4d145SBjoern A. Zeeb 		if (dev->has_rro) {
406*8ba4d145SBjoern A. Zeeb 			wed->wlan.wpdma_txfree = wed->wlan.phy_base + MT_RXQ_RING_BASE(0) +
407*8ba4d145SBjoern A. Zeeb 						 MT7996_RXQ_TXFREE0 * MT_RING_SIZE;
408*8ba4d145SBjoern A. Zeeb 			wed->wlan.txfree_tbit = ffs(MT_INT_RX_TXFREE_MAIN) - 1;
409*8ba4d145SBjoern A. Zeeb 		} else {
410*8ba4d145SBjoern A. Zeeb 			wed->wlan.txfree_tbit = ffs(MT_INT_RX_DONE_WA_MAIN) - 1;
411*8ba4d145SBjoern A. Zeeb 			wed->wlan.wpdma_txfree = wed->wlan.phy_base + MT_RXQ_RING_BASE(0) +
412*8ba4d145SBjoern A. Zeeb 						  MT7996_RXQ_MCU_WA_MAIN * MT_RING_SIZE;
413*8ba4d145SBjoern A. Zeeb 		}
414*8ba4d145SBjoern A. Zeeb 		dev->mt76.rx_token_size = MT7996_TOKEN_SIZE + wed->wlan.rx_npkt;
415*8ba4d145SBjoern A. Zeeb 	}
416*8ba4d145SBjoern A. Zeeb 
417*8ba4d145SBjoern A. Zeeb 	wed->wlan.nbuf = MT7996_HW_TOKEN_SIZE;
418*8ba4d145SBjoern A. Zeeb 	wed->wlan.token_start = MT7996_TOKEN_SIZE - wed->wlan.nbuf;
419*8ba4d145SBjoern A. Zeeb 
420*8ba4d145SBjoern A. Zeeb 	wed->wlan.amsdu_max_subframes = 8;
421*8ba4d145SBjoern A. Zeeb 	wed->wlan.amsdu_max_len = 1536;
422*8ba4d145SBjoern A. Zeeb 
423*8ba4d145SBjoern A. Zeeb 	wed->wlan.init_buf = mt7996_wed_init_buf;
424*8ba4d145SBjoern A. Zeeb 	wed->wlan.init_rx_buf = mt76_wed_init_rx_buf;
425*8ba4d145SBjoern A. Zeeb 	wed->wlan.release_rx_buf = mt76_wed_release_rx_buf;
426*8ba4d145SBjoern A. Zeeb 	wed->wlan.offload_enable = mt76_wed_offload_enable;
427*8ba4d145SBjoern A. Zeeb 	wed->wlan.offload_disable = mt76_wed_offload_disable;
428*8ba4d145SBjoern A. Zeeb 	if (!hif2) {
429*8ba4d145SBjoern A. Zeeb 		wed->wlan.reset = mt7996_mmio_wed_reset;
430*8ba4d145SBjoern A. Zeeb 		wed->wlan.reset_complete = mt76_wed_reset_complete;
431*8ba4d145SBjoern A. Zeeb 	}
432*8ba4d145SBjoern A. Zeeb 
433*8ba4d145SBjoern A. Zeeb 	if (mtk_wed_device_attach(wed))
434*8ba4d145SBjoern A. Zeeb 		return 0;
435*8ba4d145SBjoern A. Zeeb 
436*8ba4d145SBjoern A. Zeeb 	*irq = wed->irq;
437*8ba4d145SBjoern A. Zeeb 	dev->mt76.dma_dev = wed->dev;
438*8ba4d145SBjoern A. Zeeb 
439*8ba4d145SBjoern A. Zeeb 	return 1;
440*8ba4d145SBjoern A. Zeeb #else
441*8ba4d145SBjoern A. Zeeb 	return 0;
442*8ba4d145SBjoern A. Zeeb #endif
443cbb3ec25SBjoern A. Zeeb }
444cbb3ec25SBjoern A. Zeeb 
mt7996_mmio_init(struct mt76_dev * mdev,void __iomem * mem_base,u32 device_id)445cbb3ec25SBjoern A. Zeeb static int mt7996_mmio_init(struct mt76_dev *mdev,
446cbb3ec25SBjoern A. Zeeb 			    void __iomem *mem_base,
447cbb3ec25SBjoern A. Zeeb 			    u32 device_id)
448cbb3ec25SBjoern A. Zeeb {
449cbb3ec25SBjoern A. Zeeb 	struct mt76_bus_ops *bus_ops;
450cbb3ec25SBjoern A. Zeeb 	struct mt7996_dev *dev;
451cbb3ec25SBjoern A. Zeeb 
452cbb3ec25SBjoern A. Zeeb 	dev = container_of(mdev, struct mt7996_dev, mt76);
453cbb3ec25SBjoern A. Zeeb 	mt76_mmio_init(&dev->mt76, mem_base);
454*8ba4d145SBjoern A. Zeeb 	spin_lock_init(&dev->reg_lock);
455cbb3ec25SBjoern A. Zeeb 
456cbb3ec25SBjoern A. Zeeb 	switch (device_id) {
457cbb3ec25SBjoern A. Zeeb 	case 0x7990:
458cbb3ec25SBjoern A. Zeeb 		dev->reg.base = mt7996_reg_base;
459*8ba4d145SBjoern A. Zeeb 		dev->reg.offs_rev = mt7996_offs;
460*8ba4d145SBjoern A. Zeeb 		dev->reg.map = mt7996_reg_map;
461*8ba4d145SBjoern A. Zeeb 		dev->reg.map_size = ARRAY_SIZE(mt7996_reg_map);
462*8ba4d145SBjoern A. Zeeb 		break;
463*8ba4d145SBjoern A. Zeeb 	case 0x7992:
464*8ba4d145SBjoern A. Zeeb 		dev->reg.base = mt7996_reg_base;
465*8ba4d145SBjoern A. Zeeb 		dev->reg.offs_rev = mt7992_offs;
466cbb3ec25SBjoern A. Zeeb 		dev->reg.map = mt7996_reg_map;
467cbb3ec25SBjoern A. Zeeb 		dev->reg.map_size = ARRAY_SIZE(mt7996_reg_map);
468cbb3ec25SBjoern A. Zeeb 		break;
469cbb3ec25SBjoern A. Zeeb 	default:
470cbb3ec25SBjoern A. Zeeb 		return -EINVAL;
471cbb3ec25SBjoern A. Zeeb 	}
472cbb3ec25SBjoern A. Zeeb 
473cbb3ec25SBjoern A. Zeeb 	dev->bus_ops = dev->mt76.bus;
474cbb3ec25SBjoern A. Zeeb 	bus_ops = devm_kmemdup(dev->mt76.dev, dev->bus_ops, sizeof(*bus_ops),
475cbb3ec25SBjoern A. Zeeb 			       GFP_KERNEL);
476cbb3ec25SBjoern A. Zeeb 	if (!bus_ops)
477cbb3ec25SBjoern A. Zeeb 		return -ENOMEM;
478cbb3ec25SBjoern A. Zeeb 
479cbb3ec25SBjoern A. Zeeb 	bus_ops->rr = mt7996_rr;
480cbb3ec25SBjoern A. Zeeb 	bus_ops->wr = mt7996_wr;
481cbb3ec25SBjoern A. Zeeb 	bus_ops->rmw = mt7996_rmw;
482cbb3ec25SBjoern A. Zeeb 	dev->mt76.bus = bus_ops;
483cbb3ec25SBjoern A. Zeeb 
484cbb3ec25SBjoern A. Zeeb 	mdev->rev = (device_id << 16) | (mt76_rr(dev, MT_HW_REV) & 0xff);
485cbb3ec25SBjoern A. Zeeb 
486cbb3ec25SBjoern A. Zeeb 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
487cbb3ec25SBjoern A. Zeeb 
488cbb3ec25SBjoern A. Zeeb 	return 0;
489cbb3ec25SBjoern A. Zeeb }
490cbb3ec25SBjoern A. Zeeb 
mt7996_dual_hif_set_irq_mask(struct mt7996_dev * dev,bool write_reg,u32 clear,u32 set)491cbb3ec25SBjoern A. Zeeb void mt7996_dual_hif_set_irq_mask(struct mt7996_dev *dev, bool write_reg,
492cbb3ec25SBjoern A. Zeeb 				  u32 clear, u32 set)
493cbb3ec25SBjoern A. Zeeb {
494cbb3ec25SBjoern A. Zeeb 	struct mt76_dev *mdev = &dev->mt76;
495cbb3ec25SBjoern A. Zeeb 	unsigned long flags;
496cbb3ec25SBjoern A. Zeeb 
497cbb3ec25SBjoern A. Zeeb 	spin_lock_irqsave(&mdev->mmio.irq_lock, flags);
498cbb3ec25SBjoern A. Zeeb 
499cbb3ec25SBjoern A. Zeeb 	mdev->mmio.irqmask &= ~clear;
500cbb3ec25SBjoern A. Zeeb 	mdev->mmio.irqmask |= set;
501cbb3ec25SBjoern A. Zeeb 
502cbb3ec25SBjoern A. Zeeb 	if (write_reg) {
503*8ba4d145SBjoern A. Zeeb 		if (mtk_wed_device_active(&mdev->mmio.wed)) {
504*8ba4d145SBjoern A. Zeeb 			mtk_wed_device_irq_set_mask(&mdev->mmio.wed,
505*8ba4d145SBjoern A. Zeeb 						    mdev->mmio.irqmask);
506*8ba4d145SBjoern A. Zeeb 			if (mtk_wed_device_active(&mdev->mmio.wed_hif2)) {
507*8ba4d145SBjoern A. Zeeb 				mtk_wed_device_irq_set_mask(&mdev->mmio.wed_hif2,
508*8ba4d145SBjoern A. Zeeb 							    mdev->mmio.irqmask);
509*8ba4d145SBjoern A. Zeeb 			}
510*8ba4d145SBjoern A. Zeeb 		} else {
511cbb3ec25SBjoern A. Zeeb 			mt76_wr(dev, MT_INT_MASK_CSR, mdev->mmio.irqmask);
512cbb3ec25SBjoern A. Zeeb 			mt76_wr(dev, MT_INT1_MASK_CSR, mdev->mmio.irqmask);
513cbb3ec25SBjoern A. Zeeb 		}
514*8ba4d145SBjoern A. Zeeb 	}
515cbb3ec25SBjoern A. Zeeb 
516cbb3ec25SBjoern A. Zeeb 	spin_unlock_irqrestore(&mdev->mmio.irq_lock, flags);
517cbb3ec25SBjoern A. Zeeb }
518cbb3ec25SBjoern A. Zeeb 
mt7996_rx_poll_complete(struct mt76_dev * mdev,enum mt76_rxq_id q)519cbb3ec25SBjoern A. Zeeb static void mt7996_rx_poll_complete(struct mt76_dev *mdev,
520cbb3ec25SBjoern A. Zeeb 				    enum mt76_rxq_id q)
521cbb3ec25SBjoern A. Zeeb {
522cbb3ec25SBjoern A. Zeeb 	struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
523cbb3ec25SBjoern A. Zeeb 
524cbb3ec25SBjoern A. Zeeb 	mt7996_irq_enable(dev, MT_INT_RX(q));
525cbb3ec25SBjoern A. Zeeb }
526cbb3ec25SBjoern A. Zeeb 
527cbb3ec25SBjoern A. Zeeb /* TODO: support 2/4/6/8 MSI-X vectors */
mt7996_irq_tasklet(struct tasklet_struct * t)528cbb3ec25SBjoern A. Zeeb static void mt7996_irq_tasklet(struct tasklet_struct *t)
529cbb3ec25SBjoern A. Zeeb {
530cbb3ec25SBjoern A. Zeeb 	struct mt7996_dev *dev = from_tasklet(dev, t, mt76.irq_tasklet);
531*8ba4d145SBjoern A. Zeeb 	struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
532*8ba4d145SBjoern A. Zeeb 	struct mtk_wed_device *wed_hif2 = &dev->mt76.mmio.wed_hif2;
533*8ba4d145SBjoern A. Zeeb 	u32 i, intr, mask, intr1 = 0;
534cbb3ec25SBjoern A. Zeeb 
535*8ba4d145SBjoern A. Zeeb 	if (dev->hif2 && mtk_wed_device_active(wed_hif2)) {
536*8ba4d145SBjoern A. Zeeb 		mtk_wed_device_irq_set_mask(wed_hif2, 0);
537*8ba4d145SBjoern A. Zeeb 		intr1 = mtk_wed_device_irq_get(wed_hif2,
538*8ba4d145SBjoern A. Zeeb 					       dev->mt76.mmio.irqmask);
539*8ba4d145SBjoern A. Zeeb 		if (intr1 & MT_INT_RX_TXFREE_EXT)
540*8ba4d145SBjoern A. Zeeb 			napi_schedule(&dev->mt76.napi[MT_RXQ_TXFREE_BAND2]);
541*8ba4d145SBjoern A. Zeeb 	}
542*8ba4d145SBjoern A. Zeeb 
543*8ba4d145SBjoern A. Zeeb 	if (mtk_wed_device_active(wed)) {
544*8ba4d145SBjoern A. Zeeb 		mtk_wed_device_irq_set_mask(wed, 0);
545*8ba4d145SBjoern A. Zeeb 		intr = mtk_wed_device_irq_get(wed, dev->mt76.mmio.irqmask);
546*8ba4d145SBjoern A. Zeeb 		intr |= (intr1 & ~MT_INT_RX_TXFREE_EXT);
547*8ba4d145SBjoern A. Zeeb 	} else {
548cbb3ec25SBjoern A. Zeeb 		mt76_wr(dev, MT_INT_MASK_CSR, 0);
549cbb3ec25SBjoern A. Zeeb 		if (dev->hif2)
550cbb3ec25SBjoern A. Zeeb 			mt76_wr(dev, MT_INT1_MASK_CSR, 0);
551cbb3ec25SBjoern A. Zeeb 
552cbb3ec25SBjoern A. Zeeb 		intr = mt76_rr(dev, MT_INT_SOURCE_CSR);
553cbb3ec25SBjoern A. Zeeb 		intr &= dev->mt76.mmio.irqmask;
554cbb3ec25SBjoern A. Zeeb 		mt76_wr(dev, MT_INT_SOURCE_CSR, intr);
555cbb3ec25SBjoern A. Zeeb 		if (dev->hif2) {
556cbb3ec25SBjoern A. Zeeb 			intr1 = mt76_rr(dev, MT_INT1_SOURCE_CSR);
557cbb3ec25SBjoern A. Zeeb 			intr1 &= dev->mt76.mmio.irqmask;
558cbb3ec25SBjoern A. Zeeb 			mt76_wr(dev, MT_INT1_SOURCE_CSR, intr1);
559cbb3ec25SBjoern A. Zeeb 			intr |= intr1;
560cbb3ec25SBjoern A. Zeeb 		}
561*8ba4d145SBjoern A. Zeeb 	}
562cbb3ec25SBjoern A. Zeeb 
563cbb3ec25SBjoern A. Zeeb 	trace_dev_irq(&dev->mt76, intr, dev->mt76.mmio.irqmask);
564cbb3ec25SBjoern A. Zeeb 
565cbb3ec25SBjoern A. Zeeb 	mask = intr & MT_INT_RX_DONE_ALL;
566cbb3ec25SBjoern A. Zeeb 	if (intr & MT_INT_TX_DONE_MCU)
567cbb3ec25SBjoern A. Zeeb 		mask |= MT_INT_TX_DONE_MCU;
568cbb3ec25SBjoern A. Zeeb 	mt7996_irq_disable(dev, mask);
569cbb3ec25SBjoern A. Zeeb 
570cbb3ec25SBjoern A. Zeeb 	if (intr & MT_INT_TX_DONE_MCU)
571cbb3ec25SBjoern A. Zeeb 		napi_schedule(&dev->mt76.tx_napi);
572cbb3ec25SBjoern A. Zeeb 
573cbb3ec25SBjoern A. Zeeb 	for (i = 0; i < __MT_RXQ_MAX; i++) {
574cbb3ec25SBjoern A. Zeeb 		if ((intr & MT_INT_RX(i)))
575cbb3ec25SBjoern A. Zeeb 			napi_schedule(&dev->mt76.napi[i]);
576cbb3ec25SBjoern A. Zeeb 	}
577cbb3ec25SBjoern A. Zeeb 
578cbb3ec25SBjoern A. Zeeb 	if (intr & MT_INT_MCU_CMD) {
579cbb3ec25SBjoern A. Zeeb 		u32 val = mt76_rr(dev, MT_MCU_CMD);
580cbb3ec25SBjoern A. Zeeb 
581cbb3ec25SBjoern A. Zeeb 		mt76_wr(dev, MT_MCU_CMD, val);
582cbb3ec25SBjoern A. Zeeb 		if (val & (MT_MCU_CMD_ERROR_MASK | MT_MCU_CMD_WDT_MASK)) {
583cbb3ec25SBjoern A. Zeeb 			dev->recovery.state = val;
584cbb3ec25SBjoern A. Zeeb 			mt7996_reset(dev);
585cbb3ec25SBjoern A. Zeeb 		}
586cbb3ec25SBjoern A. Zeeb 	}
587cbb3ec25SBjoern A. Zeeb }
588cbb3ec25SBjoern A. Zeeb 
mt7996_irq_handler(int irq,void * dev_instance)589cbb3ec25SBjoern A. Zeeb irqreturn_t mt7996_irq_handler(int irq, void *dev_instance)
590cbb3ec25SBjoern A. Zeeb {
591cbb3ec25SBjoern A. Zeeb 	struct mt7996_dev *dev = dev_instance;
592cbb3ec25SBjoern A. Zeeb 
593*8ba4d145SBjoern A. Zeeb 	if (mtk_wed_device_active(&dev->mt76.mmio.wed))
594*8ba4d145SBjoern A. Zeeb 		mtk_wed_device_irq_set_mask(&dev->mt76.mmio.wed, 0);
595*8ba4d145SBjoern A. Zeeb 	else
596cbb3ec25SBjoern A. Zeeb 		mt76_wr(dev, MT_INT_MASK_CSR, 0);
597*8ba4d145SBjoern A. Zeeb 
598*8ba4d145SBjoern A. Zeeb 	if (dev->hif2) {
599*8ba4d145SBjoern A. Zeeb 		if (mtk_wed_device_active(&dev->mt76.mmio.wed_hif2))
600*8ba4d145SBjoern A. Zeeb 			mtk_wed_device_irq_set_mask(&dev->mt76.mmio.wed_hif2, 0);
601*8ba4d145SBjoern A. Zeeb 		else
602cbb3ec25SBjoern A. Zeeb 			mt76_wr(dev, MT_INT1_MASK_CSR, 0);
603*8ba4d145SBjoern A. Zeeb 	}
604cbb3ec25SBjoern A. Zeeb 
605cbb3ec25SBjoern A. Zeeb 	if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
606cbb3ec25SBjoern A. Zeeb 		return IRQ_NONE;
607cbb3ec25SBjoern A. Zeeb 
608cbb3ec25SBjoern A. Zeeb 	tasklet_schedule(&dev->mt76.irq_tasklet);
609cbb3ec25SBjoern A. Zeeb 
610cbb3ec25SBjoern A. Zeeb 	return IRQ_HANDLED;
611cbb3ec25SBjoern A. Zeeb }
612cbb3ec25SBjoern A. Zeeb 
mt7996_mmio_probe(struct device * pdev,void __iomem * mem_base,u32 device_id)613cbb3ec25SBjoern A. Zeeb struct mt7996_dev *mt7996_mmio_probe(struct device *pdev,
614cbb3ec25SBjoern A. Zeeb 				     void __iomem *mem_base, u32 device_id)
615cbb3ec25SBjoern A. Zeeb {
616cbb3ec25SBjoern A. Zeeb 	static const struct mt76_driver_ops drv_ops = {
617cbb3ec25SBjoern A. Zeeb 		/* txwi_size = txd size + txp size */
618cbb3ec25SBjoern A. Zeeb 		.txwi_size = MT_TXD_SIZE + sizeof(struct mt76_connac_fw_txp),
619*8ba4d145SBjoern A. Zeeb 		.link_data_size = sizeof(struct mt7996_vif_link),
620cbb3ec25SBjoern A. Zeeb 		.drv_flags = MT_DRV_TXWI_NO_FREE |
621cbb3ec25SBjoern A. Zeeb 			     MT_DRV_AMSDU_OFFLOAD |
622cbb3ec25SBjoern A. Zeeb 			     MT_DRV_HW_MGMT_TXQ,
623cbb3ec25SBjoern A. Zeeb 		.survey_flags = SURVEY_INFO_TIME_TX |
624cbb3ec25SBjoern A. Zeeb 				SURVEY_INFO_TIME_RX |
625cbb3ec25SBjoern A. Zeeb 				SURVEY_INFO_TIME_BSS_RX,
626cbb3ec25SBjoern A. Zeeb 		.token_size = MT7996_TOKEN_SIZE,
627cbb3ec25SBjoern A. Zeeb 		.tx_prepare_skb = mt7996_tx_prepare_skb,
628cbb3ec25SBjoern A. Zeeb 		.tx_complete_skb = mt76_connac_tx_complete_skb,
629cbb3ec25SBjoern A. Zeeb 		.rx_skb = mt7996_queue_rx_skb,
630cbb3ec25SBjoern A. Zeeb 		.rx_check = mt7996_rx_check,
631cbb3ec25SBjoern A. Zeeb 		.rx_poll_complete = mt7996_rx_poll_complete,
632cbb3ec25SBjoern A. Zeeb 		.sta_add = mt7996_mac_sta_add,
633*8ba4d145SBjoern A. Zeeb 		.sta_event = mt7996_mac_sta_event,
634cbb3ec25SBjoern A. Zeeb 		.sta_remove = mt7996_mac_sta_remove,
635cbb3ec25SBjoern A. Zeeb 		.update_survey = mt7996_update_channel,
636*8ba4d145SBjoern A. Zeeb 		.set_channel = mt7996_set_channel,
637*8ba4d145SBjoern A. Zeeb 		.vif_link_add = mt7996_vif_link_add,
638*8ba4d145SBjoern A. Zeeb 		.vif_link_remove = mt7996_vif_link_remove,
639cbb3ec25SBjoern A. Zeeb 	};
640cbb3ec25SBjoern A. Zeeb 	struct mt7996_dev *dev;
641cbb3ec25SBjoern A. Zeeb 	struct mt76_dev *mdev;
642cbb3ec25SBjoern A. Zeeb 	int ret;
643cbb3ec25SBjoern A. Zeeb 
644cbb3ec25SBjoern A. Zeeb 	mdev = mt76_alloc_device(pdev, sizeof(*dev), &mt7996_ops, &drv_ops);
645cbb3ec25SBjoern A. Zeeb 	if (!mdev)
646cbb3ec25SBjoern A. Zeeb 		return ERR_PTR(-ENOMEM);
647cbb3ec25SBjoern A. Zeeb 
648cbb3ec25SBjoern A. Zeeb 	dev = container_of(mdev, struct mt7996_dev, mt76);
649cbb3ec25SBjoern A. Zeeb 
650cbb3ec25SBjoern A. Zeeb 	ret = mt7996_mmio_init(mdev, mem_base, device_id);
651cbb3ec25SBjoern A. Zeeb 	if (ret)
652cbb3ec25SBjoern A. Zeeb 		goto error;
653cbb3ec25SBjoern A. Zeeb 
654cbb3ec25SBjoern A. Zeeb 	tasklet_setup(&mdev->irq_tasklet, mt7996_irq_tasklet);
655cbb3ec25SBjoern A. Zeeb 
656cbb3ec25SBjoern A. Zeeb 	mt76_wr(dev, MT_INT_MASK_CSR, 0);
657cbb3ec25SBjoern A. Zeeb 
658cbb3ec25SBjoern A. Zeeb 	return dev;
659cbb3ec25SBjoern A. Zeeb 
660cbb3ec25SBjoern A. Zeeb error:
661cbb3ec25SBjoern A. Zeeb 	mt76_free_device(&dev->mt76);
662cbb3ec25SBjoern A. Zeeb 
663cbb3ec25SBjoern A. Zeeb 	return ERR_PTR(ret);
664cbb3ec25SBjoern A. Zeeb }
665cbb3ec25SBjoern A. Zeeb 
mt7996_init(void)666cbb3ec25SBjoern A. Zeeb static int __init mt7996_init(void)
667cbb3ec25SBjoern A. Zeeb {
668cbb3ec25SBjoern A. Zeeb 	int ret;
669cbb3ec25SBjoern A. Zeeb 
670cbb3ec25SBjoern A. Zeeb 	ret = pci_register_driver(&mt7996_hif_driver);
671cbb3ec25SBjoern A. Zeeb 	if (ret)
672cbb3ec25SBjoern A. Zeeb 		return ret;
673cbb3ec25SBjoern A. Zeeb 
674cbb3ec25SBjoern A. Zeeb 	ret = pci_register_driver(&mt7996_pci_driver);
675cbb3ec25SBjoern A. Zeeb 	if (ret)
676cbb3ec25SBjoern A. Zeeb 		pci_unregister_driver(&mt7996_hif_driver);
677cbb3ec25SBjoern A. Zeeb 
678cbb3ec25SBjoern A. Zeeb 	return ret;
679cbb3ec25SBjoern A. Zeeb }
680cbb3ec25SBjoern A. Zeeb 
mt7996_exit(void)681cbb3ec25SBjoern A. Zeeb static void __exit mt7996_exit(void)
682cbb3ec25SBjoern A. Zeeb {
683cbb3ec25SBjoern A. Zeeb 	pci_unregister_driver(&mt7996_pci_driver);
684cbb3ec25SBjoern A. Zeeb 	pci_unregister_driver(&mt7996_hif_driver);
685cbb3ec25SBjoern A. Zeeb }
686cbb3ec25SBjoern A. Zeeb 
687cbb3ec25SBjoern A. Zeeb module_init(mt7996_init);
688cbb3ec25SBjoern A. Zeeb module_exit(mt7996_exit);
689*8ba4d145SBjoern A. Zeeb MODULE_DESCRIPTION("MediaTek MT7996 MMIO helpers");
690cbb3ec25SBjoern A. Zeeb MODULE_LICENSE("Dual BSD/GPL");
691