Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 2 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 3 | * (C) Copyright 2010 DENX Software Engineering |
| 4 | * Wolfgang Denk <wd@denx.de> |
| 5 | * |
Kyungmin Park | 937076f | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 6 | * (C) Copyright 2005-2009 Samsung Electronics |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 7 | * Kyungmin Park <kyungmin.park@samsung.com> |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 8 | */ |
| 9 | |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 11 | #include <env_internal.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 13 | #include <linux/stddef.h> |
| 14 | #include <malloc.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 15 | #include <search.h> |
| 16 | #include <errno.h> |
Igor Grinberg | b919ec2 | 2011-11-07 01:14:03 +0000 | [diff] [blame] | 17 | #include <onenand_uboot.h> |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 18 | |
Mike Frysinger | 7b15e2b | 2012-04-09 13:39:55 +0000 | [diff] [blame] | 19 | #include <linux/compat.h> |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 20 | #include <linux/mtd/mtd.h> |
| 21 | #include <linux/mtd/onenand.h> |
| 22 | |
David du Colombier | 034afbc | 2012-06-13 23:24:35 +0200 | [diff] [blame] | 23 | #define ONENAND_MAX_ENV_SIZE CONFIG_ENV_SIZE |
Kyungmin Park | 937076f | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 24 | #define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE) |
| 25 | |
Kyungmin Park | a9da2b4 | 2008-03-31 10:40:19 +0900 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 28 | static int env_onenand_load(void) |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 29 | { |
Kyungmin Park | 937076f | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 30 | struct mtd_info *mtd = &onenand_mtd; |
Sanjeev Premi | b821cea | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 31 | #ifdef CONFIG_ENV_ADDR_FLEX |
Amul Kumar Saha | c758e94 | 2009-11-04 10:38:46 +0530 | [diff] [blame] | 32 | struct onenand_chip *this = &onenand_chip; |
Sanjeev Premi | b821cea | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 33 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 34 | int rc; |
Peter Pearse | 2ae64f5 | 2007-11-15 08:58:00 +0000 | [diff] [blame] | 35 | size_t retlen; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 36 | #ifdef ENV_IS_EMBEDDED |
Igor Grinberg | 994bc67 | 2011-11-17 06:07:23 +0000 | [diff] [blame] | 37 | char *buf = (char *)&environment; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 38 | #else |
| 39 | loff_t env_addr = CONFIG_ENV_ADDR; |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 40 | char onenand_env[ONENAND_MAX_ENV_SIZE]; |
| 41 | char *buf = (char *)&onenand_env[0]; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 42 | #endif /* ENV_IS_EMBEDDED */ |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 43 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 44 | #ifndef ENV_IS_EMBEDDED |
| 45 | # ifdef CONFIG_ENV_ADDR_FLEX |
Amul Kumar Saha | c758e94 | 2009-11-04 10:38:46 +0530 | [diff] [blame] | 46 | if (FLEXONENAND(this)) |
| 47 | env_addr = CONFIG_ENV_ADDR_FLEX; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 48 | # endif |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 49 | /* Check OneNAND exist */ |
Kyungmin Park | 937076f | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 50 | if (mtd->writesize) |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 51 | /* Ignore read fail */ |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 52 | mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE, |
Igor Grinberg | b919ec2 | 2011-11-07 01:14:03 +0000 | [diff] [blame] | 53 | &retlen, (u_char *)buf); |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 54 | else |
Kyungmin Park | 937076f | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 55 | mtd->writesize = MAX_ONENAND_PAGESIZE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 56 | #endif /* !ENV_IS_EMBEDDED */ |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 57 | |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 58 | rc = env_import(buf, 1, H_EXTERNAL); |
Simon Goldschmidt | 42a1820 | 2018-01-31 14:47:10 +0100 | [diff] [blame] | 59 | if (!rc) |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 60 | gd->env_valid = ENV_VALID; |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 61 | |
Simon Goldschmidt | 42a1820 | 2018-01-31 14:47:10 +0100 | [diff] [blame] | 62 | return rc; |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 63 | } |
| 64 | |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 65 | static int env_onenand_save(void) |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 66 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 67 | env_t env_new; |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 68 | int ret; |
Kyungmin Park | 937076f | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 69 | struct mtd_info *mtd = &onenand_mtd; |
Sanjeev Premi | b821cea | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 70 | #ifdef CONFIG_ENV_ADDR_FLEX |
Amul Kumar Saha | c758e94 | 2009-11-04 10:38:46 +0530 | [diff] [blame] | 71 | struct onenand_chip *this = &onenand_chip; |
Sanjeev Premi | b821cea | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 72 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 73 | loff_t env_addr = CONFIG_ENV_ADDR; |
| 74 | size_t retlen; |
Marek BehĂșn | 0d1ecc9 | 2021-10-05 15:56:06 +0200 | [diff] [blame] | 75 | struct erase_info instr = {}; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 76 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 77 | ret = env_export(&env_new); |
| 78 | if (ret) |
| 79 | return ret; |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 80 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 81 | instr.len = CONFIG_ENV_SIZE; |
Sanjeev Premi | b821cea | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 82 | #ifdef CONFIG_ENV_ADDR_FLEX |
Amul Kumar Saha | c758e94 | 2009-11-04 10:38:46 +0530 | [diff] [blame] | 83 | if (FLEXONENAND(this)) { |
| 84 | env_addr = CONFIG_ENV_ADDR_FLEX; |
| 85 | instr.len = CONFIG_ENV_SIZE_FLEX; |
| 86 | instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ? |
| 87 | 1 : 0; |
| 88 | } |
Sanjeev Premi | b821cea | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 89 | #endif |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 90 | instr.addr = env_addr; |
Kyungmin Park | 937076f | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 91 | instr.mtd = mtd; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 92 | if (mtd_erase(mtd, &instr)) { |
Kyungmin Park | 4828779 | 2009-07-20 09:47:47 +0900 | [diff] [blame] | 93 | printf("OneNAND: erase failed at 0x%08llx\n", env_addr); |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 94 | return 1; |
| 95 | } |
| 96 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 97 | if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen, |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 98 | (u_char *)&env_new)) { |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 99 | printf("OneNAND: write failed at 0x%llx\n", instr.addr); |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 100 | return 2; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 106 | U_BOOT_ENV_LOCATION(onenand) = { |
| 107 | .location = ENVL_ONENAND, |
Simon Glass | ac358be | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 108 | ENV_NAME("OneNAND") |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 109 | .load = env_onenand_load, |
| 110 | .save = env_save_ptr(env_onenand_save), |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 111 | }; |