blob: db429870102dc24f36330af30eaf0c29f8d34e2d [file] [log] [blame]
Michael Jones84d7a012011-11-04 13:53:44 -04001/*
2 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
3 * York Sun (yorksun@freescale.com)
4 * Haiying Wang (haiying.wang@freescale.com)
5 * Timur Tabi (timur@freescale.com)
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Michael Jones84d7a012011-11-04 13:53:44 -04008 */
9
10#include <common.h>
11#include <command.h>
12#include <i2c.h>
13
14/* #define DEBUG */
15
16/*
17 * static eeprom: EEPROM layout
18 */
19static struct __attribute__ ((__packed__)) eeprom {
20 u8 id[16]; /* 0x01 - 0x0F Type e.g. 100wG-5111 */
21 u8 sn[10]; /* 0x10 - 0x19 Serial Number */
22 u8 date[6]; /* 0x1A - 0x1F Build Date */
23 u8 mac[6]; /* 0x20 - 0x25 MAC address */
24 u8 reserved[10];/* 0x26 - 0x2f reserved */
25 u32 crc; /* x+1 CRC32 checksum */
26} e;
27
28/* Set to 1 if we've read EEPROM into memory */
29static int has_been_read;
30
31/**
32 * show_eeprom - display the contents of the EEPROM
33 */
34static void show_eeprom(void)
35{
36 unsigned int crc;
37 char safe_string[16];
38
39#ifdef DEBUG
40 int i;
41#endif
42 u8 *p;
43
44 /* ID */
45 strncpy(safe_string, (char *)e.id, sizeof(e.id));
46 safe_string[sizeof(e.id)-1] = 0;
47 printf("ID: mvBlueLYNX-X%s\n", safe_string);
48
49 /* Serial number */
50 strncpy(safe_string, (char *)e.sn, sizeof(e.sn));
51 safe_string[sizeof(e.sn)-1] = 0;
52 printf("SN: %s\n", safe_string);
53
54 /* Build date, BCD date values, as YYMMDDhhmmss */
55 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
56 e.date[0], e.date[1], e.date[2],
57 e.date[3] & 0x7F, e.date[4], e.date[5],
58 e.date[3] & 0x80 ? "PM" : "");
59
60 /* Show MAC address */
61 p = e.mac;
62 printf("Eth: %02x:%02x:%02x:%02x:%02x:%02x\n",
63 p[0], p[1], p[2], p[3], p[4], p[5]);
64
65 crc = crc32(0, (void *)&e, sizeof(e) - 4);
66
67 if (crc == be32_to_cpu(e.crc))
68 printf("CRC: %08x\n", be32_to_cpu(e.crc));
69 else
70 printf("CRC: %08x (should be %08x)\n", be32_to_cpu(e.crc), crc);
71
72#ifdef DEBUG
73 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
74 for (i = 0; i < sizeof(e); i++) {
75 if ((i % 16) == 0)
76 printf("%02X: ", i);
77 printf("%02X ", ((u8 *)&e)[i]);
78 if (((i % 16) == 15) || (i == sizeof(e) - 1))
79 printf("\n");
80 }
81#endif
82}
83
84/**
85 * read_eeprom - read the EEPROM into memory
86 */
87static int read_eeprom(void)
88{
89 int ret;
90#ifdef CONFIG_SYS_EEPROM_BUS_NUM
91 unsigned int bus;
92#endif
93
94 if (has_been_read)
95 return 0;
96
97#ifdef CONFIG_SYS_EEPROM_BUS_NUM
98 bus = i2c_get_bus_num();
99 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
100#endif
101
102 ret = eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
103 (uchar *)&e, sizeof(e));
104
105#ifdef CONFIG_SYS_EEPROM_BUS_NUM
106 i2c_set_bus_num(bus);
107#endif
108
109#ifdef DEBUG
110 show_eeprom();
111#endif
112
113 has_been_read = (ret == 0) ? 1 : 0;
114
115 return ret;
116}
117
118/**
119 * update_crc - update the CRC
120 *
121 * This function should be called after each update to the EEPROM structure,
122 * to make sure the CRC is always correct.
123 */
124static void update_crc(void)
125{
126 u32 crc;
127
128 crc = crc32(0, (void *)&e, sizeof(e) - 4);
129 e.crc = cpu_to_be32(crc);
130}
131
132/**
133 * prog_eeprom - write the EEPROM from memory
134 */
135static int prog_eeprom(void)
136{
137 int ret = 0;
138#ifdef CONFIG_SYS_EEPROM_BUS_NUM
139 unsigned int bus;
140#endif
141
142 update_crc();
143
144#ifdef CONFIG_SYS_EEPROM_BUS_NUM
145 bus = i2c_get_bus_num();
146 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
147#endif
148
149 ret = eeprom_write(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
150 (uchar *)&e, sizeof(e));
151
152 if (!ret) {
153 /* Verify the write by reading back the EEPROM and comparing */
154 struct eeprom e2;
155#ifdef DEBUG
156 printf("%s verifying...\n", __func__);
157#endif
158 ret = eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
159 (uchar *)&e2, sizeof(e2));
160
161 if (!ret && memcmp(&e, &e2, sizeof(e)))
162 ret = -1;
163 }
164
165#ifdef CONFIG_SYS_EEPROM_BUS_NUM
166 i2c_set_bus_num(bus);
167#endif
168
169 if (ret) {
170 printf("Programming failed.\n");
171 has_been_read = 0;
172 return -1;
173 }
174
175 printf("Programming passed.\n");
176 return 0;
177}
178
179/**
180 * h2i - converts hex character into a number
181 *
182 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
183 * the integer equivalent.
184 */
185static inline u8 h2i(char p)
186{
187 if ((p >= '0') && (p <= '9'))
188 return p - '0';
189
190 if ((p >= 'A') && (p <= 'F'))
191 return (p - 'A') + 10;
192
193 if ((p >= 'a') && (p <= 'f'))
194 return (p - 'a') + 10;
195
196 return 0;
197}
198
199/**
200 * set_date - stores the build date into the EEPROM
201 *
202 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
203 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
204 * and stores it in the build date field of the EEPROM local copy.
205 */
206static void set_date(const char *string)
207{
208 unsigned int i;
209
210 if (strlen(string) != 12) {
211 printf("Usage: mac date YYMMDDhhmmss\n");
212 return;
213 }
214
215 for (i = 0; i < 6; i++)
216 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
217
218 update_crc();
219}
220
221/**
222 * set_mac_address - stores a MAC address into the EEPROM
223 *
224 * This function takes a pointer to MAC address string
225 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
226 * stores it in the MAC address field in the EEPROM local copy.
227 */
228static void set_mac_address(const char *string)
229{
230 char *p = (char *) string;
231 unsigned int i;
232
233 for (i = 0; *p && (i < 6); i++) {
234 e.mac[i] = simple_strtoul(p, &p, 16);
235 if (*p == ':')
236 p++;
237 }
238
239 update_crc();
240}
241
242int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
243{
244 char cmd;
245
246 if (argc == 1) {
247 show_eeprom();
248 return 0;
249 }
250
251 cmd = argv[1][0];
252
253 if (cmd == 'r') {
254#ifdef DEBUG
255 printf("%s read\n", __func__);
256#endif
257 read_eeprom();
258 return 0;
259 }
260
261 if (argc == 2) {
262 switch (cmd) {
263 case 's': /* save */
264#ifdef DEBUG
265 printf("%s save\n", __func__);
266#endif
267 prog_eeprom();
268 break;
269 default:
270 return cmd_usage(cmdtp);
271 }
272
273 return 0;
274 }
275
276 /* We know we have at least one parameter */
277
278 switch (cmd) {
279 case 'n': /* serial number */
280#ifdef DEBUG
281 printf("%s serial number\n", __func__);
282#endif
283 memset(e.sn, 0, sizeof(e.sn));
284 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
285 update_crc();
286 break;
287 case 'd': /* date BCD format YYMMDDhhmmss */
288 set_date(argv[2]);
289 break;
290 case 'e': /* errata */
291 printf("mac errata not implemented\n");
292 break;
293 case 'i': /* id */
294 memset(e.id, 0, sizeof(e.id));
295 strncpy((char *)e.id, argv[2], sizeof(e.id) - 1);
296 update_crc();
297 break;
298 case 'p': /* ports */
299 printf("mac ports not implemented (always 1 port)\n");
300 break;
301 case '0' ... '9':
302 /* we only have "mac 0" but any digit can be used here */
303 set_mac_address(argv[2]);
304 break;
305 case 'h': /* help */
306 default:
307 return cmd_usage(cmdtp);
308 }
309
310 return 0;
311}
312
Michael Jones71c4ae32013-02-07 23:53:36 +0000313static inline int is_portrait(void)
314{
315 int i;
316 unsigned int orient_index = 0; /* idx of char which determines orientation */
317
318 for (i = sizeof(e.id)/sizeof(*e.id) - 1; i>=0; i--) {
319 if (e.id[i] == '-') {
320 orient_index = i+1;
321 break;
322 }
323 }
324
325 return (orient_index &&
326 (e.id[orient_index] >= '5') && (e.id[orient_index] <= '8'));
327}
328
Michael Jones84d7a012011-11-04 13:53:44 -0400329int mac_read_from_eeprom(void)
330{
331 u32 crc, crc_offset = offsetof(struct eeprom, crc);
332 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
Michael Jones71c4ae32013-02-07 23:53:36 +0000333#define FILENAME_LANDSCAPE "mvBlueLynx_X.rbf"
334#define FILENAME_PORTRAIT "mvBlueLynx_X_sensor_cd.rbf"
Michael Jones84d7a012011-11-04 13:53:44 -0400335
336 if (read_eeprom()) {
337 printf("EEPROM Read failed.\n");
338 return -1;
339 }
340
341 crc = crc32(0, (void *)&e, crc_offset);
342 crcp = (void *)&e + crc_offset;
343 if (crc != be32_to_cpu(*crcp)) {
344 printf("EEPROM CRC mismatch (%08x != %08x)\n", crc,
345 be32_to_cpu(e.crc));
346 return -1;
347 }
348
349 if (memcmp(&e.mac, "\0\0\0\0\0\0", 6) &&
350 memcmp(&e.mac, "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
Wolfgang Denkcc87d182014-11-06 14:03:02 +0100351 char ethaddr[18];
Michael Jones84d7a012011-11-04 13:53:44 -0400352
353 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
354 e.mac[0],
355 e.mac[1],
356 e.mac[2],
357 e.mac[3],
358 e.mac[4],
359 e.mac[5]);
360 /* Only initialize environment variables that are blank
361 * (i.e. have not yet been set)
362 */
363 if (!getenv("ethaddr"))
364 setenv("ethaddr", ethaddr);
365 }
366
367 if (memcmp(&e.sn, "\0\0\0\0\0\0\0\0\0\0", 10) &&
368 memcmp(&e.sn, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10)) {
369 char serial_num[12];
370
371 strncpy(serial_num, (char *)e.sn, sizeof(e.sn) - 1);
372 /* Only initialize environment variables that are blank
373 * (i.e. have not yet been set)
374 */
375 if (!getenv("serial#"))
376 setenv("serial#", serial_num);
377 }
378
Michael Jones71c4ae32013-02-07 23:53:36 +0000379 /* decide which fpga file to load depending on orientation */
380 if (is_portrait())
381 setenv("fpgafilename", FILENAME_PORTRAIT);
382 else
383 setenv("fpgafilename", FILENAME_LANDSCAPE);
384
Michael Jones84d7a012011-11-04 13:53:44 -0400385 /* TODO should I calculate CRC here? */
386 return 0;
387}
388
389#ifdef CONFIG_SERIAL_TAG
390void get_board_serial(struct tag_serialnr *serialnr)
391{
392 char *serial = getenv("serial#");
393
394 if (serial && (strlen(serial) > 3)) {
395 /* use the numerical part of the serial number LXnnnnnn */
396 serialnr->high = 0;
397 serialnr->low = simple_strtoul(serial + 2, NULL, 10);
398 } else {
399 serialnr->high = 0;
400 serialnr->low = 0;
401 }
402}
403#endif