2 * flash.c - low level flash handling
4 * author: hackbard@hackdaworld.org
14 unsigned long sector_address[20]={
15 0x00000,0x02000,0x03000,0x04000,0x08000,
23 0x80000 // not a sector, end of flash!
30 void flash_init(void) {
36 void flash_reset(u8 bank) {
38 if((bank!='0')&(bank!='2'))
47 void flash_sector_erase(u8 bank,u8 sector) {
53 a18_12=sector_address[sector]<<1;
62 *((volatile u16 *)(FLASH_BANK0|a18_12))=0x30;
70 *((volatile u16 *)(FLASH_BANK2|a18_12))=0x30;
79 int flash_sec_erase(u32 addr) {
84 a18_12=addr&0x00000fffff;
87 *((volatile u16 *)(base|(0x555<<1)))=0xaa;
88 *((volatile u16 *)(base|(0x2aa<<1)))=0x55;
89 *((volatile u16 *)(base|(0x555<<1)))=0x80;
90 *((volatile u16 *)(base|(0x555<<1)))=0xaa;
91 *((volatile u16 *)(base|(0x2aa<<1)))=0x55;
92 *((volatile u16 *)(base|(a18_12<<1)))=0x30;
97 void flash_chip_erase(u8 bank) {
101 if((bank!='0')&(bank!='2'))
131 void flash_unlock_bypass(u8 bank) {
133 if((bank!='0')&(bank!='2'))
148 void flash_unlock_bypass_reset(u8 bank) {
150 if((bank!='0')&(bank!='2'))
163 int flash_write_word(u32 addr,u16 data) {
170 *((unsigned volatile short *)addr)=0xa0;
171 *((unsigned volatile short *)addr)=data;
173 check=*((unsigned short *)addr);
174 if((data&0x80)==(check&0x80))
183 #define flash_read_word(addr,data) *(data)=*((unsigned volatile short *)(addr))
185 int flash_write_buf(u32 addr,u16 *buf,int len) {
190 /* len must be a multiple of 2 */
194 /* decide the bank */
201 flash_unlock_bypass(bank);
205 for(cnt=0;cnt<len/2;cnt++) {
206 if(flash_write_word(addr,*(buf+cnt))==0)
212 flash_unlock_bypass_reset(bank);
217 void flash_read_buf(u32 addr,u16 *buf,int len) {
221 /* len must be a multiple of 2 */
225 for(cnt=0;cnt<len/2;cnt++)
226 flash_read_word(addr,buf+cnt);