]> rtime.felk.cvut.cz Git - sysless.git/blob - arch/h8300/generic/bloader/bloader.c
h8300: Make bloader work with ELF toolchain
[sysless.git] / arch / h8300 / generic / bloader / bloader.c
1 /* procesor H8S/2638 ver 1.1  */
2 #include <types.h>
3 #include <cpu_def.h>
4 #include <mcu_regs.h>
5 //#include <periph/chmod_lcd.h>
6 //#include <periph/sgm_lcd.h>
7 #include <system_def.h>
8 #include <string.h>
9
10
11 #ifndef DEB_LED_INIT
12 #define DEB_LED_INIT()
13 #define DEB_LED_OFF(num)
14 #define DEB_LED_ON(num)
15 #endif
16
17 #define BOOT_TEST
18 #define APPLICATION_START
19
20 /*#define USE_FONT_6x8*/
21
22 #ifndef HIT_LOAD_BAUD
23   #define HIT_LOAD_BAUD 0
24 #endif
25
26 /* hack for start of main, should use crt0.o instead */
27 /* Used in boot mode to start main(). */
28 __asm__ /*__volatile__*/(
29         ".global _start_hack\n\t"
30         "_start_hack : \n\t"
31         "mov.l  #0xffdffe,sp\n\t"
32         "jsr    _main\n"
33         "0: bra 0b\n"
34         );
35
36 void exit(int status)
37 {
38   while(1);
39 }
40
41 void deb_wr_hex(long hex, short digs);
42
43 char data_test[]={'D','A','T','A',0};
44
45  /*
46  *----------------------------------------------------------
47  */
48 void deb_wr_hex(long hex, short digs)
49 {
50   char c;
51   while(digs--){
52     c=((hex>>(4*digs))&0xf)+'0';
53     if(c>'9') c+='A'-'9'-1;  
54   }
55 }
56
57 static void deb_led_out(char val)
58 {
59   if (val&1)
60     DEB_LED_ON(0);
61   else
62     DEB_LED_OFF(0);
63   if (val&2)
64     DEB_LED_ON(1);
65   else
66     DEB_LED_OFF(1);
67   if (val&4)
68     DEB_LED_ON(2);
69   else
70     DEB_LED_OFF(2);
71   if (val&8)
72     DEB_LED_ON(3);
73   else
74     DEB_LED_OFF(3);
75 }
76
77
78 #ifdef BOOT_TEST
79
80 #include <boot_fn.h>
81
82 /* Provided by linker script */
83 extern char __boot_fn_start;
84 extern char __boot_fn_end;
85 extern char etext;
86
87 void RelocatedProgMode(unsigned baud)
88
89   size_t reloc_size=&__boot_fn_end-&__boot_fn_start;
90   memcpy(&__boot_fn_start,&etext,reloc_size);
91   /*deb_wr_hex((long)ProgMode_ptr,8);*/
92   ProgMode(baud);
93 }
94
95
96 void boot_test()
97 {
98   /*set power on for SCI0 and SCI1 module*/
99   *SYS_MSTPCRB&=~MSTPCRB_SCI0m;
100   *SYS_MSTPCRB&=~MSTPCRB_SCI1m;
101
102  #if 0
103   SCIInit(HIT_LOAD_BAUD);
104
105   SCISend('B');
106   SCISend('B');
107   SCISend(':');
108
109  #endif
110
111   /* switch off SCI2 module*/
112   *SYS_MSTPCRB|=MSTPCRB_SCI2m;
113   
114   *DIO_PADR |= 0x0f;
115   *DIO_PADDR = 0x0f;
116
117   if(!HIT_LOAD_BAUD) {
118     long bauddet;   
119     bauddet=SCIAutoBaud();
120     deb_wr_hex(bauddet,4);
121   }
122
123  
124   if((__u8*)&__boot_fn_start<(__u8*)0xffb000) {
125           /* If we are not in the internal RAM, copy and run us from
126            * there */
127           RelocatedProgMode(HIT_LOAD_BAUD);
128   }
129    else
130     ProgMode(HIT_LOAD_BAUD);
131 }
132
133 #endif /* BOOT_TEST */
134
135 inline int call_address(unsigned long addr)
136 {
137   typedef int (*my_call_t)(void);
138   my_call_t my_call=(my_call_t)addr;
139   return my_call();  
140 }
141
142 /*
143  *-----------------------------------------------------------
144  */
145
146
147 /* Only for debuging */
148 void deb_led_blink() {
149   while(1) {
150     deb_led_out(1);
151     FlWait(1*1000000);
152     deb_led_out(2);
153     FlWait(1*1000000);
154   };
155 };
156
157 int main()
158 {
159   __u8 *p;
160
161   _setup_board(); /* Provided in bspbase library of each board */
162
163   p=(__u8*)&deb_wr_hex;
164   if(p>=IRAM_START) p=" IRAM";
165 #ifdef SRAM_START
166   else if(p>=SRAM_START) p=" SRAM";
167 #endif
168 #ifdef XRAM_START
169   else if(p>=XRAM_START) p=" XRAM";
170 #endif
171   else if(p>(__u8*)0x4000l) p=" FLSHU";
172   else p=" FLSHB";
173
174
175 #if 0           /* FLASH timing test */
176   do{
177     deb_led_out(~0);
178     FlWait(1l);
179     deb_led_out(~1);
180     FlWait(2l);
181     deb_led_out(~2);
182     FlWait(10l);
183     deb_led_out(~3);
184     FlWait(20l);
185   }while(1);
186 #endif
187
188 #ifdef APPLICATION_START
189   if(((*FLM_FLMCR1) & FLMCR1_FWEm)==0) {
190     if (*((unsigned long *)0x4000)!=0xffffffff){
191       call_address(0x4000);
192     }
193   }
194 #endif /* APPLICATION_START */
195  
196   deb_led_out(15);
197   FlWait(1*100000);
198   deb_led_out(3);
199  
200 #ifdef BOOT_TEST
201   boot_test();
202 #endif /* BOOT_TEST */ 
203  
204   return 0;
205 };
206
207