]> rtime.felk.cvut.cz Git - sysless.git/blob - arch/h8300/generic/libs/boot/boot.c
Initial import.
[sysless.git] / arch / h8300 / generic / libs / boot / boot.c
1 #include <types.h>
2 #include <cpu_def.h>
3 #include <h8s2638h.h>
4 #include <system_def.h>
5 #include <string.h>
6 #include "boot_fn.h"
7
8 /*#define USE_FONT_6x8*/
9
10 #ifndef HIT_LOAD_BAUD
11   #define HIT_LOAD_BAUD 0
12 #endif
13
14 void exit(int status)
15 {
16   while(1);
17 }
18
19 extern char __boot_fn_start;
20 extern char __boot_fn_end;
21
22 void RelocatedProgMode(unsigned long where, unsigned baud)
23
24   void (*ProgMode_ptr)(unsigned baud);
25   unsigned long reloc_offs=where-(unsigned long)&__boot_fn_start;
26   size_t reloc_size=&__boot_fn_end-&__boot_fn_start;
27   ProgMode_ptr=&ProgMode;
28   (__u8*)ProgMode_ptr+=reloc_offs;
29   memcpy((char*)where,&__boot_fn_start,reloc_size);
30   (*ProgMode_ptr)(baud);
31 }
32
33 void flash_loader(void)
34 {
35   SCIInit(HIT_LOAD_BAUD);
36
37   if((__u8*)&__boot_fn_start<(__u8*)0xff0000)
38     RelocatedProgMode(0xffb000,HIT_LOAD_BAUD);
39    else
40     ProgMode(HIT_LOAD_BAUD);
41 }
42
43 inline int call_address(unsigned long addr)
44 {
45   typedef int (*my_call_t)(void);
46   my_call_t my_call=(my_call_t)addr;
47   return my_call();  
48 }
49
50 int main()
51 {
52   /* Internal RAM enabled, advanced interrupt mode */
53   /* *SYS_SYSCR = 1*SYSCR_RAMEm | 1*SYSCR_INTM1m ; */
54
55   /* Remap 4kB of RAM from 0xffd000-0xffdfff to 0x0-0xfff */
56   /* *FLM_RAMER= 1*RAMER_RAMSm | 0&RAMER_RAMxm */
57   /* Sideefect - sets Flash software protection */
58  
59   /* Enables access to flash control registers */
60   *IIC_SCRX |= SCRX_FLSHEm;
61   
62   /* set shaddow registers */
63   DIO_P1DDR_shaddow=0;
64   DIO_P3DDR_shaddow=0;
65     
66   /* deactivate motor and power outputs */
67  #if HW_VER_CODE<VER_CODE(0,2,0)
68   *STPM_OUT=0;
69   *PWRCTRL_OUT=PWRCTRL_BACKLm;
70  #endif /* >=VER_CODE(0,2,0) */
71
72   /* Setup system clock oscilator */
73   /* PLL mode x4, */
74   /* *SYS_LPWRCR=2&LPWRCR_STCxm; */
75   /* PLL mode x2, */
76   /* *SYS_LPWRCR=1&LPWRCR_STCxm; */
77   { const char clkrat2stc[]={0,0/*1*/,1/*2*/,1,2/*4*/,2,2,2,3/*8*/};
78     *SYS_LPWRCR=LPWRCR_STCxm&(LPWRCR_STC0m*
79                 clkrat2stc[(CPU_SYS_HZ+CPU_REF_HZ/2)/CPU_REF_HZ]);
80   }
81   /* No clock disable, immediate change, busmaster high-speed */
82   *SYS_SCKCR=(0*SCKCR_PSTOPm)|(1*SCKCR_STCSm)|(0&SCKCR_SCKxm);
83
84  #ifdef USE_FONT_6x8
85   /* set 6x8 pixel font */
86   //*DIO_P7DR |=0x10;
87  #else /* USE_FONT_6x8 */
88   /* set 8x8 pixel font */
89   //*DIO_P7DR &=~0x10;
90  #endif /* USE_FONT_6x8 */
91   //SHADDOW_REG_SET(DIO_P7DDR,0x10);
92
93   /* Setup chipselect outputs CS4 CS5 CS6 */
94   //*DIO_P7DR |=1|2|4;
95   //SHADDOW_REG_SET(DIO_P7DDR,1|2|4);
96
97   /* Setup chipselect outputs CS3 CS2 CS1 CS0 */
98   //*DIO_PGDR |=2|4|8|0x10;
99   //SHADDOW_REG_SET(DIO_PGDDR,2|4|8|0x10);
100
101   /* setup chipselect 1 - XRAM */
102   *BUS_ABWCR&=~ABWCR_ABW1m;     /* 16 bit width */
103   *BUS_ASTCR&=~ASTCR_AST1m;     /* 2 states access */
104   *BUS_WCRL&=~(WCRL_W11m|WCRL_W10m);/* 0 additional wait states */
105
106   /* setup chipselect 2 - SGM_LCD */
107   *BUS_ABWCR|=ABWCR_ABW2m;      /* 8 bit width */
108   *BUS_ASTCR|=ASTCR_AST2m;      /* 3 states access */
109   *BUS_WCRL&=~(WCRL_W21m|WCRL_W20m);/* 0 additional wait states */
110   *BUS_WCRL|=1*WCRL_W21m;       /* 0/1 additional wait state */
111
112   /* setup chipselect 3 - SRAM */
113   *BUS_ABWCR|=ABWCR_ABW3m;      /* 8 bit width */
114   *BUS_ASTCR|=ASTCR_AST3m;      /* 3 states access */
115   *BUS_WCRL&=~(WCRL_W31m|WCRL_W30m);/* 0 additional wait states */
116
117   /* setup chipselect 4 - IDE */
118   *BUS_ABWCR&=~ABWCR_ABW4m;     /* 16 bit width */
119   *BUS_ASTCR|=ASTCR_AST4m;      /* 3 states access */
120   *BUS_WCRH&=~(WCRH_W41m|WCRH_W40m);/* 0 additional wait states */
121
122   /* setup chipselect 5 - IDE */
123   *BUS_ABWCR&=~ABWCR_ABW5m;     /* 16 bit width */
124   *BUS_ASTCR|=ASTCR_AST5m;      /* 3 states access */
125   *BUS_WCRH&=~(WCRH_W51m|WCRH_W50m);/* 0 additional wait states */
126
127   /* setup chipselect 6 - KL41 */
128   *BUS_ABWCR|=ABWCR_ABW6m;      /* 8 bit width */
129   *BUS_ASTCR|=ASTCR_AST6m;      /* 3 states access */
130   *BUS_WCRH=WCRH_W61m|WCRH_W60m;        /* 3 additional wait states */
131
132          /* crross cs wait| rd/wr wait    | no burst and DRAM */
133   *BUS_BCRH=0*BCRH_ICIS1m | 0*BCRH_ICIS0m;
134          /* release      | no DMAC buffer | no external wait */
135   /* ****************************************************  */ //*BUS_BCRL=0*BCRL_BRLEm | 0*BCRL_WDBEm | 0*BCRL_WAITEm;
136   *DIO_PCDDR=0xff;              /* A0-A7 are outputs */
137   *DIO_PBDDR=0xff;              /* A8-A15 are outputs */
138   /* Setup full 20 address lines */
139   *DIO_PADR|=0x0f;
140   *DIO_PADDR=0x0f;              /* A16-A19 are outputs */
141          /* number of address output signals */
142   *SYS_PFCR=__val2mfld(PFCR_AExm,20-8);
143
144   /* Stop all modules */
145   *SYS_MSTPCRA=0xff;
146   *SYS_MSTPCRB=0xff;
147   *SYS_MSTPCRC=0xff;
148
149   /*set power on for SCI4 module*/
150   *SYS_MSTPCRC&=~MSTPCRC_SCI4m;
151
152   /* show something on debug leds */
153   *DIO_P1DR=0xf-1;
154   SHADDOW_REG_SET(DIO_P1DDR,0x0f);
155
156   /* Disable SCI 2 */
157   /* Off TxD2 on Port PA.1 */
158   /* Off RxD2 on Port PA.2 */
159   *SCI_SCR2=0;
160   *DIO_PADR|=0x06;
161   *DIO_PADDR=0x01;
162   *SCI_SMR2=0;
163  
164   /* Stop SCI4 communication */
165   // SCI4 is not aviable 
166   //*SCI_SCR4=0;
167   //*SCI_SMR4=0;
168
169   /* Output TxD4 on Port P3.7, TxD0 on P3.0 */
170   /*        RTS4 on Port P3.2               */
171   /* Input  RxD4 on Port P3.6, RxD0 on P3.1 */
172   /*        CTS4 on Port P3.3               */
173   *DIO_P3DR|=0xc5;
174   SHADDOW_REG_SET(DIO_P3DDR,0x85);
175
176   /* Enables access to flash control registers */
177   *IIC_SCRX |= SCRX_FLSHEm;
178
179   if(((*FLM_FLMCR1) & FLMCR1_FWEm)!=0){
180     flash_loader();
181   }
182   
183   if (*((unsigned long *)0x4000)!=0xffffffff){
184     call_address(0x4000);
185   }
186   
187   if (*((unsigned long *)0x200000)==0xff0055aa){
188     call_address(0x200004);
189   }
190
191   flash_loader();
192   return 0;
193 };