3 * BuS Elektronik GmbH & Co.KG <esw@bus-elektonik.de>
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include "cfm_flash.h"
30 #define PHYS_FLASH_1 CONFIG_SYS_FLASH_BASE
31 #define FLASH_BANK_SIZE 0x200000
33 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
];
35 void flash_print_info (flash_info_t
* info
)
39 switch (info
->flash_id
& FLASH_VENDMASK
) {
40 case (AMD_MANUFACT
& FLASH_VENDMASK
):
42 switch (info
->flash_id
& FLASH_TYPEMASK
) {
43 case (AMD_ID_LV160B
& FLASH_TYPEMASK
):
44 printf ("AM29LV160B (16Bit)\n");
47 printf ("Unknown Chip Type\n");
51 case FREESCALE_MANUFACT
& FLASH_VENDMASK
:
52 cfm_flash_print_info (info
);
55 printf ("Unknown Vendor ");
60 if ((info
->size
>> 20) > 0)
62 printf ("%ld MiB",info
->size
>> 20);
66 printf ("%ld KiB",info
->size
>> 10);
68 printf (" in %d Sectors\n", info
->sector_count
);
70 printf (" Sector Start Addresses:");
71 for (i
= 0; i
< info
->sector_count
; i
++) {
75 printf ("%02d: %08lX%s ", i
,info
->start
[i
],
76 info
->protect
[i
] ? " P" : " ");
81 unsigned long flash_init (void)
86 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
92 flash_info
[i
].flash_id
=
93 (AMD_MANUFACT
& FLASH_VENDMASK
) |
94 (AMD_ID_LV160B
& FLASH_TYPEMASK
);
95 flash_info
[i
].size
= FLASH_BANK_SIZE
;
96 flash_info
[i
].sector_count
= CONFIG_SYS_MAX_FLASH_SECT
;
97 memset (flash_info
[i
].protect
, 0, CONFIG_SYS_MAX_FLASH_SECT
);
98 flashbase
= PHYS_FLASH_1
;
99 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++) {
102 flash_info
[i
].start
[j
] = flashbase
;
104 if ((j
>= 1) && (j
<= 2)) {
105 /* 2nd and 3rd are 8 KiB */
106 flash_info
[i
].start
[j
] =
107 flashbase
+ 0x4000 + 0x2000 * (j
- 1);
111 flash_info
[i
].start
[j
] = flashbase
+ 0x8000;
113 if ((j
>= 4) && (j
<= 34)) {
114 /* rest is 256 KiB */
115 flash_info
[i
].start
[j
] =
116 flashbase
+ 0x10000 + 0x10000 * (j
- 4);
121 cfm_flash_init (&flash_info
[i
]);
124 panic ("configured to many flash banks!\n");
127 size
+= flash_info
[i
].size
;
130 flash_protect (FLAG_PROTECT_SET
,
131 CONFIG_SYS_FLASH_BASE
,
132 CONFIG_SYS_FLASH_BASE
+ 0xffff, &flash_info
[0]);
137 #define CMD_READ_ARRAY 0x00F0
138 #define CMD_UNLOCK1 0x00AA
139 #define CMD_UNLOCK2 0x0055
140 #define CMD_ERASE_SETUP 0x0080
141 #define CMD_ERASE_CONFIRM 0x0030
142 #define CMD_PROGRAM 0x00A0
143 #define CMD_UNLOCK_BYPASS 0x0020
145 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(info->start[0] + (0x00000555<<1)))
146 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(info->start[0] + (0x000002AA<<1)))
149 #define BIT_ERASE_DONE 0x0080
150 #define BIT_RDY_MASK 0x0080
151 #define BIT_PROGRAM_ERROR 0x0020
152 #define BIT_TIMEOUT 0x80000000 /* our flag */
156 int amd_flash_erase_sector(flash_info_t
* info
, int sector
)
162 (volatile u16
*) (info
->start
[sector
]);
164 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
165 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
166 MEM_FLASH_ADDR1
= CMD_ERASE_SETUP
;
168 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
169 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
170 *addr
= CMD_ERASE_CONFIRM
;
172 /* wait until flash is ready */
180 if (get_timer (0) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
181 MEM_FLASH_ADDR1
= CMD_READ_ARRAY
;
185 if (!state
&& (result
& 0xFFFF) & BIT_ERASE_DONE
)
189 if (state
== ERR_READY
)
192 MEM_FLASH_ADDR1
= CMD_READ_ARRAY
;
197 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
205 if (info
->flash_id
== FLASH_UNKNOWN
)
207 rc
= ERR_UNKNOWN_FLASH_TYPE
;
208 } /* (info->flash_id == FLASH_UNKNOWN) */
210 if ((s_first
< 0) || (s_first
> s_last
) || s_last
>= info
->sector_count
)
215 cflag
= icache_status ();
217 iflag
= disable_interrupts ();
219 for (sector
= s_first
; (sector
<= s_last
) && (rc
== ERR_OK
); sector
++) {
221 if (info
->protect
[sector
])
223 putc('P'); /* protected sector will not erase */
227 /* erase on unprotected sector */
229 switch (info
->flash_id
& FLASH_VENDMASK
)
231 case (AMD_MANUFACT
& FLASH_VENDMASK
):
232 rc
= amd_flash_erase_sector(info
,sector
);
234 case (FREESCALE_MANUFACT
& FLASH_VENDMASK
):
235 rc
= cfm_flash_erase_sector(info
,sector
);
238 return ERR_UNKNOWN_FLASH_VENDOR
;
253 udelay (10000); /* allow flash to settle - wait 10 ms */
256 enable_interrupts ();
264 volatile static int amd_write_word (flash_info_t
* info
, ulong dest
, u16 data
)
272 * Check if Flash is (sufficiently) erased
274 addr
= (volatile u16
*) dest
;
277 if ((result
& data
) != data
)
278 return ERR_NOT_ERASED
;
281 * Disable interrupts which might cause a timeout
282 * here. Remember that our exception vectors are
283 * at address 0 in the flash, and we don't want a
284 * (ticker) exception to happen while the flash
285 * chip is in programming mode.
288 cflag
= icache_status ();
290 iflag
= disable_interrupts ();
292 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
293 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
294 MEM_FLASH_ADDR1
= CMD_PROGRAM
;
297 /* arm simple, non interrupt dependent timer */
300 /* wait until flash is ready */
306 if (get_timer (0) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
309 if (!state
&& ((result
& BIT_RDY_MASK
) == (data
& BIT_RDY_MASK
)))
314 *addr
= CMD_READ_ARRAY
;
316 if (state
== ERR_READY
)
318 if ((*addr
!= data
) && (state
!= ERR_TIMOUT
))
319 state
= ERR_PROG_ERROR
;
322 enable_interrupts ();
330 int amd_flash_write_buff(flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
339 debug ("Byte alignment not supported\n");
344 debug ("Byte transfer not supported\n");
349 while ((cnt
>=2) && (rc
== ERR_OK
))
351 data
= *((volatile u16
*) src
);
352 rc
=amd_write_word (info
,dest
,data
);
360 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
364 switch (info
->flash_id
& FLASH_VENDMASK
)
366 case (AMD_MANUFACT
& FLASH_VENDMASK
):
367 rc
= amd_flash_write_buff(info
,src
,addr
,cnt
);
369 case (FREESCALE_MANUFACT
& FLASH_VENDMASK
):
370 rc
= cfm_flash_write_buff(info
,src
,addr
,cnt
);
373 rc
= ERR_UNKNOWN_FLASH_VENDOR
;
378 int amd_flash_protect(flash_info_t
* info
,long sector
,int prot
)
384 info
->protect
[sector
]=1;
388 info
->protect
[sector
]=0;
393 #ifdef CONFIG_SYS_FLASH_PROTECTION
395 int flash_real_protect(flash_info_t
* info
,long sector
,int prot
)
399 switch (info
->flash_id
& FLASH_VENDMASK
)
401 case (AMD_MANUFACT
& FLASH_VENDMASK
):
402 rc
= amd_flash_protect(info
,sector
,prot
);
404 case (FREESCALE_MANUFACT
& FLASH_VENDMASK
):
405 rc
= cfm_flash_protect(info
,sector
,prot
);
408 rc
= ERR_UNKNOWN_FLASH_VENDOR
;