blob: 42d945536cab79b49969c7cb4b4973a998071a3b [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
developer071927d2022-08-31 20:39:29 +080025#define PRE_CAL_INFO 16
26#define DPD_INFO_CH_SHIFT 24
27#define DPD_INFO_2G_SHIFT 16
28#define DPD_INFO_5G_SHIFT 8
29#define DPD_INFO_6G_SHIFT 0
30#define DPD_INFO_MASK GENMASK(7, 0)
31#define MT_EE_CAL_UNIT 1024
developer3abe1ad2022-01-24 11:13:32 +080032
33#define RACFG_CMD_TYPE_MASK GENMASK(14, 0)
34#define RACFG_CMD_TYPE_ETHREQ BIT(3)
35#define RACFG_CMD_TYPE_PLATFORM_MODULE GENMASK(4, 3)
36
developer3abe1ad2022-01-24 11:13:32 +080037#define set_band_val(_an, _band, _field, _val) \
38 _an->anb[_band]._field = (_val)
39#define get_band_val(_an, _band, _field) \
40 (_an->anb[_band]._field)
41
42enum atenl_rf_mode {
43 ATENL_RF_MODE_NORMAL,
44 ATENL_RF_MODE_TEST,
45 ATENL_RF_MODE_ICAP,
46 ATENL_RF_MODE_ICAP_OVERLAP,
47
48 __ATENL_RF_MODE_MAX,
49};
50
51struct atenl_rx_stat {
52 u64 total;
53 u64 ok_cnt;
54 u64 err_cnt;
55 u64 len_mismatch;
56};
57
58struct atenl_band {
59 bool valid;
60 u8 phy_idx;
61 u8 cap;
62 u8 chainmask;
63
64 enum mt76_testmode_state cur_state;
65 s8 tx_power;
66 enum atenl_rf_mode rf_mode;
67
68 bool use_tx_time;
developer5698c9c2022-05-30 16:40:23 +080069 u32 tx_time;
70 u32 tx_mpdu_len;
developer3abe1ad2022-01-24 11:13:32 +080071
72 bool reset_tx_cnt;
73 bool reset_rx_cnt;
74
75 /* history */
76 struct atenl_rx_stat rx_stat;
77};
78
developer5698c9c2022-05-30 16:40:23 +080079#define MAX_BAND_NUM 3
developer3abe1ad2022-01-24 11:13:32 +080080
81struct atenl {
82 struct atenl_band anb[MAX_BAND_NUM];
83 u16 chip_id;
developer5698c9c2022-05-30 16:40:23 +080084 u16 adie_id;
85 u8 sub_chip_id;
developer3abe1ad2022-01-24 11:13:32 +080086 u8 cur_band;
87
88 u8 mac_addr[ETH_ALEN];
89 bool unicast;
90 int sock_eth;
developer3abe1ad2022-01-24 11:13:32 +080091
92 const char *mtd_part;
93 u32 mtd_offset;
developer071927d2022-08-31 20:39:29 +080094 u8 is_main_phy;
developer3abe1ad2022-01-24 11:13:32 +080095 u8 *eeprom_data;
96 int eeprom_fd;
97 u16 eeprom_size;
developer071927d2022-08-31 20:39:29 +080098 u32 eeprom_prek_offs;
99
100 u8 *cal;
101 u32 cal_info[5];
developer3abe1ad2022-01-24 11:13:32 +0800102
103 bool cmd_mode;
developer5698c9c2022-05-30 16:40:23 +0800104
105 /* intermediate data */
106 u8 ibf_mcs;
107 u8 ibf_ant;
developer3abe1ad2022-01-24 11:13:32 +0800108};
109
110struct atenl_cmd_hdr {
111 __be32 magic_no;
112 __be16 cmd_type;
113 __be16 cmd_id;
114 __be16 len;
115 __be16 seq;
116 u8 data[2048];
117} __attribute__((packed));
118
119enum atenl_cmd {
120 HQA_CMD_UNKNOWN,
121 HQA_CMD_LEGACY, /* legacy or deprecated */
122
123 HQA_CMD_OPEN_ADAPTER,
124 HQA_CMD_CLOSE_ADAPTER,
125 HQA_CMD_GET_CHIP_ID,
126 HQA_CMD_GET_SUB_CHIP_ID,
127 HQA_CMD_SET_TX_BW,
128 HQA_CMD_SET_TX_PKT_BW,
129 HQA_CMD_SET_TX_PRI_BW,
130 HQA_CMD_GET_TX_INFO,
131 HQA_CMD_SET_TX_PATH,
132 HQA_CMD_SET_TX_POWER,
133 HQA_CMD_SET_TX_POWER_MANUAL,
134 HQA_CMD_SET_RF_MODE,
135 HQA_CMD_SET_RX_PATH,
136 HQA_CMD_SET_RX_PKT_LEN,
137 HQA_CMD_SET_FREQ_OFFSET,
138 HQA_CMD_SET_TSSI,
139 HQA_CMD_SET_CFG,
140 HQA_CMD_SET_RU,
141 HQA_CMD_SET_BAND,
developer5698c9c2022-05-30 16:40:23 +0800142 HQA_CMD_SET_EEPROM_TO_FW,
developer3abe1ad2022-01-24 11:13:32 +0800143 HQA_CMD_READ_MAC_BBP_REG,
developer5698c9c2022-05-30 16:40:23 +0800144 HQA_CMD_READ_MAC_BBP_REG_QA,
developer3abe1ad2022-01-24 11:13:32 +0800145 HQA_CMD_READ_RF_REG,
146 HQA_CMD_READ_EEPROM_BULK,
147 HQA_CMD_READ_TEMPERATURE,
148 HQA_CMD_WRITE_MAC_BBP_REG,
149 HQA_CMD_WRITE_RF_REG,
150 HQA_CMD_WRITE_EEPROM_BULK,
151 HQA_CMD_WRITE_BUFFER_DONE,
152 HQA_CMD_GET_BAND,
153 HQA_CMD_GET_CFG,
154 HQA_CMD_GET_TX_POWER,
155 HQA_CMD_GET_TX_TONE_POWER,
156 HQA_CMD_GET_EFUSE_FREE_BLOCK,
157 HQA_CMD_GET_FREQ_OFFSET,
158 HQA_CMD_GET_FW_INFO,
159 HQA_CMD_GET_RX_INFO,
160 HQA_CMD_GET_RF_CAP,
161 HQA_CMD_CHECK_EFUSE_MODE,
162 HQA_CMD_CHECK_EFUSE_MODE_TYPE,
163 HQA_CMD_CHECK_EFUSE_MODE_NATIVE,
164 HQA_CMD_ANT_SWAP_CAP,
165 HQA_CMD_RESET_TX_RX_COUNTER,
166 HQA_CMD_CONTINUOUS_TX,
167
168 HQA_CMD_EXT,
169 HQA_CMD_ERR,
170
171 __HQA_CMD_MAX_NUM,
172};
173
174enum atenl_ext_cmd {
175 HQA_EXT_CMD_UNSPEC,
176
177 HQA_EXT_CMD_SET_CHANNEL,
178 HQA_EXT_CMD_SET_TX,
179 HQA_EXT_CMD_START_TX,
180 HQA_EXT_CMD_START_RX,
181 HQA_EXT_CMD_STOP_TX,
182 HQA_EXT_CMD_STOP_RX,
183 HQA_EXT_CMD_SET_TX_TIME_OPT,
184
185 HQA_EXT_CMD_OFF_CH_SCAN,
186
187 HQA_EXT_CMD_IBF_SET_VAL,
188 HQA_EXT_CMD_IBF_GET_STATUS,
189 HQA_EXT_CMD_IBF_PROF_UPDATE_ALL,
190
191 HQA_EXT_CMD_ERR,
192
193 __HQA_EXT_CMD_MAX_NUM,
194};
195
196struct atenl_data {
197 u8 buf[RACFG_PKT_MAX_SIZE];
198 int len;
developer5698c9c2022-05-30 16:40:23 +0800199 u16 cmd_id;
200 u8 ext_id;
developer3abe1ad2022-01-24 11:13:32 +0800201 enum atenl_cmd cmd;
developer3abe1ad2022-01-24 11:13:32 +0800202 enum atenl_ext_cmd ext_cmd;
203};
204
developer5698c9c2022-05-30 16:40:23 +0800205struct atenl_ops {
developer3abe1ad2022-01-24 11:13:32 +0800206 int (*ops)(struct atenl *an, struct atenl_data *data);
developer5698c9c2022-05-30 16:40:23 +0800207 u8 cmd;
208 u8 flags;
209 u16 cmd_id;
210 u16 resp_len;
developer3abe1ad2022-01-24 11:13:32 +0800211};
212
developer5698c9c2022-05-30 16:40:23 +0800213#define ATENL_OPS_FLAG_EXT_CMD BIT(0)
214#define ATENL_OPS_FLAG_LEGACY BIT(1)
215#define ATENL_OPS_FLAG_SKIP BIT(2)
216
developer3abe1ad2022-01-24 11:13:32 +0800217static inline struct atenl_cmd_hdr * atenl_hdr(struct atenl_data *data)
218{
219 u8 *hqa_data = (u8 *)data->buf + ETH_HLEN;
220
221 return (struct atenl_cmd_hdr *)hqa_data;
222}
223
developer3abe1ad2022-01-24 11:13:32 +0800224enum atenl_phy_type {
225 ATENL_PHY_TYPE_CCK,
226 ATENL_PHY_TYPE_OFDM,
227 ATENL_PHY_TYPE_HT,
228 ATENL_PHY_TYPE_HT_GF,
229 ATENL_PHY_TYPE_VHT,
230 ATENL_PHY_TYPE_HE_SU = 8,
231 ATENL_PHY_TYPE_HE_EXT_SU,
232 ATENL_PHY_TYPE_HE_TB,
233 ATENL_PHY_TYPE_HE_MU,
234};
235
236enum atenl_e2p_mode {
237 E2P_EFUSE_MODE = 1,
238 E2P_FLASH_MODE,
239 E2P_EEPROM_MODE,
240 E2P_BIN_MODE,
241};
242
243enum atenl_band_type {
244 BAND_TYPE_UNUSE,
245 BAND_TYPE_2G,
246 BAND_TYPE_5G,
247 BAND_TYPE_2G_5G,
248 BAND_TYPE_6G,
249 BAND_TYPE_2G_6G,
250 BAND_TYPE_5G_6G,
251 BAND_TYPE_2G_5G_6G,
252};
253
254enum atenl_ch_band {
255 CH_BAND_2GHZ,
256 CH_BAND_5GHZ,
257 CH_BAND_6GHZ,
258};
259
260/* for mt7915 */
261enum {
262 MT_EE_BAND_SEL_DEFAULT,
263 MT_EE_BAND_SEL_5GHZ,
264 MT_EE_BAND_SEL_2GHZ,
265 MT_EE_BAND_SEL_DUAL,
266};
267
268/* for mt7916/mt7986 */
269enum {
270 MT_EE_BAND_SEL_2G,
271 MT_EE_BAND_SEL_5G,
272 MT_EE_BAND_SEL_6G,
273 MT_EE_BAND_SEL_5G_6G,
274};
275
276#define MT_EE_WIFI_CONF 0x190
277#define MT_EE_WIFI_CONF0_BAND_SEL GENMASK(7, 6)
278
279enum {
280 MT7976_ONE_ADIE_DBDC = 0x7,
281 MT7975_ONE_ADIE_SINGLE_BAND = 0x8, /* AX7800 */
282 MT7976_ONE_ADIE_SINGLE_BAND = 0xa, /* AX7800 */
283 MT7975_DUAL_ADIE_DBDC = 0xd, /* AX6000 */
284 MT7976_DUAL_ADIE_DBDC = 0xf, /* AX6000 */
285};
286
287enum {
288 TEST_CBW_20MHZ,
289 TEST_CBW_40MHZ,
290 TEST_CBW_80MHZ,
291 TEST_CBW_10MHZ,
292 TEST_CBW_5MHZ,
293 TEST_CBW_160MHZ,
294 TEST_CBW_8080MHZ,
295
296 TEST_CBW_MAX = TEST_CBW_8080MHZ - 1,
297};
298
299struct atenl_rx_info_hdr {
300 __be32 type;
301 __be32 ver;
302 __be32 val;
303 __be32 len;
304} __attribute__((packed));
305
306struct atenl_rx_info_band {
307 __be32 mac_rx_fcs_err_cnt;
308 __be32 mac_rx_mdrdy_cnt;
309 __be32 mac_rx_len_mismatch;
310 __be32 mac_rx_fcs_ok_cnt;
311 __be32 phy_rx_fcs_err_cnt_cck;
312 __be32 phy_rx_fcs_err_cnt_ofdm;
313 __be32 phy_rx_pd_cck;
314 __be32 phy_rx_pd_ofdm;
315 __be32 phy_rx_sig_err_cck;
316 __be32 phy_rx_sfd_err_cck;
317 __be32 phy_rx_sig_err_ofdm;
318 __be32 phy_rx_tag_err_ofdm;
319 __be32 phy_rx_mdrdy_cnt_cck;
320 __be32 phy_rx_mdrdy_cnt_ofdm;
321} __attribute__((packed));
322
323struct atenl_rx_info_path {
324 __be32 rcpi;
325 __be32 rssi;
326 __be32 fagc_ib_rssi;
327 __be32 fagc_wb_rssi;
328 __be32 inst_ib_rssi;
329 __be32 inst_wb_rssi;
330} __attribute__((packed));
331
332struct atenl_rx_info_user {
333 __be32 freq_offset;
334 __be32 snr;
335 __be32 fcs_error_cnt;
336} __attribute__((packed));
337
338struct atenl_rx_info_comm {
339 __be32 rx_fifo_full;
340 __be32 aci_hit_low;
341 __be32 aci_hit_high;
342 __be32 mu_pkt_count;
343 __be32 sig_mcs;
344 __be32 sinr;
345 __be32 driver_rx_count;
346} __attribute__((packed));
347
348enum atenl_ibf_action {
349 TXBF_ACT_INIT = 1,
350 TXBF_ACT_CHANNEL,
351 TXBF_ACT_MCS,
352 TXBF_ACT_POWER,
353 TXBF_ACT_TX_ANT,
354 TXBF_ACT_RX_START,
355 TXBF_ACT_RX_ANT,
356 TXBF_ACT_LNA_GAIN,
357 TXBF_ACT_IBF_PHASE_COMP,
358 TXBF_ACT_TX_PKT,
359 TXBF_ACT_IBF_PROF_UPDATE,
360 TXBF_ACT_EBF_PROF_UPDATE,
361 TXBF_ACT_IBF_PHASE_CAL,
362 TXBF_ACT_IBF_PHASE_E2P_UPDATE = 16,
363};
364
developer071927d2022-08-31 20:39:29 +0800365enum prek_ops {
366 PREK_SYNC_ALL = 1,
367 PREK_SYNC_GROUP,
368 PREK_SYNC_DPD_2G,
369 PREK_SYNC_DPD_5G,
370 PREK_SYNC_DPD_6G,
371 PREK_CLEAN_GROUP,
372 PREK_CLEAN_DPD,
373};
374
developer3abe1ad2022-01-24 11:13:32 +0800375static inline bool is_mt7915(struct atenl *an)
376{
377 return an->chip_id == 0x7915;
378}
379
380static inline bool is_mt7916(struct atenl *an)
381{
382 return (an->chip_id == 0x7916) || (an->chip_id == 0x7906);
383}
384
385static inline bool is_mt7986(struct atenl *an)
386{
387 return an->chip_id == 0x7986;
388}
389
390int atenl_eth_init(struct atenl *an);
391int atenl_eth_recv(struct atenl *an, struct atenl_data *data);
392int atenl_eth_send(struct atenl *an, struct atenl_data *data);
developer5698c9c2022-05-30 16:40:23 +0800393int atenl_hqa_proc_cmd(struct atenl *an);
developer3abe1ad2022-01-24 11:13:32 +0800394int atenl_nl_process(struct atenl *an, struct atenl_data *data);
395int atenl_nl_process_many(struct atenl *an, struct atenl_data *data);
396int atenl_nl_check_mtd(struct atenl *an);
397int atenl_nl_write_eeprom(struct atenl *an, u32 offset, u8 *val, int len);
developer9b7cdad2022-03-10 14:24:55 +0800398int atenl_nl_write_efuse_all(struct atenl *an);
developer3abe1ad2022-01-24 11:13:32 +0800399int atenl_nl_update_buffer_mode(struct atenl *an);
400int atenl_nl_set_state(struct atenl *an, u8 band,
401 enum mt76_testmode_state state);
developer5698c9c2022-05-30 16:40:23 +0800402int atenl_nl_set_aid(struct atenl *an, u8 band, u8 aid);
developer071927d2022-08-31 20:39:29 +0800403int atenl_nl_precal_sync_from_driver(struct atenl *an, enum prek_ops ops);
developer3abe1ad2022-01-24 11:13:32 +0800404int atenl_eeprom_init(struct atenl *an, u8 phy_idx);
405void atenl_eeprom_close(struct atenl *an);
406int atenl_eeprom_write_mtd(struct atenl *an);
developer071927d2022-08-31 20:39:29 +0800407int atenl_eeprom_update_precal(struct atenl *an, int write_offs, int size);
developer3abe1ad2022-01-24 11:13:32 +0800408int atenl_eeprom_read_from_driver(struct atenl *an, u32 offset, int len);
409void atenl_eeprom_cmd_handler(struct atenl *an, u8 phy_idx, char *cmd);
410u16 atenl_get_center_channel(u8 bw, u8 ch_band, u16 ctrl_ch);
411int atenl_reg_read(struct atenl *an, u32 offset, u32 *res);
412int atenl_reg_write(struct atenl *an, u32 offset, u32 val);
developer5698c9c2022-05-30 16:40:23 +0800413int atenl_rf_read(struct atenl *an, u32 wf_sel, u32 offset, u32 *res);
414int atenl_rf_write(struct atenl *an, u32 wf_sel, u32 offset, u32 val);
developer3abe1ad2022-01-24 11:13:32 +0800415
416#endif