forked from DediProgSW/SF100Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usbdriver.c
executable file
·649 lines (529 loc) · 14.8 KB
/
usbdriver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#include <stdio.h>
#include <string.h>
#include <usb.h>
#include "Macro.h"
#include "usbdriver.h"
unsigned int m_nbDeviceDetected = 0;
unsigned char DevIndex = 0;
extern volatile bool g_bIsSF600[16];
extern int g_CurrentSeriase;
extern char g_board_type[8];
extern int g_firmversion;
extern void Sleep(unsigned int ms);
extern CHIP_INFO Chip_Info;
extern unsigned int g_uiDevNum;
extern bool isSendFFsequence;
#define SerialFlash_FALSE -1
#define SerialFlash_TRUE 1
static usb_dev_handle *dediprog_handle[MAX_Dev_Index];
extern int is_SF100nBoardVersionGreaterThan_5_5_0(int Inde);
extern int is_SF600nBoardVersionGreaterThan_6_9_0(int Inde);
bool Is_NewUSBCommand(int Index)
{
if(is_SF100nBoardVersionGreaterThan_5_5_0(Index) || is_SF600nBoardVersionGreaterThan_6_9_0(Index))
return true;
return false;
}
extern unsigned char GetFPGAVersion(int Index);
void usb_dev_init(void)
{
usb_init();
usb_find_busses();
usb_find_devices();
}
void usb_db_init(void)
{
int i;
for(i=0; i<MAX_Dev_Index; i++)
{
usb_device_entry[i].valid = 0;
dediprog_handle[i] = NULL;
}
}
void IsSF600(int Index)
{
if( Index==-1 ) Index = DevIndex;
CNTRPIPE_RQ rq ;
unsigned char vBuffer[16] ;
int fw[3];
memset(vBuffer, '\0', 16);
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0x08 ;
rq.Value = 0 ;
rq.Index = 0 ;
rq.Length = 16;
if(InCtrlRequest(&rq, vBuffer,16,Index)==SerialFlash_FALSE)
return ;
memcpy(g_board_type,&vBuffer[0],8);
sscanf((char*)&vBuffer[8],"V:%d.%d.%d",&fw[0],&fw[1],&fw[2]);
g_firmversion=((fw[0]<<16) | (fw[1]<<8) | fw[2]);
// printf("g_firmversion=%x\r\n",g_firmversion);
// printf("g_board_type=%s\r\n",g_board_type);
if(strstr(g_board_type,"SF600") != NULL)
g_bIsSF600[Index]=true;
else
g_bIsSF600[Index]=false;
GetFPGAVersion(Index);
}
int get_usb_dev_cnt(void)
{
return dev_index;
}
/* Might be useful for other USB devices as well. static for now. */
static int FindUSBDevice(void)
{
struct usb_bus *bus;
struct usb_device *dev;
unsigned int vid = 0x0483;
unsigned int pid = 0xdada;
usb_db_init();
for (bus = usb_get_busses(); bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
if ((dev->descriptor.idVendor == vid) && (dev->descriptor.idProduct == pid))
{
usb_device_entry[dev_index].usb_device_handler = *dev;
usb_device_entry[dev_index].valid = 1;
dev_index++;
}
}
}
printf(" \n");
return dev_index;
}
int OutCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size ,int Index )
{
// unsigned long bytesWrite;
int requesttype;
int ret = 0;
if( Index==-1 )
Index = DevIndex;
if( (rq->Function!=URB_FUNCTION_VENDOR_ENDPOINT) && (g_bIsSF600[Index]==true)) return true;
requesttype = 0x00;
if( rq->Direction==VENDOR_DIRECTION_IN ) requesttype |= 0x80;
if( rq->Function==URB_FUNCTION_VENDOR_DEVICE ) requesttype |= 0x40;
if( rq->Function==URB_FUNCTION_VENDOR_INTERFACE ) requesttype |= 0x41;
if( rq->Function==URB_FUNCTION_VENDOR_ENDPOINT ) requesttype |= 0x42;
if( rq->Function==URB_FUNCTION_VENDOR_OTHER ) requesttype |= 0x43;
if (dediprog_handle[Index] ) {
ret = usb_control_msg(dediprog_handle[Index], requesttype, rq->Request, rq->Value, rq->Index, (char*)buf, buf_size, DEFAULT_TIMEOUT);
}// else
// printf("no device");
if(ret != buf_size)
{
#if 0
printf("Control Pipe output error!\n");
printf("rq->Direction=%X\n",rq->Direction);
printf("rq->Function=%X\n",rq->Function);
printf("rq->Request=%X\n",rq->Request);
printf("rq->Value=%X\n",rq->Value);
printf("rq->Index=%X\n",rq->Index);
printf("rq->Length=%X\n",rq->Length);
printf("buf_size=%X\n",buf_size);
printf("buf[0]=%X\n",buf[0]);
printf("g_bIsSF600=%d\n",g_bIsSF600);
#endif
//printf("Error=0x%x\r\n",usb_strerror());
return -1;
}
return ret;
}
int InCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size, int Index )
{
//boost::mutex::scoped_lock l(mutex);
// unsigned long bytesRead;
int requesttype;
int ret = 0;
if( (rq->Function!=URB_FUNCTION_VENDOR_ENDPOINT) && (g_bIsSF600[Index]==true)) return true;
if( Index==-1 )
Index = DevIndex;
if(sizeof(buf) == 0)
return 0;
requesttype = 0x00;
if( rq->Direction==VENDOR_DIRECTION_IN ) requesttype |= 0x80;
if( rq->Function==URB_FUNCTION_VENDOR_DEVICE ) requesttype |= 0x40;
if( rq->Function==URB_FUNCTION_VENDOR_INTERFACE ) requesttype |= 0x41;
if( rq->Function==URB_FUNCTION_VENDOR_ENDPOINT ) requesttype |= 0x42;
if( rq->Function==URB_FUNCTION_VENDOR_OTHER ) requesttype |= 0x43;
if (dediprog_handle[Index] ) {
ret = usb_control_msg(dediprog_handle[Index], requesttype, rq->Request, rq->Value, rq->Index, (char*)buf, buf_size, DEFAULT_TIMEOUT);
}// else
// printf("no device");
if(ret != buf_size)
{
// printf("Control Pipe input error!\n");
return -1;
}
return ret;
}
// part of USB driver , open usb pipes for data transfor
// should be called after usb successfully opens pipes.
int dediprog_start_appli(int Index)
{
//IsSF600(Index);
CNTRPIPE_RQ rq ;
int ret;
unsigned char vInstruction;
// special instruction
vInstruction = 0 ;
rq.Function = URB_FUNCTION_VENDOR_OTHER ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0xb ;
rq.Value = 0x00 ;
rq.Index = 0x00 ;
rq.Length = 0x01 ;
ret = OutCtrlRequest(&rq, &vInstruction, 1, Index);
return ret;
}
int dediprog_get_chipid(int Index)
{
//IsSF600(Index);
CNTRPIPE_RQ rq ;
int ret;
unsigned char vInstruction[3];
memset(vInstruction, 0, 3);
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = 0x1 ;
rq.Value = 0xff ;
if(Is_NewUSBCommand(Index))
rq.Index = 0 ;
else
rq.Index = 0x1 ;
rq.Length = 0x1 ;
vInstruction[0]= 0x9f;
ret = OutCtrlRequest(&rq, vInstruction, 1, Index);
// special instruction
memset(vInstruction, 0, 3);
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = 0x1 ;
rq.Value = 0x0 ;
rq.Index = 0x00 ;
rq.Length = 0x03 ;
ret = InCtrlRequest(&rq, vInstruction, 3, Index);
return ret;
}
// return size read
// if fail , return -1
//long CUSB::BulkPipeRead(PBYTE pBuff, UINT sz, UINT timeOut) const
int BulkPipeRead(unsigned char *pBuff, unsigned int timeOut, int Index)
{
int ret;
if( Index==-1 ) Index = DevIndex;
unsigned long cnRead = 512;
ret = usb_bulk_read(dediprog_handle[Index], 0x82, (char*)pBuff, cnRead, DEFAULT_TIMEOUT);
cnRead = ret;
return cnRead ;
}
//long CUSB::BulkPipeWrite(PBYTE pBuff, UINT sz, UINT timeOut) const
int BulkPipeWrite(unsigned char *pBuff, unsigned int size,unsigned int timeOut, int Index)
{
int ret;
int nWrite = 512 ;
char pData[512];
memset(pData, 0xFF, 512); // fill buffer with 0xFF
memcpy(pData, pBuff , size);
if( Index==-1 ) Index = DevIndex;
ret = usb_bulk_write(dediprog_handle[Index], (g_bIsSF600[Index]==true)? 0x01:0x02,pData, nWrite, DEFAULT_TIMEOUT);
nWrite = ret;
return nWrite;
}
int dediprog_set_spi_voltage(int v,int Index)
{
int ret = 0;
// int voltage_selector;
CNTRPIPE_RQ rq ;
// unsigned char vBuffer[12];
if(0 == v) Sleep(200);
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = SET_VCC ;
rq.Length = 0 ;
if(Is_NewUSBCommand(Index))
{
rq.Value = v ;
rq.Index = 0;
}
else
{
rq.Value = v ;
rq.Index = 0x04 | g_CurrentSeriase; // ID detect mode
}
ret = OutCtrlRequest(&rq, NULL, 0, Index);
if(0 != v)
{
Sleep(200);
if(isSendFFsequence)
{
unsigned char v[4]={0xff,0xff,0xff,0xff};
FlashCommand_SendCommand_OutOnlyInstruction(v,4,Index);
}
}
return ret;
}
int dediprog_set_vpp_voltage(int volt,int Index)
{
int ret;
int voltage_selector;
CNTRPIPE_RQ rq ;
switch (volt) {
case 0:
/* Admittedly this one is an assumption. */
voltage_selector = 0x0;
break;
case 9:
voltage_selector = 0x1;
break;
case 12:
voltage_selector = 0x2;
break;
default:
return 1;
}
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = 0x03 ;
rq.Value = voltage_selector ;
rq.Index = 0 ;
rq.Length = 0x0 ;
ret = OutCtrlRequest(&rq, NULL, 0, Index);
if (ret == SerialFlash_FALSE) {
// printf("Command Set VPP Voltage 0x%x failed!\n", voltage_selector);
return 1;
}
return 0;
}
int dediprog_set_spi_clk(int khz,int Index)
{
return 0;
int ret;
int hz_selector;
//IsSF600(Index);
CNTRPIPE_RQ rq ;
switch (khz) {
case 24000:
/* Admittedly this one is an assumption. */
hz_selector = 0x0;
break;
case 8000:
hz_selector = 0x1;
break;
case 12000:
hz_selector = 0x2;
break;
case 3000:
hz_selector = 0x3;
break;
case 2180:
hz_selector = 0x4;
break;
case 1500:
hz_selector = 0x5;
break;
case 750:
hz_selector = 0x6;
break;
case 375:
hz_selector = 0x7;
break;
default:
printf("Unknown clk %i KHz! Aborting.\n", khz);
return 1;
}
// printf("Setting SPI clk to %u.%03u MHz\n", khz / 1000,
// khz % 1000);
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = 0x61 ;
rq.Value = hz_selector ;
rq.Index = 0 ;
rq.Length = 0x0 ;
ret = OutCtrlRequest(&rq, NULL, 0, Index);
if (ret == SerialFlash_FALSE) {
// printf("Command Set SPI clk 0x%x failed!\n", hz_selector);
return 1;
}
return 0;
}
int usb_driver_init(void)
{
// struct usb_bus *bus;
// struct usb_device *dev;
bool result = false;
int device_cnt = 0;
int ret;
for(int i=0;i<MAX_Dev_Index;i++)
{
dediprog_handle[i]=NULL;
}
usb_dev_init();
device_cnt = FindUSBDevice();
if(g_uiDevNum == 0)
{
for(int i=0;i<device_cnt;i++)
{
if(usb_device_entry[i].valid==0)
{
printf("Error: Programmers are not connected.\n");
return 0;
}
dediprog_handle[i] = usb_open(&usb_device_entry[i].usb_device_handler);
if(dediprog_handle[i]==NULL)
{
printf("Error: Programmers are not connected.\n");
return 0;
}
ret = usb_set_configuration(dediprog_handle[i], 1);
if(ret)
{
printf("Error: Programmers USB set configuration: 0x%x.\n",ret);
return 0;
}
ret = usb_claim_interface(dediprog_handle[i], 0);
if(ret)
{
printf("Error: Programmers USB claim interface: 0x%x.\n",ret);
return 0;
}
g_bIsSF600[i]=false;
IsSF600(i);
dediprog_start_appli(i);
IsSF600(i);
result = (dediprog_handle[i] != NULL);
}
}
else
{
if(usb_device_entry[g_uiDevNum-1].valid==0)
{
printf("Error: Programmers are not connected.\n");
return 0;
}
dediprog_handle[g_uiDevNum-1] = usb_open(&usb_device_entry[g_uiDevNum-1].usb_device_handler);
if(dediprog_handle[g_uiDevNum-1]==NULL)
{
printf("Error: Programmers are not connected.\n");
return 0;
}
printf("dediprog_handle[%d]=%x\n",g_uiDevNum-1,dediprog_handle[g_uiDevNum-1]);
ret = usb_set_configuration(dediprog_handle[g_uiDevNum-1], 1);
if(ret)
{
printf("Error: Programmers USB set configuration: 0x%x.\n",ret);
return 0;
}
ret = usb_claim_interface(dediprog_handle[g_uiDevNum-1], 0);
if(ret)
{
printf("Error: Programmers USB claim interface: 0x%x.\n",ret);
return 0;
}
g_bIsSF600[g_uiDevNum-1]=false;
IsSF600(g_uiDevNum-1);
dediprog_start_appli(g_uiDevNum-1);
IsSF600(g_uiDevNum-1);
result = (dediprog_handle[g_uiDevNum-1] != NULL);
}
return result;//((dediprog_handle[i] != NULL)? 1:0);
}
int usb_driver_release(void)
{
for(int i=0;i<dev_index;i++)
{
if(dediprog_handle[i]==NULL) return 0;
usb_release_interface(dediprog_handle[i], 0);
usb_close (dediprog_handle[i]);
}
return 0;
}
#if 0 //Simon: unit test code
int usb_driver_test(void)
{
struct usb_bus *bus;
struct usb_device *dev;
unsigned int vid = 0x0483;
unsigned int pid = 0xdada;
int device_cnt = 0;
int ret, i;
char string[256];
unsigned char readaddr[3];
unsigned char writeaddr[3];
usb_dev_handle *udev;
usb_dev_init();
device_cnt = FindUSBDevice();
// printf("\ndevice_cnt =%d\n", device_cnt);
dediprog_handle = usb_open(&usb_device_entry[device_cnt-1].usb_device_handler);
ret = usb_set_configuration(dediprog_handle, 1);
ret = usb_claim_interface(dediprog_handle, 0);
dediprog_start_appli(0);
dediprog_set_spi_voltage(3300);
dediprog_get_chipid(0);
dediprog_set_leds(PASS_ON | BUSY_ON | ERROR_ON);
usb_close (dediprog_handle);
return 0;
}
#endif
bool Is_usbworking(int Index)
{
usleep(1000); // unknow reson
return ((dediprog_handle[Index] != NULL)? true:false);
}
//long long flash_ReadId(boost::tuple<unsigned int /*RDID code*/, unsigned int/*inByteCount*/, unsigned int/*outByteCount*/> command,int Index)
long flash_ReadId(unsigned int read_id_code, unsigned int out_data_size ,int Index)
{
// read status
// if(! m_usb.is_open() )
// return 0 ;
// RDID is not decoded when in DP mode
// so , first release from Deep Power Down mode or read signature
// DoRDP() ;
// send request
CNTRPIPE_RQ rq ;
unsigned char vInstruction[8];
unsigned long rc = 0;
int i;
// first control packet
vInstruction[0] = read_id_code;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_OUT ;
rq.Request = TRANSCEIVE ;
if(Is_NewUSBCommand(Index))
{
rq.Value = RESULT_IN ;
rq.Index = 0 ;
}
else
{
rq.Value = RFU ;
rq.Index = RESULT_IN ;
}
rq.Length = 1;
if(OutCtrlRequest( &rq, vInstruction, (unsigned long)1 , Index ) == SerialFlash_FALSE )
return rc; //OutCtrlRequest() return error
// second control packet : fetch data
memset(vInstruction, 0, sizeof(vInstruction));
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ;
rq.Direction = VENDOR_DIRECTION_IN ;
rq.Request = TRANSCEIVE ;
if(Is_NewUSBCommand(Index))
{
rq.Value = 0x01 ;
rq.Index = 0 ;
}
else
{
rq.Value = CTRL_TIMEOUT ;
rq.Index = NO_REGISTER ;
}
rq.Length = out_data_size ;
if(InCtrlRequest(&rq, vInstruction, (unsigned long)out_data_size, Index)==SerialFlash_FALSE)
return rc;
for(i=0; i<out_data_size; i++) {
rc = (rc<<8) + vInstruction[i];
}
//printf("\n(Simon)Flish ID 0x%x (0x%x, %d)\n",rc, read_id_code, out_data_size);
return rc;
}