- prepare OpenOCD for branching, created ./trunk/
[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 #include "config.h"
21
22 #include "arm_jtag.h"
23
24 #include "binarybuffer.h"
25 #include "log.h"
26 #include "jtag.h"
27
28 #include <stdlib.h>
29
30 int arm_jtag_set_instr(arm_jtag_t *jtag_info, u32 new_instr)
31 {
32         jtag_device_t *device = jtag_get_device(jtag_info->chain_pos);
33         
34         if (buf_get_u32(device->cur_instr, 0, device->ir_length) != new_instr)
35         {
36                 scan_field_t field;
37         
38                 field.device = jtag_info->chain_pos;
39                 field.num_bits = device->ir_length;
40                 field.out_value = calloc(CEIL(field.num_bits, 8), 1);
41                 buf_set_u32(field.out_value, 0, field.num_bits, new_instr);
42                 field.out_mask = NULL;
43                 field.in_value = NULL;
44                 field.in_check_value = NULL;
45                 field.in_check_mask = NULL;
46                 field.in_handler = NULL;
47                 field.in_handler_priv = NULL;
48                 
49                 jtag_add_ir_scan(1, &field, -1);
50                 
51                 free(field.out_value);
52         }
53         
54         return ERROR_OK;
55 }
56
57 int arm_jtag_scann(arm_jtag_t *jtag_info, u32 new_scan_chain)
58 {
59         if(jtag_info->cur_scan_chain != new_scan_chain)
60         {
61                 scan_field_t field;
62                 
63                 field.device = jtag_info->chain_pos;
64                 field.num_bits = jtag_info->scann_size;
65                 field.out_value = calloc(CEIL(field.num_bits, 8), 1);
66                 buf_set_u32(field.out_value, 0, field.num_bits, new_scan_chain);
67                 field.out_mask = NULL;
68                 //field.in_value = &scan_n_capture;
69                 field.in_value = NULL;
70                 field.in_check_value = NULL;
71                 field.in_check_mask = NULL;
72                 field.in_handler = NULL;
73                 field.in_handler_priv = NULL;
74                 
75                 arm_jtag_set_instr(jtag_info, jtag_info->scann_instr);
76                 jtag_add_dr_scan(1, &field, -1);
77                 
78                 jtag_info->cur_scan_chain = new_scan_chain;
79                 
80                 free(field.out_value);
81         }
82         
83         return ERROR_OK;
84 }
85
86 int arm_jtag_reset_callback(enum jtag_event event, void *priv)
87 {
88         arm_jtag_t *jtag_info = priv;
89         
90         if (event == JTAG_TRST_ASSERTED)
91         {
92                 jtag_info->cur_scan_chain = 0;
93         }
94         
95         return ERROR_OK;
96 }
97
98 int arm_jtag_setup_connection(arm_jtag_t *jtag_info)
99 {
100         jtag_info->scann_instr = 0x2;
101         jtag_info->cur_scan_chain = 0;
102         jtag_info->intest_instr = 0xc;
103
104         jtag_register_event_callback(arm_jtag_reset_callback, jtag_info);
105         
106         return ERROR_OK;
107 }
108
109 int arm_jtag_buf_to_u32_flip(u8 *in_buf, void *priv)
110 {
111         u32 *dest = priv;
112         
113         *dest = flip_u32(buf_get_u32(in_buf, 0, 32), 32);
114         
115         return ERROR_OK;
116 }