target: add generic Xtensa LX support
[fw/openocd] / src / target / xtensa / xtensa_chip.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /***************************************************************************
4  *   Xtensa Chip-level Target Support for OpenOCD                          *
5  *   Copyright (C) 2020-2022 Cadence Design Systems, Inc.                  *
6  ***************************************************************************/
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include "assert.h"
13 #include <target/target.h>
14 #include <target/target_type.h>
15 #include <target/arm_adi_v5.h>
16 #include <rtos/rtos.h>
17 #include "xtensa_chip.h"
18
19 int xtensa_chip_init_arch_info(struct target *target, void *arch_info,
20         struct xtensa_debug_module_config *dm_cfg)
21 {
22         struct xtensa_chip_common *xtensa_chip = (struct xtensa_chip_common *)arch_info;
23         int ret = xtensa_init_arch_info(target, &xtensa_chip->xtensa, dm_cfg);
24         if (ret != ERROR_OK)
25                 return ret;
26         /* All xtensa target structures point back to original xtensa_chip */
27         xtensa_chip->xtensa.xtensa_chip = arch_info;
28         return ERROR_OK;
29 }
30
31 int xtensa_chip_target_init(struct command_context *cmd_ctx, struct target *target)
32 {
33         return xtensa_target_init(cmd_ctx, target);
34 }
35
36 int xtensa_chip_arch_state(struct target *target)
37 {
38         return ERROR_OK;
39 }
40
41 static int xtensa_chip_poll(struct target *target)
42 {
43         enum target_state old_state = target->state;
44         int ret = xtensa_poll(target);
45
46         if (old_state != TARGET_HALTED && target->state == TARGET_HALTED) {
47                 /*Call any event callbacks that are applicable */
48                 if (old_state == TARGET_DEBUG_RUNNING)
49                         target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
50                 else
51                         target_call_event_callbacks(target, TARGET_EVENT_HALTED);
52         }
53
54         return ret;
55 }
56
57 static int xtensa_chip_virt2phys(struct target *target,
58         target_addr_t virtual, target_addr_t *physical)
59 {
60         if (physical) {
61                 *physical = virtual;
62                 return ERROR_OK;
63         }
64         return ERROR_FAIL;
65 }
66
67 static const struct xtensa_debug_ops xtensa_chip_dm_dbg_ops = {
68         .queue_enable = xtensa_dm_queue_enable,
69         .queue_reg_read = xtensa_dm_queue_reg_read,
70         .queue_reg_write = xtensa_dm_queue_reg_write
71 };
72
73 static const struct xtensa_power_ops xtensa_chip_dm_pwr_ops = {
74         .queue_reg_read = xtensa_dm_queue_pwr_reg_read,
75         .queue_reg_write = xtensa_dm_queue_pwr_reg_write
76 };
77
78 static int xtensa_chip_target_create(struct target *target, Jim_Interp *interp)
79 {
80         struct xtensa_debug_module_config xtensa_chip_dm_cfg = {
81                 .dbg_ops = &xtensa_chip_dm_dbg_ops,
82                 .pwr_ops = &xtensa_chip_dm_pwr_ops,
83                 .tap = NULL,
84                 .queue_tdi_idle = NULL,
85                 .queue_tdi_idle_arg = NULL,
86         };
87
88         xtensa_chip_dm_cfg.tap = target->tap;
89         LOG_DEBUG("JTAG: %s:%s pos %d", target->tap->chip, target->tap->tapname, target->tap->abs_chain_position);
90
91         struct xtensa_chip_common *xtensa_chip = calloc(1, sizeof(struct xtensa_chip_common));
92         if (!xtensa_chip) {
93                 LOG_ERROR("Failed to alloc chip-level memory!");
94                 return ERROR_FAIL;
95         }
96
97         int ret = xtensa_chip_init_arch_info(target, xtensa_chip, &xtensa_chip_dm_cfg);
98         if (ret != ERROR_OK) {
99                 LOG_ERROR("Failed to init arch info!");
100                 free(xtensa_chip);
101                 return ret;
102         }
103
104         /*Assume running target. If different, the first poll will fix this. */
105         target->state = TARGET_RUNNING;
106         target->debug_reason = DBG_REASON_NOTHALTED;
107         return ERROR_OK;
108 }
109
110 void xtensa_chip_target_deinit(struct target *target)
111 {
112         struct xtensa *xtensa = target_to_xtensa(target);
113         xtensa_target_deinit(target);
114         free(xtensa->xtensa_chip);
115 }
116
117 static int xtensa_chip_examine(struct target *target)
118 {
119         return xtensa_examine(target);
120 }
121
122 int xtensa_chip_jim_configure(struct target *target, struct jim_getopt_info *goi)
123 {
124         target->has_dap = false;
125         return JIM_CONTINUE;
126 }
127
128 /** Methods for generic example of Xtensa-based chip-level targets. */
129 struct target_type xtensa_chip_target = {
130         .name = "xtensa",
131
132         .poll = xtensa_chip_poll,
133         .arch_state = xtensa_chip_arch_state,
134
135         .halt = xtensa_halt,
136         .resume = xtensa_resume,
137         .step = xtensa_step,
138
139         .assert_reset = xtensa_assert_reset,
140         .deassert_reset = xtensa_deassert_reset,
141         .soft_reset_halt = xtensa_soft_reset_halt,
142
143         .virt2phys = xtensa_chip_virt2phys,
144         .mmu = xtensa_mmu_is_enabled,
145         .read_memory = xtensa_read_memory,
146         .write_memory = xtensa_write_memory,
147
148         .read_buffer = xtensa_read_buffer,
149         .write_buffer = xtensa_write_buffer,
150
151         .checksum_memory = xtensa_checksum_memory,
152
153         .get_gdb_reg_list = xtensa_get_gdb_reg_list,
154
155         .add_breakpoint = xtensa_breakpoint_add,
156         .remove_breakpoint = xtensa_breakpoint_remove,
157
158         .add_watchpoint = xtensa_watchpoint_add,
159         .remove_watchpoint = xtensa_watchpoint_remove,
160
161         .target_create = xtensa_chip_target_create,
162         .target_jim_configure = xtensa_chip_jim_configure,
163         .init_target = xtensa_chip_target_init,
164         .examine = xtensa_chip_examine,
165         .deinit_target = xtensa_chip_target_deinit,
166
167         .gdb_query_custom = xtensa_gdb_query_custom,
168
169         .commands = xtensa_command_handlers,
170 };