blob: ab0fe0baf1ecacd426de26502679ea454de955d6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haiying Wang34c68782006-07-12 10:48:05 -04002/*
Timur Tabia2b41cb2011-02-09 02:00:09 +00003 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
Haiying Wang34c68782006-07-12 10:48:05 -04004 * York Sun (yorksun@freescale.com)
5 * Haiying Wang (haiying.wang@freescale.com)
Timur Tabibdea8372008-06-19 17:56:11 -05006 * Timur Tabi (timur@freescale.com)
Haiying Wang34c68782006-07-12 10:48:05 -04007 */
8
9#include <common.h>
10#include <command.h>
11#include <i2c.h>
12#include <linux/ctype.h>
13
Timur Tabi11e98e72010-09-30 15:36:50 -050014#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -050015#include "../common/eeprom.h"
Timur Tabi11e98e72010-09-30 15:36:50 -050016#define MAX_NUM_PORTS 8
17#endif
Haiying Wang34c68782006-07-12 10:48:05 -040018
Timur Tabi11e98e72010-09-30 15:36:50 -050019#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Shengzhou Liu2fa20052013-09-13 14:46:01 +080020/* some boards with non-256-bytes EEPROM have special define */
21/* for MAX_NUM_PORTS in board-specific file */
22#ifndef MAX_NUM_PORTS
Ebony Zhu575cc912014-04-25 18:38:44 -050023#define MAX_NUM_PORTS 16
Shengzhou Liu2fa20052013-09-13 14:46:01 +080024#endif
Timur Tabi11e98e72010-09-30 15:36:50 -050025#define NXID_VERSION 1
26#endif
Haiying Wange5607542009-06-04 16:12:40 -040027
Timur Tabibdea8372008-06-19 17:56:11 -050028/**
29 * static eeprom: EEPROM layout for CCID or NXID formats
30 *
31 * See application note AN3638 for details.
32 */
33static struct __attribute__ ((__packed__)) eeprom {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020034#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -050035 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
36 u8 major; /* 0x04 Board revision, major */
37 u8 minor; /* 0x05 Board revision, minor */
38 u8 sn[10]; /* 0x06 - 0x0F Serial Number*/
39 u8 errata[2]; /* 0x10 - 0x11 Errata Level */
40 u8 date[6]; /* 0x12 - 0x17 Build Date */
41 u8 res_0[40]; /* 0x18 - 0x3f Reserved */
42 u8 mac_count; /* 0x40 Number of MAC addresses */
43 u8 mac_flag; /* 0x41 MAC table flags */
Haiying Wange5607542009-06-04 16:12:40 -040044 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
Timur Tabibdea8372008-06-19 17:56:11 -050045 u32 crc; /* 0x72 CRC32 checksum */
46#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020047#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -050048 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
49 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
50 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
51 u8 date[6]; /* 0x15 - 0x1a Build Date */
52 u8 res_0; /* 0x1b Reserved */
53 u32 version; /* 0x1c - 0x1f NXID Version */
54 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
55 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
56 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
57 u8 res_1[21]; /* 0x2b - 0x3f Reserved */
58 u8 mac_count; /* 0x40 Number of MAC addresses */
59 u8 mac_flag; /* 0x41 MAC table flags */
Ebony Zhu575cc912014-04-25 18:38:44 -050060 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0xa1 MAC addresses */
61 u8 res_2[90]; /* 0xa2 - 0xfb Reserved */
62 u32 crc; /* 0xfc - 0xff CRC32 checksum */
Timur Tabibdea8372008-06-19 17:56:11 -050063#endif
64} e;
65
66/* Set to 1 if we've read EEPROM into memory */
67static int has_been_read = 0;
68
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020069#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -050070/* Is this a valid NXID EEPROM? */
Kumar Gala4dc62452009-07-03 12:45:44 -050071#define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
72 (e.id[2] == 'I') || (e.id[3] == 'D'))
Timur Tabibdea8372008-06-19 17:56:11 -050073#endif
74
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020075#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -050076/* Is this a valid CCID EEPROM? */
Kumar Gala4dc62452009-07-03 12:45:44 -050077#define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
78 (e.id[2] == 'I') || (e.id[3] == 'D'))
Timur Tabibdea8372008-06-19 17:56:11 -050079#endif
80
81/**
82 * show_eeprom - display the contents of the EEPROM
83 */
84static void show_eeprom(void)
Haiying Wang34c68782006-07-12 10:48:05 -040085{
86 int i;
Timur Tabibdea8372008-06-19 17:56:11 -050087 unsigned int crc;
Haiying Wang34c68782006-07-12 10:48:05 -040088
Timur Tabibdea8372008-06-19 17:56:11 -050089 /* EEPROM tag ID, either CCID or NXID */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020090#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -050091 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
Jaiprakash Singhe1029ef2015-05-28 14:54:03 +053092 be32_to_cpu(e.version));
Timur Tabibdea8372008-06-19 17:56:11 -050093#else
94 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
95#endif
Haiying Wang34cf9592008-01-16 17:12:12 -050096
Timur Tabibdea8372008-06-19 17:56:11 -050097 /* Serial number */
98 printf("SN: %s\n", e.sn);
Haiying Wang34cf9592008-01-16 17:12:12 -050099
Timur Tabibdea8372008-06-19 17:56:11 -0500100 /* Errata level. */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200101#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -0500102 printf("Errata: %s\n", e.errata);
103#else
104 printf("Errata: %c%c\n",
105 e.errata[0] ? e.errata[0] : '.',
106 e.errata[1] ? e.errata[1] : '.');
107#endif
Haiying Wang34cf9592008-01-16 17:12:12 -0500108
Timur Tabibdea8372008-06-19 17:56:11 -0500109 /* Build date, BCD date values, as YYMMDDhhmmss */
110 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
111 e.date[0], e.date[1], e.date[2],
112 e.date[3] & 0x7F, e.date[4], e.date[5],
113 e.date[3] & 0x80 ? "PM" : "");
Haiying Wang34cf9592008-01-16 17:12:12 -0500114
Timur Tabibdea8372008-06-19 17:56:11 -0500115 /* Show MAC addresses */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900116 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
Haiying Wange5607542009-06-04 16:12:40 -0400117
Timur Tabibdea8372008-06-19 17:56:11 -0500118 u8 *p = e.mac[i];
Haiying Wang34cf9592008-01-16 17:12:12 -0500119
Timur Tabibdea8372008-06-19 17:56:11 -0500120 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
121 p[0], p[1], p[2], p[3], p[4], p[5]);
122 }
Haiying Wang34cf9592008-01-16 17:12:12 -0500123
Timur Tabibdea8372008-06-19 17:56:11 -0500124 crc = crc32(0, (void *)&e, sizeof(e) - 4);
Haiying Wang34c68782006-07-12 10:48:05 -0400125
Timur Tabibdea8372008-06-19 17:56:11 -0500126 if (crc == be32_to_cpu(e.crc))
127 printf("CRC: %08x\n", be32_to_cpu(e.crc));
128 else
129 printf("CRC: %08x (should be %08x)\n",
130 be32_to_cpu(e.crc), crc);
Haiying Wang34c68782006-07-12 10:48:05 -0400131
Timur Tabibdea8372008-06-19 17:56:11 -0500132#ifdef DEBUG
133 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
134 for (i = 0; i < sizeof(e); i++) {
135 if ((i % 16) == 0)
136 printf("%02X: ", i);
137 printf("%02X ", ((u8 *)&e)[i]);
138 if (((i % 16) == 15) || (i == sizeof(e) - 1))
139 printf("\n");
140 }
141#endif
Haiying Wang34c68782006-07-12 10:48:05 -0400142}
143
Timur Tabibdea8372008-06-19 17:56:11 -0500144/**
145 * read_eeprom - read the EEPROM into memory
146 */
147static int read_eeprom(void)
Haiying Wang34c68782006-07-12 10:48:05 -0400148{
Timur Tabibdea8372008-06-19 17:56:11 -0500149 int ret;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200150#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500151 unsigned int bus;
152#endif
Haiying Wang34c68782006-07-12 10:48:05 -0400153
Timur Tabibdea8372008-06-19 17:56:11 -0500154 if (has_been_read)
155 return 0;
Haiying Wang34c68782006-07-12 10:48:05 -0400156
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200157#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500158 bus = i2c_get_bus_num();
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200159 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
Timur Tabibdea8372008-06-19 17:56:11 -0500160#endif
161
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200162 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Timur Tabibdea8372008-06-19 17:56:11 -0500163 (void *)&e, sizeof(e));
164
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200165#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500166 i2c_set_bus_num(bus);
167#endif
168
169#ifdef DEBUG
170 show_eeprom();
171#endif
172
173 has_been_read = (ret == 0) ? 1 : 0;
174
175 return ret;
Haiying Wang34c68782006-07-12 10:48:05 -0400176}
177
Timur Tabibdea8372008-06-19 17:56:11 -0500178/**
Timur Tabi13c312b2009-08-28 16:56:45 -0500179 * update_crc - update the CRC
180 *
181 * This function should be called after each update to the EEPROM structure,
182 * to make sure the CRC is always correct.
183 */
184static void update_crc(void)
185{
186 u32 crc;
187
188 crc = crc32(0, (void *)&e, sizeof(e) - 4);
189 e.crc = cpu_to_be32(crc);
190}
191
192/**
Timur Tabibdea8372008-06-19 17:56:11 -0500193 * prog_eeprom - write the EEPROM from memory
194 */
195static int prog_eeprom(void)
Haiying Wang34c68782006-07-12 10:48:05 -0400196{
Timur Tabia58e8802010-08-02 13:03:23 -0500197 int ret = 0;
Anton Vorontsovc33e3b82009-09-02 02:17:24 +0400198 int i;
Timur Tabibdea8372008-06-19 17:56:11 -0500199 void *p;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200200#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500201 unsigned int bus;
202#endif
Haiying Wang34c68782006-07-12 10:48:05 -0400203
Timur Tabibdea8372008-06-19 17:56:11 -0500204 /* Set the reserved values to 0xFF */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200205#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -0500206 e.res_0 = 0xFF;
207 memset(e.res_1, 0xFF, sizeof(e.res_1));
208#else
209 memset(e.res_0, 0xFF, sizeof(e.res_0));
210#endif
Timur Tabi13c312b2009-08-28 16:56:45 -0500211 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500212
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200213#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500214 bus = i2c_get_bus_num();
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200215 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
Timur Tabibdea8372008-06-19 17:56:11 -0500216#endif
217
Timur Tabia58e8802010-08-02 13:03:23 -0500218 /*
219 * The AT24C02 datasheet says that data can only be written in page
220 * mode, which means 8 bytes at a time, and it takes up to 5ms to
221 * complete a given write.
222 */
Timur Tabi13c312b2009-08-28 16:56:45 -0500223 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200224 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Masahiro Yamadadb204642014-11-07 03:03:31 +0900225 p, min((int)(sizeof(e) - i), 8));
Haiying Wang34c68782006-07-12 10:48:05 -0400226 if (ret)
227 break;
Timur Tabibdea8372008-06-19 17:56:11 -0500228 udelay(5000); /* 5ms write cycle timing */
Haiying Wang34c68782006-07-12 10:48:05 -0400229 }
Timur Tabibdea8372008-06-19 17:56:11 -0500230
Timur Tabia58e8802010-08-02 13:03:23 -0500231 if (!ret) {
232 /* Verify the write by reading back the EEPROM and comparing */
233 struct eeprom e2;
234
235 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
236 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, (void *)&e2, sizeof(e2));
237 if (!ret && memcmp(&e, &e2, sizeof(e)))
238 ret = -1;
239 }
240
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200241#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500242 i2c_set_bus_num(bus);
243#endif
244
Haiying Wang34c68782006-07-12 10:48:05 -0400245 if (ret) {
246 printf("Programming failed.\n");
Timur Tabia58e8802010-08-02 13:03:23 -0500247 has_been_read = 0;
Haiying Wang34c68782006-07-12 10:48:05 -0400248 return -1;
Haiying Wang34c68782006-07-12 10:48:05 -0400249 }
Timur Tabibdea8372008-06-19 17:56:11 -0500250
251 printf("Programming passed.\n");
Haiying Wang34c68782006-07-12 10:48:05 -0400252 return 0;
253}
254
Timur Tabibdea8372008-06-19 17:56:11 -0500255/**
256 * h2i - converts hex character into a number
257 *
258 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
259 * the integer equivalent.
260 */
261static inline u8 h2i(char p)
262{
263 if ((p >= '0') && (p <= '9'))
264 return p - '0';
265
266 if ((p >= 'A') && (p <= 'F'))
267 return (p - 'A') + 10;
268
269 if ((p >= 'a') && (p <= 'f'))
270 return (p - 'a') + 10;
271
272 return 0;
273}
274
275/**
276 * set_date - stores the build date into the EEPROM
277 *
278 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
279 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
280 * and stores it in the build date field of the EEPROM local copy.
281 */
282static void set_date(const char *string)
283{
284 unsigned int i;
285
286 if (strlen(string) != 12) {
287 printf("Usage: mac date YYMMDDhhmmss\n");
288 return;
289 }
290
291 for (i = 0; i < 6; i++)
292 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
Timur Tabi13c312b2009-08-28 16:56:45 -0500293
294 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500295}
296
297/**
298 * set_mac_address - stores a MAC address into the EEPROM
299 *
300 * This function takes a pointer to MAC address string
301 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
302 * stores it in one of the MAC address fields of the EEPROM local copy.
303 */
304static void set_mac_address(unsigned int index, const char *string)
305{
306 char *p = (char *) string;
307 unsigned int i;
308
Timur Tabi11e98e72010-09-30 15:36:50 -0500309 if ((index >= MAX_NUM_PORTS) || !string) {
Timur Tabibdea8372008-06-19 17:56:11 -0500310 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
311 return;
312 }
313
314 for (i = 0; *p && (i < 6); i++) {
315 e.mac[index][i] = simple_strtoul(p, &p, 16);
316 if (*p == ':')
317 p++;
318 }
Timur Tabi13c312b2009-08-28 16:56:45 -0500319
320 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500321}
322
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200323int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Haiying Wang34c68782006-07-12 10:48:05 -0400324{
Timur Tabibdea8372008-06-19 17:56:11 -0500325 char cmd;
Haiying Wang34c68782006-07-12 10:48:05 -0400326
Timur Tabibdea8372008-06-19 17:56:11 -0500327 if (argc == 1) {
328 show_eeprom();
329 return 0;
330 }
331
332 cmd = argv[1][0];
333
334 if (cmd == 'r') {
335 read_eeprom();
336 return 0;
337 }
Haiying Wang34c68782006-07-12 10:48:05 -0400338
Timur Tabi13c312b2009-08-28 16:56:45 -0500339 if (cmd == 'i') {
340#ifdef CONFIG_SYS_I2C_EEPROM_NXID
341 memcpy(e.id, "NXID", sizeof(e.id));
Jaiprakash Singhe1029ef2015-05-28 14:54:03 +0530342 e.version = cpu_to_be32(NXID_VERSION);
Timur Tabi13c312b2009-08-28 16:56:45 -0500343#else
344 memcpy(e.id, "CCID", sizeof(e.id));
345#endif
Timur Tabi2f9f65f2011-02-09 13:40:51 -0600346 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500347 return 0;
348 }
349
350 if (!is_valid) {
351 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
352 return 0;
353 }
354
355 if (argc == 2) {
Haiying Wang34c68782006-07-12 10:48:05 -0400356 switch (cmd) {
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500357 case 's': /* save */
Timur Tabibdea8372008-06-19 17:56:11 -0500358 prog_eeprom();
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500359 break;
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500360 default:
Wolfgang Denk3b683112010-07-17 01:06:04 +0200361 return cmd_usage(cmdtp);
Haiying Wang34c68782006-07-12 10:48:05 -0400362 }
Timur Tabibdea8372008-06-19 17:56:11 -0500363
364 return 0;
365 }
366
367 /* We know we have at least one parameter */
368
369 switch (cmd) {
370 case 'n': /* serial number */
371 memset(e.sn, 0, sizeof(e.sn));
372 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
Timur Tabi13c312b2009-08-28 16:56:45 -0500373 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500374 break;
375 case 'e': /* errata */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200376#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -0500377 memset(e.errata, 0, 5);
378 strncpy((char *)e.errata, argv[2], 4);
379#else
380 e.errata[0] = argv[2][0];
381 e.errata[1] = argv[2][1];
382#endif
Timur Tabi13c312b2009-08-28 16:56:45 -0500383 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500384 break;
385 case 'd': /* date BCD format YYMMDDhhmmss */
386 set_date(argv[2]);
387 break;
388 case 'p': /* MAC table size */
389 e.mac_count = simple_strtoul(argv[2], NULL, 16);
Timur Tabi13c312b2009-08-28 16:56:45 -0500390 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500391 break;
Timur Tabi11e98e72010-09-30 15:36:50 -0500392 case '0' ... '9': /* "mac 0" through "mac 22" */
393 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
Timur Tabibdea8372008-06-19 17:56:11 -0500394 break;
395 case 'h': /* help */
396 default:
Wolfgang Denk3b683112010-07-17 01:06:04 +0200397 return cmd_usage(cmdtp);
Haiying Wang34c68782006-07-12 10:48:05 -0400398 }
Timur Tabibdea8372008-06-19 17:56:11 -0500399
Haiying Wang34c68782006-07-12 10:48:05 -0400400 return 0;
401}
402
Timur Tabibdea8372008-06-19 17:56:11 -0500403/**
404 * mac_read_from_eeprom - read the MAC addresses from EEPROM
405 *
406 * This function reads the MAC addresses from EEPROM and sets the
407 * appropriate environment variables for each one read.
408 *
409 * The environment variables are only set if they haven't been set already.
410 * This ensures that any user-saved variables are never overwritten.
411 *
412 * This function must be called after relocation.
Timur Tabia2b41cb2011-02-09 02:00:09 +0000413 *
414 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
415 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
416 * located at a different offset.
Timur Tabibdea8372008-06-19 17:56:11 -0500417 */
Haiying Wang34c68782006-07-12 10:48:05 -0400418int mac_read_from_eeprom(void)
419{
Timur Tabibdea8372008-06-19 17:56:11 -0500420 unsigned int i;
Timur Tabia2b41cb2011-02-09 02:00:09 +0000421 u32 crc, crc_offset = offsetof(struct eeprom, crc);
422 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
Timur Tabi13c312b2009-08-28 16:56:45 -0500423
424 puts("EEPROM: ");
Haiying Wang34c68782006-07-12 10:48:05 -0400425
Timur Tabibdea8372008-06-19 17:56:11 -0500426 if (read_eeprom()) {
Haiying Wang34c68782006-07-12 10:48:05 -0400427 printf("Read failed.\n");
York Sun9215eff2014-04-30 14:43:46 -0700428 return 0;
Haiying Wang34c68782006-07-12 10:48:05 -0400429 }
430
Timur Tabibdea8372008-06-19 17:56:11 -0500431 if (!is_valid) {
Timur Tabi13c312b2009-08-28 16:56:45 -0500432 printf("Invalid ID (%02x %02x %02x %02x)\n",
433 e.id[0], e.id[1], e.id[2], e.id[3]);
York Sun9215eff2014-04-30 14:43:46 -0700434 return 0;
Timur Tabibdea8372008-06-19 17:56:11 -0500435 }
436
Timur Tabia2b41cb2011-02-09 02:00:09 +0000437#ifdef CONFIG_SYS_I2C_EEPROM_NXID
438 /*
439 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
440 * to where it is in v0.
441 */
442 if (e.version == 0)
443 crc_offset = 0x72;
444#endif
445
446 crc = crc32(0, (void *)&e, crc_offset);
447 crcp = (void *)&e + crc_offset;
448 if (crc != be32_to_cpu(*crcp)) {
Timur Tabi13c312b2009-08-28 16:56:45 -0500449 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
York Sun9215eff2014-04-30 14:43:46 -0700450 return 0;
Haiying Wange5607542009-06-04 16:12:40 -0400451 }
452
Timur Tabia2b41cb2011-02-09 02:00:09 +0000453#ifdef CONFIG_SYS_I2C_EEPROM_NXID
454 /*
455 * MAC address #9 in v1 occupies the same position as the CRC in v0.
456 * Erase it so that it's not mistaken for a MAC address. We'll
457 * update the CRC later.
458 */
459 if (e.version == 0)
460 memset(e.mac[8], 0xff, 6);
461#endif
462
Masahiro Yamadadb204642014-11-07 03:03:31 +0900463 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
Timur Tabibdea8372008-06-19 17:56:11 -0500464 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
465 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
466 char ethaddr[18];
467 char enetvar[9];
468
469 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
470 e.mac[i][0],
471 e.mac[i][1],
472 e.mac[i][2],
473 e.mac[i][3],
474 e.mac[i][4],
475 e.mac[i][5]);
476 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
477 /* Only initialize environment variables that are blank
478 * (i.e. have not yet been set)
479 */
Simon Glass64b723f2017-08-03 12:22:12 -0600480 if (!env_get(enetvar))
Simon Glass6a38e412017-08-03 12:22:09 -0600481 env_set(enetvar, ethaddr);
Timur Tabibdea8372008-06-19 17:56:11 -0500482 }
483 }
484
Timur Tabi13c312b2009-08-28 16:56:45 -0500485#ifdef CONFIG_SYS_I2C_EEPROM_NXID
486 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
Jaiprakash Singhe1029ef2015-05-28 14:54:03 +0530487 be32_to_cpu(e.version));
Timur Tabi13c312b2009-08-28 16:56:45 -0500488#else
489 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
490#endif
491
Timur Tabia2b41cb2011-02-09 02:00:09 +0000492#ifdef CONFIG_SYS_I2C_EEPROM_NXID
493 /*
494 * Now we need to upconvert the data into v1 format. We do this last so
495 * that at boot time, U-Boot will still say "NXID v0".
496 */
497 if (e.version == 0) {
Jaiprakash Singhe1029ef2015-05-28 14:54:03 +0530498 e.version = cpu_to_be32(NXID_VERSION);
Timur Tabia2b41cb2011-02-09 02:00:09 +0000499 update_crc();
500 }
501#endif
502
Haiying Wang34c68782006-07-12 10:48:05 -0400503 return 0;
504}
Timur Tabibdea8372008-06-19 17:56:11 -0500505
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200506#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -0500507
508/**
509 * get_cpu_board_revision - get the CPU board revision on 85xx boards
510 *
511 * Read the EEPROM to determine the board revision.
512 *
513 * This function is called before relocation, so we need to read a private
514 * copy of the EEPROM into a local variable on the stack.
515 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200516 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
517 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
Timur Tabibdea8372008-06-19 17:56:11 -0500518 * so that the SPD code will work. This means that all pre-relocation I2C
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200519 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
520 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
Timur Tabibdea8372008-06-19 17:56:11 -0500521 * this function is called. Oh well.
522 */
523unsigned int get_cpu_board_revision(void)
524{
525 struct board_eeprom {
526 u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
527 u8 major; /* 0x04 Board revision, major */
528 u8 minor; /* 0x05 Board revision, minor */
529 } be;
530
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200531 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Timur Tabibdea8372008-06-19 17:56:11 -0500532 (void *)&be, sizeof(be));
533
534 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
535 return MPC85XX_CPU_BOARD_REV(0, 0);
536
537 if ((be.major == 0xff) && (be.minor == 0xff))
538 return MPC85XX_CPU_BOARD_REV(0, 0);
539
Rafal Czubak5c50d142008-10-08 13:41:30 +0200540 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
Timur Tabibdea8372008-06-19 17:56:11 -0500541}
Timur Tabibdea8372008-06-19 17:56:11 -0500542#endif