added fwflash firmware (not yet working!)
[my-code/arm.git] / betty / fwflash.c
diff --git a/betty/fwflash.c b/betty/fwflash.c
new file mode 100644 (file)
index 0000000..4462654
--- /dev/null
@@ -0,0 +1,221 @@
+/*
+ * fwflash.c - handling the betty flashes
+ *
+ * author: hackbard@hackdaworld.org
+ *
+ */
+
+/*
+ * include files
+ */
+
+#include "lpc2xxx.h"
+
+/*
+ * defines
+ */
+
+/* band 0/2 addr */
+#define BANK0                  0x80000000
+#define BANK2                  0x82000000
+#define B0HB                   0x00000000
+#define B2HB                   0x02000000
+
+/* commands */
+#define CMD_READ               'R'
+#define CMD_CHIP_ERASE         'E'
+
+#define BUFSIZE                        16
+
+/*
+ * type definitions
+ */
+
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned int u32;
+
+/*
+ * define macros
+ */
+
+#define TX_BYTE(x)             while(!(UART0_LSR&(1<<5))) continue; \
+                               UART0_THR=x;
+
+#define SEND_OK                        TX_BYTE('o'); TX_BYTE('k'); TX_BYTE('\n'); \
+                               TX_BYTE('\r');
+
+/*
+ * function prototypes
+ */
+
+void mmap_init(u8 memtype);
+void uart0_init(void);
+void uart0_send_string(char *txbuf);
+void uart0_send_char(char send);
+
+/*
+ * main function
+ */
+
+int main(void) {
+
+       /* variables */
+
+       u32 i,addrlen,datalen;
+       u8 buf[BUFSIZE];
+       u32 addr;
+       u16 *dptr;
+       u8 cmd;
+       u8 txrx;
+
+       /* memory mapping of interrupt vectors to static ram */
+
+       //mmap_init(MMAP_RAM);
+
+       /* uart initialization */
+
+       //uart0_init();
+
+       PINSEL0=0x05;                   // pin select -> tx, rx
+       UART0_FCR=0x07;                 // enable fifo
+       UART0_LCR=0x83;                 // set dlab + word length
+       UART0_DLL=0x10;                 // br: 9600 @ 10/4 mhz
+       UART0_DLM=0x00;
+       UART0_LCR=0x03;                 // unset dlab
+
+       /* external memory init */
+
+       BCFG0=0x1000FBEF;               // no boot[1:0] influence? (thnx colibri)
+                                       // BCFG2 should be fine as is
+
+       /* begin the main loop */
+       while(1) {
+
+       /* receive cmd */
+       while(1) {
+
+               while(!(UART0_LSR&(1<<0)))
+                       continue;
+               cmd=UART0_RBR;
+
+               if(cmd==CMD_CHIP_ERASE) {
+SEND_OK
+                       addrlen=0;
+                       datalen=1;
+                       break;
+               }
+
+               if(cmd==CMD_READ) {
+                       addrlen=4;
+                       datalen=1;
+                       break;
+               }
+       }
+
+       /* receive (only if there is) more data from uart0 */
+
+       addr=0;
+       for(i=0;i<addrlen;i++) {
+               while(!(UART0_LSR&(1<<0)))
+                       continue;
+               txrx=UART0_RBR;
+               addr|=(txrx<<(i*4));
+       }
+SEND_OK
+
+       for(i=0;i<datalen;i++) {
+               while(!(UART0_LSR&(1<<0)))
+                       continue;
+               buf[i++]=UART0_RBR;
+       }
+SEND_OK
+
+       /* process the cmd */
+
+       switch(cmd) {
+               case CMD_READ:
+                       dptr=(u16 *)addr;
+                       for(i=0;i<buf[0];i++) {
+                               TX_BYTE(*dptr);
+                               dptr++;
+                       }
+                       break;  
+               case CMD_CHIP_ERASE:
+                       if(buf[0]=='0')
+                               addr=B0HB;
+                       else if(buf[0]=='2')
+                               addr=B2HB;
+                       else
+                               break;
+                       // cycle 1
+                       dptr=(u16 *)(addr|0x555);
+                       *dptr=0xaa;
+                       // cycle 2
+                       dptr=(u16 *)(addr|0x2aa);
+                       *dptr=0x55;
+                       // cycle 3+4
+                       dptr=(u16 *)(addr|0x555);
+                       *dptr=0x80;
+                       *dptr=0xaa;
+                       // cycle 5
+                       dptr=(u16 *)(addr|0x2aa);
+                       *dptr=0x55;
+                       // cycle 6
+                       dptr=(u16 *)(addr|0x555);
+                       *dptr=0x10;
+SEND_OK
+                       break;
+               default:
+                       break;
+       }
+
+       /* send an ack, the cmd! */
+       TX_BYTE(cmd);
+               
+       }
+
+       return 0;
+}
+
+/*
+ * functions
+ */
+
+void mmap_init(u8 memtype) {
+
+       MEMMAP=memtype;
+}
+
+void uart0_init(void) {
+
+       PINSEL0=0x05;                   // pin select -> tx, rx
+       UART0_FCR=0x07;                 // enable fifo
+       UART0_LCR=0x83;                 // set dlab + word length
+       UART0_DLL=0x10;                 // br: 9600 @ 10/4 mhz
+       UART0_DLM=0x00;
+       UART0_LCR=0x03;                 // unset dlab
+}
+
+void uart0_send_string(char *txbuf) {
+
+       int i;
+
+       i=0;
+
+       while(txbuf[i])
+               UART0_THR=txbuf[i++];
+       UART0_THR='\n';
+       UART0_THR='\r';
+
+       while(!(UART0_LSR&(1<<6))) {}
+}
+
+void uart0_send_char(char send) {
+
+       while(!(UART0_LSR&(1<<5)))
+               continue;
+
+       UART0_THR=send;
+}
+