xref: /linux/drivers/net/wireless/mediatek/mt76/mt7915/mmio.c (revision 164666fa66669d437bdcc8d5f1744a2aee73be41)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include "mt7915.h"
5 
6 static u32 mt7915_reg_map_l1(struct mt7915_dev *dev, u32 addr)
7 {
8 	u32 offset = FIELD_GET(MT_HIF_REMAP_L1_OFFSET, addr);
9 	u32 base = FIELD_GET(MT_HIF_REMAP_L1_BASE, addr);
10 
11 	mt76_rmw_field(dev, MT_HIF_REMAP_L1, MT_HIF_REMAP_L1_MASK, base);
12 	/* use read to push write */
13 	mt76_rr(dev, MT_HIF_REMAP_L1);
14 
15 	return MT_HIF_REMAP_BASE_L1 + offset;
16 }
17 
18 static u32 mt7915_reg_map_l2(struct mt7915_dev *dev, u32 addr)
19 {
20 	u32 offset = FIELD_GET(MT_HIF_REMAP_L2_OFFSET, addr);
21 	u32 base = FIELD_GET(MT_HIF_REMAP_L2_BASE, addr);
22 
23 	mt76_rmw_field(dev, MT_HIF_REMAP_L2, MT_HIF_REMAP_L2_MASK, base);
24 	/* use read to push write */
25 	mt76_rr(dev, MT_HIF_REMAP_L2);
26 
27 	return MT_HIF_REMAP_BASE_L2 + offset;
28 }
29 
30 static u32 __mt7915_reg_addr(struct mt7915_dev *dev, u32 addr)
31 {
32 	static const struct {
33 		u32 phys;
34 		u32 mapped;
35 		u32 size;
36 	} fixed_map[] = {
37 		{ 0x00400000, 0x80000, 0x10000 }, /* WF_MCU_SYSRAM */
38 		{ 0x00410000, 0x90000, 0x10000 }, /* WF_MCU_SYSRAM (configure regs) */
39 		{ 0x40000000, 0x70000, 0x10000 }, /* WF_UMAC_SYSRAM */
40 		{ 0x54000000, 0x02000, 0x1000 }, /* WFDMA PCIE0 MCU DMA0 */
41 		{ 0x55000000, 0x03000, 0x1000 }, /* WFDMA PCIE0 MCU DMA1 */
42 		{ 0x58000000, 0x06000, 0x1000 }, /* WFDMA PCIE1 MCU DMA0 (MEM_DMA) */
43 		{ 0x59000000, 0x07000, 0x1000 }, /* WFDMA PCIE1 MCU DMA1 */
44 		{ 0x7c000000, 0xf0000, 0x10000 }, /* CONN_INFRA */
45 		{ 0x7c020000, 0xd0000, 0x10000 }, /* CONN_INFRA, WFDMA */
46 		{ 0x80020000, 0xb0000, 0x10000 }, /* WF_TOP_MISC_OFF */
47 		{ 0x81020000, 0xc0000, 0x10000 }, /* WF_TOP_MISC_ON */
48 		{ 0x820c0000, 0x08000, 0x4000 }, /* WF_UMAC_TOP (PLE) */
49 		{ 0x820c8000, 0x0c000, 0x2000 }, /* WF_UMAC_TOP (PSE) */
50 		{ 0x820cc000, 0x0e000, 0x2000 }, /* WF_UMAC_TOP (PP) */
51 		{ 0x820ce000, 0x21c00, 0x0200 }, /* WF_LMAC_TOP (WF_SEC) */
52 		{ 0x820cf000, 0x22000, 0x1000 }, /* WF_LMAC_TOP (WF_PF) */
53 		{ 0x820d0000, 0x30000, 0x10000 }, /* WF_LMAC_TOP (WF_WTBLON) */
54 		{ 0x820e0000, 0x20000, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_CFG) */
55 		{ 0x820e1000, 0x20400, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_TRB) */
56 		{ 0x820e2000, 0x20800, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_AGG) */
57 		{ 0x820e3000, 0x20c00, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_ARB) */
58 		{ 0x820e4000, 0x21000, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_TMAC) */
59 		{ 0x820e5000, 0x21400, 0x0800 }, /* WF_LMAC_TOP BN0 (WF_RMAC) */
60 		{ 0x820e7000, 0x21e00, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_DMA) */
61 		{ 0x820e9000, 0x23400, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_WTBLOFF) */
62 		{ 0x820ea000, 0x24000, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_ETBF) */
63 		{ 0x820eb000, 0x24200, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_LPON) */
64 		{ 0x820ec000, 0x24600, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_INT) */
65 		{ 0x820ed000, 0x24800, 0x0800 }, /* WF_LMAC_TOP BN0 (WF_MIB) */
66 		{ 0x820f0000, 0xa0000, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_CFG) */
67 		{ 0x820f1000, 0xa0600, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_TRB) */
68 		{ 0x820f2000, 0xa0800, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_AGG) */
69 		{ 0x820f3000, 0xa0c00, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_ARB) */
70 		{ 0x820f4000, 0xa1000, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_TMAC) */
71 		{ 0x820f5000, 0xa1400, 0x0800 }, /* WF_LMAC_TOP BN1 (WF_RMAC) */
72 		{ 0x820f7000, 0xa1e00, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_DMA) */
73 		{ 0x820f9000, 0xa3400, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_WTBLOFF) */
74 		{ 0x820fa000, 0xa4000, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_ETBF) */
75 		{ 0x820fb000, 0xa4200, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_LPON) */
76 		{ 0x820fc000, 0xa4600, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_INT) */
77 		{ 0x820fd000, 0xa4800, 0x0800 }, /* WF_LMAC_TOP BN1 (WF_MIB) */
78 	};
79 	int i;
80 
81 	if (addr < 0x100000)
82 		return addr;
83 
84 	for (i = 0; i < ARRAY_SIZE(fixed_map); i++) {
85 		u32 ofs;
86 
87 		if (addr < fixed_map[i].phys)
88 			continue;
89 
90 		ofs = addr - fixed_map[i].phys;
91 		if (ofs > fixed_map[i].size)
92 			continue;
93 
94 		return fixed_map[i].mapped + ofs;
95 	}
96 
97 	if ((addr >= 0x18000000 && addr < 0x18c00000) ||
98 	    (addr >= 0x70000000 && addr < 0x78000000))
99 		return mt7915_reg_map_l1(dev, addr);
100 
101 	return mt7915_reg_map_l2(dev, addr);
102 }
103 
104 static u32 mt7915_rr(struct mt76_dev *mdev, u32 offset)
105 {
106 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
107 	u32 addr = __mt7915_reg_addr(dev, offset);
108 
109 	return dev->bus_ops->rr(mdev, addr);
110 }
111 
112 static void mt7915_wr(struct mt76_dev *mdev, u32 offset, u32 val)
113 {
114 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
115 	u32 addr = __mt7915_reg_addr(dev, offset);
116 
117 	dev->bus_ops->wr(mdev, addr, val);
118 }
119 
120 static u32 mt7915_rmw(struct mt76_dev *mdev, u32 offset, u32 mask, u32 val)
121 {
122 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
123 	u32 addr = __mt7915_reg_addr(dev, offset);
124 
125 	return dev->bus_ops->rmw(mdev, addr, mask, val);
126 }
127 
128 int mt7915_mmio_init(struct mt76_dev *mdev, void __iomem *mem_base, int irq)
129 {
130 	struct mt76_bus_ops *bus_ops;
131 	struct mt7915_dev *dev;
132 
133 	dev = container_of(mdev, struct mt7915_dev, mt76);
134 	mt76_mmio_init(&dev->mt76, mem_base);
135 
136 	dev->bus_ops = dev->mt76.bus;
137 	bus_ops = devm_kmemdup(dev->mt76.dev, dev->bus_ops, sizeof(*bus_ops),
138 			       GFP_KERNEL);
139 	if (!bus_ops)
140 		return -ENOMEM;
141 
142 	bus_ops->rr = mt7915_rr;
143 	bus_ops->wr = mt7915_wr;
144 	bus_ops->rmw = mt7915_rmw;
145 	dev->mt76.bus = bus_ops;
146 
147 	mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
148 		    (mt76_rr(dev, MT_HW_REV) & 0xff);
149 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
150 
151 	mt76_wr(dev, MT_INT_MASK_CSR, 0);
152 
153 	return 0;
154 }
155