blob: ab2d5a7b72673992ded49a2835d2dd3d7f04d2f7 [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 Schultz0bcb3042024-05-21 23:18:22 -070094int phytec_eeprom_data_init(struct phytec_eeprom_data *data,
95 int bus_num, int addr)
96{
97 int ret, i;
Daniel Schultz0bcb3042024-05-21 23:18:22 -070098 u8 *ptr;
Daniel Schultz0bcb3042024-05-21 23:18:22 -070099
100 if (!data)
101 data = &eeprom_data;
102
103 ret = phytec_eeprom_read((u8 *)data, bus_num, addr,
Daniel Schultz794aec62024-05-21 23:18:23 -0700104 PHYTEC_API2_DATA_LEN, 0);
Daniel Schultz0bcb3042024-05-21 23:18:22 -0700105 if (ret)
106 goto err;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200107
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700108 if (data->payload.api_rev == 0xff) {
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200109 pr_err("%s: EEPROM is not flashed. Prototype?\n", __func__);
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700110 ret = -EINVAL;
111 goto err;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200112 }
113
Daniel Schultza132b302024-04-19 08:55:39 -0700114 ptr = (u8 *)data;
Daniel Schultz794aec62024-05-21 23:18:23 -0700115 for (i = 0; i < PHYTEC_API2_DATA_LEN; ++i)
Yannic Moog7bbbe182023-12-20 09:45:34 +0100116 if (ptr[i] != 0x0)
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200117 break;
118
Daniel Schultz794aec62024-05-21 23:18:23 -0700119 if (i == PHYTEC_API2_DATA_LEN) {
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200120 pr_err("%s: EEPROM data is all zero. Erased?\n", __func__);
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700121 ret = -EINVAL;
122 goto err;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200123 }
124
Daniel Schultzb31bec52024-05-21 23:18:24 -0700125 if (data->payload.api_rev >= PHYTEC_API_REV2) {
126 ret = phytec_eeprom_data_init_v2(data);
127 if (ret)
128 goto err;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200129 }
130
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700131 data->valid = true;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200132 return 0;
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700133err:
134 data->valid = false;
135 return ret;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200136}
137
138void __maybe_unused phytec_print_som_info(struct phytec_eeprom_data *data)
139{
140 struct phytec_api2_data *api2;
141 char pcb_sub_rev;
142 unsigned int ksp_no, sub_som_type1, sub_som_type2;
143
144 if (!data)
145 data = &eeprom_data;
146
Yannic Moog51b9d532024-04-19 08:55:38 -0700147 if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2)
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200148 return;
149
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700150 api2 = &data->payload.data.data_api2;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200151
152 /* Calculate PCB subrevision */
153 pcb_sub_rev = api2->pcb_sub_opt_rev & 0x0f;
154 pcb_sub_rev = pcb_sub_rev ? ((pcb_sub_rev - 1) + 'a') : ' ';
155
156 /* print standard product string */
157 if (api2->som_type <= 1) {
158 printf("SoM: %s-%03u-%s.%s PCB rev: %u%c\n",
159 phytec_som_type_str[api2->som_type], api2->som_no,
160 api2->opt, api2->bom_rev, api2->pcb_rev, pcb_sub_rev);
161 return;
162 }
163 /* print KSP/KSM string */
164 if (api2->som_type <= 3) {
165 ksp_no = (api2->ksp_no << 8) | api2->som_no;
166 printf("SoM: %s-%u ",
167 phytec_som_type_str[api2->som_type], ksp_no);
168 /* print standard product based KSP/KSM strings */
169 } else {
170 switch (api2->som_type) {
171 case 4:
172 sub_som_type1 = 0;
173 sub_som_type2 = 3;
174 break;
175 case 5:
176 sub_som_type1 = 0;
177 sub_som_type2 = 2;
178 break;
179 case 6:
180 sub_som_type1 = 1;
181 sub_som_type2 = 3;
182 break;
183 case 7:
184 sub_som_type1 = 1;
185 sub_som_type2 = 2;
186 break;
187 default:
Yannic Moog46c507e2023-12-20 09:45:36 +0100188 pr_err("%s: Invalid SoM type: %i", __func__, api2->som_type);
189 return;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200190 };
191
192 printf("SoM: %s-%03u-%s-%03u ",
193 phytec_som_type_str[sub_som_type1],
194 api2->som_no, phytec_som_type_str[sub_som_type2],
195 api2->ksp_no);
196 }
197
198 printf("Option: %s BOM rev: %s PCB rev: %u%c\n", api2->opt,
199 api2->bom_rev, api2->pcb_rev, pcb_sub_rev);
200}
201
202char * __maybe_unused phytec_get_opt(struct phytec_eeprom_data *data)
203{
204 char *opt;
205
206 if (!data)
207 data = &eeprom_data;
208
Yannic Moog51b9d532024-04-19 08:55:38 -0700209 if (!data->valid)
210 return NULL;
211
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700212 if (data->payload.api_rev < PHYTEC_API_REV2)
213 opt = data->payload.data.data_api0.opt;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200214 else
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700215 opt = data->payload.data.data_api2.opt;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200216
217 return opt;
218}
Teresa Remmete3d3ac42023-08-17 10:57:10 +0200219
220u8 __maybe_unused phytec_get_rev(struct phytec_eeprom_data *data)
221{
222 struct phytec_api2_data *api2;
223
224 if (!data)
225 data = &eeprom_data;
226
Yannic Moog51b9d532024-04-19 08:55:38 -0700227 if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2)
Teresa Remmete3d3ac42023-08-17 10:57:10 +0200228 return PHYTEC_EEPROM_INVAL;
229
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700230 api2 = &data->payload.data.data_api2;
Teresa Remmete3d3ac42023-08-17 10:57:10 +0200231
232 return api2->pcb_rev;
233}
Yannic Moog5dd7f7c2023-12-20 09:45:35 +0100234
Benjamin Hahn455dcf72024-03-06 17:18:31 +0100235u8 __maybe_unused phytec_get_som_type(struct phytec_eeprom_data *data)
236{
237 if (!data)
238 data = &eeprom_data;
Yannic Moog51b9d532024-04-19 08:55:38 -0700239
240 if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2)
Benjamin Hahn455dcf72024-03-06 17:18:31 +0100241 return PHYTEC_EEPROM_INVAL;
242
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700243 return data->payload.data.data_api2.som_type;
Benjamin Hahn455dcf72024-03-06 17:18:31 +0100244}
245
Daniel Schultza440ec22024-04-19 08:55:36 -0700246#if IS_ENABLED(CONFIG_CMD_EXTENSION)
247struct extension *phytec_add_extension(const char *name, const char *overlay,
248 const char *other)
249{
250 struct extension *extension;
251
252 if (strlen(overlay) > sizeof(extension->overlay)) {
253 pr_err("Overlay name %s is longer than %lu.\n", overlay,
254 sizeof(extension->overlay));
255 return NULL;
256 }
257
258 extension = calloc(1, sizeof(struct extension));
259 snprintf(extension->name, sizeof(extension->name), name);
260 snprintf(extension->overlay, sizeof(extension->overlay), overlay);
261 snprintf(extension->other, sizeof(extension->other), other);
262 snprintf(extension->owner, sizeof(extension->owner), "PHYTEC");
263
264 return extension;
265}
266#endif /* IS_ENABLED(CONFIG_CMD_EXTENSION) */
267
Yannic Moog5dd7f7c2023-12-20 09:45:35 +0100268#else
269
270inline int phytec_eeprom_data_setup(struct phytec_eeprom_data *data,
271 int bus_num, int addr)
272{
273 return PHYTEC_EEPROM_INVAL;
274}
275
276inline int phytec_eeprom_data_setup_fallback(struct phytec_eeprom_data *data,
277 int bus_num, int addr,
278 int addr_fallback)
279{
280 return PHYTEC_EEPROM_INVAL;
281}
282
283inline int phytec_eeprom_data_init(struct phytec_eeprom_data *data,
284 int bus_num, int addr)
285{
286 return PHYTEC_EEPROM_INVAL;
287}
288
289inline void __maybe_unused phytec_print_som_info(struct phytec_eeprom_data *data)
290{
291}
292
293inline char *__maybe_unused phytec_get_opt(struct phytec_eeprom_data *data)
294{
295 return NULL;
296}
297
298u8 __maybe_unused phytec_get_rev(struct phytec_eeprom_data *data)
299{
300 return PHYTEC_EEPROM_INVAL;
301}
302
Benjamin Hahn69ea0e32024-03-12 10:39:11 +0100303u8 __maybe_unused phytec_get_som_type(struct phytec_eeprom_data *data)
304{
305 return PHYTEC_EEPROM_INVAL;
306}
Daniel Schultza440ec22024-04-19 08:55:36 -0700307
308#if IS_ENABLED(CONFIG_CMD_EXTENSION)
309inline struct extension *phytec_add_extension(const char *name,
310 const char *overlay,
311 const char *other)
312{
313 return NULL;
314}
315#endif /* IS_ENABLED(CONFIG_CMD_EXTENSION) */
Benjamin Hahn69ea0e32024-03-12 10:39:11 +0100316
Yannic Moog5dd7f7c2023-12-20 09:45:35 +0100317#endif /* IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION) */