Remove FSF address from GPL notices
[openocd.git] / src / target / avr32_jtag.c
blob6526810e20c8c14b4d4a68347bc5572bc9786b35
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, see <http://www.gnu.org/licenses/>. *
16 ***************************************************************************/
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
22 #include "target.h"
23 #include "jtag/jtag.h"
24 #include "avr32_jtag.h"
26 static int avr32_jtag_set_instr(struct avr32_jtag *jtag_info, int new_instr)
28 struct jtag_tap *tap;
29 int busy = 0;
31 tap = jtag_info->tap;
32 if (tap == NULL)
33 return ERROR_FAIL;
35 if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr) {
36 do {
37 struct scan_field field;
38 uint8_t t[4];
39 uint8_t ret[4];
41 field.num_bits = tap->ir_length;
42 field.out_value = t;
43 buf_set_u32(t, 0, field.num_bits, new_instr);
44 field.in_value = ret;
46 jtag_add_ir_scan(tap, &field, TAP_IDLE);
47 if (jtag_execute_queue() != ERROR_OK) {
48 LOG_ERROR("%s: setting address failed", __func__);
49 return ERROR_FAIL;
51 busy = buf_get_u32(ret, 2, 1);
52 } while (busy); /* check for busy bit */
55 return ERROR_OK;
58 int avr32_jtag_nexus_set_address(struct avr32_jtag *jtag_info,
59 uint32_t addr, int mode)
61 struct scan_field fields[2];
62 uint8_t addr_buf[4];
63 uint8_t busy_buf[4];
64 int busy;
66 memset(fields, 0, sizeof(fields));
68 do {
69 memset(addr_buf, 0, sizeof(addr_buf));
70 memset(busy_buf, 0, sizeof(busy_buf));
72 buf_set_u32(addr_buf, 0, 1, mode);
73 buf_set_u32(addr_buf, 1, 7, addr);
75 fields[0].num_bits = 26;
76 fields[0].in_value = NULL;
77 fields[0].out_value = NULL;
79 fields[1].num_bits = 8;
80 fields[1].in_value = busy_buf;
81 fields[1].out_value = addr_buf;
83 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
84 if (jtag_execute_queue() != ERROR_OK) {
85 LOG_ERROR("%s: setting address failed", __func__);
86 return ERROR_FAIL;
88 busy = buf_get_u32(busy_buf, 6, 1);
89 } while (busy);
91 return ERROR_OK;
95 int avr32_jtag_nexus_read_data(struct avr32_jtag *jtag_info,
96 uint32_t *pdata)
99 struct scan_field fields[2];
100 uint8_t data_buf[4];
101 uint8_t busy_buf[4];
102 int busy;
104 do {
105 memset(data_buf, 0, sizeof(data_buf));
106 memset(busy_buf, 0, sizeof(busy_buf));
108 fields[0].num_bits = 32;
109 fields[0].out_value = NULL;
110 fields[0].in_value = data_buf;
113 fields[1].num_bits = 2;
114 fields[1].in_value = busy_buf;
115 fields[1].out_value = NULL;
117 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
119 if (jtag_execute_queue() != ERROR_OK) {
120 LOG_ERROR("%s: reading data failed", __func__);
121 return ERROR_FAIL;
124 busy = buf_get_u32(busy_buf, 0, 1);
125 } while (busy);
127 *pdata = buf_get_u32(data_buf, 0, 32);
129 return ERROR_OK;
132 int avr32_jtag_nexus_write_data(struct avr32_jtag *jtag_info,
133 uint32_t data)
136 struct scan_field fields[2];
137 uint8_t data_buf[4];
138 uint8_t busy_buf[4];
139 uint8_t dummy_buf[4];
140 int busy;
142 do {
143 memset(data_buf, 0, sizeof(data_buf));
144 memset(busy_buf, 0, sizeof(busy_buf));
145 memset(dummy_buf, 0, sizeof(dummy_buf));
147 fields[0].num_bits = 2;
148 fields[0].in_value = busy_buf;
149 fields[0].out_value = dummy_buf;
152 buf_set_u32(data_buf, 0, 32, data);
153 fields[1].num_bits = 32;
154 fields[1].in_value = NULL;
155 fields[1].out_value = data_buf;
157 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
159 if (jtag_execute_queue() != ERROR_OK) {
160 LOG_ERROR("%s: reading data failed", __func__);
161 return ERROR_FAIL;
164 busy = buf_get_u32(busy_buf, 0, 0);
165 } while (busy);
168 return ERROR_OK;
171 int avr32_jtag_nexus_read(struct avr32_jtag *jtag_info,
172 uint32_t addr, uint32_t *value)
174 avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
175 avr32_jtag_nexus_set_address(jtag_info, addr, MODE_READ);
176 avr32_jtag_nexus_read_data(jtag_info, value);
178 return ERROR_OK;
181 int avr32_jtag_nexus_write(struct avr32_jtag *jtag_info,
182 uint32_t addr, uint32_t value)
184 avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
185 avr32_jtag_nexus_set_address(jtag_info, addr, MODE_WRITE);
186 avr32_jtag_nexus_write_data(jtag_info, value);
188 return ERROR_OK;
191 int avr32_jtag_mwa_set_address(struct avr32_jtag *jtag_info, int slave,
192 uint32_t addr, int mode)
194 struct scan_field fields[2];
195 uint8_t addr_buf[4];
196 uint8_t slave_buf[4];
197 uint8_t busy_buf[4];
198 int busy;
200 memset(fields, 0, sizeof(fields));
202 do {
203 memset(addr_buf, 0, sizeof(addr_buf));
204 memset(busy_buf, 0, sizeof(busy_buf));
205 memset(slave_buf, 0, sizeof(slave_buf));
207 buf_set_u32(slave_buf, 0, 4, slave);
208 buf_set_u32(addr_buf, 0, 1, mode);
209 buf_set_u32(addr_buf, 1, 30, addr >> 2);
211 fields[0].num_bits = 31;
212 fields[0].in_value = NULL;
213 fields[0].out_value = addr_buf;
215 fields[1].num_bits = 4;
216 fields[1].in_value = busy_buf;
217 fields[1].out_value = slave_buf;
219 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
220 if (jtag_execute_queue() != ERROR_OK) {
221 LOG_ERROR("%s: setting address failed", __func__);
222 return ERROR_FAIL;
224 busy = buf_get_u32(busy_buf, 1, 1);
225 } while (busy);
227 return ERROR_OK;
230 int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info,
231 uint32_t *pdata)
234 struct scan_field fields[2];
235 uint8_t data_buf[4];
236 uint8_t busy_buf[4];
237 int busy;
239 do {
240 memset(data_buf, 0, sizeof(data_buf));
241 memset(busy_buf, 0, sizeof(busy_buf));
243 fields[0].num_bits = 32;
244 fields[0].out_value = NULL;
245 fields[0].in_value = data_buf;
248 fields[1].num_bits = 3;
249 fields[1].in_value = busy_buf;
250 fields[1].out_value = NULL;
252 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
254 if (jtag_execute_queue() != ERROR_OK) {
255 LOG_ERROR("%s: reading data failed", __func__);
256 return ERROR_FAIL;
259 busy = buf_get_u32(busy_buf, 0, 1);
260 } while (busy);
262 *pdata = buf_get_u32(data_buf, 0, 32);
264 return ERROR_OK;
267 int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info,
268 uint32_t data)
271 struct scan_field fields[2];
272 uint8_t data_buf[4];
273 uint8_t busy_buf[4];
274 uint8_t zero_buf[4];
275 int busy;
277 do {
278 memset(data_buf, 0, sizeof(data_buf));
279 memset(busy_buf, 0, sizeof(busy_buf));
280 memset(zero_buf, 0, sizeof(zero_buf));
282 buf_set_u32(data_buf, 0, 32, data);
283 fields[0].num_bits = 3;
284 fields[0].in_value = busy_buf;
285 fields[0].out_value = zero_buf;
287 fields[1].num_bits = 32;
288 fields[1].out_value = data_buf;
289 fields[1].in_value = NULL;
292 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
294 if (jtag_execute_queue() != ERROR_OK) {
295 LOG_ERROR("%s: reading data failed", __func__);
296 return ERROR_FAIL;
299 busy = buf_get_u32(busy_buf, 0, 1);
300 } while (busy);
302 return ERROR_OK;
305 int avr32_jtag_mwa_read(struct avr32_jtag *jtag_info, int slave,
306 uint32_t addr, uint32_t *value)
308 avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
309 avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_READ);
310 avr32_jtag_mwa_read_data(jtag_info, value);
312 return ERROR_OK;
315 int avr32_jtag_mwa_write(struct avr32_jtag *jtag_info, int slave,
316 uint32_t addr, uint32_t value)
318 avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
319 avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_WRITE);
320 avr32_jtag_mwa_write_data(jtag_info, value);
322 return ERROR_OK;
325 int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
327 int retval;
328 uint32_t ds;
330 retval = avr32_jtag_nexus_write(jtag_info, AVR32_OCDREG_DINST, inst);
331 if (retval != ERROR_OK)
332 return retval;
334 do {
335 retval = avr32_jtag_nexus_read(jtag_info, AVR32_OCDREG_DS, &ds);
336 if (retval != ERROR_OK)
337 return retval;
338 } while ((ds & OCDREG_DS_DBA) && !(ds & OCDREG_DS_INC));
340 return ERROR_OK;
343 int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
345 uint32_t value;
346 int res;
348 res = avr32_jtag_nexus_read(jtag, reg, &value);
349 if (res)
350 return res;
352 value |= bits;
353 res = avr32_jtag_nexus_write(jtag, reg, value);
354 if (res)
355 return res;
357 return ERROR_OK;
360 int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
362 uint32_t value;
363 int res;
365 res = avr32_jtag_nexus_read(jtag, reg, &value);
366 if (res)
367 return res;
369 value &= ~bits;
370 res = avr32_jtag_nexus_write(jtag, reg, value);
371 if (res)
372 return res;
374 return ERROR_OK;