3 #ifdef CONFIG_CALIBRATION_MODE_NEW
5 #include <linux/types.h>
6 #include <asm/arch/bits.h>
7 #include <linux/string.h>
8 #include <android_bootimg.h>
9 #include <linux/mtd/mtd.h>
10 #include <linux/mtd/nand.h>
12 #include <android_boot.h>
13 #include <environment.h>
14 #include <jffs2/jffs2.h>
15 #include <boot_mode.h>
19 #include <asm/arch/sio_drv.h>
20 #include "asm/arch/sci_types.h"
23 #define MODE_REQUEST_LENGTH 10
24 #define RUNMODE_REQUESET_CMD 0xFE
25 #define CALIBRATION_REQUEST_SUBCMD 0x1
26 #define MAX_USB_BUF_LEN 10*1024
27 #define MAX_UART_BUF_LEN 1024
30 #define TOOL_CHANNEL (1)
31 #define mdelay(n) udelay((n) * 1000)
34 typedef struct _Mode_Data
44 struct FDL_ChannelHandler *gUsedChannel;
45 static unsigned char g_usb_buf[MAX_USB_BUF_LEN];
46 static unsigned char g_uart_buf[MAX_UART_BUF_LEN];
47 extern int __dl_log_share__ ;
48 extern int usb_trans_status;
50 extern int dwc_otg_driver_init(void);
51 extern int usb_serial_init(void);
52 extern int usb_is_configured(void);
53 extern int get_cal_enum_ms(void);
54 extern int usb_is_port_open(void);
55 extern int get_cal_io_ms(void);
56 extern int gpio_get_value(int PinNo);
57 extern int gs_open(void);
58 extern int spi_channel_init(unsigned long phy_id);
59 extern int usb_is_trans_done(int direct);
60 extern int gs_read(unsigned char *buf, int *count);
61 extern int gs_write(const unsigned char *buf, int count);
62 extern void usb_wait_trans_done(int direct);
63 extern void gs_reset_usb_param(void);
64 extern void calibration_reset_composite(void);
65 extern void init_calibration_mode(void);
66 extern uint32 ap_calibration_proc(uint8 *data,uint32 count,uint8 *out_msg);
67 extern uint32 get_adc_flag(); //add by kenyliu in 2013 06 20 for bug 146310
68 extern int poweron_by_calibration(void);
69 unsigned short EndianConv_16 (unsigned short value)
71 #if 0 //def _LITTLE_ENDIAN
72 return (value >> 8 | value << 8);
78 unsigned int EndianConv_32 (unsigned int value)
80 #if 0 //def _LITTLE_ENDIAN
81 unsigned int nTmp = 0;
82 nTmp = (value >> 24 | value << 24);
84 nTmp |= ( (value >> 8) & 0x0000FF00);
85 nTmp |= ( (value << 8) & 0x00FF0000);
93 static struct FDL_ChannelHandler *Calibration_ChannelGet(int nchannel)
95 struct FDL_ChannelHandler *channel;
100 channel = &gUart1Channel;
103 channel = &gUart0Channel;
106 channel = &gUart3Channel;
109 channel = &gUart0Channel;
115 static int Calibration_ParseRequsetMode(const unsigned char *buf, int len)
117 Mode_Data_Type *pReqType = NULL;
118 char tempbuf[MODE_REQUEST_LENGTH - 2] = {0};
119 if(buf[0] != 0x7e || buf[len - 1] != 0x7e)
121 memcpy((void*)tempbuf, (void*)&buf[1], len-2);
122 pReqType = (Mode_Data_Type*)tempbuf;
123 if(pReqType->type != RUNMODE_REQUESET_CMD || pReqType->subtype != CALIBRATION_REQUEST_SUBCMD){
129 static int Calibration_SetMode(const uint8_t *pcmd, int length)
131 if(pcmd[0] != 0x7e || pcmd[length - 1] != 0x7e)
133 if(gUsedChannel->Write(gUsedChannel, pcmd, length) != length)
138 static int Calibration_ReinitUsb(void)
140 unsigned long long start_time, end_time;
141 gs_reset_usb_param();
142 calibration_reset_composite();
143 dwc_otg_driver_init();
145 start_time = get_timer_masked();
146 while(!usb_is_configured()){
147 end_time = get_timer_masked();
148 if(end_time - start_time > get_cal_enum_ms())
151 printf("USB SERIAL CONFIGED\n");
153 start_time = get_timer_masked();
154 while(!usb_is_port_open()){
155 end_time = get_timer_masked();
156 if(end_time - start_time > get_cal_io_ms())
164 static int calibration_device=0;
165 int tool_channel_open(void)
167 calibration_device = poweron_by_calibration();
168 switch(calibration_device)
170 case 1: //USB calibration ;
171 printf("USB calibration\n");
172 if(-1 == Calibration_ReinitUsb())
176 case 2: //UART calibration;
177 printf("UART calibration:...\n");
184 int tool_channel_write(char *buffer,int count)
187 switch(calibration_device)
189 case 1: //USB calibration ;
192 ret = gs_write(g_uart_buf, count);
194 printf("func: %s waitting %d write done\n", __func__, count);
196 printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
198 usb_wait_trans_done(1);
204 case 2: //UART calibration;
206 struct FDL_ChannelHandler *UartChannel;
207 UartChannel = Calibration_ChannelGet(TOOL_CHANNEL);
208 UartChannel->Write(UartChannel,buffer,count);
216 int tool_channel_read_status = 0;
217 int tool_channel_read(char *buffer,int count)
224 switch(calibration_device)
226 case 1: //USB calibration ;
228 if(usb_is_trans_done(0)){
232 printf("func: %s line %d read error %d\n", __func__, __LINE__, usb_trans_status);
234 ret = gs_read(buffer, &size);
239 printf("func: %s line %d read error %d\n", __func__, __LINE__, usb_trans_status);
246 struct FDL_ChannelHandler *UartChannel;
248 UartChannel = Calibration_ChannelGet(TOOL_CHANNEL);
250 ch = UartChannel->GetSingleChar(UartChannel);
255 buffer[index++] = ch;
256 }while(0x7E != (ch = UartChannel->GetChar(UartChannel)));
258 buffer[index++] = ch;
266 void calibration_mode(const uint8_t *pcmd, int length)
273 unsigned char buf[MODE_REQUEST_LENGTH] = {0};
275 init_calibration_mode();
277 if(CALIBRATION_CHANNEL==1)
278 __dl_log_share__ = 1;
281 gUsedChannel = Calibration_ChannelGet(CALIBRATION_CHANNEL);
282 gUsedChannel->Open(gUsedChannel, 115200);
284 while(gUsedChannel->GetChar(gUsedChannel) != 0x7e); // purge fifo
286 for(i = 1; i < MODE_REQUEST_LENGTH; i++){
287 ch = gUsedChannel->GetChar(gUsedChannel);
291 if(-1 == Calibration_ParseRequsetMode(buf, MODE_REQUEST_LENGTH))
293 if(-1 == Calibration_SetMode(pcmd, length))
296 printf("calibration_mode step2\n");
300 while(gpio_get_value(CP_AP_LIV) == 0);
301 printf("calibration_mode step3\n");
303 #if defined(CONFIG_SC7710G2)
304 serial3_flowctl_enable();
307 int nvitem_sync(void);
310 printf("calibration_mode step4\n");
313 if(tool_channel_open() == -1)
316 printf("calibration_mode step5\n");
319 count = tool_channel_read(g_usb_buf, MAX_USB_BUF_LEN);
320 if((index = ap_calibration_proc( g_usb_buf, count,g_uart_buf)) == 0){
322 if(count != gUsedChannel->Write(gUsedChannel, g_usb_buf, count)) {
328 index += gUsedChannel->Read(gUsedChannel, g_uart_buf, MAX_UART_BUF_LEN);
331 if(1 == index && 'K' == g_uart_buf[0] && 0 == gpio_get_value(CP_AP_LIV)) {
332 printf("change bandrate ...");
333 gUsedChannel->SetBaudrate(gUsedChannel, 1843200/*921600*/);
335 tool_channel_write(g_uart_buf, index);
338 if(gpio_get_value(CP_AP_LIV) == 1) {
341 //add by kenyliu in 2013 06 20 for bug 146310
342 if(0xE == get_adc_flag())
344 power_down_devices(0);
349 __dl_log_share__ = 0;
354 #endif /* CONFIG_CALIBRATION_MODE_NEW */