dsp5680xx - fix jtag debug request failure handling
[fw/openocd] / src / target / avr32_jtag.c
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
22
23 #include "target.h"
24 #include "helper/types.h"
25 #include "jtag/jtag.h"
26 #include "avr32_jtag.h"
27
28 static int avr32_jtag_set_instr(struct avr32_jtag *jtag_info, int new_instr)
29 {
30         struct jtag_tap *tap;
31         int busy = 0;
32
33         tap = jtag_info->tap;
34         if (tap == NULL)
35                 return ERROR_FAIL;
36
37         if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr)
38         {
39                 do {
40                         struct scan_field field;
41                         uint8_t t[4];
42                         uint8_t ret[4];
43
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;
48
49                         jtag_add_ir_scan(tap, &field, TAP_IDLE);
50                         if (jtag_execute_queue() != ERROR_OK)
51                         {
52                                 LOG_ERROR("%s: setting address failed", __func__);
53                                 return ERROR_FAIL;
54                         }
55                         busy = buf_get_u32(ret, 2, 1);
56                 } while (busy); /* check for busy bit */
57         }
58
59         return ERROR_OK;
60 }
61
62 int avr32_jtag_nexus_set_address(struct avr32_jtag *jtag_info, 
63                 uint32_t addr, int mode)
64 {
65         struct scan_field fields[2];
66         uint8_t addr_buf[4];
67         uint8_t busy_buf[4];
68         int busy;
69
70         memset(fields, 0, sizeof(fields));
71
72         do {
73                 memset(addr_buf, 0, sizeof(addr_buf));
74                 memset(busy_buf, 0, sizeof(busy_buf));
75
76                 buf_set_u32(addr_buf, 0, 1, mode);
77                 buf_set_u32(addr_buf, 1, 7, addr);
78
79                 fields[0].num_bits = 26;
80                 fields[0].in_value = NULL;
81                 fields[0].out_value = NULL;
82
83                 fields[1].num_bits = 8;
84                 fields[1].in_value = busy_buf;
85                 fields[1].out_value = addr_buf;
86
87                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
88                 if (jtag_execute_queue() != ERROR_OK)
89                 {
90                         LOG_ERROR("%s: setting address failed", __func__);
91                         return ERROR_FAIL;
92                 }
93                 busy = buf_get_u32(busy_buf, 6, 1);
94         } while(busy);
95
96         return ERROR_OK;
97 }
98
99
100 int avr32_jtag_nexus_read_data(struct avr32_jtag *jtag_info, 
101         uint32_t *pdata)
102 {
103
104         struct scan_field fields[2];
105         uint8_t data_buf[4];
106         uint8_t busy_buf[4];
107         int busy;
108
109         do {
110                 memset(data_buf, 0, sizeof(data_buf));
111                 memset(busy_buf, 0, sizeof(busy_buf));
112
113                 fields[0].num_bits = 32;
114                 fields[0].out_value = NULL;
115                 fields[0].in_value = data_buf;
116
117
118                 fields[1].num_bits = 2;
119                 fields[1].in_value = busy_buf;
120                 fields[1].out_value = NULL;
121
122                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
123
124                 if (jtag_execute_queue() != ERROR_OK)
125                 {
126                         LOG_ERROR("%s: reading data  failed", __func__);
127                         return ERROR_FAIL;
128                 }
129
130                 busy = buf_get_u32(busy_buf, 0, 1);
131         } while (busy);
132
133         *pdata = buf_get_u32(data_buf, 0, 32);
134
135         return ERROR_OK;
136 }
137
138
139 int avr32_jtag_nexus_write_data(struct avr32_jtag *jtag_info, 
140                 uint32_t data)
141 {
142
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;
148
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));
153
154                 fields[0].num_bits = 2;
155                 fields[0].in_value = busy_buf;
156                 fields[0].out_value = dummy_buf;
157
158
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;
163
164                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
165
166                 if (jtag_execute_queue() != ERROR_OK)
167                 {
168                         LOG_ERROR("%s: reading data  failed", __func__);
169                         return ERROR_FAIL;
170                 }
171
172                 busy = buf_get_u32(busy_buf, 0, 0);
173         } while (busy);
174
175
176         return ERROR_OK;
177 }
178
179
180
181
182 int avr32_jtag_nexus_read(struct avr32_jtag *jtag_info,
183                 uint32_t addr, uint32_t *value)
184 {
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);
188
189         return ERROR_OK;
190
191 }
192 int avr32_jtag_nexus_write(struct avr32_jtag *jtag_info,
193                 uint32_t addr, uint32_t value)
194 {
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);
198
199         return ERROR_OK;
200 }
201
202 int avr32_jtag_mwa_set_address(struct avr32_jtag *jtag_info, int slave,
203                 uint32_t addr, int mode)
204 {
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;
210
211         memset(fields, 0, sizeof(fields));
212
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));
217
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);
221
222                 fields[0].num_bits = 31;
223                 fields[0].in_value = NULL;
224                 fields[0].out_value = addr_buf;
225
226                 fields[1].num_bits = 4;
227                 fields[1].in_value = busy_buf;
228                 fields[1].out_value = slave_buf;
229
230                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
231                 if (jtag_execute_queue() != ERROR_OK)
232                 {
233                         LOG_ERROR("%s: setting address failed", __func__);
234                         return ERROR_FAIL;
235                 }
236                 busy = buf_get_u32(busy_buf, 1, 1);
237         } while(busy);
238
239         return ERROR_OK;
240 }
241
242 int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info, 
243         uint32_t *pdata)
244 {
245
246         struct scan_field fields[2];
247         uint8_t data_buf[4];
248         uint8_t busy_buf[4];
249         int busy;
250
251         do {
252                 memset(data_buf, 0, sizeof(data_buf));
253                 memset(busy_buf, 0, sizeof(busy_buf));
254
255                 fields[0].num_bits = 32;
256                 fields[0].out_value = NULL;
257                 fields[0].in_value = data_buf;
258
259
260                 fields[1].num_bits = 3;
261                 fields[1].in_value = busy_buf;
262                 fields[1].out_value = NULL;
263
264                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
265
266                 if (jtag_execute_queue() != ERROR_OK)
267                 {
268                         LOG_ERROR("%s: reading data  failed", __func__);
269                         return ERROR_FAIL;
270                 }
271
272                 busy = buf_get_u32(busy_buf, 0, 1);
273         } while (busy);
274
275         *pdata = buf_get_u32(data_buf, 0, 32);
276
277         return ERROR_OK;
278 }
279
280 int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info, 
281         uint32_t data)
282 {
283
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;
289
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));
294
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;
299
300                 fields[1].num_bits = 32;
301                 fields[1].out_value = data_buf;
302                 fields[1].in_value = NULL;
303
304
305                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
306
307                 if (jtag_execute_queue() != ERROR_OK)
308                 {
309                         LOG_ERROR("%s: reading data  failed", __func__);
310                         return ERROR_FAIL;
311                 }
312
313                 busy = buf_get_u32(busy_buf, 0, 1);
314         } while (busy);
315
316         return ERROR_OK;
317 }
318
319
320
321 int avr32_jtag_mwa_read(struct avr32_jtag *jtag_info, int slave,
322                 uint32_t addr, uint32_t *value)
323 {
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);
327
328         return ERROR_OK;
329 }
330
331 int avr32_jtag_mwa_write(struct avr32_jtag *jtag_info, int slave,
332                 uint32_t addr, uint32_t value)
333 {
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);
337
338         return ERROR_OK;
339 }
340
341 int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
342 {
343         int retval;
344         uint32_t ds;
345
346         retval = avr32_jtag_nexus_write(jtag_info, AVR32_OCDREG_DINST, inst);
347         if (retval != ERROR_OK)
348                 return retval;
349
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));
355
356         return ERROR_OK;
357 }
358
359 int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
360 {
361         uint32_t value;
362         int res;
363
364         res = avr32_jtag_nexus_read(jtag, reg, &value);
365         if (res)
366                 return res;
367
368         value |= bits;
369         res = avr32_jtag_nexus_write(jtag, reg, value);
370         if (res)
371                 return res;
372
373         return ERROR_OK;
374 }
375
376 int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
377 {
378         uint32_t value;
379         int res;
380
381         res = avr32_jtag_nexus_read(jtag, reg, &value);
382         if (res)
383                 return res;
384
385         value &= ~bits;
386         res = avr32_jtag_nexus_write(jtag, reg, value);
387         if (res)
388                 return res;
389
390         return ERROR_OK;
391 }
392