env_sf.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. *
  8. * (C) Copyright 2008 Atmel Corporation
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. #include <environment.h>
  30. #include <malloc.h>
  31. #include <spi_flash.h>
  32. #ifndef CONFIG_ENV_SPI_BUS
  33. # define CONFIG_ENV_SPI_BUS 0
  34. #endif
  35. #ifndef CONFIG_ENV_SPI_CS
  36. # define CONFIG_ENV_SPI_CS 0
  37. #endif
  38. #ifndef CONFIG_ENV_SPI_MAX_HZ
  39. # define CONFIG_ENV_SPI_MAX_HZ 1000000
  40. #endif
  41. #ifndef CONFIG_ENV_SPI_MODE
  42. # define CONFIG_ENV_SPI_MODE SPI_MODE_3
  43. #endif
  44. DECLARE_GLOBAL_DATA_PTR;
  45. /* references to names in env_common.c */
  46. extern uchar default_environment[];
  47. char * env_name_spec = "SPI Flash";
  48. env_t *env_ptr;
  49. static struct spi_flash *env_flash;
  50. uchar env_get_char_spec(int index)
  51. {
  52. return *((uchar *)(gd->env_addr + index));
  53. }
  54. int saveenv(void)
  55. {
  56. u32 saved_size, saved_offset;
  57. char *saved_buffer = NULL;
  58. u32 sector = 1;
  59. int ret;
  60. if (!env_flash) {
  61. puts("Environment SPI flash not initialized\n");
  62. return 1;
  63. }
  64. /* Is the sector larger than the env (i.e. embedded) */
  65. if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
  66. saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
  67. saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
  68. saved_buffer = malloc(saved_size);
  69. if (!saved_buffer) {
  70. ret = 1;
  71. goto done;
  72. }
  73. ret = spi_flash_read(env_flash, saved_offset, saved_size, saved_buffer);
  74. if (ret)
  75. goto done;
  76. }
  77. if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
  78. sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
  79. if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
  80. sector++;
  81. }
  82. puts("Erasing SPI flash...");
  83. ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE);
  84. if (ret)
  85. goto done;
  86. puts("Writing to SPI flash...");
  87. ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr);
  88. if (ret)
  89. goto done;
  90. if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
  91. ret = spi_flash_write(env_flash, saved_offset, saved_size, saved_buffer);
  92. if (ret)
  93. goto done;
  94. }
  95. ret = 0;
  96. puts("done\n");
  97. done:
  98. if (saved_buffer)
  99. free(saved_buffer);
  100. return ret;
  101. }
  102. void env_relocate_spec(void)
  103. {
  104. int ret;
  105. env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
  106. CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
  107. if (!env_flash)
  108. goto err_probe;
  109. ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr);
  110. if (ret)
  111. goto err_read;
  112. if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
  113. goto err_crc;
  114. gd->env_valid = 1;
  115. return;
  116. err_read:
  117. spi_flash_free(env_flash);
  118. env_flash = NULL;
  119. err_probe:
  120. err_crc:
  121. puts("*** Warning - bad CRC, using default environment\n\n");
  122. set_default_env();
  123. }
  124. int env_init(void)
  125. {
  126. /* SPI flash isn't usable before relocation */
  127. gd->env_addr = (ulong)&default_environment[0];
  128. gd->env_valid = 1;
  129. return 0;
  130. }