blob: cce73fa7be6f65df31553c2af7159b88ca725cc5 [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
24#include <common.h>
25#include <mpc8xx.h>
26
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020027flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkaffae2b2002-08-17 09:36:01 +000028
29#ifdef CONFIG_FLASH_16BIT
30#define FLASH_WORD_SIZE unsigned short
31#define FLASH_ID_MASK 0xFFFF
32#else
33#define FLASH_WORD_SIZE unsigned long
34#define FLASH_ID_MASK 0xFFFFFFFF
35#endif
36
37/*-----------------------------------------------------------------------
38 * Functions
39 */
40
Wolfgang Denk53677ef2008-05-20 16:00:29 +020041ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info);
42
wdenkaffae2b2002-08-17 09:36:01 +000043#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +020044static int write_word (flash_info_t * info, ulong dest, ulong data);
wdenkaffae2b2002-08-17 09:36:01 +000045#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +020046static int write_short (flash_info_t * info, ulong dest, ushort data);
wdenkaffae2b2002-08-17 09:36:01 +000047#endif
48/*int flash_write (uchar *, ulong, ulong); */
49/*flash_info_t *addr2info (ulong); */
50
Wolfgang Denk53677ef2008-05-20 16:00:29 +020051static void flash_get_offsets (ulong base, flash_info_t * info);
wdenkaffae2b2002-08-17 09:36:01 +000052
53/*-----------------------------------------------------------------------
54 */
55unsigned long flash_init (void)
56{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020057 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
wdenkaffae2b2002-08-17 09:36:01 +000058 volatile memctl8xx_t *memctl = &immap->im_memctl;
59 unsigned long size_b0, size_b1;
60 int i;
61
62 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020063 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +000064 flash_info[i].flash_id = FLASH_UNKNOWN;
65 }
66
67 /* Static FLASH Bank configuration here - FIXME XXX */
68
Wolfgang Denk53677ef2008-05-20 16:00:29 +020069 size_b0 =
70 flash_get_size ((volatile FLASH_WORD_SIZE *)
71 FLASH_BASE0_PRELIM, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000072 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +020073 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size_b0, size_b0 << 20);
wdenkaffae2b2002-08-17 09:36:01 +000074 }
75
Wolfgang Denk53677ef2008-05-20 16:00:29 +020076 size_b1 =
77 flash_get_size ((volatile FLASH_WORD_SIZE *)
78 FLASH_BASE1_PRELIM, &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +000079
80 if (size_b1 > size_b0) {
81 printf ("## ERROR: "
82 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
Wolfgang Denk53677ef2008-05-20 16:00:29 +020083 size_b1, size_b1 << 20, size_b0, size_b0 << 20);
84 flash_info[0].flash_id = FLASH_UNKNOWN;
85 flash_info[1].flash_id = FLASH_UNKNOWN;
86 flash_info[0].sector_count = -1;
87 flash_info[1].sector_count = -1;
88 flash_info[0].size = 0;
89 flash_info[1].size = 0;
wdenkaffae2b2002-08-17 09:36:01 +000090 return (0);
91 }
92
93 /* Remap FLASH according to real size */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020094 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
95 memctl->memc_br0 = CONFIG_SYS_FLASH_BASE | 0x00000801; /* (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V; */
wdenkaffae2b2002-08-17 09:36:01 +000096
97 /* Re-do sizing to get full correct info */
98
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020099 size_b0 = flash_get_size ((volatile FLASH_WORD_SIZE *) CONFIG_SYS_FLASH_BASE,
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200100 &flash_info[0]);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200101 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000102
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200103#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +0000104 /* monitor protection ON by default */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200105 (void) flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200106 CONFIG_SYS_MONITOR_BASE,
107 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200108 &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000109#endif
110
111 if (size_b1) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200112 memctl->memc_or1 =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200113 CONFIG_SYS_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200114 memctl->memc_br1 =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200115 (CONFIG_SYS_FLASH_BASE | 0x00000801) + (size_b0 & BR_BA_MSK);
116 /*((CONFIG_SYS_FLASH_BASE + size_b0) & BR_BA_MSK) |
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200117 BR_MS_GPCM | BR_V; */
wdenkaffae2b2002-08-17 09:36:01 +0000118
119 /* Re-do sizing to get full correct info */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200120 size_b1 =
121 flash_get_size ((volatile FLASH_WORD_SIZE
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200122 *) (CONFIG_SYS_FLASH_BASE + size_b0),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200123 &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +0000124
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200125 flash_get_offsets (CONFIG_SYS_FLASH_BASE + size_b0, &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +0000126
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200127#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +0000128 /* monitor protection ON by default */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200129 (void) flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200130 CONFIG_SYS_MONITOR_BASE,
131 CONFIG_SYS_MONITOR_BASE + monitor_flash_len -
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200132 1, &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +0000133#endif
134 } else {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200135 memctl->memc_br1 = 0; /* invalidate bank */
wdenkaffae2b2002-08-17 09:36:01 +0000136
137 flash_info[1].flash_id = FLASH_UNKNOWN;
138 flash_info[1].sector_count = -1;
139 }
140
141 flash_info[0].size = size_b0;
142 flash_info[1].size = size_b1;
143
144 return (size_b0 + size_b1);
145}
146
147/*-----------------------------------------------------------------------
148 */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200149static void flash_get_offsets (ulong base, flash_info_t * info)
wdenkaffae2b2002-08-17 09:36:01 +0000150{
151 int i;
152
153 /* set up sector start adress table */
154 if (info->flash_id & FLASH_BTYPE) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200155 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
wdenkaffae2b2002-08-17 09:36:01 +0000156
157#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200158 /* set sector offsets for bottom boot block type */
159 info->start[0] = base + 0x00000000;
160 info->start[1] = base + 0x00004000;
161 info->start[2] = base + 0x00008000;
162 info->start[3] = base + 0x0000C000;
163 info->start[4] = base + 0x00010000;
164 info->start[5] = base + 0x00014000;
165 info->start[6] = base + 0x00018000;
166 info->start[7] = base + 0x0001C000;
167 for (i = 8; i < info->sector_count; i++) {
168 info->start[i] =
169 base + (i * 0x00020000) - 0x000E0000;
170 }
171 } else {
172 /* set sector offsets for bottom boot block type */
173 info->start[0] = base + 0x00000000;
174 info->start[1] = base + 0x00008000;
175 info->start[2] = base + 0x0000C000;
176 info->start[3] = base + 0x00010000;
177 for (i = 4; i < info->sector_count; i++) {
178 info->start[i] =
179 base + (i * 0x00020000) - 0x00060000;
180 }
wdenk8bde7f72003-06-27 21:31:46 +0000181 }
wdenkaffae2b2002-08-17 09:36:01 +0000182#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200183 /* set sector offsets for bottom boot block type */
184 info->start[0] = base + 0x00000000;
185 info->start[1] = base + 0x00002000;
186 info->start[2] = base + 0x00004000;
187 info->start[3] = base + 0x00006000;
188 info->start[4] = base + 0x00008000;
189 info->start[5] = base + 0x0000A000;
190 info->start[6] = base + 0x0000C000;
191 info->start[7] = base + 0x0000E000;
192 for (i = 8; i < info->sector_count; i++) {
193 info->start[i] =
194 base + (i * 0x00010000) - 0x00070000;
195 }
196 } else {
197 /* set sector offsets for bottom boot block type */
198 info->start[0] = base + 0x00000000;
199 info->start[1] = base + 0x00004000;
200 info->start[2] = base + 0x00006000;
201 info->start[3] = base + 0x00008000;
202 for (i = 4; i < info->sector_count; i++) {
203 info->start[i] =
204 base + (i * 0x00010000) - 0x00030000;
205 }
wdenk8bde7f72003-06-27 21:31:46 +0000206 }
wdenkaffae2b2002-08-17 09:36:01 +0000207#endif
208 } else {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200209 /* set sector offsets for top boot block type */
wdenkaffae2b2002-08-17 09:36:01 +0000210 i = info->sector_count - 1;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200211 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
wdenkaffae2b2002-08-17 09:36:01 +0000212
213#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200214 info->start[i--] = base + info->size - 0x00004000;
215 info->start[i--] = base + info->size - 0x00008000;
216 info->start[i--] = base + info->size - 0x0000C000;
217 info->start[i--] = base + info->size - 0x00010000;
218 info->start[i--] = base + info->size - 0x00014000;
219 info->start[i--] = base + info->size - 0x00018000;
220 info->start[i--] = base + info->size - 0x0001C000;
221 for (; i >= 0; i--) {
222 info->start[i] = base + i * 0x00020000;
223 }
wdenkaffae2b2002-08-17 09:36:01 +0000224
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200225 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000226
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200227 info->start[i--] = base + info->size - 0x00008000;
228 info->start[i--] = base + info->size - 0x0000C000;
229 info->start[i--] = base + info->size - 0x00010000;
230 for (; i >= 0; i--) {
231 info->start[i] = base + i * 0x00020000;
232 }
wdenkaffae2b2002-08-17 09:36:01 +0000233 }
wdenkaffae2b2002-08-17 09:36:01 +0000234#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200235 info->start[i--] = base + info->size - 0x00002000;
236 info->start[i--] = base + info->size - 0x00004000;
237 info->start[i--] = base + info->size - 0x00006000;
238 info->start[i--] = base + info->size - 0x00008000;
239 info->start[i--] = base + info->size - 0x0000A000;
240 info->start[i--] = base + info->size - 0x0000C000;
241 info->start[i--] = base + info->size - 0x0000E000;
242 for (; i >= 0; i--) {
243 info->start[i] = base + i * 0x00010000;
244 }
wdenkaffae2b2002-08-17 09:36:01 +0000245
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200246 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000247
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200248 info->start[i--] = base + info->size - 0x00004000;
249 info->start[i--] = base + info->size - 0x00006000;
250 info->start[i--] = base + info->size - 0x00008000;
251 for (; i >= 0; i--) {
252 info->start[i] = base + i * 0x00010000;
253 }
wdenkaffae2b2002-08-17 09:36:01 +0000254 }
wdenkaffae2b2002-08-17 09:36:01 +0000255#endif
256 }
257
258
259}
260
261/*-----------------------------------------------------------------------
262 */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200263void flash_print_info (flash_info_t * info)
wdenkaffae2b2002-08-17 09:36:01 +0000264{
265 int i;
266 uchar *boottype;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200267 uchar botboot[] = ", bottom boot sect)\n";
268 uchar topboot[] = ", top boot sector)\n";
wdenkaffae2b2002-08-17 09:36:01 +0000269
270 if (info->flash_id == FLASH_UNKNOWN) {
271 printf ("missing or unknown FLASH type\n");
272 return;
273 }
274
275 switch (info->flash_id & FLASH_VENDMASK) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200276 case FLASH_MAN_AMD:
277 printf ("AMD ");
278 break;
279 case FLASH_MAN_FUJ:
280 printf ("FUJITSU ");
281 break;
282 case FLASH_MAN_SST:
283 printf ("SST ");
284 break;
285 case FLASH_MAN_STM:
286 printf ("STM ");
287 break;
288 case FLASH_MAN_INTEL:
289 printf ("INTEL ");
290 break;
291 default:
292 printf ("Unknown Vendor ");
293 break;
wdenkaffae2b2002-08-17 09:36:01 +0000294 }
295
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200296 if (info->flash_id & 0x0001) {
297 boottype = botboot;
wdenkaffae2b2002-08-17 09:36:01 +0000298 } else {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200299 boottype = topboot;
wdenkaffae2b2002-08-17 09:36:01 +0000300 }
301
302 switch (info->flash_id & FLASH_TYPEMASK) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200303 case FLASH_AM400B:
304 printf ("AM29LV400B (4 Mbit%s", boottype);
305 break;
306 case FLASH_AM400T:
307 printf ("AM29LV400T (4 Mbit%s", boottype);
308 break;
309 case FLASH_AM800B:
310 printf ("AM29LV800B (8 Mbit%s", boottype);
311 break;
312 case FLASH_AM800T:
313 printf ("AM29LV800T (8 Mbit%s", boottype);
314 break;
315 case FLASH_AM160B:
316 printf ("AM29LV160B (16 Mbit%s", boottype);
317 break;
318 case FLASH_AM160T:
319 printf ("AM29LV160T (16 Mbit%s", boottype);
320 break;
321 case FLASH_AM320B:
322 printf ("AM29LV320B (32 Mbit%s", boottype);
323 break;
324 case FLASH_AM320T:
325 printf ("AM29LV320T (32 Mbit%s", boottype);
326 break;
327 case FLASH_INTEL800B:
328 printf ("INTEL28F800B (8 Mbit%s", boottype);
329 break;
330 case FLASH_INTEL800T:
331 printf ("INTEL28F800T (8 Mbit%s", boottype);
332 break;
333 case FLASH_INTEL160B:
334 printf ("INTEL28F160B (16 Mbit%s", boottype);
335 break;
336 case FLASH_INTEL160T:
337 printf ("INTEL28F160T (16 Mbit%s", boottype);
338 break;
339 case FLASH_INTEL320B:
340 printf ("INTEL28F320B (32 Mbit%s", boottype);
341 break;
342 case FLASH_INTEL320T:
343 printf ("INTEL28F320T (32 Mbit%s", boottype);
344 break;
wdenkaffae2b2002-08-17 09:36:01 +0000345
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200346#if 0 /* enable when devices are available */
wdenkaffae2b2002-08-17 09:36:01 +0000347
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200348 case FLASH_INTEL640B:
349 printf ("INTEL28F640B (64 Mbit%s", boottype);
350 break;
351 case FLASH_INTEL640T:
352 printf ("INTEL28F640T (64 Mbit%s", boottype);
353 break;
wdenkaffae2b2002-08-17 09:36:01 +0000354#endif
355
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200356 default:
357 printf ("Unknown Chip Type\n");
358 break;
wdenkaffae2b2002-08-17 09:36:01 +0000359 }
360
361 printf (" Size: %ld MB in %d Sectors\n",
362 info->size >> 20, info->sector_count);
363
364 printf (" Sector Start Addresses:");
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200365 for (i = 0; i < info->sector_count; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000366 if ((i % 5) == 0)
367 printf ("\n ");
368 printf (" %08lX%s",
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200369 info->start[i], info->protect[i] ? " (RO)" : " ");
wdenkaffae2b2002-08-17 09:36:01 +0000370 }
371 printf ("\n");
372 return;
373}
374
375/*-----------------------------------------------------------------------
376 */
377
378
379/*-----------------------------------------------------------------------
380 */
381
382/*
383 * The following code cannot be run from FLASH!
384 */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200385ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info)
wdenkaffae2b2002-08-17 09:36:01 +0000386{
387 short i;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200388 ulong base = (ulong) addr;
wdenkaffae2b2002-08-17 09:36:01 +0000389 FLASH_WORD_SIZE value;
390
391 /* Write auto select command: read Manufacturer ID */
392
393
394#ifndef CONFIG_FLASH_16BIT
395
396 /*
397 * Note: if it is an AMD flash and the word at addr[0000]
wdenk8bde7f72003-06-27 21:31:46 +0000398 * is 0x00890089 this routine will think it is an Intel
399 * flash device and may(most likely) cause trouble.
wdenkaffae2b2002-08-17 09:36:01 +0000400 */
401
402 addr[0x0000] = 0x00900090;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200403 if (addr[0x0000] != 0x00890089) {
wdenkaffae2b2002-08-17 09:36:01 +0000404 addr[0x0555] = 0x00AA00AA;
405 addr[0x02AA] = 0x00550055;
406 addr[0x0555] = 0x00900090;
407#else
408
409 /*
410 * Note: if it is an AMD flash and the word at addr[0000]
wdenk8bde7f72003-06-27 21:31:46 +0000411 * is 0x0089 this routine will think it is an Intel
412 * flash device and may(most likely) cause trouble.
wdenkaffae2b2002-08-17 09:36:01 +0000413 */
414
415 addr[0x0000] = 0x0090;
416
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200417 if (addr[0x0000] != 0x0089) {
wdenkaffae2b2002-08-17 09:36:01 +0000418 addr[0x0555] = 0x00AA;
419 addr[0x02AA] = 0x0055;
420 addr[0x0555] = 0x0090;
421#endif
422 }
423 value = addr[0];
424
425 switch (value) {
426 case (AMD_MANUFACT & FLASH_ID_MASK):
427 info->flash_id = FLASH_MAN_AMD;
428 break;
429 case (FUJ_MANUFACT & FLASH_ID_MASK):
430 info->flash_id = FLASH_MAN_FUJ;
431 break;
432 case (STM_MANUFACT & FLASH_ID_MASK):
433 info->flash_id = FLASH_MAN_STM;
434 break;
435 case (SST_MANUFACT & FLASH_ID_MASK):
436 info->flash_id = FLASH_MAN_SST;
437 break;
438 case (INTEL_MANUFACT & FLASH_ID_MASK):
439 info->flash_id = FLASH_MAN_INTEL;
440 break;
441 default:
442 info->flash_id = FLASH_UNKNOWN;
443 info->sector_count = 0;
444 info->size = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200445 return (0); /* no or unknown flash */
wdenkaffae2b2002-08-17 09:36:01 +0000446
447 }
448
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200449 value = addr[1]; /* device ID */
wdenkaffae2b2002-08-17 09:36:01 +0000450
451 switch (value) {
452
453 case (AMD_ID_LV400T & FLASH_ID_MASK):
454 info->flash_id += FLASH_AM400T;
455 info->sector_count = 11;
456 info->size = 0x00100000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200457 break; /* => 1 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000458
459 case (AMD_ID_LV400B & FLASH_ID_MASK):
460 info->flash_id += FLASH_AM400B;
461 info->sector_count = 11;
462 info->size = 0x00100000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200463 break; /* => 1 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000464
465 case (AMD_ID_LV800T & FLASH_ID_MASK):
466 info->flash_id += FLASH_AM800T;
467 info->sector_count = 19;
468 info->size = 0x00200000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200469 break; /* => 2 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000470
471 case (AMD_ID_LV800B & FLASH_ID_MASK):
472 info->flash_id += FLASH_AM800B;
473 info->sector_count = 19;
474 info->size = 0x00200000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200475 break; /* => 2 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000476
477 case (AMD_ID_LV160T & FLASH_ID_MASK):
478 info->flash_id += FLASH_AM160T;
479 info->sector_count = 35;
480 info->size = 0x00400000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200481 break; /* => 4 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000482
483 case (AMD_ID_LV160B & FLASH_ID_MASK):
484 info->flash_id += FLASH_AM160B;
485 info->sector_count = 35;
486 info->size = 0x00400000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200487 break; /* => 4 MB */
488#if 0 /* enable when device IDs are available */
wdenkaffae2b2002-08-17 09:36:01 +0000489 case (AMD_ID_LV320T & FLASH_ID_MASK):
490 info->flash_id += FLASH_AM320T;
491 info->sector_count = 67;
492 info->size = 0x00800000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200493 break; /* => 8 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000494
495 case (AMD_ID_LV320B & FLASH_ID_MASK):
496 info->flash_id += FLASH_AM320B;
497 info->sector_count = 67;
498 info->size = 0x00800000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200499 break; /* => 8 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000500#endif
501
502 case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
503 info->flash_id += FLASH_INTEL800T;
504 info->sector_count = 23;
505 info->size = 0x00200000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200506 break; /* => 2 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000507
508 case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
509 info->flash_id += FLASH_INTEL800B;
510 info->sector_count = 23;
511 info->size = 0x00200000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200512 break; /* => 2 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000513
514 case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
515 info->flash_id += FLASH_INTEL160T;
516 info->sector_count = 39;
517 info->size = 0x00400000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200518 break; /* => 4 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000519
520 case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
521 info->flash_id += FLASH_INTEL160B;
522 info->sector_count = 39;
523 info->size = 0x00400000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200524 break; /* => 4 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000525
526 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
527 info->flash_id += FLASH_INTEL320T;
528 info->sector_count = 71;
529 info->size = 0x00800000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200530 break; /* => 8 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000531
532 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
533 info->flash_id += FLASH_AM320B;
534 info->sector_count = 71;
535 info->size = 0x00800000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200536 break; /* => 8 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000537
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200538#if 0 /* enable when devices are available */
wdenkaffae2b2002-08-17 09:36:01 +0000539 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
540 info->flash_id += FLASH_INTEL320T;
541 info->sector_count = 135;
542 info->size = 0x01000000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200543 break; /* => 16 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000544
545 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
546 info->flash_id += FLASH_AM320B;
547 info->sector_count = 135;
548 info->size = 0x01000000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200549 break; /* => 16 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000550#endif
551 default:
552 info->flash_id = FLASH_UNKNOWN;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200553 return (0); /* => no or unknown flash */
wdenkaffae2b2002-08-17 09:36:01 +0000554
555 }
556
557 /* set up sector start adress table */
558 if (info->flash_id & FLASH_BTYPE) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200559 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
wdenkaffae2b2002-08-17 09:36:01 +0000560
561#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200562 /* set sector offsets for bottom boot block type */
563 info->start[0] = base + 0x00000000;
564 info->start[1] = base + 0x00004000;
565 info->start[2] = base + 0x00008000;
566 info->start[3] = base + 0x0000C000;
567 info->start[4] = base + 0x00010000;
568 info->start[5] = base + 0x00014000;
569 info->start[6] = base + 0x00018000;
570 info->start[7] = base + 0x0001C000;
571 for (i = 8; i < info->sector_count; i++) {
572 info->start[i] =
573 base + (i * 0x00020000) - 0x000E0000;
574 }
575 } else {
576 /* set sector offsets for bottom boot block type */
577 info->start[0] = base + 0x00000000;
578 info->start[1] = base + 0x00008000;
579 info->start[2] = base + 0x0000C000;
580 info->start[3] = base + 0x00010000;
581 for (i = 4; i < info->sector_count; i++) {
582 info->start[i] =
583 base + (i * 0x00020000) - 0x00060000;
584 }
wdenk8bde7f72003-06-27 21:31:46 +0000585 }
wdenkaffae2b2002-08-17 09:36:01 +0000586#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200587 /* set sector offsets for bottom boot block type */
588 info->start[0] = base + 0x00000000;
589 info->start[1] = base + 0x00002000;
590 info->start[2] = base + 0x00004000;
591 info->start[3] = base + 0x00006000;
592 info->start[4] = base + 0x00008000;
593 info->start[5] = base + 0x0000A000;
594 info->start[6] = base + 0x0000C000;
595 info->start[7] = base + 0x0000E000;
596 for (i = 8; i < info->sector_count; i++) {
597 info->start[i] =
598 base + (i * 0x00010000) - 0x00070000;
599 }
600 } else {
601 /* set sector offsets for bottom boot block type */
602 info->start[0] = base + 0x00000000;
603 info->start[1] = base + 0x00004000;
604 info->start[2] = base + 0x00006000;
605 info->start[3] = base + 0x00008000;
606 for (i = 4; i < info->sector_count; i++) {
607 info->start[i] =
608 base + (i * 0x00010000) - 0x00030000;
609 }
wdenk8bde7f72003-06-27 21:31:46 +0000610 }
wdenkaffae2b2002-08-17 09:36:01 +0000611#endif
612 } else {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200613 /* set sector offsets for top boot block type */
wdenkaffae2b2002-08-17 09:36:01 +0000614 i = info->sector_count - 1;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200615 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
wdenkaffae2b2002-08-17 09:36:01 +0000616
617#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200618 info->start[i--] = base + info->size - 0x00004000;
619 info->start[i--] = base + info->size - 0x00008000;
620 info->start[i--] = base + info->size - 0x0000C000;
621 info->start[i--] = base + info->size - 0x00010000;
622 info->start[i--] = base + info->size - 0x00014000;
623 info->start[i--] = base + info->size - 0x00018000;
624 info->start[i--] = base + info->size - 0x0001C000;
625 for (; i >= 0; i--) {
626 info->start[i] = base + i * 0x00020000;
627 }
wdenkaffae2b2002-08-17 09:36:01 +0000628
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200629 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000630
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200631 info->start[i--] = base + info->size - 0x00008000;
632 info->start[i--] = base + info->size - 0x0000C000;
633 info->start[i--] = base + info->size - 0x00010000;
634 for (; i >= 0; i--) {
635 info->start[i] = base + i * 0x00020000;
636 }
wdenkaffae2b2002-08-17 09:36:01 +0000637 }
wdenkaffae2b2002-08-17 09:36:01 +0000638#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200639 info->start[i--] = base + info->size - 0x00002000;
640 info->start[i--] = base + info->size - 0x00004000;
641 info->start[i--] = base + info->size - 0x00006000;
642 info->start[i--] = base + info->size - 0x00008000;
643 info->start[i--] = base + info->size - 0x0000A000;
644 info->start[i--] = base + info->size - 0x0000C000;
645 info->start[i--] = base + info->size - 0x0000E000;
646 for (; i >= 0; i--) {
647 info->start[i] = base + i * 0x00010000;
648 }
wdenkaffae2b2002-08-17 09:36:01 +0000649
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200650 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000651
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200652 info->start[i--] = base + info->size - 0x00004000;
653 info->start[i--] = base + info->size - 0x00006000;
654 info->start[i--] = base + info->size - 0x00008000;
655 for (; i >= 0; i--) {
656 info->start[i] = base + i * 0x00010000;
657 }
wdenkaffae2b2002-08-17 09:36:01 +0000658 }
wdenkaffae2b2002-08-17 09:36:01 +0000659#endif
660 }
661
662 /* check for protected sectors */
663 for (i = 0; i < info->sector_count; i++) {
664 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
665 /* D0 = 1 if protected */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200666 addr = (volatile FLASH_WORD_SIZE *) (info->start[i]);
wdenkaffae2b2002-08-17 09:36:01 +0000667 info->protect[i] = addr[2] & 1;
668 }
669
670 /*
671 * Prevent writes to uninitialized FLASH.
672 */
673 if (info->flash_id != FLASH_UNKNOWN) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200674 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
675 if ((info->flash_id & 0xFF00) == FLASH_MAN_INTEL) {
676 *addr = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
wdenkaffae2b2002-08-17 09:36:01 +0000677 } else {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200678 *addr = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
wdenkaffae2b2002-08-17 09:36:01 +0000679 }
680 }
681
682 return (info->size);
683}
684
685
686/*-----------------------------------------------------------------------
687 */
688
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200689int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenkaffae2b2002-08-17 09:36:01 +0000690{
691
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200692 volatile FLASH_WORD_SIZE *addr =
693 (volatile FLASH_WORD_SIZE *) (info->start[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000694 int flag, prot, sect, l_sect, barf;
695 ulong start, now, last;
696 int rcode = 0;
697
698 if ((s_first < 0) || (s_first > s_last)) {
699 if (info->flash_id == FLASH_UNKNOWN) {
700 printf ("- missing\n");
701 } else {
702 printf ("- no sectors to erase\n");
703 }
704 return 1;
705 }
706
707 if ((info->flash_id == FLASH_UNKNOWN) ||
708 ((info->flash_id > FLASH_AMD_COMP) &&
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200709 ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL))) {
wdenkaffae2b2002-08-17 09:36:01 +0000710 printf ("Can't erase unknown flash type - aborted\n");
711 return 1;
712 }
713
714 prot = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200715 for (sect = s_first; sect <= s_last; ++sect) {
wdenkaffae2b2002-08-17 09:36:01 +0000716 if (info->protect[sect]) {
717 prot++;
718 }
719 }
720
721 if (prot) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200722 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenkaffae2b2002-08-17 09:36:01 +0000723 } else {
724 printf ("\n");
725 }
726
727 l_sect = -1;
728
729 /* Disable interrupts which might cause a timeout here */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200730 flag = disable_interrupts ();
731 if (info->flash_id < FLASH_AMD_COMP) {
wdenkaffae2b2002-08-17 09:36:01 +0000732#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200733 addr[0x0555] = 0x00AA00AA;
734 addr[0x02AA] = 0x00550055;
735 addr[0x0555] = 0x00800080;
736 addr[0x0555] = 0x00AA00AA;
737 addr[0x02AA] = 0x00550055;
wdenkaffae2b2002-08-17 09:36:01 +0000738#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200739 addr[0x0555] = 0x00AA;
740 addr[0x02AA] = 0x0055;
741 addr[0x0555] = 0x0080;
742 addr[0x0555] = 0x00AA;
743 addr[0x02AA] = 0x0055;
wdenkaffae2b2002-08-17 09:36:01 +0000744#endif
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200745 /* Start erase on unprotected sectors */
746 for (sect = s_first; sect <= s_last; sect++) {
747 if (info->protect[sect] == 0) { /* not protected */
748 addr = (volatile FLASH_WORD_SIZE *) (info->start[sect]);
749 addr[0] = (0x00300030 & FLASH_ID_MASK);
750 l_sect = sect;
wdenkaffae2b2002-08-17 09:36:01 +0000751 }
wdenkaffae2b2002-08-17 09:36:01 +0000752 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200753
754 /* re-enable interrupts if necessary */
755 if (flag)
756 enable_interrupts ();
757
758 /* wait at least 80us - let's wait 1 ms */
759 udelay (1000);
760
761 /*
762 * We wait for the last triggered sector
763 */
764 if (l_sect < 0)
765 goto DONE;
766
767 start = get_timer (0);
768 last = start;
769 addr = (volatile FLASH_WORD_SIZE *) (info->start[l_sect]);
770 while ((addr[0] & (0x00800080 & FLASH_ID_MASK)) !=
771 (0x00800080 & FLASH_ID_MASK)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200772 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200773 printf ("Timeout\n");
774 return 1;
775 }
776 /* show that we're waiting */
777 if ((now - last) > 1000) { /* every second */
778 serial_putc ('.');
779 last = now;
780 }
781 }
782
783 DONE:
784 /* reset to read mode */
785 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
786 addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
787 } else {
788
789
790 for (sect = s_first; sect <= s_last; sect++) {
791 if (info->protect[sect] == 0) { /* not protected */
792 barf = 0;
793#ifndef CONFIG_FLASH_16BIT
794 addr = (vu_long *) (info->start[sect]);
795 addr[0] = 0x00200020;
796 addr[0] = 0x00D000D0;
797 while (!(addr[0] & 0x00800080)); /* wait for error or finish */
798 if (addr[0] & 0x003A003A) { /* check for error */
799 barf = addr[0] & 0x003A0000;
800 if (barf) {
801 barf >>= 16;
802 } else {
803 barf = addr[0] & 0x0000003A;
804 }
805 }
806#else
807 addr = (vu_short *) (info->start[sect]);
808 addr[0] = 0x0020;
809 addr[0] = 0x00D0;
810 while (!(addr[0] & 0x0080)); /* wait for error or finish */
811 if (addr[0] & 0x003A) /* check for error */
812 barf = addr[0] & 0x003A;
813#endif
814 if (barf) {
815 printf ("\nFlash error in sector at %lx\n", (unsigned long) addr);
816 if (barf & 0x0002)
817 printf ("Block locked, not erased.\n");
818 if ((barf & 0x0030) == 0x0030)
819 printf ("Command Sequence error.\n");
820 if ((barf & 0x0030) == 0x0020)
821 printf ("Block Erase error.\n");
822 if (barf & 0x0008)
823 printf ("Vpp Low error.\n");
824 rcode = 1;
825 } else
826 printf (".");
827 l_sect = sect;
828 }
829 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
830 addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
831
832 }
wdenkaffae2b2002-08-17 09:36:01 +0000833
834 }
wdenkaffae2b2002-08-17 09:36:01 +0000835 printf (" done\n");
836 return rcode;
837}
838
839/*-----------------------------------------------------------------------
840 */
841
842/*-----------------------------------------------------------------------
843 * Copy memory to flash, returns:
844 * 0 - OK
845 * 1 - write timeout
846 * 2 - Flash not erased
847 */
848
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200849int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenkaffae2b2002-08-17 09:36:01 +0000850{
851#ifndef CONFIG_FLASH_16BIT
852 ulong cp, wp, data;
853 int l;
854#else
855 ulong cp, wp;
856 ushort data;
857#endif
858 int i, rc;
859
860#ifndef CONFIG_FLASH_16BIT
861
862
863 wp = (addr & ~3); /* get lower word aligned address */
864
865 /*
866 * handle unaligned start bytes
867 */
868 if ((l = addr - wp) != 0) {
869 data = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200870 for (i = 0, cp = wp; i < l; ++i, ++cp) {
871 data = (data << 8) | (*(uchar *) cp);
wdenkaffae2b2002-08-17 09:36:01 +0000872 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200873 for (; i < 4 && cnt > 0; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000874 data = (data << 8) | *src++;
875 --cnt;
876 ++cp;
877 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200878 for (; cnt == 0 && i < 4; ++i, ++cp) {
879 data = (data << 8) | (*(uchar *) cp);
wdenkaffae2b2002-08-17 09:36:01 +0000880 }
881
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200882 if ((rc = write_word (info, wp, data)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000883 return (rc);
884 }
885 wp += 4;
886 }
887
888 /*
889 * handle word aligned part
890 */
891 while (cnt >= 4) {
892 data = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200893 for (i = 0; i < 4; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000894 data = (data << 8) | *src++;
895 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200896 if ((rc = write_word (info, wp, data)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000897 return (rc);
898 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200899 wp += 4;
wdenkaffae2b2002-08-17 09:36:01 +0000900 cnt -= 4;
901 }
902
903 if (cnt == 0) {
904 return (0);
905 }
906
907 /*
908 * handle unaligned tail bytes
909 */
910 data = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200911 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
wdenkaffae2b2002-08-17 09:36:01 +0000912 data = (data << 8) | *src++;
913 --cnt;
914 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200915 for (; i < 4; ++i, ++cp) {
916 data = (data << 8) | (*(uchar *) cp);
wdenkaffae2b2002-08-17 09:36:01 +0000917 }
918
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200919 return (write_word (info, wp, data));
wdenkaffae2b2002-08-17 09:36:01 +0000920
921#else
922 wp = (addr & ~1); /* get lower word aligned address */
923
924 /*
925 * handle unaligned start byte
926 */
927 if (addr - wp) {
928 data = 0;
929 data = (data << 8) | *src++;
930 --cnt;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200931 if ((rc = write_short (info, wp, data)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000932 return (rc);
933 }
934 wp += 2;
935 }
936
937 /*
938 * handle word aligned part
939 */
940/* l = 0; used for debuging */
941 while (cnt >= 2) {
942 data = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200943 for (i = 0; i < 2; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000944 data = (data << 8) | *src++;
945 }
946
947/* if(!l){
948 printf("%x",data);
949 l = 1;
950 } used for debuging */
951
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200952 if ((rc = write_short (info, wp, data)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000953 return (rc);
954 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200955 wp += 2;
wdenkaffae2b2002-08-17 09:36:01 +0000956 cnt -= 2;
957 }
958
959 if (cnt == 0) {
960 return (0);
961 }
962
963 /*
964 * handle unaligned tail bytes
965 */
966 data = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200967 for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
wdenkaffae2b2002-08-17 09:36:01 +0000968 data = (data << 8) | *src++;
969 --cnt;
970 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200971 for (; i < 2; ++i, ++cp) {
972 data = (data << 8) | (*(uchar *) cp);
wdenkaffae2b2002-08-17 09:36:01 +0000973 }
974
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200975 return (write_short (info, wp, data));
wdenkaffae2b2002-08-17 09:36:01 +0000976
977
978#endif
979}
980
981/*-----------------------------------------------------------------------
982 * Write a word to Flash, returns:
983 * 0 - OK
984 * 1 - write timeout
985 * 2 - Flash not erased
986 */
987#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200988static int write_word (flash_info_t * info, ulong dest, ulong data)
wdenkaffae2b2002-08-17 09:36:01 +0000989{
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200990 vu_long *addr = (vu_long *) (info->start[0]);
991 ulong start, barf;
wdenkaffae2b2002-08-17 09:36:01 +0000992 int flag;
993
994
995 /* Check if Flash is (sufficiently) erased */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200996 if ((*((vu_long *) dest) & data) != data) {
wdenkaffae2b2002-08-17 09:36:01 +0000997 return (2);
998 }
999
1000 /* Disable interrupts which might cause a timeout here */
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001001 flag = disable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +00001002
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001003 if (info->flash_id > FLASH_AMD_COMP) {
1004 /* AMD stuff */
1005 addr[0x0555] = 0x00AA00AA;
1006 addr[0x02AA] = 0x00550055;
1007 addr[0x0555] = 0x00A000A0;
1008 } else {
1009 /* intel stuff */
1010 *addr = 0x00400040;
1011 }
1012 *((vu_long *) dest) = data;
wdenkaffae2b2002-08-17 09:36:01 +00001013
1014 /* re-enable interrupts if necessary */
1015 if (flag)
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001016 enable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +00001017
1018 /* data polling for D7 */
1019 start = get_timer (0);
1020
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001021 if (info->flash_id > FLASH_AMD_COMP) {
wdenkaffae2b2002-08-17 09:36:01 +00001022
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001023 while ((*((vu_long *) dest) & 0x00800080) !=
1024 (data & 0x00800080)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001025 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001026 return (1);
1027 }
wdenkaffae2b2002-08-17 09:36:01 +00001028 }
wdenkaffae2b2002-08-17 09:36:01 +00001029
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001030 } else {
wdenkaffae2b2002-08-17 09:36:01 +00001031
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001032 while (!(addr[0] & 0x00800080)) { /* wait for error or finish */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001033 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001034 return (1);
1035 }
wdenkaffae2b2002-08-17 09:36:01 +00001036
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001037 if (addr[0] & 0x003A003A) { /* check for error */
1038 barf = addr[0] & 0x003A0000;
1039 if (barf) {
1040 barf >>= 16;
1041 } else {
1042 barf = addr[0] & 0x0000003A;
1043 }
1044 printf ("\nFlash write error at address %lx\n", (unsigned long) dest);
1045 if (barf & 0x0002)
1046 printf ("Block locked, not erased.\n");
1047 if (barf & 0x0010)
1048 printf ("Programming error.\n");
1049 if (barf & 0x0008)
1050 printf ("Vpp Low error.\n");
1051 return (2);
1052 }
1053
1054
wdenkaffae2b2002-08-17 09:36:01 +00001055 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001056
1057 return (0);
1058
wdenkaffae2b2002-08-17 09:36:01 +00001059 }
1060
wdenkaffae2b2002-08-17 09:36:01 +00001061#else
1062
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001063static int write_short (flash_info_t * info, ulong dest, ushort data)
wdenkaffae2b2002-08-17 09:36:01 +00001064{
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001065 vu_short *addr = (vu_short *) (info->start[0]);
1066 ulong start, barf;
wdenkaffae2b2002-08-17 09:36:01 +00001067 int flag;
1068
1069 /* Check if Flash is (sufficiently) erased */
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001070 if ((*((vu_short *) dest) & data) != data) {
wdenkaffae2b2002-08-17 09:36:01 +00001071 return (2);
1072 }
1073
1074 /* Disable interrupts which might cause a timeout here */
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001075 flag = disable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +00001076
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001077 if (info->flash_id < FLASH_AMD_COMP) {
1078 /* AMD stuff */
1079 addr[0x0555] = 0x00AA;
1080 addr[0x02AA] = 0x0055;
1081 addr[0x0555] = 0x00A0;
1082 } else {
1083 /* intel stuff */
1084 *addr = 0x00D0;
1085 *addr = 0x0040;
1086 }
1087 *((vu_short *) dest) = data;
wdenkaffae2b2002-08-17 09:36:01 +00001088
1089 /* re-enable interrupts if necessary */
1090 if (flag)
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001091 enable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +00001092
1093 /* data polling for D7 */
1094 start = get_timer (0);
1095
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001096 if (info->flash_id < FLASH_AMD_COMP) {
1097 /* AMD stuff */
1098 while ((*((vu_short *) dest) & 0x0080) != (data & 0x0080)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001099 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001100 return (1);
1101 }
wdenkaffae2b2002-08-17 09:36:01 +00001102 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001103
1104 } else {
1105 /* intel stuff */
1106 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001107 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001108 return (1);
1109 }
1110
1111 if (addr[0] & 0x003A) { /* check for error */
1112 barf = addr[0] & 0x003A;
1113 printf ("\nFlash write error at address %lx\n",
1114 (unsigned long) dest);
1115 if (barf & 0x0002)
1116 printf ("Block locked, not erased.\n");
1117 if (barf & 0x0010)
1118 printf ("Programming error.\n");
1119 if (barf & 0x0008)
1120 printf ("Vpp Low error.\n");
1121 return (2);
1122 }
1123 *addr = 0x00B0;
1124 *addr = 0x0070;
1125 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001126 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001127 return (1);
1128 }
1129 *addr = 0x00FF;
wdenkaffae2b2002-08-17 09:36:01 +00001130 }
wdenkaffae2b2002-08-17 09:36:01 +00001131 return (0);
wdenkaffae2b2002-08-17 09:36:01 +00001132}
1133
wdenkaffae2b2002-08-17 09:36:01 +00001134#endif
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001135/*-----------------------------------------------------------------------*/