2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * (C) Copyright 2008 Atmel Corporation
10 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include <environment.h>
31 #include <spi_flash.h>
33 #ifndef CONFIG_ENV_SPI_BUS
34 # define CONFIG_ENV_SPI_BUS 0
36 #ifndef CONFIG_ENV_SPI_CS
37 # define CONFIG_ENV_SPI_CS 0
39 #ifndef CONFIG_ENV_SPI_MAX_HZ
40 # define CONFIG_ENV_SPI_MAX_HZ 1000000
42 #ifndef CONFIG_ENV_SPI_MODE
43 # define CONFIG_ENV_SPI_MODE SPI_MODE_3
46 DECLARE_GLOBAL_DATA_PTR;
48 /* references to names in env_common.c */
49 extern uchar default_environment[];
51 char * env_name_spec = "SPI Flash";
54 static struct spi_flash *env_flash;
56 uchar env_get_char_spec(int index)
58 return *((uchar *)(gd->env_addr + index));
63 u32 saved_size, saved_offset;
64 char *saved_buffer = NULL;
69 puts("Environment SPI flash not initialized\n");
73 /* Is the sector larger than the env (i.e. embedded) */
74 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
75 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
76 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
77 saved_buffer = malloc(saved_size);
82 ret = spi_flash_read(env_flash, saved_offset, saved_size, saved_buffer);
87 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
88 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
89 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
93 puts("Erasing SPI flash...");
94 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE);
98 puts("Writing to SPI flash...");
99 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr);
103 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
104 ret = spi_flash_write(env_flash, saved_offset, saved_size, saved_buffer);
118 void env_relocate_spec(void)
122 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
123 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
127 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr);
131 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
139 spi_flash_free(env_flash);
143 puts("*** Warning - bad CRC, using default environment\n\n");
150 /* SPI flash isn't usable before relocation */
151 gd->env_addr = (ulong)&default_environment[0];