blob: af329b9f55da6eee0665be8e91a33c0210a04ecd [file] [log] [blame]
wdenkdc7c9a12003-03-26 06:55:25 +00001/*
wdenkdc7c9a12003-03-26 06:55:25 +00002 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
3 *
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23#include <common.h>
24#include <mpc8xx.h>
25
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020026#ifndef CONFIG_ENV_ADDR
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020027#define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
wdenkdc7c9a12003-03-26 06:55:25 +000028#endif
29
Wolfgang Denk8adc9052011-11-04 15:55:49 +000030flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
wdenkdc7c9a12003-03-26 06:55:25 +000031
32/*-----------------------------------------------------------------------
33 * Functions
34 */
Wolfgang Denk8adc9052011-11-04 15:55:49 +000035static int write_word(flash_info_t *info, ulong dest, ulong data);
36
37#ifdef CONFIG_BOOT_8B
38static int my_in_8(unsigned char *addr);
39static void my_out_8(unsigned char *addr, int val);
wdenkdc7c9a12003-03-26 06:55:25 +000040#endif
Wolfgang Denk8adc9052011-11-04 15:55:49 +000041#ifdef CONFIG_BOOT_16B
42static int my_in_be16(unsigned short *addr);
43static void my_out_be16(unsigned short *addr, int val);
wdenkdc7c9a12003-03-26 06:55:25 +000044#endif
Wolfgang Denk8adc9052011-11-04 15:55:49 +000045#ifdef CONFIG_BOOT_32B
46static unsigned my_in_be32(unsigned *addr);
47static void my_out_be32(unsigned *addr, int val);
wdenkdc7c9a12003-03-26 06:55:25 +000048#endif
49/*-----------------------------------------------------------------------
50 */
51
Wolfgang Denk8adc9052011-11-04 15:55:49 +000052unsigned long flash_init(void)
wdenkdc7c9a12003-03-26 06:55:25 +000053{
Wolfgang Denk8adc9052011-11-04 15:55:49 +000054 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
wdenkdc7c9a12003-03-26 06:55:25 +000055 volatile memctl8xx_t *memctl = &immap->im_memctl;
56 unsigned long size_b0, size_b1;
57 int i;
58
Wolfgang Denk8adc9052011-11-04 15:55:49 +000059 size_b0 = 0;
60 size_b1 = 0;
wdenkdc7c9a12003-03-26 06:55:25 +000061 /* Init: no FLASHes known */
Wolfgang Denk8adc9052011-11-04 15:55:49 +000062 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
wdenkdc7c9a12003-03-26 06:55:25 +000063 flash_info[i].flash_id = FLASH_UNKNOWN;
Wolfgang Denk8adc9052011-11-04 15:55:49 +000064
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020065#ifdef CONFIG_SYS_DOC_BASE
wdenkdc7c9a12003-03-26 06:55:25 +000066#ifndef CONFIG_FEL8xx_AT
Wolfgang Denk8adc9052011-11-04 15:55:49 +000067 /* 32k bytes */
68 memctl->memc_or5 = (0xffff8000 | CONFIG_SYS_OR_TIMING_DOC);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020069 memctl->memc_br5 = CONFIG_SYS_DOC_BASE | 0x401;
wdenkdc7c9a12003-03-26 06:55:25 +000070#else
Wolfgang Denk8adc9052011-11-04 15:55:49 +000071 /* 32k bytes */
72 memctl->memc_or3 = (0xffff8000 | CONFIG_SYS_OR_TIMING_DOC);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020073 memctl->memc_br3 = CONFIG_SYS_DOC_BASE | 0x401;
wdenkdc7c9a12003-03-26 06:55:25 +000074#endif
75#endif
Wolfgang Denk8adc9052011-11-04 15:55:49 +000076#if defined(CONFIG_BOOT_8B)
77 size_b0 = 0x80000; /* 512 K */
78
wdenk8bde7f72003-06-27 21:31:46 +000079 flash_info[0].flash_id = FLASH_MAN_AMD | FLASH_AM040;
80 flash_info[0].sector_count = 8;
81 flash_info[0].size = 0x00080000;
Wolfgang Denk8adc9052011-11-04 15:55:49 +000082
wdenk8bde7f72003-06-27 21:31:46 +000083 /* set up sector start address table */
84 for (i = 0; i < flash_info[0].sector_count; i++)
Wolfgang Denk8adc9052011-11-04 15:55:49 +000085 flash_info[0].start[i] = 0x40000000 + (i * 0x10000);
86
wdenk8bde7f72003-06-27 21:31:46 +000087 /* protect all sectors */
88 for (i = 0; i < flash_info[0].sector_count; i++)
Wolfgang Denk8adc9052011-11-04 15:55:49 +000089 flash_info[0].protect[i] = 0x1;
90
91#elif defined(CONFIG_BOOT_16B)
92 size_b0 = 0x400000; /* 4MB , assume AMD29LV320B */
93
wdenk8bde7f72003-06-27 21:31:46 +000094 flash_info[0].flash_id = FLASH_MAN_AMD | FLASH_AM320B;
95 flash_info[0].sector_count = 67;
96 flash_info[0].size = 0x00400000;
Wolfgang Denk8adc9052011-11-04 15:55:49 +000097
wdenk8bde7f72003-06-27 21:31:46 +000098 /* set up sector start address table */
Wolfgang Denk8adc9052011-11-04 15:55:49 +000099 flash_info[0].start[0] = 0x40000000;
wdenk8bde7f72003-06-27 21:31:46 +0000100 flash_info[0].start[1] = 0x40000000 + 0x4000;
101 flash_info[0].start[2] = 0x40000000 + 0x6000;
102 flash_info[0].start[3] = 0x40000000 + 0x8000;
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000103
104 for (i = 4; i < flash_info[0].sector_count; i++) {
105 flash_info[0].start[i] =
106 0x40000000 + 0x10000 + ((i - 4) * 0x10000);
107 }
108
wdenk8bde7f72003-06-27 21:31:46 +0000109 /* protect all sectors */
110 for (i = 0; i < flash_info[0].sector_count; i++)
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000111 flash_info[0].protect[i] = 0x1;
wdenk8bde7f72003-06-27 21:31:46 +0000112#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000113
wdenkdc7c9a12003-03-26 06:55:25 +0000114#ifdef CONFIG_BOOT_32B
115
116 /* Static FLASH Bank configuration here - FIXME XXX */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000117 size_b0 = flash_get_size((vu_long *) FLASH_BASE0_PRELIM,
118 &flash_info[0]);
wdenkdc7c9a12003-03-26 06:55:25 +0000119
120 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000121 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
122 size_b0, size_b0 << 20);
wdenkdc7c9a12003-03-26 06:55:25 +0000123 }
124
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000125 size_b1 = flash_get_size((vu_long *) FLASH_BASE1_PRELIM,
126 &flash_info[1]);
wdenkdc7c9a12003-03-26 06:55:25 +0000127
128 if (size_b1 > size_b0) {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000129 printf("## ERROR: "
wdenkdc7c9a12003-03-26 06:55:25 +0000130 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000131 size_b1, size_b1 << 20, size_b0, size_b0 << 20);
132 flash_info[0].flash_id = FLASH_UNKNOWN;
133 flash_info[1].flash_id = FLASH_UNKNOWN;
134 flash_info[0].sector_count = -1;
135 flash_info[1].sector_count = -1;
136 flash_info[0].size = 0;
137 flash_info[1].size = 0;
138
139 return 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000140 }
141
142 /* Remap FLASH according to real size */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000143 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH |
144 (-size_b0 & OR_AM_MSK);
145 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
146 BR_MS_GPCM | BR_V;
wdenkdc7c9a12003-03-26 06:55:25 +0000147
148 /* Re-do sizing to get full correct info */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000149 size_b0 = flash_get_size((vu_long *) CONFIG_SYS_FLASH_BASE,
150 &flash_info[0]);
wdenkdc7c9a12003-03-26 06:55:25 +0000151
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000152 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkdc7c9a12003-03-26 06:55:25 +0000153
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200154#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkdc7c9a12003-03-26 06:55:25 +0000155 /* monitor protection ON by default */
156 flash_protect(FLAG_PROTECT_SET,
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000157 CONFIG_SYS_MONITOR_BASE,
158 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
159 &flash_info[0]);
wdenkdc7c9a12003-03-26 06:55:25 +0000160#endif
161
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +0200162#ifdef CONFIG_ENV_IS_IN_FLASH
wdenkdc7c9a12003-03-26 06:55:25 +0000163 /* ENV protection ON by default */
164 flash_protect(FLAG_PROTECT_SET,
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000165 CONFIG_ENV_ADDR,
166 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
wdenkdc7c9a12003-03-26 06:55:25 +0000167#endif
168
169 if (size_b1) {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000170 memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH |
171 (-size_b1 & 0xFFFF8000);
172 memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE +
173 size_b0) & BR_BA_MSK) | BR_MS_GPCM | BR_V;
wdenkdc7c9a12003-03-26 06:55:25 +0000174
175 /* Re-do sizing to get full correct info */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000176 size_b1 = flash_get_size((vu_long *)(CONFIG_SYS_FLASH_BASE +
177 size_b0), &flash_info[1]);
wdenkdc7c9a12003-03-26 06:55:25 +0000178
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000179 flash_get_offsets(CONFIG_SYS_FLASH_BASE + size_b0,
180 &flash_info[1]);
wdenkdc7c9a12003-03-26 06:55:25 +0000181
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200182#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkdc7c9a12003-03-26 06:55:25 +0000183 /* monitor protection ON by default */
184 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200185 CONFIG_SYS_MONITOR_BASE,
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000186 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
wdenkdc7c9a12003-03-26 06:55:25 +0000187 &flash_info[1]);
188#endif
189
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +0200190#ifdef CONFIG_ENV_IS_IN_FLASH
wdenkdc7c9a12003-03-26 06:55:25 +0000191 /* ENV protection ON by default */
192 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200193 CONFIG_ENV_ADDR,
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000194 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
wdenkdc7c9a12003-03-26 06:55:25 +0000195 &flash_info[1]);
196#endif
197 } else {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000198 memctl->memc_br1 = 0; /* invalidate bank */
wdenkdc7c9a12003-03-26 06:55:25 +0000199
200 flash_info[1].flash_id = FLASH_UNKNOWN;
201 flash_info[1].sector_count = -1;
202 }
203
204 flash_info[0].size = size_b0;
205 flash_info[1].size = size_b1;
206
207
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000208#endif /* CONFIG_BOOT_32B */
wdenkdc7c9a12003-03-26 06:55:25 +0000209
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000210 return size_b0 + size_b1;
wdenkdc7c9a12003-03-26 06:55:25 +0000211}
wdenkdc7c9a12003-03-26 06:55:25 +0000212
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000213
214void flash_print_info(flash_info_t *info)
wdenkdc7c9a12003-03-26 06:55:25 +0000215{
216 int i;
217
218 if (info->flash_id == FLASH_UNKNOWN) {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000219 printf("missing or unknown FLASH type\n");
wdenkdc7c9a12003-03-26 06:55:25 +0000220 return;
221 }
222
223 switch (info->flash_id & FLASH_VENDMASK) {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000224 case FLASH_MAN_AMD:
225 printf("AMD ");
226 break;
227 case FLASH_MAN_FUJ:
228 printf("FUJITSU ");
229 break;
230 default:
231 printf("Unknown Vendor ");
232 break;
wdenkdc7c9a12003-03-26 06:55:25 +0000233 }
234
235 switch (info->flash_id & FLASH_TYPEMASK) {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000236 case FLASH_AM400B:
237 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
238 break;
239 case FLASH_AM400T:
240 printf("AM29LV400T (4 Mbit, top boot sector)\n");
241 break;
242 case FLASH_AM800B:
243 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
244 break;
245 case FLASH_AM800T:
246 printf("AM29LV800T (8 Mbit, top boot sector)\n");
247 break;
248 case FLASH_AM160B:
249 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
250 break;
251 case FLASH_AM160T:
252 printf("AM29LV160T (16 Mbit, top boot sector)\n");
253 break;
254 case FLASH_AM320B:
255 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
256 break;
257 case FLASH_AM320T:
258 printf("AM29LV320T (32 Mbit, top boot sector)\n");
259 break;
260 default:
261 printf("Unknown Chip Type\n");
262 break;
wdenkdc7c9a12003-03-26 06:55:25 +0000263 }
264
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000265 printf(" Size: %ld MB in %d Sectors\n",
wdenkdc7c9a12003-03-26 06:55:25 +0000266 info->size >> 20, info->sector_count);
267
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000268 printf(" Sector Start Addresses:");
269 for (i = 0; i < info->sector_count; ++i) {
wdenkdc7c9a12003-03-26 06:55:25 +0000270 if ((i % 5) == 0)
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000271 printf("\n ");
272 printf(" %08lX%s",
273 info->start[i], info->protect[i] ? " (RO)" : " ");
wdenkdc7c9a12003-03-26 06:55:25 +0000274 }
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000275 printf("\n");
wdenkdc7c9a12003-03-26 06:55:25 +0000276 return;
277}
278
wdenkdc7c9a12003-03-26 06:55:25 +0000279/*
280 * The following code cannot be run from FLASH!
281 */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000282
283int flash_erase(flash_info_t *info, int s_first, int s_last)
wdenkdc7c9a12003-03-26 06:55:25 +0000284{
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000285 vu_long *addr = (vu_long *) (info->start[0]);
286 int flag, prot, sect, l_sect, in_mid, in_did;
wdenkdc7c9a12003-03-26 06:55:25 +0000287 ulong start, now, last;
288
289 if ((s_first < 0) || (s_first > s_last)) {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000290 if (info->flash_id == FLASH_UNKNOWN)
291 printf("- missing\n");
292 else
293 printf("- no sectors to erase\n");
294
wdenkdc7c9a12003-03-26 06:55:25 +0000295 return 1;
296 }
297
298 if ((info->flash_id == FLASH_UNKNOWN) ||
299 (info->flash_id > FLASH_AMD_COMP)) {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000300 printf("Can't erase unknown flash type %08lx - aborted\n",
301 info->flash_id);
wdenkdc7c9a12003-03-26 06:55:25 +0000302 return 1;
303 }
304
305 prot = 0;
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000306 for (sect = s_first; sect <= s_last; ++sect) {
307 if (info->protect[sect])
wdenkdc7c9a12003-03-26 06:55:25 +0000308 prot++;
wdenkdc7c9a12003-03-26 06:55:25 +0000309 }
310
311 if (prot) {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000312 printf("- Warning: %d protected sectors will not be erased!\n",
wdenkdc7c9a12003-03-26 06:55:25 +0000313 prot);
314 } else {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000315 printf("\n");
wdenkdc7c9a12003-03-26 06:55:25 +0000316 }
317
318 l_sect = -1;
319
320 /* Disable interrupts which might cause a timeout here */
321 flag = disable_interrupts();
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000322
323#if defined(CONFIG_BOOT_8B)
324 my_out_8((unsigned char *)((ulong)addr + 0x555), 0xaa);
325 my_out_8((unsigned char *)((ulong)addr + 0x2aa), 0x55);
326 my_out_8((unsigned char *)((ulong)addr + 0x555), 0x90);
327
328 in_mid = my_in_8((unsigned char *)addr);
329 in_did = my_in_8((unsigned char *)((ulong)addr + 1));
330
331 printf(" man ID=0x%x, dev ID=0x%x.\n", in_mid, in_did);
332
333 my_out_8((unsigned char *)addr, 0xf0);
wdenkdc7c9a12003-03-26 06:55:25 +0000334 udelay(1);
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000335
336 my_out_8((unsigned char *)((ulong)addr + 0x555), 0xaa);
337 my_out_8((unsigned char *)((ulong)addr + 0x2aa), 0x55);
338 my_out_8((unsigned char *)((ulong)addr + 0x555), 0x80);
339 my_out_8((unsigned char *)((ulong)addr + 0x555), 0xaa);
340 my_out_8((unsigned char *)((ulong)addr + 0x2aa), 0x55);
341
wdenkdc7c9a12003-03-26 06:55:25 +0000342 /* Start erase on unprotected sectors */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000343 for (sect = s_first; sect <= s_last; sect++) {
wdenkdc7c9a12003-03-26 06:55:25 +0000344 if (info->protect[sect] == 0) { /* not protected */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000345 addr = (vu_long *) (info->start[sect]);
wdenk8bde7f72003-06-27 21:31:46 +0000346 /*addr[0] = 0x00300030; */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000347 my_out_8((unsigned char *)((ulong)addr), 0x30);
wdenkdc7c9a12003-03-26 06:55:25 +0000348 l_sect = sect;
349 }
350 }
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000351#elif defined(CONFIG_BOOT_16B)
352 my_out_be16((unsigned short *)((ulong)addr + (0xaaa)), 0xaa);
353 my_out_be16((unsigned short *)((ulong)addr + (0x554)), 0x55);
354 my_out_be16((unsigned short *)((ulong)addr + (0xaaa)), 0x90);
355 in_mid = my_in_be16((unsigned short *)addr);
356 in_did = my_in_be16((unsigned short *)((ulong)addr + 2));
357 printf(" man ID=0x%x, dev ID=0x%x.\n", in_mid, in_did);
358 my_out_be16((unsigned short *)addr, 0xf0);
wdenkdc7c9a12003-03-26 06:55:25 +0000359 udelay(1);
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000360 my_out_be16((unsigned short *)((ulong)addr + 0xaaa), 0xaa);
361 my_out_be16((unsigned short *)((ulong)addr + 0x554), 0x55);
362 my_out_be16((unsigned short *)((ulong)addr + 0xaaa), 0x80);
363 my_out_be16((unsigned short *)((ulong)addr + 0xaaa), 0xaa);
364 my_out_be16((unsigned short *)((ulong)addr + 0x554), 0x55);
wdenkdc7c9a12003-03-26 06:55:25 +0000365 /* Start erase on unprotected sectors */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000366 for (sect = s_first; sect <= s_last; sect++) {
wdenkdc7c9a12003-03-26 06:55:25 +0000367 if (info->protect[sect] == 0) { /* not protected */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000368 addr = (vu_long *) (info->start[sect]);
369 my_out_be16((unsigned short *)((ulong)addr), 0x30);
wdenkdc7c9a12003-03-26 06:55:25 +0000370 l_sect = sect;
371 }
372 }
373
374#elif defined(CONFIG_BOOT_32B)
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000375 my_out_be32((unsigned *)((ulong)addr + 0x1554), 0xaa);
376 my_out_be32((unsigned *)((ulong)addr + 0xaa8), 0x55);
377 my_out_be32((unsigned *)((ulong)addr + 0x1554), 0x90);
378
379 in_mid = my_in_be32((unsigned *)addr);
380 in_did = my_in_be32((unsigned *)((ulong)addr + 4));
381
382 printf(" man ID=0x%x, dev ID=0x%x.\n", in_mid, in_did);
383
384 my_out_be32((unsigned *) addr, 0xf0);
wdenkdc7c9a12003-03-26 06:55:25 +0000385 udelay(1);
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000386
387 my_out_be32((unsigned *)((ulong)addr + 0x1554), 0xaa);
388 my_out_be32((unsigned *)((ulong)addr + 0xaa8), 0x55);
389 my_out_be32((unsigned *)((ulong)addr + 0x1554), 0x80);
390 my_out_be32((unsigned *)((ulong)addr + 0x1554), 0xaa);
391 my_out_be32((unsigned *)((ulong)addr + 0xaa8), 0x55);
392
wdenkdc7c9a12003-03-26 06:55:25 +0000393 /* Start erase on unprotected sectors */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000394 for (sect = s_first; sect <= s_last; sect++) {
wdenkdc7c9a12003-03-26 06:55:25 +0000395 if (info->protect[sect] == 0) { /* not protected */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000396 addr = (vu_long *) (info->start[sect]);
397 my_out_be32((unsigned *)((ulong)addr), 0x00300030);
wdenkdc7c9a12003-03-26 06:55:25 +0000398 l_sect = sect;
399 }
400 }
401
402#else
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000403#error CONFIG_BOOT_(size)B missing.
wdenkdc7c9a12003-03-26 06:55:25 +0000404#endif
405 /* re-enable interrupts if necessary */
406 if (flag)
407 enable_interrupts();
408
409 /* wait at least 80us - let's wait 1 ms */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000410 udelay(1000);
wdenkdc7c9a12003-03-26 06:55:25 +0000411
412 /*
413 * We wait for the last triggered sector
414 */
415 if (l_sect < 0)
416 goto DONE;
417
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000418 start = get_timer(0);
419 last = start;
420 addr = (vu_long *) (info->start[l_sect]);
421#if defined(CONFIG_BOOT_8B)
422 while ((my_in_8((unsigned char *) addr) & 0x80) != 0x80)
423#elif defined(CONFIG_BOOT_16B)
424 while ((my_in_be16((unsigned short *) addr) & 0x0080) != 0x0080)
wdenkdc7c9a12003-03-26 06:55:25 +0000425#elif defined(CONFIG_BOOT_32B)
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000426 while ((my_in_be32((unsigned *) addr) & 0x00800080) != 0x00800080)
wdenkdc7c9a12003-03-26 06:55:25 +0000427#else
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000428#error CONFIG_BOOT_(size)B missing.
wdenkdc7c9a12003-03-26 06:55:25 +0000429#endif
430 {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000431 now = get_timer(start);
432 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
433 printf("Timeout\n");
wdenkdc7c9a12003-03-26 06:55:25 +0000434 return 1;
435 }
436 /* show that we're waiting */
437 if ((now - last) > 1000) { /* every second */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000438 putc('.');
wdenkdc7c9a12003-03-26 06:55:25 +0000439 last = now;
440 }
441 }
442DONE:
443 /* reset to read mode */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000444 addr = (volatile unsigned long *) info->start[0];
445
446#if defined(CONFIG_BOOT_8B)
447 my_out_8((unsigned char *) addr, 0xf0);
448#elif defined(CONFIG_BOOT_16B)
449 my_out_be16((unsigned short *) addr, 0x00f0);
wdenkdc7c9a12003-03-26 06:55:25 +0000450#elif defined(CONFIG_BOOT_32B)
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000451 my_out_be32((unsigned *) addr, 0x00F000F0); /* reset bank */
wdenkdc7c9a12003-03-26 06:55:25 +0000452#else
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000453#error CONFIG_BOOT_(size)B missing.
wdenkdc7c9a12003-03-26 06:55:25 +0000454#endif
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000455 printf(" done\n");
wdenkdc7c9a12003-03-26 06:55:25 +0000456 return 0;
457}
458
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000459/*
wdenkdc7c9a12003-03-26 06:55:25 +0000460 * Copy memory to flash, returns:
461 * 0 - OK
462 * 1 - write timeout
463 * 2 - Flash not erased
464 */
465
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000466int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
wdenkdc7c9a12003-03-26 06:55:25 +0000467{
468 ulong cp, wp, data;
469 int i, l, rc;
470
471 wp = (addr & ~3); /* get lower word aligned address */
472
473 /*
474 * handle unaligned start bytes
475 */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000476 l = addr - wp;
477
478 if (l != 0) {
wdenkdc7c9a12003-03-26 06:55:25 +0000479 data = 0;
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000480 for (i = 0, cp = wp; i < l; ++i, ++cp)
481 data = (data << 8) | (*(uchar *) cp);
482
483 for (; i < 4 && cnt > 0; ++i) {
wdenkdc7c9a12003-03-26 06:55:25 +0000484 data = (data << 8) | *src++;
485 --cnt;
486 ++cp;
487 }
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000488 for (; cnt == 0 && i < 4; ++i, ++cp)
489 data = (data << 8) | (*(uchar *) cp);
wdenkdc7c9a12003-03-26 06:55:25 +0000490
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000491 rc = write_word(info, wp, data);
492
493 if (rc != 0)
494 return rc;
495
wdenkdc7c9a12003-03-26 06:55:25 +0000496 wp += 4;
497 }
498
499 /*
500 * handle word aligned part
501 */
502 while (cnt >= 4) {
503 data = 0;
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000504 for (i = 0; i < 4; ++i)
wdenkdc7c9a12003-03-26 06:55:25 +0000505 data = (data << 8) | *src++;
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000506
507 rc = write_word(info, wp, data);
508
509 if (rc != 0)
510 return rc;
511
512 wp += 4;
wdenkdc7c9a12003-03-26 06:55:25 +0000513 cnt -= 4;
514 }
515
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000516 if (cnt == 0)
517 return 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000518
519 /*
520 * handle unaligned tail bytes
521 */
522 data = 0;
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000523 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
wdenkdc7c9a12003-03-26 06:55:25 +0000524 data = (data << 8) | *src++;
525 --cnt;
526 }
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000527 for (; i < 4; ++i, ++cp)
528 data = (data << 8) | (*(uchar *) cp);
wdenkdc7c9a12003-03-26 06:55:25 +0000529
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000530 return write_word(info, wp, data);
wdenkdc7c9a12003-03-26 06:55:25 +0000531}
532
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000533/*
wdenkdc7c9a12003-03-26 06:55:25 +0000534 * Write a word to Flash, returns:
535 * 0 - OK
536 * 1 - write timeout
537 * 2 - Flash not erased
538 */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000539static int write_word(flash_info_t *info, ulong dest, ulong data)
wdenkdc7c9a12003-03-26 06:55:25 +0000540{
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000541 ulong addr = (ulong) (info->start[0]);
Wolfgang Denk48353242011-11-04 15:55:50 +0000542 ulong start;
wdenkdc7c9a12003-03-26 06:55:25 +0000543 int flag;
544 ulong i;
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000545 int data_short[2];
wdenkdc7c9a12003-03-26 06:55:25 +0000546
547 /* Check if Flash is (sufficiently) erased */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000548 if (((ulong)*(ulong *)dest & data) != data)
549 return 2;
550
wdenkdc7c9a12003-03-26 06:55:25 +0000551 /* Disable interrupts which might cause a timeout here */
552 flag = disable_interrupts();
553#if defined(CONFIG_BOOT_8B)
554#ifdef DEBUG
555 {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000556 int in_mid, in_did;
557
558 my_out_8((unsigned char *) (addr + 0x555), 0xaa);
559 my_out_8((unsigned char *) (addr + 0x2aa), 0x55);
560 my_out_8((unsigned char *) (addr + 0x555), 0x90);
561
562 in_mid = my_in_8((unsigned char *) addr);
563 in_did = my_in_8((unsigned char *) (addr + 1));
564
565 printf(" man ID=0x%x, dev ID=0x%x.\n", in_mid, in_did);
566
567 my_out_8((unsigned char *) addr, 0xf0);
568 udelay(1);
wdenkdc7c9a12003-03-26 06:55:25 +0000569 }
570#endif
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000571 {
572 int data_ch[4];
wdenkdc7c9a12003-03-26 06:55:25 +0000573
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000574 data_ch[0] = (int) ((data >> 24) & 0xff);
575 data_ch[1] = (int) ((data >> 16) & 0xff);
576 data_ch[2] = (int) ((data >> 8) & 0xff);
577 data_ch[3] = (int) (data & 0xff);
578
579 for (i = 0; i < 4; i++) {
580 my_out_8((unsigned char *) (addr + 0x555), 0xaa);
581 my_out_8((unsigned char *) (addr + 0x2aa), 0x55);
582 my_out_8((unsigned char *) (addr + 0x555), 0xa0);
583 my_out_8((unsigned char *) (dest + i), data_ch[i]);
584
585 /* re-enable interrupts if necessary */
586 if (flag)
587 enable_interrupts();
588
589 start = get_timer(0);
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000590 while ((my_in_8((unsigned char *)(dest + i))) !=
591 (data_ch[i])) {
592 if (get_timer(start) >
593 CONFIG_SYS_FLASH_WRITE_TOUT) {
594 return 1;
595 }
596 }
597 } /* for */
wdenkdc7c9a12003-03-26 06:55:25 +0000598 }
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000599#elif defined(CONFIG_BOOT_16B)
600 data_short[0] = (int) (data >> 16) & 0xffff;
601 data_short[1] = (int) data & 0xffff;
602 for (i = 0; i < 2; i++) {
603 my_out_be16((unsigned short *)((ulong)addr + 0xaaa), 0xaa);
604 my_out_be16((unsigned short *)((ulong)addr + 0x554), 0x55);
605 my_out_be16((unsigned short *)((ulong)addr + 0xaaa), 0xa0);
606 my_out_be16((unsigned short *)(dest + (i * 2)),
607 data_short[i]);
608
609 /* re-enable interrupts if necessary */
610 if (flag)
611 enable_interrupts();
612
613 start = get_timer(0);
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000614 while ((my_in_be16((unsigned short *)(dest + (i * 2)))) !=
615 (data_short[i])) {
616 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
617 return 1;
618 }
619 }
620#elif defined(CONFIG_BOOT_32B)
wdenkdc7c9a12003-03-26 06:55:25 +0000621 addr[0x0555] = 0x00AA00AA;
622 addr[0x02AA] = 0x00550055;
623 addr[0x0555] = 0x00A000A0;
624
625 *((vu_long *)dest) = data;
626
627 /* re-enable interrupts if necessary */
628 if (flag)
629 enable_interrupts();
630
631 /* data polling for D7 */
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000632 start = get_timer(0);
wdenkdc7c9a12003-03-26 06:55:25 +0000633 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000634 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
635 return 1;
wdenkdc7c9a12003-03-26 06:55:25 +0000636 }
637#endif
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000638 return 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000639}
640
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000641#ifdef CONFIG_BOOT_8B
642static int my_in_8(unsigned char *addr)
wdenkdc7c9a12003-03-26 06:55:25 +0000643{
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000644 int ret;
645 __asm__ __volatile__("lbz%U1%X1 %0,%1; eieio":"=r"(ret):"m"(*addr));
646
647 return ret;
648}
649
650static void my_out_8(unsigned char *addr, int val)
651{
652 __asm__ __volatile__("stb%U0%X0 %1,%0; eieio":"=m"(*addr):"r"(val));
wdenkdc7c9a12003-03-26 06:55:25 +0000653}
654#endif
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000655#ifdef CONFIG_BOOT_16B
656static int my_in_be16(unsigned short *addr)
wdenkdc7c9a12003-03-26 06:55:25 +0000657{
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000658 int ret;
659 __asm__ __volatile__("lhz%U1%X1 %0,%1; eieio":"=r"(ret):"m"(*addr));
660
661 return ret;
wdenkdc7c9a12003-03-26 06:55:25 +0000662}
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000663
664static void my_out_be16(unsigned short *addr, int val)
wdenkdc7c9a12003-03-26 06:55:25 +0000665{
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000666 __asm__ __volatile__("sth%U0%X0 %1,%0; eieio":"=m"(*addr):"r"(val));
wdenkdc7c9a12003-03-26 06:55:25 +0000667}
668#endif
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000669#ifdef CONFIG_BOOT_32B
670static unsigned my_in_be32(unsigned *addr)
wdenkdc7c9a12003-03-26 06:55:25 +0000671{
wdenk8bde7f72003-06-27 21:31:46 +0000672 unsigned ret;
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000673 __asm__ __volatile__("lwz%U1%X1 %0,%1; eieio":"=r"(ret):"m"(*addr));
674
675 return ret;
wdenkdc7c9a12003-03-26 06:55:25 +0000676}
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000677
678static void my_out_be32(unsigned *addr, int val)
wdenkdc7c9a12003-03-26 06:55:25 +0000679{
Wolfgang Denk8adc9052011-11-04 15:55:49 +0000680 __asm__ __volatile__("stw%U0%X0 %1,%0; eieio":"=m"(*addr):"r"(val));
wdenkdc7c9a12003-03-26 06:55:25 +0000681}
682#endif