Blame view

board/cpu86/flash.c 13.1 KB
cc1c8a136   wdenk   Initial revision
1
2
3
4
5
6
7
  /*
   * (C) Copyright 2001, 2002
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
   * Flash Routines for Intel devices
   *
   *--------------------------------------------------------------------
3765b3e7b   Wolfgang Denk   Coding Style clea...
8
   * SPDX-License-Identifier:	GPL-2.0+
cc1c8a136   wdenk   Initial revision
9
10
11
12
13
   */
  
  #include <common.h>
  #include <mpc8xx.h>
  #include "cpu86.h"
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
14
  flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
cc1c8a136   wdenk   Initial revision
15
16
17
18
19
20
21
22
23
24
25
  
  /*-----------------------------------------------------------------------
   */
  ulong flash_int_get_size (volatile unsigned long *baseaddr,
  					  flash_info_t * info)
  {
  	short i;
  	unsigned long flashtest_h, flashtest_l;
  
  	info->sector_count = info->size = 0;
  	info->flash_id = FLASH_UNKNOWN;
efa329cb8   wdenk   * Add start-up de...
26
  	/* Write identify command sequence and test FLASH answer
cc1c8a136   wdenk   Initial revision
27
  	 */
efa329cb8   wdenk   * Add start-up de...
28
29
  	baseaddr[0] = 0x00900090;
  	baseaddr[1] = 0x00900090;
cc1c8a136   wdenk   Initial revision
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  
  	flashtest_h = baseaddr[0];	/* manufacturer ID	*/
  	flashtest_l = baseaddr[1];
  
  	if (flashtest_h != INTEL_MANUFACT || flashtest_l != INTEL_MANUFACT)
  		return (0);		/* no or unknown flash	*/
  
  	flashtest_h = baseaddr[2];	/* device ID	        */
  	flashtest_l = baseaddr[3];
  
  	if (flashtest_h != flashtest_l)
  		return (0);
  
  	switch (flashtest_h) {
  	case INTEL_ID_28F160C3B:
  		info->flash_id = FLASH_28F160C3B;
  		info->sector_count = 39;
  		info->size = 0x00800000;	/* 4 * 2 MB = 8 MB	*/
  		break;
  	case INTEL_ID_28F160F3B:
  		info->flash_id = FLASH_28F160F3B;
  		info->sector_count = 39;
  		info->size = 0x00800000;	/* 4 * 2 MB = 8 MB      */
  		break;
  	default:
  		return (0);			/* no or unknown flash	*/
  	}
  
  	info->flash_id |= INTEL_MANUFACT << 16; /* set manufacturer offset */
  
  	if (info->flash_id & FLASH_BTYPE) {
  		volatile unsigned long *tmp = baseaddr;
  
  		/* set up sector start adress table (bottom sector type)
  		 * AND unlock the sectors (if our chip is 160C3)
  		 */
  		for (i = 0; i < info->sector_count; i++) {
  			if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F160C3B) {
  				tmp[0] = 0x00600060;
  				tmp[1] = 0x00600060;
  				tmp[0] = 0x00D000D0;
  				tmp[1] = 0x00D000D0;
  			}
  			info->start[i] = (uint) tmp;
  			tmp += i < 8 ? 0x2000 : 0x10000; /* pointer arith       */
  		}
  	}
  
  	memset (info->protect, 0, info->sector_count);
  
  	baseaddr[0] = 0x00FF00FF;
  	baseaddr[1] = 0x00FF00FF;
  
  	return (info->size);
  }
  
  static ulong flash_amd_get_size (vu_char *addr, flash_info_t *info)
  {
  	short i;
  	uchar vendor, devid;
  	ulong base = (ulong)addr;
  
  	/* Write auto select command: read Manufacturer ID */
  	addr[0x0555] = 0xAA;
  	addr[0x02AA] = 0x55;
  	addr[0x0555] = 0x90;
  
  	udelay(1000);
  
  	vendor = addr[0];
  	devid = addr[1] & 0xff;
  
  	/* only support AMD */
  	if (vendor != 0x01) {
  		return 0;
  	}
  
  	vendor &= 0xf;
  	devid &= 0xff;
  
  	if (devid == AMD_ID_F040B) {
  		info->flash_id     = vendor << 16 | devid;
  		info->sector_count = 8;
  		info->size         = info->sector_count * 0x10000;
  	}
  	else if (devid == AMD_ID_F080B) {
  		info->flash_id     = vendor << 16 | devid;
  		info->sector_count = 16;
  		info->size         = 4 * info->sector_count * 0x10000;
  	}
  	else if (devid == AMD_ID_F016D) {
  		info->flash_id     = vendor << 16 | devid;
  		info->sector_count = 32;
  		info->size         = 4 * info->sector_count * 0x10000;
  	}
  	else {
  		printf ("## Unknown Flash Type: %02x
  ", devid);
  		return 0;
  	}
  
  	/* check for protected sectors */
  	for (i = 0; i < info->sector_count; i++) {
  		/* sector base address */
  		info->start[i] = base + i * (info->size / info->sector_count);
  		/* read sector protection at sector address, (A7 .. A0) = 0x02 */
  		/* D0 = 1 if protected */
  		addr = (volatile unsigned char *)(info->start[i]);
  		info->protect[i] = addr[2] & 1;
  	}
  
  	/*
  	 * Prevent writes to uninitialized FLASH.
  	 */
  	if (info->flash_id != FLASH_UNKNOWN) {
  		addr = (vu_char *)info->start[0];
  		addr[0] = 0xF0; /* reset bank */
  	}
  
  	return (info->size);
  }
  
  
  /*-----------------------------------------------------------------------
   */
  unsigned long flash_init (void)
  {
  	unsigned long size_b0 = 0;
  	unsigned long size_b1 = 0;
  	int i;
  
  	/* Init: no FLASHes known
  	 */
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
163
  	for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
cc1c8a136   wdenk   Initial revision
164
165
166
167
168
169
170
  		flash_info[i].flash_id = FLASH_UNKNOWN;
  	}
  
  	/* Disable flash protection */
  	CPU86_BCR |= (CPU86_BCR_FWPT | CPU86_BCR_FWRE);
  
  	/* Static FLASH Bank configuration here (only one bank) */
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
171
172
  	size_b0 = flash_int_get_size ((ulong *) CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  	size_b1 = flash_amd_get_size ((uchar *) CONFIG_SYS_BOOTROM_BASE, &flash_info[1]);
cc1c8a136   wdenk   Initial revision
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  
  	if (size_b0 > 0 || size_b1 > 0) {
  
  		printf("(");
  
  		if (size_b0 > 0) {
  			puts ("Bank#1 - ");
  			print_size (size_b0, (size_b1 > 0) ? ", " : ") ");
  		}
  
  		if (size_b1 > 0) {
  			puts ("Bank#2 - ");
  			print_size (size_b1, ") ");
  		}
  	}
  	else {
  		printf ("## No FLASH found.
  ");
  		return 0;
  	}
  	/* protect monitor and environment sectors
  	 */
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
195
  #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_BOOTROM_BASE
cc1c8a136   wdenk   Initial revision
196
  	if (size_b1) {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
197
  		/* If U-Boot is booted from ROM the CONFIG_SYS_MONITOR_BASE > CONFIG_SYS_FLASH_BASE
cc1c8a136   wdenk   Initial revision
198
199
200
201
  		 * but we shouldn't protect it.
  		 */
  
  		flash_protect  (FLAG_PROTECT_SET,
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
202
203
  				CONFIG_SYS_MONITOR_BASE,
  				CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[1]
cc1c8a136   wdenk   Initial revision
204
205
206
  		);
  	}
  #else
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
207
  #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
cc1c8a136   wdenk   Initial revision
208
  	flash_protect (FLAG_PROTECT_SET,
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
209
210
  		       CONFIG_SYS_MONITOR_BASE,
  		       CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]
cc1c8a136   wdenk   Initial revision
211
212
213
  	);
  #endif
  #endif
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
214
215
216
  #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
  # ifndef  CONFIG_ENV_SIZE
  #  define CONFIG_ENV_SIZE	CONFIG_ENV_SECT_SIZE
cc1c8a136   wdenk   Initial revision
217
  # endif
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
218
  # if CONFIG_ENV_ADDR >= CONFIG_SYS_BOOTROM_BASE
cc1c8a136   wdenk   Initial revision
219
220
  	if (size_b1) {
  		flash_protect (FLAG_PROTECT_SET,
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
221
222
  				CONFIG_ENV_ADDR,
  				CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[1]);
cc1c8a136   wdenk   Initial revision
223
224
225
  	}
  # else
  	flash_protect (FLAG_PROTECT_SET,
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
226
227
  		       CONFIG_ENV_ADDR,
  		       CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
cc1c8a136   wdenk   Initial revision
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
  # endif
  #endif
  
  	return (size_b0 + size_b1);
  }
  
  /*-----------------------------------------------------------------------
   */
  void flash_print_info (flash_info_t * info)
  {
  	int i;
  
  	if (info->flash_id == FLASH_UNKNOWN) {
  		printf ("missing or unknown FLASH type
  ");
  		return;
  	}
  
  	switch ((info->flash_id >> 16) & 0xff) {
  	case 0x89:
  		printf ("INTEL ");
  		break;
  	case 0x1:
  		printf ("AMD ");
  		break;
  	default:
  		printf ("Unknown Vendor ");
  		break;
  	}
  
  	switch (info->flash_id & FLASH_TYPEMASK) {
  	case FLASH_28F160C3B:
  		printf ("28F160C3B (16 Mbit, bottom sector)
  ");
  		break;
  	case FLASH_28F160F3B:
  		printf ("28F160F3B (16 Mbit, bottom sector)
  ");
  		break;
  	case AMD_ID_F040B:
  		printf ("AM29F040B (4 Mbit)
  ");
  		break;
  	default:
  		printf ("Unknown Chip Type
  ");
  		break;
  	}
  
  	if (info->size < 0x100000)
  		printf ("  Size: %ld KB in %d Sectors
  ",
  				info->size >> 10, info->sector_count);
  	else
  		printf ("  Size: %ld MB in %d Sectors
  ",
  				info->size >> 20, info->sector_count);
  
  	printf ("  Sector Start Addresses:");
  	for (i = 0; i < info->sector_count; ++i) {
  		if ((i % 5) == 0)
  			printf ("
     ");
  		printf (" %08lX%s",
  			info->start[i],
  			info->protect[i] ? " (RO)" : "     "
  		);
  	}
  	printf ("
  ");
  }
  
  /*-----------------------------------------------------------------------
   */
  int flash_erase (flash_info_t * info, int s_first, int s_last)
  {
  	vu_char *addr = (vu_char *)(info->start[0]);
  	int flag, prot, sect, l_sect;
  	ulong start, now, last;
  
  	if ((s_first < 0) || (s_first > s_last)) {
  		if (info->flash_id == FLASH_UNKNOWN) {
  			printf ("- missing
  ");
  		} else {
  			printf ("- no sectors to erase
  ");
  		}
  		return 1;
  	}
  
  	prot = 0;
  	for (sect = s_first; sect <= s_last; sect++) {
  		if (info->protect[sect])
  			prot++;
  	}
  
  	if (prot) {
  		printf ("- Warning: %d protected sectors will not be erased!
  ",
  				prot);
  	} else {
  		printf ("
  ");
  	}
  
  	/* Check the type of erased flash
  	 */
  	if (info->flash_id >> 16 == 0x1) {
  		/* Erase AMD flash
  		 */
  		l_sect = -1;
  
  		/* Disable interrupts which might cause a timeout here */
  		flag = disable_interrupts();
  
  		addr[0x0555] = 0xAA;
  		addr[0x02AA] = 0x55;
  		addr[0x0555] = 0x80;
  		addr[0x0555] = 0xAA;
  		addr[0x02AA] = 0x55;
  
  		/* wait at least 80us - let's wait 1 ms */
  		udelay (1000);
  
  		/* Start erase on unprotected sectors */
  		for (sect = s_first; sect<=s_last; sect++) {
  			if (info->protect[sect] == 0) { /* not protected */
  				addr = (vu_char *)(info->start[sect]);
  				addr[0] = 0x30;
  				l_sect = sect;
  			}
  		}
  
  		/* re-enable interrupts if necessary */
  		if (flag)
  			enable_interrupts();
  
  		/* wait at least 80us - let's wait 1 ms */
  		udelay (1000);
  
  		/*
  		 * We wait for the last triggered sector
  		 */
  		if (l_sect < 0)
  			goto AMD_DONE;
  
  		start = get_timer (0);
  		last  = start;
  		addr = (vu_char *)(info->start[l_sect]);
  		while ((addr[0] & 0x80) != 0x80) {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
379
  			if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
cc1c8a136   wdenk   Initial revision
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
  				printf ("Timeout
  ");
  				return 1;
  			}
  			/* show that we're waiting */
  			if ((now - last) > 1000) {      /* every second */
  				serial_putc ('.');
  				last = now;
  			}
  		}
  
  AMD_DONE:
  		/* reset to read mode */
  		addr = (volatile unsigned char *)info->start[0];
  		addr[0] = 0xF0;     /* reset bank */
  
  	} else {
  		/* Erase Intel flash
  		 */
  
  		/* Start erase on unprotected sectors
  		 */
  		for (sect = s_first; sect <= s_last; sect++) {
  			volatile ulong *addr =
  				(volatile unsigned long *) info->start[sect];
  
  			start = get_timer (0);
  			last = start;
  			if (info->protect[sect] == 0) {
  			/* Disable interrupts which might cause a timeout here
  			 */
  				flag = disable_interrupts ();
  
  				/* Erase the block
  				 */
  				addr[0] = 0x00200020;
  				addr[1] = 0x00200020;
  				addr[0] = 0x00D000D0;
  				addr[1] = 0x00D000D0;
  
  				/* re-enable interrupts if necessary
  				 */
  				if (flag)
  					enable_interrupts ();
  
  				/* wait at least 80us - let's wait 1 ms
  				 */
  				udelay (1000);
  
  				last = start;
  				while ((addr[0] & 0x00800080) != 0x00800080 ||
  				   (addr[1] & 0x00800080) != 0x00800080) {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
432
  					if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
cc1c8a136   wdenk   Initial revision
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
  						printf ("Timeout (erase suspended!)
  ");
  						/* Suspend erase
  						 */
  						addr[0] = 0x00B000B0;
  						addr[1] = 0x00B000B0;
  						goto DONE;
  					}
  					/* show that we're waiting
  					 */
  					if ((now - last) > 1000) {	/* every second */
  						serial_putc ('.');
  						last = now;
  					}
  				}
  				if (addr[0] & 0x00220022 || addr[1] & 0x00220022) {
  					printf ("*** ERROR: erase failed!
  ");
  					goto DONE;
  				}
  			}
  			/* Clear status register and reset to read mode
  			 */
  			addr[0] = 0x00500050;
  			addr[1] = 0x00500050;
  			addr[0] = 0x00FF00FF;
  			addr[1] = 0x00FF00FF;
  		}
  	}
  
  	printf (" done
  ");
  
  DONE:
  	return 0;
  }
  
  static int write_word (flash_info_t *, volatile unsigned long *, ulong);
  static int write_byte (flash_info_t *info, ulong dest, uchar data);
  
  /*-----------------------------------------------------------------------
   * Copy memory to flash, returns:
   * 0 - OK
   * 1 - write timeout
   * 2 - Flash not erased
   */
  int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  {
  	ulong v;
  	int i, l, rc, cc = cnt, res = 0;
  
  	if (info->flash_id >> 16 == 0x1) {
  
  		/* Write to AMD 8-bit flash
  		 */
  		while (cnt > 0) {
  			if ((rc = write_byte(info, addr, *src)) != 0) {
  				return (rc);
  			}
  			addr++;
  			src++;
  			cnt--;
  		}
  
  		return (0);
  	} else {
  
  		/* Write to Intel 64-bit flash
  		 */
  		for (v=0; cc > 0; addr += 4, cc -= 4 - l) {
  			l = (addr & 3);
  			addr &= ~3;
  
  			for (i = 0; i < 4; i++) {
  				v = (v << 8) + (i < l || i - l >= cc ?
  					*((unsigned char *) addr + i) : *src++);
  			}
  
  			if ((res = write_word (info, (volatile unsigned long *) addr, v)) != 0)
  				break;
  		}
  	}
  
  	return (res);
  }
  
  /*-----------------------------------------------------------------------
   * Write a word to Flash, returns:
   * 0 - OK
   * 1 - write timeout
   * 2 - Flash not erased
   */
  static int write_word (flash_info_t * info, volatile unsigned long *addr,
  					   ulong data)
  {
  	int flag, res = 0;
  	ulong start;
  
  	/* Check if Flash is (sufficiently) erased
  	 */
  	if ((*addr & data) != data)
  		return (2);
  
  	/* Disable interrupts which might cause a timeout here
  	 */
  	flag = disable_interrupts ();
  
  	*addr = 0x00400040;
  	*addr = data;
  
  	/* re-enable interrupts if necessary
  	 */
  	if (flag)
  		enable_interrupts ();
  
  	start = get_timer (0);
  	while ((*addr & 0x00800080) != 0x00800080) {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
550
  		if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
cc1c8a136   wdenk   Initial revision
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
  			/* Suspend program
  			 */
  			*addr = 0x00B000B0;
  			res = 1;
  			goto OUT;
  		}
  	}
  
  	if (*addr & 0x00220022) {
  		printf ("*** ERROR: program failed!
  ");
  		res = 1;
  	}
  
  OUT:
  	/* Clear status register and reset to read mode
  	 */
  	*addr = 0x00500050;
  	*addr = 0x00FF00FF;
  
  	return (res);
  }
  
  /*-----------------------------------------------------------------------
   * Write a byte to Flash, returns:
   * 0 - OK
   * 1 - write timeout
   * 2 - Flash not erased
   */
  static int write_byte (flash_info_t *info, ulong dest, uchar data)
  {
  	vu_char *addr = (vu_char *)(info->start[0]);
  	ulong start;
  	int flag;
  
  	/* Check if Flash is (sufficiently) erased */
  	if ((*((vu_char *)dest) & data) != data) {
  		return (2);
  	}
  	/* Disable interrupts which might cause a timeout here */
  	flag = disable_interrupts();
  
  	addr[0x0555] = 0xAA;
  	addr[0x02AA] = 0x55;
  	addr[0x0555] = 0xA0;
  
  	*((vu_char *)dest) = data;
  
  	/* re-enable interrupts if necessary */
  	if (flag)
  		enable_interrupts();
  
  	/* data polling for D7 */
  	start = get_timer (0);
  	while ((*((vu_char *)dest) & 0x80) != (data & 0x80)) {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
606
  		if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
cc1c8a136   wdenk   Initial revision
607
608
609
610
611
612
613
614
  			return (1);
  		}
  	}
  	return (0);
  }
  
  /*-----------------------------------------------------------------------
   */