X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?p=my-code%2Farm.git;a=blobdiff_plain;f=betty%2Ffwflash.c;h=26a68a8198065f095ed306618faf003de87814e9;hp=b6d48a67848b59bcbe22be0d13867b4baa82ce46;hb=HEAD;hpb=5599fd4317d682de1c1268422bff0f474f22d65e diff --git a/betty/fwflash.c b/betty/fwflash.c index b6d48a6..26a68a8 100644 --- a/betty/fwflash.c +++ b/betty/fwflash.c @@ -32,12 +32,9 @@ #define B2F2AA (*((volatile unsigned short *)(BANK2|0x554))) // 0x2aa #define B2F (*((volatile unsigned short *)(BANK2))) -#define FLASH0_555 (*((volatile unsigned short *) 0x80000AAA)) -#define FLASH0_2AA (*((volatile unsigned short *) 0x80000554)) -#define FLASH0 (*((volatile unsigned short *) 0x80000000)) - /* commands */ #define CMD_READ 'R' +#define CMD_WRITE 'W' #define CMD_CHIP_ERASE 'E' #define CMD_SECTOR_ERASE 'S' #define CMD_CHIP_ID 'I' @@ -71,6 +68,8 @@ unsigned long sector_address[19]={ * functions */ +void uart0_send_byte(u8 send); + void mmap_init(u8 memtype) { MEMMAP=memtype; @@ -89,63 +88,20 @@ void pll_init(void) { continue; } -int flash_sector_erase(u8 flash,u8 sector) { - - u32 a18_12; - u32 base; - - if(sector>18) - return -1; - a18_12=sector_address[sector]<<1; - - if((flash!='0')|(flash!='2')) - return -1; - - switch(flash) { - case '0': - base=0x80000000; - B0F555=0xaa; - B0F2AA=0x55; - B0F555=0x80; - B0F555=0xaa; - B0F2AA=0x55; - *((volatile u16 *)(base|a18_12))=0x30; - break; - case '2': - base=0x82000000; - B2F555=0xaa; - B2F2AA=0x55; - B2F555=0x80; - B2F555=0xaa; - B2F2AA=0x55; - *((volatile u16 *)(base|a18_12))=0x30; - break; - default: - return -1; - } +void ext_mem_bank_init(void) { - return 0; -} - -void flash_sector0_erase(void) { - B0F555=0xaa; - B0F2AA=0x55; - B0F555=0x80; - B0F555=0xaa; - B0F2AA=0x55; - *((volatile u16 *)(0x80000000))=0x30; + BCFG0=0x10000420; // flash 1 + BCFG1=0x00000c42; // lcd + BCFG2=0x10000420; // flash 2 } -void flash_chip_erase(void) { +void pin_select_init() { - /* test, erase flash at bank0 */ + /* + * a[19:2] -> address lines + */ - B0F555=0xaa; - B0F2AA=0x55; - B0F555=0x80; - B0F555=0xaa; - B0F2AA=0x55; - B0F555=0x10; + PINSEL2=0x0d6041d4; } void uart0_init(void) { @@ -237,6 +193,190 @@ u8 uart0_get_byte(void) { return rx; } +void flash_reset(u8 bank) { + + if((bank!='0')&(bank!='2')) + return; + + if(bank=='0') + B0F=0xf0; + else + B2F=0xf0; +} + +void flash_sector_erase(u8 flash,u8 sector) { + + u32 a18_12; + u32 base; + + if(sector>18) + return; + a18_12=sector_address[sector]<<1; + + if((flash!='0')&(flash!='2')) + return; + + switch(flash) { + case '0': + base=0x80000000; + B0F555=0xaa; + B0F2AA=0x55; + B0F555=0x80; + B0F555=0xaa; + B0F2AA=0x55; + *((volatile u16 *)(base|a18_12))=0x30; + break; + case '2': + base=0x82000000; + B2F555=0xaa; + B2F2AA=0x55; + B2F555=0x80; + B2F555=0xaa; + B2F2AA=0x55; + *((volatile u16 *)(base|a18_12))=0x30; + break; + default: + return; + } + + return; +} + +void flash_chip_erase(u8 bank) { + + u8 status; + + if((bank!='0')&(bank!='2')) + return; + + if(bank=='0') { + B0F555=0xaa; + B0F2AA=0x55; + B0F555=0x80; + B0F555=0xaa; + B0F2AA=0x55; + B0F555=0x10; + } + else { + B2F555=0xaa; + B2F2AA=0x55; + B2F555=0x80; + B2F555=0xaa; + B2F2AA=0x55; + B2F555=0x10; + } + + while(1) { + if(bank=='0') + status=B0F; + else + status=B2F; + + if(status&0x80) { + /* send an ack */ + uart0_send_byte(status); + break; + } + } +} + +void unlock_bypass(u8 bank) { + + if((bank!='0')&(bank!='2')) + return; + + if(bank=='0') { + B0F555=0xaa; + B0F2AA=0x55; + B0F555=0x20; + } + else { + B2F555=0xaa; + B2F2AA=0x55; + B2F555=0x20; + } +} + +void unlock_bypass_reset(u8 bank) { + + if((bank!='0')&(bank!='2')) + return; + + if(bank=='0') { + B0F=0x90; + B0F=0x00; + } + else { + B2F=0x90; + B2F=0x00; + } +} + +int flash_write(u32 addr,u16 data) { + + u16 check; + + if(data==0xffff) + return 0; + + *((unsigned volatile short *)addr)=0xa0; + *((unsigned volatile short *)addr)=data; + while(1) { + check=*((unsigned short *)addr); + if((data&0x80)==(check&0x80)) + break; + } + if(data!=check) + return -1; + + return 0; +} + +int receive_data_and_write_to_flash(u32 addr,u32 datalen) { + + u8 bank; + u32 i,ret; + u16 data; + u8 cksm; + u8 byte; + + /* which bank to program */ + if(addr<0x82000000) + bank='0'; + else + bank='2'; + + /* unlock bypass */ + unlock_bypass(bank); + + /* receive and write data */ + cksm=0; + ret=0; + for(i=0;i=BANK0)&(addr+datalen<=BANK0+BANK_SIZE)) uart0_send_buf16((u16 *)addr,datalen); if((addr>=BANK2)&(addr+datalen<=BANK2+BANK_SIZE)) uart0_send_buf16((u16 *)addr,datalen); - if((addr>=BOOTLOADER)&(addr+datalen<=BOOTLOADER+BL_SIZE)) + if((addr>=BOOTLOADER)& + (addr+datalen=BANK0)&(addr+datalen<=BANK0+BANK_SIZE))| + ((addr>=BANK2)&(addr+datalen<=BANK2+BANK_SIZE))) + receive_data_and_write_to_flash(addr,datalen); + break; case CMD_CHIP_ERASE: - if(buf[0]=='0') { - flash_chip_erase(); - } - else if(buf[0]=='2') { - B2F555=0xaa; - B2F2AA=0x55; - B2F555=0x80; - B2F555=0xaa; - B2F2AA=0x55; - B2F555=0x10; - } - uart0_send_byte(buf[0]); + flash_chip_erase(buf[0]); break; case CMD_CHIP_ID: if(buf[0]=='0') { @@ -354,16 +488,19 @@ addr=0x80000000; B0F2AA=0x55; B0F555=0x90; data=*((u16 *)BANK0); + uart0_send_buf16(&data,2); data=*((u16 *)(BANK0|0x200)); + uart0_send_buf16(&data,2); } else if(buf[0]=='2') { B2F555=0xaa; B2F2AA=0x55; B2F555=0x90; data=*((u16 *)BANK2); + uart0_send_buf16(&data,2); data=*((u16 *)(BANK2|0x200)); + uart0_send_buf16(&data,2); } - uart0_send_buf16(&data,2); break; default: break;