1 /* SPDX-License-Identifier: ISC */
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #ifndef __MT7921_H
5 #define __MT7921_H
6 
7 #include "../mt792x.h"
8 #include "regs.h"
9 
10 #define MT7921_TX_RING_SIZE		2048
11 #define MT7921_TX_MCU_RING_SIZE		256
12 #define MT7921_TX_FWDL_RING_SIZE	128
13 
14 #define MT7921_RX_RING_SIZE		1536
15 #define MT7921_RX_MCU_RING_SIZE		8
16 #define MT7921_RX_MCU_WA_RING_SIZE	512
17 
18 #define MT7921_EEPROM_SIZE		3584
19 #define MT7921_TOKEN_SIZE		8192
20 
21 #define MT7921_EEPROM_BLOCK_SIZE	16
22 
23 #define MT7921_SKU_RATE_NUM		161
24 #define MT7921_SKU_MAX_DELTA_IDX	MT7921_SKU_RATE_NUM
25 #define MT7921_SKU_TABLE_SIZE		(MT7921_SKU_RATE_NUM + 1)
26 
27 #define MCU_UNI_EVENT_ROC  0x27
28 #define MCU_UNI_EVENT_CLC  0x80
29 
30 #define EXT_CMD_RADIO_LED_CTRL_ENABLE   0x1
31 #define EXT_CMD_RADIO_ON_LED            0x2
32 #define EXT_CMD_RADIO_OFF_LED           0x3
33 
34 enum {
35 	UNI_ROC_ACQUIRE,
36 	UNI_ROC_ABORT,
37 	UNI_ROC_NUM
38 };
39 
40 enum mt7921_roc_req {
41 	MT7921_ROC_REQ_JOIN,
42 	MT7921_ROC_REQ_ROC,
43 	MT7921_ROC_REQ_NUM
44 };
45 
46 enum {
47 	UNI_EVENT_ROC_GRANT = 0,
48 	UNI_EVENT_ROC_TAG_NUM
49 };
50 
51 struct mt7921_realease_info {
52 	__le16 len;
53 	u8 pad_len;
54 	u8 tag;
55 } __packed;
56 
57 struct mt7921_fw_features {
58 	u8 segment;
59 	u8 data;
60 	u8 rsv[14];
61 } __packed;
62 
63 struct mt7921_roc_grant_tlv {
64 	__le16 tag;
65 	__le16 len;
66 	u8 bss_idx;
67 	u8 tokenid;
68 	u8 status;
69 	u8 primarychannel;
70 	u8 rfsco;
71 	u8 rfband;
72 	u8 channelwidth;
73 	u8 centerfreqseg1;
74 	u8 centerfreqseg2;
75 	u8 reqtype;
76 	u8 dbdcband;
77 	u8 rsv[1];
78 	__le32 max_interval;
79 } __packed;
80 
81 enum mt7921_sdio_pkt_type {
82 	MT7921_SDIO_TXD,
83 	MT7921_SDIO_DATA,
84 	MT7921_SDIO_CMD,
85 	MT7921_SDIO_FWDL,
86 };
87 
88 struct mt7921_sdio_intr {
89 	u32 isr;
90 	struct {
91 		u32 wtqcr[16];
92 	} tx;
93 	struct {
94 		u16 num[2];
95 		u16 len0[16];
96 		u16 len1[128];
97 	} rx;
98 	u32 rec_mb[2];
99 } __packed;
100 
101 #define to_rssi(field, rxv)		((FIELD_GET(field, rxv) - 220) / 2)
102 #define to_rcpi(rssi)			(2 * (rssi) + 220)
103 
104 enum mt7921_txq_id {
105 	MT7921_TXQ_BAND0,
106 	MT7921_TXQ_BAND1,
107 	MT7921_TXQ_FWDL = 16,
108 	MT7921_TXQ_MCU_WM,
109 };
110 
111 enum mt7921_rxq_id {
112 	MT7921_RXQ_BAND0 = 0,
113 	MT7921_RXQ_BAND1,
114 	MT7921_RXQ_MCU_WM = 0,
115 };
116 
117 enum {
118 	MT7921_CLC_POWER,
119 	MT7921_CLC_CHAN,
120 	MT7921_CLC_MAX_NUM,
121 };
122 
123 struct mt7921_clc_rule {
124 	u8 alpha2[2];
125 	u8 type[2];
126 	__le16 len;
127 	u8 data[];
128 } __packed;
129 
130 struct mt7921_clc {
131 	__le32 len;
132 	u8 idx;
133 	u8 ver;
134 	u8 nr_country;
135 	u8 type;
136 	u8 rsv[8];
137 	u8 data[];
138 } __packed;
139 
140 enum mt7921_eeprom_field {
141 	MT_EE_CHIP_ID =		0x000,
142 	MT_EE_VERSION =		0x002,
143 	MT_EE_MAC_ADDR =	0x004,
144 	MT_EE_WIFI_CONF =	0x07c,
145 	MT_EE_HW_TYPE =		0x55b,
146 	__MT_EE_MAX =		0x9ff
147 };
148 
149 #define MT_EE_HW_TYPE_ENCAP			BIT(0)
150 
151 enum {
152 	TXPWR_USER,
153 	TXPWR_EEPROM,
154 	TXPWR_MAC,
155 	TXPWR_MAX_NUM,
156 };
157 
158 struct mt7921_txpwr {
159 	u8 ch;
160 	u8 rsv[3];
161 	struct {
162 		u8 ch;
163 		u8 cck[4];
164 		u8 ofdm[8];
165 		u8 ht20[8];
166 		u8 ht40[9];
167 		u8 vht20[12];
168 		u8 vht40[12];
169 		u8 vht80[12];
170 		u8 vht160[12];
171 		u8 he26[12];
172 		u8 he52[12];
173 		u8 he106[12];
174 		u8 he242[12];
175 		u8 he484[12];
176 		u8 he996[12];
177 		u8 he996x2[12];
178 	} data[TXPWR_MAX_NUM];
179 };
180 
181 extern const struct ieee80211_ops mt7921_ops;
182 
183 u32 mt7921_reg_map(struct mt792x_dev *dev, u32 addr);
184 
185 int __mt7921_start(struct mt792x_phy *phy);
186 int mt7921_register_device(struct mt792x_dev *dev);
187 void mt7921_unregister_device(struct mt792x_dev *dev);
188 int mt7921_run_firmware(struct mt792x_dev *dev);
189 int mt7921_set_channel(struct mt76_phy *mphy);
190 int mt7921_mcu_set_bss_pm(struct mt792x_dev *dev, struct ieee80211_vif *vif,
191 			  bool enable);
192 int mt7921_mcu_sta_update(struct mt792x_dev *dev, struct ieee80211_sta *sta,
193 			  struct ieee80211_vif *vif, bool enable,
194 			  enum mt76_sta_info_state state);
195 int mt7921_mcu_set_chan_info(struct mt792x_phy *phy, int cmd);
196 int mt7921_mcu_set_tx(struct mt792x_dev *dev, struct ieee80211_vif *vif);
197 int mt7921_mcu_set_eeprom(struct mt792x_dev *dev);
198 int mt7921_mcu_get_rx_rate(struct mt792x_phy *phy, struct ieee80211_vif *vif,
199 			   struct ieee80211_sta *sta, struct rate_info *rate);
200 int mt7921_mcu_fw_log_2_host(struct mt792x_dev *dev, u8 ctrl);
201 void mt7921_mcu_rx_event(struct mt792x_dev *dev, struct sk_buff *skb);
202 int mt7921_mcu_set_rxfilter(struct mt792x_dev *dev, u32 fif,
203 			    u8 bit_op, u32 bit_map);
204 int mt7921_mcu_radio_led_ctrl(struct mt792x_dev *dev, u8 value);
205 
206 static inline u32
mt7921_reg_map_l1(struct mt792x_dev * dev,u32 addr)207 mt7921_reg_map_l1(struct mt792x_dev *dev, u32 addr)
208 {
209 	u32 offset = FIELD_GET(MT_HIF_REMAP_L1_OFFSET, addr);
210 	u32 base = FIELD_GET(MT_HIF_REMAP_L1_BASE, addr);
211 
212 	mt76_rmw_field(dev, MT_HIF_REMAP_L1, MT_HIF_REMAP_L1_MASK, base);
213 	/* use read to push write */
214 	mt76_rr(dev, MT_HIF_REMAP_L1);
215 
216 	return MT_HIF_REMAP_BASE_L1 + offset;
217 }
218 
219 static inline u32
mt7921_l1_rr(struct mt792x_dev * dev,u32 addr)220 mt7921_l1_rr(struct mt792x_dev *dev, u32 addr)
221 {
222 	return mt76_rr(dev, mt7921_reg_map_l1(dev, addr));
223 }
224 
225 static inline void
mt7921_l1_wr(struct mt792x_dev * dev,u32 addr,u32 val)226 mt7921_l1_wr(struct mt792x_dev *dev, u32 addr, u32 val)
227 {
228 	mt76_wr(dev, mt7921_reg_map_l1(dev, addr), val);
229 }
230 
231 static inline u32
mt7921_l1_rmw(struct mt792x_dev * dev,u32 addr,u32 mask,u32 val)232 mt7921_l1_rmw(struct mt792x_dev *dev, u32 addr, u32 mask, u32 val)
233 {
234 	val |= mt7921_l1_rr(dev, addr) & ~mask;
235 	mt7921_l1_wr(dev, addr, val);
236 
237 	return val;
238 }
239 
240 #define mt7921_l1_set(dev, addr, val)	mt7921_l1_rmw(dev, addr, 0, val)
241 #define mt7921_l1_clear(dev, addr, val)	mt7921_l1_rmw(dev, addr, val, 0)
242 
243 void mt7921_regd_update(struct mt792x_dev *dev);
244 int mt7921_mac_init(struct mt792x_dev *dev);
245 bool mt7921_mac_wtbl_update(struct mt792x_dev *dev, int idx, u32 mask);
246 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
247 		       struct ieee80211_sta *sta);
248 int mt7921_mac_sta_event(struct mt76_dev *mdev, struct ieee80211_vif *vif,
249 			 struct ieee80211_sta *sta, enum mt76_sta_event ev);
250 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
251 			   struct ieee80211_sta *sta);
252 void mt7921_mac_reset_work(struct work_struct *work);
253 int mt7921e_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
254 			   enum mt76_txq_id qid, struct mt76_wcid *wcid,
255 			   struct ieee80211_sta *sta,
256 			   struct mt76_tx_info *tx_info);
257 
258 bool mt7921_rx_check(struct mt76_dev *mdev, void *data, int len);
259 void mt7921_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
260 			 struct sk_buff *skb, u32 *info);
261 void mt7921_stats_work(struct work_struct *work);
262 void mt7921_set_stream_he_caps(struct mt792x_phy *phy);
263 int mt7921_init_debugfs(struct mt792x_dev *dev);
264 
265 int mt7921_mcu_set_beacon_filter(struct mt792x_dev *dev,
266 				 struct ieee80211_vif *vif,
267 				 bool enable);
268 int mt7921_mcu_uni_tx_ba(struct mt792x_dev *dev,
269 			 struct ieee80211_ampdu_params *params,
270 			 bool enable);
271 int mt7921_mcu_uni_rx_ba(struct mt792x_dev *dev,
272 			 struct ieee80211_ampdu_params *params,
273 			 bool enable);
274 void mt7921_scan_work(struct work_struct *work);
275 void mt7921_roc_work(struct work_struct *work);
276 int mt7921_mcu_uni_bss_ps(struct mt792x_dev *dev, struct ieee80211_vif *vif);
277 void mt7921_coredump_work(struct work_struct *work);
278 int mt7921_get_txpwr_info(struct mt792x_dev *dev, struct mt7921_txpwr *txpwr);
279 int mt7921_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
280 			void *data, int len);
281 int mt7921_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *msg,
282 			 struct netlink_callback *cb, void *data, int len);
283 int mt7921_mcu_parse_response(struct mt76_dev *mdev, int cmd,
284 			      struct sk_buff *skb, int seq);
285 
286 int mt7921e_driver_own(struct mt792x_dev *dev);
287 int mt7921e_mac_reset(struct mt792x_dev *dev);
288 int mt7921e_mcu_init(struct mt792x_dev *dev);
289 int mt7921s_wfsys_reset(struct mt792x_dev *dev);
290 int mt7921s_mac_reset(struct mt792x_dev *dev);
291 int mt7921s_init_reset(struct mt792x_dev *dev);
292 
293 int mt7921s_mcu_init(struct mt792x_dev *dev);
294 int mt7921s_mcu_drv_pmctrl(struct mt792x_dev *dev);
295 int mt7921s_mcu_fw_pmctrl(struct mt792x_dev *dev);
296 void mt7921_mac_add_txs(struct mt792x_dev *dev, void *data);
297 void mt7921_set_runtime_pm(struct mt792x_dev *dev);
298 void mt7921_mcu_set_suspend_iter(void *priv, u8 *mac,
299 				 struct ieee80211_vif *vif);
300 void mt7921_set_ipv6_ns_work(struct work_struct *work);
301 
302 int mt7921_mcu_set_sniffer(struct mt792x_dev *dev, struct ieee80211_vif *vif,
303 			   bool enable);
304 int mt7921_mcu_config_sniffer(struct mt792x_vif *vif,
305 			      struct ieee80211_chanctx_conf *ctx);
306 int mt7921_mcu_get_temperature(struct mt792x_phy *phy);
307 
308 int mt7921_usb_sdio_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
309 				   enum mt76_txq_id qid, struct mt76_wcid *wcid,
310 				   struct ieee80211_sta *sta,
311 				   struct mt76_tx_info *tx_info);
312 void mt7921_usb_sdio_tx_complete_skb(struct mt76_dev *mdev,
313 				     struct mt76_queue_entry *e);
314 bool mt7921_usb_sdio_tx_status_data(struct mt76_dev *mdev, u8 *update);
315 
316 /* usb */
317 int mt7921_mcu_uni_add_beacon_offload(struct mt792x_dev *dev,
318 				      struct ieee80211_hw *hw,
319 				      struct ieee80211_vif *vif,
320 				      bool enable);
321 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
322 			  const struct cfg80211_sar_specs *sar);
323 
324 int mt7921_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2,
325 		       enum environment_cap env_cap);
326 int mt7921_mcu_set_roc(struct mt792x_phy *phy, struct mt792x_vif *vif,
327 		       struct ieee80211_channel *chan, int duration,
328 		       enum mt7921_roc_req type, u8 token_id);
329 int mt7921_mcu_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif,
330 			 u8 token_id);
331 void mt7921_roc_abort_sync(struct mt792x_dev *dev);
332 int mt7921_mcu_set_rssimonitor(struct mt792x_dev *dev, struct ieee80211_vif *vif);
333 #endif
334