openocd: fix SPDX tag format for files .c
[fw/openocd] / src / target / avrt.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 /***************************************************************************
4  *   Copyright (C) 2009 by Simon Qian                                      *
5  *   SimonQian@SimonQian.com                                               *
6  ***************************************************************************/
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include "avrt.h"
13 #include "target.h"
14 #include "target_type.h"
15
16 #define AVR_JTAG_INS_LEN        4
17
18 /* forward declarations */
19 static int avr_target_create(struct target *target, Jim_Interp *interp);
20 static int avr_init_target(struct command_context *cmd_ctx, struct target *target);
21
22 static int avr_arch_state(struct target *target);
23 static int avr_poll(struct target *target);
24 static int avr_halt(struct target *target);
25 static int avr_resume(struct target *target, int current, target_addr_t address,
26                 int handle_breakpoints, int debug_execution);
27 static int avr_step(struct target *target, int current, target_addr_t address,
28                 int handle_breakpoints);
29
30 static int avr_assert_reset(struct target *target);
31 static int avr_deassert_reset(struct target *target);
32
33 /* IR and DR functions */
34 static int mcu_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti);
35 static int mcu_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out, int dr_len, int rti);
36 static int mcu_write_ir_u8(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti);
37 static int mcu_write_dr_u32(struct jtag_tap *tap, uint32_t *ir_in, uint32_t ir_out, int dr_len, int rti);
38
39 struct target_type avr_target = {
40         .name = "avr",
41
42         .poll = avr_poll,
43         .arch_state = avr_arch_state,
44
45         .halt = avr_halt,
46         .resume = avr_resume,
47         .step = avr_step,
48
49         .assert_reset = avr_assert_reset,
50         .deassert_reset = avr_deassert_reset,
51 /*
52         .get_gdb_reg_list = avr_get_gdb_reg_list,
53
54         .read_memory = avr_read_memory,
55         .write_memory = avr_write_memory,
56         .bulk_write_memory = avr_bulk_write_memory,
57         .checksum_memory = avr_checksum_memory,
58         .blank_check_memory = avr_blank_check_memory,
59
60         .run_algorithm = avr_run_algorithm,
61
62         .add_breakpoint = avr_add_breakpoint,
63         .remove_breakpoint = avr_remove_breakpoint,
64         .add_watchpoint = avr_add_watchpoint,
65         .remove_watchpoint = avr_remove_watchpoint,
66 */
67         .target_create = avr_target_create,
68         .init_target = avr_init_target,
69 };
70
71 static int avr_target_create(struct target *target, Jim_Interp *interp)
72 {
73         struct avr_common *avr = calloc(1, sizeof(struct avr_common));
74
75         avr->jtag_info.tap = target->tap;
76         target->arch_info = avr;
77
78         return ERROR_OK;
79 }
80
81 static int avr_init_target(struct command_context *cmd_ctx, struct target *target)
82 {
83         LOG_DEBUG("%s", __func__);
84         return ERROR_OK;
85 }
86
87 static int avr_arch_state(struct target *target)
88 {
89         LOG_DEBUG("%s", __func__);
90         return ERROR_OK;
91 }
92
93 static int avr_poll(struct target *target)
94 {
95         if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
96                 target->state = TARGET_HALTED;
97
98         LOG_DEBUG("%s", __func__);
99         return ERROR_OK;
100 }
101
102 static int avr_halt(struct target *target)
103 {
104         LOG_DEBUG("%s", __func__);
105         return ERROR_OK;
106 }
107
108 static int avr_resume(struct target *target, int current, target_addr_t address,
109                 int handle_breakpoints, int debug_execution)
110 {
111         LOG_DEBUG("%s", __func__);
112         return ERROR_OK;
113 }
114
115 static int avr_step(struct target *target, int current, target_addr_t address, int handle_breakpoints)
116 {
117         LOG_DEBUG("%s", __func__);
118         return ERROR_OK;
119 }
120
121 static int avr_assert_reset(struct target *target)
122 {
123         target->state = TARGET_RESET;
124
125         LOG_DEBUG("%s", __func__);
126         return ERROR_OK;
127 }
128
129 static int avr_deassert_reset(struct target *target)
130 {
131         target->state = TARGET_RUNNING;
132
133         LOG_DEBUG("%s", __func__);
134         return ERROR_OK;
135 }
136
137 int avr_jtag_senddat(struct jtag_tap *tap, uint32_t *dr_in, uint32_t dr_out,
138                 int len)
139 {
140         return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
141 }
142
143 int avr_jtag_sendinstr(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out)
144 {
145         return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
146 }
147
148 /* IR and DR functions */
149 static int mcu_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out,
150                 int ir_len, int rti)
151 {
152         if (!tap) {
153                 LOG_ERROR("invalid tap");
154                 return ERROR_FAIL;
155         }
156         if (ir_len != tap->ir_length) {
157                 LOG_ERROR("invalid ir_len");
158                 return ERROR_FAIL;
159         }
160
161         {
162                 jtag_add_plain_ir_scan(tap->ir_length, ir_out, ir_in, TAP_IDLE);
163         }
164
165         return ERROR_OK;
166 }
167
168 static int mcu_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out,
169                 int dr_len, int rti)
170 {
171         if (!tap) {
172                 LOG_ERROR("invalid tap");
173                 return ERROR_FAIL;
174         }
175
176         {
177                 jtag_add_plain_dr_scan(dr_len, dr_out, dr_in, TAP_IDLE);
178         }
179
180         return ERROR_OK;
181 }
182
183 static int mcu_write_ir_u8(struct jtag_tap *tap, uint8_t *ir_in,
184                 uint8_t ir_out, int ir_len, int rti)
185 {
186         if (ir_len > 8) {
187                 LOG_ERROR("ir_len overflow, maximum is 8");
188                 return ERROR_FAIL;
189         }
190
191         mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
192
193         return ERROR_OK;
194 }
195
196 static int mcu_write_dr_u32(struct jtag_tap *tap, uint32_t *dr_in,
197                 uint32_t dr_out, int dr_len, int rti)
198 {
199         if (dr_len > 32) {
200                 LOG_ERROR("dr_len overflow, maximum is 32");
201                 return ERROR_FAIL;
202         }
203
204         mcu_write_dr(tap, (uint8_t *)dr_in, (uint8_t *)&dr_out, dr_len, rti);
205
206         return ERROR_OK;
207 }
208
209 int mcu_execute_queue(void)
210 {
211         return jtag_execute_queue();
212 }