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, see <http://www.gnu.org/licenses/>. *
16 ***************************************************************************/
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
)
35 if (buf_get_u32(tap
->cur_instr
, 0, tap
->ir_length
) != (uint32_t)new_instr
) {
37 struct scan_field field
;
41 field
.num_bits
= tap
->ir_length
;
43 buf_set_u32(t
, 0, field
.num_bits
, new_instr
);
46 jtag_add_ir_scan(tap
, &field
, TAP_IDLE
);
47 if (jtag_execute_queue() != ERROR_OK
) {
48 LOG_ERROR("%s: setting address failed", __func__
);
51 busy
= buf_get_u32(ret
, 2, 1);
52 } while (busy
); /* check for busy bit */
58 int avr32_jtag_nexus_set_address(struct avr32_jtag
*jtag_info
,
59 uint32_t addr
, int mode
)
61 struct scan_field fields
[2];
66 memset(fields
, 0, sizeof(fields
));
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__
);
88 busy
= buf_get_u32(busy_buf
, 6, 1);
95 int avr32_jtag_nexus_read_data(struct avr32_jtag
*jtag_info
,
99 struct scan_field fields
[2];
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__
);
124 busy
= buf_get_u32(busy_buf
, 0, 1);
127 *pdata
= buf_get_u32(data_buf
, 0, 32);
132 int avr32_jtag_nexus_write_data(struct avr32_jtag
*jtag_info
,
136 struct scan_field fields
[2];
139 uint8_t dummy_buf
[4];
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__
);
164 busy
= buf_get_u32(busy_buf
, 0, 0);
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
);
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
);
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];
196 uint8_t slave_buf
[4];
200 memset(fields
, 0, sizeof(fields
));
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__
);
224 busy
= buf_get_u32(busy_buf
, 1, 1);
230 int avr32_jtag_mwa_read_data(struct avr32_jtag
*jtag_info
,
234 struct scan_field fields
[2];
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__
);
259 busy
= buf_get_u32(busy_buf
, 0, 1);
262 *pdata
= buf_get_u32(data_buf
, 0, 32);
267 int avr32_jtag_mwa_write_data(struct avr32_jtag
*jtag_info
,
271 struct scan_field fields
[2];
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__
);
299 busy
= buf_get_u32(busy_buf
, 0, 1);
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
);
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
);
325 int avr32_jtag_exec(struct avr32_jtag
*jtag_info
, uint32_t inst
)
330 retval
= avr32_jtag_nexus_write(jtag_info
, AVR32_OCDREG_DINST
, inst
);
331 if (retval
!= ERROR_OK
)
335 retval
= avr32_jtag_nexus_read(jtag_info
, AVR32_OCDREG_DS
, &ds
);
336 if (retval
!= ERROR_OK
)
338 } while ((ds
& OCDREG_DS_DBA
) && !(ds
& OCDREG_DS_INC
));
343 int avr32_ocd_setbits(struct avr32_jtag
*jtag
, int reg
, uint32_t bits
)
348 res
= avr32_jtag_nexus_read(jtag
, reg
, &value
);
353 res
= avr32_jtag_nexus_write(jtag
, reg
, value
);
360 int avr32_ocd_clearbits(struct avr32_jtag
*jtag
, int reg
, uint32_t bits
)
365 res
= avr32_jtag_nexus_read(jtag
, reg
, &value
);
370 res
= avr32_jtag_nexus_write(jtag
, reg
, value
);