blob: eeb9cfd31259e632b08c31924194f398c17cc712 [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
Angelo Dureghello71a35402023-03-15 00:43:07 +010099 printf(" Size: %ld KB in %d Sectors\n",
100 info->size >> 10, info->sector_count);
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500101
102 printf(" Sector Start Addresses:");
103 for (i = 0; i < info->sector_count; ++i) {
104 if ((i % 5) == 0)
105 printf("\n ");
106 printf(" %08lX%s",
107 info->start[i], info->protect[i] ? " (RO)" : " ");
108 }
109 printf("\n");
110}
111
112/*
113 * The following code cannot be run from FLASH!
114 */
115ulong flash_get_size(FPWV * addr, flash_info_t * info)
116{
117 u16 value;
118
119 addr[FLASH_CYCLE1] = (FPWV) 0x00AA00AA; /* for Atmel, Intel ignores this */
120 addr[FLASH_CYCLE2] = (FPWV) 0x00550055; /* for Atmel, Intel ignores this */
121 addr[FLASH_CYCLE1] = (FPWV) 0x00900090; /* selects Intel or Atmel */
122
123 switch (addr[0] & 0xffff) {
124 case (u8) SST_MANUFACT:
125 info->flash_id = FLASH_MAN_SST;
126 value = addr[1];
127 break;
128 default:
129 printf("Unknown Flash\n");
130 info->flash_id = FLASH_UNKNOWN;
131 info->sector_count = 0;
132 info->size = 0;
133
134 *addr = (FPW) 0x00F000F0;
135 return (0); /* no or unknown flash */
136 }
137
138 switch (value) {
139 case (u16) SST_ID_xF6401B:
140 info->flash_id += FLASH_SST6401B;
141 break;
142 default:
143 info->flash_id = FLASH_UNKNOWN;
144 break;
145 }
146
147 info->sector_count = 0;
148 info->size = 0;
Tom Rini6a5dccc2022-11-16 13:10:41 -0500149 info->sector_count = CFG_SYS_SST_SECT;
150 info->size = CFG_SYS_SST_SECT * CFG_SYS_SST_SECTSZ;
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500151
152 /* reset ID mode */
153 *addr = (FPWV) 0x00F000F0;
154
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200155 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500156 printf("** ERROR: sector count %d > max (%d) **\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200157 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
158 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500159 }
160
161 return (info->size);
162}
163
164int flash_erase(flash_info_t * info, int s_first, int s_last)
165{
166 FPWV *addr;
167 int flag, prot, sect, count;
Masahiro Yamadac0d51fe2014-04-15 13:26:34 +0900168 ulong type, start;
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500169 int rcode = 0, flashtype = 0;
170
171 if ((s_first < 0) || (s_first > s_last)) {
172 if (info->flash_id == FLASH_UNKNOWN)
173 printf("- missing\n");
174 else
175 printf("- no sectors to erase\n");
176 return 1;
177 }
178
179 type = (info->flash_id & FLASH_VENDMASK);
180
181 switch (type) {
182 case FLASH_MAN_SST:
183 flashtype = 1;
184 break;
185 default:
186 type = (info->flash_id & FLASH_VENDMASK);
187 printf("Can't erase unknown flash type %08lx - aborted\n",
188 info->flash_id);
189 return 1;
190 }
191
192 prot = 0;
193 for (sect = s_first; sect <= s_last; ++sect) {
194 if (info->protect[sect]) {
195 prot++;
196 }
197 }
198
199 if (prot)
200 printf("- Warning: %d protected sectors will not be erased!\n",
201 prot);
202 else
203 printf("\n");
204
205 flag = disable_interrupts();
206
207 start = get_timer(0);
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500208
Tom Rini6a5dccc2022-11-16 13:10:41 -0500209 if ((s_last - s_first) == (CFG_SYS_SST_SECT - 1)) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500210 if (prot == 0) {
211 addr = (FPWV *) info->start[0];
212
213 addr[FLASH_CYCLE1] = 0x00AA; /* unlock */
214 addr[FLASH_CYCLE2] = 0x0055; /* unlock */
215 addr[FLASH_CYCLE1] = 0x0080; /* erase mode */
216 addr[FLASH_CYCLE1] = 0x00AA; /* unlock */
217 addr[FLASH_CYCLE2] = 0x0055; /* unlock */
218 *addr = 0x0030; /* erase chip */
219
220 count = 0;
221 start = get_timer(0);
222
223 while ((*addr & 0x0080) != 0x0080) {
224 if (count++ > 0x10000) {
225 spin_wheel();
226 count = 0;
227 }
228
Tom Rini78f88002022-07-23 13:05:00 -0400229 /* check timeout, 1000ms */
230 if (get_timer(start) > 1000) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500231 printf("Timeout\n");
232 *addr = 0x00F0; /* reset to read mode */
233
234 return 1;
235 }
236 }
237
238 *addr = 0x00F0; /* reset to read mode */
239
240 printf("\b. done\n");
241
242 if (flag)
243 enable_interrupts();
244
245 return 0;
Tom Rini6a5dccc2022-11-16 13:10:41 -0500246 } else if (prot == CFG_SYS_SST_SECT) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500247 return 1;
248 }
249 }
250
251 /* Start erase on unprotected sectors */
252 for (sect = s_first; sect <= s_last; sect++) {
253 if (info->protect[sect] == 0) { /* not protected */
254
255 addr = (FPWV *) (info->start[sect]);
256
257 printf(".");
258
259 /* arm simple, non interrupt dependent timer */
260 start = get_timer(0);
261
262 switch (flashtype) {
263 case 1:
264 {
265 FPWV *base; /* first address in bank */
266
267 flag = disable_interrupts();
268
Tom Rini6a5dccc2022-11-16 13:10:41 -0500269 base = (FPWV *) (CFG_SYS_FLASH_BASE); /* First sector */
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500270
271 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
272 base[FLASH_CYCLE2] = 0x0055; /* unlock */
273 base[FLASH_CYCLE1] = 0x0080; /* erase mode */
274 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
275 base[FLASH_CYCLE2] = 0x0055; /* unlock */
276 *addr = 0x0050; /* erase sector */
277
278 if (flag)
279 enable_interrupts();
280
281 while ((*addr & 0x0080) != 0x0080) {
Tom Rini78f88002022-07-23 13:05:00 -0400282 /* check timeout, 1000ms */
283 if (get_timer(start) > 1000) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500284 printf("Timeout\n");
285 *addr = 0x00F0; /* reset to read mode */
286
287 rcode = 1;
288 break;
289 }
290 }
291
292 *addr = 0x00F0; /* reset to read mode */
293 break;
294 }
295 } /* switch (flashtype) */
296 }
297 }
298 printf(" done\n");
299
300 if (flag)
301 enable_interrupts();
302
303 return rcode;
304}
305
306int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
307{
308 ulong wp, count;
309 u16 data;
Masahiro Yamadac0d51fe2014-04-15 13:26:34 +0900310 int rc;
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500311
312 if (info->flash_id == FLASH_UNKNOWN)
313 return 4;
314
315 /* get lower word aligned address */
316 wp = addr;
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500317
318 /* handle unaligned start bytes */
319 if (wp & 1) {
320 data = *((FPWV *) wp);
321 data = (data << 8) | *src;
322
323 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
324 return (rc);
325
326 wp++;
327 cnt -= 1;
328 src++;
329 }
330
331 while (cnt >= 2) {
332 /*
333 * handle word aligned part
334 */
335 count = 0;
336 data = *((FPWV *) src);
337
338 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
339 return (rc);
340
341 wp += 2;
342 src += 2;
343 cnt -= 2;
344
345 if (count++ > 0x800) {
346 spin_wheel();
347 count = 0;
348 }
349 }
350 /* handle word aligned part */
351 if (cnt) {
352 /* handle word aligned part */
353 count = 0;
354 data = *((FPWV *) wp);
355
356 data = (data & 0x00FF) | (*src << 8);
357
358 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
359 return (rc);
360
361 wp++;
362 src++;
363 cnt -= 1;
364 if (count++ > 0x800) {
365 spin_wheel();
366 count = 0;
367 }
368 }
369
370 if (cnt == 0)
371 return ERR_OK;
372
373 return ERR_OK;
374}
375
376/*-----------------------------------------------------------------------
377 * Write a word to Flash
378 * A word is 16 bits, whichever the bus width of the flash bank
379 * (not an individual chip) is.
380 *
381 * returns:
382 * 0 - OK
383 * 1 - write timeout
384 * 2 - Flash not erased
385 */
386int write_word(flash_info_t * info, FPWV * dest, u16 data)
387{
388 ulong start;
389 int flag;
390 int res = 0; /* result, assume success */
391 FPWV *base; /* first address in flash bank */
392
393 /* Check if Flash is (sufficiently) erased */
394 if ((*dest & (u8) data) != (u8) data) {
395 return (2);
396 }
397
Tom Rini6a5dccc2022-11-16 13:10:41 -0500398 base = (FPWV *) (CFG_SYS_FLASH_BASE);
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500399
400 /* Disable interrupts which might cause a timeout here */
401 flag = disable_interrupts();
402
403 base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
404 base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
405 base[FLASH_CYCLE1] = (u8) 0x00A000A0; /* selects program mode */
406
407 *dest = data; /* start programming the data */
408
409 /* re-enable interrupts if necessary */
410 if (flag)
411 enable_interrupts();
412
413 start = get_timer(0);
414
415 /* data polling for D7 */
416 while (res == 0
417 && (*dest & (u8) 0x00800080) != (data & (u8) 0x00800080)) {
Tom Rini78f88002022-07-23 13:05:00 -0400418 /* check timeout, 500ms */
419 if (get_timer(start) > 500) {
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500420 *dest = (u8) 0x00F000F0; /* reset bank */
421 res = 1;
422 }
423 }
424
425 *dest++ = (u8) 0x00F000F0; /* reset bank */
426
427 return (res);
428}
429
Tom Rini03183832017-05-08 22:14:35 -0400430static inline void spin_wheel(void)
TsiChung Liewdd8513c2008-07-23 17:11:47 -0500431{
432 static int p = 0;
433 static char w[] = "\\/-";
434
435 printf("\010%c", w[p]);
436 (++p == 3) ? (p = 0) : 0;
437}
438
439#endif