3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
14 DECLARE_GLOBAL_DATA_PTR;
16 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
18 /*-----------------------------------------------------------------------
21 #define FLAG_PROTECT_SET 0x01
22 #define FLAG_PROTECT_CLEAR 0x02
24 /* Board support for 1 or 2 flash devices */
25 #undef FLASH_PORT_WIDTH32
26 #define FLASH_PORT_WIDTH16
28 #ifdef FLASH_PORT_WIDTH16
29 #define FLASH_PORT_WIDTH ushort
30 #define FLASH_PORT_WIDTHV vu_short
32 #define FLASH_PORT_WIDTH ulong
33 #define FLASH_PORT_WIDTHV vu_long
36 #define FPW FLASH_PORT_WIDTH
37 #define FPWV FLASH_PORT_WIDTHV
39 /*-----------------------------------------------------------------------
42 static ulong flash_get_size (FPW *addr, flash_info_t *info);
43 static int write_data (flash_info_t *info, ulong dest, FPW data);
44 static void flash_get_offsets (ulong base, flash_info_t *info);
46 /*-----------------------------------------------------------------------
49 unsigned long flash_init (void)
51 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
52 volatile memctl8xx_t *memctl = &immap->im_memctl;
53 unsigned long size_b0;
56 /* Init: no FLASHes known */
57 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
58 flash_info[i].flash_id = FLASH_UNKNOWN;
61 /* Static FLASH Bank configuration here - FIXME XXX */
62 size_b0 = flash_get_size((FPW *)FLASH_BASE0_PRELIM, &flash_info[0]);
64 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
65 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
66 size_b0, size_b0<<20);
69 /* Remap FLASH according to real size */
70 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
71 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V;
73 /* Re-do sizing to get full correct info */
74 size_b0 = flash_get_size((FPW *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
76 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
78 /* monitor protection ON by default */
79 (void)flash_protect(FLAG_PROTECT_SET,
80 CONFIG_SYS_FLASH_BASE,
81 CONFIG_SYS_FLASH_BASE+monitor_flash_len-1,
84 flash_info[0].size = size_b0;
89 /*-----------------------------------------------------------------------
91 static void flash_get_offsets (ulong base, flash_info_t *info)
95 if (info->flash_id == FLASH_UNKNOWN) {
99 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
100 for (i = 0; i < info->sector_count; i++) {
101 info->start[i] = base + (i * 0x00020000);
106 /*-----------------------------------------------------------------------
108 void flash_print_info (flash_info_t *info)
112 if (info->flash_id == FLASH_UNKNOWN) {
113 printf ("missing or unknown FLASH type\n");
117 switch (info->flash_id & FLASH_VENDMASK) {
118 case FLASH_MAN_INTEL: printf ("INTEL "); break;
119 default: printf ("Unknown Vendor "); break;
122 switch (info->flash_id & FLASH_TYPEMASK) {
123 case FLASH_28F320J3A:
124 printf ("28F320J3A\n"); break;
125 case FLASH_28F640J3A:
126 printf ("28F640J3A\n"); break;
127 case FLASH_28F128J3A:
128 printf ("28F128J3A\n"); break;
129 default: printf ("Unknown Chip Type\n"); break;
132 printf (" Size: %ld MB in %d Sectors\n",
133 info->size >> 20, info->sector_count);
135 printf (" Sector Start Addresses:");
136 for (i=0; i<info->sector_count; ++i) {
141 info->protect[i] ? " (RO)" : " "
148 /*-----------------------------------------------------------------------
152 /*-----------------------------------------------------------------------
156 * The following code cannot be run from FLASH!
159 static ulong flash_get_size (FPW *addr, flash_info_t *info)
163 /* Write auto select command: read Manufacturer ID */
164 addr[0x5555] = (FPW)0x00AA00AA;
165 addr[0x2AAA] = (FPW)0x00550055;
166 addr[0x5555] = (FPW)0x00900090;
171 case (FPW)INTEL_MANUFACT:
172 info->flash_id = FLASH_MAN_INTEL;
175 info->flash_id = FLASH_UNKNOWN;
176 info->sector_count = 0;
178 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
179 return (0); /* no or unknown flash */
182 value = addr[1]; /* device ID */
185 case (FPW)INTEL_ID_28F320J3A:
186 info->flash_id += FLASH_28F320J3A;
187 info->sector_count = 32;
188 info->size = 0x00400000;
191 case (FPW)INTEL_ID_28F640J3A:
192 info->flash_id += FLASH_28F640J3A;
193 info->sector_count = 64;
194 info->size = 0x00800000;
197 case (FPW)INTEL_ID_28F128J3A:
198 info->flash_id += FLASH_28F128J3A;
199 info->sector_count = 128;
200 info->size = 0x01000000;
201 break; /* => 16 MB */
204 info->flash_id = FLASH_UNKNOWN;
208 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
209 printf ("** ERROR: sector count %d > max (%d) **\n",
210 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
211 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
214 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
220 /*-----------------------------------------------------------------------
223 int flash_erase (flash_info_t *info, int s_first, int s_last)
225 int flag, prot, sect;
226 ulong type, start, now, last;
229 if ((s_first < 0) || (s_first > s_last)) {
230 if (info->flash_id == FLASH_UNKNOWN) {
231 printf ("- missing\n");
233 printf ("- no sectors to erase\n");
238 type = (info->flash_id & FLASH_VENDMASK);
239 if ((type != FLASH_MAN_INTEL)) {
240 printf ("Can't erase unknown flash type %08lx - aborted\n",
246 for (sect=s_first; sect<=s_last; ++sect) {
247 if (info->protect[sect]) {
253 printf ("- Warning: %d protected sectors will not be erased!\n",
259 start = get_timer (0);
261 /* Start erase on unprotected sectors */
262 for (sect = s_first; sect<=s_last; sect++) {
263 if (info->protect[sect] == 0) { /* not protected */
264 FPWV *addr = (FPWV *)(info->start[sect]);
267 /* Disable interrupts which might cause a timeout here */
268 flag = disable_interrupts();
270 *addr = (FPW)0x00500050; /* clear status register */
271 *addr = (FPW)0x00200020; /* erase setup */
272 *addr = (FPW)0x00D000D0; /* erase confirm */
274 /* re-enable interrupts if necessary */
278 /* wait at least 80us - let's wait 1 ms */
281 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
282 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
283 printf ("Timeout\n");
284 *addr = (FPW)0x00B000B0; /* suspend erase */
285 *addr = (FPW)0x00FF00FF; /* reset to read mode */
290 /* show that we're waiting */
291 if ((now - last) > 1000) { /* every second */
297 *addr = (FPW)0x00FF00FF; /* reset to read mode */
304 /*-----------------------------------------------------------------------
305 * Copy memory to flash, returns:
308 * 2 - Flash not erased
309 * 4 - Flash not identified
312 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
316 int count, i, l, rc, port_width;
318 if (info->flash_id == FLASH_UNKNOWN) {
321 /* get lower word aligned address */
322 #ifdef FLASH_PORT_WIDTH16
330 /* save sernum if needed */
331 if (addr >= CONFIG_SYS_FLASH_SN_SECTOR && addr < CONFIG_SYS_FLASH_SN_BASE)
333 u_long dest = CONFIG_SYS_FLASH_SN_BASE;
334 u_short *sn = (u_short *)gd->bd->bi_sernum;
336 printf("(saving sernum)");
339 if ((rc = write_data(info, dest, sn[i])) != 0) {
347 * handle unaligned start bytes
349 if ((l = addr - wp) != 0) {
351 for (i=0, cp=wp; i<l; ++i, ++cp) {
352 data = (data << 8) | (*(uchar *)cp);
354 for (; i<port_width && cnt>0; ++i) {
355 data = (data << 8) | *src++;
359 for (; cnt==0 && i<port_width; ++i, ++cp) {
360 data = (data << 8) | (*(uchar *)cp);
363 if ((rc = write_data(info, wp, data)) != 0) {
370 * handle word aligned part
373 while (cnt >= port_width) {
375 for (i=0; i<port_width; ++i) {
376 data = (data << 8) | *src++;
378 if ((rc = write_data(info, wp, data)) != 0) {
395 * handle unaligned tail bytes
398 for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) {
399 data = (data << 8) | *src++;
402 for (; i<port_width; ++i, ++cp) {
403 data = (data << 8) | (*(uchar *)cp);
406 return (write_data(info, wp, data));
409 /*-----------------------------------------------------------------------
410 * Write a word or halfword to Flash, returns:
413 * 2 - Flash not erased
415 static int write_data (flash_info_t *info, ulong dest, FPW data)
417 FPWV *addr = (FPWV *)dest;
422 /* Check if Flash is (sufficiently) erased */
423 if ((*addr & data) != data) {
424 printf("not erased at %08lx (%x)\n",(ulong)addr,*addr);
427 /* Disable interrupts which might cause a timeout here */
428 flag = disable_interrupts();
430 *addr = (FPW)0x00400040; /* write setup */
433 /* re-enable interrupts if necessary */
437 start = get_timer (0);
439 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
440 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
441 *addr = (FPW)0x00FF00FF; /* restore read mode */
446 *addr = (FPW)0x00FF00FF; /* restore read mode */