1 /***************************************************************************
2 * Copyright (C) 2013 by Andes Technology *
3 * Hsiangkai Wang <hkwang@andestech.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
19 ***************************************************************************/
24 #include <jtag/drivers/libusb_common.h>
25 #include <helper/log.h>
26 #include <helper/time_support.h>
27 #include <target/target.h>
28 #include <jtag/jtag.h>
29 #include <target/nds32_insn.h>
30 #include <target/nds32_reg.h>
34 /* Global USB buffers */
35 static uint8_t usb_in_buffer
[AICE_IN_BUFFER_SIZE
];
36 static uint8_t usb_out_buffer
[AICE_OUT_BUFFER_SIZE
];
37 static uint32_t jtag_clock
;
38 static struct aice_usb_handler_s aice_handler
;
39 /* AICE max retry times. If AICE command timeout, retry it. */
40 static int aice_max_retry_times
= 50;
41 /* Default endian is little endian. */
42 static enum aice_target_endian data_endian
;
44 /* Constants for AICE command format length */
45 static const int32_t AICE_FORMAT_HTDA
= 3;
46 static const int32_t AICE_FORMAT_HTDB
= 6;
47 static const int32_t AICE_FORMAT_HTDC
= 7;
48 static const int32_t AICE_FORMAT_HTDD
= 10;
49 static const int32_t AICE_FORMAT_HTDMA
= 4;
50 static const int32_t AICE_FORMAT_HTDMB
= 8;
51 static const int32_t AICE_FORMAT_HTDMC
= 8;
52 static const int32_t AICE_FORMAT_HTDMD
= 12;
53 static const int32_t AICE_FORMAT_DTHA
= 6;
54 static const int32_t AICE_FORMAT_DTHB
= 2;
55 static const int32_t AICE_FORMAT_DTHMA
= 8;
56 static const int32_t AICE_FORMAT_DTHMB
= 4;
58 /* Constants for AICE command */
59 static const uint8_t AICE_CMD_SCAN_CHAIN
= 0x00;
60 static const uint8_t AICE_CMD_SELECT_TARGET
= 0x01;
61 static const uint8_t AICE_CMD_READ_DIM
= 0x02;
62 static const uint8_t AICE_CMD_READ_EDMSR
= 0x03;
63 static const uint8_t AICE_CMD_READ_DTR
= 0x04;
64 static const uint8_t AICE_CMD_READ_MEM
= 0x05;
65 static const uint8_t AICE_CMD_READ_MISC
= 0x06;
66 static const uint8_t AICE_CMD_FASTREAD_MEM
= 0x07;
67 static const uint8_t AICE_CMD_WRITE_DIM
= 0x08;
68 static const uint8_t AICE_CMD_WRITE_EDMSR
= 0x09;
69 static const uint8_t AICE_CMD_WRITE_DTR
= 0x0A;
70 static const uint8_t AICE_CMD_WRITE_MEM
= 0x0B;
71 static const uint8_t AICE_CMD_WRITE_MISC
= 0x0C;
72 static const uint8_t AICE_CMD_FASTWRITE_MEM
= 0x0D;
73 static const uint8_t AICE_CMD_EXECUTE
= 0x0E;
74 static const uint8_t AICE_CMD_READ_MEM_B
= 0x14;
75 static const uint8_t AICE_CMD_READ_MEM_H
= 0x15;
76 static const uint8_t AICE_CMD_T_READ_MISC
= 0x20;
77 static const uint8_t AICE_CMD_T_READ_EDMSR
= 0x21;
78 static const uint8_t AICE_CMD_T_READ_DTR
= 0x22;
79 static const uint8_t AICE_CMD_T_READ_DIM
= 0x23;
80 static const uint8_t AICE_CMD_T_READ_MEM_B
= 0x24;
81 static const uint8_t AICE_CMD_T_READ_MEM_H
= 0x25;
82 static const uint8_t AICE_CMD_T_READ_MEM
= 0x26;
83 static const uint8_t AICE_CMD_T_FASTREAD_MEM
= 0x27;
84 static const uint8_t AICE_CMD_T_WRITE_MISC
= 0x28;
85 static const uint8_t AICE_CMD_T_WRITE_EDMSR
= 0x29;
86 static const uint8_t AICE_CMD_T_WRITE_DTR
= 0x2A;
87 static const uint8_t AICE_CMD_T_WRITE_DIM
= 0x2B;
88 static const uint8_t AICE_CMD_T_WRITE_MEM_B
= 0x2C;
89 static const uint8_t AICE_CMD_T_WRITE_MEM_H
= 0x2D;
90 static const uint8_t AICE_CMD_T_WRITE_MEM
= 0x2E;
91 static const uint8_t AICE_CMD_T_FASTWRITE_MEM
= 0x2F;
92 static const uint8_t AICE_CMD_T_GET_TRACE_STATUS
= 0x36;
93 static const uint8_t AICE_CMD_T_EXECUTE
= 0x3E;
94 static const uint8_t AICE_CMD_AICE_PROGRAM_READ
= 0x40;
95 static const uint8_t AICE_CMD_AICE_PROGRAM_WRITE
= 0x41;
96 static const uint8_t AICE_CMD_AICE_PROGRAM_CONTROL
= 0x42;
97 static const uint8_t AICE_CMD_READ_CTRL
= 0x50;
98 static const uint8_t AICE_CMD_WRITE_CTRL
= 0x51;
99 static const uint8_t AICE_CMD_BATCH_BUFFER_READ
= 0x60;
100 static const uint8_t AICE_CMD_READ_DTR_TO_BUFFER
= 0x61;
101 static const uint8_t AICE_CMD_BATCH_BUFFER_WRITE
= 0x68;
102 static const uint8_t AICE_CMD_WRITE_DTR_FROM_BUFFER
= 0x69;
104 /***************************************************************************/
105 /* AICE commands' pack/unpack functions */
106 static void aice_pack_htda(uint8_t cmd_code
, uint8_t extra_word_length
,
109 usb_out_buffer
[0] = cmd_code
;
110 usb_out_buffer
[1] = extra_word_length
;
111 usb_out_buffer
[2] = (uint8_t)(address
& 0xFF);
114 static void aice_pack_htdc(uint8_t cmd_code
, uint8_t extra_word_length
,
115 uint32_t address
, uint32_t word
, enum aice_target_endian access_endian
)
117 usb_out_buffer
[0] = cmd_code
;
118 usb_out_buffer
[1] = extra_word_length
;
119 usb_out_buffer
[2] = (uint8_t)(address
& 0xFF);
120 if (access_endian
== AICE_BIG_ENDIAN
) {
121 usb_out_buffer
[6] = (uint8_t)((word
>> 24) & 0xFF);
122 usb_out_buffer
[5] = (uint8_t)((word
>> 16) & 0xFF);
123 usb_out_buffer
[4] = (uint8_t)((word
>> 8) & 0xFF);
124 usb_out_buffer
[3] = (uint8_t)(word
& 0xFF);
126 usb_out_buffer
[3] = (uint8_t)((word
>> 24) & 0xFF);
127 usb_out_buffer
[4] = (uint8_t)((word
>> 16) & 0xFF);
128 usb_out_buffer
[5] = (uint8_t)((word
>> 8) & 0xFF);
129 usb_out_buffer
[6] = (uint8_t)(word
& 0xFF);
133 static void aice_pack_htdma(uint8_t cmd_code
, uint8_t target_id
,
134 uint8_t extra_word_length
, uint32_t address
)
136 usb_out_buffer
[0] = cmd_code
;
137 usb_out_buffer
[1] = target_id
;
138 usb_out_buffer
[2] = extra_word_length
;
139 usb_out_buffer
[3] = (uint8_t)(address
& 0xFF);
142 static void aice_pack_htdmb(uint8_t cmd_code
, uint8_t target_id
,
143 uint8_t extra_word_length
, uint32_t address
)
145 usb_out_buffer
[0] = cmd_code
;
146 usb_out_buffer
[1] = target_id
;
147 usb_out_buffer
[2] = extra_word_length
;
148 usb_out_buffer
[3] = 0;
149 usb_out_buffer
[4] = (uint8_t)((address
>> 24) & 0xFF);
150 usb_out_buffer
[5] = (uint8_t)((address
>> 16) & 0xFF);
151 usb_out_buffer
[6] = (uint8_t)((address
>> 8) & 0xFF);
152 usb_out_buffer
[7] = (uint8_t)(address
& 0xFF);
155 static void aice_pack_htdmc(uint8_t cmd_code
, uint8_t target_id
,
156 uint8_t extra_word_length
, uint32_t address
, uint32_t word
,
157 enum aice_target_endian access_endian
)
159 usb_out_buffer
[0] = cmd_code
;
160 usb_out_buffer
[1] = target_id
;
161 usb_out_buffer
[2] = extra_word_length
;
162 usb_out_buffer
[3] = (uint8_t)(address
& 0xFF);
163 if (access_endian
== AICE_BIG_ENDIAN
) {
164 usb_out_buffer
[7] = (uint8_t)((word
>> 24) & 0xFF);
165 usb_out_buffer
[6] = (uint8_t)((word
>> 16) & 0xFF);
166 usb_out_buffer
[5] = (uint8_t)((word
>> 8) & 0xFF);
167 usb_out_buffer
[4] = (uint8_t)(word
& 0xFF);
169 usb_out_buffer
[4] = (uint8_t)((word
>> 24) & 0xFF);
170 usb_out_buffer
[5] = (uint8_t)((word
>> 16) & 0xFF);
171 usb_out_buffer
[6] = (uint8_t)((word
>> 8) & 0xFF);
172 usb_out_buffer
[7] = (uint8_t)(word
& 0xFF);
176 static void aice_pack_htdmc_multiple_data(uint8_t cmd_code
, uint8_t target_id
,
177 uint8_t extra_word_length
, uint32_t address
, uint32_t *word
,
178 uint8_t num_of_words
, enum aice_target_endian access_endian
)
180 usb_out_buffer
[0] = cmd_code
;
181 usb_out_buffer
[1] = target_id
;
182 usb_out_buffer
[2] = extra_word_length
;
183 usb_out_buffer
[3] = (uint8_t)(address
& 0xFF);
186 for (i
= 0 ; i
< num_of_words
; i
++, word
++) {
187 if (access_endian
== AICE_BIG_ENDIAN
) {
188 usb_out_buffer
[7 + i
* 4] = (uint8_t)((*word
>> 24) & 0xFF);
189 usb_out_buffer
[6 + i
* 4] = (uint8_t)((*word
>> 16) & 0xFF);
190 usb_out_buffer
[5 + i
* 4] = (uint8_t)((*word
>> 8) & 0xFF);
191 usb_out_buffer
[4 + i
* 4] = (uint8_t)(*word
& 0xFF);
193 usb_out_buffer
[4 + i
* 4] = (uint8_t)((*word
>> 24) & 0xFF);
194 usb_out_buffer
[5 + i
* 4] = (uint8_t)((*word
>> 16) & 0xFF);
195 usb_out_buffer
[6 + i
* 4] = (uint8_t)((*word
>> 8) & 0xFF);
196 usb_out_buffer
[7 + i
* 4] = (uint8_t)(*word
& 0xFF);
201 static void aice_pack_htdmd(uint8_t cmd_code
, uint8_t target_id
,
202 uint8_t extra_word_length
, uint32_t address
, uint32_t word
,
203 enum aice_target_endian access_endian
)
205 usb_out_buffer
[0] = cmd_code
;
206 usb_out_buffer
[1] = target_id
;
207 usb_out_buffer
[2] = extra_word_length
;
208 usb_out_buffer
[3] = 0;
209 usb_out_buffer
[4] = (uint8_t)((address
>> 24) & 0xFF);
210 usb_out_buffer
[5] = (uint8_t)((address
>> 16) & 0xFF);
211 usb_out_buffer
[6] = (uint8_t)((address
>> 8) & 0xFF);
212 usb_out_buffer
[7] = (uint8_t)(address
& 0xFF);
213 if (access_endian
== AICE_BIG_ENDIAN
) {
214 usb_out_buffer
[11] = (uint8_t)((word
>> 24) & 0xFF);
215 usb_out_buffer
[10] = (uint8_t)((word
>> 16) & 0xFF);
216 usb_out_buffer
[9] = (uint8_t)((word
>> 8) & 0xFF);
217 usb_out_buffer
[8] = (uint8_t)(word
& 0xFF);
219 usb_out_buffer
[8] = (uint8_t)((word
>> 24) & 0xFF);
220 usb_out_buffer
[9] = (uint8_t)((word
>> 16) & 0xFF);
221 usb_out_buffer
[10] = (uint8_t)((word
>> 8) & 0xFF);
222 usb_out_buffer
[11] = (uint8_t)(word
& 0xFF);
226 static void aice_pack_htdmd_multiple_data(uint8_t cmd_code
, uint8_t target_id
,
227 uint8_t extra_word_length
, uint32_t address
, const uint8_t *word
,
228 enum aice_target_endian access_endian
)
230 usb_out_buffer
[0] = cmd_code
;
231 usb_out_buffer
[1] = target_id
;
232 usb_out_buffer
[2] = extra_word_length
;
233 usb_out_buffer
[3] = 0;
234 usb_out_buffer
[4] = (uint8_t)((address
>> 24) & 0xFF);
235 usb_out_buffer
[5] = (uint8_t)((address
>> 16) & 0xFF);
236 usb_out_buffer
[6] = (uint8_t)((address
>> 8) & 0xFF);
237 usb_out_buffer
[7] = (uint8_t)(address
& 0xFF);
240 /* num_of_words may be over 0xFF, so use uint32_t */
241 uint32_t num_of_words
= extra_word_length
+ 1;
243 for (i
= 0 ; i
< num_of_words
; i
++, word
+= 4) {
244 if (access_endian
== AICE_BIG_ENDIAN
) {
245 usb_out_buffer
[11 + i
* 4] = word
[3];
246 usb_out_buffer
[10 + i
* 4] = word
[2];
247 usb_out_buffer
[9 + i
* 4] = word
[1];
248 usb_out_buffer
[8 + i
* 4] = word
[0];
250 usb_out_buffer
[8 + i
* 4] = word
[3];
251 usb_out_buffer
[9 + i
* 4] = word
[2];
252 usb_out_buffer
[10 + i
* 4] = word
[1];
253 usb_out_buffer
[11 + i
* 4] = word
[0];
258 static void aice_unpack_dtha(uint8_t *cmd_ack_code
, uint8_t *extra_word_length
,
259 uint32_t *word
, enum aice_target_endian access_endian
)
261 *cmd_ack_code
= usb_in_buffer
[0];
262 *extra_word_length
= usb_in_buffer
[1];
264 if (access_endian
== AICE_BIG_ENDIAN
) {
265 *word
= (usb_in_buffer
[5] << 24) |
266 (usb_in_buffer
[4] << 16) |
267 (usb_in_buffer
[3] << 8) |
270 *word
= (usb_in_buffer
[2] << 24) |
271 (usb_in_buffer
[3] << 16) |
272 (usb_in_buffer
[4] << 8) |
277 static void aice_unpack_dtha_multiple_data(uint8_t *cmd_ack_code
,
278 uint8_t *extra_word_length
, uint32_t *word
, uint8_t num_of_words
,
279 enum aice_target_endian access_endian
)
281 *cmd_ack_code
= usb_in_buffer
[0];
282 *extra_word_length
= usb_in_buffer
[1];
285 for (i
= 0 ; i
< num_of_words
; i
++, word
++) {
286 if (access_endian
== AICE_BIG_ENDIAN
) {
287 *word
= (usb_in_buffer
[5 + i
* 4] << 24) |
288 (usb_in_buffer
[4 + i
* 4] << 16) |
289 (usb_in_buffer
[3 + i
* 4] << 8) |
290 (usb_in_buffer
[2 + i
* 4]);
292 *word
= (usb_in_buffer
[2 + i
* 4] << 24) |
293 (usb_in_buffer
[3 + i
* 4] << 16) |
294 (usb_in_buffer
[4 + i
* 4] << 8) |
295 (usb_in_buffer
[5 + i
* 4]);
300 static void aice_unpack_dthb(uint8_t *cmd_ack_code
, uint8_t *extra_word_length
)
302 *cmd_ack_code
= usb_in_buffer
[0];
303 *extra_word_length
= usb_in_buffer
[1];
306 static void aice_unpack_dthma(uint8_t *cmd_ack_code
, uint8_t *target_id
,
307 uint8_t *extra_word_length
, uint32_t *word
,
308 enum aice_target_endian access_endian
)
310 *cmd_ack_code
= usb_in_buffer
[0];
311 *target_id
= usb_in_buffer
[1];
312 *extra_word_length
= usb_in_buffer
[2];
313 if (access_endian
== AICE_BIG_ENDIAN
) {
314 *word
= (usb_in_buffer
[7] << 24) |
315 (usb_in_buffer
[6] << 16) |
316 (usb_in_buffer
[5] << 8) |
319 *word
= (usb_in_buffer
[4] << 24) |
320 (usb_in_buffer
[5] << 16) |
321 (usb_in_buffer
[6] << 8) |
326 static void aice_unpack_dthma_multiple_data(uint8_t *cmd_ack_code
,
327 uint8_t *target_id
, uint8_t *extra_word_length
, uint8_t *word
,
328 enum aice_target_endian access_endian
)
330 *cmd_ack_code
= usb_in_buffer
[0];
331 *target_id
= usb_in_buffer
[1];
332 *extra_word_length
= usb_in_buffer
[2];
333 if (access_endian
== AICE_BIG_ENDIAN
) {
334 word
[0] = usb_in_buffer
[4];
335 word
[1] = usb_in_buffer
[5];
336 word
[2] = usb_in_buffer
[6];
337 word
[3] = usb_in_buffer
[7];
339 word
[0] = usb_in_buffer
[7];
340 word
[1] = usb_in_buffer
[6];
341 word
[2] = usb_in_buffer
[5];
342 word
[3] = usb_in_buffer
[4];
347 for (i
= 0; i
< *extra_word_length
; i
++) {
348 if (access_endian
== AICE_BIG_ENDIAN
) {
349 word
[0] = usb_in_buffer
[8 + i
* 4];
350 word
[1] = usb_in_buffer
[9 + i
* 4];
351 word
[2] = usb_in_buffer
[10 + i
* 4];
352 word
[3] = usb_in_buffer
[11 + i
* 4];
354 word
[0] = usb_in_buffer
[11 + i
* 4];
355 word
[1] = usb_in_buffer
[10 + i
* 4];
356 word
[2] = usb_in_buffer
[9 + i
* 4];
357 word
[3] = usb_in_buffer
[8 + i
* 4];
363 static void aice_unpack_dthmb(uint8_t *cmd_ack_code
, uint8_t *target_id
,
364 uint8_t *extra_word_length
)
366 *cmd_ack_code
= usb_in_buffer
[0];
367 *target_id
= usb_in_buffer
[1];
368 *extra_word_length
= usb_in_buffer
[2];
371 /***************************************************************************/
372 /* End of AICE commands' pack/unpack functions */
374 /* calls the given usb_bulk_* function, allowing for the data to
375 * trickle in with some timeouts */
376 static int usb_bulk_with_retries(
377 int (*f
)(jtag_libusb_device_handle
*, int, char *, int, int),
378 jtag_libusb_device_handle
*dev
, int ep
,
379 char *bytes
, int size
, int timeout
)
381 int tries
= 3, count
= 0;
383 while (tries
&& (count
< size
)) {
384 int result
= f(dev
, ep
, bytes
+ count
, size
- count
, timeout
);
387 else if ((-ETIMEDOUT
!= result
) || !--tries
)
393 static int wrap_usb_bulk_write(jtag_libusb_device_handle
*dev
, int ep
,
394 char *buff
, int size
, int timeout
)
396 /* usb_bulk_write() takes const char *buff */
397 return jtag_libusb_bulk_write(dev
, ep
, buff
, size
, timeout
);
400 static inline int usb_bulk_write_ex(jtag_libusb_device_handle
*dev
, int ep
,
401 char *bytes
, int size
, int timeout
)
403 return usb_bulk_with_retries(&wrap_usb_bulk_write
,
404 dev
, ep
, bytes
, size
, timeout
);
407 static inline int usb_bulk_read_ex(jtag_libusb_device_handle
*dev
, int ep
,
408 char *bytes
, int size
, int timeout
)
410 return usb_bulk_with_retries(&jtag_libusb_bulk_read
,
411 dev
, ep
, bytes
, size
, timeout
);
414 /* Write data from out_buffer to USB. */
415 static int aice_usb_write(uint8_t *out_buffer
, int out_length
)
419 if (out_length
> AICE_OUT_BUFFER_SIZE
) {
420 LOG_ERROR("aice_write illegal out_length=%i (max=%i)",
421 out_length
, AICE_OUT_BUFFER_SIZE
);
425 result
= usb_bulk_write_ex(aice_handler
.usb_handle
, aice_handler
.usb_write_ep
,
426 (char *)out_buffer
, out_length
, AICE_USB_TIMEOUT
);
428 DEBUG_JTAG_IO("aice_usb_write, out_length = %i, result = %i",
434 /* Read data from USB into in_buffer. */
435 static int aice_usb_read(uint8_t *in_buffer
, int expected_size
)
437 int32_t result
= usb_bulk_read_ex(aice_handler
.usb_handle
, aice_handler
.usb_read_ep
,
438 (char *)in_buffer
, expected_size
, AICE_USB_TIMEOUT
);
440 DEBUG_JTAG_IO("aice_usb_read, result = %" PRId32
, result
);
445 static uint8_t usb_out_packets_buffer
[AICE_OUT_PACKETS_BUFFER_SIZE
];
446 static uint8_t usb_in_packets_buffer
[AICE_IN_PACKETS_BUFFER_SIZE
];
447 static uint32_t usb_out_packets_buffer_length
;
448 static uint32_t usb_in_packets_buffer_length
;
449 static enum aice_command_mode aice_command_mode
;
451 static int aice_batch_buffer_write(uint8_t buf_index
, const uint8_t *word
,
452 uint32_t num_of_words
);
454 static int aice_usb_packet_flush(void)
456 if (usb_out_packets_buffer_length
== 0)
459 if (AICE_COMMAND_MODE_PACK
== aice_command_mode
) {
460 LOG_DEBUG("Flush usb packets (AICE_COMMAND_MODE_PACK)");
462 if (aice_usb_write(usb_out_packets_buffer
,
463 usb_out_packets_buffer_length
) < 0)
466 if (aice_usb_read(usb_in_packets_buffer
,
467 usb_in_packets_buffer_length
) < 0)
470 usb_out_packets_buffer_length
= 0;
471 usb_in_packets_buffer_length
= 0;
473 } else if (AICE_COMMAND_MODE_BATCH
== aice_command_mode
) {
474 LOG_DEBUG("Flush usb packets (AICE_COMMAND_MODE_BATCH)");
476 /* use BATCH_BUFFER_WRITE to fill command-batch-buffer */
477 if (aice_batch_buffer_write(AICE_BATCH_COMMAND_BUFFER_0
,
478 usb_out_packets_buffer
,
479 (usb_out_packets_buffer_length
+ 3) / 4) != ERROR_OK
)
482 usb_out_packets_buffer_length
= 0;
483 usb_in_packets_buffer_length
= 0;
485 /* enable BATCH command */
486 aice_command_mode
= AICE_COMMAND_MODE_NORMAL
;
487 if (aice_write_ctrl(AICE_WRITE_CTRL_BATCH_CTRL
, 0x80000000) != ERROR_OK
)
489 aice_command_mode
= AICE_COMMAND_MODE_BATCH
;
491 /* wait 1 second (AICE bug, workaround) */
496 uint32_t batch_status
;
500 aice_read_ctrl(AICE_READ_CTRL_BATCH_STATUS
, &batch_status
);
502 if (batch_status
& 0x1)
504 else if (batch_status
& 0xE)
517 static int aice_usb_packet_append(uint8_t *out_buffer
, int out_length
, int in_length
)
519 uint32_t max_packet_size
= AICE_OUT_PACKETS_BUFFER_SIZE
;
521 if (AICE_COMMAND_MODE_PACK
== aice_command_mode
) {
522 max_packet_size
= AICE_OUT_PACK_COMMAND_SIZE
;
523 } else if (AICE_COMMAND_MODE_BATCH
== aice_command_mode
) {
524 max_packet_size
= AICE_OUT_BATCH_COMMAND_SIZE
;
526 /* AICE_COMMAND_MODE_NORMAL */
527 if (aice_usb_packet_flush() != ERROR_OK
)
531 if (usb_out_packets_buffer_length
+ out_length
> max_packet_size
)
532 if (aice_usb_packet_flush() != ERROR_OK
) {
533 LOG_DEBUG("Flush usb packets failed");
537 LOG_DEBUG("Append usb packets 0x%02x", out_buffer
[0]);
539 memcpy(usb_out_packets_buffer
+ usb_out_packets_buffer_length
, out_buffer
, out_length
);
540 usb_out_packets_buffer_length
+= out_length
;
541 usb_in_packets_buffer_length
+= in_length
;
546 /***************************************************************************/
548 static int aice_reset_box(void)
550 if (aice_write_ctrl(AICE_WRITE_CTRL_CLEAR_TIMEOUT_STATUS
, 0x1) != ERROR_OK
)
553 /* turn off FASTMODE */
555 if (aice_read_ctrl(AICE_READ_CTRL_GET_JTAG_PIN_STATUS
, &pin_status
)
559 if (aice_write_ctrl(AICE_WRITE_CTRL_JTAG_PIN_STATUS
, pin_status
& (~0x2))
566 static int aice_scan_chain(uint32_t *id_codes
, uint8_t *num_of_ids
)
571 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
572 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
))
573 aice_usb_packet_flush();
576 aice_pack_htda(AICE_CMD_SCAN_CHAIN
, 0x0F, 0x0);
578 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDA
);
580 LOG_DEBUG("SCAN_CHAIN, length: 0x0F");
582 /** TODO: modify receive length */
583 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHA
);
584 if (AICE_FORMAT_DTHA
!= result
) {
585 LOG_ERROR("aice_usb_read failed (requested=%" PRIu32
", result=%" PRId32
")",
586 AICE_FORMAT_DTHA
, result
);
590 uint8_t cmd_ack_code
;
591 aice_unpack_dtha_multiple_data(&cmd_ack_code
, num_of_ids
, id_codes
,
592 0x10, AICE_LITTLE_ENDIAN
);
594 if (cmd_ack_code
!= AICE_CMD_SCAN_CHAIN
) {
596 if (retry_times
> aice_max_retry_times
) {
597 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
598 AICE_CMD_SCAN_CHAIN
, cmd_ack_code
);
602 /* clear timeout and retry */
603 if (aice_reset_box() != ERROR_OK
)
610 LOG_DEBUG("SCAN_CHAIN response, # of IDs: %" PRIu8
, *num_of_ids
);
612 if (*num_of_ids
== 0xFF) {
613 LOG_ERROR("No target connected");
615 } else if (*num_of_ids
== AICE_MAX_NUM_CORE
) {
616 LOG_INFO("The ice chain over 16 targets");
626 int aice_read_ctrl(uint32_t address
, uint32_t *data
)
630 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
631 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
))
632 aice_usb_packet_flush();
634 aice_pack_htda(AICE_CMD_READ_CTRL
, 0, address
);
636 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDA
);
638 LOG_DEBUG("READ_CTRL, address: 0x%" PRIx32
, address
);
640 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHA
);
641 if (AICE_FORMAT_DTHA
!= result
) {
642 LOG_ERROR("aice_usb_read failed (requested=%" PRIu32
", result=%" PRId32
")",
643 AICE_FORMAT_DTHA
, result
);
647 uint8_t cmd_ack_code
;
648 uint8_t extra_length
;
649 aice_unpack_dtha(&cmd_ack_code
, &extra_length
, data
, AICE_LITTLE_ENDIAN
);
651 LOG_DEBUG("READ_CTRL response, data: 0x%" PRIx32
, *data
);
653 if (cmd_ack_code
!= AICE_CMD_READ_CTRL
) {
654 LOG_ERROR("aice command error (command=0x%" PRIx32
", response=0x%" PRIx8
")",
655 (uint32_t)AICE_CMD_READ_CTRL
, cmd_ack_code
);
662 int aice_write_ctrl(uint32_t address
, uint32_t data
)
666 if (AICE_COMMAND_MODE_PACK
== aice_command_mode
) {
667 aice_usb_packet_flush();
668 } else if (AICE_COMMAND_MODE_BATCH
== aice_command_mode
) {
669 aice_pack_htdc(AICE_CMD_WRITE_CTRL
, 0, address
, data
, AICE_LITTLE_ENDIAN
);
670 return aice_usb_packet_append(usb_out_buffer
, AICE_FORMAT_HTDC
,
674 aice_pack_htdc(AICE_CMD_WRITE_CTRL
, 0, address
, data
, AICE_LITTLE_ENDIAN
);
676 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDC
);
678 LOG_DEBUG("WRITE_CTRL, address: 0x%" PRIx32
", data: 0x%" PRIx32
, address
, data
);
680 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHB
);
681 if (AICE_FORMAT_DTHB
!= result
) {
682 LOG_ERROR("aice_usb_read failed (requested=%" PRIu32
", result=%" PRId32
")",
683 AICE_FORMAT_DTHB
, result
);
687 uint8_t cmd_ack_code
;
688 uint8_t extra_length
;
689 aice_unpack_dthb(&cmd_ack_code
, &extra_length
);
691 LOG_DEBUG("WRITE_CTRL response");
693 if (cmd_ack_code
!= AICE_CMD_WRITE_CTRL
) {
694 LOG_ERROR("aice command error (command=0x%" PRIx8
", response=0x%" PRIx8
")",
695 AICE_CMD_WRITE_CTRL
, cmd_ack_code
);
702 int aice_read_dtr(uint8_t target_id
, uint32_t *data
)
707 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
708 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
))
709 aice_usb_packet_flush();
712 aice_pack_htdma(AICE_CMD_T_READ_DTR
, target_id
, 0, 0);
714 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMA
);
716 LOG_DEBUG("READ_DTR, COREID: %" PRIu8
, target_id
);
718 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMA
);
719 if (AICE_FORMAT_DTHMA
!= result
) {
720 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
721 AICE_FORMAT_DTHMA
, result
);
725 uint8_t cmd_ack_code
;
726 uint8_t extra_length
;
727 uint8_t res_target_id
;
728 aice_unpack_dthma(&cmd_ack_code
, &res_target_id
, &extra_length
,
729 data
, AICE_LITTLE_ENDIAN
);
731 if (cmd_ack_code
== AICE_CMD_T_READ_DTR
) {
732 LOG_DEBUG("READ_DTR response, data: 0x%" PRIx32
, *data
);
736 if (retry_times
> aice_max_retry_times
) {
737 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
738 AICE_CMD_T_READ_DTR
, cmd_ack_code
);
742 /* clear timeout and retry */
743 if (aice_reset_box() != ERROR_OK
)
753 int aice_read_dtr_to_buffer(uint8_t target_id
, uint32_t buffer_idx
)
758 if (AICE_COMMAND_MODE_PACK
== aice_command_mode
) {
759 aice_usb_packet_flush();
760 } else if (AICE_COMMAND_MODE_BATCH
== aice_command_mode
) {
761 aice_pack_htdma(AICE_CMD_READ_DTR_TO_BUFFER
, target_id
, 0, buffer_idx
);
762 return aice_usb_packet_append(usb_out_buffer
, AICE_FORMAT_HTDMA
,
767 aice_pack_htdma(AICE_CMD_READ_DTR_TO_BUFFER
, target_id
, 0, buffer_idx
);
769 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMA
);
771 LOG_DEBUG("READ_DTR_TO_BUFFER, COREID: %" PRIu8
, target_id
);
773 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
774 if (AICE_FORMAT_DTHMB
!= result
) {
775 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")", AICE_FORMAT_DTHMB
, result
);
779 uint8_t cmd_ack_code
;
780 uint8_t extra_length
;
781 uint8_t res_target_id
;
782 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
784 if (cmd_ack_code
== AICE_CMD_READ_DTR_TO_BUFFER
) {
787 if (retry_times
> aice_max_retry_times
) {
788 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
789 AICE_CMD_READ_DTR_TO_BUFFER
, cmd_ack_code
);
794 /* clear timeout and retry */
795 if (aice_reset_box() != ERROR_OK
)
805 int aice_write_dtr(uint8_t target_id
, uint32_t data
)
810 if (AICE_COMMAND_MODE_PACK
== aice_command_mode
) {
811 aice_usb_packet_flush();
812 } else if (AICE_COMMAND_MODE_BATCH
== aice_command_mode
) {
813 aice_pack_htdmc(AICE_CMD_T_WRITE_DTR
, target_id
, 0, 0, data
, AICE_LITTLE_ENDIAN
);
814 return aice_usb_packet_append(usb_out_buffer
, AICE_FORMAT_HTDMC
,
819 aice_pack_htdmc(AICE_CMD_T_WRITE_DTR
, target_id
, 0, 0, data
, AICE_LITTLE_ENDIAN
);
821 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMC
);
823 LOG_DEBUG("WRITE_DTR, COREID: %" PRIu8
", data: 0x%" PRIx32
, target_id
, data
);
825 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
826 if (AICE_FORMAT_DTHMB
!= result
) {
827 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")", AICE_FORMAT_DTHMB
, result
);
831 uint8_t cmd_ack_code
;
832 uint8_t extra_length
;
833 uint8_t res_target_id
;
834 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
836 if (cmd_ack_code
== AICE_CMD_T_WRITE_DTR
) {
837 LOG_DEBUG("WRITE_DTR response");
840 if (retry_times
> aice_max_retry_times
) {
841 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
842 AICE_CMD_T_WRITE_DTR
, cmd_ack_code
);
847 /* clear timeout and retry */
848 if (aice_reset_box() != ERROR_OK
)
858 int aice_write_dtr_from_buffer(uint8_t target_id
, uint32_t buffer_idx
)
863 if (AICE_COMMAND_MODE_PACK
== aice_command_mode
) {
864 aice_usb_packet_flush();
865 } else if (AICE_COMMAND_MODE_BATCH
== aice_command_mode
) {
866 aice_pack_htdma(AICE_CMD_WRITE_DTR_FROM_BUFFER
, target_id
, 0, buffer_idx
);
867 return aice_usb_packet_append(usb_out_buffer
, AICE_FORMAT_HTDMA
,
872 aice_pack_htdma(AICE_CMD_WRITE_DTR_FROM_BUFFER
, target_id
, 0, buffer_idx
);
874 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMA
);
876 LOG_DEBUG("WRITE_DTR_FROM_BUFFER, COREID: %" PRIu8
"", target_id
);
878 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
879 if (AICE_FORMAT_DTHMB
!= result
) {
880 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")", AICE_FORMAT_DTHMB
, result
);
884 uint8_t cmd_ack_code
;
885 uint8_t extra_length
;
886 uint8_t res_target_id
;
887 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
889 if (cmd_ack_code
== AICE_CMD_WRITE_DTR_FROM_BUFFER
) {
892 if (retry_times
> aice_max_retry_times
) {
893 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
894 AICE_CMD_WRITE_DTR_FROM_BUFFER
, cmd_ack_code
);
899 /* clear timeout and retry */
900 if (aice_reset_box() != ERROR_OK
)
910 int aice_read_misc(uint8_t target_id
, uint32_t address
, uint32_t *data
)
915 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
916 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
))
917 aice_usb_packet_flush();
920 aice_pack_htdma(AICE_CMD_T_READ_MISC
, target_id
, 0, address
);
922 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMA
);
924 LOG_DEBUG("READ_MISC, COREID: %" PRIu8
", address: 0x%" PRIx32
, target_id
, address
);
926 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMA
);
927 if (AICE_FORMAT_DTHMA
!= result
) {
928 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
929 AICE_FORMAT_DTHMA
, result
);
930 return ERROR_AICE_DISCONNECT
;
933 uint8_t cmd_ack_code
;
934 uint8_t extra_length
;
935 uint8_t res_target_id
;
936 aice_unpack_dthma(&cmd_ack_code
, &res_target_id
, &extra_length
,
937 data
, AICE_LITTLE_ENDIAN
);
939 if (cmd_ack_code
== AICE_CMD_T_READ_MISC
) {
940 LOG_DEBUG("READ_MISC response, data: 0x%" PRIx32
, *data
);
943 if (retry_times
> aice_max_retry_times
) {
944 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
945 AICE_CMD_T_READ_MISC
, cmd_ack_code
);
949 /* clear timeout and retry */
950 if (aice_reset_box() != ERROR_OK
)
960 int aice_write_misc(uint8_t target_id
, uint32_t address
, uint32_t data
)
965 if (AICE_COMMAND_MODE_PACK
== aice_command_mode
) {
966 aice_usb_packet_flush();
967 } else if (AICE_COMMAND_MODE_BATCH
== aice_command_mode
) {
968 aice_pack_htdmc(AICE_CMD_T_WRITE_MISC
, target_id
, 0, address
, data
,
970 return aice_usb_packet_append(usb_out_buffer
, AICE_FORMAT_HTDMC
,
975 aice_pack_htdmc(AICE_CMD_T_WRITE_MISC
, target_id
, 0, address
,
976 data
, AICE_LITTLE_ENDIAN
);
978 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMC
);
980 LOG_DEBUG("WRITE_MISC, COREID: %" PRIu8
", address: 0x%" PRIx32
", data: 0x%" PRIx32
,
981 target_id
, address
, data
);
983 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
984 if (AICE_FORMAT_DTHMB
!= result
) {
985 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
986 AICE_FORMAT_DTHMB
, result
);
990 uint8_t cmd_ack_code
;
991 uint8_t extra_length
;
992 uint8_t res_target_id
;
993 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
995 if (cmd_ack_code
== AICE_CMD_T_WRITE_MISC
) {
996 LOG_DEBUG("WRITE_MISC response");
999 if (retry_times
> aice_max_retry_times
) {
1000 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1001 AICE_CMD_T_WRITE_MISC
, cmd_ack_code
);
1006 /* clear timeout and retry */
1007 if (aice_reset_box() != ERROR_OK
)
1017 int aice_read_edmsr(uint8_t target_id
, uint32_t address
, uint32_t *data
)
1020 int retry_times
= 0;
1022 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
1023 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
))
1024 aice_usb_packet_flush();
1027 aice_pack_htdma(AICE_CMD_T_READ_EDMSR
, target_id
, 0, address
);
1029 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMA
);
1031 LOG_DEBUG("READ_EDMSR, COREID: %" PRIu8
", address: 0x%" PRIx32
, target_id
, address
);
1033 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMA
);
1034 if (AICE_FORMAT_DTHMA
!= result
) {
1035 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1036 AICE_FORMAT_DTHMA
, result
);
1040 uint8_t cmd_ack_code
;
1041 uint8_t extra_length
;
1042 uint8_t res_target_id
;
1043 aice_unpack_dthma(&cmd_ack_code
, &res_target_id
, &extra_length
,
1044 data
, AICE_LITTLE_ENDIAN
);
1046 if (cmd_ack_code
== AICE_CMD_T_READ_EDMSR
) {
1047 LOG_DEBUG("READ_EDMSR response, data: 0x%" PRIx32
, *data
);
1050 if (retry_times
> aice_max_retry_times
) {
1051 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1052 AICE_CMD_T_READ_EDMSR
, cmd_ack_code
);
1057 /* clear timeout and retry */
1058 if (aice_reset_box() != ERROR_OK
)
1068 int aice_write_edmsr(uint8_t target_id
, uint32_t address
, uint32_t data
)
1071 int retry_times
= 0;
1073 if (AICE_COMMAND_MODE_PACK
== aice_command_mode
) {
1074 aice_usb_packet_flush();
1075 } else if (AICE_COMMAND_MODE_BATCH
== aice_command_mode
) {
1076 aice_pack_htdmc(AICE_CMD_T_WRITE_EDMSR
, target_id
, 0, address
, data
,
1077 AICE_LITTLE_ENDIAN
);
1078 return aice_usb_packet_append(usb_out_buffer
, AICE_FORMAT_HTDMC
,
1083 aice_pack_htdmc(AICE_CMD_T_WRITE_EDMSR
, target_id
, 0, address
,
1084 data
, AICE_LITTLE_ENDIAN
);
1086 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMC
);
1088 LOG_DEBUG("WRITE_EDMSR, COREID: %" PRIu8
", address: 0x%" PRIx32
", data: 0x%" PRIx32
,
1089 target_id
, address
, data
);
1091 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
1092 if (AICE_FORMAT_DTHMB
!= result
) {
1093 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1094 AICE_FORMAT_DTHMB
, result
);
1098 uint8_t cmd_ack_code
;
1099 uint8_t extra_length
;
1100 uint8_t res_target_id
;
1101 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
1103 if (cmd_ack_code
== AICE_CMD_T_WRITE_EDMSR
) {
1104 LOG_DEBUG("WRITE_EDMSR response");
1107 if (retry_times
> aice_max_retry_times
) {
1108 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1109 AICE_CMD_T_WRITE_EDMSR
, cmd_ack_code
);
1114 /* clear timeout and retry */
1115 if (aice_reset_box() != ERROR_OK
)
1125 static int aice_switch_to_big_endian(uint32_t *word
, uint8_t num_of_words
)
1129 for (uint8_t i
= 0 ; i
< num_of_words
; i
++) {
1130 tmp
= ((word
[i
] >> 24) & 0x000000FF) |
1131 ((word
[i
] >> 8) & 0x0000FF00) |
1132 ((word
[i
] << 8) & 0x00FF0000) |
1133 ((word
[i
] << 24) & 0xFF000000);
1140 static int aice_write_dim(uint8_t target_id
, uint32_t *word
, uint8_t num_of_words
)
1143 uint32_t big_endian_word
[4];
1144 int retry_times
= 0;
1146 /** instruction is big-endian */
1147 memcpy(big_endian_word
, word
, sizeof(big_endian_word
));
1148 aice_switch_to_big_endian(big_endian_word
, num_of_words
);
1150 if (AICE_COMMAND_MODE_PACK
== aice_command_mode
) {
1151 aice_usb_packet_flush();
1152 } else if (AICE_COMMAND_MODE_BATCH
== aice_command_mode
) {
1153 aice_pack_htdmc_multiple_data(AICE_CMD_T_WRITE_DIM
, target_id
,
1154 num_of_words
- 1, 0, big_endian_word
, num_of_words
,
1155 AICE_LITTLE_ENDIAN
);
1156 return aice_usb_packet_append(usb_out_buffer
,
1157 AICE_FORMAT_HTDMC
+ (num_of_words
- 1) * 4,
1162 aice_pack_htdmc_multiple_data(AICE_CMD_T_WRITE_DIM
, target_id
, num_of_words
- 1, 0,
1163 big_endian_word
, num_of_words
, AICE_LITTLE_ENDIAN
);
1165 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMC
+ (num_of_words
- 1) * 4);
1167 LOG_DEBUG("WRITE_DIM, COREID: %" PRIu8
1168 ", data: 0x%08" PRIx32
", 0x%08" PRIx32
", 0x%08" PRIx32
", 0x%08" PRIx32
,
1173 big_endian_word
[3]);
1175 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
1176 if (AICE_FORMAT_DTHMB
!= result
) {
1177 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")", AICE_FORMAT_DTHMB
, result
);
1181 uint8_t cmd_ack_code
;
1182 uint8_t extra_length
;
1183 uint8_t res_target_id
;
1184 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
1187 if (cmd_ack_code
== AICE_CMD_T_WRITE_DIM
) {
1188 LOG_DEBUG("WRITE_DIM response");
1191 if (retry_times
> aice_max_retry_times
) {
1192 LOG_ERROR("aice command timeout (command=0x%" PRIx8
1193 ", response=0x%" PRIx8
")",
1194 AICE_CMD_T_WRITE_DIM
, cmd_ack_code
);
1199 /* clear timeout and retry */
1200 if (aice_reset_box() != ERROR_OK
)
1210 static int aice_do_execute(uint8_t target_id
)
1213 int retry_times
= 0;
1215 if (AICE_COMMAND_MODE_PACK
== aice_command_mode
) {
1216 aice_usb_packet_flush();
1217 } else if (AICE_COMMAND_MODE_BATCH
== aice_command_mode
) {
1218 aice_pack_htdmc(AICE_CMD_T_EXECUTE
, target_id
, 0, 0, 0, AICE_LITTLE_ENDIAN
);
1219 return aice_usb_packet_append(usb_out_buffer
,
1225 aice_pack_htdmc(AICE_CMD_T_EXECUTE
, target_id
, 0, 0, 0, AICE_LITTLE_ENDIAN
);
1227 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMC
);
1229 LOG_DEBUG("EXECUTE, COREID: %" PRIu8
"", target_id
);
1231 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
1232 if (AICE_FORMAT_DTHMB
!= result
) {
1233 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1234 AICE_FORMAT_DTHMB
, result
);
1238 uint8_t cmd_ack_code
;
1239 uint8_t extra_length
;
1240 uint8_t res_target_id
;
1241 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
1243 if (cmd_ack_code
== AICE_CMD_T_EXECUTE
) {
1244 LOG_DEBUG("EXECUTE response");
1247 if (retry_times
> aice_max_retry_times
) {
1248 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1249 AICE_CMD_T_EXECUTE
, cmd_ack_code
);
1254 /* clear timeout and retry */
1255 if (aice_reset_box() != ERROR_OK
)
1265 int aice_write_mem_b(uint8_t target_id
, uint32_t address
, uint32_t data
)
1268 int retry_times
= 0;
1270 LOG_DEBUG("WRITE_MEM_B, COREID: %" PRIu8
", ADDRESS %08" PRIx32
" VALUE %08" PRIx32
,
1275 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
1276 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
)) {
1277 aice_pack_htdmd(AICE_CMD_T_WRITE_MEM_B
, target_id
, 0, address
,
1278 data
& 0x000000FF, data_endian
);
1279 return aice_usb_packet_append(usb_out_buffer
, AICE_FORMAT_HTDMD
,
1283 aice_pack_htdmd(AICE_CMD_T_WRITE_MEM_B
, target_id
, 0,
1284 address
, data
& 0x000000FF, data_endian
);
1285 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMD
);
1287 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
1288 if (AICE_FORMAT_DTHMB
!= result
) {
1289 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
1290 ", result=%" PRId32
")", AICE_FORMAT_DTHMB
, result
);
1294 uint8_t cmd_ack_code
;
1295 uint8_t extra_length
;
1296 uint8_t res_target_id
;
1297 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
1299 if (cmd_ack_code
== AICE_CMD_T_WRITE_MEM_B
) {
1302 if (retry_times
> aice_max_retry_times
) {
1303 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1304 AICE_CMD_T_WRITE_MEM_B
, cmd_ack_code
);
1309 /* clear timeout and retry */
1310 if (aice_reset_box() != ERROR_OK
)
1321 int aice_write_mem_h(uint8_t target_id
, uint32_t address
, uint32_t data
)
1324 int retry_times
= 0;
1326 LOG_DEBUG("WRITE_MEM_H, COREID: %" PRIu8
", ADDRESS %08" PRIx32
" VALUE %08" PRIx32
,
1331 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
1332 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
)) {
1333 aice_pack_htdmd(AICE_CMD_T_WRITE_MEM_H
, target_id
, 0,
1334 (address
>> 1) & 0x7FFFFFFF, data
& 0x0000FFFF, data_endian
);
1335 return aice_usb_packet_append(usb_out_buffer
, AICE_FORMAT_HTDMD
,
1339 aice_pack_htdmd(AICE_CMD_T_WRITE_MEM_H
, target_id
, 0,
1340 (address
>> 1) & 0x7FFFFFFF, data
& 0x0000FFFF, data_endian
);
1341 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMD
);
1343 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
1344 if (AICE_FORMAT_DTHMB
!= result
) {
1345 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1346 AICE_FORMAT_DTHMB
, result
);
1350 uint8_t cmd_ack_code
;
1351 uint8_t extra_length
;
1352 uint8_t res_target_id
;
1353 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
1355 if (cmd_ack_code
== AICE_CMD_T_WRITE_MEM_H
) {
1358 if (retry_times
> aice_max_retry_times
) {
1359 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1360 AICE_CMD_T_WRITE_MEM_H
, cmd_ack_code
);
1365 /* clear timeout and retry */
1366 if (aice_reset_box() != ERROR_OK
)
1377 int aice_write_mem(uint8_t target_id
, uint32_t address
, uint32_t data
)
1380 int retry_times
= 0;
1382 LOG_DEBUG("WRITE_MEM, COREID: %" PRIu8
", ADDRESS %08" PRIx32
" VALUE %08" PRIx32
,
1387 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
1388 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
)) {
1389 aice_pack_htdmd(AICE_CMD_T_WRITE_MEM
, target_id
, 0,
1390 (address
>> 2) & 0x3FFFFFFF, data
, data_endian
);
1391 return aice_usb_packet_append(usb_out_buffer
, AICE_FORMAT_HTDMD
,
1395 aice_pack_htdmd(AICE_CMD_T_WRITE_MEM
, target_id
, 0,
1396 (address
>> 2) & 0x3FFFFFFF, data
, data_endian
);
1397 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMD
);
1399 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
1400 if (AICE_FORMAT_DTHMB
!= result
) {
1401 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1402 AICE_FORMAT_DTHMB
, result
);
1406 uint8_t cmd_ack_code
;
1407 uint8_t extra_length
;
1408 uint8_t res_target_id
;
1409 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
1411 if (cmd_ack_code
== AICE_CMD_T_WRITE_MEM
) {
1414 if (retry_times
> aice_max_retry_times
) {
1415 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1416 AICE_CMD_T_WRITE_MEM
, cmd_ack_code
);
1421 /* clear timeout and retry */
1422 if (aice_reset_box() != ERROR_OK
)
1433 int aice_fastread_mem(uint8_t target_id
, uint8_t *word
, uint32_t num_of_words
)
1436 int retry_times
= 0;
1438 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
1439 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
))
1440 aice_usb_packet_flush();
1443 aice_pack_htdmb(AICE_CMD_T_FASTREAD_MEM
, target_id
, num_of_words
- 1, 0);
1445 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMB
);
1447 LOG_DEBUG("FASTREAD_MEM, COREID: %" PRIu8
", # of DATA %08" PRIx32
,
1448 target_id
, num_of_words
);
1450 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMA
+ (num_of_words
- 1) * 4);
1452 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1453 AICE_FORMAT_DTHMA
+ (num_of_words
- 1) * 4, result
);
1457 uint8_t cmd_ack_code
;
1458 uint8_t extra_length
;
1459 uint8_t res_target_id
;
1460 aice_unpack_dthma_multiple_data(&cmd_ack_code
, &res_target_id
,
1461 &extra_length
, word
, data_endian
);
1463 if (cmd_ack_code
== AICE_CMD_T_FASTREAD_MEM
) {
1466 if (retry_times
> aice_max_retry_times
) {
1467 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1468 AICE_CMD_T_FASTREAD_MEM
, cmd_ack_code
);
1473 /* clear timeout and retry */
1474 if (aice_reset_box() != ERROR_OK
)
1484 int aice_fastwrite_mem(uint8_t target_id
, const uint8_t *word
, uint32_t num_of_words
)
1487 int retry_times
= 0;
1489 if (AICE_COMMAND_MODE_PACK
== aice_command_mode
) {
1490 aice_usb_packet_flush();
1491 } else if (AICE_COMMAND_MODE_BATCH
== aice_command_mode
) {
1492 aice_pack_htdmd_multiple_data(AICE_CMD_T_FASTWRITE_MEM
, target_id
,
1493 num_of_words
- 1, 0, word
, data_endian
);
1494 return aice_usb_packet_append(usb_out_buffer
,
1495 AICE_FORMAT_HTDMD
+ (num_of_words
- 1) * 4,
1500 aice_pack_htdmd_multiple_data(AICE_CMD_T_FASTWRITE_MEM
, target_id
,
1501 num_of_words
- 1, 0, word
, data_endian
);
1503 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMD
+ (num_of_words
- 1) * 4);
1505 LOG_DEBUG("FASTWRITE_MEM, COREID: %" PRIu8
", # of DATA %08" PRIx32
,
1506 target_id
, num_of_words
);
1508 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
1509 if (AICE_FORMAT_DTHMB
!= result
) {
1510 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1511 AICE_FORMAT_DTHMB
, result
);
1515 uint8_t cmd_ack_code
;
1516 uint8_t extra_length
;
1517 uint8_t res_target_id
;
1518 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
1520 if (cmd_ack_code
== AICE_CMD_T_FASTWRITE_MEM
) {
1523 if (retry_times
> aice_max_retry_times
) {
1524 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1525 AICE_CMD_T_FASTWRITE_MEM
, cmd_ack_code
);
1530 /* clear timeout and retry */
1531 if (aice_reset_box() != ERROR_OK
)
1541 int aice_read_mem_b(uint8_t target_id
, uint32_t address
, uint32_t *data
)
1544 int retry_times
= 0;
1546 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
1547 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
))
1548 aice_usb_packet_flush();
1551 aice_pack_htdmb(AICE_CMD_T_READ_MEM_B
, target_id
, 0, address
);
1553 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMB
);
1555 LOG_DEBUG("READ_MEM_B, COREID: %" PRIu8
"", target_id
);
1557 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMA
);
1558 if (AICE_FORMAT_DTHMA
!= result
) {
1559 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1560 AICE_FORMAT_DTHMA
, result
);
1564 uint8_t cmd_ack_code
;
1565 uint8_t extra_length
;
1566 uint8_t res_target_id
;
1567 aice_unpack_dthma(&cmd_ack_code
, &res_target_id
, &extra_length
,
1570 if (cmd_ack_code
== AICE_CMD_T_READ_MEM_B
) {
1571 LOG_DEBUG("READ_MEM_B response, data: 0x%02" PRIx32
, *data
);
1574 if (retry_times
> aice_max_retry_times
) {
1575 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1576 AICE_CMD_T_READ_MEM_B
, cmd_ack_code
);
1581 /* clear timeout and retry */
1582 if (aice_reset_box() != ERROR_OK
)
1592 int aice_read_mem_h(uint8_t target_id
, uint32_t address
, uint32_t *data
)
1595 int retry_times
= 0;
1597 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
1598 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
))
1599 aice_usb_packet_flush();
1602 aice_pack_htdmb(AICE_CMD_T_READ_MEM_H
, target_id
, 0, (address
>> 1) & 0x7FFFFFFF);
1604 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMB
);
1606 LOG_DEBUG("READ_MEM_H, CORE_ID: %" PRIu8
"", target_id
);
1608 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMA
);
1609 if (AICE_FORMAT_DTHMA
!= result
) {
1610 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1611 AICE_FORMAT_DTHMA
, result
);
1615 uint8_t cmd_ack_code
;
1616 uint8_t extra_length
;
1617 uint8_t res_target_id
;
1618 aice_unpack_dthma(&cmd_ack_code
, &res_target_id
, &extra_length
,
1621 if (cmd_ack_code
== AICE_CMD_T_READ_MEM_H
) {
1622 LOG_DEBUG("READ_MEM_H response, data: 0x%" PRIx32
, *data
);
1625 if (retry_times
> aice_max_retry_times
) {
1626 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1627 AICE_CMD_T_READ_MEM_H
, cmd_ack_code
);
1632 /* clear timeout and retry */
1633 if (aice_reset_box() != ERROR_OK
)
1643 int aice_read_mem(uint8_t target_id
, uint32_t address
, uint32_t *data
)
1646 int retry_times
= 0;
1648 if ((AICE_COMMAND_MODE_PACK
== aice_command_mode
) ||
1649 (AICE_COMMAND_MODE_BATCH
== aice_command_mode
))
1650 aice_usb_packet_flush();
1653 aice_pack_htdmb(AICE_CMD_T_READ_MEM
, target_id
, 0,
1654 (address
>> 2) & 0x3FFFFFFF);
1656 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMB
);
1658 LOG_DEBUG("READ_MEM, COREID: %" PRIu8
"", target_id
);
1660 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMA
);
1661 if (AICE_FORMAT_DTHMA
!= result
) {
1662 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1663 AICE_FORMAT_DTHMA
, result
);
1667 uint8_t cmd_ack_code
;
1668 uint8_t extra_length
;
1669 uint8_t res_target_id
;
1670 aice_unpack_dthma(&cmd_ack_code
, &res_target_id
, &extra_length
,
1673 if (cmd_ack_code
== AICE_CMD_T_READ_MEM
) {
1674 LOG_DEBUG("READ_MEM response, data: 0x%" PRIx32
, *data
);
1677 if (retry_times
> aice_max_retry_times
) {
1678 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1679 AICE_CMD_T_READ_MEM
, cmd_ack_code
);
1684 /* clear timeout and retry */
1685 if (aice_reset_box() != ERROR_OK
)
1695 int aice_batch_buffer_read(uint8_t buf_index
, uint32_t *word
, uint32_t num_of_words
)
1698 int retry_times
= 0;
1701 aice_pack_htdma(AICE_CMD_BATCH_BUFFER_READ
, 0, num_of_words
- 1, buf_index
);
1703 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMA
);
1705 LOG_DEBUG("BATCH_BUFFER_READ, # of DATA %08" PRIx32
, num_of_words
);
1707 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMA
+ (num_of_words
- 1) * 4);
1709 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1710 AICE_FORMAT_DTHMA
+ (num_of_words
- 1) * 4, result
);
1714 uint8_t cmd_ack_code
;
1715 uint8_t extra_length
;
1716 uint8_t res_target_id
;
1717 aice_unpack_dthma_multiple_data(&cmd_ack_code
, &res_target_id
,
1718 &extra_length
, (uint8_t *)word
, data_endian
);
1720 if (cmd_ack_code
== AICE_CMD_BATCH_BUFFER_READ
) {
1723 if (retry_times
> aice_max_retry_times
) {
1724 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1725 AICE_CMD_BATCH_BUFFER_READ
, cmd_ack_code
);
1730 /* clear timeout and retry */
1731 if (aice_reset_box() != ERROR_OK
)
1741 int aice_batch_buffer_write(uint8_t buf_index
, const uint8_t *word
, uint32_t num_of_words
)
1744 int retry_times
= 0;
1746 if (num_of_words
== 0)
1750 /* only pack AICE_CMD_BATCH_BUFFER_WRITE command header */
1751 aice_pack_htdmc(AICE_CMD_BATCH_BUFFER_WRITE
, 0, num_of_words
- 1, buf_index
,
1754 /* use append instead of pack */
1755 memcpy(usb_out_buffer
+ 4, word
, num_of_words
* 4);
1757 aice_usb_write(usb_out_buffer
, AICE_FORMAT_HTDMC
+ (num_of_words
- 1) * 4);
1759 LOG_DEBUG("BATCH_BUFFER_WRITE, # of DATA %08" PRIx32
, num_of_words
);
1761 result
= aice_usb_read(usb_in_buffer
, AICE_FORMAT_DTHMB
);
1762 if (AICE_FORMAT_DTHMB
!= result
) {
1763 LOG_ERROR("aice_usb_read failed (requested=%" PRId32
", result=%" PRId32
")",
1764 AICE_FORMAT_DTHMB
, result
);
1768 uint8_t cmd_ack_code
;
1769 uint8_t extra_length
;
1770 uint8_t res_target_id
;
1771 aice_unpack_dthmb(&cmd_ack_code
, &res_target_id
, &extra_length
);
1773 if (cmd_ack_code
== AICE_CMD_BATCH_BUFFER_WRITE
) {
1776 if (retry_times
> aice_max_retry_times
) {
1777 LOG_ERROR("aice command timeout (command=0x%" PRIx8
", response=0x%" PRIx8
")",
1778 AICE_CMD_BATCH_BUFFER_WRITE
, cmd_ack_code
);
1783 /* clear timeout and retry */
1784 if (aice_reset_box() != ERROR_OK
)
1794 /***************************************************************************/
1795 /* End of AICE commands */
1797 typedef int (*read_mem_func_t
)(uint32_t coreid
, uint32_t address
, uint32_t *data
);
1798 typedef int (*write_mem_func_t
)(uint32_t coreid
, uint32_t address
, uint32_t data
);
1800 struct aice_nds32_info core_info
[AICE_MAX_NUM_CORE
];
1801 static uint8_t total_num_of_core
;
1803 static char *custom_srst_script
;
1804 static char *custom_trst_script
;
1805 static char *custom_restart_script
;
1806 static uint32_t aice_count_to_check_dbger
= 30;
1808 static int aice_read_reg(uint32_t coreid
, uint32_t num
, uint32_t *val
);
1809 static int aice_write_reg(uint32_t coreid
, uint32_t num
, uint32_t val
);
1811 static int check_suppressed_exception(uint32_t coreid
, uint32_t dbger_value
)
1815 /* the default value of handling_suppressed_exception is false */
1816 static bool handling_suppressed_exception
;
1818 if (handling_suppressed_exception
)
1821 if ((dbger_value
& NDS_DBGER_ALL_SUPRS_EX
) == NDS_DBGER_ALL_SUPRS_EX
) {
1822 LOG_ERROR("<-- TARGET WARNING! Exception is detected and suppressed. -->");
1823 handling_suppressed_exception
= true;
1825 aice_read_reg(coreid
, IR4
, &ir4_value
);
1826 /* Clear IR6.SUPRS_EXC, IR6.IMP_EXC */
1827 aice_read_reg(coreid
, IR6
, &ir6_value
);
1829 * For MCU version(MSC_CFG.MCU == 1) like V3m
1830 * | SWID[30:16] | Reserved[15:10] | SUPRS_EXC[9] | IMP_EXC[8]
1831 * |VECTOR[7:5] | INST[4] | Exc Type[3:0] |
1833 * For non-MCU version(MSC_CFG.MCU == 0) like V3
1834 * | SWID[30:16] | Reserved[15:14] | SUPRS_EXC[13] | IMP_EXC[12]
1835 * | VECTOR[11:5] | INST[4] | Exc Type[3:0] |
1837 LOG_INFO("EVA: 0x%08" PRIx32
, ir4_value
);
1838 LOG_INFO("ITYPE: 0x%08" PRIx32
, ir6_value
);
1840 ir6_value
= ir6_value
& (~0x300); /* for MCU */
1841 ir6_value
= ir6_value
& (~0x3000); /* for non-MCU */
1842 aice_write_reg(coreid
, IR6
, ir6_value
);
1844 handling_suppressed_exception
= false;
1850 static int check_privilege(uint32_t coreid
, uint32_t dbger_value
)
1852 if ((dbger_value
& NDS_DBGER_ILL_SEC_ACC
) == NDS_DBGER_ILL_SEC_ACC
) {
1853 LOG_ERROR("<-- TARGET ERROR! Insufficient security privilege "
1854 "to execute the debug operations. -->");
1856 /* Clear DBGER.ILL_SEC_ACC */
1857 if (aice_write_misc(coreid
, NDS_EDM_MISC_DBGER
,
1858 NDS_DBGER_ILL_SEC_ACC
) != ERROR_OK
)
1865 static int aice_check_dbger(uint32_t coreid
, uint32_t expect_status
)
1868 uint32_t value_dbger
;
1871 aice_read_misc(coreid
, NDS_EDM_MISC_DBGER
, &value_dbger
);
1873 if ((value_dbger
& expect_status
) == expect_status
) {
1874 if (ERROR_OK
!= check_suppressed_exception(coreid
, value_dbger
))
1876 if (ERROR_OK
!= check_privilege(coreid
, value_dbger
))
1885 if (i
== aice_count_to_check_dbger
)
1886 then
= timeval_ms();
1887 if (i
>= aice_count_to_check_dbger
) {
1888 if ((timeval_ms() - then
) > 1000) {
1889 LOG_ERROR("Timeout (1000ms) waiting for $DBGER status "
1890 "being 0x%08" PRIx32
, expect_status
);
1900 static int aice_execute_dim(uint32_t coreid
, uint32_t *insts
, uint8_t n_inst
)
1903 if (aice_write_dim(coreid
, insts
, n_inst
) != ERROR_OK
)
1906 /** clear DBGER.DPED */
1907 if (aice_write_misc(coreid
, NDS_EDM_MISC_DBGER
, NDS_DBGER_DPED
) != ERROR_OK
)
1911 if (aice_do_execute(coreid
) != ERROR_OK
)
1914 /** read DBGER.DPED */
1915 if (aice_check_dbger(coreid
, NDS_DBGER_DPED
) != ERROR_OK
) {
1916 LOG_ERROR("<-- TARGET ERROR! Debug operations do not finish properly: "
1917 "0x%08" PRIx32
"0x%08" PRIx32
"0x%08" PRIx32
"0x%08" PRIx32
". -->",
1928 static int aice_read_reg(uint32_t coreid
, uint32_t num
, uint32_t *val
)
1930 LOG_DEBUG("aice_read_reg, reg_no: 0x%08" PRIx32
, num
);
1932 uint32_t instructions
[4]; /** execute instructions in DIM */
1934 if (NDS32_REG_TYPE_GPR
== nds32_reg_type(num
)) { /* general registers */
1935 instructions
[0] = MTSR_DTR(num
);
1936 instructions
[1] = DSB
;
1937 instructions
[2] = NOP
;
1938 instructions
[3] = BEQ_MINUS_12
;
1939 } else if (NDS32_REG_TYPE_SPR
== nds32_reg_type(num
)) { /* user special registers */
1940 instructions
[0] = MFUSR_G0(0, nds32_reg_sr_index(num
));
1941 instructions
[1] = MTSR_DTR(0);
1942 instructions
[2] = DSB
;
1943 instructions
[3] = BEQ_MINUS_12
;
1944 } else if (NDS32_REG_TYPE_AUMR
== nds32_reg_type(num
)) { /* audio registers */
1945 if ((CB_CTL
<= num
) && (num
<= CBE3
)) {
1946 instructions
[0] = AMFAR2(0, nds32_reg_sr_index(num
));
1947 instructions
[1] = MTSR_DTR(0);
1948 instructions
[2] = DSB
;
1949 instructions
[3] = BEQ_MINUS_12
;
1951 instructions
[0] = AMFAR(0, nds32_reg_sr_index(num
));
1952 instructions
[1] = MTSR_DTR(0);
1953 instructions
[2] = DSB
;
1954 instructions
[3] = BEQ_MINUS_12
;
1956 } else if (NDS32_REG_TYPE_FPU
== nds32_reg_type(num
)) { /* fpu registers */
1958 instructions
[0] = FMFCSR
;
1959 instructions
[1] = MTSR_DTR(0);
1960 instructions
[2] = DSB
;
1961 instructions
[3] = BEQ_MINUS_12
;
1962 } else if (FPCFG
== num
) {
1963 instructions
[0] = FMFCFG
;
1964 instructions
[1] = MTSR_DTR(0);
1965 instructions
[2] = DSB
;
1966 instructions
[3] = BEQ_MINUS_12
;
1968 if (FS0
<= num
&& num
<= FS31
) { /* single precision */
1969 instructions
[0] = FMFSR(0, nds32_reg_sr_index(num
));
1970 instructions
[1] = MTSR_DTR(0);
1971 instructions
[2] = DSB
;
1972 instructions
[3] = BEQ_MINUS_12
;
1973 } else if (FD0
<= num
&& num
<= FD31
) { /* double precision */
1974 instructions
[0] = FMFDR(0, nds32_reg_sr_index(num
));
1975 instructions
[1] = MTSR_DTR(0);
1976 instructions
[2] = DSB
;
1977 instructions
[3] = BEQ_MINUS_12
;
1980 } else { /* system registers */
1981 instructions
[0] = MFSR(0, nds32_reg_sr_index(num
));
1982 instructions
[1] = MTSR_DTR(0);
1983 instructions
[2] = DSB
;
1984 instructions
[3] = BEQ_MINUS_12
;
1987 aice_execute_dim(coreid
, instructions
, 4);
1989 uint32_t value_edmsw
;
1990 aice_read_edmsr(coreid
, NDS_EDM_SR_EDMSW
, &value_edmsw
);
1991 if (value_edmsw
& NDS_EDMSW_WDV
)
1992 aice_read_dtr(coreid
, val
);
1994 LOG_ERROR("<-- TARGET ERROR! The debug target failed to update "
1995 "the DTR register. -->");
2002 static int aice_usb_read_reg(uint32_t coreid
, uint32_t num
, uint32_t *val
)
2004 LOG_DEBUG("aice_usb_read_reg");
2007 *val
= core_info
[coreid
].r0_backup
;
2008 } else if (num
== R1
) {
2009 *val
= core_info
[coreid
].r1_backup
;
2010 } else if (num
== DR41
) {
2011 /* As target is halted, OpenOCD will backup DR41/DR42/DR43.
2012 * As user wants to read these registers, OpenOCD should return
2013 * the backup values, instead of reading the real values.
2014 * As user wants to write these registers, OpenOCD should write
2015 * to the backup values, instead of writing to real registers. */
2016 *val
= core_info
[coreid
].edmsw_backup
;
2017 } else if (num
== DR42
) {
2018 *val
= core_info
[coreid
].edm_ctl_backup
;
2019 } else if ((core_info
[coreid
].target_dtr_valid
== true) && (num
== DR43
)) {
2020 *val
= core_info
[coreid
].target_dtr_backup
;
2022 if (ERROR_OK
!= aice_read_reg(coreid
, num
, val
))
2029 static int aice_write_reg(uint32_t coreid
, uint32_t num
, uint32_t val
)
2031 LOG_DEBUG("aice_write_reg, reg_no: 0x%08" PRIx32
", value: 0x%08" PRIx32
, num
, val
);
2033 uint32_t instructions
[4]; /** execute instructions in DIM */
2034 uint32_t value_edmsw
;
2036 aice_write_dtr(coreid
, val
);
2037 aice_read_edmsr(coreid
, NDS_EDM_SR_EDMSW
, &value_edmsw
);
2038 if (0 == (value_edmsw
& NDS_EDMSW_RDV
)) {
2039 LOG_ERROR("<-- TARGET ERROR! AICE failed to write to the DTR register. -->");
2043 if (NDS32_REG_TYPE_GPR
== nds32_reg_type(num
)) { /* general registers */
2044 instructions
[0] = MFSR_DTR(num
);
2045 instructions
[1] = DSB
;
2046 instructions
[2] = NOP
;
2047 instructions
[3] = BEQ_MINUS_12
;
2048 } else if (NDS32_REG_TYPE_SPR
== nds32_reg_type(num
)) { /* user special registers */
2049 instructions
[0] = MFSR_DTR(0);
2050 instructions
[1] = MTUSR_G0(0, nds32_reg_sr_index(num
));
2051 instructions
[2] = DSB
;
2052 instructions
[3] = BEQ_MINUS_12
;
2053 } else if (NDS32_REG_TYPE_AUMR
== nds32_reg_type(num
)) { /* audio registers */
2054 if ((CB_CTL
<= num
) && (num
<= CBE3
)) {
2055 instructions
[0] = MFSR_DTR(0);
2056 instructions
[1] = AMTAR2(0, nds32_reg_sr_index(num
));
2057 instructions
[2] = DSB
;
2058 instructions
[3] = BEQ_MINUS_12
;
2060 instructions
[0] = MFSR_DTR(0);
2061 instructions
[1] = AMTAR(0, nds32_reg_sr_index(num
));
2062 instructions
[2] = DSB
;
2063 instructions
[3] = BEQ_MINUS_12
;
2065 } else if (NDS32_REG_TYPE_FPU
== nds32_reg_type(num
)) { /* fpu registers */
2067 instructions
[0] = MFSR_DTR(0);
2068 instructions
[1] = FMTCSR
;
2069 instructions
[2] = DSB
;
2070 instructions
[3] = BEQ_MINUS_12
;
2071 } else if (FPCFG
== num
) {
2072 /* FPCFG is readonly */
2074 if (FS0
<= num
&& num
<= FS31
) { /* single precision */
2075 instructions
[0] = MFSR_DTR(0);
2076 instructions
[1] = FMTSR(0, nds32_reg_sr_index(num
));
2077 instructions
[2] = DSB
;
2078 instructions
[3] = BEQ_MINUS_12
;
2079 } else if (FD0
<= num
&& num
<= FD31
) { /* double precision */
2080 instructions
[0] = MFSR_DTR(0);
2081 instructions
[1] = FMTDR(0, nds32_reg_sr_index(num
));
2082 instructions
[2] = DSB
;
2083 instructions
[3] = BEQ_MINUS_12
;
2087 instructions
[0] = MFSR_DTR(0);
2088 instructions
[1] = MTSR(0, nds32_reg_sr_index(num
));
2089 instructions
[2] = DSB
;
2090 instructions
[3] = BEQ_MINUS_12
;
2093 return aice_execute_dim(coreid
, instructions
, 4);
2096 static int aice_usb_write_reg(uint32_t coreid
, uint32_t num
, uint32_t val
)
2098 LOG_DEBUG("aice_usb_write_reg");
2101 core_info
[coreid
].r0_backup
= val
;
2103 core_info
[coreid
].r1_backup
= val
;
2104 else if (num
== DR42
)
2105 /* As target is halted, OpenOCD will backup DR41/DR42/DR43.
2106 * As user wants to read these registers, OpenOCD should return
2107 * the backup values, instead of reading the real values.
2108 * As user wants to write these registers, OpenOCD should write
2109 * to the backup values, instead of writing to real registers. */
2110 core_info
[coreid
].edm_ctl_backup
= val
;
2111 else if ((core_info
[coreid
].target_dtr_valid
== true) && (num
== DR43
))
2112 core_info
[coreid
].target_dtr_backup
= val
;
2114 return aice_write_reg(coreid
, num
, val
);
2119 static int aice_usb_open(struct aice_port_param_s
*param
)
2121 const uint16_t vids
[] = { param
->vid
, 0 };
2122 const uint16_t pids
[] = { param
->pid
, 0 };
2123 struct jtag_libusb_device_handle
*devh
;
2125 if (jtag_libusb_open(vids
, pids
, &devh
) != ERROR_OK
)
2128 /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
2129 * AREA!!!!!!!!!!! The behavior of libusb is not completely
2130 * consistent across Windows, Linux, and Mac OS X platforms.
2131 * The actions taken in the following compiler conditionals may
2132 * not agree with published documentation for libusb, but were
2133 * found to be necessary through trials and tribulations. Even
2134 * little tweaks can break one or more platforms, so if you do
2135 * make changes test them carefully on all platforms before
2141 jtag_libusb_reset_device(devh
);
2146 /* reopen jlink after usb_reset
2147 * on win32 this may take a second or two to re-enumerate */
2149 while ((retval
= jtag_libusb_open(vids
, pids
, &devh
)) != ERROR_OK
) {
2155 if (ERROR_OK
!= retval
)
2161 /* usb_set_configuration required under win32 */
2162 struct jtag_libusb_device
*udev
= jtag_libusb_get_device(devh
);
2163 jtag_libusb_set_configuration(devh
, 0);
2164 jtag_libusb_claim_interface(devh
, 0);
2166 unsigned int aice_read_ep
;
2167 unsigned int aice_write_ep
;
2168 jtag_libusb_get_endpoints(udev
, &aice_read_ep
, &aice_write_ep
);
2170 aice_handler
.usb_read_ep
= aice_read_ep
;
2171 aice_handler
.usb_write_ep
= aice_write_ep
;
2172 aice_handler
.usb_handle
= devh
;
2177 static int aice_usb_read_reg_64(uint32_t coreid
, uint32_t num
, uint64_t *val
)
2179 LOG_DEBUG("aice_usb_read_reg_64, %s", nds32_reg_simple_name(num
));
2182 uint32_t high_value
;
2184 if (ERROR_OK
!= aice_read_reg(coreid
, num
, &value
))
2187 aice_read_reg(coreid
, R1
, &high_value
);
2189 LOG_DEBUG("low: 0x%08" PRIx32
", high: 0x%08" PRIx32
"\n", value
, high_value
);
2191 if (data_endian
== AICE_BIG_ENDIAN
)
2192 *val
= (((uint64_t)high_value
) << 32) | value
;
2194 *val
= (((uint64_t)value
) << 32) | high_value
;
2199 static int aice_usb_write_reg_64(uint32_t coreid
, uint32_t num
, uint64_t val
)
2202 uint32_t high_value
;
2204 if (data_endian
== AICE_BIG_ENDIAN
) {
2205 value
= val
& 0xFFFFFFFF;
2206 high_value
= (val
>> 32) & 0xFFFFFFFF;
2208 high_value
= val
& 0xFFFFFFFF;
2209 value
= (val
>> 32) & 0xFFFFFFFF;
2212 LOG_DEBUG("aice_usb_write_reg_64, %s, low: 0x%08" PRIx32
", high: 0x%08" PRIx32
"\n",
2213 nds32_reg_simple_name(num
), value
, high_value
);
2215 aice_write_reg(coreid
, R1
, high_value
);
2216 return aice_write_reg(coreid
, num
, value
);
2219 static int aice_get_version_info(void)
2221 uint32_t hardware_version
;
2222 uint32_t firmware_version
;
2223 uint32_t fpga_version
;
2225 if (aice_read_ctrl(AICE_READ_CTRL_GET_HARDWARE_VERSION
, &hardware_version
) != ERROR_OK
)
2228 if (aice_read_ctrl(AICE_READ_CTRL_GET_FIRMWARE_VERSION
, &firmware_version
) != ERROR_OK
)
2231 if (aice_read_ctrl(AICE_READ_CTRL_GET_FPGA_VERSION
, &fpga_version
) != ERROR_OK
)
2234 LOG_INFO("AICE version: hw_ver = 0x%" PRIx32
", fw_ver = 0x%" PRIx32
", fpga_ver = 0x%" PRIx32
,
2235 hardware_version
, firmware_version
, fpga_version
);
2240 #define LINE_BUFFER_SIZE 1024
2242 static int aice_execute_custom_script(const char *script
)
2245 char line_buffer
[LINE_BUFFER_SIZE
];
2249 uint32_t write_ctrl_value
;
2252 script_fd
= fopen(script
, "r");
2253 if (script_fd
== NULL
) {
2256 while (fgets(line_buffer
, LINE_BUFFER_SIZE
, script_fd
) != NULL
) {
2257 /* execute operations */
2259 op_str
= strstr(line_buffer
, "set");
2260 if (op_str
!= NULL
) {
2262 goto get_reset_type
;
2265 op_str
= strstr(line_buffer
, "clear");
2269 reset_str
= strstr(op_str
, "srst");
2270 if (reset_str
!= NULL
) {
2272 write_ctrl_value
= AICE_CUSTOM_DELAY_SET_SRST
;
2274 write_ctrl_value
= AICE_CUSTOM_DELAY_CLEAN_SRST
;
2277 reset_str
= strstr(op_str
, "dbgi");
2278 if (reset_str
!= NULL
) {
2280 write_ctrl_value
= AICE_CUSTOM_DELAY_SET_DBGI
;
2282 write_ctrl_value
= AICE_CUSTOM_DELAY_CLEAN_DBGI
;
2285 reset_str
= strstr(op_str
, "trst");
2286 if (reset_str
!= NULL
) {
2288 write_ctrl_value
= AICE_CUSTOM_DELAY_SET_TRST
;
2290 write_ctrl_value
= AICE_CUSTOM_DELAY_CLEAN_TRST
;
2296 delay
= strtoul(reset_str
+ 4, NULL
, 0);
2297 write_ctrl_value
|= (delay
<< 16);
2299 if (aice_write_ctrl(AICE_WRITE_CTRL_CUSTOM_DELAY
,
2300 write_ctrl_value
) != ERROR_OK
) {
2311 static int aice_usb_set_clock(int set_clock
)
2313 if (aice_write_ctrl(AICE_WRITE_CTRL_TCK_CONTROL
,
2314 AICE_TCK_CONTROL_TCK_SCAN
) != ERROR_OK
)
2317 /* Read out TCK_SCAN clock value */
2318 uint32_t scan_clock
;
2319 if (aice_read_ctrl(AICE_READ_CTRL_GET_ICE_STATE
, &scan_clock
) != ERROR_OK
)
2324 uint32_t scan_base_freq
;
2325 if (scan_clock
& 0x8)
2326 scan_base_freq
= 48000; /* 48 MHz */
2328 scan_base_freq
= 30000; /* 30 MHz */
2330 uint32_t set_base_freq
;
2331 if (set_clock
& 0x8)
2332 set_base_freq
= 48000;
2334 set_base_freq
= 30000;
2338 set_freq
= set_base_freq
>> (set_clock
& 0x7);
2339 scan_freq
= scan_base_freq
>> (scan_clock
& 0x7);
2341 if (scan_freq
< set_freq
) {
2342 LOG_ERROR("User specifies higher jtag clock than TCK_SCAN clock");
2346 if (aice_write_ctrl(AICE_WRITE_CTRL_TCK_CONTROL
, set_clock
) != ERROR_OK
)
2349 uint32_t check_speed
;
2350 if (aice_read_ctrl(AICE_READ_CTRL_GET_ICE_STATE
, &check_speed
) != ERROR_OK
)
2353 if (((int)check_speed
& 0x0F) != set_clock
) {
2354 LOG_ERROR("Set jtag clock failed");
2361 static int aice_edm_init(uint32_t coreid
)
2363 aice_write_edmsr(coreid
, NDS_EDM_SR_DIMBR
, 0xFFFF0000);
2364 aice_write_misc(coreid
, NDS_EDM_MISC_DIMIR
, 0);
2366 /* unconditionally try to turn on V3_EDM_MODE */
2367 uint32_t edm_ctl_value
;
2368 aice_read_edmsr(coreid
, NDS_EDM_SR_EDM_CTL
, &edm_ctl_value
);
2369 aice_write_edmsr(coreid
, NDS_EDM_SR_EDM_CTL
, edm_ctl_value
| 0x00000040);
2372 aice_write_misc(coreid
, NDS_EDM_MISC_DBGER
,
2373 NDS_DBGER_DPED
| NDS_DBGER_CRST
| NDS_DBGER_AT_MAX
);
2375 /* get EDM version */
2376 uint32_t value_edmcfg
;
2377 aice_read_edmsr(coreid
, NDS_EDM_SR_EDM_CFG
, &value_edmcfg
);
2378 core_info
[coreid
].edm_version
= (value_edmcfg
>> 16) & 0xFFFF;
2383 static bool is_v2_edm(uint32_t coreid
)
2385 if ((core_info
[coreid
].edm_version
& 0x1000) == 0)
2391 static int aice_init_edm_registers(uint32_t coreid
, bool clear_dex_use_psw
)
2393 /* enable DEH_SEL & MAX_STOP & V3_EDM_MODE & DBGI_MASK */
2394 uint32_t host_edm_ctl
= core_info
[coreid
].edm_ctl_backup
| 0xA000004F;
2395 if (clear_dex_use_psw
)
2396 /* After entering debug mode, OpenOCD may set
2397 * DEX_USE_PSW accidentally through backup value
2398 * of target EDM_CTL.
2399 * So, clear DEX_USE_PSW by force. */
2400 host_edm_ctl
&= ~(0x40000000);
2402 LOG_DEBUG("aice_init_edm_registers - EDM_CTL: 0x%08" PRIx32
, host_edm_ctl
);
2404 int result
= aice_write_edmsr(coreid
, NDS_EDM_SR_EDM_CTL
, host_edm_ctl
);
2410 * EDM_CTL will be modified by OpenOCD as debugging. OpenOCD has the
2411 * responsibility to keep EDM_CTL untouched after debugging.
2413 * There are two scenarios to consider:
2414 * 1. single step/running as debugging (running under debug session)
2415 * 2. detached from gdb (exit debug session)
2417 * So, we need to bakcup EDM_CTL before halted and restore it after
2418 * running. The difference of these two scenarios is EDM_CTL.DEH_SEL
2419 * is on for scenario 1, and off for scenario 2.
2421 static int aice_backup_edm_registers(uint32_t coreid
)
2423 int result
= aice_read_edmsr(coreid
, NDS_EDM_SR_EDM_CTL
,
2424 &core_info
[coreid
].edm_ctl_backup
);
2426 /* To call aice_backup_edm_registers() after DEX on, DEX_USE_PSW
2427 * may be not correct. (For example, hit breakpoint, then backup
2428 * EDM_CTL. EDM_CTL.DEX_USE_PSW will be cleared.) Because debug
2429 * interrupt will clear DEX_USE_PSW, DEX_USE_PSW is always off after
2430 * DEX is on. It only backups correct value before OpenOCD issues DBGI.
2431 * (Backup EDM_CTL, then issue DBGI actively (refer aice_usb_halt())) */
2432 if (core_info
[coreid
].edm_ctl_backup
& 0x40000000)
2433 core_info
[coreid
].dex_use_psw_on
= true;
2435 core_info
[coreid
].dex_use_psw_on
= false;
2437 LOG_DEBUG("aice_backup_edm_registers - EDM_CTL: 0x%08" PRIx32
", DEX_USE_PSW: %s",
2438 core_info
[coreid
].edm_ctl_backup
,
2439 core_info
[coreid
].dex_use_psw_on
? "on" : "off");
2444 static int aice_restore_edm_registers(uint32_t coreid
)
2446 LOG_DEBUG("aice_restore_edm_registers -");
2448 /* set DEH_SEL, because target still under EDM control */
2449 int result
= aice_write_edmsr(coreid
, NDS_EDM_SR_EDM_CTL
,
2450 core_info
[coreid
].edm_ctl_backup
| 0x80000000);
2455 static int aice_backup_tmp_registers(uint32_t coreid
)
2457 LOG_DEBUG("backup_tmp_registers -");
2459 /* backup target DTR first(if the target DTR is valid) */
2460 uint32_t value_edmsw
;
2461 aice_read_edmsr(coreid
, NDS_EDM_SR_EDMSW
, &value_edmsw
);
2462 core_info
[coreid
].edmsw_backup
= value_edmsw
;
2463 if (value_edmsw
& 0x1) { /* EDMSW.WDV == 1 */
2464 aice_read_dtr(coreid
, &core_info
[coreid
].target_dtr_backup
);
2465 core_info
[coreid
].target_dtr_valid
= true;
2467 LOG_DEBUG("Backup target DTR: 0x%08" PRIx32
, core_info
[coreid
].target_dtr_backup
);
2469 core_info
[coreid
].target_dtr_valid
= false;
2472 /* Target DTR has been backup, then backup $R0 and $R1 */
2473 aice_read_reg(coreid
, R0
, &core_info
[coreid
].r0_backup
);
2474 aice_read_reg(coreid
, R1
, &core_info
[coreid
].r1_backup
);
2476 /* backup host DTR(if the host DTR is valid) */
2477 if (value_edmsw
& 0x2) { /* EDMSW.RDV == 1*/
2478 /* read out host DTR and write into target DTR, then use aice_read_edmsr to
2480 uint32_t instructions
[4] = {
2481 MFSR_DTR(R0
), /* R0 has already been backup */
2486 aice_execute_dim(coreid
, instructions
, 4);
2488 aice_read_dtr(coreid
, &core_info
[coreid
].host_dtr_backup
);
2489 core_info
[coreid
].host_dtr_valid
= true;
2491 LOG_DEBUG("Backup host DTR: 0x%08" PRIx32
, core_info
[coreid
].host_dtr_backup
);
2493 core_info
[coreid
].host_dtr_valid
= false;
2496 LOG_DEBUG("r0: 0x%08" PRIx32
", r1: 0x%08" PRIx32
,
2497 core_info
[coreid
].r0_backup
, core_info
[coreid
].r1_backup
);
2502 static int aice_restore_tmp_registers(uint32_t coreid
)
2504 LOG_DEBUG("restore_tmp_registers - r0: 0x%08" PRIx32
", r1: 0x%08" PRIx32
,
2505 core_info
[coreid
].r0_backup
, core_info
[coreid
].r1_backup
);
2507 if (core_info
[coreid
].target_dtr_valid
) {
2508 uint32_t instructions
[4] = {
2509 SETHI(R0
, core_info
[coreid
].target_dtr_backup
>> 12),
2510 ORI(R0
, R0
, core_info
[coreid
].target_dtr_backup
& 0x00000FFF),
2514 aice_execute_dim(coreid
, instructions
, 4);
2516 instructions
[0] = MTSR_DTR(R0
);
2517 instructions
[1] = DSB
;
2518 instructions
[2] = NOP
;
2519 instructions
[3] = BEQ_MINUS_12
;
2520 aice_execute_dim(coreid
, instructions
, 4);
2522 LOG_DEBUG("Restore target DTR: 0x%08" PRIx32
, core_info
[coreid
].target_dtr_backup
);
2525 aice_write_reg(coreid
, R0
, core_info
[coreid
].r0_backup
);
2526 aice_write_reg(coreid
, R1
, core_info
[coreid
].r1_backup
);
2528 if (core_info
[coreid
].host_dtr_valid
) {
2529 aice_write_dtr(coreid
, core_info
[coreid
].host_dtr_backup
);
2531 LOG_DEBUG("Restore host DTR: 0x%08" PRIx32
, core_info
[coreid
].host_dtr_backup
);
2537 static int aice_open_device(struct aice_port_param_s
*param
)
2539 if (ERROR_OK
!= aice_usb_open(param
))
2542 if (ERROR_FAIL
== aice_get_version_info()) {
2543 LOG_ERROR("Cannot get AICE version!");
2547 LOG_INFO("AICE initialization started");
2549 /* attempt to reset Andes EDM */
2550 if (ERROR_FAIL
== aice_reset_box()) {
2551 LOG_ERROR("Cannot initial AICE box!");
2558 static int aice_usb_set_jtag_clock(uint32_t a_clock
)
2560 jtag_clock
= a_clock
;
2562 if (ERROR_OK
!= aice_usb_set_clock(a_clock
)) {
2563 LOG_ERROR("Cannot set AICE JTAG clock!");
2570 static int aice_usb_close(void)
2572 jtag_libusb_close(aice_handler
.usb_handle
);
2574 if (custom_srst_script
)
2575 free(custom_srst_script
);
2577 if (custom_trst_script
)
2578 free(custom_trst_script
);
2580 if (custom_restart_script
)
2581 free(custom_restart_script
);
2586 static int aice_core_init(uint32_t coreid
)
2588 core_info
[coreid
].access_channel
= NDS_MEMORY_ACC_CPU
;
2589 core_info
[coreid
].memory_select
= NDS_MEMORY_SELECT_AUTO
;
2590 core_info
[coreid
].core_state
= AICE_TARGET_UNKNOWN
;
2595 static int aice_usb_idcode(uint32_t *idcode
, uint8_t *num_of_idcode
)
2599 retval
= aice_scan_chain(idcode
, num_of_idcode
);
2600 if (ERROR_OK
== retval
) {
2601 for (int i
= 0; i
< *num_of_idcode
; i
++) {
2605 total_num_of_core
= *num_of_idcode
;
2611 static int aice_usb_halt(uint32_t coreid
)
2613 if (core_info
[coreid
].core_state
== AICE_TARGET_HALTED
) {
2614 LOG_DEBUG("aice_usb_halt check halted");
2618 LOG_DEBUG("aice_usb_halt");
2620 /** backup EDM registers */
2621 aice_backup_edm_registers(coreid
);
2622 /** init EDM for host debugging */
2623 /** no need to clear dex_use_psw, because dbgi will clear it */
2624 aice_init_edm_registers(coreid
, false);
2626 /** Clear EDM_CTL.DBGIM & EDM_CTL.DBGACKM */
2627 uint32_t edm_ctl_value
;
2628 aice_read_edmsr(coreid
, NDS_EDM_SR_EDM_CTL
, &edm_ctl_value
);
2629 if (edm_ctl_value
& 0x3)
2630 aice_write_edmsr(coreid
, NDS_EDM_SR_EDM_CTL
, edm_ctl_value
& ~(0x3));
2633 uint32_t acc_ctl_value
;
2635 core_info
[coreid
].debug_under_dex_on
= false;
2636 aice_read_misc(coreid
, NDS_EDM_MISC_DBGER
, &dbger
);
2638 if (dbger
& NDS_DBGER_AT_MAX
)
2639 LOG_ERROR("<-- TARGET ERROR! Reaching the max interrupt stack level. -->");
2641 if (dbger
& NDS_DBGER_DEX
) {
2642 if (is_v2_edm(coreid
) == false) {
2643 /** debug 'debug mode'. use force_debug to issue dbgi */
2644 aice_read_misc(coreid
, NDS_EDM_MISC_ACC_CTL
, &acc_ctl_value
);
2645 acc_ctl_value
|= 0x8;
2646 aice_write_misc(coreid
, NDS_EDM_MISC_ACC_CTL
, acc_ctl_value
);
2647 core_info
[coreid
].debug_under_dex_on
= true;
2649 aice_write_misc(coreid
, NDS_EDM_MISC_EDM_CMDR
, 0);
2650 /* If CPU stalled due to AT_MAX, clear AT_MAX status. */
2651 if (dbger
& NDS_DBGER_AT_MAX
)
2652 aice_write_misc(coreid
, NDS_EDM_MISC_DBGER
, NDS_DBGER_AT_MAX
);
2655 /** Issue DBGI normally */
2656 aice_write_misc(coreid
, NDS_EDM_MISC_EDM_CMDR
, 0);
2657 /* If CPU stalled due to AT_MAX, clear AT_MAX status. */
2658 if (dbger
& NDS_DBGER_AT_MAX
)
2659 aice_write_misc(coreid
, NDS_EDM_MISC_DBGER
, NDS_DBGER_AT_MAX
);
2662 if (aice_check_dbger(coreid
, NDS_DBGER_DEX
) != ERROR_OK
) {
2663 LOG_ERROR("<-- TARGET ERROR! Unable to stop the debug target through DBGI. -->");
2667 if (core_info
[coreid
].debug_under_dex_on
) {
2668 if (core_info
[coreid
].dex_use_psw_on
== false) {
2669 /* under debug 'debug mode', force $psw to 'debug mode' bahavior */
2670 /* !!!NOTICE!!! this is workaround for debug 'debug mode'.
2671 * it is only for debugging 'debug exception handler' purpose.
2672 * after openocd detaches from target, target behavior is
2675 uint32_t debug_mode_ir0_value
;
2676 aice_read_reg(coreid
, IR0
, &ir0_value
);
2677 debug_mode_ir0_value
= ir0_value
| 0x408; /* turn on DEX, set POM = 1 */
2678 debug_mode_ir0_value
&= ~(0x000000C1); /* turn off DT/IT/GIE */
2679 aice_write_reg(coreid
, IR0
, debug_mode_ir0_value
);
2683 /** set EDM_CTL.DBGIM & EDM_CTL.DBGACKM after halt */
2684 if (edm_ctl_value
& 0x3)
2685 aice_write_edmsr(coreid
, NDS_EDM_SR_EDM_CTL
, edm_ctl_value
);
2687 /* backup r0 & r1 */
2688 aice_backup_tmp_registers(coreid
);
2689 core_info
[coreid
].core_state
= AICE_TARGET_HALTED
;
2694 static int aice_usb_state(uint32_t coreid
, enum aice_target_state_s
*state
)
2696 uint32_t dbger_value
;
2699 int result
= aice_read_misc(coreid
, NDS_EDM_MISC_DBGER
, &dbger_value
);
2701 if (ERROR_AICE_TIMEOUT
== result
) {
2702 if (aice_read_ctrl(AICE_READ_CTRL_GET_ICE_STATE
, &ice_state
) != ERROR_OK
) {
2703 LOG_ERROR("<-- AICE ERROR! AICE is unplugged. -->");
2707 if ((ice_state
& 0x20) == 0) {
2708 LOG_ERROR("<-- TARGET ERROR! Target is disconnected with AICE. -->");
2713 } else if (ERROR_AICE_DISCONNECT
== result
) {
2714 LOG_ERROR("<-- AICE ERROR! AICE is unplugged. -->");
2718 if ((dbger_value
& NDS_DBGER_ILL_SEC_ACC
) == NDS_DBGER_ILL_SEC_ACC
) {
2719 LOG_ERROR("<-- TARGET ERROR! Insufficient security privilege. -->");
2721 /* Clear ILL_SEC_ACC */
2722 aice_write_misc(coreid
, NDS_EDM_MISC_DBGER
, NDS_DBGER_ILL_SEC_ACC
);
2724 *state
= AICE_TARGET_RUNNING
;
2725 core_info
[coreid
].core_state
= AICE_TARGET_RUNNING
;
2726 } else if ((dbger_value
& NDS_DBGER_AT_MAX
) == NDS_DBGER_AT_MAX
) {
2727 /* Issue DBGI to exit cpu stall */
2728 aice_usb_halt(coreid
);
2730 /* Read OIPC to find out the trigger point */
2731 uint32_t ir11_value
;
2732 aice_read_reg(coreid
, IR11
, &ir11_value
);
2734 LOG_ERROR("<-- TARGET ERROR! Reaching the max interrupt stack level; "
2735 "CPU is stalled at 0x%08" PRIx32
" for debugging. -->", ir11_value
);
2737 *state
= AICE_TARGET_HALTED
;
2738 } else if ((dbger_value
& NDS_DBGER_CRST
) == NDS_DBGER_CRST
) {
2739 LOG_DEBUG("DBGER.CRST is on.");
2741 *state
= AICE_TARGET_RESET
;
2742 core_info
[coreid
].core_state
= AICE_TARGET_RUNNING
;
2745 aice_write_misc(coreid
, NDS_EDM_MISC_DBGER
, NDS_DBGER_CRST
);
2746 } else if ((dbger_value
& NDS_DBGER_DEX
) == NDS_DBGER_DEX
) {
2747 if (AICE_TARGET_RUNNING
== core_info
[coreid
].core_state
) {
2748 /* enter debug mode, init EDM registers */
2749 /* backup EDM registers */
2750 aice_backup_edm_registers(coreid
);
2751 /* init EDM for host debugging */
2752 aice_init_edm_registers(coreid
, true);
2753 aice_backup_tmp_registers(coreid
);
2754 core_info
[coreid
].core_state
= AICE_TARGET_HALTED
;
2755 } else if (AICE_TARGET_UNKNOWN
== core_info
[coreid
].core_state
) {
2756 /* debug 'debug mode', use force debug to halt core */
2757 aice_usb_halt(coreid
);
2759 *state
= AICE_TARGET_HALTED
;
2761 *state
= AICE_TARGET_RUNNING
;
2762 core_info
[coreid
].core_state
= AICE_TARGET_RUNNING
;
2768 static int aice_usb_reset(void)
2770 if (aice_reset_box() != ERROR_OK
)
2774 if (custom_trst_script
== NULL
) {
2775 if (aice_write_ctrl(AICE_WRITE_CTRL_JTAG_PIN_CONTROL
,
2776 AICE_JTAG_PIN_CONTROL_TRST
) != ERROR_OK
)
2779 /* custom trst operations */
2780 if (aice_execute_custom_script(custom_trst_script
) != ERROR_OK
)
2784 if (aice_usb_set_clock(jtag_clock
) != ERROR_OK
)
2790 static int aice_issue_srst(uint32_t coreid
)
2792 LOG_DEBUG("aice_issue_srst");
2794 /* After issuing srst, target will be running. So we need to restore EDM_CTL. */
2795 aice_restore_edm_registers(coreid
);
2797 if (custom_srst_script
== NULL
) {
2798 if (aice_write_ctrl(AICE_WRITE_CTRL_JTAG_PIN_CONTROL
,
2799 AICE_JTAG_PIN_CONTROL_SRST
) != ERROR_OK
)
2802 /* custom srst operations */
2803 if (aice_execute_custom_script(custom_srst_script
) != ERROR_OK
)
2807 /* wait CRST infinitely */
2808 uint32_t dbger_value
;
2811 if (aice_read_misc(coreid
,
2812 NDS_EDM_MISC_DBGER
, &dbger_value
) != ERROR_OK
)
2815 if (dbger_value
& NDS_DBGER_CRST
)
2823 core_info
[coreid
].host_dtr_valid
= false;
2824 core_info
[coreid
].target_dtr_valid
= false;
2826 core_info
[coreid
].core_state
= AICE_TARGET_RUNNING
;
2830 static int aice_issue_reset_hold(uint32_t coreid
)
2832 LOG_DEBUG("aice_issue_reset_hold");
2834 /* set no_dbgi_pin to 0 */
2835 uint32_t pin_status
;
2836 aice_read_ctrl(AICE_READ_CTRL_GET_JTAG_PIN_STATUS
, &pin_status
);
2837 if (pin_status
| 0x4)
2838 aice_write_ctrl(AICE_WRITE_CTRL_JTAG_PIN_STATUS
, pin_status
& (~0x4));
2841 if (custom_restart_script
== NULL
) {
2842 if (aice_write_ctrl(AICE_WRITE_CTRL_JTAG_PIN_CONTROL
,
2843 AICE_JTAG_PIN_CONTROL_RESTART
) != ERROR_OK
)
2846 /* custom restart operations */
2847 if (aice_execute_custom_script(custom_restart_script
) != ERROR_OK
)
2851 if (aice_check_dbger(coreid
, NDS_DBGER_CRST
| NDS_DBGER_DEX
) == ERROR_OK
) {
2852 aice_backup_tmp_registers(coreid
);
2853 core_info
[coreid
].core_state
= AICE_TARGET_HALTED
;
2857 /* set no_dbgi_pin to 1 */
2858 aice_write_ctrl(AICE_WRITE_CTRL_JTAG_PIN_STATUS
, pin_status
| 0x4);
2860 /* issue restart again */
2861 if (custom_restart_script
== NULL
) {
2862 if (aice_write_ctrl(AICE_WRITE_CTRL_JTAG_PIN_CONTROL
,
2863 AICE_JTAG_PIN_CONTROL_RESTART
) != ERROR_OK
)
2866 /* custom restart operations */
2867 if (aice_execute_custom_script(custom_restart_script
) != ERROR_OK
)
2871 if (aice_check_dbger(coreid
, NDS_DBGER_CRST
| NDS_DBGER_DEX
) == ERROR_OK
) {
2872 aice_backup_tmp_registers(coreid
);
2873 core_info
[coreid
].core_state
= AICE_TARGET_HALTED
;
2878 /* do software reset-and-hold */
2879 aice_issue_srst(coreid
);
2880 aice_usb_halt(coreid
);
2883 aice_read_reg(coreid
, IR3
, &value_ir3
);
2884 aice_write_reg(coreid
, PC
, value_ir3
& 0xFFFF0000);
2890 static int aice_issue_reset_hold_multi(void)
2892 uint32_t write_ctrl_value
= 0;
2895 write_ctrl_value
= AICE_CUSTOM_DELAY_SET_SRST
;
2896 write_ctrl_value
|= (0x200 << 16);
2897 if (aice_write_ctrl(AICE_WRITE_CTRL_CUSTOM_DELAY
,
2898 write_ctrl_value
) != ERROR_OK
)
2901 for (uint8_t i
= 0 ; i
< total_num_of_core
; i
++)
2902 aice_write_misc(i
, NDS_EDM_MISC_EDM_CMDR
, 0);
2905 write_ctrl_value
= AICE_CUSTOM_DELAY_CLEAN_SRST
;
2906 write_ctrl_value
|= (0x200 << 16);
2907 if (aice_write_ctrl(AICE_WRITE_CTRL_CUSTOM_DELAY
,
2908 write_ctrl_value
) != ERROR_OK
)
2911 for (uint8_t i
= 0; i
< total_num_of_core
; i
++)
2917 static int aice_usb_assert_srst(uint32_t coreid
, enum aice_srst_type_s srst
)
2919 if ((AICE_SRST
!= srst
) && (AICE_RESET_HOLD
!= srst
))
2923 if (aice_write_misc(coreid
, NDS_EDM_MISC_DBGER
,
2924 NDS_DBGER_CLEAR_ALL
) != ERROR_OK
)
2927 int result
= ERROR_OK
;
2928 if (AICE_SRST
== srst
)
2929 result
= aice_issue_srst(coreid
);
2931 if (1 == total_num_of_core
)
2932 result
= aice_issue_reset_hold(coreid
);
2934 result
= aice_issue_reset_hold_multi();
2937 /* Clear DBGER.CRST after reset to avoid 'core-reset checking' errors.
2938 * assert_srst is user-intentional reset behavior, so we could
2939 * clear DBGER.CRST safely.
2941 if (aice_write_misc(coreid
,
2942 NDS_EDM_MISC_DBGER
, NDS_DBGER_CRST
) != ERROR_OK
)
2948 static int aice_usb_run(uint32_t coreid
)
2950 LOG_DEBUG("aice_usb_run");
2952 uint32_t dbger_value
;
2953 if (aice_read_misc(coreid
,
2954 NDS_EDM_MISC_DBGER
, &dbger_value
) != ERROR_OK
)
2957 if ((dbger_value
& NDS_DBGER_DEX
) != NDS_DBGER_DEX
) {
2958 LOG_WARNING("<-- TARGET WARNING! The debug target exited "
2959 "the debug mode unexpectedly. -->");
2963 /* restore r0 & r1 before free run */
2964 aice_restore_tmp_registers(coreid
);
2965 core_info
[coreid
].core_state
= AICE_TARGET_RUNNING
;
2968 aice_write_misc(coreid
, NDS_EDM_MISC_DBGER
,
2969 NDS_DBGER_CLEAR_ALL
);
2971 /** restore EDM registers */
2972 /** OpenOCD should restore EDM_CTL **before** to exit debug state.
2973 * Otherwise, following instruction will read wrong EDM_CTL value.
2975 * pc -> mfsr $p0, EDM_CTL (single step)
2979 aice_restore_edm_registers(coreid
);
2981 /** execute instructions in DIM */
2982 uint32_t instructions
[4] = {
2988 int result
= aice_execute_dim(coreid
, instructions
, 4);
2993 static int aice_usb_step(uint32_t coreid
)
2995 LOG_DEBUG("aice_usb_step");
2998 uint32_t ir0_reg_num
;
3000 if (is_v2_edm(coreid
) == true)
3001 /* V2 EDM will push interrupt stack as debug exception */
3007 aice_read_reg(coreid
, ir0_reg_num
, &ir0_value
);
3008 if ((ir0_value
& 0x800) == 0) {
3010 ir0_value
|= (0x01 << 11);
3011 aice_write_reg(coreid
, ir0_reg_num
, ir0_value
);
3014 if (ERROR_FAIL
== aice_usb_run(coreid
))
3018 enum aice_target_state_s state
;
3021 if (aice_usb_state(coreid
, &state
) != ERROR_OK
)
3024 if (AICE_TARGET_HALTED
== state
)
3029 then
= timeval_ms();
3032 if ((timeval_ms() - then
) > 1000)
3033 LOG_WARNING("Timeout (1000ms) waiting for halt to complete");
3041 aice_read_reg(coreid
, ir0_reg_num
, &ir0_value
);
3042 ir0_value
&= ~(0x01 << 11);
3043 aice_write_reg(coreid
, ir0_reg_num
, ir0_value
);
3048 static int aice_usb_read_mem_b_bus(uint32_t coreid
, uint32_t address
, uint32_t *data
)
3050 return aice_read_mem_b(coreid
, address
, data
);
3053 static int aice_usb_read_mem_h_bus(uint32_t coreid
, uint32_t address
, uint32_t *data
)
3055 return aice_read_mem_h(coreid
, address
, data
);
3058 static int aice_usb_read_mem_w_bus(uint32_t coreid
, uint32_t address
, uint32_t *data
)
3060 return aice_read_mem(coreid
, address
, data
);
3063 static int aice_usb_read_mem_b_dim(uint32_t coreid
, uint32_t address
, uint32_t *data
)
3066 uint32_t instructions
[4] = {
3073 aice_execute_dim(coreid
, instructions
, 4);
3075 aice_read_dtr(coreid
, &value
);
3076 *data
= value
& 0xFF;
3081 static int aice_usb_read_mem_h_dim(uint32_t coreid
, uint32_t address
, uint32_t *data
)
3084 uint32_t instructions
[4] = {
3091 aice_execute_dim(coreid
, instructions
, 4);
3093 aice_read_dtr(coreid
, &value
);
3094 *data
= value
& 0xFFFF;
3099 static int aice_usb_read_mem_w_dim(uint32_t coreid
, uint32_t address
, uint32_t *data
)
3101 uint32_t instructions
[4] = {
3108 aice_execute_dim(coreid
, instructions
, 4);
3110 aice_read_dtr(coreid
, data
);
3115 static int aice_usb_set_address_dim(uint32_t coreid
, uint32_t address
)
3117 uint32_t instructions
[4] = {
3118 SETHI(R0
, address
>> 12),
3119 ORI(R0
, R0
, address
& 0x00000FFF),
3124 return aice_execute_dim(coreid
, instructions
, 4);
3127 static int aice_usb_read_memory_unit(uint32_t coreid
, uint32_t addr
, uint32_t size
,
3128 uint32_t count
, uint8_t *buffer
)
3130 LOG_DEBUG("aice_usb_read_memory_unit, addr: 0x%08" PRIx32
3131 ", size: %" PRIu32
", count: %" PRIu32
"",
3134 if (NDS_MEMORY_ACC_CPU
== core_info
[coreid
].access_channel
)
3135 aice_usb_set_address_dim(coreid
, addr
);
3139 read_mem_func_t read_mem_func
;
3143 if (NDS_MEMORY_ACC_BUS
== core_info
[coreid
].access_channel
)
3144 read_mem_func
= aice_usb_read_mem_b_bus
;
3146 read_mem_func
= aice_usb_read_mem_b_dim
;
3148 for (i
= 0; i
< count
; i
++) {
3149 read_mem_func(coreid
, addr
, &value
);
3150 *buffer
++ = (uint8_t)value
;
3155 if (NDS_MEMORY_ACC_BUS
== core_info
[coreid
].access_channel
)
3156 read_mem_func
= aice_usb_read_mem_h_bus
;
3158 read_mem_func
= aice_usb_read_mem_h_dim
;
3160 for (i
= 0; i
< count
; i
++) {
3161 read_mem_func(coreid
, addr
, &value
);
3162 uint16_t svalue
= value
;
3163 memcpy(buffer
, &svalue
, sizeof(uint16_t));
3169 if (NDS_MEMORY_ACC_BUS
== core_info
[coreid
].access_channel
)
3170 read_mem_func
= aice_usb_read_mem_w_bus
;
3172 read_mem_func
= aice_usb_read_mem_w_dim
;
3174 for (i
= 0; i
< count
; i
++) {
3175 read_mem_func(coreid
, addr
, &value
);
3176 memcpy(buffer
, &value
, sizeof(uint32_t));
3186 static int aice_usb_write_mem_b_bus(uint32_t coreid
, uint32_t address
, uint32_t data
)
3188 return aice_write_mem_b(coreid
, address
, data
);
3191 static int aice_usb_write_mem_h_bus(uint32_t coreid
, uint32_t address
, uint32_t data
)
3193 return aice_write_mem_h(coreid
, address
, data
);
3196 static int aice_usb_write_mem_w_bus(uint32_t coreid
, uint32_t address
, uint32_t data
)
3198 return aice_write_mem(coreid
, address
, data
);
3201 static int aice_usb_write_mem_b_dim(uint32_t coreid
, uint32_t address
, uint32_t data
)
3203 uint32_t instructions
[4] = {
3210 aice_write_dtr(coreid
, data
& 0xFF);
3211 aice_execute_dim(coreid
, instructions
, 4);
3216 static int aice_usb_write_mem_h_dim(uint32_t coreid
, uint32_t address
, uint32_t data
)
3218 uint32_t instructions
[4] = {
3225 aice_write_dtr(coreid
, data
& 0xFFFF);
3226 aice_execute_dim(coreid
, instructions
, 4);
3231 static int aice_usb_write_mem_w_dim(uint32_t coreid
, uint32_t address
, uint32_t data
)
3233 uint32_t instructions
[4] = {
3240 aice_write_dtr(coreid
, data
);
3241 aice_execute_dim(coreid
, instructions
, 4);
3246 static int aice_usb_write_memory_unit(uint32_t coreid
, uint32_t addr
, uint32_t size
,
3247 uint32_t count
, const uint8_t *buffer
)
3249 LOG_DEBUG("aice_usb_write_memory_unit, addr: 0x%08" PRIx32
3250 ", size: %" PRIu32
", count: %" PRIu32
"",
3253 if (NDS_MEMORY_ACC_CPU
== core_info
[coreid
].access_channel
)
3254 aice_usb_set_address_dim(coreid
, addr
);
3257 write_mem_func_t write_mem_func
;
3261 if (NDS_MEMORY_ACC_BUS
== core_info
[coreid
].access_channel
)
3262 write_mem_func
= aice_usb_write_mem_b_bus
;
3264 write_mem_func
= aice_usb_write_mem_b_dim
;
3266 for (i
= 0; i
< count
; i
++) {
3267 write_mem_func(coreid
, addr
, *buffer
);
3273 if (NDS_MEMORY_ACC_BUS
== core_info
[coreid
].access_channel
)
3274 write_mem_func
= aice_usb_write_mem_h_bus
;
3276 write_mem_func
= aice_usb_write_mem_h_dim
;
3278 for (i
= 0; i
< count
; i
++) {
3280 memcpy(&value
, buffer
, sizeof(uint16_t));
3282 write_mem_func(coreid
, addr
, value
);
3288 if (NDS_MEMORY_ACC_BUS
== core_info
[coreid
].access_channel
)
3289 write_mem_func
= aice_usb_write_mem_w_bus
;
3291 write_mem_func
= aice_usb_write_mem_w_dim
;
3293 for (i
= 0; i
< count
; i
++) {
3295 memcpy(&value
, buffer
, sizeof(uint32_t));
3297 write_mem_func(coreid
, addr
, value
);
3307 static int aice_bulk_read_mem(uint32_t coreid
, uint32_t addr
, uint32_t count
,
3310 uint32_t packet_size
;
3313 packet_size
= (count
>= 0x100) ? 0x100 : count
;
3317 if (aice_write_misc(coreid
, NDS_EDM_MISC_SBAR
, addr
) != ERROR_OK
)
3320 if (aice_fastread_mem(coreid
, buffer
,
3321 packet_size
) != ERROR_OK
)
3324 buffer
+= (packet_size
* 4);
3325 addr
+= (packet_size
* 4);
3326 count
-= packet_size
;
3332 static int aice_bulk_write_mem(uint32_t coreid
, uint32_t addr
, uint32_t count
,
3333 const uint8_t *buffer
)
3335 uint32_t packet_size
;
3338 packet_size
= (count
>= 0x100) ? 0x100 : count
;
3342 if (aice_write_misc(coreid
, NDS_EDM_MISC_SBAR
, addr
| 1) != ERROR_OK
)
3345 if (aice_fastwrite_mem(coreid
, buffer
,
3346 packet_size
) != ERROR_OK
)
3349 buffer
+= (packet_size
* 4);
3350 addr
+= (packet_size
* 4);
3351 count
-= packet_size
;
3357 static int aice_usb_bulk_read_mem(uint32_t coreid
, uint32_t addr
,
3358 uint32_t length
, uint8_t *buffer
)
3360 LOG_DEBUG("aice_usb_bulk_read_mem, addr: 0x%08" PRIx32
", length: 0x%08" PRIx32
, addr
, length
);
3364 if (NDS_MEMORY_ACC_CPU
== core_info
[coreid
].access_channel
)
3365 aice_usb_set_address_dim(coreid
, addr
);
3367 if (NDS_MEMORY_ACC_CPU
== core_info
[coreid
].access_channel
)
3368 retval
= aice_usb_read_memory_unit(coreid
, addr
, 4, length
/ 4, buffer
);
3370 retval
= aice_bulk_read_mem(coreid
, addr
, length
/ 4, buffer
);
3375 static int aice_usb_bulk_write_mem(uint32_t coreid
, uint32_t addr
,
3376 uint32_t length
, const uint8_t *buffer
)
3378 LOG_DEBUG("aice_usb_bulk_write_mem, addr: 0x%08" PRIx32
", length: 0x%08" PRIx32
, addr
, length
);
3382 if (NDS_MEMORY_ACC_CPU
== core_info
[coreid
].access_channel
)
3383 aice_usb_set_address_dim(coreid
, addr
);
3385 if (NDS_MEMORY_ACC_CPU
== core_info
[coreid
].access_channel
)
3386 retval
= aice_usb_write_memory_unit(coreid
, addr
, 4, length
/ 4, buffer
);
3388 retval
= aice_bulk_write_mem(coreid
, addr
, length
/ 4, buffer
);
3393 static int aice_usb_read_debug_reg(uint32_t coreid
, uint32_t addr
, uint32_t *val
)
3395 if (AICE_TARGET_HALTED
== core_info
[coreid
].core_state
) {
3396 if (NDS_EDM_SR_EDMSW
== addr
) {
3397 *val
= core_info
[coreid
].edmsw_backup
;
3398 } else if (NDS_EDM_SR_EDM_DTR
== addr
) {
3399 if (core_info
[coreid
].target_dtr_valid
) {
3400 /* if EDM_DTR has read out, clear it. */
3401 *val
= core_info
[coreid
].target_dtr_backup
;
3402 core_info
[coreid
].edmsw_backup
&= (~0x1);
3403 core_info
[coreid
].target_dtr_valid
= false;
3410 return aice_read_edmsr(coreid
, addr
, val
);
3413 static int aice_usb_write_debug_reg(uint32_t coreid
, uint32_t addr
, const uint32_t val
)
3415 if (AICE_TARGET_HALTED
== core_info
[coreid
].core_state
) {
3416 if (NDS_EDM_SR_EDM_DTR
== addr
) {
3417 core_info
[coreid
].host_dtr_backup
= val
;
3418 core_info
[coreid
].edmsw_backup
|= 0x2;
3419 core_info
[coreid
].host_dtr_valid
= true;
3423 return aice_write_edmsr(coreid
, addr
, val
);
3426 static int aice_usb_memory_access(uint32_t coreid
, enum nds_memory_access channel
)
3428 LOG_DEBUG("aice_usb_memory_access, access channel: %u", channel
);
3430 core_info
[coreid
].access_channel
= channel
;
3435 static int aice_usb_memory_mode(uint32_t coreid
, enum nds_memory_select mem_select
)
3437 if (core_info
[coreid
].memory_select
== mem_select
)
3440 LOG_DEBUG("aice_usb_memory_mode, memory select: %u", mem_select
);
3442 core_info
[coreid
].memory_select
= mem_select
;
3444 if (NDS_MEMORY_SELECT_AUTO
!= core_info
[coreid
].memory_select
)
3445 aice_write_misc(coreid
, NDS_EDM_MISC_ACC_CTL
,
3446 core_info
[coreid
].memory_select
- 1);
3448 aice_write_misc(coreid
, NDS_EDM_MISC_ACC_CTL
,
3449 NDS_MEMORY_SELECT_MEM
- 1);
3454 static int aice_usb_read_tlb(uint32_t coreid
, uint32_t virtual_address
,
3455 uint32_t *physical_address
)
3457 LOG_DEBUG("aice_usb_read_tlb, virtual address: 0x%08" PRIx32
, virtual_address
);
3459 uint32_t instructions
[4];
3460 uint32_t probe_result
;
3463 uint32_t access_page_size
;
3464 uint32_t virtual_offset
;
3465 uint32_t physical_page_number
;
3467 aice_write_dtr(coreid
, virtual_address
);
3469 /* probe TLB first */
3470 instructions
[0] = MFSR_DTR(R0
);
3471 instructions
[1] = TLBOP_TARGET_PROBE(R1
, R0
);
3472 instructions
[2] = DSB
;
3473 instructions
[3] = BEQ_MINUS_12
;
3474 aice_execute_dim(coreid
, instructions
, 4);
3476 aice_read_reg(coreid
, R1
, &probe_result
);
3478 if (probe_result
& 0x80000000)
3481 /* read TLB entry */
3482 aice_write_dtr(coreid
, probe_result
& 0x7FF);
3484 /* probe TLB first */
3485 instructions
[0] = MFSR_DTR(R0
);
3486 instructions
[1] = TLBOP_TARGET_READ(R0
);
3487 instructions
[2] = DSB
;
3488 instructions
[3] = BEQ_MINUS_12
;
3489 aice_execute_dim(coreid
, instructions
, 4);
3491 /* TODO: it should backup mr3, mr4 */
3492 aice_read_reg(coreid
, MR3
, &value_mr3
);
3493 aice_read_reg(coreid
, MR4
, &value_mr4
);
3495 access_page_size
= value_mr4
& 0xF;
3496 if (0 == access_page_size
) { /* 4K page */
3497 virtual_offset
= virtual_address
& 0x00000FFF;
3498 physical_page_number
= value_mr3
& 0xFFFFF000;
3499 } else if (1 == access_page_size
) { /* 8K page */
3500 virtual_offset
= virtual_address
& 0x00001FFF;
3501 physical_page_number
= value_mr3
& 0xFFFFE000;
3502 } else if (5 == access_page_size
) { /* 1M page */
3503 virtual_offset
= virtual_address
& 0x000FFFFF;
3504 physical_page_number
= value_mr3
& 0xFFF00000;
3509 *physical_address
= physical_page_number
| virtual_offset
;
3514 static int aice_usb_init_cache(uint32_t coreid
)
3516 LOG_DEBUG("aice_usb_init_cache");
3521 aice_read_reg(coreid
, CR1
, &value_cr1
);
3522 aice_read_reg(coreid
, CR2
, &value_cr2
);
3524 struct cache_info
*icache
= &core_info
[coreid
].icache
;
3526 icache
->set
= value_cr1
& 0x7;
3527 icache
->log2_set
= icache
->set
+ 6;
3528 icache
->set
= 64 << icache
->set
;
3529 icache
->way
= ((value_cr1
>> 3) & 0x7) + 1;
3530 icache
->line_size
= (value_cr1
>> 6) & 0x7;
3531 if (icache
->line_size
!= 0) {
3532 icache
->log2_line_size
= icache
->line_size
+ 2;
3533 icache
->line_size
= 8 << (icache
->line_size
- 1);
3535 icache
->log2_line_size
= 0;
3538 LOG_DEBUG("\ticache set: %" PRIu32
", way: %" PRIu32
", line size: %" PRIu32
", "
3539 "log2(set): %" PRIu32
", log2(line_size): %" PRIu32
"",
3540 icache
->set
, icache
->way
, icache
->line_size
,
3541 icache
->log2_set
, icache
->log2_line_size
);
3543 struct cache_info
*dcache
= &core_info
[coreid
].dcache
;
3545 dcache
->set
= value_cr2
& 0x7;
3546 dcache
->log2_set
= dcache
->set
+ 6;
3547 dcache
->set
= 64 << dcache
->set
;
3548 dcache
->way
= ((value_cr2
>> 3) & 0x7) + 1;
3549 dcache
->line_size
= (value_cr2
>> 6) & 0x7;
3550 if (dcache
->line_size
!= 0) {
3551 dcache
->log2_line_size
= dcache
->line_size
+ 2;
3552 dcache
->line_size
= 8 << (dcache
->line_size
- 1);
3554 dcache
->log2_line_size
= 0;
3557 LOG_DEBUG("\tdcache set: %" PRIu32
", way: %" PRIu32
", line size: %" PRIu32
", "
3558 "log2(set): %" PRIu32
", log2(line_size): %" PRIu32
"",
3559 dcache
->set
, dcache
->way
, dcache
->line_size
,
3560 dcache
->log2_set
, dcache
->log2_line_size
);
3562 core_info
[coreid
].cache_init
= true;
3567 static int aice_usb_dcache_inval_all(uint32_t coreid
)
3569 LOG_DEBUG("aice_usb_dcache_inval_all");
3573 uint32_t cache_index
;
3574 uint32_t instructions
[4];
3576 instructions
[0] = MFSR_DTR(R0
);
3577 instructions
[1] = L1D_IX_INVAL(R0
);
3578 instructions
[2] = DSB
;
3579 instructions
[3] = BEQ_MINUS_12
;
3581 struct cache_info
*dcache
= &core_info
[coreid
].dcache
;
3583 for (set_index
= 0; set_index
< dcache
->set
; set_index
++) {
3584 for (way_index
= 0; way_index
< dcache
->way
; way_index
++) {
3585 cache_index
= (way_index
<< (dcache
->log2_set
+ dcache
->log2_line_size
)) |
3586 (set_index
<< dcache
->log2_line_size
);
3588 if (ERROR_OK
!= aice_write_dtr(coreid
, cache_index
))
3591 if (ERROR_OK
!= aice_execute_dim(coreid
, instructions
, 4))
3599 static int aice_usb_dcache_va_inval(uint32_t coreid
, uint32_t address
)
3601 LOG_DEBUG("aice_usb_dcache_va_inval");
3603 uint32_t instructions
[4];
3605 aice_write_dtr(coreid
, address
);
3607 instructions
[0] = MFSR_DTR(R0
);
3608 instructions
[1] = L1D_VA_INVAL(R0
);
3609 instructions
[2] = DSB
;
3610 instructions
[3] = BEQ_MINUS_12
;
3612 return aice_execute_dim(coreid
, instructions
, 4);
3615 static int aice_usb_dcache_wb_all(uint32_t coreid
)
3617 LOG_DEBUG("aice_usb_dcache_wb_all");
3621 uint32_t cache_index
;
3622 uint32_t instructions
[4];
3624 instructions
[0] = MFSR_DTR(R0
);
3625 instructions
[1] = L1D_IX_WB(R0
);
3626 instructions
[2] = DSB
;
3627 instructions
[3] = BEQ_MINUS_12
;
3629 struct cache_info
*dcache
= &core_info
[coreid
].dcache
;
3631 for (set_index
= 0; set_index
< dcache
->set
; set_index
++) {
3632 for (way_index
= 0; way_index
< dcache
->way
; way_index
++) {
3633 cache_index
= (way_index
<< (dcache
->log2_set
+ dcache
->log2_line_size
)) |
3634 (set_index
<< dcache
->log2_line_size
);
3636 if (ERROR_OK
!= aice_write_dtr(coreid
, cache_index
))
3639 if (ERROR_OK
!= aice_execute_dim(coreid
, instructions
, 4))
3647 static int aice_usb_dcache_va_wb(uint32_t coreid
, uint32_t address
)
3649 LOG_DEBUG("aice_usb_dcache_va_wb");
3651 uint32_t instructions
[4];
3653 aice_write_dtr(coreid
, address
);
3655 instructions
[0] = MFSR_DTR(R0
);
3656 instructions
[1] = L1D_VA_WB(R0
);
3657 instructions
[2] = DSB
;
3658 instructions
[3] = BEQ_MINUS_12
;
3660 return aice_execute_dim(coreid
, instructions
, 4);
3663 static int aice_usb_icache_inval_all(uint32_t coreid
)
3665 LOG_DEBUG("aice_usb_icache_inval_all");
3669 uint32_t cache_index
;
3670 uint32_t instructions
[4];
3672 instructions
[0] = MFSR_DTR(R0
);
3673 instructions
[1] = L1I_IX_INVAL(R0
);
3674 instructions
[2] = ISB
;
3675 instructions
[3] = BEQ_MINUS_12
;
3677 struct cache_info
*icache
= &core_info
[coreid
].icache
;
3679 for (set_index
= 0; set_index
< icache
->set
; set_index
++) {
3680 for (way_index
= 0; way_index
< icache
->way
; way_index
++) {
3681 cache_index
= (way_index
<< (icache
->log2_set
+ icache
->log2_line_size
)) |
3682 (set_index
<< icache
->log2_line_size
);
3684 if (ERROR_OK
!= aice_write_dtr(coreid
, cache_index
))
3687 if (ERROR_OK
!= aice_execute_dim(coreid
, instructions
, 4))
3695 static int aice_usb_icache_va_inval(uint32_t coreid
, uint32_t address
)
3697 LOG_DEBUG("aice_usb_icache_va_inval");
3699 uint32_t instructions
[4];
3701 aice_write_dtr(coreid
, address
);
3703 instructions
[0] = MFSR_DTR(R0
);
3704 instructions
[1] = L1I_VA_INVAL(R0
);
3705 instructions
[2] = ISB
;
3706 instructions
[3] = BEQ_MINUS_12
;
3708 return aice_execute_dim(coreid
, instructions
, 4);
3711 static int aice_usb_cache_ctl(uint32_t coreid
, uint32_t subtype
, uint32_t address
)
3713 LOG_DEBUG("aice_usb_cache_ctl");
3717 if (core_info
[coreid
].cache_init
== false)
3718 aice_usb_init_cache(coreid
);
3721 case AICE_CACHE_CTL_L1D_INVALALL
:
3722 result
= aice_usb_dcache_inval_all(coreid
);
3724 case AICE_CACHE_CTL_L1D_VA_INVAL
:
3725 result
= aice_usb_dcache_va_inval(coreid
, address
);
3727 case AICE_CACHE_CTL_L1D_WBALL
:
3728 result
= aice_usb_dcache_wb_all(coreid
);
3730 case AICE_CACHE_CTL_L1D_VA_WB
:
3731 result
= aice_usb_dcache_va_wb(coreid
, address
);
3733 case AICE_CACHE_CTL_L1I_INVALALL
:
3734 result
= aice_usb_icache_inval_all(coreid
);
3736 case AICE_CACHE_CTL_L1I_VA_INVAL
:
3737 result
= aice_usb_icache_va_inval(coreid
, address
);
3740 result
= ERROR_FAIL
;
3747 static int aice_usb_set_retry_times(uint32_t a_retry_times
)
3749 aice_max_retry_times
= a_retry_times
;
3753 static int aice_usb_program_edm(uint32_t coreid
, char *command_sequence
)
3758 uint32_t data_value
;
3762 command_str
= strtok(command_sequence
, ";");
3763 if (command_str
== NULL
)
3768 /* process one command */
3769 while (command_str
[i
] == ' ' ||
3770 command_str
[i
] == '\n' ||
3771 command_str
[i
] == '\r' ||
3772 command_str
[i
] == '\t')
3775 /* skip ' ', '\r', '\n', '\t' */
3776 command_str
= command_str
+ i
;
3778 if (strncmp(command_str
, "write_misc", 10) == 0) {
3779 reg_name_0
= strstr(command_str
, "gen_port0");
3780 reg_name_1
= strstr(command_str
, "gen_port1");
3782 if (reg_name_0
!= NULL
) {
3783 data_value
= strtoul(reg_name_0
+ 9, NULL
, 0);
3785 if (aice_write_misc(coreid
,
3786 NDS_EDM_MISC_GEN_PORT0
, data_value
) != ERROR_OK
)
3789 } else if (reg_name_1
!= NULL
) {
3790 data_value
= strtoul(reg_name_1
+ 9, NULL
, 0);
3792 if (aice_write_misc(coreid
,
3793 NDS_EDM_MISC_GEN_PORT1
, data_value
) != ERROR_OK
)
3796 LOG_ERROR("program EDM, unsupported misc register: %s", command_str
);
3799 LOG_ERROR("program EDM, unsupported command: %s", command_str
);
3802 /* update command_str */
3803 command_str
= strtok(NULL
, ";");
3805 } while (command_str
!= NULL
);
3810 static int aice_usb_set_command_mode(enum aice_command_mode command_mode
)
3812 int retval
= ERROR_OK
;
3814 /* flush usb_packets_buffer as users change mode */
3815 retval
= aice_usb_packet_flush();
3817 if (AICE_COMMAND_MODE_BATCH
== command_mode
) {
3818 /* reset batch buffer */
3819 aice_command_mode
= AICE_COMMAND_MODE_NORMAL
;
3820 retval
= aice_write_ctrl(AICE_WRITE_CTRL_BATCH_CMD_BUF0_CTRL
, 0x40000);
3823 aice_command_mode
= command_mode
;
3828 static int aice_usb_execute(uint32_t coreid
, uint32_t *instructions
,
3829 uint32_t instruction_num
)
3832 uint8_t current_instruction_num
;
3833 uint32_t dim_instructions
[4] = {NOP
, NOP
, NOP
, BEQ_MINUS_12
};
3835 /* To execute 4 instructions as a special case */
3836 if (instruction_num
== 4)
3837 return aice_execute_dim(coreid
, instructions
, 4);
3839 for (i
= 0 ; i
< instruction_num
; i
+= 3) {
3840 if (instruction_num
- i
< 3) {
3841 current_instruction_num
= instruction_num
- i
;
3842 for (j
= current_instruction_num
; j
< 3 ; j
++)
3843 dim_instructions
[j
] = NOP
;
3845 current_instruction_num
= 3;
3848 memcpy(dim_instructions
, instructions
+ i
,
3849 current_instruction_num
* sizeof(uint32_t));
3852 if (aice_write_dim(coreid
,
3857 /** clear DBGER.DPED */
3858 if (aice_write_misc(coreid
,
3859 NDS_EDM_MISC_DBGER
, NDS_DBGER_DPED
) != ERROR_OK
)
3863 if (aice_do_execute(coreid
) != ERROR_OK
)
3866 /** check DBGER.DPED */
3867 if (aice_check_dbger(coreid
, NDS_DBGER_DPED
) != ERROR_OK
) {
3869 LOG_ERROR("<-- TARGET ERROR! Debug operations do not finish properly:"
3870 "0x%08" PRIx32
" 0x%08" PRIx32
" 0x%08" PRIx32
" 0x%08" PRIx32
". -->",
3871 dim_instructions
[0],
3872 dim_instructions
[1],
3873 dim_instructions
[2],
3874 dim_instructions
[3]);
3882 static int aice_usb_set_custom_srst_script(const char *script
)
3884 custom_srst_script
= strdup(script
);
3889 static int aice_usb_set_custom_trst_script(const char *script
)
3891 custom_trst_script
= strdup(script
);
3896 static int aice_usb_set_custom_restart_script(const char *script
)
3898 custom_restart_script
= strdup(script
);
3903 static int aice_usb_set_count_to_check_dbger(uint32_t count_to_check
)
3905 aice_count_to_check_dbger
= count_to_check
;
3910 static int aice_usb_set_data_endian(uint32_t coreid
,
3911 enum aice_target_endian target_data_endian
)
3913 data_endian
= target_data_endian
;
3918 static int fill_profiling_batch_commands(uint32_t coreid
, uint32_t reg_no
)
3920 uint32_t dim_instructions
[4];
3922 aice_usb_set_command_mode(AICE_COMMAND_MODE_BATCH
);
3925 if (aice_write_misc(coreid
, NDS_EDM_MISC_EDM_CMDR
, 0) != ERROR_OK
)
3929 dim_instructions
[0] = MTSR_DTR(0);
3930 dim_instructions
[1] = DSB
;
3931 dim_instructions
[2] = NOP
;
3932 dim_instructions
[3] = BEQ_MINUS_12
;
3933 if (aice_write_dim(coreid
, dim_instructions
, 4) != ERROR_OK
)
3935 aice_read_dtr_to_buffer(coreid
, AICE_BATCH_DATA_BUFFER_0
);
3938 if (NDS32_REG_TYPE_GPR
== nds32_reg_type(reg_no
)) {
3939 /* general registers */
3940 dim_instructions
[0] = MTSR_DTR(reg_no
);
3941 dim_instructions
[1] = DSB
;
3942 dim_instructions
[2] = NOP
;
3943 dim_instructions
[3] = BEQ_MINUS_12
;
3944 } else if (NDS32_REG_TYPE_SPR
== nds32_reg_type(reg_no
)) {
3945 /* user special registers */
3946 dim_instructions
[0] = MFUSR_G0(0, nds32_reg_sr_index(reg_no
));
3947 dim_instructions
[1] = MTSR_DTR(0);
3948 dim_instructions
[2] = DSB
;
3949 dim_instructions
[3] = BEQ_MINUS_12
;
3950 } else { /* system registers */
3951 dim_instructions
[0] = MFSR(0, nds32_reg_sr_index(reg_no
));
3952 dim_instructions
[1] = MTSR_DTR(0);
3953 dim_instructions
[2] = DSB
;
3954 dim_instructions
[3] = BEQ_MINUS_12
;
3956 if (aice_write_dim(coreid
, dim_instructions
, 4) != ERROR_OK
)
3958 aice_read_dtr_to_buffer(coreid
, AICE_BATCH_DATA_BUFFER_1
);
3961 aice_write_dtr_from_buffer(coreid
, AICE_BATCH_DATA_BUFFER_0
);
3962 dim_instructions
[0] = MFSR_DTR(0);
3963 dim_instructions
[1] = DSB
;
3964 dim_instructions
[2] = NOP
;
3965 dim_instructions
[3] = IRET
; /* free run */
3966 if (aice_write_dim(coreid
, dim_instructions
, 4) != ERROR_OK
)
3969 aice_command_mode
= AICE_COMMAND_MODE_NORMAL
;
3971 /* use BATCH_BUFFER_WRITE to fill command-batch-buffer */
3972 if (aice_batch_buffer_write(AICE_BATCH_COMMAND_BUFFER_0
,
3973 usb_out_packets_buffer
,
3974 (usb_out_packets_buffer_length
+ 3) / 4) != ERROR_OK
)
3977 usb_out_packets_buffer_length
= 0;
3978 usb_in_packets_buffer_length
= 0;
3983 static int aice_usb_profiling(uint32_t coreid
, uint32_t interval
, uint32_t iteration
,
3984 uint32_t reg_no
, uint32_t *samples
, uint32_t *num_samples
)
3986 uint32_t iteration_count
;
3987 uint32_t this_iteration
;
3988 int retval
= ERROR_OK
;
3989 const uint32_t MAX_ITERATION
= 250;
3994 if (aice_write_ctrl(AICE_WRITE_CTRL_BATCH_DIM_SIZE
, 4) != ERROR_OK
)
3997 /* Use AICE_BATCH_DATA_BUFFER_0 to read/write $DTR.
3998 * Set it to circular buffer */
3999 if (aice_write_ctrl(AICE_WRITE_CTRL_BATCH_DATA_BUF0_CTRL
, 0xC0000) != ERROR_OK
)
4002 fill_profiling_batch_commands(coreid
, reg_no
);
4004 iteration_count
= 0;
4005 while (iteration_count
< iteration
) {
4006 if (iteration
- iteration_count
< MAX_ITERATION
)
4007 this_iteration
= iteration
- iteration_count
;
4009 this_iteration
= MAX_ITERATION
;
4011 /* set number of iterations */
4012 uint32_t val_iteration
;
4013 val_iteration
= interval
<< 16 | this_iteration
;
4014 if (aice_write_ctrl(AICE_WRITE_CTRL_BATCH_ITERATION
,
4015 val_iteration
) != ERROR_OK
) {
4016 retval
= ERROR_FAIL
;
4020 /* init AICE_WRITE_CTRL_BATCH_DATA_BUF1_CTRL to store $PC */
4021 if (aice_write_ctrl(AICE_WRITE_CTRL_BATCH_DATA_BUF1_CTRL
,
4022 0x40000) != ERROR_OK
) {
4023 retval
= ERROR_FAIL
;
4027 aice_usb_run(coreid
);
4029 /* enable BATCH command */
4030 if (aice_write_ctrl(AICE_WRITE_CTRL_BATCH_CTRL
,
4031 0x80000000) != ERROR_OK
) {
4032 aice_usb_halt(coreid
);
4033 retval
= ERROR_FAIL
;
4037 /* wait a while (AICE bug, workaround) */
4038 alive_sleep(this_iteration
);
4042 uint32_t batch_status
;
4046 aice_read_ctrl(AICE_READ_CTRL_BATCH_STATUS
, &batch_status
);
4048 if (batch_status
& 0x1) {
4050 } else if (batch_status
& 0xE) {
4051 aice_usb_halt(coreid
);
4052 retval
= ERROR_FAIL
;
4062 aice_usb_halt(coreid
);
4064 /* get samples from batch data buffer */
4065 if (aice_batch_buffer_read(AICE_BATCH_DATA_BUFFER_1
,
4066 samples
+ iteration_count
, this_iteration
) != ERROR_OK
) {
4067 retval
= ERROR_FAIL
;
4071 iteration_count
+= this_iteration
;
4075 *num_samples
= iteration_count
;
4081 struct aice_port_api_s aice_usb_api
= {
4083 .open
= aice_open_device
,
4085 .close
= aice_usb_close
,
4087 .idcode
= aice_usb_idcode
,
4089 .state
= aice_usb_state
,
4091 .reset
= aice_usb_reset
,
4093 .assert_srst
= aice_usb_assert_srst
,
4095 .run
= aice_usb_run
,
4097 .halt
= aice_usb_halt
,
4099 .step
= aice_usb_step
,
4101 .read_reg
= aice_usb_read_reg
,
4103 .write_reg
= aice_usb_write_reg
,
4105 .read_reg_64
= aice_usb_read_reg_64
,
4107 .write_reg_64
= aice_usb_write_reg_64
,
4109 .read_mem_unit
= aice_usb_read_memory_unit
,
4111 .write_mem_unit
= aice_usb_write_memory_unit
,
4113 .read_mem_bulk
= aice_usb_bulk_read_mem
,
4115 .write_mem_bulk
= aice_usb_bulk_write_mem
,
4117 .read_debug_reg
= aice_usb_read_debug_reg
,
4119 .write_debug_reg
= aice_usb_write_debug_reg
,
4121 .set_jtag_clock
= aice_usb_set_jtag_clock
,
4123 .memory_access
= aice_usb_memory_access
,
4125 .memory_mode
= aice_usb_memory_mode
,
4127 .read_tlb
= aice_usb_read_tlb
,
4129 .cache_ctl
= aice_usb_cache_ctl
,
4131 .set_retry_times
= aice_usb_set_retry_times
,
4133 .program_edm
= aice_usb_program_edm
,
4135 .set_command_mode
= aice_usb_set_command_mode
,
4137 .execute
= aice_usb_execute
,
4139 .set_custom_srst_script
= aice_usb_set_custom_srst_script
,
4141 .set_custom_trst_script
= aice_usb_set_custom_trst_script
,
4143 .set_custom_restart_script
= aice_usb_set_custom_restart_script
,
4145 .set_count_to_check_dbger
= aice_usb_set_count_to_check_dbger
,
4147 .set_data_endian
= aice_usb_set_data_endian
,
4149 .profiling
= aice_usb_profiling
,