2 * (C) Copyright 2000-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 extern flash_info_t flash_info
[]; /* info for FLASH chips */
13 /*-----------------------------------------------------------------------
17 /*-----------------------------------------------------------------------
18 * Set protection status for monitor sectors
20 * The monitor is always located in the _first_ Flash bank.
21 * If necessary you have to map the second bank at lower addresses.
24 flash_protect (int flag
, ulong from
, ulong to
, flash_info_t
*info
)
26 ulong b_end
= info
->start
[0] + info
->size
- 1; /* bank end address */
27 short s_end
= info
->sector_count
- 1; /* index of last sector */
30 /* Do nothing if input data is bad. */
31 if (info
->sector_count
== 0 || info
->size
== 0 || to
< from
) {
35 /* There is nothing to do if we have no data about the flash
36 * or the protect range and flash range don't overlap.
38 if (info
->flash_id
== FLASH_UNKNOWN
||
39 to
< info
->start
[0] || from
> b_end
) {
43 for (i
=0; i
<info
->sector_count
; ++i
) {
44 ulong end
; /* last address in current sect */
46 end
= (i
== s_end
) ? b_end
: info
->start
[i
+ 1] - 1;
48 /* Update protection if any part of the sector
49 * is in the specified range.
51 if (from
<= end
&& to
>= info
->start
[i
]) {
52 if (flag
& FLAG_PROTECT_CLEAR
) {
53 #if defined(CONFIG_SYS_FLASH_PROTECTION)
54 flash_real_protect(info
, i
, 0);
57 #endif /* CONFIG_SYS_FLASH_PROTECTION */
59 else if (flag
& FLAG_PROTECT_SET
) {
60 #if defined(CONFIG_SYS_FLASH_PROTECTION)
61 flash_real_protect(info
, i
, 1);
64 #endif /* CONFIG_SYS_FLASH_PROTECTION */
70 /*-----------------------------------------------------------------------
74 addr2info (ulong addr
)
76 #ifndef CONFIG_SPD823TS
80 for (i
=0, info
= &flash_info
[0]; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
, ++info
) {
81 if (info
->flash_id
!= FLASH_UNKNOWN
&&
82 addr
>= info
->start
[0] &&
83 /* WARNING - The '- 1' is needed if the flash
84 * is at the end of the address space, since
85 * info->start[0] + info->size wraps back to 0.
86 * Please don't change this unless you understand this.
88 addr
<= info
->start
[0] + info
->size
- 1) {
92 #endif /* CONFIG_SPD823TS */
97 /*-----------------------------------------------------------------------
98 * Copy memory to flash.
99 * Make sure all target addresses are within Flash bounds,
100 * and no protected sectors are hit.
103 * ERR_TIMOUT 1 - write timeout
104 * ERR_NOT_ERASED 2 - Flash not erased
105 * ERR_PROTECTED 4 - target range includes protected sectors
106 * ERR_INVAL 8 - target address not in Flash memory
107 * ERR_ALIGN 16 - target address not aligned on boundary
108 * (only some targets require alignment)
111 flash_write (char *src
, ulong addr
, ulong cnt
)
113 #ifdef CONFIG_SPD823TS
114 return (ERR_TIMOUT
); /* any other error codes are possible as well */
117 ulong end
= addr
+ cnt
- 1;
118 flash_info_t
*info_first
= addr2info (addr
);
119 flash_info_t
*info_last
= addr2info (end
);
127 if (!info_first
|| !info_last
) {
131 for (info
= info_first
; info
<= info_last
; ++info
) {
132 ulong b_end
= info
->start
[0] + info
->size
; /* bank end addr */
133 short s_end
= info
->sector_count
- 1;
134 for (i
=0; i
<info
->sector_count
; ++i
) {
135 ulong e_addr
= (i
== s_end
) ? b_end
: info
->start
[i
+ 1];
137 if ((end
>= info
->start
[i
]) && (addr
< e_addr
) &&
138 (info
->protect
[i
] != 0) ) {
139 return (ERR_PROTECTED
);
144 printf("\rWriting ");
145 for (j
=0; j
<20; j
++) putc(177);
146 printf("\rWriting ");
148 /* finally write data to flash */
149 for (info
= info_first
; info
<= info_last
&& cnt
>0; ++info
) {
152 len
= info
->start
[0] + info
->size
- addr
;
156 if ((i
= write_buff(info
, src
, addr
, len
)) != 0) {
164 #endif /* CONFIG_SPD823TS */
167 /*-----------------------------------------------------------------------