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 ***************************************************************************/
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
)
38 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
) {
51 LOG_ERROR("%s: setting address failed", __func__
);
54 busy
= buf_get_u32(ret
, 2, 1);
55 } while (busy
); /* check for busy bit */
61 int avr32_jtag_nexus_set_address(struct avr32_jtag
*jtag_info
,
62 uint32_t addr
, int mode
)
64 struct scan_field fields
[2];
69 memset(fields
, 0, sizeof(fields
));
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__
);
91 busy
= buf_get_u32(busy_buf
, 6, 1);
98 int avr32_jtag_nexus_read_data(struct avr32_jtag
*jtag_info
,
102 struct scan_field fields
[2];
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__
);
127 busy
= buf_get_u32(busy_buf
, 0, 1);
130 *pdata
= buf_get_u32(data_buf
, 0, 32);
135 int avr32_jtag_nexus_write_data(struct avr32_jtag
*jtag_info
,
139 struct scan_field fields
[2];
142 uint8_t dummy_buf
[4];
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__
);
167 busy
= buf_get_u32(busy_buf
, 0, 0);
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
);
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
);
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];
199 uint8_t slave_buf
[4];
203 memset(fields
, 0, sizeof(fields
));
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__
);
227 busy
= buf_get_u32(busy_buf
, 1, 1);
233 int avr32_jtag_mwa_read_data(struct avr32_jtag
*jtag_info
,
237 struct scan_field fields
[2];
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__
);
262 busy
= buf_get_u32(busy_buf
, 0, 1);
265 *pdata
= buf_get_u32(data_buf
, 0, 32);
270 int avr32_jtag_mwa_write_data(struct avr32_jtag
*jtag_info
,
274 struct scan_field fields
[2];
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__
);
302 busy
= buf_get_u32(busy_buf
, 0, 1);
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
);
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
);
328 int avr32_jtag_exec(struct avr32_jtag
*jtag_info
, uint32_t inst
)
333 retval
= avr32_jtag_nexus_write(jtag_info
, AVR32_OCDREG_DINST
, inst
);
334 if (retval
!= ERROR_OK
)
338 retval
= avr32_jtag_nexus_read(jtag_info
, AVR32_OCDREG_DS
, &ds
);
339 if (retval
!= ERROR_OK
)
341 } while ((ds
& OCDREG_DS_DBA
) && !(ds
& OCDREG_DS_INC
));
346 int avr32_ocd_setbits(struct avr32_jtag
*jtag
, int reg
, uint32_t bits
)
351 res
= avr32_jtag_nexus_read(jtag
, reg
, &value
);
356 res
= avr32_jtag_nexus_write(jtag
, reg
, value
);
363 int avr32_ocd_clearbits(struct avr32_jtag
*jtag
, int reg
, uint32_t bits
)
368 res
= avr32_jtag_nexus_read(jtag
, reg
, &value
);
373 res
= avr32_jtag_nexus_write(jtag
, reg
, value
);