blob: c1bd13c29cc798c29d71233564955898a51779d9 [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 Denkd0bb8d42011-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 Denkd0bb8d42011-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 Denkd0bb8d42011-11-04 15:55:38 +000039unsigned long flash_init(void)
wdenkaffae2b2002-08-17 09:36:01 +000040{
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 */
Wolfgang Denkd0bb8d42011-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 Denkd0bb8d42011-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
59 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
60
61 if (size_b1 > size_b0) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +000062 printf("## ERROR: "
wdenkaffae2b2002-08-17 09:36:01 +000063 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
64 size_b1, size_b1<<20,
65 size_b0, size_b0<<20
66 );
67 flash_info[0].flash_id = FLASH_UNKNOWN;
68 flash_info[1].flash_id = FLASH_UNKNOWN;
69 flash_info[0].sector_count = -1;
70 flash_info[1].sector_count = -1;
71 flash_info[0].size = 0;
72 flash_info[1].size = 0;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +000073 return 0;
wdenkaffae2b2002-08-17 09:36:01 +000074 }
75
76 /* Remap FLASH according to real size */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020077 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
wdenkaffae2b2002-08-17 09:36:01 +000078#ifdef CONFIG_FLASH_16BIT
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +000079 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
80 BR_MS_GPCM | BR_V | BR_PS_16; /* 16 Bit data port */
wdenkaffae2b2002-08-17 09:36:01 +000081#else
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +000082 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
83 BR_MS_GPCM | BR_V;
wdenkaffae2b2002-08-17 09:36:01 +000084#endif
85
86 /* Re-do sizing to get full correct info */
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +000087 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE,
88 &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000089
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +000090 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000091
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020092#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +000093 /* monitor protection ON by default */
94 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020095 CONFIG_SYS_MONITOR_BASE,
96 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +000097 &flash_info[0]);
98#endif
99
100 if (size_b1) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000101 memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH |
102 (-size_b1 & 0xFFFF8000);
wdenkaffae2b2002-08-17 09:36:01 +0000103#ifdef CONFIG_FLASH_16BIT
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000104 memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) &
105 BR_BA_MSK) | BR_MS_GPCM | BR_V | BR_PS_16;
wdenkaffae2b2002-08-17 09:36:01 +0000106#else
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000107 memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) &
108 BR_BA_MSK) | BR_MS_GPCM | BR_V;
wdenkaffae2b2002-08-17 09:36:01 +0000109#endif
110
111 /* Re-do sizing to get full correct info */
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000112 size_b1 = flash_get_size((vu_long *)(CONFIG_SYS_FLASH_BASE +
113 size_b0), &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +0000114
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000115 flash_get_offsets(CONFIG_SYS_FLASH_BASE + size_b0,
116 &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +0000117
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200118#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +0000119 /* monitor protection ON by default */
120 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200121 CONFIG_SYS_MONITOR_BASE,
122 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +0000123 &flash_info[1]);
124#endif
125 } else {
126 memctl->memc_br1 = 0; /* invalidate bank */
127
128 flash_info[1].flash_id = FLASH_UNKNOWN;
129 flash_info[1].sector_count = -1;
130 }
131
132 flash_info[0].size = size_b0;
133 flash_info[1].size = size_b1;
134
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000135 return size_b0 + size_b1;
wdenkaffae2b2002-08-17 09:36:01 +0000136}
137
138/*-----------------------------------------------------------------------
139 */
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000140static void flash_get_offsets(ulong base, flash_info_t *info)
wdenkaffae2b2002-08-17 09:36:01 +0000141{
142 int i;
143
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000144 if (info->flash_id == FLASH_UNKNOWN)
wdenkaffae2b2002-08-17 09:36:01 +0000145 return;
wdenkaffae2b2002-08-17 09:36:01 +0000146
147 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000148 for (i = 0; i < info->sector_count; i++)
wdenkaffae2b2002-08-17 09:36:01 +0000149 info->start[i] = base + (i * 0x00002000);
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000150
wdenkaffae2b2002-08-17 09:36:01 +0000151 return;
152 }
153
154 /* set up sector start address table */
155 if (info->flash_id & FLASH_BTYPE) {
156 /* set sector offsets for bottom boot block type */
157#ifdef CONFIG_FLASH_16BIT
158 info->start[0] = base + 0x00000000;
159 info->start[1] = base + 0x00004000;
160 info->start[2] = base + 0x00006000;
161 info->start[3] = base + 0x00008000;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000162 for (i = 4; i < info->sector_count; i++)
wdenkaffae2b2002-08-17 09:36:01 +0000163 info->start[i] = base + (i * 0x00010000) - 0x00030000;
164#else
165 info->start[0] = base + 0x00000000;
166 info->start[1] = base + 0x00008000;
167 info->start[2] = base + 0x0000C000;
168 info->start[3] = base + 0x00010000;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000169 for (i = 4; i < info->sector_count; i++)
wdenkaffae2b2002-08-17 09:36:01 +0000170 info->start[i] = base + (i * 0x00020000) - 0x00060000;
171#endif
wdenkaffae2b2002-08-17 09:36:01 +0000172 } else {
173 /* set sector offsets for top boot block type */
174 i = info->sector_count - 1;
175 info->start[i--] = base + info->size - 0x00008000;
176 info->start[i--] = base + info->size - 0x0000C000;
177 info->start[i--] = base + info->size - 0x00010000;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000178 for (; i >= 0; i--)
wdenkaffae2b2002-08-17 09:36:01 +0000179 info->start[i] = base + i * 0x00020000;
wdenkaffae2b2002-08-17 09:36:01 +0000180 }
wdenkaffae2b2002-08-17 09:36:01 +0000181}
182
183/*-----------------------------------------------------------------------
184 */
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000185void flash_print_info(flash_info_t *info)
wdenkaffae2b2002-08-17 09:36:01 +0000186{
187 int i;
188
189 if (info->flash_id == FLASH_UNKNOWN) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000190 printf("missing or unknown FLASH type\n");
wdenkaffae2b2002-08-17 09:36:01 +0000191 return;
192 }
193
194 switch (info->flash_id & FLASH_VENDMASK) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000195 case FLASH_MAN_AMD:
196 printf("AMD ");
197 break;
198 case FLASH_MAN_FUJ:
199 printf("FUJITSU ");
200 break;
201 case FLASH_MAN_SST:
202 printf("SST ");
203 break;
204 case FLASH_MAN_STM:
205 printf("STM ");
206 break;
207 default:
208 printf("Unknown Vendor ");
209 break;
wdenkaffae2b2002-08-17 09:36:01 +0000210 }
211
212 switch (info->flash_id & FLASH_TYPEMASK) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000213 case FLASH_AM400B:
214 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
215 break;
216 case FLASH_AM400T:
217 printf("AM29LV400T (4 Mbit, top boot sector)\n");
218 break;
219 case FLASH_AM800B:
220 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
221 break;
222 case FLASH_AM800T:
223 printf("AM29LV800T (8 Mbit, top boot sector)\n");
224 break;
225 case FLASH_AM160B:
226 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
227 break;
228 case FLASH_AM160T:
229 printf("AM29LV160T (16 Mbit, top boot sector)\n");
230 break;
231 case FLASH_AM320B:
232 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
233 break;
234 case FLASH_AM320T:
235 printf("AM29LV320T (32 Mbit, top boot sector)\n");
236 break;
237 case FLASH_SST200A:
238 printf("39xF200A (2M = 128K x 16)\n");
239 break;
240 case FLASH_SST400A:
241 printf("39xF400A (4M = 256K x 16)\n");
242 break;
243 case FLASH_SST800A:
244 printf("39xF800A (8M = 512K x 16)\n");
245 break;
246 case FLASH_STM800AB:
247 printf("M29W800AB (8M = 512K x 16)\n");
248 break;
249 default:
250 printf("Unknown Chip Type\n");
251 break;
wdenkaffae2b2002-08-17 09:36:01 +0000252 }
253
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000254 printf(" Size: %ld MB in %d Sectors\n",
wdenkaffae2b2002-08-17 09:36:01 +0000255 info->size >> 20, info->sector_count);
256
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000257 printf(" Sector Start Addresses:");
258 for (i = 0; i < info->sector_count; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000259 if ((i % 5) == 0)
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000260 printf("\n ");
261 printf(" %08lX%s",
wdenkaffae2b2002-08-17 09:36:01 +0000262 info->start[i],
263 info->protect[i] ? " (RO)" : " "
264 );
265 }
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000266 printf("\n");
wdenkaffae2b2002-08-17 09:36:01 +0000267 return;
268}
269
wdenkaffae2b2002-08-17 09:36:01 +0000270/*
271 * The following code cannot be run from FLASH!
272 */
273
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000274static ulong flash_get_size(vu_long *addr, flash_info_t *info)
wdenkaffae2b2002-08-17 09:36:01 +0000275{
276 short i;
277 ulong value;
278 ulong base = (ulong)addr;
279
280 /* Write auto select command: read Manufacturer ID */
281#ifdef CONFIG_FLASH_16BIT
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000282 vu_short *s_addr = (vu_short *)addr;
wdenkaffae2b2002-08-17 09:36:01 +0000283 s_addr[0x5555] = 0x00AA;
284 s_addr[0x2AAA] = 0x0055;
285 s_addr[0x5555] = 0x0090;
286 value = s_addr[0];
287 value = value|(value<<16);
288#else
289 addr[0x5555] = 0x00AA00AA;
290 addr[0x2AAA] = 0x00550055;
291 addr[0x5555] = 0x00900090;
292 value = addr[0];
293#endif
294
295 switch (value) {
296 case AMD_MANUFACT:
297 info->flash_id = FLASH_MAN_AMD;
298 break;
299 case FUJ_MANUFACT:
300 info->flash_id = FLASH_MAN_FUJ;
301 break;
302 case SST_MANUFACT:
303 info->flash_id = FLASH_MAN_SST;
304 break;
305 case STM_MANUFACT:
306 info->flash_id = FLASH_MAN_STM;
307 break;
308 default:
309 info->flash_id = FLASH_UNKNOWN;
310 info->sector_count = 0;
311 info->size = 0;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000312 return 0; /* no or unknown flash */
wdenkaffae2b2002-08-17 09:36:01 +0000313 }
314#ifdef CONFIG_FLASH_16BIT
315 value = s_addr[1];
316 value = value|(value<<16);
317#else
318 value = addr[1]; /* device ID */
319#endif
320
321 switch (value) {
322 case AMD_ID_LV400T:
323 info->flash_id += FLASH_AM400T;
324 info->sector_count = 11;
325 info->size = 0x00100000;
326 break; /* => 1 MB */
327
328 case AMD_ID_LV400B:
329 info->flash_id += FLASH_AM400B;
330 info->sector_count = 11;
331 info->size = 0x00100000;
332 break; /* => 1 MB */
333
334 case AMD_ID_LV800T:
335 info->flash_id += FLASH_AM800T;
336 info->sector_count = 19;
337 info->size = 0x00200000;
338 break; /* => 2 MB */
339
340 case AMD_ID_LV800B:
341 info->flash_id += FLASH_AM800B;
342#ifdef CONFIG_FLASH_16BIT
343 info->sector_count = 19;
344 info->size = 0x00100000; /* => 1 MB */
345#else
346 info->sector_count = 19;
347 info->size = 0x00200000; /* => 2 MB */
348#endif
349 break;
350
351 case AMD_ID_LV160T:
352 info->flash_id += FLASH_AM160T;
353 info->sector_count = 35;
354 info->size = 0x00400000;
355 break; /* => 4 MB */
356
357 case AMD_ID_LV160B:
358 info->flash_id += FLASH_AM160B;
359#ifdef CONFIG_FLASH_16BIT
360 info->sector_count = 35;
361 info->size = 0x00200000; /* => 2 MB */
362#else
363 info->sector_count = 35;
364 info->size = 0x00400000; /* => 4 MB */
365#endif
366
367 break;
wdenkaffae2b2002-08-17 09:36:01 +0000368 case SST_ID_xF200A:
369 info->flash_id += FLASH_SST200A;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000370 info->sector_count = 64; /* 39xF200A (2M = 128K x 16) */
wdenkaffae2b2002-08-17 09:36:01 +0000371 info->size = 0x00080000;
372 break;
373 case SST_ID_xF400A:
374 info->flash_id += FLASH_SST400A;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000375 info->sector_count = 128; /* 39xF400A (4M = 256K x 16) */
wdenkaffae2b2002-08-17 09:36:01 +0000376 info->size = 0x00100000;
377 break;
378 case SST_ID_xF800A:
379 info->flash_id += FLASH_SST800A;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000380 info->sector_count = 256; /* 39xF800A (8M = 512K x 16) */
wdenkaffae2b2002-08-17 09:36:01 +0000381 info->size = 0x00200000;
382 break; /* => 2 MB */
383 case STM_ID_x800AB:
384 info->flash_id += FLASH_STM800AB;
385 info->sector_count = 19;
386 info->size = 0x00200000;
387 break; /* => 2 MB */
388 default:
389 info->flash_id = FLASH_UNKNOWN;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000390 return 0; /* => no or unknown flash */
wdenkaffae2b2002-08-17 09:36:01 +0000391
392 }
393
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200394 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000395 printf("** ERROR: sector count %d > max (%d) **\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200396 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
397 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenkaffae2b2002-08-17 09:36:01 +0000398 }
399
400 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000401 for (i = 0; i < info->sector_count; i++)
wdenkaffae2b2002-08-17 09:36:01 +0000402 info->start[i] = base + (i * 0x00002000);
wdenkaffae2b2002-08-17 09:36:01 +0000403 } else { /* AMD and Fujitsu types */
404 /* set up sector start address table */
405 if (info->flash_id & FLASH_BTYPE) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000406 /* set sector offsets for bottom boot block type */
wdenkaffae2b2002-08-17 09:36:01 +0000407#ifdef CONFIG_FLASH_16BIT
408
409 info->start[0] = base + 0x00000000;
410 info->start[1] = base + 0x00004000;
411 info->start[2] = base + 0x00006000;
412 info->start[3] = base + 0x00008000;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000413 for (i = 4; i < info->sector_count; i++)
414 info->start[i] = base +
415 (i * 0x00010000) - 0x00030000;
wdenkaffae2b2002-08-17 09:36:01 +0000416#else
417 info->start[0] = base + 0x00000000;
418 info->start[1] = base + 0x00008000;
419 info->start[2] = base + 0x0000C000;
420 info->start[3] = base + 0x00010000;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000421 for (i = 4; i < info->sector_count; i++)
422 info->start[i] = base +
423 (i * 0x00020000) - 0x00060000;
wdenkaffae2b2002-08-17 09:36:01 +0000424#endif
wdenkaffae2b2002-08-17 09:36:01 +0000425 } else {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000426 /* set sector offsets for top boot block type */
wdenkaffae2b2002-08-17 09:36:01 +0000427 i = info->sector_count - 1;
428 info->start[i--] = base + info->size - 0x00008000;
429 info->start[i--] = base + info->size - 0x0000C000;
430 info->start[i--] = base + info->size - 0x00010000;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000431 for (; i >= 0; i--)
wdenkaffae2b2002-08-17 09:36:01 +0000432 info->start[i] = base + i * 0x00020000;
wdenkaffae2b2002-08-17 09:36:01 +0000433 }
434
435 /* check for protected sectors */
436 for (i = 0; i < info->sector_count; i++) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000437 /*
438 * read sector protection at sector address:
wdenkaffae2b2002-08-17 09:36:01 +0000439 * (A7 .. A0) = 0x02
440 * D0 = 1 if protected
441 */
442#ifdef CONFIG_FLASH_16BIT
443 s_addr = (volatile unsigned short *)(info->start[i]);
444 info->protect[i] = s_addr[2] & 1;
445#else
446 addr = (volatile unsigned long *)(info->start[i]);
447 info->protect[i] = addr[2] & 1;
448#endif
449 }
450 }
451
452 /*
453 * Prevent writes to uninitialized FLASH.
454 */
455 if (info->flash_id != FLASH_UNKNOWN) {
456#ifdef CONFIG_FLASH_16BIT
457 s_addr = (volatile unsigned short *)(info->start[0]);
458 *s_addr = 0x00F0; /* reset bank */
459#else
460 addr = (volatile unsigned long *)info->start[0];
461 *addr = 0x00F000F0; /* reset bank */
462#endif
463
464 }
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000465 return info->size;
wdenkaffae2b2002-08-17 09:36:01 +0000466}
467
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000468int flash_erase(flash_info_t *info, int s_first, int s_last)
wdenkaffae2b2002-08-17 09:36:01 +0000469{
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000470 vu_long *addr = (vu_long *)(info->start[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000471 int flag, prot, sect;
472 ulong start, now, last;
473#ifdef CONFIG_FLASH_16BIT
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000474 vu_short *s_addr = (vu_short *)addr;
wdenkaffae2b2002-08-17 09:36:01 +0000475#endif
476
477 if ((s_first < 0) || (s_first > s_last)) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000478 if (info->flash_id == FLASH_UNKNOWN)
479 printf("- missing\n");
480 else
481 printf("- no sectors to erase\n");
wdenkaffae2b2002-08-17 09:36:01 +0000482 return 1;
483 }
484/*#ifndef CONFIG_FLASH_16BIT
485 ulong type;
486 type = (info->flash_id & FLASH_VENDMASK);
487 if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
488 printf ("Can't erase unknown flash type %08lx - aborted\n",
489 info->flash_id);
490 return;
491 }
492#endif*/
493 prot = 0;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000494 for (sect = s_first; sect <= s_last; ++sect) {
495 if (info->protect[sect])
wdenkaffae2b2002-08-17 09:36:01 +0000496 prot++;
wdenkaffae2b2002-08-17 09:36:01 +0000497 }
498
499 if (prot) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000500 printf("- Warning: %d protected sectors will not be erased!\n",
wdenkaffae2b2002-08-17 09:36:01 +0000501 prot);
502 } else {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000503 printf("\n");
wdenkaffae2b2002-08-17 09:36:01 +0000504 }
505
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000506 start = get_timer(0);
wdenkaffae2b2002-08-17 09:36:01 +0000507 last = start;
508 /* Start erase on unprotected sectors */
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000509 for (sect = s_first; sect <= s_last; sect++) {
wdenkaffae2b2002-08-17 09:36:01 +0000510 if (info->protect[sect] == 0) { /* not protected */
511#ifdef CONFIG_FLASH_16BIT
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000512 vu_short *s_sect_addr = (vu_short *)(info->start[sect]);
wdenkaffae2b2002-08-17 09:36:01 +0000513#else
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000514 vu_long *sect_addr = (vu_long *)(info->start[sect]);
wdenkaffae2b2002-08-17 09:36:01 +0000515#endif
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000516 /* Disable interrupts which might cause a timeout */
wdenkaffae2b2002-08-17 09:36:01 +0000517 flag = disable_interrupts();
518
519#ifdef CONFIG_FLASH_16BIT
520
521 /*printf("\ns_sect_addr=%x",s_sect_addr);*/
522 s_addr[0x5555] = 0x00AA;
523 s_addr[0x2AAA] = 0x0055;
524 s_addr[0x5555] = 0x0080;
525 s_addr[0x5555] = 0x00AA;
526 s_addr[0x2AAA] = 0x0055;
527 s_sect_addr[0] = 0x0030;
528#else
529 addr[0x5555] = 0x00AA00AA;
530 addr[0x2AAA] = 0x00550055;
531 addr[0x5555] = 0x00800080;
532 addr[0x5555] = 0x00AA00AA;
533 addr[0x2AAA] = 0x00550055;
534 sect_addr[0] = 0x00300030;
535#endif
536 /* re-enable interrupts if necessary */
537 if (flag)
538 enable_interrupts();
539
540 /* wait at least 80us - let's wait 1 ms */
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000541 udelay(1000);
wdenkaffae2b2002-08-17 09:36:01 +0000542
543#ifdef CONFIG_FLASH_16BIT
544 while ((s_sect_addr[0] & 0x0080) != 0x0080) {
545#else
546 while ((sect_addr[0] & 0x00800080) != 0x00800080) {
547#endif
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000548 now = get_timer(start);
549 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
550 printf("Timeout\n");
wdenkaffae2b2002-08-17 09:36:01 +0000551 return 1;
552 }
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000553 /* show every second that we're waiting */
554 if ((now - last) > 1000) {
555 putc('.');
wdenkaffae2b2002-08-17 09:36:01 +0000556 last = now;
557 }
558 }
559 }
560 }
561
562 /* reset to read mode */
563 addr = (volatile unsigned long *)info->start[0];
564#ifdef CONFIG_FLASH_16BIT
565 s_addr[0] = 0x00F0; /* reset bank */
566#else
567 addr[0] = 0x00F000F0; /* reset bank */
568#endif
569
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000570 printf(" done\n");
wdenkaffae2b2002-08-17 09:36:01 +0000571 return 0;
572}
573
574/*-----------------------------------------------------------------------
575 * Copy memory to flash, returns:
576 * 0 - OK
577 * 1 - write timeout
578 * 2 - Flash not erased
579 * 4 - Flash not identified
580 */
581
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000582int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
wdenkaffae2b2002-08-17 09:36:01 +0000583{
584 ulong cp, wp, data;
585 int i, l, rc;
586
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000587 if (info->flash_id == FLASH_UNKNOWN)
wdenkaffae2b2002-08-17 09:36:01 +0000588 return 4;
wdenkaffae2b2002-08-17 09:36:01 +0000589
590 wp = (addr & ~3); /* get lower word aligned address */
591
592 /*
593 * handle unaligned start bytes
594 */
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000595 l = addr - wp;
596
597 if (l != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000598 data = 0;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000599 for (i = 0, cp = wp; i < l; ++i, ++cp)
wdenkaffae2b2002-08-17 09:36:01 +0000600 data = (data << 8) | (*(uchar *)cp);
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000601
602 for (; i < 4 && cnt > 0; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000603 data = (data << 8) | *src++;
604 --cnt;
605 ++cp;
606 }
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000607 for (; cnt == 0 && i < 4; ++i, ++cp)
wdenkaffae2b2002-08-17 09:36:01 +0000608 data = (data << 8) | (*(uchar *)cp);
wdenkaffae2b2002-08-17 09:36:01 +0000609
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000610 rc = write_word(info, wp, data);
611
612 if (rc != 0)
613 return rc;
614
wdenkaffae2b2002-08-17 09:36:01 +0000615 wp += 4;
616 }
617
618 /*
619 * handle word aligned part
620 */
621 while (cnt >= 4) {
622 data = 0;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000623 for (i = 0; i < 4; ++i)
wdenkaffae2b2002-08-17 09:36:01 +0000624 data = (data << 8) | *src++;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000625
626 rc = write_word(info, wp, data);
627 if (rc != 0)
628 return rc;
629
wdenkaffae2b2002-08-17 09:36:01 +0000630 wp += 4;
631 cnt -= 4;
632 }
633
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000634 if (cnt == 0)
635 return 0;
wdenkaffae2b2002-08-17 09:36:01 +0000636
637 /*
638 * handle unaligned tail bytes
639 */
640 data = 0;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000641 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
wdenkaffae2b2002-08-17 09:36:01 +0000642 data = (data << 8) | *src++;
643 --cnt;
644 }
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000645 for (; i < 4; ++i, ++cp)
wdenkaffae2b2002-08-17 09:36:01 +0000646 data = (data << 8) | (*(uchar *)cp);
wdenkaffae2b2002-08-17 09:36:01 +0000647
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000648 return write_word(info, wp, data);
wdenkaffae2b2002-08-17 09:36:01 +0000649}
650
651/*-----------------------------------------------------------------------
652 * Write a word to Flash, returns:
653 * 0 - OK
654 * 1 - write timeout
655 * 2 - Flash not erased
656 */
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000657static int write_word(flash_info_t *info, ulong dest, ulong data)
wdenkaffae2b2002-08-17 09:36:01 +0000658{
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000659 vu_long *addr = (vu_long *)(info->start[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000660
661#ifdef CONFIG_FLASH_16BIT
662 vu_short high_data;
663 vu_short low_data;
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000664 vu_short *s_addr = (vu_short *)addr;
wdenkaffae2b2002-08-17 09:36:01 +0000665#endif
666 ulong start;
667 int flag;
668
669 /* Check if Flash is (sufficiently) erased */
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000670 if ((*((vu_long *)dest) & data) != data)
671 return 2;
wdenkaffae2b2002-08-17 09:36:01 +0000672
673#ifdef CONFIG_FLASH_16BIT
674 /* Write the 16 higher-bits */
675 /* Disable interrupts which might cause a timeout here */
676 flag = disable_interrupts();
677
678 high_data = ((data>>16) & 0x0000ffff);
679
680 s_addr[0x5555] = 0x00AA;
681 s_addr[0x2AAA] = 0x0055;
682 s_addr[0x5555] = 0x00A0;
683
684 *((vu_short *)dest) = high_data;
685
wdenkaffae2b2002-08-17 09:36:01 +0000686 /* re-enable interrupts if necessary */
687 if (flag)
688 enable_interrupts();
689
690 /* data polling for D7 */
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000691 start = get_timer(0);
wdenkaffae2b2002-08-17 09:36:01 +0000692 while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000693 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
694 return 1;
wdenkaffae2b2002-08-17 09:36:01 +0000695 }
696
wdenkaffae2b2002-08-17 09:36:01 +0000697 /* Write the 16 lower-bits */
698#endif
699
700 /* Disable interrupts which might cause a timeout here */
701 flag = disable_interrupts();
702#ifdef CONFIG_FLASH_16BIT
703 dest += 0x2;
704 low_data = (data & 0x0000ffff);
705
706 s_addr[0x5555] = 0x00AA;
707 s_addr[0x2AAA] = 0x0055;
708 s_addr[0x5555] = 0x00A0;
709 *((vu_short *)dest) = low_data;
710
711#else
712 addr[0x5555] = 0x00AA00AA;
713 addr[0x2AAA] = 0x00550055;
714 addr[0x5555] = 0x00A000A0;
715 *((vu_long *)dest) = data;
716#endif
717
718 /* re-enable interrupts if necessary */
719 if (flag)
720 enable_interrupts();
721
722 /* data polling for D7 */
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000723 start = get_timer(0);
wdenkaffae2b2002-08-17 09:36:01 +0000724
725#ifdef CONFIG_FLASH_16BIT
726 while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
727#else
728 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
729#endif
730
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000731 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
732 return 1;
wdenkaffae2b2002-08-17 09:36:01 +0000733 }
Wolfgang Denkd0bb8d42011-11-04 15:55:38 +0000734 return 0;
wdenkaffae2b2002-08-17 09:36:01 +0000735}