blob: afaabf3487933f1b7975ea9bb1112717e6a39892 [file] [log] [blame]
Tim Harveyd4daeaa2022-04-13 08:56:40 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 Gateworks Corporation
4 */
5
Tim Harveyd4daeaa2022-04-13 08:56:40 -07006#include <gsc.h>
7#include <hexdump.h>
8#include <i2c.h>
9#include <dm/uclass.h>
10
11#include "eeprom.h"
12
13/* I2C */
14#define SOM_EEPROM_BUSNO 0
15#define SOM_EEPROM_ADDR 0x51
16#define BASEBOARD_EEPROM_BUSNO 1
17#define BASEBOARD_EEPROM_ADDR 0x52
18
19struct venice_board_info som_info;
20struct venice_board_info base_info;
21char venice_model[32];
Tim Harveya6614bb2022-08-11 11:55:38 -070022char venice_baseboard_model[32];
Tim Harveyd4daeaa2022-04-13 08:56:40 -070023u32 venice_serial;
24
25/* return a mac address from EEPROM info */
26int eeprom_getmac(int index, uint8_t *address)
27{
28 int i, j;
29 u32 maclow, machigh;
30 u64 mac;
31
32 j = 0;
33 if (som_info.macno) {
34 maclow = som_info.mac[5];
35 maclow |= som_info.mac[4] << 8;
36 maclow |= som_info.mac[3] << 16;
37 maclow |= som_info.mac[2] << 24;
38 machigh = som_info.mac[1];
39 machigh |= som_info.mac[0] << 8;
40 mac = machigh;
41 mac <<= 32;
42 mac |= maclow;
43 for (i = 0; i < som_info.macno; i++, j++) {
44 if (index == j)
45 goto out;
46 }
47 }
48
49 maclow = base_info.mac[5];
50 maclow |= base_info.mac[4] << 8;
51 maclow |= base_info.mac[3] << 16;
52 maclow |= base_info.mac[2] << 24;
53 machigh = base_info.mac[1];
54 machigh |= base_info.mac[0] << 8;
55 mac = machigh;
56 mac <<= 32;
57 mac |= maclow;
58 for (i = 0; i < base_info.macno; i++, j++) {
59 if (index == j)
60 goto out;
61 }
62
63 return -EINVAL;
64
65out:
66 mac += i;
67 address[0] = (mac >> 40) & 0xff;
68 address[1] = (mac >> 32) & 0xff;
69 address[2] = (mac >> 24) & 0xff;
70 address[3] = (mac >> 16) & 0xff;
71 address[4] = (mac >> 8) & 0xff;
72 address[5] = (mac >> 0) & 0xff;
73
74 return 0;
75}
76
77static int eeprom_read(int busno, int slave, int alen, struct venice_board_info *info)
78{
79 int i;
80 int chksum;
81 unsigned char *buf = (unsigned char *)info;
82 struct udevice *dev, *bus;
83 int ret;
84
85 /* probe device */
86 ret = uclass_get_device_by_seq(UCLASS_I2C, busno, &bus);
87 if (ret)
88 return ret;
89 ret = dm_i2c_probe(bus, slave, 0, &dev);
90 if (ret)
91 return ret;
92
93 /* read eeprom config section */
94 memset(info, 0, sizeof(*info));
95 ret = i2c_set_chip_offset_len(dev, alen);
96 if (ret) {
97 puts("EEPROM: Failed to set alen\n");
98 return ret;
99 }
100 ret = dm_i2c_read(dev, 0x00, buf, sizeof(*info));
101 if (ret) {
102 if (slave == SOM_EEPROM_ADDR)
103 printf("EEPROM: Failed to read EEPROM\n");
104 return ret;
105 }
106
107 /* validate checksum */
108 for (chksum = 0, i = 0; i < (int)sizeof(*info) - 2; i++)
109 chksum += buf[i];
110 if ((info->chksum[0] != chksum >> 8) ||
111 (info->chksum[1] != (chksum & 0xff))) {
112 printf("EEPROM: I2C%d@0x%02x: Invalid Checksum\n", busno, slave);
113 print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, sizeof(*info));
114 memset(info, 0, sizeof(*info));
115 return -EINVAL;
116 }
117
118 /* sanity check valid model */
119 if (info->model[0] != 'G' || info->model[1] != 'W') {
120 printf("EEPROM: I2C%d@0x%02x: Invalid Model in EEPROM\n", busno, slave);
121 print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, sizeof(*info));
122 memset(info, 0, sizeof(*info));
123 return -EINVAL;
124 }
125
126 return 0;
127}
128
129/* determine BOM revision from model */
130int get_bom_rev(const char *str)
131{
132 int rev_bom = 0;
133 int i;
134
135 for (i = strlen(str) - 1; i > 0; i--) {
136 if (str[i] == '-')
137 break;
138 if (str[i] >= '1' && str[i] <= '9') {
139 rev_bom = str[i] - '0';
140 break;
141 }
142 }
143 return rev_bom;
144}
145
146/* determine PCB revision from model */
147char get_pcb_rev(const char *str)
148{
149 char rev_pcb = 'A';
150 int i;
151
152 for (i = strlen(str) - 1; i > 0; i--) {
153 if (str[i] == '-')
154 break;
155 if (str[i] >= 'A') {
156 rev_pcb = str[i];
157 break;
158 }
159 }
160 return rev_pcb;
161}
162
163/*
164 * get dt name based on model and detail level:
165 *
166 * For boards that are a combination of a SoM plus a Baseboard:
167 * Venice SoM part numbers are GW70xx where xx is:
168 * 7000-7019: same PCB with som dt of '0x'
169 * 7020-7039: same PCB with som dt of '2x'
170 * 7040-7059: same PCB with som dt of '4x'
171 * 7060-7079: same PCB with som dt of '6x'
172 * 7080-7099: same PCB with som dt of '8x'
173 * Venice Baseboard part numbers are GW7xxx where xxx is:
174 * 7100-7199: same PCB with base dt of '71xx'
175 * 7200-7299: same PCB with base dt of '72xx'
176 * 7300-7399: same PCB with base dt of '73xx'
177 * 7400-7499: same PCB with base dt of '74xx'
178 * 7500-7599: same PCB with base dt of '75xx'
179 * 7600-7699: same PCB with base dt of '76xx'
180 * 7700-7799: same PCB with base dt of '77xx'
181 * 7800-7899: same PCB with base dt of '78xx'
182 * DT name is comprised of:
183 * gw<base dt>-<som dt>-[base-pcb-rev][base-bom-rev][som-pcb-rev][som-bom-rev]
184 *
185 * For board models from 7900-7999 each PCB is unique with its own dt:
186 * DT name is comprised:
187 * gw<model>-[pcb-rev][bom-rev]
188 *
189 */
190#define snprintfcat(dest, sz, fmt, ...) \
191 snprintf((dest) + strlen(dest), (sz) - strlen(dest), fmt, ##__VA_ARGS__)
192const char *eeprom_get_dtb_name(int level, char *buf, int sz)
193{
194#ifdef CONFIG_IMX8MM
195 const char *pre = "imx8mm-venice-gw";
Tim Harvey0f5717f2022-04-13 11:31:09 -0700196#elif CONFIG_IMX8MN
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700197 const char *pre = "imx8mn-venice-gw";
Tim Harvey0f5717f2022-04-13 11:31:09 -0700198#elif CONFIG_IMX8MP
199 const char *pre = "imx8mp-venice-gw";
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700200#endif
201 int model, rev_pcb, rev_bom;
202
203 model = ((som_info.model[2] - '0') * 1000)
204 + ((som_info.model[3] - '0') * 100)
205 + ((som_info.model[4] - '0') * 10)
206 + (som_info.model[5] - '0');
207 rev_pcb = tolower(get_pcb_rev(som_info.model));
208 rev_bom = get_bom_rev(som_info.model);
209
210 /* som + baseboard*/
211 if (base_info.model[0]) {
212 /* baseboard id: 7100-7199->71; 7200-7299->72; etc */
213 int base = ((base_info.model[2] - '0') * 10) + (base_info.model[3] - '0');
214 /* som id: 7000-7019->1; 7020-7039->2; etc */
215 int som = ((model % 100) / 20) * 2;
216 int rev_base_pcb = tolower(get_pcb_rev(base_info.model));
217 int rev_base_bom = get_bom_rev(base_info.model);
218
219 snprintf(buf, sz, "%s%2dxx-%dx", pre, base, som);
Tim Harvey08ac93c2023-06-09 09:54:51 -0700220 /* GW79xx baseboards have no build options */
221 if (base == 79) {
222 base = (int)strtoul(base_info.model + 2, NULL, 10);
223 snprintf(buf, sz, "%s%4d-%dx", pre, base, som);
224 }
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700225 switch (level) {
226 case 0: /* full model (ie gw73xx-0x-a1a1) */
227 if (rev_base_bom)
228 snprintfcat(buf, sz, "-%c%d", rev_base_pcb, rev_base_bom);
229 else
230 snprintfcat(buf, sz, "-%c", rev_base_pcb);
231 if (rev_bom)
232 snprintfcat(buf, sz, "%c%d", rev_pcb, rev_bom);
233 else
234 snprintfcat(buf, sz, "%c", rev_pcb);
235 break;
236 case 1: /* don't care about SoM revision */
237 if (rev_base_bom)
238 snprintfcat(buf, sz, "-%c%d", rev_base_pcb, rev_base_bom);
239 else
240 snprintfcat(buf, sz, "-%c", rev_base_pcb);
241 snprintfcat(buf, sz, "xx");
242 break;
243 case 2: /* don't care about baseboard revision */
244 snprintfcat(buf, sz, "-xx");
245 if (rev_bom)
246 snprintfcat(buf, sz, "%c%d", rev_pcb, rev_bom);
247 else
248 snprintfcat(buf, sz, "%c", rev_pcb);
249 break;
250 case 3: /* don't care about SoM/baseboard revision */
251 break;
252 default:
253 return NULL;
254 }
255 } else {
256 snprintf(buf, sz, "%s%04d", pre, model);
257 switch (level) {
258 case 0: /* full model wth PCB and BOM revision first (ie gw7901-a1) */
259 if (rev_bom)
260 snprintfcat(buf, sz, "-%c%d", rev_pcb, rev_bom);
261 else
262 snprintfcat(buf, sz, "-%c", rev_pcb);
263 break;
264 case 1: /* don't care about BOM revision */
265 snprintfcat(buf, sz, "-%c", rev_pcb);
266 break;
267 case 2: /* don't care about PCB or BOM revision */
268 break;
Tim Harvey4efcc642022-04-13 11:31:08 -0700269 case 3: /* don't care about last digit of model */
270 buf[strlen(buf) - 1] = 'x';
271 break;
272 case 4: /* don't care about last two digits of model */
273 buf[strlen(buf) - 1] = 'x';
274 buf[strlen(buf) - 2] = 'x';
275 break;
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700276 default:
277 return NULL;
Tim Harvey4efcc642022-04-13 11:31:08 -0700278 break;
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700279 }
280 }
281
282 return buf;
283}
284
285static int eeprom_info(bool verbose)
286{
287 printf("Model : %s\n", venice_model);
288 printf("Serial : %d\n", som_info.serial);
289 printf("MFGDate : %02x-%02x-%02x%02x\n",
290 som_info.mfgdate[0], som_info.mfgdate[1],
291 som_info.mfgdate[2], som_info.mfgdate[3]);
292 if (base_info.model[0] && verbose) {
293 printf("SOM : %s %d %02x-%02x-%02x%02x\n",
294 som_info.model, som_info.serial,
295 som_info.mfgdate[0], som_info.mfgdate[1],
296 som_info.mfgdate[2], som_info.mfgdate[3]);
297 printf("BASE : %s %d %02x-%02x-%02x%02x\n",
298 base_info.model, base_info.serial,
299 base_info.mfgdate[0], base_info.mfgdate[1],
300 base_info.mfgdate[2], base_info.mfgdate[3]);
301 }
302
303 return 0;
304}
305
Tim Harvey1fec1822022-08-11 12:04:01 -0700306int venice_eeprom_init(int quiet)
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700307{
308 char rev_pcb;
309 int rev_bom;
310 int ret;
311
312 ret = eeprom_read(SOM_EEPROM_BUSNO, SOM_EEPROM_ADDR, 1, &som_info);
313 if (ret) {
314 puts("ERROR: Failed to probe EEPROM\n");
315 memset(&som_info, 0, sizeof(som_info));
316 return 0;
317 }
318
319 /* read optional baseboard EEPROM */
320 eeprom_read(BASEBOARD_EEPROM_BUSNO, BASEBOARD_EEPROM_ADDR, 2, &base_info);
321
322 /* create model strings */
323 if (base_info.model[0]) {
324 sprintf(venice_model, "GW%c%c%c%c-%c%c-",
325 som_info.model[2], /* family */
326 base_info.model[3], /* baseboard */
327 base_info.model[4], base_info.model[5], /* subload of baseboard */
328 som_info.model[4], som_info.model[5]); /* last 2digits of SOM */
Tim Harveya6614bb2022-08-11 11:55:38 -0700329 strlcpy(venice_baseboard_model, base_info.model, sizeof(venice_baseboard_model));
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700330
331 /* baseboard revision */
332 rev_pcb = get_pcb_rev(base_info.model);
333 rev_bom = get_bom_rev(base_info.model);
334 if (rev_bom)
335 sprintf(venice_model + strlen(venice_model), "%c%d", rev_pcb, rev_bom);
336 else
337 sprintf(venice_model + strlen(venice_model), "%c", rev_pcb);
338 /* som revision */
339 rev_pcb = get_pcb_rev(som_info.model);
340 rev_bom = get_bom_rev(som_info.model);
341 if (rev_bom)
342 sprintf(venice_model + strlen(venice_model), "%c%d", rev_pcb, rev_bom);
343 else
344 sprintf(venice_model + strlen(venice_model), "%c", rev_pcb);
345 } else {
346 strcpy(venice_model, som_info.model);
347 }
348 venice_serial = som_info.serial;
349
350 if (!quiet)
351 eeprom_info(false);
352
353 return (16 << som_info.sdram_size);
354}
355
356void board_gsc_info(void)
357{
358 eeprom_info(true);
359}
360
361const char *eeprom_get_model(void)
362{
363 return venice_model;
364}
365
Tim Harveya6614bb2022-08-11 11:55:38 -0700366const char *eeprom_get_baseboard_model(void)
367{
368 return venice_baseboard_model;
369}
370
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700371u32 eeprom_get_serial(void)
372{
373 return venice_serial;
374}