MINI2440: Auto probe for SDRAM size
[u-boot-openmoko/mini2440.git] / board / shannon / flash.c
blob475b76b30bdcafee719199c02c917e712f8cbcb8
1 /*
2 * (C) Copyright 2002
3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Alex Zuepke <azu@sysgo.de>
6 * See file CREDITS for list of people who contributed to this
7 * project.
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,
22 * MA 02111-1307 USA
25 #include <common.h>
27 ulong myflush(void);
30 #define FLASH_BANK_SIZE 0x400000 /* 4 MB */
31 #define MAIN_SECT_SIZE 0x20000 /* 128 KB */
33 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
36 #define CMD_READ_ARRAY 0x00F000F0
37 #define CMD_UNLOCK1 0x00AA00AA
38 #define CMD_UNLOCK2 0x00550055
39 #define CMD_ERASE_SETUP 0x00800080
40 #define CMD_ERASE_CONFIRM 0x00300030
41 #define CMD_PROGRAM 0x00A000A0
42 #define CMD_UNLOCK_BYPASS 0x00200020
44 #define MEM_FLASH_ADDR1 (*(volatile u32 *)(CFG_FLASH_BASE + (0x00000555 << 2)))
45 #define MEM_FLASH_ADDR2 (*(volatile u32 *)(CFG_FLASH_BASE + (0x000002AA << 2)))
47 #define BIT_ERASE_DONE 0x00800080
48 #define BIT_RDY_MASK 0x00800080
49 #define BIT_PROGRAM_ERROR 0x00200020
50 #define BIT_TIMEOUT 0x80000000 /* our flag */
52 #define READY 1
53 #define ERR 2
54 #define TMO 4
56 /*-----------------------------------------------------------------------
59 ulong flash_init(void)
61 int i, j;
62 ulong size = 0;
64 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
66 ulong flashbase = 0;
67 flash_info[i].flash_id =
68 (AMD_MANUFACT & FLASH_VENDMASK) |
69 (AMD_ID_LV160B & FLASH_TYPEMASK);
70 flash_info[i].size = FLASH_BANK_SIZE;
71 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
72 memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
73 if (i == 0)
74 flashbase = PHYS_FLASH_1;
75 else
76 panic("configured too many flash banks!\n");
77 for (j = 0; j < flash_info[i].sector_count; j++)
80 if (j <= 3)
82 /* 1st one is 32 KB */
83 if (j == 0)
85 flash_info[i].start[j] = flashbase + 0;
88 /* 2nd and 3rd are both 16 KB */
89 if ((j == 1) || (j == 2))
91 flash_info[i].start[j] = flashbase + 0x8000 + (j-1)*0x4000;
94 /* 4th 64 KB */
95 if (j == 3)
97 flash_info[i].start[j] = flashbase + 0x10000;
100 else
102 flash_info[i].start[j] = flashbase + (j - 3)*MAIN_SECT_SIZE;
105 size += flash_info[i].size;
109 * Protect monitor and environment sectors
110 * Inferno is complicated, it's hardware locked
112 #ifdef CONFIG_INFERNO
113 /* first one, 0x00000 to 0x07fff */
114 flash_protect(FLAG_PROTECT_SET,
115 CFG_FLASH_BASE + 0x00000,
116 CFG_FLASH_BASE + 0x08000 - 1,
117 &flash_info[0]);
119 /* third to 10th, 0x0c000 - 0xdffff */
120 flash_protect(FLAG_PROTECT_SET,
121 CFG_FLASH_BASE + 0x0c000,
122 CFG_FLASH_BASE + 0xe0000 - 1,
123 &flash_info[0]);
124 #else
125 flash_protect(FLAG_PROTECT_SET,
126 CFG_FLASH_BASE,
127 CFG_FLASH_BASE + monitor_flash_len - 1,
128 &flash_info[0]);
130 flash_protect(FLAG_PROTECT_SET,
131 CFG_ENV_ADDR,
132 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
133 &flash_info[0]);
134 #endif
135 return size;
138 /*-----------------------------------------------------------------------
140 void flash_print_info (flash_info_t *info)
142 int i;
144 switch (info->flash_id & FLASH_VENDMASK)
146 case (AMD_MANUFACT & FLASH_VENDMASK):
147 printf("AMD: ");
148 break;
149 default:
150 printf("Unknown Vendor ");
151 break;
154 switch (info->flash_id & FLASH_TYPEMASK)
156 case (AMD_ID_LV160B & FLASH_TYPEMASK):
157 printf("2x Amd29F160BB (16Mbit)\n");
158 break;
159 default:
160 printf("Unknown Chip Type\n");
161 goto Done;
162 break;
165 printf(" Size: %ld MB in %d Sectors\n",
166 info->size >> 20, info->sector_count);
168 printf(" Sector Start Addresses:");
169 for (i = 0; i < info->sector_count; i++)
171 if ((i % 5) == 0)
173 printf ("\n ");
175 printf (" %08lX%s", info->start[i],
176 info->protect[i] ? " (RO)" : " ");
178 printf ("\n");
180 Done:
184 /*-----------------------------------------------------------------------
187 int flash_erase (flash_info_t *info, int s_first, int s_last)
189 ulong result;
190 int iflag, cflag, prot, sect;
191 int rc = ERR_OK;
192 int chip1, chip2;
194 /* first look for protection bits */
196 if (info->flash_id == FLASH_UNKNOWN)
197 return ERR_UNKNOWN_FLASH_TYPE;
199 if ((s_first < 0) || (s_first > s_last)) {
200 return ERR_INVAL;
203 if ((info->flash_id & FLASH_VENDMASK) !=
204 (AMD_MANUFACT & FLASH_VENDMASK)) {
205 return ERR_UNKNOWN_FLASH_VENDOR;
208 prot = 0;
209 for (sect=s_first; sect<=s_last; ++sect) {
210 if (info->protect[sect]) {
211 prot++;
214 if (prot)
215 return ERR_PROTECTED;
218 * Disable interrupts which might cause a timeout
219 * here. Remember that our exception vectors are
220 * at address 0 in the flash, and we don't want a
221 * (ticker) exception to happen while the flash
222 * chip is in programming mode.
224 cflag = icache_status();
225 icache_disable();
226 iflag = disable_interrupts();
228 /* Start erase on unprotected sectors */
229 for (sect = s_first; sect<=s_last && !ctrlc(); sect++)
231 printf("Erasing sector %2d ... ", sect);
233 /* arm simple, non interrupt dependent timer */
234 reset_timer_masked();
236 if (info->protect[sect] == 0)
237 { /* not protected */
238 vu_long *addr = (vu_long *)(info->start[sect]);
240 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
241 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
242 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
244 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
245 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
246 *addr = CMD_ERASE_CONFIRM;
248 /* wait until flash is ready */
249 chip1 = chip2 = 0;
253 result = *addr;
255 /* check timeout */
256 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
258 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
259 chip1 = TMO;
260 break;
263 if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
264 chip1 = READY;
266 if (!chip1 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
267 chip1 = ERR;
269 if (!chip2 && (result >> 16) & BIT_ERASE_DONE)
270 chip2 = READY;
272 if (!chip2 && (result >> 16) & BIT_PROGRAM_ERROR)
273 chip2 = ERR;
275 } while (!chip1 || !chip2);
277 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
279 if (chip1 == ERR || chip2 == ERR)
281 rc = ERR_PROG_ERROR;
282 goto outahere;
284 if (chip1 == TMO)
286 rc = ERR_TIMOUT;
287 goto outahere;
290 printf("ok.\n");
292 else /* it was protected */
294 printf("protected!\n");
298 if (ctrlc())
299 printf("User Interrupt!\n");
301 outahere:
302 /* allow flash to settle - wait 10 ms */
303 udelay_masked(10000);
305 if (iflag)
306 enable_interrupts();
308 if (cflag)
309 icache_enable();
311 return rc;
314 /*-----------------------------------------------------------------------
315 * Copy memory to flash
318 static int write_word (flash_info_t *info, ulong dest, ulong data)
320 vu_long *addr = (vu_long *)dest;
321 ulong result;
322 int rc = ERR_OK;
323 int cflag, iflag;
324 int chip1, chip2;
327 * Check if Flash is (sufficiently) erased
329 result = *addr;
330 if ((result & data) != data)
331 return ERR_NOT_ERASED;
335 * Disable interrupts which might cause a timeout
336 * here. Remember that our exception vectors are
337 * at address 0 in the flash, and we don't want a
338 * (ticker) exception to happen while the flash
339 * chip is in programming mode.
341 cflag = icache_status();
342 icache_disable();
343 iflag = disable_interrupts();
345 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
346 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
347 MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
348 *addr = CMD_PROGRAM;
349 *addr = data;
351 /* arm simple, non interrupt dependent timer */
352 reset_timer_masked();
354 /* wait until flash is ready */
355 chip1 = chip2 = 0;
358 result = *addr;
360 /* check timeout */
361 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
363 chip1 = ERR | TMO;
364 break;
366 if (!chip1 && ((result & 0x80) == (data & 0x80)))
367 chip1 = READY;
369 if (!chip1 && ((result & 0xFFFF) & BIT_PROGRAM_ERROR))
371 result = *addr;
373 if ((result & 0x80) == (data & 0x80))
374 chip1 = READY;
375 else
376 chip1 = ERR;
379 if (!chip2 && ((result & (0x80 << 16)) == (data & (0x80 << 16))))
380 chip2 = READY;
382 if (!chip2 && ((result >> 16) & BIT_PROGRAM_ERROR))
384 result = *addr;
386 if ((result & (0x80 << 16)) == (data & (0x80 << 16)))
387 chip2 = READY;
388 else
389 chip2 = ERR;
392 } while (!chip1 || !chip2);
394 *addr = CMD_READ_ARRAY;
396 if (chip1 == ERR || chip2 == ERR || *addr != data)
397 rc = ERR_PROG_ERROR;
399 if (iflag)
400 enable_interrupts();
402 if (cflag)
403 icache_enable();
405 return rc;
408 /*-----------------------------------------------------------------------
409 * Copy memory to flash.
412 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
414 ulong cp, wp, data;
415 int l;
416 int i, rc;
418 wp = (addr & ~3); /* get lower word aligned address */
421 * handle unaligned start bytes
423 if ((l = addr - wp) != 0) {
424 data = 0;
425 for (i=0, cp=wp; i<l; ++i, ++cp) {
426 data = (data >> 8) | (*(uchar *)cp << 24);
428 for (; i<4 && cnt>0; ++i) {
429 data = (data >> 8) | (*src++ << 24);
430 --cnt;
431 ++cp;
433 for (; cnt==0 && i<4; ++i, ++cp) {
434 data = (data >> 8) | (*(uchar *)cp << 24);
437 if ((rc = write_word(info, wp, data)) != 0) {
438 return (rc);
440 wp += 4;
444 * handle word aligned part
446 while (cnt >= 4) {
447 data = *((vu_long*)src);
448 if ((rc = write_word(info, wp, data)) != 0) {
449 return (rc);
451 src += 4;
452 wp += 4;
453 cnt -= 4;
456 if (cnt == 0) {
457 return ERR_OK;
461 * handle unaligned tail bytes
463 data = 0;
464 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
465 data = (data >> 8) | (*src++ << 24);
466 --cnt;
468 for (; i<4; ++i, ++cp) {
469 data = (data >> 8) | (*(uchar *)cp << 24);
472 return write_word(info, wp, data);