Some minor URL fixes and typo fixes, no functional changes.
[openocd.git] / src / target / avr32_jtag.c
blob647afd5e45f88c2ef77c38d75a14e65b12074e06
1 /***************************************************************************
2 * Copyright (C) 2010 by Oleksandr Tymoshenko <gonzo@bluezbox.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
18 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "target.h"
25 #include "jtag/jtag.h"
26 #include "avr32_jtag.h"
28 static int avr32_jtag_set_instr(struct avr32_jtag *jtag_info, int new_instr)
30 struct jtag_tap *tap;
31 int busy = 0;
33 tap = jtag_info->tap;
34 if (tap == NULL)
35 return ERROR_FAIL;
37 if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr) {
38 do {
39 struct scan_field field;
40 uint8_t t[4];
41 uint8_t ret[4];
43 field.num_bits = tap->ir_length;
44 field.out_value = t;
45 buf_set_u32(t, 0, field.num_bits, new_instr);
46 field.in_value = ret;
48 jtag_add_ir_scan(tap, &field, TAP_IDLE);
49 if (jtag_execute_queue() != ERROR_OK) {
50 LOG_ERROR("%s: setting address failed", __func__);
51 return ERROR_FAIL;
53 busy = buf_get_u32(ret, 2, 1);
54 } while (busy); /* check for busy bit */
57 return ERROR_OK;
60 int avr32_jtag_nexus_set_address(struct avr32_jtag *jtag_info,
61 uint32_t addr, int mode)
63 struct scan_field fields[2];
64 uint8_t addr_buf[4];
65 uint8_t busy_buf[4];
66 int busy;
68 memset(fields, 0, sizeof(fields));
70 do {
71 memset(addr_buf, 0, sizeof(addr_buf));
72 memset(busy_buf, 0, sizeof(busy_buf));
74 buf_set_u32(addr_buf, 0, 1, mode);
75 buf_set_u32(addr_buf, 1, 7, addr);
77 fields[0].num_bits = 26;
78 fields[0].in_value = NULL;
79 fields[0].out_value = NULL;
81 fields[1].num_bits = 8;
82 fields[1].in_value = busy_buf;
83 fields[1].out_value = addr_buf;
85 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
86 if (jtag_execute_queue() != ERROR_OK) {
87 LOG_ERROR("%s: setting address failed", __func__);
88 return ERROR_FAIL;
90 busy = buf_get_u32(busy_buf, 6, 1);
91 } while (busy);
93 return ERROR_OK;
97 int avr32_jtag_nexus_read_data(struct avr32_jtag *jtag_info,
98 uint32_t *pdata)
101 struct scan_field fields[2];
102 uint8_t data_buf[4];
103 uint8_t busy_buf[4];
104 int busy;
106 do {
107 memset(data_buf, 0, sizeof(data_buf));
108 memset(busy_buf, 0, sizeof(busy_buf));
110 fields[0].num_bits = 32;
111 fields[0].out_value = NULL;
112 fields[0].in_value = data_buf;
115 fields[1].num_bits = 2;
116 fields[1].in_value = busy_buf;
117 fields[1].out_value = NULL;
119 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
121 if (jtag_execute_queue() != ERROR_OK) {
122 LOG_ERROR("%s: reading data failed", __func__);
123 return ERROR_FAIL;
126 busy = buf_get_u32(busy_buf, 0, 1);
127 } while (busy);
129 *pdata = buf_get_u32(data_buf, 0, 32);
131 return ERROR_OK;
134 int avr32_jtag_nexus_write_data(struct avr32_jtag *jtag_info,
135 uint32_t data)
138 struct scan_field fields[2];
139 uint8_t data_buf[4];
140 uint8_t busy_buf[4];
141 uint8_t dummy_buf[4];
142 int busy;
144 do {
145 memset(data_buf, 0, sizeof(data_buf));
146 memset(busy_buf, 0, sizeof(busy_buf));
147 memset(dummy_buf, 0, sizeof(dummy_buf));
149 fields[0].num_bits = 2;
150 fields[0].in_value = busy_buf;
151 fields[0].out_value = dummy_buf;
154 buf_set_u32(data_buf, 0, 32, data);
155 fields[1].num_bits = 32;
156 fields[1].in_value = NULL;
157 fields[1].out_value = data_buf;
159 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
161 if (jtag_execute_queue() != ERROR_OK) {
162 LOG_ERROR("%s: reading data failed", __func__);
163 return ERROR_FAIL;
166 busy = buf_get_u32(busy_buf, 0, 0);
167 } while (busy);
170 return ERROR_OK;
173 int avr32_jtag_nexus_read(struct avr32_jtag *jtag_info,
174 uint32_t addr, uint32_t *value)
176 avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
177 avr32_jtag_nexus_set_address(jtag_info, addr, MODE_READ);
178 avr32_jtag_nexus_read_data(jtag_info, value);
180 return ERROR_OK;
183 int avr32_jtag_nexus_write(struct avr32_jtag *jtag_info,
184 uint32_t addr, uint32_t value)
186 avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
187 avr32_jtag_nexus_set_address(jtag_info, addr, MODE_WRITE);
188 avr32_jtag_nexus_write_data(jtag_info, value);
190 return ERROR_OK;
193 int avr32_jtag_mwa_set_address(struct avr32_jtag *jtag_info, int slave,
194 uint32_t addr, int mode)
196 struct scan_field fields[2];
197 uint8_t addr_buf[4];
198 uint8_t slave_buf[4];
199 uint8_t busy_buf[4];
200 int busy;
202 memset(fields, 0, sizeof(fields));
204 do {
205 memset(addr_buf, 0, sizeof(addr_buf));
206 memset(busy_buf, 0, sizeof(busy_buf));
207 memset(slave_buf, 0, sizeof(slave_buf));
209 buf_set_u32(slave_buf, 0, 4, slave);
210 buf_set_u32(addr_buf, 0, 1, mode);
211 buf_set_u32(addr_buf, 1, 30, addr >> 2);
213 fields[0].num_bits = 31;
214 fields[0].in_value = NULL;
215 fields[0].out_value = addr_buf;
217 fields[1].num_bits = 4;
218 fields[1].in_value = busy_buf;
219 fields[1].out_value = slave_buf;
221 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
222 if (jtag_execute_queue() != ERROR_OK) {
223 LOG_ERROR("%s: setting address failed", __func__);
224 return ERROR_FAIL;
226 busy = buf_get_u32(busy_buf, 1, 1);
227 } while (busy);
229 return ERROR_OK;
232 int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info,
233 uint32_t *pdata)
236 struct scan_field fields[2];
237 uint8_t data_buf[4];
238 uint8_t busy_buf[4];
239 int busy;
241 do {
242 memset(data_buf, 0, sizeof(data_buf));
243 memset(busy_buf, 0, sizeof(busy_buf));
245 fields[0].num_bits = 32;
246 fields[0].out_value = NULL;
247 fields[0].in_value = data_buf;
250 fields[1].num_bits = 3;
251 fields[1].in_value = busy_buf;
252 fields[1].out_value = NULL;
254 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
256 if (jtag_execute_queue() != ERROR_OK) {
257 LOG_ERROR("%s: reading data failed", __func__);
258 return ERROR_FAIL;
261 busy = buf_get_u32(busy_buf, 0, 1);
262 } while (busy);
264 *pdata = buf_get_u32(data_buf, 0, 32);
266 return ERROR_OK;
269 int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info,
270 uint32_t data)
273 struct scan_field fields[2];
274 uint8_t data_buf[4];
275 uint8_t busy_buf[4];
276 uint8_t zero_buf[4];
277 int busy;
279 do {
280 memset(data_buf, 0, sizeof(data_buf));
281 memset(busy_buf, 0, sizeof(busy_buf));
282 memset(zero_buf, 0, sizeof(zero_buf));
284 buf_set_u32(data_buf, 0, 32, data);
285 fields[0].num_bits = 3;
286 fields[0].in_value = busy_buf;
287 fields[0].out_value = zero_buf;
289 fields[1].num_bits = 32;
290 fields[1].out_value = data_buf;
291 fields[1].in_value = NULL;
294 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
296 if (jtag_execute_queue() != ERROR_OK) {
297 LOG_ERROR("%s: reading data failed", __func__);
298 return ERROR_FAIL;
301 busy = buf_get_u32(busy_buf, 0, 1);
302 } while (busy);
304 return ERROR_OK;
307 int avr32_jtag_mwa_read(struct avr32_jtag *jtag_info, int slave,
308 uint32_t addr, uint32_t *value)
310 avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
311 avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_READ);
312 avr32_jtag_mwa_read_data(jtag_info, value);
314 return ERROR_OK;
317 int avr32_jtag_mwa_write(struct avr32_jtag *jtag_info, int slave,
318 uint32_t addr, uint32_t value)
320 avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
321 avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_WRITE);
322 avr32_jtag_mwa_write_data(jtag_info, value);
324 return ERROR_OK;
327 int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
329 int retval;
330 uint32_t ds;
332 retval = avr32_jtag_nexus_write(jtag_info, AVR32_OCDREG_DINST, inst);
333 if (retval != ERROR_OK)
334 return retval;
336 do {
337 retval = avr32_jtag_nexus_read(jtag_info, AVR32_OCDREG_DS, &ds);
338 if (retval != ERROR_OK)
339 return retval;
340 } while ((ds & OCDREG_DS_DBA) && !(ds & OCDREG_DS_INC));
342 return ERROR_OK;
345 int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
347 uint32_t value;
348 int res;
350 res = avr32_jtag_nexus_read(jtag, reg, &value);
351 if (res)
352 return res;
354 value |= bits;
355 res = avr32_jtag_nexus_write(jtag, reg, value);
356 if (res)
357 return res;
359 return ERROR_OK;
362 int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
364 uint32_t value;
365 int res;
367 res = avr32_jtag_nexus_read(jtag, reg, &value);
368 if (res)
369 return res;
371 value &= ~bits;
372 res = avr32_jtag_nexus_write(jtag, reg, value);
373 if (res)
374 return res;
376 return ERROR_OK;