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 long *)(BANK0|0xaaa))) // 0x555
29 #define B0F2AA (*((volatile unsigned long *)(BANK0|0x554))) // 0x2aa
30 #define B2F555 (*((volatile unsigned long *)(BANK2|0xaaa))) // 0x555
31 #define B2F2AA (*((volatile unsigned long *)(BANK2|0x554))) // 0x2aa
35 #define CMD_CHIP_ERASE 'E'
36 #define CMD_CHIP_ID 'I'
44 typedef unsigned char u8;
45 typedef unsigned short u16;
46 typedef unsigned int u32;
52 void mmap_init(u8 memtype) {
60 PLLCFG=0x02; // multiplier = 2
61 PLLCON=0x03; // enable and set as clk source for the lpc
66 while(!(PLLSTAT&(1<<10)))
70 void uart0_init(void) {
72 PINSEL0=0x05; // pin select -> tx, rx
73 UART0_FCR=0x07; // enable fifo
74 UART0_LCR=0x83; // set dlab + word length
75 UART0_DLL=0x04; // br: 38400 @ 10/4 mhz
77 UART0_LCR=0x03; // unset dlab
80 void uart0_send_string(char *txbuf) {
88 /* flush if tx buffer maximum reached */
90 while(!(UART0_LSR&(1<<6)))
94 /* flush if \n and \r do not fit in the tx buffer */
96 while(!(UART0_LSR&(1<<6)))
102 /* flush uart0 anyways */
103 while(!(UART0_LSR&(1<<6)))
107 void uart0_send_buf16(u16 *buf,int len) {
113 for(i=0;i<len/2;i++) {
115 while(!(UART0_LSR&(1<<6)))
117 UART0_THR=buf[i]&0xff;
118 UART0_THR=(buf[i]>>8)&0xff;
122 void uart0_send_buf32(u32 *buf,int len) {
128 for(i=0;i<len/4;i++) {
130 while(!(UART0_LSR&(1<<6)))
132 UART0_THR=buf[i]&0xff;
133 UART0_THR=(buf[i]>>8)&0xff;
134 UART0_THR=(buf[i]>>16)&0xff;
135 UART0_THR=(buf[i]>>24)&0xff;
139 void uart0_send_byte(u8 send) {
141 while(!(UART0_LSR&(1<<5)))
147 u8 uart0_get_byte(void) {
151 while(!(UART0_LSR&(1<<0)))
167 u32 i,addrlen,datalen,addr;
173 /* memory mapping of interrupt vectors to static ram */
175 //mmap_init(MMAP_RAM);
177 /* pll initialization */
180 /* uart initialization */
183 /* external memory init */
184 BCFG0=0x1000FBEF; // BCFG2 should be fine as is
186 /* begin the main loop */
192 cmd=uart0_get_byte();
216 /* receive (only if there is) more data from uart0 */
218 for(i=0;i<addrlen;i++) {
219 txrx=uart0_get_byte();
220 addr|=(txrx<<((addrlen-i-1)*8));
223 for(i=0;i<datalen;i++)
224 buf[i]=uart0_get_byte();
226 /* process the cmd */
229 /* data length to read */
230 datalen=buf[0]<<24|buf[1]<<16|buf[2]<<8|buf[3];
231 /* check addr and datalen */
232 if((addr>=BANK0)&(addr+datalen<=BANK0+BANK_SIZE))
233 uart0_send_buf16((u16 *)addr,datalen);
234 if((addr>=BANK2)&(addr+datalen<=BANK2+BANK_SIZE))
235 uart0_send_buf16((u16 *)addr,datalen);
236 if((addr>=BOOTLOADER)&(addr+datalen<=BOOTLOADER+BL_SIZE))
237 uart0_send_buf32((u32 *)addr,datalen);
248 else if(buf[0]=='2') {
256 uart0_send_byte(buf[0]);
263 data=*((u16 *)BANK0);
264 data=*((u16 *)(BANK0|0x200));
266 else if(buf[0]=='2') {
270 data=*((u16 *)BANK2);
271 data=*((u16 *)(BANK2|0x200));
273 uart0_send_buf16(&data,2);