1 // SPDX-License-Identifier: GPL-2.0-or-later
7 * Copyright (C) Altera Corporation 1998-2001
8 * Copyright (C) 2010,2011 NetUP Inc.
9 * Copyright (C) 2010,2011 Igor M. Liplianin <liplianin@netup.ru>
12 #include <linux/unaligned.h>
13 #include <linux/ctype.h>
14 #include <linux/string.h>
15 #include <linux/firmware.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <misc/altera.h>
19 #include "altera-exprt.h"
20 #include "altera-jtag.h"
23 module_param(debug
, int, 0644);
24 MODULE_PARM_DESC(debug
, "enable debugging information");
26 MODULE_DESCRIPTION("altera FPGA kernel module");
27 MODULE_AUTHOR("Igor M. Liplianin <liplianin@netup.ru>");
28 MODULE_LICENSE("GPL");
30 #define dprintk(args...) \
32 printk(KERN_DEBUG args); \
35 enum altera_fpga_opcode
{
113 struct altera_procinfo
{
116 struct altera_procinfo
*next
;
119 /* This function checks if enough parameters are available on the stack. */
120 static int altera_check_stack(int stack_ptr
, int count
, int *status
)
122 if (stack_ptr
< count
) {
123 *status
= -EOVERFLOW
;
130 static void altera_export_int(char *key
, s32 value
)
132 dprintk("Export: key = \"%s\", value = %d\n", key
, value
);
135 #define HEX_LINE_CHARS 72
136 #define HEX_LINE_BITS (HEX_LINE_CHARS * 4)
138 static void altera_export_bool_array(char *key
, u8
*data
, s32 count
)
140 char string
[HEX_LINE_CHARS
+ 1];
142 u32 size
, line
, lines
, linebits
, value
, j
, k
;
144 if (count
> HEX_LINE_BITS
) {
145 dprintk("Export: key = \"%s\", %d bits, value = HEX\n",
147 lines
= (count
+ (HEX_LINE_BITS
- 1)) / HEX_LINE_BITS
;
149 for (line
= 0; line
< lines
; ++line
) {
150 if (line
< (lines
- 1)) {
151 linebits
= HEX_LINE_BITS
;
152 size
= HEX_LINE_CHARS
;
153 offset
= count
- ((line
+ 1) * HEX_LINE_BITS
);
156 count
- ((lines
- 1) * HEX_LINE_BITS
);
157 size
= (linebits
+ 3) / 4;
165 for (k
= 0; k
< linebits
; ++k
) {
167 if (data
[i
>> 3] & (1 << (i
& 7)))
168 value
|= (1 << (i
& 3));
170 sprintf(&string
[j
], "%1x", value
);
176 sprintf(&string
[j
], "%1x", value
);
178 dprintk("%s\n", string
);
182 size
= (count
+ 3) / 4;
187 for (i
= 0; i
< count
; ++i
) {
188 if (data
[i
>> 3] & (1 << (i
& 7)))
189 value
|= (1 << (i
& 3));
191 sprintf(&string
[j
], "%1x", value
);
197 sprintf(&string
[j
], "%1x", value
);
199 dprintk("Export: key = \"%s\", %d bits, value = HEX %s\n",
204 static int altera_execute(struct altera_state
*astate
,
211 struct altera_config
*aconf
= astate
->config
;
212 char *msg_buff
= astate
->msg_buff
;
213 long *stack
= astate
->stack
;
216 u32 action_table
= 0L;
223 u32 action_count
= 0L;
227 s32
*var_size
= NULL
;
229 u8
*proc_attributes
= NULL
;
258 int current_proc
= 0;
263 dprintk("%s\n", __func__
);
265 /* Read header information */
266 if (program_size
> 52L) {
267 first_word
= get_unaligned_be32(&p
[0]);
268 version
= (first_word
& 1L);
269 *format_version
= version
+ 1;
272 action_table
= get_unaligned_be32(&p
[4]);
273 proc_table
= get_unaligned_be32(&p
[8]);
274 str_table
= get_unaligned_be32(&p
[4 + delta
]);
275 sym_table
= get_unaligned_be32(&p
[16 + delta
]);
276 data_sect
= get_unaligned_be32(&p
[20 + delta
]);
277 code_sect
= get_unaligned_be32(&p
[24 + delta
]);
278 debug_sect
= get_unaligned_be32(&p
[28 + delta
]);
279 action_count
= get_unaligned_be32(&p
[40 + delta
]);
280 proc_count
= get_unaligned_be32(&p
[44 + delta
]);
281 sym_count
= get_unaligned_be32(&p
[48 + (2 * delta
)]);
284 if ((first_word
!= 0x4A414D00L
) && (first_word
!= 0x4A414D01L
)) {
293 vars
= kcalloc(sym_count
, sizeof(long), GFP_KERNEL
);
299 var_size
= kcalloc(sym_count
, sizeof(s32
), GFP_KERNEL
);
301 if (var_size
== NULL
)
306 attrs
= kzalloc(sym_count
, GFP_KERNEL
);
312 if ((status
== 0) && (version
> 0)) {
313 proc_attributes
= kzalloc(proc_count
, GFP_KERNEL
);
315 if (proc_attributes
== NULL
)
324 for (i
= 0; i
< sym_count
; ++i
) {
325 offset
= (sym_table
+ ((11 + delta
) * i
));
327 value
= get_unaligned_be32(&p
[offset
+ 3 + delta
]);
329 attrs
[i
] = p
[offset
];
332 * use bit 7 of attribute byte to indicate that
333 * this buffer was dynamically allocated
334 * and should be freed later
338 var_size
[i
] = get_unaligned_be32(&p
[offset
+ 7 + delta
]);
342 * bit 0: 0 = read-only, 1 = read-write
343 * bit 1: 0 = not compressed, 1 = compressed
344 * bit 2: 0 = not initialized, 1 = initialized
345 * bit 3: 0 = scalar, 1 = array
346 * bit 4: 0 = Boolean, 1 = integer
347 * bit 5: 0 = declared variable,
348 * 1 = compiler created temporary variable
351 if ((attrs
[i
] & 0x0c) == 0x04)
352 /* initialized scalar variable */
354 else if ((attrs
[i
] & 0x1e) == 0x0e) {
355 /* initialized compressed Boolean array */
356 uncomp_size
= get_unaligned_le32(&p
[data_sect
+ value
]);
358 /* allocate a buffer for the uncompressed data */
359 vars
[i
] = (long)kzalloc(uncomp_size
, GFP_KERNEL
);
363 /* set flag so buffer will be freed later */
366 /* uncompress the data */
367 if (altera_shrink(&p
[data_sect
+ value
],
371 version
) != uncomp_size
)
372 /* decompression failed */
375 var_size
[i
] = uncomp_size
* 8L;
378 } else if ((attrs
[i
] & 0x1e) == 0x0c) {
379 /* initialized Boolean array */
380 vars
[i
] = value
+ data_sect
+ (long)p
;
381 } else if ((attrs
[i
] & 0x1c) == 0x1c) {
382 /* initialized integer array */
383 vars
[i
] = value
+ data_sect
;
384 } else if ((attrs
[i
] & 0x0c) == 0x08) {
385 /* uninitialized array */
387 /* flag attrs so that memory is freed */
390 if (var_size
[i
] > 0) {
395 size
= (var_size
[i
] * sizeof(s32
));
398 size
= ((var_size
[i
] + 7L) / 8L);
400 vars
[i
] = (long)kzalloc(size
, GFP_KERNEL
);
405 /* zero out memory */
406 for (j
= 0; j
< size
; ++j
)
407 ((u8
*)(vars
[i
]))[j
] = 0;
422 altera_jinit(astate
);
428 * For JBC version 2, we will execute the procedures corresponding to
429 * the selected ACTION
432 if (aconf
->action
== NULL
) {
436 int action_found
= 0;
437 for (i
= 0; (i
< action_count
) && !action_found
; ++i
) {
438 name_id
= get_unaligned_be32(&p
[action_table
+
441 name
= &p
[str_table
+ name_id
];
443 if (strncasecmp(aconf
->action
, name
, strlen(name
)) == 0) {
446 get_unaligned_be32(&p
[action_table
+
460 while ((i
!= 0) || first_time
) {
462 /* check procedure attribute byte */
475 i
= get_unaligned_be32(&p
[proc_table
+
480 * Set current_proc to the first procedure
485 ((proc_attributes
[i
] == 1) ||
486 ((proc_attributes
[i
] & 0xc0) == 0x40))) {
487 i
= get_unaligned_be32(&p
[proc_table
+
491 if ((i
!= 0) || ((i
== 0) && (current_proc
== 0) &&
492 ((proc_attributes
[0] != 1) &&
493 ((proc_attributes
[0] & 0xc0) != 0x40)))) {
496 get_unaligned_be32(&p
[proc_table
+
498 if ((pc
< code_sect
) || (pc
>= debug_sect
))
501 /* there are no procedures to execute! */
510 opcode
= (p
[pc
] & 0xff);
515 printk("opcode: %02x\n", opcode
);
517 arg_count
= (opcode
>> 6) & 3;
518 for (i
= 0; i
< arg_count
; ++i
) {
519 args
[i
] = get_unaligned_be32(&p
[pc
]);
527 if (altera_check_stack(stack_ptr
, 1, &status
)) {
528 stack
[stack_ptr
] = stack
[stack_ptr
- 1];
533 if (altera_check_stack(stack_ptr
, 2, &status
))
534 swap(stack
[stack_ptr
- 2], stack
[stack_ptr
- 1]);
537 if (altera_check_stack(stack_ptr
, 2, &status
)) {
539 stack
[stack_ptr
- 1] += stack
[stack_ptr
];
543 if (altera_check_stack(stack_ptr
, 2, &status
)) {
545 stack
[stack_ptr
- 1] -= stack
[stack_ptr
];
549 if (altera_check_stack(stack_ptr
, 2, &status
)) {
551 stack
[stack_ptr
- 1] *= stack
[stack_ptr
];
555 if (altera_check_stack(stack_ptr
, 2, &status
)) {
557 stack
[stack_ptr
- 1] /= stack
[stack_ptr
];
561 if (altera_check_stack(stack_ptr
, 2, &status
)) {
563 stack
[stack_ptr
- 1] %= stack
[stack_ptr
];
567 if (altera_check_stack(stack_ptr
, 2, &status
)) {
569 stack
[stack_ptr
- 1] <<= stack
[stack_ptr
];
573 if (altera_check_stack(stack_ptr
, 2, &status
)) {
575 stack
[stack_ptr
- 1] >>= stack
[stack_ptr
];
579 if (altera_check_stack(stack_ptr
, 1, &status
))
580 stack
[stack_ptr
- 1] ^= (-1L);
584 if (altera_check_stack(stack_ptr
, 2, &status
)) {
586 stack
[stack_ptr
- 1] &= stack
[stack_ptr
];
590 if (altera_check_stack(stack_ptr
, 2, &status
)) {
592 stack
[stack_ptr
- 1] |= stack
[stack_ptr
];
596 if (altera_check_stack(stack_ptr
, 2, &status
)) {
598 stack
[stack_ptr
- 1] ^= stack
[stack_ptr
];
602 if (!altera_check_stack(stack_ptr
, 1, &status
))
604 stack
[stack_ptr
- 1] = stack
[stack_ptr
- 1] ? 0L : 1L;
607 if (!altera_check_stack(stack_ptr
, 2, &status
))
610 stack
[stack_ptr
- 1] =
611 (stack
[stack_ptr
- 1] > stack
[stack_ptr
]) ?
616 if (!altera_check_stack(stack_ptr
, 2, &status
))
619 stack
[stack_ptr
- 1] =
620 (stack
[stack_ptr
- 1] < stack
[stack_ptr
]) ?
625 if ((version
> 0) && (stack_ptr
== 0)) {
627 * We completed one of the main procedures
629 * Find the next procedure
630 * to be executed and jump to it.
631 * If there are no more procedures, then EXIT.
633 i
= get_unaligned_be32(&p
[proc_table
+
634 (13 * current_proc
) + 4]);
636 ((proc_attributes
[i
] == 1) ||
637 ((proc_attributes
[i
] & 0xc0) == 0x40)))
638 i
= get_unaligned_be32(&p
[proc_table
+
642 /* no procedures to execute! */
644 *exit_code
= 0; /* success */
647 pc
= code_sect
+ get_unaligned_be32(
650 if ((pc
< code_sect
) ||
656 if (altera_check_stack(stack_ptr
, 1, &status
)) {
657 pc
= stack
[--stack_ptr
] + code_sect
;
658 if ((pc
<= code_sect
) ||
667 * Array short compare
668 * ...stack 0 is source 1 value
669 * ...stack 1 is source 2 value
670 * ...stack 2 is mask value
671 * ...stack 3 is count
673 if (altera_check_stack(stack_ptr
, 4, &status
)) {
674 s32 a
= stack
[--stack_ptr
];
675 s32 b
= stack
[--stack_ptr
];
676 long_tmp
= stack
[--stack_ptr
];
677 count
= stack
[stack_ptr
- 1];
679 if ((count
< 1) || (count
> 32))
682 long_tmp
&= ((-1L) >> (32 - count
));
684 stack
[stack_ptr
- 1] =
685 ((a
& long_tmp
) == (b
& long_tmp
))
693 * ...stack 0 is integer value
695 if (!altera_check_stack(stack_ptr
, 1, &status
))
697 sprintf(&msg_buff
[strlen(msg_buff
)],
698 "%ld", stack
[--stack_ptr
]);
703 printk(msg_buff
, "\n");
710 * ...stack 0 is scan data
711 * ...stack 1 is count
713 if (!altera_check_stack(stack_ptr
, 2, &status
))
715 long_tmp
= stack
[--stack_ptr
];
716 count
= stack
[--stack_ptr
];
717 put_unaligned_le32(long_tmp
, &charbuf
[0]);
718 status
= altera_drscan(astate
, count
, charbuf
, 0);
722 * DRSCAN short with capture
723 * ...stack 0 is scan data
724 * ...stack 1 is count
726 if (!altera_check_stack(stack_ptr
, 2, &status
))
728 long_tmp
= stack
[--stack_ptr
];
729 count
= stack
[stack_ptr
- 1];
730 put_unaligned_le32(long_tmp
, &charbuf
[0]);
731 status
= altera_swap_dr(astate
, count
, charbuf
,
733 stack
[stack_ptr
- 1] = get_unaligned_le32(&charbuf
[0]);
738 * ...stack 0 is scan data
739 * ...stack 1 is count
741 if (!altera_check_stack(stack_ptr
, 2, &status
))
743 long_tmp
= stack
[--stack_ptr
];
744 count
= stack
[--stack_ptr
];
745 put_unaligned_le32(long_tmp
, &charbuf
[0]);
746 status
= altera_irscan(astate
, count
, charbuf
, 0);
750 * IRSCAN short with capture
751 * ...stack 0 is scan data
752 * ...stack 1 is count
754 if (!altera_check_stack(stack_ptr
, 2, &status
))
756 long_tmp
= stack
[--stack_ptr
];
757 count
= stack
[stack_ptr
- 1];
758 put_unaligned_le32(long_tmp
, &charbuf
[0]);
759 status
= altera_swap_ir(astate
, count
, charbuf
,
761 stack
[stack_ptr
- 1] = get_unaligned_le32(&charbuf
[0]);
764 if (!altera_check_stack(stack_ptr
, 1, &status
))
766 count
= stack
[--stack_ptr
];
767 status
= altera_set_dr_pre(&astate
->js
, count
, 0, NULL
);
771 * DRPRE with literal data
772 * ...stack 0 is count
773 * ...stack 1 is literal data
775 if (!altera_check_stack(stack_ptr
, 2, &status
))
777 count
= stack
[--stack_ptr
];
778 long_tmp
= stack
[--stack_ptr
];
779 put_unaligned_le32(long_tmp
, &charbuf
[0]);
780 status
= altera_set_dr_pre(&astate
->js
, count
, 0,
786 * ...stack 0 is count
788 if (altera_check_stack(stack_ptr
, 1, &status
)) {
789 count
= stack
[--stack_ptr
];
790 status
= altera_set_dr_post(&astate
->js
, count
,
796 * DRPOST with literal data
797 * ...stack 0 is count
798 * ...stack 1 is literal data
800 if (!altera_check_stack(stack_ptr
, 2, &status
))
802 count
= stack
[--stack_ptr
];
803 long_tmp
= stack
[--stack_ptr
];
804 put_unaligned_le32(long_tmp
, &charbuf
[0]);
805 status
= altera_set_dr_post(&astate
->js
, count
, 0,
809 if (altera_check_stack(stack_ptr
, 1, &status
)) {
810 count
= stack
[--stack_ptr
];
811 status
= altera_set_ir_pre(&astate
->js
, count
,
817 * IRPRE with literal data
818 * ...stack 0 is count
819 * ...stack 1 is literal data
821 if (altera_check_stack(stack_ptr
, 2, &status
)) {
822 count
= stack
[--stack_ptr
];
823 long_tmp
= stack
[--stack_ptr
];
824 put_unaligned_le32(long_tmp
, &charbuf
[0]);
825 status
= altera_set_ir_pre(&astate
->js
, count
,
832 * ...stack 0 is count
834 if (altera_check_stack(stack_ptr
, 1, &status
)) {
835 count
= stack
[--stack_ptr
];
836 status
= altera_set_ir_post(&astate
->js
, count
,
842 * IRPOST with literal data
843 * ...stack 0 is count
844 * ...stack 1 is literal data
846 if (!altera_check_stack(stack_ptr
, 2, &status
))
848 count
= stack
[--stack_ptr
];
849 long_tmp
= stack
[--stack_ptr
];
850 put_unaligned_le32(long_tmp
, &charbuf
[0]);
851 status
= altera_set_ir_post(&astate
->js
, count
, 0,
855 if (altera_check_stack(stack_ptr
, 1, &status
)) {
857 count
= strlen(msg_buff
);
858 ch
= (char) stack
[--stack_ptr
];
859 if ((ch
< 1) || (ch
> 127)) {
861 * character code out of range
862 * instead of flagging an error,
863 * force the value to 127
867 msg_buff
[count
] = ch
;
868 msg_buff
[count
+ 1] = '\0';
872 if (altera_check_stack(stack_ptr
, 1, &status
))
873 *exit_code
= stack
[--stack_ptr
];
878 if (!altera_check_stack(stack_ptr
, 2, &status
))
881 stack
[stack_ptr
- 1] =
882 (stack
[stack_ptr
- 1] == stack
[stack_ptr
]) ?
886 if (altera_check_stack(stack_ptr
, 1, &status
))
891 if (!altera_check_stack(stack_ptr
, 1, &status
))
893 if (stack
[stack_ptr
- 1] < 0)
894 stack
[stack_ptr
- 1] = 0 - stack
[stack_ptr
- 1];
912 if (altera_check_stack(stack_ptr
, 2, &status
))
913 swap(stack
[stack_ptr
- 2], stack
[stack_ptr
- 1]);
917 if (altera_check_stack(stack_ptr
, index
, &status
))
918 swap(stack
[stack_ptr
- index
], stack
[stack_ptr
- 1]);
921 if (altera_check_stack(stack_ptr
, 2, &status
))
922 swap(stack
[stack_ptr
- 2], stack
[stack_ptr
- 1]);
926 if (altera_check_stack(stack_ptr
, index
, &status
))
927 swap(stack
[stack_ptr
- index
], stack
[stack_ptr
- 1]);
931 if (altera_check_stack(stack_ptr
, index
, &status
)) {
932 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
938 if (altera_check_stack(stack_ptr
, index
, &status
))
939 swap(stack
[stack_ptr
- index
], stack
[stack_ptr
- 1]);
942 if (altera_check_stack(stack_ptr
, 2, &status
))
943 swap(stack
[stack_ptr
- 2], stack
[stack_ptr
- 1]);
947 if (altera_check_stack(stack_ptr
, index
, &status
)) {
948 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
954 if (altera_check_stack(stack_ptr
, index
, &status
)) {
955 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
960 stack
[stack_ptr
++] = 0;
963 stack
[stack_ptr
++] = (s32
) args
[0];
966 stack
[stack_ptr
++] = vars
[args
[0]];
969 pc
= args
[0] + code_sect
;
970 if ((pc
< code_sect
) || (pc
>= debug_sect
))
974 stack
[stack_ptr
++] = pc
;
975 pc
= args
[0] + code_sect
;
976 if ((pc
< code_sect
) || (pc
>= debug_sect
))
981 * Process FOR / NEXT loop
982 * ...argument 0 is variable ID
983 * ...stack 0 is step value
984 * ...stack 1 is end value
985 * ...stack 2 is top address
987 if (altera_check_stack(stack_ptr
, 3, &status
)) {
988 s32 step
= stack
[stack_ptr
- 1];
989 s32 end
= stack
[stack_ptr
- 2];
990 s32 top
= stack
[stack_ptr
- 3];
991 s32 iterator
= vars
[args
[0]];
997 } else if (iterator
>= end
)
1003 vars
[args
[0]] = iterator
+ step
;
1004 pc
= top
+ code_sect
;
1005 if ((pc
< code_sect
) ||
1014 * ...argument 0 is string ID
1016 count
= strlen(msg_buff
);
1017 strscpy(&msg_buff
[count
],
1018 &p
[str_table
+ args
[0]],
1019 ALTERA_MESSAGE_LENGTH
- count
);
1023 * STATE intermediate state
1024 * ...argument 0 is state code
1026 status
= altera_goto_jstate(astate
, args
[0]);
1031 * ...argument 0 is state code
1033 status
= altera_goto_jstate(astate
, args
[0]);
1038 * ...argument 0 is state code
1040 status
= altera_set_irstop(&astate
->js
, args
[0]);
1045 * ...argument 0 is state code
1047 status
= altera_set_drstop(&astate
->js
, args
[0]);
1052 * Exchange top with Nth stack value
1053 * ...argument 0 is 0-based stack entry
1054 * to swap with top element
1056 index
= (args
[0]) + 1;
1057 if (altera_check_stack(stack_ptr
, index
, &status
))
1058 swap(stack
[stack_ptr
- index
], stack
[stack_ptr
- 1]);
1062 * Duplicate Nth stack value
1063 * ...argument 0 is 0-based stack entry to duplicate
1065 index
= (args
[0]) + 1;
1066 if (altera_check_stack(stack_ptr
, index
, &status
)) {
1067 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
1073 * Pop stack into scalar variable
1074 * ...argument 0 is variable ID
1075 * ...stack 0 is value
1077 if (altera_check_stack(stack_ptr
, 1, &status
))
1078 vars
[args
[0]] = stack
[--stack_ptr
];
1083 * Pop stack into integer array element
1084 * ...argument 0 is variable ID
1085 * ...stack 0 is array index
1086 * ...stack 1 is value
1088 if (!altera_check_stack(stack_ptr
, 2, &status
))
1090 variable_id
= args
[0];
1093 * If variable is read-only,
1094 * convert to writable array
1096 if ((version
> 0) &&
1097 ((attrs
[variable_id
] & 0x9c) == 0x1c)) {
1098 /* Allocate a writable buffer for this array */
1099 count
= var_size
[variable_id
];
1100 long_tmp
= vars
[variable_id
];
1101 longptr_tmp
= kcalloc(count
, sizeof(long),
1103 vars
[variable_id
] = (long)longptr_tmp
;
1105 if (vars
[variable_id
] == 0) {
1110 /* copy previous contents into buffer */
1111 for (i
= 0; i
< count
; ++i
) {
1113 get_unaligned_be32(&p
[long_tmp
]);
1114 long_tmp
+= sizeof(long);
1118 * set bit 7 - buffer was
1119 * dynamically allocated
1121 attrs
[variable_id
] |= 0x80;
1123 /* clear bit 2 - variable is writable */
1124 attrs
[variable_id
] &= ~0x04;
1125 attrs
[variable_id
] |= 0x01;
1129 /* check that variable is a writable integer array */
1130 if ((attrs
[variable_id
] & 0x1c) != 0x18)
1133 longptr_tmp
= (long *)vars
[variable_id
];
1135 /* pop the array index */
1136 index
= stack
[--stack_ptr
];
1138 /* pop the value and store it into the array */
1139 longptr_tmp
[index
] = stack
[--stack_ptr
];
1145 * Pop stack into Boolean array
1146 * ...argument 0 is variable ID
1147 * ...stack 0 is count
1148 * ...stack 1 is array index
1149 * ...stack 2 is value
1151 if (!altera_check_stack(stack_ptr
, 3, &status
))
1153 variable_id
= args
[0];
1156 * If variable is read-only,
1157 * convert to writable array
1159 if ((version
> 0) &&
1160 ((attrs
[variable_id
] & 0x9c) == 0x0c)) {
1161 /* Allocate a writable buffer for this array */
1163 (var_size
[variable_id
] + 7L) >> 3L;
1164 charptr_tmp2
= (u8
*)vars
[variable_id
];
1166 kzalloc(long_tmp
, GFP_KERNEL
);
1167 vars
[variable_id
] = (long)charptr_tmp
;
1169 if (vars
[variable_id
] == 0) {
1174 /* zero the buffer */
1176 long_idx
< long_tmp
;
1178 charptr_tmp
[long_idx
] = 0;
1181 /* copy previous contents into buffer */
1183 long_idx
< var_size
[variable_id
];
1185 long_idx2
= long_idx
;
1187 if (charptr_tmp2
[long_idx2
>> 3] &
1188 (1 << (long_idx2
& 7))) {
1189 charptr_tmp
[long_idx
>> 3] |=
1190 (1 << (long_idx
& 7));
1195 * set bit 7 - buffer was
1196 * dynamically allocated
1198 attrs
[variable_id
] |= 0x80;
1200 /* clear bit 2 - variable is writable */
1201 attrs
[variable_id
] &= ~0x04;
1202 attrs
[variable_id
] |= 0x01;
1207 * check that variable is
1208 * a writable Boolean array
1210 if ((attrs
[variable_id
] & 0x1c) != 0x08) {
1215 charptr_tmp
= (u8
*)vars
[variable_id
];
1217 /* pop the count (number of bits to copy) */
1218 long_count
= stack
[--stack_ptr
];
1220 /* pop the array index */
1221 long_idx
= stack
[--stack_ptr
];
1227 * stack 0 = array right index
1228 * stack 1 = array left index
1231 if (long_idx
> long_count
) {
1233 long_tmp
= long_count
;
1234 long_count
= 1 + long_idx
-
1236 long_idx
= long_tmp
;
1238 /* reverse POPA is not supported */
1242 long_count
= 1 + long_count
-
1248 long_tmp
= stack
[--stack_ptr
];
1250 if (long_count
< 1) {
1255 for (i
= 0; i
< long_count
; ++i
) {
1256 if (long_tmp
& (1L << (s32
) i
))
1257 charptr_tmp
[long_idx
>> 3L] |=
1258 (1L << (long_idx
& 7L));
1260 charptr_tmp
[long_idx
>> 3L] &=
1261 ~(1L << (long_idx
& 7L));
1269 * Pop stack and branch if zero
1270 * ...argument 0 is address
1271 * ...stack 0 is condition value
1273 if (altera_check_stack(stack_ptr
, 1, &status
)) {
1274 if (stack
[--stack_ptr
] == 0) {
1275 pc
= args
[0] + code_sect
;
1276 if ((pc
< code_sect
) ||
1287 * ...argument 0 is scan data variable ID
1288 * ...stack 0 is array index
1289 * ...stack 1 is count
1291 if (!altera_check_stack(stack_ptr
, 2, &status
))
1293 long_idx
= stack
[--stack_ptr
];
1294 long_count
= stack
[--stack_ptr
];
1298 * stack 0 = array right index
1299 * stack 1 = array left index
1302 long_tmp
= long_count
;
1303 long_count
= stack
[--stack_ptr
];
1305 if (long_idx
> long_tmp
) {
1307 long_idx
= long_tmp
;
1311 charptr_tmp
= (u8
*)vars
[args
[0]];
1316 * and reverse the data order
1318 charptr_tmp2
= charptr_tmp
;
1319 charptr_tmp
= kzalloc((long_count
>> 3) + 1,
1321 if (charptr_tmp
== NULL
) {
1326 long_tmp
= long_idx
+ long_count
- 1;
1328 while (long_idx2
< long_count
) {
1329 if (charptr_tmp2
[long_tmp
>> 3] &
1330 (1 << (long_tmp
& 7)))
1331 charptr_tmp
[long_idx2
>> 3] |=
1332 (1 << (long_idx2
& 7));
1334 charptr_tmp
[long_idx2
>> 3] &=
1335 ~(1 << (long_idx2
& 7));
1342 if (opcode
== 0x51) /* DS */
1343 status
= altera_drscan(astate
, long_count
,
1344 charptr_tmp
, long_idx
);
1346 status
= altera_irscan(astate
, long_count
,
1347 charptr_tmp
, long_idx
);
1355 * DRPRE with array data
1356 * ...argument 0 is variable ID
1357 * ...stack 0 is array index
1358 * ...stack 1 is count
1360 if (!altera_check_stack(stack_ptr
, 2, &status
))
1362 index
= stack
[--stack_ptr
];
1363 count
= stack
[--stack_ptr
];
1367 * stack 0 = array right index
1368 * stack 1 = array left index
1370 count
= 1 + count
- index
;
1372 charptr_tmp
= (u8
*)vars
[args
[0]];
1373 status
= altera_set_dr_pre(&astate
->js
, count
, index
,
1378 * DRPOST with array data
1379 * ...argument 0 is variable ID
1380 * ...stack 0 is array index
1381 * ...stack 1 is count
1383 if (!altera_check_stack(stack_ptr
, 2, &status
))
1385 index
= stack
[--stack_ptr
];
1386 count
= stack
[--stack_ptr
];
1390 * stack 0 = array right index
1391 * stack 1 = array left index
1393 count
= 1 + count
- index
;
1395 charptr_tmp
= (u8
*)vars
[args
[0]];
1396 status
= altera_set_dr_post(&astate
->js
, count
, index
,
1401 * IRPRE with array data
1402 * ...argument 0 is variable ID
1403 * ...stack 0 is array index
1404 * ...stack 1 is count
1406 if (!altera_check_stack(stack_ptr
, 2, &status
))
1408 index
= stack
[--stack_ptr
];
1409 count
= stack
[--stack_ptr
];
1413 * stack 0 = array right index
1414 * stack 1 = array left index
1416 count
= 1 + count
- index
;
1418 charptr_tmp
= (u8
*)vars
[args
[0]];
1419 status
= altera_set_ir_pre(&astate
->js
, count
, index
,
1425 * IRPOST with array data
1426 * ...argument 0 is variable ID
1427 * ...stack 0 is array index
1428 * ...stack 1 is count
1430 if (!altera_check_stack(stack_ptr
, 2, &status
))
1432 index
= stack
[--stack_ptr
];
1433 count
= stack
[--stack_ptr
];
1437 * stack 0 = array right index
1438 * stack 1 = array left index
1440 count
= 1 + count
- index
;
1442 charptr_tmp
= (u8
*)vars
[args
[0]];
1443 status
= altera_set_ir_post(&astate
->js
, count
, index
,
1450 * ...argument 0 is string ID
1451 * ...stack 0 is integer expression
1453 if (altera_check_stack(stack_ptr
, 1, &status
)) {
1454 name
= &p
[str_table
+ args
[0]];
1455 long_tmp
= stack
[--stack_ptr
];
1456 altera_export_int(name
, long_tmp
);
1461 * Push integer array element
1462 * ...argument 0 is variable ID
1463 * ...stack 0 is array index
1465 if (!altera_check_stack(stack_ptr
, 1, &status
))
1467 variable_id
= args
[0];
1468 index
= stack
[stack_ptr
- 1];
1470 /* check variable type */
1471 if ((attrs
[variable_id
] & 0x1f) == 0x19) {
1472 /* writable integer array */
1473 longptr_tmp
= (long *)vars
[variable_id
];
1474 stack
[stack_ptr
- 1] = longptr_tmp
[index
];
1475 } else if ((attrs
[variable_id
] & 0x1f) == 0x1c) {
1476 /* read-only integer array */
1477 long_tmp
= vars
[variable_id
] +
1478 (index
* sizeof(long));
1479 stack
[stack_ptr
- 1] =
1480 get_unaligned_be32(&p
[long_tmp
]);
1487 * Push Boolean array
1488 * ...argument 0 is variable ID
1489 * ...stack 0 is count
1490 * ...stack 1 is array index
1492 if (!altera_check_stack(stack_ptr
, 2, &status
))
1494 variable_id
= args
[0];
1496 /* check that variable is a Boolean array */
1497 if ((attrs
[variable_id
] & 0x18) != 0x08) {
1502 charptr_tmp
= (u8
*)vars
[variable_id
];
1504 /* pop the count (number of bits to copy) */
1505 count
= stack
[--stack_ptr
];
1507 /* pop the array index */
1508 index
= stack
[stack_ptr
- 1];
1512 * stack 0 = array right index
1513 * stack 1 = array left index
1515 count
= 1 + count
- index
;
1517 if ((count
< 1) || (count
> 32)) {
1524 for (i
= 0; i
< count
; ++i
)
1525 if (charptr_tmp
[(i
+ index
) >> 3] &
1526 (1 << ((i
+ index
) & 7)))
1527 long_tmp
|= (1L << i
);
1529 stack
[stack_ptr
- 1] = long_tmp
;
1534 * Dynamically change size of array
1535 * ...argument 0 is variable ID
1536 * ...stack 0 is new size
1538 if (!altera_check_stack(stack_ptr
, 1, &status
))
1540 variable_id
= args
[0];
1541 long_tmp
= stack
[--stack_ptr
];
1543 if (long_tmp
> var_size
[variable_id
]) {
1544 var_size
[variable_id
] = long_tmp
;
1546 if (attrs
[variable_id
] & 0x10)
1547 /* allocate integer array */
1548 long_tmp
*= sizeof(long);
1550 /* allocate Boolean array */
1551 long_tmp
= (long_tmp
+ 7) >> 3;
1554 * If the buffer was previously allocated,
1557 if (attrs
[variable_id
] & 0x80) {
1558 kfree((void *)vars
[variable_id
]);
1559 vars
[variable_id
] = 0;
1563 * Allocate a new buffer
1564 * of the requested size
1566 vars
[variable_id
] = (long)
1567 kzalloc(long_tmp
, GFP_KERNEL
);
1569 if (vars
[variable_id
] == 0) {
1575 * Set the attribute bit to indicate that
1576 * this buffer was dynamically allocated and
1577 * should be freed later
1579 attrs
[variable_id
] |= 0x80;
1581 /* zero out memory */
1582 count
= ((var_size
[variable_id
] + 7L) /
1584 charptr_tmp
= (u8
*)(vars
[variable_id
]);
1585 for (index
= 0; index
< count
; ++index
)
1586 charptr_tmp
[index
] = 0;
1593 * Export Boolean array
1594 * ...argument 0 is string ID
1595 * ...stack 0 is variable ID
1596 * ...stack 1 is array right index
1597 * ...stack 2 is array left index
1599 if (!altera_check_stack(stack_ptr
, 3, &status
))
1602 /* EXPV is not supported in JBC 1.0 */
1606 name
= &p
[str_table
+ args
[0]];
1607 variable_id
= stack
[--stack_ptr
];
1608 long_idx
= stack
[--stack_ptr
];/* right indx */
1609 long_idx2
= stack
[--stack_ptr
];/* left indx */
1611 if (long_idx
> long_idx2
) {
1612 /* reverse indices not supported */
1617 long_count
= 1 + long_idx2
- long_idx
;
1619 charptr_tmp
= (u8
*)vars
[variable_id
];
1620 charptr_tmp2
= NULL
;
1622 if ((long_idx
& 7L) != 0) {
1625 kzalloc(((long_count
+ 7L) / 8L),
1627 if (charptr_tmp2
== NULL
) {
1632 for (i
= 0; i
< long_count
; ++i
) {
1633 if (charptr_tmp
[k
>> 3] &
1635 charptr_tmp2
[i
>> 3] |=
1638 charptr_tmp2
[i
>> 3] &=
1643 charptr_tmp
= charptr_tmp2
;
1645 } else if (long_idx
!= 0)
1646 charptr_tmp
= &charptr_tmp
[long_idx
>> 3];
1648 altera_export_bool_array(name
, charptr_tmp
,
1651 /* free allocated buffer */
1652 if ((long_idx
& 7L) != 0)
1653 kfree(charptr_tmp2
);
1659 * ...argument 0 is dest ID
1660 * ...argument 1 is source ID
1661 * ...stack 0 is count
1662 * ...stack 1 is dest index
1663 * ...stack 2 is source index
1671 int src_reverse
= 0;
1672 int dest_reverse
= 0;
1674 if (!altera_check_stack(stack_ptr
, 3, &status
))
1677 copy_count
= stack
[--stack_ptr
];
1678 copy_index
= stack
[--stack_ptr
];
1679 copy_index2
= stack
[--stack_ptr
];
1684 * stack 0 = source right index
1685 * stack 1 = source left index
1686 * stack 2 = destination right index
1687 * stack 3 = destination left index
1689 destleft
= stack
[--stack_ptr
];
1691 if (copy_count
> copy_index
) {
1694 src_count
= 1 + copy_count
- copy_index
;
1695 /* copy_index = source start index */
1697 src_count
= 1 + copy_index
- copy_count
;
1698 /* source start index */
1699 copy_index
= copy_count
;
1702 if (copy_index2
> destleft
) {
1705 dest_count
= 1 + copy_index2
- destleft
;
1706 /* destination start index */
1707 copy_index2
= destleft
;
1709 dest_count
= 1 + destleft
- copy_index2
;
1711 copy_count
= (src_count
< dest_count
) ?
1712 src_count
: dest_count
;
1714 if ((src_reverse
|| dest_reverse
) &&
1715 (src_count
!= dest_count
))
1717 * If either the source or destination
1718 * is reversed, we can't tolerate
1719 * a length mismatch, because we
1720 * "left justify" arrays when copying.
1721 * This won't work correctly
1722 * with reversed arrays.
1730 index2
= copy_index2
;
1733 * If destination is a read-only array,
1734 * allocate a buffer and convert it to a writable array
1736 variable_id
= args
[1];
1737 if ((version
> 0) &&
1738 ((attrs
[variable_id
] & 0x9c) == 0x0c)) {
1739 /* Allocate a writable buffer for this array */
1741 (var_size
[variable_id
] + 7L) >> 3L;
1742 charptr_tmp2
= (u8
*)vars
[variable_id
];
1744 kzalloc(long_tmp
, GFP_KERNEL
);
1745 vars
[variable_id
] = (long)charptr_tmp
;
1747 if (vars
[variable_id
] == 0) {
1752 /* zero the buffer */
1753 for (long_idx
= 0L; long_idx
< long_tmp
;
1755 charptr_tmp
[long_idx
] = 0;
1757 /* copy previous contents into buffer */
1759 long_idx
< var_size
[variable_id
];
1761 long_idx2
= long_idx
;
1763 if (charptr_tmp2
[long_idx2
>> 3] &
1764 (1 << (long_idx2
& 7)))
1765 charptr_tmp
[long_idx
>> 3] |=
1766 (1 << (long_idx
& 7));
1771 set bit 7 - buffer was dynamically allocated */
1772 attrs
[variable_id
] |= 0x80;
1774 /* clear bit 2 - variable is writable */
1775 attrs
[variable_id
] &= ~0x04;
1776 attrs
[variable_id
] |= 0x01;
1779 charptr_tmp
= (u8
*)vars
[args
[1]];
1780 charptr_tmp2
= (u8
*)vars
[args
[0]];
1782 /* check if destination is a writable Boolean array */
1783 if ((attrs
[args
[1]] & 0x1c) != 0x08) {
1794 index2
+= (count
- 1);
1796 for (i
= 0; i
< count
; ++i
) {
1797 if (charptr_tmp2
[index
>> 3] &
1799 charptr_tmp
[index2
>> 3] |=
1800 (1 << (index2
& 7));
1802 charptr_tmp
[index2
>> 3] &=
1803 ~(1 << (index2
& 7));
1817 * DRSCAN with capture
1818 * IRSCAN with capture
1819 * ...argument 0 is scan data variable ID
1820 * ...argument 1 is capture variable ID
1821 * ...stack 0 is capture index
1822 * ...stack 1 is scan data index
1823 * ...stack 2 is count
1825 s32 scan_right
, scan_left
;
1826 s32 capture_count
= 0;
1831 if (!altera_check_stack(stack_ptr
, 3, &status
))
1834 capture_index
= stack
[--stack_ptr
];
1835 scan_index
= stack
[--stack_ptr
];
1839 * stack 0 = capture right index
1840 * stack 1 = capture left index
1841 * stack 2 = scan right index
1842 * stack 3 = scan left index
1845 scan_right
= stack
[--stack_ptr
];
1846 scan_left
= stack
[--stack_ptr
];
1847 capture_count
= 1 + scan_index
- capture_index
;
1848 scan_count
= 1 + scan_left
- scan_right
;
1849 scan_index
= scan_right
;
1852 long_count
= stack
[--stack_ptr
];
1854 * If capture array is read-only, allocate a buffer
1855 * and convert it to a writable array
1857 variable_id
= args
[1];
1858 if ((version
> 0) &&
1859 ((attrs
[variable_id
] & 0x9c) == 0x0c)) {
1860 /* Allocate a writable buffer for this array */
1862 (var_size
[variable_id
] + 7L) >> 3L;
1863 charptr_tmp2
= (u8
*)vars
[variable_id
];
1865 kzalloc(long_tmp
, GFP_KERNEL
);
1866 vars
[variable_id
] = (long)charptr_tmp
;
1868 if (vars
[variable_id
] == 0) {
1873 /* zero the buffer */
1874 for (long_idx
= 0L; long_idx
< long_tmp
;
1876 charptr_tmp
[long_idx
] = 0;
1878 /* copy previous contents into buffer */
1880 long_idx
< var_size
[variable_id
];
1882 long_idx2
= long_idx
;
1884 if (charptr_tmp2
[long_idx2
>> 3] &
1885 (1 << (long_idx2
& 7)))
1886 charptr_tmp
[long_idx
>> 3] |=
1887 (1 << (long_idx
& 7));
1892 * set bit 7 - buffer was
1893 * dynamically allocated
1895 attrs
[variable_id
] |= 0x80;
1897 /* clear bit 2 - variable is writable */
1898 attrs
[variable_id
] &= ~0x04;
1899 attrs
[variable_id
] |= 0x01;
1903 charptr_tmp
= (u8
*)vars
[args
[0]];
1904 charptr_tmp2
= (u8
*)vars
[args
[1]];
1906 if ((version
> 0) &&
1907 ((long_count
> capture_count
) ||
1908 (long_count
> scan_count
))) {
1914 * check that capture array
1915 * is a writable Boolean array
1917 if ((attrs
[args
[1]] & 0x1c) != 0x08) {
1923 if (opcode
== 0x82) /* DSC */
1924 status
= altera_swap_dr(astate
,
1931 status
= altera_swap_ir(astate
,
1945 * ...argument 0 is wait state
1946 * ...argument 1 is end state
1947 * ...stack 0 is cycles
1948 * ...stack 1 is microseconds
1950 if (!altera_check_stack(stack_ptr
, 2, &status
))
1952 long_tmp
= stack
[--stack_ptr
];
1955 status
= altera_wait_cycles(astate
, long_tmp
,
1958 long_tmp
= stack
[--stack_ptr
];
1960 if ((status
== 0) && (long_tmp
!= 0L))
1961 status
= altera_wait_msecs(astate
,
1965 if ((status
== 0) && (args
[1] != args
[0]))
1966 status
= altera_goto_jstate(astate
,
1970 --stack_ptr
; /* throw away MAX cycles */
1971 --stack_ptr
; /* throw away MAX microseconds */
1977 * ...argument 0 is source 1 ID
1978 * ...argument 1 is source 2 ID
1979 * ...argument 2 is mask ID
1980 * ...stack 0 is source 1 index
1981 * ...stack 1 is source 2 index
1982 * ...stack 2 is mask index
1983 * ...stack 3 is count
1986 u8
*source1
= (u8
*)vars
[args
[0]];
1987 u8
*source2
= (u8
*)vars
[args
[1]];
1988 u8
*mask
= (u8
*)vars
[args
[2]];
1993 if (!altera_check_stack(stack_ptr
, 4, &status
))
1996 index1
= stack
[--stack_ptr
];
1997 index2
= stack
[--stack_ptr
];
1998 mask_index
= stack
[--stack_ptr
];
1999 long_count
= stack
[--stack_ptr
];
2003 * stack 0 = source 1 right index
2004 * stack 1 = source 1 left index
2005 * stack 2 = source 2 right index
2006 * stack 3 = source 2 left index
2007 * stack 4 = mask right index
2008 * stack 5 = mask left index
2010 s32 mask_right
= stack
[--stack_ptr
];
2011 s32 mask_left
= stack
[--stack_ptr
];
2012 /* source 1 count */
2013 a
= 1 + index2
- index1
;
2014 /* source 2 count */
2015 b
= 1 + long_count
- mask_index
;
2016 a
= (a
< b
) ? a
: b
;
2018 b
= 1 + mask_left
- mask_right
;
2019 a
= (a
< b
) ? a
: b
;
2020 /* source 2 start index */
2021 index2
= mask_index
;
2022 /* mask start index */
2023 mask_index
= mask_right
;
2034 for (i
= 0; i
< count
; ++i
) {
2035 if (mask
[mask_index
>> 3] &
2036 (1 << (mask_index
& 7))) {
2037 a
= source1
[index1
>> 3] &
2040 b
= source2
[index2
>> 3] &
2044 if (a
!= b
) /* failure */
2053 stack
[stack_ptr
++] = long_tmp
;
2058 /* Unrecognized opcode -- ERROR! */
2066 if ((stack_ptr
< 0) || (stack_ptr
>= ALTERA_STACK_SIZE
))
2067 status
= -EOVERFLOW
;
2071 *error_address
= (s32
)(opcode_address
- code_sect
);
2075 altera_free_buffers(astate
);
2077 /* Free all dynamically allocated arrays */
2078 if ((attrs
!= NULL
) && (vars
!= NULL
))
2079 for (i
= 0; i
< sym_count
; ++i
)
2080 if (attrs
[i
] & 0x80)
2081 kfree((void *)vars
[i
]);
2086 kfree(proc_attributes
);
2091 static int altera_get_note(u8
*p
, s32 program_size
, s32
*offset
,
2092 char *key
, char *value
, int keylen
, int vallen
)
2094 * Gets key and value of NOTE fields in the JBC file.
2095 * Can be called in two modes: if offset pointer is NULL,
2096 * then the function searches for note fields which match
2097 * the key string provided. If offset is not NULL, then
2098 * the function finds the next note field of any key,
2099 * starting at the offset specified by the offset pointer.
2100 * Returns 0 for success, else appropriate error code
2103 int status
= -ENODATA
;
2104 u32 note_strings
= 0L;
2105 u32 note_table
= 0L;
2106 u32 note_count
= 0L;
2107 u32 first_word
= 0L;
2114 /* Read header information */
2115 if (program_size
> 52L) {
2116 first_word
= get_unaligned_be32(&p
[0]);
2117 version
= (first_word
& 1L);
2118 delta
= version
* 8;
2120 note_strings
= get_unaligned_be32(&p
[8 + delta
]);
2121 note_table
= get_unaligned_be32(&p
[12 + delta
]);
2122 note_count
= get_unaligned_be32(&p
[44 + (2 * delta
)]);
2125 if ((first_word
!= 0x4A414D00L
) && (first_word
!= 0x4A414D01L
))
2128 if (note_count
<= 0L)
2131 if (offset
== NULL
) {
2133 * We will search for the first note with a specific key,
2134 * and return only the value
2136 for (i
= 0; (i
< note_count
) &&
2137 (status
!= 0); ++i
) {
2138 key_ptr
= &p
[note_strings
+
2140 &p
[note_table
+ (8 * i
)])];
2141 if (key
&& !strncasecmp(key
, key_ptr
, strlen(key_ptr
))) {
2144 value_ptr
= &p
[note_strings
+
2146 &p
[note_table
+ (8 * i
) + 4])];
2149 strscpy(value
, value_ptr
, vallen
);
2155 * We will search for the next note, regardless of the key,
2156 * and return both the value and the key
2161 if ((i
>= 0) && (i
< note_count
)) {
2165 strscpy(key
, &p
[note_strings
+
2167 &p
[note_table
+ (8 * i
)])],
2171 strscpy(value
, &p
[note_strings
+
2173 &p
[note_table
+ (8 * i
) + 4])],
2183 static int altera_check_crc(u8
*p
, s32 program_size
)
2186 u16 local_expected
= 0,
2192 u32 crc_section
= 0L;
2193 u32 first_word
= 0L;
2197 if (program_size
> 52L) {
2198 first_word
= get_unaligned_be32(&p
[0]);
2199 version
= (first_word
& 1L);
2200 delta
= version
* 8;
2202 crc_section
= get_unaligned_be32(&p
[32 + delta
]);
2205 if ((first_word
!= 0x4A414D00L
) && (first_word
!= 0x4A414D01L
))
2208 if (crc_section
>= program_size
)
2212 local_expected
= (u16
)get_unaligned_be16(&p
[crc_section
]);
2214 for (i
= 0; i
< crc_section
; ++i
) {
2216 for (bit
= 0; bit
< 8; bit
++) {
2217 feedback
= (databyte
^ shift_reg
) & 0x01;
2220 shift_reg
^= 0x8408;
2226 local_actual
= (u16
)~shift_reg
;
2228 if (local_expected
!= local_actual
)
2233 if (debug
|| status
) {
2236 printk(KERN_INFO
"%s: CRC matched: %04x\n", __func__
,
2240 printk(KERN_ERR
"%s: CRC mismatch: expected %04x, "
2241 "actual %04x\n", __func__
, local_expected
,
2245 printk(KERN_ERR
"%s: error: format isn't "
2246 "recognized.\n", __func__
);
2249 printk(KERN_ERR
"%s: CRC function returned error "
2250 "code %d\n", __func__
, status
);
2258 static int altera_get_file_info(u8
*p
,
2260 int *format_version
,
2262 int *procedure_count
)
2268 if (program_size
<= 52L)
2271 first_word
= get_unaligned_be32(&p
[0]);
2273 if ((first_word
== 0x4A414D00L
) || (first_word
== 0x4A414D01L
)) {
2276 version
= (first_word
& 1L);
2277 *format_version
= version
+ 1;
2280 *action_count
= get_unaligned_be32(&p
[48]);
2281 *procedure_count
= get_unaligned_be32(&p
[52]);
2288 static int altera_get_act_info(u8
*p
,
2293 struct altera_procinfo
**proc_list
)
2296 struct altera_procinfo
*procptr
= NULL
;
2297 struct altera_procinfo
*tmpptr
= NULL
;
2298 u32 first_word
= 0L;
2299 u32 action_table
= 0L;
2300 u32 proc_table
= 0L;
2302 u32 note_strings
= 0L;
2303 u32 action_count
= 0L;
2304 u32 proc_count
= 0L;
2305 u32 act_name_id
= 0L;
2306 u32 act_desc_id
= 0L;
2307 u32 act_proc_id
= 0L;
2308 u32 act_proc_name
= 0L;
2309 u8 act_proc_attribute
= 0;
2311 if (program_size
<= 52L)
2313 /* Read header information */
2314 first_word
= get_unaligned_be32(&p
[0]);
2316 if (first_word
!= 0x4A414D01L
)
2319 action_table
= get_unaligned_be32(&p
[4]);
2320 proc_table
= get_unaligned_be32(&p
[8]);
2321 str_table
= get_unaligned_be32(&p
[12]);
2322 note_strings
= get_unaligned_be32(&p
[16]);
2323 action_count
= get_unaligned_be32(&p
[48]);
2324 proc_count
= get_unaligned_be32(&p
[52]);
2326 if (index
>= action_count
)
2329 act_name_id
= get_unaligned_be32(&p
[action_table
+ (12 * index
)]);
2330 act_desc_id
= get_unaligned_be32(&p
[action_table
+ (12 * index
) + 4]);
2331 act_proc_id
= get_unaligned_be32(&p
[action_table
+ (12 * index
) + 8]);
2333 *name
= &p
[str_table
+ act_name_id
];
2335 if (act_desc_id
< (note_strings
- str_table
))
2336 *description
= &p
[str_table
+ act_desc_id
];
2339 act_proc_name
= get_unaligned_be32(
2340 &p
[proc_table
+ (13 * act_proc_id
)]);
2341 act_proc_attribute
=
2342 (p
[proc_table
+ (13 * act_proc_id
) + 8] & 0x03);
2345 kzalloc(sizeof(struct altera_procinfo
),
2348 if (procptr
== NULL
)
2351 procptr
->name
= &p
[str_table
+ act_proc_name
];
2352 procptr
->attrs
= act_proc_attribute
;
2353 procptr
->next
= NULL
;
2355 /* add record to end of linked list */
2356 if (*proc_list
== NULL
)
2357 *proc_list
= procptr
;
2359 tmpptr
= *proc_list
;
2360 while (tmpptr
->next
!= NULL
)
2361 tmpptr
= tmpptr
->next
;
2362 tmpptr
->next
= procptr
;
2366 act_proc_id
= get_unaligned_be32(
2367 &p
[proc_table
+ (13 * act_proc_id
) + 4]);
2368 } while ((act_proc_id
!= 0) && (act_proc_id
< proc_count
));
2373 int altera_init(struct altera_config
*config
, const struct firmware
*fw
)
2375 struct altera_state
*astate
= NULL
;
2376 struct altera_procinfo
*proc_list
= NULL
;
2377 struct altera_procinfo
*procptr
= NULL
;
2380 char *action_name
= NULL
;
2381 char *description
= NULL
;
2382 int exec_result
= 0;
2384 int format_version
= 0;
2385 int action_count
= 0;
2386 int procedure_count
= 0;
2389 s32 error_address
= 0L;
2392 key
= kzalloc(33, GFP_KERNEL
);
2397 value
= kzalloc(257, GFP_KERNEL
);
2402 astate
= kzalloc(sizeof(struct altera_state
), GFP_KERNEL
);
2408 astate
->config
= config
;
2409 if (!astate
->config
->jtag_io
) {
2410 if (!IS_ENABLED(CONFIG_HAS_IOPORT
)) {
2414 dprintk("%s: using byteblaster!\n", __func__
);
2415 astate
->config
->jtag_io
= netup_jtag_io_lpt
;
2418 altera_check_crc((u8
*)fw
->data
, fw
->size
);
2421 altera_get_file_info((u8
*)fw
->data
, fw
->size
, &format_version
,
2422 &action_count
, &procedure_count
);
2423 printk(KERN_INFO
"%s: File format is %s ByteCode format\n",
2424 __func__
, (format_version
== 2) ? "Jam STAPL" :
2425 "pre-standardized Jam 1.1");
2426 while (altera_get_note((u8
*)fw
->data
, fw
->size
,
2427 &offset
, key
, value
, 32, 256) == 0)
2428 printk(KERN_INFO
"%s: NOTE \"%s\" = \"%s\"\n",
2429 __func__
, key
, value
);
2432 if (debug
&& (format_version
== 2) && (action_count
> 0)) {
2433 printk(KERN_INFO
"%s: Actions available:\n", __func__
);
2434 for (index
= 0; index
< action_count
; ++index
) {
2435 altera_get_act_info((u8
*)fw
->data
, fw
->size
,
2436 index
, &action_name
,
2440 if (description
== NULL
)
2441 printk(KERN_INFO
"%s: %s\n",
2445 printk(KERN_INFO
"%s: %s \"%s\"\n",
2450 procptr
= proc_list
;
2451 while (procptr
!= NULL
) {
2452 if (procptr
->attrs
!= 0)
2453 printk(KERN_INFO
"%s: %s (%s)\n",
2456 (procptr
->attrs
== 1) ?
2457 "optional" : "recommended");
2459 proc_list
= procptr
->next
;
2461 procptr
= proc_list
;
2465 printk(KERN_INFO
"\n");
2468 exec_result
= altera_execute(astate
, (u8
*)fw
->data
, fw
->size
,
2469 &error_address
, &exit_code
, &format_version
);
2472 exec_result
= -EREMOTEIO
;
2474 if ((format_version
== 2) && (exec_result
== -EINVAL
)) {
2475 if (astate
->config
->action
== NULL
)
2476 printk(KERN_ERR
"%s: error: no action specified for "
2477 "Jam STAPL file.\nprogram terminated.\n",
2480 printk(KERN_ERR
"%s: error: action \"%s\""
2481 " is not supported "
2482 "for this Jam STAPL file.\n"
2483 "Program terminated.\n", __func__
,
2484 astate
->config
->action
);
2486 } else if (exec_result
)
2487 printk(KERN_ERR
"%s: error %d\n", __func__
, exec_result
);
2497 EXPORT_SYMBOL(altera_init
);