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 <asm/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 long_tmp
= stack
[stack_ptr
- 2];
535 stack
[stack_ptr
- 2] = stack
[stack_ptr
- 1];
536 stack
[stack_ptr
- 1] = long_tmp
;
540 if (altera_check_stack(stack_ptr
, 2, &status
)) {
542 stack
[stack_ptr
- 1] += stack
[stack_ptr
];
546 if (altera_check_stack(stack_ptr
, 2, &status
)) {
548 stack
[stack_ptr
- 1] -= stack
[stack_ptr
];
552 if (altera_check_stack(stack_ptr
, 2, &status
)) {
554 stack
[stack_ptr
- 1] *= stack
[stack_ptr
];
558 if (altera_check_stack(stack_ptr
, 2, &status
)) {
560 stack
[stack_ptr
- 1] /= stack
[stack_ptr
];
564 if (altera_check_stack(stack_ptr
, 2, &status
)) {
566 stack
[stack_ptr
- 1] %= stack
[stack_ptr
];
570 if (altera_check_stack(stack_ptr
, 2, &status
)) {
572 stack
[stack_ptr
- 1] <<= stack
[stack_ptr
];
576 if (altera_check_stack(stack_ptr
, 2, &status
)) {
578 stack
[stack_ptr
- 1] >>= stack
[stack_ptr
];
582 if (altera_check_stack(stack_ptr
, 1, &status
))
583 stack
[stack_ptr
- 1] ^= (-1L);
587 if (altera_check_stack(stack_ptr
, 2, &status
)) {
589 stack
[stack_ptr
- 1] &= stack
[stack_ptr
];
593 if (altera_check_stack(stack_ptr
, 2, &status
)) {
595 stack
[stack_ptr
- 1] |= stack
[stack_ptr
];
599 if (altera_check_stack(stack_ptr
, 2, &status
)) {
601 stack
[stack_ptr
- 1] ^= stack
[stack_ptr
];
605 if (!altera_check_stack(stack_ptr
, 1, &status
))
607 stack
[stack_ptr
- 1] = stack
[stack_ptr
- 1] ? 0L : 1L;
610 if (!altera_check_stack(stack_ptr
, 2, &status
))
613 stack
[stack_ptr
- 1] =
614 (stack
[stack_ptr
- 1] > stack
[stack_ptr
]) ?
619 if (!altera_check_stack(stack_ptr
, 2, &status
))
622 stack
[stack_ptr
- 1] =
623 (stack
[stack_ptr
- 1] < stack
[stack_ptr
]) ?
628 if ((version
> 0) && (stack_ptr
== 0)) {
630 * We completed one of the main procedures
632 * Find the next procedure
633 * to be executed and jump to it.
634 * If there are no more procedures, then EXIT.
636 i
= get_unaligned_be32(&p
[proc_table
+
637 (13 * current_proc
) + 4]);
639 ((proc_attributes
[i
] == 1) ||
640 ((proc_attributes
[i
] & 0xc0) == 0x40)))
641 i
= get_unaligned_be32(&p
[proc_table
+
645 /* no procedures to execute! */
647 *exit_code
= 0; /* success */
650 pc
= code_sect
+ get_unaligned_be32(
653 if ((pc
< code_sect
) ||
659 if (altera_check_stack(stack_ptr
, 1, &status
)) {
660 pc
= stack
[--stack_ptr
] + code_sect
;
661 if ((pc
<= code_sect
) ||
670 * Array short compare
671 * ...stack 0 is source 1 value
672 * ...stack 1 is source 2 value
673 * ...stack 2 is mask value
674 * ...stack 3 is count
676 if (altera_check_stack(stack_ptr
, 4, &status
)) {
677 s32 a
= stack
[--stack_ptr
];
678 s32 b
= stack
[--stack_ptr
];
679 long_tmp
= stack
[--stack_ptr
];
680 count
= stack
[stack_ptr
- 1];
682 if ((count
< 1) || (count
> 32))
685 long_tmp
&= ((-1L) >> (32 - count
));
687 stack
[stack_ptr
- 1] =
688 ((a
& long_tmp
) == (b
& long_tmp
))
696 * ...stack 0 is integer value
698 if (!altera_check_stack(stack_ptr
, 1, &status
))
700 sprintf(&msg_buff
[strlen(msg_buff
)],
701 "%ld", stack
[--stack_ptr
]);
706 printk(msg_buff
, "\n");
713 * ...stack 0 is scan data
714 * ...stack 1 is count
716 if (!altera_check_stack(stack_ptr
, 2, &status
))
718 long_tmp
= stack
[--stack_ptr
];
719 count
= stack
[--stack_ptr
];
720 put_unaligned_le32(long_tmp
, &charbuf
[0]);
721 status
= altera_drscan(astate
, count
, charbuf
, 0);
725 * DRSCAN short with capture
726 * ...stack 0 is scan data
727 * ...stack 1 is count
729 if (!altera_check_stack(stack_ptr
, 2, &status
))
731 long_tmp
= stack
[--stack_ptr
];
732 count
= stack
[stack_ptr
- 1];
733 put_unaligned_le32(long_tmp
, &charbuf
[0]);
734 status
= altera_swap_dr(astate
, count
, charbuf
,
736 stack
[stack_ptr
- 1] = get_unaligned_le32(&charbuf
[0]);
741 * ...stack 0 is scan data
742 * ...stack 1 is count
744 if (!altera_check_stack(stack_ptr
, 2, &status
))
746 long_tmp
= stack
[--stack_ptr
];
747 count
= stack
[--stack_ptr
];
748 put_unaligned_le32(long_tmp
, &charbuf
[0]);
749 status
= altera_irscan(astate
, count
, charbuf
, 0);
753 * IRSCAN short with capture
754 * ...stack 0 is scan data
755 * ...stack 1 is count
757 if (!altera_check_stack(stack_ptr
, 2, &status
))
759 long_tmp
= stack
[--stack_ptr
];
760 count
= stack
[stack_ptr
- 1];
761 put_unaligned_le32(long_tmp
, &charbuf
[0]);
762 status
= altera_swap_ir(astate
, count
, charbuf
,
764 stack
[stack_ptr
- 1] = get_unaligned_le32(&charbuf
[0]);
767 if (!altera_check_stack(stack_ptr
, 1, &status
))
769 count
= stack
[--stack_ptr
];
770 status
= altera_set_dr_pre(&astate
->js
, count
, 0, NULL
);
774 * DRPRE with literal data
775 * ...stack 0 is count
776 * ...stack 1 is literal data
778 if (!altera_check_stack(stack_ptr
, 2, &status
))
780 count
= stack
[--stack_ptr
];
781 long_tmp
= stack
[--stack_ptr
];
782 put_unaligned_le32(long_tmp
, &charbuf
[0]);
783 status
= altera_set_dr_pre(&astate
->js
, count
, 0,
789 * ...stack 0 is count
791 if (altera_check_stack(stack_ptr
, 1, &status
)) {
792 count
= stack
[--stack_ptr
];
793 status
= altera_set_dr_post(&astate
->js
, count
,
799 * DRPOST with literal data
800 * ...stack 0 is count
801 * ...stack 1 is literal data
803 if (!altera_check_stack(stack_ptr
, 2, &status
))
805 count
= stack
[--stack_ptr
];
806 long_tmp
= stack
[--stack_ptr
];
807 put_unaligned_le32(long_tmp
, &charbuf
[0]);
808 status
= altera_set_dr_post(&astate
->js
, count
, 0,
812 if (altera_check_stack(stack_ptr
, 1, &status
)) {
813 count
= stack
[--stack_ptr
];
814 status
= altera_set_ir_pre(&astate
->js
, count
,
820 * IRPRE with literal data
821 * ...stack 0 is count
822 * ...stack 1 is literal data
824 if (altera_check_stack(stack_ptr
, 2, &status
)) {
825 count
= stack
[--stack_ptr
];
826 long_tmp
= stack
[--stack_ptr
];
827 put_unaligned_le32(long_tmp
, &charbuf
[0]);
828 status
= altera_set_ir_pre(&astate
->js
, count
,
835 * ...stack 0 is count
837 if (altera_check_stack(stack_ptr
, 1, &status
)) {
838 count
= stack
[--stack_ptr
];
839 status
= altera_set_ir_post(&astate
->js
, count
,
845 * IRPOST with literal data
846 * ...stack 0 is count
847 * ...stack 1 is literal data
849 if (!altera_check_stack(stack_ptr
, 2, &status
))
851 count
= stack
[--stack_ptr
];
852 long_tmp
= stack
[--stack_ptr
];
853 put_unaligned_le32(long_tmp
, &charbuf
[0]);
854 status
= altera_set_ir_post(&astate
->js
, count
, 0,
858 if (altera_check_stack(stack_ptr
, 1, &status
)) {
860 count
= strlen(msg_buff
);
861 ch
= (char) stack
[--stack_ptr
];
862 if ((ch
< 1) || (ch
> 127)) {
864 * character code out of range
865 * instead of flagging an error,
866 * force the value to 127
870 msg_buff
[count
] = ch
;
871 msg_buff
[count
+ 1] = '\0';
875 if (altera_check_stack(stack_ptr
, 1, &status
))
876 *exit_code
= stack
[--stack_ptr
];
881 if (!altera_check_stack(stack_ptr
, 2, &status
))
884 stack
[stack_ptr
- 1] =
885 (stack
[stack_ptr
- 1] == stack
[stack_ptr
]) ?
889 if (altera_check_stack(stack_ptr
, 1, &status
))
894 if (!altera_check_stack(stack_ptr
, 1, &status
))
896 if (stack
[stack_ptr
- 1] < 0)
897 stack
[stack_ptr
- 1] = 0 - stack
[stack_ptr
- 1];
915 if (altera_check_stack(stack_ptr
, 2, &status
)) {
916 long_tmp
= stack
[stack_ptr
- 2];
917 stack
[stack_ptr
- 2] = stack
[stack_ptr
- 1];
918 stack
[stack_ptr
- 1] = long_tmp
;
923 if (altera_check_stack(stack_ptr
, index
, &status
)) {
924 long_tmp
= stack
[stack_ptr
- index
];
925 stack
[stack_ptr
- index
] = stack
[stack_ptr
- 1];
926 stack
[stack_ptr
- 1] = long_tmp
;
930 if (altera_check_stack(stack_ptr
, 2, &status
)) {
931 long_tmp
= stack
[stack_ptr
- 2];
932 stack
[stack_ptr
- 2] = stack
[stack_ptr
- 1];
933 stack
[stack_ptr
- 1] = long_tmp
;
938 if (altera_check_stack(stack_ptr
, index
, &status
)) {
939 long_tmp
= stack
[stack_ptr
- index
];
940 stack
[stack_ptr
- index
] = stack
[stack_ptr
- 1];
941 stack
[stack_ptr
- 1] = long_tmp
;
946 if (altera_check_stack(stack_ptr
, index
, &status
)) {
947 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
953 if (altera_check_stack(stack_ptr
, index
, &status
)) {
954 long_tmp
= stack
[stack_ptr
- index
];
955 stack
[stack_ptr
- index
] = stack
[stack_ptr
- 1];
956 stack
[stack_ptr
- 1] = long_tmp
;
960 if (altera_check_stack(stack_ptr
, 2, &status
)) {
961 long_tmp
= stack
[stack_ptr
- 2];
962 stack
[stack_ptr
- 2] = stack
[stack_ptr
- 1];
963 stack
[stack_ptr
- 1] = long_tmp
;
968 if (altera_check_stack(stack_ptr
, index
, &status
)) {
969 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
975 if (altera_check_stack(stack_ptr
, index
, &status
)) {
976 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
981 stack
[stack_ptr
++] = 0;
984 stack
[stack_ptr
++] = (s32
) args
[0];
987 stack
[stack_ptr
++] = vars
[args
[0]];
990 pc
= args
[0] + code_sect
;
991 if ((pc
< code_sect
) || (pc
>= debug_sect
))
995 stack
[stack_ptr
++] = pc
;
996 pc
= args
[0] + code_sect
;
997 if ((pc
< code_sect
) || (pc
>= debug_sect
))
1002 * Process FOR / NEXT loop
1003 * ...argument 0 is variable ID
1004 * ...stack 0 is step value
1005 * ...stack 1 is end value
1006 * ...stack 2 is top address
1008 if (altera_check_stack(stack_ptr
, 3, &status
)) {
1009 s32 step
= stack
[stack_ptr
- 1];
1010 s32 end
= stack
[stack_ptr
- 2];
1011 s32 top
= stack
[stack_ptr
- 3];
1012 s32 iterator
= vars
[args
[0]];
1016 if (iterator
<= end
)
1018 } else if (iterator
>= end
)
1024 vars
[args
[0]] = iterator
+ step
;
1025 pc
= top
+ code_sect
;
1026 if ((pc
< code_sect
) ||
1035 * ...argument 0 is string ID
1037 count
= strlen(msg_buff
);
1038 strlcpy(&msg_buff
[count
],
1039 &p
[str_table
+ args
[0]],
1040 ALTERA_MESSAGE_LENGTH
- count
);
1044 * STATE intermediate state
1045 * ...argument 0 is state code
1047 status
= altera_goto_jstate(astate
, args
[0]);
1052 * ...argument 0 is state code
1054 status
= altera_goto_jstate(astate
, args
[0]);
1059 * ...argument 0 is state code
1061 status
= altera_set_irstop(&astate
->js
, args
[0]);
1066 * ...argument 0 is state code
1068 status
= altera_set_drstop(&astate
->js
, args
[0]);
1073 * Exchange top with Nth stack value
1074 * ...argument 0 is 0-based stack entry
1075 * to swap with top element
1077 index
= (args
[0]) + 1;
1078 if (altera_check_stack(stack_ptr
, index
, &status
)) {
1079 long_tmp
= stack
[stack_ptr
- index
];
1080 stack
[stack_ptr
- index
] = stack
[stack_ptr
- 1];
1081 stack
[stack_ptr
- 1] = long_tmp
;
1086 * Duplicate Nth stack value
1087 * ...argument 0 is 0-based stack entry to duplicate
1089 index
= (args
[0]) + 1;
1090 if (altera_check_stack(stack_ptr
, index
, &status
)) {
1091 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
1097 * Pop stack into scalar variable
1098 * ...argument 0 is variable ID
1099 * ...stack 0 is value
1101 if (altera_check_stack(stack_ptr
, 1, &status
))
1102 vars
[args
[0]] = stack
[--stack_ptr
];
1107 * Pop stack into integer array element
1108 * ...argument 0 is variable ID
1109 * ...stack 0 is array index
1110 * ...stack 1 is value
1112 if (!altera_check_stack(stack_ptr
, 2, &status
))
1114 variable_id
= args
[0];
1117 * If variable is read-only,
1118 * convert to writable array
1120 if ((version
> 0) &&
1121 ((attrs
[variable_id
] & 0x9c) == 0x1c)) {
1122 /* Allocate a writable buffer for this array */
1123 count
= var_size
[variable_id
];
1124 long_tmp
= vars
[variable_id
];
1125 longptr_tmp
= kcalloc(count
, sizeof(long),
1127 vars
[variable_id
] = (long)longptr_tmp
;
1129 if (vars
[variable_id
] == 0) {
1134 /* copy previous contents into buffer */
1135 for (i
= 0; i
< count
; ++i
) {
1137 get_unaligned_be32(&p
[long_tmp
]);
1138 long_tmp
+= sizeof(long);
1142 * set bit 7 - buffer was
1143 * dynamically allocated
1145 attrs
[variable_id
] |= 0x80;
1147 /* clear bit 2 - variable is writable */
1148 attrs
[variable_id
] &= ~0x04;
1149 attrs
[variable_id
] |= 0x01;
1153 /* check that variable is a writable integer array */
1154 if ((attrs
[variable_id
] & 0x1c) != 0x18)
1157 longptr_tmp
= (long *)vars
[variable_id
];
1159 /* pop the array index */
1160 index
= stack
[--stack_ptr
];
1162 /* pop the value and store it into the array */
1163 longptr_tmp
[index
] = stack
[--stack_ptr
];
1169 * Pop stack into Boolean array
1170 * ...argument 0 is variable ID
1171 * ...stack 0 is count
1172 * ...stack 1 is array index
1173 * ...stack 2 is value
1175 if (!altera_check_stack(stack_ptr
, 3, &status
))
1177 variable_id
= args
[0];
1180 * If variable is read-only,
1181 * convert to writable array
1183 if ((version
> 0) &&
1184 ((attrs
[variable_id
] & 0x9c) == 0x0c)) {
1185 /* Allocate a writable buffer for this array */
1187 (var_size
[variable_id
] + 7L) >> 3L;
1188 charptr_tmp2
= (u8
*)vars
[variable_id
];
1190 kzalloc(long_tmp
, GFP_KERNEL
);
1191 vars
[variable_id
] = (long)charptr_tmp
;
1193 if (vars
[variable_id
] == 0) {
1198 /* zero the buffer */
1200 long_idx
< long_tmp
;
1202 charptr_tmp
[long_idx
] = 0;
1205 /* copy previous contents into buffer */
1207 long_idx
< var_size
[variable_id
];
1209 long_idx2
= long_idx
;
1211 if (charptr_tmp2
[long_idx2
>> 3] &
1212 (1 << (long_idx2
& 7))) {
1213 charptr_tmp
[long_idx
>> 3] |=
1214 (1 << (long_idx
& 7));
1219 * set bit 7 - buffer was
1220 * dynamically allocated
1222 attrs
[variable_id
] |= 0x80;
1224 /* clear bit 2 - variable is writable */
1225 attrs
[variable_id
] &= ~0x04;
1226 attrs
[variable_id
] |= 0x01;
1231 * check that variable is
1232 * a writable Boolean array
1234 if ((attrs
[variable_id
] & 0x1c) != 0x08) {
1239 charptr_tmp
= (u8
*)vars
[variable_id
];
1241 /* pop the count (number of bits to copy) */
1242 long_count
= stack
[--stack_ptr
];
1244 /* pop the array index */
1245 long_idx
= stack
[--stack_ptr
];
1251 * stack 0 = array right index
1252 * stack 1 = array left index
1255 if (long_idx
> long_count
) {
1257 long_tmp
= long_count
;
1258 long_count
= 1 + long_idx
-
1260 long_idx
= long_tmp
;
1262 /* reverse POPA is not supported */
1266 long_count
= 1 + long_count
-
1272 long_tmp
= stack
[--stack_ptr
];
1274 if (long_count
< 1) {
1279 for (i
= 0; i
< long_count
; ++i
) {
1280 if (long_tmp
& (1L << (s32
) i
))
1281 charptr_tmp
[long_idx
>> 3L] |=
1282 (1L << (long_idx
& 7L));
1284 charptr_tmp
[long_idx
>> 3L] &=
1285 ~(1L << (long_idx
& 7L));
1293 * Pop stack and branch if zero
1294 * ...argument 0 is address
1295 * ...stack 0 is condition value
1297 if (altera_check_stack(stack_ptr
, 1, &status
)) {
1298 if (stack
[--stack_ptr
] == 0) {
1299 pc
= args
[0] + code_sect
;
1300 if ((pc
< code_sect
) ||
1311 * ...argument 0 is scan data variable ID
1312 * ...stack 0 is array index
1313 * ...stack 1 is count
1315 if (!altera_check_stack(stack_ptr
, 2, &status
))
1317 long_idx
= stack
[--stack_ptr
];
1318 long_count
= stack
[--stack_ptr
];
1322 * stack 0 = array right index
1323 * stack 1 = array left index
1326 long_tmp
= long_count
;
1327 long_count
= stack
[--stack_ptr
];
1329 if (long_idx
> long_tmp
) {
1331 long_idx
= long_tmp
;
1335 charptr_tmp
= (u8
*)vars
[args
[0]];
1340 * and reverse the data order
1342 charptr_tmp2
= charptr_tmp
;
1343 charptr_tmp
= kzalloc((long_count
>> 3) + 1,
1345 if (charptr_tmp
== NULL
) {
1350 long_tmp
= long_idx
+ long_count
- 1;
1352 while (long_idx2
< long_count
) {
1353 if (charptr_tmp2
[long_tmp
>> 3] &
1354 (1 << (long_tmp
& 7)))
1355 charptr_tmp
[long_idx2
>> 3] |=
1356 (1 << (long_idx2
& 7));
1358 charptr_tmp
[long_idx2
>> 3] &=
1359 ~(1 << (long_idx2
& 7));
1366 if (opcode
== 0x51) /* DS */
1367 status
= altera_drscan(astate
, long_count
,
1368 charptr_tmp
, long_idx
);
1370 status
= altera_irscan(astate
, long_count
,
1371 charptr_tmp
, long_idx
);
1379 * DRPRE with array data
1380 * ...argument 0 is variable ID
1381 * ...stack 0 is array index
1382 * ...stack 1 is count
1384 if (!altera_check_stack(stack_ptr
, 2, &status
))
1386 index
= stack
[--stack_ptr
];
1387 count
= stack
[--stack_ptr
];
1391 * stack 0 = array right index
1392 * stack 1 = array left index
1394 count
= 1 + count
- index
;
1396 charptr_tmp
= (u8
*)vars
[args
[0]];
1397 status
= altera_set_dr_pre(&astate
->js
, count
, index
,
1402 * DRPOST with array data
1403 * ...argument 0 is variable ID
1404 * ...stack 0 is array index
1405 * ...stack 1 is count
1407 if (!altera_check_stack(stack_ptr
, 2, &status
))
1409 index
= stack
[--stack_ptr
];
1410 count
= stack
[--stack_ptr
];
1414 * stack 0 = array right index
1415 * stack 1 = array left index
1417 count
= 1 + count
- index
;
1419 charptr_tmp
= (u8
*)vars
[args
[0]];
1420 status
= altera_set_dr_post(&astate
->js
, count
, index
,
1425 * IRPRE 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_pre(&astate
->js
, count
, index
,
1449 * IRPOST with array data
1450 * ...argument 0 is variable ID
1451 * ...stack 0 is array index
1452 * ...stack 1 is count
1454 if (!altera_check_stack(stack_ptr
, 2, &status
))
1456 index
= stack
[--stack_ptr
];
1457 count
= stack
[--stack_ptr
];
1461 * stack 0 = array right index
1462 * stack 1 = array left index
1464 count
= 1 + count
- index
;
1466 charptr_tmp
= (u8
*)vars
[args
[0]];
1467 status
= altera_set_ir_post(&astate
->js
, count
, index
,
1474 * ...argument 0 is string ID
1475 * ...stack 0 is integer expression
1477 if (altera_check_stack(stack_ptr
, 1, &status
)) {
1478 name
= &p
[str_table
+ args
[0]];
1479 long_tmp
= stack
[--stack_ptr
];
1480 altera_export_int(name
, long_tmp
);
1485 * Push integer array element
1486 * ...argument 0 is variable ID
1487 * ...stack 0 is array index
1489 if (!altera_check_stack(stack_ptr
, 1, &status
))
1491 variable_id
= args
[0];
1492 index
= stack
[stack_ptr
- 1];
1494 /* check variable type */
1495 if ((attrs
[variable_id
] & 0x1f) == 0x19) {
1496 /* writable integer array */
1497 longptr_tmp
= (long *)vars
[variable_id
];
1498 stack
[stack_ptr
- 1] = longptr_tmp
[index
];
1499 } else if ((attrs
[variable_id
] & 0x1f) == 0x1c) {
1500 /* read-only integer array */
1501 long_tmp
= vars
[variable_id
] +
1502 (index
* sizeof(long));
1503 stack
[stack_ptr
- 1] =
1504 get_unaligned_be32(&p
[long_tmp
]);
1511 * Push Boolean array
1512 * ...argument 0 is variable ID
1513 * ...stack 0 is count
1514 * ...stack 1 is array index
1516 if (!altera_check_stack(stack_ptr
, 2, &status
))
1518 variable_id
= args
[0];
1520 /* check that variable is a Boolean array */
1521 if ((attrs
[variable_id
] & 0x18) != 0x08) {
1526 charptr_tmp
= (u8
*)vars
[variable_id
];
1528 /* pop the count (number of bits to copy) */
1529 count
= stack
[--stack_ptr
];
1531 /* pop the array index */
1532 index
= stack
[stack_ptr
- 1];
1536 * stack 0 = array right index
1537 * stack 1 = array left index
1539 count
= 1 + count
- index
;
1541 if ((count
< 1) || (count
> 32)) {
1548 for (i
= 0; i
< count
; ++i
)
1549 if (charptr_tmp
[(i
+ index
) >> 3] &
1550 (1 << ((i
+ index
) & 7)))
1551 long_tmp
|= (1L << i
);
1553 stack
[stack_ptr
- 1] = long_tmp
;
1558 * Dynamically change size of array
1559 * ...argument 0 is variable ID
1560 * ...stack 0 is new size
1562 if (!altera_check_stack(stack_ptr
, 1, &status
))
1564 variable_id
= args
[0];
1565 long_tmp
= stack
[--stack_ptr
];
1567 if (long_tmp
> var_size
[variable_id
]) {
1568 var_size
[variable_id
] = long_tmp
;
1570 if (attrs
[variable_id
] & 0x10)
1571 /* allocate integer array */
1572 long_tmp
*= sizeof(long);
1574 /* allocate Boolean array */
1575 long_tmp
= (long_tmp
+ 7) >> 3;
1578 * If the buffer was previously allocated,
1581 if (attrs
[variable_id
] & 0x80) {
1582 kfree((void *)vars
[variable_id
]);
1583 vars
[variable_id
] = 0;
1587 * Allocate a new buffer
1588 * of the requested size
1590 vars
[variable_id
] = (long)
1591 kzalloc(long_tmp
, GFP_KERNEL
);
1593 if (vars
[variable_id
] == 0) {
1599 * Set the attribute bit to indicate that
1600 * this buffer was dynamically allocated and
1601 * should be freed later
1603 attrs
[variable_id
] |= 0x80;
1605 /* zero out memory */
1606 count
= ((var_size
[variable_id
] + 7L) /
1608 charptr_tmp
= (u8
*)(vars
[variable_id
]);
1609 for (index
= 0; index
< count
; ++index
)
1610 charptr_tmp
[index
] = 0;
1617 * Export Boolean array
1618 * ...argument 0 is string ID
1619 * ...stack 0 is variable ID
1620 * ...stack 1 is array right index
1621 * ...stack 2 is array left index
1623 if (!altera_check_stack(stack_ptr
, 3, &status
))
1626 /* EXPV is not supported in JBC 1.0 */
1630 name
= &p
[str_table
+ args
[0]];
1631 variable_id
= stack
[--stack_ptr
];
1632 long_idx
= stack
[--stack_ptr
];/* right indx */
1633 long_idx2
= stack
[--stack_ptr
];/* left indx */
1635 if (long_idx
> long_idx2
) {
1636 /* reverse indices not supported */
1641 long_count
= 1 + long_idx2
- long_idx
;
1643 charptr_tmp
= (u8
*)vars
[variable_id
];
1644 charptr_tmp2
= NULL
;
1646 if ((long_idx
& 7L) != 0) {
1649 kzalloc(((long_count
+ 7L) / 8L),
1651 if (charptr_tmp2
== NULL
) {
1656 for (i
= 0; i
< long_count
; ++i
) {
1657 if (charptr_tmp
[k
>> 3] &
1659 charptr_tmp2
[i
>> 3] |=
1662 charptr_tmp2
[i
>> 3] &=
1667 charptr_tmp
= charptr_tmp2
;
1669 } else if (long_idx
!= 0)
1670 charptr_tmp
= &charptr_tmp
[long_idx
>> 3];
1672 altera_export_bool_array(name
, charptr_tmp
,
1675 /* free allocated buffer */
1676 if ((long_idx
& 7L) != 0)
1677 kfree(charptr_tmp2
);
1683 * ...argument 0 is dest ID
1684 * ...argument 1 is source ID
1685 * ...stack 0 is count
1686 * ...stack 1 is dest index
1687 * ...stack 2 is source index
1695 int src_reverse
= 0;
1696 int dest_reverse
= 0;
1698 if (!altera_check_stack(stack_ptr
, 3, &status
))
1701 copy_count
= stack
[--stack_ptr
];
1702 copy_index
= stack
[--stack_ptr
];
1703 copy_index2
= stack
[--stack_ptr
];
1708 * stack 0 = source right index
1709 * stack 1 = source left index
1710 * stack 2 = destination right index
1711 * stack 3 = destination left index
1713 destleft
= stack
[--stack_ptr
];
1715 if (copy_count
> copy_index
) {
1718 src_count
= 1 + copy_count
- copy_index
;
1719 /* copy_index = source start index */
1721 src_count
= 1 + copy_index
- copy_count
;
1722 /* source start index */
1723 copy_index
= copy_count
;
1726 if (copy_index2
> destleft
) {
1729 dest_count
= 1 + copy_index2
- destleft
;
1730 /* destination start index */
1731 copy_index2
= destleft
;
1733 dest_count
= 1 + destleft
- copy_index2
;
1735 copy_count
= (src_count
< dest_count
) ?
1736 src_count
: dest_count
;
1738 if ((src_reverse
|| dest_reverse
) &&
1739 (src_count
!= dest_count
))
1741 * If either the source or destination
1742 * is reversed, we can't tolerate
1743 * a length mismatch, because we
1744 * "left justify" arrays when copying.
1745 * This won't work correctly
1746 * with reversed arrays.
1754 index2
= copy_index2
;
1757 * If destination is a read-only array,
1758 * allocate a buffer and convert it to a writable array
1760 variable_id
= args
[1];
1761 if ((version
> 0) &&
1762 ((attrs
[variable_id
] & 0x9c) == 0x0c)) {
1763 /* Allocate a writable buffer for this array */
1765 (var_size
[variable_id
] + 7L) >> 3L;
1766 charptr_tmp2
= (u8
*)vars
[variable_id
];
1768 kzalloc(long_tmp
, GFP_KERNEL
);
1769 vars
[variable_id
] = (long)charptr_tmp
;
1771 if (vars
[variable_id
] == 0) {
1776 /* zero the buffer */
1777 for (long_idx
= 0L; long_idx
< long_tmp
;
1779 charptr_tmp
[long_idx
] = 0;
1781 /* copy previous contents into buffer */
1783 long_idx
< var_size
[variable_id
];
1785 long_idx2
= long_idx
;
1787 if (charptr_tmp2
[long_idx2
>> 3] &
1788 (1 << (long_idx2
& 7)))
1789 charptr_tmp
[long_idx
>> 3] |=
1790 (1 << (long_idx
& 7));
1795 set bit 7 - buffer was dynamically allocated */
1796 attrs
[variable_id
] |= 0x80;
1798 /* clear bit 2 - variable is writable */
1799 attrs
[variable_id
] &= ~0x04;
1800 attrs
[variable_id
] |= 0x01;
1803 charptr_tmp
= (u8
*)vars
[args
[1]];
1804 charptr_tmp2
= (u8
*)vars
[args
[0]];
1806 /* check if destination is a writable Boolean array */
1807 if ((attrs
[args
[1]] & 0x1c) != 0x08) {
1818 index2
+= (count
- 1);
1820 for (i
= 0; i
< count
; ++i
) {
1821 if (charptr_tmp2
[index
>> 3] &
1823 charptr_tmp
[index2
>> 3] |=
1824 (1 << (index2
& 7));
1826 charptr_tmp
[index2
>> 3] &=
1827 ~(1 << (index2
& 7));
1841 * DRSCAN with capture
1842 * IRSCAN with capture
1843 * ...argument 0 is scan data variable ID
1844 * ...argument 1 is capture variable ID
1845 * ...stack 0 is capture index
1846 * ...stack 1 is scan data index
1847 * ...stack 2 is count
1849 s32 scan_right
, scan_left
;
1850 s32 capture_count
= 0;
1855 if (!altera_check_stack(stack_ptr
, 3, &status
))
1858 capture_index
= stack
[--stack_ptr
];
1859 scan_index
= stack
[--stack_ptr
];
1863 * stack 0 = capture right index
1864 * stack 1 = capture left index
1865 * stack 2 = scan right index
1866 * stack 3 = scan left index
1869 scan_right
= stack
[--stack_ptr
];
1870 scan_left
= stack
[--stack_ptr
];
1871 capture_count
= 1 + scan_index
- capture_index
;
1872 scan_count
= 1 + scan_left
- scan_right
;
1873 scan_index
= scan_right
;
1876 long_count
= stack
[--stack_ptr
];
1878 * If capture array is read-only, allocate a buffer
1879 * and convert it to a writable array
1881 variable_id
= args
[1];
1882 if ((version
> 0) &&
1883 ((attrs
[variable_id
] & 0x9c) == 0x0c)) {
1884 /* Allocate a writable buffer for this array */
1886 (var_size
[variable_id
] + 7L) >> 3L;
1887 charptr_tmp2
= (u8
*)vars
[variable_id
];
1889 kzalloc(long_tmp
, GFP_KERNEL
);
1890 vars
[variable_id
] = (long)charptr_tmp
;
1892 if (vars
[variable_id
] == 0) {
1897 /* zero the buffer */
1898 for (long_idx
= 0L; long_idx
< long_tmp
;
1900 charptr_tmp
[long_idx
] = 0;
1902 /* copy previous contents into buffer */
1904 long_idx
< var_size
[variable_id
];
1906 long_idx2
= long_idx
;
1908 if (charptr_tmp2
[long_idx2
>> 3] &
1909 (1 << (long_idx2
& 7)))
1910 charptr_tmp
[long_idx
>> 3] |=
1911 (1 << (long_idx
& 7));
1916 * set bit 7 - buffer was
1917 * dynamically allocated
1919 attrs
[variable_id
] |= 0x80;
1921 /* clear bit 2 - variable is writable */
1922 attrs
[variable_id
] &= ~0x04;
1923 attrs
[variable_id
] |= 0x01;
1927 charptr_tmp
= (u8
*)vars
[args
[0]];
1928 charptr_tmp2
= (u8
*)vars
[args
[1]];
1930 if ((version
> 0) &&
1931 ((long_count
> capture_count
) ||
1932 (long_count
> scan_count
))) {
1938 * check that capture array
1939 * is a writable Boolean array
1941 if ((attrs
[args
[1]] & 0x1c) != 0x08) {
1947 if (opcode
== 0x82) /* DSC */
1948 status
= altera_swap_dr(astate
,
1955 status
= altera_swap_ir(astate
,
1969 * ...argument 0 is wait state
1970 * ...argument 1 is end state
1971 * ...stack 0 is cycles
1972 * ...stack 1 is microseconds
1974 if (!altera_check_stack(stack_ptr
, 2, &status
))
1976 long_tmp
= stack
[--stack_ptr
];
1979 status
= altera_wait_cycles(astate
, long_tmp
,
1982 long_tmp
= stack
[--stack_ptr
];
1984 if ((status
== 0) && (long_tmp
!= 0L))
1985 status
= altera_wait_msecs(astate
,
1989 if ((status
== 0) && (args
[1] != args
[0]))
1990 status
= altera_goto_jstate(astate
,
1994 --stack_ptr
; /* throw away MAX cycles */
1995 --stack_ptr
; /* throw away MAX microseconds */
2001 * ...argument 0 is source 1 ID
2002 * ...argument 1 is source 2 ID
2003 * ...argument 2 is mask ID
2004 * ...stack 0 is source 1 index
2005 * ...stack 1 is source 2 index
2006 * ...stack 2 is mask index
2007 * ...stack 3 is count
2010 u8
*source1
= (u8
*)vars
[args
[0]];
2011 u8
*source2
= (u8
*)vars
[args
[1]];
2012 u8
*mask
= (u8
*)vars
[args
[2]];
2017 if (!altera_check_stack(stack_ptr
, 4, &status
))
2020 index1
= stack
[--stack_ptr
];
2021 index2
= stack
[--stack_ptr
];
2022 mask_index
= stack
[--stack_ptr
];
2023 long_count
= stack
[--stack_ptr
];
2027 * stack 0 = source 1 right index
2028 * stack 1 = source 1 left index
2029 * stack 2 = source 2 right index
2030 * stack 3 = source 2 left index
2031 * stack 4 = mask right index
2032 * stack 5 = mask left index
2034 s32 mask_right
= stack
[--stack_ptr
];
2035 s32 mask_left
= stack
[--stack_ptr
];
2036 /* source 1 count */
2037 a
= 1 + index2
- index1
;
2038 /* source 2 count */
2039 b
= 1 + long_count
- mask_index
;
2040 a
= (a
< b
) ? a
: b
;
2042 b
= 1 + mask_left
- mask_right
;
2043 a
= (a
< b
) ? a
: b
;
2044 /* source 2 start index */
2045 index2
= mask_index
;
2046 /* mask start index */
2047 mask_index
= mask_right
;
2058 for (i
= 0; i
< count
; ++i
) {
2059 if (mask
[mask_index
>> 3] &
2060 (1 << (mask_index
& 7))) {
2061 a
= source1
[index1
>> 3] &
2064 b
= source2
[index2
>> 3] &
2068 if (a
!= b
) /* failure */
2077 stack
[stack_ptr
++] = long_tmp
;
2082 /* Unrecognized opcode -- ERROR! */
2090 if ((stack_ptr
< 0) || (stack_ptr
>= ALTERA_STACK_SIZE
))
2091 status
= -EOVERFLOW
;
2095 *error_address
= (s32
)(opcode_address
- code_sect
);
2099 altera_free_buffers(astate
);
2101 /* Free all dynamically allocated arrays */
2102 if ((attrs
!= NULL
) && (vars
!= NULL
))
2103 for (i
= 0; i
< sym_count
; ++i
)
2104 if (attrs
[i
] & 0x80)
2105 kfree((void *)vars
[i
]);
2110 kfree(proc_attributes
);
2115 static int altera_get_note(u8
*p
, s32 program_size
, s32
*offset
,
2116 char *key
, char *value
, int keylen
, int vallen
)
2118 * Gets key and value of NOTE fields in the JBC file.
2119 * Can be called in two modes: if offset pointer is NULL,
2120 * then the function searches for note fields which match
2121 * the key string provided. If offset is not NULL, then
2122 * the function finds the next note field of any key,
2123 * starting at the offset specified by the offset pointer.
2124 * Returns 0 for success, else appropriate error code
2127 int status
= -ENODATA
;
2128 u32 note_strings
= 0L;
2129 u32 note_table
= 0L;
2130 u32 note_count
= 0L;
2131 u32 first_word
= 0L;
2138 /* Read header information */
2139 if (program_size
> 52L) {
2140 first_word
= get_unaligned_be32(&p
[0]);
2141 version
= (first_word
& 1L);
2142 delta
= version
* 8;
2144 note_strings
= get_unaligned_be32(&p
[8 + delta
]);
2145 note_table
= get_unaligned_be32(&p
[12 + delta
]);
2146 note_count
= get_unaligned_be32(&p
[44 + (2 * delta
)]);
2149 if ((first_word
!= 0x4A414D00L
) && (first_word
!= 0x4A414D01L
))
2152 if (note_count
<= 0L)
2155 if (offset
== NULL
) {
2157 * We will search for the first note with a specific key,
2158 * and return only the value
2160 for (i
= 0; (i
< note_count
) &&
2161 (status
!= 0); ++i
) {
2162 key_ptr
= &p
[note_strings
+
2164 &p
[note_table
+ (8 * i
)])];
2165 if (key
&& !strncasecmp(key
, key_ptr
, strlen(key_ptr
))) {
2168 value_ptr
= &p
[note_strings
+
2170 &p
[note_table
+ (8 * i
) + 4])];
2173 strlcpy(value
, value_ptr
, vallen
);
2179 * We will search for the next note, regardless of the key,
2180 * and return both the value and the key
2185 if ((i
>= 0) && (i
< note_count
)) {
2189 strlcpy(key
, &p
[note_strings
+
2191 &p
[note_table
+ (8 * i
)])],
2195 strlcpy(value
, &p
[note_strings
+
2197 &p
[note_table
+ (8 * i
) + 4])],
2207 static int altera_check_crc(u8
*p
, s32 program_size
)
2210 u16 local_expected
= 0,
2216 u32 crc_section
= 0L;
2217 u32 first_word
= 0L;
2221 if (program_size
> 52L) {
2222 first_word
= get_unaligned_be32(&p
[0]);
2223 version
= (first_word
& 1L);
2224 delta
= version
* 8;
2226 crc_section
= get_unaligned_be32(&p
[32 + delta
]);
2229 if ((first_word
!= 0x4A414D00L
) && (first_word
!= 0x4A414D01L
))
2232 if (crc_section
>= program_size
)
2236 local_expected
= (u16
)get_unaligned_be16(&p
[crc_section
]);
2238 for (i
= 0; i
< crc_section
; ++i
) {
2240 for (bit
= 0; bit
< 8; bit
++) {
2241 feedback
= (databyte
^ shift_reg
) & 0x01;
2244 shift_reg
^= 0x8408;
2250 local_actual
= (u16
)~shift_reg
;
2252 if (local_expected
!= local_actual
)
2257 if (debug
|| status
) {
2260 printk(KERN_INFO
"%s: CRC matched: %04x\n", __func__
,
2264 printk(KERN_ERR
"%s: CRC mismatch: expected %04x, "
2265 "actual %04x\n", __func__
, local_expected
,
2269 printk(KERN_ERR
"%s: error: format isn't "
2270 "recognized.\n", __func__
);
2273 printk(KERN_ERR
"%s: CRC function returned error "
2274 "code %d\n", __func__
, status
);
2282 static int altera_get_file_info(u8
*p
,
2284 int *format_version
,
2286 int *procedure_count
)
2292 if (program_size
<= 52L)
2295 first_word
= get_unaligned_be32(&p
[0]);
2297 if ((first_word
== 0x4A414D00L
) || (first_word
== 0x4A414D01L
)) {
2300 version
= (first_word
& 1L);
2301 *format_version
= version
+ 1;
2304 *action_count
= get_unaligned_be32(&p
[48]);
2305 *procedure_count
= get_unaligned_be32(&p
[52]);
2312 static int altera_get_act_info(u8
*p
,
2317 struct altera_procinfo
**proc_list
)
2320 struct altera_procinfo
*procptr
= NULL
;
2321 struct altera_procinfo
*tmpptr
= NULL
;
2322 u32 first_word
= 0L;
2323 u32 action_table
= 0L;
2324 u32 proc_table
= 0L;
2326 u32 note_strings
= 0L;
2327 u32 action_count
= 0L;
2328 u32 proc_count
= 0L;
2329 u32 act_name_id
= 0L;
2330 u32 act_desc_id
= 0L;
2331 u32 act_proc_id
= 0L;
2332 u32 act_proc_name
= 0L;
2333 u8 act_proc_attribute
= 0;
2335 if (program_size
<= 52L)
2337 /* Read header information */
2338 first_word
= get_unaligned_be32(&p
[0]);
2340 if (first_word
!= 0x4A414D01L
)
2343 action_table
= get_unaligned_be32(&p
[4]);
2344 proc_table
= get_unaligned_be32(&p
[8]);
2345 str_table
= get_unaligned_be32(&p
[12]);
2346 note_strings
= get_unaligned_be32(&p
[16]);
2347 action_count
= get_unaligned_be32(&p
[48]);
2348 proc_count
= get_unaligned_be32(&p
[52]);
2350 if (index
>= action_count
)
2353 act_name_id
= get_unaligned_be32(&p
[action_table
+ (12 * index
)]);
2354 act_desc_id
= get_unaligned_be32(&p
[action_table
+ (12 * index
) + 4]);
2355 act_proc_id
= get_unaligned_be32(&p
[action_table
+ (12 * index
) + 8]);
2357 *name
= &p
[str_table
+ act_name_id
];
2359 if (act_desc_id
< (note_strings
- str_table
))
2360 *description
= &p
[str_table
+ act_desc_id
];
2363 act_proc_name
= get_unaligned_be32(
2364 &p
[proc_table
+ (13 * act_proc_id
)]);
2365 act_proc_attribute
=
2366 (p
[proc_table
+ (13 * act_proc_id
) + 8] & 0x03);
2369 kzalloc(sizeof(struct altera_procinfo
),
2372 if (procptr
== NULL
)
2375 procptr
->name
= &p
[str_table
+ act_proc_name
];
2376 procptr
->attrs
= act_proc_attribute
;
2377 procptr
->next
= NULL
;
2379 /* add record to end of linked list */
2380 if (*proc_list
== NULL
)
2381 *proc_list
= procptr
;
2383 tmpptr
= *proc_list
;
2384 while (tmpptr
->next
!= NULL
)
2385 tmpptr
= tmpptr
->next
;
2386 tmpptr
->next
= procptr
;
2390 act_proc_id
= get_unaligned_be32(
2391 &p
[proc_table
+ (13 * act_proc_id
) + 4]);
2392 } while ((act_proc_id
!= 0) && (act_proc_id
< proc_count
));
2397 int altera_init(struct altera_config
*config
, const struct firmware
*fw
)
2399 struct altera_state
*astate
= NULL
;
2400 struct altera_procinfo
*proc_list
= NULL
;
2401 struct altera_procinfo
*procptr
= NULL
;
2404 char *action_name
= NULL
;
2405 char *description
= NULL
;
2406 int exec_result
= 0;
2408 int format_version
= 0;
2409 int action_count
= 0;
2410 int procedure_count
= 0;
2413 s32 error_address
= 0L;
2416 key
= kzalloc(33, GFP_KERNEL
);
2421 value
= kzalloc(257, GFP_KERNEL
);
2426 astate
= kzalloc(sizeof(struct altera_state
), GFP_KERNEL
);
2432 astate
->config
= config
;
2433 if (!astate
->config
->jtag_io
) {
2434 dprintk("%s: using byteblaster!\n", __func__
);
2435 astate
->config
->jtag_io
= netup_jtag_io_lpt
;
2438 altera_check_crc((u8
*)fw
->data
, fw
->size
);
2441 altera_get_file_info((u8
*)fw
->data
, fw
->size
, &format_version
,
2442 &action_count
, &procedure_count
);
2443 printk(KERN_INFO
"%s: File format is %s ByteCode format\n",
2444 __func__
, (format_version
== 2) ? "Jam STAPL" :
2445 "pre-standardized Jam 1.1");
2446 while (altera_get_note((u8
*)fw
->data
, fw
->size
,
2447 &offset
, key
, value
, 32, 256) == 0)
2448 printk(KERN_INFO
"%s: NOTE \"%s\" = \"%s\"\n",
2449 __func__
, key
, value
);
2452 if (debug
&& (format_version
== 2) && (action_count
> 0)) {
2453 printk(KERN_INFO
"%s: Actions available:\n", __func__
);
2454 for (index
= 0; index
< action_count
; ++index
) {
2455 altera_get_act_info((u8
*)fw
->data
, fw
->size
,
2456 index
, &action_name
,
2460 if (description
== NULL
)
2461 printk(KERN_INFO
"%s: %s\n",
2465 printk(KERN_INFO
"%s: %s \"%s\"\n",
2470 procptr
= proc_list
;
2471 while (procptr
!= NULL
) {
2472 if (procptr
->attrs
!= 0)
2473 printk(KERN_INFO
"%s: %s (%s)\n",
2476 (procptr
->attrs
== 1) ?
2477 "optional" : "recommended");
2479 proc_list
= procptr
->next
;
2481 procptr
= proc_list
;
2485 printk(KERN_INFO
"\n");
2488 exec_result
= altera_execute(astate
, (u8
*)fw
->data
, fw
->size
,
2489 &error_address
, &exit_code
, &format_version
);
2492 exec_result
= -EREMOTEIO
;
2494 if ((format_version
== 2) && (exec_result
== -EINVAL
)) {
2495 if (astate
->config
->action
== NULL
)
2496 printk(KERN_ERR
"%s: error: no action specified for "
2497 "Jam STAPL file.\nprogram terminated.\n",
2500 printk(KERN_ERR
"%s: error: action \"%s\""
2501 " is not supported "
2502 "for this Jam STAPL file.\n"
2503 "Program terminated.\n", __func__
,
2504 astate
->config
->action
);
2506 } else if (exec_result
)
2507 printk(KERN_ERR
"%s: error %d\n", __func__
, exec_result
);
2517 EXPORT_SYMBOL(altera_init
);