tizen 2.4 release
[profile/mobile/platform/kernel/u-boot-tm1.git] / modem_boot / modem_boot.c
1 #include <config.h>
2 #include <common.h>
3 #include <linux/types.h>
4 #include <asm/arch/bits.h>
5 #include <linux/string.h>
6 #include <android_bootimg.h>
7 #include <linux/mtd/mtd.h>
8 #include <linux/mtd/nand.h>
9 #include <nand.h>
10 #include <android_boot.h>
11 #include <environment.h>
12 #include <jffs2/jffs2.h>
13 #include <boot_mode.h>
14 #include <malloc.h>
15 #include "packet.h"
16 #include "cmd_def.h"
17
18 //#define __TEST_SPI_ONLY__
19 //#define __DL_UART0__
20 #if defined(CONFIG_SP7702) || defined(CONFIG_SP8810W)
21 #define __DL_UART1__
22 #endif
23
24 #ifdef CONFIG_SC7710G2
25 #define __DL_UART3__
26 #endif
27
28 #ifndef __SPI_MODE__
29 #define hs_channel_open    sdio_channel_open
30 #define hs_channel_close   sdio_channel_close
31 #else
32 #define hs_channel_open    SPI_channel_open
33 #define hs_channel_close   SPI_channel_close
34
35 #endif
36 #define mdelay(_ms) udelay(_ms*1000)
37
38 static int      boot_status = 0;
39
40 extern int serial3_init (void);
41 extern int serial3_getc(void);
42 extern int serial3_tstc (void);
43 extern void serial3_putc(const char c);
44 extern int serial_init (void);
45 extern int serial_getc(void);
46 extern int serial_tstc (void);
47 extern void serial_putc(const char c);
48
49 #if defined( __DL_UART0__)
50 #error please defined macro in xxxxconfig.h
51 #elif defined(__DL_UART1__) 
52 #define uart_init  serial_init
53 #define uart_getc  serial_getc
54 #define uart_tstc  serial_tstc
55 #define uart_putc  serial_putc
56 #elif defined(__DL_UART3__)     
57 #define uart_init  serial3_init
58 #define uart_getc  serial3_getc
59 #define uart_tstc  serial3_tstc
60 #define uart_putc  serial3_putc
61 #else
62 #error please defined macro in xxxxconfig.h
63 #endif
64
65 #if defined(CONFIG_SP7702) || defined(CONFIG_SP8810W)
66 #define PIN_REG_SIMDA3   (0x8C000470)
67 #define PIN_REG_SIMRST3  (0x8C000478)
68 #define PIN_CTRL_REG     (0x8b000028)
69 #endif
70
71 extern int __dl_log_share__;
72
73 void    print_message(int dir,char *buffer,int size)
74 {
75         int i;
76         if(size <= 0)
77                 return;
78         if(dir)
79                 printf("Send(%d): ",size);
80         else
81                 printf("Recv(%d): ",size);
82         if(size > 16) size =16;
83         for(i=0;i<size;i++)
84                 printf("0x%x ",buffer[i]);
85         printf("\n");
86 }
87 int uart_write(char *buffer,int size)
88 {
89         int write_len=0;
90         if(size > 0)
91         {
92                 do{
93                         uart_putc(*buffer++);
94                         size--;
95                         write_len++;
96                 }while(size>0);
97         }
98         return  write_len;
99 }
100
101 int uart_read(char *buffer,int size)
102 {
103         int read_len=0;
104         char *data=buffer;
105         if(size > 0)
106         {
107                 do{
108                         if(uart_tstc())
109                         {
110                             *buffer++ = uart_getc();
111                             size --;
112                             read_len++;
113                         }
114                 }while(size>0);
115         }
116         return read_len;
117 }
118
119 int     try_read_version_string(void)
120 {
121         unsigned char version_string[64];
122         int version_string_length = 0;
123         unsigned char read_char;
124
125         version_string_length = 0;
126         read_char = uart_getc();
127         if(read_char != 0x7e){
128                 return 1;
129         }
130         version_string[version_string_length++] = read_char;
131
132         while(uart_tstc()){
133                 read_char = uart_getc();        
134                 version_string[version_string_length++] = read_char;
135                 if(read_char == 0x7e){
136                         return parse_version_string(version_string,version_string_length);
137                 }
138         }
139         mdelay(10);
140         if(!uart_tstc())
141                 return 1;
142         
143         while(uart_tstc()){
144                 read_char = uart_getc();        
145                 version_string[version_string_length++] = read_char;
146                 if(read_char == 0x7e){
147                         return parse_version_string(version_string,version_string_length);
148                 }
149         }
150         return 1;
151         //reset_modem_device();
152 }
153
154 static void download_fdl(char *data,int size)
155 {
156         int retval;
157         int i;
158         int file_length = size;
159         int sent_length = 0;
160         int packet_size = 256;
161         unsigned long fdl_run_address = 0x40000000;
162         char    *buffer = data;
163 #ifndef __TEST_SPI_ONLY__       
164         retval = uart_send_connect_message();
165         
166         retval = uart_send_start_message(file_length,fdl_run_address);
167         do{
168                 if(sent_length + packet_size >= file_length)
169                         packet_size = file_length - sent_length;
170                 uart_send_data_message(buffer,packet_size);
171                 sent_length += packet_size;
172                 buffer += packet_size;
173         }while(sent_length < file_length);
174         uart_send_end_message();
175         
176         uart_send_exec_message(fdl_run_address);
177 #endif
178
179         //printf("\ntry to connect with FDL... \n");
180         do{
181                 mdelay(1);
182                 uart_putc(0x7E);
183         }while(!uart_tstc());
184         boot_status = 1;//uart and FDL 
185         try_read_version_string();
186         uart_send_change_spi_mode_message();
187 #if 0 //def __DL_UART0__
188     *(volatile unsigned long *)PIN_REG_SIMDA3  |= 0x00000380;
189     *(volatile unsigned long *)PIN_REG_SIMRST3 |= 0x00000380;
190     *(volatile unsigned long *)PIN_CTRL_REG &= 0xffffffbf;
191         
192     uart_init();
193     __dl_log_share__ = 0;
194     printf("__dl_log_share__ = %d \n",__dl_log_share__);
195 #endif
196         
197         mdelay(2);
198         {
199                 extern void hs_download_proc(void);
200                 extern void hs_channel_open(void);
201                 int status;
202                 
203                 hs_channel_open();
204                 hs_download_proc();
205                 hs_channel_close();
206         }
207         
208 }
209
210 int  parse_version_string(unsigned char *buffer,int length)
211 {
212         int i;
213         unsigned short temp_data[64];
214         struct pkt_header_tag packet;
215         unsigned short calculated_crc = 0;
216         unsigned short received_crc = (buffer[length-3] << 8) | buffer[length-2]; 
217
218         packet.type = (buffer[1]<<8)|buffer[2];
219         packet.length = (buffer[3]<<8)|buffer[4];
220         if((buffer[0] == 0x7e) && (buffer[length-1] == 0x7e))
221         {
222                 if(boot_status == 0)
223                         calculated_crc = crc_16_l_calc(&buffer[1],length-4);
224                 else
225                 {
226                         memcpy(temp_data,&buffer[1],length-4);
227                         calculated_crc = frm_chk(&temp_data,length-4);
228                         calculated_crc = (calculated_crc>>8)|(calculated_crc<<8);
229                 }
230         }
231
232         //printf("type 0x%x size =%d r_crc = 0x%x c_crc 0x%x\n",packet.type,packet.length,received_crc,calculated_crc);
233         if(calculated_crc == received_crc)
234                 return 0;
235         return 0;
236 }
237
238
239
240 void   bootup_modem(char *data,int size)
241 {
242         int i=0,delay=0;
243         unsigned char hand_shake_flag = 0x7e;
244         int is_empty=1;
245         int retval=0;
246         int retry_count;
247 #ifdef __DL_UART1__
248     __dl_log_share__ = 1;
249     uart_init();
250
251     *(volatile unsigned long *)PIN_REG_SIMDA3  |= 0x000003a0;
252         *(volatile unsigned long *)PIN_REG_SIMRST3 |= 0x000003a0;
253         *(volatile unsigned long *)PIN_CTRL_REG |= 0x00000040;
254 #endif
255 #ifdef __DL_UART3__
256     uart_init();
257 #endif
258
259 modem_reset:
260         retry_count = 0;
261         {
262                 extern void modem_poweroff(void);
263                 
264                 modem_poweroff();
265                 mdelay(2000);
266         }
267         
268     uart_putc(0x7E);
269     uart_putc(0x7E); 
270         
271 #ifdef __TEST_SPI_ONLY__
272         download_fdl(data,size);
273 #endif
274
275         {
276                 extern void modem_poweron(void);
277                 modem_poweron();
278         }
279         
280         //printf("try to get version string....\n");
281         for(;;)
282         {
283                 is_empty = 1;
284                 while(uart_tstc()){
285                         uart_getc();
286                 };
287
288                 uart_putc(hand_shake_flag);
289
290                 mdelay(10);
291                 retry_count++;
292                 if(uart_tstc()){
293                         is_empty = 0;
294                         //printf("UART RX FIFO is not empty....\n");
295                 }
296
297                 if(is_empty == 0)
298                 {
299                         retval = try_read_version_string();
300                         if(retval == 0)
301                                 break;
302                 }
303                 if(retry_count >200)
304                         goto modem_reset;
305         }
306         //printf("\nsuccessfully read version string !!!\n");
307         download_fdl(data,size);
308         boot_status = 2;
309
310 }
311
312 inline int modem_status(void) {
313         return boot_status;
314 }
315