blob: a121104ca899aee9cd0ca78fb200ff95d169db36 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00006 */
7
8#include <common.h>
9#include <mpc8xx.h>
10
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020011flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkaffae2b2002-08-17 09:36:01 +000012
13#ifdef CONFIG_FLASH_16BIT
14#define FLASH_WORD_SIZE unsigned short
15#define FLASH_ID_MASK 0xFFFF
16#else
17#define FLASH_WORD_SIZE unsigned long
18#define FLASH_ID_MASK 0xFFFFFFFF
19#endif
20
21/*-----------------------------------------------------------------------
22 * Functions
23 */
24
Wolfgang Denk53677ef2008-05-20 16:00:29 +020025ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info);
26
wdenkaffae2b2002-08-17 09:36:01 +000027#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +020028static int write_word (flash_info_t * info, ulong dest, ulong data);
wdenkaffae2b2002-08-17 09:36:01 +000029#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +020030static int write_short (flash_info_t * info, ulong dest, ushort data);
wdenkaffae2b2002-08-17 09:36:01 +000031#endif
32/*int flash_write (uchar *, ulong, ulong); */
33/*flash_info_t *addr2info (ulong); */
34
Wolfgang Denk53677ef2008-05-20 16:00:29 +020035static void flash_get_offsets (ulong base, flash_info_t * info);
wdenkaffae2b2002-08-17 09:36:01 +000036
37/*-----------------------------------------------------------------------
38 */
39unsigned long flash_init (void)
40{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020041 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
wdenkaffae2b2002-08-17 09:36:01 +000042 volatile memctl8xx_t *memctl = &immap->im_memctl;
43 unsigned long size_b0, size_b1;
44 int i;
45
46 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020047 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +000048 flash_info[i].flash_id = FLASH_UNKNOWN;
49 }
50
51 /* Static FLASH Bank configuration here - FIXME XXX */
52
Wolfgang Denk53677ef2008-05-20 16:00:29 +020053 size_b0 =
54 flash_get_size ((volatile FLASH_WORD_SIZE *)
55 FLASH_BASE0_PRELIM, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000056 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +020057 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size_b0, size_b0 << 20);
wdenkaffae2b2002-08-17 09:36:01 +000058 }
59
Wolfgang Denk53677ef2008-05-20 16:00:29 +020060 size_b1 =
61 flash_get_size ((volatile FLASH_WORD_SIZE *)
62 FLASH_BASE1_PRELIM, &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +000063
64 if (size_b1 > size_b0) {
65 printf ("## ERROR: "
66 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
Wolfgang Denk53677ef2008-05-20 16:00:29 +020067 size_b1, size_b1 << 20, size_b0, size_b0 << 20);
68 flash_info[0].flash_id = FLASH_UNKNOWN;
69 flash_info[1].flash_id = FLASH_UNKNOWN;
70 flash_info[0].sector_count = -1;
71 flash_info[1].sector_count = -1;
72 flash_info[0].size = 0;
73 flash_info[1].size = 0;
wdenkaffae2b2002-08-17 09:36:01 +000074 return (0);
75 }
76
77 /* Remap FLASH according to real size */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020078 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
79 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 +000080
81 /* Re-do sizing to get full correct info */
82
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020083 size_b0 = flash_get_size ((volatile FLASH_WORD_SIZE *) CONFIG_SYS_FLASH_BASE,
Wolfgang Denk53677ef2008-05-20 16:00:29 +020084 &flash_info[0]);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020085 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000086
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020087#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +000088 /* monitor protection ON by default */
Wolfgang Denk53677ef2008-05-20 16:00:29 +020089 (void) flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020090 CONFIG_SYS_MONITOR_BASE,
91 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
Wolfgang Denk53677ef2008-05-20 16:00:29 +020092 &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000093#endif
94
95 if (size_b1) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +020096 memctl->memc_or1 =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020097 CONFIG_SYS_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
Wolfgang Denk53677ef2008-05-20 16:00:29 +020098 memctl->memc_br1 =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020099 (CONFIG_SYS_FLASH_BASE | 0x00000801) + (size_b0 & BR_BA_MSK);
100 /*((CONFIG_SYS_FLASH_BASE + size_b0) & BR_BA_MSK) |
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200101 BR_MS_GPCM | BR_V; */
wdenkaffae2b2002-08-17 09:36:01 +0000102
103 /* Re-do sizing to get full correct info */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200104 size_b1 =
105 flash_get_size ((volatile FLASH_WORD_SIZE
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200106 *) (CONFIG_SYS_FLASH_BASE + size_b0),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200107 &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +0000108
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200109 flash_get_offsets (CONFIG_SYS_FLASH_BASE + size_b0, &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +0000110
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200111#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +0000112 /* monitor protection ON by default */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200113 (void) flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200114 CONFIG_SYS_MONITOR_BASE,
115 CONFIG_SYS_MONITOR_BASE + monitor_flash_len -
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200116 1, &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +0000117#endif
118 } else {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200119 memctl->memc_br1 = 0; /* invalidate bank */
wdenkaffae2b2002-08-17 09:36:01 +0000120
121 flash_info[1].flash_id = FLASH_UNKNOWN;
122 flash_info[1].sector_count = -1;
123 }
124
125 flash_info[0].size = size_b0;
126 flash_info[1].size = size_b1;
127
128 return (size_b0 + size_b1);
129}
130
131/*-----------------------------------------------------------------------
132 */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200133static void flash_get_offsets (ulong base, flash_info_t * info)
wdenkaffae2b2002-08-17 09:36:01 +0000134{
135 int i;
136
137 /* set up sector start adress table */
138 if (info->flash_id & FLASH_BTYPE) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200139 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
wdenkaffae2b2002-08-17 09:36:01 +0000140
141#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200142 /* set sector offsets for bottom boot block type */
143 info->start[0] = base + 0x00000000;
144 info->start[1] = base + 0x00004000;
145 info->start[2] = base + 0x00008000;
146 info->start[3] = base + 0x0000C000;
147 info->start[4] = base + 0x00010000;
148 info->start[5] = base + 0x00014000;
149 info->start[6] = base + 0x00018000;
150 info->start[7] = base + 0x0001C000;
151 for (i = 8; i < info->sector_count; i++) {
152 info->start[i] =
153 base + (i * 0x00020000) - 0x000E0000;
154 }
155 } else {
156 /* set sector offsets for bottom boot block type */
157 info->start[0] = base + 0x00000000;
158 info->start[1] = base + 0x00008000;
159 info->start[2] = base + 0x0000C000;
160 info->start[3] = base + 0x00010000;
161 for (i = 4; i < info->sector_count; i++) {
162 info->start[i] =
163 base + (i * 0x00020000) - 0x00060000;
164 }
wdenk8bde7f72003-06-27 21:31:46 +0000165 }
wdenkaffae2b2002-08-17 09:36:01 +0000166#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200167 /* set sector offsets for bottom boot block type */
168 info->start[0] = base + 0x00000000;
169 info->start[1] = base + 0x00002000;
170 info->start[2] = base + 0x00004000;
171 info->start[3] = base + 0x00006000;
172 info->start[4] = base + 0x00008000;
173 info->start[5] = base + 0x0000A000;
174 info->start[6] = base + 0x0000C000;
175 info->start[7] = base + 0x0000E000;
176 for (i = 8; i < info->sector_count; i++) {
177 info->start[i] =
178 base + (i * 0x00010000) - 0x00070000;
179 }
180 } else {
181 /* set sector offsets for bottom boot block type */
182 info->start[0] = base + 0x00000000;
183 info->start[1] = base + 0x00004000;
184 info->start[2] = base + 0x00006000;
185 info->start[3] = base + 0x00008000;
186 for (i = 4; i < info->sector_count; i++) {
187 info->start[i] =
188 base + (i * 0x00010000) - 0x00030000;
189 }
wdenk8bde7f72003-06-27 21:31:46 +0000190 }
wdenkaffae2b2002-08-17 09:36:01 +0000191#endif
192 } else {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200193 /* set sector offsets for top boot block type */
wdenkaffae2b2002-08-17 09:36:01 +0000194 i = info->sector_count - 1;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200195 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
wdenkaffae2b2002-08-17 09:36:01 +0000196
197#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200198 info->start[i--] = base + info->size - 0x00004000;
199 info->start[i--] = base + info->size - 0x00008000;
200 info->start[i--] = base + info->size - 0x0000C000;
201 info->start[i--] = base + info->size - 0x00010000;
202 info->start[i--] = base + info->size - 0x00014000;
203 info->start[i--] = base + info->size - 0x00018000;
204 info->start[i--] = base + info->size - 0x0001C000;
205 for (; i >= 0; i--) {
206 info->start[i] = base + i * 0x00020000;
207 }
wdenkaffae2b2002-08-17 09:36:01 +0000208
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200209 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000210
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200211 info->start[i--] = base + info->size - 0x00008000;
212 info->start[i--] = base + info->size - 0x0000C000;
213 info->start[i--] = base + info->size - 0x00010000;
214 for (; i >= 0; i--) {
215 info->start[i] = base + i * 0x00020000;
216 }
wdenkaffae2b2002-08-17 09:36:01 +0000217 }
wdenkaffae2b2002-08-17 09:36:01 +0000218#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200219 info->start[i--] = base + info->size - 0x00002000;
220 info->start[i--] = base + info->size - 0x00004000;
221 info->start[i--] = base + info->size - 0x00006000;
222 info->start[i--] = base + info->size - 0x00008000;
223 info->start[i--] = base + info->size - 0x0000A000;
224 info->start[i--] = base + info->size - 0x0000C000;
225 info->start[i--] = base + info->size - 0x0000E000;
226 for (; i >= 0; i--) {
227 info->start[i] = base + i * 0x00010000;
228 }
wdenkaffae2b2002-08-17 09:36:01 +0000229
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200230 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000231
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200232 info->start[i--] = base + info->size - 0x00004000;
233 info->start[i--] = base + info->size - 0x00006000;
234 info->start[i--] = base + info->size - 0x00008000;
235 for (; i >= 0; i--) {
236 info->start[i] = base + i * 0x00010000;
237 }
wdenkaffae2b2002-08-17 09:36:01 +0000238 }
wdenkaffae2b2002-08-17 09:36:01 +0000239#endif
240 }
241
242
243}
244
245/*-----------------------------------------------------------------------
246 */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200247void flash_print_info (flash_info_t * info)
wdenkaffae2b2002-08-17 09:36:01 +0000248{
249 int i;
250 uchar *boottype;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200251 uchar botboot[] = ", bottom boot sect)\n";
252 uchar topboot[] = ", top boot sector)\n";
wdenkaffae2b2002-08-17 09:36:01 +0000253
254 if (info->flash_id == FLASH_UNKNOWN) {
255 printf ("missing or unknown FLASH type\n");
256 return;
257 }
258
259 switch (info->flash_id & FLASH_VENDMASK) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200260 case FLASH_MAN_AMD:
261 printf ("AMD ");
262 break;
263 case FLASH_MAN_FUJ:
264 printf ("FUJITSU ");
265 break;
266 case FLASH_MAN_SST:
267 printf ("SST ");
268 break;
269 case FLASH_MAN_STM:
270 printf ("STM ");
271 break;
272 case FLASH_MAN_INTEL:
273 printf ("INTEL ");
274 break;
275 default:
276 printf ("Unknown Vendor ");
277 break;
wdenkaffae2b2002-08-17 09:36:01 +0000278 }
279
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200280 if (info->flash_id & 0x0001) {
281 boottype = botboot;
wdenkaffae2b2002-08-17 09:36:01 +0000282 } else {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200283 boottype = topboot;
wdenkaffae2b2002-08-17 09:36:01 +0000284 }
285
286 switch (info->flash_id & FLASH_TYPEMASK) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200287 case FLASH_AM400B:
288 printf ("AM29LV400B (4 Mbit%s", boottype);
289 break;
290 case FLASH_AM400T:
291 printf ("AM29LV400T (4 Mbit%s", boottype);
292 break;
293 case FLASH_AM800B:
294 printf ("AM29LV800B (8 Mbit%s", boottype);
295 break;
296 case FLASH_AM800T:
297 printf ("AM29LV800T (8 Mbit%s", boottype);
298 break;
299 case FLASH_AM160B:
300 printf ("AM29LV160B (16 Mbit%s", boottype);
301 break;
302 case FLASH_AM160T:
303 printf ("AM29LV160T (16 Mbit%s", boottype);
304 break;
305 case FLASH_AM320B:
306 printf ("AM29LV320B (32 Mbit%s", boottype);
307 break;
308 case FLASH_AM320T:
309 printf ("AM29LV320T (32 Mbit%s", boottype);
310 break;
311 case FLASH_INTEL800B:
312 printf ("INTEL28F800B (8 Mbit%s", boottype);
313 break;
314 case FLASH_INTEL800T:
315 printf ("INTEL28F800T (8 Mbit%s", boottype);
316 break;
317 case FLASH_INTEL160B:
318 printf ("INTEL28F160B (16 Mbit%s", boottype);
319 break;
320 case FLASH_INTEL160T:
321 printf ("INTEL28F160T (16 Mbit%s", boottype);
322 break;
323 case FLASH_INTEL320B:
324 printf ("INTEL28F320B (32 Mbit%s", boottype);
325 break;
326 case FLASH_INTEL320T:
327 printf ("INTEL28F320T (32 Mbit%s", boottype);
328 break;
wdenkaffae2b2002-08-17 09:36:01 +0000329
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200330#if 0 /* enable when devices are available */
wdenkaffae2b2002-08-17 09:36:01 +0000331
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200332 case FLASH_INTEL640B:
333 printf ("INTEL28F640B (64 Mbit%s", boottype);
334 break;
335 case FLASH_INTEL640T:
336 printf ("INTEL28F640T (64 Mbit%s", boottype);
337 break;
wdenkaffae2b2002-08-17 09:36:01 +0000338#endif
339
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200340 default:
341 printf ("Unknown Chip Type\n");
342 break;
wdenkaffae2b2002-08-17 09:36:01 +0000343 }
344
345 printf (" Size: %ld MB in %d Sectors\n",
346 info->size >> 20, info->sector_count);
347
348 printf (" Sector Start Addresses:");
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200349 for (i = 0; i < info->sector_count; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000350 if ((i % 5) == 0)
351 printf ("\n ");
352 printf (" %08lX%s",
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200353 info->start[i], info->protect[i] ? " (RO)" : " ");
wdenkaffae2b2002-08-17 09:36:01 +0000354 }
355 printf ("\n");
356 return;
357}
358
359/*-----------------------------------------------------------------------
360 */
361
362
363/*-----------------------------------------------------------------------
364 */
365
366/*
367 * The following code cannot be run from FLASH!
368 */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200369ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info)
wdenkaffae2b2002-08-17 09:36:01 +0000370{
371 short i;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200372 ulong base = (ulong) addr;
wdenkaffae2b2002-08-17 09:36:01 +0000373 FLASH_WORD_SIZE value;
374
375 /* Write auto select command: read Manufacturer ID */
376
377
378#ifndef CONFIG_FLASH_16BIT
379
380 /*
381 * Note: if it is an AMD flash and the word at addr[0000]
wdenk8bde7f72003-06-27 21:31:46 +0000382 * is 0x00890089 this routine will think it is an Intel
383 * flash device and may(most likely) cause trouble.
wdenkaffae2b2002-08-17 09:36:01 +0000384 */
385
386 addr[0x0000] = 0x00900090;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200387 if (addr[0x0000] != 0x00890089) {
wdenkaffae2b2002-08-17 09:36:01 +0000388 addr[0x0555] = 0x00AA00AA;
389 addr[0x02AA] = 0x00550055;
390 addr[0x0555] = 0x00900090;
391#else
392
393 /*
394 * Note: if it is an AMD flash and the word at addr[0000]
wdenk8bde7f72003-06-27 21:31:46 +0000395 * is 0x0089 this routine will think it is an Intel
396 * flash device and may(most likely) cause trouble.
wdenkaffae2b2002-08-17 09:36:01 +0000397 */
398
399 addr[0x0000] = 0x0090;
400
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200401 if (addr[0x0000] != 0x0089) {
wdenkaffae2b2002-08-17 09:36:01 +0000402 addr[0x0555] = 0x00AA;
403 addr[0x02AA] = 0x0055;
404 addr[0x0555] = 0x0090;
405#endif
406 }
407 value = addr[0];
408
409 switch (value) {
410 case (AMD_MANUFACT & FLASH_ID_MASK):
411 info->flash_id = FLASH_MAN_AMD;
412 break;
413 case (FUJ_MANUFACT & FLASH_ID_MASK):
414 info->flash_id = FLASH_MAN_FUJ;
415 break;
416 case (STM_MANUFACT & FLASH_ID_MASK):
417 info->flash_id = FLASH_MAN_STM;
418 break;
419 case (SST_MANUFACT & FLASH_ID_MASK):
420 info->flash_id = FLASH_MAN_SST;
421 break;
422 case (INTEL_MANUFACT & FLASH_ID_MASK):
423 info->flash_id = FLASH_MAN_INTEL;
424 break;
425 default:
426 info->flash_id = FLASH_UNKNOWN;
427 info->sector_count = 0;
428 info->size = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200429 return (0); /* no or unknown flash */
wdenkaffae2b2002-08-17 09:36:01 +0000430
431 }
432
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200433 value = addr[1]; /* device ID */
wdenkaffae2b2002-08-17 09:36:01 +0000434
435 switch (value) {
436
437 case (AMD_ID_LV400T & FLASH_ID_MASK):
438 info->flash_id += FLASH_AM400T;
439 info->sector_count = 11;
440 info->size = 0x00100000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200441 break; /* => 1 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000442
443 case (AMD_ID_LV400B & FLASH_ID_MASK):
444 info->flash_id += FLASH_AM400B;
445 info->sector_count = 11;
446 info->size = 0x00100000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200447 break; /* => 1 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000448
449 case (AMD_ID_LV800T & FLASH_ID_MASK):
450 info->flash_id += FLASH_AM800T;
451 info->sector_count = 19;
452 info->size = 0x00200000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200453 break; /* => 2 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000454
455 case (AMD_ID_LV800B & FLASH_ID_MASK):
456 info->flash_id += FLASH_AM800B;
457 info->sector_count = 19;
458 info->size = 0x00200000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200459 break; /* => 2 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000460
461 case (AMD_ID_LV160T & FLASH_ID_MASK):
462 info->flash_id += FLASH_AM160T;
463 info->sector_count = 35;
464 info->size = 0x00400000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200465 break; /* => 4 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000466
467 case (AMD_ID_LV160B & FLASH_ID_MASK):
468 info->flash_id += FLASH_AM160B;
469 info->sector_count = 35;
470 info->size = 0x00400000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200471 break; /* => 4 MB */
472#if 0 /* enable when device IDs are available */
wdenkaffae2b2002-08-17 09:36:01 +0000473 case (AMD_ID_LV320T & FLASH_ID_MASK):
474 info->flash_id += FLASH_AM320T;
475 info->sector_count = 67;
476 info->size = 0x00800000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200477 break; /* => 8 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000478
479 case (AMD_ID_LV320B & FLASH_ID_MASK):
480 info->flash_id += FLASH_AM320B;
481 info->sector_count = 67;
482 info->size = 0x00800000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200483 break; /* => 8 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000484#endif
485
486 case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
487 info->flash_id += FLASH_INTEL800T;
488 info->sector_count = 23;
489 info->size = 0x00200000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200490 break; /* => 2 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000491
492 case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
493 info->flash_id += FLASH_INTEL800B;
494 info->sector_count = 23;
495 info->size = 0x00200000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200496 break; /* => 2 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000497
498 case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
499 info->flash_id += FLASH_INTEL160T;
500 info->sector_count = 39;
501 info->size = 0x00400000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200502 break; /* => 4 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000503
504 case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
505 info->flash_id += FLASH_INTEL160B;
506 info->sector_count = 39;
507 info->size = 0x00400000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200508 break; /* => 4 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000509
510 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
511 info->flash_id += FLASH_INTEL320T;
512 info->sector_count = 71;
513 info->size = 0x00800000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200514 break; /* => 8 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000515
516 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
517 info->flash_id += FLASH_AM320B;
518 info->sector_count = 71;
519 info->size = 0x00800000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200520 break; /* => 8 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000521
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200522#if 0 /* enable when devices are available */
wdenkaffae2b2002-08-17 09:36:01 +0000523 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
524 info->flash_id += FLASH_INTEL320T;
525 info->sector_count = 135;
526 info->size = 0x01000000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200527 break; /* => 16 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000528
529 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
530 info->flash_id += FLASH_AM320B;
531 info->sector_count = 135;
532 info->size = 0x01000000;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200533 break; /* => 16 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000534#endif
535 default:
536 info->flash_id = FLASH_UNKNOWN;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200537 return (0); /* => no or unknown flash */
wdenkaffae2b2002-08-17 09:36:01 +0000538
539 }
540
541 /* set up sector start adress table */
542 if (info->flash_id & FLASH_BTYPE) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200543 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
wdenkaffae2b2002-08-17 09:36:01 +0000544
545#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200546 /* set sector offsets for bottom boot block type */
547 info->start[0] = base + 0x00000000;
548 info->start[1] = base + 0x00004000;
549 info->start[2] = base + 0x00008000;
550 info->start[3] = base + 0x0000C000;
551 info->start[4] = base + 0x00010000;
552 info->start[5] = base + 0x00014000;
553 info->start[6] = base + 0x00018000;
554 info->start[7] = base + 0x0001C000;
555 for (i = 8; i < info->sector_count; i++) {
556 info->start[i] =
557 base + (i * 0x00020000) - 0x000E0000;
558 }
559 } else {
560 /* set sector offsets for bottom boot block type */
561 info->start[0] = base + 0x00000000;
562 info->start[1] = base + 0x00008000;
563 info->start[2] = base + 0x0000C000;
564 info->start[3] = base + 0x00010000;
565 for (i = 4; i < info->sector_count; i++) {
566 info->start[i] =
567 base + (i * 0x00020000) - 0x00060000;
568 }
wdenk8bde7f72003-06-27 21:31:46 +0000569 }
wdenkaffae2b2002-08-17 09:36:01 +0000570#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200571 /* set sector offsets for bottom boot block type */
572 info->start[0] = base + 0x00000000;
573 info->start[1] = base + 0x00002000;
574 info->start[2] = base + 0x00004000;
575 info->start[3] = base + 0x00006000;
576 info->start[4] = base + 0x00008000;
577 info->start[5] = base + 0x0000A000;
578 info->start[6] = base + 0x0000C000;
579 info->start[7] = base + 0x0000E000;
580 for (i = 8; i < info->sector_count; i++) {
581 info->start[i] =
582 base + (i * 0x00010000) - 0x00070000;
583 }
584 } else {
585 /* set sector offsets for bottom boot block type */
586 info->start[0] = base + 0x00000000;
587 info->start[1] = base + 0x00004000;
588 info->start[2] = base + 0x00006000;
589 info->start[3] = base + 0x00008000;
590 for (i = 4; i < info->sector_count; i++) {
591 info->start[i] =
592 base + (i * 0x00010000) - 0x00030000;
593 }
wdenk8bde7f72003-06-27 21:31:46 +0000594 }
wdenkaffae2b2002-08-17 09:36:01 +0000595#endif
596 } else {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200597 /* set sector offsets for top boot block type */
wdenkaffae2b2002-08-17 09:36:01 +0000598 i = info->sector_count - 1;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200599 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
wdenkaffae2b2002-08-17 09:36:01 +0000600
601#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200602 info->start[i--] = base + info->size - 0x00004000;
603 info->start[i--] = base + info->size - 0x00008000;
604 info->start[i--] = base + info->size - 0x0000C000;
605 info->start[i--] = base + info->size - 0x00010000;
606 info->start[i--] = base + info->size - 0x00014000;
607 info->start[i--] = base + info->size - 0x00018000;
608 info->start[i--] = base + info->size - 0x0001C000;
609 for (; i >= 0; i--) {
610 info->start[i] = base + i * 0x00020000;
611 }
wdenkaffae2b2002-08-17 09:36:01 +0000612
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200613 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000614
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200615 info->start[i--] = base + info->size - 0x00008000;
616 info->start[i--] = base + info->size - 0x0000C000;
617 info->start[i--] = base + info->size - 0x00010000;
618 for (; i >= 0; i--) {
619 info->start[i] = base + i * 0x00020000;
620 }
wdenkaffae2b2002-08-17 09:36:01 +0000621 }
wdenkaffae2b2002-08-17 09:36:01 +0000622#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200623 info->start[i--] = base + info->size - 0x00002000;
624 info->start[i--] = base + info->size - 0x00004000;
625 info->start[i--] = base + info->size - 0x00006000;
626 info->start[i--] = base + info->size - 0x00008000;
627 info->start[i--] = base + info->size - 0x0000A000;
628 info->start[i--] = base + info->size - 0x0000C000;
629 info->start[i--] = base + info->size - 0x0000E000;
630 for (; i >= 0; i--) {
631 info->start[i] = base + i * 0x00010000;
632 }
wdenkaffae2b2002-08-17 09:36:01 +0000633
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200634 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000635
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200636 info->start[i--] = base + info->size - 0x00004000;
637 info->start[i--] = base + info->size - 0x00006000;
638 info->start[i--] = base + info->size - 0x00008000;
639 for (; i >= 0; i--) {
640 info->start[i] = base + i * 0x00010000;
641 }
wdenkaffae2b2002-08-17 09:36:01 +0000642 }
wdenkaffae2b2002-08-17 09:36:01 +0000643#endif
644 }
645
646 /* check for protected sectors */
647 for (i = 0; i < info->sector_count; i++) {
648 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
649 /* D0 = 1 if protected */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200650 addr = (volatile FLASH_WORD_SIZE *) (info->start[i]);
wdenkaffae2b2002-08-17 09:36:01 +0000651 info->protect[i] = addr[2] & 1;
652 }
653
654 /*
655 * Prevent writes to uninitialized FLASH.
656 */
657 if (info->flash_id != FLASH_UNKNOWN) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200658 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
659 if ((info->flash_id & 0xFF00) == FLASH_MAN_INTEL) {
660 *addr = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
wdenkaffae2b2002-08-17 09:36:01 +0000661 } else {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200662 *addr = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
wdenkaffae2b2002-08-17 09:36:01 +0000663 }
664 }
665
666 return (info->size);
667}
668
669
670/*-----------------------------------------------------------------------
671 */
672
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200673int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenkaffae2b2002-08-17 09:36:01 +0000674{
675
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200676 volatile FLASH_WORD_SIZE *addr =
677 (volatile FLASH_WORD_SIZE *) (info->start[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000678 int flag, prot, sect, l_sect, barf;
679 ulong start, now, last;
680 int rcode = 0;
681
682 if ((s_first < 0) || (s_first > s_last)) {
683 if (info->flash_id == FLASH_UNKNOWN) {
684 printf ("- missing\n");
685 } else {
686 printf ("- no sectors to erase\n");
687 }
688 return 1;
689 }
690
691 if ((info->flash_id == FLASH_UNKNOWN) ||
692 ((info->flash_id > FLASH_AMD_COMP) &&
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200693 ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL))) {
wdenkaffae2b2002-08-17 09:36:01 +0000694 printf ("Can't erase unknown flash type - aborted\n");
695 return 1;
696 }
697
698 prot = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200699 for (sect = s_first; sect <= s_last; ++sect) {
wdenkaffae2b2002-08-17 09:36:01 +0000700 if (info->protect[sect]) {
701 prot++;
702 }
703 }
704
705 if (prot) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200706 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenkaffae2b2002-08-17 09:36:01 +0000707 } else {
708 printf ("\n");
709 }
710
711 l_sect = -1;
712
713 /* Disable interrupts which might cause a timeout here */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200714 flag = disable_interrupts ();
715 if (info->flash_id < FLASH_AMD_COMP) {
wdenkaffae2b2002-08-17 09:36:01 +0000716#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200717 addr[0x0555] = 0x00AA00AA;
718 addr[0x02AA] = 0x00550055;
719 addr[0x0555] = 0x00800080;
720 addr[0x0555] = 0x00AA00AA;
721 addr[0x02AA] = 0x00550055;
wdenkaffae2b2002-08-17 09:36:01 +0000722#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200723 addr[0x0555] = 0x00AA;
724 addr[0x02AA] = 0x0055;
725 addr[0x0555] = 0x0080;
726 addr[0x0555] = 0x00AA;
727 addr[0x02AA] = 0x0055;
wdenkaffae2b2002-08-17 09:36:01 +0000728#endif
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200729 /* Start erase on unprotected sectors */
730 for (sect = s_first; sect <= s_last; sect++) {
731 if (info->protect[sect] == 0) { /* not protected */
732 addr = (volatile FLASH_WORD_SIZE *) (info->start[sect]);
733 addr[0] = (0x00300030 & FLASH_ID_MASK);
734 l_sect = sect;
wdenkaffae2b2002-08-17 09:36:01 +0000735 }
wdenkaffae2b2002-08-17 09:36:01 +0000736 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200737
738 /* re-enable interrupts if necessary */
739 if (flag)
740 enable_interrupts ();
741
742 /* wait at least 80us - let's wait 1 ms */
743 udelay (1000);
744
745 /*
746 * We wait for the last triggered sector
747 */
748 if (l_sect < 0)
749 goto DONE;
750
751 start = get_timer (0);
752 last = start;
753 addr = (volatile FLASH_WORD_SIZE *) (info->start[l_sect]);
754 while ((addr[0] & (0x00800080 & FLASH_ID_MASK)) !=
755 (0x00800080 & FLASH_ID_MASK)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200756 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200757 printf ("Timeout\n");
758 return 1;
759 }
760 /* show that we're waiting */
761 if ((now - last) > 1000) { /* every second */
762 serial_putc ('.');
763 last = now;
764 }
765 }
766
767 DONE:
768 /* reset to read mode */
769 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
770 addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
771 } else {
772
773
774 for (sect = s_first; sect <= s_last; sect++) {
775 if (info->protect[sect] == 0) { /* not protected */
776 barf = 0;
777#ifndef CONFIG_FLASH_16BIT
778 addr = (vu_long *) (info->start[sect]);
779 addr[0] = 0x00200020;
780 addr[0] = 0x00D000D0;
781 while (!(addr[0] & 0x00800080)); /* wait for error or finish */
782 if (addr[0] & 0x003A003A) { /* check for error */
783 barf = addr[0] & 0x003A0000;
784 if (barf) {
785 barf >>= 16;
786 } else {
787 barf = addr[0] & 0x0000003A;
788 }
789 }
790#else
791 addr = (vu_short *) (info->start[sect]);
792 addr[0] = 0x0020;
793 addr[0] = 0x00D0;
794 while (!(addr[0] & 0x0080)); /* wait for error or finish */
795 if (addr[0] & 0x003A) /* check for error */
796 barf = addr[0] & 0x003A;
797#endif
798 if (barf) {
799 printf ("\nFlash error in sector at %lx\n", (unsigned long) addr);
800 if (barf & 0x0002)
801 printf ("Block locked, not erased.\n");
802 if ((barf & 0x0030) == 0x0030)
803 printf ("Command Sequence error.\n");
804 if ((barf & 0x0030) == 0x0020)
805 printf ("Block Erase error.\n");
806 if (barf & 0x0008)
807 printf ("Vpp Low error.\n");
808 rcode = 1;
809 } else
810 printf (".");
811 l_sect = sect;
812 }
813 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
814 addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
815
816 }
wdenkaffae2b2002-08-17 09:36:01 +0000817
818 }
wdenkaffae2b2002-08-17 09:36:01 +0000819 printf (" done\n");
820 return rcode;
821}
822
823/*-----------------------------------------------------------------------
824 */
825
826/*-----------------------------------------------------------------------
827 * Copy memory to flash, returns:
828 * 0 - OK
829 * 1 - write timeout
830 * 2 - Flash not erased
831 */
832
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200833int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenkaffae2b2002-08-17 09:36:01 +0000834{
835#ifndef CONFIG_FLASH_16BIT
836 ulong cp, wp, data;
837 int l;
838#else
839 ulong cp, wp;
840 ushort data;
841#endif
842 int i, rc;
843
844#ifndef CONFIG_FLASH_16BIT
845
846
847 wp = (addr & ~3); /* get lower word aligned address */
848
849 /*
850 * handle unaligned start bytes
851 */
852 if ((l = addr - wp) != 0) {
853 data = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200854 for (i = 0, cp = wp; i < l; ++i, ++cp) {
855 data = (data << 8) | (*(uchar *) cp);
wdenkaffae2b2002-08-17 09:36:01 +0000856 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200857 for (; i < 4 && cnt > 0; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000858 data = (data << 8) | *src++;
859 --cnt;
860 ++cp;
861 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200862 for (; cnt == 0 && i < 4; ++i, ++cp) {
863 data = (data << 8) | (*(uchar *) cp);
wdenkaffae2b2002-08-17 09:36:01 +0000864 }
865
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200866 if ((rc = write_word (info, wp, data)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000867 return (rc);
868 }
869 wp += 4;
870 }
871
872 /*
873 * handle word aligned part
874 */
875 while (cnt >= 4) {
876 data = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200877 for (i = 0; i < 4; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000878 data = (data << 8) | *src++;
879 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200880 if ((rc = write_word (info, wp, data)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000881 return (rc);
882 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200883 wp += 4;
wdenkaffae2b2002-08-17 09:36:01 +0000884 cnt -= 4;
885 }
886
887 if (cnt == 0) {
888 return (0);
889 }
890
891 /*
892 * handle unaligned tail bytes
893 */
894 data = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200895 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
wdenkaffae2b2002-08-17 09:36:01 +0000896 data = (data << 8) | *src++;
897 --cnt;
898 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200899 for (; i < 4; ++i, ++cp) {
900 data = (data << 8) | (*(uchar *) cp);
wdenkaffae2b2002-08-17 09:36:01 +0000901 }
902
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200903 return (write_word (info, wp, data));
wdenkaffae2b2002-08-17 09:36:01 +0000904
905#else
906 wp = (addr & ~1); /* get lower word aligned address */
907
908 /*
909 * handle unaligned start byte
910 */
911 if (addr - wp) {
912 data = 0;
913 data = (data << 8) | *src++;
914 --cnt;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200915 if ((rc = write_short (info, wp, data)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000916 return (rc);
917 }
918 wp += 2;
919 }
920
921 /*
922 * handle word aligned part
923 */
924/* l = 0; used for debuging */
925 while (cnt >= 2) {
926 data = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200927 for (i = 0; i < 2; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000928 data = (data << 8) | *src++;
929 }
930
931/* if(!l){
932 printf("%x",data);
933 l = 1;
934 } used for debuging */
935
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200936 if ((rc = write_short (info, wp, data)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000937 return (rc);
938 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200939 wp += 2;
wdenkaffae2b2002-08-17 09:36:01 +0000940 cnt -= 2;
941 }
942
943 if (cnt == 0) {
944 return (0);
945 }
946
947 /*
948 * handle unaligned tail bytes
949 */
950 data = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200951 for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
wdenkaffae2b2002-08-17 09:36:01 +0000952 data = (data << 8) | *src++;
953 --cnt;
954 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200955 for (; i < 2; ++i, ++cp) {
956 data = (data << 8) | (*(uchar *) cp);
wdenkaffae2b2002-08-17 09:36:01 +0000957 }
958
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200959 return (write_short (info, wp, data));
wdenkaffae2b2002-08-17 09:36:01 +0000960
961
962#endif
963}
964
965/*-----------------------------------------------------------------------
966 * Write a word to Flash, returns:
967 * 0 - OK
968 * 1 - write timeout
969 * 2 - Flash not erased
970 */
971#ifndef CONFIG_FLASH_16BIT
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200972static int write_word (flash_info_t * info, ulong dest, ulong data)
wdenkaffae2b2002-08-17 09:36:01 +0000973{
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200974 vu_long *addr = (vu_long *) (info->start[0]);
975 ulong start, barf;
wdenkaffae2b2002-08-17 09:36:01 +0000976 int flag;
977
978
979 /* Check if Flash is (sufficiently) erased */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200980 if ((*((vu_long *) dest) & data) != data) {
wdenkaffae2b2002-08-17 09:36:01 +0000981 return (2);
982 }
983
984 /* Disable interrupts which might cause a timeout here */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200985 flag = disable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +0000986
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200987 if (info->flash_id > FLASH_AMD_COMP) {
988 /* AMD stuff */
989 addr[0x0555] = 0x00AA00AA;
990 addr[0x02AA] = 0x00550055;
991 addr[0x0555] = 0x00A000A0;
992 } else {
993 /* intel stuff */
994 *addr = 0x00400040;
995 }
996 *((vu_long *) dest) = data;
wdenkaffae2b2002-08-17 09:36:01 +0000997
998 /* re-enable interrupts if necessary */
999 if (flag)
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001000 enable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +00001001
1002 /* data polling for D7 */
1003 start = get_timer (0);
1004
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001005 if (info->flash_id > FLASH_AMD_COMP) {
wdenkaffae2b2002-08-17 09:36:01 +00001006
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001007 while ((*((vu_long *) dest) & 0x00800080) !=
1008 (data & 0x00800080)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001009 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001010 return (1);
1011 }
wdenkaffae2b2002-08-17 09:36:01 +00001012 }
wdenkaffae2b2002-08-17 09:36:01 +00001013
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001014 } else {
wdenkaffae2b2002-08-17 09:36:01 +00001015
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001016 while (!(addr[0] & 0x00800080)) { /* wait for error or finish */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001017 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001018 return (1);
1019 }
wdenkaffae2b2002-08-17 09:36:01 +00001020
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001021 if (addr[0] & 0x003A003A) { /* check for error */
1022 barf = addr[0] & 0x003A0000;
1023 if (barf) {
1024 barf >>= 16;
1025 } else {
1026 barf = addr[0] & 0x0000003A;
1027 }
1028 printf ("\nFlash write error at address %lx\n", (unsigned long) dest);
1029 if (barf & 0x0002)
1030 printf ("Block locked, not erased.\n");
1031 if (barf & 0x0010)
1032 printf ("Programming error.\n");
1033 if (barf & 0x0008)
1034 printf ("Vpp Low error.\n");
1035 return (2);
1036 }
1037
1038
wdenkaffae2b2002-08-17 09:36:01 +00001039 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001040
1041 return (0);
1042
wdenkaffae2b2002-08-17 09:36:01 +00001043 }
1044
wdenkaffae2b2002-08-17 09:36:01 +00001045#else
1046
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001047static int write_short (flash_info_t * info, ulong dest, ushort data)
wdenkaffae2b2002-08-17 09:36:01 +00001048{
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001049 vu_short *addr = (vu_short *) (info->start[0]);
1050 ulong start, barf;
wdenkaffae2b2002-08-17 09:36:01 +00001051 int flag;
1052
1053 /* Check if Flash is (sufficiently) erased */
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001054 if ((*((vu_short *) dest) & data) != data) {
wdenkaffae2b2002-08-17 09:36:01 +00001055 return (2);
1056 }
1057
1058 /* Disable interrupts which might cause a timeout here */
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001059 flag = disable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +00001060
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001061 if (info->flash_id < FLASH_AMD_COMP) {
1062 /* AMD stuff */
1063 addr[0x0555] = 0x00AA;
1064 addr[0x02AA] = 0x0055;
1065 addr[0x0555] = 0x00A0;
1066 } else {
1067 /* intel stuff */
1068 *addr = 0x00D0;
1069 *addr = 0x0040;
1070 }
1071 *((vu_short *) dest) = data;
wdenkaffae2b2002-08-17 09:36:01 +00001072
1073 /* re-enable interrupts if necessary */
1074 if (flag)
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001075 enable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +00001076
1077 /* data polling for D7 */
1078 start = get_timer (0);
1079
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001080 if (info->flash_id < FLASH_AMD_COMP) {
1081 /* AMD stuff */
1082 while ((*((vu_short *) dest) & 0x0080) != (data & 0x0080)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001083 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001084 return (1);
1085 }
wdenkaffae2b2002-08-17 09:36:01 +00001086 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001087
1088 } else {
1089 /* intel stuff */
1090 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001091 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001092 return (1);
1093 }
1094
1095 if (addr[0] & 0x003A) { /* check for error */
1096 barf = addr[0] & 0x003A;
1097 printf ("\nFlash write error at address %lx\n",
1098 (unsigned long) dest);
1099 if (barf & 0x0002)
1100 printf ("Block locked, not erased.\n");
1101 if (barf & 0x0010)
1102 printf ("Programming error.\n");
1103 if (barf & 0x0008)
1104 printf ("Vpp Low error.\n");
1105 return (2);
1106 }
1107 *addr = 0x00B0;
1108 *addr = 0x0070;
1109 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001110 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001111 return (1);
1112 }
1113 *addr = 0x00FF;
wdenkaffae2b2002-08-17 09:36:01 +00001114 }
wdenkaffae2b2002-08-17 09:36:01 +00001115 return (0);
wdenkaffae2b2002-08-17 09:36:01 +00001116}
1117
wdenkaffae2b2002-08-17 09:36:01 +00001118#endif
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001119/*-----------------------------------------------------------------------*/