blob: e80df0bcf4bb1b581834266926ce4300bead837a [file] [log] [blame]
Peter Pearseb1805292007-05-09 11:42:44 +01001/*
2 * (C) Copyright 2006 Embedded Artists AB <www.embeddedartists.com>
3 *
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
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <asm/byteorder.h>
26#include <asm/arch/hardware.h>
27
Peter Pearsefb312c82007-05-18 16:47:03 +010028static unsigned long flash_addr_table[CFG_MAX_FLASH_BANKS]
29 = CFG_FLASH_BANKS_LIST;
Peter Pearseb1805292007-05-09 11:42:44 +010030flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
31
32extern int lpc2292_copy_buffer_to_flash(flash_info_t *, ulong);
33extern int lpc2292_flash_erase(flash_info_t *, int, int);
34extern int lpc2292_write_buff (flash_info_t *, uchar *, ulong, ulong);
35static unsigned long ext_flash_init(void);
36static int ext_flash_erase(flash_info_t *, int, int);
37static int ext_write_buff(flash_info_t *, uchar *, ulong, ulong);
38
39/*-----------------------------------------------------------------------
40 */
41
42ulong flash_init (void)
43{
44 int j, k;
45 ulong size = 0;
46 ulong flashbase = 0;
47
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;
56 }
57 for (j = 0; j < 2; j++, k++) {
58 flash_info[0].start[k] = flashbase;
59 flashbase += 0x00010000;
60 }
61 for (j = 0; j < 7; j++, k++) {
62 flash_info[0].start[k] = flashbase;
63 flashbase += 0x00002000;
64 }
65 size += flash_info[0].size;
66
67 /* Protect monitor and environment sectors */
68 flash_protect (FLAG_PROTECT_SET,
69 0x0,
70 0x0 + monitor_flash_len - 1,
71 &flash_info[0]);
72
73 flash_protect (FLAG_PROTECT_SET,
74 CFG_ENV_ADDR,
75 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
76 &flash_info[0]);
77
78 size += ext_flash_init();
79
80 return size;
81}
82
83/*-----------------------------------------------------------------------
84 */
85void flash_print_info (flash_info_t * info)
86{
87 int i;
88 int erased = 0;
89 unsigned long j;
90 unsigned long count;
91 unsigned char *p;
92
93 switch (info->flash_id & FLASH_VENDMASK) {
94 case (PHILIPS_LPC2292 & FLASH_VENDMASK):
95 printf("Philips: ");
96 break;
97 case FLASH_MAN_AMD:
98 printf("AMD: ");
99 break;
100 default:
101 printf ("Unknown Vendor ");
102 break;
103 }
104
105 switch (info->flash_id & FLASH_TYPEMASK) {
106 case (PHILIPS_LPC2292 & FLASH_TYPEMASK):
107 printf("LPC2292 internal flash\n");
108 break;
109 case FLASH_S29GL128N:
110 printf ("S29GL128N (128 Mbit, uniform sector size)\n");
111 break;
112 default:
113 printf("Unknown Chip Type\n");
114 return;
115 }
116
117 printf (" Size: %ld KB in %d Sectors\n",
118 info->size >> 10, info->sector_count);
119
120 printf (" Sector Start Addresses:");
121 for (i = 0; i < info->sector_count; i++) {
122 if ((i % 5) == 0) {
123 printf ("\n ");
124 }
125 if (i < (info->sector_count - 1)) {
126 count = info->start[i+1] - info->start[i];
127 }
128 else {
129 count = info->start[0] + info->size - info->start[i];
130 }
131 p = (unsigned char*)(info->start[i]);
132 erased = 1;
133 for (j = 0; j < count; j++) {
134 if (*p != 0xFF) {
135 erased = 0;
136 break;
137 }
138 p++;
139 }
140 printf (" %08lX%s%s", info->start[i], info->protect[i] ? " RO" : " ",
141 erased ? " E" : " ");
142 }
143 printf ("\n");
144}
145
146int flash_erase (flash_info_t * info, int s_first, int s_last)
147{
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);
153 default:
154 return ERR_PROTECTED;
155 }
156 return ERR_PROTECTED;
157}
158
159int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
160{
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);
166 default:
167 return ERR_PROG_ERROR;
168 }
169 return ERR_PROG_ERROR;
170}
171
172/*--------------------------------------------------------------------------
173 * From here on is code for the external S29GL128N taken from cam5200_flash.c
174 */
175
176#define CFG_FLASH_WORD_SIZE unsigned short
177
178static int wait_for_DQ7_32(flash_info_t * info, int sect)
179{
180 ulong start, now, last;
181 volatile CFG_FLASH_WORD_SIZE *addr =
182 (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
183
184 start = get_timer(0);
185 last = start;
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) {
189 printf("Timeout\n");
190 return -1;
191 }
192 /* show that we're waiting */
193 if ((now - last) > 1000) { /* every second */
194 putc('.');
195 last = now;
196 }
197 }
198 return 0;
199}
200
201int ext_flash_erase(flash_info_t * info, int s_first, int s_last)
202{
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;
206
207 ret = 0;
208 if ((s_first < 0) || (s_first > s_last)) {
209 if (info->flash_id == FLASH_UNKNOWN)
210 printf("- missing\n");
211 else
212 printf("- no sectors to erase\n");
213 return 1;
214 }
215
216 if (info->flash_id == FLASH_UNKNOWN) {
217 printf("Can't erase unknown flash type - aborted\n");
218 return 1;
219 }
220
221 prot = 0;
222 for (sect = s_first; sect <= s_last; ++sect) {
223 if (info->protect[sect])
224 prot++;
225 }
226
227 if (prot)
228 printf("- Warning: %d protected sectors will not be erased!", prot);
229
230 printf("\n");
231
232 l_sect = -1;
233
234 /* Disable interrupts which might cause a timeout here */
235 flag = disable_interrupts();
236
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]);
241
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 */
248
249 l_sect = sect;
250 /*
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)!!
256 */
257 ret = wait_for_DQ7_32(info, sect);
258 if (ret) {
259 ret = ERR_PROTECTED;
260 break;
261 }
262 }
263 }
264
265 /* re-enable interrupts if necessary */
266 if (flag)
267 enable_interrupts();
268
269 /* wait at least 80us - let's wait 1 ms */
270 udelay(1000);
271
272 /* reset to read mode */
273 addr = (CFG_FLASH_WORD_SIZE *) info->start[0];
274 addr[0] = (CFG_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
275
276 if (ret)
277 printf(" error\n");
278 else
279 printf(" done\n");
280 return ret;
281}
282
283static ulong flash_get_size(vu_long * addr, flash_info_t * info)
284{
285 short i;
286 CFG_FLASH_WORD_SIZE value;
287 ulong base = (ulong) addr;
288 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) addr;
289
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;
294 udelay(1000);
295
296 value = addr2[0];
297
298 switch (value) {
299 case (CFG_FLASH_WORD_SIZE) AMD_MANUFACT:
300 info->flash_id = FLASH_MAN_AMD;
301 break;
302 default:
303 info->flash_id = FLASH_UNKNOWN;
304 info->sector_count = 0;
305 info->size = 0;
306 return (0); /* no or unknown flash */
307 }
308
309 value = addr2[1]; /* device ID */
310
311 switch (value) {
312 case (CFG_FLASH_WORD_SIZE)AMD_ID_MIRROR:
313 value = addr2[14];
314 switch(value) {
315 case (CFG_FLASH_WORD_SIZE)AMD_ID_GL128N_2:
316 value = addr2[15];
317 if (value != (CFG_FLASH_WORD_SIZE)AMD_ID_GL128N_3) {
318 info->flash_id = FLASH_UNKNOWN;
319 } else {
320 info->flash_id += FLASH_S29GL128N;
321 info->sector_count = 128;
322 info->size = 0x01000000;
323 }
324 break;
325 default:
326 info->flash_id = FLASH_UNKNOWN;
327 return(0);
328 }
329 break;
330
331 default:
332 info->flash_id = FLASH_UNKNOWN;
333 return (0); /* => no or unknown flash */
334 }
335
336 /* set up sector start address table */
337 for (i = 0; i < info->sector_count; i++)
338 info->start[i] = base + (i * 0x00020000);
339
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]);
345
346 info->protect[i] = addr2[2] & 1;
347 }
348
349 /* issue bank reset to return to read mode */
350 addr2[0] = (CFG_FLASH_WORD_SIZE) 0x00F000F0;
351
352 return (info->size);
353}
354
355static unsigned long ext_flash_init(void)
356{
357 unsigned long total_b = 0;
358 unsigned long size_b[CFG_MAX_FLASH_BANKS];
359 int i;
360
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;
366
367 /* call flash_get_size() to initialize sector address */
368 size_b[i] = flash_get_size((vu_long *) flash_addr_table[i],
369 &flash_info[i]);
370
371 flash_info[i].size = size_b[i];
372
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;
378 }
379
380 total_b += flash_info[i].size;
381 }
382
383 return total_b;
384}
385
386static int write_word(flash_info_t * info, ulong dest, ushort data)
387{
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;
391 ulong start;
392 int flag;
393
394 /* Check if Flash is (sufficiently) erased */
395 if ((*dest2 & *data2) != *data2) {
396 return (2);
397 }
398
399 /* Disable interrupts which might cause a timeout here */
400 flag = disable_interrupts();
401
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;
405 *dest2 = *data2;
406
407 /* re-enable interrupts if necessary */
408 if (flag)
409 enable_interrupts();
410
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)) {
415
416 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
417 printf("WRITE_TOUT\n");
418 return (1);
419 }
420 }
421 return (0);
422}
423
424/*-----------------------------------------------------------------------
425 * This is taken from the original flash.c for the LPC2292 SODIMM board
426 * and modified to suit.
427 */
428
429int ext_write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
430{
431 ushort tmp;
432 ulong i;
433 uchar* src_org;
434 uchar* dst_org;
435 ulong cnt_org = cnt;
436 int ret = ERR_OK;
437
438 src_org = src;
439 dst_org = (uchar*)addr;
440
441 if (addr & 1) { /* if odd address */
442 tmp = *((uchar*)(addr - 1)); /* little endian */
443 tmp |= (*src << 8);
444 if (write_word(info, addr - 1, tmp))
445 return ERR_PROG_ERROR;
446 addr += 1;
447 cnt -= 1;
448 src++;
449 }
450 while (cnt > 1) {
451 tmp = ((*(src+1)) << 8) + (*src); /* little endian */
452 if (write_word(info, addr, tmp))
453 return ERR_PROG_ERROR;
454 addr += 2;
455 src += 2;
456 cnt -= 2;
457 }
458 if (cnt > 0) {
459 tmp = (*((uchar*)(addr + 1))) << 8;
460 tmp |= *src;
461 if (write_word(info, addr, tmp))
462 return ERR_PROG_ERROR;
463 }
464
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;
469 break;
470 }
471 dst_org++;
472 src_org++;
473 }
474
475 return ret;
476}