]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/cogent/flash.c
Coding Style cleanup: replace leading SPACEs by TABs
[karo-tx-uboot.git] / board / cogent / flash.c
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <board/cogent/flash.h>
10 #include <linux/compiler.h>
11
12 flash_info_t    flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
13
14 #if defined(CONFIG_ENV_IS_IN_FLASH)
15 # ifndef  CONFIG_ENV_ADDR
16 #  define CONFIG_ENV_ADDR       (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
17 # endif
18 # ifndef  CONFIG_ENV_SIZE
19 #  define CONFIG_ENV_SIZE       CONFIG_ENV_SECT_SIZE
20 # endif
21 # ifndef  CONFIG_ENV_SECT_SIZE
22 #  define CONFIG_ENV_SECT_SIZE  CONFIG_ENV_SIZE
23 # endif
24 #endif
25
26 /*-----------------------------------------------------------------------
27  * Functions
28  */
29 static int write_word (flash_info_t *info, ulong dest, ulong data);
30
31 /*-----------------------------------------------------------------------
32  */
33
34
35 #if defined(CONFIG_CMA302)
36
37 /*
38  * probe for the existence of flash at address "addr"
39  * 0 = yes, 1 = bad Manufacturer's Id, 2 = bad Device Id
40  */
41 static int
42 c302f_probe_word(c302f_addr_t addr)
43 {
44         /* reset the flash */
45         *addr = C302F_BNK_CMD_RST;
46
47         /* check the manufacturer id */
48         *addr = C302F_BNK_CMD_RD_ID;
49         if (*C302F_BNK_ADDR_MAN(addr) != C302F_BNK_RD_ID_MAN)
50                 return 1;
51
52         /* check the device id */
53         *addr = C302F_BNK_CMD_RD_ID;
54         if (*C302F_BNK_ADDR_DEV(addr) != C302F_BNK_RD_ID_DEV)
55                 return 2;
56
57 #ifdef FLASH_DEBUG
58         {
59                 int i;
60
61                 printf("\nMaster Lock Config = 0x%08lx\n",
62                         *C302F_BNK_ADDR_CFGM(addr));
63                 for (i = 0; i < C302F_BNK_NBLOCKS; i++)
64                         printf("Block %2d Lock Config = 0x%08lx\n",
65                                 i, *C302F_BNK_ADDR_CFG(i, addr));
66         }
67 #endif
68
69         /* reset the flash again */
70         *addr = C302F_BNK_CMD_RST;
71
72         return 0;
73 }
74
75 /*
76  * probe for Cogent CMA302 flash module at address "base" and store
77  * info for any found into flash_info entry "fip". Must find at least
78  * one bank.
79  */
80 static void
81 c302f_probe(flash_info_t *fip, c302f_addr_t base)
82 {
83         c302f_addr_t addr, eaddr;
84         int nbanks;
85
86         fip->size = 0L;
87         fip->sector_count = 0;
88
89         addr = base;
90         eaddr = C302F_BNK_ADDR_BASE(addr, C302F_MAX_BANKS);
91         nbanks = 0;
92
93         while (addr < eaddr) {
94                 c302f_addr_t addrw, eaddrw, addrb;
95                 int i, osc, nsc;
96
97                 addrw = addr;
98                 eaddrw = C302F_BNK_ADDR_NEXT_WORD(addrw);
99
100                 while (addrw < eaddrw)
101                         if (c302f_probe_word(addrw++) != 0)
102                                 goto out;
103
104                 /* bank exists - append info for this bank to *fip */
105                 fip->flash_id = FLASH_MAN_INTEL|FLASH_28F008S5;
106                 fip->size += C302F_BNK_SIZE;
107                 osc = fip->sector_count;
108                 fip->sector_count += C302F_BNK_NBLOCKS;
109                 if ((nsc = fip->sector_count) >= CONFIG_SYS_MAX_FLASH_SECT)
110                         panic("Too many sectors in flash at address 0x%08lx\n",
111                                 (unsigned long)base);
112
113                 addrb = addr;
114                 for (i = osc; i < nsc; i++) {
115                         fip->start[i] = (ulong)addrb;
116                         fip->protect[i] = 0;
117                         addrb = C302F_BNK_ADDR_NEXT_BLK(addrb);
118                 }
119
120                 addr = C302F_BNK_ADDR_NEXT_BNK(addr);
121                 nbanks++;
122         }
123
124 out:
125         if (nbanks == 0)
126                 panic("ERROR: no flash found at address 0x%08lx\n",
127                         (unsigned long)base);
128 }
129
130 static void
131 c302f_reset(flash_info_t *info, int sect)
132 {
133         c302f_addr_t addrw, eaddrw;
134
135         addrw = (c302f_addr_t)info->start[sect];
136         eaddrw = C302F_BNK_ADDR_NEXT_WORD(addrw);
137
138         while (addrw < eaddrw) {
139 #ifdef FLASH_DEBUG
140                 printf("  writing reset cmd to addr 0x%08lx\n",
141                         (unsigned long)addrw);
142 #endif
143                 *addrw = C302F_BNK_CMD_RST;
144                 addrw++;
145         }
146 }
147
148 static void
149 c302f_erase_init(flash_info_t *info, int sect)
150 {
151         c302f_addr_t addrw, saddrw, eaddrw;
152         int flag;
153
154 #ifdef FLASH_DEBUG
155         printf("0x%08lx C302F_BNK_CMD_PROG\n", C302F_BNK_CMD_PROG);
156         printf("0x%08lx C302F_BNK_CMD_ERASE1\n", C302F_BNK_CMD_ERASE1);
157         printf("0x%08lx C302F_BNK_CMD_ERASE2\n", C302F_BNK_CMD_ERASE2);
158         printf("0x%08lx C302F_BNK_CMD_CLR_STAT\n", C302F_BNK_CMD_CLR_STAT);
159         printf("0x%08lx C302F_BNK_CMD_RST\n", C302F_BNK_CMD_RST);
160         printf("0x%08lx C302F_BNK_STAT_RDY\n", C302F_BNK_STAT_RDY);
161         printf("0x%08lx C302F_BNK_STAT_ERR\n", C302F_BNK_STAT_ERR);
162 #endif
163
164         saddrw = (c302f_addr_t)info->start[sect];
165         eaddrw = C302F_BNK_ADDR_NEXT_WORD(saddrw);
166
167 #ifdef FLASH_DEBUG
168         printf("erasing sector %d, start addr = 0x%08lx "
169                 "(bank next word addr = 0x%08lx)\n", sect,
170                 (unsigned long)saddrw, (unsigned long)eaddrw);
171 #endif
172
173         /* Disable intrs which might cause a timeout here */
174         flag = disable_interrupts();
175
176         for (addrw = saddrw; addrw < eaddrw; addrw++) {
177 #ifdef FLASH_DEBUG
178                 printf("  writing erase cmd to addr 0x%08lx\n",
179                         (unsigned long)addrw);
180 #endif
181                 *addrw = C302F_BNK_CMD_ERASE1;
182                 *addrw = C302F_BNK_CMD_ERASE2;
183         }
184
185         /* re-enable interrupts if necessary */
186         if (flag)
187                 enable_interrupts();
188 }
189
190 static int
191 c302f_erase_poll(flash_info_t *info, int sect)
192 {
193         c302f_addr_t addrw, saddrw, eaddrw;
194         int sectdone, haderr;
195
196         saddrw = (c302f_addr_t)info->start[sect];
197         eaddrw = C302F_BNK_ADDR_NEXT_WORD(saddrw);
198
199         sectdone = 1;
200         haderr = 0;
201
202         for (addrw = saddrw; addrw < eaddrw; addrw++) {
203                 c302f_word_t stat = *addrw;
204
205 #ifdef FLASH_DEBUG
206                 printf("  checking status at addr "
207                         "0x%08lx [0x%08lx]\n",
208                         (unsigned long)addrw, stat);
209 #endif
210                 if ((stat & C302F_BNK_STAT_RDY) != C302F_BNK_STAT_RDY)
211                         sectdone = 0;
212                 else if ((stat & C302F_BNK_STAT_ERR) != 0) {
213                         printf(" failed on sector %d "
214                                 "(stat = 0x%08lx) at "
215                                 "address 0x%08lx\n",
216                                 sect, stat,
217                                 (unsigned long)addrw);
218                         *addrw = C302F_BNK_CMD_CLR_STAT;
219                         haderr = 1;
220                 }
221         }
222
223         if (haderr)
224                 return (-1);
225         else
226                 return (sectdone);
227 }
228
229 static int
230 c302f_write_word(c302f_addr_t addr, c302f_word_t value)
231 {
232         c302f_word_t stat;
233         ulong start;
234         int flag, retval;
235
236         /* Disable interrupts which might cause a timeout here */
237         flag = disable_interrupts();
238
239         *addr = C302F_BNK_CMD_PROG;
240
241         *addr = value;
242
243         /* re-enable interrupts if necessary */
244         if (flag)
245                 enable_interrupts();
246
247         retval = 0;
248
249         /* data polling for D7 */
250         start = get_timer (0);
251         do {
252                 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
253                         retval = 1;
254                         goto done;
255                 }
256                 stat = *addr;
257         } while ((stat & C302F_BNK_STAT_RDY) != C302F_BNK_STAT_RDY);
258
259         if ((stat & C302F_BNK_STAT_ERR) != 0) {
260                 printf("flash program failed (stat = 0x%08lx) "
261                         "at address 0x%08lx\n", (ulong)stat, (ulong)addr);
262                 *addr = C302F_BNK_CMD_CLR_STAT;
263                 retval = 3;
264         }
265
266 done:
267         /* reset to read mode */
268         *addr = C302F_BNK_CMD_RST;
269
270         return (retval);
271 }
272
273 #endif  /* CONFIG_CMA302 */
274
275 unsigned long
276 flash_init(void)
277 {
278         unsigned long total;
279         int i;
280         __maybe_unused flash_info_t *fip;
281
282         /* Init: no FLASHes known */
283         for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
284                 flash_info[i].flash_id = FLASH_UNKNOWN;
285         }
286
287         fip = &flash_info[0];
288         total = 0L;
289
290 #if defined(CONFIG_CMA302)
291         c302f_probe(fip, (c302f_addr_t)CONFIG_SYS_FLASH_BASE);
292         total += fip->size;
293         fip++;
294 #endif
295
296 #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
297         /* not yet ...
298         cmbf_probe(fip, (cmbf_addr_t)CMA_MB_FLASH_BASE);
299         total += fip->size;
300         fip++;
301         */
302 #endif
303
304         /*
305          * protect monitor and environment sectors
306          */
307
308 #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
309         flash_protect(FLAG_PROTECT_SET,
310                       CONFIG_SYS_MONITOR_BASE,
311                       CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
312                       &flash_info[0]);
313 #endif
314
315 #ifdef  CONFIG_ENV_IS_IN_FLASH
316         /* ENV protection ON by default */
317         flash_protect(FLAG_PROTECT_SET,
318                       CONFIG_ENV_ADDR,
319                       CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
320                       &flash_info[0]);
321 #endif
322         return total;
323 }
324
325 /*-----------------------------------------------------------------------
326  */
327 void
328 flash_print_info(flash_info_t *info)
329 {
330         int i;
331
332         if (info->flash_id == FLASH_UNKNOWN) {
333                 printf ("missing or unknown FLASH type\n");
334                 return;
335         }
336
337         switch (info->flash_id & FLASH_VENDMASK) {
338         case FLASH_MAN_INTEL:   printf ("INTEL ");              break;
339         default:                printf ("Unknown Vendor ");     break;
340         }
341
342         switch (info->flash_id & FLASH_TYPEMASK) {
343         case FLASH_28F008S5:    printf ("28F008S5\n");
344                                 break;
345         default:                printf ("Unknown Chip Type\n");
346                                 break;
347         }
348
349         printf ("  Size: %ld MB in %d Sectors\n",
350                 info->size >> 20, info->sector_count);
351
352         printf ("  Sector Start Addresses:");
353         for (i=0; i<info->sector_count; ++i) {
354                 if ((i % 4) == 0)
355                         printf ("\n   ");
356                 printf (" %2d - %08lX%s", i,
357                         info->start[i],
358                         info->protect[i] ? " (RO)" : "     "
359                 );
360         }
361         printf ("\n");
362         return;
363 }
364
365 /*-----------------------------------------------------------------------
366  */
367
368
369 /*-----------------------------------------------------------------------
370  */
371
372 /*
373  * The following code cannot be run from FLASH!
374  */
375
376 int
377 flash_erase(flash_info_t *info, int s_first, int s_last)
378 {
379         int prot, sect, haderr;
380         ulong start, now, last;
381         void (*erase_init)(flash_info_t *, int);
382         int (*erase_poll)(flash_info_t *, int);
383         void (*reset)(flash_info_t *, int);
384         int rcode = 0;
385
386 #ifdef FLASH_DEBUG
387         printf("\nflash_erase: erase %d sectors (%d to %d incl.) from\n"
388                 "  Bank # %d: ", s_last - s_first + 1, s_first, s_last,
389                 (info - flash_info) + 1);
390         flash_print_info(info);
391 #endif
392
393         if ((s_first < 0) || (s_first > s_last)) {
394                 if (info->flash_id == FLASH_UNKNOWN) {
395                         printf ("- missing\n");
396                 } else {
397                         printf ("- no sectors to erase\n");
398                 }
399                 return 1;
400         }
401
402         switch (info->flash_id) {
403
404 #if defined(CONFIG_CMA302)
405         case FLASH_MAN_INTEL|FLASH_28F008S5:
406                 erase_init = c302f_erase_init;
407                 erase_poll = c302f_erase_poll;
408                 reset = c302f_reset;
409                 break;
410 #endif
411
412 #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
413         case FLASH_MAN_INTEL|FLASH_28F800_B:
414         case FLASH_MAN_AMD|FLASH_AM29F800B:
415                 /* not yet ...
416                 erase_init = cmbf_erase_init;
417                 erase_poll = cmbf_erase_poll;
418                 reset = cmbf_reset;
419                 break;
420                 */
421 #endif
422
423         default:
424                 printf ("Flash type %08lx not supported - aborted\n",
425                         info->flash_id);
426                 return 1;
427         }
428
429         prot = 0;
430         for (sect=s_first; sect<=s_last; ++sect) {
431                 if (info->protect[sect]) {
432                         prot++;
433                 }
434         }
435
436         if (prot) {
437                 printf("- Warning: %d protected sector%s will not be erased!\n",
438                         prot, (prot > 1 ? "s" : ""));
439         }
440
441         start = get_timer (0);
442         last = 0;
443         haderr = 0;
444
445         for (sect = s_first; sect <= s_last; sect++) {
446                 if (info->protect[sect] == 0) { /* not protected */
447                         ulong estart;
448                         int sectdone;
449
450                         (*erase_init)(info, sect);
451
452                         /* wait at least 80us - let's wait 1 ms */
453                         udelay (1000);
454
455                         estart = get_timer(start);
456
457                         do {
458                                 now = get_timer(start);
459
460                                 if (now - estart > CONFIG_SYS_FLASH_ERASE_TOUT) {
461                                         printf ("Timeout (sect %d)\n", sect);
462                                         haderr = 1;
463                                         break;
464                                 }
465
466 #ifndef FLASH_DEBUG
467                                 /* show that we're waiting */
468                                 if ((now - last) > 1000) { /* every second */
469                                         putc ('.');
470                                         last = now;
471                                 }
472 #endif
473
474                                 sectdone = (*erase_poll)(info, sect);
475
476                                 if (sectdone < 0) {
477                                         haderr = 1;
478                                         break;
479                                 }
480
481                         } while (!sectdone);
482
483                         if (haderr)
484                                 break;
485                 }
486         }
487
488         if (haderr > 0) {
489                 printf (" failed\n");
490                 rcode = 1;
491         }
492         else
493                 printf (" done\n");
494
495         /* reset to read mode */
496         for (sect = s_first; sect <= s_last; sect++) {
497                 if (info->protect[sect] == 0) { /* not protected */
498                         (*reset)(info, sect);
499                 }
500         }
501         return rcode;
502 }
503
504 /*-----------------------------------------------------------------------
505  * Copy memory to flash, returns:
506  * 0 - OK
507  * 1 - write timeout
508  * 2 - Flash not erased
509  * 3 - write error
510  */
511
512 int
513 write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
514 {
515         ulong cp, wp, data;
516         int i, l, rc;
517         ulong start, now, last;
518
519         wp = (addr & ~3);       /* get lower word aligned address */
520
521         /*
522          * handle unaligned start bytes
523          */
524         if ((l = addr - wp) != 0) {
525                 data = 0;
526                 for (i=0, cp=wp; i<l; ++i, ++cp) {
527                         data = (data << 8) | (*(uchar *)cp);
528                 }
529                 for (; i<4 && cnt>0; ++i) {
530                         data = (data << 8) | *src++;
531                         --cnt;
532                         ++cp;
533                 }
534                 for (; cnt==0 && i<4; ++i, ++cp) {
535                         data = (data << 8) | (*(uchar *)cp);
536                 }
537
538                 if ((rc = write_word(info, wp, data)) != 0) {
539                         return (rc);
540                 }
541                 wp += 4;
542         }
543
544         /*
545          * handle word aligned part
546          */
547         start = get_timer (0);
548         last = 0;
549         while (cnt >= 4) {
550                 data = 0;
551                 for (i=0; i<4; ++i) {
552                         data = (data << 8) | *src++;
553                 }
554                 if ((rc = write_word(info, wp, data)) != 0) {
555                         return (rc);
556                 }
557                 wp  += 4;
558                 cnt -= 4;
559
560                 /* show that we're waiting */
561                 now = get_timer(start);
562                 if ((now - last) > 1000) {      /* every second */
563                         putc ('.');
564                         last = now;
565                 }
566         }
567
568         if (cnt == 0) {
569                 return (0);
570         }
571
572         /*
573          * handle unaligned tail bytes
574          */
575         data = 0;
576         for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
577                 data = (data << 8) | *src++;
578                 --cnt;
579         }
580         for (; i<4; ++i, ++cp) {
581                 data = (data << 8) | (*(uchar *)cp);
582         }
583
584         return (write_word(info, wp, data));
585 }
586
587 /*-----------------------------------------------------------------------
588  * Write a word to Flash, returns:
589  * 0 - OK
590  * 1 - write timeout
591  * 2 - Flash not erased
592  * 3 - write error
593  */
594 static int
595 write_word(flash_info_t *info, ulong dest, ulong data)
596 {
597         int retval;
598
599         /* Check if Flash is (sufficiently) erased */
600         if ((*(ulong *)dest & data) != data) {
601                 return (2);
602         }
603
604         switch (info->flash_id) {
605
606 #if defined(CONFIG_CMA302)
607         case FLASH_MAN_INTEL|FLASH_28F008S5:
608                 retval = c302f_write_word((c302f_addr_t)dest, (c302f_word_t)data);
609                 break;
610 #endif
611
612 #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
613         case FLASH_MAN_INTEL|FLASH_28F800_B:
614         case FLASH_MAN_AMD|FLASH_AM29F800B:
615                 /* not yet ...
616                 retval = cmbf_write_word((cmbf_addr_t)dest, (cmbf_word_t)data);
617                 */
618                 retval = 3;
619                 break;
620 #endif
621
622         default:
623                 printf ("Flash type %08lx not supported - aborted\n",
624                         info->flash_id);
625                 retval = 3;
626                 break;
627         }
628
629         return (retval);
630 }
631
632 /*-----------------------------------------------------------------------
633  */