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 "xparameters.h"
45 #ifdef CFG_ENV_IS_IN_EEPROM
49 #define IIC_DELAY 5000
51 static u8 envStep = 0; /* 0 means crc has not been read */
52 const u8 hex[] = "0123456789ABCDEF"; /* lookup table for ML300 CRC */
54 /************************************************************************
55 * Use Xilinx provided driver to send data to EEPROM using iic bus.
58 send(u32 adr, u8 * data, u32 len)
60 u8 sendBuf[34]; /* first 2-bit is address and others are data */
65 for (pos = 0; pos < len; pos += 32) {
69 /* Put address and data bits together */
70 sendBuf[0] = (u8) ((adr + pos) >> 8);
71 sendBuf[1] = (u8) (adr + pos);
72 memcpy(&sendBuf[2], &data[pos], wlen);
74 /* Send to EEPROM through iic bus */
75 ret = XIic_Send(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1,
82 /************************************************************************
83 * Use Xilinx provided driver to read data from EEPROM using the iic bus.
86 receive(u32 adr, u8 * data, u32 len)
91 address[0] = (u8) (adr >> 8);
92 address[1] = (u8) adr;
94 /* Provide EEPROM address */
96 XIic_Send(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1, address,
98 /* Receive data from EEPROM */
100 XIic_Recv(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1, data, len);
103 /************************************************************************
104 * Convert a hexadecimal string to its equivalent integer value.
109 u8 n; /* position in string */
110 u8 m; /* position in digit[] to shift */
111 u8 count; /* loop index */
112 u8 intValue; /* integer value of hex string */
113 u8 digit[2]; /* hold values to convert */
115 for (n = 0; n < 2; n++) {
116 if (hexStg[n] == '\0')
118 if (hexStg[n] > 0x29 && hexStg[n] < 0x40)
119 digit[n] = hexStg[n] & 0x0f;
120 else if (hexStg[n] >= 'a' && hexStg[n] <= 'f')
121 digit[n] = (hexStg[n] & 0x0f) + 9;
122 else if (hexStg[n] >= 'A' && hexStg[n] <= 'F')
123 digit[n] = (hexStg[n] & 0x0f) + 9;
133 intValue = intValue | (digit[n] << (m << 2));
134 m--; /* adjust the position to set */
135 n++; /* next digit to process */
141 /************************************************************************
142 * Convert an integer string to its equivalent value.
148 while (*string >= '0' && *string <= '9') {
150 res += *string - '0';
157 /************************************************************************
158 * Key-value pairs are separated by "=" sign.
161 findKey(uchar * buffer, int *loc, u8 len)
165 for (i = 0; i < len; i++)
166 if (buffer[i] == '=') {
171 /* return -1 is no "=" sign found */
175 /************************************************************************
176 * Compute a new ML300 CRC when user calls the saveenv command.
177 * Also update EEPROM with new CRC value.
180 update_crc(u32 len, uchar * data)
182 uchar temp[6] = { 'C', '=', 0x00, 0x00, 0x00, 0x00 };
183 u32 crc; /* new crc value */
188 /* calculate new CRC */
189 for (i = 0; i < len; i++)
192 /* CRC includes key for check sum */
195 /* compose new CRC to be updated */
196 temp[2] = hex[(crc >> 4) & 0xf];
197 temp[3] = hex[crc & 0xf];
199 /* check to see if env size exceeded */
200 if (len + 6 > ENV_SIZE) {
201 printf("ERROR: not enough space to store CRC on EEPROM");
205 memcpy(data + len, temp, 6);
209 /************************************************************************
210 * Read out ML300 CRC and compare it with a runtime calculated ML300 CRC.
211 * If equal, then pass back a u-boot CRC value, otherwise pass back
212 * junk to indicate CRC error.
215 read_crc(uchar * buffer, int len)
218 u32 crc; /* runtime crc */
219 u8 old[2] = { 0xff, 0xff }; /* current CRC in EEPROM */
220 u8 stop; /* indication of end of env data */
221 u8 pre; /* previous EEPROM data bit */
224 addr = CFG_ENV_OFFSET; /* start from first env address */
230 /* calculate runtime CRC according to ML300 and read back
231 old CRC stored in the EEPROM */
232 while (n < CFG_ENV_SIZE) {
233 receive(addr, buffer, len);
235 /* found two null chars, end of env */
236 if ((pre || buffer[0]) == 0)
239 findKey(buffer, &loc, len);
241 /* found old check sum, read and store old CRC */
242 if ((loc == 0 && pre == 'C')
243 || (loc > 0 && buffer[loc - 1] == 'C'))
244 receive(addr + loc + 1, old, 2);
246 pre = buffer[len - 1];
248 /* calculate runtime ML300 CRC */
253 stop = buffer[i] || buffer[i - 1];
255 } while (stop && (i < len));
264 /* exclude old CRC from runtime calculation */
265 crc -= (old[0] + old[1]);
267 /* match CRC values, send back u-boot CRC */
268 if ((old[0] == hex[(crc >> 4) & 0xf])
269 && (old[1] == hex[crc & 0xf])) {
273 CFG_ENV_OFFSET - offsetof(env_t, crc) + offsetof(env_t,
275 /* calculate u-boot crc */
276 while (n < ENV_SIZE) {
277 receive(addr, buffer, len);
278 crc = crc32(crc, buffer, len);
283 memcpy(buffer, &crc, 4);
287 /************************************************************************
288 * Convert IP address to hexadecimals.
291 ip_ml300(uchar * s, uchar * res)
298 for (i = 0; i < 4; i++) {
299 sprintf(temp, "%02x", atoi(s));
300 s = (uchar *)strchr((char *)s, '.') + 1;
301 strcat((char *)res, temp);
305 /************************************************************************
306 * Change 0xff (255), a dummy null char to 0x00.
309 change_null(uchar * s)
312 change_null((uchar *)strchr((char *)s + 1, 255));
313 *(strchr((char *)s, 255)) = '\0';
317 /************************************************************************
318 * Update environment variable name and values to u-boot standard.
323 char *s; /* pointer to env value */
324 char temp[20]; /* temp storage for addresses */
329 sprintf(temp, "%c%c.%c%c.%c%c.%c%c.%c%c.%c%c",
330 s[0], s[1], s[ 2], s[ 3],
331 s[4], s[5], s[ 6], s[ 7],
332 s[8], s[9], s[10], s[11] );
333 setenv("ethaddr", temp);
340 setenv("serial#", s);
347 sprintf(temp, "%d.%d.%d.%d", axtoi((u8 *)s), axtoi((u8 *)(s + 2)),
348 axtoi((u8 *)(s + 4)), axtoi((u8 *)(s + 6)));
349 setenv("ipaddr", temp);
356 sprintf(temp, "%d.%d.%d.%d", axtoi((u8 *)s), axtoi((u8 *)(s + 2)),
357 axtoi((u8 *)(s + 4)), axtoi((u8 *)(s + 6)));
358 setenv("serverip", temp);
365 setenv("bootargs", s);
372 setenv("bootfile", s);
379 setenv("bootcmd", s);
383 /* Don't include C (CRC) */
387 /************************************************************************
388 * Save user modified environment values back to EEPROM.
393 char eprom[ENV_SIZE]; /* buffer to be written back to EEPROM */
395 char ff[] = { 0xff, 0x00 }; /* dummy null value */
396 u32 len; /* length of env to be written to EEPROM */
401 s = getenv("ethaddr");
404 sprintf(temp, "%c%c%c%c%c%c%c%c%c%c%c%c",
405 *s, *(s + 1), *(s + 3), *(s + 4), *(s + 6), *(s + 7),
406 *(s + 9), *(s + 10), *(s + 12), *(s + 13), *(s + 15),
413 s = getenv("serial#");
421 s = getenv("ipaddr");
424 ip_ml300((uchar *)s, (uchar *)temp);
430 s = getenv("serverip");
433 ip_ml300((uchar *)s, (uchar *)temp);
439 s = getenv("bootargs");
447 s = getenv("bootfile");
455 s = getenv("bootcmd");
462 len = strlen(eprom); /* find env length without crc */
463 change_null((uchar *)eprom); /* change 0xff to 0x00 */
465 /* update EEPROM env values if there is enough space */
466 if (update_crc(len, (uchar *)eprom) == 0)
467 send(CFG_ENV_OFFSET, (uchar *)eprom, len + 6);
470 /************************************************************************
471 * U-boot call for EEPROM read associated activities.
474 i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len)
478 /* first read call is for crc */
479 read_crc(buffer, len);
482 } else if (envStep == 1) {
483 /* then read out EEPROM content for runtime u-boot CRC calculation */
484 receive(addr, buffer, len);
486 if (addr + len - CFG_ENV_OFFSET == CFG_ENV_SIZE)
487 /* end of runtime crc read */
493 /* when call getenv_r */
494 receive(addr, buffer, len);
495 } else if (addr + len < CFG_ENV_OFFSET + CFG_ENV_SIZE) {
496 /* calling env_relocate(), but don't read out
497 crc value from EEPROM */
498 receive(addr, buffer + 4, len);
500 receive(addr, buffer + 4, len - 4);
507 /************************************************************************
508 * U-boot call for EEPROM write acativities.
511 i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len)
513 /* save env on last page write called by u-boot */
514 if (addr + len >= CFG_ENV_OFFSET + CFG_ENV_SIZE)
520 /************************************************************************
524 i2c_probe(uchar chip)