wdenk | 5779d8d | 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> |
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> |
| 25 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 28 | env_t *env_ptr = NULL; |
| 29 | |
| 30 | char * env_name_spec = "dataflash"; |
| 31 | |
| 32 | extern int read_dataflash (unsigned long addr, unsigned long size, char |
| 33 | *result); |
| 34 | extern int write_dataflash (unsigned long addr_dest, unsigned long addr_src, |
| 35 | unsigned long size); |
| 36 | extern int AT91F_DataflashInit (void); |
| 37 | extern uchar default_environment[]; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | uchar env_get_char_spec (int index) |
| 41 | { |
| 42 | uchar c; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 43 | read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t,data), |
Stelian Pop | 9604b6e | 2008-02-11 10:50:19 +0000 | [diff] [blame] | 44 | 1, (char *)&c); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 45 | return (c); |
| 46 | } |
| 47 | |
| 48 | void env_relocate_spec (void) |
| 49 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 50 | read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, (char *)env_ptr); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int saveenv(void) |
| 54 | { |
Stelian Pop | 9604b6e | 2008-02-11 10:50:19 +0000 | [diff] [blame] | 55 | /* env must be copied to do not alter env structure in memory*/ |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 56 | unsigned char temp[CONFIG_ENV_SIZE]; |
| 57 | memcpy(temp, env_ptr, CONFIG_ENV_SIZE); |
| 58 | return write_dataflash(CONFIG_ENV_ADDR, (unsigned long)temp, CONFIG_ENV_SIZE); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /************************************************************************ |
| 62 | * Initialize Environment use |
| 63 | * |
| 64 | * We are still running from ROM, so data use is limited |
| 65 | * Use a (moderately small) buffer on the stack |
| 66 | */ |
| 67 | int env_init(void) |
| 68 | { |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 69 | ulong crc, len, new; |
| 70 | unsigned off; |
| 71 | uchar buf[64]; |
| 72 | if (gd->env_valid == 0){ |
| 73 | AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */ |
| 74 | |
| 75 | /* read old CRC */ |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 76 | read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), |
Stelian Pop | 9604b6e | 2008-02-11 10:50:19 +0000 | [diff] [blame] | 77 | sizeof(ulong), (char *)&crc); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 78 | new = 0; |
| 79 | len = ENV_SIZE; |
| 80 | off = offsetof(env_t,data); |
| 81 | while (len > 0) { |
| 82 | int n = (len > sizeof(buf)) ? sizeof(buf) : len; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 83 | read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 84 | new = crc32 (new, buf, n); |
| 85 | len -= n; |
| 86 | off += n; |
| 87 | } |
| 88 | if (crc == new) { |
| 89 | gd->env_addr = offsetof(env_t,data); |
| 90 | gd->env_valid = 1; |
| 91 | } else { |
| 92 | gd->env_addr = (ulong)&default_environment[0]; |
| 93 | gd->env_valid = 0; |
| 94 | } |
| 95 | } |
| 96 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 97 | return (0); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 98 | } |