1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2009 by Simon Qian *
5 * SimonQian@SimonQian.com *
6 ***************************************************************************/
14 #include "target_type.h"
16 #define AVR_JTAG_INS_LEN 4
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);
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);
30 static int avr_assert_reset(struct target *target);
31 static int avr_deassert_reset(struct target *target);
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);
39 struct target_type avr_target = {
43 .arch_state = avr_arch_state,
49 .assert_reset = avr_assert_reset,
50 .deassert_reset = avr_deassert_reset,
52 .get_gdb_reg_list = avr_get_gdb_reg_list,
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,
60 .run_algorithm = avr_run_algorithm,
62 .add_breakpoint = avr_add_breakpoint,
63 .remove_breakpoint = avr_remove_breakpoint,
64 .add_watchpoint = avr_add_watchpoint,
65 .remove_watchpoint = avr_remove_watchpoint,
67 .target_create = avr_target_create,
68 .init_target = avr_init_target,
71 static int avr_target_create(struct target *target, Jim_Interp *interp)
73 struct avr_common *avr = calloc(1, sizeof(struct avr_common));
75 avr->jtag_info.tap = target->tap;
76 target->arch_info = avr;
81 static int avr_init_target(struct command_context *cmd_ctx, struct target *target)
83 LOG_DEBUG("%s", __func__);
87 static int avr_arch_state(struct target *target)
89 LOG_DEBUG("%s", __func__);
93 static int avr_poll(struct target *target)
95 if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
96 target->state = TARGET_HALTED;
98 LOG_DEBUG("%s", __func__);
102 static int avr_halt(struct target *target)
104 LOG_DEBUG("%s", __func__);
108 static int avr_resume(struct target *target, int current, target_addr_t address,
109 int handle_breakpoints, int debug_execution)
111 LOG_DEBUG("%s", __func__);
115 static int avr_step(struct target *target, int current, target_addr_t address, int handle_breakpoints)
117 LOG_DEBUG("%s", __func__);
121 static int avr_assert_reset(struct target *target)
123 target->state = TARGET_RESET;
125 LOG_DEBUG("%s", __func__);
129 static int avr_deassert_reset(struct target *target)
131 target->state = TARGET_RUNNING;
133 LOG_DEBUG("%s", __func__);
137 int avr_jtag_senddat(struct jtag_tap *tap, uint32_t *dr_in, uint32_t dr_out,
140 return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
143 int avr_jtag_sendinstr(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out)
145 return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
148 /* IR and DR functions */
149 static int mcu_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out,
153 LOG_ERROR("invalid tap");
156 if (ir_len != tap->ir_length) {
157 LOG_ERROR("invalid ir_len");
162 jtag_add_plain_ir_scan(tap->ir_length, ir_out, ir_in, TAP_IDLE);
168 static int mcu_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out,
172 LOG_ERROR("invalid tap");
177 jtag_add_plain_dr_scan(dr_len, dr_out, dr_in, TAP_IDLE);
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)
187 LOG_ERROR("ir_len overflow, maximum is 8");
191 mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
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)
200 LOG_ERROR("dr_len overflow, maximum is 32");
204 mcu_write_dr(tap, (uint8_t *)dr_in, (uint8_t *)&dr_out, dr_len, rti);
209 int mcu_execute_queue(void)
211 return jtag_execute_queue();