3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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,
27 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
29 /*-----------------------------------------------------------------------
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 *)CONFIG_SYS_IMMR;
42 volatile memctl8xx_t *memctl = &immap->im_memctl;
46 /* Init: no FLASHes known */
47 for (i=0; i<CONFIG_SYS_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",
60 /* Remap FLASH according to real size */
61 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size & 0xFFFF8000);
62 memctl->memc_br0 = (CONFIG_SYS_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 *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
68 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
70 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
71 /* monitor protection ON by default */
72 flash_protect(FLAG_PROTECT_SET,
73 CONFIG_SYS_MONITOR_BASE,
74 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
78 flash_info[0].size = size;
83 /*-----------------------------------------------------------------------
85 static void flash_get_offsets (ulong base, flash_info_t *info)
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;
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)
118 if (info->flash_id == FLASH_UNKNOWN) {
119 printf ("missing or unknown FLASH type\n");
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");
132 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
134 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
136 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
138 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
140 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
142 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
144 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
146 default: printf ("Unknown Chip Type\n");
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) {
159 info->protect[i] ? " (RO)" : " "
166 /*-----------------------------------------------------------------------
170 /*-----------------------------------------------------------------------
174 * The following code cannot be run from FLASH!
177 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
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;
192 case (AMD_MANUFACT & 0xFF):
193 info->flash_id = FLASH_MAN_AMD;
195 case (FUJ_MANUFACT & 0xFF):
196 info->flash_id = FLASH_MAN_FUJ;
199 info->flash_id = FLASH_UNKNOWN;
200 info->sector_count = 0;
202 return (0); /* no or unknown flash */
205 value = caddr[2]; /* device ID */
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;
226 case (AMD_ID_LV800B & 0xFF):
227 info->flash_id += FLASH_AM800B;
228 info->sector_count = 19;
229 info->size = 0x00100000;
232 case (AMD_ID_LV160T & 0xFF):
233 info->flash_id += FLASH_AM160T;
234 info->sector_count = 35;
235 info->size = 0x00200000;
238 case (AMD_ID_LV160B & 0xFF):
239 info->flash_id += FLASH_AM160B;
240 info->sector_count = 35;
241 info->size = 0x00200000;
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;
250 case (AMD_ID_LV320B & 0xFF):
251 info->flash_id += FLASH_AM320B;
252 info->sector_count = 67;
253 info->size = 0x00400000;
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;
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 */
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");
316 printf ("- no sectors to erase\n");
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",
329 for (sect=s_first; sect<=s_last; ++sect) {
330 if (info->protect[sect]) {
336 printf ("- Warning: %d protected sectors will not be erased!\n",
344 /* Disable interrupts which might cause a timeout here */
345 flag = disable_interrupts();
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]);
362 /* re-enable interrupts if necessary */
366 /* wait at least 80us - let's wait 1 ms */
370 * We wait for the last triggered sector
375 start = get_timer (0);
377 addr = (vu_char*)(info->start[l_sect]);
378 while ((addr[0] & 0x80) != 0x80) {
379 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
380 printf ("Timeout\n");
383 /* show that we're waiting */
384 if ((now - last) > 1000) { /* every second */
391 /* reset to read mode */
392 addr = (vu_char *)info->start[0];
393 addr[0] = 0xF0; /* reset bank */
399 /*-----------------------------------------------------------------------
400 * Copy memory to flash, returns:
403 * 2 - Flash not erased
406 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
411 if ((rc = write_byte(info, addr++, *src++)) != 0) {
420 /*-----------------------------------------------------------------------
421 * Write a word to Flash, returns:
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]);
432 /* Check if Flash is (sufficiently) erased */
433 if ((*((vu_char *)dest) & data) != data) {
436 /* Disable interrupts which might cause a timeout here */
437 flag = disable_interrupts();
443 *((vu_char *)dest) = data;
445 /* re-enable interrupts if necessary */
449 /* data polling for D7 */
450 start = get_timer (0);
451 while ((*((vu_char *)dest) & 0x80) != (data & 0x80)) {
452 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
459 /*-----------------------------------------------------------------------