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