-
Notifications
You must be signed in to change notification settings - Fork 385
Expand file tree
/
Copy pathanlogic.cpp
More file actions
279 lines (245 loc) · 6.85 KB
/
Copy pathanlogic.cpp
File metadata and controls
279 lines (245 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright (C) 2020 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
*/
#include <string.h>
#include <stdexcept>
#include "anlogic.hpp"
#include "anlogicBitParser.hpp"
#include "jtag.hpp"
#include "device.hpp"
#include "display.hpp"
#include "progressBar.hpp"
#include "spiFlash.hpp"
#define REFRESH 0x01
#define IDCODE 0x06
#define JTAG_PROGRAM 0x30
#define SPI_PROGRAM 0x39
#define CFG_IN 0x3b
#define JTAG_START 0x3d
#define BYPASS 0xFF
#define IRLENGTH 8
Anlogic::Anlogic(Jtag *jtag, const std::string &filename,
const std::string &file_type,
Device::prog_type_t prg_type, bool verify, int8_t verbose):
Device(jtag, filename, file_type, verify, verbose),
FlashInterface(filename, verbose, 0, verify), _target_freq(0)
{
if (prg_type == Device::RD_FLASH) {
_mode = Device::READ_MODE;
} else if (!_file_extension.empty()) {
if (_file_extension == "svf") {
_mode = Device::MEM_MODE;
} else if (_file_extension == "bit") {
_mode = (prg_type == Device::WR_SRAM)? Device::MEM_MODE:
Device::SPI_MODE;
} else {
if (prg_type == Device::WR_FLASH)
_mode = Device::SPI_MODE;
else
throw std::runtime_error("incompatible file format");
}
}
}
Anlogic::~Anlogic()
{}
void Anlogic::reset()
{
_jtag->shiftIR(BYPASS, IRLENGTH);
_jtag->shiftIR(REFRESH, IRLENGTH);
_jtag->toggleClk(15);
_jtag->shiftIR(BYPASS, IRLENGTH);
_jtag->toggleClk(200000);
}
void Anlogic::program(unsigned int offset, bool unprotect_flash)
{
if (_mode == Device::NONE_MODE)
return;
AnlogicBitParser bit(_filename, (_mode == Device::MEM_MODE), _verbose);
printInfo("Parse file ", false);
if (bit.parse() == EXIT_FAILURE) {
printError("FAIL");
return;
}
printSuccess("DONE");
if (_verbose)
bit.displayHeader();
const uint8_t *data = bit.getData();
int len = bit.getLength() / 8;
if (_mode == Device::SPI_MODE) {
FlashInterface::write(offset, data, len, unprotect_flash);
return;
}
if (_mode == Device::MEM_MODE) {
// Loading device with 'bypass' instruction.
_jtag->shiftIR(BYPASS, IRLENGTH);
_jtag->shiftIR(BYPASS, IRLENGTH);
// Verify Device id.
// SIR 8 TDI (06) ;
// SDR 32 TDI (00000000) TDO (0a014c35) MASK (ffffffff) ;
// Boundary Scan Chain Contents
// Position 1: BG256
// Loading device with 'refresh' instruction.
_jtag->shiftIR(REFRESH, IRLENGTH);
// Loading device with 'bypass' & 'spi_program' instruction.
_jtag->shiftIR(BYPASS, IRLENGTH);
_jtag->shiftIR(SPI_PROGRAM, IRLENGTH);
_jtag->toggleClk(50000);
// Loading device with 'jtag program' instruction.
_jtag->shiftIR(JTAG_PROGRAM, IRLENGTH);
_jtag->toggleClk(15);
// Loading device with a `cfg_in` instruction.
_jtag->shiftIR(CFG_IN, IRLENGTH);
_jtag->toggleClk(15);
ProgressBar progress("Loading", len, 50, _quiet);
int pos = 0;
const uint8_t *ptr = data;
while (len > 0) {
int xfer_len = (len > 512)?512:len;
Jtag::tapState_t tx_end = (len - xfer_len) ? Jtag::SHIFT_DR : Jtag::RUN_TEST_IDLE;
_jtag->shiftDR(ptr, NULL, xfer_len * 8, tx_end);
len -= xfer_len;
progress.display(pos);
pos += xfer_len;
ptr+=xfer_len;
}
progress.done();
_jtag->toggleClk(100);
// Loading device with a `jtag start` instruction.
_jtag->shiftIR(JTAG_START, IRLENGTH);
_jtag->toggleClk(15);
// Loading device with 'bypass' instruction.
_jtag->shiftIR(BYPASS, IRLENGTH);
_jtag->toggleClk(1000);
// ??
_jtag->shiftIR(0x31, IRLENGTH);
_jtag->toggleClk(100);
_jtag->shiftIR(JTAG_START, IRLENGTH);
_jtag->toggleClk(15);
_jtag->shiftIR(BYPASS, IRLENGTH);
_jtag->toggleClk(15);
}
}
uint32_t Anlogic::idCode()
{
unsigned char tx_data[4];
unsigned char rx_data[4];
tx_data[0] = IDCODE;
_jtag->go_test_logic_reset();
_jtag->shiftIR(tx_data, NULL, IRLENGTH);
memset(tx_data, 0, 4);
_jtag->shiftDR(tx_data, rx_data, 32);
return ((rx_data[0] & 0x000000ff) |
((rx_data[1] << 8) & 0x0000ff00) |
((rx_data[2] << 16) & 0x00ff0000) |
((rx_data[3] << 24) & 0xff000000));
}
bool Anlogic::prepare_flash_access()
{
_target_freq = _jtag->getClkFreq();
if (_target_freq > 6000000) {
_jtag->setClkFreq(6000000);
}
for (int i = 0; i < 5; i++)
_jtag->shiftIR(BYPASS, IRLENGTH);
//Verify Device id.
//SIR 8 TDI (06) ;
//SDR 32 TDI (00000000) TDO (0a014c35) MASK (ffffffff) ;
//Boundary Scan Chain Contents
//Position 1: BG256
//Loading device with 'refresh' instruction.
_jtag->shiftIR(REFRESH, IRLENGTH);
//Loading device with 'bypass' & 'spi_program' instruction.
_jtag->shiftIR(BYPASS, IRLENGTH);
_jtag->shiftIR(SPI_PROGRAM, IRLENGTH);
for (int i = 0; i < 4; i++)
_jtag->toggleClk(50000);
return true;
}
void Anlogic::restore_flash_access_frequency()
{
if (_target_freq > 6000000) {
_jtag->setClkFreq(_target_freq);
}
}
/* SPI wrapper
* For read operation a delay of one bit is added
* So add one bit more and move everything by one
* In write only operations to care about this delay
*/
int Anlogic::spi_put(uint8_t cmd, const uint8_t *tx, uint8_t *rx, uint32_t len)
{
int xfer_len = len + 1;
if (rx)
xfer_len++;
std::vector<uint8_t> jtx, jrx;
jtx.resize(xfer_len);
jrx.resize(xfer_len);
jtx[0] = AnlogicBitParser::reverseByte(cmd);
if (tx != NULL) {
for (uint32_t i = 0; i < len; i++)
jtx[i+1] = AnlogicBitParser::reverseByte(tx[i]);
}
/* write anlogic command before sending packet */
uint8_t op = 0x60;
_jtag->shiftDR(&op, NULL, 8);
_jtag->shiftDR(jtx.data(), (rx == NULL)? NULL: jrx.data(), 8*xfer_len);
if (rx != NULL) {
for (uint32_t i=0; i < len; i++)
rx[i] = AnlogicBitParser::reverseByte(jrx[i+1]>>1)
| (jrx[i+2]&0x01);
}
return 0;
}
int Anlogic::spi_put(const uint8_t *tx, uint8_t *rx, uint32_t len)
{
int xfer_len = len;
if (rx)
xfer_len++;
std::vector<uint8_t> jtx, jrx;
jtx.resize(xfer_len);
jrx.resize(xfer_len);
if (tx != NULL) {
for (uint32_t i = 0; i < len; i++)
jtx[i] = AnlogicBitParser::reverseByte(tx[i]);
}
/* write anlogic command before sending packet */
uint8_t op = 0x60;
_jtag->shiftDR(&op, NULL, 8);
_jtag->shiftDR(jtx.data(), (rx == NULL)? NULL: jrx.data(), 8*xfer_len);
if (rx != NULL) {
for (uint32_t i=0; i < len; i++)
rx[i] = AnlogicBitParser::reverseByte(jrx[i]>>1) |
(jrx[i+1]&0x01);
}
return 0;
}
int Anlogic::spi_wait(uint8_t cmd, uint8_t mask, uint8_t cond,
uint32_t timeout, bool verbose)
{
uint8_t rx[3];
uint8_t tx[3];
tx[0] = AnlogicBitParser::reverseByte(cmd);
uint8_t op = 0x60;
uint8_t tmp;
uint32_t count = 0;
do {
_jtag->shiftDR(&op, NULL, 8);
_jtag->shiftDR(tx, rx, 8 * 3);
tmp = (AnlogicBitParser::reverseByte(rx[1]>>1)) | (0x01 & rx[2]);
count ++;
if (count == timeout) {
printf("timeout: %x %x %x\n", tmp, rx[0], rx[1]);
break;
}
if (verbose) {
printf("%x %x %x %u\n", tmp, mask, cond, count);
}
} while ((tmp & mask) != cond);
if (count == timeout) {
printf("%02x\n", tmp);
std::cout << "wait: Error" << std::endl;
return -ETIME;
}
return 0;
}