3 * Elmeg Communications Systems GmbH, Juergen Selent (j.selent@elmeg.de)
5 * Support for the Elmeg VoVPN Gateway Module
6 * ------------------------------------------
7 * This is a signle bank flashdriver for INTEL 28F320J3, 28F640J3
8 * and 28F128J3A flashs working in 8 Bit mode.
10 * Most of this code is taken from existing u-boot source code.
12 * SPDX-License-Identifier: GPL-2.0+
17 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
19 #define FLASH_CMD_READ_ID 0x90
20 #define FLASH_CMD_READ_STATUS 0x70
21 #define FLASH_CMD_RESET 0xff
22 #define FLASH_CMD_BLOCK_ERASE 0x20
23 #define FLASH_CMD_ERASE_CONFIRM 0xd0
24 #define FLASH_CMD_CLEAR_STATUS 0x50
25 #define FLASH_CMD_SUSPEND_ERASE 0xb0
26 #define FLASH_CMD_WRITE 0x40
27 #define FLASH_CMD_WRITE_BUFF 0xe8
28 #define FLASH_CMD_PROG_RESUME 0xd0
29 #define FLASH_CMD_PROTECT 0x60
30 #define FLASH_CMD_PROTECT_SET 0x01
31 #define FLASH_CMD_PROTECT_CLEAR 0xd0
32 #define FLASH_STATUS_DONE 0x80
34 #define FLASH_WRITE_BUFFER_SIZE 32
36 #ifdef CONFIG_SYS_FLASH_16BIT
37 #define FLASH_WORD_SIZE unsigned short
38 #define FLASH_ID_MASK 0xffff
39 #define FLASH_CMD_ADDR_SHIFT 0
41 #define FLASH_WORD_SIZE unsigned char
42 #define FLASH_ID_MASK 0xff
43 /* A0 is not used in either 8x or 16x for READ ID */
44 #define FLASH_CMD_ADDR_SHIFT 1
49 flash_get(volatile FLASH_WORD_SIZE *addr, flash_info_t *info)
51 volatile FLASH_WORD_SIZE *p;
52 FLASH_WORD_SIZE value;
55 addr[0] = FLASH_CMD_READ_ID;
58 value = addr[0 << FLASH_CMD_ADDR_SHIFT];
60 case (INTEL_MANUFACT & FLASH_ID_MASK):
61 info->flash_id = FLASH_MAN_INTEL;
64 info->flash_id = FLASH_UNKNOWN;
65 info->sector_count = 0;
67 *addr = FLASH_CMD_RESET;
73 value = addr[1 << FLASH_CMD_ADDR_SHIFT];
75 case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
76 info->flash_id += FLASH_28F320J3A;
77 info->sector_count = 32;
78 info->size = 0x00400000;
80 case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
81 info->flash_id += FLASH_28F640J3A;
82 info->sector_count = 64;
83 info->size = 0x00800000;
85 case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
86 info->flash_id += FLASH_28F128J3A;
87 info->sector_count = 128;
88 info->size = 0x01000000;
91 info->flash_id = FLASH_UNKNOWN;
92 info->sector_count = 0;
94 *addr = FLASH_CMD_RESET;
99 for (i = 0; i < info->sector_count; i++) {
100 info->start[i] = (unsigned long)addr + (i * info->size/info->sector_count);
103 /* check protected sectors */
104 for (i = 0; i < info->sector_count; i++) {
105 p = (volatile FLASH_WORD_SIZE *)(info->start[i]);
106 info->protect[i] = p[2 << FLASH_CMD_ADDR_SHIFT] & 1;
110 *addr = FLASH_CMD_RESET;
120 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
121 flash_info[i].flash_id = FLASH_UNKNOWN;
123 size = flash_get((volatile FLASH_WORD_SIZE *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
124 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
125 printf ("## Unknown FLASH Size=0x%08lx\n", size);
129 /* always protect 1 sector containing the HRCW */
130 flash_protect(FLAG_PROTECT_SET,
131 flash_info[0].start[0],
132 flash_info[0].start[1] - 1,
135 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
136 flash_protect(FLAG_PROTECT_SET,
137 CONFIG_SYS_MONITOR_FLASH,
138 CONFIG_SYS_MONITOR_FLASH+CONFIG_SYS_MONITOR_LEN-1,
141 #ifdef CONFIG_ENV_IS_IN_FLASH
142 flash_protect(FLAG_PROTECT_SET,
144 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
151 flash_print_info(flash_info_t *info)
155 if (info->flash_id == FLASH_UNKNOWN) {
156 printf ("missing or unknown FLASH type\n");
160 switch (info->flash_id & FLASH_VENDMASK) {
161 case FLASH_MAN_INTEL: printf ("INTEL "); break;
162 default: printf ("Unknown Vendor "); break;
165 switch (info->flash_id & FLASH_TYPEMASK) {
166 case FLASH_28F320J3A: printf ("28F320JA3 (32 Mbit)\n");
168 case FLASH_28F640J3A: printf ("28F640JA3 (64 Mbit)\n");
170 case FLASH_28F128J3A: printf ("28F128JA3 (128 Mbit)\n");
172 default: printf ("Unknown Chip Type");
176 printf (" Size: %ld MB in %d Sectors\n",
177 info->size >> 20, info->sector_count);
179 printf (" Sector Start Addresses:");
180 for (i=0; i<info->sector_count; ++i) {
185 info->protect[i] ? " (RO)" : " "
192 flash_erase(flash_info_t *info, int s_first, int s_last)
194 unsigned long start, now, last;
195 int flag, prot, sect;
196 volatile FLASH_WORD_SIZE *addr;
197 FLASH_WORD_SIZE status;
199 if ((s_first < 0) || (s_first > s_last)) {
200 if (info->flash_id == FLASH_UNKNOWN) {
201 printf ("- missing\n");
203 printf ("- no sectors to erase\n");
208 if (info->flash_id == FLASH_UNKNOWN) {
209 printf ("Cannot erase unknown flash - aborted\n");
214 for (sect=s_first; sect<=s_last; ++sect) {
215 if (info->protect[sect]) {
221 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
226 start = get_timer (0);
229 for (sect = s_first; sect<=s_last; sect++) {
230 if (info->protect[sect]) {
234 addr = (volatile FLASH_WORD_SIZE *)(info->start[sect]);
235 /* Disable interrupts which might cause a timeout here */
236 flag = disable_interrupts();
239 printf("Erase sector %d at start addr 0x%08X", sect, (unsigned int)info->start[sect]);
242 *addr = FLASH_CMD_CLEAR_STATUS;
243 *addr = FLASH_CMD_BLOCK_ERASE;
244 *addr = FLASH_CMD_ERASE_CONFIRM;
246 /* re-enable interrupts if necessary */
251 /* wait at least 80us - let's wait 1 ms */
254 while (((status = *addr) & FLASH_STATUS_DONE) != FLASH_STATUS_DONE) {
255 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
256 printf("Flash erase timeout at address %lx\n", info->start[sect]);
257 *addr = FLASH_CMD_SUSPEND_ERASE;
258 *addr = FLASH_CMD_RESET;
262 /* show that we're waiting */
263 if ((now - last) > 1000) { /* every second */
268 *addr = FLASH_CMD_RESET;
275 write_buff2( volatile FLASH_WORD_SIZE *dst,
276 volatile FLASH_WORD_SIZE *src,
280 FLASH_WORD_SIZE status;
283 start = get_timer (0);
285 /* Disable interrupts which might cause a timeout here */
286 flag = disable_interrupts();
287 dst[0] = FLASH_CMD_WRITE_BUFF;
288 if ((status = *dst) & FLASH_STATUS_DONE) {
292 /* re-enable interrupts if necessary */
297 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
301 dst[0] = (FLASH_WORD_SIZE)(cnt - 1);
302 for (i=0; i<cnt; i++) {
305 dst[0] = FLASH_CMD_PROG_RESUME;
315 poll_status( volatile FLASH_WORD_SIZE *addr )
319 start = get_timer (0);
320 /* wait for error or finish */
322 if (*addr == FLASH_STATUS_DONE) {
323 if (*addr == FLASH_STATUS_DONE) {
327 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
328 *addr = FLASH_CMD_RESET;
332 *addr = FLASH_CMD_RESET;
337 * write_buff return values:
340 * 2 - Flash not erased
341 * 4 - Flash not identified
344 write_buff(flash_info_t *info, uchar *src, ulong udst, ulong cnt)
346 volatile FLASH_WORD_SIZE *addr, *dst;
350 if (info->flash_id == FLASH_UNKNOWN) {
354 addr = (volatile FLASH_WORD_SIZE *)(info->start[0]);
355 dst = (volatile FLASH_WORD_SIZE *) udst;
357 #ifdef CONFIG_SYS_FLASH_16BIT
361 /* Check if buffer write is possible */
362 if (cnt > 1 && (((unsigned long)dst & (FLASH_WRITE_BUFFER_SIZE - 1)) == 0)) {
363 bcnt = cnt > FLASH_WRITE_BUFFER_SIZE ? FLASH_WRITE_BUFFER_SIZE : cnt;
364 /* Check if Flash is (sufficiently) erased */
365 for (i=0; i<bcnt; i++) {
366 if ((dst[i] & src[i]) != src[i]) {
370 if (write_buff2( dst,src,bcnt ) != 0) {
371 addr[0] = FLASH_CMD_READ_STATUS;
373 if (poll_status( dst ) != 0) {
382 /* Check if Flash is (sufficiently) erased */
383 if ((*dst & *src) != *src) {
387 /* Disable interrupts which might cause a timeout here */
388 flag = disable_interrupts();
389 addr[0] = FLASH_CMD_ERASE_CONFIRM;
390 addr[0] = FLASH_CMD_WRITE;
392 /* re-enable interrupts if necessary */
397 if (poll_status( dst ) != 0) {
407 flash_real_protect(flash_info_t *info, long sector, int prot)
409 volatile FLASH_WORD_SIZE *addr;
412 addr = (volatile FLASH_WORD_SIZE *)(info->start[sector]);
413 *addr = FLASH_CMD_CLEAR_STATUS;
414 *addr = FLASH_CMD_PROTECT;
417 *addr = FLASH_CMD_PROTECT_SET;
419 *addr = FLASH_CMD_PROTECT_CLEAR;
422 /* wait for error or finish */
423 start = get_timer (0);
424 while(!(addr[0] & FLASH_STATUS_DONE)){
425 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
426 printf("Flash protect timeout at address %lx\n", info->start[sector]);
427 addr[0] = FLASH_CMD_RESET;
432 /* Set software protect flag */
433 info->protect[sector] = prot;
434 *addr = FLASH_CMD_RESET;