2 * fwflash.c - handling the betty flashes
4 * author: hackbard@hackdaworld.org
20 /* bank 0/2 and boootloader addr/size */
21 #define BANK0 0x80000000
22 #define BANK2 0x82000000
23 #define BANK_SIZE 0x00100000
24 #define BOOTLOADER 0x7fffe000
25 #define BL_SIZE 0x00002000
27 /* flash cmd addresses - flash[0-18] <--> arm[1-19]*/
28 #define B0F555 (*((volatile unsigned short *)(BANK0|0xaaa))) // 0x555
29 #define B0F2AA (*((volatile unsigned short *)(BANK0|0x554))) // 0x2aa
30 #define B0F (*((volatile unsigned short *)(BANK0)))
31 #define B2F555 (*((volatile unsigned short *)(BANK2|0xaaa))) // 0x555
32 #define B2F2AA (*((volatile unsigned short *)(BANK2|0x554))) // 0x2aa
33 #define B2F (*((volatile unsigned short *)(BANK2)))
38 #define CMD_CHIP_ERASE 'E'
39 #define CMD_SECTOR_ERASE 'S'
40 #define CMD_CHIP_ID 'I'
48 typedef unsigned char u8;
49 typedef unsigned short u16;
50 typedef unsigned int u32;
56 unsigned long sector_address[19]={
57 0x00000,0x02000,0x03000,0x04000,0x08000,
71 void mmap_init(u8 memtype) {
79 PLLCFG=0x42; // multiplier = 3 (for cclk), dividor = 4 (for f_cco)
80 PLLCON=0x03; // enable and set as clk source for the lpc
85 while(!(PLLSTAT&(1<<10)))
89 int flash_sector_erase(u8 flash,u8 sector) {
96 a18_12=sector_address[sector]<<1;
98 if((flash!='0')|(flash!='2'))
109 *((volatile u16 *)(base|a18_12))=0x30;
118 *((volatile u16 *)(base|a18_12))=0x30;
127 int flash_chip_erase(u8 bank) {
129 if((bank!='0')|(bank!='2'))
150 void unlock_bypass(u8 bank) {
152 if((bank!='0')|(bank!='2'))
167 void unlock_bypass_reset(u8 bank) {
169 if((bank!='0')|(bank!='2'))
182 void flash_write(u32 addr,u16 data) {
184 *((volatile unsigned short *)addr)=0xa0;
185 *((volatile unsigned short *)addr)=data;
188 void ext_mem_bank_init(void) {
190 BCFG0=0x10000420; // flash 1
191 BCFG1=0x00000c42; // lcd
192 BCFG2=0x10000420; // flash 2
195 void pin_select_init() {
198 * a[19:2] -> address lines
204 void uart0_init(void) {
206 PINSEL0=0x05; // pin select -> tx, rx
207 UART0_FCR=0x07; // enable fifo
208 UART0_LCR=0x83; // set dlab + word length
209 UART0_DLL=0x04; // br: 38400 @ 10/4 mhz
211 UART0_LCR=0x03; // unset dlab
214 void uart0_send_string(char *txbuf) {
221 UART0_THR=txbuf[i++];
222 /* flush if tx buffer maximum reached */
224 while(!(UART0_LSR&(1<<6)))
228 /* flush if \n and \r do not fit in the tx buffer */
230 while(!(UART0_LSR&(1<<6)))
236 /* flush uart0 anyways */
237 while(!(UART0_LSR&(1<<6)))
241 void uart0_send_buf16(u16 *buf,int len) {
247 for(i=0;i<len/2;i++) {
249 while(!(UART0_LSR&(1<<6)))
251 UART0_THR=buf[i]&0xff;
252 UART0_THR=(buf[i]>>8)&0xff;
256 void uart0_send_buf32(u32 *buf,int len) {
262 for(i=0;i<len/4;i++) {
264 while(!(UART0_LSR&(1<<6)))
266 UART0_THR=buf[i]&0xff;
267 UART0_THR=(buf[i]>>8)&0xff;
268 UART0_THR=(buf[i]>>16)&0xff;
269 UART0_THR=(buf[i]>>24)&0xff;
273 void uart0_send_byte(u8 send) {
275 while(!(UART0_LSR&(1<<5)))
281 u8 uart0_get_byte(void) {
285 while(!(UART0_LSR&(1<<0)))
293 void receive_data_and_write_to_flash(u32 addr,u32 datalen) {
299 /* which bank to program */
308 /* receive and write data */
309 for(i=0;i<datalen/2;i++) {
310 data=uart0_get_byte();
311 data=uart0_get_byte()<<8;
312 *((unsigned volatile short *)addr)=0xa0;
313 *((unsigned volatile short *)addr)=data;
318 unlock_bypass_reset(bank);
329 u32 i,addrlen,datalen,addr;
335 /* memory mapping of interrupt vectors to static ram */
337 //mmap_init(MMAP_RAM);
339 /* pll initialization */
342 /* uart initialization */
345 /* external memory init */
348 /* pin select init */
351 /* begin the main loop */
357 cmd=uart0_get_byte();
364 case CMD_SECTOR_ERASE:
388 /* receive (only if there is) more data from uart0 */
390 for(i=0;i<addrlen;i++) {
391 txrx=uart0_get_byte();
392 addr|=(txrx<<((addrlen-i-1)*8));
395 for(i=0;i<datalen;i++)
396 buf[i]=uart0_get_byte();
398 /* process the cmd */
400 case CMD_SECTOR_ERASE:
401 flash_sector_erase(buf[0],buf[1]);
404 /* data length to read */
405 datalen=buf[0]<<24|buf[1]<<16|buf[2]<<8|buf[3];
406 /* check addr and datalen */
407 if((addr>=BANK0)&(addr+datalen<=BANK0+BANK_SIZE))
408 uart0_send_buf16((u16 *)addr,datalen);
409 if((addr>=BANK2)&(addr+datalen<=BANK2+BANK_SIZE))
410 uart0_send_buf16((u16 *)addr,datalen);
411 if((addr>=BOOTLOADER)&(addr+datalen<=BOOTLOADER+BL_SIZE))
412 uart0_send_buf32((u32 *)addr,datalen);
416 datalen=buf[0]<<24|buf[1]<<16|buf[2]<<8|buf[3];
417 /* check addr and data len */
418 if(((addr>=BANK0)&(addr+datalen<=BANK0+BANK_SIZE))|
419 ((addr>=BANK2)&(addr+datalen<=BANK2+BANK_SIZE)))
420 receive_data_and_write_to_flash(addr,datalen);
423 flash_chip_erase(buf[0]);
430 data=*((u16 *)BANK0);
431 uart0_send_buf16(&data,2);
432 data=*((u16 *)(BANK0|0x200));
433 uart0_send_buf16(&data,2);
435 else if(buf[0]=='2') {
439 data=*((u16 *)BANK2);
440 uart0_send_buf16(&data,2);
441 data=*((u16 *)(BANK2|0x200));
442 uart0_send_buf16(&data,2);