MINI2440: Removed extra SDRAM probe
[u-boot-openmoko/mini2440.git] / board / mini2440 / flash.c
blobb4b85995f14a402dafcd1266ce77ac5f2239dd1d
1 /*
2 * (C) Copyright 2002
3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Alex Zuepke <azu@sysgo.de>
5 *
6 * (C) Copyright 2009
7 * Michel Pollet <buserror@gmail.com>
9 * See file CREDITS for list of people who contributed to this
10 * project.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
28 #include <common.h>
30 ulong myflush (void);
33 #define FLASH_BANK_SIZE PHYS_FLASH_SIZE
34 #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
36 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
39 #define CMD_READ_ARRAY 0x000000F0
40 #define CMD_UNLOCK1 0x000000AA
41 #define CMD_UNLOCK2 0x00000055
42 #define CMD_ERASE_SETUP 0x00000080
43 #define CMD_ERASE_CONFIRM 0x00000030
44 #define CMD_PROGRAM 0x000000A0
45 #define CMD_UNLOCK_BYPASS 0x00000020
47 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555 << 1)))
48 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA << 1)))
50 #define BIT_ERASE_DONE 0x00000080
51 #define BIT_RDY_MASK 0x00000080
52 #define BIT_PROGRAM_ERROR 0x00000020
53 #define BIT_TIMEOUT 0x80000000 /* our flag */
55 #define READY 1
56 #define ERR 2
57 #define TMO 4
59 /*-----------------------------------------------------------------------
62 ulong flash_init (void)
64 int i, j;
65 ulong size = 0;
67 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
68 ulong flashbase = 0;
70 flash_info[i].flash_id =
71 #if defined(CONFIG_AMD_LV400)
72 (AMD_MANUFACT & FLASH_VENDMASK) |
73 (AMD_ID_LV400B & FLASH_TYPEMASK);
74 #elif defined(CONFIG_AMD_LV800)
75 (AMD_MANUFACT & FLASH_VENDMASK) |
76 (AMD_ID_LV800B & FLASH_TYPEMASK);
77 #elif defined(CONFIG_SST_VF1601)
78 (SST_MANUFACT & FLASH_VENDMASK) |
79 (SST_ID_xF1601 & FLASH_TYPEMASK);
80 #else
81 #error "Unknown flash configured"
82 #endif
83 flash_info[i].size = FLASH_BANK_SIZE;
84 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
85 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
86 if (i == 0)
87 flashbase = PHYS_FLASH_1;
88 else
89 panic ("configured too many flash banks!\n");
90 for (j = 0; j < flash_info[i].sector_count; j++) {
91 #ifndef CONFIG_SST_VF1601
92 if (j <= 3) {
93 /* 1st one is 16 KB */
94 if (j == 0) {
95 flash_info[i].start[j] =
96 flashbase + 0;
99 /* 2nd and 3rd are both 8 KB */
100 if ((j == 1) || (j == 2)) {
101 flash_info[i].start[j] =
102 flashbase + 0x4000 + (j -
103 1) *
104 0x2000;
107 /* 4th 32 KB */
108 if (j == 3) {
109 flash_info[i].start[j] =
110 flashbase + 0x8000;
112 } else {
113 flash_info[i].start[j] =
114 flashbase + (j - 3) * MAIN_SECT_SIZE;
116 #else
117 flash_info[i].start[j] =
118 flashbase + (j) * MAIN_SECT_SIZE;
119 #endif
121 size += flash_info[i].size;
124 flash_protect (FLAG_PROTECT_SET,
125 CFG_FLASH_BASE,
126 CFG_FLASH_BASE + monitor_flash_len - 1,
127 &flash_info[0]);
129 #if defined(CFG_ENV_ADDR)
130 flash_protect (FLAG_PROTECT_SET,
131 CFG_ENV_ADDR,
132 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
133 #endif
134 return size;
137 /*-----------------------------------------------------------------------
139 void flash_print_info (flash_info_t * info)
141 int i;
143 switch (info->flash_id & FLASH_VENDMASK) {
144 case (AMD_MANUFACT & FLASH_VENDMASK):
145 printf ("AMD: ");
146 break;
147 case (SST_MANUFACT & FLASH_VENDMASK):
148 printf ("SST: ");
149 break;
150 default:
151 printf ("Unknown Vendor ");
152 break;
155 switch (info->flash_id & FLASH_TYPEMASK) {
156 case (AMD_ID_LV400B & FLASH_TYPEMASK):
157 printf ("1x Amd29LV400BB (4Mbit)\n");
158 break;
159 case (AMD_ID_LV800B & FLASH_TYPEMASK):
160 printf ("1x Amd29LV800BB (8Mbit)\n");
161 break;
162 case (SST_ID_xF1601 & FLASH_TYPEMASK):
163 printf ("1x SST39VF1601 (2Mbit)\n");
164 break;
165 default:
166 printf ("Unknown Chip Type\n");
167 goto Done;
168 break;
171 printf (" Size: %ld MB in %d Sectors\n",
172 info->size >> 20, info->sector_count);
174 printf (" Sector Start Addresses:");
175 for (i = 0; i < info->sector_count; i++) {
176 if ((i % 5) == 0) {
177 printf ("\n ");
179 printf (" %08lX%s", info->start[i],
180 info->protect[i] ? " (RO)" : " ");
182 printf ("\n");
184 Done:;
187 /*-----------------------------------------------------------------------
190 int flash_erase (flash_info_t * info, int s_first, int s_last)
192 ushort result;
193 int iflag, cflag, prot, sect;
194 int rc = ERR_OK;
195 int chip;
197 /* first look for protection bits */
199 if (info->flash_id == FLASH_UNKNOWN)
200 return ERR_UNKNOWN_FLASH_TYPE;
202 if ((s_first < 0) || (s_first > s_last)) {
203 return ERR_INVAL;
206 #ifdef CONFIG_SST_VF1601
207 if ((info->flash_id & FLASH_VENDMASK) !=
208 (SST_MANUFACT & FLASH_VENDMASK)) {
209 return ERR_UNKNOWN_FLASH_VENDOR;
211 #else
212 if ((info->flash_id & FLASH_VENDMASK) !=
213 (AMD_MANUFACT & FLASH_VENDMASK)) {
214 return ERR_UNKNOWN_FLASH_VENDOR;
216 #endif
218 prot = 0;
219 for (sect = s_first; sect <= s_last; ++sect) {
220 if (info->protect[sect]) {
221 prot++;
224 if (prot)
225 return ERR_PROTECTED;
228 * Disable interrupts which might cause a timeout
229 * here. Remember that our exception vectors are
230 * at address 0 in the flash, and we don't want a
231 * (ticker) exception to happen while the flash
232 * chip is in programming mode.
234 cflag = icache_status ();
235 icache_disable ();
236 iflag = disable_interrupts ();
238 /* Start erase on unprotected sectors */
239 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
240 printf ("Erasing sector %2d ... ", sect);
242 /* arm simple, non interrupt dependent timer */
243 reset_timer_masked ();
245 if (info->protect[sect] == 0) { /* not protected */
246 vu_short *addr = (vu_short *) (info->start[sect]);
248 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
249 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
250 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
252 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
253 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
254 *addr = CMD_ERASE_CONFIRM;
256 #ifndef CONFIG_SST_VF1601
257 /* wait until flash is ready */
258 chip = 0;
260 do {
261 result = *addr;
263 /* check timeout */
264 if (get_timer_masked () >
265 CFG_FLASH_ERASE_TOUT) {
266 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
267 chip = TMO;
268 break;
271 if (!chip
272 && (result & 0xFFFF) & BIT_ERASE_DONE)
273 chip = READY;
275 if (!chip
276 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
277 chip = ERR;
279 } while (!chip);
281 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
283 if (chip == ERR) {
284 rc = ERR_PROG_ERROR;
285 goto outahere;
287 if (chip == TMO) {
288 rc = ERR_TIMOUT;
289 goto outahere;
291 #else
292 /* wait until flash is ready */
293 while(1){
294 unsigned short i;
295 i = *((volatile unsigned short *)addr)&0x40;
296 if(i!=*((volatile unsigned short *)addr)&0x40)
297 continue;
298 if(*((volatile unsigned short *)addr)&0x80)
299 break;
301 #endif
302 printf ("ok.\n");
303 } else { /* it was protected */
305 printf ("protected!\n");
309 if (ctrlc ())
310 printf ("User Interrupt!\n");
312 outahere:
313 /* allow flash to settle - wait 10 ms */
314 udelay_masked (10000);
316 if (iflag)
317 enable_interrupts ();
319 if (cflag)
320 icache_enable ();
322 return rc;
325 /*-----------------------------------------------------------------------
326 * Copy memory to flash
329 volatile static int write_hword (flash_info_t * info, ulong dest, ushort data)
331 vu_short *addr = (vu_short *) dest;
332 ushort result;
333 int rc = ERR_OK;
334 int cflag, iflag;
335 int chip;
338 * Check if Flash is (sufficiently) erased
340 result = *addr;
341 if ((result & data) != data)
342 return ERR_NOT_ERASED;
346 * Disable interrupts which might cause a timeout
347 * here. Remember that our exception vectors are
348 * at address 0 in the flash, and we don't want a
349 * (ticker) exception to happen while the flash
350 * chip is in programming mode.
352 cflag = icache_status ();
353 icache_disable ();
354 iflag = disable_interrupts ();
356 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
357 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
358 MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
359 *addr = CMD_PROGRAM;
360 *addr = data;
362 /* arm simple, non interrupt dependent timer */
363 reset_timer_masked ();
365 /* wait until flash is ready */
366 chip = 0;
367 do {
368 result = *addr;
370 /* check timeout */
371 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
372 chip = ERR | TMO;
373 break;
375 if (!chip && ((result & 0x80) == (data & 0x80)))
376 chip = READY;
378 if (!chip && ((result & 0xFFFF) & BIT_PROGRAM_ERROR)) {
379 result = *addr;
381 if ((result & 0x80) == (data & 0x80))
382 chip = READY;
383 else
384 chip = ERR;
387 } while (!chip);
389 *addr = CMD_READ_ARRAY;
391 if (chip == ERR || *addr != data)
392 rc = ERR_PROG_ERROR;
394 if (iflag)
395 enable_interrupts ();
397 if (cflag)
398 icache_enable ();
400 return rc;
403 /*-----------------------------------------------------------------------
404 * Copy memory to flash.
407 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
409 ulong cp, wp;
410 int l;
411 int i, rc;
412 ushort data;
414 wp = (addr & ~1); /* get lower word aligned address */
417 * handle unaligned start bytes
419 if ((l = addr - wp) != 0) {
420 data = 0;
421 for (i = 0, cp = wp; i < l; ++i, ++cp) {
422 data = (data >> 8) | (*(uchar *) cp << 8);
424 for (; i < 2 && cnt > 0; ++i) {
425 data = (data >> 8) | (*src++ << 8);
426 --cnt;
427 ++cp;
429 for (; cnt == 0 && i < 2; ++i, ++cp) {
430 data = (data >> 8) | (*(uchar *) cp << 8);
433 if ((rc = write_hword (info, wp, data)) != 0) {
434 return (rc);
436 wp += 2;
440 * handle word aligned part
442 while (cnt >= 2) {
443 data = *((vu_short *) src);
444 if ((rc = write_hword (info, wp, data)) != 0) {
445 return (rc);
447 src += 2;
448 wp += 2;
449 cnt -= 2;
452 if (cnt == 0) {
453 return ERR_OK;
457 * handle unaligned tail bytes
459 data = 0;
460 for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
461 data = (data >> 8) | (*src++ << 8);
462 --cnt;
464 for (; i < 2; ++i, ++cp) {
465 data = (data >> 8) | (*(uchar *) cp << 8);
468 return write_hword (info, wp, data);