env_dataflash.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include <command.h>
  22. #include <environment.h>
  23. #include <linux/stddef.h>
  24. #include <dataflash.h>
  25. DECLARE_GLOBAL_DATA_PTR;
  26. env_t *env_ptr = NULL;
  27. char * env_name_spec = "dataflash";
  28. extern int read_dataflash (unsigned long addr, unsigned long size, char
  29. *result);
  30. extern int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
  31. unsigned long size);
  32. extern int AT91F_DataflashInit (void);
  33. extern uchar default_environment[];
  34. /* extern int default_environment_size; */
  35. uchar env_get_char_spec (int index)
  36. {
  37. uchar c;
  38. read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t,data),
  39. 1, (char *)&c);
  40. return (c);
  41. }
  42. void env_relocate_spec (void)
  43. {
  44. read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, (char *)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[CONFIG_ENV_SIZE];
  50. memcpy(temp, env_ptr, CONFIG_ENV_SIZE);
  51. return write_dataflash(CONFIG_ENV_ADDR, (unsigned long)temp, CONFIG_ENV_SIZE);
  52. }
  53. /************************************************************************
  54. * Initialize Environment use
  55. *
  56. * We are still running from ROM, so data use is limited
  57. * Use a (moderately small) buffer on the stack
  58. */
  59. int env_init(void)
  60. {
  61. ulong crc, len, new;
  62. unsigned off;
  63. uchar buf[64];
  64. if (gd->env_valid == 0){
  65. AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */
  66. /* read old CRC */
  67. read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
  68. sizeof(ulong), (char *)&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(CONFIG_ENV_ADDR + off, n, (char *)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. }