env_eeprom.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. unsigned int off;
  86. off = CONFIG_ENV_OFFSET;
  87. #ifdef CONFIG_ENV_OFFSET_REDUND
  88. if (gd->env_valid == 2)
  89. off = CONFIG_ENV_OFFSET_REDUND;
  90. #endif
  91. eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
  92. off + index + offsetof(env_t,data),
  93. &c, 1);
  94. return (c);
  95. }
  96. void env_relocate_spec (void)
  97. {
  98. unsigned int off = CONFIG_ENV_OFFSET;
  99. #ifdef CONFIG_ENV_OFFSET_REDUND
  100. if (gd->env_valid == 2)
  101. off = CONFIG_ENV_OFFSET_REDUND;
  102. #endif
  103. eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
  104. off,
  105. (uchar*)env_ptr,
  106. CONFIG_ENV_SIZE);
  107. }
  108. int saveenv(void)
  109. {
  110. int rc;
  111. unsigned int off = CONFIG_ENV_OFFSET;
  112. #ifdef CONFIG_ENV_OFFSET_REDUND
  113. unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
  114. char flag_obsolete = OBSOLETE_FLAG;
  115. if (gd->env_valid == 1) {
  116. off = CONFIG_ENV_OFFSET_REDUND;
  117. off_red = CONFIG_ENV_OFFSET;
  118. }
  119. env_ptr->flags = ACTIVE_FLAG;
  120. #endif
  121. rc = eeprom_bus_write (CONFIG_SYS_DEF_EEPROM_ADDR,
  122. off,
  123. (uchar *)env_ptr,
  124. CONFIG_ENV_SIZE);
  125. #ifdef CONFIG_ENV_OFFSET_REDUND
  126. if (rc == 0) {
  127. eeprom_bus_write (CONFIG_SYS_DEF_EEPROM_ADDR,
  128. off_red + offsetof(env_t,flags),
  129. (uchar *)&flag_obsolete,
  130. 1);
  131. if (gd->env_valid == 1)
  132. gd->env_valid = 2;
  133. else
  134. gd->env_valid = 1;
  135. }
  136. #endif
  137. return rc;
  138. }
  139. /************************************************************************
  140. * Initialize Environment use
  141. *
  142. * We are still running from ROM, so data use is limited
  143. * Use a (moderately small) buffer on the stack
  144. */
  145. #ifdef CONFIG_ENV_OFFSET_REDUND
  146. int env_init(void)
  147. {
  148. ulong len;
  149. ulong crc[2], crc_tmp;
  150. unsigned int off, off_env[2];
  151. uchar buf[64];
  152. int crc_ok[2] = {0,0};
  153. unsigned char flags[2];
  154. int i;
  155. eeprom_init (); /* prepare for EEPROM read/write */
  156. off_env[0] = CONFIG_ENV_OFFSET;
  157. off_env[1] = CONFIG_ENV_OFFSET_REDUND;
  158. for (i = 0; i < 2; i++) {
  159. /* read CRC */
  160. eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
  161. off_env[i] + offsetof(env_t,crc),
  162. (uchar *)&crc[i], sizeof(ulong));
  163. /* read FLAGS */
  164. eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
  165. off_env[i] + offsetof(env_t,flags),
  166. (uchar *)&flags[i], sizeof(uchar));
  167. crc_tmp= 0;
  168. len = ENV_SIZE;
  169. off = off_env[i] + offsetof(env_t,data);
  170. while (len > 0) {
  171. int n = (len > sizeof(buf)) ? sizeof(buf) : len;
  172. eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR, off,
  173. buf, n);
  174. crc_tmp = crc32 (crc_tmp, buf, n);
  175. len -= n;
  176. off += n;
  177. }
  178. if (crc_tmp == crc[i])
  179. crc_ok[i] = 1;
  180. }
  181. if (!crc_ok[0] && !crc_ok[1]) {
  182. gd->env_addr = 0;
  183. gd->env_valid = 0;
  184. return 0;
  185. } else if (crc_ok[0] && !crc_ok[1]) {
  186. gd->env_valid = 1;
  187. }
  188. else if (!crc_ok[0] && crc_ok[1]) {
  189. gd->env_valid = 2;
  190. } else {
  191. /* both ok - check serial */
  192. if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
  193. gd->env_valid = 1;
  194. else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
  195. gd->env_valid = 2;
  196. else if (flags[0] == 0xFF && flags[1] == 0)
  197. gd->env_valid = 2;
  198. else if(flags[1] == 0xFF && flags[0] == 0)
  199. gd->env_valid = 1;
  200. else /* flags are equal - almost impossible */
  201. gd->env_valid = 1;
  202. }
  203. if (gd->env_valid == 2)
  204. gd->env_addr = off_env[1] + offsetof(env_t,data);
  205. else if (gd->env_valid == 1)
  206. gd->env_addr = off_env[0] + offsetof(env_t,data);
  207. return (0);
  208. }
  209. #else
  210. int env_init(void)
  211. {
  212. ulong crc, len, new;
  213. unsigned off;
  214. uchar buf[64];
  215. eeprom_init (); /* prepare for EEPROM read/write */
  216. /* read old CRC */
  217. eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
  218. CONFIG_ENV_OFFSET+offsetof(env_t,crc),
  219. (uchar *)&crc, sizeof(ulong));
  220. new = 0;
  221. len = ENV_SIZE;
  222. off = offsetof(env_t,data);
  223. while (len > 0) {
  224. int n = (len > sizeof(buf)) ? sizeof(buf) : len;
  225. eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
  226. CONFIG_ENV_OFFSET + off, buf, n);
  227. new = crc32 (new, buf, n);
  228. len -= n;
  229. off += n;
  230. }
  231. if (crc == new) {
  232. gd->env_addr = offsetof(env_t,data);
  233. gd->env_valid = 1;
  234. } else {
  235. gd->env_addr = 0;
  236. gd->env_valid = 0;
  237. }
  238. return (0);
  239. }
  240. #endif