]>
Commit | Line | Data |
---|---|---|
d7e8ce10 KP |
1 | /* |
2 | * (C) Copyright 2005-2007 Samsung Electronics | |
3 | * Kyungmin Park <[email protected]> | |
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> | |
d7e8ce10 KP |
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 | ||
d438d508 | 40 | #define ONENAND_ENV_SIZE(mtd) (mtd.writesize - ENV_HEADER_SIZE) |
d7e8ce10 KP |
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 | ||
a9da2b41 KP |
52 | DECLARE_GLOBAL_DATA_PTR; |
53 | ||
d7e8ce10 KP |
54 | uchar env_get_char_spec(int index) |
55 | { | |
d7e8ce10 KP |
56 | return (*((uchar *) (gd->env_addr + index))); |
57 | } | |
58 | ||
59 | void env_relocate_spec(void) | |
60 | { | |
d7e8ce10 KP |
61 | unsigned long env_addr; |
62 | int use_default = 0; | |
2ae64f51 | 63 | size_t retlen; |
d7e8ce10 | 64 | |
0e8d1586 | 65 | env_addr = CONFIG_ENV_ADDR; |
d7e8ce10 KP |
66 | |
67 | /* Check OneNAND exist */ | |
d438d508 | 68 | if (onenand_mtd.writesize) |
d7e8ce10 | 69 | /* Ignore read fail */ |
d438d508 | 70 | onenand_read(&onenand_mtd, env_addr, onenand_mtd.writesize, |
d7e8ce10 KP |
71 | &retlen, (u_char *) env_ptr); |
72 | else | |
d438d508 | 73 | onenand_mtd.writesize = MAX_ONENAND_PAGESIZE; |
d7e8ce10 KP |
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 | { | |
0e8d1586 | 92 | unsigned long env_addr = CONFIG_ENV_ADDR; |
a9da2b41 KP |
93 | struct erase_info instr = { |
94 | .callback = NULL, | |
95 | }; | |
2ae64f51 | 96 | size_t retlen; |
d7e8ce10 | 97 | |
0e8d1586 | 98 | instr.len = CONFIG_ENV_SIZE; |
d7e8ce10 | 99 | instr.addr = env_addr; |
5e46b1e5 | 100 | instr.mtd = &onenand_mtd; |
d7e8ce10 | 101 | if (onenand_erase(&onenand_mtd, &instr)) { |
0a5676be | 102 | printf("OneNAND: erase failed at 0x%08lx\n", env_addr); |
d7e8ce10 KP |
103 | return 1; |
104 | } | |
105 | ||
106 | /* update crc */ | |
107 | env_ptr->crc = | |
a9da2b41 | 108 | crc32(0, env_ptr->data, ONENAND_ENV_SIZE(onenand_mtd)); |
d7e8ce10 | 109 | |
d438d508 | 110 | if (onenand_write(&onenand_mtd, env_addr, onenand_mtd.writesize, &retlen, |
d7e8ce10 KP |
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 | { | |
d7e8ce10 KP |
121 | /* use default */ |
122 | gd->env_addr = (ulong) & default_environment[0]; | |
123 | gd->env_valid = 1; | |
124 | ||
125 | return 0; | |
126 | } |