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)
7 * SPDX-License-Identifier: GPL-2.0+
17 * static eeprom: EEPROM layout
19 static 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 */
28 /* Set to 1 if we've read EEPROM into memory */
29 static int has_been_read
;
32 * show_eeprom - display the contents of the EEPROM
34 static void show_eeprom(void)
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
);
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
);
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" : "");
60 /* Show MAC address */
62 printf("Eth: %02x:%02x:%02x:%02x:%02x:%02x\n",
63 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5]);
65 crc
= crc32(0, (void *)&e
, sizeof(e
) - 4);
67 if (crc
== be32_to_cpu(e
.crc
))
68 printf("CRC: %08x\n", be32_to_cpu(e
.crc
));
70 printf("CRC: %08x (should be %08x)\n", be32_to_cpu(e
.crc
), crc
);
73 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e
));
74 for (i
= 0; i
< sizeof(e
); i
++) {
77 printf("%02X ", ((u8
*)&e
)[i
]);
78 if (((i
% 16) == 15) || (i
== sizeof(e
) - 1))
85 * read_eeprom - read the EEPROM into memory
87 static int read_eeprom(void)
90 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
97 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
98 bus
= i2c_get_bus_num();
99 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM
);
102 ret
= eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR
, 0,
103 (uchar
*)&e
, sizeof(e
));
105 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
106 i2c_set_bus_num(bus
);
113 has_been_read
= (ret
== 0) ? 1 : 0;
119 * update_crc - update the CRC
121 * This function should be called after each update to the EEPROM structure,
122 * to make sure the CRC is always correct.
124 static void update_crc(void)
128 crc
= crc32(0, (void *)&e
, sizeof(e
) - 4);
129 e
.crc
= cpu_to_be32(crc
);
133 * prog_eeprom - write the EEPROM from memory
135 static int prog_eeprom(void)
138 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
144 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
145 bus
= i2c_get_bus_num();
146 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM
);
149 ret
= eeprom_write(CONFIG_SYS_I2C_EEPROM_ADDR
, 0,
150 (uchar
*)&e
, sizeof(e
));
153 /* Verify the write by reading back the EEPROM and comparing */
156 printf("%s verifying...\n", __func__
);
158 ret
= eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR
, 0,
159 (uchar
*)&e2
, sizeof(e2
));
161 if (!ret
&& memcmp(&e
, &e2
, sizeof(e
)))
165 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
166 i2c_set_bus_num(bus
);
170 printf("Programming failed.\n");
175 printf("Programming passed.\n");
180 * h2i - converts hex character into a number
182 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
183 * the integer equivalent.
185 static inline u8
h2i(char p
)
187 if ((p
>= '0') && (p
<= '9'))
190 if ((p
>= 'A') && (p
<= 'F'))
191 return (p
- 'A') + 10;
193 if ((p
>= 'a') && (p
<= 'f'))
194 return (p
- 'a') + 10;
200 * set_date - stores the build date into the EEPROM
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.
206 static void set_date(const char *string
)
210 if (strlen(string
) != 12) {
211 printf("Usage: mac date YYMMDDhhmmss\n");
215 for (i
= 0; i
< 6; i
++)
216 e
.date
[i
] = h2i(string
[2 * i
]) << 4 | h2i(string
[2 * i
+ 1]);
222 * set_mac_address - stores a MAC address into the EEPROM
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.
228 static void set_mac_address(const char *string
)
230 char *p
= (char *) string
;
233 for (i
= 0; *p
&& (i
< 6); i
++) {
234 e
.mac
[i
] = simple_strtoul(p
, &p
, 16);
242 int do_mac(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
255 printf("%s read\n", __func__
);
265 printf("%s save\n", __func__
);
270 return cmd_usage(cmdtp
);
276 /* We know we have at least one parameter */
279 case 'n': /* serial number */
281 printf("%s serial number\n", __func__
);
283 memset(e
.sn
, 0, sizeof(e
.sn
));
284 strncpy((char *)e
.sn
, argv
[2], sizeof(e
.sn
) - 1);
287 case 'd': /* date BCD format YYMMDDhhmmss */
290 case 'e': /* errata */
291 printf("mac errata not implemented\n");
294 memset(e
.id
, 0, sizeof(e
.id
));
295 strncpy((char *)e
.id
, argv
[2], sizeof(e
.id
) - 1);
298 case 'p': /* ports */
299 printf("mac ports not implemented (always 1 port)\n");
302 /* we only have "mac 0" but any digit can be used here */
303 set_mac_address(argv
[2]);
307 return cmd_usage(cmdtp
);
313 static inline int is_portrait(void)
316 unsigned int orient_index
= 0; /* idx of char which determines orientation */
318 for (i
= sizeof(e
.id
)/sizeof(*e
.id
) - 1; i
>=0; i
--) {
319 if (e
.id
[i
] == '-') {
325 return (orient_index
&&
326 (e
.id
[orient_index
] >= '5') && (e
.id
[orient_index
] <= '8'));
329 int mac_read_from_eeprom(void)
331 u32 crc
, crc_offset
= offsetof(struct eeprom
, crc
);
332 u32
*crcp
; /* Pointer to the CRC in the data read from the EEPROM */
333 #define FILENAME_LANDSCAPE "mvBlueLynx_X.rbf"
334 #define FILENAME_PORTRAIT "mvBlueLynx_X_sensor_cd.rbf"
337 printf("EEPROM Read failed.\n");
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
,
349 if (memcmp(&e
.mac
, "\0\0\0\0\0\0", 6) &&
350 memcmp(&e
.mac
, "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
353 sprintf(ethaddr
, "%02X:%02X:%02X:%02X:%02X:%02X",
360 /* Only initialize environment variables that are blank
361 * (i.e. have not yet been set)
363 if (!getenv("ethaddr"))
364 setenv("ethaddr", ethaddr
);
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)) {
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)
375 if (!getenv("serial#"))
376 setenv("serial#", serial_num
);
379 /* decide which fpga file to load depending on orientation */
381 setenv("fpgafilename", FILENAME_PORTRAIT
);
383 setenv("fpgafilename", FILENAME_LANDSCAPE
);
385 /* TODO should I calculate CRC here? */
389 #ifdef CONFIG_SERIAL_TAG
390 void get_board_serial(struct tag_serialnr
*serialnr
)
392 char *serial
= getenv("serial#");
394 if (serial
&& (strlen(serial
) > 3)) {
395 /* use the numerical part of the serial number LXnnnnnn */
397 serialnr
->low
= simple_strtoul(serial
+ 2, NULL
, 10);