flash/nor/at91samd: Use 32-bit register writes for ST-Link compat
[fw/openocd] / src / jtag / interface.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /***************************************************************************
4  *   Copyright (C) 2005 by Dominic Rath                                    *
5  *   Dominic.Rath@gmx.de                                                   *
6  *                                                                         *
7  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
8  *   oyvind.harboe@zylin.com                                               *
9  *                                                                         *
10  *   Copyright (C) 2009 Zachary T Welch                                    *
11  *   zw@superlucidity.net                                                  *
12  ***************************************************************************/
13
14 #ifndef OPENOCD_JTAG_INTERFACE_H
15 #define OPENOCD_JTAG_INTERFACE_H
16
17 #include <jtag/jtag.h>
18 #include <jtag/swim.h>
19 #include <target/arm_tpiu_swo.h>
20
21 /* @file
22  * The "Cable Helper API" is what the cable drivers can use to help
23  * implement their "Cable API".  So a Cable Helper API is a set of
24  * helper functions used by cable drivers, and this is different from a
25  * Cable API.  A "Cable API" is what higher level code used to talk to a
26  * cable.
27  */
28
29
30 /** implementation of wrapper function tap_set_state() */
31 void tap_set_state_impl(tap_state_t new_state);
32
33 /**
34  * This function sets the state of a "state follower" which tracks the
35  * state of the TAPs connected to the cable.  The state follower is
36  * hopefully always in the same state as the actual TAPs in the jtag
37  * chain, and will be so if there are no bugs in the tracking logic
38  * within that cable driver.
39  *
40  * All the cable drivers call this function to indicate the state they
41  * think the TAPs attached to their cables are in.  Because this
42  * function can also log transitions, it will be helpful to call this
43  * function with every transition that the TAPs being manipulated are
44  * expected to traverse, not just end points of a multi-step state path.
45  *
46  * @param new_state The state we think the TAPs are currently in (or
47  * are about to enter).
48  */
49 #define tap_set_state(new_state) \
50         do { \
51                 LOG_DEBUG_IO("tap_set_state(%s)", tap_state_name(new_state)); \
52                 tap_set_state_impl(new_state); \
53         } while (0)
54
55 /**
56  * This function gets the state of the "state follower" which tracks the
57  * state of the TAPs connected to the cable. @see tap_set_state @return
58  * tap_state_t The state the TAPs are in now.
59  */
60 tap_state_t tap_get_state(void);
61
62 /**
63  * This function sets the state of an "end state follower" which tracks
64  * the state that any cable driver thinks will be the end (resultant)
65  * state of the current TAP SIR or SDR operation.
66  *
67  * At completion of that TAP operation this value is copied into the
68  * state follower via tap_set_state().
69  *
70  * @param new_end_state The state the TAPs should enter at completion of
71  * a pending TAP operation.
72  */
73 void tap_set_end_state(tap_state_t new_end_state);
74
75 /**
76  * For more information, @see tap_set_end_state
77  * @return tap_state_t - The state the TAPs should be in at completion of the current TAP operation.
78  */
79 tap_state_t tap_get_end_state(void);
80
81 /**
82  * This function provides a "bit sequence" indicating what has to be
83  * done with TMS during a sequence of seven TAP clock cycles in order to
84  * get from state \a "from" to state \a "to".
85  *
86  * The length of the sequence must be determined with a parallel call to
87  * tap_get_tms_path_len().
88  *
89  * @param from The starting state.
90  * @param to The desired final state.
91  * @return int The required TMS bit sequence, with the first bit in the
92  * sequence at bit 0.
93  */
94 int tap_get_tms_path(tap_state_t from, tap_state_t to);
95
96 /**
97  * Function int tap_get_tms_path_len
98  * returns the total number of bits that represents a TMS path
99  * transition as given by the function tap_get_tms_path().
100  *
101  * For at least one interface (JLink) it's not OK to simply "pad" TMS
102  * sequences to fit a whole byte.  (I suspect this is a general TAP
103  * problem within OOCD.) Padding TMS causes all manner of instability
104  * that's not easily discovered.  Using this routine we can apply
105  * EXACTLY the state transitions required to make something work - no
106  * more - no less.
107  *
108  * @param from is the starting state
109  * @param to is the resultant or final state
110  * @return int - the total number of bits in a transition.
111  */
112 int tap_get_tms_path_len(tap_state_t from, tap_state_t to);
113
114
115 /**
116  * Function tap_move_ndx
117  * when given a stable state, returns an index from 0-5.  The index corresponds to a
118  * sequence of stable states which are given in this order: <p>
119  * { TAP_RESET, TAP_IDLE, TAP_DRSHIFT, TAP_DRPAUSE, TAP_IRSHIFT, TAP_IRPAUSE }
120  * <p>
121  * This sequence corresponds to look up tables which are used in some of the
122  * cable drivers.
123  * @param astate is the stable state to find in the sequence.  If a non stable
124  *  state is passed, this may cause the program to output an error message
125  *  and terminate.
126  * @return int - the array (or sequence) index as described above
127  */
128 int tap_move_ndx(tap_state_t astate);
129
130 /**
131  * Function tap_is_state_stable
132  * returns true if the \a astate is stable.
133  */
134 bool tap_is_state_stable(tap_state_t astate);
135
136 /**
137  * Function tap_state_transition
138  * takes a current TAP state and returns the next state according to the tms value.
139  * @param current_state is the state of a TAP currently.
140  * @param tms is either zero or non-zero, just like a real TMS line in a jtag interface.
141  * @return tap_state_t - the next state a TAP would enter.
142  */
143 tap_state_t tap_state_transition(tap_state_t current_state, bool tms);
144
145 /** Allow switching between old and new TMS tables. @see tap_get_tms_path */
146 void tap_use_new_tms_table(bool use_new);
147 /** @returns True if new TMS table is active; false otherwise. */
148 bool tap_uses_new_tms_table(void);
149
150
151 /**
152  * @brief Prints verbose TAP state transitions for the given TMS/TDI buffers.
153  * @param tms_buf must points to a buffer containing the TMS bitstream.
154  * @param tdi_buf must points to a buffer containing the TDI bitstream.
155  * @param tap_len must specify the length of the TMS/TDI bitstreams.
156  * @param start_tap_state must specify the current TAP state.
157  * @returns the final TAP state; pass as @a start_tap_state in following call.
158  */
159 static inline tap_state_t jtag_debug_state_machine(const void *tms_buf,
160                 const void *tdi_buf, unsigned tap_len, tap_state_t start_tap_state)
161 {
162         /* Private declaration */
163         tap_state_t jtag_debug_state_machine_(const void *tms_buf, const void *tdi_buf,
164                         unsigned tap_len, tap_state_t start_tap_state);
165
166         if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO))
167                 return jtag_debug_state_machine_(tms_buf, tdi_buf, tap_len, start_tap_state);
168         else
169                 return start_tap_state;
170 }
171
172 /**
173  * Represents a driver for a debugging interface.
174  *
175  * @todo Rename; perhaps "debug_driver".  This isn't an interface,
176  * it's a driver!  Also, not all drivers support JTAG.
177  *
178  * @todo We need a per-instance structure too, and changes to pass
179  * that structure to the driver.  Instances can for example be in
180  * either SWD or JTAG modes.  This will help remove globals, and
181  * eventually to cope with systems which have more than one such
182  * debugging interface.
183  */
184 struct jtag_interface {
185         /**
186          * Bit vector listing capabilities exposed by this driver.
187          */
188         unsigned supported;
189 #define DEBUG_CAP_TMS_SEQ       (1 << 0)
190
191         /**
192          * Execute queued commands.
193          * @returns ERROR_OK on success, or an error code on failure.
194          */
195         int (*execute_queue)(void);
196 };
197
198 /**
199  * Represents a driver for a debugging interface
200  *
201  * @todo We need a per-instance structure too, and changes to pass
202  * that structure to the driver.  Instances can for example be in
203  * either SWD or JTAG modes.  This will help remove globals, and
204  * eventually to cope with systems which have more than one such
205  * debugging interface.
206  */
207 struct adapter_driver {
208         /** The name of the interface driver. */
209         const char * const name;
210
211         /** transports supported in C code (NULL terminated vector) */
212         const char * const *transports;
213
214         /**
215          * The interface driver may register additional commands to expose
216          * additional features not covered by the standard command set.
217          */
218         const struct command_registration *commands;
219
220         /**
221          * Interface driver must initialize any resources and connect to a
222          * JTAG device.
223          *
224          * quit() is invoked if and only if init() succeeds. quit() is always
225          * invoked if init() succeeds. Same as malloc() + free(). Always
226          * invoke free() if malloc() succeeds and do not invoke free()
227          * otherwise.
228          *
229          * @returns ERROR_OK on success, or an error code on failure.
230          */
231         int (*init)(void);
232
233         /**
234          * Interface driver must tear down all resources and disconnect from
235          * the JTAG device.
236          *
237          * @returns ERROR_OK on success, or an error code on failure.
238          */
239         int (*quit)(void);
240
241         /**
242          * Control (assert/deassert) the signals SRST and TRST on the interface.
243          * This function is synchronous and should be called after the adapter
244          * queue has been properly flushed.
245          * This function is optional.
246          * Adapters that don't support resets can either not define this function
247          * or return an error code.
248          * Adapters that don't support one of the two reset should ignore the
249          * request to assert the missing signal and eventually log an error.
250          *
251          * @param srst 1 to assert SRST, 0 to deassert SRST.
252          * @param trst 1 to assert TRST, 0 to deassert TRST.
253          * @returns ERROR_OK on success, or an error code on failure.
254          */
255         int (*reset)(int srst, int trst);
256
257         /**
258          * Set the interface speed.
259          * @param speed The new interface speed setting.
260          * @returns ERROR_OK on success, or an error code on failure.
261          */
262         int (*speed)(int speed);
263
264         /**
265          * Returns JTAG maximum speed for KHz. 0 = RTCK. The function returns
266          *  a failure if it can't support the KHz/RTCK.
267          *
268          *  WARNING!!!! if RTCK is *slow* then think carefully about
269          *  whether you actually want to support this in the driver.
270          *  Many target scripts are written to handle the absence of RTCK
271          *  and use a fallback kHz TCK.
272          * @returns ERROR_OK on success, or an error code on failure.
273          */
274         int (*khz)(int khz, int *jtag_speed);
275
276         /**
277          * Calculate the clock frequency (in KHz) for the given @a speed.
278          * @param speed The desired interface speed setting.
279          * @param khz On return, contains the speed in KHz (0 for RTCK).
280          * @returns ERROR_OK on success, or an error code if the
281          * interface cannot support the specified speed (KHz or RTCK).
282          */
283         int (*speed_div)(int speed, int *khz);
284
285         /**
286          * Read and clear the power dropout flag. Note that a power dropout
287          * can be transitionary, easily much less than a ms.
288          *
289          * To find out if the power is *currently* on, one must invoke this
290          * method twice.  Once to clear the power dropout flag and a second
291          * time to read the current state.  The default implementation
292          * never reports power dropouts.
293          *
294          * @returns ERROR_OK on success, or an error code on failure.
295          */
296         int (*power_dropout)(int *power_dropout);
297
298         /**
299          * Read and clear the srst asserted detection flag.
300          *
301          * Like power_dropout this does *not* read the current
302          * state.  SRST assertion is transitionary and may be much
303          * less than 1ms, so the interface driver must watch for these
304          * events until this routine is called.
305          *
306          * @param srst_asserted On return, indicates whether SRST has
307          * been asserted.
308          * @returns ERROR_OK on success, or an error code on failure.
309          */
310         int (*srst_asserted)(int *srst_asserted);
311
312         /**
313          * Configure trace parameters for the adapter
314          *
315          * @param enabled Whether to enable trace
316          * @param pin_protocol Configured pin protocol
317          * @param port_size Trace port width for sync mode
318          * @param trace_freq A pointer to the configured trace
319          * frequency; if it points to 0, the adapter driver must write
320          * its maximum supported rate there
321          * @param traceclkin_freq TRACECLKIN frequency provided to the TPIU in Hz
322          * @param prescaler Pointer to the SWO prescaler calculated by the
323          * adapter
324          * @returns ERROR_OK on success, an error code on failure.
325          */
326         int (*config_trace)(bool enabled, enum tpiu_pin_protocol pin_protocol,
327                 uint32_t port_size, unsigned int *trace_freq,
328                 unsigned int traceclkin_freq, uint16_t *prescaler);
329
330         /**
331          * Poll for new trace data
332          *
333          * @param buf A pointer to buffer to store received data
334          * @param size A pointer to buffer size; must be filled with
335          * the actual amount of bytes written
336          *
337          * @returns ERROR_OK on success, an error code on failure.
338          */
339         int (*poll_trace)(uint8_t *buf, size_t *size);
340
341         /** Low-level JTAG APIs */
342         struct jtag_interface *jtag_ops;
343
344         /** Low-level SWD APIs */
345         const struct swd_driver *swd_ops;
346
347         /* DAP APIs over JTAG transport */
348         const struct dap_ops *dap_jtag_ops;
349
350         /* DAP APIs over SWD transport */
351         const struct dap_ops *dap_swd_ops;
352
353         /* SWIM APIs */
354         const struct swim_driver *swim_ops;
355 };
356
357 extern const char * const jtag_only[];
358
359 int adapter_resets(int assert_trst, int assert_srst);
360 int adapter_assert_reset(void);
361 int adapter_deassert_reset(void);
362 int adapter_config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
363                 uint32_t port_size, unsigned int *trace_freq,
364                 unsigned int traceclkin_freq, uint16_t *prescaler);
365 int adapter_poll_trace(uint8_t *buf, size_t *size);
366
367 #endif /* OPENOCD_JTAG_INTERFACE_H */