tizen 2.4 release
[profile/mobile/platform/kernel/u-boot-tm1.git] / property / calibration_mode.c
1 #include <config.h>
2 #include <common.h>
3 #ifdef CONFIG_CALIBRATION_MODE_NEW
4
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>
11 #include <nand.h>
12 #include <android_boot.h>
13 #include <environment.h>
14 #include <jffs2/jffs2.h>
15 #include <boot_mode.h>
16 #include <malloc.h>
17
18 #include <asm/io.h>
19 #include <asm/arch/sio_drv.h>
20 #include "asm/arch/sci_types.h"
21
22
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
28
29
30 #define TOOL_CHANNEL    (1)
31 #define mdelay(n)       udelay((n) * 1000)
32
33
34 typedef struct _Mode_Data
35 {
36     uint32 sn;
37     uint16 length;
38     uint8 type;
39     uint8 subtype;
40 }Mode_Data_Type;
41
42
43
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;
49
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(const 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)
70 {
71 #if 0 //def _LITTLE_ENDIAN
72     return (value >> 8 | value << 8);
73 #else
74     return value;
75 #endif
76 }
77
78 unsigned int EndianConv_32 (unsigned int value)
79 {
80 #if 0 //def _LITTLE_ENDIAN
81     unsigned int nTmp = 0;
82     nTmp = (value >> 24 | value << 24);
83
84     nTmp |= ( (value >> 8) & 0x0000FF00);
85     nTmp |= ( (value << 8) & 0x00FF0000);
86     return nTmp;
87 #else
88     return value;
89 #endif
90 }
91
92
93 static struct FDL_ChannelHandler *Calibration_ChannelGet(int nchannel)
94 {
95     struct FDL_ChannelHandler *channel;
96
97     switch (nchannel)
98     {
99         case 1:
100             channel = &gUart1Channel;
101             break;
102         case 0:
103             channel = &gUart0Channel;
104             break;
105         case 3:
106             channel = &gUart3Channel;
107             break;
108         default:
109             channel = &gUart0Channel;
110             break;
111     }
112
113     return channel;
114 }
115 static int Calibration_ParseRequsetMode(const unsigned char *buf, int len)
116 {
117     Mode_Data_Type *pReqType = NULL;
118     char tempbuf[MODE_REQUEST_LENGTH - 2] = {0};
119     if(buf[0] != 0x7e || buf[len - 1] != 0x7e)
120         return -1;
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){
124         return -1;
125     }
126     return 0;
127 }
128
129 static int Calibration_SetMode(const uint8_t *pcmd, int length)
130 {
131     if(pcmd[0] != 0x7e || pcmd[length - 1] != 0x7e)
132         return -1;
133     if(gUsedChannel->Write(gUsedChannel, pcmd, length) != length)
134         return -1;
135     return 0;
136 }
137
138 static int Calibration_ReinitUsb(void)
139 {
140     unsigned long long start_time, end_time;
141     gs_reset_usb_param();
142     calibration_reset_composite();
143     dwc_otg_driver_init();
144     usb_serial_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())
149             return -1;
150     }
151     printf("USB SERIAL CONFIGED\n");
152
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())
157             return -1;
158     }
159     return 0;
160 }
161
162
163
164 static int      calibration_device=0;
165 int  tool_channel_open(void)
166 {
167     calibration_device = poweron_by_calibration();
168     switch(calibration_device)
169     {
170         case 1: //USB calibration ;
171             printf("USB calibration\n");
172             if(-1 == Calibration_ReinitUsb())
173                 return -1;
174             gs_open();
175             break;
176         case 2: //UART calibration;
177             printf("UART calibration:...\n");
178             break;
179         default:
180             break;
181     }
182     return 0;
183 }
184 int  tool_channel_write(char *buffer,int count)
185 {
186     int ret;
187     switch(calibration_device)
188     {
189         case 1: //USB calibration ;
190             {
191                 while(count > 0){
192                     ret = gs_write(g_uart_buf, count);
193 #if 0
194                     printf("func: %s waitting %d write done\n", __func__, count);
195                     if(usb_trans_status)
196                         printf("func: %s line %d usb trans with error %d\n", __func__, __LINE__, usb_trans_status);
197 #endif
198                     usb_wait_trans_done(1);
199                     if(ret > 0)
200                         count -= ret;
201                 }
202             }
203             break;
204         case 2: //UART calibration;
205             {
206                 struct FDL_ChannelHandler *UartChannel;
207                 UartChannel = Calibration_ChannelGet(TOOL_CHANNEL);
208                 UartChannel->Write(UartChannel,buffer,count);
209             }
210             break;
211         default:
212             break;
213     }
214 }
215
216 int  tool_channel_read_status = 0;
217 int  tool_channel_read(char *buffer,int count)
218 {
219     int index;
220     int size = count;
221     int ch;
222
223     index = 0;
224     switch(calibration_device)
225     {
226         case 1: //USB calibration ;
227             {
228                 if(usb_is_trans_done(0)){
229                     int ret;
230 #if 0
231                     if(usb_trans_status)
232                         printf("func: %s line %d read error %d\n", __func__, __LINE__, usb_trans_status);
233 #endif
234                     ret = gs_read(buffer, &size);
235                     if(ret)
236                         index = size;
237 #if 0
238                     if(usb_trans_status)
239                         printf("func: %s line %d read error %d\n", __func__, __LINE__, usb_trans_status);
240 #endif
241                 }
242             }
243             break;
244         case 2:
245             {
246                 struct FDL_ChannelHandler *UartChannel;
247
248                 UartChannel = Calibration_ChannelGet(TOOL_CHANNEL);
249
250                 ch = UartChannel->GetSingleChar(UartChannel);
251                 if(0x7E == ch )
252                 {
253                     do
254                     {
255                         buffer[index++] = ch;
256                     }while(0x7E != (ch = UartChannel->GetChar(UartChannel)));
257
258                     buffer[index++] = ch;
259                 }
260             }
261         default:
262             break;
263     }
264     return index;
265 }
266 void calibration_mode(const uint8_t *pcmd, int length)
267 {
268     int ret;
269     int count = 0;
270     unsigned char ch;
271     int index = 0;
272     int i;
273     unsigned char buf[MODE_REQUEST_LENGTH] = {0};
274
275     init_calibration_mode();
276 #ifndef __DL_UART0__
277     if(CALIBRATION_CHANNEL==1)
278         __dl_log_share__ = 1;
279 #endif
280
281     gUsedChannel = Calibration_ChannelGet(CALIBRATION_CHANNEL);
282     gUsedChannel->Open(gUsedChannel, 115200);
283 #if 1
284     while(gUsedChannel->GetChar(gUsedChannel) != 0x7e); // purge fifo
285     buf[0] = 0x7e;
286     for(i = 1; i < MODE_REQUEST_LENGTH; i++){
287         ch = gUsedChannel->GetChar(gUsedChannel);
288         buf[i] = ch;
289     }
290
291     if(-1 == Calibration_ParseRequsetMode(buf, MODE_REQUEST_LENGTH))
292         return ;
293     if(-1 == Calibration_SetMode(pcmd, length))
294         return ;
295
296     printf("calibration_mode step2\n");
297
298     // wait for cp ready
299     i=0;
300     while(gpio_get_value(CP_AP_LIV) == 0);
301     printf("calibration_mode step3\n");
302
303 #if defined(CONFIG_SC7710G2)
304     serial3_flowctl_enable();
305 #endif
306
307     int nvitem_sync(void);
308     nvitem_sync();
309
310     printf("calibration_mode step4\n");
311
312 #endif
313     if(tool_channel_open() == -1)
314         return;
315
316     printf("calibration_mode step5\n");
317
318     while(TRUE){
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){
321             if(count > 0){
322                 if(count != gUsedChannel->Write(gUsedChannel, g_usb_buf, count)) {
323                     return ;
324                 }
325                 mdelay(3);
326             }
327             count = 0;
328             index += gUsedChannel->Read(gUsedChannel, g_uart_buf, MAX_UART_BUF_LEN);
329         }
330         if(index > 0){
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*/);
334             }else {
335                 tool_channel_write(g_uart_buf, index);
336             }
337         }
338         if(gpio_get_value(CP_AP_LIV) == 1) {
339             nvitem_sync();
340         }
341         //add by kenyliu in 2013 06 20 for bug 146310
342         if(0xE == get_adc_flag())
343         {
344             power_down_devices();
345         }
346         //end kenyliu
347     }
348 #ifndef __DL_UART0__
349     __dl_log_share__ = 0;
350 #endif
351
352 }
353
354 #endif /* CONFIG_CALIBRATION_MODE_NEW */