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 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 8 | #include <command.h> |
| 9 | #include <environment.h> |
| 10 | #include <linux/stddef.h> |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 11 | #include <dataflash.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 12 | #include <search.h> |
| 13 | #include <errno.h> |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 14 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Simon Glass | 3fa4c22 | 2017-08-03 12:22:16 -0600 | [diff] [blame] | 17 | static int env_dataflash_get_char(int index) |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 18 | { |
| 19 | uchar c; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 20 | |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 21 | read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data), |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 22 | 1, (char *)&c); |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 23 | return c; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 26 | static int env_dataflash_load(void) |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 27 | { |
Bo Shen | 5c7bad5 | 2013-10-08 16:30:21 +0800 | [diff] [blame] | 28 | ulong crc, new = 0; |
| 29 | unsigned off; |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 30 | char buf[CONFIG_ENV_SIZE]; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 31 | |
Bo Shen | 5c7bad5 | 2013-10-08 16:30:21 +0800 | [diff] [blame] | 32 | /* Read old CRC */ |
| 33 | read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), |
| 34 | sizeof(ulong), (char *)&crc); |
| 35 | |
| 36 | /* Read whole environment */ |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 37 | read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf); |
| 38 | |
Bo Shen | 5c7bad5 | 2013-10-08 16:30:21 +0800 | [diff] [blame] | 39 | /* Calculate the CRC */ |
| 40 | off = offsetof(env_t, data); |
| 41 | new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE); |
| 42 | |
| 43 | if (crc == new) |
| 44 | env_import(buf, 1); |
| 45 | else |
| 46 | set_default_env("!bad CRC"); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 47 | |
| 48 | return 0; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Wolfgang Denk | 460a9ff | 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 | |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 55 | static int env_dataflash_save(void) |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 56 | { |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 57 | env_t env_new; |
| 58 | int ret; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 59 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 60 | ret = env_export(&env_new); |
| 61 | if (ret) |
| 62 | return ret; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 63 | |
| 64 | return write_dataflash(CONFIG_ENV_ADDR, |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 65 | (unsigned long)&env_new, |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 66 | CONFIG_ENV_SIZE); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 69 | U_BOOT_ENV_LOCATION(dataflash) = { |
| 70 | .location = ENVL_DATAFLASH, |
Simon Glass | d8273ed | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 71 | ENV_NAME("dataflash") |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 72 | .get_char = env_dataflash_get_char, |
| 73 | .load = env_dataflash_load, |
| 74 | .save = env_save_ptr(env_dataflash_save), |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 75 | }; |