wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
wdenk | a09491a | 2004-04-08 22:31:29 +0000 | [diff] [blame] | 24 | /* #define DEBUG */ |
| 25 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 26 | #include <common.h> |
| 27 | #include <flash.h> |
| 28 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 29 | #if !defined(CONFIG_SYS_NO_FLASH) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 30 | |
Marian Balakowicz | 513b4a1 | 2005-10-11 19:09:42 +0200 | [diff] [blame] | 31 | extern flash_info_t flash_info[]; /* info for FLASH chips */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 32 | |
| 33 | /*----------------------------------------------------------------------- |
| 34 | * Functions |
| 35 | */ |
| 36 | |
| 37 | /*----------------------------------------------------------------------- |
| 38 | * Set protection status for monitor sectors |
| 39 | * |
| 40 | * The monitor is always located in the _first_ Flash bank. |
| 41 | * If necessary you have to map the second bank at lower addresses. |
| 42 | */ |
| 43 | void |
| 44 | flash_protect (int flag, ulong from, ulong to, flash_info_t *info) |
| 45 | { |
| 46 | ulong b_end = info->start[0] + info->size - 1; /* bank end address */ |
| 47 | short s_end = info->sector_count - 1; /* index of last sector */ |
| 48 | int i; |
| 49 | |
| 50 | /* Do nothing if input data is bad. */ |
| 51 | if (info->sector_count == 0 || info->size == 0 || to < from) { |
| 52 | return; |
| 53 | } |
| 54 | |
Eugene OBrien | c59d1a0 | 2007-07-31 10:24:56 +0200 | [diff] [blame] | 55 | debug ("flash_protect %s: from 0x%08lX to 0x%08lX\n", |
| 56 | (flag & FLAG_PROTECT_SET) ? "ON" : |
| 57 | (flag & FLAG_PROTECT_CLEAR) ? "OFF" : "???", |
| 58 | from, to); |
| 59 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 60 | /* There is nothing to do if we have no data about the flash |
| 61 | * or the protect range and flash range don't overlap. |
| 62 | */ |
| 63 | if (info->flash_id == FLASH_UNKNOWN || |
| 64 | to < info->start[0] || from > b_end) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | for (i=0; i<info->sector_count; ++i) { |
| 69 | ulong end; /* last address in current sect */ |
| 70 | |
| 71 | end = (i == s_end) ? b_end : info->start[i + 1] - 1; |
| 72 | |
| 73 | /* Update protection if any part of the sector |
| 74 | * is in the specified range. |
| 75 | */ |
| 76 | if (from <= end && to >= info->start[i]) { |
| 77 | if (flag & FLAG_PROTECT_CLEAR) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 78 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 79 | flash_real_protect(info, i, 0); |
| 80 | #else |
| 81 | info->protect[i] = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 82 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | a09491a | 2004-04-08 22:31:29 +0000 | [diff] [blame] | 83 | debug ("protect off %d\n", i); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 84 | } |
| 85 | else if (flag & FLAG_PROTECT_SET) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 86 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 87 | flash_real_protect(info, i, 1); |
| 88 | #else |
| 89 | info->protect[i] = 1; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 90 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | a09491a | 2004-04-08 22:31:29 +0000 | [diff] [blame] | 91 | debug ("protect on %d\n", i); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /*----------------------------------------------------------------------- |
| 98 | */ |
| 99 | |
| 100 | flash_info_t * |
| 101 | addr2info (ulong addr) |
| 102 | { |
| 103 | #ifndef CONFIG_SPD823TS |
| 104 | flash_info_t *info; |
| 105 | int i; |
| 106 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 107 | for (i=0, info = &flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 108 | if (info->flash_id != FLASH_UNKNOWN && |
| 109 | addr >= info->start[0] && |
| 110 | /* WARNING - The '- 1' is needed if the flash |
| 111 | * is at the end of the address space, since |
| 112 | * info->start[0] + info->size wraps back to 0. |
| 113 | * Please don't change this unless you understand this. |
| 114 | */ |
| 115 | addr <= info->start[0] + info->size - 1) { |
| 116 | return (info); |
| 117 | } |
| 118 | } |
| 119 | #endif /* CONFIG_SPD823TS */ |
| 120 | |
| 121 | return (NULL); |
| 122 | } |
| 123 | |
| 124 | /*----------------------------------------------------------------------- |
| 125 | * Copy memory to flash. |
| 126 | * Make sure all target addresses are within Flash bounds, |
| 127 | * and no protected sectors are hit. |
| 128 | * Returns: |
| 129 | * ERR_OK 0 - OK |
| 130 | * ERR_TIMOUT 1 - write timeout |
| 131 | * ERR_NOT_ERASED 2 - Flash not erased |
| 132 | * ERR_PROTECTED 4 - target range includes protected sectors |
| 133 | * ERR_INVAL 8 - target address not in Flash memory |
| 134 | * ERR_ALIGN 16 - target address not aligned on boundary |
| 135 | * (only some targets require alignment) |
| 136 | */ |
| 137 | int |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 138 | flash_write (char *src, ulong addr, ulong cnt) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 139 | { |
| 140 | #ifdef CONFIG_SPD823TS |
| 141 | return (ERR_TIMOUT); /* any other error codes are possible as well */ |
| 142 | #else |
| 143 | int i; |
| 144 | ulong end = addr + cnt - 1; |
| 145 | flash_info_t *info_first = addr2info (addr); |
| 146 | flash_info_t *info_last = addr2info (end ); |
| 147 | flash_info_t *info; |
| 148 | |
| 149 | if (cnt == 0) { |
| 150 | return (ERR_OK); |
| 151 | } |
| 152 | |
| 153 | if (!info_first || !info_last) { |
| 154 | return (ERR_INVAL); |
| 155 | } |
| 156 | |
| 157 | for (info = info_first; info <= info_last; ++info) { |
| 158 | ulong b_end = info->start[0] + info->size; /* bank end addr */ |
| 159 | short s_end = info->sector_count - 1; |
| 160 | for (i=0; i<info->sector_count; ++i) { |
| 161 | ulong e_addr = (i == s_end) ? b_end : info->start[i + 1]; |
| 162 | |
| 163 | if ((end >= info->start[i]) && (addr < e_addr) && |
| 164 | (info->protect[i] != 0) ) { |
| 165 | return (ERR_PROTECTED); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* finally write data to flash */ |
| 171 | for (info = info_first; info <= info_last && cnt>0; ++info) { |
| 172 | ulong len; |
| 173 | |
| 174 | len = info->start[0] + info->size - addr; |
| 175 | if (len > cnt) |
| 176 | len = cnt; |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 177 | if ((i = write_buff(info, (uchar *)src, addr, len)) != 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 178 | return (i); |
| 179 | } |
| 180 | cnt -= len; |
| 181 | addr += len; |
| 182 | src += len; |
| 183 | } |
| 184 | return (ERR_OK); |
| 185 | #endif /* CONFIG_SPD823TS */ |
| 186 | } |
| 187 | |
| 188 | /*----------------------------------------------------------------------- |
| 189 | */ |
| 190 | |
| 191 | void flash_perror (int err) |
| 192 | { |
| 193 | switch (err) { |
| 194 | case ERR_OK: |
| 195 | break; |
| 196 | case ERR_TIMOUT: |
| 197 | puts ("Timeout writing to Flash\n"); |
| 198 | break; |
| 199 | case ERR_NOT_ERASED: |
| 200 | puts ("Flash not Erased\n"); |
| 201 | break; |
| 202 | case ERR_PROTECTED: |
| 203 | puts ("Can't write to protected Flash sectors\n"); |
| 204 | break; |
| 205 | case ERR_INVAL: |
| 206 | puts ("Outside available Flash\n"); |
| 207 | break; |
| 208 | case ERR_ALIGN: |
| 209 | puts ("Start and/or end address not on sector boundary\n"); |
| 210 | break; |
| 211 | case ERR_UNKNOWN_FLASH_VENDOR: |
| 212 | puts ("Unknown Vendor of Flash\n"); |
| 213 | break; |
| 214 | case ERR_UNKNOWN_FLASH_TYPE: |
| 215 | puts ("Unknown Type of Flash\n"); |
| 216 | break; |
| 217 | case ERR_PROG_ERROR: |
| 218 | puts ("General Flash Programming Error\n"); |
| 219 | break; |
| 220 | default: |
| 221 | printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err); |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /*----------------------------------------------------------------------- |
| 227 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 228 | #endif /* !CONFIG_SYS_NO_FLASH */ |