blob: 407882592a16d8417844fcc559f531f4b5425435 [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
93 /* intermediate data */
94 u8 ibf_mcs;
95 u8 ibf_ant;
developer3abe1ad2022-01-24 11:13:32 +080096};
97
98struct atenl_cmd_hdr {
99 __be32 magic_no;
100 __be16 cmd_type;
101 __be16 cmd_id;
102 __be16 len;
103 __be16 seq;
104 u8 data[2048];
105} __attribute__((packed));
106
107enum atenl_cmd {
108 HQA_CMD_UNKNOWN,
109 HQA_CMD_LEGACY, /* legacy or deprecated */
110
111 HQA_CMD_OPEN_ADAPTER,
112 HQA_CMD_CLOSE_ADAPTER,
113 HQA_CMD_GET_CHIP_ID,
114 HQA_CMD_GET_SUB_CHIP_ID,
115 HQA_CMD_SET_TX_BW,
116 HQA_CMD_SET_TX_PKT_BW,
117 HQA_CMD_SET_TX_PRI_BW,
118 HQA_CMD_GET_TX_INFO,
119 HQA_CMD_SET_TX_PATH,
120 HQA_CMD_SET_TX_POWER,
121 HQA_CMD_SET_TX_POWER_MANUAL,
122 HQA_CMD_SET_RF_MODE,
123 HQA_CMD_SET_RX_PATH,
124 HQA_CMD_SET_RX_PKT_LEN,
125 HQA_CMD_SET_FREQ_OFFSET,
126 HQA_CMD_SET_TSSI,
127 HQA_CMD_SET_CFG,
128 HQA_CMD_SET_RU,
129 HQA_CMD_SET_BAND,
developer5698c9c2022-05-30 16:40:23 +0800130 HQA_CMD_SET_EEPROM_TO_FW,
developer3abe1ad2022-01-24 11:13:32 +0800131 HQA_CMD_READ_MAC_BBP_REG,
developer5698c9c2022-05-30 16:40:23 +0800132 HQA_CMD_READ_MAC_BBP_REG_QA,
developer3abe1ad2022-01-24 11:13:32 +0800133 HQA_CMD_READ_RF_REG,
134 HQA_CMD_READ_EEPROM_BULK,
135 HQA_CMD_READ_TEMPERATURE,
136 HQA_CMD_WRITE_MAC_BBP_REG,
137 HQA_CMD_WRITE_RF_REG,
138 HQA_CMD_WRITE_EEPROM_BULK,
139 HQA_CMD_WRITE_BUFFER_DONE,
140 HQA_CMD_GET_BAND,
141 HQA_CMD_GET_CFG,
142 HQA_CMD_GET_TX_POWER,
143 HQA_CMD_GET_TX_TONE_POWER,
144 HQA_CMD_GET_EFUSE_FREE_BLOCK,
145 HQA_CMD_GET_FREQ_OFFSET,
146 HQA_CMD_GET_FW_INFO,
147 HQA_CMD_GET_RX_INFO,
148 HQA_CMD_GET_RF_CAP,
149 HQA_CMD_CHECK_EFUSE_MODE,
150 HQA_CMD_CHECK_EFUSE_MODE_TYPE,
151 HQA_CMD_CHECK_EFUSE_MODE_NATIVE,
152 HQA_CMD_ANT_SWAP_CAP,
153 HQA_CMD_RESET_TX_RX_COUNTER,
154 HQA_CMD_CONTINUOUS_TX,
155
156 HQA_CMD_EXT,
157 HQA_CMD_ERR,
158
159 __HQA_CMD_MAX_NUM,
160};
161
162enum atenl_ext_cmd {
163 HQA_EXT_CMD_UNSPEC,
164
165 HQA_EXT_CMD_SET_CHANNEL,
166 HQA_EXT_CMD_SET_TX,
167 HQA_EXT_CMD_START_TX,
168 HQA_EXT_CMD_START_RX,
169 HQA_EXT_CMD_STOP_TX,
170 HQA_EXT_CMD_STOP_RX,
171 HQA_EXT_CMD_SET_TX_TIME_OPT,
172
173 HQA_EXT_CMD_OFF_CH_SCAN,
174
175 HQA_EXT_CMD_IBF_SET_VAL,
176 HQA_EXT_CMD_IBF_GET_STATUS,
177 HQA_EXT_CMD_IBF_PROF_UPDATE_ALL,
178
179 HQA_EXT_CMD_ERR,
180
181 __HQA_EXT_CMD_MAX_NUM,
182};
183
184struct atenl_data {
185 u8 buf[RACFG_PKT_MAX_SIZE];
186 int len;
developer5698c9c2022-05-30 16:40:23 +0800187 u16 cmd_id;
188 u8 ext_id;
developer3abe1ad2022-01-24 11:13:32 +0800189 enum atenl_cmd cmd;
developer3abe1ad2022-01-24 11:13:32 +0800190 enum atenl_ext_cmd ext_cmd;
191};
192
developer5698c9c2022-05-30 16:40:23 +0800193struct atenl_ops {
developer3abe1ad2022-01-24 11:13:32 +0800194 int (*ops)(struct atenl *an, struct atenl_data *data);
developer5698c9c2022-05-30 16:40:23 +0800195 u8 cmd;
196 u8 flags;
197 u16 cmd_id;
198 u16 resp_len;
developer3abe1ad2022-01-24 11:13:32 +0800199};
200
developer5698c9c2022-05-30 16:40:23 +0800201#define ATENL_OPS_FLAG_EXT_CMD BIT(0)
202#define ATENL_OPS_FLAG_LEGACY BIT(1)
203#define ATENL_OPS_FLAG_SKIP BIT(2)
204
developer3abe1ad2022-01-24 11:13:32 +0800205static inline struct atenl_cmd_hdr * atenl_hdr(struct atenl_data *data)
206{
207 u8 *hqa_data = (u8 *)data->buf + ETH_HLEN;
208
209 return (struct atenl_cmd_hdr *)hqa_data;
210}
211
developer3abe1ad2022-01-24 11:13:32 +0800212enum atenl_phy_type {
213 ATENL_PHY_TYPE_CCK,
214 ATENL_PHY_TYPE_OFDM,
215 ATENL_PHY_TYPE_HT,
216 ATENL_PHY_TYPE_HT_GF,
217 ATENL_PHY_TYPE_VHT,
218 ATENL_PHY_TYPE_HE_SU = 8,
219 ATENL_PHY_TYPE_HE_EXT_SU,
220 ATENL_PHY_TYPE_HE_TB,
221 ATENL_PHY_TYPE_HE_MU,
222};
223
224enum atenl_e2p_mode {
225 E2P_EFUSE_MODE = 1,
226 E2P_FLASH_MODE,
227 E2P_EEPROM_MODE,
228 E2P_BIN_MODE,
229};
230
231enum atenl_band_type {
232 BAND_TYPE_UNUSE,
233 BAND_TYPE_2G,
234 BAND_TYPE_5G,
235 BAND_TYPE_2G_5G,
236 BAND_TYPE_6G,
237 BAND_TYPE_2G_6G,
238 BAND_TYPE_5G_6G,
239 BAND_TYPE_2G_5G_6G,
240};
241
242enum atenl_ch_band {
243 CH_BAND_2GHZ,
244 CH_BAND_5GHZ,
245 CH_BAND_6GHZ,
246};
247
248/* for mt7915 */
249enum {
250 MT_EE_BAND_SEL_DEFAULT,
251 MT_EE_BAND_SEL_5GHZ,
252 MT_EE_BAND_SEL_2GHZ,
253 MT_EE_BAND_SEL_DUAL,
254};
255
256/* for mt7916/mt7986 */
257enum {
258 MT_EE_BAND_SEL_2G,
259 MT_EE_BAND_SEL_5G,
260 MT_EE_BAND_SEL_6G,
261 MT_EE_BAND_SEL_5G_6G,
262};
263
264#define MT_EE_WIFI_CONF 0x190
265#define MT_EE_WIFI_CONF0_BAND_SEL GENMASK(7, 6)
266
267enum {
268 MT7976_ONE_ADIE_DBDC = 0x7,
269 MT7975_ONE_ADIE_SINGLE_BAND = 0x8, /* AX7800 */
270 MT7976_ONE_ADIE_SINGLE_BAND = 0xa, /* AX7800 */
271 MT7975_DUAL_ADIE_DBDC = 0xd, /* AX6000 */
272 MT7976_DUAL_ADIE_DBDC = 0xf, /* AX6000 */
273};
274
275enum {
276 TEST_CBW_20MHZ,
277 TEST_CBW_40MHZ,
278 TEST_CBW_80MHZ,
279 TEST_CBW_10MHZ,
280 TEST_CBW_5MHZ,
281 TEST_CBW_160MHZ,
282 TEST_CBW_8080MHZ,
283
284 TEST_CBW_MAX = TEST_CBW_8080MHZ - 1,
285};
286
287struct atenl_rx_info_hdr {
288 __be32 type;
289 __be32 ver;
290 __be32 val;
291 __be32 len;
292} __attribute__((packed));
293
294struct atenl_rx_info_band {
295 __be32 mac_rx_fcs_err_cnt;
296 __be32 mac_rx_mdrdy_cnt;
297 __be32 mac_rx_len_mismatch;
298 __be32 mac_rx_fcs_ok_cnt;
299 __be32 phy_rx_fcs_err_cnt_cck;
300 __be32 phy_rx_fcs_err_cnt_ofdm;
301 __be32 phy_rx_pd_cck;
302 __be32 phy_rx_pd_ofdm;
303 __be32 phy_rx_sig_err_cck;
304 __be32 phy_rx_sfd_err_cck;
305 __be32 phy_rx_sig_err_ofdm;
306 __be32 phy_rx_tag_err_ofdm;
307 __be32 phy_rx_mdrdy_cnt_cck;
308 __be32 phy_rx_mdrdy_cnt_ofdm;
309} __attribute__((packed));
310
311struct atenl_rx_info_path {
312 __be32 rcpi;
313 __be32 rssi;
314 __be32 fagc_ib_rssi;
315 __be32 fagc_wb_rssi;
316 __be32 inst_ib_rssi;
317 __be32 inst_wb_rssi;
318} __attribute__((packed));
319
320struct atenl_rx_info_user {
321 __be32 freq_offset;
322 __be32 snr;
323 __be32 fcs_error_cnt;
324} __attribute__((packed));
325
326struct atenl_rx_info_comm {
327 __be32 rx_fifo_full;
328 __be32 aci_hit_low;
329 __be32 aci_hit_high;
330 __be32 mu_pkt_count;
331 __be32 sig_mcs;
332 __be32 sinr;
333 __be32 driver_rx_count;
334} __attribute__((packed));
335
336enum atenl_ibf_action {
337 TXBF_ACT_INIT = 1,
338 TXBF_ACT_CHANNEL,
339 TXBF_ACT_MCS,
340 TXBF_ACT_POWER,
341 TXBF_ACT_TX_ANT,
342 TXBF_ACT_RX_START,
343 TXBF_ACT_RX_ANT,
344 TXBF_ACT_LNA_GAIN,
345 TXBF_ACT_IBF_PHASE_COMP,
346 TXBF_ACT_TX_PKT,
347 TXBF_ACT_IBF_PROF_UPDATE,
348 TXBF_ACT_EBF_PROF_UPDATE,
349 TXBF_ACT_IBF_PHASE_CAL,
350 TXBF_ACT_IBF_PHASE_E2P_UPDATE = 16,
351};
352
353static inline bool is_mt7915(struct atenl *an)
354{
355 return an->chip_id == 0x7915;
356}
357
358static inline bool is_mt7916(struct atenl *an)
359{
360 return (an->chip_id == 0x7916) || (an->chip_id == 0x7906);
361}
362
363static inline bool is_mt7986(struct atenl *an)
364{
365 return an->chip_id == 0x7986;
366}
367
368int atenl_eth_init(struct atenl *an);
369int atenl_eth_recv(struct atenl *an, struct atenl_data *data);
370int atenl_eth_send(struct atenl *an, struct atenl_data *data);
developer5698c9c2022-05-30 16:40:23 +0800371int atenl_hqa_proc_cmd(struct atenl *an);
developer3abe1ad2022-01-24 11:13:32 +0800372int atenl_nl_process(struct atenl *an, struct atenl_data *data);
373int atenl_nl_process_many(struct atenl *an, struct atenl_data *data);
374int atenl_nl_check_mtd(struct atenl *an);
375int atenl_nl_write_eeprom(struct atenl *an, u32 offset, u8 *val, int len);
developer9b7cdad2022-03-10 14:24:55 +0800376int atenl_nl_write_efuse_all(struct atenl *an);
developer3abe1ad2022-01-24 11:13:32 +0800377int atenl_nl_update_buffer_mode(struct atenl *an);
378int atenl_nl_set_state(struct atenl *an, u8 band,
379 enum mt76_testmode_state state);
developer5698c9c2022-05-30 16:40:23 +0800380int atenl_nl_set_aid(struct atenl *an, u8 band, u8 aid);
developer3abe1ad2022-01-24 11:13:32 +0800381int atenl_eeprom_init(struct atenl *an, u8 phy_idx);
382void atenl_eeprom_close(struct atenl *an);
383int atenl_eeprom_write_mtd(struct atenl *an);
384int atenl_eeprom_read_from_driver(struct atenl *an, u32 offset, int len);
385void atenl_eeprom_cmd_handler(struct atenl *an, u8 phy_idx, char *cmd);
386u16 atenl_get_center_channel(u8 bw, u8 ch_band, u16 ctrl_ch);
387int atenl_reg_read(struct atenl *an, u32 offset, u32 *res);
388int atenl_reg_write(struct atenl *an, u32 offset, u32 val);
developer5698c9c2022-05-30 16:40:23 +0800389int atenl_rf_read(struct atenl *an, u32 wf_sel, u32 offset, u32 *res);
390int atenl_rf_write(struct atenl *an, u32 wf_sel, u32 offset, u32 val);
developer3abe1ad2022-01-24 11:13:32 +0800391
392#endif