OpenOCD
avr32_jtag.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2010 by Oleksandr Tymoshenko <gonzo@bluezbox.com> *
5  ***************************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "target.h"
12 #include "jtag/jtag.h"
13 #include "avr32_jtag.h"
14 
15 static int avr32_jtag_set_instr(struct avr32_jtag *jtag_info, int new_instr)
16 {
17  struct jtag_tap *tap;
18  int busy = 0;
19 
20  tap = jtag_info->tap;
21  if (!tap)
22  return ERROR_FAIL;
23 
24  if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr) {
25  do {
26  struct scan_field field;
27  uint8_t t[4] = { 0 };
28  uint8_t ret[4];
29 
30  field.num_bits = tap->ir_length;
31  field.out_value = t;
32  buf_set_u32(t, 0, field.num_bits, new_instr);
33  field.in_value = ret;
34 
35  jtag_add_ir_scan(tap, &field, TAP_IDLE);
36  if (jtag_execute_queue() != ERROR_OK) {
37  LOG_ERROR("%s: setting address failed", __func__);
38  return ERROR_FAIL;
39  }
40  busy = buf_get_u32(ret, 2, 1);
41  } while (busy); /* check for busy bit */
42  }
43 
44  return ERROR_OK;
45 }
46 
47 static int avr32_jtag_nexus_set_address(struct avr32_jtag *jtag_info,
48  uint32_t addr, int mode)
49 {
50  struct scan_field fields[2];
51  uint8_t addr_buf[4];
52  uint8_t busy_buf[4];
53  int busy;
54 
55  memset(fields, 0, sizeof(fields));
56 
57  do {
58  memset(addr_buf, 0, sizeof(addr_buf));
59  memset(busy_buf, 0, sizeof(busy_buf));
60 
61  buf_set_u32(addr_buf, 0, 1, mode);
62  buf_set_u32(addr_buf, 1, 7, addr);
63 
64  fields[0].num_bits = 26;
65  fields[0].in_value = NULL;
66  fields[0].out_value = NULL;
67 
68  fields[1].num_bits = 8;
69  fields[1].in_value = busy_buf;
70  fields[1].out_value = addr_buf;
71 
72  jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
73  if (jtag_execute_queue() != ERROR_OK) {
74  LOG_ERROR("%s: setting address failed", __func__);
75  return ERROR_FAIL;
76  }
77  busy = buf_get_u32(busy_buf, 6, 1);
78  } while (busy);
79 
80  return ERROR_OK;
81 }
82 
83 
84 static int avr32_jtag_nexus_read_data(struct avr32_jtag *jtag_info,
85  uint32_t *pdata)
86 {
87 
88  struct scan_field fields[2];
89  uint8_t data_buf[4];
90  uint8_t busy_buf[4];
91  int busy;
92 
93  do {
94  memset(data_buf, 0, sizeof(data_buf));
95  memset(busy_buf, 0, sizeof(busy_buf));
96 
97  fields[0].num_bits = 32;
98  fields[0].out_value = NULL;
99  fields[0].in_value = data_buf;
100 
101 
102  fields[1].num_bits = 2;
103  fields[1].in_value = busy_buf;
104  fields[1].out_value = NULL;
105 
106  jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
107 
108  if (jtag_execute_queue() != ERROR_OK) {
109  LOG_ERROR("%s: reading data failed", __func__);
110  return ERROR_FAIL;
111  }
112 
113  busy = buf_get_u32(busy_buf, 0, 1);
114  } while (busy);
115 
116  *pdata = buf_get_u32(data_buf, 0, 32);
117 
118  return ERROR_OK;
119 }
120 
121 static int avr32_jtag_nexus_write_data(struct avr32_jtag *jtag_info,
122  uint32_t data)
123 {
124 
125  struct scan_field fields[2];
126  uint8_t data_buf[4];
127  uint8_t busy_buf[4];
128  uint8_t dummy_buf[4];
129  int busy;
130 
131  do {
132  memset(data_buf, 0, sizeof(data_buf));
133  memset(busy_buf, 0, sizeof(busy_buf));
134  memset(dummy_buf, 0, sizeof(dummy_buf));
135 
136  fields[0].num_bits = 2;
137  fields[0].in_value = busy_buf;
138  fields[0].out_value = dummy_buf;
139 
140 
141  buf_set_u32(data_buf, 0, 32, data);
142  fields[1].num_bits = 32;
143  fields[1].in_value = NULL;
144  fields[1].out_value = data_buf;
145 
146  jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
147 
148  if (jtag_execute_queue() != ERROR_OK) {
149  LOG_ERROR("%s: reading data failed", __func__);
150  return ERROR_FAIL;
151  }
152 
153  busy = buf_get_u32(busy_buf, 0, 0);
154  } while (busy);
155 
156 
157  return ERROR_OK;
158 }
159 
160 int avr32_jtag_nexus_read(struct avr32_jtag *jtag_info,
161  uint32_t addr, uint32_t *value)
162 {
165  return avr32_jtag_nexus_read_data(jtag_info, value);
166 }
167 
168 int avr32_jtag_nexus_write(struct avr32_jtag *jtag_info,
169  uint32_t addr, uint32_t value)
170 {
173  return avr32_jtag_nexus_write_data(jtag_info, value);
174 }
175 
176 static int avr32_jtag_mwa_set_address(struct avr32_jtag *jtag_info, int slave,
177  uint32_t addr, int mode)
178 {
179  struct scan_field fields[2];
180  uint8_t addr_buf[4];
181  uint8_t slave_buf[4];
182  uint8_t busy_buf[4];
183  int busy;
184 
185  memset(fields, 0, sizeof(fields));
186 
187  do {
188  memset(addr_buf, 0, sizeof(addr_buf));
189  memset(busy_buf, 0, sizeof(busy_buf));
190  memset(slave_buf, 0, sizeof(slave_buf));
191 
192  buf_set_u32(slave_buf, 0, 4, slave);
193  buf_set_u32(addr_buf, 0, 1, mode);
194  buf_set_u32(addr_buf, 1, 30, addr >> 2);
195 
196  fields[0].num_bits = 31;
197  fields[0].in_value = NULL;
198  fields[0].out_value = addr_buf;
199 
200  fields[1].num_bits = 4;
201  fields[1].in_value = busy_buf;
202  fields[1].out_value = slave_buf;
203 
204  jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
205  if (jtag_execute_queue() != ERROR_OK) {
206  LOG_ERROR("%s: setting address failed", __func__);
207  return ERROR_FAIL;
208  }
209  busy = buf_get_u32(busy_buf, 1, 1);
210  } while (busy);
211 
212  return ERROR_OK;
213 }
214 
215 static int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info,
216  uint32_t *pdata)
217 {
218 
219  struct scan_field fields[2];
220  uint8_t data_buf[4];
221  uint8_t busy_buf[4];
222  int busy;
223 
224  do {
225  memset(data_buf, 0, sizeof(data_buf));
226  memset(busy_buf, 0, sizeof(busy_buf));
227 
228  fields[0].num_bits = 32;
229  fields[0].out_value = NULL;
230  fields[0].in_value = data_buf;
231 
232 
233  fields[1].num_bits = 3;
234  fields[1].in_value = busy_buf;
235  fields[1].out_value = NULL;
236 
237  jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
238 
239  if (jtag_execute_queue() != ERROR_OK) {
240  LOG_ERROR("%s: reading data failed", __func__);
241  return ERROR_FAIL;
242  }
243 
244  busy = buf_get_u32(busy_buf, 0, 1);
245  } while (busy);
246 
247  *pdata = buf_get_u32(data_buf, 0, 32);
248 
249  return ERROR_OK;
250 }
251 
252 static int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info,
253  uint32_t data)
254 {
255 
256  struct scan_field fields[2];
257  uint8_t data_buf[4];
258  uint8_t busy_buf[4];
259  uint8_t zero_buf[4];
260  int busy;
261 
262  do {
263  memset(data_buf, 0, sizeof(data_buf));
264  memset(busy_buf, 0, sizeof(busy_buf));
265  memset(zero_buf, 0, sizeof(zero_buf));
266 
267  buf_set_u32(data_buf, 0, 32, data);
268  fields[0].num_bits = 3;
269  fields[0].in_value = busy_buf;
270  fields[0].out_value = zero_buf;
271 
272  fields[1].num_bits = 32;
273  fields[1].out_value = data_buf;
274  fields[1].in_value = NULL;
275 
276 
277  jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
278 
279  if (jtag_execute_queue() != ERROR_OK) {
280  LOG_ERROR("%s: reading data failed", __func__);
281  return ERROR_FAIL;
282  }
283 
284  busy = buf_get_u32(busy_buf, 0, 1);
285  } while (busy);
286 
287  return ERROR_OK;
288 }
289 
290 int avr32_jtag_mwa_read(struct avr32_jtag *jtag_info, int slave,
291  uint32_t addr, uint32_t *value)
292 {
294  avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_READ);
295  avr32_jtag_mwa_read_data(jtag_info, value);
296 
297  return ERROR_OK;
298 }
299 
300 int avr32_jtag_mwa_write(struct avr32_jtag *jtag_info, int slave,
301  uint32_t addr, uint32_t value)
302 {
304  avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_WRITE);
305  avr32_jtag_mwa_write_data(jtag_info, value);
306 
307  return ERROR_OK;
308 }
309 
310 int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
311 {
312  int retval;
313  uint32_t ds;
314 
315  retval = avr32_jtag_nexus_write(jtag_info, AVR32_OCDREG_DINST, inst);
316  if (retval != ERROR_OK)
317  return retval;
318 
319  do {
320  retval = avr32_jtag_nexus_read(jtag_info, AVR32_OCDREG_DS, &ds);
321  if (retval != ERROR_OK)
322  return retval;
323  } while ((ds & OCDREG_DS_DBA) && !(ds & OCDREG_DS_INC));
324 
325  return ERROR_OK;
326 }
327 
328 int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
329 {
330  uint32_t value;
331  int res;
332 
333  res = avr32_jtag_nexus_read(jtag, reg, &value);
334  if (res)
335  return res;
336 
337  value |= bits;
338  res = avr32_jtag_nexus_write(jtag, reg, value);
339  if (res)
340  return res;
341 
342  return ERROR_OK;
343 }
344 
345 int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
346 {
347  uint32_t value;
348  int res;
349 
350  res = avr32_jtag_nexus_read(jtag, reg, &value);
351  if (res)
352  return res;
353 
354  value &= ~bits;
355  res = avr32_jtag_nexus_write(jtag, reg, value);
356  if (res)
357  return res;
358 
359  return ERROR_OK;
360 }
enum arm_mode mode
Definition: armv4_5.c:277
int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
Definition: avr32_jtag.c:310
static int avr32_jtag_set_instr(struct avr32_jtag *jtag_info, int new_instr)
Definition: avr32_jtag.c:15
static int avr32_jtag_nexus_write_data(struct avr32_jtag *jtag_info, uint32_t data)
Definition: avr32_jtag.c:121
int avr32_jtag_nexus_write(struct avr32_jtag *jtag_info, uint32_t addr, uint32_t value)
Definition: avr32_jtag.c:168
static int avr32_jtag_nexus_set_address(struct avr32_jtag *jtag_info, uint32_t addr, int mode)
Definition: avr32_jtag.c:47
static int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info, uint32_t *pdata)
Definition: avr32_jtag.c:215
static int avr32_jtag_nexus_read_data(struct avr32_jtag *jtag_info, uint32_t *pdata)
Definition: avr32_jtag.c:84
int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
Definition: avr32_jtag.c:328
int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
Definition: avr32_jtag.c:345
int avr32_jtag_nexus_read(struct avr32_jtag *jtag_info, uint32_t addr, uint32_t *value)
Definition: avr32_jtag.c:160
static int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info, uint32_t data)
Definition: avr32_jtag.c:252
int avr32_jtag_mwa_write(struct avr32_jtag *jtag_info, int slave, uint32_t addr, uint32_t value)
Definition: avr32_jtag.c:300
static int avr32_jtag_mwa_set_address(struct avr32_jtag *jtag_info, int slave, uint32_t addr, int mode)
Definition: avr32_jtag.c:176
int avr32_jtag_mwa_read(struct avr32_jtag *jtag_info, int slave, uint32_t addr, uint32_t *value)
Definition: avr32_jtag.c:290
#define AVR32_OCDREG_DS
Definition: avr32_jtag.h:34
#define AVR32_OCDREG_DINST
Definition: avr32_jtag.h:48
#define OCDREG_DS_DBA
Definition: avr32_jtag.h:44
#define MODE_WRITE
Definition: avr32_jtag.h:59
#define MODE_READ
Definition: avr32_jtag.h:60
#define AVR32_INST_NEXUS_ACCESS
Definition: avr32_jtag.h:14
#define OCDREG_DS_INC
Definition: avr32_jtag.h:42
#define AVR32_INST_MW_ACCESS
Definition: avr32_jtag.h:15
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:99
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:31
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1037
void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
Definition: jtag/core.c:374
void jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, tap_state_t state)
Generate a DR SCAN using the fields passed to the function.
Definition: jtag/core.c:451
The JTAG interface can be implemented with a software or hardware fifo.
@ TAP_IDLE
Definition: jtag.h:53
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define ERROR_OK
Definition: log.h:164
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct jtag_tap * tap
Definition: avr32_jtag.h:73
Definition: jtag.h:101
uint8_t * cur_instr
current instruction
Definition: jtag.h:132
int ir_length
size of instruction register
Definition: jtag.h:110
Definition: register.h:111
This structure defines a single scan field in the scan.
Definition: jtag.h:87
int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
#define NULL
Definition: usb.h:16