Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2005-2007 Samsung Electronics |
| 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> |
| 25 | |
| 26 | #if defined(CFG_ENV_IS_IN_ONENAND) /* Environment is in OneNAND */ |
| 27 | |
| 28 | #include <command.h> |
| 29 | #include <environment.h> |
| 30 | #include <linux/stddef.h> |
| 31 | #include <malloc.h> |
| 32 | |
| 33 | #include <linux/mtd/compat.h> |
| 34 | #include <linux/mtd/mtd.h> |
| 35 | #include <linux/mtd/onenand.h> |
| 36 | |
| 37 | extern struct mtd_info onenand_mtd; |
| 38 | extern struct onenand_chip onenand_chip; |
| 39 | |
| 40 | /* References to names in env_common.c */ |
| 41 | extern uchar default_environment[]; |
| 42 | |
| 43 | #define ONENAND_ENV_SIZE(mtd) (mtd.oobblock - ENV_HEADER_SIZE) |
| 44 | |
| 45 | char *env_name_spec = "OneNAND"; |
| 46 | |
| 47 | #ifdef ENV_IS_EMBEDDED |
| 48 | extern uchar environment[]; |
| 49 | env_t *env_ptr = (env_t *) (&environment[0]); |
| 50 | #else /* ! ENV_IS_EMBEDDED */ |
| 51 | static unsigned char onenand_env[MAX_ONENAND_PAGESIZE]; |
| 52 | env_t *env_ptr = (env_t *) onenand_env; |
| 53 | #endif /* ENV_IS_EMBEDDED */ |
| 54 | |
Kyungmin Park | a9da2b4 | 2008-03-31 10:40:19 +0900 | [diff] [blame] | 55 | DECLARE_GLOBAL_DATA_PTR; |
| 56 | |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 57 | uchar env_get_char_spec(int index) |
| 58 | { |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 59 | return (*((uchar *) (gd->env_addr + index))); |
| 60 | } |
| 61 | |
| 62 | void env_relocate_spec(void) |
| 63 | { |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 64 | unsigned long env_addr; |
| 65 | int use_default = 0; |
Peter Pearse | 2ae64f5 | 2007-11-15 08:58:00 +0000 | [diff] [blame] | 66 | size_t retlen; |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 67 | |
| 68 | env_addr = CFG_ENV_ADDR; |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 69 | |
| 70 | /* Check OneNAND exist */ |
| 71 | if (onenand_mtd.oobblock) |
| 72 | /* Ignore read fail */ |
| 73 | onenand_read(&onenand_mtd, env_addr, onenand_mtd.oobblock, |
| 74 | &retlen, (u_char *) env_ptr); |
| 75 | else |
| 76 | onenand_mtd.oobblock = MAX_ONENAND_PAGESIZE; |
| 77 | |
| 78 | if (crc32(0, env_ptr->data, ONENAND_ENV_SIZE(onenand_mtd)) != |
| 79 | env_ptr->crc) |
| 80 | use_default = 1; |
| 81 | |
| 82 | if (use_default) { |
| 83 | memcpy(env_ptr->data, default_environment, |
| 84 | ONENAND_ENV_SIZE(onenand_mtd)); |
| 85 | env_ptr->crc = |
| 86 | crc32(0, env_ptr->data, ONENAND_ENV_SIZE(onenand_mtd)); |
| 87 | } |
| 88 | |
| 89 | gd->env_addr = (ulong) & env_ptr->data; |
| 90 | gd->env_valid = 1; |
| 91 | } |
| 92 | |
| 93 | int saveenv(void) |
| 94 | { |
| 95 | unsigned long env_addr = CFG_ENV_ADDR; |
Kyungmin Park | a9da2b4 | 2008-03-31 10:40:19 +0900 | [diff] [blame] | 96 | struct erase_info instr = { |
| 97 | .callback = NULL, |
| 98 | }; |
Peter Pearse | 2ae64f5 | 2007-11-15 08:58:00 +0000 | [diff] [blame] | 99 | size_t retlen; |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 100 | |
| 101 | instr.len = CFG_ENV_SIZE; |
| 102 | instr.addr = env_addr; |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 103 | if (onenand_erase(&onenand_mtd, &instr)) { |
Jean-Christophe PLAGNIOL-VILLARD | 0a5676b | 2008-07-12 14:36:34 +0200 | [diff] [blame] | 104 | printf("OneNAND: erase failed at 0x%08lx\n", env_addr); |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | /* update crc */ |
| 109 | env_ptr->crc = |
Kyungmin Park | a9da2b4 | 2008-03-31 10:40:19 +0900 | [diff] [blame] | 110 | crc32(0, env_ptr->data, ONENAND_ENV_SIZE(onenand_mtd)); |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 111 | |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 112 | if (onenand_write(&onenand_mtd, env_addr, onenand_mtd.oobblock, &retlen, |
| 113 | (u_char *) env_ptr)) { |
| 114 | printf("OneNAND: write failed at 0x%08x\n", instr.addr); |
| 115 | return 2; |
| 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int env_init(void) |
| 122 | { |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 123 | /* use default */ |
| 124 | gd->env_addr = (ulong) & default_environment[0]; |
| 125 | gd->env_valid = 1; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | #endif /* CFG_ENV_IS_IN_ONENAND */ |