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 <misc/altera.h>
32 #include "altera-exprt.h"
33 #include "altera-jtag.h"
36 module_param(debug, int, 0644);
37 MODULE_PARM_DESC(debug, "enable debugging information");
39 MODULE_DESCRIPTION("altera FPGA kernel module");
40 MODULE_AUTHOR("Igor M. Liplianin <liplianin@netup.ru>");
41 MODULE_LICENSE("GPL");
43 #define dprintk(args...) \
45 printk(KERN_DEBUG args); \
48 enum altera_fpga_opcode {
126 struct altera_procinfo {
129 struct altera_procinfo *next;
132 /* This function checks if enough parameters are available on the stack. */
133 static int altera_check_stack(int stack_ptr, int count, int *status)
135 if (stack_ptr < count) {
136 *status = -EOVERFLOW;
143 static void altera_export_int(char *key, s32 value)
145 dprintk("Export: key = \"%s\", value = %d\n", key, value);
148 #define HEX_LINE_CHARS 72
149 #define HEX_LINE_BITS (HEX_LINE_CHARS * 4)
151 static void altera_export_bool_array(char *key, u8 *data, s32 count)
153 char string[HEX_LINE_CHARS + 1];
155 u32 size, line, lines, linebits, value, j, k;
157 if (count > HEX_LINE_BITS) {
158 dprintk("Export: key = \"%s\", %d bits, value = HEX\n",
160 lines = (count + (HEX_LINE_BITS - 1)) / HEX_LINE_BITS;
162 for (line = 0; line < lines; ++line) {
163 if (line < (lines - 1)) {
164 linebits = HEX_LINE_BITS;
165 size = HEX_LINE_CHARS;
166 offset = count - ((line + 1) * HEX_LINE_BITS);
169 count - ((lines - 1) * HEX_LINE_BITS);
170 size = (linebits + 3) / 4;
178 for (k = 0; k < linebits; ++k) {
180 if (data[i >> 3] & (1 << (i & 7)))
181 value |= (1 << (i & 3));
183 sprintf(&string[j], "%1x", value);
189 sprintf(&string[j], "%1x", value);
191 dprintk("%s\n", string);
195 size = (count + 3) / 4;
200 for (i = 0; i < count; ++i) {
201 if (data[i >> 3] & (1 << (i & 7)))
202 value |= (1 << (i & 3));
204 sprintf(&string[j], "%1x", value);
210 sprintf(&string[j], "%1x", value);
212 dprintk("Export: key = \"%s\", %d bits, value = HEX %s\n",
217 static int altera_execute(struct altera_state *astate,
224 struct altera_config *aconf = astate->config;
225 char *msg_buff = astate->msg_buff;
226 long *stack = astate->stack;
229 u32 action_table = 0L;
236 u32 action_count = 0L;
240 s32 *var_size = NULL;
242 u8 *proc_attributes = NULL;
271 int current_proc = 0;
276 dprintk("%s\n", __func__);
278 /* Read header information */
279 if (program_size > 52L) {
280 first_word = get_unaligned_be32(&p[0]);
281 version = (first_word & 1L);
282 *format_version = version + 1;
285 action_table = get_unaligned_be32(&p[4]);
286 proc_table = get_unaligned_be32(&p[8]);
287 str_table = get_unaligned_be32(&p[4 + delta]);
288 sym_table = get_unaligned_be32(&p[16 + delta]);
289 data_sect = get_unaligned_be32(&p[20 + delta]);
290 code_sect = get_unaligned_be32(&p[24 + delta]);
291 debug_sect = get_unaligned_be32(&p[28 + delta]);
292 action_count = get_unaligned_be32(&p[40 + delta]);
293 proc_count = get_unaligned_be32(&p[44 + delta]);
294 sym_count = get_unaligned_be32(&p[48 + (2 * delta)]);
297 if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L)) {
306 vars = kzalloc(sym_count * sizeof(long), GFP_KERNEL);
312 var_size = kzalloc(sym_count * sizeof(s32), GFP_KERNEL);
314 if (var_size == NULL)
319 attrs = kzalloc(sym_count, GFP_KERNEL);
325 if ((status == 0) && (version > 0)) {
326 proc_attributes = kzalloc(proc_count, GFP_KERNEL);
328 if (proc_attributes == NULL)
337 for (i = 0; i < sym_count; ++i) {
338 offset = (sym_table + ((11 + delta) * i));
340 value = get_unaligned_be32(&p[offset + 3 + delta]);
342 attrs[i] = p[offset];
345 * use bit 7 of attribute byte to indicate that
346 * this buffer was dynamically allocated
347 * and should be freed later
351 var_size[i] = get_unaligned_be32(&p[offset + 7 + delta]);
355 * bit 0: 0 = read-only, 1 = read-write
356 * bit 1: 0 = not compressed, 1 = compressed
357 * bit 2: 0 = not initialized, 1 = initialized
358 * bit 3: 0 = scalar, 1 = array
359 * bit 4: 0 = Boolean, 1 = integer
360 * bit 5: 0 = declared variable,
361 * 1 = compiler created temporary variable
364 if ((attrs[i] & 0x0c) == 0x04)
365 /* initialized scalar variable */
367 else if ((attrs[i] & 0x1e) == 0x0e) {
368 /* initialized compressed Boolean array */
369 uncomp_size = get_unaligned_le32(&p[data_sect + value]);
371 /* allocate a buffer for the uncompressed data */
372 vars[i] = (long)kzalloc(uncomp_size, GFP_KERNEL);
376 /* set flag so buffer will be freed later */
379 /* uncompress the data */
380 if (altera_shrink(&p[data_sect + value],
384 version) != uncomp_size)
385 /* decompression failed */
388 var_size[i] = uncomp_size * 8L;
391 } else if ((attrs[i] & 0x1e) == 0x0c) {
392 /* initialized Boolean array */
393 vars[i] = value + data_sect + (long)p;
394 } else if ((attrs[i] & 0x1c) == 0x1c) {
395 /* initialized integer array */
396 vars[i] = value + data_sect;
397 } else if ((attrs[i] & 0x0c) == 0x08) {
398 /* uninitialized array */
400 /* flag attrs so that memory is freed */
403 if (var_size[i] > 0) {
408 size = (var_size[i] * sizeof(s32));
411 size = ((var_size[i] + 7L) / 8L);
413 vars[i] = (long)kzalloc(size, GFP_KERNEL);
418 /* zero out memory */
419 for (j = 0; j < size; ++j)
420 ((u8 *)(vars[i]))[j] = 0;
435 altera_jinit(astate);
441 * For JBC version 2, we will execute the procedures corresponding to
442 * the selected ACTION
445 if (aconf->action == NULL) {
449 int action_found = 0;
450 for (i = 0; (i < action_count) && !action_found; ++i) {
451 name_id = get_unaligned_be32(&p[action_table +
454 name = &p[str_table + name_id];
456 if (strnicmp(aconf->action, name, strlen(name)) == 0) {
459 get_unaligned_be32(&p[action_table +
473 while ((i != 0) || first_time) {
475 /* check procedure attribute byte */
488 i = get_unaligned_be32(&p[proc_table +
493 * Set current_proc to the first procedure
498 ((proc_attributes[i] == 1) ||
499 ((proc_attributes[i] & 0xc0) == 0x40))) {
500 i = get_unaligned_be32(&p[proc_table +
504 if ((i != 0) || ((i == 0) && (current_proc == 0) &&
505 ((proc_attributes[0] != 1) &&
506 ((proc_attributes[0] & 0xc0) != 0x40)))) {
509 get_unaligned_be32(&p[proc_table +
511 if ((pc < code_sect) || (pc >= debug_sect))
514 /* there are no procedures to execute! */
523 opcode = (p[pc] & 0xff);
528 printk("opcode: %02x\n", opcode);
530 arg_count = (opcode >> 6) & 3;
531 for (i = 0; i < arg_count; ++i) {
532 args[i] = get_unaligned_be32(&p[pc]);
540 if (altera_check_stack(stack_ptr, 1, &status)) {
541 stack[stack_ptr] = stack[stack_ptr - 1];
546 if (altera_check_stack(stack_ptr, 2, &status)) {
547 long_tmp = stack[stack_ptr - 2];
548 stack[stack_ptr - 2] = stack[stack_ptr - 1];
549 stack[stack_ptr - 1] = long_tmp;
553 if (altera_check_stack(stack_ptr, 2, &status)) {
555 stack[stack_ptr - 1] += stack[stack_ptr];
559 if (altera_check_stack(stack_ptr, 2, &status)) {
561 stack[stack_ptr - 1] -= stack[stack_ptr];
565 if (altera_check_stack(stack_ptr, 2, &status)) {
567 stack[stack_ptr - 1] *= stack[stack_ptr];
571 if (altera_check_stack(stack_ptr, 2, &status)) {
573 stack[stack_ptr - 1] /= stack[stack_ptr];
577 if (altera_check_stack(stack_ptr, 2, &status)) {
579 stack[stack_ptr - 1] %= stack[stack_ptr];
583 if (altera_check_stack(stack_ptr, 2, &status)) {
585 stack[stack_ptr - 1] <<= stack[stack_ptr];
589 if (altera_check_stack(stack_ptr, 2, &status)) {
591 stack[stack_ptr - 1] >>= stack[stack_ptr];
595 if (altera_check_stack(stack_ptr, 1, &status))
596 stack[stack_ptr - 1] ^= (-1L);
600 if (altera_check_stack(stack_ptr, 2, &status)) {
602 stack[stack_ptr - 1] &= stack[stack_ptr];
606 if (altera_check_stack(stack_ptr, 2, &status)) {
608 stack[stack_ptr - 1] |= stack[stack_ptr];
612 if (altera_check_stack(stack_ptr, 2, &status)) {
614 stack[stack_ptr - 1] ^= stack[stack_ptr];
618 if (!altera_check_stack(stack_ptr, 1, &status))
620 stack[stack_ptr - 1] = stack[stack_ptr - 1] ? 0L : 1L;
623 if (!altera_check_stack(stack_ptr, 2, &status))
626 stack[stack_ptr - 1] =
627 (stack[stack_ptr - 1] > stack[stack_ptr]) ?
632 if (!altera_check_stack(stack_ptr, 2, &status))
635 stack[stack_ptr - 1] =
636 (stack[stack_ptr - 1] < stack[stack_ptr]) ?
641 if ((version > 0) && (stack_ptr == 0)) {
643 * We completed one of the main procedures
645 * Find the next procedure
646 * to be executed and jump to it.
647 * If there are no more procedures, then EXIT.
649 i = get_unaligned_be32(&p[proc_table +
650 (13 * current_proc) + 4]);
652 ((proc_attributes[i] == 1) ||
653 ((proc_attributes[i] & 0xc0) == 0x40)))
654 i = get_unaligned_be32(&p[proc_table +
658 /* no procedures to execute! */
660 *exit_code = 0; /* success */
663 pc = code_sect + get_unaligned_be32(
666 if ((pc < code_sect) ||
672 if (altera_check_stack(stack_ptr, 1, &status)) {
673 pc = stack[--stack_ptr] + code_sect;
674 if ((pc <= code_sect) ||
683 * Array short compare
684 * ...stack 0 is source 1 value
685 * ...stack 1 is source 2 value
686 * ...stack 2 is mask value
687 * ...stack 3 is count
689 if (altera_check_stack(stack_ptr, 4, &status)) {
690 s32 a = stack[--stack_ptr];
691 s32 b = stack[--stack_ptr];
692 long_tmp = stack[--stack_ptr];
693 count = stack[stack_ptr - 1];
695 if ((count < 1) || (count > 32))
698 long_tmp &= ((-1L) >> (32 - count));
700 stack[stack_ptr - 1] =
701 ((a & long_tmp) == (b & long_tmp))
709 * ...stack 0 is integer value
711 if (!altera_check_stack(stack_ptr, 1, &status))
713 sprintf(&msg_buff[strlen(msg_buff)],
714 "%ld", stack[--stack_ptr]);
719 printk(msg_buff, "\n");
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];
733 put_unaligned_le32(long_tmp, &charbuf[0]);
734 status = altera_drscan(astate, count, charbuf, 0);
738 * DRSCAN short with capture
739 * ...stack 0 is scan data
740 * ...stack 1 is count
742 if (!altera_check_stack(stack_ptr, 2, &status))
744 long_tmp = stack[--stack_ptr];
745 count = stack[stack_ptr - 1];
746 put_unaligned_le32(long_tmp, &charbuf[0]);
747 status = altera_swap_dr(astate, count, charbuf,
749 stack[stack_ptr - 1] = get_unaligned_le32(&charbuf[0]);
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];
761 put_unaligned_le32(long_tmp, &charbuf[0]);
762 status = altera_irscan(astate, count, charbuf, 0);
766 * IRSCAN short with capture
767 * ...stack 0 is scan data
768 * ...stack 1 is count
770 if (!altera_check_stack(stack_ptr, 2, &status))
772 long_tmp = stack[--stack_ptr];
773 count = stack[stack_ptr - 1];
774 put_unaligned_le32(long_tmp, &charbuf[0]);
775 status = altera_swap_ir(astate, count, charbuf,
777 stack[stack_ptr - 1] = get_unaligned_le32(&charbuf[0]);
780 if (!altera_check_stack(stack_ptr, 1, &status))
782 count = stack[--stack_ptr];
783 status = altera_set_dr_pre(&astate->js, count, 0, NULL);
787 * DRPRE with literal data
788 * ...stack 0 is count
789 * ...stack 1 is literal data
791 if (!altera_check_stack(stack_ptr, 2, &status))
793 count = stack[--stack_ptr];
794 long_tmp = stack[--stack_ptr];
795 put_unaligned_le32(long_tmp, &charbuf[0]);
796 status = altera_set_dr_pre(&astate->js, count, 0,
802 * ...stack 0 is count
804 if (altera_check_stack(stack_ptr, 1, &status)) {
805 count = stack[--stack_ptr];
806 status = altera_set_dr_post(&astate->js, count,
812 * DRPOST with literal data
813 * ...stack 0 is count
814 * ...stack 1 is literal data
816 if (!altera_check_stack(stack_ptr, 2, &status))
818 count = stack[--stack_ptr];
819 long_tmp = stack[--stack_ptr];
820 put_unaligned_le32(long_tmp, &charbuf[0]);
821 status = altera_set_dr_post(&astate->js, count, 0,
825 if (altera_check_stack(stack_ptr, 1, &status)) {
826 count = stack[--stack_ptr];
827 status = altera_set_ir_pre(&astate->js, count,
833 * IRPRE with literal data
834 * ...stack 0 is count
835 * ...stack 1 is literal data
837 if (altera_check_stack(stack_ptr, 2, &status)) {
838 count = stack[--stack_ptr];
839 long_tmp = stack[--stack_ptr];
840 put_unaligned_le32(long_tmp, &charbuf[0]);
841 status = altera_set_ir_pre(&astate->js, count,
848 * ...stack 0 is count
850 if (altera_check_stack(stack_ptr, 1, &status)) {
851 count = stack[--stack_ptr];
852 status = altera_set_ir_post(&astate->js, count,
858 * IRPOST with literal data
859 * ...stack 0 is count
860 * ...stack 1 is literal data
862 if (!altera_check_stack(stack_ptr, 2, &status))
864 count = stack[--stack_ptr];
865 long_tmp = stack[--stack_ptr];
866 put_unaligned_le32(long_tmp, &charbuf[0]);
867 status = altera_set_ir_post(&astate->js, count, 0,
871 if (altera_check_stack(stack_ptr, 1, &status)) {
873 count = strlen(msg_buff);
874 ch = (char) stack[--stack_ptr];
875 if ((ch < 1) || (ch > 127)) {
877 * character code out of range
878 * instead of flagging an error,
879 * force the value to 127
883 msg_buff[count] = ch;
884 msg_buff[count + 1] = '\0';
888 if (altera_check_stack(stack_ptr, 1, &status))
889 *exit_code = stack[--stack_ptr];
894 if (!altera_check_stack(stack_ptr, 2, &status))
897 stack[stack_ptr - 1] =
898 (stack[stack_ptr - 1] == stack[stack_ptr]) ?
902 if (altera_check_stack(stack_ptr, 1, &status))
907 if (!altera_check_stack(stack_ptr, 1, &status))
909 if (stack[stack_ptr - 1] < 0)
910 stack[stack_ptr - 1] = 0 - stack[stack_ptr - 1];
928 if (altera_check_stack(stack_ptr, 2, &status)) {
929 long_tmp = stack[stack_ptr - 2];
930 stack[stack_ptr - 2] = stack[stack_ptr - 1];
931 stack[stack_ptr - 1] = long_tmp;
936 if (altera_check_stack(stack_ptr, index, &status)) {
937 long_tmp = stack[stack_ptr - index];
938 stack[stack_ptr - index] = stack[stack_ptr - 1];
939 stack[stack_ptr - 1] = long_tmp;
943 if (altera_check_stack(stack_ptr, 2, &status)) {
944 long_tmp = stack[stack_ptr - 2];
945 stack[stack_ptr - 2] = stack[stack_ptr - 1];
946 stack[stack_ptr - 1] = long_tmp;
951 if (altera_check_stack(stack_ptr, index, &status)) {
952 long_tmp = stack[stack_ptr - index];
953 stack[stack_ptr - index] = stack[stack_ptr - 1];
954 stack[stack_ptr - 1] = long_tmp;
959 if (altera_check_stack(stack_ptr, index, &status)) {
960 stack[stack_ptr] = stack[stack_ptr - index];
966 if (altera_check_stack(stack_ptr, index, &status)) {
967 long_tmp = stack[stack_ptr - index];
968 stack[stack_ptr - index] = stack[stack_ptr - 1];
969 stack[stack_ptr - 1] = long_tmp;
973 if (altera_check_stack(stack_ptr, 2, &status)) {
974 long_tmp = stack[stack_ptr - 2];
975 stack[stack_ptr - 2] = stack[stack_ptr - 1];
976 stack[stack_ptr - 1] = long_tmp;
981 if (altera_check_stack(stack_ptr, index, &status)) {
982 stack[stack_ptr] = stack[stack_ptr - index];
988 if (altera_check_stack(stack_ptr, index, &status)) {
989 stack[stack_ptr] = stack[stack_ptr - index];
994 stack[stack_ptr++] = 0;
997 stack[stack_ptr++] = (s32) args[0];
1000 stack[stack_ptr++] = vars[args[0]];
1003 pc = args[0] + code_sect;
1004 if ((pc < code_sect) || (pc >= debug_sect))
1008 stack[stack_ptr++] = pc;
1009 pc = args[0] + code_sect;
1010 if ((pc < code_sect) || (pc >= debug_sect))
1015 * Process FOR / NEXT loop
1016 * ...argument 0 is variable ID
1017 * ...stack 0 is step value
1018 * ...stack 1 is end value
1019 * ...stack 2 is top address
1021 if (altera_check_stack(stack_ptr, 3, &status)) {
1022 s32 step = stack[stack_ptr - 1];
1023 s32 end = stack[stack_ptr - 2];
1024 s32 top = stack[stack_ptr - 3];
1025 s32 iterator = vars[args[0]];
1029 if (iterator <= end)
1031 } else if (iterator >= end)
1037 vars[args[0]] = iterator + step;
1038 pc = top + code_sect;
1039 if ((pc < code_sect) ||
1048 * ...argument 0 is string ID
1050 count = strlen(msg_buff);
1051 strlcpy(&msg_buff[count],
1052 &p[str_table + args[0]],
1053 ALTERA_MESSAGE_LENGTH - count);
1057 * STATE intermediate state
1058 * ...argument 0 is state code
1060 status = altera_goto_jstate(astate, args[0]);
1065 * ...argument 0 is state code
1067 status = altera_goto_jstate(astate, args[0]);
1072 * ...argument 0 is state code
1074 status = altera_set_irstop(&astate->js, args[0]);
1079 * ...argument 0 is state code
1081 status = altera_set_drstop(&astate->js, args[0]);
1086 * Exchange top with Nth stack value
1087 * ...argument 0 is 0-based stack entry
1088 * to swap with top element
1090 index = (args[0]) + 1;
1091 if (altera_check_stack(stack_ptr, index, &status)) {
1092 long_tmp = stack[stack_ptr - index];
1093 stack[stack_ptr - index] = stack[stack_ptr - 1];
1094 stack[stack_ptr - 1] = long_tmp;
1099 * Duplicate Nth stack value
1100 * ...argument 0 is 0-based stack entry to duplicate
1102 index = (args[0]) + 1;
1103 if (altera_check_stack(stack_ptr, index, &status)) {
1104 stack[stack_ptr] = stack[stack_ptr - index];
1110 * Pop stack into scalar variable
1111 * ...argument 0 is variable ID
1112 * ...stack 0 is value
1114 if (altera_check_stack(stack_ptr, 1, &status))
1115 vars[args[0]] = stack[--stack_ptr];
1120 * Pop stack into integer array element
1121 * ...argument 0 is variable ID
1122 * ...stack 0 is array index
1123 * ...stack 1 is value
1125 if (!altera_check_stack(stack_ptr, 2, &status))
1127 variable_id = args[0];
1130 * If variable is read-only,
1131 * convert to writable array
1133 if ((version > 0) &&
1134 ((attrs[variable_id] & 0x9c) == 0x1c)) {
1135 /* Allocate a writable buffer for this array */
1136 count = var_size[variable_id];
1137 long_tmp = vars[variable_id];
1138 longptr_tmp = kzalloc(count * sizeof(long),
1140 vars[variable_id] = (long)longptr_tmp;
1142 if (vars[variable_id] == 0) {
1147 /* copy previous contents into buffer */
1148 for (i = 0; i < count; ++i) {
1150 get_unaligned_be32(&p[long_tmp]);
1151 long_tmp += sizeof(long);
1155 * set bit 7 - buffer was
1156 * dynamically allocated
1158 attrs[variable_id] |= 0x80;
1160 /* clear bit 2 - variable is writable */
1161 attrs[variable_id] &= ~0x04;
1162 attrs[variable_id] |= 0x01;
1166 /* check that variable is a writable integer array */
1167 if ((attrs[variable_id] & 0x1c) != 0x18)
1170 longptr_tmp = (long *)vars[variable_id];
1172 /* pop the array index */
1173 index = stack[--stack_ptr];
1175 /* pop the value and store it into the array */
1176 longptr_tmp[index] = stack[--stack_ptr];
1182 * Pop stack into Boolean array
1183 * ...argument 0 is variable ID
1184 * ...stack 0 is count
1185 * ...stack 1 is array index
1186 * ...stack 2 is value
1188 if (!altera_check_stack(stack_ptr, 3, &status))
1190 variable_id = args[0];
1193 * If variable is read-only,
1194 * convert to writable array
1196 if ((version > 0) &&
1197 ((attrs[variable_id] & 0x9c) == 0x0c)) {
1198 /* Allocate a writable buffer for this array */
1200 (var_size[variable_id] + 7L) >> 3L;
1201 charptr_tmp2 = (u8 *)vars[variable_id];
1203 kzalloc(long_tmp, GFP_KERNEL);
1204 vars[variable_id] = (long)charptr_tmp;
1206 if (vars[variable_id] == 0) {
1211 /* zero the buffer */
1213 long_idx < long_tmp;
1215 charptr_tmp[long_idx] = 0;
1218 /* copy previous contents into buffer */
1220 long_idx < var_size[variable_id];
1222 long_idx2 = long_idx;
1224 if (charptr_tmp2[long_idx2 >> 3] &
1225 (1 << (long_idx2 & 7))) {
1226 charptr_tmp[long_idx >> 3] |=
1227 (1 << (long_idx & 7));
1232 * set bit 7 - buffer was
1233 * dynamically allocated
1235 attrs[variable_id] |= 0x80;
1237 /* clear bit 2 - variable is writable */
1238 attrs[variable_id] &= ~0x04;
1239 attrs[variable_id] |= 0x01;
1244 * check that variable is
1245 * a writable Boolean array
1247 if ((attrs[variable_id] & 0x1c) != 0x08) {
1252 charptr_tmp = (u8 *)vars[variable_id];
1254 /* pop the count (number of bits to copy) */
1255 long_count = stack[--stack_ptr];
1257 /* pop the array index */
1258 long_idx = stack[--stack_ptr];
1264 * stack 0 = array right index
1265 * stack 1 = array left index
1268 if (long_idx > long_count) {
1270 long_tmp = long_count;
1271 long_count = 1 + long_idx -
1273 long_idx = long_tmp;
1275 /* reverse POPA is not supported */
1279 long_count = 1 + long_count -
1285 long_tmp = stack[--stack_ptr];
1287 if (long_count < 1) {
1292 for (i = 0; i < long_count; ++i) {
1293 if (long_tmp & (1L << (s32) i))
1294 charptr_tmp[long_idx >> 3L] |=
1295 (1L << (long_idx & 7L));
1297 charptr_tmp[long_idx >> 3L] &=
1298 ~(1L << (long_idx & 7L));
1306 * Pop stack and branch if zero
1307 * ...argument 0 is address
1308 * ...stack 0 is condition value
1310 if (altera_check_stack(stack_ptr, 1, &status)) {
1311 if (stack[--stack_ptr] == 0) {
1312 pc = args[0] + code_sect;
1313 if ((pc < code_sect) ||
1324 * ...argument 0 is scan data variable ID
1325 * ...stack 0 is array index
1326 * ...stack 1 is count
1328 if (!altera_check_stack(stack_ptr, 2, &status))
1330 long_idx = stack[--stack_ptr];
1331 long_count = stack[--stack_ptr];
1335 * stack 0 = array right index
1336 * stack 1 = array left index
1339 long_tmp = long_count;
1340 long_count = stack[--stack_ptr];
1342 if (long_idx > long_tmp) {
1344 long_idx = long_tmp;
1348 charptr_tmp = (u8 *)vars[args[0]];
1353 * and reverse the data order
1355 charptr_tmp2 = charptr_tmp;
1356 charptr_tmp = kzalloc((long_count >> 3) + 1,
1358 if (charptr_tmp == NULL) {
1363 long_tmp = long_idx + long_count - 1;
1365 while (long_idx2 < long_count) {
1366 if (charptr_tmp2[long_tmp >> 3] &
1367 (1 << (long_tmp & 7)))
1368 charptr_tmp[long_idx2 >> 3] |=
1369 (1 << (long_idx2 & 7));
1371 charptr_tmp[long_idx2 >> 3] &=
1372 ~(1 << (long_idx2 & 7));
1379 if (opcode == 0x51) /* DS */
1380 status = altera_drscan(astate, long_count,
1381 charptr_tmp, long_idx);
1383 status = altera_irscan(astate, long_count,
1384 charptr_tmp, long_idx);
1392 * DRPRE with array data
1393 * ...argument 0 is variable ID
1394 * ...stack 0 is array index
1395 * ...stack 1 is count
1397 if (!altera_check_stack(stack_ptr, 2, &status))
1399 index = stack[--stack_ptr];
1400 count = stack[--stack_ptr];
1404 * stack 0 = array right index
1405 * stack 1 = array left index
1407 count = 1 + count - index;
1409 charptr_tmp = (u8 *)vars[args[0]];
1410 status = altera_set_dr_pre(&astate->js, count, index,
1415 * DRPOST with array data
1416 * ...argument 0 is variable ID
1417 * ...stack 0 is array index
1418 * ...stack 1 is count
1420 if (!altera_check_stack(stack_ptr, 2, &status))
1422 index = stack[--stack_ptr];
1423 count = stack[--stack_ptr];
1427 * stack 0 = array right index
1428 * stack 1 = array left index
1430 count = 1 + count - index;
1432 charptr_tmp = (u8 *)vars[args[0]];
1433 status = altera_set_dr_post(&astate->js, count, index,
1438 * IRPRE with array data
1439 * ...argument 0 is variable ID
1440 * ...stack 0 is array index
1441 * ...stack 1 is count
1443 if (!altera_check_stack(stack_ptr, 2, &status))
1445 index = stack[--stack_ptr];
1446 count = stack[--stack_ptr];
1450 * stack 0 = array right index
1451 * stack 1 = array left index
1453 count = 1 + count - index;
1455 charptr_tmp = (u8 *)vars[args[0]];
1456 status = altera_set_ir_pre(&astate->js, count, index,
1462 * IRPOST with array data
1463 * ...argument 0 is variable ID
1464 * ...stack 0 is array index
1465 * ...stack 1 is count
1467 if (!altera_check_stack(stack_ptr, 2, &status))
1469 index = stack[--stack_ptr];
1470 count = stack[--stack_ptr];
1474 * stack 0 = array right index
1475 * stack 1 = array left index
1477 count = 1 + count - index;
1479 charptr_tmp = (u8 *)vars[args[0]];
1480 status = altera_set_ir_post(&astate->js, count, index,
1487 * ...argument 0 is string ID
1488 * ...stack 0 is integer expression
1490 if (altera_check_stack(stack_ptr, 1, &status)) {
1491 name = &p[str_table + args[0]];
1492 long_tmp = stack[--stack_ptr];
1493 altera_export_int(name, long_tmp);
1498 * Push integer array element
1499 * ...argument 0 is variable ID
1500 * ...stack 0 is array index
1502 if (!altera_check_stack(stack_ptr, 1, &status))
1504 variable_id = args[0];
1505 index = stack[stack_ptr - 1];
1507 /* check variable type */
1508 if ((attrs[variable_id] & 0x1f) == 0x19) {
1509 /* writable integer array */
1510 longptr_tmp = (long *)vars[variable_id];
1511 stack[stack_ptr - 1] = longptr_tmp[index];
1512 } else if ((attrs[variable_id] & 0x1f) == 0x1c) {
1513 /* read-only integer array */
1514 long_tmp = vars[variable_id] +
1515 (index * sizeof(long));
1516 stack[stack_ptr - 1] =
1517 get_unaligned_be32(&p[long_tmp]);
1524 * Push Boolean array
1525 * ...argument 0 is variable ID
1526 * ...stack 0 is count
1527 * ...stack 1 is array index
1529 if (!altera_check_stack(stack_ptr, 2, &status))
1531 variable_id = args[0];
1533 /* check that variable is a Boolean array */
1534 if ((attrs[variable_id] & 0x18) != 0x08) {
1539 charptr_tmp = (u8 *)vars[variable_id];
1541 /* pop the count (number of bits to copy) */
1542 count = stack[--stack_ptr];
1544 /* pop the array index */
1545 index = stack[stack_ptr - 1];
1549 * stack 0 = array right index
1550 * stack 1 = array left index
1552 count = 1 + count - index;
1554 if ((count < 1) || (count > 32)) {
1561 for (i = 0; i < count; ++i)
1562 if (charptr_tmp[(i + index) >> 3] &
1563 (1 << ((i + index) & 7)))
1564 long_tmp |= (1L << i);
1566 stack[stack_ptr - 1] = long_tmp;
1571 * Dynamically change size of array
1572 * ...argument 0 is variable ID
1573 * ...stack 0 is new size
1575 if (!altera_check_stack(stack_ptr, 1, &status))
1577 variable_id = args[0];
1578 long_tmp = stack[--stack_ptr];
1580 if (long_tmp > var_size[variable_id]) {
1581 var_size[variable_id] = long_tmp;
1583 if (attrs[variable_id] & 0x10)
1584 /* allocate integer array */
1585 long_tmp *= sizeof(long);
1587 /* allocate Boolean array */
1588 long_tmp = (long_tmp + 7) >> 3;
1591 * If the buffer was previously allocated,
1594 if (attrs[variable_id] & 0x80) {
1595 kfree((void *)vars[variable_id]);
1596 vars[variable_id] = 0;
1600 * Allocate a new buffer
1601 * of the requested size
1603 vars[variable_id] = (long)
1604 kzalloc(long_tmp, GFP_KERNEL);
1606 if (vars[variable_id] == 0) {
1612 * Set the attribute bit to indicate that
1613 * this buffer was dynamically allocated and
1614 * should be freed later
1616 attrs[variable_id] |= 0x80;
1618 /* zero out memory */
1619 count = ((var_size[variable_id] + 7L) /
1621 charptr_tmp = (u8 *)(vars[variable_id]);
1622 for (index = 0; index < count; ++index)
1623 charptr_tmp[index] = 0;
1630 * Export Boolean array
1631 * ...argument 0 is string ID
1632 * ...stack 0 is variable ID
1633 * ...stack 1 is array right index
1634 * ...stack 2 is array left index
1636 if (!altera_check_stack(stack_ptr, 3, &status))
1639 /* EXPV is not supported in JBC 1.0 */
1643 name = &p[str_table + args[0]];
1644 variable_id = stack[--stack_ptr];
1645 long_idx = stack[--stack_ptr];/* right indx */
1646 long_idx2 = stack[--stack_ptr];/* left indx */
1648 if (long_idx > long_idx2) {
1649 /* reverse indices not supported */
1654 long_count = 1 + long_idx2 - long_idx;
1656 charptr_tmp = (u8 *)vars[variable_id];
1657 charptr_tmp2 = NULL;
1659 if ((long_idx & 7L) != 0) {
1662 kzalloc(((long_count + 7L) / 8L),
1664 if (charptr_tmp2 == NULL) {
1669 for (i = 0; i < long_count; ++i) {
1670 if (charptr_tmp[k >> 3] &
1672 charptr_tmp2[i >> 3] |=
1675 charptr_tmp2[i >> 3] &=
1680 charptr_tmp = charptr_tmp2;
1682 } else if (long_idx != 0)
1683 charptr_tmp = &charptr_tmp[long_idx >> 3];
1685 altera_export_bool_array(name, charptr_tmp,
1688 /* free allocated buffer */
1689 if ((long_idx & 7L) != 0)
1690 kfree(charptr_tmp2);
1696 * ...argument 0 is dest ID
1697 * ...argument 1 is source ID
1698 * ...stack 0 is count
1699 * ...stack 1 is dest index
1700 * ...stack 2 is source index
1708 int src_reverse = 0;
1709 int dest_reverse = 0;
1711 if (!altera_check_stack(stack_ptr, 3, &status))
1714 copy_count = stack[--stack_ptr];
1715 copy_index = stack[--stack_ptr];
1716 copy_index2 = stack[--stack_ptr];
1721 * stack 0 = source right index
1722 * stack 1 = source left index
1723 * stack 2 = destination right index
1724 * stack 3 = destination left index
1726 destleft = stack[--stack_ptr];
1728 if (copy_count > copy_index) {
1731 src_count = 1 + copy_count - copy_index;
1732 /* copy_index = source start index */
1734 src_count = 1 + copy_index - copy_count;
1735 /* source start index */
1736 copy_index = copy_count;
1739 if (copy_index2 > destleft) {
1742 dest_count = 1 + copy_index2 - destleft;
1743 /* destination start index */
1744 copy_index2 = destleft;
1746 dest_count = 1 + destleft - copy_index2;
1748 copy_count = (src_count < dest_count) ?
1749 src_count : dest_count;
1751 if ((src_reverse || dest_reverse) &&
1752 (src_count != dest_count))
1754 * If either the source or destination
1755 * is reversed, we can't tolerate
1756 * a length mismatch, because we
1757 * "left justify" arrays when copying.
1758 * This won't work correctly
1759 * with reversed arrays.
1767 index2 = copy_index2;
1770 * If destination is a read-only array,
1771 * allocate a buffer and convert it to a writable array
1773 variable_id = args[1];
1774 if ((version > 0) &&
1775 ((attrs[variable_id] & 0x9c) == 0x0c)) {
1776 /* Allocate a writable buffer for this array */
1778 (var_size[variable_id] + 7L) >> 3L;
1779 charptr_tmp2 = (u8 *)vars[variable_id];
1781 kzalloc(long_tmp, GFP_KERNEL);
1782 vars[variable_id] = (long)charptr_tmp;
1784 if (vars[variable_id] == 0) {
1789 /* zero the buffer */
1790 for (long_idx = 0L; long_idx < long_tmp;
1792 charptr_tmp[long_idx] = 0;
1794 /* copy previous contents into buffer */
1796 long_idx < var_size[variable_id];
1798 long_idx2 = long_idx;
1800 if (charptr_tmp2[long_idx2 >> 3] &
1801 (1 << (long_idx2 & 7)))
1802 charptr_tmp[long_idx >> 3] |=
1803 (1 << (long_idx & 7));
1808 set bit 7 - buffer was dynamically allocated */
1809 attrs[variable_id] |= 0x80;
1811 /* clear bit 2 - variable is writable */
1812 attrs[variable_id] &= ~0x04;
1813 attrs[variable_id] |= 0x01;
1816 charptr_tmp = (u8 *)vars[args[1]];
1817 charptr_tmp2 = (u8 *)vars[args[0]];
1819 /* check if destination is a writable Boolean array */
1820 if ((attrs[args[1]] & 0x1c) != 0x08) {
1831 index2 += (count - 1);
1833 for (i = 0; i < count; ++i) {
1834 if (charptr_tmp2[index >> 3] &
1836 charptr_tmp[index2 >> 3] |=
1837 (1 << (index2 & 7));
1839 charptr_tmp[index2 >> 3] &=
1840 ~(1 << (index2 & 7));
1854 * DRSCAN with capture
1855 * IRSCAN with capture
1856 * ...argument 0 is scan data variable ID
1857 * ...argument 1 is capture variable ID
1858 * ...stack 0 is capture index
1859 * ...stack 1 is scan data index
1860 * ...stack 2 is count
1862 s32 scan_right, scan_left;
1863 s32 capture_count = 0;
1868 if (!altera_check_stack(stack_ptr, 3, &status))
1871 capture_index = stack[--stack_ptr];
1872 scan_index = stack[--stack_ptr];
1876 * stack 0 = capture right index
1877 * stack 1 = capture left index
1878 * stack 2 = scan right index
1879 * stack 3 = scan left index
1882 scan_right = stack[--stack_ptr];
1883 scan_left = stack[--stack_ptr];
1884 capture_count = 1 + scan_index - capture_index;
1885 scan_count = 1 + scan_left - scan_right;
1886 scan_index = scan_right;
1889 long_count = stack[--stack_ptr];
1891 * If capture array is read-only, allocate a buffer
1892 * and convert it to a writable array
1894 variable_id = args[1];
1895 if ((version > 0) &&
1896 ((attrs[variable_id] & 0x9c) == 0x0c)) {
1897 /* Allocate a writable buffer for this array */
1899 (var_size[variable_id] + 7L) >> 3L;
1900 charptr_tmp2 = (u8 *)vars[variable_id];
1902 kzalloc(long_tmp, GFP_KERNEL);
1903 vars[variable_id] = (long)charptr_tmp;
1905 if (vars[variable_id] == 0) {
1910 /* zero the buffer */
1911 for (long_idx = 0L; long_idx < long_tmp;
1913 charptr_tmp[long_idx] = 0;
1915 /* copy previous contents into buffer */
1917 long_idx < var_size[variable_id];
1919 long_idx2 = long_idx;
1921 if (charptr_tmp2[long_idx2 >> 3] &
1922 (1 << (long_idx2 & 7)))
1923 charptr_tmp[long_idx >> 3] |=
1924 (1 << (long_idx & 7));
1929 * set bit 7 - buffer was
1930 * dynamically allocated
1932 attrs[variable_id] |= 0x80;
1934 /* clear bit 2 - variable is writable */
1935 attrs[variable_id] &= ~0x04;
1936 attrs[variable_id] |= 0x01;
1940 charptr_tmp = (u8 *)vars[args[0]];
1941 charptr_tmp2 = (u8 *)vars[args[1]];
1943 if ((version > 0) &&
1944 ((long_count > capture_count) ||
1945 (long_count > scan_count))) {
1951 * check that capture array
1952 * is a writable Boolean array
1954 if ((attrs[args[1]] & 0x1c) != 0x08) {
1960 if (opcode == 0x82) /* DSC */
1961 status = altera_swap_dr(astate,
1968 status = altera_swap_ir(astate,
1982 * ...argument 0 is wait state
1983 * ...argument 1 is end state
1984 * ...stack 0 is cycles
1985 * ...stack 1 is microseconds
1987 if (!altera_check_stack(stack_ptr, 2, &status))
1989 long_tmp = stack[--stack_ptr];
1992 status = altera_wait_cycles(astate, long_tmp,
1995 long_tmp = stack[--stack_ptr];
1997 if ((status == 0) && (long_tmp != 0L))
1998 status = altera_wait_msecs(astate,
2002 if ((status == 0) && (args[1] != args[0]))
2003 status = altera_goto_jstate(astate,
2007 --stack_ptr; /* throw away MAX cycles */
2008 --stack_ptr; /* throw away MAX microseconds */
2014 * ...argument 0 is source 1 ID
2015 * ...argument 1 is source 2 ID
2016 * ...argument 2 is mask ID
2017 * ...stack 0 is source 1 index
2018 * ...stack 1 is source 2 index
2019 * ...stack 2 is mask index
2020 * ...stack 3 is count
2023 u8 *source1 = (u8 *)vars[args[0]];
2024 u8 *source2 = (u8 *)vars[args[1]];
2025 u8 *mask = (u8 *)vars[args[2]];
2030 if (!altera_check_stack(stack_ptr, 4, &status))
2033 index1 = stack[--stack_ptr];
2034 index2 = stack[--stack_ptr];
2035 mask_index = stack[--stack_ptr];
2036 long_count = stack[--stack_ptr];
2040 * stack 0 = source 1 right index
2041 * stack 1 = source 1 left index
2042 * stack 2 = source 2 right index
2043 * stack 3 = source 2 left index
2044 * stack 4 = mask right index
2045 * stack 5 = mask left index
2047 s32 mask_right = stack[--stack_ptr];
2048 s32 mask_left = stack[--stack_ptr];
2049 /* source 1 count */
2050 a = 1 + index2 - index1;
2051 /* source 2 count */
2052 b = 1 + long_count - mask_index;
2053 a = (a < b) ? a : b;
2055 b = 1 + mask_left - mask_right;
2056 a = (a < b) ? a : b;
2057 /* source 2 start index */
2058 index2 = mask_index;
2059 /* mask start index */
2060 mask_index = mask_right;
2071 for (i = 0; i < count; ++i) {
2072 if (mask[mask_index >> 3] &
2073 (1 << (mask_index & 7))) {
2074 a = source1[index1 >> 3] &
2077 b = source2[index2 >> 3] &
2081 if (a != b) /* failure */
2090 stack[stack_ptr++] = long_tmp;
2095 /* Unrecognized opcode -- ERROR! */
2103 if ((stack_ptr < 0) || (stack_ptr >= ALTERA_STACK_SIZE))
2104 status = -EOVERFLOW;
2108 *error_address = (s32)(opcode_address - code_sect);
2112 altera_free_buffers(astate);
2114 /* Free all dynamically allocated arrays */
2115 if ((attrs != NULL) && (vars != NULL))
2116 for (i = 0; i < sym_count; ++i)
2117 if (attrs[i] & 0x80)
2118 kfree((void *)vars[i]);
2123 kfree(proc_attributes);
2128 static int altera_get_note(u8 *p, s32 program_size,
2129 s32 *offset, char *key, char *value, int length)
2131 * Gets key and value of NOTE fields in the JBC file.
2132 * Can be called in two modes: if offset pointer is NULL,
2133 * then the function searches for note fields which match
2134 * the key string provided. If offset is not NULL, then
2135 * the function finds the next note field of any key,
2136 * starting at the offset specified by the offset pointer.
2137 * Returns 0 for success, else appropriate error code
2140 int status = -ENODATA;
2141 u32 note_strings = 0L;
2142 u32 note_table = 0L;
2143 u32 note_count = 0L;
2144 u32 first_word = 0L;
2151 /* Read header information */
2152 if (program_size > 52L) {
2153 first_word = get_unaligned_be32(&p[0]);
2154 version = (first_word & 1L);
2155 delta = version * 8;
2157 note_strings = get_unaligned_be32(&p[8 + delta]);
2158 note_table = get_unaligned_be32(&p[12 + delta]);
2159 note_count = get_unaligned_be32(&p[44 + (2 * delta)]);
2162 if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L))
2165 if (note_count <= 0L)
2168 if (offset == NULL) {
2170 * We will search for the first note with a specific key,
2171 * and return only the value
2173 for (i = 0; (i < note_count) &&
2174 (status != 0); ++i) {
2175 key_ptr = &p[note_strings +
2177 &p[note_table + (8 * i)])];
2178 if ((strnicmp(key, key_ptr, strlen(key_ptr)) == 0) &&
2182 value_ptr = &p[note_strings +
2184 &p[note_table + (8 * i) + 4])];
2187 strlcpy(value, value_ptr, length);
2193 * We will search for the next note, regardless of the key,
2194 * and return both the value and the key
2199 if ((i >= 0) && (i < note_count)) {
2203 strlcpy(key, &p[note_strings +
2205 &p[note_table + (8 * i)])],
2209 strlcpy(value, &p[note_strings +
2211 &p[note_table + (8 * i) + 4])],
2221 static int altera_check_crc(u8 *p, s32 program_size)
2224 u16 local_expected = 0,
2230 u32 crc_section = 0L;
2231 u32 first_word = 0L;
2235 if (program_size > 52L) {
2236 first_word = get_unaligned_be32(&p[0]);
2237 version = (first_word & 1L);
2238 delta = version * 8;
2240 crc_section = get_unaligned_be32(&p[32 + delta]);
2243 if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L))
2246 if (crc_section >= program_size)
2250 local_expected = (u16)get_unaligned_be16(&p[crc_section]);
2252 for (i = 0; i < crc_section; ++i) {
2254 for (bit = 0; bit < 8; bit++) {
2255 feedback = (databyte ^ shift_reg) & 0x01;
2258 shift_reg ^= 0x8408;
2264 local_actual = (u16)~shift_reg;
2266 if (local_expected != local_actual)
2271 if (debug || status) {
2274 printk(KERN_INFO "%s: CRC matched: %04x\n", __func__,
2278 printk(KERN_ERR "%s: CRC mismatch: expected %04x, "
2279 "actual %04x\n", __func__, local_expected,
2283 printk(KERN_ERR "%s: expected CRC not found, "
2284 "actual CRC = %04x\n", __func__,
2288 printk(KERN_ERR "%s: error: format isn't "
2289 "recognized.\n", __func__);
2292 printk(KERN_ERR "%s: CRC function returned error "
2293 "code %d\n", __func__, status);
2301 static int altera_get_file_info(u8 *p,
2303 int *format_version,
2305 int *procedure_count)
2311 if (program_size <= 52L)
2314 first_word = get_unaligned_be32(&p[0]);
2316 if ((first_word == 0x4A414D00L) || (first_word == 0x4A414D01L)) {
2319 version = (first_word & 1L);
2320 *format_version = version + 1;
2323 *action_count = get_unaligned_be32(&p[48]);
2324 *procedure_count = get_unaligned_be32(&p[52]);
2331 static int altera_get_act_info(u8 *p,
2336 struct altera_procinfo **proc_list)
2339 struct altera_procinfo *procptr = NULL;
2340 struct altera_procinfo *tmpptr = NULL;
2341 u32 first_word = 0L;
2342 u32 action_table = 0L;
2343 u32 proc_table = 0L;
2345 u32 note_strings = 0L;
2346 u32 action_count = 0L;
2347 u32 proc_count = 0L;
2348 u32 act_name_id = 0L;
2349 u32 act_desc_id = 0L;
2350 u32 act_proc_id = 0L;
2351 u32 act_proc_name = 0L;
2352 u8 act_proc_attribute = 0;
2354 if (program_size <= 52L)
2356 /* Read header information */
2357 first_word = get_unaligned_be32(&p[0]);
2359 if (first_word != 0x4A414D01L)
2362 action_table = get_unaligned_be32(&p[4]);
2363 proc_table = get_unaligned_be32(&p[8]);
2364 str_table = get_unaligned_be32(&p[12]);
2365 note_strings = get_unaligned_be32(&p[16]);
2366 action_count = get_unaligned_be32(&p[48]);
2367 proc_count = get_unaligned_be32(&p[52]);
2369 if (index >= action_count)
2372 act_name_id = get_unaligned_be32(&p[action_table + (12 * index)]);
2373 act_desc_id = get_unaligned_be32(&p[action_table + (12 * index) + 4]);
2374 act_proc_id = get_unaligned_be32(&p[action_table + (12 * index) + 8]);
2376 *name = &p[str_table + act_name_id];
2378 if (act_desc_id < (note_strings - str_table))
2379 *description = &p[str_table + act_desc_id];
2382 act_proc_name = get_unaligned_be32(
2383 &p[proc_table + (13 * act_proc_id)]);
2384 act_proc_attribute =
2385 (p[proc_table + (13 * act_proc_id) + 8] & 0x03);
2387 procptr = (struct altera_procinfo *)
2388 kzalloc(sizeof(struct altera_procinfo),
2391 if (procptr == NULL)
2394 procptr->name = &p[str_table + act_proc_name];
2395 procptr->attrs = act_proc_attribute;
2396 procptr->next = NULL;
2398 /* add record to end of linked list */
2399 if (*proc_list == NULL)
2400 *proc_list = procptr;
2402 tmpptr = *proc_list;
2403 while (tmpptr->next != NULL)
2404 tmpptr = tmpptr->next;
2405 tmpptr->next = procptr;
2409 act_proc_id = get_unaligned_be32(
2410 &p[proc_table + (13 * act_proc_id) + 4]);
2411 } while ((act_proc_id != 0) && (act_proc_id < proc_count));
2416 int altera_init(struct altera_config *config, const struct firmware *fw)
2418 struct altera_state *astate = NULL;
2419 struct altera_procinfo *proc_list = NULL;
2420 struct altera_procinfo *procptr = NULL;
2423 char *action_name = NULL;
2424 char *description = NULL;
2425 int exec_result = 0;
2427 int format_version = 0;
2428 int action_count = 0;
2429 int procedure_count = 0;
2432 s32 error_address = 0L;
2435 key = kzalloc(33, GFP_KERNEL);
2440 value = kzalloc(257, GFP_KERNEL);
2445 astate = kzalloc(sizeof(struct altera_state), GFP_KERNEL);
2451 astate->config = config;
2452 if (!astate->config->jtag_io) {
2453 dprintk(KERN_INFO "%s: using byteblaster!\n", __func__);
2454 astate->config->jtag_io = netup_jtag_io_lpt;
2457 altera_check_crc((u8 *)fw->data, fw->size);
2460 altera_get_file_info((u8 *)fw->data, fw->size, &format_version,
2461 &action_count, &procedure_count);
2462 printk(KERN_INFO "%s: File format is %s ByteCode format\n",
2463 __func__, (format_version == 2) ? "Jam STAPL" :
2464 "pre-standardized Jam 1.1");
2465 while (altera_get_note((u8 *)fw->data, fw->size,
2466 &offset, key, value, 256) == 0)
2467 printk(KERN_INFO "%s: NOTE \"%s\" = \"%s\"\n",
2468 __func__, key, value);
2471 if (debug && (format_version == 2) && (action_count > 0)) {
2472 printk(KERN_INFO "%s: Actions available:\n", __func__);
2473 for (index = 0; index < action_count; ++index) {
2474 altera_get_act_info((u8 *)fw->data, fw->size,
2475 index, &action_name,
2479 if (description == NULL)
2480 printk(KERN_INFO "%s: %s\n",
2484 printk(KERN_INFO "%s: %s \"%s\"\n",
2489 procptr = proc_list;
2490 while (procptr != NULL) {
2491 if (procptr->attrs != 0)
2492 printk(KERN_INFO "%s: %s (%s)\n",
2495 (procptr->attrs == 1) ?
2496 "optional" : "recommended");
2498 proc_list = procptr->next;
2500 procptr = proc_list;
2504 printk(KERN_INFO "\n");
2507 exec_result = altera_execute(astate, (u8 *)fw->data, fw->size,
2508 &error_address, &exit_code, &format_version);
2511 exec_result = -EREMOTEIO;
2513 if ((format_version == 2) && (exec_result == -EINVAL)) {
2514 if (astate->config->action == NULL)
2515 printk(KERN_ERR "%s: error: no action specified for "
2516 "Jam STAPL file.\nprogram terminated.\n",
2519 printk(KERN_ERR "%s: error: action \"%s\""
2520 " is not supported "
2521 "for this Jam STAPL file.\n"
2522 "Program terminated.\n", __func__,
2523 astate->config->action);
2525 } else if (exec_result)
2526 printk(KERN_ERR "%s: error %d\n", __func__, exec_result);
2536 EXPORT_SYMBOL(altera_init);