6 * Copyright (C) Altera Corporation 1998-2001
7 * Copyright (C) 2010,2011 NetUP Inc.
8 * Copyright (C) 2010,2011 Igor M. Liplianin <liplianin@netup.ru>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <asm/unaligned.h>
27 #include <linux/ctype.h>
28 #include <linux/string.h>
29 #include <linux/firmware.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <misc/altera.h>
33 #include "altera-exprt.h"
34 #include "altera-jtag.h"
37 module_param(debug
, int, 0644);
38 MODULE_PARM_DESC(debug
, "enable debugging information");
40 MODULE_DESCRIPTION("altera FPGA kernel module");
41 MODULE_AUTHOR("Igor M. Liplianin <liplianin@netup.ru>");
42 MODULE_LICENSE("GPL");
44 #define dprintk(args...) \
46 printk(KERN_DEBUG args); \
49 enum altera_fpga_opcode
{
127 struct altera_procinfo
{
130 struct altera_procinfo
*next
;
133 /* This function checks if enough parameters are available on the stack. */
134 static int altera_check_stack(int stack_ptr
, int count
, int *status
)
136 if (stack_ptr
< count
) {
137 *status
= -EOVERFLOW
;
144 static void altera_export_int(char *key
, s32 value
)
146 dprintk("Export: key = \"%s\", value = %d\n", key
, value
);
149 #define HEX_LINE_CHARS 72
150 #define HEX_LINE_BITS (HEX_LINE_CHARS * 4)
152 static void altera_export_bool_array(char *key
, u8
*data
, s32 count
)
154 char string
[HEX_LINE_CHARS
+ 1];
156 u32 size
, line
, lines
, linebits
, value
, j
, k
;
158 if (count
> HEX_LINE_BITS
) {
159 dprintk("Export: key = \"%s\", %d bits, value = HEX\n",
161 lines
= (count
+ (HEX_LINE_BITS
- 1)) / HEX_LINE_BITS
;
163 for (line
= 0; line
< lines
; ++line
) {
164 if (line
< (lines
- 1)) {
165 linebits
= HEX_LINE_BITS
;
166 size
= HEX_LINE_CHARS
;
167 offset
= count
- ((line
+ 1) * HEX_LINE_BITS
);
170 count
- ((lines
- 1) * HEX_LINE_BITS
);
171 size
= (linebits
+ 3) / 4;
179 for (k
= 0; k
< linebits
; ++k
) {
181 if (data
[i
>> 3] & (1 << (i
& 7)))
182 value
|= (1 << (i
& 3));
184 sprintf(&string
[j
], "%1x", value
);
190 sprintf(&string
[j
], "%1x", value
);
192 dprintk("%s\n", string
);
196 size
= (count
+ 3) / 4;
201 for (i
= 0; i
< count
; ++i
) {
202 if (data
[i
>> 3] & (1 << (i
& 7)))
203 value
|= (1 << (i
& 3));
205 sprintf(&string
[j
], "%1x", value
);
211 sprintf(&string
[j
], "%1x", value
);
213 dprintk("Export: key = \"%s\", %d bits, value = HEX %s\n",
218 static int altera_execute(struct altera_state
*astate
,
225 struct altera_config
*aconf
= astate
->config
;
226 char *msg_buff
= astate
->msg_buff
;
227 long *stack
= astate
->stack
;
230 u32 action_table
= 0L;
237 u32 action_count
= 0L;
241 s32
*var_size
= NULL
;
243 u8
*proc_attributes
= NULL
;
272 int current_proc
= 0;
277 dprintk("%s\n", __func__
);
279 /* Read header information */
280 if (program_size
> 52L) {
281 first_word
= get_unaligned_be32(&p
[0]);
282 version
= (first_word
& 1L);
283 *format_version
= version
+ 1;
286 action_table
= get_unaligned_be32(&p
[4]);
287 proc_table
= get_unaligned_be32(&p
[8]);
288 str_table
= get_unaligned_be32(&p
[4 + delta
]);
289 sym_table
= get_unaligned_be32(&p
[16 + delta
]);
290 data_sect
= get_unaligned_be32(&p
[20 + delta
]);
291 code_sect
= get_unaligned_be32(&p
[24 + delta
]);
292 debug_sect
= get_unaligned_be32(&p
[28 + delta
]);
293 action_count
= get_unaligned_be32(&p
[40 + delta
]);
294 proc_count
= get_unaligned_be32(&p
[44 + delta
]);
295 sym_count
= get_unaligned_be32(&p
[48 + (2 * delta
)]);
298 if ((first_word
!= 0x4A414D00L
) && (first_word
!= 0x4A414D01L
)) {
307 vars
= kzalloc(sym_count
* sizeof(long), GFP_KERNEL
);
313 var_size
= kzalloc(sym_count
* sizeof(s32
), GFP_KERNEL
);
315 if (var_size
== NULL
)
320 attrs
= kzalloc(sym_count
, GFP_KERNEL
);
326 if ((status
== 0) && (version
> 0)) {
327 proc_attributes
= kzalloc(proc_count
, GFP_KERNEL
);
329 if (proc_attributes
== NULL
)
338 for (i
= 0; i
< sym_count
; ++i
) {
339 offset
= (sym_table
+ ((11 + delta
) * i
));
341 value
= get_unaligned_be32(&p
[offset
+ 3 + delta
]);
343 attrs
[i
] = p
[offset
];
346 * use bit 7 of attribute byte to indicate that
347 * this buffer was dynamically allocated
348 * and should be freed later
352 var_size
[i
] = get_unaligned_be32(&p
[offset
+ 7 + delta
]);
356 * bit 0: 0 = read-only, 1 = read-write
357 * bit 1: 0 = not compressed, 1 = compressed
358 * bit 2: 0 = not initialized, 1 = initialized
359 * bit 3: 0 = scalar, 1 = array
360 * bit 4: 0 = Boolean, 1 = integer
361 * bit 5: 0 = declared variable,
362 * 1 = compiler created temporary variable
365 if ((attrs
[i
] & 0x0c) == 0x04)
366 /* initialized scalar variable */
368 else if ((attrs
[i
] & 0x1e) == 0x0e) {
369 /* initialized compressed Boolean array */
370 uncomp_size
= get_unaligned_le32(&p
[data_sect
+ value
]);
372 /* allocate a buffer for the uncompressed data */
373 vars
[i
] = (long)kzalloc(uncomp_size
, GFP_KERNEL
);
377 /* set flag so buffer will be freed later */
380 /* uncompress the data */
381 if (altera_shrink(&p
[data_sect
+ value
],
385 version
) != uncomp_size
)
386 /* decompression failed */
389 var_size
[i
] = uncomp_size
* 8L;
392 } else if ((attrs
[i
] & 0x1e) == 0x0c) {
393 /* initialized Boolean array */
394 vars
[i
] = value
+ data_sect
+ (long)p
;
395 } else if ((attrs
[i
] & 0x1c) == 0x1c) {
396 /* initialized integer array */
397 vars
[i
] = value
+ data_sect
;
398 } else if ((attrs
[i
] & 0x0c) == 0x08) {
399 /* uninitialized array */
401 /* flag attrs so that memory is freed */
404 if (var_size
[i
] > 0) {
409 size
= (var_size
[i
] * sizeof(s32
));
412 size
= ((var_size
[i
] + 7L) / 8L);
414 vars
[i
] = (long)kzalloc(size
, GFP_KERNEL
);
419 /* zero out memory */
420 for (j
= 0; j
< size
; ++j
)
421 ((u8
*)(vars
[i
]))[j
] = 0;
436 altera_jinit(astate
);
442 * For JBC version 2, we will execute the procedures corresponding to
443 * the selected ACTION
446 if (aconf
->action
== NULL
) {
450 int action_found
= 0;
451 for (i
= 0; (i
< action_count
) && !action_found
; ++i
) {
452 name_id
= get_unaligned_be32(&p
[action_table
+
455 name
= &p
[str_table
+ name_id
];
457 if (strnicmp(aconf
->action
, name
, strlen(name
)) == 0) {
460 get_unaligned_be32(&p
[action_table
+
474 while ((i
!= 0) || first_time
) {
476 /* check procedure attribute byte */
489 i
= get_unaligned_be32(&p
[proc_table
+
494 * Set current_proc to the first procedure
499 ((proc_attributes
[i
] == 1) ||
500 ((proc_attributes
[i
] & 0xc0) == 0x40))) {
501 i
= get_unaligned_be32(&p
[proc_table
+
505 if ((i
!= 0) || ((i
== 0) && (current_proc
== 0) &&
506 ((proc_attributes
[0] != 1) &&
507 ((proc_attributes
[0] & 0xc0) != 0x40)))) {
510 get_unaligned_be32(&p
[proc_table
+
512 if ((pc
< code_sect
) || (pc
>= debug_sect
))
515 /* there are no procedures to execute! */
524 opcode
= (p
[pc
] & 0xff);
529 printk("opcode: %02x\n", opcode
);
531 arg_count
= (opcode
>> 6) & 3;
532 for (i
= 0; i
< arg_count
; ++i
) {
533 args
[i
] = get_unaligned_be32(&p
[pc
]);
541 if (altera_check_stack(stack_ptr
, 1, &status
)) {
542 stack
[stack_ptr
] = stack
[stack_ptr
- 1];
547 if (altera_check_stack(stack_ptr
, 2, &status
)) {
548 long_tmp
= stack
[stack_ptr
- 2];
549 stack
[stack_ptr
- 2] = stack
[stack_ptr
- 1];
550 stack
[stack_ptr
- 1] = long_tmp
;
554 if (altera_check_stack(stack_ptr
, 2, &status
)) {
556 stack
[stack_ptr
- 1] += stack
[stack_ptr
];
560 if (altera_check_stack(stack_ptr
, 2, &status
)) {
562 stack
[stack_ptr
- 1] -= stack
[stack_ptr
];
566 if (altera_check_stack(stack_ptr
, 2, &status
)) {
568 stack
[stack_ptr
- 1] *= stack
[stack_ptr
];
572 if (altera_check_stack(stack_ptr
, 2, &status
)) {
574 stack
[stack_ptr
- 1] /= stack
[stack_ptr
];
578 if (altera_check_stack(stack_ptr
, 2, &status
)) {
580 stack
[stack_ptr
- 1] %= stack
[stack_ptr
];
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
, 1, &status
))
597 stack
[stack_ptr
- 1] ^= (-1L);
601 if (altera_check_stack(stack_ptr
, 2, &status
)) {
603 stack
[stack_ptr
- 1] &= stack
[stack_ptr
];
607 if (altera_check_stack(stack_ptr
, 2, &status
)) {
609 stack
[stack_ptr
- 1] |= stack
[stack_ptr
];
613 if (altera_check_stack(stack_ptr
, 2, &status
)) {
615 stack
[stack_ptr
- 1] ^= stack
[stack_ptr
];
619 if (!altera_check_stack(stack_ptr
, 1, &status
))
621 stack
[stack_ptr
- 1] = stack
[stack_ptr
- 1] ? 0L : 1L;
624 if (!altera_check_stack(stack_ptr
, 2, &status
))
627 stack
[stack_ptr
- 1] =
628 (stack
[stack_ptr
- 1] > stack
[stack_ptr
]) ?
633 if (!altera_check_stack(stack_ptr
, 2, &status
))
636 stack
[stack_ptr
- 1] =
637 (stack
[stack_ptr
- 1] < stack
[stack_ptr
]) ?
642 if ((version
> 0) && (stack_ptr
== 0)) {
644 * We completed one of the main procedures
646 * Find the next procedure
647 * to be executed and jump to it.
648 * If there are no more procedures, then EXIT.
650 i
= get_unaligned_be32(&p
[proc_table
+
651 (13 * current_proc
) + 4]);
653 ((proc_attributes
[i
] == 1) ||
654 ((proc_attributes
[i
] & 0xc0) == 0x40)))
655 i
= get_unaligned_be32(&p
[proc_table
+
659 /* no procedures to execute! */
661 *exit_code
= 0; /* success */
664 pc
= code_sect
+ get_unaligned_be32(
667 if ((pc
< code_sect
) ||
673 if (altera_check_stack(stack_ptr
, 1, &status
)) {
674 pc
= stack
[--stack_ptr
] + code_sect
;
675 if ((pc
<= code_sect
) ||
684 * Array short compare
685 * ...stack 0 is source 1 value
686 * ...stack 1 is source 2 value
687 * ...stack 2 is mask value
688 * ...stack 3 is count
690 if (altera_check_stack(stack_ptr
, 4, &status
)) {
691 s32 a
= stack
[--stack_ptr
];
692 s32 b
= stack
[--stack_ptr
];
693 long_tmp
= stack
[--stack_ptr
];
694 count
= stack
[stack_ptr
- 1];
696 if ((count
< 1) || (count
> 32))
699 long_tmp
&= ((-1L) >> (32 - count
));
701 stack
[stack_ptr
- 1] =
702 ((a
& long_tmp
) == (b
& long_tmp
))
710 * ...stack 0 is integer value
712 if (!altera_check_stack(stack_ptr
, 1, &status
))
714 sprintf(&msg_buff
[strlen(msg_buff
)],
715 "%ld", stack
[--stack_ptr
]);
720 printk(msg_buff
, "\n");
727 * ...stack 0 is scan data
728 * ...stack 1 is count
730 if (!altera_check_stack(stack_ptr
, 2, &status
))
732 long_tmp
= stack
[--stack_ptr
];
733 count
= stack
[--stack_ptr
];
734 put_unaligned_le32(long_tmp
, &charbuf
[0]);
735 status
= altera_drscan(astate
, count
, charbuf
, 0);
739 * DRSCAN short with capture
740 * ...stack 0 is scan data
741 * ...stack 1 is count
743 if (!altera_check_stack(stack_ptr
, 2, &status
))
745 long_tmp
= stack
[--stack_ptr
];
746 count
= stack
[stack_ptr
- 1];
747 put_unaligned_le32(long_tmp
, &charbuf
[0]);
748 status
= altera_swap_dr(astate
, count
, charbuf
,
750 stack
[stack_ptr
- 1] = get_unaligned_le32(&charbuf
[0]);
755 * ...stack 0 is scan data
756 * ...stack 1 is count
758 if (!altera_check_stack(stack_ptr
, 2, &status
))
760 long_tmp
= stack
[--stack_ptr
];
761 count
= stack
[--stack_ptr
];
762 put_unaligned_le32(long_tmp
, &charbuf
[0]);
763 status
= altera_irscan(astate
, count
, charbuf
, 0);
767 * IRSCAN short with capture
768 * ...stack 0 is scan data
769 * ...stack 1 is count
771 if (!altera_check_stack(stack_ptr
, 2, &status
))
773 long_tmp
= stack
[--stack_ptr
];
774 count
= stack
[stack_ptr
- 1];
775 put_unaligned_le32(long_tmp
, &charbuf
[0]);
776 status
= altera_swap_ir(astate
, count
, charbuf
,
778 stack
[stack_ptr
- 1] = get_unaligned_le32(&charbuf
[0]);
781 if (!altera_check_stack(stack_ptr
, 1, &status
))
783 count
= stack
[--stack_ptr
];
784 status
= altera_set_dr_pre(&astate
->js
, count
, 0, NULL
);
788 * DRPRE with literal data
789 * ...stack 0 is count
790 * ...stack 1 is literal data
792 if (!altera_check_stack(stack_ptr
, 2, &status
))
794 count
= stack
[--stack_ptr
];
795 long_tmp
= stack
[--stack_ptr
];
796 put_unaligned_le32(long_tmp
, &charbuf
[0]);
797 status
= altera_set_dr_pre(&astate
->js
, count
, 0,
803 * ...stack 0 is count
805 if (altera_check_stack(stack_ptr
, 1, &status
)) {
806 count
= stack
[--stack_ptr
];
807 status
= altera_set_dr_post(&astate
->js
, count
,
813 * DRPOST with literal data
814 * ...stack 0 is count
815 * ...stack 1 is literal data
817 if (!altera_check_stack(stack_ptr
, 2, &status
))
819 count
= stack
[--stack_ptr
];
820 long_tmp
= stack
[--stack_ptr
];
821 put_unaligned_le32(long_tmp
, &charbuf
[0]);
822 status
= altera_set_dr_post(&astate
->js
, count
, 0,
826 if (altera_check_stack(stack_ptr
, 1, &status
)) {
827 count
= stack
[--stack_ptr
];
828 status
= altera_set_ir_pre(&astate
->js
, count
,
834 * IRPRE with literal data
835 * ...stack 0 is count
836 * ...stack 1 is literal data
838 if (altera_check_stack(stack_ptr
, 2, &status
)) {
839 count
= stack
[--stack_ptr
];
840 long_tmp
= stack
[--stack_ptr
];
841 put_unaligned_le32(long_tmp
, &charbuf
[0]);
842 status
= altera_set_ir_pre(&astate
->js
, count
,
849 * ...stack 0 is count
851 if (altera_check_stack(stack_ptr
, 1, &status
)) {
852 count
= stack
[--stack_ptr
];
853 status
= altera_set_ir_post(&astate
->js
, count
,
859 * IRPOST with literal data
860 * ...stack 0 is count
861 * ...stack 1 is literal data
863 if (!altera_check_stack(stack_ptr
, 2, &status
))
865 count
= stack
[--stack_ptr
];
866 long_tmp
= stack
[--stack_ptr
];
867 put_unaligned_le32(long_tmp
, &charbuf
[0]);
868 status
= altera_set_ir_post(&astate
->js
, count
, 0,
872 if (altera_check_stack(stack_ptr
, 1, &status
)) {
874 count
= strlen(msg_buff
);
875 ch
= (char) stack
[--stack_ptr
];
876 if ((ch
< 1) || (ch
> 127)) {
878 * character code out of range
879 * instead of flagging an error,
880 * force the value to 127
884 msg_buff
[count
] = ch
;
885 msg_buff
[count
+ 1] = '\0';
889 if (altera_check_stack(stack_ptr
, 1, &status
))
890 *exit_code
= stack
[--stack_ptr
];
895 if (!altera_check_stack(stack_ptr
, 2, &status
))
898 stack
[stack_ptr
- 1] =
899 (stack
[stack_ptr
- 1] == stack
[stack_ptr
]) ?
903 if (altera_check_stack(stack_ptr
, 1, &status
))
908 if (!altera_check_stack(stack_ptr
, 1, &status
))
910 if (stack
[stack_ptr
- 1] < 0)
911 stack
[stack_ptr
- 1] = 0 - stack
[stack_ptr
- 1];
929 if (altera_check_stack(stack_ptr
, 2, &status
)) {
930 long_tmp
= stack
[stack_ptr
- 2];
931 stack
[stack_ptr
- 2] = stack
[stack_ptr
- 1];
932 stack
[stack_ptr
- 1] = long_tmp
;
937 if (altera_check_stack(stack_ptr
, index
, &status
)) {
938 long_tmp
= stack
[stack_ptr
- index
];
939 stack
[stack_ptr
- index
] = stack
[stack_ptr
- 1];
940 stack
[stack_ptr
- 1] = long_tmp
;
944 if (altera_check_stack(stack_ptr
, 2, &status
)) {
945 long_tmp
= stack
[stack_ptr
- 2];
946 stack
[stack_ptr
- 2] = stack
[stack_ptr
- 1];
947 stack
[stack_ptr
- 1] = long_tmp
;
952 if (altera_check_stack(stack_ptr
, index
, &status
)) {
953 long_tmp
= stack
[stack_ptr
- index
];
954 stack
[stack_ptr
- index
] = stack
[stack_ptr
- 1];
955 stack
[stack_ptr
- 1] = long_tmp
;
960 if (altera_check_stack(stack_ptr
, index
, &status
)) {
961 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
967 if (altera_check_stack(stack_ptr
, index
, &status
)) {
968 long_tmp
= stack
[stack_ptr
- index
];
969 stack
[stack_ptr
- index
] = stack
[stack_ptr
- 1];
970 stack
[stack_ptr
- 1] = long_tmp
;
974 if (altera_check_stack(stack_ptr
, 2, &status
)) {
975 long_tmp
= stack
[stack_ptr
- 2];
976 stack
[stack_ptr
- 2] = stack
[stack_ptr
- 1];
977 stack
[stack_ptr
- 1] = long_tmp
;
982 if (altera_check_stack(stack_ptr
, index
, &status
)) {
983 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
989 if (altera_check_stack(stack_ptr
, index
, &status
)) {
990 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
995 stack
[stack_ptr
++] = 0;
998 stack
[stack_ptr
++] = (s32
) args
[0];
1001 stack
[stack_ptr
++] = vars
[args
[0]];
1004 pc
= args
[0] + code_sect
;
1005 if ((pc
< code_sect
) || (pc
>= debug_sect
))
1009 stack
[stack_ptr
++] = pc
;
1010 pc
= args
[0] + code_sect
;
1011 if ((pc
< code_sect
) || (pc
>= debug_sect
))
1016 * Process FOR / NEXT loop
1017 * ...argument 0 is variable ID
1018 * ...stack 0 is step value
1019 * ...stack 1 is end value
1020 * ...stack 2 is top address
1022 if (altera_check_stack(stack_ptr
, 3, &status
)) {
1023 s32 step
= stack
[stack_ptr
- 1];
1024 s32 end
= stack
[stack_ptr
- 2];
1025 s32 top
= stack
[stack_ptr
- 3];
1026 s32 iterator
= vars
[args
[0]];
1030 if (iterator
<= end
)
1032 } else if (iterator
>= end
)
1038 vars
[args
[0]] = iterator
+ step
;
1039 pc
= top
+ code_sect
;
1040 if ((pc
< code_sect
) ||
1049 * ...argument 0 is string ID
1051 count
= strlen(msg_buff
);
1052 strlcpy(&msg_buff
[count
],
1053 &p
[str_table
+ args
[0]],
1054 ALTERA_MESSAGE_LENGTH
- count
);
1058 * STATE intermediate state
1059 * ...argument 0 is state code
1061 status
= altera_goto_jstate(astate
, args
[0]);
1066 * ...argument 0 is state code
1068 status
= altera_goto_jstate(astate
, args
[0]);
1073 * ...argument 0 is state code
1075 status
= altera_set_irstop(&astate
->js
, args
[0]);
1080 * ...argument 0 is state code
1082 status
= altera_set_drstop(&astate
->js
, args
[0]);
1087 * Exchange top with Nth stack value
1088 * ...argument 0 is 0-based stack entry
1089 * to swap with top element
1091 index
= (args
[0]) + 1;
1092 if (altera_check_stack(stack_ptr
, index
, &status
)) {
1093 long_tmp
= stack
[stack_ptr
- index
];
1094 stack
[stack_ptr
- index
] = stack
[stack_ptr
- 1];
1095 stack
[stack_ptr
- 1] = long_tmp
;
1100 * Duplicate Nth stack value
1101 * ...argument 0 is 0-based stack entry to duplicate
1103 index
= (args
[0]) + 1;
1104 if (altera_check_stack(stack_ptr
, index
, &status
)) {
1105 stack
[stack_ptr
] = stack
[stack_ptr
- index
];
1111 * Pop stack into scalar variable
1112 * ...argument 0 is variable ID
1113 * ...stack 0 is value
1115 if (altera_check_stack(stack_ptr
, 1, &status
))
1116 vars
[args
[0]] = stack
[--stack_ptr
];
1121 * Pop stack into integer array element
1122 * ...argument 0 is variable ID
1123 * ...stack 0 is array index
1124 * ...stack 1 is value
1126 if (!altera_check_stack(stack_ptr
, 2, &status
))
1128 variable_id
= args
[0];
1131 * If variable is read-only,
1132 * convert to writable array
1134 if ((version
> 0) &&
1135 ((attrs
[variable_id
] & 0x9c) == 0x1c)) {
1136 /* Allocate a writable buffer for this array */
1137 count
= var_size
[variable_id
];
1138 long_tmp
= vars
[variable_id
];
1139 longptr_tmp
= kzalloc(count
* sizeof(long),
1141 vars
[variable_id
] = (long)longptr_tmp
;
1143 if (vars
[variable_id
] == 0) {
1148 /* copy previous contents into buffer */
1149 for (i
= 0; i
< count
; ++i
) {
1151 get_unaligned_be32(&p
[long_tmp
]);
1152 long_tmp
+= sizeof(long);
1156 * set bit 7 - buffer was
1157 * dynamically allocated
1159 attrs
[variable_id
] |= 0x80;
1161 /* clear bit 2 - variable is writable */
1162 attrs
[variable_id
] &= ~0x04;
1163 attrs
[variable_id
] |= 0x01;
1167 /* check that variable is a writable integer array */
1168 if ((attrs
[variable_id
] & 0x1c) != 0x18)
1171 longptr_tmp
= (long *)vars
[variable_id
];
1173 /* pop the array index */
1174 index
= stack
[--stack_ptr
];
1176 /* pop the value and store it into the array */
1177 longptr_tmp
[index
] = stack
[--stack_ptr
];
1183 * Pop stack into Boolean array
1184 * ...argument 0 is variable ID
1185 * ...stack 0 is count
1186 * ...stack 1 is array index
1187 * ...stack 2 is value
1189 if (!altera_check_stack(stack_ptr
, 3, &status
))
1191 variable_id
= args
[0];
1194 * If variable is read-only,
1195 * convert to writable array
1197 if ((version
> 0) &&
1198 ((attrs
[variable_id
] & 0x9c) == 0x0c)) {
1199 /* Allocate a writable buffer for this array */
1201 (var_size
[variable_id
] + 7L) >> 3L;
1202 charptr_tmp2
= (u8
*)vars
[variable_id
];
1204 kzalloc(long_tmp
, GFP_KERNEL
);
1205 vars
[variable_id
] = (long)charptr_tmp
;
1207 if (vars
[variable_id
] == 0) {
1212 /* zero the buffer */
1214 long_idx
< long_tmp
;
1216 charptr_tmp
[long_idx
] = 0;
1219 /* copy previous contents into buffer */
1221 long_idx
< var_size
[variable_id
];
1223 long_idx2
= long_idx
;
1225 if (charptr_tmp2
[long_idx2
>> 3] &
1226 (1 << (long_idx2
& 7))) {
1227 charptr_tmp
[long_idx
>> 3] |=
1228 (1 << (long_idx
& 7));
1233 * set bit 7 - buffer was
1234 * dynamically allocated
1236 attrs
[variable_id
] |= 0x80;
1238 /* clear bit 2 - variable is writable */
1239 attrs
[variable_id
] &= ~0x04;
1240 attrs
[variable_id
] |= 0x01;
1245 * check that variable is
1246 * a writable Boolean array
1248 if ((attrs
[variable_id
] & 0x1c) != 0x08) {
1253 charptr_tmp
= (u8
*)vars
[variable_id
];
1255 /* pop the count (number of bits to copy) */
1256 long_count
= stack
[--stack_ptr
];
1258 /* pop the array index */
1259 long_idx
= stack
[--stack_ptr
];
1265 * stack 0 = array right index
1266 * stack 1 = array left index
1269 if (long_idx
> long_count
) {
1271 long_tmp
= long_count
;
1272 long_count
= 1 + long_idx
-
1274 long_idx
= long_tmp
;
1276 /* reverse POPA is not supported */
1280 long_count
= 1 + long_count
-
1286 long_tmp
= stack
[--stack_ptr
];
1288 if (long_count
< 1) {
1293 for (i
= 0; i
< long_count
; ++i
) {
1294 if (long_tmp
& (1L << (s32
) i
))
1295 charptr_tmp
[long_idx
>> 3L] |=
1296 (1L << (long_idx
& 7L));
1298 charptr_tmp
[long_idx
>> 3L] &=
1299 ~(1L << (long_idx
& 7L));
1307 * Pop stack and branch if zero
1308 * ...argument 0 is address
1309 * ...stack 0 is condition value
1311 if (altera_check_stack(stack_ptr
, 1, &status
)) {
1312 if (stack
[--stack_ptr
] == 0) {
1313 pc
= args
[0] + code_sect
;
1314 if ((pc
< code_sect
) ||
1325 * ...argument 0 is scan data variable ID
1326 * ...stack 0 is array index
1327 * ...stack 1 is count
1329 if (!altera_check_stack(stack_ptr
, 2, &status
))
1331 long_idx
= stack
[--stack_ptr
];
1332 long_count
= stack
[--stack_ptr
];
1336 * stack 0 = array right index
1337 * stack 1 = array left index
1340 long_tmp
= long_count
;
1341 long_count
= stack
[--stack_ptr
];
1343 if (long_idx
> long_tmp
) {
1345 long_idx
= long_tmp
;
1349 charptr_tmp
= (u8
*)vars
[args
[0]];
1354 * and reverse the data order
1356 charptr_tmp2
= charptr_tmp
;
1357 charptr_tmp
= kzalloc((long_count
>> 3) + 1,
1359 if (charptr_tmp
== NULL
) {
1364 long_tmp
= long_idx
+ long_count
- 1;
1366 while (long_idx2
< long_count
) {
1367 if (charptr_tmp2
[long_tmp
>> 3] &
1368 (1 << (long_tmp
& 7)))
1369 charptr_tmp
[long_idx2
>> 3] |=
1370 (1 << (long_idx2
& 7));
1372 charptr_tmp
[long_idx2
>> 3] &=
1373 ~(1 << (long_idx2
& 7));
1380 if (opcode
== 0x51) /* DS */
1381 status
= altera_drscan(astate
, long_count
,
1382 charptr_tmp
, long_idx
);
1384 status
= altera_irscan(astate
, long_count
,
1385 charptr_tmp
, long_idx
);
1393 * DRPRE with array data
1394 * ...argument 0 is variable ID
1395 * ...stack 0 is array index
1396 * ...stack 1 is count
1398 if (!altera_check_stack(stack_ptr
, 2, &status
))
1400 index
= stack
[--stack_ptr
];
1401 count
= stack
[--stack_ptr
];
1405 * stack 0 = array right index
1406 * stack 1 = array left index
1408 count
= 1 + count
- index
;
1410 charptr_tmp
= (u8
*)vars
[args
[0]];
1411 status
= altera_set_dr_pre(&astate
->js
, count
, index
,
1416 * DRPOST with array data
1417 * ...argument 0 is variable ID
1418 * ...stack 0 is array index
1419 * ...stack 1 is count
1421 if (!altera_check_stack(stack_ptr
, 2, &status
))
1423 index
= stack
[--stack_ptr
];
1424 count
= stack
[--stack_ptr
];
1428 * stack 0 = array right index
1429 * stack 1 = array left index
1431 count
= 1 + count
- index
;
1433 charptr_tmp
= (u8
*)vars
[args
[0]];
1434 status
= altera_set_dr_post(&astate
->js
, count
, index
,
1439 * IRPRE with array data
1440 * ...argument 0 is variable ID
1441 * ...stack 0 is array index
1442 * ...stack 1 is count
1444 if (!altera_check_stack(stack_ptr
, 2, &status
))
1446 index
= stack
[--stack_ptr
];
1447 count
= stack
[--stack_ptr
];
1451 * stack 0 = array right index
1452 * stack 1 = array left index
1454 count
= 1 + count
- index
;
1456 charptr_tmp
= (u8
*)vars
[args
[0]];
1457 status
= altera_set_ir_pre(&astate
->js
, count
, index
,
1463 * IRPOST with array data
1464 * ...argument 0 is variable ID
1465 * ...stack 0 is array index
1466 * ...stack 1 is count
1468 if (!altera_check_stack(stack_ptr
, 2, &status
))
1470 index
= stack
[--stack_ptr
];
1471 count
= stack
[--stack_ptr
];
1475 * stack 0 = array right index
1476 * stack 1 = array left index
1478 count
= 1 + count
- index
;
1480 charptr_tmp
= (u8
*)vars
[args
[0]];
1481 status
= altera_set_ir_post(&astate
->js
, count
, index
,
1488 * ...argument 0 is string ID
1489 * ...stack 0 is integer expression
1491 if (altera_check_stack(stack_ptr
, 1, &status
)) {
1492 name
= &p
[str_table
+ args
[0]];
1493 long_tmp
= stack
[--stack_ptr
];
1494 altera_export_int(name
, long_tmp
);
1499 * Push integer array element
1500 * ...argument 0 is variable ID
1501 * ...stack 0 is array index
1503 if (!altera_check_stack(stack_ptr
, 1, &status
))
1505 variable_id
= args
[0];
1506 index
= stack
[stack_ptr
- 1];
1508 /* check variable type */
1509 if ((attrs
[variable_id
] & 0x1f) == 0x19) {
1510 /* writable integer array */
1511 longptr_tmp
= (long *)vars
[variable_id
];
1512 stack
[stack_ptr
- 1] = longptr_tmp
[index
];
1513 } else if ((attrs
[variable_id
] & 0x1f) == 0x1c) {
1514 /* read-only integer array */
1515 long_tmp
= vars
[variable_id
] +
1516 (index
* sizeof(long));
1517 stack
[stack_ptr
- 1] =
1518 get_unaligned_be32(&p
[long_tmp
]);
1525 * Push Boolean array
1526 * ...argument 0 is variable ID
1527 * ...stack 0 is count
1528 * ...stack 1 is array index
1530 if (!altera_check_stack(stack_ptr
, 2, &status
))
1532 variable_id
= args
[0];
1534 /* check that variable is a Boolean array */
1535 if ((attrs
[variable_id
] & 0x18) != 0x08) {
1540 charptr_tmp
= (u8
*)vars
[variable_id
];
1542 /* pop the count (number of bits to copy) */
1543 count
= stack
[--stack_ptr
];
1545 /* pop the array index */
1546 index
= stack
[stack_ptr
- 1];
1550 * stack 0 = array right index
1551 * stack 1 = array left index
1553 count
= 1 + count
- index
;
1555 if ((count
< 1) || (count
> 32)) {
1562 for (i
= 0; i
< count
; ++i
)
1563 if (charptr_tmp
[(i
+ index
) >> 3] &
1564 (1 << ((i
+ index
) & 7)))
1565 long_tmp
|= (1L << i
);
1567 stack
[stack_ptr
- 1] = long_tmp
;
1572 * Dynamically change size of array
1573 * ...argument 0 is variable ID
1574 * ...stack 0 is new size
1576 if (!altera_check_stack(stack_ptr
, 1, &status
))
1578 variable_id
= args
[0];
1579 long_tmp
= stack
[--stack_ptr
];
1581 if (long_tmp
> var_size
[variable_id
]) {
1582 var_size
[variable_id
] = long_tmp
;
1584 if (attrs
[variable_id
] & 0x10)
1585 /* allocate integer array */
1586 long_tmp
*= sizeof(long);
1588 /* allocate Boolean array */
1589 long_tmp
= (long_tmp
+ 7) >> 3;
1592 * If the buffer was previously allocated,
1595 if (attrs
[variable_id
] & 0x80) {
1596 kfree((void *)vars
[variable_id
]);
1597 vars
[variable_id
] = 0;
1601 * Allocate a new buffer
1602 * of the requested size
1604 vars
[variable_id
] = (long)
1605 kzalloc(long_tmp
, GFP_KERNEL
);
1607 if (vars
[variable_id
] == 0) {
1613 * Set the attribute bit to indicate that
1614 * this buffer was dynamically allocated and
1615 * should be freed later
1617 attrs
[variable_id
] |= 0x80;
1619 /* zero out memory */
1620 count
= ((var_size
[variable_id
] + 7L) /
1622 charptr_tmp
= (u8
*)(vars
[variable_id
]);
1623 for (index
= 0; index
< count
; ++index
)
1624 charptr_tmp
[index
] = 0;
1631 * Export Boolean array
1632 * ...argument 0 is string ID
1633 * ...stack 0 is variable ID
1634 * ...stack 1 is array right index
1635 * ...stack 2 is array left index
1637 if (!altera_check_stack(stack_ptr
, 3, &status
))
1640 /* EXPV is not supported in JBC 1.0 */
1644 name
= &p
[str_table
+ args
[0]];
1645 variable_id
= stack
[--stack_ptr
];
1646 long_idx
= stack
[--stack_ptr
];/* right indx */
1647 long_idx2
= stack
[--stack_ptr
];/* left indx */
1649 if (long_idx
> long_idx2
) {
1650 /* reverse indices not supported */
1655 long_count
= 1 + long_idx2
- long_idx
;
1657 charptr_tmp
= (u8
*)vars
[variable_id
];
1658 charptr_tmp2
= NULL
;
1660 if ((long_idx
& 7L) != 0) {
1663 kzalloc(((long_count
+ 7L) / 8L),
1665 if (charptr_tmp2
== NULL
) {
1670 for (i
= 0; i
< long_count
; ++i
) {
1671 if (charptr_tmp
[k
>> 3] &
1673 charptr_tmp2
[i
>> 3] |=
1676 charptr_tmp2
[i
>> 3] &=
1681 charptr_tmp
= charptr_tmp2
;
1683 } else if (long_idx
!= 0)
1684 charptr_tmp
= &charptr_tmp
[long_idx
>> 3];
1686 altera_export_bool_array(name
, charptr_tmp
,
1689 /* free allocated buffer */
1690 if ((long_idx
& 7L) != 0)
1691 kfree(charptr_tmp2
);
1697 * ...argument 0 is dest ID
1698 * ...argument 1 is source ID
1699 * ...stack 0 is count
1700 * ...stack 1 is dest index
1701 * ...stack 2 is source index
1709 int src_reverse
= 0;
1710 int dest_reverse
= 0;
1712 if (!altera_check_stack(stack_ptr
, 3, &status
))
1715 copy_count
= stack
[--stack_ptr
];
1716 copy_index
= stack
[--stack_ptr
];
1717 copy_index2
= stack
[--stack_ptr
];
1722 * stack 0 = source right index
1723 * stack 1 = source left index
1724 * stack 2 = destination right index
1725 * stack 3 = destination left index
1727 destleft
= stack
[--stack_ptr
];
1729 if (copy_count
> copy_index
) {
1732 src_count
= 1 + copy_count
- copy_index
;
1733 /* copy_index = source start index */
1735 src_count
= 1 + copy_index
- copy_count
;
1736 /* source start index */
1737 copy_index
= copy_count
;
1740 if (copy_index2
> destleft
) {
1743 dest_count
= 1 + copy_index2
- destleft
;
1744 /* destination start index */
1745 copy_index2
= destleft
;
1747 dest_count
= 1 + destleft
- copy_index2
;
1749 copy_count
= (src_count
< dest_count
) ?
1750 src_count
: dest_count
;
1752 if ((src_reverse
|| dest_reverse
) &&
1753 (src_count
!= dest_count
))
1755 * If either the source or destination
1756 * is reversed, we can't tolerate
1757 * a length mismatch, because we
1758 * "left justify" arrays when copying.
1759 * This won't work correctly
1760 * with reversed arrays.
1768 index2
= copy_index2
;
1771 * If destination is a read-only array,
1772 * allocate a buffer and convert it to a writable array
1774 variable_id
= args
[1];
1775 if ((version
> 0) &&
1776 ((attrs
[variable_id
] & 0x9c) == 0x0c)) {
1777 /* Allocate a writable buffer for this array */
1779 (var_size
[variable_id
] + 7L) >> 3L;
1780 charptr_tmp2
= (u8
*)vars
[variable_id
];
1782 kzalloc(long_tmp
, GFP_KERNEL
);
1783 vars
[variable_id
] = (long)charptr_tmp
;
1785 if (vars
[variable_id
] == 0) {
1790 /* zero the buffer */
1791 for (long_idx
= 0L; long_idx
< long_tmp
;
1793 charptr_tmp
[long_idx
] = 0;
1795 /* copy previous contents into buffer */
1797 long_idx
< var_size
[variable_id
];
1799 long_idx2
= long_idx
;
1801 if (charptr_tmp2
[long_idx2
>> 3] &
1802 (1 << (long_idx2
& 7)))
1803 charptr_tmp
[long_idx
>> 3] |=
1804 (1 << (long_idx
& 7));
1809 set bit 7 - buffer was dynamically allocated */
1810 attrs
[variable_id
] |= 0x80;
1812 /* clear bit 2 - variable is writable */
1813 attrs
[variable_id
] &= ~0x04;
1814 attrs
[variable_id
] |= 0x01;
1817 charptr_tmp
= (u8
*)vars
[args
[1]];
1818 charptr_tmp2
= (u8
*)vars
[args
[0]];
1820 /* check if destination is a writable Boolean array */
1821 if ((attrs
[args
[1]] & 0x1c) != 0x08) {
1832 index2
+= (count
- 1);
1834 for (i
= 0; i
< count
; ++i
) {
1835 if (charptr_tmp2
[index
>> 3] &
1837 charptr_tmp
[index2
>> 3] |=
1838 (1 << (index2
& 7));
1840 charptr_tmp
[index2
>> 3] &=
1841 ~(1 << (index2
& 7));
1855 * DRSCAN with capture
1856 * IRSCAN with capture
1857 * ...argument 0 is scan data variable ID
1858 * ...argument 1 is capture variable ID
1859 * ...stack 0 is capture index
1860 * ...stack 1 is scan data index
1861 * ...stack 2 is count
1863 s32 scan_right
, scan_left
;
1864 s32 capture_count
= 0;
1869 if (!altera_check_stack(stack_ptr
, 3, &status
))
1872 capture_index
= stack
[--stack_ptr
];
1873 scan_index
= stack
[--stack_ptr
];
1877 * stack 0 = capture right index
1878 * stack 1 = capture left index
1879 * stack 2 = scan right index
1880 * stack 3 = scan left index
1883 scan_right
= stack
[--stack_ptr
];
1884 scan_left
= stack
[--stack_ptr
];
1885 capture_count
= 1 + scan_index
- capture_index
;
1886 scan_count
= 1 + scan_left
- scan_right
;
1887 scan_index
= scan_right
;
1890 long_count
= stack
[--stack_ptr
];
1892 * If capture array is read-only, allocate a buffer
1893 * and convert it to a writable array
1895 variable_id
= args
[1];
1896 if ((version
> 0) &&
1897 ((attrs
[variable_id
] & 0x9c) == 0x0c)) {
1898 /* Allocate a writable buffer for this array */
1900 (var_size
[variable_id
] + 7L) >> 3L;
1901 charptr_tmp2
= (u8
*)vars
[variable_id
];
1903 kzalloc(long_tmp
, GFP_KERNEL
);
1904 vars
[variable_id
] = (long)charptr_tmp
;
1906 if (vars
[variable_id
] == 0) {
1911 /* zero the buffer */
1912 for (long_idx
= 0L; long_idx
< long_tmp
;
1914 charptr_tmp
[long_idx
] = 0;
1916 /* copy previous contents into buffer */
1918 long_idx
< var_size
[variable_id
];
1920 long_idx2
= long_idx
;
1922 if (charptr_tmp2
[long_idx2
>> 3] &
1923 (1 << (long_idx2
& 7)))
1924 charptr_tmp
[long_idx
>> 3] |=
1925 (1 << (long_idx
& 7));
1930 * set bit 7 - buffer was
1931 * dynamically allocated
1933 attrs
[variable_id
] |= 0x80;
1935 /* clear bit 2 - variable is writable */
1936 attrs
[variable_id
] &= ~0x04;
1937 attrs
[variable_id
] |= 0x01;
1941 charptr_tmp
= (u8
*)vars
[args
[0]];
1942 charptr_tmp2
= (u8
*)vars
[args
[1]];
1944 if ((version
> 0) &&
1945 ((long_count
> capture_count
) ||
1946 (long_count
> scan_count
))) {
1952 * check that capture array
1953 * is a writable Boolean array
1955 if ((attrs
[args
[1]] & 0x1c) != 0x08) {
1961 if (opcode
== 0x82) /* DSC */
1962 status
= altera_swap_dr(astate
,
1969 status
= altera_swap_ir(astate
,
1983 * ...argument 0 is wait state
1984 * ...argument 1 is end state
1985 * ...stack 0 is cycles
1986 * ...stack 1 is microseconds
1988 if (!altera_check_stack(stack_ptr
, 2, &status
))
1990 long_tmp
= stack
[--stack_ptr
];
1993 status
= altera_wait_cycles(astate
, long_tmp
,
1996 long_tmp
= stack
[--stack_ptr
];
1998 if ((status
== 0) && (long_tmp
!= 0L))
1999 status
= altera_wait_msecs(astate
,
2003 if ((status
== 0) && (args
[1] != args
[0]))
2004 status
= altera_goto_jstate(astate
,
2008 --stack_ptr
; /* throw away MAX cycles */
2009 --stack_ptr
; /* throw away MAX microseconds */
2015 * ...argument 0 is source 1 ID
2016 * ...argument 1 is source 2 ID
2017 * ...argument 2 is mask ID
2018 * ...stack 0 is source 1 index
2019 * ...stack 1 is source 2 index
2020 * ...stack 2 is mask index
2021 * ...stack 3 is count
2024 u8
*source1
= (u8
*)vars
[args
[0]];
2025 u8
*source2
= (u8
*)vars
[args
[1]];
2026 u8
*mask
= (u8
*)vars
[args
[2]];
2031 if (!altera_check_stack(stack_ptr
, 4, &status
))
2034 index1
= stack
[--stack_ptr
];
2035 index2
= stack
[--stack_ptr
];
2036 mask_index
= stack
[--stack_ptr
];
2037 long_count
= stack
[--stack_ptr
];
2041 * stack 0 = source 1 right index
2042 * stack 1 = source 1 left index
2043 * stack 2 = source 2 right index
2044 * stack 3 = source 2 left index
2045 * stack 4 = mask right index
2046 * stack 5 = mask left index
2048 s32 mask_right
= stack
[--stack_ptr
];
2049 s32 mask_left
= stack
[--stack_ptr
];
2050 /* source 1 count */
2051 a
= 1 + index2
- index1
;
2052 /* source 2 count */
2053 b
= 1 + long_count
- mask_index
;
2054 a
= (a
< b
) ? a
: b
;
2056 b
= 1 + mask_left
- mask_right
;
2057 a
= (a
< b
) ? a
: b
;
2058 /* source 2 start index */
2059 index2
= mask_index
;
2060 /* mask start index */
2061 mask_index
= mask_right
;
2072 for (i
= 0; i
< count
; ++i
) {
2073 if (mask
[mask_index
>> 3] &
2074 (1 << (mask_index
& 7))) {
2075 a
= source1
[index1
>> 3] &
2078 b
= source2
[index2
>> 3] &
2082 if (a
!= b
) /* failure */
2091 stack
[stack_ptr
++] = long_tmp
;
2096 /* Unrecognized opcode -- ERROR! */
2104 if ((stack_ptr
< 0) || (stack_ptr
>= ALTERA_STACK_SIZE
))
2105 status
= -EOVERFLOW
;
2109 *error_address
= (s32
)(opcode_address
- code_sect
);
2113 altera_free_buffers(astate
);
2115 /* Free all dynamically allocated arrays */
2116 if ((attrs
!= NULL
) && (vars
!= NULL
))
2117 for (i
= 0; i
< sym_count
; ++i
)
2118 if (attrs
[i
] & 0x80)
2119 kfree((void *)vars
[i
]);
2124 kfree(proc_attributes
);
2129 static int altera_get_note(u8
*p
, s32 program_size
,
2130 s32
*offset
, char *key
, char *value
, int length
)
2132 * Gets key and value of NOTE fields in the JBC file.
2133 * Can be called in two modes: if offset pointer is NULL,
2134 * then the function searches for note fields which match
2135 * the key string provided. If offset is not NULL, then
2136 * the function finds the next note field of any key,
2137 * starting at the offset specified by the offset pointer.
2138 * Returns 0 for success, else appropriate error code
2141 int status
= -ENODATA
;
2142 u32 note_strings
= 0L;
2143 u32 note_table
= 0L;
2144 u32 note_count
= 0L;
2145 u32 first_word
= 0L;
2152 /* Read header information */
2153 if (program_size
> 52L) {
2154 first_word
= get_unaligned_be32(&p
[0]);
2155 version
= (first_word
& 1L);
2156 delta
= version
* 8;
2158 note_strings
= get_unaligned_be32(&p
[8 + delta
]);
2159 note_table
= get_unaligned_be32(&p
[12 + delta
]);
2160 note_count
= get_unaligned_be32(&p
[44 + (2 * delta
)]);
2163 if ((first_word
!= 0x4A414D00L
) && (first_word
!= 0x4A414D01L
))
2166 if (note_count
<= 0L)
2169 if (offset
== NULL
) {
2171 * We will search for the first note with a specific key,
2172 * and return only the value
2174 for (i
= 0; (i
< note_count
) &&
2175 (status
!= 0); ++i
) {
2176 key_ptr
= &p
[note_strings
+
2178 &p
[note_table
+ (8 * i
)])];
2179 if ((strnicmp(key
, key_ptr
, strlen(key_ptr
)) == 0) &&
2183 value_ptr
= &p
[note_strings
+
2185 &p
[note_table
+ (8 * i
) + 4])];
2188 strlcpy(value
, value_ptr
, length
);
2194 * We will search for the next note, regardless of the key,
2195 * and return both the value and the key
2200 if ((i
>= 0) && (i
< note_count
)) {
2204 strlcpy(key
, &p
[note_strings
+
2206 &p
[note_table
+ (8 * i
)])],
2210 strlcpy(value
, &p
[note_strings
+
2212 &p
[note_table
+ (8 * i
) + 4])],
2222 static int altera_check_crc(u8
*p
, s32 program_size
)
2225 u16 local_expected
= 0,
2231 u32 crc_section
= 0L;
2232 u32 first_word
= 0L;
2236 if (program_size
> 52L) {
2237 first_word
= get_unaligned_be32(&p
[0]);
2238 version
= (first_word
& 1L);
2239 delta
= version
* 8;
2241 crc_section
= get_unaligned_be32(&p
[32 + delta
]);
2244 if ((first_word
!= 0x4A414D00L
) && (first_word
!= 0x4A414D01L
))
2247 if (crc_section
>= program_size
)
2251 local_expected
= (u16
)get_unaligned_be16(&p
[crc_section
]);
2253 for (i
= 0; i
< crc_section
; ++i
) {
2255 for (bit
= 0; bit
< 8; bit
++) {
2256 feedback
= (databyte
^ shift_reg
) & 0x01;
2259 shift_reg
^= 0x8408;
2265 local_actual
= (u16
)~shift_reg
;
2267 if (local_expected
!= local_actual
)
2272 if (debug
|| status
) {
2275 printk(KERN_INFO
"%s: CRC matched: %04x\n", __func__
,
2279 printk(KERN_ERR
"%s: CRC mismatch: expected %04x, "
2280 "actual %04x\n", __func__
, local_expected
,
2284 printk(KERN_ERR
"%s: expected CRC not found, "
2285 "actual CRC = %04x\n", __func__
,
2289 printk(KERN_ERR
"%s: error: format isn't "
2290 "recognized.\n", __func__
);
2293 printk(KERN_ERR
"%s: CRC function returned error "
2294 "code %d\n", __func__
, status
);
2302 static int altera_get_file_info(u8
*p
,
2304 int *format_version
,
2306 int *procedure_count
)
2312 if (program_size
<= 52L)
2315 first_word
= get_unaligned_be32(&p
[0]);
2317 if ((first_word
== 0x4A414D00L
) || (first_word
== 0x4A414D01L
)) {
2320 version
= (first_word
& 1L);
2321 *format_version
= version
+ 1;
2324 *action_count
= get_unaligned_be32(&p
[48]);
2325 *procedure_count
= get_unaligned_be32(&p
[52]);
2332 static int altera_get_act_info(u8
*p
,
2337 struct altera_procinfo
**proc_list
)
2340 struct altera_procinfo
*procptr
= NULL
;
2341 struct altera_procinfo
*tmpptr
= NULL
;
2342 u32 first_word
= 0L;
2343 u32 action_table
= 0L;
2344 u32 proc_table
= 0L;
2346 u32 note_strings
= 0L;
2347 u32 action_count
= 0L;
2348 u32 proc_count
= 0L;
2349 u32 act_name_id
= 0L;
2350 u32 act_desc_id
= 0L;
2351 u32 act_proc_id
= 0L;
2352 u32 act_proc_name
= 0L;
2353 u8 act_proc_attribute
= 0;
2355 if (program_size
<= 52L)
2357 /* Read header information */
2358 first_word
= get_unaligned_be32(&p
[0]);
2360 if (first_word
!= 0x4A414D01L
)
2363 action_table
= get_unaligned_be32(&p
[4]);
2364 proc_table
= get_unaligned_be32(&p
[8]);
2365 str_table
= get_unaligned_be32(&p
[12]);
2366 note_strings
= get_unaligned_be32(&p
[16]);
2367 action_count
= get_unaligned_be32(&p
[48]);
2368 proc_count
= get_unaligned_be32(&p
[52]);
2370 if (index
>= action_count
)
2373 act_name_id
= get_unaligned_be32(&p
[action_table
+ (12 * index
)]);
2374 act_desc_id
= get_unaligned_be32(&p
[action_table
+ (12 * index
) + 4]);
2375 act_proc_id
= get_unaligned_be32(&p
[action_table
+ (12 * index
) + 8]);
2377 *name
= &p
[str_table
+ act_name_id
];
2379 if (act_desc_id
< (note_strings
- str_table
))
2380 *description
= &p
[str_table
+ act_desc_id
];
2383 act_proc_name
= get_unaligned_be32(
2384 &p
[proc_table
+ (13 * act_proc_id
)]);
2385 act_proc_attribute
=
2386 (p
[proc_table
+ (13 * act_proc_id
) + 8] & 0x03);
2389 kzalloc(sizeof(struct altera_procinfo
),
2392 if (procptr
== NULL
)
2395 procptr
->name
= &p
[str_table
+ act_proc_name
];
2396 procptr
->attrs
= act_proc_attribute
;
2397 procptr
->next
= NULL
;
2399 /* add record to end of linked list */
2400 if (*proc_list
== NULL
)
2401 *proc_list
= procptr
;
2403 tmpptr
= *proc_list
;
2404 while (tmpptr
->next
!= NULL
)
2405 tmpptr
= tmpptr
->next
;
2406 tmpptr
->next
= procptr
;
2410 act_proc_id
= get_unaligned_be32(
2411 &p
[proc_table
+ (13 * act_proc_id
) + 4]);
2412 } while ((act_proc_id
!= 0) && (act_proc_id
< proc_count
));
2417 int altera_init(struct altera_config
*config
, const struct firmware
*fw
)
2419 struct altera_state
*astate
= NULL
;
2420 struct altera_procinfo
*proc_list
= NULL
;
2421 struct altera_procinfo
*procptr
= NULL
;
2424 char *action_name
= NULL
;
2425 char *description
= NULL
;
2426 int exec_result
= 0;
2428 int format_version
= 0;
2429 int action_count
= 0;
2430 int procedure_count
= 0;
2433 s32 error_address
= 0L;
2436 key
= kzalloc(33, GFP_KERNEL
);
2441 value
= kzalloc(257, GFP_KERNEL
);
2446 astate
= kzalloc(sizeof(struct altera_state
), GFP_KERNEL
);
2452 astate
->config
= config
;
2453 if (!astate
->config
->jtag_io
) {
2454 dprintk(KERN_INFO
"%s: using byteblaster!\n", __func__
);
2455 astate
->config
->jtag_io
= netup_jtag_io_lpt
;
2458 altera_check_crc((u8
*)fw
->data
, fw
->size
);
2461 altera_get_file_info((u8
*)fw
->data
, fw
->size
, &format_version
,
2462 &action_count
, &procedure_count
);
2463 printk(KERN_INFO
"%s: File format is %s ByteCode format\n",
2464 __func__
, (format_version
== 2) ? "Jam STAPL" :
2465 "pre-standardized Jam 1.1");
2466 while (altera_get_note((u8
*)fw
->data
, fw
->size
,
2467 &offset
, key
, value
, 256) == 0)
2468 printk(KERN_INFO
"%s: NOTE \"%s\" = \"%s\"\n",
2469 __func__
, key
, value
);
2472 if (debug
&& (format_version
== 2) && (action_count
> 0)) {
2473 printk(KERN_INFO
"%s: Actions available:\n", __func__
);
2474 for (index
= 0; index
< action_count
; ++index
) {
2475 altera_get_act_info((u8
*)fw
->data
, fw
->size
,
2476 index
, &action_name
,
2480 if (description
== NULL
)
2481 printk(KERN_INFO
"%s: %s\n",
2485 printk(KERN_INFO
"%s: %s \"%s\"\n",
2490 procptr
= proc_list
;
2491 while (procptr
!= NULL
) {
2492 if (procptr
->attrs
!= 0)
2493 printk(KERN_INFO
"%s: %s (%s)\n",
2496 (procptr
->attrs
== 1) ?
2497 "optional" : "recommended");
2499 proc_list
= procptr
->next
;
2501 procptr
= proc_list
;
2505 printk(KERN_INFO
"\n");
2508 exec_result
= altera_execute(astate
, (u8
*)fw
->data
, fw
->size
,
2509 &error_address
, &exit_code
, &format_version
);
2512 exec_result
= -EREMOTEIO
;
2514 if ((format_version
== 2) && (exec_result
== -EINVAL
)) {
2515 if (astate
->config
->action
== NULL
)
2516 printk(KERN_ERR
"%s: error: no action specified for "
2517 "Jam STAPL file.\nprogram terminated.\n",
2520 printk(KERN_ERR
"%s: error: action \"%s\""
2521 " is not supported "
2522 "for this Jam STAPL file.\n"
2523 "Program terminated.\n", __func__
,
2524 astate
->config
->action
);
2526 } else if (exec_result
)
2527 printk(KERN_ERR
"%s: error %d\n", __func__
, exec_result
);
2537 EXPORT_SYMBOL(altera_init
);