3 * Copyright (c) 2004 Cucy Systems (http://www.cucy.com)
4 * Curt Brune <curt@cucy.com>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <asm/hardware.h>
29 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
32 FLASH_DEV_U9_512KB = 0,
36 #define FLASH_DQ7 (0x80)
37 #define FLASH_DQ5 (0x20)
39 #define PROG_ADDR (0xAAA)
40 #define SETUP_ADDR (0xAAA)
41 #define ID_ADDR (0xAAA)
42 #define UNLOCK_ADDR1 (0xAAA)
43 #define UNLOCK_ADDR2 (0x555)
45 #define UNLOCK_CMD1 (0xAA)
46 #define UNLOCK_CMD2 (0x55)
47 #define ERASE_SUSPEND_CMD (0xB0)
48 #define ERASE_RESUME_CMD (0x30)
49 #define RESET_CMD (0xF0)
51 #define SELECT_CMD (0x90)
52 #define CHIPERASE_CMD (0x10)
53 #define BYPASS_CMD (0x20)
54 #define SECERASE_CMD (0x30)
55 #define PROG_CMD (0xa0)
56 #define SETUP_CMD (0x80)
59 #define WRITE_UNLOCK(addr) { \
60 PUT__U8( addr + UNLOCK_ADDR1, UNLOCK_CMD1); \
61 PUT__U8( addr + UNLOCK_ADDR2, UNLOCK_CMD2); \
64 /* auto select command */
65 #define CMD_ID(addr) WRITE_UNLOCK(addr); { \
66 PUT__U8( addr + ID_ADDR, ID_CMD); \
69 #define CMD_RESET(addr) WRITE_UNLOCK(addr); { \
70 PUT__U8( addr + ID_ADDR, RESET_CMD); \
73 #define CMD_ERASE_SEC(base, addr) WRITE_UNLOCK(base); \
74 PUT__U8( base + SETUP_ADDR, SETUP_CMD); \
76 PUT__U8( addr, SECERASE_CMD);
78 #define CMD_ERASE_CHIP(base) WRITE_UNLOCK(base); \
79 PUT__U8( base + SETUP_ADDR, SETUP_CMD); \
81 PUT__U8( base + SETUP_ADDR, CHIPERASE_CMD);
83 /* prepare for bypass programming */
84 #define CMD_UNLOCK_BYPASS(addr) WRITE_UNLOCK(addr); { \
85 PUT__U8( addr + ID_ADDR, 0x20); \
88 /* terminate bypass programming */
89 #define CMD_BYPASS_RESET(addr) { \
90 PUT__U8(addr, 0x90); \
91 PUT__U8(addr, 0x00); \
95 inline static void FLASH_CMD_UNLOCK (FLASH_DEV dev, u32 base)
98 case FLASH_DEV_U7_2MB:
99 PUT__U8 (base + 0xAAA, 0xAA);
100 PUT__U8 (base + 0x555, 0x55);
102 case FLASH_DEV_U9_512KB:
103 PUT__U8 (base + 0x555, 0xAA);
104 PUT__U8 (base + 0x2AA, 0x55);
109 inline static void FLASH_CMD_SELECT (FLASH_DEV dev, u32 base)
112 case FLASH_DEV_U7_2MB:
113 FLASH_CMD_UNLOCK (dev, base);
114 PUT__U8 (base + 0xAAA, SELECT_CMD);
116 case FLASH_DEV_U9_512KB:
117 FLASH_CMD_UNLOCK (dev, base);
118 PUT__U8 (base + 0x555, SELECT_CMD);
123 inline static void FLASH_CMD_RESET (FLASH_DEV dev, u32 base)
126 case FLASH_DEV_U7_2MB:
127 FLASH_CMD_UNLOCK (dev, base);
128 PUT__U8 (base + 0xAAA, RESET_CMD);
130 case FLASH_DEV_U9_512KB:
131 FLASH_CMD_UNLOCK (dev, base);
132 PUT__U8 (base + 0x555, RESET_CMD);
137 inline static void FLASH_CMD_ERASE_SEC (FLASH_DEV dev, u32 base, u32 addr)
140 case FLASH_DEV_U7_2MB:
141 FLASH_CMD_UNLOCK (dev, base);
142 PUT__U8 (base + 0xAAA, SETUP_CMD);
143 FLASH_CMD_UNLOCK (dev, base);
144 PUT__U8 (addr, SECERASE_CMD);
146 case FLASH_DEV_U9_512KB:
147 FLASH_CMD_UNLOCK (dev, base);
148 PUT__U8 (base + 0x555, SETUP_CMD);
149 FLASH_CMD_UNLOCK (dev, base);
150 PUT__U8 (addr, SECERASE_CMD);
155 inline static void FLASH_CMD_ERASE_CHIP (FLASH_DEV dev, u32 base)
158 case FLASH_DEV_U7_2MB:
159 FLASH_CMD_UNLOCK (dev, base);
160 PUT__U8 (base + 0xAAA, SETUP_CMD);
161 FLASH_CMD_UNLOCK (dev, base);
162 PUT__U8 (base, CHIPERASE_CMD);
164 case FLASH_DEV_U9_512KB:
165 FLASH_CMD_UNLOCK (dev, base);
166 PUT__U8 (base + 0x555, SETUP_CMD);
167 FLASH_CMD_UNLOCK (dev, base);
168 PUT__U8 (base, CHIPERASE_CMD);
173 inline static void FLASH_CMD_UNLOCK_BYPASS (FLASH_DEV dev, u32 base)
176 case FLASH_DEV_U7_2MB:
177 FLASH_CMD_UNLOCK (dev, base);
178 PUT__U8 (base + 0xAAA, BYPASS_CMD);
180 case FLASH_DEV_U9_512KB:
181 FLASH_CMD_UNLOCK (dev, base);
182 PUT__U8 (base + 0x555, BYPASS_CMD);
187 inline static void FLASH_CMD_BYPASS_RESET (FLASH_DEV dev, u32 base)
189 PUT__U8 (base, SELECT_CMD);
193 /* poll for flash command completion */
194 static u16 _flash_poll (FLASH_DEV dev, u32 addr, u16 data, ulong timeOut)
200 volatile u16 flashData;
204 while (get_timer (t0) < timeOut) {
205 /* for( i = 0; i < POLL_LOOPS; i++) { */
207 flashData = GET__U8 (addr);
209 /* FLASH_DQ7 = Data? */
210 if ((flashData & FLASH_DQ7) == (data & FLASH_DQ7)) {
215 /* Check Timeout (FLASH_DQ5==1) */
216 if (flashData & FLASH_DQ5) {
218 flashData = GET__U8 (addr);
220 /* FLASH_DQ7 = Data? */
221 if (!((flashData & FLASH_DQ7) == (data & FLASH_DQ7))) {
222 printf ("_flash_poll(): FLASH_DQ7 & flashData not equal to write value\n");
223 error = ERR_PROG_ERROR;
225 FLASH_CMD_RESET (dev, addr);
236 printf ("_flash_poll(): Timeout\n");
243 flashData = GET__U8 (addr);
244 if (flashData != data) {
245 error = ERR_PROG_ERROR;
246 printf ("_flash_poll(): flashData(0x%04x) not equal to data(0x%04x)\n",
254 /*-----------------------------------------------------------------------
256 static int _flash_check_protection (flash_info_t * info, int s_first, int s_last)
260 for (sect = s_first; sect <= s_last; sect++)
261 if (info->protect[sect]) {
262 printf (" Flash sector %d protected.\n", sect);
268 static int _detectFlash (FLASH_DEV dev, u32 base, u8 venId, u8 devId)
271 u32 baseAddr = base | CACHE_DISABLE_MASK;
272 u8 vendorId, deviceId;
274 /* printf(__FUNCTION__"(): detecting flash @ 0x%08x\n", base); */
276 /* Send auto select command and read manufacturer info */
277 FLASH_CMD_SELECT (dev, baseAddr);
278 vendorId = GET__U8 (baseAddr);
279 FLASH_CMD_RESET (dev, baseAddr);
281 /* Send auto select command and read device info */
282 FLASH_CMD_SELECT (dev, baseAddr);
284 if (dev == FLASH_DEV_U7_2MB) {
285 deviceId = GET__U8 (baseAddr + 2);
286 } else if (dev == FLASH_DEV_U9_512KB) {
287 deviceId = GET__U8 (baseAddr + 1);
292 FLASH_CMD_RESET (dev, baseAddr);
294 /* printf (__FUNCTION__"(): found vendorId 0x%04x, deviceId 0x%04x\n",
298 return (vendorId == venId) && (deviceId == devId);
302 /******************************************************************************
304 * Public u-boot interface functions below
306 *****************************************************************************/
308 /***************************************************************************
310 * Flash initialization
312 * This board has two banks of flash, but the base addresses depend on
313 * how the board is jumpered.
315 * The two flash types are:
317 * AMD Am29LV160DB (2MB) sectors layout 16KB, 2x8KB, 32KB, 31x64KB
319 * AMD Am29LV040B (512KB) sectors: 8x64KB
320 *****************************************************************************/
322 unsigned long flash_init (void)
330 #if CFG_MAX_FLASH_BANKS == 2
335 /* configure PHYS_FLASH_1 */
336 if (_detectFlash (FLASH_DEV_U7_2MB, PHYS_FLASH_1, 0x1, 0x49)) {
338 amd160base = PHYS_FLASH_1;
339 #if CFG_MAX_FLASH_BANKS == 1
343 (FLASH_DEV_U9_512KB, PHYS_FLASH_2, 0x1, 0x4F)) {
345 amd040base = PHYS_FLASH_2;
348 "(): Unable to detect PHYS_FLASH_2: 0x%08x\n",
351 } else if (_detectFlash (FLASH_DEV_U9_512KB, PHYS_FLASH_1, 0x1, 0x4F)) {
353 amd040base = PHYS_FLASH_1;
354 if (_detectFlash (FLASH_DEV_U7_2MB, PHYS_FLASH_2, 0x1, 0x49)) {
356 amd160base = PHYS_FLASH_2;
359 "(): Unable to detect PHYS_FLASH_2: 0x%08x\n",
366 "(): Unable to detect PHYS_FLASH_1: 0x%08x\n",
370 /* Configure AMD Am29LV160DB (2MB) */
371 info = &flash_info[amd160];
372 info->flash_id = FLASH_DEV_U7_2MB;
373 info->sector_count = 35;
374 info->size = 2 * 1024 * 1024; /* 2MB */
377 1*32K Small Main Block */
378 info->start[0] = amd160base;
379 info->start[1] = amd160base + 0x4000;
380 info->start[2] = amd160base + 0x6000;
381 info->start[3] = amd160base + 0x8000;
382 for (i = 1; i < info->sector_count; i++)
383 info->start[3 + i] = amd160base + i * (64 * 1024);
385 for (i = 0; i < info->sector_count; i++) {
386 /* Write auto select command sequence and query sector protection */
387 FLASH_CMD_SELECT (info->flash_id,
388 info->start[i] | CACHE_DISABLE_MASK);
390 GET__U8 (((info->start[i] + 4) | CACHE_DISABLE_MASK));
391 FLASH_CMD_RESET (info->flash_id,
392 amd160base | CACHE_DISABLE_MASK);
393 info->protect[i] = (flashtest & 0x0001);
397 * protect monitor and environment sectors in 2MB flash
399 flash_protect (FLAG_PROTECT_SET,
400 amd160base, amd160base + monitor_flash_len - 1, info);
402 flash_protect (FLAG_PROTECT_SET,
403 CFG_ENV_ADDR, CFG_ENV_ADDR + CFG_ENV_SIZE - 1, info);
405 #if CFG_MAX_FLASH_BANKS == 2
406 /* Configure AMD Am29LV040B (512KB) */
407 info = &flash_info[amd040];
408 info->flash_id = FLASH_DEV_U9_512KB;
409 info->sector_count = 8;
410 info->size = 512 * 1024; /* 512KB, 8 x 64KB */
411 for (i = 0; i < info->sector_count; i++) {
412 info->start[i] = amd040base + i * (64 * 1024);
413 /* Write auto select command sequence and query sector protection */
414 FLASH_CMD_SELECT (info->flash_id,
415 info->start[i] | CACHE_DISABLE_MASK);
417 GET__U8 (((info->start[i] + 2) | CACHE_DISABLE_MASK));
418 FLASH_CMD_RESET (info->flash_id,
419 amd040base | CACHE_DISABLE_MASK);
420 info->protect[i] = (flashtest & 0x0001);
424 return flash_info[0].size
425 #if CFG_MAX_FLASH_BANKS == 2
431 void flash_print_info (flash_info_t * info)
435 if (info->flash_id == FLASH_DEV_U7_2MB) {
436 printf ("AMD Am29LV160DB (2MB) 16KB,2x8KB,32KB,31x64KB\n");
437 } else if (info->flash_id == FLASH_DEV_U9_512KB) {
438 printf ("AMD Am29LV040B (512KB) 8x64KB\n");
440 printf ("Unknown flash_id ...\n");
444 printf (" Size: %ld KB in %d Sectors\n",
445 info->size >> 10, info->sector_count);
446 printf (" Sector Start Addresses:");
447 for (i = 0; i < info->sector_count; i++) {
450 printf (" S%02d @ 0x%08lX%s", i,
451 info->start[i], info->protect[i] ? " !" : " ");
456 int flash_erase (flash_info_t * info, int s_first, int s_last)
462 /* check flash protection bits */
463 if (_flash_check_protection (info, s_first, s_last)) {
464 printf (" Flash erase aborted due to protected sectors\n");
465 return ERR_PROTECTED;
468 if ((s_first < info->sector_count) && (s_first <= s_last)) {
469 for (i = s_first; i <= s_last && !error; i++) {
470 printf (" Erasing Sector %d @ 0x%08lx ... ", i,
472 /* bypass the cache to access the flash memory */
473 FLASH_CMD_ERASE_SEC (info->flash_id,
475 start[0] | CACHE_DISABLE_MASK),
477 start[i] | CACHE_DISABLE_MASK));
478 /* look for sector to become 0xFF after erase */
479 error = _flash_poll (info->flash_id,
481 start[i] | CACHE_DISABLE_MASK,
482 0xFF, CFG_FLASH_ERASE_TOUT);
483 FLASH_CMD_RESET (info->flash_id,
485 start[0] | CACHE_DISABLE_MASK));
497 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
504 /* bypass the cache to access the flash memory */
505 FLASH_CMD_UNLOCK_BYPASS (info->flash_id,
506 (info->start[0] | CACHE_DISABLE_MASK));
508 /* Write the Data to Flash */
510 bp = (u8 *) (addr | CACHE_DISABLE_MASK);
513 for (n = 0; n < cnt && !error; n++, bp++, bps++) {
515 if (!(n % (cnt / 15))) {
519 /* write the flash command for flash memory */
525 /* Check if the write is done */
526 for (i = 0; i < 0xff; i++);
527 error = _flash_poll (info->flash_id, (u32) bp, *bps,
528 CFG_FLASH_WRITE_TOUT);
534 /* Reset the Flash Mode to read */
535 FLASH_CMD_BYPASS_RESET (info->flash_id, info->start[0]);