env_onenand.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. char *env_name_spec = "OneNAND";
  39. #define ONENAND_MAX_ENV_SIZE 4096
  40. #define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
  41. DECLARE_GLOBAL_DATA_PTR;
  42. uchar env_get_char_spec(int index)
  43. {
  44. return (*((uchar *)(gd->env_addr + index)));
  45. }
  46. void env_relocate_spec(void)
  47. {
  48. struct mtd_info *mtd = &onenand_mtd;
  49. #ifdef CONFIG_ENV_ADDR_FLEX
  50. struct onenand_chip *this = &onenand_chip;
  51. #endif
  52. int rc;
  53. size_t retlen;
  54. #ifdef ENV_IS_EMBEDDED
  55. char *buf = (char *)&environment;
  56. #else
  57. loff_t env_addr = CONFIG_ENV_ADDR;
  58. char onenand_env[ONENAND_MAX_ENV_SIZE];
  59. char *buf = (char *)&onenand_env[0];
  60. #endif /* ENV_IS_EMBEDDED */
  61. #ifndef ENV_IS_EMBEDDED
  62. # ifdef CONFIG_ENV_ADDR_FLEX
  63. if (FLEXONENAND(this))
  64. env_addr = CONFIG_ENV_ADDR_FLEX;
  65. # endif
  66. /* Check OneNAND exist */
  67. if (mtd->writesize)
  68. /* Ignore read fail */
  69. mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
  70. &retlen, (u_char *)buf);
  71. else
  72. mtd->writesize = MAX_ONENAND_PAGESIZE;
  73. #endif /* !ENV_IS_EMBEDDED */
  74. rc = env_import(buf, 1);
  75. if (rc)
  76. gd->env_valid = 1;
  77. }
  78. int saveenv(void)
  79. {
  80. env_t env_new;
  81. ssize_t len;
  82. char *res;
  83. struct mtd_info *mtd = &onenand_mtd;
  84. #ifdef CONFIG_ENV_ADDR_FLEX
  85. struct onenand_chip *this = &onenand_chip;
  86. #endif
  87. loff_t env_addr = CONFIG_ENV_ADDR;
  88. size_t retlen;
  89. struct erase_info instr = {
  90. .callback = NULL,
  91. };
  92. res = (char *)&env_new.data;
  93. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
  94. if (len < 0) {
  95. error("Cannot export environment: errno = %d\n", errno);
  96. return 1;
  97. }
  98. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  99. instr.len = CONFIG_ENV_SIZE;
  100. #ifdef CONFIG_ENV_ADDR_FLEX
  101. if (FLEXONENAND(this)) {
  102. env_addr = CONFIG_ENV_ADDR_FLEX;
  103. instr.len = CONFIG_ENV_SIZE_FLEX;
  104. instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
  105. 1 : 0;
  106. }
  107. #endif
  108. instr.addr = env_addr;
  109. instr.mtd = mtd;
  110. if (mtd->erase(mtd, &instr)) {
  111. printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
  112. return 1;
  113. }
  114. if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
  115. (u_char *)&env_new)) {
  116. printf("OneNAND: write failed at 0x%llx\n", instr.addr);
  117. return 2;
  118. }
  119. return 0;
  120. }
  121. int env_init(void)
  122. {
  123. /* use default */
  124. gd->env_addr = (ulong) & default_environment[0];
  125. gd->env_valid = 1;
  126. return 0;
  127. }