blob: 683978e473c79303b26143304bd19128144d4c39 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
wdenk7e780362004-04-08 22:31:29 +000024/* #define DEBUG */
25
wdenkaffae2b2002-08-17 09:36:01 +000026#include <common.h>
27#include <flash.h>
28
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020029#if !defined(CONFIG_SYS_NO_FLASH)
wdenkaffae2b2002-08-17 09:36:01 +000030
Marian Balakowicze6f2e902005-10-11 19:09:42 +020031extern flash_info_t flash_info[]; /* info for FLASH chips */
wdenkaffae2b2002-08-17 09:36:01 +000032
33/*-----------------------------------------------------------------------
34 * Functions
35 */
36
37/*-----------------------------------------------------------------------
38 * Set protection status for monitor sectors
39 *
40 * The monitor is always located in the _first_ Flash bank.
41 * If necessary you have to map the second bank at lower addresses.
42 */
43void
44flash_protect (int flag, ulong from, ulong to, flash_info_t *info)
45{
Mike Frysingera4e8d9f2010-07-28 23:45:03 -040046 ulong b_end;
47 short s_end;
wdenkaffae2b2002-08-17 09:36:01 +000048 int i;
49
50 /* Do nothing if input data is bad. */
Mike Frysingera4e8d9f2010-07-28 23:45:03 -040051 if (!info || info->sector_count == 0 || info->size == 0 || to < from) {
wdenkaffae2b2002-08-17 09:36:01 +000052 return;
53 }
54
Mike Frysingera4e8d9f2010-07-28 23:45:03 -040055 s_end = info->sector_count - 1; /* index of last sector */
56 b_end = info->start[0] + info->size - 1; /* bank end address */
57
Eugene OBriend2f68002007-07-31 10:24:56 +020058 debug ("flash_protect %s: from 0x%08lX to 0x%08lX\n",
59 (flag & FLAG_PROTECT_SET) ? "ON" :
60 (flag & FLAG_PROTECT_CLEAR) ? "OFF" : "???",
61 from, to);
62
wdenkaffae2b2002-08-17 09:36:01 +000063 /* There is nothing to do if we have no data about the flash
64 * or the protect range and flash range don't overlap.
65 */
66 if (info->flash_id == FLASH_UNKNOWN ||
67 to < info->start[0] || from > b_end) {
68 return;
69 }
70
71 for (i=0; i<info->sector_count; ++i) {
72 ulong end; /* last address in current sect */
73
74 end = (i == s_end) ? b_end : info->start[i + 1] - 1;
75
76 /* Update protection if any part of the sector
77 * is in the specified range.
78 */
79 if (from <= end && to >= info->start[i]) {
80 if (flag & FLAG_PROTECT_CLEAR) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020081#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenkaffae2b2002-08-17 09:36:01 +000082 flash_real_protect(info, i, 0);
83#else
84 info->protect[i] = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020085#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk7e780362004-04-08 22:31:29 +000086 debug ("protect off %d\n", i);
wdenkaffae2b2002-08-17 09:36:01 +000087 }
88 else if (flag & FLAG_PROTECT_SET) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020089#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenkaffae2b2002-08-17 09:36:01 +000090 flash_real_protect(info, i, 1);
91#else
92 info->protect[i] = 1;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020093#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk7e780362004-04-08 22:31:29 +000094 debug ("protect on %d\n", i);
wdenkaffae2b2002-08-17 09:36:01 +000095 }
96 }
97 }
98}
99
100/*-----------------------------------------------------------------------
101 */
102
103flash_info_t *
104addr2info (ulong addr)
105{
106#ifndef CONFIG_SPD823TS
107 flash_info_t *info;
108 int i;
109
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200110 for (i=0, info = &flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
wdenkaffae2b2002-08-17 09:36:01 +0000111 if (info->flash_id != FLASH_UNKNOWN &&
112 addr >= info->start[0] &&
113 /* WARNING - The '- 1' is needed if the flash
114 * is at the end of the address space, since
115 * info->start[0] + info->size wraps back to 0.
116 * Please don't change this unless you understand this.
117 */
118 addr <= info->start[0] + info->size - 1) {
119 return (info);
120 }
121 }
122#endif /* CONFIG_SPD823TS */
123
124 return (NULL);
125}
126
127/*-----------------------------------------------------------------------
128 * Copy memory to flash.
129 * Make sure all target addresses are within Flash bounds,
130 * and no protected sectors are hit.
131 * Returns:
132 * ERR_OK 0 - OK
133 * ERR_TIMOUT 1 - write timeout
134 * ERR_NOT_ERASED 2 - Flash not erased
135 * ERR_PROTECTED 4 - target range includes protected sectors
136 * ERR_INVAL 8 - target address not in Flash memory
137 * ERR_ALIGN 16 - target address not aligned on boundary
138 * (only some targets require alignment)
139 */
140int
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200141flash_write (char *src, ulong addr, ulong cnt)
wdenkaffae2b2002-08-17 09:36:01 +0000142{
143#ifdef CONFIG_SPD823TS
144 return (ERR_TIMOUT); /* any other error codes are possible as well */
145#else
146 int i;
147 ulong end = addr + cnt - 1;
148 flash_info_t *info_first = addr2info (addr);
149 flash_info_t *info_last = addr2info (end );
150 flash_info_t *info;
151
152 if (cnt == 0) {
153 return (ERR_OK);
154 }
155
156 if (!info_first || !info_last) {
157 return (ERR_INVAL);
158 }
159
160 for (info = info_first; info <= info_last; ++info) {
161 ulong b_end = info->start[0] + info->size; /* bank end addr */
162 short s_end = info->sector_count - 1;
163 for (i=0; i<info->sector_count; ++i) {
164 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
165
166 if ((end >= info->start[i]) && (addr < e_addr) &&
167 (info->protect[i] != 0) ) {
168 return (ERR_PROTECTED);
169 }
170 }
171 }
172
173 /* finally write data to flash */
174 for (info = info_first; info <= info_last && cnt>0; ++info) {
175 ulong len;
176
177 len = info->start[0] + info->size - addr;
178 if (len > cnt)
179 len = cnt;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200180 if ((i = write_buff(info, (uchar *)src, addr, len)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000181 return (i);
182 }
183 cnt -= len;
184 addr += len;
185 src += len;
186 }
187 return (ERR_OK);
188#endif /* CONFIG_SPD823TS */
189}
190
191/*-----------------------------------------------------------------------
192 */
193
194void flash_perror (int err)
195{
196 switch (err) {
197 case ERR_OK:
198 break;
199 case ERR_TIMOUT:
200 puts ("Timeout writing to Flash\n");
201 break;
202 case ERR_NOT_ERASED:
203 puts ("Flash not Erased\n");
204 break;
205 case ERR_PROTECTED:
206 puts ("Can't write to protected Flash sectors\n");
207 break;
208 case ERR_INVAL:
209 puts ("Outside available Flash\n");
210 break;
211 case ERR_ALIGN:
212 puts ("Start and/or end address not on sector boundary\n");
213 break;
214 case ERR_UNKNOWN_FLASH_VENDOR:
215 puts ("Unknown Vendor of Flash\n");
216 break;
217 case ERR_UNKNOWN_FLASH_TYPE:
218 puts ("Unknown Type of Flash\n");
219 break;
220 case ERR_PROG_ERROR:
221 puts ("General Flash Programming Error\n");
222 break;
223 default:
224 printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
225 break;
226 }
227}
228
229/*-----------------------------------------------------------------------
230 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200231#endif /* !CONFIG_SYS_NO_FLASH */