Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * LowLevel function for DataFlash environment support |
wdenk | 5779d8d | 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 | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 19 | */ |
| 20 | #include <common.h> |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 21 | #include <command.h> |
| 22 | #include <environment.h> |
| 23 | #include <linux/stddef.h> |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 24 | #include <dataflash.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 25 | #include <search.h> |
| 26 | #include <errno.h> |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 27 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 30 | env_t *env_ptr; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 31 | |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 32 | char *env_name_spec = "dataflash"; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 33 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 34 | uchar env_get_char_spec(int index) |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 35 | { |
| 36 | uchar c; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 37 | |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 38 | read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 39 | 1, (char *)&c); |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 40 | return c; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 43 | void env_relocate_spec(void) |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 44 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 45 | char buf[CONFIG_ENV_SIZE]; |
| 46 | |
| 47 | read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf); |
| 48 | |
| 49 | env_import(buf, 1); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 52 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 53 | #error No support for redundant environment on dataflash yet! |
| 54 | #endif |
| 55 | |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 56 | int saveenv(void) |
| 57 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 58 | env_t env_new; |
| 59 | ssize_t len; |
| 60 | char *res; |
| 61 | |
| 62 | res = (char *)&env_new.data; |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 63 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 64 | if (len < 0) { |
| 65 | error("Cannot export environment: errno = %d\n", errno); |
| 66 | return 1; |
| 67 | } |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 68 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 69 | |
| 70 | return write_dataflash(CONFIG_ENV_ADDR, |
| 71 | (unsigned long)&env_new, |
| 72 | CONFIG_ENV_SIZE); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 75 | /* |
| 76 | * Initialize environment use |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 77 | * |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 78 | * We are still running from ROM, so data use is limited. |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 79 | * Use a (moderately small) buffer on the stack |
| 80 | */ |
| 81 | int env_init(void) |
| 82 | { |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 83 | ulong crc, len = ENV_SIZE, new = 0; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 84 | unsigned off; |
| 85 | uchar buf[64]; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 86 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 87 | if (gd->env_valid) |
| 88 | return 0; |
| 89 | |
| 90 | AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */ |
| 91 | |
| 92 | /* read old CRC */ |
| 93 | read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), |
| 94 | sizeof(ulong), (char *)&crc); |
| 95 | |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 96 | off = offsetof(env_t, data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 97 | while (len > 0) { |
| 98 | int n = (len > sizeof(buf)) ? sizeof(buf) : len; |
| 99 | |
| 100 | read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf); |
| 101 | |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 102 | new = crc32(new, buf, n); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 103 | len -= n; |
| 104 | off += n; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 107 | if (crc == new) { |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 108 | gd->env_addr = offsetof(env_t, data); |
| 109 | gd->env_valid = 1; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 110 | } else { |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 111 | gd->env_addr = (ulong)&default_environment[0]; |
| 112 | gd->env_valid = 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return 0; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 116 | } |