1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2009 SoftPLC Corporation *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29 ***************************************************************************/
34 #include <jtag/jtag.h>
37 struct cmd_queue_page
{
40 struct cmd_queue_page
*next
;
43 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
44 static struct cmd_queue_page
*cmd_queue_pages
= NULL
;
46 struct jtag_command
*jtag_command_queue
= NULL
;
47 static struct jtag_command
**next_command_pointer
= &jtag_command_queue
;
49 void jtag_queue_command(struct jtag_command
* cmd
)
51 // this command goes on the end, so ensure the queue terminates
54 struct jtag_command
**last_cmd
= next_command_pointer
;
55 assert(NULL
!= last_cmd
);
56 assert(NULL
== *last_cmd
);
59 // store location where the next command pointer will be stored
60 next_command_pointer
= &cmd
->next
;
63 void* cmd_queue_alloc(size_t size
)
65 struct cmd_queue_page
**p_page
= &cmd_queue_pages
;
71 * We align/round the *SIZE* per below
72 * so that all pointers returned by
73 * this function are reasonably well
76 * If we did not, then an "odd-length" request would cause the
77 * *next* allocation to be at an *odd* address, and because
78 * this function has the same type of api as malloc() - we
79 * must also return pointers that have the same type of
82 * What I do not/have is a reasonable portable means
85 * The solution here, is based on these suggestions.
86 * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
89 union worse_case_align
{
95 #define ALIGN_SIZE (sizeof(union worse_case_align))
97 /* The alignment process. */
98 size
= (size
+ ALIGN_SIZE
-1) & (~(ALIGN_SIZE
-1));
103 while ((*p_page
)->next
)
104 p_page
= &((*p_page
)->next
);
105 if (CMD_QUEUE_PAGE_SIZE
- (*p_page
)->used
< size
)
106 p_page
= &((*p_page
)->next
);
111 *p_page
= malloc(sizeof(struct cmd_queue_page
));
113 (*p_page
)->address
= malloc(CMD_QUEUE_PAGE_SIZE
);
114 (*p_page
)->next
= NULL
;
117 offset
= (*p_page
)->used
;
118 (*p_page
)->used
+= size
;
120 t
= (uint8_t *)((*p_page
)->address
);
124 void cmd_queue_free(void)
126 struct cmd_queue_page
*page
= cmd_queue_pages
;
130 struct cmd_queue_page
*last
= page
;
136 cmd_queue_pages
= NULL
;
139 void jtag_command_queue_reset(void)
143 jtag_command_queue
= NULL
;
144 next_command_pointer
= &jtag_command_queue
;
147 enum scan_type
jtag_scan_type(const struct scan_command
*cmd
)
152 for (i
= 0; i
< cmd
->num_fields
; i
++)
154 if (cmd
->fields
[i
].in_value
)
156 if (cmd
->fields
[i
].out_value
)
163 int jtag_scan_size(const struct scan_command
*cmd
)
168 /* count bits in scan command */
169 for (i
= 0; i
< cmd
->num_fields
; i
++)
171 bit_count
+= cmd
->fields
[i
].num_bits
;
177 int jtag_build_buffer(const struct scan_command
*cmd
, uint8_t **buffer
)
182 bit_count
= jtag_scan_size(cmd
);
183 *buffer
= calloc(1,DIV_ROUND_UP(bit_count
, 8));
187 DEBUG_JTAG_IO("%s num_fields: %i",
188 cmd
->ir_scan
? "IRSCAN" : "DRSCAN",
191 for (i
= 0; i
< cmd
->num_fields
; i
++)
193 if (cmd
->fields
[i
].out_value
)
195 #ifdef _DEBUG_JTAG_IO_
196 char *char_buf
= buf_to_str(cmd
->fields
[i
].out_value
,
197 (cmd
->fields
[i
].num_bits
> DEBUG_JTAG_IOZ
)
199 : cmd
->fields
[i
].num_bits
, 16);
201 LOG_DEBUG("fields[%i].out_value[%i]: 0x%s", i
,
202 cmd
->fields
[i
].num_bits
, char_buf
);
205 buf_set_buf(cmd
->fields
[i
].out_value
, 0, *buffer
,
206 bit_count
, cmd
->fields
[i
].num_bits
);
210 DEBUG_JTAG_IO("fields[%i].out_value[%i]: NULL",
211 i
, cmd
->fields
[i
].num_bits
);
214 bit_count
+= cmd
->fields
[i
].num_bits
;
217 //DEBUG_JTAG_IO("bit_count totalling: %i", bit_count);
222 int jtag_read_buffer(uint8_t *buffer
, const struct scan_command
*cmd
)
228 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
231 for (i
= 0; i
< cmd
->num_fields
; i
++)
233 /* if neither in_value nor in_handler
234 * are specified we don't have to examine this field
236 if (cmd
->fields
[i
].in_value
)
238 int num_bits
= cmd
->fields
[i
].num_bits
;
239 uint8_t *captured
= buf_set_buf(buffer
, bit_count
, malloc(DIV_ROUND_UP(num_bits
, 8)), 0, num_bits
);
241 #ifdef _DEBUG_JTAG_IO_
242 char *char_buf
= buf_to_str(captured
,
243 (num_bits
> DEBUG_JTAG_IOZ
)
247 LOG_DEBUG("fields[%i].in_value[%i]: 0x%s",
248 i
, num_bits
, char_buf
);
252 if (cmd
->fields
[i
].in_value
)
254 buf_cpy(captured
, cmd
->fields
[i
].in_value
, num_bits
);
259 bit_count
+= cmd
->fields
[i
].num_bits
;