0acfc6f4637ee3466992585c91c3aa53685d745c
[fw/openocd] / src / jtag / bitbang.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Øyvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "bitbang.h"
28 #include "interface.h"
29 #include "commands.h"
30
31 /**
32  * Function bitbang_stableclocks
33  * issues a number of clock cycles while staying in a stable state.
34  * Because the TMS value required to stay in the RESET state is a 1, whereas
35  * the TMS value required to stay in any of the other stable states is a 0,
36  * this function checks the current stable state to decide on the value of TMS
37  * to use.
38  */
39 static void bitbang_stableclocks(int num_cycles);
40
41
42 bitbang_interface_t *bitbang_interface;
43
44 /* DANGER!!!! clock absolutely *MUST* be 0 in idle or reset won't work!
45  *
46  * Set this to 1 and str912 reset halt will fail.
47  *
48  * If someone can submit a patch with an explanation it will be greatly
49  * appreciated, but as far as I can tell (ØH) DCLK is generated upon
50  * clk=0 in TAP_IDLE. Good luck deducing that from the ARM documentation!
51  * The ARM documentation uses the term "DCLK is asserted while in the TAP_IDLE
52  * state". With hardware there is no such thing as *while* in a state. There
53  * are only edges. So clk => 0 is in fact a very subtle state transition that
54  * happens *while* in the TAP_IDLE state. "#&¤"#¤&"#&"#&
55  *
56  * For "reset halt" the last thing that happens before srst is asserted
57  * is that the breakpoint is set up. If DCLK is not wiggled one last
58  * time before the reset, then the breakpoint is not set up and
59  * "reset halt" will fail to halt.
60  *
61  */
62 #define CLOCK_IDLE() 0
63
64
65 /* The bitbang driver leaves the TCK 0 when in idle */
66 static void bitbang_end_state(tap_state_t state)
67 {
68         if (tap_is_state_stable(state))
69                 tap_set_end_state(state);
70         else
71         {
72                 LOG_ERROR("BUG: %i is not a valid end state", state);
73                 exit(-1);
74         }
75 }
76
77 static void bitbang_state_move(int skip)
78 {
79         int i=0, tms=0;
80         uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
81         int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
82
83         for (i = skip; i < tms_count; i++)
84         {
85                 tms = (tms_scan >> i) & 1;
86                 bitbang_interface->write(0, tms, 0);
87                 bitbang_interface->write(1, tms, 0);
88         }
89         bitbang_interface->write(CLOCK_IDLE(), tms, 0);
90
91         tap_set_state(tap_get_end_state());
92 }
93
94 static void bitbang_path_move(pathmove_command_t *cmd)
95 {
96         int num_states = cmd->num_states;
97         int state_count;
98         int tms = 0;
99
100         state_count = 0;
101         while (num_states)
102         {
103                 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
104                 {
105                         tms = 0;
106                 }
107                 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
108                 {
109                         tms = 1;
110                 }
111                 else
112                 {
113                         LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
114                         exit(-1);
115                 }
116
117                 bitbang_interface->write(0, tms, 0);
118                 bitbang_interface->write(1, tms, 0);
119
120                 tap_set_state(cmd->path[state_count]);
121                 state_count++;
122                 num_states--;
123         }
124
125         bitbang_interface->write(CLOCK_IDLE(), tms, 0);
126
127         tap_set_end_state(tap_get_state());
128 }
129
130 static void bitbang_runtest(int num_cycles)
131 {
132         int i;
133
134         tap_state_t saved_end_state = tap_get_end_state();
135
136         /* only do a state_move when we're not already in IDLE */
137         if (tap_get_state() != TAP_IDLE)
138         {
139                 bitbang_end_state(TAP_IDLE);
140                 bitbang_state_move(0);
141         }
142
143         /* execute num_cycles */
144         for (i = 0; i < num_cycles; i++)
145         {
146                 bitbang_interface->write(0, 0, 0);
147                 bitbang_interface->write(1, 0, 0);
148         }
149         bitbang_interface->write(CLOCK_IDLE(), 0, 0);
150
151         /* finish in end_state */
152         bitbang_end_state(saved_end_state);
153         if (tap_get_state() != tap_get_end_state())
154                 bitbang_state_move(0);
155 }
156
157
158 static void bitbang_stableclocks(int num_cycles)
159 {
160         int tms = (tap_get_state() == TAP_RESET ? 1 : 0);
161         int i;
162
163         /* send num_cycles clocks onto the cable */
164         for (i = 0; i < num_cycles; i++)
165         {
166                 bitbang_interface->write(1, tms, 0);
167                 bitbang_interface->write(0, tms, 0);
168         }
169 }
170
171
172
173 static void bitbang_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
174 {
175         tap_state_t saved_end_state = tap_get_end_state();
176         int bit_cnt;
177
178         if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) || (ir_scan && (tap_get_state() == TAP_IRSHIFT))))
179         {
180                 if (ir_scan)
181                         bitbang_end_state(TAP_IRSHIFT);
182                 else
183                         bitbang_end_state(TAP_DRSHIFT);
184
185                 bitbang_state_move(0);
186                 bitbang_end_state(saved_end_state);
187         }
188
189         for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++)
190         {
191                 int val=0;
192                 int tms=(bit_cnt==scan_size-1) ? 1 : 0;
193                 int tdi;
194                 int bytec=bit_cnt/8;
195                 int bcval=1<<(bit_cnt % 8);
196
197                 /* if we're just reading the scan, but don't care about the output
198                  * default to outputting 'low', this also makes valgrind traces more readable,
199                  * as it removes the dependency on an uninitialised value
200                  */
201                 tdi=0;
202                 if ((type != SCAN_IN) && (buffer[bytec] & bcval))
203                         tdi=1;
204
205                 bitbang_interface->write(0, tms, tdi);
206
207                 if (type!=SCAN_OUT)
208                         val=bitbang_interface->read();
209
210                 bitbang_interface->write(1, tms, tdi);
211
212                 if (type != SCAN_OUT)
213                 {
214                         if (val)
215                                 buffer[bytec] |= bcval;
216                         else
217                                 buffer[bytec] &= ~bcval;
218                 }
219         }
220
221         if (tap_get_state() != tap_get_end_state())
222         {
223                 /* we *KNOW* the above loop transitioned out of
224                  * the shift state, so we skip the first state
225                  * and move directly to the end state.
226                  */
227                 bitbang_state_move(1);
228         }
229 }
230
231 int bitbang_execute_queue(void)
232 {
233         jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
234         int scan_size;
235         enum scan_type type;
236         uint8_t *buffer;
237         int retval;
238
239         if (!bitbang_interface)
240         {
241                 LOG_ERROR("BUG: Bitbang interface called, but not yet initialized");
242                 exit(-1);
243         }
244
245         /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
246          * that wasn't handled by a caller-provided error handler
247          */
248         retval = ERROR_OK;
249
250         if(bitbang_interface->blink)
251                 bitbang_interface->blink(1);
252
253         while (cmd)
254         {
255                 switch (cmd->type)
256                 {
257                         case JTAG_RESET:
258 #ifdef _DEBUG_JTAG_IO_
259                                 LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
260 #endif
261                                 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
262                                 {
263                                         tap_set_state(TAP_RESET);
264                                 }
265                                 bitbang_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
266                                 break;
267                         case JTAG_RUNTEST:
268 #ifdef _DEBUG_JTAG_IO_
269                                 LOG_DEBUG("runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name(cmd->cmd.runtest->end_state) );
270 #endif
271                                 bitbang_end_state(cmd->cmd.runtest->end_state);
272                                 bitbang_runtest(cmd->cmd.runtest->num_cycles);
273                                 break;
274
275                         case JTAG_STABLECLOCKS:
276                                 /* this is only allowed while in a stable state.  A check for a stable
277                                  * state was done in jtag_add_clocks()
278                                  */
279                                 bitbang_stableclocks(cmd->cmd.stableclocks->num_cycles);
280                                 break;
281
282                         case JTAG_STATEMOVE:
283 #ifdef _DEBUG_JTAG_IO_
284                                 LOG_DEBUG("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
285 #endif
286                                 bitbang_end_state(cmd->cmd.statemove->end_state);
287                                 bitbang_state_move(0);
288                                 break;
289                         case JTAG_PATHMOVE:
290 #ifdef _DEBUG_JTAG_IO_
291                                 LOG_DEBUG("pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states,
292                                         tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
293 #endif
294                                 bitbang_path_move(cmd->cmd.pathmove);
295                                 break;
296                         case JTAG_SCAN:
297 #ifdef _DEBUG_JTAG_IO_
298                                 LOG_DEBUG("%s scan end in %s",  (cmd->cmd.scan->ir_scan) ? "IR" : "DR", tap_state_name(cmd->cmd.scan->end_state) );
299 #endif
300                                 bitbang_end_state(cmd->cmd.scan->end_state);
301                                 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
302                                 type = jtag_scan_type(cmd->cmd.scan);
303                                 bitbang_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
304                                 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
305                                         retval = ERROR_JTAG_QUEUE_FAILED;
306                                 if (buffer)
307                                         free(buffer);
308                                 break;
309                         case JTAG_SLEEP:
310 #ifdef _DEBUG_JTAG_IO_
311                                 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
312 #endif
313                                 jtag_sleep(cmd->cmd.sleep->us);
314                                 break;
315                         default:
316                                 LOG_ERROR("BUG: unknown JTAG command type encountered");
317                                 exit(-1);
318                 }
319                 cmd = cmd->next;
320         }
321         if(bitbang_interface->blink)
322                 bitbang_interface->blink(0);
323
324         return retval;
325 }