a23ddf7b1c70a9f473897958c6e200c083126498
[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, 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)
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] = { 0 };
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 static 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 static 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 static 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         return avr32_jtag_nexus_read_data(jtag_info, value);
177 }
178
179 int avr32_jtag_nexus_write(struct avr32_jtag *jtag_info,
180                 uint32_t addr, uint32_t value)
181 {
182         avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
183         avr32_jtag_nexus_set_address(jtag_info, addr, MODE_WRITE);
184         return avr32_jtag_nexus_write_data(jtag_info, value);
185 }
186
187 static int avr32_jtag_mwa_set_address(struct avr32_jtag *jtag_info, int slave,
188                 uint32_t addr, int mode)
189 {
190         struct scan_field fields[2];
191         uint8_t addr_buf[4];
192         uint8_t slave_buf[4];
193         uint8_t busy_buf[4];
194         int busy;
195
196         memset(fields, 0, sizeof(fields));
197
198         do {
199                 memset(addr_buf, 0, sizeof(addr_buf));
200                 memset(busy_buf, 0, sizeof(busy_buf));
201                 memset(slave_buf, 0, sizeof(slave_buf));
202
203                 buf_set_u32(slave_buf, 0, 4, slave);
204                 buf_set_u32(addr_buf, 0, 1, mode);
205                 buf_set_u32(addr_buf, 1, 30, addr >> 2);
206
207                 fields[0].num_bits = 31;
208                 fields[0].in_value = NULL;
209                 fields[0].out_value = addr_buf;
210
211                 fields[1].num_bits = 4;
212                 fields[1].in_value = busy_buf;
213                 fields[1].out_value = slave_buf;
214
215                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
216                 if (jtag_execute_queue() != ERROR_OK) {
217                         LOG_ERROR("%s: setting address failed", __func__);
218                         return ERROR_FAIL;
219                 }
220                 busy = buf_get_u32(busy_buf, 1, 1);
221         } while (busy);
222
223         return ERROR_OK;
224 }
225
226 static int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info,
227         uint32_t *pdata)
228 {
229
230         struct scan_field fields[2];
231         uint8_t data_buf[4];
232         uint8_t busy_buf[4];
233         int busy;
234
235         do {
236                 memset(data_buf, 0, sizeof(data_buf));
237                 memset(busy_buf, 0, sizeof(busy_buf));
238
239                 fields[0].num_bits = 32;
240                 fields[0].out_value = NULL;
241                 fields[0].in_value = data_buf;
242
243
244                 fields[1].num_bits = 3;
245                 fields[1].in_value = busy_buf;
246                 fields[1].out_value = NULL;
247
248                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
249
250                 if (jtag_execute_queue() != ERROR_OK) {
251                         LOG_ERROR("%s: reading data  failed", __func__);
252                         return ERROR_FAIL;
253                 }
254
255                 busy = buf_get_u32(busy_buf, 0, 1);
256         } while (busy);
257
258         *pdata = buf_get_u32(data_buf, 0, 32);
259
260         return ERROR_OK;
261 }
262
263 static int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info,
264         uint32_t data)
265 {
266
267         struct scan_field fields[2];
268         uint8_t data_buf[4];
269         uint8_t busy_buf[4];
270         uint8_t zero_buf[4];
271         int busy;
272
273         do {
274                 memset(data_buf, 0, sizeof(data_buf));
275                 memset(busy_buf, 0, sizeof(busy_buf));
276                 memset(zero_buf, 0, sizeof(zero_buf));
277
278                 buf_set_u32(data_buf, 0, 32, data);
279                 fields[0].num_bits = 3;
280                 fields[0].in_value = busy_buf;
281                 fields[0].out_value = zero_buf;
282
283                 fields[1].num_bits = 32;
284                 fields[1].out_value = data_buf;
285                 fields[1].in_value = NULL;
286
287
288                 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
289
290                 if (jtag_execute_queue() != ERROR_OK) {
291                         LOG_ERROR("%s: reading data  failed", __func__);
292                         return ERROR_FAIL;
293                 }
294
295                 busy = buf_get_u32(busy_buf, 0, 1);
296         } while (busy);
297
298         return ERROR_OK;
299 }
300
301 int avr32_jtag_mwa_read(struct avr32_jtag *jtag_info, int slave,
302                 uint32_t addr, uint32_t *value)
303 {
304         avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
305         avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_READ);
306         avr32_jtag_mwa_read_data(jtag_info, value);
307
308         return ERROR_OK;
309 }
310
311 int avr32_jtag_mwa_write(struct avr32_jtag *jtag_info, int slave,
312                 uint32_t addr, uint32_t value)
313 {
314         avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
315         avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_WRITE);
316         avr32_jtag_mwa_write_data(jtag_info, value);
317
318         return ERROR_OK;
319 }
320
321 int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
322 {
323         int retval;
324         uint32_t ds;
325
326         retval = avr32_jtag_nexus_write(jtag_info, AVR32_OCDREG_DINST, inst);
327         if (retval != ERROR_OK)
328                 return retval;
329
330         do {
331                 retval = avr32_jtag_nexus_read(jtag_info, AVR32_OCDREG_DS, &ds);
332                 if (retval != ERROR_OK)
333                         return retval;
334         } while ((ds & OCDREG_DS_DBA) && !(ds & OCDREG_DS_INC));
335
336         return ERROR_OK;
337 }
338
339 int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
340 {
341         uint32_t value;
342         int res;
343
344         res = avr32_jtag_nexus_read(jtag, reg, &value);
345         if (res)
346                 return res;
347
348         value |= bits;
349         res = avr32_jtag_nexus_write(jtag, reg, value);
350         if (res)
351                 return res;
352
353         return ERROR_OK;
354 }
355
356 int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
357 {
358         uint32_t value;
359         int res;
360
361         res = avr32_jtag_nexus_read(jtag, reg, &value);
362         if (res)
363                 return res;
364
365         value &= ~bits;
366         res = avr32_jtag_nexus_write(jtag, reg, value);
367         if (res)
368                 return res;
369
370         return ERROR_OK;
371 }