blob: fbd4835416085b791fa0abaa911c73c0f87b8b95 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChung Liewdd8513c2008-07-23 17:11:47 -05002/*
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)
TsiChung Liewdd8513c2008-07-23 17:11:47 -05008 */
9
10#include <common.h>
Simon Glass8e201882020-05-10 11:39:54 -060011#include <flash.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070013#include <irq_func.h>
TsiChung Liewdd8513c2008-07-23 17:11:47 -050014
15#include <asm/immap.h>
16
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020017#ifndef CONFIG_SYS_FLASH_CFI
TsiChung Liewdd8513c2008-07-23 17:11:47 -050018typedef unsigned short FLASH_PORT_WIDTH;
19typedef 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
33ulong flash_get_size(FPWV * addr, flash_info_t * info);
34int flash_get_offsets(ulong base, flash_info_t * info);
35int write_word(flash_info_t * info, FPWV * dest, u16 data);
Tom Rini03183832017-05-08 22:14:35 -040036static inline void spin_wheel(void);
TsiChung Liewdd8513c2008-07-23 17:11:47 -050037
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020038flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
TsiChung Liewdd8513c2008-07-23 17:11:47 -050039
40ulong flash_init(void)
41{
42 ulong size = 0;
43 ulong fbase = 0;
44
Tom Rini6a5dccc2022-11-16 13:10:41 -050045 fbase = (ulong) CFG_SYS_FLASH_BASE;
TsiChung Liewdd8513c2008-07-23 17:11:47 -050046 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,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020053 CONFIG_SYS_MONITOR_BASE,
54 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
TsiChung Liewdd8513c2008-07-23 17:11:47 -050055
56 return size;
57}
58
59int flash_get_offsets(ulong base, flash_info_t * info)
60{
Masahiro Yamadaf26bab32014-07-22 10:57:18 +090061 int i;
TsiChung Liewdd8513c2008-07-23 17:11:47 -050062
63 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
64
65 info->start[0] = base;
Masahiro Yamadaf26bab32014-07-22 10:57:18 +090066 info->protect[0] = 0;
Tom Rini6a5dccc2022-11-16 13:10:41 -050067 for (i = 1; i < CFG_SYS_SST_SECT; i++) {
Masahiro Yamadaf26bab32014-07-22 10:57:18 +090068 info->start[i] = info->start[i - 1]
Tom Rini6a5dccc2022-11-16 13:10:41 -050069 + CFG_SYS_SST_SECTSZ;
Masahiro Yamadaf26bab32014-07-22 10:57:18 +090070 info->protect[i] = 0;
TsiChung Liewdd8513c2008-07-23 17:11:47 -050071 }
72 }
73
74 return ERR_OK;
75}
76
77void 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 */
131ulong 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;
Tom Rini6a5dccc2022-11-16 13:10:41 -0500165 info->sector_count = CFG_SYS_SST_SECT;
166 info->size = CFG_SYS_SST_SECT * CFG_SYS_SST_SECTSZ;
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500167
168 /* reset ID mode */
169 *addr = (FPWV) 0x00F000F0;
170
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200171 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500172 printf("** ERROR: sector count %d > max (%d) **\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200173 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
174 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500175 }
176
177 return (info->size);
178}
179
180int flash_erase(flash_info_t * info, int s_first, int s_last)
181{
182 FPWV *addr;
183 int flag, prot, sect, count;
Masahiro Yamadac0d51fe2014-04-15 13:26:34 +0900184 ulong type, start;
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500185 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);
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500224
Tom Rini6a5dccc2022-11-16 13:10:41 -0500225 if ((s_last - s_first) == (CFG_SYS_SST_SECT - 1)) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500226 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
Tom Rini78f88002022-07-23 13:05:00 -0400245 /* check timeout, 1000ms */
246 if (get_timer(start) > 1000) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500247 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;
Tom Rini6a5dccc2022-11-16 13:10:41 -0500262 } else if (prot == CFG_SYS_SST_SECT) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500263 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
Tom Rini6a5dccc2022-11-16 13:10:41 -0500285 base = (FPWV *) (CFG_SYS_FLASH_BASE); /* First sector */
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500286
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) {
Tom Rini78f88002022-07-23 13:05:00 -0400298 /* check timeout, 1000ms */
299 if (get_timer(start) > 1000) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500300 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
322int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
323{
324 ulong wp, count;
325 u16 data;
Masahiro Yamadac0d51fe2014-04-15 13:26:34 +0900326 int rc;
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500327
328 if (info->flash_id == FLASH_UNKNOWN)
329 return 4;
330
331 /* get lower word aligned address */
332 wp = addr;
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500333
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 */
402int 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
Tom Rini6a5dccc2022-11-16 13:10:41 -0500414 base = (FPWV *) (CFG_SYS_FLASH_BASE);
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500415
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)) {
Tom Rini78f88002022-07-23 13:05:00 -0400434 /* check timeout, 500ms */
435 if (get_timer(start) > 500) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500436 *dest = (u8) 0x00F000F0; /* reset bank */
437 res = 1;
438 }
439 }
440
441 *dest++ = (u8) 0x00F000F0; /* reset bank */
442
443 return (res);
444}
445
Tom Rini03183832017-05-08 22:14:35 -0400446static inline void spin_wheel(void)
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500447{
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