blob: 97ffa68aa21297d4be5bb0819a7da0fb85759669 [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
2 * (C) Copyright 2001
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02003 * Stäubli Faverges - <www.staubli.com>
wdenk5b1d7132002-11-03 00:07:02 +00004 * Pierre AUBERT p.aubert@staubli.com
5 * U-Boot port on RPXClassic LF (CLLF_BW31) board
6 *
7 * RPXClassic uses Am29DL323B flash memory with 2 banks
8 *
9 *
10 * (C) Copyright 2000
11 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
12 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020013 * SPDX-License-Identifier: GPL-2.0+
wdenk5b1d7132002-11-03 00:07:02 +000014 */
15
16
17#include <common.h>
18#include <mpc8xx.h>
19
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020020flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenk5b1d7132002-11-03 00:07:02 +000021
22/*-----------------------------------------------------------------------
23 * Functions
24 */
25static ulong flash_get_size (vu_long *addr, flash_info_t *info);
26static int write_word (flash_info_t *info, ulong dest, ulong data);
27static void flash_get_offsets (ulong base, flash_info_t *info);
28
29/*-----------------------------------------------------------------------
30 */
31
32unsigned long flash_init (void)
33{
34 unsigned long size_b0 ;
35 int i;
36
37 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020038 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
wdenk5b1d7132002-11-03 00:07:02 +000039 flash_info[i].flash_id = FLASH_UNKNOWN;
40 }
41
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020042 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenk5b1d7132002-11-03 00:07:02 +000043
44
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020045 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenk5b1d7132002-11-03 00:07:02 +000046
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020047#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenk5b1d7132002-11-03 00:07:02 +000048 /* monitor protection ON by default */
49 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020050 CONFIG_SYS_MONITOR_BASE,
51 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
wdenk5b1d7132002-11-03 00:07:02 +000052 &flash_info[0]);
53#endif
54
55 flash_info[0].size = size_b0;
56
57 return (size_b0);
58}
59
60/*-----------------------------------------------------------------------
61 */
62static void flash_get_offsets (ulong base, flash_info_t *info)
63{
64 int i;
65
66 if (info->flash_id & FLASH_BTYPE) {
67 /* set sector offsets for bottom boot block type */
68 info->start[0] = base + 0x00000000;
69 info->start[1] = base + 0x00008000;
70 info->start[2] = base + 0x00010000;
71 info->start[3] = base + 0x00018000;
72 info->start[4] = base + 0x00020000;
73 info->start[5] = base + 0x00028000;
74 info->start[6] = base + 0x00030000;
75 info->start[7] = base + 0x00038000;
76 for (i = 8; i < info->sector_count; i++) {
77 info->start[i] = base + ((i-7) * 0x00040000) ;
78 }
79 }
80}
81
82/*-----------------------------------------------------------------------
83 */
84void flash_print_info (flash_info_t *info)
85{
86 int i;
87
88 if (info->flash_id == FLASH_UNKNOWN) {
89 printf ("missing or unknown FLASH type\n");
90 return;
91 }
92
93 switch (info->flash_id & FLASH_VENDMASK) {
94 case FLASH_MAN_AMD: printf ("AMD "); break;
95 default: printf ("Unknown Vendor "); break;
96 }
97
98 switch (info->flash_id & FLASH_TYPEMASK) {
wdenk57b2d802003-06-27 21:31:46 +000099 case FLASH_AMDL323B:
100 printf ("AMDL323DB (16 Mbytes, bottom boot sect)\n");
101 break;
wdenk5b1d7132002-11-03 00:07:02 +0000102 default:
wdenk57b2d802003-06-27 21:31:46 +0000103 printf ("Unknown Chip Type\n");
104 break;
wdenk5b1d7132002-11-03 00:07:02 +0000105 }
106
107 printf (" Size: %ld MB in %d Sectors\n",
108 info->size >> 20, info->sector_count);
109
110 printf (" Sector Start Addresses:");
111 for (i=0; i<info->sector_count; ++i) {
112 if ((i % 5) == 0)
113 printf ("\n ");
114 printf (" %08lX%s",
115 info->start[i],
116 info->protect[i] ? " (RO)" : " "
117 );
118 }
119 printf ("\n");
120}
121
122/*-----------------------------------------------------------------------
123 */
124
125
126/*-----------------------------------------------------------------------
127 */
128
129/*
130 * The following code cannot be run from FLASH!
131 */
132
133static ulong flash_get_size (vu_long *addr, flash_info_t *info)
134{
135 short i;
136 ulong value;
137 ulong base = (ulong)addr;
138
wdenk57b2d802003-06-27 21:31:46 +0000139 /* Reset flash componeny */
140 addr [0] = 0xf0f0f0f0;
wdenk5b1d7132002-11-03 00:07:02 +0000141
wdenk57b2d802003-06-27 21:31:46 +0000142 /* Write auto select command: read Manufacturer ID */
143 addr[0xAAA] = 0xAAAAAAAA ;
wdenk5b1d7132002-11-03 00:07:02 +0000144 addr[0x555] = 0x55555555 ;
145 addr[0xAAA] = 0x90909090 ;
146
147 value = addr[0] ;
148
149 switch (value & 0x00FF00FF) {
150 case AMD_MANUFACT:
151 info->flash_id = FLASH_MAN_AMD;
152 break;
153 default:
154 info->flash_id = FLASH_UNKNOWN;
155 info->sector_count = 0;
156 info->size = 0;
157 return (0); /* no or unknown flash */
158 }
159
160 value = addr[2] ; /* device ID */
161
162 switch (value & 0x00FF00FF) {
wdenk57b2d802003-06-27 21:31:46 +0000163 case (AMD_ID_DL323B & 0x00FF00FF):
164 info->flash_id += FLASH_AMDL323B;
165 info->sector_count = 71;
166 info->size = 0x01000000; /* 16 Mb */
wdenk5b1d7132002-11-03 00:07:02 +0000167
wdenk57b2d802003-06-27 21:31:46 +0000168 break;
wdenk5b1d7132002-11-03 00:07:02 +0000169 default:
170 info->flash_id = FLASH_UNKNOWN;
171 return (0); /* => no or unknown flash */
172
173 }
174 /* set up sector start address table */
wdenk57b2d802003-06-27 21:31:46 +0000175 /* set sector offsets for bottom boot block type */
176 info->start[0] = base + 0x00000000;
177 info->start[1] = base + 0x00008000;
178 info->start[2] = base + 0x00010000;
179 info->start[3] = base + 0x00018000;
180 info->start[4] = base + 0x00020000;
181 info->start[5] = base + 0x00028000;
182 info->start[6] = base + 0x00030000;
183 info->start[7] = base + 0x00038000;
184 for (i = 8; i < info->sector_count; i++) {
185 info->start[i] = base + ((i-7) * 0x00040000) ;
186 }
wdenk5b1d7132002-11-03 00:07:02 +0000187
188 /* check for protected sectors */
wdenk57b2d802003-06-27 21:31:46 +0000189 for (i = 0; i < 23; i++) {
190 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
191 /* D0 = 1 if protected */
192 addr = (volatile unsigned long *)(info->start[i]);
193 info->protect[i] = addr[4] & 1 ;
wdenk5b1d7132002-11-03 00:07:02 +0000194 }
wdenk57b2d802003-06-27 21:31:46 +0000195 /* Check for protected sectors in the 2nd bank */
196 addr[0x100AAA] = 0xAAAAAAAA ;
197 addr[0x100555] = 0x55555555 ;
198 addr[0x100AAA] = 0x90909090 ;
wdenk5b1d7132002-11-03 00:07:02 +0000199
wdenk57b2d802003-06-27 21:31:46 +0000200 for (i = 23; i < info->sector_count; i++) {
201 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
202 /* D0 = 1 if protected */
203 addr = (volatile unsigned long *)(info->start[i]);
204 info->protect[i] = addr[4] & 1 ;
wdenk5b1d7132002-11-03 00:07:02 +0000205 }
206
207 /*
208 * Prevent writes to uninitialized FLASH.
209 */
210 if (info->flash_id != FLASH_UNKNOWN) {
211 addr = (volatile unsigned long *)info->start[0];
212
213 *addr = 0xF0F0F0F0; /* reset bank 1 */
214 addr = (volatile unsigned long *)info->start[23];
215
216 *addr = 0xF0F0F0F0; /* reset bank 2 */
217
218 }
219
220 return (info->size);
221}
222
223
224/*-----------------------------------------------------------------------
225 */
226
227int flash_erase (flash_info_t *info, int s_first, int s_last)
228{
229 vu_long *addr = (vu_long*)(info->start[0]);
230 int flag, prot, sect, l_sect;
231 ulong start, now, last;
232
233 if ((s_first < 0) || (s_first > s_last)) {
234 if (info->flash_id == FLASH_UNKNOWN) {
235 printf ("- missing\n");
236 } else {
237 printf ("- no sectors to erase\n");
238 }
239 return 1;
240 }
241
242 if ((info->flash_id == FLASH_UNKNOWN) ||
243 (info->flash_id > FLASH_AMD_COMP)) {
244 printf ("Can't erase unknown flash type %08lx - aborted\n",
245 info->flash_id);
246 return 1;
247 }
248
249 prot = 0;
250 for (sect=s_first; sect<=s_last; ++sect) {
251 if (info->protect[sect]) {
252 prot++;
253 }
254 }
255
256 if (prot) {
257 printf ("- Warning: %d protected sectors will not be erased!\n",
258 prot);
259 } else {
260 printf ("\n");
261 }
262
263 l_sect = -1;
264
265 /* Disable interrupts which might cause a timeout here */
266 flag = disable_interrupts();
267
268 addr[0xAAA] = 0xAAAAAAAA;
269 addr[0x555] = 0x55555555;
270 addr[0xAAA] = 0x80808080;
271 addr[0xAAA] = 0xAAAAAAAA;
272 addr[0x555] = 0x55555555;
273
274 /* Start erase on unprotected sectors */
275 for (sect = s_first; sect<=s_last; sect++) {
276 if (info->protect[sect] == 0) { /* not protected */
277 addr = (vu_long *)(info->start[sect]) ;
278 addr[0] = 0x30303030 ;
279 l_sect = sect;
280 }
281 }
282
283 /* re-enable interrupts if necessary */
284 if (flag)
285 enable_interrupts();
286
287 /* wait at least 80us - let's wait 1 ms */
288 udelay (1000);
289
290 /*
291 * We wait for the last triggered sector
292 */
293 if (l_sect < 0)
294 goto DONE;
295
296 start = get_timer (0);
297 last = start;
298 addr = (vu_long *)(info->start[l_sect]);
299 while ((addr[0] & 0x80808080) != 0x80808080) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200300 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk5b1d7132002-11-03 00:07:02 +0000301 printf ("Timeout\n");
302 return 1;
303 }
304 /* show that we're waiting */
305 if ((now - last) > 1000) { /* every second */
306 putc ('.');
307 last = now;
308 }
309 }
310
311DONE:
312 /* reset to read mode */
313 addr = (vu_long *)info->start[0];
314 addr[0] = 0xF0F0F0F0; /* reset bank */
315
316 printf (" done\n");
wdenk57b2d802003-06-27 21:31:46 +0000317 return 0;
wdenk5b1d7132002-11-03 00:07:02 +0000318}
319
320/*-----------------------------------------------------------------------
321 * Copy memory to flash, returns:
322 * 0 - OK
323 * 1 - write timeout
324 * 2 - Flash not erased
325 */
326
327int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
328{
329 ulong cp, wp, data;
330 int i, l, rc;
331
332 wp = (addr & ~3); /* get lower word aligned address */
333
334 /*
335 * handle unaligned start bytes
336 */
337 if ((l = addr - wp) != 0) {
338 data = 0;
339 for (i=0, cp=wp; i<l; ++i, ++cp) {
340 data = (data << 8) | (*(uchar *)cp);
341 }
342 for (; i<4 && cnt>0; ++i) {
343 data = (data << 8) | *src++;
344 --cnt;
345 ++cp;
346 }
347 for (; cnt==0 && i<4; ++i, ++cp) {
348 data = (data << 8) | (*(uchar *)cp);
349 }
350
351 if ((rc = write_word(info, wp, data)) != 0) {
352 return (rc);
353 }
354 wp += 4;
355 }
356
357 /*
358 * handle word aligned part
359 */
360 while (cnt >= 4) {
361 data = 0;
362 for (i=0; i<4; ++i) {
363 data = (data << 8) | *src++;
364 }
365 if ((rc = write_word(info, wp, data)) != 0) {
366 return (rc);
367 }
368 wp += 4;
369 cnt -= 4;
370 }
371
372 if (cnt == 0) {
373 return (0);
374 }
375
376 /*
377 * handle unaligned tail bytes
378 */
379 data = 0;
380 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
381 data = (data << 8) | *src++;
382 --cnt;
383 }
384 for (; i<4; ++i, ++cp) {
385 data = (data << 8) | (*(uchar *)cp);
386 }
387
388 return (write_word(info, wp, data));
389}
390
391/*-----------------------------------------------------------------------
392 * Write a word to Flash, returns:
393 * 0 - OK
394 * 1 - write timeout
395 * 2 - Flash not erased
396 */
397static int write_word (flash_info_t *info, ulong dest, ulong data)
398{
399 vu_long *addr = (vu_long *)(info->start[0]);
400 ulong start;
401 int flag;
402
403 /* Check if Flash is (sufficiently) erased */
404 if ((*((vu_long *)dest) & data) != data) {
405 return (2);
406 }
407 /* Disable interrupts which might cause a timeout here */
408 flag = disable_interrupts();
409
410 addr[0xAAA] = 0xAAAAAAAA;
411 addr[0x555] = 0x55555555;
412 addr[0xAAA] = 0xA0A0A0A0;
413
414 *((vu_long *)dest) = data;
415
416 /* re-enable interrupts if necessary */
417 if (flag)
418 enable_interrupts();
419
420 /* data polling for D7 */
421 start = get_timer (0);
422 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200423 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk5b1d7132002-11-03 00:07:02 +0000424 return (1);
425 }
426 }
427 return (0);
428}
429
430/*-----------------------------------------------------------------------
431 */