Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2023 PHYTEC Messtechnik GmbH |
| 4 | * Author: Teresa Remmet <t.remmet@phytec.de> |
| 5 | */ |
| 6 | |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 7 | #include <dm/device.h> |
| 8 | #include <dm/uclass.h> |
| 9 | #include <i2c.h> |
| 10 | #include <u-boot/crc.h> |
Daniel Schultz | a440ec2 | 2024-04-19 08:55:36 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <extension_board.h> |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 13 | |
| 14 | #include "phytec_som_detection.h" |
| 15 | |
| 16 | struct phytec_eeprom_data eeprom_data; |
| 17 | |
Yannic Moog | 5dd7f7c | 2023-12-20 09:45:35 +0100 | [diff] [blame] | 18 | #if IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION) |
| 19 | |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 20 | int phytec_eeprom_data_setup_fallback(struct phytec_eeprom_data *data, |
| 21 | int bus_num, int addr, int addr_fallback) |
| 22 | { |
| 23 | int ret; |
| 24 | |
| 25 | ret = phytec_eeprom_data_init(data, bus_num, addr); |
| 26 | if (ret) { |
| 27 | pr_err("%s: init failed. Trying fall back address 0x%x\n", |
| 28 | __func__, addr_fallback); |
| 29 | ret = phytec_eeprom_data_init(data, bus_num, addr_fallback); |
| 30 | } |
| 31 | |
| 32 | if (ret) |
| 33 | pr_err("%s: EEPROM data init failed\n", __func__); |
| 34 | |
| 35 | return ret; |
| 36 | } |
| 37 | |
| 38 | int phytec_eeprom_data_setup(struct phytec_eeprom_data *data, |
| 39 | int bus_num, int addr) |
| 40 | { |
| 41 | int ret; |
| 42 | |
| 43 | ret = phytec_eeprom_data_init(data, bus_num, addr); |
| 44 | if (ret) |
| 45 | pr_err("%s: EEPROM data init failed\n", __func__); |
| 46 | |
| 47 | return ret; |
| 48 | } |
| 49 | |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 50 | int phytec_eeprom_read(u8 *data, int bus_num, int addr, int size, int offset) |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 51 | { |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 52 | int ret; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 53 | |
| 54 | #if CONFIG_IS_ENABLED(DM_I2C) |
| 55 | struct udevice *dev; |
| 56 | |
| 57 | ret = i2c_get_chip_for_busnum(bus_num, addr, 2, &dev); |
| 58 | if (ret) { |
| 59 | pr_err("%s: i2c EEPROM not found: %i.\n", __func__, ret); |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 60 | return ret; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 63 | ret = dm_i2c_read(dev, offset, (uint8_t *)data, size); |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 64 | if (ret) { |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 65 | pr_err("%s: Unable to read EEPROM data: %i\n", __func__, ret); |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 66 | return ret; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 67 | } |
| 68 | #else |
| 69 | i2c_set_bus_num(bus_num); |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 70 | ret = i2c_read(addr, offset, 2, (uint8_t *)data, size); |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 71 | #endif |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 72 | return ret; |
| 73 | } |
| 74 | |
Daniel Schultz | b31bec5 | 2024-05-21 23:18:24 -0700 | [diff] [blame] | 75 | int phytec_eeprom_data_init_v2(struct phytec_eeprom_data *data) |
| 76 | { |
| 77 | unsigned int crc; |
| 78 | |
| 79 | if (!data) |
| 80 | return -1; |
| 81 | |
| 82 | crc = crc8(0, (const unsigned char *)&data->payload, PHYTEC_API2_DATA_LEN); |
| 83 | debug("%s: crc: %x\n", __func__, crc); |
| 84 | |
| 85 | if (crc) { |
| 86 | pr_err("%s: CRC mismatch. EEPROM data is not usable.\n", |
| 87 | __func__); |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Daniel Schultz | 03e619d | 2024-05-21 23:18:25 -0700 | [diff] [blame] | 94 | #if IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION_BLOCKS) |
| 95 | |
| 96 | int phytec_eeprom_data_init_v3_block(struct phytec_eeprom_data *data, |
| 97 | struct phytec_api3_block_header *header, |
| 98 | u8 *payload) |
| 99 | { |
| 100 | struct phytec_api3_element *element = NULL; |
| 101 | struct phytec_api3_element *list_iterator; |
| 102 | |
| 103 | if (!header) |
| 104 | return -1; |
| 105 | if (!payload) |
| 106 | return -1; |
| 107 | |
| 108 | debug("%s: block type: %i\n", __func__, header->block_type); |
| 109 | switch (header->block_type) { |
| 110 | case PHYTEC_API3_BLOCK_MAC: |
| 111 | element = phytec_blocks_init_mac(header, payload); |
| 112 | break; |
| 113 | default: |
| 114 | debug("%s: Unknown block type %i\n", __func__, |
| 115 | header->block_type); |
| 116 | } |
| 117 | if (!element) |
| 118 | return -1; |
| 119 | |
| 120 | if (!data->payload.block_head) { |
| 121 | data->payload.block_head = element; |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | list_iterator = data->payload.block_head; |
| 126 | while (list_iterator && list_iterator->next) |
| 127 | list_iterator = list_iterator->next; |
| 128 | list_iterator->next = element; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | int phytec_eeprom_data_init_v3(struct phytec_eeprom_data *data, |
| 134 | int bus_num, int addr) |
| 135 | { |
| 136 | int ret, i; |
| 137 | struct phytec_api3_header header; |
| 138 | unsigned int crc; |
| 139 | u8 *payload; |
| 140 | int block_addr; |
| 141 | struct phytec_api3_block_header *block_header; |
| 142 | |
| 143 | if (!data) |
| 144 | return -1; |
| 145 | |
| 146 | ret = phytec_eeprom_read((uint8_t *)&header, bus_num, addr, |
| 147 | PHYTEC_API3_DATA_HEADER_LEN, |
| 148 | PHYTEC_API2_DATA_LEN); |
| 149 | if (ret) { |
| 150 | pr_err("%s: Failed to read API v3 data header.\n", __func__); |
| 151 | goto err; |
| 152 | } |
| 153 | |
| 154 | crc = crc8(0, (const unsigned char *)&header, |
| 155 | PHYTEC_API3_DATA_HEADER_LEN); |
| 156 | debug("%s: crc: %x\n", __func__, crc); |
| 157 | if (crc) { |
| 158 | pr_err("%s: CRC mismatch. API3 header is unusable.\n", |
| 159 | __func__); |
| 160 | goto err; |
| 161 | } |
| 162 | |
| 163 | debug("%s: data length: %i\n", __func__, header.data_length); |
| 164 | payload = malloc(header.data_length); |
| 165 | if (!payload) { |
| 166 | pr_err("%s: Unable to allocate memory\n", __func__); |
| 167 | goto err_payload; |
| 168 | } |
| 169 | |
| 170 | ret = phytec_eeprom_read(payload, bus_num, addr, header.data_length, |
| 171 | PHYTEC_API3_DATA_HEADER_LEN + |
| 172 | PHYTEC_API2_DATA_LEN); |
| 173 | if (ret) { |
| 174 | pr_err("%s: Failed to read API v3 data payload.\n", __func__); |
| 175 | goto err_payload; |
| 176 | } |
| 177 | |
| 178 | block_addr = 0; |
| 179 | debug("%s: block count: %i\n", __func__, header.block_count); |
| 180 | for (i = 0; i < header.block_count; i++) { |
| 181 | debug("%s: block_addr: %i\n", __func__, block_addr); |
| 182 | block_header = (struct phytec_api3_block_header *) |
| 183 | &payload[block_addr]; |
| 184 | crc = crc8(0, (const unsigned char *)block_header, |
| 185 | PHYTEC_API3_BLOCK_HEADER_LEN); |
| 186 | |
| 187 | debug("%s: crc: %x\n", __func__, crc); |
| 188 | if (crc) { |
| 189 | pr_err("%s: CRC mismatch. API3 block header is unusable\n", |
| 190 | __func__); |
| 191 | goto err_payload; |
| 192 | } |
| 193 | |
| 194 | ret = phytec_eeprom_data_init_v3_block(data, block_header, |
| 195 | &payload[block_addr + PHYTEC_API3_BLOCK_HEADER_LEN]); |
| 196 | /* Ignore failed block initialization and continue. */ |
| 197 | if (ret) |
| 198 | debug("%s: Unable to create block with index %i.\n", |
| 199 | __func__, i); |
| 200 | |
| 201 | block_addr = block_header->next_block; |
| 202 | } |
| 203 | |
| 204 | free(payload); |
| 205 | return 0; |
| 206 | err_payload: |
| 207 | free(payload); |
| 208 | err: |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | #else |
| 213 | |
| 214 | inline int phytec_eeprom_data_init_v3(struct phytec_eeprom_data *data, |
| 215 | int bus_num, int addr) |
| 216 | { |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | #endif |
| 221 | |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 222 | int phytec_eeprom_data_init(struct phytec_eeprom_data *data, |
| 223 | int bus_num, int addr) |
| 224 | { |
| 225 | int ret, i; |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 226 | u8 *ptr; |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 227 | |
| 228 | if (!data) |
| 229 | data = &eeprom_data; |
| 230 | |
| 231 | ret = phytec_eeprom_read((u8 *)data, bus_num, addr, |
Daniel Schultz | 794aec6 | 2024-05-21 23:18:23 -0700 | [diff] [blame] | 232 | PHYTEC_API2_DATA_LEN, 0); |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 233 | if (ret) |
| 234 | goto err; |
Daniel Schultz | 03e619d | 2024-05-21 23:18:25 -0700 | [diff] [blame] | 235 | data->payload.block_head = NULL; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 236 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 237 | if (data->payload.api_rev == 0xff) { |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 238 | pr_err("%s: EEPROM is not flashed. Prototype?\n", __func__); |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 239 | ret = -EINVAL; |
| 240 | goto err; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Daniel Schultz | a132b30 | 2024-04-19 08:55:39 -0700 | [diff] [blame] | 243 | ptr = (u8 *)data; |
Daniel Schultz | 794aec6 | 2024-05-21 23:18:23 -0700 | [diff] [blame] | 244 | for (i = 0; i < PHYTEC_API2_DATA_LEN; ++i) |
Yannic Moog | 7bbbe18 | 2023-12-20 09:45:34 +0100 | [diff] [blame] | 245 | if (ptr[i] != 0x0) |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 246 | break; |
| 247 | |
Daniel Schultz | 794aec6 | 2024-05-21 23:18:23 -0700 | [diff] [blame] | 248 | if (i == PHYTEC_API2_DATA_LEN) { |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 249 | pr_err("%s: EEPROM data is all zero. Erased?\n", __func__); |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 250 | ret = -EINVAL; |
| 251 | goto err; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Daniel Schultz | b31bec5 | 2024-05-21 23:18:24 -0700 | [diff] [blame] | 254 | if (data->payload.api_rev >= PHYTEC_API_REV2) { |
| 255 | ret = phytec_eeprom_data_init_v2(data); |
| 256 | if (ret) |
| 257 | goto err; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Daniel Schultz | 03e619d | 2024-05-21 23:18:25 -0700 | [diff] [blame] | 260 | if (IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION_BLOCKS)) |
| 261 | if (data->payload.api_rev >= PHYTEC_API_REV3) { |
| 262 | ret = phytec_eeprom_data_init_v3(data, bus_num, addr); |
| 263 | if (ret) |
| 264 | goto err; |
| 265 | } |
| 266 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 267 | data->valid = true; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 268 | return 0; |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 269 | err: |
| 270 | data->valid = false; |
| 271 | return ret; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Daniel Schultz | bda3915 | 2025-01-23 06:43:49 -0800 | [diff] [blame] | 274 | static int phytec_get_product_name(struct phytec_eeprom_data *data, |
| 275 | char *product) |
| 276 | { |
| 277 | struct phytec_api2_data *api2; |
| 278 | unsigned int ksp_no, som_type; |
| 279 | int len; |
| 280 | |
| 281 | if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | api2 = &data->payload.data.data_api2; |
| 285 | |
| 286 | if (api2->som_type > 1 && api2->som_type <= 3) { |
| 287 | ksp_no = (api2->ksp_no << 8) | api2->som_no; |
| 288 | len = snprintf(product, PHYTEC_PRODUCT_NAME_MAX_LEN + 1, |
| 289 | "%s-%04u", phytec_som_type_str[api2->som_type], |
| 290 | ksp_no); |
| 291 | if (len != PHYTEC_PRODUCT_NAME_KSP_LEN) |
| 292 | return -EINVAL; |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | switch (api2->som_type) { |
| 297 | case 0: |
| 298 | som_type = api2->som_type; |
| 299 | break; |
| 300 | case 4: |
| 301 | som_type = 0; |
| 302 | break; |
| 303 | case 5: |
| 304 | som_type = 0; |
| 305 | break; |
| 306 | case 6: |
| 307 | som_type = 1; |
| 308 | break; |
| 309 | case 7: |
| 310 | som_type = 1; |
| 311 | break; |
| 312 | default: |
| 313 | pr_err("%s: Invalid SOM type: %i", __func__, api2->som_type); |
| 314 | return -EINVAL; |
| 315 | }; |
| 316 | |
| 317 | len = snprintf(product, PHYTEC_PRODUCT_NAME_MAX_LEN + 1, "%s-%03u", |
| 318 | phytec_som_type_str[som_type], api2->som_no); |
| 319 | if (len != PHYTEC_PRODUCT_NAME_STD_LEN) |
| 320 | return -EINVAL; |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static int phytec_get_part_number(struct phytec_eeprom_data *data, |
| 325 | char *part) |
| 326 | { |
| 327 | char product_name[PHYTEC_PRODUCT_NAME_MAX_LEN + 1] = {'\0'}; |
| 328 | struct phytec_api2_data *api2; |
| 329 | unsigned int ksp_type; |
| 330 | int res, len; |
| 331 | |
| 332 | if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2) |
| 333 | return -EINVAL; |
| 334 | |
| 335 | api2 = &data->payload.data.data_api2; |
| 336 | |
| 337 | res = phytec_get_product_name(data, product_name); |
| 338 | if (res) |
| 339 | return res; |
| 340 | |
| 341 | if (api2->som_type <= 1) { |
| 342 | len = snprintf(part, PHYTEC_PART_NUMBER_MAX_LEN + 1, |
| 343 | "%s-%s.%s", product_name, api2->opt, |
| 344 | api2->bom_rev); |
| 345 | if (len < PHYTEC_PART_NUMBER_STD_LEN) |
| 346 | return -EINVAL; |
| 347 | return 0; |
| 348 | } |
| 349 | if (api2->som_type <= 3) { |
| 350 | snprintf(part, PHYTEC_PART_NUMBER_MAX_LEN + 1, "%s.%s", |
| 351 | product_name, api2->bom_rev); |
| 352 | if (len != PHYTEC_PART_NUMBER_KSP_LEN) |
| 353 | return -EINVAL; |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | switch (api2->som_type) { |
| 358 | case 4: |
| 359 | ksp_type = 3; |
| 360 | break; |
| 361 | case 5: |
| 362 | ksp_type = 2; |
| 363 | break; |
| 364 | case 6: |
| 365 | ksp_type = 3; |
| 366 | break; |
| 367 | case 7: |
| 368 | ksp_type = 2; |
| 369 | break; |
| 370 | default: |
| 371 | pr_err("%s: Invalid SOM type: %i", __func__, api2->som_type); |
| 372 | return -EINVAL; |
| 373 | }; |
| 374 | |
| 375 | len = snprintf(part, PHYTEC_PART_NUMBER_MAX_LEN + 1, "%s-%s%02u.%s", |
| 376 | product_name, phytec_som_type_str[ksp_type], |
| 377 | api2->ksp_no, api2->bom_rev); |
| 378 | if (len < PHYTEC_PART_NUMBER_STD_KSP_LEN) |
| 379 | return -EINVAL; |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 384 | void __maybe_unused phytec_print_som_info(struct phytec_eeprom_data *data) |
| 385 | { |
Daniel Schultz | bda3915 | 2025-01-23 06:43:49 -0800 | [diff] [blame] | 386 | char part_number[PHYTEC_PART_NUMBER_MAX_LEN + 1] = {'\0'}; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 387 | struct phytec_api2_data *api2; |
| 388 | char pcb_sub_rev; |
Daniel Schultz | bda3915 | 2025-01-23 06:43:49 -0800 | [diff] [blame] | 389 | int res; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 390 | |
| 391 | if (!data) |
| 392 | data = &eeprom_data; |
| 393 | |
Yannic Moog | 51b9d53 | 2024-04-19 08:55:38 -0700 | [diff] [blame] | 394 | if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2) |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 395 | return; |
| 396 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 397 | api2 = &data->payload.data.data_api2; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 398 | |
| 399 | /* Calculate PCB subrevision */ |
| 400 | pcb_sub_rev = api2->pcb_sub_opt_rev & 0x0f; |
| 401 | pcb_sub_rev = pcb_sub_rev ? ((pcb_sub_rev - 1) + 'a') : ' '; |
| 402 | |
Daniel Schultz | bda3915 | 2025-01-23 06:43:49 -0800 | [diff] [blame] | 403 | res = phytec_get_part_number(data, part_number); |
| 404 | if (res) |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 405 | return; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 406 | |
Daniel Schultz | bda3915 | 2025-01-23 06:43:49 -0800 | [diff] [blame] | 407 | printf("SOM: %s\n", part_number); |
| 408 | printf("PCB Rev.: %u%c\n", api2->pcb_rev, pcb_sub_rev); |
| 409 | if (api2->som_type > 1) |
| 410 | printf("Options: %s\n", api2->opt); |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | char * __maybe_unused phytec_get_opt(struct phytec_eeprom_data *data) |
| 414 | { |
| 415 | char *opt; |
| 416 | |
| 417 | if (!data) |
| 418 | data = &eeprom_data; |
| 419 | |
Yannic Moog | 51b9d53 | 2024-04-19 08:55:38 -0700 | [diff] [blame] | 420 | if (!data->valid) |
| 421 | return NULL; |
| 422 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 423 | if (data->payload.api_rev < PHYTEC_API_REV2) |
| 424 | opt = data->payload.data.data_api0.opt; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 425 | else |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 426 | opt = data->payload.data.data_api2.opt; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 427 | |
| 428 | return opt; |
| 429 | } |
Teresa Remmet | e3d3ac4 | 2023-08-17 10:57:10 +0200 | [diff] [blame] | 430 | |
| 431 | u8 __maybe_unused phytec_get_rev(struct phytec_eeprom_data *data) |
| 432 | { |
| 433 | struct phytec_api2_data *api2; |
| 434 | |
| 435 | if (!data) |
| 436 | data = &eeprom_data; |
| 437 | |
Yannic Moog | 51b9d53 | 2024-04-19 08:55:38 -0700 | [diff] [blame] | 438 | if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2) |
Teresa Remmet | e3d3ac4 | 2023-08-17 10:57:10 +0200 | [diff] [blame] | 439 | return PHYTEC_EEPROM_INVAL; |
| 440 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 441 | api2 = &data->payload.data.data_api2; |
Teresa Remmet | e3d3ac4 | 2023-08-17 10:57:10 +0200 | [diff] [blame] | 442 | |
| 443 | return api2->pcb_rev; |
| 444 | } |
Yannic Moog | 5dd7f7c | 2023-12-20 09:45:35 +0100 | [diff] [blame] | 445 | |
Benjamin Hahn | 455dcf7 | 2024-03-06 17:18:31 +0100 | [diff] [blame] | 446 | u8 __maybe_unused phytec_get_som_type(struct phytec_eeprom_data *data) |
| 447 | { |
| 448 | if (!data) |
| 449 | data = &eeprom_data; |
Yannic Moog | 51b9d53 | 2024-04-19 08:55:38 -0700 | [diff] [blame] | 450 | |
| 451 | if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2) |
Benjamin Hahn | 455dcf7 | 2024-03-06 17:18:31 +0100 | [diff] [blame] | 452 | return PHYTEC_EEPROM_INVAL; |
| 453 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 454 | return data->payload.data.data_api2.som_type; |
Benjamin Hahn | 455dcf7 | 2024-03-06 17:18:31 +0100 | [diff] [blame] | 455 | } |
| 456 | |
Daniel Schultz | bda3915 | 2025-01-23 06:43:49 -0800 | [diff] [blame] | 457 | #if IS_ENABLED(CONFIG_OF_LIBFDT) |
| 458 | int phytec_ft_board_fixup(struct phytec_eeprom_data *data, void *blob) |
| 459 | { |
| 460 | char product_name[PHYTEC_PRODUCT_NAME_MAX_LEN + 1] = {'\0'}; |
| 461 | char part_number[PHYTEC_PART_NUMBER_MAX_LEN + 1] = {'\0'}; |
| 462 | int res; |
| 463 | |
| 464 | if (!data) |
| 465 | data = &eeprom_data; |
| 466 | |
| 467 | if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2) |
| 468 | return -EINVAL; |
| 469 | |
| 470 | res = phytec_get_product_name(data, product_name); |
| 471 | if (res) |
| 472 | return res; |
| 473 | |
| 474 | fdt_setprop(blob, 0, "phytec,som-product-name", product_name, |
| 475 | strlen(product_name) + 1); |
| 476 | |
| 477 | res = phytec_get_part_number(data, part_number); |
| 478 | if (res) |
| 479 | return res; |
| 480 | |
| 481 | fdt_setprop(blob, 0, "phytec,som-part-number", part_number, |
| 482 | strlen(part_number) + 1); |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | #endif /* IS_ENABLED(CONFIG_OF_LIBFDT) */ |
| 487 | |
Daniel Schultz | a440ec2 | 2024-04-19 08:55:36 -0700 | [diff] [blame] | 488 | #if IS_ENABLED(CONFIG_CMD_EXTENSION) |
| 489 | struct extension *phytec_add_extension(const char *name, const char *overlay, |
| 490 | const char *other) |
| 491 | { |
| 492 | struct extension *extension; |
| 493 | |
| 494 | if (strlen(overlay) > sizeof(extension->overlay)) { |
| 495 | pr_err("Overlay name %s is longer than %lu.\n", overlay, |
| 496 | sizeof(extension->overlay)); |
| 497 | return NULL; |
| 498 | } |
| 499 | |
| 500 | extension = calloc(1, sizeof(struct extension)); |
| 501 | snprintf(extension->name, sizeof(extension->name), name); |
| 502 | snprintf(extension->overlay, sizeof(extension->overlay), overlay); |
| 503 | snprintf(extension->other, sizeof(extension->other), other); |
| 504 | snprintf(extension->owner, sizeof(extension->owner), "PHYTEC"); |
| 505 | |
| 506 | return extension; |
| 507 | } |
| 508 | #endif /* IS_ENABLED(CONFIG_CMD_EXTENSION) */ |
| 509 | |
Daniel Schultz | 03e619d | 2024-05-21 23:18:25 -0700 | [diff] [blame] | 510 | struct phytec_api3_element * |
| 511 | __maybe_unused phytec_get_block_head(struct phytec_eeprom_data *data) |
| 512 | { |
| 513 | if (!data) |
| 514 | data = &eeprom_data; |
| 515 | if (!data->valid) |
| 516 | return NULL; |
| 517 | |
| 518 | return data->payload.block_head; |
| 519 | } |
| 520 | |
Yannic Moog | 5dd7f7c | 2023-12-20 09:45:35 +0100 | [diff] [blame] | 521 | #else |
| 522 | |
| 523 | inline int phytec_eeprom_data_setup(struct phytec_eeprom_data *data, |
| 524 | int bus_num, int addr) |
| 525 | { |
| 526 | return PHYTEC_EEPROM_INVAL; |
| 527 | } |
| 528 | |
| 529 | inline int phytec_eeprom_data_setup_fallback(struct phytec_eeprom_data *data, |
| 530 | int bus_num, int addr, |
| 531 | int addr_fallback) |
| 532 | { |
| 533 | return PHYTEC_EEPROM_INVAL; |
| 534 | } |
| 535 | |
| 536 | inline int phytec_eeprom_data_init(struct phytec_eeprom_data *data, |
| 537 | int bus_num, int addr) |
| 538 | { |
| 539 | return PHYTEC_EEPROM_INVAL; |
| 540 | } |
| 541 | |
| 542 | inline void __maybe_unused phytec_print_som_info(struct phytec_eeprom_data *data) |
| 543 | { |
| 544 | } |
| 545 | |
| 546 | inline char *__maybe_unused phytec_get_opt(struct phytec_eeprom_data *data) |
| 547 | { |
| 548 | return NULL; |
| 549 | } |
| 550 | |
| 551 | u8 __maybe_unused phytec_get_rev(struct phytec_eeprom_data *data) |
| 552 | { |
| 553 | return PHYTEC_EEPROM_INVAL; |
| 554 | } |
| 555 | |
Benjamin Hahn | 69ea0e3 | 2024-03-12 10:39:11 +0100 | [diff] [blame] | 556 | u8 __maybe_unused phytec_get_som_type(struct phytec_eeprom_data *data) |
| 557 | { |
| 558 | return PHYTEC_EEPROM_INVAL; |
| 559 | } |
Daniel Schultz | a440ec2 | 2024-04-19 08:55:36 -0700 | [diff] [blame] | 560 | |
Daniel Schultz | 03e619d | 2024-05-21 23:18:25 -0700 | [diff] [blame] | 561 | inline struct phytec_api3_element * __maybe_unused |
| 562 | phytec_get_block_head(struct phytec_eeprom_data *data) |
| 563 | { |
| 564 | return NULL; |
| 565 | } |
| 566 | |
Daniel Schultz | bda3915 | 2025-01-23 06:43:49 -0800 | [diff] [blame] | 567 | #if IS_ENABLED(CONFIG_OF_LIBFDT) |
| 568 | inline int phytec_ft_board_fixup(struct phytec_eeprom_data *data, void *blob) |
| 569 | { |
| 570 | return 0; |
| 571 | } |
| 572 | #endif /* IS_ENABLED(CONFIG_OF_LIBFDT) */ |
Daniel Schultz | a440ec2 | 2024-04-19 08:55:36 -0700 | [diff] [blame] | 573 | #if IS_ENABLED(CONFIG_CMD_EXTENSION) |
| 574 | inline struct extension *phytec_add_extension(const char *name, |
| 575 | const char *overlay, |
| 576 | const char *other) |
| 577 | { |
| 578 | return NULL; |
| 579 | } |
| 580 | #endif /* IS_ENABLED(CONFIG_CMD_EXTENSION) */ |
Benjamin Hahn | 69ea0e3 | 2024-03-12 10:39:11 +0100 | [diff] [blame] | 581 | |
Yannic Moog | 5dd7f7c | 2023-12-20 09:45:35 +0100 | [diff] [blame] | 582 | #endif /* IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION) */ |