3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
13 #if defined(CONFIG_ENV_IS_IN_FLASH)
14 # ifndef CONFIG_ENV_ADDR
15 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
17 # ifndef CONFIG_ENV_SIZE
18 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
20 # ifndef CONFIG_ENV_SECT_SIZE
21 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
25 /*-----------------------------------------------------------------------
28 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
29 static int write_word (flash_info_t *info, ulong dest, ulong data);
30 static void flash_get_offsets (ulong base, flash_info_t *info);
32 /*-----------------------------------------------------------------------
35 unsigned long flash_init (void)
37 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
38 volatile memctl8xx_t *memctl = &immap->im_memctl;
39 volatile ip860_bcsr_t *bcsr = (ip860_bcsr_t *)BCSR_BASE;
43 /* Init: enable write,
44 * or we cannot even write flash commands
46 bcsr->bd_ctrl |= BD_CTRL_FLWE;
48 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
49 flash_info[i].flash_id = FLASH_UNKNOWN;
52 /* Static FLASH Bank configuration here - FIXME XXX */
54 size = flash_get_size((vu_long *)FLASH_BASE, &flash_info[0]);
56 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
57 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
61 /* Remap FLASH according to real size */
62 memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH | (-size & 0xFFFF8000);
63 memctl->memc_br1 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
64 (memctl->memc_br1 & ~(BR_BA_MSK));
66 /* Re-do sizing to get full correct info */
67 size = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
69 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
71 flash_info[0].size = size;
73 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
74 /* monitor protection ON by default */
75 flash_protect(FLAG_PROTECT_SET,
76 CONFIG_SYS_MONITOR_BASE,
77 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
81 #ifdef CONFIG_ENV_IS_IN_FLASH
82 /* ENV protection ON by default */
83 flash_protect(FLAG_PROTECT_SET,
85 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
91 /*-----------------------------------------------------------------------
93 static void flash_get_offsets (ulong base, flash_info_t *info)
97 /* all possible flash types
98 * (28F016SV, 28F160S3, 28F320S3)
99 * have the same erase block size: 64 kB per chip,
103 /* set up sector start address table */
104 for (i = 0; i < info->sector_count; i++) {
105 info->start[i] = base;
110 /*-----------------------------------------------------------------------
112 void flash_print_info (flash_info_t *info)
116 if (info->flash_id == FLASH_UNKNOWN) {
117 printf ("missing or unknown FLASH type\n");
121 switch (info->flash_id & FLASH_VENDMASK) {
122 case FLASH_MAN_INTEL: printf ("Intel "); break;
123 default: printf ("Unknown Vendor "); break;
126 switch (info->flash_id & FLASH_TYPEMASK) {
127 case FLASH_28F016SV: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
129 case FLASH_28F160S3: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
131 case FLASH_28F320S3: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
133 default: printf ("Unknown Chip Type\n");
137 printf (" Size: %ld MB in %d Sectors\n",
138 info->size >> 20, info->sector_count);
140 printf (" Sector Start Addresses:");
141 for (i=0; i<info->sector_count; ++i) {
146 info->protect[i] ? " (RO)" : " "
153 /*-----------------------------------------------------------------------
157 /*-----------------------------------------------------------------------
161 * The following code cannot be run from FLASH!
164 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
168 ulong base = (ulong)addr;
170 /* Write "Intelligent Identifier" command: read Manufacturer ID */
175 case (MT_MANUFACT & 0x00FF00FF): /* MT or => Intel */
176 case (INTEL_ALT_MANU & 0x00FF00FF):
177 info->flash_id = FLASH_MAN_INTEL;
180 info->flash_id = FLASH_UNKNOWN;
181 info->sector_count = 0;
183 return (0); /* no or unknown flash */
186 value = addr[1]; /* device ID */
189 case (INTEL_ID_28F016S):
190 info->flash_id += FLASH_28F016SV;
191 info->sector_count = 32;
192 info->size = 0x00400000;
193 break; /* => 2x2 MB */
195 case (INTEL_ID_28F160S3):
196 info->flash_id += FLASH_28F160S3;
197 info->sector_count = 32;
198 info->size = 0x00400000;
199 break; /* => 2x2 MB */
201 case (INTEL_ID_28F320S3):
202 info->flash_id += FLASH_28F320S3;
203 info->sector_count = 64;
204 info->size = 0x00800000;
205 break; /* => 2x4 MB */
208 info->flash_id = FLASH_UNKNOWN;
209 return (0); /* => no or unknown flash */
213 /* set up sector start address table */
214 for (i = 0; i < info->sector_count; i++) {
215 info->start[i] = base + (i * 0x00020000);
216 /* don't know how to check sector protection */
217 info->protect[i] = 0;
221 * Prevent writes to uninitialized FLASH.
223 if (info->flash_id != FLASH_UNKNOWN) {
224 addr = (vu_long *)info->start[0];
226 *addr = 0xFFFFFF; /* reset bank to read array mode */
233 /*-----------------------------------------------------------------------
236 int flash_erase (flash_info_t *info, int s_first, int s_last)
238 int flag, prot, sect;
239 ulong start, now, last;
241 if ((s_first < 0) || (s_first > s_last)) {
242 if (info->flash_id == FLASH_UNKNOWN) {
243 printf ("- missing\n");
245 printf ("- no sectors to erase\n");
250 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
251 printf ("Can't erase unknown flash type %08lx - aborted\n",
257 for (sect=s_first; sect<=s_last; ++sect) {
258 if (info->protect[sect]) {
264 printf ("- Warning: %d protected sectors will not be erased!\n",
270 start = get_timer (0);
273 /* Start erase on unprotected sectors */
274 for (sect = s_first; sect<=s_last; sect++) {
275 if (info->protect[sect] == 0) { /* not protected */
276 vu_long *addr = (vu_long *)(info->start[sect]);
278 /* Disable interrupts which might cause a timeout here */
279 flag = disable_interrupts();
281 /* Single Block Erase Command */
285 /* Resume Command, as per errata update */
288 /* re-enable interrupts if necessary */
292 /* wait at least 80us - let's wait 1 ms */
295 while ((*addr & 0x00800080) != 0x00800080) {
296 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
297 printf ("Timeout\n");
298 *addr = 0xFFFFFFFF; /* reset bank */
301 /* show that we're waiting */
302 if ((now - last) > 1000) { /* every second */
308 /* reset to read mode */
317 /*-----------------------------------------------------------------------
318 * Copy memory to flash, returns:
321 * 2 - Flash not erased
324 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
329 wp = (addr & ~3); /* get lower word aligned address */
332 * handle unaligned start bytes
334 if ((l = addr - wp) != 0) {
336 for (i=0, cp=wp; i<l; ++i, ++cp) {
337 data = (data << 8) | (*(uchar *)cp);
339 for (; i<4 && cnt>0; ++i) {
340 data = (data << 8) | *src++;
344 for (; cnt==0 && i<4; ++i, ++cp) {
345 data = (data << 8) | (*(uchar *)cp);
348 if ((rc = write_word(info, wp, data)) != 0) {
355 * handle word aligned part
359 for (i=0; i<4; ++i) {
360 data = (data << 8) | *src++;
362 if ((rc = write_word(info, wp, data)) != 0) {
374 * handle unaligned tail bytes
377 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
378 data = (data << 8) | *src++;
381 for (; i<4; ++i, ++cp) {
382 data = (data << 8) | (*(uchar *)cp);
385 return (write_word(info, wp, data));
388 /*-----------------------------------------------------------------------
389 * Write a word to Flash, returns:
392 * 2 - Flash not erased
394 static int write_word (flash_info_t *info, ulong dest, ulong data)
396 vu_long *addr = (vu_long *)dest;
400 /* Check if Flash is (sufficiently) erased */
401 if ((*addr & data) != data) {
404 /* Disable interrupts which might cause a timeout here */
405 flag = disable_interrupts();
413 /* re-enable interrupts if necessary */
417 /* data polling for D7 */
418 start = get_timer (0);
420 while (((csr = *addr) & 0x00800080) != 0x00800080) {
421 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
426 if (csr & 0x00400040) {
427 printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
431 /* Clear Status Registers Command */
433 /* Reset to read array mode */
439 /*-----------------------------------------------------------------------