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