-
Notifications
You must be signed in to change notification settings - Fork 3
/
progalgxcf.cpp
457 lines (393 loc) · 11.2 KB
/
progalgxcf.cpp
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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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
/* XCF Flash PROM JTAG programming algorithms
Copyright (C) 2004-2011 Andrew Rogers
2009 Uwe Bonnes
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Changes:
Dmitry Teytelman [[email protected]] 14 Jun 2006 [applied 13 Aug 2006]:
Code cleanup for clean -Wall compile.
*/
#include <string.h>
#include <stdexcept>
#include "progalgxcf.h"
#include "utilities.h"
const byte ProgAlgXCF::SERASE=0x0a;
const byte ProgAlgXCF::ISCTESTSTATUS=0xe3;
const byte ProgAlgXCF::ISC_ENABLE=0xe8;
const byte ProgAlgXCF::ISC_PROGRAM=0xea;
const byte ProgAlgXCF::ISC_ADDRESS_SHIFT=0xeb;
const byte ProgAlgXCF::ISC_ERASE=0xec;
const byte ProgAlgXCF::ISC_DATA_SHIFT=0xed;
const byte ProgAlgXCF::CONFIG=0xee;
const byte ProgAlgXCF::ISC_READ=0xef;
const byte ProgAlgXCF::ISC_DISABLE=0xf0;
const byte ProgAlgXCF::IDCODE=0xfe;
const byte ProgAlgXCF::BYPASS=0xff;
const byte ProgAlgXCF::BIT3=0x08;
const byte ProgAlgXCF::BIT4=0x10;
static const unsigned int MAX_BLOCK_SIZE = 4096;
static const unsigned int FRAMES_PER_BLOCK = 32;
ProgAlgXCF::ProgAlgXCF(Jtag &j, int size_ind)
{
use_optimized_algs = false;
switch (size_ind & 0xfffffef) /* part number of XC18V has X in bitmask */
{
case 0x23:
size = 1<<19;
block_size = 2048;
break;
case 0x24:
size = 1<<20;
block_size = 2048;
break;
case 0x25:
size = 2<<20;
block_size = 4096;
break;
case 0x26:
size = 4<<20;
block_size = 4096;
break;
case 0x44:
size = 1<<20;
block_size = 2048;
use_optimized_algs = true;
break;
case 0x45:
size = 2<<20;
block_size = 4096;
use_optimized_algs = true;
break;
case 0x46:
size = 4<<20;
block_size = 4096;
use_optimized_algs = true;
break;
default:
fprintf(stderr,"Unknown XCF device size code %x\n", size_ind);
throw std::invalid_argument("Unknown XCF device size code");
}
jtag=&j;
}
/* For XCF, implement "xflow_erase_optimized" for the serial devices
* from the XCF..1532.bsd" files
* and for XC18V flow_erase
*/
int ProgAlgXCF::erase()
{
byte data[4];
int i;
byte ircap[1];
Timer timer;
jtag->shiftIR(&ISC_DISABLE);
jtag->Usleep(110000);
jtag->shiftIR(&BYPASS,ircap);
if((ircap[0]&BIT3)==BIT3){
fprintf(stderr,"Device is write protected! Aborting\n");
return 1;
}
jtag->shiftIR(&ISC_ENABLE);
if (! use_optimized_algs)
data[0]=0x34;
else
data[0]=0x37;
jtag->shiftDR(data,0,6);
jtag->shiftIR(&ISC_ADDRESS_SHIFT);
jtag->longToByteArray(1,data);
jtag->shiftDR(data,0,16);
jtag->cycleTCK(1);
if(jtag->getVerbose())
{
fprintf(stderr, "Erasing");
fflush(stderr);
}
jtag->shiftIR(&ISC_ERASE);
for(i=0; i<32;i++)
{
byte xcstatus[1];
if(jtag->getVerbose())
{
fprintf(stderr, ".");
fflush(stderr);
}
jtag->Usleep(500000);
if (! use_optimized_algs)
{
if (i == 31)
break;
}
else
{
jtag->shiftIR(&ISCTESTSTATUS);
jtag->shiftDR(0,xcstatus,8);
if (xcstatus[0] & 0x04)
break;
}
}
if (i > 31)
{
fprintf(stderr,"\nErased failed! Aborting\n");
return 1;
}
if (jtag->getVerbose())
fprintf(stderr, "done\nErase time %.1f ms\n", timer.elapsed() * 1.0e3);
return 0;
}
int ProgAlgXCF::program(BitFile &file)
{
byte data[MAX_BLOCK_SIZE/8];
unsigned int skipbits, nbits;
skipbits = file.getOffset() * 8;
if (skipbits % block_size != 0)
{
fprintf(stderr, "Programming does not start at block boundary (offset = %u bits), aborting\n", skipbits);
return -1;
}
if (skipbits > size)
{
fprintf(stderr,"Program start outside PROM area (offset = %u bits), aborting\n", skipbits);
return -1;
}
if (file.getRLength() != 0)
nbits = file.getRLength() * 8;
else
nbits = file.getLength();
if (nbits > size - skipbits)
{
fprintf(stderr,"Program outside PROM areas requested, clipping\n");
nbits = size - skipbits;
}
if (nbits % block_size != 0)
fprintf(stderr, "Programming does not end at block boundary (nbits = %u), padding\n", nbits);
unsigned int skipblocks = skipbits / block_size;
unsigned int nblocks = (nbits + block_size - 1) / block_size;
Timer timer;
jtag->shiftIR(&ISC_DISABLE);
jtag->Usleep(1000);
jtag->setTapState(Jtag::TEST_LOGIC_RESET);
jtag->shiftIR(&ISC_ENABLE);
if (! use_optimized_algs)
data[0]=0x34;
else
data[0]=0x37;
jtag->shiftDR(data,0,6);
for (unsigned int i = 0; i < nblocks; i++)
{
unsigned int frame = (skipblocks + i) * FRAMES_PER_BLOCK;
int j;
if (jtag->getVerbose())
{
fprintf(stderr, "\rProgramming block %6u/%6u at XCF frame 0x%04x",
i, nblocks, frame);
fflush(stderr);
}
jtag->shiftIR(&ISC_DATA_SHIFT);
if ((i+1) * block_size <= nbits)
{
jtag->shiftDR(&(file.getData())[i*block_size/8], 0, block_size);
}
else
{
unsigned int rembytes = (nbits - (i * block_size) + 7) / 8;
unsigned int padbytes = block_size / 8 - rembytes;
memset(data, 0xff, padbytes);
jtag->shiftDR(&(file.getData())[i*block_size/8], 0, rembytes*8, 0, false);
jtag->shiftDR(data, 0, padbytes*8);
}
jtag->longToByteArray(frame, data);
jtag->shiftIR(&ISC_ADDRESS_SHIFT);
jtag->cycleTCK(1);
jtag->shiftDR(data,0,16);
jtag->cycleTCK(1);
jtag->shiftIR(&ISC_PROGRAM);
if (! use_optimized_algs)
{
jtag->Usleep(14000);
}
else
{
for (j = 0; j < 28; j++)
{
byte xcstatus[1];
jtag->Usleep(500);
jtag->shiftIR(&ISCTESTSTATUS);
jtag->shiftDR(0,xcstatus,8);
if (xcstatus[0] & 0x04)
break;
else if(jtag->getVerbose())
{
fprintf(stderr, ".");
fflush(stderr);
}
}
if (j == 28)
{
fprintf(stderr,"\nProgramming block %u (frame 0x%04x) failed! Aborting\n", i, frame);
return 1;
}
}
}
if (! use_optimized_algs) /* only on XC18V*/
{
jtag->shiftIR(&ISC_ADDRESS_SHIFT);
jtag->cycleTCK(1);
jtag->longToByteArray(1,data);
jtag->shiftDR(data, 0, 16);
jtag->cycleTCK(1);
jtag->shiftIR(&SERASE);
jtag->Usleep(37000);
}
if (jtag->getVerbose())
fprintf(stderr, "done\nProgramming time %.1f ms\n", timer.elapsed() * 1.0e3);
jtag->shiftIR(&BYPASS);
jtag->cycleTCK(1);
jtag->tapTestLogicReset();
return 0;
}
int ProgAlgXCF::verify(BitFile &file)
{
byte data[MAX_BLOCK_SIZE/8];
unsigned int skipbits, nbits;
skipbits = file.getOffset() * 8;
if (skipbits % block_size != 0)
{
fprintf(stderr, "Verify does not start at block boundary (offset = %u bits), aborting\n", skipbits);
return 1;
}
if (skipbits > size)
{
fprintf(stderr,"Verify start outside PROM area (offset = %u bits), aborting\n", skipbits);
return -1;
}
if (file.getRLength() != 0)
nbits = file.getRLength() * 8;
else
nbits = file.getLength();
if (nbits > size - skipbits)
{
fprintf(stderr,"Verify outside PROM areas requested, clipping\n");
nbits = size - skipbits;
}
unsigned int skipblocks = skipbits / block_size;
unsigned int nblocks = (nbits + block_size - 1) / block_size;
Timer timer;
jtag->setTapState(Jtag::TEST_LOGIC_RESET);
jtag->shiftIR(&ISC_ENABLE);
data[0]=0x34;
jtag->shiftDR(data,0,6);
for (unsigned int i = 0; i < nblocks; i++)
{
unsigned int frame = (skipblocks + i) * FRAMES_PER_BLOCK;
int res;
if(jtag->getVerbose())
{
fprintf(stderr, "\rVerify block %6u/%6u at XCF frame 0x%04x",
i, nblocks, frame);
fflush(stderr);
}
jtag->longToByteArray(frame,data);
jtag->shiftIR(&ISC_ADDRESS_SHIFT);
jtag->shiftDR(data,0,16);
jtag->cycleTCK(1);
jtag->shiftIR(&ISC_READ);
jtag->Usleep(50);
jtag->shiftDR(0,data,block_size);
unsigned int blkbytes = block_size / 8;
if ((i + 1) * block_size > nbits)
blkbytes = (nbits - (i * block_size) + 7) / 8;
res = memcmp(data, &(file.getData())[i*block_size/8], blkbytes);
if (res)
{
fprintf(stderr, "\nVerify failed at block %u (frame 0x%04x)\n",
i, frame);
return res;
}
}
if (jtag->getVerbose())
fprintf(stderr, "\nSuccess! Verify time %.1f ms\n", timer.elapsed() * 1.0e3);
jtag->tapTestLogicReset();
return 0;
}
int ProgAlgXCF::read(BitFile &file)
{
byte data[MAX_BLOCK_SIZE/8];
unsigned int skipbits, nbits;
skipbits = file.getOffset() * 8;
if (skipbits % block_size != 0)
{
fprintf(stderr, "Read does not start at block boundary (offset = %u bits), aborting\n", skipbits);
return -1;
}
if (skipbits > size)
{
fprintf(stderr,"Read start outside PROM area (offset = %u bits), aborting\n", skipbits);
return -1;
}
if (file.getRLength() != 0)
nbits = file.getRLength() * 8;
else
nbits = size - skipbits;
if (nbits > size - skipbits)
{
fprintf(stderr,"Read outside PROM areas requested, clipping\n");
nbits = size - skipbits;
}
unsigned int skipblocks = skipbits / block_size;
unsigned int nblocks = (nbits + block_size - 1) / block_size;
file.setLength(nbits);
Timer timer;
jtag->setTapState(Jtag::TEST_LOGIC_RESET);
jtag->shiftIR(&ISC_ENABLE);
data[0]=0x34;
jtag->shiftDR(data,0,6);
for (unsigned int i = 0; i < nblocks; i++)
{
unsigned int frame = (skipblocks + i) * FRAMES_PER_BLOCK;
if(jtag->getVerbose())
{
fprintf(stderr, "\rReading block %6u/%6u at XCF frame 0x%04x",
i, nblocks, frame);
fflush(stderr);
}
jtag->longToByteArray(frame,data);
jtag->shiftIR(&ISC_ADDRESS_SHIFT);
jtag->shiftDR(data,0,16);
jtag->cycleTCK(1);
jtag->shiftIR(&ISC_READ);
jtag->Usleep(50);
jtag->shiftDR(0,data,block_size);
unsigned int blkbytes = block_size / 8;
if ((i + 1) * block_size > nbits)
blkbytes = (nbits - (i * block_size) + 7) / 8;
memcpy(&(file.getData())[i*block_size/8], data, blkbytes);
}
if (jtag->getVerbose())
fprintf(stderr, "\nSuccess! Read time %.1f ms\n", timer.elapsed() * 1.0e3);
jtag->tapTestLogicReset();
return 0;
}
void ProgAlgXCF::disable()
{
jtag->shiftIR(&ISC_DISABLE);
jtag->Usleep(110000);
jtag->shiftIR(&BYPASS);
jtag->cycleTCK(1);
jtag->tapTestLogicReset();
}
void ProgAlgXCF::reconfig(void)
{
jtag->shiftIR(&CONFIG);
jtag->cycleTCK(1);
jtag->shiftIR(&BYPASS);
jtag->cycleTCK(1);
jtag->tapTestLogicReset();
}