blob: 22d867a5e874bdf9574c6d8fcfbe0ba0dfa70cff [file] [log] [blame]
developer3abe1ad2022-01-24 11:13:32 +08001/* Copyright (C) 2021-2022 Mediatek Inc. */
2#ifndef __ATENL_H
3#define __ATENL_H
4
5#include <arpa/inet.h>
6#include <errno.h>
7#include <fcntl.h>
8#include <limits.h>
9#include <linux/nl80211.h>
10#include <net/if.h>
11#include <stdbool.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15
16#include "nl.h"
17#include "util.h"
developer5698c9c2022-05-30 16:40:23 +080018#include "debug.h"
developer3abe1ad2022-01-24 11:13:32 +080019
developer3abe1ad2022-01-24 11:13:32 +080020#define BRIDGE_NAME "br-lan"
21#define ETH_P_RACFG 0x2880
22#define RACFG_PKT_MAX_SIZE 1600
23#define RACFG_HLEN 12
24#define RACFG_MAGIC_NO 0x18142880
25
26#define RACFG_CMD_TYPE_MASK GENMASK(14, 0)
27#define RACFG_CMD_TYPE_ETHREQ BIT(3)
28#define RACFG_CMD_TYPE_PLATFORM_MODULE GENMASK(4, 3)
29
developer3abe1ad2022-01-24 11:13:32 +080030#define set_band_val(_an, _band, _field, _val) \
31 _an->anb[_band]._field = (_val)
32#define get_band_val(_an, _band, _field) \
33 (_an->anb[_band]._field)
34
35enum atenl_rf_mode {
36 ATENL_RF_MODE_NORMAL,
37 ATENL_RF_MODE_TEST,
38 ATENL_RF_MODE_ICAP,
39 ATENL_RF_MODE_ICAP_OVERLAP,
40
41 __ATENL_RF_MODE_MAX,
42};
43
44struct atenl_rx_stat {
45 u64 total;
46 u64 ok_cnt;
47 u64 err_cnt;
48 u64 len_mismatch;
49};
50
51struct atenl_band {
52 bool valid;
53 u8 phy_idx;
54 u8 cap;
55 u8 chainmask;
56
57 enum mt76_testmode_state cur_state;
58 s8 tx_power;
59 enum atenl_rf_mode rf_mode;
60
61 bool use_tx_time;
developer5698c9c2022-05-30 16:40:23 +080062 u32 tx_time;
63 u32 tx_mpdu_len;
developer3abe1ad2022-01-24 11:13:32 +080064
65 bool reset_tx_cnt;
66 bool reset_rx_cnt;
67
68 /* history */
69 struct atenl_rx_stat rx_stat;
70};
71
developer5698c9c2022-05-30 16:40:23 +080072#define MAX_BAND_NUM 3
developer3abe1ad2022-01-24 11:13:32 +080073
74struct atenl {
75 struct atenl_band anb[MAX_BAND_NUM];
76 u16 chip_id;
developer5698c9c2022-05-30 16:40:23 +080077 u16 adie_id;
78 u8 sub_chip_id;
developer3abe1ad2022-01-24 11:13:32 +080079 u8 cur_band;
80
81 u8 mac_addr[ETH_ALEN];
82 bool unicast;
83 int sock_eth;
developer3abe1ad2022-01-24 11:13:32 +080084
85 const char *mtd_part;
86 u32 mtd_offset;
87 u8 *eeprom_data;
88 int eeprom_fd;
89 u16 eeprom_size;
developer3abe1ad2022-01-24 11:13:32 +080090
91 bool cmd_mode;
developer5698c9c2022-05-30 16:40:23 +080092
developer763ab652022-06-14 18:38:23 +080093 bool ibf_cal;
developer5698c9c2022-05-30 16:40:23 +080094 /* intermediate data */
95 u8 ibf_mcs;
96 u8 ibf_ant;
developer3abe1ad2022-01-24 11:13:32 +080097};
98
99struct atenl_cmd_hdr {
100 __be32 magic_no;
101 __be16 cmd_type;
102 __be16 cmd_id;
103 __be16 len;
104 __be16 seq;
105 u8 data[2048];
106} __attribute__((packed));
107
108enum atenl_cmd {
109 HQA_CMD_UNKNOWN,
110 HQA_CMD_LEGACY, /* legacy or deprecated */
111
112 HQA_CMD_OPEN_ADAPTER,
113 HQA_CMD_CLOSE_ADAPTER,
114 HQA_CMD_GET_CHIP_ID,
115 HQA_CMD_GET_SUB_CHIP_ID,
116 HQA_CMD_SET_TX_BW,
117 HQA_CMD_SET_TX_PKT_BW,
118 HQA_CMD_SET_TX_PRI_BW,
119 HQA_CMD_GET_TX_INFO,
120 HQA_CMD_SET_TX_PATH,
121 HQA_CMD_SET_TX_POWER,
122 HQA_CMD_SET_TX_POWER_MANUAL,
123 HQA_CMD_SET_RF_MODE,
124 HQA_CMD_SET_RX_PATH,
125 HQA_CMD_SET_RX_PKT_LEN,
126 HQA_CMD_SET_FREQ_OFFSET,
127 HQA_CMD_SET_TSSI,
128 HQA_CMD_SET_CFG,
129 HQA_CMD_SET_RU,
130 HQA_CMD_SET_BAND,
developer5698c9c2022-05-30 16:40:23 +0800131 HQA_CMD_SET_EEPROM_TO_FW,
developer3abe1ad2022-01-24 11:13:32 +0800132 HQA_CMD_READ_MAC_BBP_REG,
developer5698c9c2022-05-30 16:40:23 +0800133 HQA_CMD_READ_MAC_BBP_REG_QA,
developer3abe1ad2022-01-24 11:13:32 +0800134 HQA_CMD_READ_RF_REG,
135 HQA_CMD_READ_EEPROM_BULK,
136 HQA_CMD_READ_TEMPERATURE,
137 HQA_CMD_WRITE_MAC_BBP_REG,
138 HQA_CMD_WRITE_RF_REG,
139 HQA_CMD_WRITE_EEPROM_BULK,
140 HQA_CMD_WRITE_BUFFER_DONE,
141 HQA_CMD_GET_BAND,
142 HQA_CMD_GET_CFG,
143 HQA_CMD_GET_TX_POWER,
144 HQA_CMD_GET_TX_TONE_POWER,
145 HQA_CMD_GET_EFUSE_FREE_BLOCK,
146 HQA_CMD_GET_FREQ_OFFSET,
147 HQA_CMD_GET_FW_INFO,
148 HQA_CMD_GET_RX_INFO,
149 HQA_CMD_GET_RF_CAP,
150 HQA_CMD_CHECK_EFUSE_MODE,
151 HQA_CMD_CHECK_EFUSE_MODE_TYPE,
152 HQA_CMD_CHECK_EFUSE_MODE_NATIVE,
153 HQA_CMD_ANT_SWAP_CAP,
154 HQA_CMD_RESET_TX_RX_COUNTER,
155 HQA_CMD_CONTINUOUS_TX,
156
157 HQA_CMD_EXT,
158 HQA_CMD_ERR,
159
160 __HQA_CMD_MAX_NUM,
161};
162
163enum atenl_ext_cmd {
164 HQA_EXT_CMD_UNSPEC,
165
166 HQA_EXT_CMD_SET_CHANNEL,
167 HQA_EXT_CMD_SET_TX,
168 HQA_EXT_CMD_START_TX,
169 HQA_EXT_CMD_START_RX,
170 HQA_EXT_CMD_STOP_TX,
171 HQA_EXT_CMD_STOP_RX,
172 HQA_EXT_CMD_SET_TX_TIME_OPT,
173
174 HQA_EXT_CMD_OFF_CH_SCAN,
175
176 HQA_EXT_CMD_IBF_SET_VAL,
177 HQA_EXT_CMD_IBF_GET_STATUS,
178 HQA_EXT_CMD_IBF_PROF_UPDATE_ALL,
179
180 HQA_EXT_CMD_ERR,
181
182 __HQA_EXT_CMD_MAX_NUM,
183};
184
185struct atenl_data {
186 u8 buf[RACFG_PKT_MAX_SIZE];
187 int len;
developer5698c9c2022-05-30 16:40:23 +0800188 u16 cmd_id;
189 u8 ext_id;
developer3abe1ad2022-01-24 11:13:32 +0800190 enum atenl_cmd cmd;
developer3abe1ad2022-01-24 11:13:32 +0800191 enum atenl_ext_cmd ext_cmd;
192};
193
developer5698c9c2022-05-30 16:40:23 +0800194struct atenl_ops {
developer3abe1ad2022-01-24 11:13:32 +0800195 int (*ops)(struct atenl *an, struct atenl_data *data);
developer5698c9c2022-05-30 16:40:23 +0800196 u8 cmd;
197 u8 flags;
198 u16 cmd_id;
199 u16 resp_len;
developer3abe1ad2022-01-24 11:13:32 +0800200};
201
developer5698c9c2022-05-30 16:40:23 +0800202#define ATENL_OPS_FLAG_EXT_CMD BIT(0)
203#define ATENL_OPS_FLAG_LEGACY BIT(1)
204#define ATENL_OPS_FLAG_SKIP BIT(2)
205
developer3abe1ad2022-01-24 11:13:32 +0800206static inline struct atenl_cmd_hdr * atenl_hdr(struct atenl_data *data)
207{
208 u8 *hqa_data = (u8 *)data->buf + ETH_HLEN;
209
210 return (struct atenl_cmd_hdr *)hqa_data;
211}
212
developer3abe1ad2022-01-24 11:13:32 +0800213enum atenl_phy_type {
214 ATENL_PHY_TYPE_CCK,
215 ATENL_PHY_TYPE_OFDM,
216 ATENL_PHY_TYPE_HT,
217 ATENL_PHY_TYPE_HT_GF,
218 ATENL_PHY_TYPE_VHT,
219 ATENL_PHY_TYPE_HE_SU = 8,
220 ATENL_PHY_TYPE_HE_EXT_SU,
221 ATENL_PHY_TYPE_HE_TB,
222 ATENL_PHY_TYPE_HE_MU,
223};
224
225enum atenl_e2p_mode {
226 E2P_EFUSE_MODE = 1,
227 E2P_FLASH_MODE,
228 E2P_EEPROM_MODE,
229 E2P_BIN_MODE,
230};
231
232enum atenl_band_type {
233 BAND_TYPE_UNUSE,
234 BAND_TYPE_2G,
235 BAND_TYPE_5G,
236 BAND_TYPE_2G_5G,
237 BAND_TYPE_6G,
238 BAND_TYPE_2G_6G,
239 BAND_TYPE_5G_6G,
240 BAND_TYPE_2G_5G_6G,
241};
242
243enum atenl_ch_band {
244 CH_BAND_2GHZ,
245 CH_BAND_5GHZ,
246 CH_BAND_6GHZ,
247};
248
249/* for mt7915 */
250enum {
251 MT_EE_BAND_SEL_DEFAULT,
252 MT_EE_BAND_SEL_5GHZ,
253 MT_EE_BAND_SEL_2GHZ,
254 MT_EE_BAND_SEL_DUAL,
255};
256
257/* for mt7916/mt7986 */
258enum {
259 MT_EE_BAND_SEL_2G,
260 MT_EE_BAND_SEL_5G,
261 MT_EE_BAND_SEL_6G,
262 MT_EE_BAND_SEL_5G_6G,
263};
264
265#define MT_EE_WIFI_CONF 0x190
266#define MT_EE_WIFI_CONF0_BAND_SEL GENMASK(7, 6)
267
268enum {
269 MT7976_ONE_ADIE_DBDC = 0x7,
270 MT7975_ONE_ADIE_SINGLE_BAND = 0x8, /* AX7800 */
271 MT7976_ONE_ADIE_SINGLE_BAND = 0xa, /* AX7800 */
272 MT7975_DUAL_ADIE_DBDC = 0xd, /* AX6000 */
273 MT7976_DUAL_ADIE_DBDC = 0xf, /* AX6000 */
274};
275
276enum {
277 TEST_CBW_20MHZ,
278 TEST_CBW_40MHZ,
279 TEST_CBW_80MHZ,
280 TEST_CBW_10MHZ,
281 TEST_CBW_5MHZ,
282 TEST_CBW_160MHZ,
283 TEST_CBW_8080MHZ,
284
285 TEST_CBW_MAX = TEST_CBW_8080MHZ - 1,
286};
287
288struct atenl_rx_info_hdr {
289 __be32 type;
290 __be32 ver;
291 __be32 val;
292 __be32 len;
293} __attribute__((packed));
294
295struct atenl_rx_info_band {
296 __be32 mac_rx_fcs_err_cnt;
297 __be32 mac_rx_mdrdy_cnt;
298 __be32 mac_rx_len_mismatch;
299 __be32 mac_rx_fcs_ok_cnt;
300 __be32 phy_rx_fcs_err_cnt_cck;
301 __be32 phy_rx_fcs_err_cnt_ofdm;
302 __be32 phy_rx_pd_cck;
303 __be32 phy_rx_pd_ofdm;
304 __be32 phy_rx_sig_err_cck;
305 __be32 phy_rx_sfd_err_cck;
306 __be32 phy_rx_sig_err_ofdm;
307 __be32 phy_rx_tag_err_ofdm;
308 __be32 phy_rx_mdrdy_cnt_cck;
309 __be32 phy_rx_mdrdy_cnt_ofdm;
310} __attribute__((packed));
311
312struct atenl_rx_info_path {
313 __be32 rcpi;
314 __be32 rssi;
315 __be32 fagc_ib_rssi;
316 __be32 fagc_wb_rssi;
317 __be32 inst_ib_rssi;
318 __be32 inst_wb_rssi;
319} __attribute__((packed));
320
321struct atenl_rx_info_user {
322 __be32 freq_offset;
323 __be32 snr;
324 __be32 fcs_error_cnt;
325} __attribute__((packed));
326
327struct atenl_rx_info_comm {
328 __be32 rx_fifo_full;
329 __be32 aci_hit_low;
330 __be32 aci_hit_high;
331 __be32 mu_pkt_count;
332 __be32 sig_mcs;
333 __be32 sinr;
334 __be32 driver_rx_count;
335} __attribute__((packed));
336
337enum atenl_ibf_action {
338 TXBF_ACT_INIT = 1,
339 TXBF_ACT_CHANNEL,
340 TXBF_ACT_MCS,
341 TXBF_ACT_POWER,
342 TXBF_ACT_TX_ANT,
343 TXBF_ACT_RX_START,
344 TXBF_ACT_RX_ANT,
345 TXBF_ACT_LNA_GAIN,
346 TXBF_ACT_IBF_PHASE_COMP,
347 TXBF_ACT_TX_PKT,
348 TXBF_ACT_IBF_PROF_UPDATE,
349 TXBF_ACT_EBF_PROF_UPDATE,
350 TXBF_ACT_IBF_PHASE_CAL,
351 TXBF_ACT_IBF_PHASE_E2P_UPDATE = 16,
352};
353
354static inline bool is_mt7915(struct atenl *an)
355{
356 return an->chip_id == 0x7915;
357}
358
359static inline bool is_mt7916(struct atenl *an)
360{
361 return (an->chip_id == 0x7916) || (an->chip_id == 0x7906);
362}
363
364static inline bool is_mt7986(struct atenl *an)
365{
366 return an->chip_id == 0x7986;
367}
368
369int atenl_eth_init(struct atenl *an);
370int atenl_eth_recv(struct atenl *an, struct atenl_data *data);
371int atenl_eth_send(struct atenl *an, struct atenl_data *data);
developer5698c9c2022-05-30 16:40:23 +0800372int atenl_hqa_proc_cmd(struct atenl *an);
developer3abe1ad2022-01-24 11:13:32 +0800373int atenl_nl_process(struct atenl *an, struct atenl_data *data);
374int atenl_nl_process_many(struct atenl *an, struct atenl_data *data);
375int atenl_nl_check_mtd(struct atenl *an);
376int atenl_nl_write_eeprom(struct atenl *an, u32 offset, u8 *val, int len);
developer9b7cdad2022-03-10 14:24:55 +0800377int atenl_nl_write_efuse_all(struct atenl *an);
developer3abe1ad2022-01-24 11:13:32 +0800378int atenl_nl_update_buffer_mode(struct atenl *an);
379int atenl_nl_set_state(struct atenl *an, u8 band,
380 enum mt76_testmode_state state);
developer5698c9c2022-05-30 16:40:23 +0800381int atenl_nl_set_aid(struct atenl *an, u8 band, u8 aid);
developer3abe1ad2022-01-24 11:13:32 +0800382int atenl_eeprom_init(struct atenl *an, u8 phy_idx);
383void atenl_eeprom_close(struct atenl *an);
384int atenl_eeprom_write_mtd(struct atenl *an);
385int atenl_eeprom_read_from_driver(struct atenl *an, u32 offset, int len);
386void atenl_eeprom_cmd_handler(struct atenl *an, u8 phy_idx, char *cmd);
387u16 atenl_get_center_channel(u8 bw, u8 ch_band, u16 ctrl_ch);
388int atenl_reg_read(struct atenl *an, u32 offset, u32 *res);
389int atenl_reg_write(struct atenl *an, u32 offset, u32 val);
developer5698c9c2022-05-30 16:40:23 +0800390int atenl_rf_read(struct atenl *an, u32 wf_sel, u32 offset, u32 *res);
391int atenl_rf_write(struct atenl *an, u32 wf_sel, u32 offset, u32 val);
developer3abe1ad2022-01-24 11:13:32 +0800392
393#endif