MINI2440: No longer need the environment in NOR
[u-boot-openmoko/mini2440.git] / board / mini2440 / flash.c
blob5ad0264fc1f8da571e8bcf8e7122ca2abd5a6be1
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 PHYS_FLASH_SIZE
31 #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
33 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
36 #define CMD_READ_ARRAY 0x000000F0
37 #define CMD_UNLOCK1 0x000000AA
38 #define CMD_UNLOCK2 0x00000055
39 #define CMD_ERASE_SETUP 0x00000080
40 #define CMD_ERASE_CONFIRM 0x00000030
41 #define CMD_PROGRAM 0x000000A0
42 #define CMD_UNLOCK_BYPASS 0x00000020
44 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555 << 1)))
45 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA << 1)))
47 #define BIT_ERASE_DONE 0x00000080
48 #define BIT_RDY_MASK 0x00000080
49 #define BIT_PROGRAM_ERROR 0x00000020
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++) {
65 ulong flashbase = 0;
67 flash_info[i].flash_id =
68 #if defined(CONFIG_AMD_LV400)
69 (AMD_MANUFACT & FLASH_VENDMASK) |
70 (AMD_ID_LV400B & FLASH_TYPEMASK);
71 #elif defined(CONFIG_AMD_LV800)
72 (AMD_MANUFACT & FLASH_VENDMASK) |
73 (AMD_ID_LV800B & FLASH_TYPEMASK);
74 #elif defined(CONFIG_SST_VF1601)
75 (SST_MANUFACT & FLASH_VENDMASK) |
76 (SST_ID_xF1601 & FLASH_TYPEMASK);
77 #else
78 #error "Unknown flash configured"
79 #endif
80 flash_info[i].size = FLASH_BANK_SIZE;
81 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
82 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
83 if (i == 0)
84 flashbase = PHYS_FLASH_1;
85 else
86 panic ("configured too many flash banks!\n");
87 for (j = 0; j < flash_info[i].sector_count; j++) {
88 #ifndef CONFIG_SST_VF1601
89 if (j <= 3) {
90 /* 1st one is 16 KB */
91 if (j == 0) {
92 flash_info[i].start[j] =
93 flashbase + 0;
96 /* 2nd and 3rd are both 8 KB */
97 if ((j == 1) || (j == 2)) {
98 flash_info[i].start[j] =
99 flashbase + 0x4000 + (j -
100 1) *
101 0x2000;
104 /* 4th 32 KB */
105 if (j == 3) {
106 flash_info[i].start[j] =
107 flashbase + 0x8000;
109 } else {
110 flash_info[i].start[j] =
111 flashbase + (j - 3) * MAIN_SECT_SIZE;
113 #else
114 flash_info[i].start[j] =
115 flashbase + (j) * MAIN_SECT_SIZE;
116 #endif
118 size += flash_info[i].size;
121 flash_protect (FLAG_PROTECT_SET,
122 CFG_FLASH_BASE,
123 CFG_FLASH_BASE + monitor_flash_len - 1,
124 &flash_info[0]);
126 #if defined(CFG_ENV_ADDR)
127 flash_protect (FLAG_PROTECT_SET,
128 CFG_ENV_ADDR,
129 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
130 #endif
131 return size;
134 /*-----------------------------------------------------------------------
136 void flash_print_info (flash_info_t * info)
138 int i;
140 switch (info->flash_id & FLASH_VENDMASK) {
141 case (AMD_MANUFACT & FLASH_VENDMASK):
142 printf ("AMD: ");
143 break;
144 case (SST_MANUFACT & FLASH_VENDMASK):
145 printf ("SST: ");
146 break;
147 default:
148 printf ("Unknown Vendor ");
149 break;
152 switch (info->flash_id & FLASH_TYPEMASK) {
153 case (AMD_ID_LV400B & FLASH_TYPEMASK):
154 printf ("1x Amd29LV400BB (4Mbit)\n");
155 break;
156 case (AMD_ID_LV800B & FLASH_TYPEMASK):
157 printf ("1x Amd29LV800BB (8Mbit)\n");
158 break;
159 case (SST_ID_xF1601 & FLASH_TYPEMASK):
160 printf ("1x SST39VF1601 (2Mbit)\n");
161 break;
162 default:
163 printf ("Unknown Chip Type\n");
164 goto Done;
165 break;
168 printf (" Size: %ld MB in %d Sectors\n",
169 info->size >> 20, info->sector_count);
171 printf (" Sector Start Addresses:");
172 for (i = 0; i < info->sector_count; i++) {
173 if ((i % 5) == 0) {
174 printf ("\n ");
176 printf (" %08lX%s", info->start[i],
177 info->protect[i] ? " (RO)" : " ");
179 printf ("\n");
181 Done:;
184 /*-----------------------------------------------------------------------
187 int flash_erase (flash_info_t * info, int s_first, int s_last)
189 ushort result;
190 int iflag, cflag, prot, sect;
191 int rc = ERR_OK;
192 int chip;
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 #ifdef CONFIG_SST_VF1601
204 if ((info->flash_id & FLASH_VENDMASK) !=
205 (SST_MANUFACT & FLASH_VENDMASK)) {
206 return ERR_UNKNOWN_FLASH_VENDOR;
208 #else
209 if ((info->flash_id & FLASH_VENDMASK) !=
210 (AMD_MANUFACT & FLASH_VENDMASK)) {
211 return ERR_UNKNOWN_FLASH_VENDOR;
213 #endif
215 prot = 0;
216 for (sect = s_first; sect <= s_last; ++sect) {
217 if (info->protect[sect]) {
218 prot++;
221 if (prot)
222 return ERR_PROTECTED;
225 * Disable interrupts which might cause a timeout
226 * here. Remember that our exception vectors are
227 * at address 0 in the flash, and we don't want a
228 * (ticker) exception to happen while the flash
229 * chip is in programming mode.
231 cflag = icache_status ();
232 icache_disable ();
233 iflag = disable_interrupts ();
235 /* Start erase on unprotected sectors */
236 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
237 printf ("Erasing sector %2d ... ", sect);
239 /* arm simple, non interrupt dependent timer */
240 reset_timer_masked ();
242 if (info->protect[sect] == 0) { /* not protected */
243 vu_short *addr = (vu_short *) (info->start[sect]);
245 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
246 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
247 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
249 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
250 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
251 *addr = CMD_ERASE_CONFIRM;
253 #ifndef CONFIG_SST_VF1601
254 /* wait until flash is ready */
255 chip = 0;
257 do {
258 result = *addr;
260 /* check timeout */
261 if (get_timer_masked () >
262 CFG_FLASH_ERASE_TOUT) {
263 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
264 chip = TMO;
265 break;
268 if (!chip
269 && (result & 0xFFFF) & BIT_ERASE_DONE)
270 chip = READY;
272 if (!chip
273 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
274 chip = ERR;
276 } while (!chip);
278 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
280 if (chip == ERR) {
281 rc = ERR_PROG_ERROR;
282 goto outahere;
284 if (chip == TMO) {
285 rc = ERR_TIMOUT;
286 goto outahere;
288 #else
289 /* wait until flash is ready */
290 while(1){
291 unsigned short i;
292 i = *((volatile unsigned short *)addr)&0x40;
293 if(i!=*((volatile unsigned short *)addr)&0x40)
294 continue;
295 if(*((volatile unsigned short *)addr)&0x80)
296 break;
298 #endif
299 printf ("ok.\n");
300 } else { /* it was protected */
302 printf ("protected!\n");
306 if (ctrlc ())
307 printf ("User Interrupt!\n");
309 outahere:
310 /* allow flash to settle - wait 10 ms */
311 udelay_masked (10000);
313 if (iflag)
314 enable_interrupts ();
316 if (cflag)
317 icache_enable ();
319 return rc;
322 /*-----------------------------------------------------------------------
323 * Copy memory to flash
326 volatile static int write_hword (flash_info_t * info, ulong dest, ushort data)
328 vu_short *addr = (vu_short *) dest;
329 ushort result;
330 int rc = ERR_OK;
331 int cflag, iflag;
332 int chip;
335 * Check if Flash is (sufficiently) erased
337 result = *addr;
338 if ((result & data) != data)
339 return ERR_NOT_ERASED;
343 * Disable interrupts which might cause a timeout
344 * here. Remember that our exception vectors are
345 * at address 0 in the flash, and we don't want a
346 * (ticker) exception to happen while the flash
347 * chip is in programming mode.
349 cflag = icache_status ();
350 icache_disable ();
351 iflag = disable_interrupts ();
353 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
354 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
355 MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
356 *addr = CMD_PROGRAM;
357 *addr = data;
359 /* arm simple, non interrupt dependent timer */
360 reset_timer_masked ();
362 /* wait until flash is ready */
363 chip = 0;
364 do {
365 result = *addr;
367 /* check timeout */
368 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
369 chip = ERR | TMO;
370 break;
372 if (!chip && ((result & 0x80) == (data & 0x80)))
373 chip = READY;
375 if (!chip && ((result & 0xFFFF) & BIT_PROGRAM_ERROR)) {
376 result = *addr;
378 if ((result & 0x80) == (data & 0x80))
379 chip = READY;
380 else
381 chip = ERR;
384 } while (!chip);
386 *addr = CMD_READ_ARRAY;
388 if (chip == ERR || *addr != data)
389 rc = ERR_PROG_ERROR;
391 if (iflag)
392 enable_interrupts ();
394 if (cflag)
395 icache_enable ();
397 return rc;
400 /*-----------------------------------------------------------------------
401 * Copy memory to flash.
404 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
406 ulong cp, wp;
407 int l;
408 int i, rc;
409 ushort data;
411 wp = (addr & ~1); /* get lower word aligned address */
414 * handle unaligned start bytes
416 if ((l = addr - wp) != 0) {
417 data = 0;
418 for (i = 0, cp = wp; i < l; ++i, ++cp) {
419 data = (data >> 8) | (*(uchar *) cp << 8);
421 for (; i < 2 && cnt > 0; ++i) {
422 data = (data >> 8) | (*src++ << 8);
423 --cnt;
424 ++cp;
426 for (; cnt == 0 && i < 2; ++i, ++cp) {
427 data = (data >> 8) | (*(uchar *) cp << 8);
430 if ((rc = write_hword (info, wp, data)) != 0) {
431 return (rc);
433 wp += 2;
437 * handle word aligned part
439 while (cnt >= 2) {
440 data = *((vu_short *) src);
441 if ((rc = write_hword (info, wp, data)) != 0) {
442 return (rc);
444 src += 2;
445 wp += 2;
446 cnt -= 2;
449 if (cnt == 0) {
450 return ERR_OK;
454 * handle unaligned tail bytes
456 data = 0;
457 for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
458 data = (data >> 8) | (*src++ << 8);
459 --cnt;
461 for (; i < 2; ++i, ++cp) {
462 data = (data >> 8) | (*(uchar *) cp << 8);
465 return write_hword (info, wp, data);