env_nvram.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /*
  26. * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
  27. *
  28. * It might not be possible in all cases to use 'memcpy()' to copy
  29. * the environment to NVRAM, as the NVRAM might not be mapped into
  30. * the memory space. (I.e. this is the case for the BAB750). In those
  31. * cases it might be possible to access the NVRAM using a different
  32. * method. For example, the RTC on the BAB750 is accessible in IO
  33. * space using its address and data registers. To enable usage of
  34. * NVRAM in those cases I invented the functions 'nvram_read()' and
  35. * 'nvram_write()', which will be activated upon the configuration
  36. * #define CFG_NVRAM_ACCESS_ROUTINE. Note, that those functions are
  37. * strongly dependent on the used HW, and must be redefined for each
  38. * board that wants to use them.
  39. */
  40. #include <common.h>
  41. #ifdef CFG_ENV_IS_IN_NVRAM /* Environment is in NVRAM */
  42. #include <command.h>
  43. #include <environment.h>
  44. #include <linux/stddef.h>
  45. #include <malloc.h>
  46. #ifdef CFG_NVRAM_ACCESS_ROUTINE
  47. extern void *nvram_read(void *dest, const long src, size_t count);
  48. extern void nvram_write(long dest, const void *src, size_t count);
  49. env_t *env_ptr = NULL;
  50. #else
  51. env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
  52. #endif
  53. char * env_name_spec = "NVRAM";
  54. extern uchar default_environment[];
  55. extern int default_environment_size;
  56. extern uchar (*env_get_char)(int);
  57. extern uchar env_get_char_memory (int index);
  58. #ifdef CONFIG_AMIGAONEG3SE
  59. uchar env_get_char_spec (int index)
  60. {
  61. #ifdef CFG_NVRAM_ACCESS_ROUTINE
  62. uchar c;
  63. nvram_read(&c, CFG_ENV_ADDR+index, 1);
  64. return c;
  65. #else
  66. DECLARE_GLOBAL_DATA_PTR;
  67. uchar retval;
  68. enable_nvram();
  69. retval = *((uchar *)(gd->env_addr + index));
  70. disable_nvram();
  71. return retval;
  72. #endif
  73. }
  74. #else
  75. uchar env_get_char_spec (int index)
  76. {
  77. #ifdef CFG_NVRAM_ACCESS_ROUTINE
  78. uchar c;
  79. nvram_read(&c, CFG_ENV_ADDR+index, 1);
  80. return c;
  81. #else
  82. DECLARE_GLOBAL_DATA_PTR;
  83. return *((uchar *)(gd->env_addr + index));
  84. #endif
  85. }
  86. #endif
  87. void env_relocate_spec (void)
  88. {
  89. #if defined(CFG_NVRAM_ACCESS_ROUTINE)
  90. nvram_read(env_ptr, CFG_ENV_ADDR, CFG_ENV_SIZE);
  91. #else
  92. memcpy (env_ptr, (void*)CFG_ENV_ADDR, CFG_ENV_SIZE);
  93. #endif
  94. }
  95. int saveenv (void)
  96. {
  97. int rcode = 0;
  98. #ifdef CONFIG_AMIGAONEG3SE
  99. enable_nvram();
  100. #endif
  101. #ifdef CFG_NVRAM_ACCESS_ROUTINE
  102. nvram_write(CFG_ENV_ADDR, env_ptr, CFG_ENV_SIZE);
  103. #else
  104. if (memcpy ((char *)CFG_ENV_ADDR, env_ptr, CFG_ENV_SIZE) == NULL)
  105. rcode = 1 ;
  106. #endif
  107. #ifdef CONFIG_AMIGAONEG3SE
  108. udelay(10000);
  109. disable_nvram();
  110. #endif
  111. return rcode;
  112. }
  113. /************************************************************************
  114. * Initialize Environment use
  115. *
  116. * We are still running from ROM, so data use is limited
  117. */
  118. int env_init (void)
  119. {
  120. DECLARE_GLOBAL_DATA_PTR;
  121. #ifdef CONFIG_AMIGAONEG3SE
  122. enable_nvram();
  123. #endif
  124. #if defined(CFG_NVRAM_ACCESS_ROUTINE)
  125. ulong crc;
  126. uchar data[ENV_SIZE];
  127. nvram_read (&crc, CFG_ENV_ADDR, sizeof(ulong));
  128. nvram_read (data, CFG_ENV_ADDR+sizeof(ulong), ENV_SIZE);
  129. if (crc32(0, data, ENV_SIZE) == crc) {
  130. gd->env_addr = (ulong)CFG_ENV_ADDR + sizeof(long);
  131. #else
  132. if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
  133. gd->env_addr = (ulong)&(env_ptr->data);
  134. #endif
  135. gd->env_valid = 1;
  136. } else {
  137. gd->env_addr = (ulong)&default_environment[0];
  138. gd->env_valid = 0;
  139. }
  140. #ifdef CONFIG_AMIGAONEG3SE
  141. disable_nvram();
  142. #endif
  143. return (0);
  144. }
  145. #endif /* CFG_ENV_IS_IN_NVRAM */