blob: 2f52e2561b4f7967155736e3ab534242945cd4e5 [file] [log] [blame]
Haavard Skinnemoeneb8951c2008-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>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020029#include <environment.h>
Mike Frysinger9af10d52008-12-11 06:23:37 -050030#include <malloc.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020031#include <spi_flash.h>
32
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020033#ifndef CONFIG_ENV_SPI_BUS
34# define CONFIG_ENV_SPI_BUS 0
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020035#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020036#ifndef CONFIG_ENV_SPI_CS
37# define CONFIG_ENV_SPI_CS 0
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020038#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020039#ifndef CONFIG_ENV_SPI_MAX_HZ
40# define CONFIG_ENV_SPI_MAX_HZ 1000000
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020041#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020042#ifndef CONFIG_ENV_SPI_MODE
43# define CONFIG_ENV_SPI_MODE SPI_MODE_3
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020044#endif
45
46DECLARE_GLOBAL_DATA_PTR;
47
48/* references to names in env_common.c */
49extern uchar default_environment[];
50extern int default_environment_size;
51
52char * env_name_spec = "SPI Flash";
53env_t *env_ptr;
54
55static struct spi_flash *env_flash;
56
57uchar env_get_char_spec(int index)
58{
59 return *((uchar *)(gd->env_addr + index));
60}
61
62int saveenv(void)
63{
Mike Frysinger9af10d52008-12-11 06:23:37 -050064 u32 saved_size, saved_offset;
65 char *saved_buffer = NULL;
TsiChung Liew08792d72008-08-06 19:37:17 -050066 u32 sector = 1;
Mike Frysinger9af10d52008-12-11 06:23:37 -050067 int ret;
TsiChung Liew08792d72008-08-06 19:37:17 -050068
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020069 if (!env_flash) {
70 puts("Environment SPI flash not initialized\n");
71 return 1;
72 }
73
Mike Frysinger9af10d52008-12-11 06:23:37 -050074 /* Is the sector larger than the env (i.e. embedded) */
75 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
76 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
77 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
78 saved_buffer = malloc(saved_size);
79 if (!saved_buffer) {
80 ret = 1;
81 goto done;
82 }
83 ret = spi_flash_read(env_flash, saved_offset, saved_size, saved_buffer);
84 if (ret)
85 goto done;
86 }
87
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020088 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
89 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
90 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew08792d72008-08-06 19:37:17 -050091 sector++;
92 }
93
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020094 puts("Erasing SPI flash...");
Mike Frysinger9af10d52008-12-11 06:23:37 -050095 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE);
96 if (ret)
97 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020098
99 puts("Writing to SPI flash...");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500100 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr);
101 if (ret)
102 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200103
Mike Frysinger9af10d52008-12-11 06:23:37 -0500104 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
105 ret = spi_flash_write(env_flash, saved_offset, saved_size, saved_buffer);
106 if (ret)
107 goto done;
108 }
109
110 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200111 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500112
113 done:
114 if (saved_buffer)
115 free(saved_buffer);
116 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200117}
118
119void env_relocate_spec(void)
120{
121 int ret;
122
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200123 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
124 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200125 if (!env_flash)
126 goto err_probe;
127
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200128 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr);
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200129 if (ret)
130 goto err_read;
131
132 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
133 goto err_crc;
134
135 gd->env_valid = 1;
136
137 return;
138
139err_read:
140 spi_flash_free(env_flash);
141 env_flash = NULL;
142err_probe:
143err_crc:
144 puts("*** Warning - bad CRC, using default environment\n\n");
145
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200146 if (default_environment_size > CONFIG_ENV_SIZE) {
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200147 gd->env_valid = 0;
148 puts("*** Error - default environment is too large\n\n");
149 return;
150 }
151
152 memset(env_ptr, 0, sizeof(env_t));
153 memcpy(env_ptr->data, default_environment, default_environment_size);
154 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
155 gd->env_valid = 1;
156}
157
158int env_init(void)
159{
160 /* SPI flash isn't usable before relocation */
161 gd->env_addr = (ulong)&default_environment[0];
162 gd->env_valid = 1;
163
164 return 0;
165}