deadbat-alternate.patch
[u-boot-openmoko/mini2440.git] / board / hermes / flash.c
blob799fe83f3e6c46139bbddf3cc1daae8c31ec327c
1 /*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
24 #include <common.h>
25 #include <mpc8xx.h>
27 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
29 /*-----------------------------------------------------------------------
30 * Functions
32 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
33 static int write_byte (flash_info_t *info, ulong dest, uchar data);
34 static void flash_get_offsets (ulong base, flash_info_t *info);
36 /*-----------------------------------------------------------------------
39 unsigned long flash_init (void)
41 volatile immap_t *immap = (immap_t *)CFG_IMMR;
42 volatile memctl8xx_t *memctl = &immap->im_memctl;
43 unsigned long size;
44 int i;
46 /* Init: no FLASHes known */
47 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
48 flash_info[i].flash_id = FLASH_UNKNOWN;
51 /* Static FLASH Bank configuration here - FIXME XXX */
53 size = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
55 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
56 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
57 size, size<<20);
60 /* Remap FLASH according to real size */
61 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size & 0xFFFF8000);
62 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) |
63 (memctl->memc_br0 & ~(BR_BA_MSK));
65 /* Re-do sizing to get full correct info */
66 size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
68 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
70 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
71 /* monitor protection ON by default */
72 flash_protect(FLAG_PROTECT_SET,
73 CFG_MONITOR_BASE,
74 CFG_MONITOR_BASE+monitor_flash_len-1,
75 &flash_info[0]);
76 #endif
78 flash_info[0].size = size;
80 return (size);
83 /*-----------------------------------------------------------------------
85 static void flash_get_offsets (ulong base, flash_info_t *info)
87 int i;
89 /* set up sector start address table */
90 if (info->flash_id & FLASH_BTYPE) {
91 /* set sector offsets for bottom boot block type */
92 info->start[0] = base + 0x00000000;
93 info->start[1] = base + 0x00004000;
94 info->start[2] = base + 0x00006000;
95 info->start[3] = base + 0x00008000;
96 for (i = 4; i < info->sector_count; i++) {
97 info->start[i] = base + (i * 0x00010000) - 0x00030000;
99 } else {
100 /* set sector offsets for top boot block type */
101 i = info->sector_count - 1;
102 info->start[i--] = base + info->size - 0x00004000;
103 info->start[i--] = base + info->size - 0x00006000;
104 info->start[i--] = base + info->size - 0x00008000;
105 for (; i >= 0; i--) {
106 info->start[i] = base + i * 0x00010000;
112 /*-----------------------------------------------------------------------
114 void flash_print_info (flash_info_t *info)
116 int i;
118 if (info->flash_id == FLASH_UNKNOWN) {
119 printf ("missing or unknown FLASH type\n");
120 return;
123 switch (info->flash_id & FLASH_VENDMASK) {
124 case FLASH_MAN_AMD: printf ("AMD "); break;
125 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
126 default: printf ("Unknown Vendor "); break;
129 switch (info->flash_id & FLASH_TYPEMASK) {
130 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
131 break;
132 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
133 break;
134 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
135 break;
136 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
137 break;
138 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
139 break;
140 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
141 break;
142 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
143 break;
144 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
145 break;
146 default: printf ("Unknown Chip Type\n");
147 break;
150 printf (" Size: %ld MB in %d Sectors\n",
151 info->size >> 20, info->sector_count);
153 printf (" Sector Start Addresses:");
154 for (i=0; i<info->sector_count; ++i) {
155 if ((i % 5) == 0)
156 printf ("\n ");
157 printf (" %08lX%s",
158 info->start[i],
159 info->protect[i] ? " (RO)" : " "
162 printf ("\n");
163 return;
166 /*-----------------------------------------------------------------------
170 /*-----------------------------------------------------------------------
174 * The following code cannot be run from FLASH!
177 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
179 short i;
180 uchar value;
181 vu_char *caddr = (vu_char *)addr;
182 ulong base = (ulong)addr;
185 /* Write auto select command: read Manufacturer ID */
186 caddr[0x0AAA] = 0xAA;
187 caddr[0x0555] = 0x55;
188 caddr[0x0AAA] = 0x90;
190 value = caddr[0];
191 switch (value) {
192 case (AMD_MANUFACT & 0xFF):
193 info->flash_id = FLASH_MAN_AMD;
194 break;
195 case (FUJ_MANUFACT & 0xFF):
196 info->flash_id = FLASH_MAN_FUJ;
197 break;
198 default:
199 info->flash_id = FLASH_UNKNOWN;
200 info->sector_count = 0;
201 info->size = 0;
202 return (0); /* no or unknown flash */
205 value = caddr[2]; /* device ID */
207 switch (value) {
208 case (AMD_ID_LV400T & 0xFF):
209 info->flash_id += FLASH_AM400T;
210 info->sector_count = 11;
211 info->size = 0x00080000;
212 break; /* => 512 kB */
214 case (AMD_ID_LV400B & 0xFF):
215 info->flash_id += FLASH_AM400B;
216 info->sector_count = 11;
217 info->size = 0x00080000;
218 break; /* => 512 kB */
220 case (AMD_ID_LV800T & 0xFF):
221 info->flash_id += FLASH_AM800T;
222 info->sector_count = 19;
223 info->size = 0x00100000;
224 break; /* => 1 MB */
226 case (AMD_ID_LV800B & 0xFF):
227 info->flash_id += FLASH_AM800B;
228 info->sector_count = 19;
229 info->size = 0x00100000;
230 break; /* => 1 MB */
232 case (AMD_ID_LV160T & 0xFF):
233 info->flash_id += FLASH_AM160T;
234 info->sector_count = 35;
235 info->size = 0x00200000;
236 break; /* => 2 MB */
238 case (AMD_ID_LV160B & 0xFF):
239 info->flash_id += FLASH_AM160B;
240 info->sector_count = 35;
241 info->size = 0x00200000;
242 break; /* => 2 MB */
243 #if 0 /* enable when device IDs are available */
244 case (AMD_ID_LV320T & 0xFF):
245 info->flash_id += FLASH_AM320T;
246 info->sector_count = 67;
247 info->size = 0x00400000;
248 break; /* => 4 MB */
250 case (AMD_ID_LV320B & 0xFF):
251 info->flash_id += FLASH_AM320B;
252 info->sector_count = 67;
253 info->size = 0x00400000;
254 break; /* => 4 MB */
255 #endif
256 default:
257 info->flash_id = FLASH_UNKNOWN;
258 return (0); /* => no or unknown flash */
262 /* set up sector start address table */
263 if (info->flash_id & FLASH_BTYPE) {
264 /* set sector offsets for bottom boot block type */
265 info->start[0] = base + 0x00000000;
266 info->start[1] = base + 0x00004000;
267 info->start[2] = base + 0x00006000;
268 info->start[3] = base + 0x00008000;
269 for (i = 4; i < info->sector_count; i++) {
270 info->start[i] = base + (i * 0x00010000) - 0x00030000;
272 } else {
273 /* set sector offsets for top boot block type */
274 i = info->sector_count - 1;
275 info->start[i--] = base + info->size - 0x00004000;
276 info->start[i--] = base + info->size - 0x00006000;
277 info->start[i--] = base + info->size - 0x00008000;
278 for (; i >= 0; i--) {
279 info->start[i] = base + i * 0x00010000;
283 /* check for protected sectors */
284 for (i = 0; i < info->sector_count; i++) {
285 /* read sector protection: D0 = 1 if protected */
286 caddr = (volatile unsigned char *)(info->start[i]);
287 info->protect[i] = caddr[4] & 1;
291 * Prevent writes to uninitialized FLASH.
293 if (info->flash_id != FLASH_UNKNOWN) {
294 caddr = (vu_char *)info->start[0];
296 *caddr = 0xF0; /* reset bank */
299 return (info->size);
303 /*-----------------------------------------------------------------------
306 int flash_erase (flash_info_t *info, int s_first, int s_last)
308 vu_char *addr = (vu_char*)(info->start[0]);
309 int flag, prot, sect, l_sect;
310 ulong start, now, last;
312 if ((s_first < 0) || (s_first > s_last)) {
313 if (info->flash_id == FLASH_UNKNOWN) {
314 printf ("- missing\n");
315 } else {
316 printf ("- no sectors to erase\n");
318 return 1;
321 if ((info->flash_id == FLASH_UNKNOWN) ||
322 (info->flash_id > FLASH_AMD_COMP)) {
323 printf ("Can't erase unknown flash type %08lx - aborted\n",
324 info->flash_id);
325 return 1;
328 prot = 0;
329 for (sect=s_first; sect<=s_last; ++sect) {
330 if (info->protect[sect]) {
331 prot++;
335 if (prot) {
336 printf ("- Warning: %d protected sectors will not be erased!\n",
337 prot);
338 } else {
339 printf ("\n");
342 l_sect = -1;
344 /* Disable interrupts which might cause a timeout here */
345 flag = disable_interrupts();
347 addr[0x0AAA] = 0xAA;
348 addr[0x0555] = 0x55;
349 addr[0x0AAA] = 0x80;
350 addr[0x0AAA] = 0xAA;
351 addr[0x0555] = 0x55;
353 /* Start erase on unprotected sectors */
354 for (sect = s_first; sect<=s_last; sect++) {
355 if (info->protect[sect] == 0) { /* not protected */
356 addr = (vu_char*)(info->start[sect]);
357 addr[0] = 0x30;
358 l_sect = sect;
362 /* re-enable interrupts if necessary */
363 if (flag)
364 enable_interrupts();
366 /* wait at least 80us - let's wait 1 ms */
367 udelay (1000);
370 * We wait for the last triggered sector
372 if (l_sect < 0)
373 goto DONE;
375 start = get_timer (0);
376 last = start;
377 addr = (vu_char*)(info->start[l_sect]);
378 while ((addr[0] & 0x80) != 0x80) {
379 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
380 printf ("Timeout\n");
381 return 1;
383 /* show that we're waiting */
384 if ((now - last) > 1000) { /* every second */
385 putc ('.');
386 last = now;
390 DONE:
391 /* reset to read mode */
392 addr = (vu_char *)info->start[0];
393 addr[0] = 0xF0; /* reset bank */
395 printf (" done\n");
396 return 0;
399 /*-----------------------------------------------------------------------
400 * Copy memory to flash, returns:
401 * 0 - OK
402 * 1 - write timeout
403 * 2 - Flash not erased
406 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
408 int rc;
410 while (cnt > 0) {
411 if ((rc = write_byte(info, addr++, *src++)) != 0) {
412 return (rc);
414 --cnt;
417 return (0);
420 /*-----------------------------------------------------------------------
421 * Write a word to Flash, returns:
422 * 0 - OK
423 * 1 - write timeout
424 * 2 - Flash not erased
426 static int write_byte (flash_info_t *info, ulong dest, uchar data)
428 vu_char *addr = (vu_char*)(info->start[0]);
429 ulong start;
430 int flag;
432 /* Check if Flash is (sufficiently) erased */
433 if ((*((vu_char *)dest) & data) != data) {
434 return (2);
436 /* Disable interrupts which might cause a timeout here */
437 flag = disable_interrupts();
439 addr[0x0AAA] = 0xAA;
440 addr[0x0555] = 0x55;
441 addr[0x0AAA] = 0xA0;
443 *((vu_char *)dest) = data;
445 /* re-enable interrupts if necessary */
446 if (flag)
447 enable_interrupts();
449 /* data polling for D7 */
450 start = get_timer (0);
451 while ((*((vu_char *)dest) & 0x80) != (data & 0x80)) {
452 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
453 return (1);
456 return (0);
459 /*-----------------------------------------------------------------------