blob: d371bd757cb5c24036ace1a0b789d130034d9354 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09002/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02003 * (C) Copyright 2010 DENX Software Engineering
4 * Wolfgang Denk <wd@denx.de>
5 *
Kyungmin Parka1f416c2009-07-11 16:49:55 +09006 * (C) Copyright 2005-2009 Samsung Electronics
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09007 * Kyungmin Park <kyungmin.park@samsung.com>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09008 */
9
10#include <common.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090011#include <command.h>
12#include <environment.h>
13#include <linux/stddef.h>
14#include <malloc.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020015#include <search.h>
16#include <errno.h>
Igor Grinbergd0ed0032011-11-07 01:14:03 +000017#include <onenand_uboot.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090018
Mike Frysinger11d1a092012-04-09 13:39:55 +000019#include <linux/compat.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090020#include <linux/mtd/mtd.h>
21#include <linux/mtd/onenand.h>
22
David du Colombier80bd6da2012-06-13 23:24:35 +020023#define ONENAND_MAX_ENV_SIZE CONFIG_ENV_SIZE
Kyungmin Parka1f416c2009-07-11 16:49:55 +090024#define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
25
Kyungmin Park012f2aa2008-03-31 10:40:19 +090026DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass99778492017-08-03 12:22:17 -060028static int env_onenand_load(void)
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090029{
Kyungmin Parka1f416c2009-07-11 16:49:55 +090030 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053031#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053032 struct onenand_chip *this = &onenand_chip;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053033#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020034 int rc;
Peter Pearsef4026142007-11-15 08:58:00 +000035 size_t retlen;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020036#ifdef ENV_IS_EMBEDDED
Igor Grinberg23b54b92011-11-17 06:07:23 +000037 char *buf = (char *)&environment;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020038#else
39 loff_t env_addr = CONFIG_ENV_ADDR;
Tom Rinia840b8e2013-04-05 14:55:21 -040040 char onenand_env[ONENAND_MAX_ENV_SIZE];
41 char *buf = (char *)&onenand_env[0];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020042#endif /* ENV_IS_EMBEDDED */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090043
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020044#ifndef ENV_IS_EMBEDDED
45# ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053046 if (FLEXONENAND(this))
47 env_addr = CONFIG_ENV_ADDR_FLEX;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020048# endif
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090049 /* Check OneNAND exist */
Kyungmin Parka1f416c2009-07-11 16:49:55 +090050 if (mtd->writesize)
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090051 /* Ignore read fail */
Sergey Lapin3a38a552013-01-14 03:46:50 +000052 mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
Igor Grinbergd0ed0032011-11-07 01:14:03 +000053 &retlen, (u_char *)buf);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090054 else
Kyungmin Parka1f416c2009-07-11 16:49:55 +090055 mtd->writesize = MAX_ONENAND_PAGESIZE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020056#endif /* !ENV_IS_EMBEDDED */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090057
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020058 rc = env_import(buf, 1);
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +010059 if (!rc)
Simon Glass4bc2ad22017-08-03 12:21:56 -060060 gd->env_valid = ENV_VALID;
Simon Glass99778492017-08-03 12:22:17 -060061
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +010062 return rc;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090063}
64
Simon Glass082af922017-08-03 12:22:01 -060065static int env_onenand_save(void)
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090066{
Tom Rinia840b8e2013-04-05 14:55:21 -040067 env_t env_new;
Marek Vasutd73c1292014-03-05 19:59:50 +010068 int ret;
Kyungmin Parka1f416c2009-07-11 16:49:55 +090069 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053070#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053071 struct onenand_chip *this = &onenand_chip;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053072#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020073 loff_t env_addr = CONFIG_ENV_ADDR;
74 size_t retlen;
Kyungmin Park012f2aa2008-03-31 10:40:19 +090075 struct erase_info instr = {
76 .callback = NULL,
77 };
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020078
Marek Vasutd73c1292014-03-05 19:59:50 +010079 ret = env_export(&env_new);
80 if (ret)
81 return ret;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090082
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020083 instr.len = CONFIG_ENV_SIZE;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053084#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053085 if (FLEXONENAND(this)) {
86 env_addr = CONFIG_ENV_ADDR_FLEX;
87 instr.len = CONFIG_ENV_SIZE_FLEX;
88 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
89 1 : 0;
90 }
Sanjeev Premi6173ce82009-12-18 14:55:24 +053091#endif
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090092 instr.addr = env_addr;
Kyungmin Parka1f416c2009-07-11 16:49:55 +090093 instr.mtd = mtd;
Sergey Lapin3a38a552013-01-14 03:46:50 +000094 if (mtd_erase(mtd, &instr)) {
Kyungmin Park458e0ff2009-07-20 09:47:47 +090095 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090096 return 1;
97 }
98
Sergey Lapin3a38a552013-01-14 03:46:50 +000099 if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
Tom Rinia840b8e2013-04-05 14:55:21 -0400100 (u_char *)&env_new)) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200101 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900102 return 2;
103 }
104
105 return 0;
106}
107
Simon Glassc10a88e2017-08-03 12:21:58 -0600108U_BOOT_ENV_LOCATION(onenand) = {
109 .location = ENVL_ONENAND,
Simon Glassd8273ed2017-08-03 12:22:03 -0600110 ENV_NAME("OneNAND")
Simon Glass082af922017-08-03 12:22:01 -0600111 .load = env_onenand_load,
112 .save = env_save_ptr(env_onenand_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600113};