summaryrefslogtreecommitdiff
path: root/board/evb4510/flash.c
blob: bcefafcd933bbab9de9ede85c23c45f02e8b5210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
379
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
432
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
/*
 *
 * Copyright (c) 2004	Cucy Systems (http://www.cucy.com)
 * Curt Brune <curt@cucy.com>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <asm/hardware.h>
#include <flash.h>

flash_info_t flash_info[CFG_MAX_FLASH_BANKS];

typedef enum {
	FLASH_DEV_U9_512KB = 0,
	FLASH_DEV_U7_2MB = 1
} FLASH_DEV;

#define FLASH_DQ7		(0x80)
#define FLASH_DQ5		(0x20)

#define PROG_ADDR		(0xAAA)
#define SETUP_ADDR		(0xAAA)
#define ID_ADDR			(0xAAA)
#define UNLOCK_ADDR1		(0xAAA)
#define UNLOCK_ADDR2		(0x555)

#define UNLOCK_CMD1		(0xAA)
#define UNLOCK_CMD2		(0x55)
#define ERASE_SUSPEND_CMD	(0xB0)
#define ERASE_RESUME_CMD	(0x30)
#define RESET_CMD		(0xF0)
#define ID_CMD			(0x90)
#define SELECT_CMD		(0x90)
#define CHIPERASE_CMD		(0x10)
#define BYPASS_CMD		(0x20)
#define SECERASE_CMD		(0x30)
#define PROG_CMD		(0xa0)
#define SETUP_CMD		(0x80)

#if 0
#define WRITE_UNLOCK(addr) { \
	PUT__U8( addr + UNLOCK_ADDR1, UNLOCK_CMD1); \
	PUT__U8( addr + UNLOCK_ADDR2, UNLOCK_CMD2); \
}

/* auto select command */
#define CMD_ID(addr) WRITE_UNLOCK(addr); { \
	PUT__U8( addr + ID_ADDR, ID_CMD); \
}

#define CMD_RESET(addr) WRITE_UNLOCK(addr); { \
	PUT__U8( addr + ID_ADDR, RESET_CMD); \
}

#define CMD_ERASE_SEC(base, addr) WRITE_UNLOCK(base); \
	PUT__U8( base + SETUP_ADDR, SETUP_CMD); \
	WRITE_UNLOCK(base); \
	PUT__U8( addr, SECERASE_CMD);

#define CMD_ERASE_CHIP(base) WRITE_UNLOCK(base); \
	PUT__U8( base + SETUP_ADDR, SETUP_CMD); \
	WRITE_UNLOCK(base); \
	PUT__U8( base + SETUP_ADDR, CHIPERASE_CMD);

/* prepare for bypass programming */
#define CMD_UNLOCK_BYPASS(addr) WRITE_UNLOCK(addr); { \
	PUT__U8( addr + ID_ADDR, 0x20); \
}

/* terminate bypass programming */
#define CMD_BYPASS_RESET(addr) { \
	PUT__U8(addr, 0x90); \
	PUT__U8(addr, 0x00); \
}
#endif

inline static void FLASH_CMD_UNLOCK (FLASH_DEV dev, u32 base)
{
	switch (dev) {
	case FLASH_DEV_U7_2MB:
		PUT__U8 (base + 0xAAA, 0xAA);
		PUT__U8 (base + 0x555, 0x55);
		break;
	case FLASH_DEV_U9_512KB:
		PUT__U8 (base + 0x555, 0xAA);
		PUT__U8 (base + 0x2AA, 0x55);
		break;
	}
}

inline static void FLASH_CMD_SELECT (FLASH_DEV dev, u32 base)
{
	switch (dev) {
	case FLASH_DEV_U7_2MB:
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base + 0xAAA, SELECT_CMD);
		break;
	case FLASH_DEV_U9_512KB:
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base + 0x555, SELECT_CMD);
		break;
	}
}

inline static void FLASH_CMD_RESET (FLASH_DEV dev, u32 base)
{
	switch (dev) {
	case FLASH_DEV_U7_2MB:
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base + 0xAAA, RESET_CMD);
		break;
	case FLASH_DEV_U9_512KB:
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base + 0x555, RESET_CMD);
		break;
	}
}

inline static void FLASH_CMD_ERASE_SEC (FLASH_DEV dev, u32 base, u32 addr)
{
	switch (dev) {
	case FLASH_DEV_U7_2MB:
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base + 0xAAA, SETUP_CMD);
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (addr, SECERASE_CMD);
		break;
	case FLASH_DEV_U9_512KB:
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base + 0x555, SETUP_CMD);
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (addr, SECERASE_CMD);
		break;
	}
}

inline static void FLASH_CMD_ERASE_CHIP (FLASH_DEV dev, u32 base)
{
	switch (dev) {
	case FLASH_DEV_U7_2MB:
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base + 0xAAA, SETUP_CMD);
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base, CHIPERASE_CMD);
		break;
	case FLASH_DEV_U9_512KB:
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base + 0x555, SETUP_CMD);
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base, CHIPERASE_CMD);
		break;
	}
}

inline static void FLASH_CMD_UNLOCK_BYPASS (FLASH_DEV dev, u32 base)
{
	switch (dev) {
	case FLASH_DEV_U7_2MB:
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base + 0xAAA, BYPASS_CMD);
		break;
	case FLASH_DEV_U9_512KB:
		FLASH_CMD_UNLOCK (dev, base);
		PUT__U8 (base + 0x555, BYPASS_CMD);
		break;
	}
}

inline static void FLASH_CMD_BYPASS_RESET (FLASH_DEV dev, u32 base)
{
	PUT__U8 (base, SELECT_CMD);
	PUT__U8 (base, 0x0);
}

/* poll for flash command completion */
static u16 _flash_poll (FLASH_DEV dev, u32 addr, u16 data, ulong timeOut)
{
	u32 done = 0;
	ulong t0;

	u16 error = 0;
	volatile u16 flashData;

	data = data & 0xFF;
	t0 = get_timer (0);
	while (get_timer (t0) < timeOut) {
		/*	for( i = 0; i < POLL_LOOPS; i++) { */
		/*  Read the Data */
		flashData = GET__U8 (addr);

		/*  FLASH_DQ7 = Data? */
		if ((flashData & FLASH_DQ7) == (data & FLASH_DQ7)) {
			done = 1;
			break;
		}

		/*  Check Timeout (FLASH_DQ5==1) */
		if (flashData & FLASH_DQ5) {
			/*  Read the Data */
			flashData = GET__U8 (addr);

			/*  FLASH_DQ7 = Data? */
			if (!((flashData & FLASH_DQ7) == (data & FLASH_DQ7))) {
				printf ("_flash_poll(): FLASH_DQ7 & flashData not equal to write value\n");
				error = ERR_PROG_ERROR;
			}
			FLASH_CMD_RESET (dev, addr);
			done = 1;
			break;
		}
		/*  spin delay */
		udelay (10);
	}


	/*  error update */
	if (!done) {
		printf ("_flash_poll(): Timeout\n");
		error = ERR_TIMOUT;
	}

	/*  Check the data */
	if (!error) {
		/*  Read the Data */
		flashData = GET__U8 (addr);
		if (flashData != data) {
			error = ERR_PROG_ERROR;
			printf ("_flash_poll(): flashData(0x%04x) not equal to data(0x%04x)\n",
				flashData, data);
		}
	}

	return error;
}

/*-----------------------------------------------------------------------
 */
static int _flash_check_protection (flash_info_t * info, int s_first, int s_last)
{
	int sect, prot = 0;

	for (sect = s_first; sect <= s_last; sect++)
		if (info->protect[sect]) {
			printf ("  Flash sector %d protected.\n", sect);
			prot++;
		}
	return prot;
}

static int _detectFlash (FLASH_DEV dev, u32 base, u8 venId, u8 devId)
{

	u32 baseAddr = base | CACHE_DISABLE_MASK;
	u8 vendorId, deviceId;

	/*	printf(__FUNCTION__"(): detecting flash @ 0x%08x\n", base); */

	/* Send auto select command and read manufacturer info */
	FLASH_CMD_SELECT (dev, baseAddr);
	vendorId = GET__U8 (baseAddr);
	FLASH_CMD_RESET (dev, baseAddr);

	/* Send auto select command and read device info */
	FLASH_CMD_SELECT (dev, baseAddr);

	if (dev == FLASH_DEV_U7_2MB) {
		deviceId = GET__U8 (baseAddr + 2);
	} else if (dev == FLASH_DEV_U9_512KB) {
		deviceId = GET__U8 (baseAddr + 1);
	} else {
		return 0;
	}

	FLASH_CMD_RESET (dev, baseAddr);

	/* printf (__FUNCTION__"(): found vendorId 0x%04x, deviceId 0x%04x\n",
		vendorId, deviceId);
	 */

	return (vendorId == venId) && (deviceId == devId);

}

/******************************************************************************
 *
 * Public u-boot interface functions below
 *
 *****************************************************************************/

/***************************************************************************
 *
 * Flash initialization
 *
 * This board has two banks of flash, but the base addresses depend on
 * how the board is jumpered.
 *
 * The two flash types are:
 *
 *   AMD Am29LV160DB (2MB) sectors layout 16KB, 2x8KB, 32KB, 31x64KB
 *
 *   AMD Am29LV040B  (512KB)  sectors: 8x64KB
 *****************************************************************************/

unsigned long flash_init (void)
{
	flash_info_t *info;
	u16 i;
	u32 flashtest;
	s16 amd160 = -1;
	u32 amd160base = 0;

#if CFG_MAX_FLASH_BANKS == 2
	s16 amd040 = -1;
	u32 amd040base = 0;
#endif

	/* configure PHYS_FLASH_1 */
	if (_detectFlash (FLASH_DEV_U7_2MB, PHYS_FLASH_1, 0x1, 0x49)) {
		amd160 = 0;
		amd160base = PHYS_FLASH_1;
#if CFG_MAX_FLASH_BANKS == 1
	}
#else
		if (_detectFlash
		    (FLASH_DEV_U9_512KB, PHYS_FLASH_2, 0x1, 0x4F)) {
			amd040 = 1;
			amd040base = PHYS_FLASH_2;
		} else {
			printf (__FUNCTION__
				"(): Unable to detect PHYS_FLASH_2: 0x%08x\n",
				PHYS_FLASH_2);
		}
	} else if (_detectFlash (FLASH_DEV_U9_512KB, PHYS_FLASH_1, 0x1, 0x4F)) {
		amd040 = 0;
		amd040base = PHYS_FLASH_1;
		if (_detectFlash (FLASH_DEV_U7_2MB, PHYS_FLASH_2, 0x1, 0x49)) {
			amd160 = 1;
			amd160base = PHYS_FLASH_2;
		} else {
			printf (__FUNCTION__
				"(): Unable to detect PHYS_FLASH_2: 0x%08x\n",
				PHYS_FLASH_2);
		}
	}
#endif
	else {
		printf ("flash_init(): Unable to detect PHYS_FLASH_1: 0x%08x\n",
			PHYS_FLASH_1);
	}

	/* Configure AMD Am29LV160DB (2MB) */
	info = &flash_info[amd160];
	info->flash_id = FLASH_DEV_U7_2MB;
	info->sector_count = 35;
	info->size = 2 * 1024 * 1024;	/* 2MB */
	/* 1*16K Boot Block
	   2*8K Parameter Block
	   1*32K Small Main Block */
	info->start[0] = amd160base;
	info->start[1] = amd160base + 0x4000;
	info->start[2] = amd160base + 0x6000;
	info->start[3] = amd160base + 0x8000;
	for (i = 1; i < info->sector_count; i++)
		info->start[3 + i] = amd160base + i * (64 * 1024);

	for (i = 0; i < info->sector_count; i++) {
		/* Write auto select command sequence and query sector protection */
		FLASH_CMD_SELECT (info->flash_id,
				  info->start[i] | CACHE_DISABLE_MASK);
		flashtest =
			GET__U8 (((info->start[i] + 4) | CACHE_DISABLE_MASK));
		FLASH_CMD_RESET (info->flash_id,
				 amd160base | CACHE_DISABLE_MASK);
		info->protect[i] = (flashtest & 0x0001);
	}

	/*
	 * protect monitor and environment sectors in 2MB flash
	 */
	flash_protect (FLAG_PROTECT_SET,
		       amd160base, amd160base + monitor_flash_len - 1, info);

	flash_protect (FLAG_PROTECT_SET,
		       CONFIG_ENV_ADDR, CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, info);

#if CFG_MAX_FLASH_BANKS == 2
	/* Configure AMD Am29LV040B (512KB) */
	info = &flash_info[amd040];
	info->flash_id = FLASH_DEV_U9_512KB;
	info->sector_count = 8;
	info->size = 512 * 1024;	/* 512KB, 8 x 64KB */
	for (i = 0; i < info->sector_count; i++) {
		info->start[i] = amd040base + i * (64 * 1024);
		/* Write auto select command sequence and query sector protection */
		FLASH_CMD_SELECT (info->flash_id,
				  info->start[i] | CACHE_DISABLE_MASK);
		flashtest =
			GET__U8 (((info->start[i] + 2) | CACHE_DISABLE_MASK));
		FLASH_CMD_RESET (info->flash_id,
				 amd040base | CACHE_DISABLE_MASK);
		info->protect[i] = (flashtest & 0x0001);
	}
#endif

	return flash_info[0].size
#if CFG_MAX_FLASH_BANKS == 2
		+ flash_info[1].size
#endif
		;
}

void flash_print_info (flash_info_t * info)
{
	int i;

	if (info->flash_id == FLASH_DEV_U7_2MB) {
		printf ("AMD Am29LV160DB (2MB) 16KB,2x8KB,32KB,31x64KB\n");
	} else if (info->flash_id == FLASH_DEV_U9_512KB) {
		printf ("AMD Am29LV040B	 (512KB) 8x64KB\n");
	} else {
		printf ("Unknown flash_id ...\n");
		return;
	}

	printf ("  Size: %ld KB in %d Sectors\n",
		info->size >> 10, info->sector_count);
	printf ("  Sector Start Addresses:");
	for (i = 0; i < info->sector_count; i++) {
		if ((i % 4) == 0)
			printf ("\n   ");
		printf (" S%02d @ 0x%08lX%s", i,
			info->start[i], info->protect[i] ? " !" : "  ");
	}
	printf ("\n");
}

int flash_erase (flash_info_t * info, int s_first, int s_last)
{
	u16 i, error = 0;

	printf ("\n");

	/* check flash protection bits */
	if (_flash_check_protection (info, s_first, s_last)) {
		printf ("  Flash erase aborted due to protected sectors\n");
		return ERR_PROTECTED;
	}

	if ((s_first < info->sector_count) && (s_first <= s_last)) {
		for (i = s_first; i <= s_last && !error; i++) {
			printf ("  Erasing Sector %d @ 0x%08lx ... ", i,
				info->start[i]);
			/* bypass the cache to access the flash memory */
			FLASH_CMD_ERASE_SEC (info->flash_id,
					     (info->
					      start[0] | CACHE_DISABLE_MASK),
					     (info->
					      start[i] | CACHE_DISABLE_MASK));
			/* look for sector to become 0xFF after erase */
			error = _flash_poll (info->flash_id,
					     info->
					     start[i] | CACHE_DISABLE_MASK,
					     0xFF, CFG_FLASH_ERASE_TOUT);
			FLASH_CMD_RESET (info->flash_id,
					 (info->
					  start[0] | CACHE_DISABLE_MASK));
			printf ("done\n");
			if (error) {
				break;
			}
		}
	} else
		error = ERR_INVAL;

	return error;
}

int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
{
	u16 error = 0, i;
	u32 n;
	u8 *bp, *bps;

	/*  Write Setup */
	/* bypass the cache to access the flash memory */
	FLASH_CMD_UNLOCK_BYPASS (info->flash_id,
				 (info->start[0] | CACHE_DISABLE_MASK));

	/*  Write the Data to Flash */

	bp = (u8 *) (addr | CACHE_DISABLE_MASK);
	bps = (u8 *) src;

	for (n = 0; n < cnt && !error; n++, bp++, bps++) {

		if (!(n % (cnt / 15))) {
			printf (".");
		}

		/*  write the flash command for flash memory */
		*bp = 0xA0;

		/*  Write the data */
		*bp = *bps;

		/*  Check if the write is done */
		for (i = 0; i < 0xff; i++);
		error = _flash_poll (info->flash_id, (u32) bp, *bps,
				     CFG_FLASH_WRITE_TOUT);
		if (error) {
			return error;
		}
	}

	/*  Reset the Flash Mode to read */
	FLASH_CMD_BYPASS_RESET (info->flash_id, info->start[0]);

	printf (" ");

	return error;
}