env_onenand.c 3.3 KB

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