blob: 166c3eae565ed7517538185e53dc6f483abb297c [file] [log] [blame]
Teresa Remmeta6f8da52023-08-17 10:57:06 +02001// 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 Remmeta6f8da52023-08-17 10:57:06 +02007#include <dm/device.h>
8#include <dm/uclass.h>
9#include <i2c.h>
10#include <u-boot/crc.h>
Daniel Schultza440ec22024-04-19 08:55:36 -070011#include <malloc.h>
12#include <extension_board.h>
Teresa Remmeta6f8da52023-08-17 10:57:06 +020013
14#include "phytec_som_detection.h"
15
16struct phytec_eeprom_data eeprom_data;
17
Yannic Moog5dd7f7c2023-12-20 09:45:35 +010018#if IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION)
19
Teresa Remmeta6f8da52023-08-17 10:57:06 +020020int 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
38int 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 Schultz0bcb3042024-05-21 23:18:22 -070050int phytec_eeprom_read(u8 *data, int bus_num, int addr, int size, int offset)
Teresa Remmeta6f8da52023-08-17 10:57:06 +020051{
Daniel Schultz0bcb3042024-05-21 23:18:22 -070052 int ret;
Teresa Remmeta6f8da52023-08-17 10:57:06 +020053
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 Schultz0bcb3042024-05-21 23:18:22 -070060 return ret;
Teresa Remmeta6f8da52023-08-17 10:57:06 +020061 }
62
Daniel Schultz0bcb3042024-05-21 23:18:22 -070063 ret = dm_i2c_read(dev, offset, (uint8_t *)data, size);
Teresa Remmeta6f8da52023-08-17 10:57:06 +020064 if (ret) {
Yannic Moogdb2dfb02024-04-19 08:55:37 -070065 pr_err("%s: Unable to read EEPROM data: %i\n", __func__, ret);
Daniel Schultz0bcb3042024-05-21 23:18:22 -070066 return ret;
Teresa Remmeta6f8da52023-08-17 10:57:06 +020067 }
68#else
69 i2c_set_bus_num(bus_num);
Daniel Schultz0bcb3042024-05-21 23:18:22 -070070 ret = i2c_read(addr, offset, 2, (uint8_t *)data, size);
Teresa Remmeta6f8da52023-08-17 10:57:06 +020071#endif
Daniel Schultz0bcb3042024-05-21 23:18:22 -070072 return ret;
73}
74
Daniel Schultzb31bec52024-05-21 23:18:24 -070075int 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 Schultz03e619d2024-05-21 23:18:25 -070094#if IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION_BLOCKS)
95
96int 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
133int 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;
206err_payload:
207 free(payload);
208err:
209 return -1;
210}
211
212#else
213
214inline 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 Schultz0bcb3042024-05-21 23:18:22 -0700222int phytec_eeprom_data_init(struct phytec_eeprom_data *data,
223 int bus_num, int addr)
224{
225 int ret, i;
Daniel Schultz0bcb3042024-05-21 23:18:22 -0700226 u8 *ptr;
Daniel Schultz0bcb3042024-05-21 23:18:22 -0700227
228 if (!data)
229 data = &eeprom_data;
230
231 ret = phytec_eeprom_read((u8 *)data, bus_num, addr,
Daniel Schultz794aec62024-05-21 23:18:23 -0700232 PHYTEC_API2_DATA_LEN, 0);
Daniel Schultz0bcb3042024-05-21 23:18:22 -0700233 if (ret)
234 goto err;
Daniel Schultz03e619d2024-05-21 23:18:25 -0700235 data->payload.block_head = NULL;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200236
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700237 if (data->payload.api_rev == 0xff) {
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200238 pr_err("%s: EEPROM is not flashed. Prototype?\n", __func__);
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700239 ret = -EINVAL;
240 goto err;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200241 }
242
Daniel Schultza132b302024-04-19 08:55:39 -0700243 ptr = (u8 *)data;
Daniel Schultz794aec62024-05-21 23:18:23 -0700244 for (i = 0; i < PHYTEC_API2_DATA_LEN; ++i)
Yannic Moog7bbbe182023-12-20 09:45:34 +0100245 if (ptr[i] != 0x0)
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200246 break;
247
Daniel Schultz794aec62024-05-21 23:18:23 -0700248 if (i == PHYTEC_API2_DATA_LEN) {
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200249 pr_err("%s: EEPROM data is all zero. Erased?\n", __func__);
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700250 ret = -EINVAL;
251 goto err;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200252 }
253
Daniel Schultzb31bec52024-05-21 23:18:24 -0700254 if (data->payload.api_rev >= PHYTEC_API_REV2) {
255 ret = phytec_eeprom_data_init_v2(data);
256 if (ret)
257 goto err;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200258 }
259
Daniel Schultz03e619d2024-05-21 23:18:25 -0700260 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 Moogdb2dfb02024-04-19 08:55:37 -0700267 data->valid = true;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200268 return 0;
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700269err:
270 data->valid = false;
271 return ret;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200272}
273
274void __maybe_unused phytec_print_som_info(struct phytec_eeprom_data *data)
275{
276 struct phytec_api2_data *api2;
277 char pcb_sub_rev;
278 unsigned int ksp_no, sub_som_type1, sub_som_type2;
279
280 if (!data)
281 data = &eeprom_data;
282
Yannic Moog51b9d532024-04-19 08:55:38 -0700283 if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2)
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200284 return;
285
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700286 api2 = &data->payload.data.data_api2;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200287
288 /* Calculate PCB subrevision */
289 pcb_sub_rev = api2->pcb_sub_opt_rev & 0x0f;
290 pcb_sub_rev = pcb_sub_rev ? ((pcb_sub_rev - 1) + 'a') : ' ';
291
292 /* print standard product string */
293 if (api2->som_type <= 1) {
294 printf("SoM: %s-%03u-%s.%s PCB rev: %u%c\n",
295 phytec_som_type_str[api2->som_type], api2->som_no,
296 api2->opt, api2->bom_rev, api2->pcb_rev, pcb_sub_rev);
297 return;
298 }
299 /* print KSP/KSM string */
300 if (api2->som_type <= 3) {
301 ksp_no = (api2->ksp_no << 8) | api2->som_no;
302 printf("SoM: %s-%u ",
303 phytec_som_type_str[api2->som_type], ksp_no);
304 /* print standard product based KSP/KSM strings */
305 } else {
306 switch (api2->som_type) {
307 case 4:
308 sub_som_type1 = 0;
309 sub_som_type2 = 3;
310 break;
311 case 5:
312 sub_som_type1 = 0;
313 sub_som_type2 = 2;
314 break;
315 case 6:
316 sub_som_type1 = 1;
317 sub_som_type2 = 3;
318 break;
319 case 7:
320 sub_som_type1 = 1;
321 sub_som_type2 = 2;
322 break;
323 default:
Yannic Moog46c507e2023-12-20 09:45:36 +0100324 pr_err("%s: Invalid SoM type: %i", __func__, api2->som_type);
325 return;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200326 };
327
328 printf("SoM: %s-%03u-%s-%03u ",
329 phytec_som_type_str[sub_som_type1],
330 api2->som_no, phytec_som_type_str[sub_som_type2],
331 api2->ksp_no);
332 }
333
334 printf("Option: %s BOM rev: %s PCB rev: %u%c\n", api2->opt,
335 api2->bom_rev, api2->pcb_rev, pcb_sub_rev);
336}
337
338char * __maybe_unused phytec_get_opt(struct phytec_eeprom_data *data)
339{
340 char *opt;
341
342 if (!data)
343 data = &eeprom_data;
344
Yannic Moog51b9d532024-04-19 08:55:38 -0700345 if (!data->valid)
346 return NULL;
347
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700348 if (data->payload.api_rev < PHYTEC_API_REV2)
349 opt = data->payload.data.data_api0.opt;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200350 else
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700351 opt = data->payload.data.data_api2.opt;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200352
353 return opt;
354}
Teresa Remmete3d3ac42023-08-17 10:57:10 +0200355
356u8 __maybe_unused phytec_get_rev(struct phytec_eeprom_data *data)
357{
358 struct phytec_api2_data *api2;
359
360 if (!data)
361 data = &eeprom_data;
362
Yannic Moog51b9d532024-04-19 08:55:38 -0700363 if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2)
Teresa Remmete3d3ac42023-08-17 10:57:10 +0200364 return PHYTEC_EEPROM_INVAL;
365
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700366 api2 = &data->payload.data.data_api2;
Teresa Remmete3d3ac42023-08-17 10:57:10 +0200367
368 return api2->pcb_rev;
369}
Yannic Moog5dd7f7c2023-12-20 09:45:35 +0100370
Benjamin Hahn455dcf72024-03-06 17:18:31 +0100371u8 __maybe_unused phytec_get_som_type(struct phytec_eeprom_data *data)
372{
373 if (!data)
374 data = &eeprom_data;
Yannic Moog51b9d532024-04-19 08:55:38 -0700375
376 if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2)
Benjamin Hahn455dcf72024-03-06 17:18:31 +0100377 return PHYTEC_EEPROM_INVAL;
378
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700379 return data->payload.data.data_api2.som_type;
Benjamin Hahn455dcf72024-03-06 17:18:31 +0100380}
381
Daniel Schultza440ec22024-04-19 08:55:36 -0700382#if IS_ENABLED(CONFIG_CMD_EXTENSION)
383struct extension *phytec_add_extension(const char *name, const char *overlay,
384 const char *other)
385{
386 struct extension *extension;
387
388 if (strlen(overlay) > sizeof(extension->overlay)) {
389 pr_err("Overlay name %s is longer than %lu.\n", overlay,
390 sizeof(extension->overlay));
391 return NULL;
392 }
393
394 extension = calloc(1, sizeof(struct extension));
395 snprintf(extension->name, sizeof(extension->name), name);
396 snprintf(extension->overlay, sizeof(extension->overlay), overlay);
397 snprintf(extension->other, sizeof(extension->other), other);
398 snprintf(extension->owner, sizeof(extension->owner), "PHYTEC");
399
400 return extension;
401}
402#endif /* IS_ENABLED(CONFIG_CMD_EXTENSION) */
403
Daniel Schultz03e619d2024-05-21 23:18:25 -0700404struct phytec_api3_element *
405 __maybe_unused phytec_get_block_head(struct phytec_eeprom_data *data)
406{
407 if (!data)
408 data = &eeprom_data;
409 if (!data->valid)
410 return NULL;
411
412 return data->payload.block_head;
413}
414
Yannic Moog5dd7f7c2023-12-20 09:45:35 +0100415#else
416
417inline int phytec_eeprom_data_setup(struct phytec_eeprom_data *data,
418 int bus_num, int addr)
419{
420 return PHYTEC_EEPROM_INVAL;
421}
422
423inline int phytec_eeprom_data_setup_fallback(struct phytec_eeprom_data *data,
424 int bus_num, int addr,
425 int addr_fallback)
426{
427 return PHYTEC_EEPROM_INVAL;
428}
429
430inline int phytec_eeprom_data_init(struct phytec_eeprom_data *data,
431 int bus_num, int addr)
432{
433 return PHYTEC_EEPROM_INVAL;
434}
435
436inline void __maybe_unused phytec_print_som_info(struct phytec_eeprom_data *data)
437{
438}
439
440inline char *__maybe_unused phytec_get_opt(struct phytec_eeprom_data *data)
441{
442 return NULL;
443}
444
445u8 __maybe_unused phytec_get_rev(struct phytec_eeprom_data *data)
446{
447 return PHYTEC_EEPROM_INVAL;
448}
449
Benjamin Hahn69ea0e32024-03-12 10:39:11 +0100450u8 __maybe_unused phytec_get_som_type(struct phytec_eeprom_data *data)
451{
452 return PHYTEC_EEPROM_INVAL;
453}
Daniel Schultza440ec22024-04-19 08:55:36 -0700454
Daniel Schultz03e619d2024-05-21 23:18:25 -0700455inline struct phytec_api3_element * __maybe_unused
456 phytec_get_block_head(struct phytec_eeprom_data *data)
457{
458 return NULL;
459}
460
Daniel Schultza440ec22024-04-19 08:55:36 -0700461#if IS_ENABLED(CONFIG_CMD_EXTENSION)
462inline struct extension *phytec_add_extension(const char *name,
463 const char *overlay,
464 const char *other)
465{
466 return NULL;
467}
468#endif /* IS_ENABLED(CONFIG_CMD_EXTENSION) */
Benjamin Hahn69ea0e32024-03-12 10:39:11 +0100469
Yannic Moog5dd7f7c2023-12-20 09:45:35 +0100470#endif /* IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION) */