xref: /OK3568_Linux_fs/u-boot/board/freescale/common/sys_eeprom.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
3*4882a593Smuzhiyun  * York Sun (yorksun@freescale.com)
4*4882a593Smuzhiyun  * Haiying Wang (haiying.wang@freescale.com)
5*4882a593Smuzhiyun  * Timur Tabi (timur@freescale.com)
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <common.h>
11*4882a593Smuzhiyun #include <command.h>
12*4882a593Smuzhiyun #include <i2c.h>
13*4882a593Smuzhiyun #include <linux/ctype.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_CCID
16*4882a593Smuzhiyun #include "../common/eeprom.h"
17*4882a593Smuzhiyun #define MAX_NUM_PORTS	8
18*4882a593Smuzhiyun #endif
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
21*4882a593Smuzhiyun /* some boards with non-256-bytes EEPROM have special define */
22*4882a593Smuzhiyun /* for MAX_NUM_PORTS in board-specific file */
23*4882a593Smuzhiyun #ifndef MAX_NUM_PORTS
24*4882a593Smuzhiyun #define MAX_NUM_PORTS	16
25*4882a593Smuzhiyun #endif
26*4882a593Smuzhiyun #define NXID_VERSION	1
27*4882a593Smuzhiyun #endif
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /**
30*4882a593Smuzhiyun  * static eeprom: EEPROM layout for CCID or NXID formats
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * See application note AN3638 for details.
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun static struct __attribute__ ((__packed__)) eeprom {
35*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_CCID
36*4882a593Smuzhiyun 	u8 id[4];         /* 0x00 - 0x03 EEPROM Tag 'CCID' */
37*4882a593Smuzhiyun 	u8 major;         /* 0x04        Board revision, major */
38*4882a593Smuzhiyun 	u8 minor;         /* 0x05        Board revision, minor */
39*4882a593Smuzhiyun 	u8 sn[10];        /* 0x06 - 0x0F Serial Number*/
40*4882a593Smuzhiyun 	u8 errata[2];     /* 0x10 - 0x11 Errata Level */
41*4882a593Smuzhiyun 	u8 date[6];       /* 0x12 - 0x17 Build Date */
42*4882a593Smuzhiyun 	u8 res_0[40];     /* 0x18 - 0x3f Reserved */
43*4882a593Smuzhiyun 	u8 mac_count;     /* 0x40        Number of MAC addresses */
44*4882a593Smuzhiyun 	u8 mac_flag;      /* 0x41        MAC table flags */
45*4882a593Smuzhiyun 	u8 mac[MAX_NUM_PORTS][6];     /* 0x42 - 0x71 MAC addresses */
46*4882a593Smuzhiyun 	u32 crc;          /* 0x72        CRC32 checksum */
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
49*4882a593Smuzhiyun 	u8 id[4];         /* 0x00 - 0x03 EEPROM Tag 'NXID' */
50*4882a593Smuzhiyun 	u8 sn[12];        /* 0x04 - 0x0F Serial Number */
51*4882a593Smuzhiyun 	u8 errata[5];     /* 0x10 - 0x14 Errata Level */
52*4882a593Smuzhiyun 	u8 date[6];       /* 0x15 - 0x1a Build Date */
53*4882a593Smuzhiyun 	u8 res_0;         /* 0x1b        Reserved */
54*4882a593Smuzhiyun 	u32 version;      /* 0x1c - 0x1f NXID Version */
55*4882a593Smuzhiyun 	u8 tempcal[8];    /* 0x20 - 0x27 Temperature Calibration Factors */
56*4882a593Smuzhiyun 	u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
57*4882a593Smuzhiyun 	u8 tempcalflags;  /* 0x2a        Temperature Calibration Flags */
58*4882a593Smuzhiyun 	u8 res_1[21];     /* 0x2b - 0x3f Reserved */
59*4882a593Smuzhiyun 	u8 mac_count;     /* 0x40        Number of MAC addresses */
60*4882a593Smuzhiyun 	u8 mac_flag;      /* 0x41        MAC table flags */
61*4882a593Smuzhiyun 	u8 mac[MAX_NUM_PORTS][6];     /* 0x42 - 0xa1 MAC addresses */
62*4882a593Smuzhiyun 	u8 res_2[90];     /* 0xa2 - 0xfb Reserved */
63*4882a593Smuzhiyun 	u32 crc;          /* 0xfc - 0xff CRC32 checksum */
64*4882a593Smuzhiyun #endif
65*4882a593Smuzhiyun } e;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /* Set to 1 if we've read EEPROM into memory */
68*4882a593Smuzhiyun static int has_been_read = 0;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
71*4882a593Smuzhiyun /* Is this a valid NXID EEPROM? */
72*4882a593Smuzhiyun #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
73*4882a593Smuzhiyun 		  (e.id[2] == 'I') || (e.id[3] == 'D'))
74*4882a593Smuzhiyun #endif
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_CCID
77*4882a593Smuzhiyun /* Is this a valid CCID EEPROM? */
78*4882a593Smuzhiyun #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
79*4882a593Smuzhiyun 		  (e.id[2] == 'I') || (e.id[3] == 'D'))
80*4882a593Smuzhiyun #endif
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /**
83*4882a593Smuzhiyun  * show_eeprom - display the contents of the EEPROM
84*4882a593Smuzhiyun  */
show_eeprom(void)85*4882a593Smuzhiyun static void show_eeprom(void)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	int i;
88*4882a593Smuzhiyun 	unsigned int crc;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	/* EEPROM tag ID, either CCID or NXID */
91*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
92*4882a593Smuzhiyun 	printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
93*4882a593Smuzhiyun 	       be32_to_cpu(e.version));
94*4882a593Smuzhiyun #else
95*4882a593Smuzhiyun 	printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
96*4882a593Smuzhiyun #endif
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	/* Serial number */
99*4882a593Smuzhiyun 	printf("SN: %s\n", e.sn);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	/* Errata level. */
102*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
103*4882a593Smuzhiyun 	printf("Errata: %s\n", e.errata);
104*4882a593Smuzhiyun #else
105*4882a593Smuzhiyun 	printf("Errata: %c%c\n",
106*4882a593Smuzhiyun 		e.errata[0] ? e.errata[0] : '.',
107*4882a593Smuzhiyun 		e.errata[1] ? e.errata[1] : '.');
108*4882a593Smuzhiyun #endif
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	/* Build date, BCD date values, as YYMMDDhhmmss */
111*4882a593Smuzhiyun 	printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
112*4882a593Smuzhiyun 		e.date[0], e.date[1], e.date[2],
113*4882a593Smuzhiyun 		e.date[3] & 0x7F, e.date[4], e.date[5],
114*4882a593Smuzhiyun 		e.date[3] & 0x80 ? "PM" : "");
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	/* Show MAC addresses  */
117*4882a593Smuzhiyun 	for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 		u8 *p = e.mac[i];
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 		printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
122*4882a593Smuzhiyun 			p[0], p[1], p[2], p[3],	p[4], p[5]);
123*4882a593Smuzhiyun 	}
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	crc = crc32(0, (void *)&e, sizeof(e) - 4);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	if (crc == be32_to_cpu(e.crc))
128*4882a593Smuzhiyun 		printf("CRC: %08x\n", be32_to_cpu(e.crc));
129*4882a593Smuzhiyun 	else
130*4882a593Smuzhiyun 		printf("CRC: %08x (should be %08x)\n",
131*4882a593Smuzhiyun 			be32_to_cpu(e.crc), crc);
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun #ifdef DEBUG
134*4882a593Smuzhiyun 	printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
135*4882a593Smuzhiyun 	for (i = 0; i < sizeof(e); i++) {
136*4882a593Smuzhiyun 		if ((i % 16) == 0)
137*4882a593Smuzhiyun 			printf("%02X: ", i);
138*4882a593Smuzhiyun 		printf("%02X ", ((u8 *)&e)[i]);
139*4882a593Smuzhiyun 		if (((i % 16) == 15) || (i == sizeof(e) - 1))
140*4882a593Smuzhiyun 			printf("\n");
141*4882a593Smuzhiyun 	}
142*4882a593Smuzhiyun #endif
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun  * read_eeprom - read the EEPROM into memory
147*4882a593Smuzhiyun  */
read_eeprom(void)148*4882a593Smuzhiyun static int read_eeprom(void)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	int ret;
151*4882a593Smuzhiyun #ifdef CONFIG_SYS_EEPROM_BUS_NUM
152*4882a593Smuzhiyun 	unsigned int bus;
153*4882a593Smuzhiyun #endif
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	if (has_been_read)
156*4882a593Smuzhiyun 		return 0;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun #ifdef CONFIG_SYS_EEPROM_BUS_NUM
159*4882a593Smuzhiyun 	bus = i2c_get_bus_num();
160*4882a593Smuzhiyun 	i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
161*4882a593Smuzhiyun #endif
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
164*4882a593Smuzhiyun 		(void *)&e, sizeof(e));
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun #ifdef CONFIG_SYS_EEPROM_BUS_NUM
167*4882a593Smuzhiyun 	i2c_set_bus_num(bus);
168*4882a593Smuzhiyun #endif
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun #ifdef DEBUG
171*4882a593Smuzhiyun 	show_eeprom();
172*4882a593Smuzhiyun #endif
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	has_been_read = (ret == 0) ? 1 : 0;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	return ret;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /**
180*4882a593Smuzhiyun  *  update_crc - update the CRC
181*4882a593Smuzhiyun  *
182*4882a593Smuzhiyun  *  This function should be called after each update to the EEPROM structure,
183*4882a593Smuzhiyun  *  to make sure the CRC is always correct.
184*4882a593Smuzhiyun  */
update_crc(void)185*4882a593Smuzhiyun static void update_crc(void)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	u32 crc;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	crc = crc32(0, (void *)&e, sizeof(e) - 4);
190*4882a593Smuzhiyun 	e.crc = cpu_to_be32(crc);
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun /**
194*4882a593Smuzhiyun  * prog_eeprom - write the EEPROM from memory
195*4882a593Smuzhiyun  */
prog_eeprom(void)196*4882a593Smuzhiyun static int prog_eeprom(void)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	int ret = 0;
199*4882a593Smuzhiyun 	int i;
200*4882a593Smuzhiyun 	void *p;
201*4882a593Smuzhiyun #ifdef CONFIG_SYS_EEPROM_BUS_NUM
202*4882a593Smuzhiyun 	unsigned int bus;
203*4882a593Smuzhiyun #endif
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/* Set the reserved values to 0xFF   */
206*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
207*4882a593Smuzhiyun 	e.res_0 = 0xFF;
208*4882a593Smuzhiyun 	memset(e.res_1, 0xFF, sizeof(e.res_1));
209*4882a593Smuzhiyun #else
210*4882a593Smuzhiyun 	memset(e.res_0, 0xFF, sizeof(e.res_0));
211*4882a593Smuzhiyun #endif
212*4882a593Smuzhiyun 	update_crc();
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun #ifdef CONFIG_SYS_EEPROM_BUS_NUM
215*4882a593Smuzhiyun 	bus = i2c_get_bus_num();
216*4882a593Smuzhiyun 	i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
217*4882a593Smuzhiyun #endif
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	/*
220*4882a593Smuzhiyun 	 * The AT24C02 datasheet says that data can only be written in page
221*4882a593Smuzhiyun 	 * mode, which means 8 bytes at a time, and it takes up to 5ms to
222*4882a593Smuzhiyun 	 * complete a given write.
223*4882a593Smuzhiyun 	 */
224*4882a593Smuzhiyun 	for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
225*4882a593Smuzhiyun 		ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
226*4882a593Smuzhiyun 				p, min((int)(sizeof(e) - i), 8));
227*4882a593Smuzhiyun 		if (ret)
228*4882a593Smuzhiyun 			break;
229*4882a593Smuzhiyun 		udelay(5000);	/* 5ms write cycle timing */
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	if (!ret) {
233*4882a593Smuzhiyun 		/* Verify the write by reading back the EEPROM and comparing */
234*4882a593Smuzhiyun 		struct eeprom e2;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 		ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
237*4882a593Smuzhiyun 			CONFIG_SYS_I2C_EEPROM_ADDR_LEN, (void *)&e2, sizeof(e2));
238*4882a593Smuzhiyun 		if (!ret && memcmp(&e, &e2, sizeof(e)))
239*4882a593Smuzhiyun 			ret = -1;
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun #ifdef CONFIG_SYS_EEPROM_BUS_NUM
243*4882a593Smuzhiyun 	i2c_set_bus_num(bus);
244*4882a593Smuzhiyun #endif
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	if (ret) {
247*4882a593Smuzhiyun 		printf("Programming failed.\n");
248*4882a593Smuzhiyun 		has_been_read = 0;
249*4882a593Smuzhiyun 		return -1;
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	printf("Programming passed.\n");
253*4882a593Smuzhiyun 	return 0;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun /**
257*4882a593Smuzhiyun  * h2i - converts hex character into a number
258*4882a593Smuzhiyun  *
259*4882a593Smuzhiyun  * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
260*4882a593Smuzhiyun  * the integer equivalent.
261*4882a593Smuzhiyun  */
h2i(char p)262*4882a593Smuzhiyun static inline u8 h2i(char p)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	if ((p >= '0') && (p <= '9'))
265*4882a593Smuzhiyun 		return p - '0';
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	if ((p >= 'A') && (p <= 'F'))
268*4882a593Smuzhiyun 		return (p - 'A') + 10;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	if ((p >= 'a') && (p <= 'f'))
271*4882a593Smuzhiyun 		return (p - 'a') + 10;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	return 0;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /**
277*4882a593Smuzhiyun  * set_date - stores the build date into the EEPROM
278*4882a593Smuzhiyun  *
279*4882a593Smuzhiyun  * This function takes a pointer to a string in the format "YYMMDDhhmmss"
280*4882a593Smuzhiyun  * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
281*4882a593Smuzhiyun  * and stores it in the build date field of the EEPROM local copy.
282*4882a593Smuzhiyun  */
set_date(const char * string)283*4882a593Smuzhiyun static void set_date(const char *string)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun 	unsigned int i;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	if (strlen(string) != 12) {
288*4882a593Smuzhiyun 		printf("Usage: mac date YYMMDDhhmmss\n");
289*4882a593Smuzhiyun 		return;
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	for (i = 0; i < 6; i++)
293*4882a593Smuzhiyun 		e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	update_crc();
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun /**
299*4882a593Smuzhiyun  * set_mac_address - stores a MAC address into the EEPROM
300*4882a593Smuzhiyun  *
301*4882a593Smuzhiyun  * This function takes a pointer to MAC address string
302*4882a593Smuzhiyun  * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
303*4882a593Smuzhiyun  * stores it in one of the MAC address fields of the EEPROM local copy.
304*4882a593Smuzhiyun  */
set_mac_address(unsigned int index,const char * string)305*4882a593Smuzhiyun static void set_mac_address(unsigned int index, const char *string)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun 	char *p = (char *) string;
308*4882a593Smuzhiyun 	unsigned int i;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	if ((index >= MAX_NUM_PORTS) || !string) {
311*4882a593Smuzhiyun 		printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
312*4882a593Smuzhiyun 		return;
313*4882a593Smuzhiyun 	}
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	for (i = 0; *p && (i < 6); i++) {
316*4882a593Smuzhiyun 		e.mac[index][i] = simple_strtoul(p, &p, 16);
317*4882a593Smuzhiyun 		if (*p == ':')
318*4882a593Smuzhiyun 			p++;
319*4882a593Smuzhiyun 	}
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	update_crc();
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
do_mac(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])324*4882a593Smuzhiyun int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	char cmd;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	if (argc == 1) {
329*4882a593Smuzhiyun 		show_eeprom();
330*4882a593Smuzhiyun 		return 0;
331*4882a593Smuzhiyun 	}
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	cmd = argv[1][0];
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	if (cmd == 'r') {
336*4882a593Smuzhiyun 		read_eeprom();
337*4882a593Smuzhiyun 		return 0;
338*4882a593Smuzhiyun 	}
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	if (cmd == 'i') {
341*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
342*4882a593Smuzhiyun 		memcpy(e.id, "NXID", sizeof(e.id));
343*4882a593Smuzhiyun 		e.version = cpu_to_be32(NXID_VERSION);
344*4882a593Smuzhiyun #else
345*4882a593Smuzhiyun 		memcpy(e.id, "CCID", sizeof(e.id));
346*4882a593Smuzhiyun #endif
347*4882a593Smuzhiyun 		update_crc();
348*4882a593Smuzhiyun 		return 0;
349*4882a593Smuzhiyun 	}
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	if (!is_valid) {
352*4882a593Smuzhiyun 		printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
353*4882a593Smuzhiyun 		return 0;
354*4882a593Smuzhiyun 	}
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	if (argc == 2) {
357*4882a593Smuzhiyun 		switch (cmd) {
358*4882a593Smuzhiyun 		case 's':	/* save */
359*4882a593Smuzhiyun 			prog_eeprom();
360*4882a593Smuzhiyun 			break;
361*4882a593Smuzhiyun 		default:
362*4882a593Smuzhiyun 			return cmd_usage(cmdtp);
363*4882a593Smuzhiyun 		}
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 		return 0;
366*4882a593Smuzhiyun 	}
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	/* We know we have at least one parameter  */
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	switch (cmd) {
371*4882a593Smuzhiyun 	case 'n':	/* serial number */
372*4882a593Smuzhiyun 		memset(e.sn, 0, sizeof(e.sn));
373*4882a593Smuzhiyun 		strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
374*4882a593Smuzhiyun 		update_crc();
375*4882a593Smuzhiyun 		break;
376*4882a593Smuzhiyun 	case 'e':	/* errata */
377*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
378*4882a593Smuzhiyun 		memset(e.errata, 0, 5);
379*4882a593Smuzhiyun 		strncpy((char *)e.errata, argv[2], 4);
380*4882a593Smuzhiyun #else
381*4882a593Smuzhiyun 		e.errata[0] = argv[2][0];
382*4882a593Smuzhiyun 		e.errata[1] = argv[2][1];
383*4882a593Smuzhiyun #endif
384*4882a593Smuzhiyun 		update_crc();
385*4882a593Smuzhiyun 		break;
386*4882a593Smuzhiyun 	case 'd':	/* date BCD format YYMMDDhhmmss */
387*4882a593Smuzhiyun 		set_date(argv[2]);
388*4882a593Smuzhiyun 		break;
389*4882a593Smuzhiyun 	case 'p':	/* MAC table size */
390*4882a593Smuzhiyun 		e.mac_count = simple_strtoul(argv[2], NULL, 16);
391*4882a593Smuzhiyun 		update_crc();
392*4882a593Smuzhiyun 		break;
393*4882a593Smuzhiyun 	case '0' ... '9':	/* "mac 0" through "mac 22" */
394*4882a593Smuzhiyun 		set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
395*4882a593Smuzhiyun 		break;
396*4882a593Smuzhiyun 	case 'h':	/* help */
397*4882a593Smuzhiyun 	default:
398*4882a593Smuzhiyun 		return cmd_usage(cmdtp);
399*4882a593Smuzhiyun 	}
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	return 0;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun /**
405*4882a593Smuzhiyun  * mac_read_from_eeprom - read the MAC addresses from EEPROM
406*4882a593Smuzhiyun  *
407*4882a593Smuzhiyun  * This function reads the MAC addresses from EEPROM and sets the
408*4882a593Smuzhiyun  * appropriate environment variables for each one read.
409*4882a593Smuzhiyun  *
410*4882a593Smuzhiyun  * The environment variables are only set if they haven't been set already.
411*4882a593Smuzhiyun  * This ensures that any user-saved variables are never overwritten.
412*4882a593Smuzhiyun  *
413*4882a593Smuzhiyun  * This function must be called after relocation.
414*4882a593Smuzhiyun  *
415*4882a593Smuzhiyun  * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
416*4882a593Smuzhiyun  * format.  In a v0 EEPROM, there are only eight MAC addresses and the CRC is
417*4882a593Smuzhiyun  * located at a different offset.
418*4882a593Smuzhiyun  */
mac_read_from_eeprom(void)419*4882a593Smuzhiyun int mac_read_from_eeprom(void)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	unsigned int i;
422*4882a593Smuzhiyun 	u32 crc, crc_offset = offsetof(struct eeprom, crc);
423*4882a593Smuzhiyun 	u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	puts("EEPROM: ");
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	if (read_eeprom()) {
428*4882a593Smuzhiyun 		printf("Read failed.\n");
429*4882a593Smuzhiyun 		return 0;
430*4882a593Smuzhiyun 	}
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	if (!is_valid) {
433*4882a593Smuzhiyun 		printf("Invalid ID (%02x %02x %02x %02x)\n",
434*4882a593Smuzhiyun 		       e.id[0], e.id[1], e.id[2], e.id[3]);
435*4882a593Smuzhiyun 		return 0;
436*4882a593Smuzhiyun 	}
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
439*4882a593Smuzhiyun 	/*
440*4882a593Smuzhiyun 	 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
441*4882a593Smuzhiyun 	 * to where it is in v0.
442*4882a593Smuzhiyun 	 */
443*4882a593Smuzhiyun 	if (e.version == 0)
444*4882a593Smuzhiyun 		crc_offset = 0x72;
445*4882a593Smuzhiyun #endif
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	crc = crc32(0, (void *)&e, crc_offset);
448*4882a593Smuzhiyun 	crcp = (void *)&e + crc_offset;
449*4882a593Smuzhiyun 	if (crc != be32_to_cpu(*crcp)) {
450*4882a593Smuzhiyun 		printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
451*4882a593Smuzhiyun 		return 0;
452*4882a593Smuzhiyun 	}
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
455*4882a593Smuzhiyun 	/*
456*4882a593Smuzhiyun 	 * MAC address #9 in v1 occupies the same position as the CRC in v0.
457*4882a593Smuzhiyun 	 * Erase it so that it's not mistaken for a MAC address.  We'll
458*4882a593Smuzhiyun 	 * update the CRC later.
459*4882a593Smuzhiyun 	 */
460*4882a593Smuzhiyun 	if (e.version == 0)
461*4882a593Smuzhiyun 		memset(e.mac[8], 0xff, 6);
462*4882a593Smuzhiyun #endif
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
465*4882a593Smuzhiyun 		if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
466*4882a593Smuzhiyun 		    memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
467*4882a593Smuzhiyun 			char ethaddr[18];
468*4882a593Smuzhiyun 			char enetvar[9];
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 			sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
471*4882a593Smuzhiyun 				e.mac[i][0],
472*4882a593Smuzhiyun 				e.mac[i][1],
473*4882a593Smuzhiyun 				e.mac[i][2],
474*4882a593Smuzhiyun 				e.mac[i][3],
475*4882a593Smuzhiyun 				e.mac[i][4],
476*4882a593Smuzhiyun 				e.mac[i][5]);
477*4882a593Smuzhiyun 			sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
478*4882a593Smuzhiyun 			/* Only initialize environment variables that are blank
479*4882a593Smuzhiyun 			 * (i.e. have not yet been set)
480*4882a593Smuzhiyun 			 */
481*4882a593Smuzhiyun 			if (!env_get(enetvar))
482*4882a593Smuzhiyun 				env_set(enetvar, ethaddr);
483*4882a593Smuzhiyun 		}
484*4882a593Smuzhiyun 	}
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
487*4882a593Smuzhiyun 	printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
488*4882a593Smuzhiyun 	       be32_to_cpu(e.version));
489*4882a593Smuzhiyun #else
490*4882a593Smuzhiyun 	printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
491*4882a593Smuzhiyun #endif
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_NXID
494*4882a593Smuzhiyun 	/*
495*4882a593Smuzhiyun 	 * Now we need to upconvert the data into v1 format.  We do this last so
496*4882a593Smuzhiyun 	 * that at boot time, U-Boot will still say "NXID v0".
497*4882a593Smuzhiyun 	 */
498*4882a593Smuzhiyun 	if (e.version == 0) {
499*4882a593Smuzhiyun 		e.version = cpu_to_be32(NXID_VERSION);
500*4882a593Smuzhiyun 		update_crc();
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun #endif
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	return 0;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EEPROM_CCID
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun /**
510*4882a593Smuzhiyun  * get_cpu_board_revision - get the CPU board revision on 85xx boards
511*4882a593Smuzhiyun  *
512*4882a593Smuzhiyun  * Read the EEPROM to determine the board revision.
513*4882a593Smuzhiyun  *
514*4882a593Smuzhiyun  * This function is called before relocation, so we need to read a private
515*4882a593Smuzhiyun  * copy of the EEPROM into a local variable on the stack.
516*4882a593Smuzhiyun  *
517*4882a593Smuzhiyun  * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM.  The global
518*4882a593Smuzhiyun  * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
519*4882a593Smuzhiyun  * so that the SPD code will work.  This means that all pre-relocation I2C
520*4882a593Smuzhiyun  * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus.  So if
521*4882a593Smuzhiyun  * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
522*4882a593Smuzhiyun  * this function is called.  Oh well.
523*4882a593Smuzhiyun  */
get_cpu_board_revision(void)524*4882a593Smuzhiyun unsigned int get_cpu_board_revision(void)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun 	struct board_eeprom {
527*4882a593Smuzhiyun 		u32 id;           /* 0x00 - 0x03 EEPROM Tag 'CCID' */
528*4882a593Smuzhiyun 		u8 major;         /* 0x04        Board revision, major */
529*4882a593Smuzhiyun 		u8 minor;         /* 0x05        Board revision, minor */
530*4882a593Smuzhiyun 	} be;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
533*4882a593Smuzhiyun 		(void *)&be, sizeof(be));
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
536*4882a593Smuzhiyun 		return MPC85XX_CPU_BOARD_REV(0, 0);
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	if ((be.major == 0xff) && (be.minor == 0xff))
539*4882a593Smuzhiyun 		return MPC85XX_CPU_BOARD_REV(0, 0);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun #endif
544