blob: d40c8062bdd37af6301fdcce53697a2f9d25e180 [file] [log] [blame]
developer3abe1ad2022-01-24 11:13:32 +08001#define _GNU_SOURCE
2#include <fcntl.h>
3#include <sys/mman.h>
4#include <sys/stat.h>
5#include <sys/wait.h>
6
7#include "atenl.h"
8
developer11f4a0b2023-03-31 17:43:25 +08009#define EEPROM_PART_SIZE 0xFF000
developera6267142022-01-26 20:50:22 +080010char *eeprom_file;
developer3abe1ad2022-01-24 11:13:32 +080011
developer82af5012022-11-01 11:15:32 +080012static int
13atenl_create_file(struct atenl *an, bool flash_mode)
developer3abe1ad2022-01-24 11:13:32 +080014{
developer82af5012022-11-01 11:15:32 +080015 char fname[64], buf[1024];
16 ssize_t w, len, max_len, total_len = 0;
17 int fd_ori, fd, ret;
developer3abe1ad2022-01-24 11:13:32 +080018
developer82af5012022-11-01 11:15:32 +080019 /* reserve space for pre-cal data in flash mode */
20 if (flash_mode) {
21 atenl_dbg("%s: init eeprom with flash mode\n", __func__);
22 max_len = EEPROM_PART_SIZE;
23 } else {
24 atenl_dbg("%s: init eeprom with efuse mode\n", __func__);
developerf90c9af2022-12-28 22:40:23 +080025 max_len = 0x1e00;
developer3abe1ad2022-01-24 11:13:32 +080026 }
developer3abe1ad2022-01-24 11:13:32 +080027
developer82af5012022-11-01 11:15:32 +080028 snprintf(fname, sizeof(fname),
29 "/sys/kernel/debug/ieee80211/phy%d/mt76/eeprom",
30 get_band_val(an, 0, phy_idx));
31 fd_ori = open(fname, O_RDONLY);
32 if (fd_ori < 0)
developer3abe1ad2022-01-24 11:13:32 +080033 return -1;
developer3abe1ad2022-01-24 11:13:32 +080034
developera6267142022-01-26 20:50:22 +080035 fd = open(eeprom_file, O_RDWR | O_CREAT | O_EXCL, 00644);
developer3abe1ad2022-01-24 11:13:32 +080036 if (fd < 0)
37 goto out;
38
developer82af5012022-11-01 11:15:32 +080039 while ((len = read(fd_ori, buf, sizeof(buf))) > 0) {
developer3abe1ad2022-01-24 11:13:32 +080040retry:
41 w = write(fd, buf, len);
developer5698c9c2022-05-30 16:40:23 +080042 if (w > 0) {
developer82af5012022-11-01 11:15:32 +080043 total_len += len;
developer3abe1ad2022-01-24 11:13:32 +080044 continue;
developer5698c9c2022-05-30 16:40:23 +080045 }
developer3abe1ad2022-01-24 11:13:32 +080046
47 if (errno == EINTR)
48 goto retry;
49
50 perror("write");
developera6267142022-01-26 20:50:22 +080051 unlink(eeprom_file);
developer3abe1ad2022-01-24 11:13:32 +080052 close(fd);
53 fd = -1;
54 goto out;
55 }
56
developer82af5012022-11-01 11:15:32 +080057 /* reserve space for pre-cal data in flash mode */
58 len = sizeof(buf);
59 memset(buf, 0, len);
60 while (total_len < max_len) {
developer3abe1ad2022-01-24 11:13:32 +080061 w = write(fd, buf, len);
developer3abe1ad2022-01-24 11:13:32 +080062
developer82af5012022-11-01 11:15:32 +080063 if (w > 0) {
64 total_len += len;
65 continue;
66 }
developer3abe1ad2022-01-24 11:13:32 +080067
developer82af5012022-11-01 11:15:32 +080068 if (errno != EINTR) {
69 perror("write");
70 unlink(eeprom_file);
71 close(fd);
72 fd = -1;
73 goto out;
74 }
developer3abe1ad2022-01-24 11:13:32 +080075 }
76
developer82af5012022-11-01 11:15:32 +080077
developer3abe1ad2022-01-24 11:13:32 +080078 ret = lseek(fd, 0, SEEK_SET);
79 if (ret) {
80 close(fd_ori);
81 close(fd);
82 return ret;
83 }
84
85out:
86 close(fd_ori);
87 return fd;
88}
89
90static bool
91atenl_eeprom_file_exists(void)
92{
93 struct stat st;
94
developera6267142022-01-26 20:50:22 +080095 return stat(eeprom_file, &st) == 0;
developer3abe1ad2022-01-24 11:13:32 +080096}
97
98static int
99atenl_eeprom_init_file(struct atenl *an, bool flash_mode)
100{
101 int fd;
102
developer82af5012022-11-01 11:15:32 +0800103 if (!atenl_eeprom_file_exists())
104 return atenl_create_file(an, flash_mode);
developer3abe1ad2022-01-24 11:13:32 +0800105
developera6267142022-01-26 20:50:22 +0800106 fd = open(eeprom_file, O_RDWR);
developer3abe1ad2022-01-24 11:13:32 +0800107 if (fd < 0)
108 perror("open");
109
developer3abe1ad2022-01-24 11:13:32 +0800110 return fd;
111}
112
113static void
developer5698c9c2022-05-30 16:40:23 +0800114atenl_eeprom_init_chip_id(struct atenl *an)
115{
116 an->chip_id = *(u16 *)an->eeprom_data;
117
118 if (is_mt7915(an)) {
119 an->adie_id = 0x7975;
120 } else if (is_mt7916(an)) {
121 an->adie_id = 0x7976;
122 } else if (is_mt7986(an)) {
123 bool is_7975 = false;
124 u32 val;
125 u8 sub_id;
126
127 atenl_reg_read(an, 0x18050000, &val);
128
129 switch (val & 0xf) {
130 case MT7975_ONE_ADIE_SINGLE_BAND:
131 is_7975 = true;
132 /* fallthrough */
133 case MT7976_ONE_ADIE_SINGLE_BAND:
134 sub_id = 0xa;
135 break;
136 case MT7976_ONE_ADIE_DBDC:
137 sub_id = 0x7;
138 break;
139 case MT7975_DUAL_ADIE_DBDC:
140 is_7975 = true;
141 /* fallthrough */
142 case MT7976_DUAL_ADIE_DBDC:
143 default:
144 sub_id = 0xf;
145 break;
146 }
147
148 an->sub_chip_id = sub_id;
149 an->adie_id = is_7975 ? 0x7975 : 0x7976;
developerf90c9af2022-12-28 22:40:23 +0800150 } else if (is_mt7996(an)) {
151 /* TODO: parse info if required */
developer5698c9c2022-05-30 16:40:23 +0800152 }
153}
154
155static void
developer3abe1ad2022-01-24 11:13:32 +0800156atenl_eeprom_init_max_size(struct atenl *an)
157{
158 switch (an->chip_id) {
159 case 0x7915:
160 an->eeprom_size = 3584;
developer071927d2022-08-31 20:39:29 +0800161 an->eeprom_prek_offs = 0x62;
developer3abe1ad2022-01-24 11:13:32 +0800162 break;
163 case 0x7906:
164 case 0x7916:
165 case 0x7986:
166 an->eeprom_size = 4096;
developer071927d2022-08-31 20:39:29 +0800167 an->eeprom_prek_offs = 0x19a;
developer3abe1ad2022-01-24 11:13:32 +0800168 break;
developerf90c9af2022-12-28 22:40:23 +0800169 case 0x7990:
170 an->eeprom_size = 7680;
171 an->eeprom_prek_offs = 0x1a5;
developer3abe1ad2022-01-24 11:13:32 +0800172 default:
173 break;
174 }
175}
176
177static void
178atenl_eeprom_init_band_cap(struct atenl *an)
179{
developerf90c9af2022-12-28 22:40:23 +0800180#define EAGLE_BAND_SEL(index) MT_EE_WIFI_EAGLE_CONF##index##_BAND_SEL
developer3abe1ad2022-01-24 11:13:32 +0800181 u8 *eeprom = an->eeprom_data;
182
183 if (is_mt7915(an)) {
184 u8 val = eeprom[MT_EE_WIFI_CONF];
185 u8 band_sel = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val);
186 struct atenl_band *anb = &an->anb[0];
187
188 /* MT7915A */
189 if (band_sel == MT_EE_BAND_SEL_DEFAULT) {
190 anb->valid = true;
191 anb->cap = BAND_TYPE_2G_5G;
192 return;
193 }
194
195 /* MT7915D */
196 if (band_sel == MT_EE_BAND_SEL_2GHZ) {
197 anb->valid = true;
198 anb->cap = BAND_TYPE_2G;
199 }
200
201 val = eeprom[MT_EE_WIFI_CONF + 1];
202 band_sel = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val);
203 anb++;
204
205 if (band_sel == MT_EE_BAND_SEL_5GHZ) {
206 anb->valid = true;
207 anb->cap = BAND_TYPE_5G;
208 }
209 } else if (is_mt7916(an) || is_mt7986(an)) {
210 struct atenl_band *anb;
211 u8 val, band_sel;
212 int i;
213
214 for (i = 0; i < 2; i++) {
215 val = eeprom[MT_EE_WIFI_CONF + i];
216 band_sel = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val);
217 anb = &an->anb[i];
218
219 anb->valid = true;
220 switch (band_sel) {
221 case MT_EE_BAND_SEL_2G:
222 anb->cap = BAND_TYPE_2G;
223 break;
224 case MT_EE_BAND_SEL_5G:
225 anb->cap = BAND_TYPE_5G;
226 break;
227 case MT_EE_BAND_SEL_6G:
228 anb->cap = BAND_TYPE_6G;
229 break;
230 case MT_EE_BAND_SEL_5G_6G:
231 anb->cap = BAND_TYPE_5G_6G;
232 break;
233 default:
234 break;
235 }
236 }
developerf90c9af2022-12-28 22:40:23 +0800237 } else if (is_mt7996(an)) {
238 struct atenl_band *anb;
239 u8 val, band_sel;
240 u8 band_sel_mask[3] = {EAGLE_BAND_SEL(0), EAGLE_BAND_SEL(1),
241 EAGLE_BAND_SEL(2)};
242 int i;
243
244 for (i = 0; i < 3; i++) {
245 val = eeprom[MT_EE_WIFI_CONF + i];
246 band_sel = FIELD_GET(band_sel_mask[i], val);
247 anb = &an->anb[i];
248
249 anb->valid = true;
250 switch (band_sel) {
251 case MT_EE_EAGLE_BAND_SEL_2GHZ:
252 anb->cap = BAND_TYPE_2G;
253 break;
254 case MT_EE_EAGLE_BAND_SEL_5GHZ:
255 anb->cap = BAND_TYPE_5G;
256 break;
257 case MT_EE_EAGLE_BAND_SEL_6GHZ:
258 anb->cap = BAND_TYPE_6G;
259 break;
260 case MT_EE_EAGLE_BAND_SEL_5GHZ_6GHZ:
261 anb->cap = BAND_TYPE_5G_6G;
262 break;
263 default:
264 break;
265 }
266 }
developer3abe1ad2022-01-24 11:13:32 +0800267 }
268}
269
270static void
271atenl_eeprom_init_antenna_cap(struct atenl *an)
272{
273 if (is_mt7915(an)) {
274 if (an->anb[0].cap == BAND_TYPE_2G_5G)
275 an->anb[0].chainmask = 0xf;
276 else {
277 an->anb[0].chainmask = 0x3;
278 an->anb[1].chainmask = 0xc;
279 }
280 } else if (is_mt7916(an)) {
281 an->anb[0].chainmask = 0x3;
282 an->anb[1].chainmask = 0x3;
283 } else if (is_mt7986(an)) {
284 an->anb[0].chainmask = 0xf;
285 an->anb[1].chainmask = 0xf;
developerf90c9af2022-12-28 22:40:23 +0800286 } else if (is_mt7996(an)) {
287 an->anb[0].chainmask = 0xf;
288 an->anb[1].chainmask = 0xf;
289 an->anb[2].chainmask = 0xf;
developer3abe1ad2022-01-24 11:13:32 +0800290 }
291}
292
293int atenl_eeprom_init(struct atenl *an, u8 phy_idx)
294{
295 bool flash_mode;
296 int eeprom_fd;
developera6267142022-01-26 20:50:22 +0800297 char buf[30];
developer071927d2022-08-31 20:39:29 +0800298 u8 main_phy_idx = phy_idx;
developer3abe1ad2022-01-24 11:13:32 +0800299
300 set_band_val(an, 0, phy_idx, phy_idx);
developer3abe1ad2022-01-24 11:13:32 +0800301 atenl_nl_check_mtd(an);
302 flash_mode = an->mtd_part != NULL;
303
developerf90c9af2022-12-28 22:40:23 +0800304 // Get the first main phy index for this chip
developer071927d2022-08-31 20:39:29 +0800305 if (flash_mode)
developerf90c9af2022-12-28 22:40:23 +0800306 main_phy_idx -= an->band_idx;
developer071927d2022-08-31 20:39:29 +0800307
308 snprintf(buf, sizeof(buf), "/tmp/atenl-eeprom-phy%u", main_phy_idx);
309 eeprom_file = strdup(buf);
310
developer3abe1ad2022-01-24 11:13:32 +0800311 eeprom_fd = atenl_eeprom_init_file(an, flash_mode);
312 if (eeprom_fd < 0)
313 return -1;
314
315 an->eeprom_data = mmap(NULL, EEPROM_PART_SIZE, PROT_READ | PROT_WRITE,
developer071927d2022-08-31 20:39:29 +0800316 MAP_SHARED, eeprom_fd, 0);
developer3abe1ad2022-01-24 11:13:32 +0800317 if (!an->eeprom_data) {
318 perror("mmap");
319 close(eeprom_fd);
320 return -1;
321 }
322
323 an->eeprom_fd = eeprom_fd;
developer5698c9c2022-05-30 16:40:23 +0800324 atenl_eeprom_init_chip_id(an);
developer3abe1ad2022-01-24 11:13:32 +0800325 atenl_eeprom_init_max_size(an);
326 atenl_eeprom_init_band_cap(an);
327 atenl_eeprom_init_antenna_cap(an);
328
329 if (get_band_val(an, 1, valid))
330 set_band_val(an, 1, phy_idx, phy_idx + 1);
331
developer77215642023-05-15 13:52:35 +0800332 if (get_band_val(an, 2, valid))
333 set_band_val(an, 2, phy_idx, phy_idx + 2);
334
developer3abe1ad2022-01-24 11:13:32 +0800335 return 0;
336}
337
338void atenl_eeprom_close(struct atenl *an)
339{
340 msync(an->eeprom_data, EEPROM_PART_SIZE, MS_SYNC);
341 munmap(an->eeprom_data, EEPROM_PART_SIZE);
342 close(an->eeprom_fd);
343
developer5698c9c2022-05-30 16:40:23 +0800344 if (!an->cmd_mode) {
developera6267142022-01-26 20:50:22 +0800345 if (remove(eeprom_file))
developer3abe1ad2022-01-24 11:13:32 +0800346 perror("remove");
developera6267142022-01-26 20:50:22 +0800347 }
348
349 free(eeprom_file);
developer3abe1ad2022-01-24 11:13:32 +0800350}
351
developer071927d2022-08-31 20:39:29 +0800352int atenl_eeprom_update_precal(struct atenl *an, int write_offs, int size)
353{
354 u32 offs = an->eeprom_prek_offs;
355 u8 cal_indicator, *eeprom, *pre_cal;
356
357 if (!an->cal && !an->cal_info)
358 return 0;
359
360 eeprom = an->eeprom_data;
361 pre_cal = eeprom + an->eeprom_size;
362 cal_indicator = an->cal_info[4];
363
364 memcpy(eeprom + offs, &cal_indicator, sizeof(u8));
365 memcpy(pre_cal, an->cal_info, PRE_CAL_INFO);
366 pre_cal += (PRE_CAL_INFO + write_offs);
367
368 if (an->cal)
369 memcpy(pre_cal, an->cal, size);
370 else
371 memset(pre_cal, 0, size);
372
373 return 0;
374}
375
developer3abe1ad2022-01-24 11:13:32 +0800376int atenl_eeprom_write_mtd(struct atenl *an)
377{
378 bool flash_mode = an->mtd_part != NULL;
379 pid_t pid;
developer071927d2022-08-31 20:39:29 +0800380 char offset[10];
developer3abe1ad2022-01-24 11:13:32 +0800381
382 if (!flash_mode)
383 return 0;
384
385 pid = fork();
386 if (pid < 0) {
387 perror("Fork");
388 return EXIT_FAILURE;
389 } else if (pid == 0) {
developer3abe1ad2022-01-24 11:13:32 +0800390 int ret;
developer071927d2022-08-31 20:39:29 +0800391 char *part = strdup(an->mtd_part);
392 snprintf(offset, sizeof(offset), "%d", an->mtd_offset);
393 char *cmd[] = {"mtd", "-p", offset, "write", eeprom_file, part, NULL};
developer3abe1ad2022-01-24 11:13:32 +0800394
395 ret = execvp("mtd", cmd);
396 if (ret < 0) {
developer5698c9c2022-05-30 16:40:23 +0800397 atenl_err("%s: exec error\n", __func__);
developer3abe1ad2022-01-24 11:13:32 +0800398 exit(0);
399 }
400 } else {
401 wait(&pid);
402 }
403
404 return 0;
405}
406
developer5698c9c2022-05-30 16:40:23 +0800407/* Directly read values from driver's eeprom.
developer3abe1ad2022-01-24 11:13:32 +0800408 * It's usally used to get calibrated data from driver.
409 */
410int atenl_eeprom_read_from_driver(struct atenl *an, u32 offset, int len)
411{
412 u8 *eeprom_data = an->eeprom_data + offset;
413 char fname[64], buf[1024];
414 int fd_ori, ret;
415 ssize_t rd;
416
417 snprintf(fname, sizeof(fname),
418 "/sys/kernel/debug/ieee80211/phy%d/mt76/eeprom",
419 get_band_val(an, 0, phy_idx));
420 fd_ori = open(fname, O_RDONLY);
421 if (fd_ori < 0)
422 return -1;
423
424 ret = lseek(fd_ori, offset, SEEK_SET);
425 if (ret < 0)
426 goto out;
427
428 while ((rd = read(fd_ori, buf, sizeof(buf))) > 0 && len) {
429 if (len < rd) {
430 memcpy(eeprom_data, buf, len);
431 break;
432 } else {
433 memcpy(eeprom_data, buf, rd);
434 eeprom_data += rd;
435 len -= rd;
436 }
437 }
438
439 ret = 0;
440out:
441 close(fd_ori);
442 return ret;
443}
444
445/* Update all eeprom values to driver before writing efuse */
446static void
447atenl_eeprom_sync_to_driver(struct atenl *an)
448{
449 int i;
450
developera6267142022-01-26 20:50:22 +0800451 for (i = 0; i < an->eeprom_size; i += 16)
developer3abe1ad2022-01-24 11:13:32 +0800452 atenl_nl_write_eeprom(an, i, &an->eeprom_data[i], 16);
453}
454
455void atenl_eeprom_cmd_handler(struct atenl *an, u8 phy_idx, char *cmd)
456{
457 bool flash_mode;
458
459 an->cmd_mode = true;
460
461 atenl_eeprom_init(an, phy_idx);
462 flash_mode = an->mtd_part != NULL;
463
464 if (!strncmp(cmd, "sync eeprom all", 15)) {
465 atenl_eeprom_write_mtd(an);
466 } else if (!strncmp(cmd, "eeprom", 6)) {
467 char *s = strchr(cmd, ' ');
468
469 if (!s) {
developer5698c9c2022-05-30 16:40:23 +0800470 atenl_err("eeprom: please type a correct command\n");
developer3abe1ad2022-01-24 11:13:32 +0800471 return;
472 }
473
474 s++;
475 if (!strncmp(s, "reset", 5)) {
developera6267142022-01-26 20:50:22 +0800476 unlink(eeprom_file);
developer3abe1ad2022-01-24 11:13:32 +0800477 } else if (!strncmp(s, "file", 4)) {
developera6267142022-01-26 20:50:22 +0800478 atenl_info("%s\n", eeprom_file);
developer3abe1ad2022-01-24 11:13:32 +0800479 atenl_info("Flash mode: %d\n", flash_mode);
480 } else if (!strncmp(s, "set", 3)) {
481 u32 offset, val;
482
483 s = strchr(s, ' ');
484 if (!s)
485 return;
486 s++;
487
488 if (!sscanf(s, "%x=%x", &offset, &val) ||
489 offset > EEPROM_PART_SIZE)
490 return;
491
492 an->eeprom_data[offset] = val;
493 atenl_info("set offset 0x%x to 0x%x\n", offset, val);
developer9b7cdad2022-03-10 14:24:55 +0800494 } else if (!strncmp(s, "update buffermode", 17)) {
developera6267142022-01-26 20:50:22 +0800495 atenl_eeprom_sync_to_driver(an);
developer3abe1ad2022-01-24 11:13:32 +0800496 atenl_nl_update_buffer_mode(an);
497 } else if (!strncmp(s, "write", 5)) {
498 s = strchr(s, ' ');
499 if (!s)
500 return;
501 s++;
502
developer9b7cdad2022-03-10 14:24:55 +0800503 if (!strncmp(s, "flash", 5)) {
developer3abe1ad2022-01-24 11:13:32 +0800504 atenl_eeprom_write_mtd(an);
developer9b7cdad2022-03-10 14:24:55 +0800505 } else if (!strncmp(s, "to efuse", 8)) {
506 atenl_eeprom_sync_to_driver(an);
507 atenl_nl_write_efuse_all(an);
508 }
developer3abe1ad2022-01-24 11:13:32 +0800509 } else if (!strncmp(s, "read", 4)) {
510 u32 offset;
511
512 s = strchr(s, ' ');
513 if (!s)
514 return;
515 s++;
516
517 if (!sscanf(s, "%x", &offset) ||
518 offset > EEPROM_PART_SIZE)
519 return;
520
521 atenl_info("val = 0x%x (%u)\n", an->eeprom_data[offset],
522 an->eeprom_data[offset]);
developer071927d2022-08-31 20:39:29 +0800523 } else if (!strncmp(s, "precal", 6)) {
524 s = strchr(s, ' ');
525 if (!s)
526 return;
527 s++;
528
529 if (!strncmp(s, "sync group", 10)) {
530 atenl_nl_precal_sync_from_driver(an, PREK_SYNC_GROUP);
531 } else if (!strncmp(s, "sync dpd 2g", 11)) {
532 atenl_nl_precal_sync_from_driver(an, PREK_SYNC_DPD_2G);
533 } else if (!strncmp(s, "sync dpd 5g", 11)) {
534 atenl_nl_precal_sync_from_driver(an, PREK_SYNC_DPD_5G);
535 } else if (!strncmp(s, "sync dpd 6g", 11)) {
536 atenl_nl_precal_sync_from_driver(an, PREK_SYNC_DPD_6G);
537 } else if (!strncmp(s, "group clean", 11)) {
538 atenl_nl_precal_sync_from_driver(an, PREK_CLEAN_GROUP);
539 } else if (!strncmp(s, "dpd clean", 9)) {
540 atenl_nl_precal_sync_from_driver(an, PREK_CLEAN_DPD);
541 } else if (!strncmp(s, "sync", 4)) {
542 atenl_nl_precal_sync_from_driver(an, PREK_SYNC_ALL);
543 }
developer44ae8e92023-07-21 13:42:14 +0800544 } else if (!strncmp(s, "ibf sync", 8)) {
545 atenl_get_ibf_cal_result(an);
developer9b7cdad2022-03-10 14:24:55 +0800546 } else {
developer44ae8e92023-07-21 13:42:14 +0800547 atenl_err("Unknown eeprom command: %s\n", cmd);
548 }
developer3abe1ad2022-01-24 11:13:32 +0800549 } else {
550 atenl_err("Unknown command: %s\n", cmd);
551 }
552}