env_onenand.c 3.4 KB

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