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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
18 ***************************************************************************/
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
) {
39 struct scan_field field
;
43 field
.num_bits
= tap
->ir_length
;
45 buf_set_u32(t
, 0, field
.num_bits
, new_instr
);
48 jtag_add_ir_scan(tap
, &field
, TAP_IDLE
);
49 if (jtag_execute_queue() != ERROR_OK
) {
50 LOG_ERROR("%s: setting address failed", __func__
);
53 busy
= buf_get_u32(ret
, 2, 1);
54 } while (busy
); /* check for busy bit */
60 int avr32_jtag_nexus_set_address(struct avr32_jtag
*jtag_info
,
61 uint32_t addr
, int mode
)
63 struct scan_field fields
[2];
68 memset(fields
, 0, sizeof(fields
));
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__
);
90 busy
= buf_get_u32(busy_buf
, 6, 1);
97 int avr32_jtag_nexus_read_data(struct avr32_jtag
*jtag_info
,
101 struct scan_field fields
[2];
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__
);
126 busy
= buf_get_u32(busy_buf
, 0, 1);
129 *pdata
= buf_get_u32(data_buf
, 0, 32);
134 int avr32_jtag_nexus_write_data(struct avr32_jtag
*jtag_info
,
138 struct scan_field fields
[2];
141 uint8_t dummy_buf
[4];
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__
);
166 busy
= buf_get_u32(busy_buf
, 0, 0);
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
);
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
);
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];
198 uint8_t slave_buf
[4];
202 memset(fields
, 0, sizeof(fields
));
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__
);
226 busy
= buf_get_u32(busy_buf
, 1, 1);
232 int avr32_jtag_mwa_read_data(struct avr32_jtag
*jtag_info
,
236 struct scan_field fields
[2];
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__
);
261 busy
= buf_get_u32(busy_buf
, 0, 1);
264 *pdata
= buf_get_u32(data_buf
, 0, 32);
269 int avr32_jtag_mwa_write_data(struct avr32_jtag
*jtag_info
,
273 struct scan_field fields
[2];
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__
);
301 busy
= buf_get_u32(busy_buf
, 0, 1);
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
);
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
);
327 int avr32_jtag_exec(struct avr32_jtag
*jtag_info
, uint32_t inst
)
332 retval
= avr32_jtag_nexus_write(jtag_info
, AVR32_OCDREG_DINST
, inst
);
333 if (retval
!= ERROR_OK
)
337 retval
= avr32_jtag_nexus_read(jtag_info
, AVR32_OCDREG_DS
, &ds
);
338 if (retval
!= ERROR_OK
)
340 } while ((ds
& OCDREG_DS_DBA
) && !(ds
& OCDREG_DS_INC
));
345 int avr32_ocd_setbits(struct avr32_jtag
*jtag
, int reg
, uint32_t bits
)
350 res
= avr32_jtag_nexus_read(jtag
, reg
, &value
);
355 res
= avr32_jtag_nexus_write(jtag
, reg
, value
);
362 int avr32_ocd_clearbits(struct avr32_jtag
*jtag
, int reg
, uint32_t bits
)
367 res
= avr32_jtag_nexus_read(jtag
, reg
, &value
);
372 res
= avr32_jtag_nexus_write(jtag
, reg
, value
);