load_sernum_ethaddr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * (C) Copyright 2000, 2001, 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2005
  6. * Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /* #define DEBUG */
  27. #include <common.h>
  28. #include <net.h>
  29. #define I2C_CHIP 0x50 /* I2C bus address of onboard EEPROM */
  30. #define I2C_ALEN 1 /* length of EEPROM addresses in bytes */
  31. #define I2C_OFFSET 0x0 /* start address of manufacturere data block
  32. * in EEPROM */
  33. /* 64 Byte manufacturer data block in EEPROM */
  34. struct manufacturer_data {
  35. unsigned int serial_number; /* serial number (0...999999) */
  36. unsigned short hardware; /* hardware version (e.g. V1.02) */
  37. unsigned short manuf_date; /* manufacture date (e.g. 25/02) */
  38. unsigned char name[20]; /* device name (in CHIP.INI) */
  39. unsigned char macadr[6]; /* MAC address */
  40. signed char a_kal[4]; /* calibration value for U */
  41. signed char i_kal[4]; /* calibration value for I */
  42. unsigned char reserve[18]; /* reserved */
  43. unsigned short save_nr; /* save count */
  44. unsigned short chksum; /* checksum */
  45. };
  46. int i2c_read (unsigned char chip, unsigned int addr, int alen,
  47. unsigned char *buffer, int len);
  48. /*-----------------------------------------------------------------------
  49. * Process manufacturer data block in EEPROM:
  50. *
  51. * If we boot on a system fresh from factory, check if the manufacturer data
  52. * in the EEPROM is valid and save some information it contains.
  53. *
  54. * CMC manufacturer data is defined as follows:
  55. *
  56. * - located in the onboard EEPROM
  57. * - starts at offset 0x0
  58. * - size 0x00000040
  59. *
  60. * Internal structure: see struct definition
  61. */
  62. int misc_init_r(void)
  63. {
  64. struct manufacturer_data data;
  65. char serial [9];
  66. unsigned short chksum;
  67. unsigned char *p;
  68. unsigned short i;
  69. #if !defined(CONFIG_HARD_I2C) && !defined(CONFIG_SOFT_I2C)
  70. #error you must define some I2C support (CONFIG_HARD_I2C or CONFIG_SOFT_I2C)
  71. #endif
  72. if (i2c_read(I2C_CHIP, I2C_OFFSET, I2C_ALEN, (unsigned char *)&data,
  73. sizeof(data)) != 0) {
  74. puts ("Error reading manufacturer data from EEPROM\n");
  75. return -1;
  76. }
  77. /* check if manufacturer data block is valid */
  78. p = (unsigned char *)&data;
  79. chksum = 0;
  80. for (i = 0; i < (sizeof(data) - sizeof(data.chksum)); i++)
  81. chksum += *p++;
  82. debug ("checksum of manufacturer data block: %#.4x\n", chksum);
  83. if (chksum != data.chksum) {
  84. puts ("Error: manufacturer data block has invalid checksum\n");
  85. return -1;
  86. }
  87. /* copy serial number */
  88. sprintf (serial, "%d", data.serial_number);
  89. /* set serial# and ethaddr if not yet defined */
  90. if (getenv("serial#") == NULL) {
  91. setenv ("serial#", serial);
  92. }
  93. if (getenv("ethaddr") == NULL) {
  94. eth_setenv_enetaddr("ethaddr", data.macadr);
  95. }
  96. return 0;
  97. }