2 * (C) Copyright 2000-2006
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,
27 extern flash_info_t flash_info
[]; /* info for FLASH chips */
29 /*-----------------------------------------------------------------------
33 /*-----------------------------------------------------------------------
34 * Set protection status for monitor sectors
36 * The monitor is always located in the _first_ Flash bank.
37 * If necessary you have to map the second bank at lower addresses.
40 flash_protect (int flag
, ulong from
, ulong to
, flash_info_t
*info
)
42 ulong b_end
= info
->start
[0] + info
->size
- 1; /* bank end address */
43 short s_end
= info
->sector_count
- 1; /* index of last sector */
46 /* Do nothing if input data is bad. */
47 if (info
->sector_count
== 0 || info
->size
== 0 || to
< from
) {
51 /* There is nothing to do if we have no data about the flash
52 * or the protect range and flash range don't overlap.
54 if (info
->flash_id
== FLASH_UNKNOWN
||
55 to
< info
->start
[0] || from
> b_end
) {
59 for (i
=0; i
<info
->sector_count
; ++i
) {
60 ulong end
; /* last address in current sect */
62 end
= (i
== s_end
) ? b_end
: info
->start
[i
+ 1] - 1;
64 /* Update protection if any part of the sector
65 * is in the specified range.
67 if (from
<= end
&& to
>= info
->start
[i
]) {
68 if (flag
& FLAG_PROTECT_CLEAR
) {
69 #if defined(CFG_FLASH_PROTECTION)
70 flash_real_protect(info
, i
, 0);
73 #endif /* CFG_FLASH_PROTECTION */
75 else if (flag
& FLAG_PROTECT_SET
) {
76 #if defined(CFG_FLASH_PROTECTION)
77 flash_real_protect(info
, i
, 1);
80 #endif /* CFG_FLASH_PROTECTION */
86 /*-----------------------------------------------------------------------
90 addr2info (ulong addr
)
92 #ifndef CONFIG_SPD823TS
96 for (i
=0, info
=&flash_info
[0]; i
<CFG_MAX_FLASH_BANKS
; ++i
, ++info
) {
97 if (info
->flash_id
!= FLASH_UNKNOWN
&&
98 addr
>= info
->start
[0] &&
99 /* WARNING - The '- 1' is needed if the flash
100 * is at the end of the address space, since
101 * info->start[0] + info->size wraps back to 0.
102 * Please don't change this unless you understand this.
104 addr
<= info
->start
[0] + info
->size
- 1) {
108 #endif /* CONFIG_SPD823TS */
113 /*-----------------------------------------------------------------------
114 * Copy memory to flash.
115 * Make sure all target addresses are within Flash bounds,
116 * and no protected sectors are hit.
119 * ERR_TIMOUT 1 - write timeout
120 * ERR_NOT_ERASED 2 - Flash not erased
121 * ERR_PROTECTED 4 - target range includes protected sectors
122 * ERR_INVAL 8 - target address not in Flash memory
123 * ERR_ALIGN 16 - target address not aligned on boundary
124 * (only some targets require alignment)
127 flash_write (char *src
, ulong addr
, ulong cnt
)
129 #ifdef CONFIG_SPD823TS
130 return (ERR_TIMOUT
); /* any other error codes are possible as well */
133 ulong end
= addr
+ cnt
- 1;
134 flash_info_t
*info_first
= addr2info (addr
);
135 flash_info_t
*info_last
= addr2info (end
);
143 if (!info_first
|| !info_last
) {
147 for (info
= info_first
; info
<= info_last
; ++info
) {
148 ulong b_end
= info
->start
[0] + info
->size
; /* bank end addr */
149 short s_end
= info
->sector_count
- 1;
150 for (i
=0; i
<info
->sector_count
; ++i
) {
151 ulong e_addr
= (i
== s_end
) ? b_end
: info
->start
[i
+ 1];
153 if ((end
>= info
->start
[i
]) && (addr
< e_addr
) &&
154 (info
->protect
[i
] != 0) ) {
155 return (ERR_PROTECTED
);
160 printf("\rWriting ");
161 for (j
=0; j
<20; j
++) putc(177);
162 printf("\rWriting ");
164 /* finally write data to flash */
165 for (info
= info_first
; info
<= info_last
&& cnt
>0; ++info
) {
168 len
= info
->start
[0] + info
->size
- addr
;
172 if ((i
= write_buff(info
, src
, addr
, len
)) != 0) {
180 #endif /* CONFIG_SPD823TS */
183 /*-----------------------------------------------------------------------