Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 1 | /* |
| 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 Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 29 | #include <environment.h> |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 30 | #include <malloc.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 31 | #include <spi_flash.h> |
| 32 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 33 | #ifndef CONFIG_ENV_SPI_BUS |
| 34 | # define CONFIG_ENV_SPI_BUS 0 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 35 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 36 | #ifndef CONFIG_ENV_SPI_CS |
| 37 | # define CONFIG_ENV_SPI_CS 0 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 38 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 39 | #ifndef CONFIG_ENV_SPI_MAX_HZ |
| 40 | # define CONFIG_ENV_SPI_MAX_HZ 1000000 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 41 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 42 | #ifndef CONFIG_ENV_SPI_MODE |
| 43 | # define CONFIG_ENV_SPI_MODE SPI_MODE_3 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | DECLARE_GLOBAL_DATA_PTR; |
| 47 | |
| 48 | /* references to names in env_common.c */ |
| 49 | extern uchar default_environment[]; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 50 | |
| 51 | char * env_name_spec = "SPI Flash"; |
| 52 | env_t *env_ptr; |
| 53 | |
| 54 | static struct spi_flash *env_flash; |
| 55 | |
| 56 | uchar env_get_char_spec(int index) |
| 57 | { |
| 58 | return *((uchar *)(gd->env_addr + index)); |
| 59 | } |
| 60 | |
| 61 | int saveenv(void) |
| 62 | { |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 63 | u32 saved_size, saved_offset; |
| 64 | char *saved_buffer = NULL; |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 65 | u32 sector = 1; |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 66 | int ret; |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 67 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 68 | if (!env_flash) { |
| 69 | puts("Environment SPI flash not initialized\n"); |
| 70 | return 1; |
| 71 | } |
| 72 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 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); |
| 78 | if (!saved_buffer) { |
| 79 | ret = 1; |
| 80 | goto done; |
| 81 | } |
| 82 | ret = spi_flash_read(env_flash, saved_offset, saved_size, saved_buffer); |
| 83 | if (ret) |
| 84 | goto done; |
| 85 | } |
| 86 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 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) |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 90 | sector++; |
| 91 | } |
| 92 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 93 | puts("Erasing SPI flash..."); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 94 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE); |
| 95 | if (ret) |
| 96 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 97 | |
| 98 | puts("Writing to SPI flash..."); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 99 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr); |
| 100 | if (ret) |
| 101 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 102 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 103 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 104 | ret = spi_flash_write(env_flash, saved_offset, saved_size, saved_buffer); |
| 105 | if (ret) |
| 106 | goto done; |
| 107 | } |
| 108 | |
| 109 | ret = 0; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 110 | puts("done\n"); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 111 | |
| 112 | done: |
| 113 | if (saved_buffer) |
| 114 | free(saved_buffer); |
| 115 | return ret; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void env_relocate_spec(void) |
| 119 | { |
| 120 | int ret; |
| 121 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 122 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 123 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 124 | if (!env_flash) |
| 125 | goto err_probe; |
| 126 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 127 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr); |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 128 | if (ret) |
| 129 | goto err_read; |
| 130 | |
| 131 | if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc) |
| 132 | goto err_crc; |
| 133 | |
| 134 | gd->env_valid = 1; |
| 135 | |
| 136 | return; |
| 137 | |
| 138 | err_read: |
| 139 | spi_flash_free(env_flash); |
| 140 | env_flash = NULL; |
| 141 | err_probe: |
| 142 | err_crc: |
| 143 | puts("*** Warning - bad CRC, using default environment\n\n"); |
| 144 | |
Mike Frysinger | 09304f5 | 2009-07-24 17:51:27 -0400 | [diff] [blame] | 145 | set_default_env(); |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int env_init(void) |
| 149 | { |
| 150 | /* SPI flash isn't usable before relocation */ |
| 151 | gd->env_addr = (ulong)&default_environment[0]; |
| 152 | gd->env_valid = 1; |
| 153 | |
| 154 | return 0; |
| 155 | } |