1 #include "calibration_detect.h"
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];
11 char *get_calibration_parameter(void)
13 if(s_is_calibration_mode != 0)
14 return calibration_cmd_buf;
19 bool is_calibration_by_uart(void)
21 return (2 == s_is_calibration_mode);
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
30 #define CALIBERATE_DEVICE_NULL 0
31 #define CALIBERATE_DEVICE_USB 1
32 #define CALIBERATE_DEVICE_UART 2
34 extern int charger_connected(void);
35 typedef struct tag_cali_command {
36 unsigned int reserved;
39 unsigned char sub_cmd;
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;
50 static int caliberate_device = CALIBERATE_DEVICE_NULL;
52 static void send_caliberation_request(void)
56 unsigned char *data = (unsigned char *)&cmd;
59 cmd.cmd = CALIBERATE_COMMOND_T;
60 cmd.size = CALIBERATE_STRING_LEN-2;
61 cmd.sub_cmd = CALIBERATE_COMMAND_REQ;
63 serial_putc(CALIBERATE_HEAD);
65 for (i = 0; i < sizeof(COMMAND_T); i++)
68 serial_putc(CALIBERATE_HEAD);
71 static int receive_caliberation_response(uint8_t *buf,int len)
75 uint32_t is_not_empty = 0;
76 uint32_t start_time = 0,current_time = 0;
78 if ((buf == NULL) || (len == 0))
81 is_not_empty = serial_tstc();
83 start_time = get_timer_masked();
87 if (count < CALIBERATE_STRING_LEN)
89 } while (serial_tstc());
91 if ((count >= CALIBERATE_STRING_LEN) || (count >= len)) {
92 caliberate_device = CALIBERATE_DEVICE_UART;
96 current_time = get_timer_masked();
97 } while((current_time - start_time) < 500);
103 unsigned int check_caliberate(uint8_t * buf, int len)
105 unsigned int command = 0;
106 unsigned int freq = 0;
108 if (len != CALIBERATE_STRING_LEN)
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);
120 command += freq << 8;
127 int pctool_mode_detect_uart(void)
130 unsigned int caliberate_mode;
134 printf("%s\n", "uart calibrate detecting");
135 send_caliberation_request();
137 #ifdef CONFIG_MODEM_CALIBERATE
138 for(i = 0; i < 20; i++)
141 start_time = get_timer_masked();
142 printf("uart calibrate configuration start_time=%llu\n", start_time);
144 got = receive_caliberation_response(buf, sizeof(buf));
145 if (caliberate_device == CALIBERATE_DEVICE_UART)
148 now_time = get_timer_masked();
149 if ((now_time - start_time) > CALIBRATE_ENUM_MS) {
150 printf("usb calibrate configuration timeout\n");
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]);
160 caliberate_mode = check_caliberate(buf, CALIBERATE_STRING_LEN);
161 if (!caliberate_mode) {
162 printf("func: %s line: %d caliberate failed\n", __func__, __LINE__);
165 calibration_cmd_buf=malloc(1024);
166 if(calibration_cmd_buf==NULL){
167 printf("%s: out of memory\n", __func__);
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);
177 #if defined(BOOT_NATIVE_LINUX_MODEM)
180 char* bootargs = CONFIG_BOOTARGS;
182 pos = strstr(bootargs, "console=");
185 str_len = pos-bootargs;
186 strncpy(&calibration_cmd_buf[0], bootargs, str_len);
188 pos = strstr(bootargs, " ");
192 pos = CONFIG_BOOTARGS;
195 sprintf(&calibration_cmd_buf[str_len], "%s", pos);
196 str_len = strlen(calibration_cmd_buf);
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;
202 return CMD_CALIBRATION_MODE;
207 /* nerver come to here */
212 int check_pctool_cmd(uint8_t* buf, int len)
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;
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;
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;
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;
247 printf("checked command from pc , and return value = %d \n" , command);
252 static unsigned long long start_time;
253 static unsigned long long now_time;
256 extern int power_button_pressed(void);
257 static int recheck_power_button(void)
262 ret = power_button_pressed();
280 now_time = get_timer_masked();
282 if(now_time - start_time>count_ms)
289 void cali_usb_debug(uint8_t *buf)
292 for(i = 0; i<20; i++)
294 while(!usb_serial_configed)
295 usb_gadget_handle_interrupts();
296 printf("USB SERIAL CONFIGED\n");
300 ret = gs_write(buf, 20);
301 printf("func: %s waitting write done\n", __func__);
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);
310 usb_wait_trans_done(0);
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);
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]);
324 int cali_usb_prepare(void)
327 if(dwc_otg_driver_init() < 0)
329 printf("%s\n", "dwc_otg_driver_init error");
332 if(usb_serial_init() < 0)
334 printf("%s\n", "usb_serial_init error");
339 int cali_usb_enum(void)
342 count_ms = get_cal_enum_ms();
343 start_time = get_timer_masked();
344 while(!usb_is_configured()){
349 printf("usb calibrate configuration timeout\n");
353 printf("USB SERIAL CONFIGED\n");
355 start_time = get_timer_masked();
356 count_ms = get_cal_io_ms();
357 while(!usb_is_port_open()){
362 printf("usb calibrate port open timeout\n");
367 printf("USB SERIAL PORT OPENED\n");
370 int cali_get_cmd(uint8_t *buf, int len)
376 start_time = get_timer_masked();
379 if(usb_is_trans_done(0))
382 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
383 ret = gs_read(buf + got, &count);
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]);
398 printf("usb read timeout\n");
405 printf("caliberate:what got from host total %d is \n", got);
407 printf("0x%x ", buf[i]);
412 int reply_to_pctool(uint8_t* buf, int len)
415 dprintf("func: %s waitting %d write done\n", __func__, len);
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();
425 printf("func: %s line %d usb trans timeout", __func__, __LINE__);
432 int translate_packet(char *dest,char *src,int size)
435 int translated_size = 0;
437 dest[translated_size++] = 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;
447 dest[translated_size++] = src[i];
449 dest[translated_size++] = 0x7E;
450 return translated_size;
453 int prepare_reply_buf(uint8_t* buf, int status)
456 MSG_HEAD_T* msg_head_ptr;
457 TOOLS_DIAG_AP_CNF_T* aprsp;
458 int total_len,rsplen;
459 printf("preparing reply buf");
461 printf("in function prepare_reply_buf, buf = NULL\n");
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);
469 printf("in function prepare_reply_buf: Buffer malloc failed\n");
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));
476 aprsp->status = status;
477 aprsp->length = CALIBERATE_CNF_LEN;
479 total_len = translate_packet((char*)pctool_cnf_buf,(char*)rsp_ptr,((MSG_HEAD_T*)rsp_ptr)->len);
484 int pctool_mode_detect(void)
489 printf("%s\n", "uart cooperating with pc tool");
490 if(get_mode_from_gpio())
491 return pctool_mode_detect_uart();
493 printf("%s\n", "usb cooperating with pc tool");
495 if(!charger_connected())
498 ret = cali_usb_prepare();
503 cali_usb_debug(pctool_cmd_buf);
505 ret = cali_usb_enum();
508 ret = cali_get_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN );
511 command = check_pctool_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN);
512 printf("func: %s caliberate_mode: %x \n",__func__, command&0xff);
515 printf("func: %s line: %d caliberate failed\n", __func__, __LINE__);
518 ret = reply_to_pctool(pctool_cmd_buf, CALIBERATE_STRING_LEN);
521 #ifdef CONFIG_SECURE_BOOT
522 if (CALIBERATE_COMMAND_PROGRAMKEY == command)
524 ret = cali_get_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN_14);
527 command = check_pctool_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN_14);
530 else if(command = DIAG_AP_CMD_PROGRAM_KEY)
532 ret = secure_efuse_program();//jiekou_from_beijing;
535 ret = prepare_reply_buf(pctool_cmd_buf ,CALIBERATE_CNF_SCS);
537 printk("write efuse failed and error code = %d \n" , ret);
538 ret = prepare_reply_buf(pctool_cmd_buf , CALIBERATE_CNF_FAIL);
541 ret = reply_to_pctool(pctool_cnf_buf, ret);
544 ret = cali_get_cmd(pctool_cmd_buf,CALIBERATE_STRING_LEN_16);
547 command = check_pctool_cmd(pctool_cmd_buf , CALIBERATE_STRING_LEN_16);
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;
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;
562 ret = prepare_reply_buf(pctool_cmd_buf ,CALIBERATE_CNF_SCS);
563 ret = reply_to_pctool(pctool_cnf_buf, ret);
568 calibration_cmd_buf=malloc(1024);
569 if(calibration_cmd_buf==NULL){
570 printf("%s: out of memory\n", __func__);
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;
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;
591 int poweron_by_calibration(void)
593 return s_is_calibration_mode;
596 int cali_file_check(void)
599 #define CALI_MAGIC (0x49424143) //CALI
600 #define CALI_COMP (0x504D4F43) //COMP
603 if(do_fs_file_read("prodnv", "/adc.bin", (char *)nv_buffer,sizeof(nv_buffer)))
606 if((nv_buffer[0] != CALI_MAGIC)||(nv_buffer[1]!=CALI_COMP))
612 #ifndef CONFIG_AP_ADC_CALIBRATION
613 #if defined(CONFIG_EMMC_BOOT) && defined (CONFIG_SC8825)
614 #include "calibration_nv_struct.h"
617 #define VLX_RAND_TO_U32( _addr ) if( (_addr) & 0x3 ){_addr += 0x4 -((_addr) & 0x3); }
619 u32 Vlx_GetFixedNvitemAddr(u16 identifier, u32 search_start, u32 search_end)
621 u32 start_addr, end_addr;
623 volatile u16 *flash_ptr;
625 start_addr = search_start;
626 end_addr = search_end;
627 start_addr += sizeof(u32); //skip update flag
629 while(start_addr < end_addr)
631 flash_ptr = (volatile u16 *)(start_addr);
640 return (start_addr + 4);
644 start_addr += 4 + len +(len & 0x1);
645 VLX_RAND_TO_U32( start_addr )
652 int read_adc_calibration_data(char *buffer,int size)
654 #if defined (CONFIG_SC8830) || defined (CONFIG_SC9630)
656 if(do_fs_file_read("prodnv", "/adc.bin", (char *)nv_buffer,sizeof(nv_buffer)))
661 memcpy(buffer,&nv_buffer[2],size);
663 #elif defined(CONFIG_EMMC_BOOT) && defined (CONFIG_SC8825)
664 #define FIXNV_ADR 0x80480000
665 calibration_param_T *calibration_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)
671 calibration_base = (calibration_param_T *)item_base;
672 if(!((calibration_base->adc).reserved[7] & (BIT_9)))
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;