Kyungmin Park | af3d11c | 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> |
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 | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 40 | #define ONENAND_ENV_SIZE(mtd) (mtd.writesize - ENV_HEADER_SIZE) |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 41 | |
| 42 | char *env_name_spec = "OneNAND"; |
| 43 | |
| 44 | #ifdef ENV_IS_EMBEDDED |
| 45 | extern uchar environment[]; |
| 46 | env_t *env_ptr = (env_t *) (&environment[0]); |
| 47 | #else /* ! ENV_IS_EMBEDDED */ |
| 48 | static unsigned char onenand_env[MAX_ONENAND_PAGESIZE]; |
| 49 | env_t *env_ptr = (env_t *) onenand_env; |
| 50 | #endif /* ENV_IS_EMBEDDED */ |
| 51 | |
Kyungmin Park | 012f2aa | 2008-03-31 10:40:19 +0900 | [diff] [blame] | 52 | DECLARE_GLOBAL_DATA_PTR; |
| 53 | |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 54 | uchar env_get_char_spec(int index) |
| 55 | { |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 56 | return (*((uchar *) (gd->env_addr + index))); |
| 57 | } |
| 58 | |
| 59 | void env_relocate_spec(void) |
| 60 | { |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 61 | unsigned long env_addr; |
| 62 | int use_default = 0; |
Peter Pearse | f402614 | 2007-11-15 08:58:00 +0000 | [diff] [blame] | 63 | size_t retlen; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 64 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 65 | env_addr = CONFIG_ENV_ADDR; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 66 | |
| 67 | /* Check OneNAND exist */ |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 68 | if (onenand_mtd.writesize) |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 69 | /* Ignore read fail */ |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 70 | onenand_read(&onenand_mtd, env_addr, onenand_mtd.writesize, |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 71 | &retlen, (u_char *) env_ptr); |
| 72 | else |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 73 | onenand_mtd.writesize = MAX_ONENAND_PAGESIZE; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 74 | |
| 75 | if (crc32(0, env_ptr->data, ONENAND_ENV_SIZE(onenand_mtd)) != |
| 76 | env_ptr->crc) |
| 77 | use_default = 1; |
| 78 | |
| 79 | if (use_default) { |
| 80 | memcpy(env_ptr->data, default_environment, |
| 81 | ONENAND_ENV_SIZE(onenand_mtd)); |
| 82 | env_ptr->crc = |
| 83 | crc32(0, env_ptr->data, ONENAND_ENV_SIZE(onenand_mtd)); |
| 84 | } |
| 85 | |
| 86 | gd->env_addr = (ulong) & env_ptr->data; |
| 87 | gd->env_valid = 1; |
| 88 | } |
| 89 | |
| 90 | int saveenv(void) |
| 91 | { |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 92 | unsigned long env_addr = CONFIG_ENV_ADDR; |
Kyungmin Park | 012f2aa | 2008-03-31 10:40:19 +0900 | [diff] [blame] | 93 | struct erase_info instr = { |
| 94 | .callback = NULL, |
| 95 | }; |
Peter Pearse | f402614 | 2007-11-15 08:58:00 +0000 | [diff] [blame] | 96 | size_t retlen; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 97 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 98 | instr.len = CONFIG_ENV_SIZE; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 99 | instr.addr = env_addr; |
Stefan Roese | 7da2f70 | 2008-11-27 14:11:37 +0100 | [diff] [blame] | 100 | instr.mtd = &onenand_mtd; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 101 | if (onenand_erase(&onenand_mtd, &instr)) { |
Jean-Christophe PLAGNIOL-VILLARD | c4fb57c | 2008-07-12 14:36:34 +0200 | [diff] [blame] | 102 | printf("OneNAND: erase failed at 0x%08lx\n", env_addr); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | /* update crc */ |
| 107 | env_ptr->crc = |
Kyungmin Park | 012f2aa | 2008-03-31 10:40:19 +0900 | [diff] [blame] | 108 | crc32(0, env_ptr->data, ONENAND_ENV_SIZE(onenand_mtd)); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 109 | |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 110 | if (onenand_write(&onenand_mtd, env_addr, onenand_mtd.writesize, &retlen, |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 111 | (u_char *) env_ptr)) { |
| 112 | printf("OneNAND: write failed at 0x%08x\n", instr.addr); |
| 113 | return 2; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int env_init(void) |
| 120 | { |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 121 | /* use default */ |
| 122 | gd->env_addr = (ulong) & default_environment[0]; |
| 123 | gd->env_valid = 1; |
| 124 | |
| 125 | return 0; |
| 126 | } |