]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/iphase4539/flash.c
d3122edf21c828463b92350977ca55ed88bff4f1
[karo-tx-uboot.git] / board / iphase4539 / flash.c
1 /*
2  * (C) Copyright 2001
3  * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
4  *
5  * (C) Copyright 2002
6  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7  *
8  * Adapted for Interphase 4539 by Wolfgang Grandegger <wg@denx.de>.
9  *
10  * SPDX-License-Identifier:     GPL-2.0+ 
11  */
12
13 #include <config.h>
14 #include <common.h>
15 #include <flash.h>
16 #include <asm/io.h>
17
18 flash_info_t    flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
19
20 extern int hwc_flash_size(void);
21 static ulong flash_get_size (u32 addr, flash_info_t *info);
22 static int flash_get_offsets (u32 base, flash_info_t *info);
23 static int write_word (flash_info_t *info, ulong dest, ulong data);
24 static void flash_reset (u32 addr);
25
26 #define out8(a,v) *(volatile unsigned char*)(a) = v
27 #define in8(a)    *(volatile unsigned char*)(a)
28 #define in32(a)   *(volatile unsigned long*)(a)
29 #define iobarrier_rw() eieio()
30
31 unsigned long flash_init (void)
32 {
33         unsigned int i;
34         unsigned long flash_size = 0;
35         unsigned long bank_size;
36         unsigned int bank = 0;
37
38         /* Init: no FLASHes known */
39         for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
40                 flash_info[i].flash_id = FLASH_UNKNOWN;
41                 flash_info[i].sector_count = 0;
42                 flash_info[i].size = 0;
43         }
44
45         /* Initialise the BOOT Flash */
46         if (bank == CONFIG_SYS_MAX_FLASH_BANKS) {
47                 puts ("Warning: not all Flashes are initialised !");
48                 return flash_size;
49         }
50
51         bank_size = flash_get_size (CONFIG_SYS_FLASH_BASE, flash_info + bank);
52         if (bank_size) {
53 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE && \
54     CONFIG_SYS_MONITOR_BASE < CONFIG_SYS_FLASH_BASE + CONFIG_SYS_MAX_FLASH_SIZE
55                 /* monitor protection ON by default */
56                 flash_protect(FLAG_PROTECT_SET,
57                               CONFIG_SYS_MONITOR_BASE,
58                               CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
59                               flash_info + bank);
60 #endif
61
62 #ifdef CONFIG_ENV_IS_IN_FLASH
63                 /* ENV protection ON by default */
64                 flash_protect(FLAG_PROTECT_SET,
65                               CONFIG_ENV_ADDR,
66                               CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
67                               flash_info + bank);
68 #endif
69
70                 /* HWC protection ON by default */
71                 flash_protect(FLAG_PROTECT_SET,
72                               CONFIG_SYS_FLASH_BASE,
73                               CONFIG_SYS_FLASH_BASE + 0x10000 - 1,
74                               flash_info + bank);
75
76                 flash_size += bank_size;
77                 bank++;
78         } else {
79                 puts ("Warning: the BOOT Flash is not initialised !");
80         }
81
82         return flash_size;
83 }
84
85 /*
86  * The following code cannot be run from FLASH!
87  */
88 static ulong flash_get_size (u32 addr, flash_info_t *info)
89 {
90         volatile uchar value;
91 #if 0
92         int i;
93 #endif
94
95         /* Write auto select command: read Manufacturer ID */
96         out8(addr + 0x0555, 0xAA);
97         iobarrier_rw();
98         udelay(10);
99         out8(addr + 0x02AA, 0x55);
100         iobarrier_rw();
101         udelay(10);
102         out8(addr + 0x0555, 0x90);
103         iobarrier_rw();
104         udelay(10);
105
106         value = in8(addr);
107         iobarrier_rw();
108         udelay(10);
109         switch (value | (value << 16)) {
110         case AMD_MANUFACT:
111                 info->flash_id = FLASH_MAN_AMD;
112                 break;
113
114         case FUJ_MANUFACT:
115                 info->flash_id = FLASH_MAN_FUJ;
116                 break;
117
118         default:
119                 info->flash_id = FLASH_UNKNOWN;
120                 flash_reset (addr);
121                 return 0;
122         }
123
124         value = in8(addr + 1);                  /* device ID            */
125         iobarrier_rw();
126
127         switch (value) {
128         case AMD_ID_LV033C:
129                 info->flash_id += FLASH_AM033C;
130                 info->size = hwc_flash_size();
131                 if (info->size > CONFIG_SYS_MAX_FLASH_SIZE) {
132                         printf("U-Boot supports only %d MB\n",
133                                CONFIG_SYS_MAX_FLASH_SIZE);
134                         info->size = CONFIG_SYS_MAX_FLASH_SIZE;
135                 }
136                 info->sector_count = info->size / 0x10000;
137                 break;                          /* => 4 MB              */
138
139         default:
140                 info->flash_id = FLASH_UNKNOWN;
141                 flash_reset (addr);
142                 return (0);                     /* => no or unknown flash */
143
144         }
145
146         if (!flash_get_offsets (addr, info)) {
147                 flash_reset (addr);
148                 return 0;
149         }
150
151 #if 0
152         /* check for protected sectors */
153         for (i = 0; i < info->sector_count; i++) {
154                 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
155                 /* D0 = 1 if protected */
156                 value = in8(info->start[i] + 2);
157                 iobarrier_rw();
158                 info->protect[i] = (value & 1) != 0;
159         }
160 #endif
161
162         /*
163          * Reset bank to read mode
164          */
165         flash_reset (addr);
166
167         return (info->size);
168 }
169
170 static int flash_get_offsets (u32 base, flash_info_t *info)
171 {
172         unsigned int i, size;
173
174         switch (info->flash_id & FLASH_TYPEMASK) {
175         case FLASH_AM033C:
176                 /* set sector offsets for uniform sector type   */
177                 size = info->size / info->sector_count;
178                 for (i = 0; i < info->sector_count; i++) {
179                         info->start[i] = base + i * size;
180                 }
181                 break;
182         default:
183                 return 0;
184         }
185
186         return 1;
187 }
188
189 int flash_erase (flash_info_t *info, int s_first, int s_last)
190 {
191         volatile u32 addr = info->start[0];
192         int flag, prot, sect, l_sect;
193         ulong start, now, last;
194
195         if (s_first < 0 || s_first > s_last) {
196                 if (info->flash_id == FLASH_UNKNOWN) {
197                         printf ("- missing\n");
198                 } else {
199                         printf ("- no sectors to erase\n");
200                 }
201                 return 1;
202         }
203
204         if (info->flash_id == FLASH_UNKNOWN ||
205             info->flash_id > FLASH_AMD_COMP) {
206                 printf ("Can't erase unknown flash type %08lx - aborted\n",
207                         info->flash_id);
208                 return 1;
209         }
210
211         prot = 0;
212         for (sect=s_first; sect<=s_last; ++sect) {
213                 if (info->protect[sect]) {
214                         prot++;
215                 }
216         }
217
218         if (prot) {
219                 printf ("- Warning: %d protected sectors will not be erased!\n",
220                         prot);
221         } else {
222                 printf ("\n");
223         }
224
225         l_sect = -1;
226
227         /* Disable interrupts which might cause a timeout here */
228         flag = disable_interrupts();
229
230         out8(addr + 0x555, 0xAA);
231         iobarrier_rw();
232         out8(addr + 0x2AA, 0x55);
233         iobarrier_rw();
234         out8(addr + 0x555, 0x80);
235         iobarrier_rw();
236         out8(addr + 0x555, 0xAA);
237         iobarrier_rw();
238         out8(addr + 0x2AA, 0x55);
239         iobarrier_rw();
240
241         /* Start erase on unprotected sectors */
242         for (sect = s_first; sect<=s_last; sect++) {
243                 if (info->protect[sect] == 0) { /* not protected */
244                         addr = info->start[sect];
245                         out8(addr, 0x30);
246                         iobarrier_rw();
247                         l_sect = sect;
248                 }
249         }
250
251         /* re-enable interrupts if necessary */
252         if (flag)
253                 enable_interrupts();
254
255         /* wait at least 80us - let's wait 1 ms */
256         udelay (1000);
257
258         /*
259          * We wait for the last triggered sector
260          */
261         if (l_sect < 0)
262                 goto DONE;
263
264         start = get_timer (0);
265         last  = start;
266         addr = info->start[l_sect];
267         while ((in8(addr) & 0x80) != 0x80) {
268                 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
269                         printf ("Timeout\n");
270                         return 1;
271                 }
272                 /* show that we're waiting */
273                 if ((now - last) > 1000) {      /* every second */
274                         putc ('.');
275                         last = now;
276                 }
277                 iobarrier_rw();
278         }
279
280 DONE:
281         /* reset to read mode */
282         flash_reset (info->start[0]);
283
284         printf (" done\n");
285         return 0;
286 }
287
288 /*
289  * Copy memory to flash, returns:
290  * 0 - OK
291  * 1 - write timeout
292  * 2 - Flash not erased
293  */
294 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
295 {
296         ulong cp, wp, data;
297         int i, l, rc;
298
299         wp = (addr & ~3);       /* get lower word aligned address */
300
301         /*
302          * handle unaligned start bytes
303          */
304         if ((l = addr - wp) != 0) {
305                 data = 0;
306                 for (i=0, cp=wp; i<l; ++i, ++cp) {
307                         data = (data << 8) | (*(uchar *)cp);
308                 }
309                 for (; i<4 && cnt>0; ++i) {
310                         data = (data << 8) | *src++;
311                         --cnt;
312                         ++cp;
313                 }
314                 for (; cnt==0 && i<4; ++i, ++cp) {
315                         data = (data << 8) | (*(uchar *)cp);
316                 }
317
318                 if ((rc = write_word(info, wp, data)) != 0) {
319                         return (rc);
320                 }
321                 wp += 4;
322         }
323
324         /*
325          * handle word aligned part
326          */
327         while (cnt >= 4) {
328                 data = 0;
329                 for (i=0; i<4; ++i) {
330                         data = (data << 8) | *src++;
331                 }
332                 if ((rc = write_word(info, wp, data)) != 0) {
333                         return (rc);
334                 }
335                 wp  += 4;
336                 cnt -= 4;
337         }
338
339         if (cnt == 0) {
340                 return (0);
341         }
342
343         /*
344          * handle unaligned tail bytes
345          */
346         data = 0;
347         for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
348                 data = (data << 8) | *src++;
349                 --cnt;
350         }
351         for (; i<4; ++i, ++cp) {
352                 data = (data << 8) | (*(uchar *)cp);
353         }
354
355         return (write_word(info, wp, data));
356 }
357
358 /*
359  * Write a word to Flash, returns:
360  * 0 - OK
361  * 1 - write timeout
362  * 2 - Flash not erased
363  */
364 static int write_word (flash_info_t *info, ulong dest, ulong data)
365 {
366         volatile u32 addr = info->start[0];
367         ulong start;
368         int flag, i;
369
370         /* Check if Flash is (sufficiently) erased */
371         if ((in32(dest) & data) != data) {
372                 return (2);
373         }
374         /* Disable interrupts which might cause a timeout here */
375         flag = disable_interrupts();
376
377         /* first, perform an unlock bypass command to speed up flash writes */
378         out8(addr + 0x555, 0xAA);
379         iobarrier_rw();
380         out8(addr + 0x2AA, 0x55);
381         iobarrier_rw();
382         out8(addr + 0x555, 0x20);
383         iobarrier_rw();
384
385         /* write each byte out */
386         for (i = 0; i < 4; i++) {
387                 char *data_ch = (char *)&data;
388                 out8(addr, 0xA0);
389                 iobarrier_rw();
390                 out8(dest+i, data_ch[i]);
391                 iobarrier_rw();
392                 udelay(10); /* XXX */
393         }
394
395         /* we're done, now do an unlock bypass reset */
396         out8(addr, 0x90);
397         iobarrier_rw();
398         out8(addr, 0x00);
399         iobarrier_rw();
400
401         /* re-enable interrupts if necessary */
402         if (flag)
403                 enable_interrupts();
404
405         /* data polling for D7 */
406         start = get_timer (0);
407         while ((in32(dest) & 0x80808080) != (data & 0x80808080)) {
408                 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
409                         return (1);
410                 }
411                 iobarrier_rw();
412         }
413
414         flash_reset (addr);
415
416         return (0);
417 }
418
419 /*
420  * Reset bank to read mode
421  */
422 static void flash_reset (u32 addr)
423 {
424         out8(addr, 0xF0);       /* reset bank */
425         iobarrier_rw();
426 }
427
428 void flash_print_info (flash_info_t *info)
429 {
430         int i;
431
432         if (info->flash_id == FLASH_UNKNOWN) {
433                 printf ("missing or unknown FLASH type\n");
434                 return;
435         }
436
437         switch (info->flash_id & FLASH_VENDMASK) {
438         case FLASH_MAN_AMD:     printf ("AMD ");                break;
439         case FLASH_MAN_FUJ:     printf ("FUJITSU ");            break;
440         case FLASH_MAN_BM:      printf ("BRIGHT MICRO ");       break;
441         default:                printf ("Unknown Vendor ");     break;
442         }
443
444         switch (info->flash_id & FLASH_TYPEMASK) {
445         case FLASH_AM033C:      printf ("AM29LV033C (32 Mbit, uniform sectors)\n");
446                                 break;
447         default:                printf ("Unknown Chip Type\n");
448                                 break;
449         }
450
451         if (info->size % 0x100000 == 0) {
452                 printf ("  Size: %ld MB in %d Sectors\n",
453                         info->size / 0x100000, info->sector_count);
454         }
455         else if (info->size % 0x400 == 0) {
456                 printf ("  Size: %ld KB in %d Sectors\n",
457                         info->size / 0x400, info->sector_count);
458         }
459         else {
460                 printf ("  Size: %ld B in %d Sectors\n",
461                         info->size, info->sector_count);
462         }
463
464         printf ("  Sector Start Addresses:");
465         for (i=0; i<info->sector_count; ++i) {
466                 if ((i % 5) == 0)
467                         printf ("\n   ");
468                 printf (" %08lX%s",
469                         info->start[i],
470                         info->protect[i] ? " (RO)" : "     "
471                 );
472         }
473         printf ("\n");
474 }