docs: Fix incorrect -rtos option values
[openocd/jflash.git] / src / target / avr32_jtag.c
blobc88f44e800f9286df15daf839cc28c2bec15836c
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "target.h"
25 #include "helper/types.h"
26 #include "jtag/jtag.h"
27 #include "avr32_jtag.h"
29 static int avr32_jtag_set_instr(struct avr32_jtag *jtag_info, int new_instr)
31 struct jtag_tap *tap;
32 int busy = 0;
34 tap = jtag_info->tap;
35 if (tap == NULL)
36 return ERROR_FAIL;
38 if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr) {
39 do {
40 struct scan_field field;
41 uint8_t t[4];
42 uint8_t ret[4];
44 field.num_bits = tap->ir_length;
45 field.out_value = t;
46 buf_set_u32(t, 0, field.num_bits, new_instr);
47 field.in_value = ret;
49 jtag_add_ir_scan(tap, &field, TAP_IDLE);
50 if (jtag_execute_queue() != ERROR_OK) {
51 LOG_ERROR("%s: setting address failed", __func__);
52 return ERROR_FAIL;
54 busy = buf_get_u32(ret, 2, 1);
55 } while (busy); /* check for busy bit */
58 return ERROR_OK;
61 int avr32_jtag_nexus_set_address(struct avr32_jtag *jtag_info,
62 uint32_t addr, int mode)
64 struct scan_field fields[2];
65 uint8_t addr_buf[4];
66 uint8_t busy_buf[4];
67 int busy;
69 memset(fields, 0, sizeof(fields));
71 do {
72 memset(addr_buf, 0, sizeof(addr_buf));
73 memset(busy_buf, 0, sizeof(busy_buf));
75 buf_set_u32(addr_buf, 0, 1, mode);
76 buf_set_u32(addr_buf, 1, 7, addr);
78 fields[0].num_bits = 26;
79 fields[0].in_value = NULL;
80 fields[0].out_value = NULL;
82 fields[1].num_bits = 8;
83 fields[1].in_value = busy_buf;
84 fields[1].out_value = addr_buf;
86 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
87 if (jtag_execute_queue() != ERROR_OK) {
88 LOG_ERROR("%s: setting address failed", __func__);
89 return ERROR_FAIL;
91 busy = buf_get_u32(busy_buf, 6, 1);
92 } while (busy);
94 return ERROR_OK;
98 int avr32_jtag_nexus_read_data(struct avr32_jtag *jtag_info,
99 uint32_t *pdata)
102 struct scan_field fields[2];
103 uint8_t data_buf[4];
104 uint8_t busy_buf[4];
105 int busy;
107 do {
108 memset(data_buf, 0, sizeof(data_buf));
109 memset(busy_buf, 0, sizeof(busy_buf));
111 fields[0].num_bits = 32;
112 fields[0].out_value = NULL;
113 fields[0].in_value = data_buf;
116 fields[1].num_bits = 2;
117 fields[1].in_value = busy_buf;
118 fields[1].out_value = NULL;
120 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
122 if (jtag_execute_queue() != ERROR_OK) {
123 LOG_ERROR("%s: reading data failed", __func__);
124 return ERROR_FAIL;
127 busy = buf_get_u32(busy_buf, 0, 1);
128 } while (busy);
130 *pdata = buf_get_u32(data_buf, 0, 32);
132 return ERROR_OK;
135 int avr32_jtag_nexus_write_data(struct avr32_jtag *jtag_info,
136 uint32_t data)
139 struct scan_field fields[2];
140 uint8_t data_buf[4];
141 uint8_t busy_buf[4];
142 uint8_t dummy_buf[4];
143 int busy;
145 do {
146 memset(data_buf, 0, sizeof(data_buf));
147 memset(busy_buf, 0, sizeof(busy_buf));
148 memset(dummy_buf, 0, sizeof(dummy_buf));
150 fields[0].num_bits = 2;
151 fields[0].in_value = busy_buf;
152 fields[0].out_value = dummy_buf;
155 buf_set_u32(data_buf, 0, 32, data);
156 fields[1].num_bits = 32;
157 fields[1].in_value = NULL;
158 fields[1].out_value = data_buf;
160 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
162 if (jtag_execute_queue() != ERROR_OK) {
163 LOG_ERROR("%s: reading data failed", __func__);
164 return ERROR_FAIL;
167 busy = buf_get_u32(busy_buf, 0, 0);
168 } while (busy);
171 return ERROR_OK;
174 int avr32_jtag_nexus_read(struct avr32_jtag *jtag_info,
175 uint32_t addr, uint32_t *value)
177 avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
178 avr32_jtag_nexus_set_address(jtag_info, addr, MODE_READ);
179 avr32_jtag_nexus_read_data(jtag_info, value);
181 return ERROR_OK;
184 int avr32_jtag_nexus_write(struct avr32_jtag *jtag_info,
185 uint32_t addr, uint32_t value)
187 avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
188 avr32_jtag_nexus_set_address(jtag_info, addr, MODE_WRITE);
189 avr32_jtag_nexus_write_data(jtag_info, value);
191 return ERROR_OK;
194 int avr32_jtag_mwa_set_address(struct avr32_jtag *jtag_info, int slave,
195 uint32_t addr, int mode)
197 struct scan_field fields[2];
198 uint8_t addr_buf[4];
199 uint8_t slave_buf[4];
200 uint8_t busy_buf[4];
201 int busy;
203 memset(fields, 0, sizeof(fields));
205 do {
206 memset(addr_buf, 0, sizeof(addr_buf));
207 memset(busy_buf, 0, sizeof(busy_buf));
208 memset(slave_buf, 0, sizeof(slave_buf));
210 buf_set_u32(slave_buf, 0, 4, slave);
211 buf_set_u32(addr_buf, 0, 1, mode);
212 buf_set_u32(addr_buf, 1, 30, addr >> 2);
214 fields[0].num_bits = 31;
215 fields[0].in_value = NULL;
216 fields[0].out_value = addr_buf;
218 fields[1].num_bits = 4;
219 fields[1].in_value = busy_buf;
220 fields[1].out_value = slave_buf;
222 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
223 if (jtag_execute_queue() != ERROR_OK) {
224 LOG_ERROR("%s: setting address failed", __func__);
225 return ERROR_FAIL;
227 busy = buf_get_u32(busy_buf, 1, 1);
228 } while (busy);
230 return ERROR_OK;
233 int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info,
234 uint32_t *pdata)
237 struct scan_field fields[2];
238 uint8_t data_buf[4];
239 uint8_t busy_buf[4];
240 int busy;
242 do {
243 memset(data_buf, 0, sizeof(data_buf));
244 memset(busy_buf, 0, sizeof(busy_buf));
246 fields[0].num_bits = 32;
247 fields[0].out_value = NULL;
248 fields[0].in_value = data_buf;
251 fields[1].num_bits = 3;
252 fields[1].in_value = busy_buf;
253 fields[1].out_value = NULL;
255 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
257 if (jtag_execute_queue() != ERROR_OK) {
258 LOG_ERROR("%s: reading data failed", __func__);
259 return ERROR_FAIL;
262 busy = buf_get_u32(busy_buf, 0, 1);
263 } while (busy);
265 *pdata = buf_get_u32(data_buf, 0, 32);
267 return ERROR_OK;
270 int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info,
271 uint32_t data)
274 struct scan_field fields[2];
275 uint8_t data_buf[4];
276 uint8_t busy_buf[4];
277 uint8_t zero_buf[4];
278 int busy;
280 do {
281 memset(data_buf, 0, sizeof(data_buf));
282 memset(busy_buf, 0, sizeof(busy_buf));
283 memset(zero_buf, 0, sizeof(zero_buf));
285 buf_set_u32(data_buf, 0, 32, data);
286 fields[0].num_bits = 3;
287 fields[0].in_value = busy_buf;
288 fields[0].out_value = zero_buf;
290 fields[1].num_bits = 32;
291 fields[1].out_value = data_buf;
292 fields[1].in_value = NULL;
295 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
297 if (jtag_execute_queue() != ERROR_OK) {
298 LOG_ERROR("%s: reading data failed", __func__);
299 return ERROR_FAIL;
302 busy = buf_get_u32(busy_buf, 0, 1);
303 } while (busy);
305 return ERROR_OK;
308 int avr32_jtag_mwa_read(struct avr32_jtag *jtag_info, int slave,
309 uint32_t addr, uint32_t *value)
311 avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
312 avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_READ);
313 avr32_jtag_mwa_read_data(jtag_info, value);
315 return ERROR_OK;
318 int avr32_jtag_mwa_write(struct avr32_jtag *jtag_info, int slave,
319 uint32_t addr, uint32_t value)
321 avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
322 avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_WRITE);
323 avr32_jtag_mwa_write_data(jtag_info, value);
325 return ERROR_OK;
328 int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
330 int retval;
331 uint32_t ds;
333 retval = avr32_jtag_nexus_write(jtag_info, AVR32_OCDREG_DINST, inst);
334 if (retval != ERROR_OK)
335 return retval;
337 do {
338 retval = avr32_jtag_nexus_read(jtag_info, AVR32_OCDREG_DS, &ds);
339 if (retval != ERROR_OK)
340 return retval;
341 } while ((ds & OCDREG_DS_DBA) && !(ds & OCDREG_DS_INC));
343 return ERROR_OK;
346 int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
348 uint32_t value;
349 int res;
351 res = avr32_jtag_nexus_read(jtag, reg, &value);
352 if (res)
353 return res;
355 value |= bits;
356 res = avr32_jtag_nexus_write(jtag, reg, value);
357 if (res)
358 return res;
360 return ERROR_OK;
363 int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
365 uint32_t value;
366 int res;
368 res = avr32_jtag_nexus_read(jtag, reg, &value);
369 if (res)
370 return res;
372 value &= ~bits;
373 res = avr32_jtag_nexus_write(jtag, reg, value);
374 if (res)
375 return res;
377 return ERROR_OK;