Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2010 DENX Software Engineering |
| 3 | * Wolfgang Denk <wd@denx.de> |
| 4 | * |
Kyungmin Park | a1f416c | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 5 | * (C) Copyright 2005-2009 Samsung Electronics |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 6 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 7 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 12 | #include <command.h> |
| 13 | #include <environment.h> |
| 14 | #include <linux/stddef.h> |
| 15 | #include <malloc.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 16 | #include <search.h> |
| 17 | #include <errno.h> |
Igor Grinberg | d0ed003 | 2011-11-07 01:14:03 +0000 | [diff] [blame] | 18 | #include <onenand_uboot.h> |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 19 | |
Mike Frysinger | 11d1a09 | 2012-04-09 13:39:55 +0000 | [diff] [blame] | 20 | #include <linux/compat.h> |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 21 | #include <linux/mtd/mtd.h> |
| 22 | #include <linux/mtd/onenand.h> |
| 23 | |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 24 | char *env_name_spec = "OneNAND"; |
| 25 | |
David du Colombier | 80bd6da | 2012-06-13 23:24:35 +0200 | [diff] [blame] | 26 | #define ONENAND_MAX_ENV_SIZE CONFIG_ENV_SIZE |
Kyungmin Park | a1f416c | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 27 | #define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE) |
| 28 | |
Kyungmin Park | 012f2aa | 2008-03-31 10:40:19 +0900 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 31 | void env_relocate_spec(void) |
| 32 | { |
Kyungmin Park | a1f416c | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 33 | struct mtd_info *mtd = &onenand_mtd; |
Sanjeev Premi | 6173ce8 | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 34 | #ifdef CONFIG_ENV_ADDR_FLEX |
Amul Kumar Saha | 4773566 | 2009-11-04 10:38:46 +0530 | [diff] [blame] | 35 | struct onenand_chip *this = &onenand_chip; |
Sanjeev Premi | 6173ce8 | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 36 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 37 | int rc; |
Peter Pearse | f402614 | 2007-11-15 08:58:00 +0000 | [diff] [blame] | 38 | size_t retlen; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 39 | #ifdef ENV_IS_EMBEDDED |
Igor Grinberg | 23b54b9 | 2011-11-17 06:07:23 +0000 | [diff] [blame] | 40 | char *buf = (char *)&environment; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 41 | #else |
| 42 | loff_t env_addr = CONFIG_ENV_ADDR; |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 43 | char onenand_env[ONENAND_MAX_ENV_SIZE]; |
| 44 | char *buf = (char *)&onenand_env[0]; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 45 | #endif /* ENV_IS_EMBEDDED */ |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 46 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 47 | #ifndef ENV_IS_EMBEDDED |
| 48 | # ifdef CONFIG_ENV_ADDR_FLEX |
Amul Kumar Saha | 4773566 | 2009-11-04 10:38:46 +0530 | [diff] [blame] | 49 | if (FLEXONENAND(this)) |
| 50 | env_addr = CONFIG_ENV_ADDR_FLEX; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 51 | # endif |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 52 | /* Check OneNAND exist */ |
Kyungmin Park | a1f416c | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 53 | if (mtd->writesize) |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 54 | /* Ignore read fail */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 55 | mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE, |
Igor Grinberg | d0ed003 | 2011-11-07 01:14:03 +0000 | [diff] [blame] | 56 | &retlen, (u_char *)buf); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 57 | else |
Kyungmin Park | a1f416c | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 58 | mtd->writesize = MAX_ONENAND_PAGESIZE; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 59 | #endif /* !ENV_IS_EMBEDDED */ |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 60 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 61 | rc = env_import(buf, 1); |
| 62 | if (rc) |
| 63 | gd->env_valid = 1; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int saveenv(void) |
| 67 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 68 | env_t env_new; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 69 | ssize_t len; |
| 70 | char *res; |
Kyungmin Park | a1f416c | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 71 | struct mtd_info *mtd = &onenand_mtd; |
Sanjeev Premi | 6173ce8 | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 72 | #ifdef CONFIG_ENV_ADDR_FLEX |
Amul Kumar Saha | 4773566 | 2009-11-04 10:38:46 +0530 | [diff] [blame] | 73 | struct onenand_chip *this = &onenand_chip; |
Sanjeev Premi | 6173ce8 | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 74 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 75 | loff_t env_addr = CONFIG_ENV_ADDR; |
| 76 | size_t retlen; |
Kyungmin Park | 012f2aa | 2008-03-31 10:40:19 +0900 | [diff] [blame] | 77 | struct erase_info instr = { |
| 78 | .callback = NULL, |
| 79 | }; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 80 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 81 | res = (char *)&env_new.data; |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 82 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 83 | if (len < 0) { |
| 84 | error("Cannot export environment: errno = %d\n", errno); |
| 85 | return 1; |
| 86 | } |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 87 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 88 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 89 | instr.len = CONFIG_ENV_SIZE; |
Sanjeev Premi | 6173ce8 | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 90 | #ifdef CONFIG_ENV_ADDR_FLEX |
Amul Kumar Saha | 4773566 | 2009-11-04 10:38:46 +0530 | [diff] [blame] | 91 | if (FLEXONENAND(this)) { |
| 92 | env_addr = CONFIG_ENV_ADDR_FLEX; |
| 93 | instr.len = CONFIG_ENV_SIZE_FLEX; |
| 94 | instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ? |
| 95 | 1 : 0; |
| 96 | } |
Sanjeev Premi | 6173ce8 | 2009-12-18 14:55:24 +0530 | [diff] [blame] | 97 | #endif |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 98 | instr.addr = env_addr; |
Kyungmin Park | a1f416c | 2009-07-11 16:49:55 +0900 | [diff] [blame] | 99 | instr.mtd = mtd; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 100 | if (mtd_erase(mtd, &instr)) { |
Kyungmin Park | 458e0ff | 2009-07-20 09:47:47 +0900 | [diff] [blame] | 101 | printf("OneNAND: erase failed at 0x%08llx\n", env_addr); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 102 | return 1; |
| 103 | } |
| 104 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 105 | if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen, |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 106 | (u_char *)&env_new)) { |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 107 | printf("OneNAND: write failed at 0x%llx\n", instr.addr); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 108 | return 2; |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | int env_init(void) |
| 115 | { |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 116 | /* use default */ |
Igor Grinberg | d0ed003 | 2011-11-07 01:14:03 +0000 | [diff] [blame] | 117 | gd->env_addr = (ulong)&default_environment[0]; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 118 | gd->env_valid = 1; |
| 119 | |
| 120 | return 0; |
| 121 | } |