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 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
33 * static eeprom: EEPROM layout
35 static struct __attribute__ ((__packed__)) eeprom {
36 u8 id[16]; /* 0x01 - 0x0F Type e.g. 100wG-5111 */
37 u8 sn[10]; /* 0x10 - 0x19 Serial Number */
38 u8 date[6]; /* 0x1A - 0x1F Build Date */
39 u8 mac[6]; /* 0x20 - 0x25 MAC address */
40 u8 reserved[10];/* 0x26 - 0x2f reserved */
41 u32 crc; /* x+1 CRC32 checksum */
44 /* Set to 1 if we've read EEPROM into memory */
45 static int has_been_read;
48 * show_eeprom - display the contents of the EEPROM
50 static void show_eeprom(void)
61 strncpy(safe_string, (char *)e.id, sizeof(e.id));
62 safe_string[sizeof(e.id)-1] = 0;
63 printf("ID: mvBlueLYNX-X%s\n", safe_string);
66 strncpy(safe_string, (char *)e.sn, sizeof(e.sn));
67 safe_string[sizeof(e.sn)-1] = 0;
68 printf("SN: %s\n", safe_string);
70 /* Build date, BCD date values, as YYMMDDhhmmss */
71 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
72 e.date[0], e.date[1], e.date[2],
73 e.date[3] & 0x7F, e.date[4], e.date[5],
74 e.date[3] & 0x80 ? "PM" : "");
76 /* Show MAC address */
78 printf("Eth: %02x:%02x:%02x:%02x:%02x:%02x\n",
79 p[0], p[1], p[2], p[3], p[4], p[5]);
81 crc = crc32(0, (void *)&e, sizeof(e) - 4);
83 if (crc == be32_to_cpu(e.crc))
84 printf("CRC: %08x\n", be32_to_cpu(e.crc));
86 printf("CRC: %08x (should be %08x)\n", be32_to_cpu(e.crc), crc);
89 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
90 for (i = 0; i < sizeof(e); i++) {
93 printf("%02X ", ((u8 *)&e)[i]);
94 if (((i % 16) == 15) || (i == sizeof(e) - 1))
101 * read_eeprom - read the EEPROM into memory
103 static int read_eeprom(void)
106 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
113 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
114 bus = i2c_get_bus_num();
115 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
118 ret = eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
119 (uchar *)&e, sizeof(e));
121 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
122 i2c_set_bus_num(bus);
129 has_been_read = (ret == 0) ? 1 : 0;
135 * update_crc - update the CRC
137 * This function should be called after each update to the EEPROM structure,
138 * to make sure the CRC is always correct.
140 static void update_crc(void)
144 crc = crc32(0, (void *)&e, sizeof(e) - 4);
145 e.crc = cpu_to_be32(crc);
149 * prog_eeprom - write the EEPROM from memory
151 static int prog_eeprom(void)
154 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
160 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
161 bus = i2c_get_bus_num();
162 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
165 ret = eeprom_write(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
166 (uchar *)&e, sizeof(e));
169 /* Verify the write by reading back the EEPROM and comparing */
172 printf("%s verifying...\n", __func__);
174 ret = eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
175 (uchar *)&e2, sizeof(e2));
177 if (!ret && memcmp(&e, &e2, sizeof(e)))
181 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
182 i2c_set_bus_num(bus);
186 printf("Programming failed.\n");
191 printf("Programming passed.\n");
196 * h2i - converts hex character into a number
198 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
199 * the integer equivalent.
201 static inline u8 h2i(char p)
203 if ((p >= '0') && (p <= '9'))
206 if ((p >= 'A') && (p <= 'F'))
207 return (p - 'A') + 10;
209 if ((p >= 'a') && (p <= 'f'))
210 return (p - 'a') + 10;
216 * set_date - stores the build date into the EEPROM
218 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
219 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
220 * and stores it in the build date field of the EEPROM local copy.
222 static void set_date(const char *string)
226 if (strlen(string) != 12) {
227 printf("Usage: mac date YYMMDDhhmmss\n");
231 for (i = 0; i < 6; i++)
232 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
238 * set_mac_address - stores a MAC address into the EEPROM
240 * This function takes a pointer to MAC address string
241 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
242 * stores it in the MAC address field in the EEPROM local copy.
244 static void set_mac_address(const char *string)
246 char *p = (char *) string;
249 for (i = 0; *p && (i < 6); i++) {
250 e.mac[i] = simple_strtoul(p, &p, 16);
258 int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
271 printf("%s read\n", __func__);
281 printf("%s save\n", __func__);
286 return cmd_usage(cmdtp);
292 /* We know we have at least one parameter */
295 case 'n': /* serial number */
297 printf("%s serial number\n", __func__);
299 memset(e.sn, 0, sizeof(e.sn));
300 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
303 case 'd': /* date BCD format YYMMDDhhmmss */
306 case 'e': /* errata */
307 printf("mac errata not implemented\n");
310 memset(e.id, 0, sizeof(e.id));
311 strncpy((char *)e.id, argv[2], sizeof(e.id) - 1);
314 case 'p': /* ports */
315 printf("mac ports not implemented (always 1 port)\n");
318 /* we only have "mac 0" but any digit can be used here */
319 set_mac_address(argv[2]);
323 return cmd_usage(cmdtp);
329 static inline int is_portrait(void)
332 unsigned int orient_index = 0; /* idx of char which determines orientation */
334 for (i = sizeof(e.id)/sizeof(*e.id) - 1; i>=0; i--) {
335 if (e.id[i] == '-') {
341 return (orient_index &&
342 (e.id[orient_index] >= '5') && (e.id[orient_index] <= '8'));
345 int mac_read_from_eeprom(void)
347 u32 crc, crc_offset = offsetof(struct eeprom, crc);
348 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
349 #define FILENAME_LANDSCAPE "mvBlueLynx_X.rbf"
350 #define FILENAME_PORTRAIT "mvBlueLynx_X_sensor_cd.rbf"
353 printf("EEPROM Read failed.\n");
357 crc = crc32(0, (void *)&e, crc_offset);
358 crcp = (void *)&e + crc_offset;
359 if (crc != be32_to_cpu(*crcp)) {
360 printf("EEPROM CRC mismatch (%08x != %08x)\n", crc,
365 if (memcmp(&e.mac, "\0\0\0\0\0\0", 6) &&
366 memcmp(&e.mac, "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
369 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
376 /* Only initialize environment variables that are blank
377 * (i.e. have not yet been set)
379 if (!getenv("ethaddr"))
380 setenv("ethaddr", ethaddr);
383 if (memcmp(&e.sn, "\0\0\0\0\0\0\0\0\0\0", 10) &&
384 memcmp(&e.sn, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10)) {
387 strncpy(serial_num, (char *)e.sn, sizeof(e.sn) - 1);
388 /* Only initialize environment variables that are blank
389 * (i.e. have not yet been set)
391 if (!getenv("serial#"))
392 setenv("serial#", serial_num);
395 /* decide which fpga file to load depending on orientation */
397 setenv("fpgafilename", FILENAME_PORTRAIT);
399 setenv("fpgafilename", FILENAME_LANDSCAPE);
401 /* TODO should I calculate CRC here? */
405 #ifdef CONFIG_SERIAL_TAG
406 void get_board_serial(struct tag_serialnr *serialnr)
408 char *serial = getenv("serial#");
410 if (serial && (strlen(serial) > 3)) {
411 /* use the numerical part of the serial number LXnnnnnn */
413 serialnr->low = simple_strtoul(serial + 2, NULL, 10);