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+
16 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
18 #if defined(CONFIG_ENV_IS_IN_FLASH)
19 # ifndef CONFIG_ENV_ADDR
20 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
22 # ifndef CONFIG_ENV_SIZE
23 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
25 # ifndef CONFIG_ENV_SECT_SIZE
26 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
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
44 #define FLASH_PORT_WIDTH ulong
45 #define FLASH_PORT_WIDTHV vu_long
48 #define FPW FLASH_PORT_WIDTH
49 #define FPWV FLASH_PORT_WIDTHV
51 /*-----------------------------------------------------------------------
54 static ulong flash_get_size (FPW * addr, flash_info_t * info);
55 static int write_data (flash_info_t * info, ulong dest, FPW data);
56 static void flash_get_offsets (ulong base, flash_info_t * info);
58 /*-----------------------------------------------------------------------
61 unsigned long flash_init (void)
63 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
64 volatile memctl8xx_t *memctl = &immap->im_memctl;
65 unsigned long size_b0;
68 /* Init: no FLASHes known */
69 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
70 flash_info[i].flash_id = FLASH_UNKNOWN;
73 /* Static FLASH Bank configuration here - FIXME XXX */
74 size_b0 = flash_get_size ((FPW *) FLASH_BASE0_PRELIM, &flash_info[0]);
76 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
77 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
78 size_b0, size_b0 << 20);
81 /* Remap FLASH according to real size */
82 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
83 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V;
85 /* Re-do sizing to get full correct info */
86 size_b0 = flash_get_size ((FPW *) CONFIG_SYS_FLASH_BASE, &flash_info[0]);
88 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
90 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
91 /* monitor protection ON by default */
92 (void) flash_protect (FLAG_PROTECT_SET,
93 CONFIG_SYS_FLASH_BASE,
94 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
98 #ifdef CONFIG_ENV_IS_IN_FLASH
99 /* ENV protection ON by default */
100 flash_protect (FLAG_PROTECT_SET,
102 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
106 flash_info[0].size = size_b0;
111 /*-----------------------------------------------------------------------
113 static void flash_get_offsets (ulong base, flash_info_t * info)
117 if (info->flash_id == FLASH_UNKNOWN) {
121 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
122 for (i = 0; i < info->sector_count; i++) {
123 info->start[i] = base + (i * 0x00020000);
128 /*-----------------------------------------------------------------------
130 void flash_print_info (flash_info_t * info)
134 if (info->flash_id == FLASH_UNKNOWN) {
135 printf ("missing or unknown FLASH type\n");
139 switch (info->flash_id & FLASH_VENDMASK) {
140 case FLASH_MAN_INTEL:
144 printf ("Unknown Vendor ");
148 switch (info->flash_id & FLASH_TYPEMASK) {
149 case FLASH_28F320J3A:
150 printf ("28F320J3A\n");
152 case FLASH_28F640J3A:
153 printf ("28F640J3A\n");
155 case FLASH_28F128J3A:
156 printf ("28F128J3A\n");
159 printf ("Unknown Chip Type\n");
163 printf (" Size: %ld MB in %d Sectors\n",
164 info->size >> 20, info->sector_count);
166 printf (" Sector Start Addresses:");
167 for (i = 0; i < info->sector_count; ++i) {
172 info->protect[i] ? " (RO)" : " ");
178 /*-----------------------------------------------------------------------
182 /*-----------------------------------------------------------------------
186 * The following code cannot be run from FLASH!
189 static ulong flash_get_size (FPW * addr, flash_info_t * info)
193 /* Make sure Block Lock Bits get cleared */
194 addr[0] = (FPW) 0x00FF00FF;
195 addr[0] = (FPW) 0x00600060;
196 addr[0] = (FPW) 0x00D000D0;
197 addr[0] = (FPW) 0x00FF00FF;
199 /* Write auto select command: read Manufacturer ID */
200 addr[0x5555] = (FPW) 0x00AA00AA;
201 addr[0x2AAA] = (FPW) 0x00550055;
202 addr[0x5555] = (FPW) 0x00900090;
206 debug("Manuf. ID @ 0x%08lx: 0x%08x\n", (ulong)addr, value);
209 case (FPW) INTEL_MANUFACT:
210 info->flash_id = FLASH_MAN_INTEL;
213 info->flash_id = FLASH_UNKNOWN;
214 info->sector_count = 0;
216 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
217 return (0); /* no or unknown flash */
220 value = addr[1]; /* device ID */
222 debug("Device ID @ 0x%08lx: 0x%08x\n", (ulong)(&addr[1]), value);
225 case (FPW) INTEL_ID_28F320J3A:
226 info->flash_id += FLASH_28F320J3A;
227 info->sector_count = 32;
228 info->size = 0x00400000;
231 case (FPW) INTEL_ID_28F640J3A:
232 info->flash_id += FLASH_28F640J3A;
233 info->sector_count = 64;
234 info->size = 0x00800000;
237 case (FPW) INTEL_ID_28F128J3A:
238 info->flash_id += FLASH_28F128J3A;
239 info->sector_count = 128;
240 info->size = 0x01000000;
241 break; /* => 16 MB */
244 info->flash_id = FLASH_UNKNOWN;
248 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
249 printf ("** ERROR: sector count %d > max (%d) **\n",
250 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
251 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
254 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
260 /*-----------------------------------------------------------------------
263 int flash_erase (flash_info_t * info, int s_first, int s_last)
265 int flag, prot, sect;
266 ulong type, start, now, last;
269 if ((s_first < 0) || (s_first > s_last)) {
270 if (info->flash_id == FLASH_UNKNOWN) {
271 printf ("- missing\n");
273 printf ("- no sectors to erase\n");
278 type = (info->flash_id & FLASH_VENDMASK);
279 if ((type != FLASH_MAN_INTEL)) {
280 printf ("Can't erase unknown flash type %08lx - aborted\n",
286 for (sect = s_first; sect <= s_last; ++sect) {
287 if (info->protect[sect]) {
293 printf ("- Warning: %d protected sectors will not be erased!\n",
299 start = get_timer (0);
301 /* Start erase on unprotected sectors */
302 for (sect = s_first; sect <= s_last; sect++) {
303 if (info->protect[sect] == 0) { /* not protected */
304 FPWV *addr = (FPWV *) (info->start[sect]);
307 /* Disable interrupts which might cause a timeout here */
308 flag = disable_interrupts ();
310 *addr = (FPW) 0x00500050; /* clear status register */
311 *addr = (FPW) 0x00200020; /* erase setup */
312 *addr = (FPW) 0x00D000D0; /* erase confirm */
314 /* re-enable interrupts if necessary */
316 enable_interrupts ();
318 /* wait at least 80us - let's wait 1 ms */
321 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
322 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
323 printf ("Timeout\n");
324 *addr = (FPW) 0x00B000B0; /* suspend erase */
325 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
330 /* show that we're waiting */
331 if ((now - last) > 1000) { /* every second */
337 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
344 /*-----------------------------------------------------------------------
345 * Copy memory to flash, returns:
348 * 2 - Flash not erased
349 * 4 - Flash not identified
352 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
357 int i, l, rc, port_width;
359 if (info->flash_id == FLASH_UNKNOWN) {
362 /* get lower word aligned address */
363 #ifdef FLASH_PORT_WIDTH16
372 * handle unaligned start bytes
374 if ((l = addr - wp) != 0) {
376 for (i = 0, cp = wp; i < l; ++i, ++cp) {
377 data = (data << 8) | (*(uchar *) cp);
379 for (; i < port_width && cnt > 0; ++i) {
380 data = (data << 8) | *src++;
384 for (; cnt == 0 && i < port_width; ++i, ++cp) {
385 data = (data << 8) | (*(uchar *) cp);
388 if ((rc = write_data (info, wp, data)) != 0) {
395 * handle word aligned part
397 while (cnt >= port_width) {
399 for (i = 0; i < port_width; ++i) {
400 data = (data << 8) | *src++;
402 if ((rc = write_data (info, wp, data)) != 0) {
414 * handle unaligned tail bytes
417 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
418 data = (data << 8) | *src++;
421 for (; i < port_width; ++i, ++cp) {
422 data = (data << 8) | (*(uchar *) cp);
425 return (write_data (info, wp, data));
428 /*-----------------------------------------------------------------------
429 * Write a word or halfword to Flash, returns:
432 * 2 - Flash not erased
434 static int write_data (flash_info_t * info, ulong dest, FPW data)
436 FPWV *addr = (FPWV *) dest;
441 /* Check if Flash is (sufficiently) erased */
442 if ((*addr & data) != data) {
443 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
446 /* Disable interrupts which might cause a timeout here */
447 flag = disable_interrupts ();
449 *addr = (FPW) 0x00400040; /* write setup */
452 /* re-enable interrupts if necessary */
454 enable_interrupts ();
456 start = get_timer (0);
458 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
459 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
460 *addr = (FPW) 0x00FF00FF; /* restore read mode */
465 *addr = (FPW) 0x00FF00FF; /* restore read mode */