3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
13 #if !defined(CONFIG_SYS_NO_FLASH)
14 #include <mtd/cfi_flash.h>
16 extern flash_info_t flash_info[]; /* info for FLASH chips */
18 /*-----------------------------------------------------------------------
22 /*-----------------------------------------------------------------------
23 * Set protection status for monitor sectors
25 * The monitor is always located in the _first_ Flash bank.
26 * If necessary you have to map the second bank at lower addresses.
29 flash_protect (int flag, ulong from, ulong to, flash_info_t *info)
35 /* Do nothing if input data is bad. */
36 if (!info || info->sector_count == 0 || info->size == 0 || to < from) {
40 s_end = info->sector_count - 1; /* index of last sector */
41 b_end = info->start[0] + info->size - 1; /* bank end address */
43 debug ("flash_protect %s: from 0x%08lX to 0x%08lX\n",
44 (flag & FLAG_PROTECT_SET) ? "ON" :
45 (flag & FLAG_PROTECT_CLEAR) ? "OFF" : "???",
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.
51 if (info->flash_id == FLASH_UNKNOWN ||
52 to < info->start[0] || from > b_end) {
56 for (i=0; i<info->sector_count; ++i) {
57 ulong end; /* last address in current sect */
59 end = (i == s_end) ? b_end : info->start[i + 1] - 1;
61 /* Update protection if any part of the sector
62 * is in the specified range.
64 if (from <= end && to >= info->start[i]) {
65 if (flag & FLAG_PROTECT_CLEAR) {
66 #if defined(CONFIG_SYS_FLASH_PROTECTION)
67 flash_real_protect(info, i, 0);
70 #endif /* CONFIG_SYS_FLASH_PROTECTION */
71 debug ("protect off %d\n", i);
73 else if (flag & FLAG_PROTECT_SET) {
74 #if defined(CONFIG_SYS_FLASH_PROTECTION)
75 flash_real_protect(info, i, 1);
78 #endif /* CONFIG_SYS_FLASH_PROTECTION */
79 debug ("protect on %d\n", i);
85 /*-----------------------------------------------------------------------
89 addr2info (ulong addr)
94 for (i=0, info = &flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
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.
102 addr <= info->start[0] + info->size - 1) {
110 /*-----------------------------------------------------------------------
111 * Copy memory to flash.
112 * Make sure all target addresses are within Flash bounds,
113 * and no protected sectors are hit.
116 * ERR_TIMOUT 1 - write timeout
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)
124 flash_write (char *src, ulong addr, ulong cnt)
127 ulong end = addr + cnt - 1;
128 flash_info_t *info_first = addr2info (addr);
129 flash_info_t *info_last = addr2info (end );
131 __maybe_unused char *src_orig = src;
132 __maybe_unused char *addr_orig = (char *)addr;
133 __maybe_unused ulong cnt_orig = cnt;
139 if (!info_first || !info_last) {
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];
149 if ((end >= info->start[i]) && (addr < e_addr) &&
150 (info->protect[i] != 0) ) {
151 return (ERR_PROTECTED);
156 /* finally write data to flash */
157 for (info = info_first; info <= info_last && cnt>0; ++info) {
160 len = info->start[0] + info->size - addr;
163 if ((i = write_buff(info, (uchar *)src, addr, len)) != 0) {
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;
176 #endif /* CONFIG_SYS_FLASH_VERIFY_AFTER_WRITE */
181 /*-----------------------------------------------------------------------
184 void flash_perror (int err)
190 puts ("Timeout writing to Flash\n");
193 puts ("Flash not Erased\n");
196 puts ("Can't write to protected Flash sectors\n");
199 puts ("Outside available Flash\n");
202 puts ("Start and/or end address not on sector boundary\n");
204 case ERR_UNKNOWN_FLASH_VENDOR:
205 puts ("Unknown Vendor of Flash\n");
207 case ERR_UNKNOWN_FLASH_TYPE:
208 puts ("Unknown Type of Flash\n");
211 puts ("General Flash Programming Error\n");
214 puts("Flash Programming Aborted\n");
217 printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
222 /*-----------------------------------------------------------------------
224 #endif /* !CONFIG_SYS_NO_FLASH */