]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/RPXlite/flash.c
nand: remove CONFIG_SYS_NAND_PAGE_SIZE
[karo-tx-uboot.git] / board / RPXlite / 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 /*
9  * Yoo. Jonghoon, IPone, yooth@ipone.co.kr
10  * U-Boot port on RPXlite board
11  *
12  * Some of flash control words are modified. (from 2x16bit device
13  * to 4x8bit device)
14  * RPXLite board I tested has only 4 AM29LV800BB devices. Other devices
15  * are not tested.
16  *
17  * (?) Does an RPXLite board which
18  *      does not use AM29LV800 flash memory exist ?
19  *      I don't know...
20  */
21
22 #include <common.h>
23 #include <mpc8xx.h>
24
25 flash_info_t    flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
26
27 /*-----------------------------------------------------------------------
28  * Functions
29  */
30 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
31 static int write_word (flash_info_t *info, ulong dest, ulong data);
32 static void flash_get_offsets (ulong base, flash_info_t *info);
33
34 /*-----------------------------------------------------------------------
35  */
36
37 unsigned long flash_init (void)
38 {
39 /*      volatile immap_t     *immap  = (immap_t *)CONFIG_SYS_IMMR; */
40 /*      volatile memctl8xx_t *memctl = &immap->im_memctl; */
41         unsigned long size_b0 ;
42         int i;
43
44         /* Init: no FLASHes known */
45         for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
46                 flash_info[i].flash_id = FLASH_UNKNOWN;
47         }
48
49         /* Static FLASH Bank configuration here - FIXME XXX */
50 /*
51         size_b0 = flash_get_size((vu_long *)FLASH_BASE_DEBUG, &flash_info[0]);
52
53         if (flash_info[0].flash_id == FLASH_UNKNOWN) {
54                 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
55                         size_b0, size_b0<<20);
56         }
57 */
58         /* Remap FLASH according to real size */
59 /*%%%
60         memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
61         memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
62 %%%*/
63         /* Re-do sizing to get full correct info */
64
65         size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
66         flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
67
68 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
69         /* monitor protection ON by default */
70         flash_protect(FLAG_PROTECT_SET,
71                       CONFIG_SYS_MONITOR_BASE,
72                       CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
73                       &flash_info[0]);
74 #endif
75
76         flash_info[0].size = size_b0;
77
78         return (size_b0);
79 }
80
81 /*-----------------------------------------------------------------------
82  */
83 static void flash_get_offsets (ulong base, flash_info_t *info)
84 {
85         int i;
86
87         /* set up sector start address table */
88         if (info->flash_id & FLASH_BTYPE) {
89                 /* set sector offsets for bottom boot block type        */
90                 info->start[0] = base + 0x00000000;
91                 info->start[1] = base + 0x00010000;
92                 info->start[2] = base + 0x00018000;
93                 info->start[3] = base + 0x00020000;
94                 for (i = 4; i < info->sector_count; i++) {
95                         info->start[i] = base + ((i-3) * 0x00040000) ;
96                 }
97         } else {
98                 /* set sector offsets for top boot block type           */
99                 i = info->sector_count - 1;
100                 info->start[i--] = base + info->size - 0x00010000;
101                 info->start[i--] = base + info->size - 0x00018000;
102                 info->start[i--] = base + info->size - 0x00020000;
103                 for (; i >= 0; i--) {
104                         info->start[i] = base + i * 0x00040000;
105                 }
106         }
107
108 }
109
110 /*-----------------------------------------------------------------------
111  */
112 void flash_print_info  (flash_info_t *info)
113 {
114         int i;
115
116         if (info->flash_id == FLASH_UNKNOWN) {
117                 printf ("missing or unknown FLASH type\n");
118                 return;
119         }
120
121         switch (info->flash_id & FLASH_VENDMASK) {
122         case FLASH_MAN_AMD:     printf ("AMD ");                break;
123         case FLASH_MAN_FUJ:     printf ("FUJITSU ");            break;
124         default:                printf ("Unknown Vendor ");     break;
125         }
126
127         switch (info->flash_id & FLASH_TYPEMASK) {
128         case FLASH_AM400B:      printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
129                                 break;
130         case FLASH_AM400T:      printf ("AM29LV400T (4 Mbit, top boot sector)\n");
131                                 break;
132         case FLASH_AM800B:      printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
133                                 break;
134         case FLASH_AM800T:      printf ("AM29LV800T (8 Mbit, top boot sector)\n");
135                                 break;
136         case FLASH_AM160B:      printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
137                                 break;
138         case FLASH_AM160T:      printf ("AM29LV160T (16 Mbit, top boot sector)\n");
139                                 break;
140         case FLASH_AM320B:      printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
141                                 break;
142         case FLASH_AM320T:      printf ("AM29LV320T (32 Mbit, top boot sector)\n");
143                                 break;
144         default:                printf ("Unknown Chip Type\n");
145                                 break;
146         }
147
148         printf ("  Size: %ld MB in %d Sectors\n",
149                 info->size >> 20, info->sector_count);
150
151         printf ("  Sector Start Addresses:");
152         for (i=0; i<info->sector_count; ++i) {
153                 if ((i % 5) == 0)
154                         printf ("\n   ");
155                 printf (" %08lX%s",
156                         info->start[i],
157                         info->protect[i] ? " (RO)" : "     "
158                 );
159         }
160         printf ("\n");
161         return;
162 }
163
164 /*-----------------------------------------------------------------------
165  */
166
167
168 /*-----------------------------------------------------------------------
169  */
170
171 /*
172  * The following code cannot be run from FLASH!
173  */
174
175 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
176 {
177         short i;
178         ulong value;
179         ulong base = (ulong)addr;
180
181         /* Write auto select command: read Manufacturer ID */
182         addr[0xAAA] = 0x00AA00AA ;
183         addr[0x555] = 0x00550055 ;
184         addr[0xAAA] = 0x00900090 ;
185
186         value = addr[0] ;
187
188         switch (value & 0x00FF00FF) {
189         case AMD_MANUFACT:
190                 info->flash_id = FLASH_MAN_AMD;
191                 break;
192         case FUJ_MANUFACT:
193                 info->flash_id = FLASH_MAN_FUJ;
194                 break;
195         default:
196                 info->flash_id = FLASH_UNKNOWN;
197                 info->sector_count = 0;
198                 info->size = 0;
199                 return (0);                     /* no or unknown flash  */
200         }
201
202         value = addr[2] ;               /* device ID            */
203
204         switch (value & 0x00FF00FF) {
205         case (AMD_ID_LV400T & 0x00FF00FF):
206                 info->flash_id += FLASH_AM400T;
207                 info->sector_count = 11;
208                 info->size = 0x00100000;
209                 break;                          /* => 1 MB              */
210
211         case (AMD_ID_LV400B & 0x00FF00FF):
212                 info->flash_id += FLASH_AM400B;
213                 info->sector_count = 11;
214                 info->size = 0x00100000;
215                 break;                          /* => 1 MB              */
216
217         case (AMD_ID_LV800T & 0x00FF00FF):
218                 info->flash_id += FLASH_AM800T;
219                 info->sector_count = 19;
220                 info->size = 0x00200000;
221                 break;                          /* => 2 MB              */
222
223         case (AMD_ID_LV800B & 0x00FF00FF):
224                 info->flash_id += FLASH_AM800B;
225                 info->sector_count = 19;
226                 info->size = 0x00400000;        /*%%% Size doubled by yooth */
227                 break;                          /* => 4 MB              */
228
229         case (AMD_ID_LV160T & 0x00FF00FF):
230                 info->flash_id += FLASH_AM160T;
231                 info->sector_count = 35;
232                 info->size = 0x00400000;
233                 break;                          /* => 4 MB              */
234
235         case (AMD_ID_LV160B & 0x00FF00FF):
236                 info->flash_id += FLASH_AM160B;
237                 info->sector_count = 35;
238                 info->size = 0x00400000;
239                 break;                          /* => 4 MB              */
240 #if 0   /* enable when device IDs are available */
241         case AMD_ID_LV320T:
242                 info->flash_id += FLASH_AM320T;
243                 info->sector_count = 67;
244                 info->size = 0x00800000;
245                 break;                          /* => 8 MB              */
246
247         case AMD_ID_LV320B:
248                 info->flash_id += FLASH_AM320B;
249                 info->sector_count = 67;
250                 info->size = 0x00800000;
251                 break;                          /* => 8 MB              */
252 #endif
253         default:
254                 info->flash_id = FLASH_UNKNOWN;
255                 return (0);                     /* => no or unknown flash */
256
257         }
258         /*%%% sector start address modified */
259         /* set up sector start address table */
260         if (info->flash_id & FLASH_BTYPE) {
261                 /* set sector offsets for bottom boot block type        */
262                 info->start[0] = base + 0x00000000;
263                 info->start[1] = base + 0x00010000;
264                 info->start[2] = base + 0x00018000;
265                 info->start[3] = base + 0x00020000;
266                 for (i = 4; i < info->sector_count; i++) {
267                         info->start[i] = base + ((i-3) * 0x00040000) ;
268                 }
269         } else {
270                 /* set sector offsets for top boot block type           */
271                 i = info->sector_count - 1;
272                 info->start[i--] = base + info->size - 0x00010000;
273                 info->start[i--] = base + info->size - 0x00018000;
274                 info->start[i--] = base + info->size - 0x00020000;
275                 for (; i >= 0; i--) {
276                         info->start[i] = base + i * 0x00040000;
277                 }
278         }
279
280         /* check for protected sectors */
281         for (i = 0; i < info->sector_count; i++) {
282                 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
283                 /* D0 = 1 if protected */
284                 addr = (volatile unsigned long *)(info->start[i]);
285                 info->protect[i] = addr[4] & 1 ;
286         }
287
288         /*
289          * Prevent writes to uninitialized FLASH.
290          */
291         if (info->flash_id != FLASH_UNKNOWN) {
292                 addr = (volatile unsigned long *)info->start[0];
293
294                 *addr = 0xF0F0F0F0;     /* reset bank */
295         }
296
297         return (info->size);
298 }
299
300
301 /*-----------------------------------------------------------------------
302  */
303
304 int     flash_erase (flash_info_t *info, int s_first, int s_last)
305 {
306         vu_long *addr = (vu_long*)(info->start[0]);
307         int flag, prot, sect, l_sect;
308         ulong start, now, last;
309
310         if ((s_first < 0) || (s_first > s_last)) {
311                 if (info->flash_id == FLASH_UNKNOWN) {
312                         printf ("- missing\n");
313                 } else {
314                         printf ("- no sectors to erase\n");
315                 }
316                 return 1;
317         }
318
319         if ((info->flash_id == FLASH_UNKNOWN) ||
320             (info->flash_id > FLASH_AMD_COMP)) {
321                 printf ("Can't erase unknown flash type %08lx - aborted\n",
322                         info->flash_id);
323                 return 1;
324         }
325
326         prot = 0;
327         for (sect=s_first; sect<=s_last; ++sect) {
328                 if (info->protect[sect]) {
329                         prot++;
330                 }
331         }
332
333         if (prot) {
334                 printf ("- Warning: %d protected sectors will not be erased!\n",
335                         prot);
336         } else {
337                 printf ("\n");
338         }
339
340         l_sect = -1;
341
342         /* Disable interrupts which might cause a timeout here */
343         flag = disable_interrupts();
344
345         addr[0xAAA] = 0xAAAAAAAA;
346         addr[0x555] = 0x55555555;
347         addr[0xAAA] = 0x80808080;
348         addr[0xAAA] = 0xAAAAAAAA;
349         addr[0x555] = 0x55555555;
350
351         /* Start erase on unprotected sectors */
352         for (sect = s_first; sect<=s_last; sect++) {
353                 if (info->protect[sect] == 0) { /* not protected */
354                         addr = (vu_long *)(info->start[sect]) ;
355                         addr[0] = 0x30303030 ;
356                         l_sect = sect;
357                 }
358         }
359
360         /* re-enable interrupts if necessary */
361         if (flag)
362                 enable_interrupts();
363
364         /* wait at least 80us - let's wait 1 ms */
365         udelay (1000);
366
367         /*
368          * We wait for the last triggered sector
369          */
370         if (l_sect < 0)
371                 goto DONE;
372
373         start = get_timer (0);
374         last  = start;
375         addr = (vu_long *)(info->start[l_sect]);
376         while ((addr[0] & 0x80808080) != 0x80808080) {
377                 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
378                         printf ("Timeout\n");
379                         return 1;
380                 }
381                 /* show that we're waiting */
382                 if ((now - last) > 1000) {      /* every second */
383                         putc ('.');
384                         last = now;
385                 }
386         }
387
388 DONE:
389         /* reset to read mode */
390         addr = (vu_long *)info->start[0];
391         addr[0] = 0xF0F0F0F0;   /* reset bank */
392
393         printf (" done\n");
394         return 0;
395 }
396
397 /*-----------------------------------------------------------------------
398  * Copy memory to flash, returns:
399  * 0 - OK
400  * 1 - write timeout
401  * 2 - Flash not erased
402  */
403
404 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
405 {
406         ulong cp, wp, data;
407         int i, l, rc;
408
409         wp = (addr & ~3);       /* get lower word aligned address */
410
411         /*
412          * handle unaligned start bytes
413          */
414         if ((l = addr - wp) != 0) {
415                 data = 0;
416                 for (i=0, cp=wp; i<l; ++i, ++cp) {
417                         data = (data << 8) | (*(uchar *)cp);
418                 }
419                 for (; i<4 && cnt>0; ++i) {
420                         data = (data << 8) | *src++;
421                         --cnt;
422                         ++cp;
423                 }
424                 for (; cnt==0 && i<4; ++i, ++cp) {
425                         data = (data << 8) | (*(uchar *)cp);
426                 }
427
428                 if ((rc = write_word(info, wp, data)) != 0) {
429                         return (rc);
430                 }
431                 wp += 4;
432         }
433
434         /*
435          * handle word aligned part
436          */
437         while (cnt >= 4) {
438                 data = 0;
439                 for (i=0; i<4; ++i) {
440                         data = (data << 8) | *src++;
441                 }
442                 if ((rc = write_word(info, wp, data)) != 0) {
443                         return (rc);
444                 }
445                 wp  += 4;
446                 cnt -= 4;
447         }
448
449         if (cnt == 0) {
450                 return (0);
451         }
452
453         /*
454          * handle unaligned tail bytes
455          */
456         data = 0;
457         for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
458                 data = (data << 8) | *src++;
459                 --cnt;
460         }
461         for (; i<4; ++i, ++cp) {
462                 data = (data << 8) | (*(uchar *)cp);
463         }
464
465         return (write_word(info, wp, data));
466 }
467
468 /*-----------------------------------------------------------------------
469  * Write a word to Flash, returns:
470  * 0 - OK
471  * 1 - write timeout
472  * 2 - Flash not erased
473  */
474 static int write_word (flash_info_t *info, ulong dest, ulong data)
475 {
476         vu_long *addr = (vu_long *)(info->start[0]);
477         ulong start;
478         int flag;
479
480         /* Check if Flash is (sufficiently) erased */
481         if ((*((vu_long *)dest) & data) != data) {
482                 return (2);
483         }
484         /* Disable interrupts which might cause a timeout here */
485         flag = disable_interrupts();
486
487         addr[0xAAA] = 0xAAAAAAAA;
488         addr[0x555] = 0x55555555;
489         addr[0xAAA] = 0xA0A0A0A0;
490
491         *((vu_long *)dest) = data;
492
493         /* re-enable interrupts if necessary */
494         if (flag)
495                 enable_interrupts();
496
497         /* data polling for D7 */
498         start = get_timer (0);
499         while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
500                 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
501                         return (1);
502                 }
503         }
504         return (0);
505 }
506
507 /*-----------------------------------------------------------------------
508  */