blob: a089fe9bc9047dad356a17436df05b0a93806579 [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
75int 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;
81 const unsigned int payload_size = sizeof(struct phytec_eeprom_payload);
82
83 if (!data)
84 data = &eeprom_data;
85
86 ret = phytec_eeprom_read((u8 *)data, bus_num, addr,
87 payload_size, 0);
88 if (ret)
89 goto err;
Teresa Remmeta6f8da52023-08-17 10:57:06 +020090
Yannic Moogdb2dfb02024-04-19 08:55:37 -070091 if (data->payload.api_rev == 0xff) {
Teresa Remmeta6f8da52023-08-17 10:57:06 +020092 pr_err("%s: EEPROM is not flashed. Prototype?\n", __func__);
Yannic Moogdb2dfb02024-04-19 08:55:37 -070093 ret = -EINVAL;
94 goto err;
Teresa Remmeta6f8da52023-08-17 10:57:06 +020095 }
96
Daniel Schultza132b302024-04-19 08:55:39 -070097 ptr = (u8 *)data;
Yannic Moogdb2dfb02024-04-19 08:55:37 -070098 for (i = 0; i < payload_size; ++i)
Yannic Moog7bbbe182023-12-20 09:45:34 +010099 if (ptr[i] != 0x0)
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200100 break;
101
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700102 if (i == payload_size) {
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200103 pr_err("%s: EEPROM data is all zero. Erased?\n", __func__);
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700104 ret = -EINVAL;
105 goto err;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200106 }
107
108 /* We are done here for early revisions */
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700109 if (data->payload.api_rev <= PHYTEC_API_REV1) {
110 data->valid = true;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200111 return 0;
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700112 }
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200113
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700114 crc = crc8(0, (const unsigned char *)&data->payload, payload_size);
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200115 debug("%s: crc: %x\n", __func__, crc);
116
117 if (crc) {
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700118 pr_err("%s: CRC mismatch. EEPROM data is not usable.\n",
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200119 __func__);
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700120 ret = -EINVAL;
121 goto err;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200122 }
123
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700124 data->valid = true;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200125 return 0;
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700126err:
127 data->valid = false;
128 return ret;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200129}
130
131void __maybe_unused phytec_print_som_info(struct phytec_eeprom_data *data)
132{
133 struct phytec_api2_data *api2;
134 char pcb_sub_rev;
135 unsigned int ksp_no, sub_som_type1, sub_som_type2;
136
137 if (!data)
138 data = &eeprom_data;
139
Yannic Moog51b9d532024-04-19 08:55:38 -0700140 if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2)
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200141 return;
142
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700143 api2 = &data->payload.data.data_api2;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200144
145 /* Calculate PCB subrevision */
146 pcb_sub_rev = api2->pcb_sub_opt_rev & 0x0f;
147 pcb_sub_rev = pcb_sub_rev ? ((pcb_sub_rev - 1) + 'a') : ' ';
148
149 /* print standard product string */
150 if (api2->som_type <= 1) {
151 printf("SoM: %s-%03u-%s.%s PCB rev: %u%c\n",
152 phytec_som_type_str[api2->som_type], api2->som_no,
153 api2->opt, api2->bom_rev, api2->pcb_rev, pcb_sub_rev);
154 return;
155 }
156 /* print KSP/KSM string */
157 if (api2->som_type <= 3) {
158 ksp_no = (api2->ksp_no << 8) | api2->som_no;
159 printf("SoM: %s-%u ",
160 phytec_som_type_str[api2->som_type], ksp_no);
161 /* print standard product based KSP/KSM strings */
162 } else {
163 switch (api2->som_type) {
164 case 4:
165 sub_som_type1 = 0;
166 sub_som_type2 = 3;
167 break;
168 case 5:
169 sub_som_type1 = 0;
170 sub_som_type2 = 2;
171 break;
172 case 6:
173 sub_som_type1 = 1;
174 sub_som_type2 = 3;
175 break;
176 case 7:
177 sub_som_type1 = 1;
178 sub_som_type2 = 2;
179 break;
180 default:
Yannic Moog46c507e2023-12-20 09:45:36 +0100181 pr_err("%s: Invalid SoM type: %i", __func__, api2->som_type);
182 return;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200183 };
184
185 printf("SoM: %s-%03u-%s-%03u ",
186 phytec_som_type_str[sub_som_type1],
187 api2->som_no, phytec_som_type_str[sub_som_type2],
188 api2->ksp_no);
189 }
190
191 printf("Option: %s BOM rev: %s PCB rev: %u%c\n", api2->opt,
192 api2->bom_rev, api2->pcb_rev, pcb_sub_rev);
193}
194
195char * __maybe_unused phytec_get_opt(struct phytec_eeprom_data *data)
196{
197 char *opt;
198
199 if (!data)
200 data = &eeprom_data;
201
Yannic Moog51b9d532024-04-19 08:55:38 -0700202 if (!data->valid)
203 return NULL;
204
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700205 if (data->payload.api_rev < PHYTEC_API_REV2)
206 opt = data->payload.data.data_api0.opt;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200207 else
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700208 opt = data->payload.data.data_api2.opt;
Teresa Remmeta6f8da52023-08-17 10:57:06 +0200209
210 return opt;
211}
Teresa Remmete3d3ac42023-08-17 10:57:10 +0200212
213u8 __maybe_unused phytec_get_rev(struct phytec_eeprom_data *data)
214{
215 struct phytec_api2_data *api2;
216
217 if (!data)
218 data = &eeprom_data;
219
Yannic Moog51b9d532024-04-19 08:55:38 -0700220 if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2)
Teresa Remmete3d3ac42023-08-17 10:57:10 +0200221 return PHYTEC_EEPROM_INVAL;
222
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700223 api2 = &data->payload.data.data_api2;
Teresa Remmete3d3ac42023-08-17 10:57:10 +0200224
225 return api2->pcb_rev;
226}
Yannic Moog5dd7f7c2023-12-20 09:45:35 +0100227
Benjamin Hahn455dcf72024-03-06 17:18:31 +0100228u8 __maybe_unused phytec_get_som_type(struct phytec_eeprom_data *data)
229{
230 if (!data)
231 data = &eeprom_data;
Yannic Moog51b9d532024-04-19 08:55:38 -0700232
233 if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2)
Benjamin Hahn455dcf72024-03-06 17:18:31 +0100234 return PHYTEC_EEPROM_INVAL;
235
Yannic Moogdb2dfb02024-04-19 08:55:37 -0700236 return data->payload.data.data_api2.som_type;
Benjamin Hahn455dcf72024-03-06 17:18:31 +0100237}
238
Daniel Schultza440ec22024-04-19 08:55:36 -0700239#if IS_ENABLED(CONFIG_CMD_EXTENSION)
240struct extension *phytec_add_extension(const char *name, const char *overlay,
241 const char *other)
242{
243 struct extension *extension;
244
245 if (strlen(overlay) > sizeof(extension->overlay)) {
246 pr_err("Overlay name %s is longer than %lu.\n", overlay,
247 sizeof(extension->overlay));
248 return NULL;
249 }
250
251 extension = calloc(1, sizeof(struct extension));
252 snprintf(extension->name, sizeof(extension->name), name);
253 snprintf(extension->overlay, sizeof(extension->overlay), overlay);
254 snprintf(extension->other, sizeof(extension->other), other);
255 snprintf(extension->owner, sizeof(extension->owner), "PHYTEC");
256
257 return extension;
258}
259#endif /* IS_ENABLED(CONFIG_CMD_EXTENSION) */
260
Yannic Moog5dd7f7c2023-12-20 09:45:35 +0100261#else
262
263inline int phytec_eeprom_data_setup(struct phytec_eeprom_data *data,
264 int bus_num, int addr)
265{
266 return PHYTEC_EEPROM_INVAL;
267}
268
269inline int phytec_eeprom_data_setup_fallback(struct phytec_eeprom_data *data,
270 int bus_num, int addr,
271 int addr_fallback)
272{
273 return PHYTEC_EEPROM_INVAL;
274}
275
276inline int phytec_eeprom_data_init(struct phytec_eeprom_data *data,
277 int bus_num, int addr)
278{
279 return PHYTEC_EEPROM_INVAL;
280}
281
282inline void __maybe_unused phytec_print_som_info(struct phytec_eeprom_data *data)
283{
284}
285
286inline char *__maybe_unused phytec_get_opt(struct phytec_eeprom_data *data)
287{
288 return NULL;
289}
290
291u8 __maybe_unused phytec_get_rev(struct phytec_eeprom_data *data)
292{
293 return PHYTEC_EEPROM_INVAL;
294}
295
Benjamin Hahn69ea0e32024-03-12 10:39:11 +0100296u8 __maybe_unused phytec_get_som_type(struct phytec_eeprom_data *data)
297{
298 return PHYTEC_EEPROM_INVAL;
299}
Daniel Schultza440ec22024-04-19 08:55:36 -0700300
301#if IS_ENABLED(CONFIG_CMD_EXTENSION)
302inline struct extension *phytec_add_extension(const char *name,
303 const char *overlay,
304 const char *other)
305{
306 return NULL;
307}
308#endif /* IS_ENABLED(CONFIG_CMD_EXTENSION) */
Benjamin Hahn69ea0e32024-03-12 10:39:11 +0100309
Yannic Moog5dd7f7c2023-12-20 09:45:35 +0100310#endif /* IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION) */