env_dataflash.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* LowLevel function for DataFlash environment support
  2. * Author : Gilles Gastaldi (Atmel)
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  17. * MA 02111-1307 USA
  18. *
  19. */
  20. #include <common.h>
  21. #if defined(CFG_ENV_IS_IN_DATAFLASH) /* Environment is in DataFlash */
  22. #include <command.h>
  23. #include <environment.h>
  24. #include <linux/stddef.h>
  25. #include <dataflash.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. env_t *env_ptr = NULL;
  28. char * env_name_spec = "dataflash";
  29. extern int read_dataflash (unsigned long addr, unsigned long size, char
  30. *result);
  31. extern int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
  32. unsigned long size);
  33. extern int AT91F_DataflashInit (void);
  34. extern uchar default_environment[];
  35. /* extern int default_environment_size; */
  36. uchar env_get_char_spec (int index)
  37. {
  38. uchar c;
  39. read_dataflash (CFG_ENV_ADDR+index+offsetof(env_t,data),1,&c);
  40. return (c);
  41. }
  42. void env_relocate_spec (void)
  43. {
  44. read_dataflash (CFG_ENV_ADDR,CFG_ENV_SIZE,(uchar *)env_ptr);
  45. }
  46. int saveenv(void)
  47. {
  48. /* env must be copied to do not alter env structure in memory*/
  49. unsigned char temp[CFG_ENV_SIZE];
  50. int i;
  51. memcpy(temp, env_ptr, CFG_ENV_SIZE);
  52. return write_dataflash (CFG_ENV_ADDR, (unsigned long)temp, CFG_ENV_SIZE);
  53. }
  54. /************************************************************************
  55. * Initialize Environment use
  56. *
  57. * We are still running from ROM, so data use is limited
  58. * Use a (moderately small) buffer on the stack
  59. */
  60. int env_init(void)
  61. {
  62. ulong crc, len, new;
  63. unsigned off;
  64. uchar buf[64];
  65. if (gd->env_valid == 0){
  66. AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */
  67. /* read old CRC */
  68. read_dataflash (CFG_ENV_ADDR+offsetof(env_t,crc),sizeof(ulong),&crc);
  69. new = 0;
  70. len = ENV_SIZE;
  71. off = offsetof(env_t,data);
  72. while (len > 0) {
  73. int n = (len > sizeof(buf)) ? sizeof(buf) : len;
  74. read_dataflash (CFG_ENV_ADDR+off,n , buf);
  75. new = crc32 (new, buf, n);
  76. len -= n;
  77. off += n;
  78. }
  79. if (crc == new) {
  80. gd->env_addr = offsetof(env_t,data);
  81. gd->env_valid = 1;
  82. } else {
  83. gd->env_addr = (ulong)&default_environment[0];
  84. gd->env_valid = 0;
  85. }
  86. }
  87. return (0);
  88. }
  89. #endif /* CFG_ENV_IS_IN_DATAFLASH */