blob: 2e3045c5f5de69b9de5acaa60eb58df1c8f435af [file] [log] [blame]
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09001/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02002 * (C) Copyright 2010 DENX Software Engineering
3 * Wolfgang Denk <wd@denx.de>
4 *
Kyungmin Parka1f416c2009-07-11 16:49:55 +09005 * (C) Copyright 2005-2009 Samsung Electronics
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09006 * Kyungmin Park <kyungmin.park@samsung.com>
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09009 */
10
11#include <common.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090012#include <command.h>
13#include <environment.h>
14#include <linux/stddef.h>
15#include <malloc.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020016#include <search.h>
17#include <errno.h>
Igor Grinbergd0ed0032011-11-07 01:14:03 +000018#include <onenand_uboot.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090019
Mike Frysinger11d1a092012-04-09 13:39:55 +000020#include <linux/compat.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090021#include <linux/mtd/mtd.h>
22#include <linux/mtd/onenand.h>
23
David du Colombier80bd6da2012-06-13 23:24:35 +020024#define ONENAND_MAX_ENV_SIZE CONFIG_ENV_SIZE
Kyungmin Parka1f416c2009-07-11 16:49:55 +090025#define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
26
Kyungmin Park012f2aa2008-03-31 10:40:19 +090027DECLARE_GLOBAL_DATA_PTR;
28
Simon Glass99778492017-08-03 12:22:17 -060029static int env_onenand_load(void)
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090030{
Kyungmin Parka1f416c2009-07-11 16:49:55 +090031 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053032#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053033 struct onenand_chip *this = &onenand_chip;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053034#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020035 int rc;
Peter Pearsef4026142007-11-15 08:58:00 +000036 size_t retlen;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020037#ifdef ENV_IS_EMBEDDED
Igor Grinberg23b54b92011-11-17 06:07:23 +000038 char *buf = (char *)&environment;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020039#else
40 loff_t env_addr = CONFIG_ENV_ADDR;
Tom Rinia840b8e2013-04-05 14:55:21 -040041 char onenand_env[ONENAND_MAX_ENV_SIZE];
42 char *buf = (char *)&onenand_env[0];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020043#endif /* ENV_IS_EMBEDDED */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090044
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020045#ifndef ENV_IS_EMBEDDED
46# ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053047 if (FLEXONENAND(this))
48 env_addr = CONFIG_ENV_ADDR_FLEX;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020049# endif
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090050 /* Check OneNAND exist */
Kyungmin Parka1f416c2009-07-11 16:49:55 +090051 if (mtd->writesize)
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090052 /* Ignore read fail */
Sergey Lapin3a38a552013-01-14 03:46:50 +000053 mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
Igor Grinbergd0ed0032011-11-07 01:14:03 +000054 &retlen, (u_char *)buf);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090055 else
Kyungmin Parka1f416c2009-07-11 16:49:55 +090056 mtd->writesize = MAX_ONENAND_PAGESIZE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020057#endif /* !ENV_IS_EMBEDDED */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090058
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020059 rc = env_import(buf, 1);
60 if (rc)
Simon Glass4bc2ad22017-08-03 12:21:56 -060061 gd->env_valid = ENV_VALID;
Simon Glass99778492017-08-03 12:22:17 -060062
63 return rc ? 0 : -EIO;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090064}
65
Simon Glass082af922017-08-03 12:22:01 -060066static int env_onenand_save(void)
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090067{
Tom Rinia840b8e2013-04-05 14:55:21 -040068 env_t env_new;
Marek Vasutd73c1292014-03-05 19:59:50 +010069 int ret;
Kyungmin Parka1f416c2009-07-11 16:49:55 +090070 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053071#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053072 struct onenand_chip *this = &onenand_chip;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053073#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020074 loff_t env_addr = CONFIG_ENV_ADDR;
75 size_t retlen;
Kyungmin Park012f2aa2008-03-31 10:40:19 +090076 struct erase_info instr = {
77 .callback = NULL,
78 };
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020079
Marek Vasutd73c1292014-03-05 19:59:50 +010080 ret = env_export(&env_new);
81 if (ret)
82 return ret;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090083
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020084 instr.len = CONFIG_ENV_SIZE;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053085#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053086 if (FLEXONENAND(this)) {
87 env_addr = CONFIG_ENV_ADDR_FLEX;
88 instr.len = CONFIG_ENV_SIZE_FLEX;
89 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
90 1 : 0;
91 }
Sanjeev Premi6173ce82009-12-18 14:55:24 +053092#endif
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090093 instr.addr = env_addr;
Kyungmin Parka1f416c2009-07-11 16:49:55 +090094 instr.mtd = mtd;
Sergey Lapin3a38a552013-01-14 03:46:50 +000095 if (mtd_erase(mtd, &instr)) {
Kyungmin Park458e0ff2009-07-20 09:47:47 +090096 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090097 return 1;
98 }
99
Sergey Lapin3a38a552013-01-14 03:46:50 +0000100 if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
Tom Rinia840b8e2013-04-05 14:55:21 -0400101 (u_char *)&env_new)) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200102 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900103 return 2;
104 }
105
106 return 0;
107}
108
Simon Glassc10a88e2017-08-03 12:21:58 -0600109U_BOOT_ENV_LOCATION(onenand) = {
110 .location = ENVL_ONENAND,
Simon Glassd8273ed2017-08-03 12:22:03 -0600111 ENV_NAME("OneNAND")
Simon Glass082af922017-08-03 12:22:01 -0600112 .load = env_onenand_load,
113 .save = env_save_ptr(env_onenand_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600114};