3 #include <linux/types.h>
\r
4 #include <asm/arch/bits.h>
\r
5 #include <linux/string.h>
\r
6 #include <android_bootimg.h>
\r
7 #include <linux/mtd/mtd.h>
\r
8 #include <linux/mtd/nand.h>
\r
10 #include <android_boot.h>
\r
11 #include <environment.h>
\r
12 #include "asm/arch/cmddef.h"
\r
13 #include "asm/arch/sci_types.h"
\r
15 #ifdef CONFIG_EMMC_BOOT
\r
17 #include "../disk/part_uefi.h"
\r
18 extern int Calibration_read_partition(block_dev_desc_t *p_block_dev, wchar_t* partition_name, char *buf, int len);
\r
19 extern int Calibration_write_partition(block_dev_desc_t *p_block_dev, wchar_t* partition_name, char *buf, int len);
\r
20 static unsigned int nv_buffer[256]={0};
\r
21 static nv_read_flag = 0;
\r
24 extern int nand_part_write(char *partname,char *buffer, int size);
\r
25 extern int nand_part_read(char *partname,char *buffer, int size);
\r
26 #ifdef CONFIG_AP_ADC_CALIBRATION
\r
28 #define AP_ADC_CALIB 1
\r
29 #define AP_ADC_LOAD 2
\r
30 #define AP_ADC_SAVE 3
\r
31 #define AP_GET_VOLT 4
\r
33 #define PRECISION_1MV (1<<24)
\r
34 #define PRECISION_10MV (0)
\r
35 #define MAX_VOLTAGE (0xFFFFFF)
\r
39 uint32 adc[2]; // calibration of ADC, two test point
\r
40 uint32 battery[2]; // calibraton of battery(include resistance), two test point
\r
41 uint32 reserved[8]; // reserved for feature use.
\r
46 MSG_HEAD_T msg_head;
\r
47 TOOLS_DIAG_AP_CNF_T diag_ap_cnf;
\r
48 TOOLS_AP_ADC_REQ_T ap_adc_req;
\r
50 static unsigned char g_usb_buf_dest[8*1024];
\r
51 static int power_off_Flag = 0; //add by kenyliu in 2013 06 20 for bug 146310
\r
53 static int AccessADCDataFile(uint8 flag, char *lpBuff, int size)
\r
55 #ifdef CONFIG_EMMC_BOOT
\r
56 block_dev_desc_t *p_block_dev = NULL;
\r
57 p_block_dev = get_dev("mmc", 1);
\r
58 if(NULL == p_block_dev){
\r
61 if(nv_read_flag == 0){
\r
62 if(do_fs_file_read("prodnv", "/adc.bin", (char *)nv_buffer,sizeof(nv_buffer)))
\r
66 printf("EMC_Read:nv_buffer[255]=0x%x \n",nv_buffer[255]);
\r
67 if(nv_read_flag == 1){
\r
69 if(nv_buffer[255] != 0x5a5a5a5a)
\r
71 memcpy(lpBuff,&nv_buffer[0],size);
\r
73 nv_buffer[255] = 0x5a5a5a5a;
\r
74 memcpy(&nv_buffer[0],lpBuff,size);
\r
75 if(ext4_write_content(1, L"prodnv", "/adc.bin", (char *)nv_buffer, 0, sizeof(nv_buffer)))
\r
81 char *file_partition = "modem";
\r
84 if (flag == 0) // read ADC data
\r
86 nand_part_read(file_partition,lpBuff,size);
\r
89 else if (flag == 1) //write ADC data
\r
92 nand_part_write(file_partition,lpBuff,size);
\r
103 /*copy from packet.c and modify*/
\r
104 static int untranslate_packet_header(char *dest,char *src,int size, int unpackSize)
\r
107 int translated_size = 0;
\r
110 for(i=0;i<size;i++){
\r
120 dest[translated_size++] = src[i];
\r
126 unsigned short crc;
\r
127 crc = crc_16_l_calc((char const *)dest,translated_size-2);
\r
128 return translated_size;
\r
131 if((dest[translated_size-1] == 0x7D)&&(!flag))
\r
136 dest[translated_size-1] = 0x7E;
\r
138 else if(src[i] == 0x5D)
\r
140 dest[translated_size-1] = 0x7D;
\r
146 dest[translated_size++] = src[i];
\r
149 if (translated_size >= unpackSize+1 && unpackSize != -1)
\r
151 return translated_size;
\r
158 return translated_size;
\r
161 static int translate_packet(char *dest,char *src,int size)
\r
164 int translated_size = 0;
\r
166 dest[translated_size++] = 0x7E;
\r
168 for(i=0;i<size;i++){
\r
169 if(src[i] == 0x7E){
\r
170 dest[translated_size++] = 0x7D;
\r
171 dest[translated_size++] = 0x5E;
\r
172 } else if(src[i] == 0x7D) {
\r
173 dest[translated_size++] = 0x7D;
\r
174 dest[translated_size++] = 0x5D;
\r
176 dest[translated_size++] = src[i];
\r
178 dest[translated_size++] = 0x7E;
\r
179 return translated_size;
\r
182 static uint8 is_adc_calibration(char *dest, int destSize, char *src,int srcSize)
\r
184 int translated_size = 0;
\r
185 int msghead_size = sizeof(MSG_HEAD_T);
\r
187 memset(dest, 0, destSize);
\r
188 translated_size = untranslate_packet_header(dest, src, srcSize, msghead_size);
\r
189 if (translated_size >= msghead_size )
\r
191 MSG_HEAD_T* lpHeader = (MSG_HEAD_T *)dest;
\r
192 if (DIAG_AP_F == lpHeader->type)
\r
194 TOOLS_DIAG_AP_CMD_T *lpAPCmd =(TOOLS_DIAG_AP_CMD_T *)(lpHeader+1);
\r
195 memset(dest, 0, destSize);
\r
196 translated_size = untranslate_packet_header(dest, src, srcSize, -1);
\r
198 switch (lpAPCmd->cmd)
\r
200 case DIAG_AP_CMD_ADC:
\r
202 TOOLS_AP_ADC_REQ_T *lpAPADCReq =(TOOLS_AP_ADC_REQ_T *)(lpAPCmd+1);
\r
203 if (lpAPADCReq->operate == 0)
\r
205 return AP_ADC_CALIB;
\r
207 else if (lpAPADCReq->operate == 1)
\r
209 return AP_ADC_LOAD;
\r
211 else if (lpAPADCReq->operate == 2)
\r
213 return AP_ADC_SAVE;
\r
225 } else if(DIAG_POWER_SUPPLY_F == lpHeader->type){
\r
226 return AP_GET_VOLT;
\r
228 //add by kenyliu in 2013 06 20 for bug 146310
\r
229 else if((DIAG_CURRENT_TEST_F == lpHeader->type) && (0xE == lpHeader->subtype)){
\r
230 power_off_Flag =0xE;
\r
237 static uint32 ap_adc_calibration(uint32 channel, MSG_AP_ADC_CNF *pMsgADC)
\r
239 volatile uint32 adc_channel = 0, adc_result = 0;
\r
241 adc_channel = channel;
\r
243 if (adc_channel <= 8)
\r
245 if (adc_channel == 0)
\r
253 adc_result += ADC_GetValue(adc_channel-1, 0);
\r
256 pMsgADC->diag_ap_cnf.status = 0;
\r
257 pMsgADC->ap_adc_req. parameters[0]= (uint16)(adc_result&0xFFFF);
\r
261 pMsgADC->diag_ap_cnf.status = 1;
\r
262 pMsgADC->ap_adc_req. parameters[0] = 0xFFFF;
\r
265 printf("\n ReadADC: ( channel = %x)", adc_channel);
\r
270 static int ap_adc_save(TOOLS_AP_ADC_REQ_T *pADCReq, MSG_AP_ADC_CNF *pMsgADC)
\r
272 AP_ADC_T adcValue = {0};
\r
274 ret = AccessADCDataFile(1, pADCReq->parameters, sizeof(pADCReq->parameters));
\r
277 pMsgADC->diag_ap_cnf.status = 0;
\r
281 pMsgADC->diag_ap_cnf.status = 1;
\r
286 static int ap_adc_load(MSG_AP_ADC_CNF *pMsgADC)
\r
288 int ret = AccessADCDataFile(0, pMsgADC->ap_adc_req.parameters, sizeof(pMsgADC->ap_adc_req.parameters));
\r
291 pMsgADC->diag_ap_cnf.status = 0;
\r
295 pMsgADC->diag_ap_cnf.status = 1;
\r
300 static uint32 ap_get_voltage(uint32 channel, MSG_AP_ADC_CNF *pMsgADC)
\r
302 volatile uint32 adc_channel = 0, adc_result = 0;
\r
303 uint32 voltage = 0;
\r
307 adc_channel = channel;
\r
309 if (adc_channel <= 8)
\r
311 if (adc_channel == 0)
\r
319 adc_result += ADC_GetValue(adc_channel-1, 0);
\r
322 voltage = sprdbat_auxadc2vbatvol(adc_result);
\r
323 msg = (MSG_HEAD_T *)pMsgADC;
\r
325 // *para = (voltage/10);
\r
326 if (voltage > MAX_VOLTAGE)
\r
328 *para = (PRECISION_10MV | ((voltage/10) & MAX_VOLTAGE));
\r
332 *para = (PRECISION_1MV | (voltage & MAX_VOLTAGE));
\r
337 pMsgADC->diag_ap_cnf.status = 1;
\r
338 pMsgADC->ap_adc_req. parameters[0] = 0xFFFF;
\r
340 pMsgADC->msg_head.len = 12;
\r
341 printf("\n Read voltage : %d\n", voltage);
\r
345 uint8 ap_adc_process(int flag, char * src, int size, MSG_AP_ADC_CNF * pMsgADC)
\r
347 MSG_HEAD_T *lpHeader = (MSG_HEAD_T *)src;
\r
348 TOOLS_DIAG_AP_CMD_T *lpAPCmd =(TOOLS_DIAG_AP_CMD_T *)(lpHeader+1);
\r
349 TOOLS_AP_ADC_REQ_T *lpApADCReq = (TOOLS_AP_ADC_REQ_T *)(lpAPCmd+1);
\r
350 memcpy(&(pMsgADC->msg_head), lpHeader, sizeof(MSG_HEAD_T));
\r
351 pMsgADC->msg_head.len = sizeof(TOOLS_DIAG_AP_CNF_T)+sizeof(TOOLS_AP_ADC_REQ_T)+sizeof(MSG_HEAD_T);
\r
352 pMsgADC->diag_ap_cnf.length = sizeof(TOOLS_AP_ADC_REQ_T);
\r
353 memcpy(&(pMsgADC->ap_adc_req), lpApADCReq, sizeof(TOOLS_AP_ADC_REQ_T));
\r
359 uint32 channel = lpApADCReq->parameters[0];
\r
360 ap_adc_calibration(channel, pMsgADC);
\r
364 ap_adc_load(pMsgADC);
\r
367 ap_adc_save(lpApADCReq, pMsgADC);
\r
370 ap_get_voltage(6,pMsgADC);
\r
378 int write_adc_calibration_data(char *data, int size)
\r
381 #ifdef CONFIG_AP_ADC_CALIBRATION
\r
382 ret = AccessADCDataFile(1, data, size);
\r
386 int read_adc_calibration_data(char *buffer,int size)
\r
388 #ifdef CONFIG_AP_ADC_CALIBRATION
\r
392 ret = AccessADCDataFile(0, buffer, size);
\r
398 uint32 ap_calibration_proc(uint8 *data,uint32 count,uint8 *out_msg)
\r
400 #ifdef CONFIG_AP_ADC_CALIBRATION
\r
405 adcFlag = is_adc_calibration(g_usb_buf_dest, sizeof(g_usb_buf_dest), data, count );
\r
409 MSG_AP_ADC_CNF adcMsg = {0};
\r
410 printf("\n adcFlag = %d", adcFlag);
\r
412 ap_adc_process(adcFlag, g_usb_buf_dest, count, &adcMsg);
\r
413 index = translate_packet(out_msg, &adcMsg, adcMsg.msg_head.len);
\r
422 //add by kenyliu in 2013 06 20 for bug 146310
\r
425 #ifdef CONFIG_AP_ADC_CALIBRATION
\r
426 return power_off_Flag;
\r