1
2
3
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
39
40
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
82
87
88
89
90
91
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
124
141
142
143
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
330
331
332
333
334
335
336
337
338
339
340
341
342
343
349
350
351
352
353
354
355
356
357
358
359
360
361
362
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
579
587
588
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
670
671
672
673
674
675
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
728
729
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
793
794
795
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
825
826
827
828
829
830
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
865
866
867
868
869
870
871
872
873
875
876
877
878
879
883
884
885
889
890
891
895
896
897
898
899
900
901
902
903
911
912
913
914
915
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
950
951
952
953
954
955
956
957
966
967
978
979
980
981
988
993
994
995
996
997
998
999
1003
1004
1005
1006
1007
1008
1009
1010
1014
1015
1016
1017
1023
1031
1032
1033
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1180
1181
1182
1183
1184
1185
1186
1187
1188
1193
1198
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1286
1287
1288
1289
1290
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1344
1345
1346
1347
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1381
1382
1383
1384
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1445
1446
1447
1448
1449
1450
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1688
1689
1690
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1812
1813
1814
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1861
1862
1863
1864
1865
1866
1870
1874
1875
1876
1880
1881
1882
1883
1884
1885
1886
1887
1888
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1971
1975
1976
1977
1978
1979
1983
1984
1985
1986
1987
1988
1989
1990
1991
1995
1996
1997
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2064
2065
2066
2070
2071
2072
2073
2074
2078
2079
2080
2081
2082
2083
2084
2085
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2115
2116
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2223
2224
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "target.h"
#include "target_type.h"
#include "dsp5680xx.h"
static struct dsp5680xx_common dsp5680xx_context;
#define _E "DSP5680XX_ERROR:%d\nAt:%s:%d:%s"
#define err_check(r, c, m) if (r != ERROR_OK) {LOG_ERROR(_E, c, __func__, __LINE__, m); return r; }
#define err_check_propagate(retval) if (retval != ERROR_OK) return retval;
#define DEBUG_MSG "Debug mode be enabled to read mem."
#define DEBUG_FAIL { err_check(ERROR_FAIL, DSP5680XX_ERROR_NOT_IN_DEBUG, DEBUG_MSG) }
#define CHECK_DBG if (!dsp5680xx_context.debug_mode_enabled) DEBUG_FAIL
#define HALT_MSG "Target must be halted."
#define HALT_FAIL { err_check(ERROR_FAIL, DSP5680XX_ERROR_TARGET_RUNNING, HALT_MSG) }
#define CHECK_HALT(target) if (target->state != TARGET_HALTED) HALT_FAIL
#define check_halt_and_debug(target) { CHECK_HALT(target); CHECK_DBG; }
10 defines
static int dsp5680xx_execute_queue(void)
{
int retval;
retval = jtag_execute_queue();
return retval;
}dsp5680xx_execute_queue (void) { ... }
/* ... */
static int reset_jtag(void)
{
int retval;
tap_state_t states[2];
const char *cp = "RESET";
states[0] = tap_state_by_name(cp);
retval = jtag_add_statemove(states[0]);
err_check_propagate(retval);
retval = jtag_execute_queue();
err_check_propagate(retval);
jtag_add_pathmove(0, states + 1);
retval = jtag_execute_queue();
return retval;
}reset_jtag (void) { ... }
static int dsp5680xx_drscan(struct target *target, uint8_t *d_in,
uint8_t *d_out, int len)
{
/* ... */
int retval = ERROR_OK;
if (!target->tap) {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_JTAG_INVALID_TAP,
"Invalid tap");
}if (!target->tap) { ... }
if (len > 32) {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_JTAG_DR_LEN_OVERFLOW,
"dr_len overflow, maximum is 32");
}if (len > 32) { ... }
jtag_add_plain_dr_scan(len, d_in, d_out, TAP_IDLE);
if (dsp5680xx_context.flush) {
retval = dsp5680xx_execute_queue();
err_check(retval, DSP5680XX_ERROR_JTAG_DRSCAN,
"drscan failed!");
}if (dsp5680xx_context.flush) { ... }
if (d_out)
LOG_DEBUG("Data read (%d bits): 0x%04X", len, *d_out);
else
LOG_DEBUG("Data read was discarded.");
return retval;
}dsp5680xx_drscan (struct target *target, uint8_t *d_in, uint8_t *d_out, int len) { ... }
/* ... */
static int dsp5680xx_irscan(struct target *target, uint32_t *d_in,
uint32_t *d_out, uint8_t ir_len)
{
int retval = ERROR_OK;
uint16_t tap_ir_len = DSP5680XX_JTAG_MASTER_TAP_IRLEN;
if (!target->tap) {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_JTAG_INVALID_TAP,
"Invalid tap");
}if (!target->tap) { ... }
if (ir_len != target->tap->ir_length) {
if (target->tap->enabled) {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_INVALID_IR_LEN,
"Invalid irlen");
}if (target->tap->enabled) { ... } else {
struct jtag_tap *t =
jtag_tap_by_string("dsp568013.chp");
if ((!t)
|| ((t->enabled) && (ir_len != tap_ir_len))) {
retval = ERROR_FAIL;
err_check(retval,
DSP5680XX_ERROR_INVALID_IR_LEN,
"Invalid irlen");
}if ((!t) || ((t->enabled) && (ir_len != tap_ir_len))) { ... }
}else { ... }
}if (ir_len != target->tap->ir_length) { ... }
jtag_add_plain_ir_scan(ir_len, (uint8_t *) d_in, (uint8_t *) d_out,
TAP_IDLE);
if (dsp5680xx_context.flush) {
retval = dsp5680xx_execute_queue();
err_check(retval, DSP5680XX_ERROR_JTAG_IRSCAN,
"irscan failed!");
}if (dsp5680xx_context.flush) { ... }
return retval;
}dsp5680xx_irscan (struct target *target, uint32_t *d_in, uint32_t *d_out, uint8_t ir_len) { ... }
static int dsp5680xx_jtag_status(struct target *target, uint8_t *status)
{
uint32_t read_from_ir;
uint32_t instr;
int retval;
instr = JTAG_INSTR_ENABLE_ONCE;
retval =
dsp5680xx_irscan(target, &instr, &read_from_ir,
DSP5680XX_JTAG_CORE_TAP_IRLEN);
err_check_propagate(retval);
if (status)
*status = (uint8_t) read_from_ir;
return ERROR_OK;
}dsp5680xx_jtag_status (struct target *target, uint8_t *status) { ... }
static int jtag_data_read(struct target *target, uint8_t *data_read,
int num_bits)
{
uint32_t bogus_instr = 0;
int retval =
dsp5680xx_drscan(target, (uint8_t *) &bogus_instr, data_read,
num_bits);
LOG_DEBUG("Data read (%d bits): 0x%04X", num_bits, *data_read);
return retval;
}jtag_data_read (struct target *target, uint8_t *data_read, int num_bits) { ... }
#define jtag_data_read8(target, data_read) jtag_data_read(target, data_read, 8)
#define jtag_data_read16(target, data_read) jtag_data_read(target, data_read, 16)
#define jtag_data_read32(target, data_read) jtag_data_read(target, data_read, 32)
static uint32_t data_read_dummy;
static int jtag_data_write(struct target *target, uint32_t instr, int num_bits,
uint32_t *data_read)
{
int retval;
retval =
dsp5680xx_drscan(target, (uint8_t *) &instr,
(uint8_t *) &data_read_dummy, num_bits);
err_check_propagate(retval);
if (data_read)
*data_read = data_read_dummy;
return retval;
}jtag_data_write (struct target *target, uint32_t instr, int num_bits, uint32_t *data_read) { ... }
#define jtag_data_write8(target, instr, data_read) jtag_data_write(target, instr, 8, data_read)
#define jtag_data_write16(target, instr, data_read) jtag_data_write(target, instr, 16, data_read)
#define jtag_data_write24(target, instr, data_read) jtag_data_write(target, instr, 24, data_read)
#define jtag_data_write32(target, instr, data_read) jtag_data_write(target, instr, 32, data_read)
/* ... */
static int eonce_instruction_exec_single(struct target *target, uint8_t instr,
uint8_t rw, uint8_t go, uint8_t ex,
uint8_t *eonce_status)
{
int retval;
uint32_t dr_out_tmp;
uint8_t instr_with_flags = instr | (rw << 7) | (go << 6) | (ex << 5);
retval = jtag_data_write(target, instr_with_flags, 8, &dr_out_tmp);
err_check_propagate(retval);
if (eonce_status)
*eonce_status = (uint8_t) dr_out_tmp;
return retval;
}eonce_instruction_exec_single (struct target *target, uint8_t instr, uint8_t rw, uint8_t go, uint8_t ex, uint8_t *eonce_status) { ... }
#define dsp5680xx_exe_1(target, oc1, oc2, oc3) dsp5680xx_exe1(target, oc1)
#define dsp5680xx_exe_2(target, oc1, oc2, oc3) dsp5680xx_exe2(target, oc1, oc2)
#define dsp5680xx_exe_3(target, oc1, oc2, oc3) dsp5680xx_exe3(target, oc1, oc2, oc3)
#define dsp5680xx_exe_generic(t, words, oc1, oc2, oc3) dsp5680xx_exe_##words(t, oc1, oc2, oc3)
static int dsp5680xx_exe1(struct target *target, uint16_t opcode)
{
int retval;
retval = eonce_instruction_exec_single(target, 0x04, 0, 1, 0, NULL);
err_check_propagate(retval);
retval = jtag_data_write16(target, opcode, NULL);
err_check_propagate(retval);
return retval;
}dsp5680xx_exe1 (struct target *target, uint16_t opcode) { ... }
static int dsp5680xx_exe2(struct target *target, uint16_t opcode1,
uint16_t opcode2)
{
int retval;
retval = eonce_instruction_exec_single(target, 0x04, 0, 0, 0, NULL);
err_check_propagate(retval);
retval = jtag_data_write16(target, opcode1, NULL);
err_check_propagate(retval);
retval = eonce_instruction_exec_single(target, 0x04, 0, 1, 0, NULL);
err_check_propagate(retval);
retval = jtag_data_write16(target, opcode2, NULL);
err_check_propagate(retval);
return retval;
}dsp5680xx_exe2 (struct target *target, uint16_t opcode1, uint16_t opcode2) { ... }
static int dsp5680xx_exe3(struct target *target, uint16_t opcode1,
uint16_t opcode2, uint16_t opcode3)
{
int retval;
retval = eonce_instruction_exec_single(target, 0x04, 0, 0, 0, NULL);
err_check_propagate(retval);
retval = jtag_data_write16(target, opcode1, NULL);
err_check_propagate(retval);
retval = eonce_instruction_exec_single(target, 0x04, 0, 0, 0, NULL);
err_check_propagate(retval);
retval = jtag_data_write16(target, opcode2, NULL);
err_check_propagate(retval);
retval = eonce_instruction_exec_single(target, 0x04, 0, 1, 0, NULL);
err_check_propagate(retval);
retval = jtag_data_write16(target, opcode3, NULL);
err_check_propagate(retval);
return retval;
}dsp5680xx_exe3 (struct target *target, uint16_t opcode1, uint16_t opcode2, uint16_t opcode3) { ... }
/* ... */
static int core_tx_upper_data(struct target *target, uint16_t data,
uint32_t *eonce_status_low)
{
int retval;
retval =
eonce_instruction_exec_single(target, DSP5680XX_ONCE_ORX1, 0, 0, 0,
NULL);
err_check_propagate(retval);
retval = jtag_data_write16(target, data, eonce_status_low);
err_check_propagate(retval);
return retval;
}core_tx_upper_data (struct target *target, uint16_t data, uint32_t *eonce_status_low) { ... }
#define CMD1 eonce_instruction_exec_single(target, DSP5680XX_ONCE_ORX, 0, 0, 0, NULL);
#define CMD2 jtag_data_write16((t, data)
#define core_tx_lower_data(t, data) PT1\ PT2
/* ... */
static int core_rx_upper_data(struct target *target, uint8_t *data_read)
{
int retval;
retval =
eonce_instruction_exec_single(target, DSP5680XX_ONCE_OTX1, 1, 0, 0,
NULL);
err_check_propagate(retval);
retval = jtag_data_read16(target, data_read);
err_check_propagate(retval);
return retval;
...}
/* ... */
static int core_rx_lower_data(struct target *target, uint8_t *data_read)
{
int retval;
retval =
eonce_instruction_exec_single(target, DSP5680XX_ONCE_OTX, 1, 0, 0,
NULL);
err_check_propagate(retval);
retval = jtag_data_read16(target, data_read);
err_check_propagate(retval);
return retval;
...}
/* ... */
#define exe(a, b, c, d, e) dsp5680xx_exe_generic(a, b, c, d, e)
#define core_move_long_to_r0(target, value) exe(target, 3, 0xe418, value&0xffff, value>>16)
#define core_move_long_to_n(target, value) exe(target, 3, 0xe41e, value&0xffff, value>>16)
#define core_move_at_r0_to_y0(target) exe(target, 1, 0xF514, 0, 0)
#define core_move_at_r0_to_y1(target) exe(target, 1, 0xF714, 0, 0)
#define core_move_long_at_r0_y(target) exe(target, 1, 0xF734, 0, 0)
#define core_move_y0_at_r0(target) exe(target, 1, 0xd514, 0, 0)
#define eonce_bfclr_at_r0(target, value) exe(target, 2, 0x8040, value, 0)
#define core_move_value_to_y0(target, value) exe(target, 2, 0x8745, value, 0)
#define core_move_y0_at_r0_inc(target) exe(target, 1, 0xd500, 0, 0)
#define core_move_y0_at_pr0_inc(target) exe(target, 1, 0x8560, 0, 0)
#define core_move_at_pr0_inc_to_y0(target) exe(target, 1, 0x8568, 0, 0)
#define core_move_at_pr0_inc_to_y1(target) exe(target, 1, 0x8768, 0, 0)
#define core_move_long_to_r2(target, value) exe(target, 3, 0xe41A, value&0xffff, value>>16)
#define core_move_y0_at_r2(target) exe(target, 1, 0xd516, 0, 0)
#define core_move_value_at_r2(target, value) exe(target, 2, 0x8642, value, 0)
#define core_move_value_at_r0(target, value) exe(target, 2, 0x8640, value, 0)
#define core_move_value_at_r2_disp(target, value, disp) exe(target, 3, 0x8646, value, disp)
#define core_move_at_r2_to_y0(target) exe(target, 1, 0xF516, 0, 0)
#define core_move_at_pr2_inc_to_y0(target) exe(target, 1, 0x856A, 0, 0)
#define core_move_long_to_r1(target, value) exe(target, 3, 0xE419, value&0xffff, value>>16)
#define core_move_long_to_r3(target, value) exe(target, 3, 0xE41B, value&0xffff, value>>16)
#define core_move_y0_at_pr3_inc(target) exe(target, 1, 0x8563, 0, 0)
#define core_move_y0_at_r3(target) exe(target, 1, 0xD503, 0, 0)
#define core_move_long_to_r4(target, value) exe(target, 3, 0xE41C, value&0xffff, value>>16)
#define core_move_pc_to_r4(target) exe(target, 1, 0xE716, 0, 0)
#define core_move_r4_to_y(target) exe(target, 1, 0xe764, 0, 0)
#define core_move_at_pr0_inc_to_y0(target) exe(target, 1, 0x8568, 0, 0)
#define core_move_at_r0_inc_to_y0(target) exe(target, 1, 0xf500, 0, 0)
#define core_move_at_r0_y0(target) exe(target, 1, 0xF514, 0, 0)
#define eonce_nop(target) exe(target, 1, 0xe700, 0, 0)
#define core_move_at_r2_disp_to_y0(target, disp) exe(target, 2, 0xF542, disp, 0)
#define core_move_y1_at_r2(target) exe(target, 1, 0xd716, 0, 0)
#define core_move_y1_at_r0(target) exe(target, 1, 0xd714, 0, 0)
#define core_move_byte_y0_at_r0(target) exe(target, 1, 0xd5a0, 0, 0)
#define core_move_y1_at_pr0_inc(target) exe(target, 1, 0x8760, 0, 0)
#define core_move_y1_at_r0_inc(target) exe(target, 1, 0xD700, 0, 0)
#define core_move_long_to_y(target, value) exe(target, 3, 0xe417, value&0xffff, value>>16)
38 defines
static int core_move_value_to_pc(struct target *target, uint32_t value)
{
check_halt_and_debug(target);
int retval;
retval =
dsp5680xx_exe_generic(target, 3, 0xE71E, value & 0xffff,
value >> 16);
err_check_propagate(retval);
return retval;
...}
static int eonce_load_tx_rx_to_r0(struct target *target)
{
int retval;
retval =
core_move_long_to_r0(target,
((MC568013_EONCE_TX_RX_ADDR) +
(MC568013_EONCE_OBASE_ADDR << 16)));
return retval;
...}
static int core_load_tx_rx_high_addr_to_r0(struct target *target)
{
int retval = 0;
retval =
core_move_long_to_r0(target,
((MC568013_EONCE_TX1_RX1_HIGH_ADDR) +
(MC568013_EONCE_OBASE_ADDR << 16)));
return retval;
...}
static int dsp5680xx_read_core_reg(struct target *target, uint8_t reg_addr,
uint16_t *data_read)
{
int retval;
uint32_t dummy_data_to_shift_into_dr;
retval = eonce_instruction_exec_single(target, reg_addr, 1, 0, 0, NULL);
err_check_propagate(retval);
retval =
dsp5680xx_drscan(target, (uint8_t *) &dummy_data_to_shift_into_dr,
(uint8_t *) data_read, 8);
err_check_propagate(retval);
LOG_DEBUG("Reg. data: 0x%02X.", *data_read);
return retval;
...}
static int eonce_read_status_reg(struct target *target, uint16_t *data)
{
int retval;
retval = dsp5680xx_read_core_reg(target, DSP5680XX_ONCE_OSR, data);
err_check_propagate(retval);
return retval;
...}
/* ... */
static int eonce_exit_debug_mode(struct target *target, uint8_t *eonce_status)
{
int retval;
retval =
eonce_instruction_exec_single(target, 0x1F, 0, 0, 1, eonce_status);
err_check_propagate(retval);
return retval;
...}
static int switch_tap(struct target *target, struct jtag_tap *master_tap,
struct jtag_tap *core_tap)
{
int retval = ERROR_OK;
uint32_t instr;
uint32_t ir_out;
if (!master_tap) {
master_tap = jtag_tap_by_string("dsp568013.chp");
if (!master_tap) {
retval = ERROR_FAIL;
const char *msg = "Failed to get master tap.";
err_check(retval, DSP5680XX_ERROR_JTAG_TAP_FIND_MASTER,
msg);
...}
...}
if (!core_tap) {
core_tap = jtag_tap_by_string("dsp568013.cpu");
if (!core_tap) {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_JTAG_TAP_FIND_CORE,
"Failed to get core tap.");
...}
...}
if (!(((int)master_tap->enabled) ^ ((int)core_tap->enabled))) {
LOG_WARNING
("Master:%d\nCore:%d\nOnly 1 should be enabled.\n",
(int)master_tap->enabled, (int)core_tap->enabled);
...}
if (master_tap->enabled) {
instr = 0x5;
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_MASTER_TAP_IRLEN);
err_check_propagate(retval);
instr = 0x2;
retval =
dsp5680xx_drscan(target, (uint8_t *) &instr,
(uint8_t *) &ir_out, 4);
err_check_propagate(retval);
core_tap->enabled = true;
master_tap->enabled = false;
...} else {
instr = 0x08;
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_CORE_TAP_IRLEN);
err_check_propagate(retval);
instr = 0x1;
retval =
dsp5680xx_drscan(target, (uint8_t *) &instr,
(uint8_t *) &ir_out, 4);
err_check_propagate(retval);
core_tap->enabled = false;
master_tap->enabled = true;
...}
return retval;
...}
/* ... */
static int eonce_enter_debug_mode_without_reset(struct target *target,
uint16_t *eonce_status)
{
int retval;
uint32_t instr = JTAG_INSTR_DEBUG_REQUEST;
uint32_t ir_out;
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_CORE_TAP_IRLEN);
err_check_propagate(retval);
instr = JTAG_INSTR_ENABLE_ONCE;
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_CORE_TAP_IRLEN);
err_check_propagate(retval);
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_CORE_TAP_IRLEN);
err_check_propagate(retval);
if ((ir_out & JTAG_STATUS_MASK) == JTAG_STATUS_DEBUG)
target->state = TARGET_HALTED;
else {
retval = ERROR_FAIL;
err_check_propagate(retval);
...}
uint16_t data_read_from_dr;
retval = eonce_read_status_reg(target, &data_read_from_dr);
err_check_propagate(retval);
if ((data_read_from_dr & 0x30) == 0x30) {
LOG_DEBUG("EOnCE successfully entered debug mode.");
dsp5680xx_context.debug_mode_enabled = true;
retval = ERROR_OK;
...} else {
dsp5680xx_context.debug_mode_enabled = false;
retval = ERROR_TARGET_FAILURE;
/* ... */
err_check_propagate(retval);
...}
if (eonce_status)
*eonce_status = data_read_from_dr;
return retval;
...}
/* ... */
static int eonce_enter_debug_mode(struct target *target,
uint16_t *eonce_status)
{
int retval = ERROR_OK;
uint32_t instr = JTAG_INSTR_DEBUG_REQUEST;
uint32_t ir_out;
uint16_t instr_16;
uint16_t read_16;
retval = eonce_enter_debug_mode_without_reset(target, eonce_status);
if (retval == ERROR_OK)
return retval;
struct jtag_tap *tap_chp;
struct jtag_tap *tap_cpu;
tap_chp = jtag_tap_by_string("dsp568013.chp");
if (!tap_chp) {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_JTAG_TAP_FIND_MASTER,
"Failed to get master tap.");
...}
tap_cpu = jtag_tap_by_string("dsp568013.cpu");
if (!tap_cpu) {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_JTAG_TAP_FIND_CORE,
"Failed to get master tap.");
...}
tap_chp->enabled = true;
tap_cpu->enabled = false;
instr = MASTER_TAP_CMD_IDCODE;
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_MASTER_TAP_IRLEN);
err_check_propagate(retval);
jtag_add_sleep(TIME_DIV_FREESCALE * 100 * 1000);
jtag_add_reset(0, 1);
jtag_add_sleep(TIME_DIV_FREESCALE * 200 * 1000);
instr = 0x0606ffff;
retval =
dsp5680xx_drscan(target, (uint8_t *) &instr, (uint8_t *) &ir_out,
32);
err_check_propagate(retval);
tap_chp->enabled = true;
retval = switch_tap(target, tap_chp, tap_cpu);
err_check_propagate(retval);
instr = JTAG_INSTR_ENABLE_ONCE;
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_CORE_TAP_IRLEN);
err_check_propagate(retval);
instr = JTAG_INSTR_DEBUG_REQUEST;
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_CORE_TAP_IRLEN);
err_check_propagate(retval);
instr_16 = 0x1;
retval =
dsp5680xx_drscan(target, (uint8_t *) &instr_16,
(uint8_t *) &read_16, 8);
err_check_propagate(retval);
instr_16 = 0x20;
retval =
dsp5680xx_drscan(target, (uint8_t *) &instr_16,
(uint8_t *) &read_16, 8);
err_check_propagate(retval);
jtag_add_sleep(TIME_DIV_FREESCALE * 100 * 1000);
jtag_add_reset(0, 0);
jtag_add_sleep(TIME_DIV_FREESCALE * 300 * 1000);
instr = JTAG_INSTR_ENABLE_ONCE;
for (int i = 0; i < 3; i++) {
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_CORE_TAP_IRLEN);
err_check_propagate(retval);
...}
if ((ir_out & JTAG_STATUS_MASK) == JTAG_STATUS_DEBUG)
target->state = TARGET_HALTED;
else {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_HALT,
"Failed to halt target.");
...}
for (int i = 0; i < 3; i++) {
instr_16 = 0x86;
dsp5680xx_drscan(target, (uint8_t *) &instr_16,
(uint8_t *) &read_16, 16);
instr_16 = 0xff;
dsp5680xx_drscan(target, (uint8_t *) &instr_16,
(uint8_t *) &read_16, 16);
...}
uint16_t data_read_from_dr;
retval = eonce_read_status_reg(target, &data_read_from_dr);
err_check_propagate(retval);
if ((data_read_from_dr & 0x30) == 0x30) {
LOG_DEBUG("EOnCE successfully entered debug mode.");
dsp5680xx_context.debug_mode_enabled = true;
retval = ERROR_OK;
...} else {
const char *msg = "Failed to set EOnCE module to debug mode";
retval = ERROR_TARGET_FAILURE;
err_check(retval, DSP5680XX_ERROR_ENTER_DEBUG_MODE, msg);
...}
if (eonce_status)
*eonce_status = data_read_from_dr;
return retval;
...}
/* ... */
static int eonce_pc_store(struct target *target)
{
uint8_t tmp[2];
int retval;
retval = core_move_pc_to_r4(target);
err_check_propagate(retval);
retval = core_move_r4_to_y(target);
err_check_propagate(retval);
retval = eonce_load_tx_rx_to_r0(target);
err_check_propagate(retval);
retval = core_move_y0_at_r0(target);
err_check_propagate(retval);
retval = core_rx_lower_data(target, tmp);
err_check_propagate(retval);
LOG_USER("PC value: 0x%X%X\n", tmp[1], tmp[0]);
dsp5680xx_context.stored_pc = (tmp[0] | (tmp[1] << 8));
return ERROR_OK;
...}
static int dsp5680xx_target_create(struct target *target, Jim_Interp *interp)
{
struct dsp5680xx_common *dsp5680xx =
calloc(1, sizeof(struct dsp5680xx_common));
target->arch_info = dsp5680xx;
return ERROR_OK;
...}
static int dsp5680xx_init_target(struct command_context *cmd_ctx,
struct target *target)
{
dsp5680xx_context.stored_pc = 0;
dsp5680xx_context.flush = 1;
dsp5680xx_context.debug_mode_enabled = false;
LOG_DEBUG("target initiated!");
/* ... */
return ERROR_OK;
...}
static int dsp5680xx_arch_state(struct target *target)
{
LOG_USER("%s not implemented yet.", __func__);
return ERROR_OK;
...}
static int dsp5680xx_assert_reset(struct target *target)
{
target->state = TARGET_RESET;
return ERROR_OK;
...}
static int dsp5680xx_deassert_reset(struct target *target)
{
target->state = TARGET_RUNNING;
return ERROR_OK;
...}
static int dsp5680xx_halt(struct target *target)
{
int retval;
uint16_t eonce_status = 0xbeef;
if ((target->state == TARGET_HALTED)
&& (dsp5680xx_context.debug_mode_enabled)) {
LOG_USER("Target already halted and in debug mode.");
return ERROR_OK;
...} else {
if (target->state == TARGET_HALTED)
LOG_USER
("Target already halted, re attempting to enter debug mode.");
...}
retval = eonce_enter_debug_mode(target, &eonce_status);
err_check_propagate(retval);
retval = eonce_pc_store(target);
err_check_propagate(retval);
if (dsp5680xx_context.debug_mode_enabled) {
retval = eonce_pc_store(target);
err_check_propagate(retval);
...}
return retval;
...}
static int dsp5680xx_poll(struct target *target)
{
int retval;
uint8_t jtag_status;
uint8_t eonce_status;
uint16_t read_tmp;
retval = dsp5680xx_jtag_status(target, &jtag_status);
err_check_propagate(retval);
if (jtag_status == JTAG_STATUS_DEBUG)
if (target->state != TARGET_HALTED) {
retval = eonce_enter_debug_mode(target, &read_tmp);
err_check_propagate(retval);
eonce_status = (uint8_t) read_tmp;
if ((eonce_status & EONCE_STAT_MASK) !=
DSP5680XX_ONCE_OSCR_DEBUG_M) {
const char *msg =
"%s: Failed to put EOnCE in debug mode.Flash locked?...";
LOG_WARNING(msg, __func__);
return ERROR_TARGET_FAILURE;
...} else {
target->state = TARGET_HALTED;
return ERROR_OK;
...}
...}
if (jtag_status == JTAG_STATUS_NORMAL) {
if (target->state == TARGET_RESET) {
retval = dsp5680xx_halt(target);
err_check_propagate(retval);
retval = eonce_exit_debug_mode(target, &eonce_status);
err_check_propagate(retval);
if ((eonce_status & EONCE_STAT_MASK) !=
DSP5680XX_ONCE_OSCR_NORMAL_M) {
const char *msg =
"%s: JTAG running, but EOnCE run failed.Try resetting..";
LOG_WARNING(msg, __func__);
return ERROR_TARGET_FAILURE;
...} else {
target->state = TARGET_RUNNING;
return ERROR_OK;
...}
...}
if (target->state != TARGET_RUNNING) {
retval = eonce_read_status_reg(target, &read_tmp);
err_check_propagate(retval);
eonce_status = (uint8_t) read_tmp;
if ((eonce_status & EONCE_STAT_MASK) !=
DSP5680XX_ONCE_OSCR_NORMAL_M) {
LOG_WARNING
("Inconsistent target status. Restart!");
return ERROR_TARGET_FAILURE;
...}
...}
target->state = TARGET_RUNNING;
return ERROR_OK;
...}
if (jtag_status == JTAG_STATUS_DEAD) {
LOG_ERROR
("%s: Cannot communicate with JTAG. Check connection...",
__func__);
target->state = TARGET_UNKNOWN;
return ERROR_TARGET_FAILURE;
...}
if (target->state == TARGET_UNKNOWN) {
LOG_ERROR("%s: Target status invalid - communication failure",
__func__);
return ERROR_TARGET_FAILURE;
...}
return ERROR_OK;
...}
static int dsp5680xx_resume(struct target *target, int current,
target_addr_t address, int hb, int d)
{
if (target->state == TARGET_RUNNING) {
LOG_USER("Target already running.");
return ERROR_OK;
...}
int retval;
uint8_t eonce_status;
uint8_t jtag_status;
if (dsp5680xx_context.debug_mode_enabled) {
if (!current) {
retval = core_move_value_to_pc(target, address);
err_check_propagate(retval);
...}
int retry = 20;
while (retry-- > 1) {
retval = eonce_exit_debug_mode(target, &eonce_status);
err_check_propagate(retval);
if (eonce_status == DSP5680XX_ONCE_OSCR_NORMAL_M)
break;
...}
if (retry == 0) {
retval = ERROR_TARGET_FAILURE;
err_check(retval, DSP5680XX_ERROR_EXIT_DEBUG_MODE,
"Failed to exit debug mode...");
...} else {
target->state = TARGET_RUNNING;
dsp5680xx_context.debug_mode_enabled = false;
...}
LOG_DEBUG("EOnCE status: 0x%02X.", eonce_status);
...} else {
/* ... */
jtag_add_reset(0, 1);
jtag_add_sleep(TIME_DIV_FREESCALE * 200 * 1000);
retval = reset_jtag();
err_check(retval, DSP5680XX_ERROR_JTAG_RESET,
"Failed to reset JTAG state machine");
jtag_add_sleep(TIME_DIV_FREESCALE * 100 * 1000);
jtag_add_reset(0, 0);
jtag_add_sleep(TIME_DIV_FREESCALE * 300 * 1000);
retval = dsp5680xx_jtag_status(target, &jtag_status);
err_check_propagate(retval);
if ((jtag_status & JTAG_STATUS_MASK) == JTAG_STATUS_NORMAL) {
target->state = TARGET_RUNNING;
dsp5680xx_context.debug_mode_enabled = false;
...} else {
retval = ERROR_TARGET_FAILURE;
err_check(retval, DSP5680XX_ERROR_RESUME,
"Failed to resume target");
...}
...}
return ERROR_OK;
...}
/* ... */
static int dsp5680xx_convert_address(uint32_t *address, int *pmem)
{
/* ... */
if (*address >= S_FILE_DATA_OFFSET) {
*pmem = 0;
if (((*address) & 0xff0000) != 0xff0000)
*address -= S_FILE_DATA_OFFSET;
...}
return ERROR_OK;
...}
static int dsp5680xx_read_16_single(struct target *t, uint32_t a,
uint8_t *data_read, int r_pmem)
{
struct target *target = t;
uint32_t address = a;
int retval;
retval = core_move_long_to_r0(target, address);
err_check_propagate(retval);
if (r_pmem)
retval = core_move_at_pr0_inc_to_y0(target);
else
retval = core_move_at_r0_to_y0(target);
err_check_propagate(retval);
retval = eonce_load_tx_rx_to_r0(target);
err_check_propagate(retval);
retval = core_move_y0_at_r0(target);
err_check_propagate(retval);
retval = core_rx_lower_data(target, data_read);
err_check_propagate(retval);
LOG_DEBUG("%s:Data read from 0x%06" PRIX32 ": 0x%02X%02X", __func__, address,
data_read[1], data_read[0]);
return retval;
...}
static int dsp5680xx_read_32_single(struct target *t, uint32_t a,
uint8_t *data_read, int r_pmem)
{
struct target *target = t;
uint32_t address = a;
int retval;
address = (address & 0xFFFFF);
retval = core_move_long_to_r0(target, address);
err_check_propagate(retval);
if (r_pmem) {
retval = core_move_at_pr0_inc_to_y0(target);
err_check_propagate(retval);
retval = core_move_at_pr0_inc_to_y1(target);
err_check_propagate(retval);
...} else {
retval = core_move_at_r0_inc_to_y0(target);
err_check_propagate(retval);
retval = core_move_at_r0_to_y1(target);
err_check_propagate(retval);
...}
retval = eonce_load_tx_rx_to_r0(target);
err_check_propagate(retval);
retval = core_move_y0_at_r0_inc(target);
err_check_propagate(retval);
retval = core_move_y1_at_r0(target);
err_check_propagate(retval);
retval = core_rx_lower_data(target, data_read);
err_check_propagate(retval);
retval = core_rx_upper_data(target, data_read + 2);
err_check_propagate(retval);
return retval;
...}
static int dsp5680xx_read(struct target *t, target_addr_t a, uint32_t size,
uint32_t count, uint8_t *buf)
{
struct target *target = t;
uint32_t address = a;
uint8_t *buffer = buf;
check_halt_and_debug(target);
int retval = ERROR_OK;
int pmem = 1;
retval = dsp5680xx_convert_address(&address, &pmem);
err_check_propagate(retval);
dsp5680xx_context.flush = 0;
int counter = FLUSH_COUNT_READ_WRITE;
for (unsigned i = 0; i < count; i++) {
if (--counter == 0) {
dsp5680xx_context.flush = 1;
counter = FLUSH_COUNT_READ_WRITE;
...}
switch (size) {
case 1:
if (!(i % 2))
retval =
dsp5680xx_read_16_single(target,
address + i / 2,
buffer + i, pmem);
break;case 1:
case 2:
retval =
dsp5680xx_read_16_single(target, address + i,
buffer + 2 * i, pmem);
break;case 2:
case 4:
retval =
dsp5680xx_read_32_single(target, address + 2 * i,
buffer + 4 * i, pmem);
break;case 4:
default:
LOG_USER("%s: Invalid read size.", __func__);
break;default
...}
err_check_propagate(retval);
dsp5680xx_context.flush = 0;
...}
dsp5680xx_context.flush = 1;
retval = dsp5680xx_execute_queue();
err_check_propagate(retval);
return retval;
...}
static int dsp5680xx_write_16_single(struct target *t, uint32_t a,
uint16_t data, uint8_t w_pmem)
{
struct target *target = t;
uint32_t address = a;
int retval = 0;
retval = core_move_long_to_r0(target, address);
err_check_propagate(retval);
if (w_pmem) {
retval = core_move_value_to_y0(target, data);
err_check_propagate(retval);
retval = core_move_y0_at_pr0_inc(target);
err_check_propagate(retval);
...} else {
retval = core_move_value_at_r0(target, data);
err_check_propagate(retval);
...}
return retval;
...}
static int dsp5680xx_write_32_single(struct target *t, uint32_t a,
uint32_t data, int w_pmem)
{
struct target *target = t;
uint32_t address = a;
int retval = ERROR_OK;
retval = core_move_long_to_r0(target, address);
err_check_propagate(retval);
retval = core_move_long_to_y(target, data);
err_check_propagate(retval);
if (w_pmem)
retval = core_move_y0_at_pr0_inc(target);
else
retval = core_move_y0_at_r0_inc(target);
err_check_propagate(retval);
if (w_pmem)
retval = core_move_y1_at_pr0_inc(target);
else
retval = core_move_y1_at_r0_inc(target);
err_check_propagate(retval);
return retval;
...}
static int dsp5680xx_write_8(struct target *t, uint32_t a, uint32_t c,
const uint8_t *d, int pmem)
{
struct target *target = t;
uint32_t address = a;
uint32_t count = c;
const uint8_t *data = d;
int retval = 0;
uint16_t data_16;
uint32_t iter;
int counter = FLUSH_COUNT_READ_WRITE;
for (iter = 0; iter < count / 2; iter++) {
if (--counter == 0) {
dsp5680xx_context.flush = 1;
counter = FLUSH_COUNT_READ_WRITE;
...}
data_16 = (data[2 * iter] | (data[2 * iter + 1] << 8));
retval =
dsp5680xx_write_16_single(target, address + iter, data_16,
pmem);
if (retval != ERROR_OK) {
LOG_ERROR("%s: Could not write to p:0x%04" PRIX32, __func__,
address);
dsp5680xx_context.flush = 1;
return retval;
...}
dsp5680xx_context.flush = 0;
...}
dsp5680xx_context.flush = 1;
uint16_t data_old;
if ((count == 1) || (count % 2)) {
retval =
dsp5680xx_read(target, address + iter, 1, 1,
(uint8_t *) &data_old);
err_check_propagate(retval);
if (count == 1)
data_old = (((data_old & 0xff) << 8) | data[0]);
else
data_old =
(((data_old & 0xff) << 8) | data[2 * iter + 1]);
retval =
dsp5680xx_write_16_single(target, address + iter, data_old,
pmem);
err_check_propagate(retval);
...}
return retval;
...}
static int dsp5680xx_write_16(struct target *t, uint32_t a, uint32_t c,
const uint8_t *d, int pmem)
{
struct target *target = t;
uint32_t address = a;
uint32_t count = c;
const uint8_t *data = d;
int retval = ERROR_OK;
uint32_t iter;
int counter = FLUSH_COUNT_READ_WRITE;
for (iter = 0; iter < count; iter++) {
if (--counter == 0) {
dsp5680xx_context.flush = 1;
counter = FLUSH_COUNT_READ_WRITE;
...}
retval =
dsp5680xx_write_16_single(target, address + iter,
data[iter], pmem);
if (retval != ERROR_OK) {
LOG_ERROR("%s: Could not write to p:0x%04" PRIX32, __func__,
address);
dsp5680xx_context.flush = 1;
return retval;
...}
dsp5680xx_context.flush = 0;
...}
dsp5680xx_context.flush = 1;
return retval;
...}
static int dsp5680xx_write_32(struct target *t, uint32_t a, uint32_t c,
const uint8_t *d, int pmem)
{
struct target *target = t;
uint32_t address = a;
uint32_t count = c;
const uint8_t *data = d;
int retval = ERROR_OK;
uint32_t iter;
int counter = FLUSH_COUNT_READ_WRITE;
for (iter = 0; iter < count; iter++) {
if (--counter == 0) {
dsp5680xx_context.flush = 1;
counter = FLUSH_COUNT_READ_WRITE;
...}
retval =
dsp5680xx_write_32_single(target, address + (iter << 1),
data[iter], pmem);
if (retval != ERROR_OK) {
LOG_ERROR("%s: Could not write to p:0x%04" PRIX32, __func__,
address);
dsp5680xx_context.flush = 1;
return retval;
...}
dsp5680xx_context.flush = 0;
...}
dsp5680xx_context.flush = 1;
return retval;
...}
/* ... */
static int dsp5680xx_write(struct target *target, target_addr_t a, uint32_t size, uint32_t count,
const uint8_t *b)
{
uint32_t address = a;
uint8_t const *buffer = b;
check_halt_and_debug(target);
int retval = 0;
int p_mem = 1;
retval = dsp5680xx_convert_address(&address, &p_mem);
err_check_propagate(retval);
switch (size) {
case 1:
retval =
dsp5680xx_write_8(target, address, count, buffer, p_mem);
break;case 1:
case 2:
retval =
dsp5680xx_write_16(target, address, count, buffer, p_mem);
break;case 2:
case 4:
retval =
dsp5680xx_write_32(target, address, count, buffer, p_mem);
break;case 4:
default:
retval = ERROR_TARGET_DATA_ABORT;
err_check(retval, DSP5680XX_ERROR_INVALID_DATA_SIZE_UNIT,
"Invalid data size.");
break;default
...}
return retval;
...}
static int dsp5680xx_write_buffer(struct target *t, target_addr_t a, uint32_t size,
const uint8_t *b)
{
check_halt_and_debug(t);
return dsp5680xx_write(t, a, 1, size, b);
...}
/* ... */
static int dsp5680xx_read_buffer(struct target *target, target_addr_t address, uint32_t size,
uint8_t *buffer)
{
check_halt_and_debug(target);
return dsp5680xx_read(target, address, 2, size / 2, buffer);
...}
/* ... */
static int dsp5680xx_checksum_memory(struct target *target, target_addr_t address, uint32_t size,
uint32_t *checksum)
{
return ERROR_FAIL;
...}
/* ... */
static int perl_crc(const uint8_t *buff8, uint32_t word_count)
{
uint16_t checksum = 0xffff;
uint16_t data, fbmisr;
uint32_t i;
for (i = 0; i < word_count; i++) {
data = (buff8[2 * i] | (buff8[2 * i + 1] << 8));
fbmisr =
(checksum & 2) >> 1 ^ (checksum & 4) >> 2 ^ (checksum & 16)
>> 4 ^ (checksum & 0x8000) >> 15;
checksum = (data ^ ((checksum << 1) | fbmisr));
...}
i--;
for (; !(i & 0x80000000); i--) {
data = (buff8[2 * i] | (buff8[2 * i + 1] << 8));
fbmisr =
(checksum & 2) >> 1 ^ (checksum & 4) >> 2 ^ (checksum & 16)
>> 4 ^ (checksum & 0x8000) >> 15;
checksum = (data ^ ((checksum << 1) | fbmisr));
...}
return checksum;
...}
/* ... */
static int dsp5680xx_f_sim_reset(struct target *target)
{
int retval = ERROR_OK;
uint16_t sim_cmd = SIM_CMD_RESET;
uint32_t sim_addr;
if (strcmp(target->tap->chip, "dsp568013") == 0) {
sim_addr = MC568013_SIM_BASE_ADDR + S_FILE_DATA_OFFSET;
retval =
dsp5680xx_write(target, sim_addr, 1, 2,
(const uint8_t *)&sim_cmd);
err_check_propagate(retval);
...}
return retval;
...}
/* ... */
static int dsp5680xx_soft_reset_halt(struct target *target)
{
int retval;
retval = dsp5680xx_halt(target);
err_check_propagate(retval);
retval = dsp5680xx_f_sim_reset(target);
err_check_propagate(retval);
return retval;
...}
int dsp5680xx_f_protect_check(struct target *target, uint16_t *protected)
{
int retval;
check_halt_and_debug(target);
if (!protected) {
const char *msg = "NULL pointer not valid.";
err_check(ERROR_FAIL,
DSP5680XX_ERROR_PROTECT_CHECK_INVALID_ARGS, msg);
...}
retval =
dsp5680xx_read_16_single(target, HFM_BASE_ADDR | HFM_PROT,
(uint8_t *) protected, 0);
err_check_propagate(retval);
return retval;
...}
/* ... */
static int dsp5680xx_f_ex(struct target *target, uint16_t c, uint32_t address, uint32_t data,
uint16_t *hfm_ustat, int pmem)
{
uint32_t command = c;
int retval;
retval = core_load_tx_rx_high_addr_to_r0(target);
err_check_propagate(retval);
retval = core_move_long_to_r2(target, HFM_BASE_ADDR);
err_check_propagate(retval);
uint8_t i[2];
int watchdog = 100;
do {
retval = core_move_at_r2_disp_to_y0(target, HFM_USTAT);
err_check_propagate(retval);
retval = core_move_y0_at_r0(target);
err_check_propagate(retval);
retval = core_rx_upper_data(target, i);
err_check_propagate(retval);
if ((watchdog--) == 1) {
retval = ERROR_TARGET_FAILURE;
const char *msg =
"Timed out waiting for FM to finish old command.";
err_check(retval, DSP5680XX_ERROR_FM_BUSY, msg);
...}
...} while (!(i[0] & 0x40));
dsp5680xx_context.flush = 0;
retval = core_move_value_at_r2_disp(target, 0x00, HFM_CNFG);
err_check_propagate(retval);
retval = core_move_value_at_r2_disp(target, 0x04, HFM_USTAT);
err_check_propagate(retval);
retval = core_move_value_at_r2_disp(target, 0x10, HFM_USTAT);
err_check_propagate(retval);
retval = core_move_value_at_r2_disp(target, 0x20, HFM_USTAT);
err_check_propagate(retval);
retval = core_move_value_at_r2_disp(target, 0x00, HFM_PROT);
err_check_propagate(retval);
retval = core_move_value_at_r2_disp(target, 0x00, HFM_PROTB);
err_check_propagate(retval);
retval = core_move_value_to_y0(target, data);
err_check_propagate(retval);
retval = core_move_long_to_r3(target, address);
err_check_propagate(retval);
if (pmem) {
retval = core_move_y0_at_pr3_inc(target);
err_check_propagate(retval);
...} else {
retval = core_move_y0_at_r3(target);
err_check_propagate(retval);
...}
retval = core_move_value_at_r2_disp(target, command, HFM_CMD);
err_check_propagate(retval);
retval = core_move_value_at_r2_disp(target, 0x80, HFM_USTAT);
err_check_propagate(retval);
dsp5680xx_context.flush = 1;
retval = dsp5680xx_execute_queue();
err_check_propagate(retval);
watchdog = 100;
do {
retval = core_move_at_r2_disp_to_y0(target, HFM_USTAT);
err_check_propagate(retval);
retval = core_move_y0_at_r0(target);
err_check_propagate(retval);
retval = core_rx_upper_data(target, i);
err_check_propagate(retval);
if ((watchdog--) == 1) {
retval = ERROR_TARGET_FAILURE;
err_check(retval, DSP5680XX_ERROR_FM_CMD_TIMED_OUT,
"FM execution did not finish.");
...}
...} while (!(i[0] & 0x40));
*hfm_ustat = ((i[0] << 8) | (i[1]));
if (i[0] & HFM_USTAT_MASK_PVIOL_ACCER) {
retval = ERROR_TARGET_FAILURE;
const char *msg =
"pviol and/or accer bits set. HFM command execution error";
err_check(retval, DSP5680XX_ERROR_FM_EXEC, msg);
...}
return ERROR_OK;
...}
/* ... */
static int set_fm_ck_div(struct target *target)
{
uint8_t i[2];
int retval;
retval = core_move_long_to_r2(target, HFM_BASE_ADDR);
err_check_propagate(retval);
retval = core_load_tx_rx_high_addr_to_r0(target);
err_check_propagate(retval);
retval = core_move_at_r2_to_y0(target);
err_check_propagate(retval);
retval = core_move_y0_at_r0(target);
err_check_propagate(retval);
retval = core_rx_upper_data(target, i);
err_check_propagate(retval);
unsigned int hfm_at_wrong_value = 0;
if ((i[0] & 0x7f) != HFM_CLK_DEFAULT) {
LOG_DEBUG("HFM CLK divisor contained incorrect value (0x%02X).",
i[0] & 0x7f);
hfm_at_wrong_value = 1;
...} else {
LOG_DEBUG
("HFM CLK divisor was already set to correct value (0x%02X).",
i[0] & 0x7f);
return ERROR_OK;
...}
retval = core_move_value_at_r2(target, HFM_CLK_DEFAULT);
err_check_propagate(retval);
retval = core_move_at_r2_to_y0(target);
err_check_propagate(retval);
retval = core_move_y0_at_r0(target);
err_check_propagate(retval);
retval = core_rx_upper_data(target, i);
err_check_propagate(retval);
if (i[0] != (0x80 | (HFM_CLK_DEFAULT & 0x7f))) {
retval = ERROR_TARGET_FAILURE;
err_check(retval, DSP5680XX_ERROR_FM_SET_CLK,
"Unable to set HFM CLK divisor.");
...}
if (hfm_at_wrong_value)
LOG_DEBUG("HFM CLK divisor set to 0x%02x.", i[0] & 0x7f);
return ERROR_OK;
...}
/* ... */
static int dsp5680xx_f_signature(struct target *target, uint32_t address, uint32_t words,
uint16_t *signature)
{
int retval;
uint16_t hfm_ustat;
if (!dsp5680xx_context.debug_mode_enabled) {
retval = eonce_enter_debug_mode_without_reset(target, NULL);
/* ... */
err_check(retval, DSP5680XX_ERROR_HALT,
"Failed to halt target.");
...}
retval =
dsp5680xx_f_ex(target, HFM_CALCULATE_DATA_SIGNATURE, address, words,
&hfm_ustat, 1);
err_check_propagate(retval);
retval =
dsp5680xx_read_16_single(target, HFM_BASE_ADDR | HFM_DATA,
(uint8_t *) signature, 0);
return retval;
...}
int dsp5680xx_f_erase_check(struct target *target, uint8_t *erased,
uint32_t sector)
{
int retval;
uint16_t hfm_ustat;
uint32_t tmp;
if (!dsp5680xx_context.debug_mode_enabled) {
retval = dsp5680xx_halt(target);
err_check_propagate(retval);
...}
retval = set_fm_ck_div(target);
err_check_propagate(retval);
/* ... */
tmp = HFM_FLASH_BASE_ADDR + sector * HFM_SECTOR_SIZE / 2;
retval =
dsp5680xx_f_ex(target, HFM_ERASE_VERIFY, tmp, 0, &hfm_ustat, 1);
err_check_propagate(retval);
if (erased)
*erased = (uint8_t) (hfm_ustat & HFM_USTAT_MASK_BLANK);
return retval;
...}
/* ... */
static int erase_sector(struct target *target, int sector, uint16_t *hfm_ustat)
{
int retval;
uint32_t tmp = HFM_FLASH_BASE_ADDR + sector * HFM_SECTOR_SIZE / 2;
retval = dsp5680xx_f_ex(target, HFM_PAGE_ERASE, tmp, 0, hfm_ustat, 1);
err_check_propagate(retval);
return retval;
...}
/* ... */
static int mass_erase(struct target *target, uint16_t *hfm_ustat)
{
int retval;
retval = dsp5680xx_f_ex(target, HFM_MASS_ERASE, 0, 0, hfm_ustat, 1);
return retval;
...}
int dsp5680xx_f_erase(struct target *target, int first, int last)
{
int retval;
if (!dsp5680xx_context.debug_mode_enabled) {
retval = dsp5680xx_halt(target);
err_check_propagate(retval);
...}
/* ... */
retval = dsp5680xx_f_sim_reset(target);
err_check_propagate(retval);
/* ... */
retval = set_fm_ck_div(target);
err_check_propagate(retval);
uint16_t hfm_ustat;
int do_mass_erase = ((!(first | last))
|| ((first == 0)
&& (last == (HFM_SECTOR_COUNT - 1))));
if (do_mass_erase) {
retval = mass_erase(target, &hfm_ustat);
err_check_propagate(retval);
...} else {
for (int i = first; i <= last; i++) {
retval = erase_sector(target, i, &hfm_ustat);
err_check_propagate(retval);
...}
...}
return ERROR_OK;
...}
/* ... */
static const uint16_t pgm_write_pflash[] = {
0x8A46, 0x0013, 0x807D, 0xE700,
0xE700, 0x8A44, 0xFFFE, 0x017B,
0xE700, 0xF514, 0x8563, 0x8646,
0x0020, 0x0014, 0x8646, 0x0080,
0x0013, 0xF042, 0x0013, 0x8B40,
0x2004, 0x8246, 0x0013, 0x0020,
0xA967, 0x8B40, 0x1065, 0x8246,
0x0013, 0x0010, 0xA961
...};
static const uint32_t pgm_write_pflash_length = 31;
int dsp5680xx_f_wr(struct target *t, const uint8_t *b, uint32_t a, uint32_t count,
int is_flash_lock)
{
struct target *target = t;
uint32_t address = a;
const uint8_t *buffer = b;
int retval = ERROR_OK;
if (!dsp5680xx_context.debug_mode_enabled) {
retval = eonce_enter_debug_mode(target, NULL);
err_check_propagate(retval);
...}
/* ... */
const uint32_t len = pgm_write_pflash_length;
uint32_t ram_addr = 0x8700;
/* ... */
if (!is_flash_lock) {
retval =
dsp5680xx_write(target, ram_addr, 1, len * 2,
(uint8_t *) pgm_write_pflash);
err_check_propagate(retval);
retval = dsp5680xx_execute_queue();
err_check_propagate(retval);
...}
/* ... */
retval = set_fm_ck_div(target);
err_check_propagate(retval);
/* ... */
dsp5680xx_context.flush = 0;
retval = core_move_long_to_r3(target, address);
err_check_propagate(retval);
core_load_tx_rx_high_addr_to_r0(target);
err_check_propagate(retval);
retval = core_move_long_to_r2(target, HFM_BASE_ADDR);
err_check_propagate(retval);
/* ... */
retval = core_move_value_at_r2_disp(target, 0x00, HFM_CNFG);
err_check_propagate(retval);
retval = core_move_value_at_r2_disp(target, 0x04, HFM_USTAT);
err_check_propagate(retval);
retval = core_move_value_at_r2_disp(target, 0x10, HFM_USTAT);
err_check_propagate(retval);
retval = core_move_value_at_r2_disp(target, 0x20, HFM_USTAT);
err_check_propagate(retval);
retval = core_move_value_at_r2_disp(target, 0x00, HFM_PROT);
err_check_propagate(retval);
retval = core_move_value_at_r2_disp(target, 0x00, HFM_PROTB);
err_check_propagate(retval);
if (count % 2) {
retval = ERROR_FAIL;
const char *msg = "Cannot handle odd number of words.";
err_check(retval, DSP5680XX_ERROR_FLASHING_INVALID_WORD_COUNT,
msg);
...}
dsp5680xx_context.flush = 1;
retval = dsp5680xx_execute_queue();
err_check_propagate(retval);
uint32_t drscan_data;
uint16_t tmp = (buffer[0] | (buffer[1] << 8));
retval = core_tx_upper_data(target, tmp, &drscan_data);
err_check_propagate(retval);
retval = dsp5680xx_resume(target, 0, ram_addr, 0, 0);
err_check_propagate(retval);
int counter = FLUSH_COUNT_FLASH;
dsp5680xx_context.flush = 0;
uint32_t i;
for (i = 1; (i < count / 2) && (i < HFM_SIZE_WORDS); i++) {
if (--counter == 0) {
dsp5680xx_context.flush = 1;
counter = FLUSH_COUNT_FLASH;
...}
tmp = (buffer[2 * i] | (buffer[2 * i + 1] << 8));
retval = core_tx_upper_data(target, tmp, &drscan_data);
if (retval != ERROR_OK) {
dsp5680xx_context.flush = 1;
err_check_propagate(retval);
...}
dsp5680xx_context.flush = 0;
...}
dsp5680xx_context.flush = 1;
if (!is_flash_lock) {
/* ... */
uint16_t signature;
uint16_t pc_crc;
retval = dsp5680xx_f_signature(target, address, i, &signature);
err_check_propagate(retval);
pc_crc = perl_crc(buffer, i);
if (pc_crc != signature) {
retval = ERROR_FAIL;
const char *msg =
"Flashed data failed CRC check, flash again!";
err_check(retval, DSP5680XX_ERROR_FLASHING_CRC, msg);
...}
...}
return retval;
...}
int dsp5680xx_f_unlock(struct target *target)
{
int retval = ERROR_OK;
uint16_t eonce_status;
uint32_t instr;
uint32_t ir_out;
struct jtag_tap *tap_chp;
struct jtag_tap *tap_cpu;
tap_chp = jtag_tap_by_string("dsp568013.chp");
if (!tap_chp) {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_JTAG_TAP_ENABLE_MASTER,
"Failed to get master tap.");
...}
tap_cpu = jtag_tap_by_string("dsp568013.cpu");
if (!tap_cpu) {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_JTAG_TAP_ENABLE_CORE,
"Failed to get master tap.");
...}
retval = eonce_enter_debug_mode_without_reset(target, &eonce_status);
if (retval == ERROR_OK)
LOG_WARNING("Memory was not locked.");
jtag_add_reset(0, 1);
jtag_add_sleep(TIME_DIV_FREESCALE * 200 * 1000);
retval = reset_jtag();
err_check(retval, DSP5680XX_ERROR_JTAG_RESET,
"Failed to reset JTAG state machine");
jtag_add_sleep(150);
tap_chp->enabled = true;
retval = switch_tap(target, tap_chp, tap_cpu);
err_check_propagate(retval);
instr = JTAG_INSTR_DEBUG_REQUEST;
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_CORE_TAP_IRLEN);
err_check_propagate(retval);
jtag_add_sleep(TIME_DIV_FREESCALE * 100 * 1000);
jtag_add_reset(0, 0);
jtag_add_sleep(TIME_DIV_FREESCALE * 300 * 1000);
tap_chp->enabled = false;
retval = switch_tap(target, tap_chp, tap_cpu);
err_check_propagate(retval);
instr = MASTER_TAP_CMD_FLASH_ERASE;
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_MASTER_TAP_IRLEN);
err_check_propagate(retval);
instr = HFM_CLK_DEFAULT;
retval = dsp5680xx_drscan(target, (uint8_t *) &instr, (uint8_t *) &ir_out, 16);
err_check_propagate(retval);
jtag_add_sleep(TIME_DIV_FREESCALE * 150 * 1000);
jtag_add_reset(0, 1);
jtag_add_sleep(TIME_DIV_FREESCALE * 200 * 1000);
retval = reset_jtag();
err_check(retval, DSP5680XX_ERROR_JTAG_RESET,
"Failed to reset JTAG state machine");
jtag_add_sleep(150);
instr = 0x0606ffff;
retval = dsp5680xx_drscan(target, (uint8_t *) &instr, (uint8_t *) &ir_out,
32);
err_check_propagate(retval);
instr = 0x5;
retval =
dsp5680xx_irscan(target, &instr, &ir_out,
DSP5680XX_JTAG_MASTER_TAP_IRLEN);
err_check_propagate(retval);
instr = 0x2;
retval = dsp5680xx_drscan(target, (uint8_t *) &instr, (uint8_t *) &ir_out,
4);
err_check_propagate(retval);
tap_cpu->enabled = true;
tap_chp->enabled = false;
target->state = TARGET_RUNNING;
dsp5680xx_context.debug_mode_enabled = false;
return retval;
...}
int dsp5680xx_f_lock(struct target *target)
{
int retval;
struct jtag_tap *tap_chp;
struct jtag_tap *tap_cpu;
uint16_t lock_word = HFM_LOCK_FLASH;
retval = dsp5680xx_f_wr(target, (uint8_t *)&lock_word, HFM_LOCK_ADDR_L, 2, 1);
err_check_propagate(retval);
jtag_add_reset(0, 1);
jtag_add_sleep(TIME_DIV_FREESCALE * 200 * 1000);
retval = reset_jtag();
err_check(retval, DSP5680XX_ERROR_JTAG_RESET,
"Failed to reset JTAG state machine");
jtag_add_sleep(TIME_DIV_FREESCALE * 100 * 1000);
jtag_add_reset(0, 0);
jtag_add_sleep(TIME_DIV_FREESCALE * 300 * 1000);
tap_chp = jtag_tap_by_string("dsp568013.chp");
if (!tap_chp) {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_JTAG_TAP_ENABLE_MASTER,
"Failed to get master tap.");
...}
tap_cpu = jtag_tap_by_string("dsp568013.cpu");
if (!tap_cpu) {
retval = ERROR_FAIL;
err_check(retval, DSP5680XX_ERROR_JTAG_TAP_ENABLE_CORE,
"Failed to get master tap.");
...}
target->state = TARGET_RUNNING;
dsp5680xx_context.debug_mode_enabled = false;
tap_cpu->enabled = false;
tap_chp->enabled = true;
retval = switch_tap(target, tap_chp, tap_cpu);
return retval;
...}
static int dsp5680xx_step(struct target *target, int current, target_addr_t address,
int handle_breakpoints)
{
err_check(ERROR_FAIL, DSP5680XX_ERROR_NOT_IMPLEMENTED_STEP,
"Not implemented yet.");
...}
struct target_type dsp5680xx_target = {
.name = "dsp5680xx",
.poll = dsp5680xx_poll,
.arch_state = dsp5680xx_arch_state,
.halt = dsp5680xx_halt,
.resume = dsp5680xx_resume,
.step = dsp5680xx_step,
.write_buffer = dsp5680xx_write_buffer,
.read_buffer = dsp5680xx_read_buffer,
.assert_reset = dsp5680xx_assert_reset,
.deassert_reset = dsp5680xx_deassert_reset,
.soft_reset_halt = dsp5680xx_soft_reset_halt,
.read_memory = dsp5680xx_read,
.write_memory = dsp5680xx_write,
.checksum_memory = dsp5680xx_checksum_memory,
.target_create = dsp5680xx_target_create,
.init_target = dsp5680xx_init_target,
...};