1 /***************************************************************************
2 * Copyright (C) 2010 by Oleksandr Tymoshenko <gonzo@bluezbox.com> *
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. *
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. *
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 ***************************************************************************/
24 #include "helper/types.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)
37 if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr)
40 struct scan_field field;
44 field.num_bits = tap->ir_length;
46 buf_set_u32(t, 0, field.num_bits, new_instr);
49 jtag_add_ir_scan(tap, &field, TAP_IDLE);
50 if (jtag_execute_queue() != ERROR_OK)
52 LOG_ERROR("%s: setting address failed", __func__);
55 busy = buf_get_u32(ret, 2, 1);
56 } while (busy); /* check for busy bit */
62 int avr32_jtag_nexus_set_address(struct avr32_jtag *jtag_info,
63 uint32_t addr, int mode)
65 struct scan_field fields[2];
70 memset(fields, 0, sizeof(fields));
73 memset(addr_buf, 0, sizeof(addr_buf));
74 memset(busy_buf, 0, sizeof(busy_buf));
76 buf_set_u32(addr_buf, 0, 1, mode);
77 buf_set_u32(addr_buf, 1, 7, addr);
79 fields[0].num_bits = 26;
80 fields[0].in_value = NULL;
81 fields[0].out_value = NULL;
83 fields[1].num_bits = 8;
84 fields[1].in_value = busy_buf;
85 fields[1].out_value = addr_buf;
87 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
88 if (jtag_execute_queue() != ERROR_OK)
90 LOG_ERROR("%s: setting address failed", __func__);
93 busy = buf_get_u32(busy_buf, 6, 1);
100 int avr32_jtag_nexus_read_data(struct avr32_jtag *jtag_info,
104 struct scan_field fields[2];
110 memset(data_buf, 0, sizeof(data_buf));
111 memset(busy_buf, 0, sizeof(busy_buf));
113 fields[0].num_bits = 32;
114 fields[0].out_value = NULL;
115 fields[0].in_value = data_buf;
118 fields[1].num_bits = 2;
119 fields[1].in_value = busy_buf;
120 fields[1].out_value = NULL;
122 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
124 if (jtag_execute_queue() != ERROR_OK)
126 LOG_ERROR("%s: reading data failed", __func__);
130 busy = buf_get_u32(busy_buf, 0, 1);
133 *pdata = buf_get_u32(data_buf, 0, 32);
139 int avr32_jtag_nexus_write_data(struct avr32_jtag *jtag_info,
143 struct scan_field fields[2];
146 uint8_t dummy_buf[4];
150 memset(data_buf, 0, sizeof(data_buf));
151 memset(busy_buf, 0, sizeof(busy_buf));
152 memset(dummy_buf, 0, sizeof(dummy_buf));
154 fields[0].num_bits = 2;
155 fields[0].in_value = busy_buf;
156 fields[0].out_value = dummy_buf;
159 buf_set_u32(data_buf, 0, 32, data);
160 fields[1].num_bits = 32;
161 fields[1].in_value = NULL;
162 fields[1].out_value = data_buf;
164 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
166 if (jtag_execute_queue() != ERROR_OK)
168 LOG_ERROR("%s: reading data failed", __func__);
172 busy = buf_get_u32(busy_buf, 0, 0);
182 int avr32_jtag_nexus_read(struct avr32_jtag *jtag_info,
183 uint32_t addr, uint32_t *value)
185 avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
186 avr32_jtag_nexus_set_address(jtag_info, addr, MODE_READ);
187 avr32_jtag_nexus_read_data(jtag_info, value);
192 int avr32_jtag_nexus_write(struct avr32_jtag *jtag_info,
193 uint32_t addr, uint32_t value)
195 avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
196 avr32_jtag_nexus_set_address(jtag_info, addr, MODE_WRITE);
197 avr32_jtag_nexus_write_data(jtag_info, value);
202 int avr32_jtag_mwa_set_address(struct avr32_jtag *jtag_info, int slave,
203 uint32_t addr, int mode)
205 struct scan_field fields[2];
207 uint8_t slave_buf[4];
211 memset(fields, 0, sizeof(fields));
214 memset(addr_buf, 0, sizeof(addr_buf));
215 memset(busy_buf, 0, sizeof(busy_buf));
216 memset(slave_buf, 0, sizeof(slave_buf));
218 buf_set_u32(slave_buf, 0, 4, slave);
219 buf_set_u32(addr_buf, 0, 1, mode);
220 buf_set_u32(addr_buf, 1, 30, addr >> 2);
222 fields[0].num_bits = 31;
223 fields[0].in_value = NULL;
224 fields[0].out_value = addr_buf;
226 fields[1].num_bits = 4;
227 fields[1].in_value = busy_buf;
228 fields[1].out_value = slave_buf;
230 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
231 if (jtag_execute_queue() != ERROR_OK)
233 LOG_ERROR("%s: setting address failed", __func__);
236 busy = buf_get_u32(busy_buf, 1, 1);
242 int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info,
246 struct scan_field fields[2];
252 memset(data_buf, 0, sizeof(data_buf));
253 memset(busy_buf, 0, sizeof(busy_buf));
255 fields[0].num_bits = 32;
256 fields[0].out_value = NULL;
257 fields[0].in_value = data_buf;
260 fields[1].num_bits = 3;
261 fields[1].in_value = busy_buf;
262 fields[1].out_value = NULL;
264 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
266 if (jtag_execute_queue() != ERROR_OK)
268 LOG_ERROR("%s: reading data failed", __func__);
272 busy = buf_get_u32(busy_buf, 0, 1);
275 *pdata = buf_get_u32(data_buf, 0, 32);
280 int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info,
284 struct scan_field fields[2];
291 memset(data_buf, 0, sizeof(data_buf));
292 memset(busy_buf, 0, sizeof(busy_buf));
293 memset(zero_buf, 0, sizeof(zero_buf));
295 buf_set_u32(data_buf, 0, 32, data);
296 fields[0].num_bits = 3;
297 fields[0].in_value = busy_buf;
298 fields[0].out_value = zero_buf;
300 fields[1].num_bits = 32;
301 fields[1].out_value = data_buf;
302 fields[1].in_value = NULL;
305 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
307 if (jtag_execute_queue() != ERROR_OK)
309 LOG_ERROR("%s: reading data failed", __func__);
313 busy = buf_get_u32(busy_buf, 0, 1);
321 int avr32_jtag_mwa_read(struct avr32_jtag *jtag_info, int slave,
322 uint32_t addr, uint32_t *value)
324 avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
325 avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_READ);
326 avr32_jtag_mwa_read_data(jtag_info, value);
331 int avr32_jtag_mwa_write(struct avr32_jtag *jtag_info, int slave,
332 uint32_t addr, uint32_t value)
334 avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
335 avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_WRITE);
336 avr32_jtag_mwa_write_data(jtag_info, value);
341 int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
346 retval = avr32_jtag_nexus_write(jtag_info, AVR32_OCDREG_DINST, inst);
347 if (retval != ERROR_OK)
351 retval = avr32_jtag_nexus_read(jtag_info, AVR32_OCDREG_DS, &ds);
352 if (retval != ERROR_OK)
354 } while ((ds & OCDREG_DS_DBA) && !(ds & OCDREG_DS_INC));
359 int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
364 res = avr32_jtag_nexus_read(jtag, reg, &value);
369 res = avr32_jtag_nexus_write(jtag, reg, value);
376 int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
381 res = avr32_jtag_nexus_read(jtag, reg, &value);
386 res = avr32_jtag_nexus_write(jtag, reg, value);