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