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