Haavard Skinnemoen | 8c66497 | 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 | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 29 | #include <environment.h> |
| 30 | #include <spi_flash.h> |
| 31 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 32 | #ifndef CONFIG_ENV_SPI_BUS |
| 33 | # define CONFIG_ENV_SPI_BUS 0 |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 34 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 35 | #ifndef CONFIG_ENV_SPI_CS |
| 36 | # define CONFIG_ENV_SPI_CS 0 |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 37 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 38 | #ifndef CONFIG_ENV_SPI_MAX_HZ |
| 39 | # define CONFIG_ENV_SPI_MAX_HZ 1000000 |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 40 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 41 | #ifndef CONFIG_ENV_SPI_MODE |
| 42 | # define CONFIG_ENV_SPI_MODE SPI_MODE_3 |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | DECLARE_GLOBAL_DATA_PTR; |
| 46 | |
| 47 | /* references to names in env_common.c */ |
| 48 | extern uchar default_environment[]; |
| 49 | extern int default_environment_size; |
| 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 | { |
TsiChung Liew | 07efc9e | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 63 | u32 sector = 1; |
| 64 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 65 | if (!env_flash) { |
| 66 | puts("Environment SPI flash not initialized\n"); |
| 67 | return 1; |
| 68 | } |
| 69 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 70 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 71 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 72 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
TsiChung Liew | 07efc9e | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 73 | sector++; |
| 74 | } |
| 75 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 76 | puts("Erasing SPI flash..."); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 77 | if (spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE)) |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 78 | return 1; |
| 79 | |
| 80 | puts("Writing to SPI flash..."); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 81 | if (spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr)) |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 82 | return 1; |
| 83 | |
| 84 | puts("done\n"); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | void env_relocate_spec(void) |
| 89 | { |
| 90 | int ret; |
| 91 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 92 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 93 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 94 | if (!env_flash) |
| 95 | goto err_probe; |
| 96 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 97 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr); |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 98 | if (ret) |
| 99 | goto err_read; |
| 100 | |
| 101 | if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc) |
| 102 | goto err_crc; |
| 103 | |
| 104 | gd->env_valid = 1; |
| 105 | |
| 106 | return; |
| 107 | |
| 108 | err_read: |
| 109 | spi_flash_free(env_flash); |
| 110 | env_flash = NULL; |
| 111 | err_probe: |
| 112 | err_crc: |
| 113 | puts("*** Warning - bad CRC, using default environment\n\n"); |
| 114 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 115 | if (default_environment_size > CONFIG_ENV_SIZE) { |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 116 | gd->env_valid = 0; |
| 117 | puts("*** Error - default environment is too large\n\n"); |
| 118 | return; |
| 119 | } |
| 120 | |
| 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); |
| 124 | gd->env_valid = 1; |
| 125 | } |
| 126 | |
| 127 | int env_init(void) |
| 128 | { |
| 129 | /* SPI flash isn't usable before relocation */ |
| 130 | gd->env_addr = (ulong)&default_environment[0]; |
| 131 | gd->env_valid = 1; |
| 132 | |
| 133 | return 0; |
| 134 | } |