TCL/SPEAr: move DDR activation in common code
[openocd/dsp568013.git] / src / target / avr32_jtag.c
blobb6b5e37b0eb6fa4626c7d294d7554d76aa66bd10
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 ***************************************************************************/
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
23 #include "target.h"
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)
30 struct jtag_tap *tap;
31 int busy = 0;
33 tap = jtag_info->tap;
34 if (tap == NULL)
35 return ERROR_FAIL;
37 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)
52 LOG_ERROR("%s: setting address failed", __func__);
53 return ERROR_FAIL;
55 busy = buf_get_u32(ret, 2, 1);
56 } while (busy); /* check for busy bit */
59 return ERROR_OK;
62 int avr32_jtag_nexus_set_address(struct avr32_jtag *jtag_info,
63 uint32_t addr, int mode)
65 struct scan_field fields[2];
66 uint8_t addr_buf[4];
67 uint8_t busy_buf[4];
68 int busy;
70 memset(fields, 0, sizeof(fields));
72 do {
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__);
91 return ERROR_FAIL;
93 busy = buf_get_u32(busy_buf, 6, 1);
94 } while(busy);
96 return ERROR_OK;
100 int avr32_jtag_nexus_read_data(struct avr32_jtag *jtag_info,
101 uint32_t *pdata)
104 struct scan_field fields[2];
105 uint8_t data_buf[4];
106 uint8_t busy_buf[4];
107 int busy;
109 do {
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__);
127 return ERROR_FAIL;
130 busy = buf_get_u32(busy_buf, 0, 1);
131 } while (busy);
133 *pdata = buf_get_u32(data_buf, 0, 32);
135 return ERROR_OK;
139 int avr32_jtag_nexus_write_data(struct avr32_jtag *jtag_info,
140 uint32_t data)
143 struct scan_field fields[2];
144 uint8_t data_buf[4];
145 uint8_t busy_buf[4];
146 uint8_t dummy_buf[4];
147 int busy;
149 do {
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__);
169 return ERROR_FAIL;
172 busy = buf_get_u32(busy_buf, 0, 0);
173 } while (busy);
176 return ERROR_OK;
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);
189 return ERROR_OK;
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);
199 return ERROR_OK;
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];
206 uint8_t addr_buf[4];
207 uint8_t slave_buf[4];
208 uint8_t busy_buf[4];
209 int busy;
211 memset(fields, 0, sizeof(fields));
213 do {
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__);
234 return ERROR_FAIL;
236 busy = buf_get_u32(busy_buf, 1, 1);
237 } while(busy);
239 return ERROR_OK;
242 int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info,
243 uint32_t *pdata)
246 struct scan_field fields[2];
247 uint8_t data_buf[4];
248 uint8_t busy_buf[4];
249 int busy;
251 do {
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__);
269 return ERROR_FAIL;
272 busy = buf_get_u32(busy_buf, 0, 1);
273 } while (busy);
275 *pdata = buf_get_u32(data_buf, 0, 32);
277 return ERROR_OK;
280 int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info,
281 uint32_t data)
284 struct scan_field fields[2];
285 uint8_t data_buf[4];
286 uint8_t busy_buf[4];
287 uint8_t zero_buf[4];
288 int busy;
290 do {
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__);
310 return ERROR_FAIL;
313 busy = buf_get_u32(busy_buf, 0, 1);
314 } while (busy);
316 return ERROR_OK;
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);
328 return ERROR_OK;
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);
338 return ERROR_OK;
341 int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
343 int retval;
344 uint32_t ds;
346 retval = avr32_jtag_nexus_write(jtag_info, AVR32_OCDREG_DINST, inst);
347 if (retval != ERROR_OK)
348 return retval;
350 do {
351 retval = avr32_jtag_nexus_read(jtag_info, AVR32_OCDREG_DS, &ds);
352 if (retval != ERROR_OK)
353 return retval;
354 } while ((ds & OCDREG_DS_DBA) && !(ds & OCDREG_DS_INC));
356 return ERROR_OK;
359 int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
361 uint32_t value;
362 int res;
364 res = avr32_jtag_nexus_read(jtag, reg, &value);
365 if (res)
366 return res;
368 value |= bits;
369 res = avr32_jtag_nexus_write(jtag, reg, value);
370 if (res)
371 return res;
373 return ERROR_OK;
376 int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
378 uint32_t value;
379 int res;
381 res = avr32_jtag_nexus_read(jtag, reg, &value);
382 if (res)
383 return res;
385 value &= ~bits;
386 res = avr32_jtag_nexus_write(jtag, reg, value);
387 if (res)
388 return res;
390 return ERROR_OK;