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