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,
26 #include <linux/byteorder/swab.h>
28 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
30 /*-----------------------------------------------------------------------
33 #define FLAG_PROTECT_SET 0x01
34 #define FLAG_PROTECT_CLEAR 0x02
36 /* Board support for 1 or 2 flash devices */
37 #undef FLASH_PORT_WIDTH32
38 #define FLASH_PORT_WIDTH16
40 #ifdef FLASH_PORT_WIDTH16
41 #define FLASH_PORT_WIDTH ushort
42 #define FLASH_PORT_WIDTHV vu_short
43 #define SWAP(x) __swab16(x)
45 #define FLASH_PORT_WIDTH ulong
46 #define FLASH_PORT_WIDTHV vu_long
47 #define SWAP(x) __swab32(x)
50 #define FPW FLASH_PORT_WIDTH
51 #define FPWV FLASH_PORT_WIDTHV
53 /*-----------------------------------------------------------------------
56 static ulong flash_get_size (FPW *addr, flash_info_t *info);
57 static int write_data (flash_info_t *info, ulong dest, FPW data);
58 static void flash_get_offsets (ulong base, flash_info_t *info);
60 /*-----------------------------------------------------------------------
63 unsigned long flash_init (void)
65 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
66 volatile memctl8xx_t *memctl = &immap->im_memctl;
67 unsigned long size_b0;
70 /* Init: no FLASHes known */
71 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
72 flash_info[i].flash_id = FLASH_UNKNOWN;
75 /* Static FLASH Bank configuration here - FIXME XXX */
76 size_b0 = flash_get_size((FPW *)FLASH_BASE0_PRELIM, &flash_info[0]);
78 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
79 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
80 size_b0, size_b0<<20);
83 /* Remap FLASH according to real size */
84 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
85 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V;
87 /* Re-do sizing to get full correct info */
88 size_b0 = flash_get_size((FPW *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
90 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
92 /* monitor protection ON by default */
93 (void)flash_protect(FLAG_PROTECT_SET,
94 CONFIG_SYS_FLASH_BASE,
95 CONFIG_SYS_FLASH_BASE+monitor_flash_len-1,
98 flash_info[0].size = size_b0;
103 /*-----------------------------------------------------------------------
105 static void flash_get_offsets (ulong base, flash_info_t *info)
109 if (info->flash_id == FLASH_UNKNOWN) {
113 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
114 for (i = 0; i < info->sector_count; i++) {
115 info->start[i] = base + (i * 0x00020000);
120 /*-----------------------------------------------------------------------
122 void flash_print_info (flash_info_t *info)
126 if (info->flash_id == FLASH_UNKNOWN) {
127 printf ("missing or unknown FLASH type\n");
131 switch (info->flash_id & FLASH_VENDMASK) {
132 case FLASH_MAN_INTEL: printf ("INTEL "); break;
133 default: printf ("Unknown Vendor "); break;
136 switch (info->flash_id & FLASH_TYPEMASK) {
137 case FLASH_28F640J5 :
138 printf ("28F640J5 \n"); break;
139 default: printf ("Unknown Chip Type=0x%lXh\n",
140 info->flash_id & FLASH_TYPEMASK); break;
143 printf (" Size: %ld MB in %d Sectors\n",
144 info->size >> 20, info->sector_count);
146 printf (" Sector Start Addresses:");
147 for (i=0; i<info->sector_count; ++i) {
152 info->protect[i] ? " (RO)" : " "
158 /*-----------------------------------------------------------------------
162 /*-----------------------------------------------------------------------
166 * The following code cannot be run from FLASH!
169 static ulong flash_get_size (FPW *addr, flash_info_t *info)
173 /* Write auto select command: read Manufacturer ID */
174 addr[0x5555] = (FPW)0xAA00AA00;
175 addr[0x2AAA] = (FPW)0x55005500;
176 addr[0x5555] = (FPW)0x90009000;
178 value = SWAP(addr[0]);
181 case (FPW)INTEL_MANUFACT:
182 info->flash_id = FLASH_MAN_INTEL;
185 info->flash_id = FLASH_UNKNOWN;
186 info->sector_count = 0;
188 addr[0] = (FPW)0xFF00FF00; /* restore read mode */
189 return (0); /* no or unknown flash */
192 value = SWAP(addr[1]); /* device ID no swap !*/
195 case (FPW)INTEL_ID_28F640J5 :
196 info->flash_id += FLASH_28F640J5 ;
197 info->sector_count = 64;
198 info->size = 0x00800000;
202 info->flash_id = FLASH_UNKNOWN;
206 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
207 printf ("** ERROR: sector count %d > max (%d) **\n",
208 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
209 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
212 addr[0] = (FPW)0xFF00FF00; /* restore read mode */
218 /*-----------------------------------------------------------------------
221 int flash_erase (flash_info_t *info, int s_first, int s_last)
223 int flag, prot, sect;
224 ulong type, start, now, last;
227 if ((s_first < 0) || (s_first > s_last)) {
228 if (info->flash_id == FLASH_UNKNOWN) {
229 printf ("- missing\n");
231 printf ("- no sectors to erase\n");
236 type = (info->flash_id & FLASH_VENDMASK);
237 if ((type != FLASH_MAN_INTEL)) {
238 printf ("Can't erase unknown flash type %08lx - aborted\n",
244 for (sect=s_first; sect<=s_last; ++sect) {
245 if (info->protect[sect]) {
251 printf ("- Warning: %d protected sectors will not be erased!\n",
257 start = get_timer (0);
259 /* Start erase on unprotected sectors */
260 for (sect = s_first; sect<=s_last; sect++) {
261 if (info->protect[sect] == 0) { /* not protected */
262 FPWV *addr = (FPWV *)(info->start[sect]);
265 /* Disable interrupts which might cause a timeout here */
266 flag = disable_interrupts();
268 *addr = (FPW)0x50005000; /* clear status register */
269 *addr = (FPW)0x20002000; /* erase setup */
270 *addr = (FPW)0xD000D000; /* erase confirm */
272 /* re-enable interrupts if necessary */
276 /* wait at least 80us - let's wait 1 ms */
279 while (((status = SWAP(*addr)) & (FPW)0x00800080) != (FPW)0x00800080) {
280 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
281 printf ("Timeout\n");
282 *addr = (FPW)0xB000B000; /* suspend erase */
283 *addr = (FPW)0xFF00FF00; /* reset to read mode */
288 /* show that we're waiting */
289 if ((now - last) > 1000) { /* every second */
295 *addr = (FPW)0xFF00FF00; /* reset to read mode */
302 /*-----------------------------------------------------------------------
303 * Copy memory to flash, returns:
306 * 2 - Flash not erased
307 * 4 - Flash not identified
310 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
314 int count, i, l, rc, port_width;
316 if (info->flash_id == FLASH_UNKNOWN) {
319 /* get lower word aligned address */
320 #ifdef FLASH_PORT_WIDTH16
329 * handle unaligned start bytes
331 if ((l = addr - wp) != 0) {
333 for (i=0, cp=wp; i<l; ++i, ++cp) {
334 data = (data << 8) | (*(uchar *)cp);
336 for (; i<port_width && cnt>0; ++i) {
337 data = (data << 8) | *src++;
341 for (; cnt==0 && i<port_width; ++i, ++cp) {
342 data = (data << 8) | (*(uchar *)cp);
345 if ((rc = write_data(info, wp, data)) != 0) {
352 * handle word aligned part
355 while (cnt >= port_width) {
357 for (i=0; i<port_width; ++i) {
358 data = (data << 8) | *src++;
360 if ((rc = write_data(info, wp, data)) != 0) {
365 if ((wp & 0xfff) == 0)
377 * handle unaligned tail bytes
380 for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) {
381 data = (data << 8) | *src++;
384 for (; i<port_width; ++i, ++cp) {
385 data = (data << 8) | (*(uchar *)cp);
388 return (write_data(info, wp, data));
391 /*-----------------------------------------------------------------------
392 * Write a word or halfword to Flash, returns:
395 * 2 - Flash not erased
397 static int write_data (flash_info_t *info, ulong dest, FPW data)
399 FPWV *addr = (FPWV *)dest;
404 /* Check if Flash is (sufficiently) erased */
405 if ((*addr & data) != data) {
406 printf("not erased at %08lx (%x)\n",(ulong)addr,*addr);
409 /* Disable interrupts which might cause a timeout here */
410 flag = disable_interrupts();
412 *addr = (FPW)0x40004000; /* write setup */
415 /* re-enable interrupts if necessary */
419 start = get_timer (0);
421 while (((status = SWAP(*addr)) & (FPW)0x00800080) != (FPW)0x00800080) {
422 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
423 *addr = (FPW)0xFF00FF00; /* restore read mode */
428 *addr = (FPW)0xFF00FF00; /* restore read mode */