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 0x00000800
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) {
57 void uart0_init(void) {
59 PINSEL0=0x05; // pin select -> tx, rx
60 UART0_FCR=0x07; // enable fifo
61 UART0_LCR=0x83; // set dlab + word length
62 //UART0_DLL=0x10; // br: 9600 @ 10/4 mhz
63 UART0_DLL=0x04; // br: 38400 @ 10/4 mhz
65 UART0_LCR=0x03; // unset dlab
68 void uart0_send_string(char *txbuf) {
76 /* flush if tx buffer maximum reached */
78 while(!(UART0_LSR&(1<<6)))
82 /* flush if \n and \r do not fit in the tx buffer */
84 while(!(UART0_LSR&(1<<6)))
90 /* flush uart0 anyways */
91 while(!(UART0_LSR&(1<<6)))
95 void uart0_send_buf16(u16 *buf,int len) {
101 for(i=0;i<len/2;i++) {
103 while(!(UART0_LSR&(1<<6)))
105 UART0_THR=buf[i]&0xff;
106 UART0_THR=(buf[i]>>8)&0xff;
110 void uart0_send_buf32(u32 *buf,int len) {
116 for(i=0;i<len/4;i++) {
118 while(!(UART0_LSR&(1<<6)))
120 UART0_THR=buf[i]&0xff;
121 UART0_THR=(buf[i]>>8)&0xff;
122 UART0_THR=(buf[i]>>16)&0xff;
123 UART0_THR=(buf[i]>>24)&0xff;
127 void uart0_send_byte(u8 send) {
129 while(!(UART0_LSR&(1<<5)))
135 u8 uart0_get_byte(void) {
139 while(!(UART0_LSR&(1<<0)))
155 u32 i,addrlen,datalen,addr;
161 /* memory mapping of interrupt vectors to static ram */
163 //mmap_init(MMAP_RAM);
165 /* uart initialization */
168 /* external memory init */
169 BCFG0=0x1000FBEF; // BCFG2 should be fine as is
171 /* begin the main loop */
177 cmd=uart0_get_byte();
201 /* receive (only if there is) more data from uart0 */
203 for(i=0;i<addrlen;i++) {
204 txrx=uart0_get_byte();
205 addr|=(txrx<<((addrlen-i-1)*8));
208 for(i=0;i<datalen;i++)
209 buf[i]=uart0_get_byte();
211 /* process the cmd */
214 /* data length to read */
215 datalen=buf[0]<<24|buf[1]<<16|buf[2]<<8|buf[3];
216 /* check addr and datalen */
217 if((addr>=BANK0)&(addr+datalen<=BANK0+BANK_SIZE))
218 uart0_send_buf16((u16 *)addr,datalen);
219 if((addr>=BANK2)&(addr+datalen<=BANK2+BANK_SIZE))
220 uart0_send_buf16((u16 *)addr,datalen);
221 if((addr>=BOOTLOADER)&(addr+datalen<=BOOTLOADER+BL_SIZE))
222 uart0_send_buf32((u32 *)addr,datalen);
233 else if(buf[0]=='2') {
241 uart0_send_byte(buf[0]);
248 data=*((u16 *)BANK0);
249 data=*((u16 *)(BANK0|0x200));
251 else if(buf[0]=='2') {
255 data=*((u16 *)BANK2);
256 data=*((u16 *)(BANK2|0x200));
258 uart0_send_buf16(&data,2);