env_onenand.c 2.4 KB
Newer Older
1
/*
2 3 4
 * (C) Copyright 2010 DENX Software Engineering
 * Wolfgang Denk <wd@denx.de>
 *
5
 * (C) Copyright 2005-2009 Samsung Electronics
6 7
 * Kyungmin Park <kyungmin.park@samsung.com>
 *
8
 * SPDX-License-Identifier:	GPL-2.0+
9 10 11 12 13 14 15
 */

#include <common.h>
#include <command.h>
#include <environment.h>
#include <linux/stddef.h>
#include <malloc.h>
16 17
#include <search.h>
#include <errno.h>
18
#include <onenand_uboot.h>
19

20
#include <linux/compat.h>
21 22 23 24 25
#include <linux/mtd/mtd.h>
#include <linux/mtd/onenand.h>

char *env_name_spec = "OneNAND";

26
#define ONENAND_MAX_ENV_SIZE	CONFIG_ENV_SIZE
27 28
#define ONENAND_ENV_SIZE(mtd)	(ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)

29 30
DECLARE_GLOBAL_DATA_PTR;

31 32
void env_relocate_spec(void)
{
33
	struct mtd_info *mtd = &onenand_mtd;
34
#ifdef CONFIG_ENV_ADDR_FLEX
35
	struct onenand_chip *this = &onenand_chip;
36
#endif
37
	int rc;
38
	size_t retlen;
39
#ifdef ENV_IS_EMBEDDED
40
	char *buf = (char *)&environment;
41 42
#else
	loff_t env_addr = CONFIG_ENV_ADDR;
43 44
	char onenand_env[ONENAND_MAX_ENV_SIZE];
	char *buf = (char *)&onenand_env[0];
45
#endif /* ENV_IS_EMBEDDED */
46

47 48
#ifndef ENV_IS_EMBEDDED
# ifdef CONFIG_ENV_ADDR_FLEX
49 50
	if (FLEXONENAND(this))
		env_addr = CONFIG_ENV_ADDR_FLEX;
51
# endif
52
	/* Check OneNAND exist */
53
	if (mtd->writesize)
54
		/* Ignore read fail */
55
		mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
56
				&retlen, (u_char *)buf);
57
	else
58
		mtd->writesize = MAX_ONENAND_PAGESIZE;
59
#endif /* !ENV_IS_EMBEDDED */
60

61 62 63
	rc = env_import(buf, 1);
	if (rc)
		gd->env_valid = 1;
64 65 66 67
}

int saveenv(void)
{
68
	env_t	env_new;
69
	int ret;
70
	struct mtd_info *mtd = &onenand_mtd;
71
#ifdef CONFIG_ENV_ADDR_FLEX
72
	struct onenand_chip *this = &onenand_chip;
73
#endif
74 75
	loff_t	env_addr = CONFIG_ENV_ADDR;
	size_t	retlen;
76 77 78
	struct erase_info instr = {
		.callback	= NULL,
	};
79

80 81 82
	ret = env_export(&env_new);
	if (ret)
		return ret;
83

84
	instr.len = CONFIG_ENV_SIZE;
85
#ifdef CONFIG_ENV_ADDR_FLEX
86 87 88 89 90 91
	if (FLEXONENAND(this)) {
		env_addr = CONFIG_ENV_ADDR_FLEX;
		instr.len = CONFIG_ENV_SIZE_FLEX;
		instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
				1 : 0;
	}
92
#endif
93
	instr.addr = env_addr;
94
	instr.mtd = mtd;
95
	if (mtd_erase(mtd, &instr)) {
96
		printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
97 98 99
		return 1;
	}

100
	if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
101
			(u_char *)&env_new)) {
102
		printf("OneNAND: write failed at 0x%llx\n", instr.addr);
103 104 105 106 107 108 109 110 111
		return 2;
	}

	return 0;
}

int env_init(void)
{
	/* use default */
112
	gd->env_addr = (ulong)&default_environment[0];
113 114 115 116
	gd->env_valid = 1;

	return 0;
}