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 | |
| 75 | int phytec_eeprom_data_init(struct phytec_eeprom_data *data, |
| 76 | int bus_num, int addr) |
| 77 | { |
| 78 | int ret, i; |
| 79 | unsigned int crc; |
| 80 | u8 *ptr; |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 81 | |
| 82 | if (!data) |
| 83 | data = &eeprom_data; |
| 84 | |
| 85 | ret = phytec_eeprom_read((u8 *)data, bus_num, addr, |
Daniel Schultz | 794aec6 | 2024-05-21 23:18:23 -0700 | [diff] [blame^] | 86 | PHYTEC_API2_DATA_LEN, 0); |
Daniel Schultz | 0bcb304 | 2024-05-21 23:18:22 -0700 | [diff] [blame] | 87 | if (ret) |
| 88 | goto err; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 89 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 90 | if (data->payload.api_rev == 0xff) { |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 91 | pr_err("%s: EEPROM is not flashed. Prototype?\n", __func__); |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 92 | ret = -EINVAL; |
| 93 | goto err; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Daniel Schultz | a132b30 | 2024-04-19 08:55:39 -0700 | [diff] [blame] | 96 | ptr = (u8 *)data; |
Daniel Schultz | 794aec6 | 2024-05-21 23:18:23 -0700 | [diff] [blame^] | 97 | for (i = 0; i < PHYTEC_API2_DATA_LEN; ++i) |
Yannic Moog | 7bbbe18 | 2023-12-20 09:45:34 +0100 | [diff] [blame] | 98 | if (ptr[i] != 0x0) |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 99 | break; |
| 100 | |
Daniel Schultz | 794aec6 | 2024-05-21 23:18:23 -0700 | [diff] [blame^] | 101 | if (i == PHYTEC_API2_DATA_LEN) { |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 102 | pr_err("%s: EEPROM data is all zero. Erased?\n", __func__); |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 103 | ret = -EINVAL; |
| 104 | goto err; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* We are done here for early revisions */ |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 108 | if (data->payload.api_rev <= PHYTEC_API_REV1) { |
| 109 | data->valid = true; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 110 | return 0; |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 111 | } |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 112 | |
Daniel Schultz | 794aec6 | 2024-05-21 23:18:23 -0700 | [diff] [blame^] | 113 | crc = crc8(0, (const unsigned char *)&data->payload, PHYTEC_API2_DATA_LEN); |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 114 | debug("%s: crc: %x\n", __func__, crc); |
| 115 | |
| 116 | if (crc) { |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 117 | pr_err("%s: CRC mismatch. EEPROM data is not usable.\n", |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 118 | __func__); |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 119 | ret = -EINVAL; |
| 120 | goto err; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 123 | data->valid = true; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 124 | return 0; |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 125 | err: |
| 126 | data->valid = false; |
| 127 | return ret; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void __maybe_unused phytec_print_som_info(struct phytec_eeprom_data *data) |
| 131 | { |
| 132 | struct phytec_api2_data *api2; |
| 133 | char pcb_sub_rev; |
| 134 | unsigned int ksp_no, sub_som_type1, sub_som_type2; |
| 135 | |
| 136 | if (!data) |
| 137 | data = &eeprom_data; |
| 138 | |
Yannic Moog | 51b9d53 | 2024-04-19 08:55:38 -0700 | [diff] [blame] | 139 | if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2) |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 140 | return; |
| 141 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 142 | api2 = &data->payload.data.data_api2; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 143 | |
| 144 | /* Calculate PCB subrevision */ |
| 145 | pcb_sub_rev = api2->pcb_sub_opt_rev & 0x0f; |
| 146 | pcb_sub_rev = pcb_sub_rev ? ((pcb_sub_rev - 1) + 'a') : ' '; |
| 147 | |
| 148 | /* print standard product string */ |
| 149 | if (api2->som_type <= 1) { |
| 150 | printf("SoM: %s-%03u-%s.%s PCB rev: %u%c\n", |
| 151 | phytec_som_type_str[api2->som_type], api2->som_no, |
| 152 | api2->opt, api2->bom_rev, api2->pcb_rev, pcb_sub_rev); |
| 153 | return; |
| 154 | } |
| 155 | /* print KSP/KSM string */ |
| 156 | if (api2->som_type <= 3) { |
| 157 | ksp_no = (api2->ksp_no << 8) | api2->som_no; |
| 158 | printf("SoM: %s-%u ", |
| 159 | phytec_som_type_str[api2->som_type], ksp_no); |
| 160 | /* print standard product based KSP/KSM strings */ |
| 161 | } else { |
| 162 | switch (api2->som_type) { |
| 163 | case 4: |
| 164 | sub_som_type1 = 0; |
| 165 | sub_som_type2 = 3; |
| 166 | break; |
| 167 | case 5: |
| 168 | sub_som_type1 = 0; |
| 169 | sub_som_type2 = 2; |
| 170 | break; |
| 171 | case 6: |
| 172 | sub_som_type1 = 1; |
| 173 | sub_som_type2 = 3; |
| 174 | break; |
| 175 | case 7: |
| 176 | sub_som_type1 = 1; |
| 177 | sub_som_type2 = 2; |
| 178 | break; |
| 179 | default: |
Yannic Moog | 46c507e | 2023-12-20 09:45:36 +0100 | [diff] [blame] | 180 | pr_err("%s: Invalid SoM type: %i", __func__, api2->som_type); |
| 181 | return; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | printf("SoM: %s-%03u-%s-%03u ", |
| 185 | phytec_som_type_str[sub_som_type1], |
| 186 | api2->som_no, phytec_som_type_str[sub_som_type2], |
| 187 | api2->ksp_no); |
| 188 | } |
| 189 | |
| 190 | printf("Option: %s BOM rev: %s PCB rev: %u%c\n", api2->opt, |
| 191 | api2->bom_rev, api2->pcb_rev, pcb_sub_rev); |
| 192 | } |
| 193 | |
| 194 | char * __maybe_unused phytec_get_opt(struct phytec_eeprom_data *data) |
| 195 | { |
| 196 | char *opt; |
| 197 | |
| 198 | if (!data) |
| 199 | data = &eeprom_data; |
| 200 | |
Yannic Moog | 51b9d53 | 2024-04-19 08:55:38 -0700 | [diff] [blame] | 201 | if (!data->valid) |
| 202 | return NULL; |
| 203 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 204 | if (data->payload.api_rev < PHYTEC_API_REV2) |
| 205 | opt = data->payload.data.data_api0.opt; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 206 | else |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 207 | opt = data->payload.data.data_api2.opt; |
Teresa Remmet | a6f8da5 | 2023-08-17 10:57:06 +0200 | [diff] [blame] | 208 | |
| 209 | return opt; |
| 210 | } |
Teresa Remmet | e3d3ac4 | 2023-08-17 10:57:10 +0200 | [diff] [blame] | 211 | |
| 212 | u8 __maybe_unused phytec_get_rev(struct phytec_eeprom_data *data) |
| 213 | { |
| 214 | struct phytec_api2_data *api2; |
| 215 | |
| 216 | if (!data) |
| 217 | data = &eeprom_data; |
| 218 | |
Yannic Moog | 51b9d53 | 2024-04-19 08:55:38 -0700 | [diff] [blame] | 219 | if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2) |
Teresa Remmet | e3d3ac4 | 2023-08-17 10:57:10 +0200 | [diff] [blame] | 220 | return PHYTEC_EEPROM_INVAL; |
| 221 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 222 | api2 = &data->payload.data.data_api2; |
Teresa Remmet | e3d3ac4 | 2023-08-17 10:57:10 +0200 | [diff] [blame] | 223 | |
| 224 | return api2->pcb_rev; |
| 225 | } |
Yannic Moog | 5dd7f7c | 2023-12-20 09:45:35 +0100 | [diff] [blame] | 226 | |
Benjamin Hahn | 455dcf7 | 2024-03-06 17:18:31 +0100 | [diff] [blame] | 227 | u8 __maybe_unused phytec_get_som_type(struct phytec_eeprom_data *data) |
| 228 | { |
| 229 | if (!data) |
| 230 | data = &eeprom_data; |
Yannic Moog | 51b9d53 | 2024-04-19 08:55:38 -0700 | [diff] [blame] | 231 | |
| 232 | if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2) |
Benjamin Hahn | 455dcf7 | 2024-03-06 17:18:31 +0100 | [diff] [blame] | 233 | return PHYTEC_EEPROM_INVAL; |
| 234 | |
Yannic Moog | db2dfb0 | 2024-04-19 08:55:37 -0700 | [diff] [blame] | 235 | return data->payload.data.data_api2.som_type; |
Benjamin Hahn | 455dcf7 | 2024-03-06 17:18:31 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Daniel Schultz | a440ec2 | 2024-04-19 08:55:36 -0700 | [diff] [blame] | 238 | #if IS_ENABLED(CONFIG_CMD_EXTENSION) |
| 239 | struct extension *phytec_add_extension(const char *name, const char *overlay, |
| 240 | const char *other) |
| 241 | { |
| 242 | struct extension *extension; |
| 243 | |
| 244 | if (strlen(overlay) > sizeof(extension->overlay)) { |
| 245 | pr_err("Overlay name %s is longer than %lu.\n", overlay, |
| 246 | sizeof(extension->overlay)); |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | extension = calloc(1, sizeof(struct extension)); |
| 251 | snprintf(extension->name, sizeof(extension->name), name); |
| 252 | snprintf(extension->overlay, sizeof(extension->overlay), overlay); |
| 253 | snprintf(extension->other, sizeof(extension->other), other); |
| 254 | snprintf(extension->owner, sizeof(extension->owner), "PHYTEC"); |
| 255 | |
| 256 | return extension; |
| 257 | } |
| 258 | #endif /* IS_ENABLED(CONFIG_CMD_EXTENSION) */ |
| 259 | |
Yannic Moog | 5dd7f7c | 2023-12-20 09:45:35 +0100 | [diff] [blame] | 260 | #else |
| 261 | |
| 262 | inline int phytec_eeprom_data_setup(struct phytec_eeprom_data *data, |
| 263 | int bus_num, int addr) |
| 264 | { |
| 265 | return PHYTEC_EEPROM_INVAL; |
| 266 | } |
| 267 | |
| 268 | inline int phytec_eeprom_data_setup_fallback(struct phytec_eeprom_data *data, |
| 269 | int bus_num, int addr, |
| 270 | int addr_fallback) |
| 271 | { |
| 272 | return PHYTEC_EEPROM_INVAL; |
| 273 | } |
| 274 | |
| 275 | inline int phytec_eeprom_data_init(struct phytec_eeprom_data *data, |
| 276 | int bus_num, int addr) |
| 277 | { |
| 278 | return PHYTEC_EEPROM_INVAL; |
| 279 | } |
| 280 | |
| 281 | inline void __maybe_unused phytec_print_som_info(struct phytec_eeprom_data *data) |
| 282 | { |
| 283 | } |
| 284 | |
| 285 | inline char *__maybe_unused phytec_get_opt(struct phytec_eeprom_data *data) |
| 286 | { |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | u8 __maybe_unused phytec_get_rev(struct phytec_eeprom_data *data) |
| 291 | { |
| 292 | return PHYTEC_EEPROM_INVAL; |
| 293 | } |
| 294 | |
Benjamin Hahn | 69ea0e3 | 2024-03-12 10:39:11 +0100 | [diff] [blame] | 295 | u8 __maybe_unused phytec_get_som_type(struct phytec_eeprom_data *data) |
| 296 | { |
| 297 | return PHYTEC_EEPROM_INVAL; |
| 298 | } |
Daniel Schultz | a440ec2 | 2024-04-19 08:55:36 -0700 | [diff] [blame] | 299 | |
| 300 | #if IS_ENABLED(CONFIG_CMD_EXTENSION) |
| 301 | inline struct extension *phytec_add_extension(const char *name, |
| 302 | const char *overlay, |
| 303 | const char *other) |
| 304 | { |
| 305 | return NULL; |
| 306 | } |
| 307 | #endif /* IS_ENABLED(CONFIG_CMD_EXTENSION) */ |
Benjamin Hahn | 69ea0e3 | 2024-03-12 10:39:11 +0100 | [diff] [blame] | 308 | |
Yannic Moog | 5dd7f7c | 2023-12-20 09:45:35 +0100 | [diff] [blame] | 309 | #endif /* IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION) */ |