developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 1 | #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 | |
developer | 8e1f514 | 2023-03-31 17:43:25 +0800 | [diff] [blame] | 9 | #define EEPROM_PART_SIZE 0xFF000 |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 10 | char *eeprom_file; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 11 | |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 12 | static int |
| 13 | atenl_create_file(struct atenl *an, bool flash_mode) |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 14 | { |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 15 | char fname[64], buf[1024]; |
| 16 | ssize_t w, len, max_len, total_len = 0; |
| 17 | int fd_ori, fd, ret; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 18 | |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 19 | /* reserve space for pre-cal data in flash mode */ |
| 20 | if (flash_mode) { |
developer | 1042c08 | 2023-10-27 19:57:13 +0800 | [diff] [blame] | 21 | atenl_dbg("%s: init eeprom with flash / binfile mode\n", __func__); |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 22 | max_len = EEPROM_PART_SIZE; |
| 23 | } else { |
developer | 1042c08 | 2023-10-27 19:57:13 +0800 | [diff] [blame] | 24 | atenl_dbg("%s: init eeprom with efuse / default bin mode\n", __func__); |
developer | bb73fa2 | 2022-12-28 22:40:23 +0800 | [diff] [blame] | 25 | max_len = 0x1e00; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 26 | } |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 27 | |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 28 | snprintf(fname, sizeof(fname), |
| 29 | "/sys/kernel/debug/ieee80211/phy%d/mt76/eeprom", |
developer | 8604599 | 2024-06-03 16:24:42 +0800 | [diff] [blame] | 30 | an->main_phy_idx); |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 31 | fd_ori = open(fname, O_RDONLY); |
| 32 | if (fd_ori < 0) |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 33 | return -1; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 34 | |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 35 | fd = open(eeprom_file, O_RDWR | O_CREAT | O_EXCL, 00644); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 36 | if (fd < 0) |
| 37 | goto out; |
| 38 | |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 39 | while ((len = read(fd_ori, buf, sizeof(buf))) > 0) { |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 40 | retry: |
| 41 | w = write(fd, buf, len); |
developer | f30d447 | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 42 | if (w > 0) { |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 43 | total_len += len; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 44 | continue; |
developer | f30d447 | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 45 | } |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 46 | |
| 47 | if (errno == EINTR) |
| 48 | goto retry; |
| 49 | |
| 50 | perror("write"); |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 51 | unlink(eeprom_file); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 52 | close(fd); |
| 53 | fd = -1; |
| 54 | goto out; |
| 55 | } |
| 56 | |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 57 | /* reserve space for pre-cal data in flash mode */ |
| 58 | len = sizeof(buf); |
| 59 | memset(buf, 0, len); |
| 60 | while (total_len < max_len) { |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 61 | w = write(fd, buf, len); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 62 | |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 63 | if (w > 0) { |
| 64 | total_len += len; |
| 65 | continue; |
| 66 | } |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 67 | |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 68 | if (errno != EINTR) { |
| 69 | perror("write"); |
| 70 | unlink(eeprom_file); |
| 71 | close(fd); |
| 72 | fd = -1; |
| 73 | goto out; |
| 74 | } |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 75 | } |
| 76 | |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 77 | |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 78 | ret = lseek(fd, 0, SEEK_SET); |
| 79 | if (ret) { |
| 80 | close(fd_ori); |
| 81 | close(fd); |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | out: |
| 86 | close(fd_ori); |
| 87 | return fd; |
| 88 | } |
| 89 | |
| 90 | static bool |
| 91 | atenl_eeprom_file_exists(void) |
| 92 | { |
| 93 | struct stat st; |
| 94 | |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 95 | return stat(eeprom_file, &st) == 0; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int |
| 99 | atenl_eeprom_init_file(struct atenl *an, bool flash_mode) |
| 100 | { |
| 101 | int fd; |
| 102 | |
developer | 0296d8b | 2022-11-01 11:15:32 +0800 | [diff] [blame] | 103 | if (!atenl_eeprom_file_exists()) |
| 104 | return atenl_create_file(an, flash_mode); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 105 | |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 106 | fd = open(eeprom_file, O_RDWR); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 107 | if (fd < 0) |
| 108 | perror("open"); |
| 109 | |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 110 | return fd; |
| 111 | } |
| 112 | |
| 113 | static void |
developer | f30d447 | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 114 | atenl_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; |
developer | ae49e8a | 2023-10-31 16:53:21 +0800 | [diff] [blame] | 120 | } else if (is_mt7916(an) || is_mt7981(an)) { |
developer | f30d447 | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 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; |
developer | bb73fa2 | 2022-12-28 22:40:23 +0800 | [diff] [blame] | 150 | } else if (is_mt7996(an)) { |
| 151 | /* TODO: parse info if required */ |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 152 | } else if (is_mt7992(an)) { |
| 153 | /* TODO: parse info if required */ |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 154 | } else if (is_mt7990(an)) { |
| 155 | /* TODO: parse info if required */ |
developer | f30d447 | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | static void |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 160 | atenl_eeprom_init_max_size(struct atenl *an) |
| 161 | { |
| 162 | switch (an->chip_id) { |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 163 | case MT7915_DEVICE_ID: |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 164 | an->eeprom_size = 3584; |
developer | 454c465 | 2022-08-31 20:39:29 +0800 | [diff] [blame] | 165 | an->eeprom_prek_offs = 0x62; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 166 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 167 | case MT7916_EEPROM_CHIP_ID: |
| 168 | case MT7916_DEVICE_ID: |
| 169 | case MT7981_DEVICE_ID: |
| 170 | case MT7986_DEVICE_ID: |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 171 | an->eeprom_size = 4096; |
developer | 454c465 | 2022-08-31 20:39:29 +0800 | [diff] [blame] | 172 | an->eeprom_prek_offs = 0x19a; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 173 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 174 | case MT7996_DEVICE_ID: |
| 175 | case MT7992_DEVICE_ID: |
| 176 | case MT7990_DEVICE_ID: |
developer | bb73fa2 | 2022-12-28 22:40:23 +0800 | [diff] [blame] | 177 | an->eeprom_size = 7680; |
| 178 | an->eeprom_prek_offs = 0x1a5; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 179 | default: |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | atenl_eeprom_init_band_cap(struct atenl *an) |
| 186 | { |
developer | bb73fa2 | 2022-12-28 22:40:23 +0800 | [diff] [blame] | 187 | #define EAGLE_BAND_SEL(index) MT_EE_WIFI_EAGLE_CONF##index##_BAND_SEL |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 188 | u8 *eeprom = an->eeprom_data; |
| 189 | |
| 190 | if (is_mt7915(an)) { |
| 191 | u8 val = eeprom[MT_EE_WIFI_CONF]; |
| 192 | u8 band_sel = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val); |
| 193 | struct atenl_band *anb = &an->anb[0]; |
| 194 | |
| 195 | /* MT7915A */ |
| 196 | if (band_sel == MT_EE_BAND_SEL_DEFAULT) { |
| 197 | anb->valid = true; |
| 198 | anb->cap = BAND_TYPE_2G_5G; |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | /* MT7915D */ |
| 203 | if (band_sel == MT_EE_BAND_SEL_2GHZ) { |
| 204 | anb->valid = true; |
| 205 | anb->cap = BAND_TYPE_2G; |
| 206 | } |
| 207 | |
| 208 | val = eeprom[MT_EE_WIFI_CONF + 1]; |
| 209 | band_sel = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val); |
| 210 | anb++; |
| 211 | |
| 212 | if (band_sel == MT_EE_BAND_SEL_5GHZ) { |
| 213 | anb->valid = true; |
| 214 | anb->cap = BAND_TYPE_5G; |
| 215 | } |
developer | ae49e8a | 2023-10-31 16:53:21 +0800 | [diff] [blame] | 216 | } else if (is_mt7916(an) || is_mt7981(an) || is_mt7986(an)) { |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 217 | struct atenl_band *anb; |
| 218 | u8 val, band_sel; |
| 219 | int i; |
| 220 | |
| 221 | for (i = 0; i < 2; i++) { |
| 222 | val = eeprom[MT_EE_WIFI_CONF + i]; |
| 223 | band_sel = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val); |
| 224 | anb = &an->anb[i]; |
| 225 | |
| 226 | anb->valid = true; |
| 227 | switch (band_sel) { |
| 228 | case MT_EE_BAND_SEL_2G: |
| 229 | anb->cap = BAND_TYPE_2G; |
| 230 | break; |
| 231 | case MT_EE_BAND_SEL_5G: |
| 232 | anb->cap = BAND_TYPE_5G; |
| 233 | break; |
| 234 | case MT_EE_BAND_SEL_6G: |
| 235 | anb->cap = BAND_TYPE_6G; |
| 236 | break; |
| 237 | case MT_EE_BAND_SEL_5G_6G: |
| 238 | anb->cap = BAND_TYPE_5G_6G; |
| 239 | break; |
| 240 | default: |
| 241 | break; |
| 242 | } |
| 243 | } |
developer | bb73fa2 | 2022-12-28 22:40:23 +0800 | [diff] [blame] | 244 | } else if (is_mt7996(an)) { |
| 245 | struct atenl_band *anb; |
| 246 | u8 val, band_sel; |
| 247 | u8 band_sel_mask[3] = {EAGLE_BAND_SEL(0), EAGLE_BAND_SEL(1), |
| 248 | EAGLE_BAND_SEL(2)}; |
| 249 | int i; |
| 250 | |
| 251 | for (i = 0; i < 3; i++) { |
| 252 | val = eeprom[MT_EE_WIFI_CONF + i]; |
| 253 | band_sel = FIELD_GET(band_sel_mask[i], val); |
| 254 | anb = &an->anb[i]; |
| 255 | |
| 256 | anb->valid = true; |
| 257 | switch (band_sel) { |
| 258 | case MT_EE_EAGLE_BAND_SEL_2GHZ: |
| 259 | anb->cap = BAND_TYPE_2G; |
| 260 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 261 | case MT_EE_EAGLE_BAND_SEL_5GHZ_LOW: |
| 262 | case MT_EE_EAGLE_BAND_SEL_5GHZ_HIGH: |
developer | bb73fa2 | 2022-12-28 22:40:23 +0800 | [diff] [blame] | 263 | case MT_EE_EAGLE_BAND_SEL_5GHZ: |
| 264 | anb->cap = BAND_TYPE_5G; |
| 265 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 266 | case MT_EE_EAGLE_BAND_SEL_6GHZ_LOW: |
| 267 | case MT_EE_EAGLE_BAND_SEL_6GHZ_HIGH: |
developer | bb73fa2 | 2022-12-28 22:40:23 +0800 | [diff] [blame] | 268 | case MT_EE_EAGLE_BAND_SEL_6GHZ: |
| 269 | anb->cap = BAND_TYPE_6G; |
| 270 | break; |
developer | bb73fa2 | 2022-12-28 22:40:23 +0800 | [diff] [blame] | 271 | default: |
| 272 | break; |
| 273 | } |
| 274 | } |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 275 | } else if (is_mt7992(an) || is_mt7990(an)) { |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 276 | struct atenl_band *anb; |
| 277 | u8 val, band_sel; |
| 278 | u8 band_sel_mask[2] = {EAGLE_BAND_SEL(0), EAGLE_BAND_SEL(1)}; |
| 279 | int i; |
| 280 | |
| 281 | for (i = 0; i < 2; i++) { |
| 282 | val = eeprom[MT_EE_WIFI_CONF + i]; |
| 283 | band_sel = FIELD_GET(band_sel_mask[i], val); |
| 284 | anb = &an->anb[i]; |
| 285 | |
| 286 | anb->valid = true; |
| 287 | switch (band_sel) { |
| 288 | case MT_EE_EAGLE_BAND_SEL_2GHZ: |
| 289 | anb->cap = BAND_TYPE_2G; |
| 290 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 291 | case MT_EE_EAGLE_BAND_SEL_5GHZ_LOW: |
| 292 | case MT_EE_EAGLE_BAND_SEL_5GHZ_HIGH: |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 293 | case MT_EE_EAGLE_BAND_SEL_5GHZ: |
| 294 | anb->cap = BAND_TYPE_5G; |
| 295 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 296 | case MT_EE_EAGLE_BAND_SEL_6GHZ_LOW: |
| 297 | case MT_EE_EAGLE_BAND_SEL_6GHZ_HIGH: |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 298 | case MT_EE_EAGLE_BAND_SEL_6GHZ: |
| 299 | anb->cap = BAND_TYPE_6G; |
| 300 | break; |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 301 | default: |
| 302 | break; |
| 303 | } |
| 304 | } |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | static void |
| 309 | atenl_eeprom_init_antenna_cap(struct atenl *an) |
| 310 | { |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 311 | switch (an->chip_id) { |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 312 | case MT7915_DEVICE_ID: |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 313 | if (an->anb[0].cap == BAND_TYPE_2G_5G) |
| 314 | an->anb[0].chainmask = 0xf; |
| 315 | else { |
| 316 | an->anb[0].chainmask = 0x3; |
| 317 | an->anb[1].chainmask = 0xc; |
| 318 | } |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 319 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 320 | case MT7916_EEPROM_CHIP_ID: |
| 321 | case MT7916_DEVICE_ID: |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 322 | an->anb[0].chainmask = 0x3; |
| 323 | an->anb[1].chainmask = 0x3; |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 324 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 325 | case MT7981_DEVICE_ID: |
developer | ae49e8a | 2023-10-31 16:53:21 +0800 | [diff] [blame] | 326 | an->anb[0].chainmask = 0x3; |
| 327 | an->anb[1].chainmask = 0x7; |
| 328 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 329 | case MT7986_DEVICE_ID: |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 330 | an->anb[0].chainmask = 0xf; |
| 331 | an->anb[1].chainmask = 0xf; |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 332 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 333 | case MT7996_DEVICE_ID: |
| 334 | /* TODO: handle 4T5R */ |
developer | bb73fa2 | 2022-12-28 22:40:23 +0800 | [diff] [blame] | 335 | an->anb[0].chainmask = 0xf; |
| 336 | an->anb[1].chainmask = 0xf; |
| 337 | an->anb[2].chainmask = 0xf; |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 338 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 339 | case MT7992_DEVICE_ID: |
| 340 | /* TODO: handle BE7200 2i5i 5T5R */ |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 341 | an->anb[0].chainmask = 0xf; |
| 342 | an->anb[1].chainmask = 0xf; |
| 343 | break; |
developer | 9efec3f | 2024-09-23 13:43:05 +0800 | [diff] [blame^] | 344 | case MT7990_DEVICE_ID: |
| 345 | an->anb[0].chainmask = 0x3; |
| 346 | an->anb[1].chainmask = 0x7; |
| 347 | break; |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 348 | default: |
| 349 | break; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | int atenl_eeprom_init(struct atenl *an, u8 phy_idx) |
| 354 | { |
| 355 | bool flash_mode; |
| 356 | int eeprom_fd; |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 357 | char buf[30]; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 358 | |
| 359 | set_band_val(an, 0, phy_idx, phy_idx); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 360 | atenl_nl_check_mtd(an); |
| 361 | flash_mode = an->mtd_part != NULL; |
| 362 | |
developer | bb73fa2 | 2022-12-28 22:40:23 +0800 | [diff] [blame] | 363 | // Get the first main phy index for this chip |
developer | 8604599 | 2024-06-03 16:24:42 +0800 | [diff] [blame] | 364 | an->main_phy_idx = phy_idx - an->band_idx; |
| 365 | snprintf(buf, sizeof(buf), "/tmp/atenl-eeprom-phy%u", an->main_phy_idx); |
developer | 454c465 | 2022-08-31 20:39:29 +0800 | [diff] [blame] | 366 | eeprom_file = strdup(buf); |
| 367 | |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 368 | eeprom_fd = atenl_eeprom_init_file(an, flash_mode); |
| 369 | if (eeprom_fd < 0) |
| 370 | return -1; |
| 371 | |
| 372 | an->eeprom_data = mmap(NULL, EEPROM_PART_SIZE, PROT_READ | PROT_WRITE, |
developer | 454c465 | 2022-08-31 20:39:29 +0800 | [diff] [blame] | 373 | MAP_SHARED, eeprom_fd, 0); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 374 | if (!an->eeprom_data) { |
| 375 | perror("mmap"); |
| 376 | close(eeprom_fd); |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | an->eeprom_fd = eeprom_fd; |
developer | f30d447 | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 381 | atenl_eeprom_init_chip_id(an); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 382 | atenl_eeprom_init_max_size(an); |
| 383 | atenl_eeprom_init_band_cap(an); |
| 384 | atenl_eeprom_init_antenna_cap(an); |
| 385 | |
| 386 | if (get_band_val(an, 1, valid)) |
| 387 | set_band_val(an, 1, phy_idx, phy_idx + 1); |
| 388 | |
developer | 8686910 | 2023-05-15 13:52:35 +0800 | [diff] [blame] | 389 | if (get_band_val(an, 2, valid)) |
| 390 | set_band_val(an, 2, phy_idx, phy_idx + 2); |
| 391 | |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | void atenl_eeprom_close(struct atenl *an) |
| 396 | { |
| 397 | msync(an->eeprom_data, EEPROM_PART_SIZE, MS_SYNC); |
| 398 | munmap(an->eeprom_data, EEPROM_PART_SIZE); |
| 399 | close(an->eeprom_fd); |
| 400 | |
developer | f30d447 | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 401 | if (!an->cmd_mode) { |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 402 | if (remove(eeprom_file)) |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 403 | perror("remove"); |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | free(eeprom_file); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 407 | } |
| 408 | |
developer | 454c465 | 2022-08-31 20:39:29 +0800 | [diff] [blame] | 409 | int atenl_eeprom_update_precal(struct atenl *an, int write_offs, int size) |
| 410 | { |
| 411 | u32 offs = an->eeprom_prek_offs; |
| 412 | u8 cal_indicator, *eeprom, *pre_cal; |
| 413 | |
| 414 | if (!an->cal && !an->cal_info) |
| 415 | return 0; |
| 416 | |
| 417 | eeprom = an->eeprom_data; |
| 418 | pre_cal = eeprom + an->eeprom_size; |
| 419 | cal_indicator = an->cal_info[4]; |
| 420 | |
| 421 | memcpy(eeprom + offs, &cal_indicator, sizeof(u8)); |
| 422 | memcpy(pre_cal, an->cal_info, PRE_CAL_INFO); |
| 423 | pre_cal += (PRE_CAL_INFO + write_offs); |
| 424 | |
| 425 | if (an->cal) |
| 426 | memcpy(pre_cal, an->cal, size); |
| 427 | else |
| 428 | memset(pre_cal, 0, size); |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 433 | int atenl_eeprom_write_mtd(struct atenl *an) |
| 434 | { |
developer | 091f334 | 2023-11-10 11:26:03 +0800 | [diff] [blame] | 435 | #define TMP_FILE "/tmp/tmp_eeprom.bin" |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 436 | pid_t pid; |
developer | 091f334 | 2023-11-10 11:26:03 +0800 | [diff] [blame] | 437 | u32 size = an->eeprom_size; |
| 438 | u32 *precal_info = an->eeprom_data + an->eeprom_size; |
| 439 | u32 precal_size = precal_info[0] + precal_info[1]; |
| 440 | char cmd[100]; |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 441 | |
developer | 1042c08 | 2023-10-27 19:57:13 +0800 | [diff] [blame] | 442 | if (an->mtd_part == NULL || !(~an->mtd_offset)) |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 443 | return 0; |
| 444 | |
developer | 091f334 | 2023-11-10 11:26:03 +0800 | [diff] [blame] | 445 | if (precal_size) |
| 446 | size += PRE_CAL_INFO + precal_size; |
| 447 | |
| 448 | sprintf(cmd, "dd if=%s of=%s bs=1 count=%d", eeprom_file, TMP_FILE, size); |
| 449 | system(cmd); |
| 450 | |
| 451 | sprintf(cmd, "mtd -p %d write %s %s", an->mtd_offset, TMP_FILE, an->mtd_part); |
| 452 | system(cmd); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 453 | |
developer | 091f334 | 2023-11-10 11:26:03 +0800 | [diff] [blame] | 454 | sprintf(cmd, "rm %s", TMP_FILE); |
| 455 | system(cmd); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
developer | f30d447 | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 460 | /* Directly read values from driver's eeprom. |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 461 | * It's usally used to get calibrated data from driver. |
| 462 | */ |
| 463 | int atenl_eeprom_read_from_driver(struct atenl *an, u32 offset, int len) |
| 464 | { |
| 465 | u8 *eeprom_data = an->eeprom_data + offset; |
| 466 | char fname[64], buf[1024]; |
| 467 | int fd_ori, ret; |
| 468 | ssize_t rd; |
| 469 | |
| 470 | snprintf(fname, sizeof(fname), |
| 471 | "/sys/kernel/debug/ieee80211/phy%d/mt76/eeprom", |
developer | 8604599 | 2024-06-03 16:24:42 +0800 | [diff] [blame] | 472 | an->main_phy_idx); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 473 | fd_ori = open(fname, O_RDONLY); |
| 474 | if (fd_ori < 0) |
| 475 | return -1; |
| 476 | |
| 477 | ret = lseek(fd_ori, offset, SEEK_SET); |
| 478 | if (ret < 0) |
| 479 | goto out; |
| 480 | |
| 481 | while ((rd = read(fd_ori, buf, sizeof(buf))) > 0 && len) { |
| 482 | if (len < rd) { |
| 483 | memcpy(eeprom_data, buf, len); |
| 484 | break; |
| 485 | } else { |
| 486 | memcpy(eeprom_data, buf, rd); |
| 487 | eeprom_data += rd; |
| 488 | len -= rd; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | ret = 0; |
| 493 | out: |
| 494 | close(fd_ori); |
| 495 | return ret; |
| 496 | } |
| 497 | |
developer | dcf5eb3 | 2024-09-03 17:06:32 +0800 | [diff] [blame] | 498 | /* Update all eeprom values to driver before writing efuse or ext eeprom */ |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 499 | static void |
| 500 | atenl_eeprom_sync_to_driver(struct atenl *an) |
| 501 | { |
| 502 | int i; |
| 503 | |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 504 | for (i = 0; i < an->eeprom_size; i += 16) |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 505 | atenl_nl_write_eeprom(an, i, &an->eeprom_data[i], 16); |
| 506 | } |
| 507 | |
| 508 | void atenl_eeprom_cmd_handler(struct atenl *an, u8 phy_idx, char *cmd) |
| 509 | { |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 510 | an->cmd_mode = true; |
| 511 | |
| 512 | atenl_eeprom_init(an, phy_idx); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 513 | |
| 514 | if (!strncmp(cmd, "sync eeprom all", 15)) { |
| 515 | atenl_eeprom_write_mtd(an); |
| 516 | } else if (!strncmp(cmd, "eeprom", 6)) { |
| 517 | char *s = strchr(cmd, ' '); |
| 518 | |
| 519 | if (!s) { |
developer | f30d447 | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 520 | atenl_err("eeprom: please type a correct command\n"); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 521 | return; |
| 522 | } |
| 523 | |
| 524 | s++; |
| 525 | if (!strncmp(s, "reset", 5)) { |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 526 | unlink(eeprom_file); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 527 | } else if (!strncmp(s, "file", 4)) { |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 528 | atenl_info("%s\n", eeprom_file); |
developer | 1042c08 | 2023-10-27 19:57:13 +0800 | [diff] [blame] | 529 | if (an->mtd_part != NULL) |
| 530 | atenl_info("%s mode\n", |
| 531 | ~an->mtd_offset == 0 ? "Binfile" : "Flash"); |
| 532 | else |
| 533 | atenl_info("Efuse / Default bin mode\n"); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 534 | } else if (!strncmp(s, "set", 3)) { |
| 535 | u32 offset, val; |
| 536 | |
| 537 | s = strchr(s, ' '); |
| 538 | if (!s) |
| 539 | return; |
| 540 | s++; |
| 541 | |
| 542 | if (!sscanf(s, "%x=%x", &offset, &val) || |
| 543 | offset > EEPROM_PART_SIZE) |
| 544 | return; |
| 545 | |
| 546 | an->eeprom_data[offset] = val; |
| 547 | atenl_info("set offset 0x%x to 0x%x\n", offset, val); |
developer | 48fbcb6 | 2022-03-10 14:24:55 +0800 | [diff] [blame] | 548 | } else if (!strncmp(s, "update buffermode", 17)) { |
developer | 2bf395f | 2022-01-26 20:50:22 +0800 | [diff] [blame] | 549 | atenl_eeprom_sync_to_driver(an); |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 550 | atenl_nl_update_buffer_mode(an); |
| 551 | } else if (!strncmp(s, "write", 5)) { |
| 552 | s = strchr(s, ' '); |
| 553 | if (!s) |
| 554 | return; |
| 555 | s++; |
| 556 | |
developer | 48fbcb6 | 2022-03-10 14:24:55 +0800 | [diff] [blame] | 557 | if (!strncmp(s, "flash", 5)) { |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 558 | atenl_eeprom_write_mtd(an); |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 559 | } else if (!strncmp(s, "to efuse", 8)) { |
| 560 | atenl_eeprom_sync_to_driver(an); |
| 561 | atenl_nl_write_efuse_all(an); |
developer | dcf5eb3 | 2024-09-03 17:06:32 +0800 | [diff] [blame] | 562 | } else if (!strncmp(s, "to ext", 6)) { |
| 563 | atenl_eeprom_sync_to_driver(an); |
| 564 | atenl_nl_write_ext_eeprom_all(an); |
developer | e2e8ccc | 2023-10-13 18:41:57 +0800 | [diff] [blame] | 565 | } |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 566 | } else if (!strncmp(s, "read", 4)) { |
| 567 | u32 offset; |
| 568 | |
| 569 | s = strchr(s, ' '); |
| 570 | if (!s) |
| 571 | return; |
| 572 | s++; |
| 573 | |
| 574 | if (!sscanf(s, "%x", &offset) || |
| 575 | offset > EEPROM_PART_SIZE) |
| 576 | return; |
| 577 | |
| 578 | atenl_info("val = 0x%x (%u)\n", an->eeprom_data[offset], |
| 579 | an->eeprom_data[offset]); |
developer | 454c465 | 2022-08-31 20:39:29 +0800 | [diff] [blame] | 580 | } else if (!strncmp(s, "precal", 6)) { |
| 581 | s = strchr(s, ' '); |
| 582 | if (!s) |
| 583 | return; |
| 584 | s++; |
| 585 | |
| 586 | if (!strncmp(s, "sync group", 10)) { |
| 587 | atenl_nl_precal_sync_from_driver(an, PREK_SYNC_GROUP); |
| 588 | } else if (!strncmp(s, "sync dpd 2g", 11)) { |
| 589 | atenl_nl_precal_sync_from_driver(an, PREK_SYNC_DPD_2G); |
| 590 | } else if (!strncmp(s, "sync dpd 5g", 11)) { |
| 591 | atenl_nl_precal_sync_from_driver(an, PREK_SYNC_DPD_5G); |
| 592 | } else if (!strncmp(s, "sync dpd 6g", 11)) { |
| 593 | atenl_nl_precal_sync_from_driver(an, PREK_SYNC_DPD_6G); |
| 594 | } else if (!strncmp(s, "group clean", 11)) { |
| 595 | atenl_nl_precal_sync_from_driver(an, PREK_CLEAN_GROUP); |
| 596 | } else if (!strncmp(s, "dpd clean", 9)) { |
| 597 | atenl_nl_precal_sync_from_driver(an, PREK_CLEAN_DPD); |
| 598 | } else if (!strncmp(s, "sync", 4)) { |
| 599 | atenl_nl_precal_sync_from_driver(an, PREK_SYNC_ALL); |
| 600 | } |
developer | 25a2729 | 2023-07-21 13:42:14 +0800 | [diff] [blame] | 601 | } else if (!strncmp(s, "ibf sync", 8)) { |
| 602 | atenl_get_ibf_cal_result(an); |
developer | 48fbcb6 | 2022-03-10 14:24:55 +0800 | [diff] [blame] | 603 | } else { |
developer | 25a2729 | 2023-07-21 13:42:14 +0800 | [diff] [blame] | 604 | atenl_err("Unknown eeprom command: %s\n", cmd); |
| 605 | } |
developer | 22c7ab6 | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 606 | } else { |
| 607 | atenl_err("Unknown command: %s\n", cmd); |
| 608 | } |
| 609 | } |