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 * 8 kBytes sectors */
46 { 2, 32*1024 }, /* 2 * 32 kBytes sectors */
47 { 30, 64*1024 }, /* 30 * 64 kBytes sectors */
50 OrgDef OrgAT49BV16x4A
[] =
52 { 8, 8*1024 }, /* 8 * 8 kBytes sectors */
53 { 31, 64*1024 }, /* 31 * 64 kBytes sectors */
56 OrgDef OrgAT49BV6416
[] =
58 { 8, 8*1024 }, /* 8 * 8 kBytes sectors */
59 { 127, 64*1024 }, /* 127 * 64 kBytes sectors */
62 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
];
64 /* AT49BV1614A Codes */
65 #define FLASH_CODE1 0xAA
66 #define FLASH_CODE2 0x55
67 #define ID_IN_CODE 0x90
68 #define ID_OUT_CODE 0xF0
71 #define CMD_READ_ARRAY 0x00F0
72 #define CMD_UNLOCK1 0x00AA
73 #define CMD_UNLOCK2 0x0055
74 #define CMD_ERASE_SETUP 0x0080
75 #define CMD_ERASE_CONFIRM 0x0030
76 #define CMD_PROGRAM 0x00A0
77 #define CMD_UNLOCK_BYPASS 0x0020
78 #define CMD_SECTOR_UNLOCK 0x0070
80 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00005555<<1)))
81 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00002AAA<<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 MEM_FLASH_ADDR1
= FLASH_CODE1
;
99 MEM_FLASH_ADDR2
= FLASH_CODE2
;
100 MEM_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 MEM_FLASH_ADDR1
= FLASH_CODE1
;
107 MEM_FLASH_ADDR2
= FLASH_CODE2
;
108 MEM_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");
120 } else { /* AT49BV1614 Flash */
121 info
->flash_id
|= ATM_ID_BV1614
& FLASH_TYPEMASK
;
122 printf ("AT49BV1614 (16Mbit)\n");
125 } else if ((device_code
& FLASH_TYPEMASK
) == (ATM_ID_BV6416
& FLASH_TYPEMASK
)) {
126 info
->flash_id
|= ATM_ID_BV6416
& FLASH_TYPEMASK
;
127 printf ("AT49BV6416 (64Mbit)\n");
131 ushort
flash_number_sector(OrgDef
*pOrgDef
, unsigned int nb_blocks
)
133 int i
, nb_sectors
= 0;
135 for (i
=0; i
<nb_blocks
; i
++){
136 nb_sectors
+= pOrgDef
[i
].sector_number
;
142 void flash_unlock_sector(flash_info_t
* info
, unsigned int sector
)
144 volatile u16
*addr
= (volatile u16
*) (info
->start
[sector
]);
146 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
147 *addr
= CMD_SECTOR_UNLOCK
;
151 ulong
flash_init (void)
154 unsigned int flash_nb_blocks
, sector
;
155 unsigned int start_address
;
160 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; i
++) {
163 flash_identification (&flash_info
[i
]);
165 if ((flash_info
[i
].flash_id
& FLASH_TYPEMASK
) ==
166 (ATM_ID_BV1614
& FLASH_TYPEMASK
)) {
168 pOrgDef
= OrgAT49BV16x4
;
169 flash_nb_blocks
= sizeof (OrgAT49BV16x4
) / sizeof (OrgDef
);
170 } else if ((flash_info
[i
].flash_id
& FLASH_TYPEMASK
) ==
171 (ATM_ID_BV1614A
& FLASH_TYPEMASK
)){ /* AT49BV1614A Flash */
173 pOrgDef
= OrgAT49BV16x4A
;
174 flash_nb_blocks
= sizeof (OrgAT49BV16x4A
) / sizeof (OrgDef
);
175 } else if ((flash_info
[i
].flash_id
& FLASH_TYPEMASK
) ==
176 (ATM_ID_BV6416
& FLASH_TYPEMASK
)){ /* AT49BV6416 Flash */
178 pOrgDef
= OrgAT49BV6416
;
179 flash_nb_blocks
= sizeof (OrgAT49BV6416
) / sizeof (OrgDef
);
182 pOrgDef
= OrgAT49BV16x4
;
185 flash_info
[i
].sector_count
= flash_number_sector(pOrgDef
, flash_nb_blocks
);
186 memset (flash_info
[i
].protect
, 0, flash_info
[i
].sector_count
);
189 flashbase
= PHYS_FLASH_1
;
191 panic ("configured too many flash banks!\n");
194 start_address
= flashbase
;
195 flash_info
[i
].size
= 0;
197 for (j
= 0; j
< flash_nb_blocks
; j
++) {
198 for (k
= 0; k
< pOrgDef
[j
].sector_number
; k
++) {
199 flash_info
[i
].start
[sector
++] = start_address
;
200 start_address
+= pOrgDef
[j
].sector_size
;
201 flash_info
[i
].size
+= pOrgDef
[j
].sector_size
;
205 size
+= flash_info
[i
].size
;
207 if ((flash_info
[i
].flash_id
& FLASH_TYPEMASK
) ==
208 (ATM_ID_BV6416
& FLASH_TYPEMASK
)){ /* AT49BV6416 Flash */
210 /* Unlock all sectors at reset */
211 for (j
=0; j
<flash_info
[i
].sector_count
; j
++){
212 flash_unlock_sector(&flash_info
[i
], j
);
217 /* Protect binary boot image */
218 flash_protect (FLAG_PROTECT_SET
,
220 CFG_FLASH_BASE
+ CFG_BOOT_SIZE
- 1, &flash_info
[0]);
222 /* Protect environment variables */
223 flash_protect (FLAG_PROTECT_SET
,
225 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1, &flash_info
[0]);
227 /* Protect U-Boot gzipped image */
228 flash_protect (FLAG_PROTECT_SET
,
230 CFG_U_BOOT_BASE
+ CFG_U_BOOT_SIZE
- 1, &flash_info
[0]);
235 /*-----------------------------------------------------------------------
237 void flash_print_info (flash_info_t
* info
)
241 switch (info
->flash_id
& FLASH_VENDMASK
) {
242 case (ATM_MANUFACT
& FLASH_VENDMASK
):
246 printf ("Unknown Vendor ");
250 switch (info
->flash_id
& FLASH_TYPEMASK
) {
251 case (ATM_ID_BV1614
& FLASH_TYPEMASK
):
252 printf ("AT49BV1614 (16Mbit)\n");
254 case (ATM_ID_BV1614A
& FLASH_TYPEMASK
):
255 printf ("AT49BV1614A (16Mbit)\n");
257 case (ATM_ID_BV6416
& FLASH_TYPEMASK
):
258 printf ("AT49BV6416 (64Mbit)\n");
261 printf ("Unknown Chip Type\n");
265 printf (" Size: %ld MB in %d Sectors\n",
266 info
->size
>> 20, info
->sector_count
);
268 printf (" Sector Start Addresses:");
269 for (i
= 0; i
< info
->sector_count
; i
++) {
273 printf (" %08lX%s", info
->start
[i
],
274 info
->protect
[i
] ? " (RO)" : " ");
279 /*-----------------------------------------------------------------------
282 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
285 int iflag
, cflag
, prot
, sect
;
289 /* first look for protection bits */
291 if (info
->flash_id
== FLASH_UNKNOWN
)
292 return ERR_UNKNOWN_FLASH_TYPE
;
294 if ((s_first
< 0) || (s_first
> s_last
)) {
298 if ((info
->flash_id
& FLASH_VENDMASK
) !=
299 (ATM_MANUFACT
& FLASH_VENDMASK
)) {
300 return ERR_UNKNOWN_FLASH_VENDOR
;
304 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
305 if (info
->protect
[sect
]) {
310 return ERR_PROTECTED
;
313 * Disable interrupts which might cause a timeout
314 * here. Remember that our exception vectors are
315 * at address 0 in the flash, and we don't want a
316 * (ticker) exception to happen while the flash
317 * chip is in programming mode.
319 cflag
= icache_status ();
321 iflag
= disable_interrupts ();
323 /* Start erase on unprotected sectors */
324 for (sect
= s_first
; sect
<= s_last
&& !ctrlc (); sect
++) {
325 printf ("Erasing sector %2d ... ", sect
);
327 /* arm simple, non interrupt dependent timer */
328 reset_timer_masked ();
330 if (info
->protect
[sect
] == 0) { /* not protected */
331 volatile u16
*addr
= (volatile u16
*) (info
->start
[sect
]);
333 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
334 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
335 MEM_FLASH_ADDR1
= CMD_ERASE_SETUP
;
337 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
338 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
339 *addr
= CMD_ERASE_CONFIRM
;
341 /* wait until flash is ready */
348 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT
) {
349 MEM_FLASH_ADDR1
= CMD_READ_ARRAY
;
354 if (!chip1
&& (result
& 0xFFFF) & BIT_ERASE_DONE
)
359 MEM_FLASH_ADDR1
= CMD_READ_ARRAY
;
371 } else { /* it was protected */
372 printf ("protected!\n");
377 printf ("User Interrupt!\n");
380 /* allow flash to settle - wait 10 ms */
381 udelay_masked (10000);
384 enable_interrupts ();
392 /*-----------------------------------------------------------------------
393 * Copy memory to flash
396 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
)
398 volatile u16
*addr
= (volatile u16
*) dest
;
405 * Check if Flash is (sufficiently) erased
408 if ((result
& data
) != data
)
409 return ERR_NOT_ERASED
;
412 * Disable interrupts which might cause a timeout
413 * here. Remember that our exception vectors are
414 * at address 0 in the flash, and we don't want a
415 * (ticker) exception to happen while the flash
416 * chip is in programming mode.
418 cflag
= icache_status ();
420 iflag
= disable_interrupts ();
422 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
423 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
424 MEM_FLASH_ADDR1
= CMD_PROGRAM
;
427 /* arm simple, non interrupt dependent timer */
428 reset_timer_masked ();
430 /* wait until flash is ready */
436 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT
) {
440 if (!chip1
&& ((result
& 0x80) == (data
& 0x80)))
445 *addr
= CMD_READ_ARRAY
;
447 if (chip1
== ERR
|| *addr
!= data
)
451 enable_interrupts ();
459 /*-----------------------------------------------------------------------
460 * Copy memory to flash.
463 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
469 printf ("unaligned destination not supported\n");
474 printf ("unaligned source not supported\n");
481 data
= *((volatile u16
*) src
);
482 if ((rc
= write_word (info
, wp
, data
)) != 0) {
491 data
= (*((volatile u8
*) src
)) | (*((volatile u8
*) (wp
+ 1)) <<
493 if ((rc
= write_word (info
, wp
, data
)) != 0) {