3 * Martin Winistoerfer, martinwinistoerfer@gmx.ch.
5 * SPDX-License-Identifier: GPL-2.0+
11 * Discription: This Driver is for 28F320J3A, 28F640J3A and
12 * 28F128J3A Intel flashs working in 16 Bit mode.
13 * They are single bank flashs.
15 * Most of this code is taken from existing u-boot
23 #if defined(CONFIG_ENV_IS_IN_FLASH)
24 # ifndef CONFIG_ENV_ADDR
25 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
27 # ifndef CONFIG_ENV_SIZE
28 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
30 # ifndef CONFIG_ENV_SECT_SIZE
31 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
35 #define FLASH_ID_MASK 0xFFFF
36 #define FLASH_BLOCK_SIZE 0x00010000
37 #define FLASH_CMD_READ_ID 0x0090
38 #define FLASH_CMD_RESET 0x00ff
39 #define FLASH_CMD_BLOCK_ERASE 0x0020
40 #define FLASH_CMD_ERASE_CONFIRM 0x00D0
41 #define FLASH_CMD_CLEAR_STATUS 0x0050
42 #define FLASH_CMD_SUSPEND_ERASE 0x00B0
43 #define FLASH_CMD_WRITE 0x0040
44 #define FLASH_CMD_PROTECT 0x0060
45 #define FLASH_CMD_PROTECT_SET 0x0001
46 #define FLASH_CMD_PROTECT_CLEAR 0x00D0
47 #define FLASH_STATUS_DONE 0x0080
49 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
52 * Local function prototypes
54 static ulong flash_get_size (vu_short *addr, flash_info_t *info);
55 static int write_short (flash_info_t *info, ulong dest, ushort data);
56 static void flash_get_offsets (ulong base, flash_info_t *info);
62 unsigned long flash_init (void)
64 unsigned long size_b0;
67 /* Init: no FLASHes known */
68 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
69 flash_info[i].flash_id = FLASH_UNKNOWN;
72 /* Static FLASH Bank configuration here - FIXME XXX */
74 debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
76 size_b0 = flash_get_size((vu_short *)FLASH_BASE0_PRELIM, &flash_info[0]);
78 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
79 printf ("## Unknown FLASH on Bank 0: "
80 "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
81 flash_info[0].flash_id,
82 size_b0, size_b0<<20);
85 flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
87 flash_info[0].size = size_b0;
89 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
90 /* monitor protection ON by default */
91 flash_protect(FLAG_PROTECT_SET,
92 CONFIG_SYS_MONITOR_BASE,
93 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
97 #ifdef CONFIG_ENV_IS_IN_FLASH
98 /* ENV protection ON by default */
99 flash_protect(FLAG_PROTECT_SET,
101 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
109 * Compute start adress of each sector (block)
112 static void flash_get_offsets (ulong base, flash_info_t *info)
116 if (info->flash_id == FLASH_UNKNOWN) {
120 switch (info->flash_id & FLASH_VENDMASK) {
121 case FLASH_MAN_INTEL:
122 for (i = 0; i < info->sector_count; i++) {
123 info->start[i] = base + i * FLASH_BLOCK_SIZE;
128 printf ("Don't know sector offsets for flash type 0x%lx\n",
135 * Print flash information
137 void flash_print_info (flash_info_t *info)
141 if (info->flash_id == FLASH_UNKNOWN) {
142 printf ("missing or unknown FLASH type\n");
146 switch (info->flash_id & FLASH_VENDMASK) {
147 case FLASH_MAN_AMD: printf ("AMD "); break;
148 case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
149 case FLASH_MAN_SST: printf ("SST "); break;
150 case FLASH_MAN_STM: printf ("STM "); break;
151 case FLASH_MAN_INTEL: printf ("Intel "); break;
152 case FLASH_MAN_MT: printf ("MT "); break;
153 default: printf ("Unknown Vendor "); break;
156 switch (info->flash_id & FLASH_TYPEMASK) {
157 case FLASH_28F320J3A: printf ("28F320J3A (32Mbit) 16-Bit\n");
159 case FLASH_28F640J3A: printf ("28F640J3A (64Mbit) 16-Bit\n");
161 case FLASH_28F128J3A: printf ("28F128J3A (128Mbit) 16-Bit\n");
163 default: printf ("Unknown Chip Type\n");
167 if (info->size >= (1 << 20)) {
172 printf (" Size: %ld %cB in %d Sectors\n",
174 (i == 20) ? 'M' : 'k',
177 printf (" Sector Start Addresses:");
178 for (i=0; i<info->sector_count; ++i) {
183 info->protect[i] ? " (RO)" : " "
191 * Get size of flash in bytes.
192 * The following code cannot be run from FLASH!
195 static ulong flash_get_size (vu_short *addr, flash_info_t *info)
199 /* Read Manufacturer ID */
200 addr[0] = FLASH_CMD_READ_ID;
204 case (AMD_MANUFACT & FLASH_ID_MASK):
205 info->flash_id = FLASH_MAN_AMD;
207 case (FUJ_MANUFACT & FLASH_ID_MASK):
208 info->flash_id = FLASH_MAN_FUJ;
210 case (SST_MANUFACT & FLASH_ID_MASK):
211 info->flash_id = FLASH_MAN_SST;
213 case (STM_MANUFACT & FLASH_ID_MASK):
214 info->flash_id = FLASH_MAN_STM;
216 case (INTEL_MANUFACT & FLASH_ID_MASK):
217 info->flash_id = FLASH_MAN_INTEL;
220 info->flash_id = FLASH_UNKNOWN;
221 info->sector_count = 0;
223 addr[0] = FLASH_CMD_RESET; /* restore read mode */
224 return (0); /* no or unknown flash */
227 value = addr[1]; /* device ID */
230 case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
231 info->flash_id += FLASH_28F320J3A;
232 info->sector_count = 32;
233 info->size = 0x00400000;
234 break; /* => 32 MBit */
236 case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
237 info->flash_id += FLASH_28F640J3A;
238 info->sector_count = 64;
239 info->size = 0x00800000;
240 break; /* => 64 MBit */
242 case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
243 info->flash_id += FLASH_28F128J3A;
244 info->sector_count = 128;
245 info->size = 0x01000000;
246 break; /* => 128 MBit */
249 info->flash_id = FLASH_UNKNOWN;
250 addr[0] = FLASH_CMD_RESET; /* restore read mode */
251 return (0); /* => no or unknown flash */
255 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
256 printf ("** ERROR: sector count %d > max (%d) **\n",
257 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
258 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
261 addr[0] = FLASH_CMD_RESET; /* restore read mode */
268 * Erase unprotected sectors
271 int flash_erase (flash_info_t *info, int s_first, int s_last)
273 int flag, prot, sect;
274 ulong start, now, last;
276 if ((s_first < 0) || (s_first > s_last)) {
277 if (info->flash_id == FLASH_UNKNOWN) {
278 printf ("- missing\n");
280 printf ("- no sectors to erase\n");
285 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
286 printf ("Can erase only Intel flash types - aborted\n");
291 for (sect=s_first; sect<=s_last; ++sect) {
292 if (info->protect[sect]) {
298 printf ("- Warning: %d protected sectors will not be erased!\n",
304 start = get_timer (0);
307 /* Start erase on unprotected sectors */
308 for (sect = s_first; sect<=s_last; sect++) {
309 if (info->protect[sect] == 0) { /* not protected */
310 vu_short *addr = (vu_short *)(info->start[sect]);
311 unsigned long status;
313 /* Disable interrupts which might cause a timeout here */
314 flag = disable_interrupts();
317 printf("Erase sector %d at start addr 0x%08X", sect, (unsigned int)info->start[sect]);
320 *addr = FLASH_CMD_CLEAR_STATUS;
321 *addr = FLASH_CMD_BLOCK_ERASE;
322 *addr = FLASH_CMD_ERASE_CONFIRM;
324 /* re-enable interrupts if necessary */
328 /* wait at least 80us - let's wait 1 ms */
331 while (((status = *addr) & FLASH_STATUS_DONE) != FLASH_STATUS_DONE) {
332 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
333 printf("Flash erase timeout at address %lx\n", info->start[sect]);
334 *addr = FLASH_CMD_SUSPEND_ERASE;
335 *addr = FLASH_CMD_RESET;
339 /* show that we're waiting */
340 if ((now - last) > 1000) { /* every second */
345 *addr = FLASH_CMD_RESET;
353 * Copy memory to flash, returns:
356 * 2 - Flash not erased
357 * 4 - Flash not identified
360 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
366 if (info->flash_id == FLASH_UNKNOWN) {
370 wp = (addr & ~1); /* get lower word aligned address */
373 * handle unaligned start byte
378 data = (data << 8) | *src++;
380 if ((rc = write_short(info, wp, data)) != 0) {
387 * handle word aligned part
392 for (i=0; i<2; ++i) {
393 data = (data << 8) | *src++;
396 if ((rc = write_short(info, wp, data)) != 0) {
408 * handle unaligned tail bytes
412 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
413 data = (data << 8) | *src++;
416 for (; i<2; ++i, ++cp) {
417 data = (data << 8) | (*(uchar *)cp);
420 return (write_short(info, wp, data));
425 * Write 16 bit (short) to flash
428 static int write_short (flash_info_t *info, ulong dest, ushort data)
430 vu_short *addr = (vu_short*)(info->start[0]);
434 /* Check if Flash is (sufficiently) erased */
435 if ((*((vu_short *)dest) & data) != data) {
439 /* Disable interrupts which might cause a timeout here */
440 flag = disable_interrupts();
442 if (!(info->flash_id & FLASH_VENDMASK)) {
445 *addr = FLASH_CMD_ERASE_CONFIRM;
446 *addr = FLASH_CMD_WRITE;
448 *((vu_short *)dest) = data;
450 /* re-enable interrupts if necessary */
455 /* data polling for D7 */
456 start = get_timer (0);
458 /* wait for error or finish */
459 while(!(addr[0] & FLASH_STATUS_DONE)){
460 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
461 addr[0] = FLASH_CMD_RESET;
466 *addr = FLASH_CMD_RESET;
471 * Protects a flash sector
474 int flash_real_protect(flash_info_t *info, long sector, int prot)
476 vu_short *addr = (vu_short*)(info->start[sector]);
479 *addr = FLASH_CMD_CLEAR_STATUS;
480 *addr = FLASH_CMD_PROTECT;
483 *addr = FLASH_CMD_PROTECT_SET;
485 *addr = FLASH_CMD_PROTECT_CLEAR;
488 /* wait for error or finish */
489 start = get_timer (0);
490 while(!(addr[0] & FLASH_STATUS_DONE)){
491 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
492 printf("Flash protect timeout at address %lx\n", info->start[sector]);
493 addr[0] = FLASH_CMD_RESET;
497 /* Set software protect flag */
498 info->protect[sector] = prot;
499 *addr = FLASH_CMD_RESET;