3 * Lineo, Inc. <www.lineo.com>
4 * Bernhard Kuhn <bkuhn@lineo.com>
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Alex Zuepke <azu@sysgo.de>
10 * See file CREDITS for list of people who contributed to this
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 /* Flash Organization Structure */
37 unsigned int sector_number;
38 unsigned int sector_size;
42 /* Flash Organizations */
43 OrgDef OrgAT49BV16x4[] =
45 { 8, 8*1024 }, /* 8 * 8kBytes sectors */
46 { 2, 32*1024 }, /* 2 * 32kBytes sectors */
47 { 30, 64*1024 } /* 30 * 64kBytes sectors */
50 OrgDef OrgAT49BV16x4A[] =
52 { 8, 8*1024 }, /* 8 * 8kBytes sectors */
53 { 31, 64*1024 } /* 31 * 64kBytes sectors */
57 #define FLASH_BANK_SIZE 0x200000 /* 2 MB */
58 #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
60 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
62 /* AT49BV1614A Codes */
63 #define FLASH_CODE1 0xAA
64 #define FLASH_CODE2 0x55
65 #define ID_IN_CODE 0x90
66 #define ID_OUT_CODE 0xF0
69 #define CMD_READ_ARRAY 0x00F0
70 #define CMD_UNLOCK1 0x00AA
71 #define CMD_UNLOCK2 0x0055
72 #define CMD_ERASE_SETUP 0x0080
73 #define CMD_ERASE_CONFIRM 0x0030
74 #define CMD_PROGRAM 0x00A0
75 #define CMD_UNLOCK_BYPASS 0x0020
77 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00005555<<1)))
78 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00002AAA<<1)))
80 #define IDENT_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x0000555<<1)))
81 #define IDENT_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x0000AAA<<1)))
83 #define BIT_ERASE_DONE 0x0080
84 #define BIT_RDY_MASK 0x0080
85 #define BIT_PROGRAM_ERROR 0x0020
86 #define BIT_TIMEOUT 0x80000000 /* our flag */
92 /*-----------------------------------------------------------------------
94 void flash_identification (flash_info_t * info)
96 volatile u16 manuf_code, device_code, add_device_code;
98 IDENT_FLASH_ADDR1 = FLASH_CODE1;
99 IDENT_FLASH_ADDR2 = FLASH_CODE2;
100 IDENT_FLASH_ADDR1 = ID_IN_CODE;
102 manuf_code = *(volatile u16 *) CFG_FLASH_BASE;
103 device_code = *(volatile u16 *) (CFG_FLASH_BASE + 2);
104 add_device_code = *(volatile u16 *) (CFG_FLASH_BASE + (3 << 1));
106 IDENT_FLASH_ADDR1 = FLASH_CODE1;
107 IDENT_FLASH_ADDR2 = FLASH_CODE2;
108 IDENT_FLASH_ADDR1 = ID_OUT_CODE;
111 info->flash_id = ATM_MANUFACT & FLASH_VENDMASK;
114 if ((device_code & FLASH_TYPEMASK) == (ATM_ID_BV1614 & FLASH_TYPEMASK)) {
116 if ((add_device_code & FLASH_TYPEMASK) ==
117 (ATM_ID_BV1614A & FLASH_TYPEMASK)) {
118 info->flash_id |= ATM_ID_BV1614A & FLASH_TYPEMASK;
119 printf ("AT49BV1614A (16Mbit)\n");
122 } else { /* AT49BV1614 Flash */
123 info->flash_id |= ATM_ID_BV1614 & FLASH_TYPEMASK;
124 printf ("AT49BV1614 (16Mbit)\n");
129 ulong flash_init (void)
132 unsigned int flash_nb_blocks, sector;
133 unsigned int start_address;
138 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
141 flash_identification (&flash_info[i]);
143 flash_info[i].size = FLASH_BANK_SIZE;
145 if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
146 (ATM_ID_BV1614 & FLASH_TYPEMASK)) {
147 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
148 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
150 pOrgDef = OrgAT49BV16x4;
151 flash_nb_blocks = sizeof (OrgAT49BV16x4) / sizeof (OrgDef);
152 } else { /* AT49BV1614A Flash */
153 flash_info[i].sector_count = CFG_MAX_FLASH_SECT - 1;
154 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT - 1);
156 pOrgDef = OrgAT49BV16x4A;
157 flash_nb_blocks = sizeof (OrgAT49BV16x4A) / sizeof (OrgDef);
161 flashbase = PHYS_FLASH_1;
163 panic ("configured to many flash banks!\n");
166 start_address = flashbase;
168 for (j = 0; j < flash_nb_blocks; j++) {
169 for (k = 0; k < pOrgDef[j].sector_number; k++) {
170 flash_info[i].start[sector++] = start_address;
171 start_address += pOrgDef[j].sector_size;
175 size += flash_info[i].size;
178 /* Protect binary boot image */
179 flash_protect (FLAG_PROTECT_SET,
181 CFG_FLASH_BASE + CFG_BOOT_SIZE - 1, &flash_info[0]);
183 /* Protect environment variables */
184 flash_protect (FLAG_PROTECT_SET,
186 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
188 /* Protect U-Boot gzipped image */
189 flash_protect (FLAG_PROTECT_SET,
191 CFG_U_BOOT_BASE + CFG_U_BOOT_SIZE - 1, &flash_info[0]);
196 /*-----------------------------------------------------------------------
198 void flash_print_info (flash_info_t * info)
202 switch (info->flash_id & FLASH_VENDMASK) {
203 case (ATM_MANUFACT & FLASH_VENDMASK):
207 printf ("Unknown Vendor ");
211 switch (info->flash_id & FLASH_TYPEMASK) {
212 case (ATM_ID_BV1614 & FLASH_TYPEMASK):
213 printf ("AT49BV1614 (16Mbit)\n");
215 case (ATM_ID_BV1614A & FLASH_TYPEMASK):
216 printf ("AT49BV1614A (16Mbit)\n");
219 printf ("Unknown Chip Type\n");
224 printf (" Size: %ld MB in %d Sectors\n",
225 info->size >> 20, info->sector_count);
227 printf (" Sector Start Addresses:");
228 for (i = 0; i < info->sector_count; i++) {
232 printf (" %08lX%s", info->start[i],
233 info->protect[i] ? " (RO)" : " ");
240 /*-----------------------------------------------------------------------
243 int flash_erase (flash_info_t * info, int s_first, int s_last)
246 int iflag, cflag, prot, sect;
250 /* first look for protection bits */
252 if (info->flash_id == FLASH_UNKNOWN)
253 return ERR_UNKNOWN_FLASH_TYPE;
255 if ((s_first < 0) || (s_first > s_last)) {
259 if ((info->flash_id & FLASH_VENDMASK) !=
260 (ATM_MANUFACT & FLASH_VENDMASK)) {
261 return ERR_UNKNOWN_FLASH_VENDOR;
265 for (sect = s_first; sect <= s_last; ++sect) {
266 if (info->protect[sect]) {
271 return ERR_PROTECTED;
274 * Disable interrupts which might cause a timeout
275 * here. Remember that our exception vectors are
276 * at address 0 in the flash, and we don't want a
277 * (ticker) exception to happen while the flash
278 * chip is in programming mode.
280 cflag = icache_status ();
282 iflag = disable_interrupts ();
284 /* Start erase on unprotected sectors */
285 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
286 printf ("Erasing sector %2d ... ", sect);
288 /* arm simple, non interrupt dependent timer */
289 reset_timer_masked ();
291 if (info->protect[sect] == 0) { /* not protected */
292 volatile u16 *addr = (volatile u16 *) (info->start[sect]);
294 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
295 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
296 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
298 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
299 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
300 *addr = CMD_ERASE_CONFIRM;
302 /* wait until flash is ready */
309 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
310 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
315 if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
320 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
332 } else { /* it was protected */
333 printf ("protected!\n");
338 printf ("User Interrupt!\n");
341 /* allow flash to settle - wait 10 ms */
342 udelay_masked (10000);
345 enable_interrupts ();
353 /*-----------------------------------------------------------------------
354 * Copy memory to flash
357 volatile static int write_word (flash_info_t * info, ulong dest,
360 volatile u16 *addr = (volatile u16 *) dest;
367 * Check if Flash is (sufficiently) erased
370 if ((result & data) != data)
371 return ERR_NOT_ERASED;
375 * Disable interrupts which might cause a timeout
376 * here. Remember that our exception vectors are
377 * at address 0 in the flash, and we don't want a
378 * (ticker) exception to happen while the flash
379 * chip is in programming mode.
381 cflag = icache_status ();
383 iflag = disable_interrupts ();
385 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
386 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
387 MEM_FLASH_ADDR1 = CMD_PROGRAM;
390 /* arm simple, non interrupt dependent timer */
391 reset_timer_masked ();
393 /* wait until flash is ready */
399 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
403 if (!chip1 && ((result & 0x80) == (data & 0x80)))
408 *addr = CMD_READ_ARRAY;
410 if (chip1 == ERR || *addr != data)
414 enable_interrupts ();
422 /*-----------------------------------------------------------------------
423 * Copy memory to flash.
426 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
432 printf ("unaligned destination not supported\n");
437 printf ("unaligned source not supported\n");
444 data = *((volatile u16 *) src);
445 if ((rc = write_word (info, wp, data)) != 0) {
454 data = (*((volatile u8 *) src)) | (*((volatile u8 *) (wp + 1)) <<
456 if ((rc = write_word (info, wp, data)) != 0) {