env_dataflash.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. uchar env_get_char_spec (int index)
  35. {
  36. uchar c;
  37. read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t,data),
  38. 1, (char *)&c);
  39. return (c);
  40. }
  41. void env_relocate_spec (void)
  42. {
  43. read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, (char *)env_ptr);
  44. }
  45. int saveenv(void)
  46. {
  47. /* env must be copied to do not alter env structure in memory*/
  48. unsigned char temp[CONFIG_ENV_SIZE];
  49. memcpy(temp, env_ptr, CONFIG_ENV_SIZE);
  50. return write_dataflash(CONFIG_ENV_ADDR, (unsigned long)temp, CONFIG_ENV_SIZE);
  51. }
  52. /************************************************************************
  53. * Initialize Environment use
  54. *
  55. * We are still running from ROM, so data use is limited
  56. * Use a (moderately small) buffer on the stack
  57. */
  58. int env_init(void)
  59. {
  60. ulong crc, len, new;
  61. unsigned off;
  62. uchar buf[64];
  63. if (gd->env_valid == 0){
  64. AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */
  65. /* read old CRC */
  66. read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
  67. sizeof(ulong), (char *)&crc);
  68. new = 0;
  69. len = ENV_SIZE;
  70. off = offsetof(env_t,data);
  71. while (len > 0) {
  72. int n = (len > sizeof(buf)) ? sizeof(buf) : len;
  73. read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf);
  74. new = crc32 (new, buf, n);
  75. len -= n;
  76. off += n;
  77. }
  78. if (crc == new) {
  79. gd->env_addr = offsetof(env_t,data);
  80. gd->env_valid = 1;
  81. } else {
  82. gd->env_addr = (ulong)&default_environment[0];
  83. gd->env_valid = 0;
  84. }
  85. }
  86. return (0);
  87. }