3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 DECLARE_GLOBAL_DATA_PTR;
32 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
34 /*-----------------------------------------------------------------------
37 #define FLAG_PROTECT_SET 0x01
38 #define FLAG_PROTECT_CLEAR 0x02
40 /* Board support for 1 or 2 flash devices */
41 #undef FLASH_PORT_WIDTH32
42 #define FLASH_PORT_WIDTH16
44 #ifdef FLASH_PORT_WIDTH16
45 #define FLASH_PORT_WIDTH ushort
46 #define FLASH_PORT_WIDTHV vu_short
48 #define FLASH_PORT_WIDTH ulong
49 #define FLASH_PORT_WIDTHV vu_long
52 #define FPW FLASH_PORT_WIDTH
53 #define FPWV FLASH_PORT_WIDTHV
55 /*-----------------------------------------------------------------------
58 static ulong flash_get_size (FPW *addr, flash_info_t *info);
59 static int write_data (flash_info_t *info, ulong dest, FPW data);
60 static void flash_get_offsets (ulong base, flash_info_t *info);
62 /*-----------------------------------------------------------------------
65 unsigned long flash_init (void)
67 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
68 volatile memctl8xx_t *memctl = &immap->im_memctl;
69 unsigned long size_b0;
72 /* Init: no FLASHes known */
73 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
74 flash_info[i].flash_id = FLASH_UNKNOWN;
77 /* Static FLASH Bank configuration here - FIXME XXX */
78 size_b0 = flash_get_size((FPW *)FLASH_BASE0_PRELIM, &flash_info[0]);
80 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
81 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
82 size_b0, size_b0<<20);
85 /* Remap FLASH according to real size */
86 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
87 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V;
89 /* Re-do sizing to get full correct info */
90 size_b0 = flash_get_size((FPW *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
92 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
94 /* monitor protection ON by default */
95 (void)flash_protect(FLAG_PROTECT_SET,
96 CONFIG_SYS_FLASH_BASE,
97 CONFIG_SYS_FLASH_BASE+monitor_flash_len-1,
100 flash_info[0].size = size_b0;
105 /*-----------------------------------------------------------------------
107 static void flash_get_offsets (ulong base, flash_info_t *info)
111 if (info->flash_id == FLASH_UNKNOWN) {
115 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
116 for (i = 0; i < info->sector_count; i++) {
117 info->start[i] = base + (i * 0x00020000);
122 /*-----------------------------------------------------------------------
124 void flash_print_info (flash_info_t *info)
128 if (info->flash_id == FLASH_UNKNOWN) {
129 printf ("missing or unknown FLASH type\n");
133 switch (info->flash_id & FLASH_VENDMASK) {
134 case FLASH_MAN_INTEL: printf ("INTEL "); break;
135 default: printf ("Unknown Vendor "); break;
138 switch (info->flash_id & FLASH_TYPEMASK) {
139 case FLASH_28F320J3A:
140 printf ("28F320J3A\n"); break;
141 case FLASH_28F640J3A:
142 printf ("28F640J3A\n"); break;
143 case FLASH_28F128J3A:
144 printf ("28F128J3A\n"); break;
145 default: printf ("Unknown Chip Type\n"); break;
148 printf (" Size: %ld MB in %d Sectors\n",
149 info->size >> 20, info->sector_count);
151 printf (" Sector Start Addresses:");
152 for (i=0; i<info->sector_count; ++i) {
157 info->protect[i] ? " (RO)" : " "
164 /*-----------------------------------------------------------------------
168 /*-----------------------------------------------------------------------
172 * The following code cannot be run from FLASH!
175 static ulong flash_get_size (FPW *addr, flash_info_t *info)
179 /* Write auto select command: read Manufacturer ID */
180 addr[0x5555] = (FPW)0x00AA00AA;
181 addr[0x2AAA] = (FPW)0x00550055;
182 addr[0x5555] = (FPW)0x00900090;
187 case (FPW)INTEL_MANUFACT:
188 info->flash_id = FLASH_MAN_INTEL;
191 info->flash_id = FLASH_UNKNOWN;
192 info->sector_count = 0;
194 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
195 return (0); /* no or unknown flash */
198 value = addr[1]; /* device ID */
201 case (FPW)INTEL_ID_28F320J3A:
202 info->flash_id += FLASH_28F320J3A;
203 info->sector_count = 32;
204 info->size = 0x00400000;
207 case (FPW)INTEL_ID_28F640J3A:
208 info->flash_id += FLASH_28F640J3A;
209 info->sector_count = 64;
210 info->size = 0x00800000;
213 case (FPW)INTEL_ID_28F128J3A:
214 info->flash_id += FLASH_28F128J3A;
215 info->sector_count = 128;
216 info->size = 0x01000000;
217 break; /* => 16 MB */
220 info->flash_id = FLASH_UNKNOWN;
224 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
225 printf ("** ERROR: sector count %d > max (%d) **\n",
226 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
227 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
230 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
236 /*-----------------------------------------------------------------------
239 int flash_erase (flash_info_t *info, int s_first, int s_last)
241 int flag, prot, sect;
242 ulong type, start, now, last;
245 if ((s_first < 0) || (s_first > s_last)) {
246 if (info->flash_id == FLASH_UNKNOWN) {
247 printf ("- missing\n");
249 printf ("- no sectors to erase\n");
254 type = (info->flash_id & FLASH_VENDMASK);
255 if ((type != FLASH_MAN_INTEL)) {
256 printf ("Can't erase unknown flash type %08lx - aborted\n",
262 for (sect=s_first; sect<=s_last; ++sect) {
263 if (info->protect[sect]) {
269 printf ("- Warning: %d protected sectors will not be erased!\n",
275 start = get_timer (0);
277 /* Start erase on unprotected sectors */
278 for (sect = s_first; sect<=s_last; sect++) {
279 if (info->protect[sect] == 0) { /* not protected */
280 FPWV *addr = (FPWV *)(info->start[sect]);
283 /* Disable interrupts which might cause a timeout here */
284 flag = disable_interrupts();
286 *addr = (FPW)0x00500050; /* clear status register */
287 *addr = (FPW)0x00200020; /* erase setup */
288 *addr = (FPW)0x00D000D0; /* erase confirm */
290 /* re-enable interrupts if necessary */
294 /* wait at least 80us - let's wait 1 ms */
297 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
298 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
299 printf ("Timeout\n");
300 *addr = (FPW)0x00B000B0; /* suspend erase */
301 *addr = (FPW)0x00FF00FF; /* reset to read mode */
306 /* show that we're waiting */
307 if ((now - last) > 1000) { /* every second */
313 *addr = (FPW)0x00FF00FF; /* reset to read mode */
320 /*-----------------------------------------------------------------------
321 * Copy memory to flash, returns:
324 * 2 - Flash not erased
325 * 4 - Flash not identified
328 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
332 int count, i, l, rc, port_width;
334 if (info->flash_id == FLASH_UNKNOWN) {
337 /* get lower word aligned address */
338 #ifdef FLASH_PORT_WIDTH16
346 /* save sernum if needed */
347 if (addr >= CONFIG_SYS_FLASH_SN_SECTOR && addr < CONFIG_SYS_FLASH_SN_BASE)
349 u_long dest = CONFIG_SYS_FLASH_SN_BASE;
350 u_short *sn = (u_short *)gd->bd->bi_sernum;
352 printf("(saving sernum)");
355 if ((rc = write_data(info, dest, sn[i])) != 0) {
363 * handle unaligned start bytes
365 if ((l = addr - wp) != 0) {
367 for (i=0, cp=wp; i<l; ++i, ++cp) {
368 data = (data << 8) | (*(uchar *)cp);
370 for (; i<port_width && cnt>0; ++i) {
371 data = (data << 8) | *src++;
375 for (; cnt==0 && i<port_width; ++i, ++cp) {
376 data = (data << 8) | (*(uchar *)cp);
379 if ((rc = write_data(info, wp, data)) != 0) {
386 * handle word aligned part
389 while (cnt >= port_width) {
391 for (i=0; i<port_width; ++i) {
392 data = (data << 8) | *src++;
394 if ((rc = write_data(info, wp, data)) != 0) {
411 * handle unaligned tail bytes
414 for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) {
415 data = (data << 8) | *src++;
418 for (; i<port_width; ++i, ++cp) {
419 data = (data << 8) | (*(uchar *)cp);
422 return (write_data(info, wp, data));
425 /*-----------------------------------------------------------------------
426 * Write a word or halfword to Flash, returns:
429 * 2 - Flash not erased
431 static int write_data (flash_info_t *info, ulong dest, FPW data)
433 FPWV *addr = (FPWV *)dest;
438 /* Check if Flash is (sufficiently) erased */
439 if ((*addr & data) != data) {
440 printf("not erased at %08lx (%x)\n",(ulong)addr,*addr);
443 /* Disable interrupts which might cause a timeout here */
444 flag = disable_interrupts();
446 *addr = (FPW)0x00400040; /* write setup */
449 /* re-enable interrupts if necessary */
453 start = get_timer (0);
455 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
456 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
457 *addr = (FPW)0x00FF00FF; /* restore read mode */
462 *addr = (FPW)0x00FF00FF; /* restore read mode */