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>
30 #include <spi_flash.h>
32 #ifndef CFG_ENV_SPI_BUS
33 # define CFG_ENV_SPI_BUS 0
35 #ifndef CFG_ENV_SPI_CS
36 # define CFG_ENV_SPI_CS 0
38 #ifndef CFG_ENV_SPI_MAX_HZ
39 # define CFG_ENV_SPI_MAX_HZ 1000000
41 #ifndef CFG_ENV_SPI_MODE
42 # define CFG_ENV_SPI_MODE SPI_MODE_3
45 DECLARE_GLOBAL_DATA_PTR
;
47 /* references to names in env_common.c */
48 extern uchar default_environment
[];
49 extern int default_environment_size
;
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
));
66 puts("Environment SPI flash not initialized\n");
70 if (CFG_ENV_SIZE
> CFG_ENV_SECT_SIZE
) {
71 sector
= CFG_ENV_SIZE
/ CFG_ENV_SECT_SIZE
;
72 if (CFG_ENV_SIZE
% CFG_ENV_SECT_SIZE
)
76 puts("Erasing SPI flash...");
77 if (spi_flash_erase(env_flash
, CFG_ENV_OFFSET
, sector
* CFG_ENV_SECT_SIZE
))
80 puts("Writing to SPI flash...");
81 if (spi_flash_write(env_flash
, CFG_ENV_OFFSET
, CFG_ENV_SIZE
, env_ptr
))
88 void env_relocate_spec(void)
92 env_flash
= spi_flash_probe(CFG_ENV_SPI_BUS
, CFG_ENV_SPI_CS
,
93 CFG_ENV_SPI_MAX_HZ
, CFG_ENV_SPI_MODE
);
97 ret
= spi_flash_read(env_flash
, CFG_ENV_OFFSET
, CFG_ENV_SIZE
, env_ptr
);
101 if (crc32(0, env_ptr
->data
, ENV_SIZE
) != env_ptr
->crc
)
109 spi_flash_free(env_flash
);
113 puts("*** Warning - bad CRC, using default environment\n\n");
115 if (default_environment_size
> CFG_ENV_SIZE
) {
117 puts("*** Error - default environment is too large\n\n");
121 memset(env_ptr
, 0, sizeof(env_t
));
122 memcpy(env_ptr
->data
, default_environment
, default_environment_size
);
123 env_ptr
->crc
= crc32(0, env_ptr
->data
, ENV_SIZE
);
129 /* SPI flash isn't usable before relocation */
130 gd
->env_addr
= (ulong
)&default_environment
[0];