blob: 2a46971ff1af8dced63572c9f0747a3583337018 [file] [log] [blame]
developer22c7ab62022-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
9#define EEPROM_PART_SIZE 20480
10#define EEPROM_FILE "/tmp/atenl-eeprom"
11
12static FILE *mtd_open(const char *mtd)
13{
14 char line[128], name[64];
15 FILE *fp;
16 int i;
17
18 fp = fopen("/proc/mtd", "r");
19 if (!fp)
20 return NULL;
21
22 snprintf(name, sizeof(name), "\"%s\"", mtd);
23 while (fgets(line, sizeof(line), fp)) {
24 if (!sscanf(line, "mtd%d:", &i) || !strstr(line, name))
25 continue;
26
27 snprintf(line, sizeof(line), "/dev/mtd%d", i);
28 fclose(fp);
29 return fopen(line, "r");
30 }
31 fclose(fp);
32
33 return NULL;
34}
35
36static int
37atenl_flash_create_file(struct atenl *an)
38{
39 char buf[1024];
40 ssize_t len;
41 FILE *f;
42 int fd, ret;
43
44 f = mtd_open(an->mtd_part);
45 if (!f) {
46 fprintf(stderr, "Failed to open MTD device\n");
47 return -1;
48 }
49
50 fd = open(EEPROM_FILE, O_RDWR | O_CREAT | O_EXCL, 00644);
51 if (fd < 0)
52 goto out;
53
54 while ((len = fread(buf, 1, sizeof(buf), f)) > 0) {
55 ssize_t w;
56
57retry:
58 w = write(fd, buf, len);
59 if (w > 0)
60 continue;
61
62 if (errno == EINTR)
63 goto retry;
64
65 perror("write");
66 unlink(EEPROM_FILE);
67 close(fd);
68 fd = -1;
69 goto out;
70 }
71
72 ret = lseek(fd, 0, SEEK_SET);
73 if (ret) {
74 fclose(f);
75 close(fd);
76 return ret;
77 }
78
79out:
80 fclose(f);
81 return fd;
82}
83
84static int
85atenl_efuse_create_file(struct atenl *an)
86{
87 char fname[64], buf[1024];
88 ssize_t len;
89 int fd_ori, fd, ret;
90
91 snprintf(fname, sizeof(fname),
92 "/sys/kernel/debug/ieee80211/phy%d/mt76/eeprom", get_band_val(an, 0, phy_idx));
93 fd_ori = open(fname, O_RDONLY);
94 if (fd_ori < 0)
95 return -1;
96
97 fd = open(EEPROM_FILE, O_RDWR | O_CREAT | O_EXCL, 00644);
98 if (fd < 0)
99 goto out;
100
101 while ((len = read(fd_ori, buf, sizeof(buf))) > 0) {
102 ssize_t w;
103
104retry:
105 w = write(fd, buf, len);
106 if (w > 0)
107 continue;
108
109 if (errno == EINTR)
110 goto retry;
111
112 perror("write");
113 unlink(EEPROM_FILE);
114 close(fd);
115 fd = -1;
116 goto out;
117 }
118
119 ret = lseek(fd, 0, SEEK_SET);
120 if (ret) {
121 close(fd_ori);
122 close(fd);
123 return ret;
124 }
125
126out:
127 close(fd_ori);
128 return fd;
129}
130
131static bool
132atenl_eeprom_file_exists(void)
133{
134 struct stat st;
135
136 return stat(EEPROM_FILE, &st) == 0;
137}
138
139static int
140atenl_eeprom_init_file(struct atenl *an, bool flash_mode)
141{
142 int fd;
143
144 if (!atenl_eeprom_file_exists()) {
145 if (flash_mode)
146 atenl_dbg("[%d]%s: init eeprom with flash mode\n", getpid(), __func__);
147 else
148 atenl_dbg("[%d]%s: init eeprom with efuse mode\n", getpid(), __func__);
149
150 if (flash_mode)
151 return atenl_flash_create_file(an);
152
153 return atenl_efuse_create_file(an);
154 }
155
156 fd = open(EEPROM_FILE, O_RDWR);
157 if (fd < 0)
158 perror("open");
159
160 an->eeprom_exist = true;
161
162 return fd;
163}
164
165static void
166atenl_eeprom_init_max_size(struct atenl *an)
167{
168 switch (an->chip_id) {
169 case 0x7915:
170 an->eeprom_size = 3584;
171 break;
172 case 0x7906:
173 case 0x7916:
174 case 0x7986:
175 an->eeprom_size = 4096;
176 break;
177 default:
178 break;
179 }
180}
181
182static void
183atenl_eeprom_init_band_cap(struct atenl *an)
184{
185 u8 *eeprom = an->eeprom_data;
186
187 if (is_mt7915(an)) {
188 u8 val = eeprom[MT_EE_WIFI_CONF];
189 u8 band_sel = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val);
190 struct atenl_band *anb = &an->anb[0];
191
192 /* MT7915A */
193 if (band_sel == MT_EE_BAND_SEL_DEFAULT) {
194 anb->valid = true;
195 anb->cap = BAND_TYPE_2G_5G;
196 return;
197 }
198
199 /* MT7915D */
200 if (band_sel == MT_EE_BAND_SEL_2GHZ) {
201 anb->valid = true;
202 anb->cap = BAND_TYPE_2G;
203 }
204
205 val = eeprom[MT_EE_WIFI_CONF + 1];
206 band_sel = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val);
207 anb++;
208
209 if (band_sel == MT_EE_BAND_SEL_5GHZ) {
210 anb->valid = true;
211 anb->cap = BAND_TYPE_5G;
212 }
213 } else if (is_mt7916(an) || is_mt7986(an)) {
214 struct atenl_band *anb;
215 u8 val, band_sel;
216 int i;
217
218 for (i = 0; i < 2; i++) {
219 val = eeprom[MT_EE_WIFI_CONF + i];
220 band_sel = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val);
221 anb = &an->anb[i];
222
223 anb->valid = true;
224 switch (band_sel) {
225 case MT_EE_BAND_SEL_2G:
226 anb->cap = BAND_TYPE_2G;
227 break;
228 case MT_EE_BAND_SEL_5G:
229 anb->cap = BAND_TYPE_5G;
230 break;
231 case MT_EE_BAND_SEL_6G:
232 anb->cap = BAND_TYPE_6G;
233 break;
234 case MT_EE_BAND_SEL_5G_6G:
235 anb->cap = BAND_TYPE_5G_6G;
236 break;
237 default:
238 break;
239 }
240 }
241 }
242}
243
244static void
245atenl_eeprom_init_antenna_cap(struct atenl *an)
246{
247 if (is_mt7915(an)) {
248 if (an->anb[0].cap == BAND_TYPE_2G_5G)
249 an->anb[0].chainmask = 0xf;
250 else {
251 an->anb[0].chainmask = 0x3;
252 an->anb[1].chainmask = 0xc;
253 }
254 } else if (is_mt7916(an)) {
255 an->anb[0].chainmask = 0x3;
256 an->anb[1].chainmask = 0x3;
257 } else if (is_mt7986(an)) {
258 an->anb[0].chainmask = 0xf;
259 an->anb[1].chainmask = 0xf;
260 }
261}
262
263int atenl_eeprom_init(struct atenl *an, u8 phy_idx)
264{
265 bool flash_mode;
266 int eeprom_fd;
267
268 set_band_val(an, 0, phy_idx, phy_idx);
269
270 atenl_nl_check_mtd(an);
271 flash_mode = an->mtd_part != NULL;
272
273 eeprom_fd = atenl_eeprom_init_file(an, flash_mode);
274 if (eeprom_fd < 0)
275 return -1;
276
277 an->eeprom_data = mmap(NULL, EEPROM_PART_SIZE, PROT_READ | PROT_WRITE,
278 MAP_SHARED, eeprom_fd, an->mtd_offset);
279 if (!an->eeprom_data) {
280 perror("mmap");
281 close(eeprom_fd);
282 return -1;
283 }
284
285 an->eeprom_fd = eeprom_fd;
286 an->chip_id = *(u16 *)an->eeprom_data;
287 atenl_eeprom_init_max_size(an);
288 atenl_eeprom_init_band_cap(an);
289 atenl_eeprom_init_antenna_cap(an);
290
291 if (get_band_val(an, 1, valid))
292 set_band_val(an, 1, phy_idx, phy_idx + 1);
293
294 return 0;
295}
296
297void atenl_eeprom_close(struct atenl *an)
298{
299 msync(an->eeprom_data, EEPROM_PART_SIZE, MS_SYNC);
300 munmap(an->eeprom_data, EEPROM_PART_SIZE);
301 close(an->eeprom_fd);
302
303 if (!an->eeprom_exist && (an->child_pid || an->cmd_mode))
304 if (remove(EEPROM_FILE))
305 perror("remove");
306}
307
308int atenl_eeprom_write_mtd(struct atenl *an)
309{
310 bool flash_mode = an->mtd_part != NULL;
311 pid_t pid;
312
313 if (!flash_mode)
314 return 0;
315
316 pid = fork();
317 if (pid < 0) {
318 perror("Fork");
319 return EXIT_FAILURE;
320 } else if (pid == 0) {
321 char *part = strdup(an->mtd_part);
322 char *cmd[] = {"mtd", "write", EEPROM_FILE, part, NULL};
323 int ret;
324
325 ret = execvp("mtd", cmd);
326 if (ret < 0) {
327 fprintf(stderr, "%s: execl error\n", __func__);
328 exit(0);
329 }
330 } else {
331 wait(&pid);
332 }
333
334 return 0;
335}
336
337/* Directly read some value from driver's eeprom.
338 * It's usally used to get calibrated data from driver.
339 */
340int atenl_eeprom_read_from_driver(struct atenl *an, u32 offset, int len)
341{
342 u8 *eeprom_data = an->eeprom_data + offset;
343 char fname[64], buf[1024];
344 int fd_ori, ret;
345 ssize_t rd;
346
347 snprintf(fname, sizeof(fname),
348 "/sys/kernel/debug/ieee80211/phy%d/mt76/eeprom",
349 get_band_val(an, 0, phy_idx));
350 fd_ori = open(fname, O_RDONLY);
351 if (fd_ori < 0)
352 return -1;
353
354 ret = lseek(fd_ori, offset, SEEK_SET);
355 if (ret < 0)
356 goto out;
357
358 while ((rd = read(fd_ori, buf, sizeof(buf))) > 0 && len) {
359 if (len < rd) {
360 memcpy(eeprom_data, buf, len);
361 break;
362 } else {
363 memcpy(eeprom_data, buf, rd);
364 eeprom_data += rd;
365 len -= rd;
366 }
367 }
368
369 ret = 0;
370out:
371 close(fd_ori);
372 return ret;
373}
374
375/* Update all eeprom values to driver before writing efuse */
376static void
377atenl_eeprom_sync_to_driver(struct atenl *an)
378{
379 int i;
380
381 for (i = 0; i < 3584; i += 16)
382 atenl_nl_write_eeprom(an, i, &an->eeprom_data[i], 16);
383}
384
385void atenl_eeprom_cmd_handler(struct atenl *an, u8 phy_idx, char *cmd)
386{
387 bool flash_mode;
388
389 an->cmd_mode = true;
390
391 atenl_eeprom_init(an, phy_idx);
392 flash_mode = an->mtd_part != NULL;
393
394 if (!strncmp(cmd, "sync eeprom all", 15)) {
395 atenl_eeprom_write_mtd(an);
396 } else if (!strncmp(cmd, "eeprom", 6)) {
397 char *s = strchr(cmd, ' ');
398
399 if (!s) {
400 fprintf(stderr, "eeprom: please type a correct command\n");
401 return;
402 }
403
404 s++;
405 if (!strncmp(s, "reset", 5)) {
406 unlink(EEPROM_FILE);
407 } else if (!strncmp(s, "file", 4)) {
408 atenl_info("%s\n", EEPROM_FILE);
409 atenl_info("Flash mode: %d\n", flash_mode);
410 } else if (!strncmp(s, "set", 3)) {
411 u32 offset, val;
412
413 s = strchr(s, ' ');
414 if (!s)
415 return;
416 s++;
417
418 if (!sscanf(s, "%x=%x", &offset, &val) ||
419 offset > EEPROM_PART_SIZE)
420 return;
421
422 an->eeprom_data[offset] = val;
423 atenl_info("set offset 0x%x to 0x%x\n", offset, val);
424 } else if (!strncmp(s, "update", 6)) {
425 atenl_nl_update_buffer_mode(an);
426 } else if (!strncmp(s, "write", 5)) {
427 s = strchr(s, ' ');
428 if (!s)
429 return;
430 s++;
431
432 if (!strncmp(s, "flash", 5)) {
433 atenl_eeprom_write_mtd(an);
434 } else if (!strncmp(s, "efuse", 5)) {
435 atenl_eeprom_sync_to_driver(an);
436 atenl_nl_write_efuse_all(an, NULL);
437 }
438 } else if (!strncmp(s, "read", 4)) {
439 u32 offset;
440
441 s = strchr(s, ' ');
442 if (!s)
443 return;
444 s++;
445
446 if (!sscanf(s, "%x", &offset) ||
447 offset > EEPROM_PART_SIZE)
448 return;
449
450 atenl_info("val = 0x%x (%u)\n", an->eeprom_data[offset],
451 an->eeprom_data[offset]);
452 }
453 } else {
454 atenl_err("Unknown command: %s\n", cmd);
455 }
456}