bootmode: remove build warnings
[profile/mobile/platform/kernel/u-boot-tm1.git] / property / calibration_detect.c
1 #include "calibration_detect.h"
2 #include <stdbool.h>
3 #include <malloc.h>
4
5 static unsigned int nv_buffer[256]={0};
6 static int s_is_calibration_mode = 0;
7 char *calibration_cmd_buf;
8 uint8_t pctool_cmd_buf[20];
9 uint8_t pctool_cnf_buf[20];
10
11 char *get_calibration_parameter(void)
12 {
13         if(s_is_calibration_mode != 0)
14         return calibration_cmd_buf;
15         else
16         return NULL;
17 }
18
19 bool is_calibration_by_uart(void)
20 {
21        return (2 == s_is_calibration_mode);
22 }
23
24 #define mdelay(_ms) udelay(_ms*1000)
25 #define CALIBERATE_STRING_LEN 10
26 #define CALIBERATE_HEAD 0x7e
27 #define CALIBERATE_COMMOND_T 0xfe
28 #define CALIBERATE_COMMAND_REQ  1
29
30 #define CALIBERATE_DEVICE_NULL  0
31 #define CALIBERATE_DEVICE_USB   1
32 #define CALIBERATE_DEVICE_UART  2
33
34 extern int charger_connected(void);
35 typedef  struct tag_cali_command {
36         unsigned int    reserved;
37         unsigned short  size;
38         unsigned char   cmd;
39         unsigned char   sub_cmd;
40 } COMMAND_T;
41
42 extern int get_mode_from_gpio(void);
43 extern int do_fs_file_read(char *mpart, char *filenm, void *buf, int len);
44 extern int serial_tstc(void);
45 static unsigned long long start_time;
46 static unsigned long long now_time;
47
48
49
50 static int caliberate_device = CALIBERATE_DEVICE_NULL;
51
52 static void send_caliberation_request(void)
53 {
54         COMMAND_T cmd;
55         unsigned int i;
56         unsigned char *data = (unsigned char *)&cmd;
57
58         cmd.reserved = 0;
59         cmd.cmd = CALIBERATE_COMMOND_T;
60         cmd.size = CALIBERATE_STRING_LEN-2;
61         cmd.sub_cmd = CALIBERATE_COMMAND_REQ;
62
63         serial_putc(CALIBERATE_HEAD);
64
65         for (i = 0; i < sizeof(COMMAND_T); i++)
66              serial_putc(data[i]);
67
68         serial_putc(CALIBERATE_HEAD);
69 }
70
71 static int receive_caliberation_response(uint8_t *buf,int len)
72 {
73         int count = 0;
74         int ch;
75         uint32_t is_not_empty = 0;
76         uint32_t start_time = 0,current_time = 0;
77
78         if ((buf == NULL) || (len == 0))
79                 return 0;
80
81         is_not_empty = serial_tstc();
82         if (is_not_empty) {
83              start_time = get_timer_masked();
84              do {
85                   do {
86                         ch = serial_getc();
87                         if (count < CALIBERATE_STRING_LEN)
88                                 buf[count++] = ch;
89                   } while (serial_tstc());
90
91                   if ((count >= CALIBERATE_STRING_LEN) || (count >= len)) {
92                        caliberate_device = CALIBERATE_DEVICE_UART;
93                        break;
94                   }
95
96                   current_time = get_timer_masked();
97              } while((current_time - start_time) < 500);
98         }
99
100         return count;
101 }
102
103 unsigned int check_caliberate(uint8_t * buf, int len)
104 {
105         unsigned int command = 0;
106         unsigned int freq = 0;
107
108         if (len != CALIBERATE_STRING_LEN)
109                 return 0;
110
111         if ((*buf == CALIBERATE_HEAD) && (*(buf + len -1) == CALIBERATE_HEAD)) {
112                 if ((*(buf+7) == CALIBERATE_COMMOND_T) && (*(buf + len - 2) != 0x1)) {
113                         command = *(buf + len - 2);
114                         command &= 0x7f;
115
116                         freq = *(buf + 1);
117                         freq = freq << 8;
118                         freq += *(buf + 2);
119
120                         command += freq << 8;
121                 }
122         }
123
124         return command;
125 }
126
127 int pctool_mode_detect_uart(void)
128 {
129         int i ;
130         unsigned int caliberate_mode;
131         uint8_t buf[20];
132         int got = 0;
133
134         printf("%s\n", "uart calibrate detecting");
135         send_caliberation_request();
136
137 #ifdef CONFIG_MODEM_CALIBERATE
138         for(i = 0; i < 20; i++)
139                 buf[i] = i + 'a';
140
141         start_time = get_timer_masked();
142         printf("uart calibrate configuration start_time=%llu\n", start_time);
143         while (1) {
144                 got = receive_caliberation_response(buf, sizeof(buf));
145                 if (caliberate_device == CALIBERATE_DEVICE_UART)
146                         break;
147
148                 now_time = get_timer_masked();
149                 if ((now_time - start_time) > CALIBRATE_ENUM_MS) {
150                         printf("usb calibrate configuration timeout\n");
151                         return -1;
152                 }
153         }
154
155         printf("caliberate : what got from host total %d is \n", got);
156         for (i = 0; i < got; i++)
157                 printf("0x%x ", buf[i]);
158         printf("\n");
159
160         caliberate_mode = check_caliberate(buf, CALIBERATE_STRING_LEN);
161         if (!caliberate_mode) {
162                 printf("func: %s line: %d caliberate failed\n", __func__, __LINE__);
163                 return -1;
164         } else {
165         calibration_cmd_buf=malloc(1024);
166         if(calibration_cmd_buf==NULL){
167             printf("%s: out of memory\n", __func__);
168             return -1;
169         }
170         memset(calibration_cmd_buf, 0, 1024);
171         if (caliberate_device == CALIBERATE_DEVICE_UART)
172                 sprintf(calibration_cmd_buf, " androidboot.mode=cali calibration=%d,%d,0", caliberate_mode&0xff, (caliberate_mode&(~0xff)) >> 8);
173         s_is_calibration_mode = 2;
174 #if defined(CONFIG_SC7710G2)
175         vlx_nand_boot(BOOT_PART, buf, BACKLIGHT_OFF);
176 #else
177         #if defined(BOOT_NATIVE_LINUX_MODEM)
178
179         int str_len = 0;
180         char* bootargs = CONFIG_BOOTARGS;
181         char* pos = NULL;
182         pos = strstr(bootargs, "console=");
183         if(NULL != pos)
184         {
185             str_len = pos-bootargs;
186             strncpy(&calibration_cmd_buf[0], bootargs, str_len);
187             bootargs = pos;
188             pos = strstr(bootargs, " ");
189         }
190         else
191         {
192             pos = CONFIG_BOOTARGS;
193             str_len = 0;
194         }
195         sprintf(&calibration_cmd_buf[str_len], "%s", pos);
196         str_len = strlen(calibration_cmd_buf);
197
198         if (caliberate_device == CALIBERATE_DEVICE_UART)
199             sprintf(&calibration_cmd_buf[str_len], " androidboot.mode=cali calibration=%d,%d,130 ", caliberate_mode&0xff, (caliberate_mode&(~0xff)) >> 8);
200         return CMD_CALIBRATION_MODE;
201         #else
202         return CMD_CALIBRATION_MODE;
203         #endif
204 #endif
205         }
206
207         /* nerver come to here */
208         return -1;
209 #endif
210 }
211
212 int check_pctool_cmd(uint8_t* buf, int len)
213 {
214     int command = 0;
215     unsigned int freq = 0;
216     MSG_HEAD_T* msg_head_ptr;
217     uint8_t* msg_ptr = buf + 1;
218     TOOLS_DIAG_AP_EXIT_CMD_T* msg_exit_prokey;
219     TOOLS_DIAG_AP_CMD_T* msg_enter_prokey;
220
221         if((*buf == CALIBERATE_HEAD) && (*(buf + len -1) == CALIBERATE_HEAD)){
222             msg_head_ptr = (MSG_HEAD_T*)(buf + 1);
223             switch(msg_head_ptr->type){
224                 case CALIBERATE_COMMOND_T:
225                                 command = msg_head_ptr->subtype;
226                                 command &= 0x3f;
227                     freq = *(buf+1);
228                     freq = freq<<8;
229                     freq += *(buf+2);
230                     command += freq<<8;
231                     break;
232                 case CALIBERATE_PROKEY_COMMOND_T:
233                     if(DIAG_AP_CMD_PROGRAM_KEY == *(msg_ptr + sizeof(MSG_HEAD_T))){
234                         msg_enter_prokey =  (TOOLS_DIAG_AP_CMD_T*)(msg_ptr + sizeof(MSG_HEAD_T));
235                         command = msg_enter_prokey->cmd;
236                     }
237                     else if(DIAG_AP_CMD_EXIT_PROGRAM_KEY == *(msg_ptr + sizeof(MSG_HEAD_T))){
238                         msg_exit_prokey = (TOOLS_DIAG_AP_EXIT_CMD_T*)(msg_ptr + sizeof(MSG_HEAD_T));
239                         command = msg_exit_prokey->para;
240                     }
241                             break;
242                 default:
243                     command = -1;
244                     break;
245                 }
246         }
247         printf("checked command from pc , and return value = %d \n" , command);
248         return command;
249 }
250
251 static int count_ms;
252 static unsigned long long start_time;
253 static unsigned long long now_time;
254
255 #if 0
256 extern int power_button_pressed(void);
257 static int recheck_power_button(void)
258 {
259     int cnt = 0;
260     int ret = 0;
261     do{
262         ret = power_button_pressed();
263         if(ret == 0)
264           cnt++;
265         else
266           return 1;
267
268         if(cnt>4)
269           return 0;
270         else{
271             mdelay(1);
272         }
273     }while(1);
274 }
275 #endif
276
277 int is_timeout(void)
278 {
279
280     now_time = get_timer_masked();
281
282     if(now_time - start_time>count_ms)
283         return 1;
284     else{
285         return 0;
286     }
287 }
288
289 void cali_usb_debug(uint8_t *buf)
290 {   int ret;
291         int i ;
292         for(i = 0; i<20; i++)
293         buf[i] = i+'a';
294         while(!usb_serial_configed)
295                 usb_gadget_handle_interrupts();
296         printf("USB SERIAL CONFIGED\n");
297     gs_open();
298 #if WRITE_DEBUG
299                 while(1){
300                         ret = gs_write(buf, 20);
301                         printf("func: %s waitting write done\n", __func__);
302                         if(usb_trans_status)
303                                 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
304                         usb_wait_trans_done(1);
305                         printf("func: %s readly send %d\n", __func__, ret);
306                 }
307 #else
308                 while(1){
309                         int count = 20;
310                         usb_wait_trans_done(0);
311                         if(usb_trans_status)
312                                                 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
313                         ret = gs_read(buf, &count);
314                         printf("func: %s readly read %d ret(%d)\n", __func__, count, ret);
315                         if(usb_trans_status)
316                                 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
317                         for(i = 0; i<count; i++)
318                                 printf("%c ", buf[i]);
319                         printf("\n");
320                 }
321
322 #endif
323 }
324 int cali_usb_prepare(void)
325 {
326         usb_in_cal(1);
327         if(dwc_otg_driver_init() < 0)
328                 {
329                         printf("%s\n", "dwc_otg_driver_init error");
330                         return 0;
331                 }
332         if(usb_serial_init() < 0)
333                 {
334                         printf("%s\n", "usb_serial_init error");
335                         return 0;
336                 }
337         return 1;
338 }
339 int cali_usb_enum(void)
340 {
341         int ret = 0;
342         count_ms = get_cal_enum_ms();
343         start_time = get_timer_masked();
344         while(!usb_is_configured()){
345                 ret = is_timeout();
346                 if(ret == 0)
347                         continue;
348                 else{
349                         printf("usb calibrate configuration timeout\n");
350                         return 0;
351                         }
352                 }
353         printf("USB SERIAL CONFIGED\n");
354
355         start_time = get_timer_masked();
356         count_ms = get_cal_io_ms();
357         while(!usb_is_port_open()){
358                 ret = is_timeout();
359                 if(ret == 0)
360                         continue;
361                 else{
362                         printf("usb calibrate port open timeout\n");
363                         return 0;
364                         }
365                 }
366         gs_open();
367         printf("USB SERIAL PORT OPENED\n");
368         return 1;
369 }
370 int cali_get_cmd(uint8_t *buf, int len)
371 {
372         int got = 0;
373         int count = len;
374         int ret = 0;
375         int i;
376         start_time = get_timer_masked();
377
378         while(got < len){
379                 if(usb_is_trans_done(0))
380                 {
381                         if(usb_trans_status)
382                                 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
383                         ret = gs_read(buf + got, &count);
384                         if(usb_trans_status)
385                                 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
386                         for(i=0; i<count; i++)
387                                 dprintf("0x%x \n", buf[got+i]);
388                         dprintf("\n");
389                         got+=count;
390                 }
391                 if(got<len){
392                         ret = is_timeout();
393                         if(ret == 0){
394                                 count=len-got;
395                                 continue;
396                                 }
397                         else{
398                                 printf("usb read timeout\n");
399                                 return 0;
400                                 }
401                 }else{
402                         break;
403                 }
404         }
405         printf("caliberate:what got from host total %d is \n", got);
406         for(i=0; i<got;i++)
407                 printf("0x%x ", buf[i]);
408         printf("\n");
409         return 1;
410 }
411
412 int reply_to_pctool(uint8_t* buf, int len)
413 {
414         gs_write(buf, len);
415         dprintf("func: %s waitting %d write done\n", __func__, len);
416         if(usb_trans_status)
417                 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
418         usb_wait_trans_done(1);
419         start_time = get_timer_masked();
420         count_ms = get_cal_io_ms();
421         while(!usb_port_open){
422                 usb_gadget_handle_interrupts();
423                 if(is_timeout())
424                         {
425                         printf("func: %s line %d usb trans timeout", __func__, __LINE__);
426                         return 0;
427                 }
428         }
429         return 1;
430 }
431
432 int translate_packet(char *dest,char *src,int size)
433 {
434     int i;
435     int translated_size = 0;
436
437     dest[translated_size++] = 0x7E;
438
439     for(i=0;i<size;i++){
440         if(src[i] == 0x7E){
441             dest[translated_size++] = 0x7D;
442             dest[translated_size++] = 0x5E;
443         } else if(src[i] == 0x7D) {
444             dest[translated_size++] = 0x7D;
445             dest[translated_size++] = 0x5D;
446         } else
447             dest[translated_size++] = src[i];
448     }
449     dest[translated_size++] = 0x7E;
450     return translated_size;
451 }
452
453 int prepare_reply_buf(uint8_t* buf, int status)
454 {
455     char *rsp_ptr;
456     MSG_HEAD_T* msg_head_ptr;
457     TOOLS_DIAG_AP_CNF_T* aprsp;
458         int total_len,rsplen;
459         printf("preparing reply buf");
460     if(NULL == buf){
461             printf("in function prepare_reply_buf, buf = NULL\n");
462         return 0;
463     }
464
465     msg_head_ptr = (MSG_HEAD_T*)(buf + 1);
466     rsplen = sizeof(TOOLS_DIAG_AP_CNF_T) + sizeof(MSG_HEAD_T);
467     rsp_ptr = (char*)malloc(rsplen);
468     if(NULL == rsp_ptr){
469             printf("in function prepare_reply_buf: Buffer malloc failed\n");
470             return 0;
471     }
472     aprsp = (TOOLS_DIAG_AP_CNF_T*)(rsp_ptr + sizeof(MSG_HEAD_T));
473         msg_head_ptr->len = rsplen;
474     memcpy(rsp_ptr,msg_head_ptr,sizeof(MSG_HEAD_T));
475
476     aprsp->status = status;
477     aprsp->length = CALIBERATE_CNF_LEN;
478
479     total_len = translate_packet((char*)pctool_cnf_buf,(char*)rsp_ptr,((MSG_HEAD_T*)rsp_ptr)->len);
480     free(rsp_ptr);
481     return total_len;
482 }
483
484 int pctool_mode_detect(void)
485 {
486
487         int ret , command;
488
489         printf("%s\n", "uart cooperating with pc tool");
490         if(get_mode_from_gpio())
491              return pctool_mode_detect_uart();
492
493         printf("%s\n", "usb cooperating with pc tool");
494
495         if(!charger_connected())
496                 return -1;
497
498         ret = cali_usb_prepare();
499         if (!ret)
500                 return -1;
501
502 #if IO_DEBUG
503         cali_usb_debug(pctool_cmd_buf);
504 #endif
505         ret = cali_usb_enum();
506         if (!ret)
507                 return -1;
508         ret = cali_get_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN );
509         if (!ret)
510                 return -1;
511         command = check_pctool_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN);
512         printf("func: %s caliberate_mode: %x \n",__func__, command&0xff);
513         if (!command)
514                 {
515                 printf("func: %s line: %d caliberate failed\n", __func__, __LINE__);
516                 return -1;
517                 }
518         ret = reply_to_pctool(pctool_cmd_buf, CALIBERATE_STRING_LEN);
519         if(!ret)
520                 return -1;
521 #ifdef CONFIG_SECURE_BOOT
522         if (CALIBERATE_COMMAND_PROGRAMKEY == command)
523                 {
524                         ret = cali_get_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN_14);
525                         if(!ret)
526                                 return -1;
527                         command = check_pctool_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN_14);
528                         if(!command)
529                                 return -1;
530                         else if(command = DIAG_AP_CMD_PROGRAM_KEY)
531                         {
532                                 ret = secure_efuse_program();//jiekou_from_beijing;
533                         }
534             if(!ret)
535                 ret = prepare_reply_buf(pctool_cmd_buf ,CALIBERATE_CNF_SCS);
536             else{
537                                 printk("write efuse failed and error code = %d \n" , ret);
538                 ret = prepare_reply_buf(pctool_cmd_buf , CALIBERATE_CNF_FAIL);
539             }
540                         if(ret)
541                                 ret = reply_to_pctool(pctool_cnf_buf, ret);
542             if(!ret)
543                     return -1;
544                         ret = cali_get_cmd(pctool_cmd_buf,CALIBERATE_STRING_LEN_16);
545                         if(!ret)
546                                 return -1;
547             command = check_pctool_cmd(pctool_cmd_buf , CALIBERATE_STRING_LEN_16);
548             if(!command)
549                                 return -1;
550                         switch(command){
551                                 case 0xffff:
552                                         ret = prepare_reply_buf(pctool_cmd_buf ,CALIBERATE_CNF_SCS);
553                                         ret = reply_to_pctool(pctool_cnf_buf, ret);
554                                         return CMD_POWER_DOWN_DEVICE;
555                                         break;
556                                 case 0xfffe:
557                                         ret = prepare_reply_buf(pctool_cmd_buf ,CALIBERATE_CNF_SCS);
558                                         ret = reply_to_pctool(pctool_cnf_buf, ret);
559                                         return CMD_CHARGE_MODE;
560                                         break;
561                                 default:
562                                         ret = prepare_reply_buf(pctool_cmd_buf ,CALIBERATE_CNF_SCS);
563                                         ret = reply_to_pctool(pctool_cnf_buf, ret);
564                                         break;
565                     }
566         }
567 #endif
568         calibration_cmd_buf=malloc(1024);
569        if(calibration_cmd_buf==NULL){
570            printf("%s: out of memory\n", __func__);
571            return -1;
572        }
573         memset(calibration_cmd_buf, 0, 1024);
574     s_is_calibration_mode=1;
575         switch (command & 0xff){
576                 case CALIBERATE_COMMAND_AUTOTEST:
577                         sprintf(calibration_cmd_buf, CONFIG_BOOTARGS" androidboot.mode=engtest autotest=1");
578                         ret = CMD_AUTOTEST_MODE;
579                         udc_power_off();
580                         break;
581                 default:
582                         sprintf(calibration_cmd_buf,CONFIG_BOOTARGS" androidboot.mode=cali calibration=%d,%d,146 \n", command&0xff, (command&(~0xff))>>8);
583                         ret = CMD_CALIBRATION_MODE;
584                         udc_power_off();
585                         break;
586     }
587         return ret;
588 }
589
590
591 int poweron_by_calibration(void)
592 {
593         return s_is_calibration_mode;
594 }
595
596 int cali_file_check(void)
597 {
598
599 #define CALI_MAGIC      (0x49424143) //CALI
600 #define CALI_COMP       (0x504D4F43) //COMP
601
602
603         if(do_fs_file_read("prodnv", "/adc.bin", (char *)nv_buffer,sizeof(nv_buffer)))
604                 return 1;
605
606         if((nv_buffer[0] != CALI_MAGIC)||(nv_buffer[1]!=CALI_COMP))
607                 return 1;
608         else
609                 return 0;
610 }
611
612 #ifndef CONFIG_AP_ADC_CALIBRATION
613 #if defined(CONFIG_EMMC_BOOT) && defined (CONFIG_SC8825)
614 #include "calibration_nv_struct.h"
615
616 #define VLX_ADC_ID   2
617 #define VLX_RAND_TO_U32( _addr )        if( (_addr) & 0x3 ){_addr += 0x4 -((_addr) & 0x3); }
618
619 u32 Vlx_GetFixedNvitemAddr(u16 identifier, u32 search_start, u32 search_end)
620 {
621         u32 start_addr, end_addr;
622         u16 id, len;
623         volatile u16 *flash_ptr;
624
625         start_addr = search_start;
626         end_addr   = search_end;
627         start_addr += sizeof(u32); //skip update flag
628
629         while(start_addr < end_addr)
630         {
631                 flash_ptr = (volatile u16 *)(start_addr);
632                 id  = *flash_ptr++;
633                 len = *flash_ptr;
634                 if(0xFFFF == id)
635                 {
636                         return 0xFFFFFFFF;
637                 }
638                 if(identifier == id)
639                 {
640                         return (start_addr + 4);
641                 }
642                 else
643                 {
644                         start_addr += 4 + len +(len & 0x1);
645                         VLX_RAND_TO_U32( start_addr )
646                 }
647         }
648         return 0xFFFFFFFF;
649 }
650 #endif
651
652 int read_adc_calibration_data(char *buffer,int size)
653 {
654 #if  defined (CONFIG_SC8830) || defined (CONFIG_SC9630)
655 #if 0
656         if(do_fs_file_read("prodnv", "/adc.bin", (char *)nv_buffer,sizeof(nv_buffer)))
657                 return 0;
658 #endif
659         if(size>48)
660                 size=48;
661         memcpy(buffer,&nv_buffer[2],size);
662         return size;
663 #elif defined(CONFIG_EMMC_BOOT) && defined (CONFIG_SC8825)
664         #define FIXNV_ADR        0x80480000
665         calibration_param_T *calibration_base;
666         u32 item_base;
667         uint16 *value = (uint16 *)(&buffer[8]);
668         item_base = Vlx_GetFixedNvitemAddr(VLX_ADC_ID, FIXNV_ADR, (FIXNV_ADR+FIXNV_SIZE));
669         if(item_base == 0xFFFFFFFF)
670                 return 0;
671         calibration_base = (calibration_param_T *)item_base;
672         if(!((calibration_base->adc).reserved[7] & (BIT_9)))
673                 return 0;
674
675         value[0] = ((calibration_base->adc).battery[0]) & 0xFFFF;
676         value[1] = ((calibration_base->adc).battery[0] >> 16 ) & 0xFFFF;
677         value[2] = ((calibration_base->adc).battery[1]) & 0xFFFF;
678         value[3] = ((calibration_base->adc).battery[1] >> 16 ) & 0xFFFF;
679         return size;
680 #endif
681         return 0;
682 }
683 #endif