xref: /OK3568_Linux_fs/u-boot/env/onenand.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2010 DENX Software Engineering
3*4882a593Smuzhiyun  * Wolfgang Denk <wd@denx.de>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * (C) Copyright 2005-2009 Samsung Electronics
6*4882a593Smuzhiyun  * Kyungmin Park <kyungmin.park@samsung.com>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <common.h>
12*4882a593Smuzhiyun #include <command.h>
13*4882a593Smuzhiyun #include <environment.h>
14*4882a593Smuzhiyun #include <linux/stddef.h>
15*4882a593Smuzhiyun #include <malloc.h>
16*4882a593Smuzhiyun #include <search.h>
17*4882a593Smuzhiyun #include <errno.h>
18*4882a593Smuzhiyun #include <onenand_uboot.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <linux/compat.h>
21*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
22*4882a593Smuzhiyun #include <linux/mtd/onenand.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define ONENAND_MAX_ENV_SIZE	CONFIG_ENV_SIZE
25*4882a593Smuzhiyun #define ONENAND_ENV_SIZE(mtd)	(ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
28*4882a593Smuzhiyun 
env_onenand_load(void)29*4882a593Smuzhiyun static int env_onenand_load(void)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	struct mtd_info *mtd = &onenand_mtd;
32*4882a593Smuzhiyun #ifdef CONFIG_ENV_ADDR_FLEX
33*4882a593Smuzhiyun 	struct onenand_chip *this = &onenand_chip;
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun 	int rc;
36*4882a593Smuzhiyun 	size_t retlen;
37*4882a593Smuzhiyun #ifdef ENV_IS_EMBEDDED
38*4882a593Smuzhiyun 	char *buf = (char *)&environment;
39*4882a593Smuzhiyun #else
40*4882a593Smuzhiyun 	loff_t env_addr = CONFIG_ENV_ADDR;
41*4882a593Smuzhiyun 	char onenand_env[ONENAND_MAX_ENV_SIZE];
42*4882a593Smuzhiyun 	char *buf = (char *)&onenand_env[0];
43*4882a593Smuzhiyun #endif /* ENV_IS_EMBEDDED */
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #ifndef ENV_IS_EMBEDDED
46*4882a593Smuzhiyun # ifdef CONFIG_ENV_ADDR_FLEX
47*4882a593Smuzhiyun 	if (FLEXONENAND(this))
48*4882a593Smuzhiyun 		env_addr = CONFIG_ENV_ADDR_FLEX;
49*4882a593Smuzhiyun # endif
50*4882a593Smuzhiyun 	/* Check OneNAND exist */
51*4882a593Smuzhiyun 	if (mtd->writesize)
52*4882a593Smuzhiyun 		/* Ignore read fail */
53*4882a593Smuzhiyun 		mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
54*4882a593Smuzhiyun 				&retlen, (u_char *)buf);
55*4882a593Smuzhiyun 	else
56*4882a593Smuzhiyun 		mtd->writesize = MAX_ONENAND_PAGESIZE;
57*4882a593Smuzhiyun #endif /* !ENV_IS_EMBEDDED */
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	rc = env_import(buf, 1);
60*4882a593Smuzhiyun 	if (rc)
61*4882a593Smuzhiyun 		gd->env_valid = ENV_VALID;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	return rc ? 0 : -EIO;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
env_onenand_save(void)66*4882a593Smuzhiyun static int env_onenand_save(void)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	env_t	env_new;
69*4882a593Smuzhiyun 	int ret;
70*4882a593Smuzhiyun 	struct mtd_info *mtd = &onenand_mtd;
71*4882a593Smuzhiyun #ifdef CONFIG_ENV_ADDR_FLEX
72*4882a593Smuzhiyun 	struct onenand_chip *this = &onenand_chip;
73*4882a593Smuzhiyun #endif
74*4882a593Smuzhiyun 	loff_t	env_addr = CONFIG_ENV_ADDR;
75*4882a593Smuzhiyun 	size_t	retlen;
76*4882a593Smuzhiyun 	struct erase_info instr = {
77*4882a593Smuzhiyun 		.callback	= NULL,
78*4882a593Smuzhiyun 	};
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	ret = env_export(&env_new);
81*4882a593Smuzhiyun 	if (ret)
82*4882a593Smuzhiyun 		return ret;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	instr.len = CONFIG_ENV_SIZE;
85*4882a593Smuzhiyun #ifdef CONFIG_ENV_ADDR_FLEX
86*4882a593Smuzhiyun 	if (FLEXONENAND(this)) {
87*4882a593Smuzhiyun 		env_addr = CONFIG_ENV_ADDR_FLEX;
88*4882a593Smuzhiyun 		instr.len = CONFIG_ENV_SIZE_FLEX;
89*4882a593Smuzhiyun 		instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
90*4882a593Smuzhiyun 				1 : 0;
91*4882a593Smuzhiyun 	}
92*4882a593Smuzhiyun #endif
93*4882a593Smuzhiyun 	instr.addr = env_addr;
94*4882a593Smuzhiyun 	instr.mtd = mtd;
95*4882a593Smuzhiyun 	if (mtd_erase(mtd, &instr)) {
96*4882a593Smuzhiyun 		printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
97*4882a593Smuzhiyun 		return 1;
98*4882a593Smuzhiyun 	}
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
101*4882a593Smuzhiyun 			(u_char *)&env_new)) {
102*4882a593Smuzhiyun 		printf("OneNAND: write failed at 0x%llx\n", instr.addr);
103*4882a593Smuzhiyun 		return 2;
104*4882a593Smuzhiyun 	}
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	return 0;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun U_BOOT_ENV_LOCATION(onenand) = {
110*4882a593Smuzhiyun 	.location	= ENVL_ONENAND,
111*4882a593Smuzhiyun 	ENV_NAME("OneNAND")
112*4882a593Smuzhiyun 	.load		= env_onenand_load,
113*4882a593Smuzhiyun 	.save		= env_save_ptr(env_onenand_save),
114*4882a593Smuzhiyun };
115