- Added support for native MinGW builds (thanks to Spencer Oliver and Michael Fischer...
[fw/openocd] / src / target / arm_jtag.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "arm_jtag.h"
25
26 #include "binarybuffer.h"
27 #include "log.h"
28 #include "jtag.h"
29
30 #include <stdlib.h>
31
32 int arm_jtag_set_instr(arm_jtag_t *jtag_info, u32 new_instr)
33 {
34         jtag_device_t *device = jtag_get_device(jtag_info->chain_pos);
35         
36         if (buf_get_u32(device->cur_instr, 0, device->ir_length) != new_instr)
37         {
38                 scan_field_t field;
39         
40                 field.device = jtag_info->chain_pos;
41                 field.num_bits = device->ir_length;
42                 field.out_value = calloc(CEIL(field.num_bits, 8), 1);
43                 buf_set_u32(field.out_value, 0, field.num_bits, new_instr);
44                 field.out_mask = NULL;
45                 field.in_value = NULL;
46                 field.in_check_value = NULL;
47                 field.in_check_mask = NULL;
48                 field.in_handler = NULL;
49                 field.in_handler_priv = NULL;
50                 
51                 jtag_add_ir_scan(1, &field, -1);
52                 
53                 free(field.out_value);
54         }
55         
56         return ERROR_OK;
57 }
58
59 int arm_jtag_scann(arm_jtag_t *jtag_info, u32 new_scan_chain)
60 {
61         if(jtag_info->cur_scan_chain != new_scan_chain)
62         {
63                 scan_field_t field;
64                 
65                 field.device = jtag_info->chain_pos;
66                 field.num_bits = jtag_info->scann_size;
67                 field.out_value = calloc(CEIL(field.num_bits, 8), 1);
68                 buf_set_u32(field.out_value, 0, field.num_bits, new_scan_chain);
69                 field.out_mask = NULL;
70                 //field.in_value = &scan_n_capture;
71                 field.in_value = NULL;
72                 field.in_check_value = NULL;
73                 field.in_check_mask = NULL;
74                 field.in_handler = NULL;
75                 field.in_handler_priv = NULL;
76                 
77                 arm_jtag_set_instr(jtag_info, jtag_info->scann_instr);
78                 jtag_add_dr_scan(1, &field, -1);
79                 
80                 jtag_info->cur_scan_chain = new_scan_chain;
81                 
82                 free(field.out_value);
83         }
84         
85         return ERROR_OK;
86 }
87
88 int arm_jtag_reset_callback(enum jtag_event event, void *priv)
89 {
90         arm_jtag_t *jtag_info = priv;
91         
92         if (event == JTAG_TRST_ASSERTED)
93         {
94                 jtag_info->cur_scan_chain = 0;
95         }
96         
97         return ERROR_OK;
98 }
99
100 int arm_jtag_setup_connection(arm_jtag_t *jtag_info)
101 {
102         jtag_info->scann_instr = 0x2;
103         jtag_info->cur_scan_chain = 0;
104         jtag_info->intest_instr = 0xc;
105
106         jtag_register_event_callback(arm_jtag_reset_callback, jtag_info);
107         
108         return ERROR_OK;
109 }
110
111 int arm_jtag_buf_to_u32_flip(u8 *in_buf, void *priv)
112 {
113         u32 *dest = priv;
114         
115         *dest = flip_u32(buf_get_u32(in_buf, 0, 32), 32);
116         
117         return ERROR_OK;
118 }