3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #if !defined(CONFIG_SYS_NO_FLASH)
30 #include <mtd/cfi_flash.h>
32 extern flash_info_t flash_info
[]; /* info for FLASH chips */
34 /*-----------------------------------------------------------------------
38 /*-----------------------------------------------------------------------
39 * Set protection status for monitor sectors
41 * The monitor is always located in the _first_ Flash bank.
42 * If necessary you have to map the second bank at lower addresses.
45 flash_protect (int flag
, ulong from
, ulong to
, flash_info_t
*info
)
51 /* Do nothing if input data is bad. */
52 if (!info
|| info
->sector_count
== 0 || info
->size
== 0 || to
< from
) {
56 s_end
= info
->sector_count
- 1; /* index of last sector */
57 b_end
= info
->start
[0] + info
->size
- 1; /* bank end address */
59 debug ("flash_protect %s: from 0x%08lX to 0x%08lX\n",
60 (flag
& FLAG_PROTECT_SET
) ? "ON" :
61 (flag
& FLAG_PROTECT_CLEAR
) ? "OFF" : "???",
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.
67 if (info
->flash_id
== FLASH_UNKNOWN
||
68 to
< info
->start
[0] || from
> b_end
) {
72 for (i
=0; i
<info
->sector_count
; ++i
) {
73 ulong end
; /* last address in current sect */
75 end
= (i
== s_end
) ? b_end
: info
->start
[i
+ 1] - 1;
77 /* Update protection if any part of the sector
78 * is in the specified range.
80 if (from
<= end
&& to
>= info
->start
[i
]) {
81 if (flag
& FLAG_PROTECT_CLEAR
) {
82 #if defined(CONFIG_SYS_FLASH_PROTECTION)
83 flash_real_protect(info
, i
, 0);
86 #endif /* CONFIG_SYS_FLASH_PROTECTION */
87 debug ("protect off %d\n", i
);
89 else if (flag
& FLAG_PROTECT_SET
) {
90 #if defined(CONFIG_SYS_FLASH_PROTECTION)
91 flash_real_protect(info
, i
, 1);
94 #endif /* CONFIG_SYS_FLASH_PROTECTION */
95 debug ("protect on %d\n", i
);
101 /*-----------------------------------------------------------------------
105 addr2info (ulong addr
)
107 #ifndef CONFIG_SPD823TS
111 for (i
=0, info
= &flash_info
[0]; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
, ++info
) {
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.
119 addr
<= info
->start
[0] + info
->size
- 1) {
123 #endif /* CONFIG_SPD823TS */
128 /*-----------------------------------------------------------------------
129 * Copy memory to flash.
130 * Make sure all target addresses are within Flash bounds,
131 * and no protected sectors are hit.
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)
142 flash_write (char *src
, ulong addr
, ulong cnt
)
144 #ifdef CONFIG_SPD823TS
145 return (ERR_TIMOUT
); /* any other error codes are possible as well */
148 ulong end
= addr
+ cnt
- 1;
149 flash_info_t
*info_first
= addr2info (addr
);
150 flash_info_t
*info_last
= addr2info (end
);
157 if (!info_first
|| !info_last
) {
161 for (info
= info_first
; info
<= info_last
; ++info
) {
162 ulong b_end
= info
->start
[0] + info
->size
; /* bank end addr */
163 short s_end
= info
->sector_count
- 1;
164 for (i
=0; i
<info
->sector_count
; ++i
) {
165 ulong e_addr
= (i
== s_end
) ? b_end
: info
->start
[i
+ 1];
167 if ((end
>= info
->start
[i
]) && (addr
< e_addr
) &&
168 (info
->protect
[i
] != 0) ) {
169 return (ERR_PROTECTED
);
174 /* finally write data to flash */
175 for (info
= info_first
; info
<= info_last
&& cnt
>0; ++info
) {
178 len
= info
->start
[0] + info
->size
- addr
;
181 if ((i
= write_buff(info
, (uchar
*)src
, addr
, len
)) != 0) {
189 #endif /* CONFIG_SPD823TS */
192 /*-----------------------------------------------------------------------
195 void flash_perror (int err
)
201 puts ("Timeout writing to Flash\n");
204 puts ("Flash not Erased\n");
207 puts ("Can't write to protected Flash sectors\n");
210 puts ("Outside available Flash\n");
213 puts ("Start and/or end address not on sector boundary\n");
215 case ERR_UNKNOWN_FLASH_VENDOR
:
216 puts ("Unknown Vendor of Flash\n");
218 case ERR_UNKNOWN_FLASH_TYPE
:
219 puts ("Unknown Type of Flash\n");
222 puts ("General Flash Programming Error\n");
225 printf ("%s[%d] FIXME: rc=%d\n", __FILE__
, __LINE__
, err
);
230 /*-----------------------------------------------------------------------
232 #endif /* !CONFIG_SYS_NO_FLASH */