12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
21 #define DWNLD_HANDSHAKE_LOC 0x02
22 #define DWNLD_TYPE_LOC 0x04
23 #define DWNLD_SIZE_MSW_LOC 0x06
24 #define DWNLD_SIZE_LSW_LOC 0x08
25 #define DWNLD_PS_HDR_LOC 0x0A
27 #define MAX_DSP_WAIT_LOOPS 40
28 #define DSP_WAIT_SLEEP_TIME 1000
29 #define DSP_WAIT_DISPATCH_LVL 50
31 #define HANDSHAKE_TIMEOUT_VALUE 0xF1F1
32 #define HANDSHAKE_RESET_VALUE 0xFEFE
33 #define HANDSHAKE_RESET_VALUE_USB 0xFE7E
34 #define HANDSHAKE_DSP_BL_READY 0xFEFE
35 #define HANDSHAKE_DSP_BL_READY_USB 0xFE7E
36 #define HANDSHAKE_DRIVER_READY 0xFFFF
37 #define HANDSHAKE_SEND_DATA 0x0000
39 #define HANDSHAKE_REQUEST 0x0001
40 #define HANDSHAKE_RESPONSE 0x0000
42 #define REQUEST_CODE_LENGTH 0x0000
43 #define REQUEST_RUN_ADDRESS 0x0001
44 #define REQUEST_CODE_SEGMENT 0x0002
45 #define REQUEST_DONE_BL 0x0003
46 #define REQUEST_DONE_CL 0x0004
47 #define REQUEST_VERSION_INFO 0x0005
48 #define REQUEST_CODE_BY_VERSION 0x0006
49 #define REQUEST_MAILBOX_DATA 0x0007
50 #define REQUEST_FILE_CHECKSUM 0x0008
52 #define STATE_START_DWNLD 0x01
53 #define STATE_BOOT_DWNLD 0x02
54 #define STATE_CODE_DWNLD 0x03
55 #define STATE_DONE_DWNLD 0x04
56 #define STATE_SECTION_PROV 0x05
57 #define STATE_DONE_PROV 0x06
58 #define STATE_DONE_FILE 0x07
60 #define MAX_LENGTH 0x7f0
63 #define DWNLD_MAG_TYPE_LOC 0x00
64 #define DWNLD_MAG_LEN_LOC 0x01
65 #define DWNLD_MAG_ADDR_LOC 0x02
66 #define DWNLD_MAG_CHKSUM_LOC 0x03
67 #define DWNLD_MAG_VAL_LOC 0x04
69 #define HANDSHAKE_MAG_DSP_BL_READY 0xFEFE0000
70 #define HANDSHAKE_MAG_DSP_ENTRY 0x01000000
71 #define HANDSHAKE_MAG_DSP_DATA 0x02000000
72 #define HANDSHAKE_MAG_DSP_DONE 0x03000000
74 #define HANDSHAKE_MAG_DRV_READY 0xFFFF0000
75 #define HANDSHAKE_MAG_DRV_DATA 0x02FECDAB
76 #define HANDSHAKE_MAG_DRV_ENTRY 0x01FECDAB
78 #define HANDSHAKE_MAG_TIMEOUT_VALUE 0xF1F1
82 #define DWNLD_MAG1_HANDSHAKE_LOC 0x00
83 #define DWNLD_MAG1_TYPE_LOC 0x01
84 #define DWNLD_MAG1_SIZE_LOC 0x02
85 #define DWNLD_MAG1_PS_HDR_LOC 0x03
134 while (loopcnt < 10) {
137 DEBUG(
"check_usb_db: read FT1000_REG_DOORBELL value is %x\n",
140 DEBUG(
"FT1000:Got checkusb doorbell\n");
156 while (loopcnt < 20) {
159 DEBUG(
"FT1000:check_usb_db:Doorbell = 0x%x\n", temp);
164 DEBUG(
"check_usb_db: door bell is cleared, return 0\n");
195 while (loopcnt < 100) {
200 DEBUG(
" get_handshake: fcodeldr is %d\n",
203 status = check_usb_db(ft1000dev);
205 DEBUG(
"get_handshake: check_usb_db failed\n");
216 handshake =
ntohs(handshake);
221 if ((handshake == expected_value) ||
253 tempx = (
u32)handshake_value;
254 tempx =
ntohl(tempx);
256 tempword = (
u16)(tempx & 0xffff);
259 tempword = (
u16)(tempx >> 16);
277 while (loopcnt < 100) {
278 if (pft1000info->
usbboot == 2) {
281 for (temp = 0; temp < 16; temp++) {
282 DEBUG(
"tempbuf %d = 0x%x\n", temp,
287 (
u8 *)&handshake, 1);
288 DEBUG(
"handshake from read_dpram16 = 0x%x\n",
293 handshake = pft1000info->
tempbuf[1];
300 (
u8 *)&handshake, 1);
305 handshake =
ntohs(handshake);
306 if ((handshake == expected_value) ||
314 static void put_handshake_usb(
struct ft1000_device *ft1000dev,
u16 handshake_value)
318 for (i=0; i<1000; i++);
344 tempx =
ntohl(tempx);
349 tempx |= (tempword << 16);
350 tempx =
ntohl(tempx);
352 request_type = (
u16)tempx;
368 tempx =
ntohl(tempx);
370 if (pft1000info->
usbboot == 2) {
371 tempx = pft1000info->
tempbuf[2];
372 tempword = pft1000info->
tempbuf[3];
379 tempx |= (tempword << 16);
380 tempx =
ntohl(tempx);
382 request_type = (
u16)tempx;
409 value =
ntohl(value);
416 value |= (tempword << 16);
417 value =
ntohl(value);
442 tempx =
ntohl(lvalue);
467 chksum = ((((((usPtr[0] ^ usPtr[1]) ^ usPtr[2]) ^ usPtr[3]) ^
468 usPtr[4]) ^ usPtr[5]) ^ usPtr[6]);
473 static int check_buffers(
u16 *buff_w,
u16 *buff_r,
int len,
int offset)
477 for (i = 0; i < len; i++) {
478 if (buff_w[i] != buff_r[i + offset])
502 static u32 write_blk (
struct ft1000_device *ft1000dev,
u16 **pUsFile,
u8 **pUcFile,
long word_length)
509 u16 resultbuffer[64];
514 tempword = *(*pUsFile);
517 tempword = *(*pUsFile);
521 *pUcFile = *pUcFile + 4;
523 tempword = (
u16)word_length;
524 word_length = (word_length / 16) + 1;
525 for (; word_length > 0; word_length--)
533 tempbuffer[i++] = *(*pUsFile);
535 tempbuffer[
i] = *(*pUsFile);
537 *pUcFile = *pUcFile + 4;
566 if ( (tempbuffer[31] & 0xfe00) == 0xfe00)
572 if ( (tempbuffer[31] & 0xfe00) == 0xfe00)
574 if (check_buffers(tempbuffer, resultbuffer, 28, 0)) {
575 DEBUG(
"FT1000:download:DPRAM write failed 1 during bootloading\n");
582 if (check_buffers(tempbuffer, resultbuffer, 16, 24)) {
583 DEBUG(
"FT1000:download:DPRAM write failed 2 during bootloading\n");
592 if (check_buffers(tempbuffer, resultbuffer, 32, 0)) {
593 DEBUG(
"FT1000:download:DPRAM write failed 3 during bootloading\n");
609 DEBUG(
"FT1000:download:Write failed tempbuffer[31] = 0x%x\n", tempbuffer[31]);
614 dpram = dpram + loopcnt;
620 static void usb_dnld_complete (
struct urb *
urb)
643 u8 **pUcFile,
long word_length)
648 byte_length = word_length * 4;
650 if (byte_length && ((byte_length % 64) == 0))
653 if (byte_length < 64)
658 usb_fill_bulk_urb(ft1000dev->
tx_urb,
660 usb_sndbulkpipe(ft1000dev->
dev,
662 ft1000dev->
tx_buf, byte_length, usb_dnld_complete,
667 *pUsFile = *pUsFile + (word_length << 1);
668 *pUcFile = *pUcFile + (word_length << 2);
692 u16 pseudo_header_len;
700 long requested_version;
701 bool correct_version;
712 u32 image_chksum = 0;
719 DEBUG(
"Entered scram_dnldr...\n");
742 correct_version =
FALSE;
747 DEBUG(
"FT1000:STATE_START_DWNLD\n");
750 get_handshake_usb(ft1000dev,
759 (
"scram_dnldr: handshake is HANDSHAKE_DSP_BL_READY, call put_handshake(HANDSHAKE_DRIVER_READY)\n");
764 (
"FT1000:download:Download error: Handshake failed\n");
773 DEBUG(
"FT1000:STATE_BOOT_DWNLD\n");
783 DEBUG(
"FT1000:REQUEST_RUN_ADDRESS\n");
785 loader_code_address);
788 DEBUG(
"FT1000:REQUEST_CODE_LENGTH\n");
793 DEBUG(
"FT1000:REQUEST_DONE_BL\n");
795 s_file = (
u16 *) (boot_end);
796 c_file = (
u8 *) (boot_end);
810 (
"FT1000:download:Download error: Max length exceeded\n");
814 if ((word_length * 2 + c_file) >
820 (
"FT1000:download:Download error: Requested len=%d exceeds BOOT code boundary.\n",
829 if (word_length & 0x1)
831 word_length = word_length / 2;
834 write_blk(ft1000dev, &s_file,
835 &c_file, word_length);
840 (
"FT1000:download:Download error: Bad request type=%d in BOOT download state.\n",
846 put_handshake_usb(ft1000dev,
853 (
"FT1000:download:Download error: Handshake failed\n");
864 get_handshake_usb(ft1000dev,
875 get_request_type_usb(ft1000dev);
881 (
"FT1000:download:image_chksum = 0x%8x\n",
888 (
"FT1000:download: REQUEST_RUN_ADDRESS\n");
889 if (correct_version) {
891 (
"FT1000:download:run_address = 0x%8x\n",
897 (
"FT1000:download:Download error: Got Run address request before image offset request.\n");
904 (
"FT1000:download:REQUEST_CODE_LENGTH\n");
905 if (correct_version) {
907 (
"FT1000:download:run_size = 0x%8x\n",
913 (
"FT1000:download:Download error: Got Size request before image offset request.\n");
922 (
u16 *) (pFileStart +
931 if (!correct_version) {
933 (
"FT1000:download:Download error: Got Code Segment request before image offset request.\n");
943 (
"FT1000:download:Download error: Max length exceeded\n");
947 if ((word_length * 2 + c_file) >
953 (
"FT1000:download:Download error: Requested len=%d exceeds DSP code boundary.\n",
962 if (word_length & 0x1)
964 word_length = word_length / 2;
966 write_blk_fifo(ft1000dev, &s_file,
967 &c_file, word_length);
970 if (pft1000info->
usbboot == 1) {
982 (
"FT1000:download: REQUEST_MAILBOX_DATA\n");
990 (
struct drv_msg *)&(pft1000info->
996 data = (
u16 *) & mailbox_data->
data[0];
998 if (word_length & 0x1)
1001 word_length = (word_length / 2);
1003 for (; word_length > 0; word_length--) {
1006 templong |= (*data++ << 16);
1009 (ft1000dev, dpram++,
1017 (
"FT1000:download:REQUEST_VERSION_INFO\n");
1027 (
u16 *) (pFileStart +
1032 if (word_length & 0x1)
1035 word_length = (word_length / 2);
1037 for (; word_length > 0; word_length--) {
1039 templong =
ntohs(*s_file++);
1040 temp =
ntohs(*s_file++);
1041 templong |= (temp << 16);
1044 (ft1000dev, dpram++,
1052 (
"FT1000:download:REQUEST_CODE_BY_VERSION\n");
1053 correct_version =
FALSE;
1069 requested_version) {
1070 correct_version =
TRUE;
1072 (
"FT1000:download: correct_version is TRUE\n");
1079 (
u8 *) (pFileStart +
1083 (
u8 *) (pFileStart +
1093 (
u32) dsp_img_info->
1101 if (!correct_version) {
1106 (
"FT1000:download:Download error: Bad Version Request = 0x%x.\n",
1107 (
int)requested_version);
1115 (
"FT1000:download:Download error: Bad request type=%d in CODE download state.\n",
1121 put_handshake_usb(ft1000dev,
1128 (
"FT1000:download:Download error: Handshake failed\n");
1135 DEBUG(
"FT1000:download:Code loader is done...\n");
1140 DEBUG(
"FT1000:download:STATE_SECTION_PROV\n");
1158 memcpy(pbuffer, (
void *)c_file,
1159 (
u32) (pseudo_header_len +
1175 (
u8 *) ((
unsigned long)
1177 (
u32) ((pseudo_header_len + 1) & 0xFFFFFFFE) +
sizeof(
struct pseudo_hdr));
1178 if ((
unsigned long)(c_file) -
1196 (
"ft1000:download: after STATE_SECTION_PROV, state = %d, status= %d\n",
1201 DEBUG(
"FT1000:download:STATE_DONE_PROV\n");
1229 DEBUG(
"Download exiting with status = 0x%8x\n", status);