blob: fd1b4ddf660d895c523e040cf047b0154c048e5c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkaffae2b2002-08-17 09:36:01 +00005 */
6
wdenka09491a2004-04-08 22:31:29 +00007/* #define DEBUG */
8
wdenkaffae2b2002-08-17 09:36:01 +00009#include <flash.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010011#include <u-boot/uuid.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060012#include <linux/string.h>
wdenkaffae2b2002-08-17 09:36:01 +000013
Stefan Roesefb9a7302010-08-31 10:00:10 +020014#include <mtd/cfi_flash.h>
wdenkaffae2b2002-08-17 09:36:01 +000015
wdenkaffae2b2002-08-17 09:36:01 +000016/*-----------------------------------------------------------------------
17 * Functions
18 */
19
20/*-----------------------------------------------------------------------
21 * Set protection status for monitor sectors
22 *
23 * The monitor is always located in the _first_ Flash bank.
24 * If necessary you have to map the second bank at lower addresses.
25 */
26void
Simon Glassc7443162020-05-10 11:39:53 -060027flash_protect(int flag, ulong from, ulong to, flash_info_t *info)
wdenkaffae2b2002-08-17 09:36:01 +000028{
Mike Frysingere0726db2010-07-28 23:45:03 -040029 ulong b_end;
30 short s_end;
wdenkaffae2b2002-08-17 09:36:01 +000031 int i;
32
33 /* Do nothing if input data is bad. */
Mike Frysingere0726db2010-07-28 23:45:03 -040034 if (!info || info->sector_count == 0 || info->size == 0 || to < from) {
wdenkaffae2b2002-08-17 09:36:01 +000035 return;
36 }
37
Mike Frysingere0726db2010-07-28 23:45:03 -040038 s_end = info->sector_count - 1; /* index of last sector */
39 b_end = info->start[0] + info->size - 1; /* bank end address */
40
Simon Glassed38aef2020-05-10 11:40:03 -060041 debug("%s %s: from 0x%08lX to 0x%08lX\n", __func__,
42 (flag & FLAG_PROTECT_SET) ? "ON" :
Simon Glass8f055af2020-05-10 11:40:04 -060043 (flag & FLAG_PROTECT_CLEAR) ? "OFF" : "???",
Simon Glassed38aef2020-05-10 11:40:03 -060044 from, to);
Eugene OBrienc59d1a02007-07-31 10:24:56 +020045
wdenkaffae2b2002-08-17 09:36:01 +000046 /* There is nothing to do if we have no data about the flash
47 * or the protect range and flash range don't overlap.
48 */
49 if (info->flash_id == FLASH_UNKNOWN ||
50 to < info->start[0] || from > b_end) {
51 return;
52 }
53
54 for (i=0; i<info->sector_count; ++i) {
55 ulong end; /* last address in current sect */
56
57 end = (i == s_end) ? b_end : info->start[i + 1] - 1;
58
59 /* Update protection if any part of the sector
60 * is in the specified range.
61 */
62 if (from <= end && to >= info->start[i]) {
63 if (flag & FLAG_PROTECT_CLEAR) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020064#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenkaffae2b2002-08-17 09:36:01 +000065 flash_real_protect(info, i, 0);
66#else
67 info->protect[i] = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020068#endif /* CONFIG_SYS_FLASH_PROTECTION */
Simon Glass8f055af2020-05-10 11:40:04 -060069 debug("protect off %d\n", i);
wdenkaffae2b2002-08-17 09:36:01 +000070 }
71 else if (flag & FLAG_PROTECT_SET) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020072#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenkaffae2b2002-08-17 09:36:01 +000073 flash_real_protect(info, i, 1);
74#else
75 info->protect[i] = 1;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020076#endif /* CONFIG_SYS_FLASH_PROTECTION */
Simon Glass8f055af2020-05-10 11:40:04 -060077 debug("protect on %d\n", i);
wdenkaffae2b2002-08-17 09:36:01 +000078 }
79 }
80 }
81}
82
83/*-----------------------------------------------------------------------
84 */
85
86flash_info_t *
Simon Glassc7443162020-05-10 11:39:53 -060087addr2info(ulong addr)
wdenkaffae2b2002-08-17 09:36:01 +000088{
wdenkaffae2b2002-08-17 09:36:01 +000089 flash_info_t *info;
90 int i;
91
Patrick Delaunay6c5f5602022-01-04 14:23:58 +010092 for (i = 0, info = &flash_info[0]; i < CFI_FLASH_BANKS; ++i, ++info) {
wdenkaffae2b2002-08-17 09:36:01 +000093 if (info->flash_id != FLASH_UNKNOWN &&
94 addr >= info->start[0] &&
95 /* WARNING - The '- 1' is needed if the flash
96 * is at the end of the address space, since
97 * info->start[0] + info->size wraps back to 0.
98 * Please don't change this unless you understand this.
99 */
100 addr <= info->start[0] + info->size - 1) {
101 return (info);
102 }
103 }
wdenkaffae2b2002-08-17 09:36:01 +0000104
105 return (NULL);
106}
107
108/*-----------------------------------------------------------------------
109 * Copy memory to flash.
110 * Make sure all target addresses are within Flash bounds,
111 * and no protected sectors are hit.
112 * Returns:
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200113 * FL_ERR_OK 0 - OK
114 * FL_ERR_TIMEOUT 1 - write timeout
115 * FL_ERR_NOT_ERASED 2 - Flash not erased
116 * FL_ERR_PROTECTED 4 - target range includes protected sectors
117 * FL_ERR_INVAL 8 - target address not in Flash memory
118 * FL_ERR_ALIGN 16 - target address not aligned on boundary
119 * (only some targets require alignment)
wdenkaffae2b2002-08-17 09:36:01 +0000120 */
121int
Simon Glassc7443162020-05-10 11:39:53 -0600122flash_write(char *src, ulong addr, ulong cnt)
wdenkaffae2b2002-08-17 09:36:01 +0000123{
wdenkaffae2b2002-08-17 09:36:01 +0000124 int i;
125 ulong end = addr + cnt - 1;
Simon Glassc7443162020-05-10 11:39:53 -0600126 flash_info_t *info_first = addr2info(addr);
127 flash_info_t *info_last = addr2info(end);
wdenkaffae2b2002-08-17 09:36:01 +0000128 flash_info_t *info;
Stefan Roesed20cba52013-04-04 15:53:14 +0200129 __maybe_unused char *src_orig = src;
130 __maybe_unused char *addr_orig = (char *)addr;
131 __maybe_unused ulong cnt_orig = cnt;
wdenkaffae2b2002-08-17 09:36:01 +0000132
133 if (cnt == 0) {
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200134 return (FL_ERR_OK);
wdenkaffae2b2002-08-17 09:36:01 +0000135 }
136
137 if (!info_first || !info_last) {
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200138 return (FL_ERR_INVAL);
wdenkaffae2b2002-08-17 09:36:01 +0000139 }
140
141 for (info = info_first; info <= info_last; ++info) {
142 ulong b_end = info->start[0] + info->size; /* bank end addr */
143 short s_end = info->sector_count - 1;
144 for (i=0; i<info->sector_count; ++i) {
145 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
146
147 if ((end >= info->start[i]) && (addr < e_addr) &&
148 (info->protect[i] != 0) ) {
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200149 return (FL_ERR_PROTECTED);
wdenkaffae2b2002-08-17 09:36:01 +0000150 }
151 }
152 }
153
154 /* finally write data to flash */
155 for (info = info_first; info <= info_last && cnt>0; ++info) {
156 ulong len;
157
158 len = info->start[0] + info->size - addr;
159 if (len > cnt)
160 len = cnt;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200161 if ((i = write_buff(info, (uchar *)src, addr, len)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000162 return (i);
163 }
164 cnt -= len;
165 addr += len;
166 src += len;
167 }
Stefan Roesed20cba52013-04-04 15:53:14 +0200168
169#if defined(CONFIG_FLASH_VERIFY)
170 if (memcmp(src_orig, addr_orig, cnt_orig)) {
171 printf("\nVerify failed!\n");
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200172 return FL_ERR_PROG_ERROR;
Stefan Roesed20cba52013-04-04 15:53:14 +0200173 }
174#endif /* CONFIG_SYS_FLASH_VERIFY_AFTER_WRITE */
175
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200176 return (FL_ERR_OK);
wdenkaffae2b2002-08-17 09:36:01 +0000177}
178
179/*-----------------------------------------------------------------------
180 */
181
Simon Glassa606ffc2019-12-28 10:44:40 -0700182void flash_perror(int err)
wdenkaffae2b2002-08-17 09:36:01 +0000183{
184 switch (err) {
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200185 case FL_ERR_OK:
wdenkaffae2b2002-08-17 09:36:01 +0000186 break;
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200187 case FL_ERR_TIMEOUT:
wdenkaffae2b2002-08-17 09:36:01 +0000188 puts ("Timeout writing to Flash\n");
189 break;
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200190 case FL_ERR_NOT_ERASED:
wdenkaffae2b2002-08-17 09:36:01 +0000191 puts ("Flash not Erased\n");
192 break;
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200193 case FL_ERR_PROTECTED:
wdenkaffae2b2002-08-17 09:36:01 +0000194 puts ("Can't write to protected Flash sectors\n");
195 break;
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200196 case FL_ERR_INVAL:
wdenkaffae2b2002-08-17 09:36:01 +0000197 puts ("Outside available Flash\n");
198 break;
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200199 case FL_ERR_ALIGN:
wdenkaffae2b2002-08-17 09:36:01 +0000200 puts ("Start and/or end address not on sector boundary\n");
201 break;
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200202 case FL_ERR_UNKNOWN_FLASH_VENDOR:
wdenkaffae2b2002-08-17 09:36:01 +0000203 puts ("Unknown Vendor of Flash\n");
204 break;
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200205 case FL_ERR_UNKNOWN_FLASH_TYPE:
wdenkaffae2b2002-08-17 09:36:01 +0000206 puts ("Unknown Type of Flash\n");
207 break;
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200208 case FL_ERR_PROG_ERROR:
wdenkaffae2b2002-08-17 09:36:01 +0000209 puts ("General Flash Programming Error\n");
210 break;
Jerome Forissier523bc4d2024-09-11 11:58:15 +0200211 case FL_ERR_ABORTED:
Joe Hershberger497c32f2012-08-17 15:36:41 -0500212 puts("Flash Programming Aborted\n");
213 break;
wdenkaffae2b2002-08-17 09:36:01 +0000214 default:
215 printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
216 break;
217 }
218}