blob: 0958e73d60cfe9cc6562592b8edaf54924327d8b [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
Wolfgang Denk8220f7e2011-11-04 15:55:38 +000027flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
wdenkaffae2b2002-08-17 09:36:01 +000028
29/*-----------------------------------------------------------------------
30 * Functions
31 */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +000032static ulong flash_get_size(vu_long *addr, flash_info_t *info);
33static int write_word(flash_info_t *info, ulong dest, ulong data);
34static void flash_get_offsets(ulong base, flash_info_t *info);
wdenkaffae2b2002-08-17 09:36:01 +000035
36/*-----------------------------------------------------------------------
37 */
38
Wolfgang Denk8220f7e2011-11-04 15:55:38 +000039unsigned long flash_init(void)
wdenkaffae2b2002-08-17 09:36:01 +000040{
Jean-Christophe PLAGNIOL-VILLARD03836942008-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;
Wolfgang Denk8a210ff2011-11-04 15:55:39 +000043 unsigned long size_b0;
wdenkaffae2b2002-08-17 09:36:01 +000044 int i;
45
46 /* Init: no FLASHes known */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +000047 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
wdenkaffae2b2002-08-17 09:36:01 +000048 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenkaffae2b2002-08-17 09:36:01 +000049
50 /* Static FLASH Bank configuration here - FIXME XXX */
51
52 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
53
54 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +000055 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
wdenkaffae2b2002-08-17 09:36:01 +000056 size_b0, size_b0<<20);
57 }
58
wdenkaffae2b2002-08-17 09:36:01 +000059 /* Remap FLASH according to real size */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020060 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
wdenkaffae2b2002-08-17 09:36:01 +000061#ifdef CONFIG_FLASH_16BIT
Wolfgang Denk8220f7e2011-11-04 15:55:38 +000062 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
63 BR_MS_GPCM | BR_V | BR_PS_16; /* 16 Bit data port */
wdenkaffae2b2002-08-17 09:36:01 +000064#else
Wolfgang Denk8220f7e2011-11-04 15:55:38 +000065 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
66 BR_MS_GPCM | BR_V;
wdenkaffae2b2002-08-17 09:36:01 +000067#endif
68
69 /* Re-do sizing to get full correct info */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +000070 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE,
71 &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000072
Wolfgang Denk8220f7e2011-11-04 15:55:38 +000073 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000074
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020075#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +000076 /* monitor protection ON by default */
77 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020078 CONFIG_SYS_MONITOR_BASE,
79 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +000080 &flash_info[0]);
81#endif
82
Wolfgang Denk8a210ff2011-11-04 15:55:39 +000083 memctl->memc_br1 = 0; /* invalidate bank 1 */
wdenkaffae2b2002-08-17 09:36:01 +000084
85 flash_info[0].size = size_b0;
wdenkaffae2b2002-08-17 09:36:01 +000086
Wolfgang Denk8a210ff2011-11-04 15:55:39 +000087 return size_b0;
wdenkaffae2b2002-08-17 09:36:01 +000088}
89
90/*-----------------------------------------------------------------------
91 */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +000092static void flash_get_offsets(ulong base, flash_info_t *info)
wdenkaffae2b2002-08-17 09:36:01 +000093{
94 int i;
95
Wolfgang Denk8220f7e2011-11-04 15:55:38 +000096 if (info->flash_id == FLASH_UNKNOWN)
wdenkaffae2b2002-08-17 09:36:01 +000097 return;
wdenkaffae2b2002-08-17 09:36:01 +000098
99 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000100 for (i = 0; i < info->sector_count; i++)
wdenkaffae2b2002-08-17 09:36:01 +0000101 info->start[i] = base + (i * 0x00002000);
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000102
wdenkaffae2b2002-08-17 09:36:01 +0000103 return;
104 }
105
106 /* set up sector start address table */
107 if (info->flash_id & FLASH_BTYPE) {
108 /* set sector offsets for bottom boot block type */
109#ifdef CONFIG_FLASH_16BIT
110 info->start[0] = base + 0x00000000;
111 info->start[1] = base + 0x00004000;
112 info->start[2] = base + 0x00006000;
113 info->start[3] = base + 0x00008000;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000114 for (i = 4; i < info->sector_count; i++)
wdenkaffae2b2002-08-17 09:36:01 +0000115 info->start[i] = base + (i * 0x00010000) - 0x00030000;
116#else
117 info->start[0] = base + 0x00000000;
118 info->start[1] = base + 0x00008000;
119 info->start[2] = base + 0x0000C000;
120 info->start[3] = base + 0x00010000;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000121 for (i = 4; i < info->sector_count; i++)
wdenkaffae2b2002-08-17 09:36:01 +0000122 info->start[i] = base + (i * 0x00020000) - 0x00060000;
123#endif
wdenkaffae2b2002-08-17 09:36:01 +0000124 } else {
125 /* set sector offsets for top boot block type */
126 i = info->sector_count - 1;
127 info->start[i--] = base + info->size - 0x00008000;
128 info->start[i--] = base + info->size - 0x0000C000;
129 info->start[i--] = base + info->size - 0x00010000;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000130 for (; i >= 0; i--)
wdenkaffae2b2002-08-17 09:36:01 +0000131 info->start[i] = base + i * 0x00020000;
wdenkaffae2b2002-08-17 09:36:01 +0000132 }
wdenkaffae2b2002-08-17 09:36:01 +0000133}
134
135/*-----------------------------------------------------------------------
136 */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000137void flash_print_info(flash_info_t *info)
wdenkaffae2b2002-08-17 09:36:01 +0000138{
139 int i;
140
141 if (info->flash_id == FLASH_UNKNOWN) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000142 printf("missing or unknown FLASH type\n");
wdenkaffae2b2002-08-17 09:36:01 +0000143 return;
144 }
145
146 switch (info->flash_id & FLASH_VENDMASK) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000147 case FLASH_MAN_AMD:
148 printf("AMD ");
149 break;
150 case FLASH_MAN_FUJ:
151 printf("FUJITSU ");
152 break;
153 case FLASH_MAN_SST:
154 printf("SST ");
155 break;
156 case FLASH_MAN_STM:
157 printf("STM ");
158 break;
159 default:
160 printf("Unknown Vendor ");
161 break;
wdenkaffae2b2002-08-17 09:36:01 +0000162 }
163
164 switch (info->flash_id & FLASH_TYPEMASK) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000165 case FLASH_AM400B:
166 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
167 break;
168 case FLASH_AM400T:
169 printf("AM29LV400T (4 Mbit, top boot sector)\n");
170 break;
171 case FLASH_AM800B:
172 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
173 break;
174 case FLASH_AM800T:
175 printf("AM29LV800T (8 Mbit, top boot sector)\n");
176 break;
177 case FLASH_AM160B:
178 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
179 break;
180 case FLASH_AM160T:
181 printf("AM29LV160T (16 Mbit, top boot sector)\n");
182 break;
183 case FLASH_AM320B:
184 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
185 break;
186 case FLASH_AM320T:
187 printf("AM29LV320T (32 Mbit, top boot sector)\n");
188 break;
189 case FLASH_SST200A:
190 printf("39xF200A (2M = 128K x 16)\n");
191 break;
192 case FLASH_SST400A:
193 printf("39xF400A (4M = 256K x 16)\n");
194 break;
195 case FLASH_SST800A:
196 printf("39xF800A (8M = 512K x 16)\n");
197 break;
198 case FLASH_STM800AB:
199 printf("M29W800AB (8M = 512K x 16)\n");
200 break;
201 default:
202 printf("Unknown Chip Type\n");
203 break;
wdenkaffae2b2002-08-17 09:36:01 +0000204 }
205
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000206 printf(" Size: %ld MB in %d Sectors\n",
wdenkaffae2b2002-08-17 09:36:01 +0000207 info->size >> 20, info->sector_count);
208
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000209 printf(" Sector Start Addresses:");
210 for (i = 0; i < info->sector_count; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000211 if ((i % 5) == 0)
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000212 printf("\n ");
213 printf(" %08lX%s",
wdenkaffae2b2002-08-17 09:36:01 +0000214 info->start[i],
215 info->protect[i] ? " (RO)" : " "
216 );
217 }
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000218 printf("\n");
wdenkaffae2b2002-08-17 09:36:01 +0000219 return;
220}
221
wdenkaffae2b2002-08-17 09:36:01 +0000222/*
223 * The following code cannot be run from FLASH!
224 */
225
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000226static ulong flash_get_size(vu_long *addr, flash_info_t *info)
wdenkaffae2b2002-08-17 09:36:01 +0000227{
228 short i;
229 ulong value;
230 ulong base = (ulong)addr;
231
232 /* Write auto select command: read Manufacturer ID */
233#ifdef CONFIG_FLASH_16BIT
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000234 vu_short *s_addr = (vu_short *)addr;
wdenkaffae2b2002-08-17 09:36:01 +0000235 s_addr[0x5555] = 0x00AA;
236 s_addr[0x2AAA] = 0x0055;
237 s_addr[0x5555] = 0x0090;
238 value = s_addr[0];
239 value = value|(value<<16);
240#else
241 addr[0x5555] = 0x00AA00AA;
242 addr[0x2AAA] = 0x00550055;
243 addr[0x5555] = 0x00900090;
244 value = addr[0];
245#endif
246
247 switch (value) {
248 case AMD_MANUFACT:
249 info->flash_id = FLASH_MAN_AMD;
250 break;
251 case FUJ_MANUFACT:
252 info->flash_id = FLASH_MAN_FUJ;
253 break;
254 case SST_MANUFACT:
255 info->flash_id = FLASH_MAN_SST;
256 break;
257 case STM_MANUFACT:
258 info->flash_id = FLASH_MAN_STM;
259 break;
260 default:
261 info->flash_id = FLASH_UNKNOWN;
262 info->sector_count = 0;
263 info->size = 0;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000264 return 0; /* no or unknown flash */
wdenkaffae2b2002-08-17 09:36:01 +0000265 }
266#ifdef CONFIG_FLASH_16BIT
267 value = s_addr[1];
268 value = value|(value<<16);
269#else
270 value = addr[1]; /* device ID */
271#endif
272
273 switch (value) {
274 case AMD_ID_LV400T:
275 info->flash_id += FLASH_AM400T;
276 info->sector_count = 11;
277 info->size = 0x00100000;
278 break; /* => 1 MB */
279
280 case AMD_ID_LV400B:
281 info->flash_id += FLASH_AM400B;
282 info->sector_count = 11;
283 info->size = 0x00100000;
284 break; /* => 1 MB */
285
286 case AMD_ID_LV800T:
287 info->flash_id += FLASH_AM800T;
288 info->sector_count = 19;
289 info->size = 0x00200000;
290 break; /* => 2 MB */
291
292 case AMD_ID_LV800B:
293 info->flash_id += FLASH_AM800B;
294#ifdef CONFIG_FLASH_16BIT
295 info->sector_count = 19;
296 info->size = 0x00100000; /* => 1 MB */
297#else
298 info->sector_count = 19;
299 info->size = 0x00200000; /* => 2 MB */
300#endif
301 break;
302
303 case AMD_ID_LV160T:
304 info->flash_id += FLASH_AM160T;
305 info->sector_count = 35;
306 info->size = 0x00400000;
307 break; /* => 4 MB */
308
309 case AMD_ID_LV160B:
310 info->flash_id += FLASH_AM160B;
311#ifdef CONFIG_FLASH_16BIT
312 info->sector_count = 35;
313 info->size = 0x00200000; /* => 2 MB */
314#else
315 info->sector_count = 35;
316 info->size = 0x00400000; /* => 4 MB */
317#endif
318
319 break;
wdenkaffae2b2002-08-17 09:36:01 +0000320 case SST_ID_xF200A:
321 info->flash_id += FLASH_SST200A;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000322 info->sector_count = 64; /* 39xF200A (2M = 128K x 16) */
wdenkaffae2b2002-08-17 09:36:01 +0000323 info->size = 0x00080000;
324 break;
325 case SST_ID_xF400A:
326 info->flash_id += FLASH_SST400A;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000327 info->sector_count = 128; /* 39xF400A (4M = 256K x 16) */
wdenkaffae2b2002-08-17 09:36:01 +0000328 info->size = 0x00100000;
329 break;
330 case SST_ID_xF800A:
331 info->flash_id += FLASH_SST800A;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000332 info->sector_count = 256; /* 39xF800A (8M = 512K x 16) */
wdenkaffae2b2002-08-17 09:36:01 +0000333 info->size = 0x00200000;
334 break; /* => 2 MB */
335 case STM_ID_x800AB:
336 info->flash_id += FLASH_STM800AB;
337 info->sector_count = 19;
338 info->size = 0x00200000;
339 break; /* => 2 MB */
340 default:
341 info->flash_id = FLASH_UNKNOWN;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000342 return 0; /* => no or unknown flash */
wdenkaffae2b2002-08-17 09:36:01 +0000343
344 }
345
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200346 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000347 printf("** ERROR: sector count %d > max (%d) **\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200348 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
349 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenkaffae2b2002-08-17 09:36:01 +0000350 }
351
352 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000353 for (i = 0; i < info->sector_count; i++)
wdenkaffae2b2002-08-17 09:36:01 +0000354 info->start[i] = base + (i * 0x00002000);
wdenkaffae2b2002-08-17 09:36:01 +0000355 } else { /* AMD and Fujitsu types */
356 /* set up sector start address table */
357 if (info->flash_id & FLASH_BTYPE) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000358 /* set sector offsets for bottom boot block type */
wdenkaffae2b2002-08-17 09:36:01 +0000359#ifdef CONFIG_FLASH_16BIT
360
361 info->start[0] = base + 0x00000000;
362 info->start[1] = base + 0x00004000;
363 info->start[2] = base + 0x00006000;
364 info->start[3] = base + 0x00008000;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000365 for (i = 4; i < info->sector_count; i++)
366 info->start[i] = base +
367 (i * 0x00010000) - 0x00030000;
wdenkaffae2b2002-08-17 09:36:01 +0000368#else
369 info->start[0] = base + 0x00000000;
370 info->start[1] = base + 0x00008000;
371 info->start[2] = base + 0x0000C000;
372 info->start[3] = base + 0x00010000;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000373 for (i = 4; i < info->sector_count; i++)
374 info->start[i] = base +
375 (i * 0x00020000) - 0x00060000;
wdenkaffae2b2002-08-17 09:36:01 +0000376#endif
wdenkaffae2b2002-08-17 09:36:01 +0000377 } else {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000378 /* set sector offsets for top boot block type */
wdenkaffae2b2002-08-17 09:36:01 +0000379 i = info->sector_count - 1;
380 info->start[i--] = base + info->size - 0x00008000;
381 info->start[i--] = base + info->size - 0x0000C000;
382 info->start[i--] = base + info->size - 0x00010000;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000383 for (; i >= 0; i--)
wdenkaffae2b2002-08-17 09:36:01 +0000384 info->start[i] = base + i * 0x00020000;
wdenkaffae2b2002-08-17 09:36:01 +0000385 }
386
387 /* check for protected sectors */
388 for (i = 0; i < info->sector_count; i++) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000389 /*
390 * read sector protection at sector address:
wdenkaffae2b2002-08-17 09:36:01 +0000391 * (A7 .. A0) = 0x02
392 * D0 = 1 if protected
393 */
394#ifdef CONFIG_FLASH_16BIT
395 s_addr = (volatile unsigned short *)(info->start[i]);
396 info->protect[i] = s_addr[2] & 1;
397#else
398 addr = (volatile unsigned long *)(info->start[i]);
399 info->protect[i] = addr[2] & 1;
400#endif
401 }
402 }
403
404 /*
405 * Prevent writes to uninitialized FLASH.
406 */
407 if (info->flash_id != FLASH_UNKNOWN) {
408#ifdef CONFIG_FLASH_16BIT
409 s_addr = (volatile unsigned short *)(info->start[0]);
410 *s_addr = 0x00F0; /* reset bank */
411#else
412 addr = (volatile unsigned long *)info->start[0];
413 *addr = 0x00F000F0; /* reset bank */
414#endif
415
416 }
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000417 return info->size;
wdenkaffae2b2002-08-17 09:36:01 +0000418}
419
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000420int flash_erase(flash_info_t *info, int s_first, int s_last)
wdenkaffae2b2002-08-17 09:36:01 +0000421{
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000422 vu_long *addr = (vu_long *)(info->start[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000423 int flag, prot, sect;
424 ulong start, now, last;
425#ifdef CONFIG_FLASH_16BIT
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000426 vu_short *s_addr = (vu_short *)addr;
wdenkaffae2b2002-08-17 09:36:01 +0000427#endif
428
429 if ((s_first < 0) || (s_first > s_last)) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000430 if (info->flash_id == FLASH_UNKNOWN)
431 printf("- missing\n");
432 else
433 printf("- no sectors to erase\n");
wdenkaffae2b2002-08-17 09:36:01 +0000434 return 1;
435 }
436/*#ifndef CONFIG_FLASH_16BIT
437 ulong type;
438 type = (info->flash_id & FLASH_VENDMASK);
439 if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
440 printf ("Can't erase unknown flash type %08lx - aborted\n",
441 info->flash_id);
442 return;
443 }
444#endif*/
445 prot = 0;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000446 for (sect = s_first; sect <= s_last; ++sect) {
447 if (info->protect[sect])
wdenkaffae2b2002-08-17 09:36:01 +0000448 prot++;
wdenkaffae2b2002-08-17 09:36:01 +0000449 }
450
451 if (prot) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000452 printf("- Warning: %d protected sectors will not be erased!\n",
wdenkaffae2b2002-08-17 09:36:01 +0000453 prot);
454 } else {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000455 printf("\n");
wdenkaffae2b2002-08-17 09:36:01 +0000456 }
457
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000458 start = get_timer(0);
wdenkaffae2b2002-08-17 09:36:01 +0000459 last = start;
460 /* Start erase on unprotected sectors */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000461 for (sect = s_first; sect <= s_last; sect++) {
wdenkaffae2b2002-08-17 09:36:01 +0000462 if (info->protect[sect] == 0) { /* not protected */
463#ifdef CONFIG_FLASH_16BIT
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000464 vu_short *s_sect_addr = (vu_short *)(info->start[sect]);
wdenkaffae2b2002-08-17 09:36:01 +0000465#else
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000466 vu_long *sect_addr = (vu_long *)(info->start[sect]);
wdenkaffae2b2002-08-17 09:36:01 +0000467#endif
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000468 /* Disable interrupts which might cause a timeout */
wdenkaffae2b2002-08-17 09:36:01 +0000469 flag = disable_interrupts();
470
471#ifdef CONFIG_FLASH_16BIT
472
473 /*printf("\ns_sect_addr=%x",s_sect_addr);*/
474 s_addr[0x5555] = 0x00AA;
475 s_addr[0x2AAA] = 0x0055;
476 s_addr[0x5555] = 0x0080;
477 s_addr[0x5555] = 0x00AA;
478 s_addr[0x2AAA] = 0x0055;
479 s_sect_addr[0] = 0x0030;
480#else
481 addr[0x5555] = 0x00AA00AA;
482 addr[0x2AAA] = 0x00550055;
483 addr[0x5555] = 0x00800080;
484 addr[0x5555] = 0x00AA00AA;
485 addr[0x2AAA] = 0x00550055;
486 sect_addr[0] = 0x00300030;
487#endif
488 /* re-enable interrupts if necessary */
489 if (flag)
490 enable_interrupts();
491
492 /* wait at least 80us - let's wait 1 ms */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000493 udelay(1000);
wdenkaffae2b2002-08-17 09:36:01 +0000494
495#ifdef CONFIG_FLASH_16BIT
496 while ((s_sect_addr[0] & 0x0080) != 0x0080) {
497#else
498 while ((sect_addr[0] & 0x00800080) != 0x00800080) {
499#endif
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000500 now = get_timer(start);
501 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
502 printf("Timeout\n");
wdenkaffae2b2002-08-17 09:36:01 +0000503 return 1;
504 }
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000505 /* show every second that we're waiting */
506 if ((now - last) > 1000) {
507 putc('.');
wdenkaffae2b2002-08-17 09:36:01 +0000508 last = now;
509 }
510 }
511 }
512 }
513
514 /* reset to read mode */
515 addr = (volatile unsigned long *)info->start[0];
516#ifdef CONFIG_FLASH_16BIT
517 s_addr[0] = 0x00F0; /* reset bank */
518#else
519 addr[0] = 0x00F000F0; /* reset bank */
520#endif
521
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000522 printf(" done\n");
wdenkaffae2b2002-08-17 09:36:01 +0000523 return 0;
524}
525
526/*-----------------------------------------------------------------------
527 * Copy memory to flash, returns:
528 * 0 - OK
529 * 1 - write timeout
530 * 2 - Flash not erased
531 * 4 - Flash not identified
532 */
533
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000534int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
wdenkaffae2b2002-08-17 09:36:01 +0000535{
536 ulong cp, wp, data;
537 int i, l, rc;
538
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000539 if (info->flash_id == FLASH_UNKNOWN)
wdenkaffae2b2002-08-17 09:36:01 +0000540 return 4;
wdenkaffae2b2002-08-17 09:36:01 +0000541
542 wp = (addr & ~3); /* get lower word aligned address */
543
544 /*
545 * handle unaligned start bytes
546 */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000547 l = addr - wp;
548
549 if (l != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000550 data = 0;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000551 for (i = 0, cp = wp; i < l; ++i, ++cp)
wdenkaffae2b2002-08-17 09:36:01 +0000552 data = (data << 8) | (*(uchar *)cp);
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000553
554 for (; i < 4 && cnt > 0; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000555 data = (data << 8) | *src++;
556 --cnt;
557 ++cp;
558 }
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000559 for (; cnt == 0 && i < 4; ++i, ++cp)
wdenkaffae2b2002-08-17 09:36:01 +0000560 data = (data << 8) | (*(uchar *)cp);
wdenkaffae2b2002-08-17 09:36:01 +0000561
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000562 rc = write_word(info, wp, data);
563
564 if (rc != 0)
565 return rc;
566
wdenkaffae2b2002-08-17 09:36:01 +0000567 wp += 4;
568 }
569
570 /*
571 * handle word aligned part
572 */
573 while (cnt >= 4) {
574 data = 0;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000575 for (i = 0; i < 4; ++i)
wdenkaffae2b2002-08-17 09:36:01 +0000576 data = (data << 8) | *src++;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000577
578 rc = write_word(info, wp, data);
579 if (rc != 0)
580 return rc;
581
wdenkaffae2b2002-08-17 09:36:01 +0000582 wp += 4;
583 cnt -= 4;
584 }
585
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000586 if (cnt == 0)
587 return 0;
wdenkaffae2b2002-08-17 09:36:01 +0000588
589 /*
590 * handle unaligned tail bytes
591 */
592 data = 0;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000593 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
wdenkaffae2b2002-08-17 09:36:01 +0000594 data = (data << 8) | *src++;
595 --cnt;
596 }
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000597 for (; i < 4; ++i, ++cp)
wdenkaffae2b2002-08-17 09:36:01 +0000598 data = (data << 8) | (*(uchar *)cp);
wdenkaffae2b2002-08-17 09:36:01 +0000599
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000600 return write_word(info, wp, data);
wdenkaffae2b2002-08-17 09:36:01 +0000601}
602
603/*-----------------------------------------------------------------------
604 * Write a word to Flash, returns:
605 * 0 - OK
606 * 1 - write timeout
607 * 2 - Flash not erased
608 */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000609static int write_word(flash_info_t *info, ulong dest, ulong data)
wdenkaffae2b2002-08-17 09:36:01 +0000610{
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000611 vu_long *addr = (vu_long *)(info->start[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000612
613#ifdef CONFIG_FLASH_16BIT
614 vu_short high_data;
615 vu_short low_data;
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000616 vu_short *s_addr = (vu_short *)addr;
wdenkaffae2b2002-08-17 09:36:01 +0000617#endif
618 ulong start;
619 int flag;
620
621 /* Check if Flash is (sufficiently) erased */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000622 if ((*((vu_long *)dest) & data) != data)
623 return 2;
wdenkaffae2b2002-08-17 09:36:01 +0000624
625#ifdef CONFIG_FLASH_16BIT
626 /* Write the 16 higher-bits */
627 /* Disable interrupts which might cause a timeout here */
628 flag = disable_interrupts();
629
630 high_data = ((data>>16) & 0x0000ffff);
631
632 s_addr[0x5555] = 0x00AA;
633 s_addr[0x2AAA] = 0x0055;
634 s_addr[0x5555] = 0x00A0;
635
636 *((vu_short *)dest) = high_data;
637
wdenkaffae2b2002-08-17 09:36:01 +0000638 /* re-enable interrupts if necessary */
639 if (flag)
640 enable_interrupts();
641
642 /* data polling for D7 */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000643 start = get_timer(0);
wdenkaffae2b2002-08-17 09:36:01 +0000644 while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000645 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
646 return 1;
wdenkaffae2b2002-08-17 09:36:01 +0000647 }
648
wdenkaffae2b2002-08-17 09:36:01 +0000649 /* Write the 16 lower-bits */
650#endif
651
652 /* Disable interrupts which might cause a timeout here */
653 flag = disable_interrupts();
654#ifdef CONFIG_FLASH_16BIT
655 dest += 0x2;
656 low_data = (data & 0x0000ffff);
657
658 s_addr[0x5555] = 0x00AA;
659 s_addr[0x2AAA] = 0x0055;
660 s_addr[0x5555] = 0x00A0;
661 *((vu_short *)dest) = low_data;
662
663#else
664 addr[0x5555] = 0x00AA00AA;
665 addr[0x2AAA] = 0x00550055;
666 addr[0x5555] = 0x00A000A0;
667 *((vu_long *)dest) = data;
668#endif
669
670 /* re-enable interrupts if necessary */
671 if (flag)
672 enable_interrupts();
673
674 /* data polling for D7 */
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000675 start = get_timer(0);
wdenkaffae2b2002-08-17 09:36:01 +0000676
677#ifdef CONFIG_FLASH_16BIT
678 while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
679#else
680 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
681#endif
682
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000683 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
684 return 1;
wdenkaffae2b2002-08-17 09:36:01 +0000685 }
Wolfgang Denk8220f7e2011-11-04 15:55:38 +0000686 return 0;
wdenkaffae2b2002-08-17 09:36:01 +0000687}