2 * (C) Copyright 2006 Embedded Artists AB <www.embeddedartists.com>
4 * (C) Copyright 2007 Gary Jennejohn garyj@denx.de
5 * Modified to use the routines in cpu/arm720t/lpc2292/flash.c.
6 * Heavily modified to support the SMN42 board from Siemens
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include <asm/byteorder.h>
26 #include <asm/arch/hardware.h>
28 static unsigned long flash_addr_table[CFG_MAX_FLASH_BANKS]
29 = CFG_FLASH_BANKS_LIST;
30 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
32 extern int lpc2292_copy_buffer_to_flash(flash_info_t *, ulong);
33 extern int lpc2292_flash_erase(flash_info_t *, int, int);
34 extern int lpc2292_write_buff (flash_info_t *, uchar *, ulong, ulong);
35 static unsigned long ext_flash_init(void);
36 static int ext_flash_erase(flash_info_t *, int, int);
37 static int ext_write_buff(flash_info_t *, uchar *, ulong, ulong);
39 /*-----------------------------------------------------------------------
42 ulong flash_init (void)
48 flash_info[0].flash_id = PHILIPS_LPC2292;
49 flash_info[0].size = 0x003E000; /* 256 - 8 KB */
50 flash_info[0].sector_count = 17;
51 memset (flash_info[0].protect, 0, 17);
52 flashbase = 0x00000000;
53 for (j = 0, k = 0; j < 8; j++, k++) {
54 flash_info[0].start[k] = flashbase;
55 flashbase += 0x00002000;
57 for (j = 0; j < 2; j++, k++) {
58 flash_info[0].start[k] = flashbase;
59 flashbase += 0x00010000;
61 for (j = 0; j < 7; j++, k++) {
62 flash_info[0].start[k] = flashbase;
63 flashbase += 0x00002000;
65 size += flash_info[0].size;
67 /* Protect monitor and environment sectors */
68 flash_protect (FLAG_PROTECT_SET,
70 0x0 + monitor_flash_len - 1,
73 flash_protect (FLAG_PROTECT_SET,
75 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
78 size += ext_flash_init();
83 /*-----------------------------------------------------------------------
85 void flash_print_info (flash_info_t * info)
93 switch (info->flash_id & FLASH_VENDMASK) {
94 case (PHILIPS_LPC2292 & FLASH_VENDMASK):
101 printf ("Unknown Vendor ");
105 switch (info->flash_id & FLASH_TYPEMASK) {
106 case (PHILIPS_LPC2292 & FLASH_TYPEMASK):
107 printf("LPC2292 internal flash\n");
109 case FLASH_S29GL128N:
110 printf ("S29GL128N (128 Mbit, uniform sector size)\n");
113 printf("Unknown Chip Type\n");
117 printf (" Size: %ld KB in %d Sectors\n",
118 info->size >> 10, info->sector_count);
120 printf (" Sector Start Addresses:");
121 for (i = 0; i < info->sector_count; i++) {
125 if (i < (info->sector_count - 1)) {
126 count = info->start[i+1] - info->start[i];
129 count = info->start[0] + info->size - info->start[i];
131 p = (unsigned char*)(info->start[i]);
133 for (j = 0; j < count; j++) {
140 printf (" %08lX%s%s", info->start[i], info->protect[i] ? " RO" : " ",
141 erased ? " E" : " ");
146 int flash_erase (flash_info_t * info, int s_first, int s_last)
148 switch (info->flash_id & FLASH_TYPEMASK) {
149 case (PHILIPS_LPC2292 & FLASH_TYPEMASK):
150 return lpc2292_flash_erase(info, s_first, s_last);
151 case FLASH_S29GL128N:
152 return ext_flash_erase(info, s_first, s_last);
154 return ERR_PROTECTED;
156 return ERR_PROTECTED;
159 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
161 switch (info->flash_id & FLASH_TYPEMASK) {
162 case (PHILIPS_LPC2292 & FLASH_TYPEMASK):
163 return lpc2292_write_buff(info, src, addr, cnt);
164 case FLASH_S29GL128N:
165 return ext_write_buff(info, src, addr, cnt);
167 return ERR_PROG_ERROR;
169 return ERR_PROG_ERROR;
172 /*--------------------------------------------------------------------------
173 * From here on is code for the external S29GL128N taken from cam5200_flash.c
176 #define CFG_FLASH_WORD_SIZE unsigned short
178 static int wait_for_DQ7_32(flash_info_t * info, int sect)
180 ulong start, now, last;
181 volatile CFG_FLASH_WORD_SIZE *addr =
182 (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
184 start = get_timer(0);
186 while ((addr[0] & (CFG_FLASH_WORD_SIZE) 0x00800080) !=
187 (CFG_FLASH_WORD_SIZE) 0x00800080) {
188 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
192 /* show that we're waiting */
193 if ((now - last) > 1000) { /* every second */
201 int ext_flash_erase(flash_info_t * info, int s_first, int s_last)
203 volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
204 volatile CFG_FLASH_WORD_SIZE *addr2;
205 int flag, prot, sect, l_sect, ret;
208 if ((s_first < 0) || (s_first > s_last)) {
209 if (info->flash_id == FLASH_UNKNOWN)
210 printf("- missing\n");
212 printf("- no sectors to erase\n");
216 if (info->flash_id == FLASH_UNKNOWN) {
217 printf("Can't erase unknown flash type - aborted\n");
222 for (sect = s_first; sect <= s_last; ++sect) {
223 if (info->protect[sect])
228 printf("- Warning: %d protected sectors will not be erased!", prot);
234 /* Disable interrupts which might cause a timeout here */
235 flag = disable_interrupts();
237 /* Start erase on unprotected sectors */
238 for (sect = s_first; sect <= s_last; sect++) {
239 if (info->protect[sect] == 0) { /* not protected */
240 addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
242 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
243 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
244 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00800080;
245 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
246 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
247 addr2[0] = (CFG_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
251 * Wait for each sector to complete, it's more
252 * reliable. According to AMD Spec, you must
253 * issue all erase commands within a specified
254 * timeout. This has been seen to fail, especially
255 * if printf()s are included (for debug)!!
257 ret = wait_for_DQ7_32(info, sect);
265 /* re-enable interrupts if necessary */
269 /* wait at least 80us - let's wait 1 ms */
272 /* reset to read mode */
273 addr = (CFG_FLASH_WORD_SIZE *) info->start[0];
274 addr[0] = (CFG_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
283 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
286 CFG_FLASH_WORD_SIZE value;
287 ulong base = (ulong) addr;
288 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) addr;
290 /* Write auto select command: read Manufacturer ID */
291 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
292 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
293 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00900090;
299 case (CFG_FLASH_WORD_SIZE) AMD_MANUFACT:
300 info->flash_id = FLASH_MAN_AMD;
303 info->flash_id = FLASH_UNKNOWN;
304 info->sector_count = 0;
306 return (0); /* no or unknown flash */
309 value = addr2[1]; /* device ID */
312 case (CFG_FLASH_WORD_SIZE)AMD_ID_MIRROR:
315 case (CFG_FLASH_WORD_SIZE)AMD_ID_GL128N_2:
317 if (value != (CFG_FLASH_WORD_SIZE)AMD_ID_GL128N_3) {
318 info->flash_id = FLASH_UNKNOWN;
320 info->flash_id += FLASH_S29GL128N;
321 info->sector_count = 128;
322 info->size = 0x01000000;
326 info->flash_id = FLASH_UNKNOWN;
332 info->flash_id = FLASH_UNKNOWN;
333 return (0); /* => no or unknown flash */
336 /* set up sector start address table */
337 for (i = 0; i < info->sector_count; i++)
338 info->start[i] = base + (i * 0x00020000);
340 /* check for protected sectors */
341 for (i = 0; i < info->sector_count; i++) {
342 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
343 /* D0 = 1 if protected */
344 addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
346 info->protect[i] = addr2[2] & 1;
349 /* issue bank reset to return to read mode */
350 addr2[0] = (CFG_FLASH_WORD_SIZE) 0x00F000F0;
355 static unsigned long ext_flash_init(void)
357 unsigned long total_b = 0;
358 unsigned long size_b[CFG_MAX_FLASH_BANKS];
361 /* Init: no FLASHes known */
362 for (i = 1; i < CFG_MAX_FLASH_BANKS; ++i) {
363 flash_info[i].flash_id = FLASH_UNKNOWN;
364 flash_info[i].sector_count = -1;
365 flash_info[i].size = 0;
367 /* call flash_get_size() to initialize sector address */
368 size_b[i] = flash_get_size((vu_long *) flash_addr_table[i],
371 flash_info[i].size = size_b[i];
373 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
374 printf("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
375 i+1, size_b[i], size_b[i] << 20);
376 flash_info[i].sector_count = -1;
377 flash_info[i].size = 0;
380 total_b += flash_info[i].size;
386 static int write_word(flash_info_t * info, ulong dest, ushort data)
388 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
389 volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *) dest;
390 volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *) &data;
394 /* Check if Flash is (sufficiently) erased */
395 if ((*dest2 & *data2) != *data2) {
399 /* Disable interrupts which might cause a timeout here */
400 flag = disable_interrupts();
402 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
403 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
404 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00A000A0;
407 /* re-enable interrupts if necessary */
411 /* data polling for D7 */
412 start = get_timer(0);
413 while ((*dest2 & (CFG_FLASH_WORD_SIZE) 0x00800080) !=
414 (*data2 & (CFG_FLASH_WORD_SIZE) 0x00800080)) {
416 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
417 printf("WRITE_TOUT\n");
424 /*-----------------------------------------------------------------------
425 * This is taken from the original flash.c for the LPC2292 SODIMM board
426 * and modified to suit.
429 int ext_write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
439 dst_org = (uchar*)addr;
441 if (addr & 1) { /* if odd address */
442 tmp = *((uchar*)(addr - 1)); /* little endian */
444 if (write_word(info, addr - 1, tmp))
445 return ERR_PROG_ERROR;
451 tmp = ((*(src+1)) << 8) + (*src); /* little endian */
452 if (write_word(info, addr, tmp))
453 return ERR_PROG_ERROR;
459 tmp = (*((uchar*)(addr + 1))) << 8;
461 if (write_word(info, addr, tmp))
462 return ERR_PROG_ERROR;
465 for (i = 0; i < cnt_org; i++) {
466 if (*dst_org != *src_org) {
467 printf("Write failed. Byte %lX differs\n", i);
468 ret = ERR_PROG_ERROR;