1 /******************************************************************************
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
12 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
13 * COURTESY TO YOU. BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
14 * ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, APPLICATION OR STANDARD,
15 * XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION IS FREE
16 * FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE FOR OBTAINING
17 * ANY THIRD PARTY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
18 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
19 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY
20 * WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM
21 * CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE.
25 * Xilinx hardware products are not intended for use in life support
26 * appliances, devices, or systems. Use in such applications is
27 * expressly prohibited.
30 * (c) Copyright 2002-2004 Xilinx Inc.
31 * All rights reserved.
34 * You should have received a copy of the GNU General Public License along
35 * with this program; if not, write to the Free Software Foundation, Inc.,
36 * 675 Mass Ave, Cambridge, MA 02139, USA.
38 ******************************************************************************/
41 #include <environment.h>
43 #include <configs/ml300.h>
44 #include "xparameters.h"
46 #ifdef CFG_ENV_IS_IN_EEPROM
50 #define IIC_DELAY 5000
52 static u8 envStep = 0; /* 0 means crc has not been read */
53 const u8 hex[] = "0123456789ABCDEF"; /* lookup table for ML300 CRC */
55 /************************************************************************
56 * Use Xilinx provided driver to send data to EEPROM using iic bus.
59 send(u32 adr, u8 * data, u32 len)
61 u8 sendBuf[34]; /* first 2-bit is address and others are data */
66 for (pos = 0; pos < len; pos += 32) {
70 /* Put address and data bits together */
71 sendBuf[0] = (u8) ((adr + pos) >> 8);
72 sendBuf[1] = (u8) (adr + pos);
73 memcpy(&sendBuf[2], &data[pos], wlen);
75 /* Send to EEPROM through iic bus */
76 ret = XIic_Send(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1,
83 /************************************************************************
84 * Use Xilinx provided driver to read data from EEPROM using the iic bus.
87 receive(u32 adr, u8 * data, u32 len)
92 address[0] = (u8) (adr >> 8);
93 address[1] = (u8) adr;
95 /* Provide EEPROM address */
97 XIic_Send(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1, address,
99 /* Receive data from EEPROM */
101 XIic_Recv(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1, data, len);
104 /************************************************************************
105 * Convert a hexadecimal string to its equivalent integer value.
110 u8 n; /* position in string */
111 u8 m; /* position in digit[] to shift */
112 u8 count; /* loop index */
113 u8 intValue; /* integer value of hex string */
114 u8 digit[2]; /* hold values to convert */
116 for (n = 0; n < 2; n++) {
117 if (hexStg[n] == '\0')
119 if (hexStg[n] > 0x29 && hexStg[n] < 0x40)
120 digit[n] = hexStg[n] & 0x0f;
121 else if (hexStg[n] >= 'a' && hexStg[n] <= 'f')
122 digit[n] = (hexStg[n] & 0x0f) + 9;
123 else if (hexStg[n] >= 'A' && hexStg[n] <= 'F')
124 digit[n] = (hexStg[n] & 0x0f) + 9;
134 intValue = intValue | (digit[n] << (m << 2));
135 m--; /* adjust the position to set */
136 n++; /* next digit to process */
142 /************************************************************************
143 * Convert an integer string to its equivalent value.
149 while (*string >= '0' && *string <= '9') {
151 res += *string - '0';
158 /************************************************************************
159 * Key-value pairs are separated by "=" sign.
162 findKey(uchar * buffer, int *loc, u8 len)
166 for (i = 0; i < len; i++)
167 if (buffer[i] == '=') {
172 /* return -1 is no "=" sign found */
176 /************************************************************************
177 * Compute a new ML300 CRC when user calls the saveenv command.
178 * Also update EEPROM with new CRC value.
181 update_crc(u32 len, uchar * data)
183 uchar temp[6] = { 'C', '=', 0x00, 0x00, 0x00, 0x00 };
184 u32 crc; /* new crc value */
189 /* calculate new CRC */
190 for (i = 0; i < len; i++)
193 /* CRC includes key for check sum */
196 /* compose new CRC to be updated */
197 temp[2] = hex[(crc >> 4) & 0xf];
198 temp[3] = hex[crc & 0xf];
200 /* check to see if env size exceeded */
201 if (len + 6 > ENV_SIZE) {
202 printf("ERROR: not enough space to store CRC on EEPROM");
206 memcpy(data + len, temp, 6);
210 /************************************************************************
211 * Read out ML300 CRC and compare it with a runtime calculated ML300 CRC.
212 * If equal, then pass back a u-boot CRC value, otherwise pass back
213 * junk to indicate CRC error.
216 read_crc(uchar * buffer, int len)
219 u32 crc; /* runtime crc */
220 u8 old[2] = { 0xff, 0xff }; /* current CRC in EEPROM */
221 u8 stop; /* indication of end of env data */
222 u8 pre; /* previous EEPROM data bit */
225 addr = CFG_ENV_OFFSET; /* start from first env address */
231 /* calculate runtime CRC according to ML300 and read back
232 old CRC stored in the EEPROM */
233 while (n < CFG_ENV_SIZE) {
234 receive(addr, buffer, len);
236 /* found two null chars, end of env */
237 if ((pre || buffer[0]) == 0)
240 findKey(buffer, &loc, len);
242 /* found old check sum, read and store old CRC */
243 if ((loc == 0 && pre == 'C')
244 || (loc > 0 && buffer[loc - 1] == 'C'))
245 receive(addr + loc + 1, old, 2);
247 pre = buffer[len - 1];
249 /* calculate runtime ML300 CRC */
254 stop = buffer[i] || buffer[i - 1];
256 } while (stop && (i < len));
265 /* exclude old CRC from runtime calculation */
266 crc -= (old[0] + old[1]);
268 /* match CRC values, send back u-boot CRC */
269 if ((old[0] == hex[(crc >> 4) & 0xf])
270 && (old[1] == hex[crc & 0xf])) {
274 CFG_ENV_OFFSET - offsetof(env_t, crc) + offsetof(env_t,
276 /* calculate u-boot crc */
277 while (n < ENV_SIZE) {
278 receive(addr, buffer, len);
279 crc = crc32(crc, buffer, len);
284 memcpy(buffer, &crc, 4);
288 /************************************************************************
289 * Convert IP address to hexadecimals.
292 ip_ml300(uchar * s, uchar * res)
299 for (i = 0; i < 4; i++) {
300 sprintf(temp, "%02x", atoi(s));
301 s = (uchar *)strchr((char *)s, '.') + 1;
302 strcat((char *)res, temp);
306 /************************************************************************
307 * Change 0xff (255), a dummy null char to 0x00.
310 change_null(uchar * s)
313 change_null((uchar *)strchr((char *)s + 1, 255));
314 *(strchr((char *)s, 255)) = '\0';
318 /************************************************************************
319 * Update environment variable name and values to u-boot standard.
324 char *s; /* pointer to env value */
325 char temp[20]; /* temp storage for addresses */
330 sprintf(temp, "%c%c.%c%c.%c%c.%c%c.%c%c.%c%c",
331 s[0], s[1], s[ 2], s[ 3],
332 s[4], s[5], s[ 6], s[ 7],
333 s[8], s[9], s[10], s[11] );
334 setenv("ethaddr", temp);
341 setenv("serial#", s);
348 sprintf(temp, "%d.%d.%d.%d", axtoi((u8 *)s), axtoi((u8 *)(s + 2)),
349 axtoi((u8 *)(s + 4)), axtoi((u8 *)(s + 6)));
350 setenv("ipaddr", temp);
357 sprintf(temp, "%d.%d.%d.%d", axtoi((u8 *)s), axtoi((u8 *)(s + 2)),
358 axtoi((u8 *)(s + 4)), axtoi((u8 *)(s + 6)));
359 setenv("serverip", temp);
366 setenv("bootargs", s);
373 setenv("bootfile", s);
380 setenv("bootcmd", s);
384 /* Don't include C (CRC) */
388 /************************************************************************
389 * Save user modified environment values back to EEPROM.
394 char eprom[ENV_SIZE]; /* buffer to be written back to EEPROM */
396 char ff[] = { 0xff, 0x00 }; /* dummy null value */
397 u32 len; /* length of env to be written to EEPROM */
402 s = getenv("ethaddr");
405 sprintf(temp, "%c%c%c%c%c%c%c%c%c%c%c%c",
406 *s, *(s + 1), *(s + 3), *(s + 4), *(s + 6), *(s + 7),
407 *(s + 9), *(s + 10), *(s + 12), *(s + 13), *(s + 15),
414 s = getenv("serial#");
422 s = getenv("ipaddr");
425 ip_ml300((uchar *)s, (uchar *)temp);
431 s = getenv("serverip");
434 ip_ml300((uchar *)s, (uchar *)temp);
440 s = getenv("bootargs");
448 s = getenv("bootfile");
456 s = getenv("bootcmd");
463 len = strlen(eprom); /* find env length without crc */
464 change_null((uchar *)eprom); /* change 0xff to 0x00 */
466 /* update EEPROM env values if there is enough space */
467 if (update_crc(len, (uchar *)eprom) == 0)
468 send(CFG_ENV_OFFSET, (uchar *)eprom, len + 6);
471 /************************************************************************
472 * U-boot call for EEPROM read associated activities.
475 i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len)
479 /* first read call is for crc */
480 read_crc(buffer, len);
483 } else if (envStep == 1) {
484 /* then read out EEPROM content for runtime u-boot CRC calculation */
485 receive(addr, buffer, len);
487 if (addr + len - CFG_ENV_OFFSET == CFG_ENV_SIZE)
488 /* end of runtime crc read */
494 /* when call getenv_r */
495 receive(addr, buffer, len);
496 } else if (addr + len < CFG_ENV_OFFSET + CFG_ENV_SIZE) {
497 /* calling env_relocate(), but don't read out
498 crc value from EEPROM */
499 receive(addr, buffer + 4, len);
501 receive(addr, buffer + 4, len - 4);
508 /************************************************************************
509 * U-boot call for EEPROM write acativities.
512 i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len)
514 /* save env on last page write called by u-boot */
515 if (addr + len >= CFG_ENV_OFFSET + CFG_ENV_SIZE)
521 /************************************************************************
525 i2c_probe(uchar chip)