openocd: fix SPDX tag format for files .c
[fw/openocd] / src / jtag / drivers / dummy.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 /***************************************************************************
4  *   Copyright (C) 2008 by Ã˜yvind Harboe                                   *
5  *   oyvind.harboe@zylin.com                                               *
6  ***************************************************************************/
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <jtag/interface.h>
13 #include "bitbang.h"
14 #include "hello.h"
15
16 /* my private tap controller state, which tracks state for calling code */
17 static tap_state_t dummy_state = TAP_RESET;
18
19 static int dummy_clock;         /* edge detector */
20
21 static int clock_count;         /* count clocks in any stable state, only stable states */
22
23 static uint32_t dummy_data;
24
25 static bb_value_t dummy_read(void)
26 {
27         int data = 1 & dummy_data;
28         dummy_data = (dummy_data >> 1) | (1 << 31);
29         return data ? BB_HIGH : BB_LOW;
30 }
31
32 static int dummy_write(int tck, int tms, int tdi)
33 {
34         /* TAP standard: "state transitions occur on rising edge of clock" */
35         if (tck != dummy_clock) {
36                 if (tck) {
37                         tap_state_t old_state = dummy_state;
38                         dummy_state = tap_state_transition(old_state, tms);
39
40                         if (old_state != dummy_state) {
41                                 if (clock_count) {
42                                         LOG_DEBUG("dummy_tap: %d stable clocks", clock_count);
43                                         clock_count = 0;
44                                 }
45
46                                 LOG_DEBUG("dummy_tap: %s", tap_state_name(dummy_state));
47
48 #if defined(DEBUG)
49                                 if (dummy_state == TAP_DRCAPTURE)
50                                         dummy_data = 0x01255043;
51 #endif
52                         } else {
53                                 /* this is a stable state clock edge, no change of state here,
54                                  * simply increment clock_count for subsequent logging
55                                  */
56                                 ++clock_count;
57                         }
58                 }
59                 dummy_clock = tck;
60         }
61         return ERROR_OK;
62 }
63
64 static int dummy_reset(int trst, int srst)
65 {
66         dummy_clock = 0;
67
68         if (trst || (srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
69                 dummy_state = TAP_RESET;
70
71         LOG_DEBUG("reset to: %s", tap_state_name(dummy_state));
72         return ERROR_OK;
73 }
74
75 static int dummy_led(int on)
76 {
77         return ERROR_OK;
78 }
79
80 static struct bitbang_interface dummy_bitbang = {
81                 .read = &dummy_read,
82                 .write = &dummy_write,
83                 .blink = &dummy_led,
84         };
85
86 static int dummy_khz(int khz, int *jtag_speed)
87 {
88         if (khz == 0)
89                 *jtag_speed = 0;
90         else
91                 *jtag_speed = 64000/khz;
92         return ERROR_OK;
93 }
94
95 static int dummy_speed_div(int speed, int *khz)
96 {
97         if (speed == 0)
98                 *khz = 0;
99         else
100                 *khz = 64000/speed;
101
102         return ERROR_OK;
103 }
104
105 static int dummy_speed(int speed)
106 {
107         return ERROR_OK;
108 }
109
110 static int dummy_init(void)
111 {
112         bitbang_interface = &dummy_bitbang;
113
114         return ERROR_OK;
115 }
116
117 static int dummy_quit(void)
118 {
119         return ERROR_OK;
120 }
121
122 static const struct command_registration dummy_command_handlers[] = {
123         {
124                 .name = "dummy",
125                 .mode = COMMAND_ANY,
126                 .help = "dummy interface driver commands",
127                 .chain = hello_command_handlers,
128                 .usage = "",
129         },
130         COMMAND_REGISTRATION_DONE,
131 };
132
133 /* The dummy driver is used to easily check the code path
134  * where the target is unresponsive.
135  */
136 static struct jtag_interface dummy_interface = {
137         .supported = DEBUG_CAP_TMS_SEQ,
138         .execute_queue = &bitbang_execute_queue,
139 };
140
141 struct adapter_driver dummy_adapter_driver = {
142         .name = "dummy",
143         .transports = jtag_only,
144         .commands = dummy_command_handlers,
145
146         .init = &dummy_init,
147         .quit = &dummy_quit,
148         .reset = &dummy_reset,
149         .speed = &dummy_speed,
150         .khz = &dummy_khz,
151         .speed_div = &dummy_speed_div,
152
153         .jtag_ops = &dummy_interface,
154 };