env_eeprom.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  6. * Andreas Heppel <aheppel@sysgo.de>
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <command.h>
  27. #include <environment.h>
  28. #include <linux/stddef.h>
  29. #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
  30. #include <i2c.h>
  31. #endif
  32. DECLARE_GLOBAL_DATA_PTR;
  33. env_t *env_ptr = NULL;
  34. char * env_name_spec = "EEPROM";
  35. int env_eeprom_bus = -1;
  36. static int eeprom_bus_read (unsigned dev_addr, unsigned offset, uchar *buffer,
  37. unsigned cnt)
  38. {
  39. int rcode;
  40. #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
  41. int old_bus = i2c_get_bus_num();
  42. if (gd->flags & GD_FLG_RELOC) {
  43. if (env_eeprom_bus == -1) {
  44. I2C_MUX_DEVICE *dev = NULL;
  45. dev = i2c_mux_ident_muxstring(
  46. (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
  47. if (dev != NULL) {
  48. env_eeprom_bus = dev->busid;
  49. } else
  50. printf ("error adding env eeprom bus.\n");
  51. }
  52. if (old_bus != env_eeprom_bus) {
  53. i2c_set_bus_num(env_eeprom_bus);
  54. old_bus = env_eeprom_bus;
  55. }
  56. } else {
  57. rcode = i2c_mux_ident_muxstring_f(
  58. (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
  59. }
  60. #endif
  61. rcode = eeprom_read (dev_addr, offset, buffer, cnt);
  62. #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
  63. if (old_bus != env_eeprom_bus)
  64. i2c_set_bus_num(old_bus);
  65. #endif
  66. return rcode;
  67. }
  68. static int eeprom_bus_write (unsigned dev_addr, unsigned offset, uchar *buffer,
  69. unsigned cnt)
  70. {
  71. int rcode;
  72. #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
  73. int old_bus = i2c_get_bus_num();
  74. rcode = i2c_mux_ident_muxstring_f((uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
  75. #endif
  76. rcode = eeprom_write (dev_addr, offset, buffer, cnt);
  77. #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
  78. i2c_set_bus_num(old_bus);
  79. #endif
  80. return rcode;
  81. }
  82. uchar env_get_char_spec (int index)
  83. {
  84. uchar c;
  85. eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
  86. CONFIG_ENV_OFFSET+index+offsetof(env_t,data),
  87. &c, 1);
  88. return (c);
  89. }
  90. void env_relocate_spec (void)
  91. {
  92. eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
  93. CONFIG_ENV_OFFSET,
  94. (uchar*)env_ptr,
  95. CONFIG_ENV_SIZE);
  96. }
  97. int saveenv(void)
  98. {
  99. return eeprom_bus_write (CONFIG_SYS_DEF_EEPROM_ADDR,
  100. CONFIG_ENV_OFFSET,
  101. (uchar *)env_ptr,
  102. CONFIG_ENV_SIZE);
  103. }
  104. /************************************************************************
  105. * Initialize Environment use
  106. *
  107. * We are still running from ROM, so data use is limited
  108. * Use a (moderately small) buffer on the stack
  109. */
  110. int env_init(void)
  111. {
  112. ulong crc, len, new;
  113. unsigned off;
  114. uchar buf[64];
  115. eeprom_init (); /* prepare for EEPROM read/write */
  116. /* read old CRC */
  117. eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
  118. CONFIG_ENV_OFFSET+offsetof(env_t,crc),
  119. (uchar *)&crc, sizeof(ulong));
  120. new = 0;
  121. len = ENV_SIZE;
  122. off = offsetof(env_t,data);
  123. while (len > 0) {
  124. int n = (len > sizeof(buf)) ? sizeof(buf) : len;
  125. eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
  126. CONFIG_ENV_OFFSET + off, buf, n);
  127. new = crc32 (new, buf, n);
  128. len -= n;
  129. off += n;
  130. }
  131. if (crc == new) {
  132. gd->env_addr = offsetof(env_t,data);
  133. gd->env_valid = 1;
  134. } else {
  135. gd->env_addr = 0;
  136. gd->env_valid = 0;
  137. }
  138. return (0);
  139. }