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) |
Stefan Roese | fb9a730 | 2010-08-31 10:00:10 +0200 | [diff] [blame] | 30 | #include <mtd/cfi_flash.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 31 | |
Marian Balakowicz | 513b4a1 | 2005-10-11 19:09:42 +0200 | [diff] [blame] | 32 | extern flash_info_t flash_info[]; /* info for FLASH chips */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 33 | |
| 34 | /*----------------------------------------------------------------------- |
| 35 | * Functions |
| 36 | */ |
| 37 | |
| 38 | /*----------------------------------------------------------------------- |
| 39 | * Set protection status for monitor sectors |
| 40 | * |
| 41 | * The monitor is always located in the _first_ Flash bank. |
| 42 | * If necessary you have to map the second bank at lower addresses. |
| 43 | */ |
| 44 | void |
| 45 | flash_protect (int flag, ulong from, ulong to, flash_info_t *info) |
| 46 | { |
Mike Frysinger | e0726db | 2010-07-28 23:45:03 -0400 | [diff] [blame] | 47 | ulong b_end; |
| 48 | short s_end; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 49 | int i; |
| 50 | |
| 51 | /* Do nothing if input data is bad. */ |
Mike Frysinger | e0726db | 2010-07-28 23:45:03 -0400 | [diff] [blame] | 52 | if (!info || info->sector_count == 0 || info->size == 0 || to < from) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
Mike Frysinger | e0726db | 2010-07-28 23:45:03 -0400 | [diff] [blame] | 56 | s_end = info->sector_count - 1; /* index of last sector */ |
| 57 | b_end = info->start[0] + info->size - 1; /* bank end address */ |
| 58 | |
Eugene OBrien | c59d1a0 | 2007-07-31 10:24:56 +0200 | [diff] [blame] | 59 | debug ("flash_protect %s: from 0x%08lX to 0x%08lX\n", |
| 60 | (flag & FLAG_PROTECT_SET) ? "ON" : |
| 61 | (flag & FLAG_PROTECT_CLEAR) ? "OFF" : "???", |
| 62 | from, to); |
| 63 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 64 | /* There is nothing to do if we have no data about the flash |
| 65 | * or the protect range and flash range don't overlap. |
| 66 | */ |
| 67 | if (info->flash_id == FLASH_UNKNOWN || |
| 68 | to < info->start[0] || from > b_end) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | for (i=0; i<info->sector_count; ++i) { |
| 73 | ulong end; /* last address in current sect */ |
| 74 | |
| 75 | end = (i == s_end) ? b_end : info->start[i + 1] - 1; |
| 76 | |
| 77 | /* Update protection if any part of the sector |
| 78 | * is in the specified range. |
| 79 | */ |
| 80 | if (from <= end && to >= info->start[i]) { |
| 81 | if (flag & FLAG_PROTECT_CLEAR) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 82 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 83 | flash_real_protect(info, i, 0); |
| 84 | #else |
| 85 | info->protect[i] = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 86 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | a09491a | 2004-04-08 22:31:29 +0000 | [diff] [blame] | 87 | debug ("protect off %d\n", i); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 88 | } |
| 89 | else if (flag & FLAG_PROTECT_SET) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 90 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 91 | flash_real_protect(info, i, 1); |
| 92 | #else |
| 93 | info->protect[i] = 1; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 94 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | a09491a | 2004-04-08 22:31:29 +0000 | [diff] [blame] | 95 | debug ("protect on %d\n", i); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /*----------------------------------------------------------------------- |
| 102 | */ |
| 103 | |
| 104 | flash_info_t * |
| 105 | addr2info (ulong addr) |
| 106 | { |
| 107 | #ifndef CONFIG_SPD823TS |
| 108 | flash_info_t *info; |
| 109 | int i; |
| 110 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 111 | 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] | 112 | if (info->flash_id != FLASH_UNKNOWN && |
| 113 | addr >= info->start[0] && |
| 114 | /* WARNING - The '- 1' is needed if the flash |
| 115 | * is at the end of the address space, since |
| 116 | * info->start[0] + info->size wraps back to 0. |
| 117 | * Please don't change this unless you understand this. |
| 118 | */ |
| 119 | addr <= info->start[0] + info->size - 1) { |
| 120 | return (info); |
| 121 | } |
| 122 | } |
| 123 | #endif /* CONFIG_SPD823TS */ |
| 124 | |
| 125 | return (NULL); |
| 126 | } |
| 127 | |
| 128 | /*----------------------------------------------------------------------- |
| 129 | * Copy memory to flash. |
| 130 | * Make sure all target addresses are within Flash bounds, |
| 131 | * and no protected sectors are hit. |
| 132 | * Returns: |
| 133 | * ERR_OK 0 - OK |
| 134 | * ERR_TIMOUT 1 - write timeout |
| 135 | * ERR_NOT_ERASED 2 - Flash not erased |
| 136 | * ERR_PROTECTED 4 - target range includes protected sectors |
| 137 | * ERR_INVAL 8 - target address not in Flash memory |
| 138 | * ERR_ALIGN 16 - target address not aligned on boundary |
| 139 | * (only some targets require alignment) |
| 140 | */ |
| 141 | int |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 142 | flash_write (char *src, ulong addr, ulong cnt) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 143 | { |
| 144 | #ifdef CONFIG_SPD823TS |
| 145 | return (ERR_TIMOUT); /* any other error codes are possible as well */ |
| 146 | #else |
| 147 | int i; |
| 148 | ulong end = addr + cnt - 1; |
| 149 | flash_info_t *info_first = addr2info (addr); |
| 150 | flash_info_t *info_last = addr2info (end ); |
| 151 | flash_info_t *info; |
Stefan Roese | d20cba5 | 2013-04-04 15:53:14 +0200 | [diff] [blame] | 152 | __maybe_unused char *src_orig = src; |
| 153 | __maybe_unused char *addr_orig = (char *)addr; |
| 154 | __maybe_unused ulong cnt_orig = cnt; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 155 | |
| 156 | if (cnt == 0) { |
| 157 | return (ERR_OK); |
| 158 | } |
| 159 | |
| 160 | if (!info_first || !info_last) { |
| 161 | return (ERR_INVAL); |
| 162 | } |
| 163 | |
| 164 | for (info = info_first; info <= info_last; ++info) { |
| 165 | ulong b_end = info->start[0] + info->size; /* bank end addr */ |
| 166 | short s_end = info->sector_count - 1; |
| 167 | for (i=0; i<info->sector_count; ++i) { |
| 168 | ulong e_addr = (i == s_end) ? b_end : info->start[i + 1]; |
| 169 | |
| 170 | if ((end >= info->start[i]) && (addr < e_addr) && |
| 171 | (info->protect[i] != 0) ) { |
| 172 | return (ERR_PROTECTED); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* finally write data to flash */ |
| 178 | for (info = info_first; info <= info_last && cnt>0; ++info) { |
| 179 | ulong len; |
| 180 | |
| 181 | len = info->start[0] + info->size - addr; |
| 182 | if (len > cnt) |
| 183 | len = cnt; |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 184 | if ((i = write_buff(info, (uchar *)src, addr, len)) != 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 185 | return (i); |
| 186 | } |
| 187 | cnt -= len; |
| 188 | addr += len; |
| 189 | src += len; |
| 190 | } |
Stefan Roese | d20cba5 | 2013-04-04 15:53:14 +0200 | [diff] [blame] | 191 | |
| 192 | #if defined(CONFIG_FLASH_VERIFY) |
| 193 | if (memcmp(src_orig, addr_orig, cnt_orig)) { |
| 194 | printf("\nVerify failed!\n"); |
| 195 | return ERR_PROG_ERROR; |
| 196 | } |
| 197 | #endif /* CONFIG_SYS_FLASH_VERIFY_AFTER_WRITE */ |
| 198 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 199 | return (ERR_OK); |
| 200 | #endif /* CONFIG_SPD823TS */ |
| 201 | } |
| 202 | |
| 203 | /*----------------------------------------------------------------------- |
| 204 | */ |
| 205 | |
| 206 | void flash_perror (int err) |
| 207 | { |
| 208 | switch (err) { |
| 209 | case ERR_OK: |
| 210 | break; |
| 211 | case ERR_TIMOUT: |
| 212 | puts ("Timeout writing to Flash\n"); |
| 213 | break; |
| 214 | case ERR_NOT_ERASED: |
| 215 | puts ("Flash not Erased\n"); |
| 216 | break; |
| 217 | case ERR_PROTECTED: |
| 218 | puts ("Can't write to protected Flash sectors\n"); |
| 219 | break; |
| 220 | case ERR_INVAL: |
| 221 | puts ("Outside available Flash\n"); |
| 222 | break; |
| 223 | case ERR_ALIGN: |
| 224 | puts ("Start and/or end address not on sector boundary\n"); |
| 225 | break; |
| 226 | case ERR_UNKNOWN_FLASH_VENDOR: |
| 227 | puts ("Unknown Vendor of Flash\n"); |
| 228 | break; |
| 229 | case ERR_UNKNOWN_FLASH_TYPE: |
| 230 | puts ("Unknown Type of Flash\n"); |
| 231 | break; |
| 232 | case ERR_PROG_ERROR: |
| 233 | puts ("General Flash Programming Error\n"); |
| 234 | break; |
Joe Hershberger | 497c32f | 2012-08-17 15:36:41 -0500 | [diff] [blame] | 235 | case ERR_ABORTED: |
| 236 | puts("Flash Programming Aborted\n"); |
| 237 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 238 | default: |
| 239 | printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err); |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /*----------------------------------------------------------------------- |
| 245 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 246 | #endif /* !CONFIG_SYS_NO_FLASH */ |