2 * flash.c - low level flash handling
4 * author: hackbard@hackdaworld.org
13 static unsigned long sector_address[19]={
14 0x00000,0x02000,0x03000,0x04000,0x08000,
28 void flash_init(void) {
31 * idle clocks between rad & write: 0+1
32 * length of read access: 1+3
33 * bls lines high during write access
34 * length of write access: 0+1
35 * no write protect, no burst-rom
39 BCFG0=0x10000420; // flash 1
40 BCFG2=0x10000420; // flash 2
44 * p3.25: chip select 2
45 * p2.15 - p2.8: data bus
46 * a[1:15] -> address lines
49 PINSEL2=(PINSEL2&P2MASK)|(1<<8);
50 PINSEL2=(PINSEL2&P2MASK&~((1<<15)|(1<<14)))|(1<<14);
51 PINSEL2=(PINSEL2&P2MASK&~((1<<5)|(1<<4)))|(1<<4);
52 PINSEL2=(PINSEL2&P2MASK)|(1<<24);
53 PINSEL2=(PINSEL2&P2MASK&~((1<<27)|(1<<26)|(1<<25)))|(1<<27)|(1<<26);
56 void flash_reset(u8 bank) {
58 if((bank!='0')&(bank!='2'))
67 void flash_sector_erase(u8 bank,u8 sector) {
73 a18_12=sector_address[sector]<<1;
82 *((volatile u16 *)(FLASH_BANK0|a18_12))=0x30;
90 *((volatile u16 *)(FLASH_BANK2|a18_12))=0x30;
99 void flash_chip_erase(u8 bank) {
103 if((bank!='0')&(bank!='2'))
133 void flash_unlock_bypass(u8 bank) {
135 if((bank!='0')&(bank!='2'))
150 void flash_unlock_bypass_reset(u8 bank) {
152 if((bank!='0')&(bank!='2'))
165 int flash_write_word(u32 addr,u16 data) {
172 *((unsigned volatile short *)addr)=0xa0;
173 *((unsigned volatile short *)addr)=data;
175 check=*((unsigned short *)addr);
176 if((data&0x80)==(check&0x80))
185 #define flash_read_word(addr,data) *(data)=*((unsigned volatile short *)(addr))
187 int flash_write_buf(u32 addr,u16 *buf,int len) {
192 /* len must be a multiple of 2 */
196 /* decide the bank */
203 flash_unlock_bypass(bank);
207 for(cnt=0;cnt<len/2;cnt++) {
208 if(flash_write_word(addr,*(buf+cnt))==0)
214 flash_unlock_bypass_reset(bank);
219 void flash_read_buf(u32 addr,u16 *buf,int len) {
223 /* len must be a multiple of 2 */
227 for(cnt=0;cnt<len/2;cnt++)
228 flash_read_word(addr,buf+cnt);