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
|
/***************************************************************************
* Copyright (C) 2010 by Oleksandr Tymoshenko <gonzo@bluezbox.com> *
* *
* 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, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "target.h"
#include "jtag/jtag.h"
#include "avr32_jtag.h"
static int avr32_jtag_set_instr(struct avr32_jtag *jtag_info, int new_instr)
{
struct jtag_tap *tap;
int busy = 0;
tap = jtag_info->tap;
if (tap == NULL)
return ERROR_FAIL;
if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr) {
do {
struct scan_field field;
uint8_t t[4] = { 0 };
uint8_t ret[4];
field.num_bits = tap->ir_length;
field.out_value = t;
buf_set_u32(t, 0, field.num_bits, new_instr);
field.in_value = ret;
jtag_add_ir_scan(tap, &field, TAP_IDLE);
if (jtag_execute_queue() != ERROR_OK) {
LOG_ERROR("%s: setting address failed", __func__);
return ERROR_FAIL;
}
busy = buf_get_u32(ret, 2, 1);
} while (busy); /* check for busy bit */
}
return ERROR_OK;
}
static int avr32_jtag_nexus_set_address(struct avr32_jtag *jtag_info,
uint32_t addr, int mode)
{
struct scan_field fields[2];
uint8_t addr_buf[4];
uint8_t busy_buf[4];
int busy;
memset(fields, 0, sizeof(fields));
do {
memset(addr_buf, 0, sizeof(addr_buf));
memset(busy_buf, 0, sizeof(busy_buf));
buf_set_u32(addr_buf, 0, 1, mode);
buf_set_u32(addr_buf, 1, 7, addr);
fields[0].num_bits = 26;
fields[0].in_value = NULL;
fields[0].out_value = NULL;
fields[1].num_bits = 8;
fields[1].in_value = busy_buf;
fields[1].out_value = addr_buf;
jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
if (jtag_execute_queue() != ERROR_OK) {
LOG_ERROR("%s: setting address failed", __func__);
return ERROR_FAIL;
}
busy = buf_get_u32(busy_buf, 6, 1);
} while (busy);
return ERROR_OK;
}
static int avr32_jtag_nexus_read_data(struct avr32_jtag *jtag_info,
uint32_t *pdata)
{
struct scan_field fields[2];
uint8_t data_buf[4];
uint8_t busy_buf[4];
int busy;
do {
memset(data_buf, 0, sizeof(data_buf));
memset(busy_buf, 0, sizeof(busy_buf));
fields[0].num_bits = 32;
fields[0].out_value = NULL;
fields[0].in_value = data_buf;
fields[1].num_bits = 2;
fields[1].in_value = busy_buf;
fields[1].out_value = NULL;
jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
if (jtag_execute_queue() != ERROR_OK) {
LOG_ERROR("%s: reading data failed", __func__);
return ERROR_FAIL;
}
busy = buf_get_u32(busy_buf, 0, 1);
} while (busy);
*pdata = buf_get_u32(data_buf, 0, 32);
return ERROR_OK;
}
static int avr32_jtag_nexus_write_data(struct avr32_jtag *jtag_info,
uint32_t data)
{
struct scan_field fields[2];
uint8_t data_buf[4];
uint8_t busy_buf[4];
uint8_t dummy_buf[4];
int busy;
do {
memset(data_buf, 0, sizeof(data_buf));
memset(busy_buf, 0, sizeof(busy_buf));
memset(dummy_buf, 0, sizeof(dummy_buf));
fields[0].num_bits = 2;
fields[0].in_value = busy_buf;
fields[0].out_value = dummy_buf;
buf_set_u32(data_buf, 0, 32, data);
fields[1].num_bits = 32;
fields[1].in_value = NULL;
fields[1].out_value = data_buf;
jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
if (jtag_execute_queue() != ERROR_OK) {
LOG_ERROR("%s: reading data failed", __func__);
return ERROR_FAIL;
}
busy = buf_get_u32(busy_buf, 0, 0);
} while (busy);
return ERROR_OK;
}
int avr32_jtag_nexus_read(struct avr32_jtag *jtag_info,
uint32_t addr, uint32_t *value)
{
avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
avr32_jtag_nexus_set_address(jtag_info, addr, MODE_READ);
return avr32_jtag_nexus_read_data(jtag_info, value);
}
int avr32_jtag_nexus_write(struct avr32_jtag *jtag_info,
uint32_t addr, uint32_t value)
{
avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
avr32_jtag_nexus_set_address(jtag_info, addr, MODE_WRITE);
return avr32_jtag_nexus_write_data(jtag_info, value);
}
static int avr32_jtag_mwa_set_address(struct avr32_jtag *jtag_info, int slave,
uint32_t addr, int mode)
{
struct scan_field fields[2];
uint8_t addr_buf[4];
uint8_t slave_buf[4];
uint8_t busy_buf[4];
int busy;
memset(fields, 0, sizeof(fields));
do {
memset(addr_buf, 0, sizeof(addr_buf));
memset(busy_buf, 0, sizeof(busy_buf));
memset(slave_buf, 0, sizeof(slave_buf));
buf_set_u32(slave_buf, 0, 4, slave);
buf_set_u32(addr_buf, 0, 1, mode);
buf_set_u32(addr_buf, 1, 30, addr >> 2);
fields[0].num_bits = 31;
fields[0].in_value = NULL;
fields[0].out_value = addr_buf;
fields[1].num_bits = 4;
fields[1].in_value = busy_buf;
fields[1].out_value = slave_buf;
jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
if (jtag_execute_queue() != ERROR_OK) {
LOG_ERROR("%s: setting address failed", __func__);
return ERROR_FAIL;
}
busy = buf_get_u32(busy_buf, 1, 1);
} while (busy);
return ERROR_OK;
}
static int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info,
uint32_t *pdata)
{
struct scan_field fields[2];
uint8_t data_buf[4];
uint8_t busy_buf[4];
int busy;
do {
memset(data_buf, 0, sizeof(data_buf));
memset(busy_buf, 0, sizeof(busy_buf));
fields[0].num_bits = 32;
fields[0].out_value = NULL;
fields[0].in_value = data_buf;
fields[1].num_bits = 3;
fields[1].in_value = busy_buf;
fields[1].out_value = NULL;
jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
if (jtag_execute_queue() != ERROR_OK) {
LOG_ERROR("%s: reading data failed", __func__);
return ERROR_FAIL;
}
busy = buf_get_u32(busy_buf, 0, 1);
} while (busy);
*pdata = buf_get_u32(data_buf, 0, 32);
return ERROR_OK;
}
static int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info,
uint32_t data)
{
struct scan_field fields[2];
uint8_t data_buf[4];
uint8_t busy_buf[4];
uint8_t zero_buf[4];
int busy;
do {
memset(data_buf, 0, sizeof(data_buf));
memset(busy_buf, 0, sizeof(busy_buf));
memset(zero_buf, 0, sizeof(zero_buf));
buf_set_u32(data_buf, 0, 32, data);
fields[0].num_bits = 3;
fields[0].in_value = busy_buf;
fields[0].out_value = zero_buf;
fields[1].num_bits = 32;
fields[1].out_value = data_buf;
fields[1].in_value = NULL;
jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
if (jtag_execute_queue() != ERROR_OK) {
LOG_ERROR("%s: reading data failed", __func__);
return ERROR_FAIL;
}
busy = buf_get_u32(busy_buf, 0, 1);
} while (busy);
return ERROR_OK;
}
int avr32_jtag_mwa_read(struct avr32_jtag *jtag_info, int slave,
uint32_t addr, uint32_t *value)
{
avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_READ);
avr32_jtag_mwa_read_data(jtag_info, value);
return ERROR_OK;
}
int avr32_jtag_mwa_write(struct avr32_jtag *jtag_info, int slave,
uint32_t addr, uint32_t value)
{
avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_WRITE);
avr32_jtag_mwa_write_data(jtag_info, value);
return ERROR_OK;
}
int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
{
int retval;
uint32_t ds;
retval = avr32_jtag_nexus_write(jtag_info, AVR32_OCDREG_DINST, inst);
if (retval != ERROR_OK)
return retval;
do {
retval = avr32_jtag_nexus_read(jtag_info, AVR32_OCDREG_DS, &ds);
if (retval != ERROR_OK)
return retval;
} while ((ds & OCDREG_DS_DBA) && !(ds & OCDREG_DS_INC));
return ERROR_OK;
}
int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
{
uint32_t value;
int res;
res = avr32_jtag_nexus_read(jtag, reg, &value);
if (res)
return res;
value |= bits;
res = avr32_jtag_nexus_write(jtag, reg, value);
if (res)
return res;
return ERROR_OK;
}
int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
{
uint32_t value;
int res;
res = avr32_jtag_nexus_read(jtag, reg, &value);
if (res)
return res;
value &= ~bits;
res = avr32_jtag_nexus_write(jtag, reg, value);
if (res)
return res;
return ERROR_OK;
}
|