1 /*****************************************************************************
2 * Copyright (C) 2016 by Matthias Welwarsky <matthias.welwarsky@sysgo.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 ****************************************************************************/
20 #include "target_type.h"
22 #include "arm_adi_v5.h"
24 #include <jtag/jtag.h>
32 static int mem_ap_target_create(struct target
*target
, Jim_Interp
*interp
)
34 struct mem_ap
*mem_ap
;
35 struct adiv5_private_config
*pc
;
37 pc
= (struct adiv5_private_config
*)target
->private_config
;
41 if (pc
->ap_num
== DP_APSEL_INVALID
) {
42 LOG_ERROR("AP number not specified");
46 mem_ap
= calloc(1, sizeof(struct mem_ap
));
48 LOG_ERROR("Out of memory");
52 mem_ap
->ap_num
= pc
->ap_num
;
53 mem_ap
->arm
.common_magic
= ARM_COMMON_MAGIC
;
54 mem_ap
->arm
.dap
= pc
->dap
;
56 target
->arch_info
= mem_ap
;
61 static int mem_ap_init_target(struct command_context
*cmd_ctx
, struct target
*target
)
63 LOG_DEBUG("%s", __func__
);
64 target
->state
= TARGET_UNKNOWN
;
68 static int mem_ap_arch_state(struct target
*target
)
70 LOG_DEBUG("%s", __func__
);
74 static int mem_ap_poll(struct target
*target
)
76 if (target
->state
== TARGET_UNKNOWN
)
77 target
->state
= TARGET_RUNNING
;
82 static int mem_ap_halt(struct target
*target
)
84 LOG_DEBUG("%s", __func__
);
85 target
->state
= TARGET_HALTED
;
89 static int mem_ap_resume(struct target
*target
, int current
, target_addr_t address
,
90 int handle_breakpoints
, int debug_execution
)
92 LOG_DEBUG("%s", __func__
);
93 target
->state
= TARGET_RUNNING
;
97 static int mem_ap_step(struct target
*target
, int current
, target_addr_t address
,
98 int handle_breakpoints
)
100 LOG_DEBUG("%s", __func__
);
101 target
->state
= TARGET_HALTED
;
105 static int mem_ap_assert_reset(struct target
*target
)
107 target
->state
= TARGET_RESET
;
109 LOG_DEBUG("%s", __func__
);
113 static int mem_ap_examine(struct target
*target
)
115 struct mem_ap
*mem_ap
= target
->arch_info
;
117 if (!target_was_examined(target
)) {
118 mem_ap
->ap
= dap_ap(mem_ap
->arm
.dap
, mem_ap
->ap_num
);
119 target_set_examined(target
);
120 target
->state
= TARGET_UNKNOWN
;
121 return mem_ap_init(mem_ap
->ap
);
127 static int mem_ap_deassert_reset(struct target
*target
)
129 if (target
->reset_halt
)
130 target
->state
= TARGET_HALTED
;
132 target
->state
= TARGET_RUNNING
;
134 LOG_DEBUG("%s", __func__
);
138 static int mem_ap_read_memory(struct target
*target
, target_addr_t address
,
139 uint32_t size
, uint32_t count
, uint8_t *buffer
)
141 struct mem_ap
*mem_ap
= target
->arch_info
;
143 LOG_DEBUG("Reading memory at physical address 0x" TARGET_ADDR_FMT
144 "; size %" PRId32
"; count %" PRId32
, address
, size
, count
);
146 if (count
== 0 || buffer
== NULL
)
147 return ERROR_COMMAND_SYNTAX_ERROR
;
149 return mem_ap_read_buf(mem_ap
->ap
, buffer
, size
, count
, address
);
152 static int mem_ap_write_memory(struct target
*target
, target_addr_t address
,
153 uint32_t size
, uint32_t count
,
154 const uint8_t *buffer
)
156 struct mem_ap
*mem_ap
= target
->arch_info
;
158 LOG_DEBUG("Writing memory at physical address 0x" TARGET_ADDR_FMT
159 "; size %" PRId32
"; count %" PRId32
, address
, size
, count
);
161 if (count
== 0 || buffer
== NULL
)
162 return ERROR_COMMAND_SYNTAX_ERROR
;
164 return mem_ap_write_buf(mem_ap
->ap
, buffer
, size
, count
, address
);
167 struct target_type mem_ap_target
= {
170 .target_create
= mem_ap_target_create
,
171 .init_target
= mem_ap_init_target
,
172 .examine
= mem_ap_examine
,
173 .target_jim_configure
= adiv5_jim_configure
,
176 .arch_state
= mem_ap_arch_state
,
179 .resume
= mem_ap_resume
,
182 .assert_reset
= mem_ap_assert_reset
,
183 .deassert_reset
= mem_ap_deassert_reset
,
185 .read_memory
= mem_ap_read_memory
,
186 .write_memory
= mem_ap_write_memory
,