Convert CONFIG_SYS_FLASH_ERASE_TOUT et al to Kconfig
[platform/kernel/u-boot.git] / board / freescale / m5253demo / flash.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2003
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
7  * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
8  */
9
10 #include <common.h>
11 #include <flash.h>
12 #include <init.h>
13 #include <irq_func.h>
14
15 #include <asm/immap.h>
16
17 #ifndef CONFIG_SYS_FLASH_CFI
18 typedef unsigned short FLASH_PORT_WIDTH;
19 typedef volatile unsigned short FLASH_PORT_WIDTHV;
20
21 #define FPW             FLASH_PORT_WIDTH
22 #define FPWV            FLASH_PORT_WIDTHV
23
24 #define FLASH_CYCLE1    0x5555
25 #define FLASH_CYCLE2    0x2aaa
26
27 #define SYNC                    __asm__("nop")
28
29 /*-----------------------------------------------------------------------
30  * Functions
31  */
32
33 ulong flash_get_size(FPWV * addr, flash_info_t * info);
34 int flash_get_offsets(ulong base, flash_info_t * info);
35 int write_word(flash_info_t * info, FPWV * dest, u16 data);
36 static inline void spin_wheel(void);
37
38 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
39
40 ulong flash_init(void)
41 {
42         ulong size = 0;
43         ulong fbase = 0;
44
45         fbase = (ulong) CONFIG_SYS_FLASH_BASE;
46         flash_get_size((FPWV *) fbase, &flash_info[0]);
47         flash_get_offsets((ulong) fbase, &flash_info[0]);
48         fbase += flash_info[0].size;
49         size += flash_info[0].size;
50
51         /* Protect monitor and environment sectors */
52         flash_protect(FLAG_PROTECT_SET,
53                       CONFIG_SYS_MONITOR_BASE,
54                       CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
55
56         return size;
57 }
58
59 int flash_get_offsets(ulong base, flash_info_t * info)
60 {
61         int i;
62
63         if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
64
65                 info->start[0] = base;
66                 info->protect[0] = 0;
67                 for (i = 1; i < CONFIG_SYS_SST_SECT; i++) {
68                         info->start[i] = info->start[i - 1]
69                                                 + CONFIG_SYS_SST_SECTSZ;
70                         info->protect[i] = 0;
71                 }
72         }
73
74         return ERR_OK;
75 }
76
77 void flash_print_info(flash_info_t * info)
78 {
79         int i;
80
81         switch (info->flash_id & FLASH_VENDMASK) {
82         case FLASH_MAN_SST:
83                 printf("SST ");
84                 break;
85         default:
86                 printf("Unknown Vendor ");
87                 break;
88         }
89
90         switch (info->flash_id & FLASH_TYPEMASK) {
91         case FLASH_SST6401B:
92                 printf("SST39VF6401B\n");
93                 break;
94         default:
95                 printf("Unknown Chip Type\n");
96                 return;
97         }
98
99         if (info->size > 0x100000) {
100                 int remainder;
101
102                 printf("  Size: %ld", info->size >> 20);
103
104                 remainder = (info->size % 0x100000);
105                 if (remainder) {
106                         remainder >>= 10;
107                         remainder = (int)((float)
108                                           (((float)remainder / (float)1024) *
109                                            10000));
110                         printf(".%d ", remainder);
111                 }
112
113                 printf("MB in %d Sectors\n", info->sector_count);
114         } else
115                 printf("  Size: %ld KB in %d Sectors\n",
116                        info->size >> 10, info->sector_count);
117
118         printf("  Sector Start Addresses:");
119         for (i = 0; i < info->sector_count; ++i) {
120                 if ((i % 5) == 0)
121                         printf("\n   ");
122                 printf(" %08lX%s",
123                        info->start[i], info->protect[i] ? " (RO)" : "     ");
124         }
125         printf("\n");
126 }
127
128 /*
129  * The following code cannot be run from FLASH!
130  */
131 ulong flash_get_size(FPWV * addr, flash_info_t * info)
132 {
133         u16 value;
134
135         addr[FLASH_CYCLE1] = (FPWV) 0x00AA00AA; /* for Atmel, Intel ignores this */
136         addr[FLASH_CYCLE2] = (FPWV) 0x00550055; /* for Atmel, Intel ignores this */
137         addr[FLASH_CYCLE1] = (FPWV) 0x00900090; /* selects Intel or Atmel */
138
139         switch (addr[0] & 0xffff) {
140         case (u8) SST_MANUFACT:
141                 info->flash_id = FLASH_MAN_SST;
142                 value = addr[1];
143                 break;
144         default:
145                 printf("Unknown Flash\n");
146                 info->flash_id = FLASH_UNKNOWN;
147                 info->sector_count = 0;
148                 info->size = 0;
149
150                 *addr = (FPW) 0x00F000F0;
151                 return (0);     /* no or unknown flash  */
152         }
153
154         switch (value) {
155         case (u16) SST_ID_xF6401B:
156                 info->flash_id += FLASH_SST6401B;
157                 break;
158         default:
159                 info->flash_id = FLASH_UNKNOWN;
160                 break;
161         }
162
163         info->sector_count = 0;
164         info->size = 0;
165         info->sector_count = CONFIG_SYS_SST_SECT;
166         info->size = CONFIG_SYS_SST_SECT * CONFIG_SYS_SST_SECTSZ;
167
168         /* reset ID mode */
169         *addr = (FPWV) 0x00F000F0;
170
171         if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
172                 printf("** ERROR: sector count %d > max (%d) **\n",
173                        info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
174                 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
175         }
176
177         return (info->size);
178 }
179
180 int flash_erase(flash_info_t * info, int s_first, int s_last)
181 {
182         FPWV *addr;
183         int flag, prot, sect, count;
184         ulong type, start;
185         int rcode = 0, flashtype = 0;
186
187         if ((s_first < 0) || (s_first > s_last)) {
188                 if (info->flash_id == FLASH_UNKNOWN)
189                         printf("- missing\n");
190                 else
191                         printf("- no sectors to erase\n");
192                 return 1;
193         }
194
195         type = (info->flash_id & FLASH_VENDMASK);
196
197         switch (type) {
198         case FLASH_MAN_SST:
199                 flashtype = 1;
200                 break;
201         default:
202                 type = (info->flash_id & FLASH_VENDMASK);
203                 printf("Can't erase unknown flash type %08lx - aborted\n",
204                        info->flash_id);
205                 return 1;
206         }
207
208         prot = 0;
209         for (sect = s_first; sect <= s_last; ++sect) {
210                 if (info->protect[sect]) {
211                         prot++;
212                 }
213         }
214
215         if (prot)
216                 printf("- Warning: %d protected sectors will not be erased!\n",
217                        prot);
218         else
219                 printf("\n");
220
221         flag = disable_interrupts();
222
223         start = get_timer(0);
224
225         if ((s_last - s_first) == (CONFIG_SYS_SST_SECT - 1)) {
226                 if (prot == 0) {
227                         addr = (FPWV *) info->start[0];
228
229                         addr[FLASH_CYCLE1] = 0x00AA;    /* unlock */
230                         addr[FLASH_CYCLE2] = 0x0055;    /* unlock */
231                         addr[FLASH_CYCLE1] = 0x0080;    /* erase mode */
232                         addr[FLASH_CYCLE1] = 0x00AA;    /* unlock */
233                         addr[FLASH_CYCLE2] = 0x0055;    /* unlock */
234                         *addr = 0x0030; /* erase chip */
235
236                         count = 0;
237                         start = get_timer(0);
238
239                         while ((*addr & 0x0080) != 0x0080) {
240                                 if (count++ > 0x10000) {
241                                         spin_wheel();
242                                         count = 0;
243                                 }
244
245                                 /* check timeout, 1000ms */
246                                 if (get_timer(start) > 1000) {
247                                         printf("Timeout\n");
248                                         *addr = 0x00F0; /* reset to read mode */
249
250                                         return 1;
251                                 }
252                         }
253
254                         *addr = 0x00F0; /* reset to read mode */
255
256                         printf("\b. done\n");
257
258                         if (flag)
259                                 enable_interrupts();
260
261                         return 0;
262                 } else if (prot == CONFIG_SYS_SST_SECT) {
263                         return 1;
264                 }
265         }
266
267         /* Start erase on unprotected sectors */
268         for (sect = s_first; sect <= s_last; sect++) {
269                 if (info->protect[sect] == 0) { /* not protected */
270
271                         addr = (FPWV *) (info->start[sect]);
272
273                         printf(".");
274
275                         /* arm simple, non interrupt dependent timer */
276                         start = get_timer(0);
277
278                         switch (flashtype) {
279                         case 1:
280                                 {
281                                         FPWV *base;     /* first address in bank */
282
283                                         flag = disable_interrupts();
284
285                                         base = (FPWV *) (CONFIG_SYS_FLASH_BASE);        /* First sector */
286
287                                         base[FLASH_CYCLE1] = 0x00AA;    /* unlock */
288                                         base[FLASH_CYCLE2] = 0x0055;    /* unlock */
289                                         base[FLASH_CYCLE1] = 0x0080;    /* erase mode */
290                                         base[FLASH_CYCLE1] = 0x00AA;    /* unlock */
291                                         base[FLASH_CYCLE2] = 0x0055;    /* unlock */
292                                         *addr = 0x0050; /* erase sector */
293
294                                         if (flag)
295                                                 enable_interrupts();
296
297                                         while ((*addr & 0x0080) != 0x0080) {
298                                                 /* check timeout, 1000ms */
299                                                 if (get_timer(start) > 1000) {
300                                                         printf("Timeout\n");
301                                                         *addr = 0x00F0; /* reset to read mode */
302
303                                                         rcode = 1;
304                                                         break;
305                                                 }
306                                         }
307
308                                         *addr = 0x00F0; /* reset to read mode */
309                                         break;
310                                 }
311                         }       /* switch (flashtype) */
312                 }
313         }
314         printf(" done\n");
315
316         if (flag)
317                 enable_interrupts();
318
319         return rcode;
320 }
321
322 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
323 {
324         ulong wp, count;
325         u16 data;
326         int rc;
327
328         if (info->flash_id == FLASH_UNKNOWN)
329                 return 4;
330
331         /* get lower word aligned address */
332         wp = addr;
333
334         /* handle unaligned start bytes */
335         if (wp & 1) {
336                 data = *((FPWV *) wp);
337                 data = (data << 8) | *src;
338
339                 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
340                         return (rc);
341
342                 wp++;
343                 cnt -= 1;
344                 src++;
345         }
346
347         while (cnt >= 2) {
348                 /*
349                  * handle word aligned part
350                  */
351                 count = 0;
352                 data = *((FPWV *) src);
353
354                 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
355                         return (rc);
356
357                 wp += 2;
358                 src += 2;
359                 cnt -= 2;
360
361                 if (count++ > 0x800) {
362                         spin_wheel();
363                         count = 0;
364                 }
365         }
366         /* handle word aligned part */
367         if (cnt) {
368                 /* handle word aligned part */
369                 count = 0;
370                 data = *((FPWV *) wp);
371
372                 data = (data & 0x00FF) | (*src << 8);
373
374                 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
375                         return (rc);
376
377                 wp++;
378                 src++;
379                 cnt -= 1;
380                 if (count++ > 0x800) {
381                         spin_wheel();
382                         count = 0;
383                 }
384         }
385
386         if (cnt == 0)
387                 return ERR_OK;
388
389         return ERR_OK;
390 }
391
392 /*-----------------------------------------------------------------------
393  * Write a word to Flash
394  * A word is 16 bits, whichever the bus width of the flash bank
395  * (not an individual chip) is.
396  *
397  * returns:
398  * 0 - OK
399  * 1 - write timeout
400  * 2 - Flash not erased
401  */
402 int write_word(flash_info_t * info, FPWV * dest, u16 data)
403 {
404         ulong start;
405         int flag;
406         int res = 0;            /* result, assume success */
407         FPWV *base;             /* first address in flash bank */
408
409         /* Check if Flash is (sufficiently) erased */
410         if ((*dest & (u8) data) != (u8) data) {
411                 return (2);
412         }
413
414         base = (FPWV *) (CONFIG_SYS_FLASH_BASE);
415
416         /* Disable interrupts which might cause a timeout here */
417         flag = disable_interrupts();
418
419         base[FLASH_CYCLE1] = (u8) 0x00AA00AA;   /* unlock */
420         base[FLASH_CYCLE2] = (u8) 0x00550055;   /* unlock */
421         base[FLASH_CYCLE1] = (u8) 0x00A000A0;   /* selects program mode */
422
423         *dest = data;           /* start programming the data */
424
425         /* re-enable interrupts if necessary */
426         if (flag)
427                 enable_interrupts();
428
429         start = get_timer(0);
430
431         /* data polling for D7 */
432         while (res == 0
433                && (*dest & (u8) 0x00800080) != (data & (u8) 0x00800080)) {
434                 /* check timeout, 500ms */
435                 if (get_timer(start) > 500) {
436                         *dest = (u8) 0x00F000F0;        /* reset bank */
437                         res = 1;
438                 }
439         }
440
441         *dest++ = (u8) 0x00F000F0;      /* reset bank */
442
443         return (res);
444 }
445
446 static inline void spin_wheel(void)
447 {
448         static int p = 0;
449         static char w[] = "\\/-";
450
451         printf("\010%c", w[p]);
452         (++p == 3) ? (p = 0) : 0;
453 }
454
455 #endif