blob: fbe17dd966cb3795ee9218b8b50abdd7c9c8a3ac [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4 *
5 * (C) Copyright 2001
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00009 */
10
11#include <common.h>
12#include <mpc8xx.h>
13
Mike Frysinger0107cf62009-02-11 19:36:20 -050014DECLARE_GLOBAL_DATA_PTR;
15
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020016flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkaffae2b2002-08-17 09:36:01 +000017
18/*-----------------------------------------------------------------------
19 * Protection Flags:
20 */
21#define FLAG_PROTECT_SET 0x01
22#define FLAG_PROTECT_CLEAR 0x02
23
24/* Board support for 1 or 2 flash devices */
25#undef FLASH_PORT_WIDTH32
26#define FLASH_PORT_WIDTH16
27
28#ifdef FLASH_PORT_WIDTH16
29#define FLASH_PORT_WIDTH ushort
30#define FLASH_PORT_WIDTHV vu_short
31#else
32#define FLASH_PORT_WIDTH ulong
33#define FLASH_PORT_WIDTHV vu_long
34#endif
35
36#define FPW FLASH_PORT_WIDTH
37#define FPWV FLASH_PORT_WIDTHV
38
39/*-----------------------------------------------------------------------
40 * Functions
41 */
42static ulong flash_get_size (FPW *addr, flash_info_t *info);
43static int write_data (flash_info_t *info, ulong dest, FPW data);
44static void flash_get_offsets (ulong base, flash_info_t *info);
45
46/*-----------------------------------------------------------------------
47 */
48
49unsigned long flash_init (void)
50{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
wdenkaffae2b2002-08-17 09:36:01 +000052 volatile memctl8xx_t *memctl = &immap->im_memctl;
53 unsigned long size_b0;
54 int i;
55
56 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020057 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +000058 flash_info[i].flash_id = FLASH_UNKNOWN;
59 }
60
61 /* Static FLASH Bank configuration here - FIXME XXX */
62 size_b0 = flash_get_size((FPW *)FLASH_BASE0_PRELIM, &flash_info[0]);
63
64 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
65 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
66 size_b0, size_b0<<20);
67 }
68
69 /* Remap FLASH according to real size */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020070 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
71 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V;
wdenkaffae2b2002-08-17 09:36:01 +000072
73 /* Re-do sizing to get full correct info */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020074 size_b0 = flash_get_size((FPW *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000075
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020076 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000077
78 /* monitor protection ON by default */
79 (void)flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020080 CONFIG_SYS_FLASH_BASE,
81 CONFIG_SYS_FLASH_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +000082 &flash_info[0]);
83
84 flash_info[0].size = size_b0;
85
86 return (size_b0);
87}
88
89/*-----------------------------------------------------------------------
90 */
91static void flash_get_offsets (ulong base, flash_info_t *info)
92{
93 int i;
94
95 if (info->flash_id == FLASH_UNKNOWN) {
96 return;
97 }
98
99 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
100 for (i = 0; i < info->sector_count; i++) {
101 info->start[i] = base + (i * 0x00020000);
102 }
103 }
104}
105
106/*-----------------------------------------------------------------------
107 */
108void flash_print_info (flash_info_t *info)
109{
110 int i;
111
112 if (info->flash_id == FLASH_UNKNOWN) {
113 printf ("missing or unknown FLASH type\n");
114 return;
115 }
116
117 switch (info->flash_id & FLASH_VENDMASK) {
118 case FLASH_MAN_INTEL: printf ("INTEL "); break;
119 default: printf ("Unknown Vendor "); break;
120 }
121
122 switch (info->flash_id & FLASH_TYPEMASK) {
123 case FLASH_28F320J3A:
124 printf ("28F320J3A\n"); break;
125 case FLASH_28F640J3A:
126 printf ("28F640J3A\n"); break;
127 case FLASH_28F128J3A:
128 printf ("28F128J3A\n"); break;
129 default: printf ("Unknown Chip Type\n"); break;
130 }
131
132 printf (" Size: %ld MB in %d Sectors\n",
133 info->size >> 20, info->sector_count);
134
135 printf (" Sector Start Addresses:");
136 for (i=0; i<info->sector_count; ++i) {
137 if ((i % 5) == 0)
138 printf ("\n ");
139 printf (" %08lX%s",
140 info->start[i],
141 info->protect[i] ? " (RO)" : " "
142 );
143 }
144 printf ("\n");
145 return;
146}
147
148/*-----------------------------------------------------------------------
149 */
150
151
152/*-----------------------------------------------------------------------
153 */
154
155/*
156 * The following code cannot be run from FLASH!
157 */
158
159static ulong flash_get_size (FPW *addr, flash_info_t *info)
160{
161 FPW value;
162
163 /* Write auto select command: read Manufacturer ID */
164 addr[0x5555] = (FPW)0x00AA00AA;
165 addr[0x2AAA] = (FPW)0x00550055;
166 addr[0x5555] = (FPW)0x00900090;
167
168 value = addr[0];
169
170 switch (value) {
171 case (FPW)INTEL_MANUFACT:
172 info->flash_id = FLASH_MAN_INTEL;
173 break;
174 default:
175 info->flash_id = FLASH_UNKNOWN;
176 info->sector_count = 0;
177 info->size = 0;
178 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
179 return (0); /* no or unknown flash */
180 }
181
182 value = addr[1]; /* device ID */
183
184 switch (value) {
185 case (FPW)INTEL_ID_28F320J3A:
186 info->flash_id += FLASH_28F320J3A;
187 info->sector_count = 32;
188 info->size = 0x00400000;
189 break; /* => 4 MB */
190
191 case (FPW)INTEL_ID_28F640J3A:
192 info->flash_id += FLASH_28F640J3A;
193 info->sector_count = 64;
194 info->size = 0x00800000;
195 break; /* => 8 MB */
196
197 case (FPW)INTEL_ID_28F128J3A:
198 info->flash_id += FLASH_28F128J3A;
199 info->sector_count = 128;
200 info->size = 0x01000000;
201 break; /* => 16 MB */
202
203 default:
204 info->flash_id = FLASH_UNKNOWN;
205 break;
206 }
207
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200208 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
wdenkaffae2b2002-08-17 09:36:01 +0000209 printf ("** ERROR: sector count %d > max (%d) **\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200210 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
211 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenkaffae2b2002-08-17 09:36:01 +0000212 }
213
214 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
215
216 return (info->size);
217}
218
219
220/*-----------------------------------------------------------------------
221 */
222
223int flash_erase (flash_info_t *info, int s_first, int s_last)
224{
225 int flag, prot, sect;
226 ulong type, start, now, last;
227 int rcode = 0;
228
229 if ((s_first < 0) || (s_first > s_last)) {
230 if (info->flash_id == FLASH_UNKNOWN) {
231 printf ("- missing\n");
232 } else {
233 printf ("- no sectors to erase\n");
234 }
235 return 1;
236 }
237
238 type = (info->flash_id & FLASH_VENDMASK);
239 if ((type != FLASH_MAN_INTEL)) {
240 printf ("Can't erase unknown flash type %08lx - aborted\n",
241 info->flash_id);
242 return 1;
243 }
244
245 prot = 0;
246 for (sect=s_first; sect<=s_last; ++sect) {
247 if (info->protect[sect]) {
248 prot++;
249 }
250 }
251
252 if (prot) {
253 printf ("- Warning: %d protected sectors will not be erased!\n",
254 prot);
255 } else {
256 printf ("\n");
257 }
258
259 start = get_timer (0);
260 last = start;
261 /* Start erase on unprotected sectors */
262 for (sect = s_first; sect<=s_last; sect++) {
263 if (info->protect[sect] == 0) { /* not protected */
264 FPWV *addr = (FPWV *)(info->start[sect]);
265 FPW status;
266
267 /* Disable interrupts which might cause a timeout here */
268 flag = disable_interrupts();
269
270 *addr = (FPW)0x00500050; /* clear status register */
271 *addr = (FPW)0x00200020; /* erase setup */
272 *addr = (FPW)0x00D000D0; /* erase confirm */
273
274 /* re-enable interrupts if necessary */
275 if (flag)
276 enable_interrupts();
277
278 /* wait at least 80us - let's wait 1 ms */
279 udelay (1000);
280
281 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200282 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000283 printf ("Timeout\n");
284 *addr = (FPW)0x00B000B0; /* suspend erase */
285 *addr = (FPW)0x00FF00FF; /* reset to read mode */
286 rcode = 1;
287 break;
288 }
289
290 /* show that we're waiting */
291 if ((now - last) > 1000) { /* every second */
292 putc ('.');
293 last = now;
294 }
295 }
296
297 *addr = (FPW)0x00FF00FF; /* reset to read mode */
298 printf (" done\n");
299 }
300 }
301 return rcode;
302}
303
304/*-----------------------------------------------------------------------
305 * Copy memory to flash, returns:
306 * 0 - OK
307 * 1 - write timeout
308 * 2 - Flash not erased
309 * 4 - Flash not identified
310 */
311
312int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
313{
314 ulong cp, wp;
315 FPW data;
316 int count, i, l, rc, port_width;
317
318 if (info->flash_id == FLASH_UNKNOWN) {
319 return 4;
320 }
321/* get lower word aligned address */
322#ifdef FLASH_PORT_WIDTH16
323 wp = (addr & ~1);
324 port_width = 2;
325#else
326 wp = (addr & ~3);
327 port_width = 4;
328#endif
329
330 /* save sernum if needed */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200331 if (addr >= CONFIG_SYS_FLASH_SN_SECTOR && addr < CONFIG_SYS_FLASH_SN_BASE)
wdenkaffae2b2002-08-17 09:36:01 +0000332 {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200333 u_long dest = CONFIG_SYS_FLASH_SN_BASE;
Mike Frysinger0107cf62009-02-11 19:36:20 -0500334 u_short *sn = (u_short *)gd->bd->bi_sernum;
wdenkaffae2b2002-08-17 09:36:01 +0000335
336 printf("(saving sernum)");
337 for (i=0; i<4; i++)
338 {
339 if ((rc = write_data(info, dest, sn[i])) != 0) {
340 return (rc);
341 }
342 dest += port_width;
343 }
344 }
345
346 /*
347 * handle unaligned start bytes
348 */
349 if ((l = addr - wp) != 0) {
350 data = 0;
351 for (i=0, cp=wp; i<l; ++i, ++cp) {
352 data = (data << 8) | (*(uchar *)cp);
353 }
354 for (; i<port_width && cnt>0; ++i) {
355 data = (data << 8) | *src++;
356 --cnt;
357 ++cp;
358 }
359 for (; cnt==0 && i<port_width; ++i, ++cp) {
360 data = (data << 8) | (*(uchar *)cp);
361 }
362
363 if ((rc = write_data(info, wp, data)) != 0) {
364 return (rc);
365 }
366 wp += port_width;
367 }
368
369 /*
370 * handle word aligned part
371 */
372 count = 0;
373 while (cnt >= port_width) {
374 data = 0;
375 for (i=0; i<port_width; ++i) {
376 data = (data << 8) | *src++;
377 }
378 if ((rc = write_data(info, wp, data)) != 0) {
379 return (rc);
380 }
381 wp += port_width;
382 cnt -= port_width;
383 if (count++ > 0x800)
384 {
385 putc('.');
386 count = 0;
387 }
388 }
389
390 if (cnt == 0) {
391 return (0);
392 }
393
394 /*
395 * handle unaligned tail bytes
396 */
397 data = 0;
398 for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) {
399 data = (data << 8) | *src++;
400 --cnt;
401 }
402 for (; i<port_width; ++i, ++cp) {
403 data = (data << 8) | (*(uchar *)cp);
404 }
405
406 return (write_data(info, wp, data));
407}
408
409/*-----------------------------------------------------------------------
410 * Write a word or halfword to Flash, returns:
411 * 0 - OK
412 * 1 - write timeout
413 * 2 - Flash not erased
414 */
415static int write_data (flash_info_t *info, ulong dest, FPW data)
416{
417 FPWV *addr = (FPWV *)dest;
418 ulong status;
419 ulong start;
420 int flag;
421
422 /* Check if Flash is (sufficiently) erased */
423 if ((*addr & data) != data) {
424 printf("not erased at %08lx (%x)\n",(ulong)addr,*addr);
425 return (2);
426 }
427 /* Disable interrupts which might cause a timeout here */
428 flag = disable_interrupts();
429
430 *addr = (FPW)0x00400040; /* write setup */
431 *addr = data;
432
433 /* re-enable interrupts if necessary */
434 if (flag)
435 enable_interrupts();
436
437 start = get_timer (0);
438
439 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200440 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000441 *addr = (FPW)0x00FF00FF; /* restore read mode */
442 return (1);
443 }
444 }
445
446 *addr = (FPW)0x00FF00FF; /* restore read mode */
447
448 return (0);
449}