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