Debian Codegraph @

src/jtagio.cc

raw symbols SCIP
1 /*
2 * avarice - The "avarice" program.
3 * Copyright (C) 2001 Scott Finneran
4 * Copyright (C) 2002, 2003, 2004 Intel Corporation
5 * Copyright (C) 2005 Joerg Wunsch
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License Version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
19 *
20 * This file contains functions for interfacing with the JTAG box.
21 *
22 * $Id: jtagio.cc 385 2020-08-31 22:53:49Z joerg_wunsch $
23 */
24
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32 #include <termios.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <errno.h>
36
37 #include "avarice.h"
38 #include "jtag.h"
39 #include "jtag1.h"
40
41 /** Send a command to the jtag, and check result.
42
43 Increase *tries, abort if reaches MAX_JTAG_COMM_ATTEMPS
44
45 Reads first response byte. If no response is received within
46 JTAG_RESPONSE_TIMEOUT, returns false. If response byte is
47 JTAG_R_RESP_OK returns true, otherwise returns false.
48 **/
49
50 SendResult jtag1::sendJtagCommand(uchar *command, int commandSize, int *tries)
51 {
52 if ((*tries)++ >= MAX_JTAG_COMM_ATTEMPS)
53 throw jtag_exception("JTAG communication failed");
54
55 debugOut("\ncommand[%c, %d]: ", command[0], *tries);
56
57 for (int i = 0; i < commandSize; i++)
58 debugOut("%.2X ", command[i]);
59
60 debugOut("\n");
61
62 // before writing, clean up any "unfinished business".
63 if (tcflush(jtagBox, TCIFLUSH) < 0)
64 throw jtag_exception();
65
66 int count = safewrite(command, commandSize);
67 if (count < 0)
68 throw jtag_exception();
69 else if (count != commandSize)
70 // this shouldn't happen
71 throw jtag_exception();
72
73 // And wait for all characters to go to the JTAG box.... can't hurt!
74 if (tcdrain(jtagBox) < 0)
75 throw jtag_exception();
76
77 // We should get JTAG_R_OK, but we might get JTAG_R_INFO too (we just
78 // ignore it)
79 for (;;)
80 {
81 uchar ok;
82 count = timeout_read(&ok, 1, JTAG_RESPONSE_TIMEOUT);
83 if (count < 0)
84 throw jtag_exception();
85
86 // timed out
87 if (count == 0)
88 {
89 debugOut("Timed out.\n");
90 return send_failed;
91 }
92
93 switch (ok)
94 {
95 case JTAG_R_OK: return send_ok;
96 case JTAG_R_INFO:
97 unsigned char infobuf[2];
98
99 /* An info ("IDR dirty") response. Ignore it. */
100 debugOut("Info response: ");
101 count = timeout_read(infobuf, 2, JTAG_RESPONSE_TIMEOUT);
102 for (int i = 0; i < count; i++)
103 {
104 debugOut("%.2X ", infobuf[i]);
105 }
106 debugOut("\n");
107 if (count != 2 || infobuf[1] != JTAG_R_OK)
108 return send_failed;
109 else
110 return (SendResult)(mcu_data + infobuf[0]);
111 break;
112 default:
113 debugOut("Out of sync, reponse was `%02x'\n", ok);
114 return send_failed;
115 }
116 }
117 }
118
119 /** Get a 'responseSize' byte response from the JTAG ICE
120
121 Returns NULL if no bytes received for JTAG_COMM_TIMEOUT microseconds
122 Returns a dynamically allocated buffer containing the reponse (caller
123 must free) otherwise
124 **/
125 uchar *jtag1::getJtagResponse(int responseSize)
126 {
127 uchar *response;
128 int numCharsRead;
129
130 // Increase by 1 because of the zero termination.
131 //
132 // note: IT IS THE CALLERS RESPONSIBILITY TO delete() THIS.
133 response = new uchar[responseSize + 1];
134 response[responseSize] = '\0';
135
136 numCharsRead = timeout_read(response, responseSize,
137 JTAG_RESPONSE_TIMEOUT);
138 if (numCharsRead < 0)
139 throw jtag_exception();
140
141 debugOut("response: ");
142 for (int i = 0; i < numCharsRead; i++)
143 {
144 debugOut("%.2X ", response[i]);
145 }
146 debugOut("\n");
147
148 if (numCharsRead < responseSize) // timeout problem
149 {
150 debugOut("Timed Out (partial response)\n");
151 delete [] response;
152 return NULL;
153 }
154
155 return response;
156 }
157
158 uchar *jtag1::doJtagCommand(uchar *command, int commandSize, int responseSize)
159 {
160 int tryCount = 0;
161
162 // Send command until we get RESP_OK
163 for (;;)
164 {
165 uchar *response;
166 static uchar sync[] = { ' ' };
167 static uchar stop[] = { 'S', JTAG_EOM };
168
169 switch (sendJtagCommand(command, commandSize, &tryCount))
170 {
171 case send_ok:
172 response = getJtagResponse(responseSize);
173 if (response == NULL)
174 throw jtag_exception();
175 return response;
176 case send_failed:
177 // We're out of sync. Attempt to resync.
178 while (sendJtagCommand(sync, sizeof sync, &tryCount) != send_ok)
179 ;
180 break;
181 default:
182 /* "IDR dirty", aka I/O debug register dirty, aka we got some
183 data from the target processor. This seems to be
184 indefinitely repeated if we don't do anything. Asking for
185 the sign on seems to shut it up, so we do that. (another
186 option is to do a 'forced stop' ('F' command), but that is a
187 bit more intrusive --- it should be ok, as we currently only
188 send commands to stopped targets, but...) */
189 if (sendJtagCommand(stop, sizeof stop, &tryCount) == send_ok)
190 getJtagResponse(8);
191 break;
192 }
193 }
194
195 }
196
197 bool jtag1::doSimpleJtagCommand(unsigned char cmd, int responseSize)
198 {
199 uchar *response;
200 uchar command[] = { cmd, JTAG_EOM };
201 bool result;
202
203 response = doJtagCommand(command, sizeof command, responseSize);
204 result = responseSize == 0 || response[responseSize - 1] == JTAG_R_OK;
205
206 delete [] response;
207
208 return result;
209 }
210
211
212 /** Set PC and JTAG ICE bitrate to BIT_RATE_xxx specified by 'newBitRate' **/
213 void jtag1::changeBitRate(int newBitRate)
214 {
215 uchar jtagrate;
216
217 switch (newBitRate) {
218 case 9600:
219 jtagrate = BIT_RATE_9600;
220 break;
221 case 19200:
222 jtagrate = BIT_RATE_19200;
223 break;
224 case 38400:
225 jtagrate = BIT_RATE_38400;
226 break;
227 case 57600:
228 jtagrate = BIT_RATE_57600;
229 break;
230 case 115200:
231 jtagrate = BIT_RATE_115200;
232 break;
233 default:
234 return;
235 }
236 setJtagParameter(JTAG_P_BITRATE, jtagrate);
237 changeLocalBitRate(newBitRate);
238 }
239
240 /** Set the JTAG ICE device descriptor data for specified device type **/
241 void jtag1::setDeviceDescriptor(jtag_device_def_type *dev)
242 {
243 uchar *response = NULL;
244 uchar *command = (uchar *)(&dev->dev_desc1);
245
246 response = doJtagCommand(command, sizeof dev->dev_desc1, 1);
247 if (response[0] != JTAG_R_OK)
248 throw jtag_exception ("JTAG ICE: Failed to set device description");
249
250 delete [] response;
251 }
252
253 /** Check for emulator using the 'S' command *without* retries
254 (used at startup to check sync only) **/
255 bool jtag1::checkForEmulator(void)
256 {
257 uchar *response;
258 uchar command[] = { 'S', JTAG_EOM };
259 bool result;
260 int tries = 0;
261
262 if (!sendJtagCommand(command, sizeof command, &tries))
263 return false;
264
265 response = getJtagResponse(8);
266 result = response && !strcmp((char *)response, "AVRNOCDA");
267
268 delete [] response;
269
270 return result;
271 }
272
273 /** Attempt to synchronise with JTAG at specified bitrate **/
274 bool jtag1::synchroniseAt(int bitrate)
275 {
276 debugOut("Attempting synchronisation at bitrate %d\n", bitrate);
277
278 changeLocalBitRate(bitrate);
279
280 int tries = 0;
281 while (tries < MAX_JTAG_SYNC_ATTEMPS)
282 {
283 // 'S ' works if this is the first string sent after power up.
284 // But if another char is sent, the JTAG seems to go in to some
285 // internal mode. 'SE ' takes it out of there, apparently (sometimes
286 // 'E ' is enough, but not always...)
287 sendJtagCommand((uchar *)"SE ", 4, &tries);
288 usleep(2 * JTAG_COMM_TIMEOUT); // let rest of response come before we ignore it
289 if (tcflush(jtagBox, TCIFLUSH) < 0)
290 throw jtag_exception();
291 if (checkForEmulator())
292 return true;
293 }
294 return false;
295 }
296
297 /** Attempt to synchronise with JTAG ICE at all possible bit rates **/
298 void jtag1::startJtagLink(void)
299 {
300 static int bitrates[] =
301 { 115200, 19200, 57600, 38400, 9600 };
302
303 for (unsigned int i = 0; i < sizeof bitrates / sizeof *bitrates; i++)
304 if (synchroniseAt(bitrates[i]))
305 return;
306
307 throw jtag_exception("Failed to synchronise with the JTAG ICE (is it connected and powered?)");
308 }
309
310 /** Device automatic configuration
311 Determines the device being controlled by the JTAG ICE and configures
312 the system accordingly.
313
314 May be overridden by command line parameter.
315
316 */
317 void jtag1::deviceAutoConfig(void)
318 {
319 unsigned int device_id;
320 jtag_device_def_type *pDevice = deviceDefinitions;
321
322 // Auto config
323 debugOut("Automatic device detection: ");
324
325 /* Set daisy chain information */
326 configDaisyChain();
327
328 /* Read in the JTAG device ID to determine device */
329 device_id = getJtagParameter(JTAG_P_JTAGID_BYTE0);
330 /* Reading the first byte resumes the program (weird, no?) */
331 interruptProgram();
332 device_id += (getJtagParameter(JTAG_P_JTAGID_BYTE1) << 8) +
333 (getJtagParameter(JTAG_P_JTAGID_BYTE2) << 16) +
334 (getJtagParameter(JTAG_P_JTAGID_BYTE3) << 24);
335
336
337 debugOut("JTAG id = 0x%0X : Ver = 0x%0x : Device = 0x%0x : Manuf = 0x%0x\n",
338 device_id,
339 (device_id & 0xF0000000) >> 28,
340 (device_id & 0x0FFFF000) >> 12,
341 (device_id & 0x00000FFE) >> 1);
342
343 device_id = (device_id & 0x0FFFF000) >> 12;
344 statusOut("Reported JTAG device ID: 0x%0X\n", device_id);
345
346 if (device_name == 0)
347 {
348 while (pDevice->name)
349 {
350 if (pDevice->device_id == device_id)
351 break;
352
353 pDevice++;
354 }
355 if ((pDevice->device_flags & DEVFL_MKII_ONLY) != 0)
356 {
357 fprintf(stderr, "Device is not supported by JTAG ICE mkI");
358 throw jtag_exception();
359 }
360 if (pDevice->name == 0)
361 {
362 unknownDevice(device_id);
363 throw jtag_exception();
364 }
365 }
366 else
367 {
368 debugOut("Looking for device: %s\n", device_name);
369
370 while (pDevice->name)
371 {
372 if (strcasecmp(pDevice->name, device_name) == 0)
373 break;
374
375 pDevice++;
376 }
377 if ((pDevice->device_flags & DEVFL_MKII_ONLY) != 0)
378 {
379 fprintf(stderr, "Device is not supported by JTAG ICE mkI");
380 throw jtag_exception();
381 }
382 if (pDevice->name == 0)
383 {
384 unknownDevice(device_id, false);
385 throw jtag_exception();
386 }
387 }
388
389 if (device_name)
390 {
391 if (device_id != pDevice->device_id)
392 {
393 statusOut("Configured for device ID: 0x%0X %s -- FORCED with %s\n",
394 pDevice->device_id, pDevice->name, device_name);
395 }
396 else
397 {
398 statusOut("Configured for device ID: 0x%0X %s -- Matched with "
399 "%s\n", pDevice->device_id, pDevice->name, device_name);
400 }
401 }
402 else
403 {
404 statusOut("Configured for device ID: 0x%0X %s\n",
405 pDevice->device_id, pDevice->name);
406 }
407
408 device_name = (char*)pDevice->name;
409
410 deviceDef = pDevice;
411
412 setDeviceDescriptor(pDevice);
413 }
414
415
416 void jtag1::initJtagBox(void)
417 {
418 statusOut("JTAG config starting.\n");
419
420 startJtagLink();
421 changeBitRate(115200);
422
423 uchar hw_ver = getJtagParameter(JTAG_P_HW_VERSION);
424 statusOut("Hardware Version: 0x%02x\n", hw_ver);
425
426 uchar sw_ver = getJtagParameter(JTAG_P_SW_VERSION);
427 statusOut("Software Version: 0x%02x\n", sw_ver);
428
429 interruptProgram();
430
431 deviceAutoConfig();
432
433 // Clear out the breakpoints.
434 deleteAllBreakpoints();
435
436 statusOut("JTAG config complete.\n");
437 }
438
439
440 void jtag1::initJtagOnChipDebugging(unsigned long bitrate)
441 {
442 statusOut("Preparing the target device for On Chip Debugging.\n");
443
444 uchar br;
445 if (bitrate >= 1000000UL)
446 br = JTAG_BITRATE_1_MHz;
447 else if (bitrate >= 500000)
448 br = JTAG_BITRATE_500_KHz;
449 else if (bitrate >= 250000)
450 br = JTAG_BITRATE_250_KHz;
451 else
452 br = JTAG_BITRATE_125_KHz;
453 // Set JTAG bitrate
454 setJtagParameter(JTAG_P_CLOCK, br);
455
456 // Ensure on-chip debug enable fuse is enabled ie '0'
457 jtagActivateOcdenFuse();
458
459 resetProgram(false);
460 setJtagParameter(JTAG_P_TIMERS_RUNNING, 0x00);
461 resetProgram(true);
462 }
463
464 void jtag1::configDaisyChain(void)
465 {
466 /* Set daisy chain information (if needed) */
467 if (dchain.units_before > 0)
468 setJtagParameter(JTAG_P_UNITS_BEFORE,dchain.units_before);
469 if (dchain.units_after > 0)
470 setJtagParameter(JTAG_P_UNITS_AFTER,dchain.units_after);
471 if (dchain.bits_before > 0)
472 setJtagParameter(JTAG_P_BIT_BEFORE,dchain.bits_before);
473 if (dchain.bits_after > 0)
474 setJtagParameter(JTAG_P_BIT_AFTER,dchain.bits_after);
475 }
476