3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
7 * Hacked for the Hymod board by Murray.Jensen@csiro.au, 20-Oct-00
12 #include <board/hymod/flash.h>
14 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
16 /*-----------------------------------------------------------------------
19 #define FLAG_PROTECT_SET 0x01
20 #define FLAG_PROTECT_CLEAR 0x02
22 /*-----------------------------------------------------------------------
26 * probe for flash bank at address "base" and store info about it
27 * in the flash_info entry "fip". Fatal error if nothing there.
30 bank_probe (flash_info_t *fip, volatile bank_addr_t base)
32 volatile bank_addr_t addr;
39 /* put flash into read id mode */
40 *base = BANK_CMD_RD_ID;
42 /* check the manufacturer id - must be intel */
43 word = *BANK_REG_MAN_CODE (base);
44 if (word != BANK_FILL_WORD (INTEL_MANUFACT&0xff))
45 panic ("\nbad manufacturer's code (0x%08lx) at addr 0x%08lx",
46 (unsigned long)word, (unsigned long)base);
48 /* check the device id */
49 word = *BANK_REG_DEV_CODE (base);
52 case BANK_FILL_WORD (INTEL_ID_28F320J5&0xff):
53 fip->flash_id = FLASH_MAN_INTEL | FLASH_28F320J5;
54 fip->sector_count = 32;
57 case BANK_FILL_WORD (INTEL_ID_28F640J5&0xff):
58 fip->flash_id = FLASH_MAN_INTEL | FLASH_28F640J5;
59 fip->sector_count = 64;
62 case BANK_FILL_WORD (INTEL_ID_28F320J3A&0xff):
63 fip->flash_id = FLASH_MAN_INTEL | FLASH_28F320J3A;
64 fip->sector_count = 32;
67 case BANK_FILL_WORD (INTEL_ID_28F640J3A&0xff):
68 fip->flash_id = FLASH_MAN_INTEL | FLASH_28F640J3A;
69 fip->sector_count = 64;
72 case BANK_FILL_WORD (INTEL_ID_28F128J3A&0xff):
73 fip->flash_id = FLASH_MAN_INTEL | FLASH_28F128J3A;
74 fip->sector_count = 128;
78 panic ("\nbad device code (0x%08lx) at addr 0x%08lx",
79 (unsigned long)word, (unsigned long)base);
82 if (fip->sector_count >= CONFIG_SYS_MAX_FLASH_SECT)
83 panic ("\ntoo many sectors (%d) in flash at address 0x%08lx",
84 fip->sector_count, (unsigned long)base);
87 for (i = 0; i < fip->sector_count; i++) {
88 fip->start[i] = (unsigned long)addr;
90 addr = BANK_ADDR_NEXT_BLK (addr);
93 fip->size = (bank_size_t)addr - (bank_size_t)base;
100 bank_reset (flash_info_t *info, int sect)
102 volatile bank_addr_t addr = (bank_addr_t)info->start[sect];
105 printf ("writing reset cmd to addr 0x%08lx\n", (unsigned long)addr);
108 *addr = BANK_CMD_RST;
112 bank_erase_init (flash_info_t *info, int sect)
114 volatile bank_addr_t addr = (bank_addr_t)info->start[sect];
118 printf ("erasing sector %d, addr = 0x%08lx\n",
119 sect, (unsigned long)addr);
122 /* Disable intrs which might cause a timeout here */
123 flag = disable_interrupts ();
126 printf ("writing erase cmd to addr 0x%08lx\n", (unsigned long)addr);
128 *addr = BANK_CMD_ERASE1;
129 *addr = BANK_CMD_ERASE2;
131 /* re-enable interrupts if necessary */
133 enable_interrupts ();
137 bank_erase_poll (flash_info_t *info, int sect)
139 volatile bank_addr_t addr = (bank_addr_t)info->start[sect];
140 bank_word_t stat = *addr;
143 printf ("checking status at addr 0x%08lx [0x%08lx]\n",
144 (unsigned long)addr, (unsigned long)stat);
147 if ((stat & BANK_STAT_RDY) == BANK_STAT_RDY) {
148 if ((stat & BANK_STAT_ERR) != 0) {
149 printf ("failed on sector %d [0x%08lx] at "
150 "address 0x%08lx\n", sect,
151 (unsigned long)stat, (unsigned long)addr);
152 *addr = BANK_CMD_CLR_STAT;
163 bank_write_word (volatile bank_addr_t addr, bank_word_t value)
169 /* Disable interrupts which might cause a timeout here */
170 flag = disable_interrupts ();
172 *addr = BANK_CMD_PROG;
176 /* re-enable interrupts if necessary */
178 enable_interrupts ();
182 /* data polling for D7 */
183 start = get_timer (0);
185 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
190 } while ((stat & BANK_STAT_RDY) != BANK_STAT_RDY);
192 if ((stat & BANK_STAT_ERR) != 0) {
193 printf ("flash program failed [0x%08lx] at address 0x%08lx\n",
194 (unsigned long)stat, (unsigned long)addr);
195 *addr = BANK_CMD_CLR_STAT;
200 /* reset to read mode */
201 *addr = BANK_CMD_RST;
206 /*-----------------------------------------------------------------------
214 /* Init: no FLASHes known */
215 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
216 flash_info[i].flash_id = FLASH_UNKNOWN;
219 bank_probe (&flash_info[0], (bank_addr_t)CONFIG_SYS_FLASH_BASE);
222 * protect monitor and environment sectors
225 #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
226 (void)flash_protect (FLAG_PROTECT_SET,
227 CONFIG_SYS_MONITOR_BASE,
228 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
232 #if defined(CONFIG_SYS_FLASH_ENV_ADDR)
233 (void)flash_protect (FLAG_PROTECT_SET,
234 CONFIG_SYS_FLASH_ENV_ADDR,
235 #if defined(CONFIG_SYS_FLASH_ENV_BUF)
236 CONFIG_SYS_FLASH_ENV_ADDR + CONFIG_SYS_FLASH_ENV_BUF - 1,
238 CONFIG_SYS_FLASH_ENV_ADDR + CONFIG_SYS_FLASH_ENV_SIZE - 1,
243 return flash_info[0].size;
246 /*-----------------------------------------------------------------------
249 flash_print_info (flash_info_t *info)
253 if (info->flash_id == FLASH_UNKNOWN) {
254 printf ("missing or unknown FLASH type\n");
258 switch (info->flash_id & FLASH_VENDMASK) {
259 case FLASH_MAN_INTEL: printf ("INTEL "); break;
260 default: printf ("Unknown Vendor "); break;
263 switch (info->flash_id & FLASH_TYPEMASK) {
264 case FLASH_28F320J5: printf ("28F320J5 (32 Mbit, 2 x 16bit)\n");
266 case FLASH_28F640J5: printf ("28F640J5 (64 Mbit, 2 x 16bit)\n");
268 case FLASH_28F320J3A: printf ("28F320J3A (32 Mbit, 2 x 16bit)\n");
270 case FLASH_28F640J3A: printf ("28F640J3A (64 Mbit, 2 x 16bit)\n");
272 case FLASH_28F128J3A: printf ("28F320J3A (128 Mbit, 2 x 16bit)\n");
274 default: printf ("Unknown Chip Type\n");
278 printf (" Size: %ld MB in %d Sectors\n",
279 info->size >> 20, info->sector_count);
281 printf (" Sector Start Addresses:");
282 for (i=0; i<info->sector_count; ++i) {
287 info->protect[i] ? " (RO)" : " "
295 * The following code cannot be run from FLASH!
298 /*-----------------------------------------------------------------------
302 flash_erase (flash_info_t *info, int s_first, int s_last)
304 int prot, sect, haderr;
305 ulong start, now, last;
309 printf ("\nflash_erase: erase %d sectors (%d to %d incl.) from\n"
310 " Bank # %d: ", s_last - s_first + 1, s_first, s_last,
311 (info - flash_info) + 1);
312 flash_print_info (info);
315 if ((s_first < 0) || (s_first > s_last)) {
316 if (info->flash_id == FLASH_UNKNOWN) {
317 printf ("- missing\n");
319 printf ("- no sectors to erase\n");
325 for (sect = s_first; sect <= s_last; ++sect) {
326 if (info->protect[sect]) {
332 printf ("- Warning: %d protected sector%s will not be erased\n",
333 prot, (prot > 1 ? "s" : ""));
336 start = get_timer (0);
340 for (sect = s_first; sect <= s_last; sect++) {
341 if (info->protect[sect] == 0) { /* not protected */
345 bank_erase_init (info, sect);
347 /* wait at least 80us - let's wait 1 ms */
350 estart = get_timer (start);
353 now = get_timer (start);
355 if (now - estart > CONFIG_SYS_FLASH_ERASE_TOUT) {
356 printf ("Timeout (sect %d)\n", sect);
363 /* show that we're waiting */
364 if ((now - last) > 1000) { /* every second */
370 sectdone = bank_erase_poll (info, sect);
386 printf (" failed\n");
390 /* reset to read mode */
391 for (sect = s_first; sect <= s_last; sect++) {
392 if (info->protect[sect] == 0) { /* not protected */
393 bank_reset (info, sect);
399 /*-----------------------------------------------------------------------
400 * Write a word to Flash, returns:
403 * 2 - Flash not erased
407 write_word (flash_info_t *info, ulong dest, ulong data)
409 /* Check if Flash is (sufficiently) erased */
410 if ((*(ulong *)dest & data) != data)
413 return (bank_write_word ((bank_addr_t)dest, (bank_word_t)data));
416 /*-----------------------------------------------------------------------
417 * Copy memory to flash, returns:
420 * 2 - Flash not erased
425 write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
430 wp = (addr & ~3); /* get lower word aligned address */
433 * handle unaligned start bytes
435 if ((l = addr - wp) != 0) {
437 for (i=0, cp=wp; i<l; ++i, ++cp) {
438 data = (data << 8) | (*(uchar *)cp);
440 for (; i<4 && cnt>0; ++i) {
441 data = (data << 8) | *src++;
445 for (; cnt==0 && i<4; ++i, ++cp) {
446 data = (data << 8) | (*(uchar *)cp);
449 if ((rc = write_word (info, wp, data)) != 0) {
456 * handle word aligned part
460 for (i=0; i<4; ++i) {
461 data = (data << 8) | *src++;
463 if ((rc = write_word (info, wp, data)) != 0) {
475 * handle unaligned tail bytes
478 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
479 data = (data << 8) | *src++;
482 for (; i<4; ++i, ++cp) {
483 data = (data << 8) | (*(uchar *)cp);
486 return (write_word (info, wp, data));
489 /*-----------------------------------------------------------------------