env_onenand.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * (C) Copyright 2010 DENX Software Engineering
  3. * Wolfgang Denk <wd@denx.de>
  4. *
  5. * (C) Copyright 2005-2009 Samsung Electronics
  6. * Kyungmin Park <kyungmin.park@samsung.com>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <environment.h>
  29. #include <linux/stddef.h>
  30. #include <malloc.h>
  31. #include <search.h>
  32. #include <errno.h>
  33. #include <linux/mtd/compat.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/onenand.h>
  36. extern struct mtd_info onenand_mtd;
  37. extern struct onenand_chip onenand_chip;
  38. /* References to names in env_common.c */
  39. extern uchar default_environment[];
  40. char *env_name_spec = "OneNAND";
  41. #define ONENAND_MAX_ENV_SIZE 4096
  42. #define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
  43. #ifdef ENV_IS_EMBEDDED
  44. extern uchar environment[];
  45. #endif /* ENV_IS_EMBEDDED */
  46. DECLARE_GLOBAL_DATA_PTR;
  47. uchar env_get_char_spec(int index)
  48. {
  49. return (*((uchar *)(gd->env_addr + index)));
  50. }
  51. void env_relocate_spec(void)
  52. {
  53. struct mtd_info *mtd = &onenand_mtd;
  54. #ifdef CONFIG_ENV_ADDR_FLEX
  55. struct onenand_chip *this = &onenand_chip;
  56. #endif
  57. int rc;
  58. size_t retlen;
  59. #ifdef ENV_IS_EMBEDDED
  60. char *buf = (char *)&environment[0];
  61. #else
  62. loff_t env_addr = CONFIG_ENV_ADDR;
  63. char onenand_env[ONENAND_MAX_ENV_SIZE];
  64. char *buf = (char *)&onenand_env[0];
  65. #endif /* ENV_IS_EMBEDDED */
  66. #ifndef ENV_IS_EMBEDDED
  67. # ifdef CONFIG_ENV_ADDR_FLEX
  68. if (FLEXONENAND(this))
  69. env_addr = CONFIG_ENV_ADDR_FLEX;
  70. # endif
  71. /* Check OneNAND exist */
  72. if (mtd->writesize)
  73. /* Ignore read fail */
  74. mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
  75. &retlen, (u_char *)buf);
  76. else
  77. mtd->writesize = MAX_ONENAND_PAGESIZE;
  78. #endif /* !ENV_IS_EMBEDDED */
  79. rc = env_import(buf, 1);
  80. if (rc)
  81. gd->env_valid = 1;
  82. }
  83. int saveenv(void)
  84. {
  85. env_t env_new;
  86. ssize_t len;
  87. char *res;
  88. struct mtd_info *mtd = &onenand_mtd;
  89. #ifdef CONFIG_ENV_ADDR_FLEX
  90. struct onenand_chip *this = &onenand_chip;
  91. #endif
  92. loff_t env_addr = CONFIG_ENV_ADDR;
  93. size_t retlen;
  94. struct erase_info instr = {
  95. .callback = NULL,
  96. };
  97. res = (char *)&env_new.data;
  98. len = hexport('\0', &res, ENV_SIZE);
  99. if (len < 0) {
  100. error("Cannot export environment: errno = %d\n", errno);
  101. return 1;
  102. }
  103. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  104. instr.len = CONFIG_ENV_SIZE;
  105. #ifdef CONFIG_ENV_ADDR_FLEX
  106. if (FLEXONENAND(this)) {
  107. env_addr = CONFIG_ENV_ADDR_FLEX;
  108. instr.len = CONFIG_ENV_SIZE_FLEX;
  109. instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
  110. 1 : 0;
  111. }
  112. #endif
  113. instr.addr = env_addr;
  114. instr.mtd = mtd;
  115. if (mtd->erase(mtd, &instr)) {
  116. printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
  117. return 1;
  118. }
  119. if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
  120. (u_char *)&env_new)) {
  121. printf("OneNAND: write failed at 0x%llx\n", instr.addr);
  122. return 2;
  123. }
  124. return 0;
  125. }
  126. int env_init(void)
  127. {
  128. /* use default */
  129. gd->env_addr = (ulong) & default_environment[0];
  130. gd->env_valid = 1;
  131. return 0;
  132. }