wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 1 | /* LowLevel function for DataFlash environment support |
| 2 | * Author : Gilles Gastaldi (Atmel) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation; either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 17 | * MA 02111-1307 USA |
| 18 | * |
| 19 | */ |
| 20 | #include <common.h> |
| 21 | |
| 22 | #if defined(CFG_ENV_IS_IN_DATAFLASH) /* Environment is in DataFlash */ |
| 23 | |
| 24 | #include <command.h> |
| 25 | #include <environment.h> |
| 26 | #include <linux/stddef.h> |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 27 | #include <dataflash.h> |
| 28 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 31 | env_t *env_ptr = NULL; |
| 32 | |
| 33 | char * env_name_spec = "dataflash"; |
| 34 | |
| 35 | extern int read_dataflash (unsigned long addr, unsigned long size, char |
| 36 | *result); |
| 37 | extern int write_dataflash (unsigned long addr_dest, unsigned long addr_src, |
| 38 | unsigned long size); |
| 39 | extern int AT91F_DataflashInit (void); |
| 40 | extern uchar default_environment[]; |
| 41 | /* extern int default_environment_size; */ |
| 42 | |
| 43 | |
| 44 | uchar env_get_char_spec (int index) |
| 45 | { |
| 46 | uchar c; |
Stelian Pop | f2a9ac9 | 2008-02-11 10:50:19 +0000 | [diff] [blame] | 47 | read_dataflash(CFG_ENV_ADDR + index + offsetof(env_t,data), |
| 48 | 1, (char *)&c); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 49 | return (c); |
| 50 | } |
| 51 | |
| 52 | void env_relocate_spec (void) |
| 53 | { |
Stelian Pop | f2a9ac9 | 2008-02-11 10:50:19 +0000 | [diff] [blame] | 54 | read_dataflash(CFG_ENV_ADDR, CFG_ENV_SIZE, (char *)env_ptr); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | int saveenv(void) |
| 58 | { |
Stelian Pop | f2a9ac9 | 2008-02-11 10:50:19 +0000 | [diff] [blame] | 59 | /* env must be copied to do not alter env structure in memory*/ |
| 60 | unsigned char temp[CFG_ENV_SIZE]; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 61 | memcpy(temp, env_ptr, CFG_ENV_SIZE); |
Stelian Pop | f2a9ac9 | 2008-02-11 10:50:19 +0000 | [diff] [blame] | 62 | return write_dataflash(CFG_ENV_ADDR, (unsigned long)temp, CFG_ENV_SIZE); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /************************************************************************ |
| 66 | * Initialize Environment use |
| 67 | * |
| 68 | * We are still running from ROM, so data use is limited |
| 69 | * Use a (moderately small) buffer on the stack |
| 70 | */ |
| 71 | int env_init(void) |
| 72 | { |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 73 | ulong crc, len, new; |
| 74 | unsigned off; |
| 75 | uchar buf[64]; |
| 76 | if (gd->env_valid == 0){ |
| 77 | AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */ |
| 78 | |
| 79 | /* read old CRC */ |
Stelian Pop | f2a9ac9 | 2008-02-11 10:50:19 +0000 | [diff] [blame] | 80 | read_dataflash(CFG_ENV_ADDR + offsetof(env_t, crc), |
| 81 | sizeof(ulong), (char *)&crc); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 82 | new = 0; |
| 83 | len = ENV_SIZE; |
| 84 | off = offsetof(env_t,data); |
| 85 | while (len > 0) { |
| 86 | int n = (len > sizeof(buf)) ? sizeof(buf) : len; |
Stelian Pop | f2a9ac9 | 2008-02-11 10:50:19 +0000 | [diff] [blame] | 87 | read_dataflash(CFG_ENV_ADDR + off, n, (char *)buf); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 88 | new = crc32 (new, buf, n); |
| 89 | len -= n; |
| 90 | off += n; |
| 91 | } |
| 92 | if (crc == new) { |
| 93 | gd->env_addr = offsetof(env_t,data); |
| 94 | gd->env_valid = 1; |
| 95 | } else { |
| 96 | gd->env_addr = (ulong)&default_environment[0]; |
| 97 | gd->env_valid = 0; |
| 98 | } |
| 99 | } |
| 100 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 101 | return (0); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | #endif /* CFG_ENV_IS_IN_DATAFLASH */ |