2 * (C) Copyright 2000-2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 #define PHYS_FLASH_1 CONFIG_SYS_FLASH_BASE
12 #define FLASH_BANK_SIZE 0x200000
14 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
16 void flash_print_info (flash_info_t * info)
20 switch (info->flash_id & FLASH_VENDMASK) {
21 case (AMD_MANUFACT & FLASH_VENDMASK):
25 printf ("Unknown Vendor ");
29 switch (info->flash_id & FLASH_TYPEMASK) {
30 case (AMD_ID_PL160CB & FLASH_TYPEMASK):
31 printf ("AM29PL160CB (16Mbit)\n");
34 printf ("Unknown Chip Type\n");
39 printf (" Size: %ld MB in %d Sectors\n",
40 info->size >> 20, info->sector_count);
42 printf (" Sector Start Addresses:");
43 for (i = 0; i < info->sector_count; i++) {
47 printf (" %08lX%s", info->start[i],
48 info->protect[i] ? " (RO)" : " ");
57 unsigned long flash_init (void)
62 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
65 flash_info[i].flash_id =
66 (AMD_MANUFACT & FLASH_VENDMASK) |
67 (AMD_ID_PL160CB & FLASH_TYPEMASK);
68 flash_info[i].size = FLASH_BANK_SIZE;
69 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
70 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
72 flashbase = PHYS_FLASH_1;
74 panic ("configured to many flash banks!\n");
76 for (j = 0; j < flash_info[i].sector_count; j++) {
79 flash_info[i].start[j] = flashbase;
81 if ((j >= 1) && (j <= 2)) {
82 /* 2nd and 3rd are 8 KiB */
83 flash_info[i].start[j] =
84 flashbase + 0x4000 + 0x2000 * (j - 1);
88 flash_info[i].start[j] = flashbase + 0x8000;
90 if ((j >= 4) && (j <= 10)) {
92 flash_info[i].start[j] =
93 flashbase + 0x40000 + 0x40000 * (j -
97 size += flash_info[i].size;
100 flash_protect (FLAG_PROTECT_SET,
101 CONFIG_SYS_FLASH_BASE,
102 CONFIG_SYS_FLASH_BASE + 0x3ffff, &flash_info[0]);
108 #define CMD_READ_ARRAY 0x00F0
109 #define CMD_UNLOCK1 0x00AA
110 #define CMD_UNLOCK2 0x0055
111 #define CMD_ERASE_SETUP 0x0080
112 #define CMD_ERASE_CONFIRM 0x0030
113 #define CMD_PROGRAM 0x00A0
114 #define CMD_UNLOCK_BYPASS 0x0020
116 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x00000555<<1)))
117 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x000002AA<<1)))
119 #define BIT_ERASE_DONE 0x0080
120 #define BIT_RDY_MASK 0x0080
121 #define BIT_PROGRAM_ERROR 0x0020
122 #define BIT_TIMEOUT 0x80000000 /* our flag */
129 int flash_erase (flash_info_t * info, int s_first, int s_last)
132 int iflag, cflag, prot, sect;
137 /* first look for protection bits */
139 if (info->flash_id == FLASH_UNKNOWN)
140 return ERR_UNKNOWN_FLASH_TYPE;
142 if ((s_first < 0) || (s_first > s_last)) {
146 if ((info->flash_id & FLASH_VENDMASK) !=
147 (AMD_MANUFACT & FLASH_VENDMASK)) {
148 return ERR_UNKNOWN_FLASH_VENDOR;
152 for (sect = s_first; sect <= s_last; ++sect) {
153 if (info->protect[sect]) {
158 return ERR_PROTECTED;
161 * Disable interrupts which might cause a timeout
162 * here. Remember that our exception vectors are
163 * at address 0 in the flash, and we don't want a
164 * (ticker) exception to happen while the flash
165 * chip is in programming mode.
168 cflag = icache_status ();
170 iflag = disable_interrupts ();
174 /* Start erase on unprotected sectors */
175 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
176 printf ("Erasing sector %2d ... ", sect);
178 /* arm simple, non interrupt dependent timer */
179 start = get_timer(0);
181 if (info->protect[sect] == 0) { /* not protected */
183 (volatile u16 *) (info->start[sect]);
185 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
186 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
187 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
189 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
190 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
191 *addr = CMD_ERASE_CONFIRM;
193 /* wait until flash is ready */
200 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
201 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
207 && (result & 0xFFFF) & BIT_ERASE_DONE)
212 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
224 } else { /* it was protected */
226 printf ("protected!\n");
231 printf ("User Interrupt!\n");
234 /* allow flash to settle - wait 10 ms */
238 enable_interrupts ();
246 static int write_word (flash_info_t * info, ulong dest, ulong data)
248 volatile u16 *addr = (volatile u16 *) dest;
256 * Check if Flash is (sufficiently) erased
259 if ((result & data) != data)
260 return ERR_NOT_ERASED;
264 * Disable interrupts which might cause a timeout
265 * here. Remember that our exception vectors are
266 * at address 0 in the flash, and we don't want a
267 * (ticker) exception to happen while the flash
268 * chip is in programming mode.
271 cflag = icache_status ();
273 iflag = disable_interrupts ();
275 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
276 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
277 MEM_FLASH_ADDR1 = CMD_PROGRAM;
280 /* arm simple, non interrupt dependent timer */
281 start = get_timer(0);
283 /* wait until flash is ready */
289 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
293 if (!chip1 && ((result & 0x80) == (data & 0x80)))
298 *addr = CMD_READ_ARRAY;
300 if (chip1 == ERR || *addr != data)
304 enable_interrupts ();
313 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
319 printf ("unaligned destination not supported\n");
325 printf ("odd transfer sizes not supported\n");
333 data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
335 if ((rc = write_word (info, wp - 1, data)) != 0) {
344 data = *((volatile u16 *) src);
345 if ((rc = write_word (info, wp, data)) != 0) {
354 data = (*((volatile u8 *) src) << 8) |
355 *((volatile u8 *) (wp + 1));
356 if ((rc = write_word (info, wp, data)) != 0) {