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