]> git.sur5r.net Git - openocd/blob - src/target/avr32_jtag.c
rtos: support gdb_get_register_packet
[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, see <http://www.gnu.org/licenses/>. *
16  ***************************************************************************/
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include "target.h"
23 #include "jtag/jtag.h"
24 #include "avr32_jtag.h"
25
26 static int avr32_jtag_set_instr(struct avr32_jtag *jtag_info, int new_instr)
27 {
28         struct jtag_tap *tap;
29         int busy = 0;
30
31         tap = jtag_info->tap;
32         if (tap == NULL)
33                 return ERROR_FAIL;
34
35         if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr) {
36                 do {
37                         struct scan_field field;
38                         uint8_t t[4];
39                         uint8_t ret[4];
40
41                         field.num_bits = tap->ir_length;
42                         field.out_value = t;
43                         buf_set_u32(t, 0, field.num_bits, new_instr);
44                         field.in_value = ret;
45
46                         jtag_add_ir_scan(tap, &field, TAP_IDLE);
47                         if (jtag_execute_queue() != ERROR_OK) {
48                                 LOG_ERROR("%s: setting address failed", __func__);
49                                 return ERROR_FAIL;
50                         }
51                         busy = buf_get_u32(ret, 2, 1);
52                 } while (busy); /* check for busy bit */
53         }
54
55         return ERROR_OK;
56 }
57
58 int avr32_jtag_nexus_set_address(struct avr32_jtag *jtag_info,
59                 uint32_t addr, int mode)
60 {
61         struct scan_field fields[2];
62         uint8_t addr_buf[4];
63         uint8_t busy_buf[4];
64         int busy;
65
66         memset(fields, 0, sizeof(fields));
67
68         do {
69                 memset(addr_buf, 0, sizeof(addr_buf));
70                 memset(busy_buf, 0, sizeof(busy_buf));
71
72                 buf_set_u32(addr_buf, 0, 1, mode);
73                 buf_set_u32(addr_buf, 1, 7, addr);
74
75                 fields[0].num_bits = 26;
76                 fields[0].in_value = NULL;
77                 fields[0].out_value = NULL;
78
79                 fields[1].num_bits = 8;
80                 fields[1].in_value = busy_buf;
81                 fields[1].out_value = addr_buf;
82
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__);
86                         return ERROR_FAIL;
87                 }
88                 busy = buf_get_u32(busy_buf, 6, 1);
89         } while (busy);
90
91         return ERROR_OK;
92 }
93
94
95 int avr32_jtag_nexus_read_data(struct avr32_jtag *jtag_info,
96         uint32_t *pdata)
97 {
98
99         struct scan_field fields[2];
100         uint8_t data_buf[4];
101         uint8_t busy_buf[4];
102         int busy;
103
104         do {
105                 memset(data_buf, 0, sizeof(data_buf));
106                 memset(busy_buf, 0, sizeof(busy_buf));
107
108                 fields[0].num_bits = 32;
109                 fields[0].out_value = NULL;
110                 fields[0].in_value = data_buf;
111
112
113                 fields[1].num_bits = 2;
114                 fields[1].in_value = busy_buf;
115                 fields[1].out_value = NULL;
116
117                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
118
119                 if (jtag_execute_queue() != ERROR_OK) {
120                         LOG_ERROR("%s: reading data  failed", __func__);
121                         return ERROR_FAIL;
122                 }
123
124                 busy = buf_get_u32(busy_buf, 0, 1);
125         } while (busy);
126
127         *pdata = buf_get_u32(data_buf, 0, 32);
128
129         return ERROR_OK;
130 }
131
132 int avr32_jtag_nexus_write_data(struct avr32_jtag *jtag_info,
133                 uint32_t data)
134 {
135
136         struct scan_field fields[2];
137         uint8_t data_buf[4];
138         uint8_t busy_buf[4];
139         uint8_t dummy_buf[4];
140         int busy;
141
142         do {
143                 memset(data_buf, 0, sizeof(data_buf));
144                 memset(busy_buf, 0, sizeof(busy_buf));
145                 memset(dummy_buf, 0, sizeof(dummy_buf));
146
147                 fields[0].num_bits = 2;
148                 fields[0].in_value = busy_buf;
149                 fields[0].out_value = dummy_buf;
150
151
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;
156
157                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
158
159                 if (jtag_execute_queue() != ERROR_OK) {
160                         LOG_ERROR("%s: reading data  failed", __func__);
161                         return ERROR_FAIL;
162                 }
163
164                 busy = buf_get_u32(busy_buf, 0, 0);
165         } while (busy);
166
167
168         return ERROR_OK;
169 }
170
171 int avr32_jtag_nexus_read(struct avr32_jtag *jtag_info,
172                 uint32_t addr, uint32_t *value)
173 {
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);
177
178         return ERROR_OK;
179
180 }
181 int avr32_jtag_nexus_write(struct avr32_jtag *jtag_info,
182                 uint32_t addr, uint32_t value)
183 {
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);
187
188         return ERROR_OK;
189 }
190
191 int avr32_jtag_mwa_set_address(struct avr32_jtag *jtag_info, int slave,
192                 uint32_t addr, int mode)
193 {
194         struct scan_field fields[2];
195         uint8_t addr_buf[4];
196         uint8_t slave_buf[4];
197         uint8_t busy_buf[4];
198         int busy;
199
200         memset(fields, 0, sizeof(fields));
201
202         do {
203                 memset(addr_buf, 0, sizeof(addr_buf));
204                 memset(busy_buf, 0, sizeof(busy_buf));
205                 memset(slave_buf, 0, sizeof(slave_buf));
206
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);
210
211                 fields[0].num_bits = 31;
212                 fields[0].in_value = NULL;
213                 fields[0].out_value = addr_buf;
214
215                 fields[1].num_bits = 4;
216                 fields[1].in_value = busy_buf;
217                 fields[1].out_value = slave_buf;
218
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__);
222                         return ERROR_FAIL;
223                 }
224                 busy = buf_get_u32(busy_buf, 1, 1);
225         } while (busy);
226
227         return ERROR_OK;
228 }
229
230 int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info,
231         uint32_t *pdata)
232 {
233
234         struct scan_field fields[2];
235         uint8_t data_buf[4];
236         uint8_t busy_buf[4];
237         int busy;
238
239         do {
240                 memset(data_buf, 0, sizeof(data_buf));
241                 memset(busy_buf, 0, sizeof(busy_buf));
242
243                 fields[0].num_bits = 32;
244                 fields[0].out_value = NULL;
245                 fields[0].in_value = data_buf;
246
247
248                 fields[1].num_bits = 3;
249                 fields[1].in_value = busy_buf;
250                 fields[1].out_value = NULL;
251
252                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
253
254                 if (jtag_execute_queue() != ERROR_OK) {
255                         LOG_ERROR("%s: reading data  failed", __func__);
256                         return ERROR_FAIL;
257                 }
258
259                 busy = buf_get_u32(busy_buf, 0, 1);
260         } while (busy);
261
262         *pdata = buf_get_u32(data_buf, 0, 32);
263
264         return ERROR_OK;
265 }
266
267 int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info,
268         uint32_t data)
269 {
270
271         struct scan_field fields[2];
272         uint8_t data_buf[4];
273         uint8_t busy_buf[4];
274         uint8_t zero_buf[4];
275         int busy;
276
277         do {
278                 memset(data_buf, 0, sizeof(data_buf));
279                 memset(busy_buf, 0, sizeof(busy_buf));
280                 memset(zero_buf, 0, sizeof(zero_buf));
281
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;
286
287                 fields[1].num_bits = 32;
288                 fields[1].out_value = data_buf;
289                 fields[1].in_value = NULL;
290
291
292                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
293
294                 if (jtag_execute_queue() != ERROR_OK) {
295                         LOG_ERROR("%s: reading data  failed", __func__);
296                         return ERROR_FAIL;
297                 }
298
299                 busy = buf_get_u32(busy_buf, 0, 1);
300         } while (busy);
301
302         return ERROR_OK;
303 }
304
305 int avr32_jtag_mwa_read(struct avr32_jtag *jtag_info, int slave,
306                 uint32_t addr, uint32_t *value)
307 {
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);
311
312         return ERROR_OK;
313 }
314
315 int avr32_jtag_mwa_write(struct avr32_jtag *jtag_info, int slave,
316                 uint32_t addr, uint32_t value)
317 {
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);
321
322         return ERROR_OK;
323 }
324
325 int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
326 {
327         int retval;
328         uint32_t ds;
329
330         retval = avr32_jtag_nexus_write(jtag_info, AVR32_OCDREG_DINST, inst);
331         if (retval != ERROR_OK)
332                 return retval;
333
334         do {
335                 retval = avr32_jtag_nexus_read(jtag_info, AVR32_OCDREG_DS, &ds);
336                 if (retval != ERROR_OK)
337                         return retval;
338         } while ((ds & OCDREG_DS_DBA) && !(ds & OCDREG_DS_INC));
339
340         return ERROR_OK;
341 }
342
343 int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
344 {
345         uint32_t value;
346         int res;
347
348         res = avr32_jtag_nexus_read(jtag, reg, &value);
349         if (res)
350                 return res;
351
352         value |= bits;
353         res = avr32_jtag_nexus_write(jtag, reg, value);
354         if (res)
355                 return res;
356
357         return ERROR_OK;
358 }
359
360 int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
361 {
362         uint32_t value;
363         int res;
364
365         res = avr32_jtag_nexus_read(jtag, reg, &value);
366         if (res)
367                 return res;
368
369         value &= ~bits;
370         res = avr32_jtag_nexus_write(jtag, reg, value);
371         if (res)
372                 return res;
373
374         return ERROR_OK;
375 }
376