1 #include "calibration_detect.h"
4 static unsigned int nv_buffer[256]={0};
5 static int s_is_calibration_mode = 0;
6 char *calibration_cmd_buf;
7 uint8_t pctool_cmd_buf[20];
8 uint8_t pctool_cnf_buf[20];
10 char *get_calibration_parameter(void)
12 if(s_is_calibration_mode != 0)
13 return calibration_cmd_buf;
18 bool is_calibration_by_uart(void)
20 return (2 == s_is_calibration_mode);
23 #define mdelay(_ms) udelay(_ms*1000)
24 #define CALIBERATE_STRING_LEN 10
25 #define CALIBERATE_HEAD 0x7e
26 #define CALIBERATE_COMMOND_T 0xfe
27 #define CALIBERATE_COMMAND_REQ 1
29 #define CALIBERATE_DEVICE_NULL 0
30 #define CALIBERATE_DEVICE_USB 1
31 #define CALIBERATE_DEVICE_UART 2
33 extern int charger_connected(void);
34 typedef struct tag_cali_command {
35 unsigned int reserved;
38 unsigned char sub_cmd;
41 extern int serial_tstc(void);
42 static unsigned long long start_time;
43 static unsigned long long now_time;
47 static caliberate_device = CALIBERATE_DEVICE_NULL;
49 static void send_caliberation_request(void)
53 unsigned char *data = (unsigned char *)&cmd;
56 cmd.cmd = CALIBERATE_COMMOND_T;
57 cmd.size = CALIBERATE_STRING_LEN-2;
58 cmd.sub_cmd = CALIBERATE_COMMAND_REQ;
60 serial_putc(CALIBERATE_HEAD);
62 for (i = 0; i < sizeof(COMMAND_T); i++)
65 serial_putc(CALIBERATE_HEAD);
68 static int receive_caliberation_response(uint8_t *buf,int len)
72 uint32_t is_not_empty = 0;
73 uint32_t start_time = 0,current_time = 0;
75 if ((buf == NULL) || (len == 0))
78 is_not_empty = serial_tstc();
80 start_time = get_timer_masked();
84 if (count < CALIBERATE_STRING_LEN)
86 } while (serial_tstc());
88 if ((count >= CALIBERATE_STRING_LEN) || (count >= len)) {
89 caliberate_device = CALIBERATE_DEVICE_UART;
93 current_time = get_timer_masked();
94 } while((current_time - start_time) < 500);
100 unsigned int check_caliberate(uint8_t * buf, int len)
102 unsigned int command = 0;
103 unsigned int freq = 0;
105 if (len != CALIBERATE_STRING_LEN)
108 if ((*buf == CALIBERATE_HEAD) && (*(buf + len -1) == CALIBERATE_HEAD)) {
109 if ((*(buf+7) == CALIBERATE_COMMOND_T) && (*(buf + len - 2) != 0x1)) {
110 command = *(buf + len - 2);
117 command += freq << 8;
124 int pctool_mode_detect_uart(void)
128 unsigned int caliberate_mode;
132 printf("%s\n", "uart calibrate detecting");
134 send_caliberation_request();
136 #ifdef CONFIG_MODEM_CALIBERATE
137 for(i = 0; i < 20; i++)
140 start_time = get_timer_masked();
141 printf("uart calibrate configuration start_time=%d\n", start_time);
143 got = receive_caliberation_response(buf, sizeof(buf));
144 if (caliberate_device == CALIBERATE_DEVICE_UART)
147 now_time = get_timer_masked();
148 if ((now_time - start_time) > CALIBRATE_ENUM_MS) {
149 printf("usb calibrate configuration timeout\n");
154 printf("caliberate : what got from host total %d is \n", got);
155 for (i = 0; i < got; i++)
156 printf("0x%x ", buf[i]);
159 caliberate_mode = check_caliberate(buf, CALIBERATE_STRING_LEN);
160 if (!caliberate_mode) {
161 printf("func: %s line: %d caliberate failed\n", __func__, __LINE__);
164 calibration_cmd_buf=malloc(1024);
165 if(calibration_cmd_buf==NULL){
166 printf("%s: out of memory\n", __func__);
169 memset(calibration_cmd_buf, 0, 1024);
170 if (caliberate_device == CALIBERATE_DEVICE_UART)
171 sprintf(calibration_cmd_buf, " androidboot.mode=cali calibration=%d,%d,0", caliberate_mode&0xff, (caliberate_mode&(~0xff)) >> 8);
172 s_is_calibration_mode = 2;
173 #if defined(CONFIG_SC7710G2)
174 vlx_nand_boot(BOOT_PART, buf, BACKLIGHT_OFF);
176 #if defined(BOOT_NATIVE_LINUX_MODEM)
179 char* bootargs = CONFIG_BOOTARGS;
181 pos = strstr(bootargs, "console=");
184 str_len = pos-bootargs;
185 strncpy(&calibration_cmd_buf[0], bootargs, str_len);
187 pos = strstr(bootargs, " ");
191 pos = CONFIG_BOOTARGS;
194 sprintf(&calibration_cmd_buf[str_len], "%s", pos);
195 str_len = strlen(calibration_cmd_buf);
197 if (caliberate_device == CALIBERATE_DEVICE_UART)
198 sprintf(&calibration_cmd_buf[str_len], " androidboot.mode=cali calibration=%d,%d,130 ", caliberate_mode&0xff, (caliberate_mode&(~0xff)) >> 8);
199 return CMD_CALIBRATION_MODE;
201 return CMD_CALIBRATION_MODE;
206 /* nerver come to here */
211 int check_pctool_cmd(uint8_t* buf, int len)
214 unsigned int freq = 0;
215 MSG_HEAD_T* msg_head_ptr;
216 uint8_t* msg_ptr = buf + 1;
217 TOOLS_DIAG_AP_EXIT_CMD_T* msg_exit_prokey;
218 TOOLS_DIAG_AP_CMD_T* msg_enter_prokey;
220 if((*buf == CALIBERATE_HEAD) && (*(buf + len -1) == CALIBERATE_HEAD)){
221 msg_head_ptr = (MSG_HEAD_T*)(buf + 1);
222 switch(msg_head_ptr->type){
223 case CALIBERATE_COMMOND_T:
224 command = msg_head_ptr->subtype;
231 case CALIBERATE_PROKEY_COMMOND_T:
232 if(DIAG_AP_CMD_PROGRAM_KEY == *(msg_ptr + sizeof(MSG_HEAD_T))){
233 msg_enter_prokey = (TOOLS_DIAG_AP_CMD_T*)(msg_ptr + sizeof(MSG_HEAD_T));
234 command = msg_enter_prokey->cmd;
236 else if(DIAG_AP_CMD_EXIT_PROGRAM_KEY == *(msg_ptr + sizeof(MSG_HEAD_T))){
237 msg_exit_prokey = (TOOLS_DIAG_AP_EXIT_CMD_T*)(msg_ptr + sizeof(MSG_HEAD_T));
238 command = msg_exit_prokey->para;
246 printf("checked command from pc , and return value = %d \n" , command);
250 extern int power_button_pressed(void);
252 static unsigned long long start_time;
253 static unsigned long long now_time;
255 static int recheck_power_button(void)
260 ret = power_button_pressed();
276 now_time = get_timer_masked();
278 if(now_time - start_time>count_ms)
285 void cali_usb_debug(uint8_t *buf)
288 for(i = 0; i<20; i++)
290 while(!usb_serial_configed)
291 usb_gadget_handle_interrupts();
292 printf("USB SERIAL CONFIGED\n");
296 ret = gs_write(buf, 20);
297 printf("func: %s waitting write done\n", __func__);
299 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
300 usb_wait_trans_done(1);
301 printf("func: %s readly send %d\n", __func__, ret);
306 usb_wait_trans_done(0);
308 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
309 ret = gs_read(buf, &count);
310 printf("func: %s readly read %d\n", __func__, count);
312 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
313 for(i = 0; i<count; i++)
314 printf("%c ", buf[i]);
320 int cali_usb_prepare()
324 if(dwc_otg_driver_init() < 0)
326 printf("%s\n", "dwc_otg_driver_init error");
329 if(usb_serial_init() < 0)
331 printf("%s\n", "usb_serial_init error");
339 count_ms = get_cal_enum_ms();
340 start_time = get_timer_masked();
341 while(!usb_is_configured()){
346 printf("usb calibrate configuration timeout\n");
350 printf("USB SERIAL CONFIGED\n");
352 start_time = get_timer_masked();
353 count_ms = get_cal_io_ms();
354 while(!usb_is_port_open()){
359 printf("usb calibrate port open timeout\n");
364 printf("USB SERIAL PORT OPENED\n");
367 int cali_get_cmd(uint8_t *buf, int len)
373 start_time = get_timer_masked();
376 if(usb_is_trans_done(0))
379 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
380 ret = gs_read(buf + got, &count);
382 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
383 for(i=0; i<count; i++)
384 dprintf("0x%x \n", buf[got+i]);
395 printf("usb read timeout\n");
402 printf("caliberate:what got from host total %d is \n", got);
404 printf("0x%x ", buf[i]);
409 int reply_to_pctool(uint8_t* buf, int len)
412 dprintf("func: %s waitting %d write done\n", __func__, len);
414 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
415 usb_wait_trans_done(1);
416 start_time = get_timer_masked();
417 count_ms = get_cal_io_ms();
418 while(!usb_port_open){
419 usb_gadget_handle_interrupts();
422 printf("func: %s line %d usb trans timeout", __func__, __LINE__);
429 int translate_packet(char *dest,char *src,int size)
432 int translated_size = 0;
434 dest[translated_size++] = 0x7E;
438 dest[translated_size++] = 0x7D;
439 dest[translated_size++] = 0x5E;
440 } else if(src[i] == 0x7D) {
441 dest[translated_size++] = 0x7D;
442 dest[translated_size++] = 0x5D;
444 dest[translated_size++] = src[i];
446 dest[translated_size++] = 0x7E;
447 return translated_size;
450 int prepare_reply_buf(uint8_t* buf, int status)
454 MSG_HEAD_T* msg_head_ptr;
455 TOOLS_DIAG_AP_CNF_T* aprsp;
456 int total_len,rsplen;
457 printf("preparing reply buf");
459 printf("in function prepare_reply_buf, buf = NULL\n");
463 msg_head_ptr = (MSG_HEAD_T*)(buf + 1);
464 rsplen = sizeof(TOOLS_DIAG_AP_CNF_T) + sizeof(MSG_HEAD_T);
465 rsp_ptr = (char*)malloc(rsplen);
467 printf("in function prepare_reply_buf: Buffer malloc failed\n");
470 aprsp = (TOOLS_DIAG_AP_CNF_T*)(rsp_ptr + sizeof(MSG_HEAD_T));
471 msg_head_ptr->len = rsplen;
472 memcpy(rsp_ptr,msg_head_ptr,sizeof(MSG_HEAD_T));
474 aprsp->status = status;
475 aprsp->length = CALIBERATE_CNF_LEN;
477 total_len = translate_packet((unsigned char*)pctool_cnf_buf,(unsigned char*)rsp_ptr,((MSG_HEAD_T*)rsp_ptr)->len);
482 int pctool_mode_detect(void)
486 unsigned int caliberate_mode;
488 printf("%s\n", "uart cooperating with pc tool");
489 if(get_mode_from_gpio())
490 return pctool_mode_detect_uart();
492 printf("%s\n", "usb cooperating with pc tool");
494 if(!charger_connected())
497 ret = cali_usb_prepare();
502 cali_usb_debug(pctool_cmd_buf);
504 ret = cali_usb_enum();
507 ret = cali_get_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN );
510 command = check_pctool_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN);
511 printf("func: %s caliberate_mode: %x \n",__func__, command&0xff);
514 printf("func: %s line: %d caliberate failed\n", __func__, __LINE__);
517 ret = reply_to_pctool(pctool_cmd_buf, CALIBERATE_STRING_LEN);
520 #ifdef CONFIG_SECURE_BOOT
521 if (CALIBERATE_COMMAND_PROGRAMKEY == command)
523 ret = cali_get_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN_14);
526 command = check_pctool_cmd(pctool_cmd_buf, CALIBERATE_STRING_LEN_14);
529 else if(command = DIAG_AP_CMD_PROGRAM_KEY)
531 ret = secure_efuse_program();//jiekou_from_beijing;
534 ret = prepare_reply_buf(pctool_cmd_buf ,CALIBERATE_CNF_SCS);
536 printk("write efuse failed and error code = %d \n" , ret);
537 ret = prepare_reply_buf(pctool_cmd_buf , CALIBERATE_CNF_FAIL);
540 ret = reply_to_pctool(pctool_cnf_buf, ret);
543 ret = cali_get_cmd(pctool_cmd_buf,CALIBERATE_STRING_LEN_16);
546 command = check_pctool_cmd(pctool_cmd_buf , CALIBERATE_STRING_LEN_16);
551 ret = prepare_reply_buf(pctool_cmd_buf ,CALIBERATE_CNF_SCS);
552 ret = reply_to_pctool(pctool_cnf_buf, ret);
553 return CMD_POWER_DOWN_DEVICE;
556 ret = prepare_reply_buf(pctool_cmd_buf ,CALIBERATE_CNF_SCS);
557 ret = reply_to_pctool(pctool_cnf_buf, ret);
558 return CMD_CHARGE_MODE;
561 ret = prepare_reply_buf(pctool_cmd_buf ,CALIBERATE_CNF_SCS);
562 ret = reply_to_pctool(pctool_cnf_buf, ret);
567 calibration_cmd_buf=malloc(1024);
568 if(calibration_cmd_buf==NULL){
569 printf("%s: out of memory\n", __func__);
572 memset(calibration_cmd_buf, 0, 1024);
573 s_is_calibration_mode=1;
574 switch (command & 0xff){
575 case CALIBERATE_COMMAND_AUTOTEST:
576 sprintf(calibration_cmd_buf, CONFIG_BOOTARGS" androidboot.mode=engtest autotest=1");
577 ret = CMD_AUTOTEST_MODE;
581 sprintf(calibration_cmd_buf,CONFIG_BOOTARGS" androidboot.mode=cali calibration=%d,%d,146 \n", command&0xff, (command&(~0xff))>>8);
582 ret = CMD_CALIBRATION_MODE;
590 int poweron_by_calibration(void)
592 return s_is_calibration_mode;
595 int cali_file_check(void)
598 #define CALI_MAGIC (0x49424143) //CALI
599 #define CALI_COMP (0x504D4F43) //COMP
602 if(do_fs_file_read("prodnv", "/adc.bin", (char *)nv_buffer,sizeof(nv_buffer)))
605 if((nv_buffer[0] != CALI_MAGIC)||(nv_buffer[1]!=CALI_COMP))
611 #ifndef CONFIG_AP_ADC_CALIBRATION
612 #if defined(CONFIG_EMMC_BOOT) && defined (CONFIG_SC8825)
613 #include "calibration_nv_struct.h"
616 #define VLX_RAND_TO_U32( _addr ) if( (_addr) & 0x3 ){_addr += 0x4 -((_addr) & 0x3); }
618 u32 Vlx_GetFixedNvitemAddr(u16 identifier, u32 search_start, u32 search_end)
620 u32 start_addr, end_addr;
622 volatile u16 *flash_ptr;
624 start_addr = search_start;
625 end_addr = search_end;
626 start_addr += sizeof(u32); //skip update flag
628 while(start_addr < end_addr)
630 flash_ptr = (volatile u16 *)(start_addr);
639 return (start_addr + 4);
643 start_addr += 4 + len +(len & 0x1);
644 VLX_RAND_TO_U32( start_addr )
651 int read_adc_calibration_data(char *buffer,int size)
653 #if defined (CONFIG_SC8830) || defined (CONFIG_SC9630)
655 if(do_fs_file_read("prodnv", "/adc.bin", (char *)nv_buffer,sizeof(nv_buffer)))
660 memcpy(buffer,&nv_buffer[2],size);
662 #elif defined(CONFIG_EMMC_BOOT) && defined (CONFIG_SC8825)
663 #define FIXNV_ADR 0x80480000
664 calibration_param_T *calibration_base;
666 uint16 *value = (uint16 *)(&buffer[8]);
667 item_base = Vlx_GetFixedNvitemAddr(VLX_ADC_ID, FIXNV_ADR, (FIXNV_ADR+FIXNV_SIZE));
668 if(item_base == 0xFFFFFFFF)
670 calibration_base = (calibration_param_T *)item_base;
671 if(!((calibration_base->adc).reserved[7] & (BIT_9)))
674 value[0] = ((calibration_base->adc).battery[0]) & 0xFFFF;
675 value[1] = ((calibration_base->adc).battery[0] >> 16 ) & 0xFFFF;
676 value[2] = ((calibration_base->adc).battery[1]) & 0xFFFF;
677 value[3] = ((calibration_base->adc).battery[1] >> 16 ) & 0xFFFF;