blob: 1faa2cb62a34ac99d2dd4ca048e5e2083b879d20 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +09002/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02003 * (C) Copyright 2010 DENX Software Engineering
4 * Wolfgang Denk <wd@denx.de>
5 *
Kyungmin Park937076f2009-07-11 16:49:55 +09006 * (C) Copyright 2005-2009 Samsung Electronics
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +09007 * Kyungmin Park <kyungmin.park@samsung.com>
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +09008 */
9
Tom Rinid678a592024-05-18 20:20:43 -060010#include <common.h>
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090011#include <command.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060012#include <env_internal.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090014#include <linux/stddef.h>
15#include <malloc.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020016#include <search.h>
17#include <errno.h>
Igor Grinbergb919ec22011-11-07 01:14:03 +000018#include <onenand_uboot.h>
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090019
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000020#include <linux/compat.h>
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090021#include <linux/mtd/mtd.h>
22#include <linux/mtd/onenand.h>
23
David du Colombier034afbc2012-06-13 23:24:35 +020024#define ONENAND_MAX_ENV_SIZE CONFIG_ENV_SIZE
Kyungmin Park937076f2009-07-11 16:49:55 +090025#define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
26
Kyungmin Parka9da2b42008-03-31 10:40:19 +090027DECLARE_GLOBAL_DATA_PTR;
28
Simon Glassc5951992017-08-03 12:22:17 -060029static int env_onenand_load(void)
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090030{
Kyungmin Park937076f2009-07-11 16:49:55 +090031 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premib821cea2009-12-18 14:55:24 +053032#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +053033 struct onenand_chip *this = &onenand_chip;
Sanjeev Premib821cea2009-12-18 14:55:24 +053034#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020035 int rc;
Peter Pearse2ae64f52007-11-15 08:58:00 +000036 size_t retlen;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020037#ifdef ENV_IS_EMBEDDED
Igor Grinberg994bc672011-11-17 06:07:23 +000038 char *buf = (char *)&environment;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020039#else
40 loff_t env_addr = CONFIG_ENV_ADDR;
Tom Rinicd0f4fa2013-04-05 14:55:21 -040041 char onenand_env[ONENAND_MAX_ENV_SIZE];
42 char *buf = (char *)&onenand_env[0];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020043#endif /* ENV_IS_EMBEDDED */
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090044
Wolfgang Denkea882ba2010-06-20 23:33:59 +020045#ifndef ENV_IS_EMBEDDED
46# ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +053047 if (FLEXONENAND(this))
48 env_addr = CONFIG_ENV_ADDR_FLEX;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020049# endif
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090050 /* Check OneNAND exist */
Kyungmin Park937076f2009-07-11 16:49:55 +090051 if (mtd->writesize)
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090052 /* Ignore read fail */
Sergey Lapindfe64e22013-01-14 03:46:50 +000053 mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
Igor Grinbergb919ec22011-11-07 01:14:03 +000054 &retlen, (u_char *)buf);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090055 else
Kyungmin Park937076f2009-07-11 16:49:55 +090056 mtd->writesize = MAX_ONENAND_PAGESIZE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020057#endif /* !ENV_IS_EMBEDDED */
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090058
Marek Vasut890feec2020-07-07 20:51:35 +020059 rc = env_import(buf, 1, H_EXTERNAL);
Simon Goldschmidt42a18202018-01-31 14:47:10 +010060 if (!rc)
Simon Glass203e94f2017-08-03 12:21:56 -060061 gd->env_valid = ENV_VALID;
Simon Glassc5951992017-08-03 12:22:17 -060062
Simon Goldschmidt42a18202018-01-31 14:47:10 +010063 return rc;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090064}
65
Simon Glasse5bce242017-08-03 12:22:01 -060066static int env_onenand_save(void)
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090067{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040068 env_t env_new;
Marek Vasut7ce15262014-03-05 19:59:50 +010069 int ret;
Kyungmin Park937076f2009-07-11 16:49:55 +090070 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premib821cea2009-12-18 14:55:24 +053071#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +053072 struct onenand_chip *this = &onenand_chip;
Sanjeev Premib821cea2009-12-18 14:55:24 +053073#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020074 loff_t env_addr = CONFIG_ENV_ADDR;
75 size_t retlen;
Marek BehĂșn0d1ecc92021-10-05 15:56:06 +020076 struct erase_info instr = {};
Wolfgang Denkea882ba2010-06-20 23:33:59 +020077
Marek Vasut7ce15262014-03-05 19:59:50 +010078 ret = env_export(&env_new);
79 if (ret)
80 return ret;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090081
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020082 instr.len = CONFIG_ENV_SIZE;
Sanjeev Premib821cea2009-12-18 14:55:24 +053083#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +053084 if (FLEXONENAND(this)) {
85 env_addr = CONFIG_ENV_ADDR_FLEX;
86 instr.len = CONFIG_ENV_SIZE_FLEX;
87 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
88 1 : 0;
89 }
Sanjeev Premib821cea2009-12-18 14:55:24 +053090#endif
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090091 instr.addr = env_addr;
Kyungmin Park937076f2009-07-11 16:49:55 +090092 instr.mtd = mtd;
Sergey Lapindfe64e22013-01-14 03:46:50 +000093 if (mtd_erase(mtd, &instr)) {
Kyungmin Park48287792009-07-20 09:47:47 +090094 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090095 return 1;
96 }
97
Sergey Lapindfe64e22013-01-14 03:46:50 +000098 if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
Tom Rinicd0f4fa2013-04-05 14:55:21 -040099 (u_char *)&env_new)) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200100 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900101 return 2;
102 }
103
104 return 0;
105}
106
Simon Glass4415f1d2017-08-03 12:21:58 -0600107U_BOOT_ENV_LOCATION(onenand) = {
108 .location = ENVL_ONENAND,
Simon Glassac358be2017-08-03 12:22:03 -0600109 ENV_NAME("OneNAND")
Simon Glasse5bce242017-08-03 12:22:01 -0600110 .load = env_onenand_load,
111 .save = env_save_ptr(env_onenand_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600112};