blob: 4b4463218092fc48e29c5dce2dd67fb1c90d216a [file] [log] [blame]
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +09001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2010 DENX Software Engineering
3 * Wolfgang Denk <wd@denx.de>
4 *
Kyungmin Park937076f2009-07-11 16:49:55 +09005 * (C) Copyright 2005-2009 Samsung Electronics
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +09006 * Kyungmin Park <kyungmin.park@samsung.com>
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +09009 */
10
11#include <common.h>
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090012#include <command.h>
13#include <environment.h>
14#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
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090024char *env_name_spec = "OneNAND";
25
David du Colombier034afbc2012-06-13 23:24:35 +020026#define ONENAND_MAX_ENV_SIZE CONFIG_ENV_SIZE
Kyungmin Park937076f2009-07-11 16:49:55 +090027#define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
28
Kyungmin Parka9da2b42008-03-31 10:40:19 +090029DECLARE_GLOBAL_DATA_PTR;
30
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090031void env_relocate_spec(void)
32{
Kyungmin Park937076f2009-07-11 16:49:55 +090033 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premib821cea2009-12-18 14:55:24 +053034#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +053035 struct onenand_chip *this = &onenand_chip;
Sanjeev Premib821cea2009-12-18 14:55:24 +053036#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020037 int rc;
Peter Pearse2ae64f52007-11-15 08:58:00 +000038 size_t retlen;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020039#ifdef ENV_IS_EMBEDDED
Igor Grinberg994bc672011-11-17 06:07:23 +000040 char *buf = (char *)&environment;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020041#else
42 loff_t env_addr = CONFIG_ENV_ADDR;
Tom Rinicd0f4fa2013-04-05 14:55:21 -040043 char onenand_env[ONENAND_MAX_ENV_SIZE];
44 char *buf = (char *)&onenand_env[0];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020045#endif /* ENV_IS_EMBEDDED */
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090046
Wolfgang Denkea882ba2010-06-20 23:33:59 +020047#ifndef ENV_IS_EMBEDDED
48# ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +053049 if (FLEXONENAND(this))
50 env_addr = CONFIG_ENV_ADDR_FLEX;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020051# endif
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090052 /* Check OneNAND exist */
Kyungmin Park937076f2009-07-11 16:49:55 +090053 if (mtd->writesize)
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090054 /* Ignore read fail */
Sergey Lapindfe64e22013-01-14 03:46:50 +000055 mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
Igor Grinbergb919ec22011-11-07 01:14:03 +000056 &retlen, (u_char *)buf);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090057 else
Kyungmin Park937076f2009-07-11 16:49:55 +090058 mtd->writesize = MAX_ONENAND_PAGESIZE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020059#endif /* !ENV_IS_EMBEDDED */
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090060
Wolfgang Denkea882ba2010-06-20 23:33:59 +020061 rc = env_import(buf, 1);
62 if (rc)
63 gd->env_valid = 1;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090064}
65
66int saveenv(void)
67{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040068 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020069 ssize_t len;
70 char *res;
Kyungmin Park937076f2009-07-11 16:49:55 +090071 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premib821cea2009-12-18 14:55:24 +053072#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +053073 struct onenand_chip *this = &onenand_chip;
Sanjeev Premib821cea2009-12-18 14:55:24 +053074#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020075 loff_t env_addr = CONFIG_ENV_ADDR;
76 size_t retlen;
Kyungmin Parka9da2b42008-03-31 10:40:19 +090077 struct erase_info instr = {
78 .callback = NULL,
79 };
Wolfgang Denkea882ba2010-06-20 23:33:59 +020080
Tom Rinicd0f4fa2013-04-05 14:55:21 -040081 res = (char *)&env_new.data;
Joe Hershbergerbe112352012-12-11 22:16:23 -060082 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020083 if (len < 0) {
84 error("Cannot export environment: errno = %d\n", errno);
85 return 1;
86 }
Tom Rinicd0f4fa2013-04-05 14:55:21 -040087 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090088
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020089 instr.len = CONFIG_ENV_SIZE;
Sanjeev Premib821cea2009-12-18 14:55:24 +053090#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +053091 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 Premib821cea2009-12-18 14:55:24 +053097#endif
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090098 instr.addr = env_addr;
Kyungmin Park937076f2009-07-11 16:49:55 +090099 instr.mtd = mtd;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000100 if (mtd_erase(mtd, &instr)) {
Kyungmin Park48287792009-07-20 09:47:47 +0900101 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900102 return 1;
103 }
104
Sergey Lapindfe64e22013-01-14 03:46:50 +0000105 if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400106 (u_char *)&env_new)) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200107 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900108 return 2;
109 }
110
111 return 0;
112}
113
114int env_init(void)
115{
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900116 /* use default */
Igor Grinbergb919ec22011-11-07 01:14:03 +0000117 gd->env_addr = (ulong)&default_environment[0];
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900118 gd->env_valid = 1;
119
120 return 0;
121}