blob: 9077d783c344cbbd23026342309189cbf57b71c3 [file] [log] [blame]
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02001/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
7 *
8 * (C) Copyright 2008 Atmel Corporation
9 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28#include <common.h>
29
30#ifdef CFG_ENV_IS_IN_SPI_FLASH
31
32#include <environment.h>
33#include <spi_flash.h>
34
35#ifndef CFG_ENV_SPI_BUS
36# define CFG_ENV_SPI_BUS 0
37#endif
38#ifndef CFG_ENV_SPI_CS
39# define CFG_ENV_SPI_CS 0
40#endif
41#ifndef CFG_ENV_SPI_MAX_HZ
42# define CFG_ENV_SPI_MAX_HZ 1000000
43#endif
44#ifndef CFG_ENV_SPI_MODE
45# define CFG_ENV_SPI_MODE SPI_MODE_3
46#endif
47
48DECLARE_GLOBAL_DATA_PTR;
49
50/* references to names in env_common.c */
51extern uchar default_environment[];
52extern int default_environment_size;
53
54char * env_name_spec = "SPI Flash";
55env_t *env_ptr;
56
57static struct spi_flash *env_flash;
58
59uchar env_get_char_spec(int index)
60{
61 return *((uchar *)(gd->env_addr + index));
62}
63
64int saveenv(void)
65{
TsiChung Liew07efc9e2008-08-06 19:37:17 -050066 u32 sector = 1;
67
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020068 if (!env_flash) {
69 puts("Environment SPI flash not initialized\n");
70 return 1;
71 }
72
TsiChung Liew07efc9e2008-08-06 19:37:17 -050073 if (CFG_ENV_SIZE > CFG_ENV_SECT_SIZE) {
74 sector = CFG_ENV_SIZE / CFG_ENV_SECT_SIZE;
75 if (CFG_ENV_SIZE % CFG_ENV_SECT_SIZE)
76 sector++;
77 }
78
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020079 puts("Erasing SPI flash...");
TsiChung Liew07efc9e2008-08-06 19:37:17 -050080 if (spi_flash_erase(env_flash, CFG_ENV_OFFSET, sector * CFG_ENV_SECT_SIZE))
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020081 return 1;
82
83 puts("Writing to SPI flash...");
84 if (spi_flash_write(env_flash, CFG_ENV_OFFSET, CFG_ENV_SIZE, env_ptr))
85 return 1;
86
87 puts("done\n");
88 return 0;
89}
90
91void env_relocate_spec(void)
92{
93 int ret;
94
95 env_flash = spi_flash_probe(CFG_ENV_SPI_BUS, CFG_ENV_SPI_CS,
96 CFG_ENV_SPI_MAX_HZ, CFG_ENV_SPI_MODE);
97 if (!env_flash)
98 goto err_probe;
99
100 ret = spi_flash_read(env_flash, CFG_ENV_OFFSET, CFG_ENV_SIZE, env_ptr);
101 if (ret)
102 goto err_read;
103
104 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
105 goto err_crc;
106
107 gd->env_valid = 1;
108
109 return;
110
111err_read:
112 spi_flash_free(env_flash);
113 env_flash = NULL;
114err_probe:
115err_crc:
116 puts("*** Warning - bad CRC, using default environment\n\n");
117
118 if (default_environment_size > CFG_ENV_SIZE) {
119 gd->env_valid = 0;
120 puts("*** Error - default environment is too large\n\n");
121 return;
122 }
123
124 memset(env_ptr, 0, sizeof(env_t));
125 memcpy(env_ptr->data, default_environment, default_environment_size);
126 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
127 gd->env_valid = 1;
128}
129
130int env_init(void)
131{
132 /* SPI flash isn't usable before relocation */
133 gd->env_addr = (ulong)&default_environment[0];
134 gd->env_valid = 1;
135
136 return 0;
137}
138
139#endif /* CFG_ENV_IS_IN_SPI_FLASH */