mips32, add option to avoid check in last instruction
[openocd.git] / src / target / ls1_sap.c
blobbc46ed4db814c1f5101d6f0dffa98827a5190378
1 /***************************************************************************
2 * Copyright (C) 2015 by Esben Haabendal *
3 * eha@deif.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 ***************************************************************************/
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
20 #include "target.h"
21 #include "target_type.h"
23 #include <jtag/jtag.h>
25 struct ls1_sap {
26 struct jtag_tap *tap;
29 static int ls1_sap_target_create(struct target *target, Jim_Interp *interp)
31 struct ls1_sap *ls1_sap = calloc(1, sizeof(struct ls1_sap));
33 ls1_sap->tap = target->tap;
34 target->arch_info = ls1_sap;
36 return ERROR_OK;
39 static int ls1_sap_init_target(struct command_context *cmd_ctx, struct target *target)
41 LOG_DEBUG("%s", __func__);
42 return ERROR_OK;
45 static int ls1_sap_arch_state(struct target *target)
47 LOG_DEBUG("%s", __func__);
48 return ERROR_OK;
51 static int ls1_sap_poll(struct target *target)
53 if ((target->state == TARGET_UNKNOWN) ||
54 (target->state == TARGET_RUNNING) ||
55 (target->state == TARGET_DEBUG_RUNNING))
56 target->state = TARGET_HALTED;
58 return ERROR_OK;
61 static int ls1_sap_halt(struct target *target)
63 LOG_DEBUG("%s", __func__);
64 return ERROR_OK;
67 static int ls1_sap_resume(struct target *target, int current, target_addr_t address,
68 int handle_breakpoints, int debug_execution)
70 LOG_DEBUG("%s", __func__);
71 return ERROR_OK;
74 static int ls1_sap_step(struct target *target, int current, target_addr_t address,
75 int handle_breakpoints)
77 LOG_DEBUG("%s", __func__);
78 return ERROR_OK;
81 static int ls1_sap_assert_reset(struct target *target)
83 target->state = TARGET_RESET;
85 LOG_DEBUG("%s", __func__);
86 return ERROR_OK;
89 static int ls1_sap_deassert_reset(struct target *target)
91 target->state = TARGET_RUNNING;
93 LOG_DEBUG("%s", __func__);
94 return ERROR_OK;
97 static void ls1_sap_set_instr(struct jtag_tap *tap, uint32_t new_instr)
99 struct scan_field field;
101 if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) == new_instr)
102 return;
104 field.num_bits = tap->ir_length;
105 uint8_t *t = calloc(DIV_ROUND_UP(field.num_bits, 8), 1);
106 field.out_value = t;
107 buf_set_u32(t, 0, field.num_bits, new_instr);
108 field.in_value = NULL;
109 jtag_add_ir_scan(tap, &field, TAP_IDLE);
110 free(t);
113 static void ls1_sap_set_addr_high(struct jtag_tap *tap, uint16_t addr_high)
115 struct scan_field field;
116 uint8_t buf[2];
118 ls1_sap_set_instr(tap, 0x21);
120 field.num_bits = 16;
121 field.out_value = buf;
122 buf_set_u32(buf, 0, 16, addr_high);
123 field.in_value = NULL;
124 field.check_value = NULL;
125 field.check_mask = NULL;
126 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
129 static void ls1_sap_memory_cmd(struct jtag_tap *tap, uint32_t address,
130 int32_t size, bool rnw)
132 struct scan_field field;
133 uint8_t cmd[8];
135 ls1_sap_set_instr(tap, 0x24);
137 field.num_bits = 64;
138 field.out_value = cmd;
139 buf_set_u64(cmd, 0, 9, 0);
140 buf_set_u64(cmd, 9, 3, size);
141 buf_set_u64(cmd, 12, 1, rnw);
142 buf_set_u64(cmd, 13, 3, 0);
143 buf_set_u64(cmd, 16, 32, address);
144 buf_set_u64(cmd, 48, 16, 0);
145 field.in_value = NULL;
146 field.check_value = NULL;
147 field.check_mask = NULL;
148 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
151 static void ls1_sap_memory_read(struct jtag_tap *tap, uint32_t size,
152 uint8_t *value)
154 struct scan_field field;
156 ls1_sap_set_instr(tap, 0x25);
158 field.num_bits = 8 * size;
159 field.out_value = NULL;
160 field.in_value = value;
161 field.check_value = NULL;
162 field.check_mask = NULL;
163 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
166 static void ls1_sap_memory_write(struct jtag_tap *tap, uint32_t size,
167 const uint8_t *value)
169 struct scan_field field;
171 ls1_sap_set_instr(tap, 0x25);
173 field.num_bits = 8 * size;
174 field.out_value = value;
175 field.in_value = NULL;
176 field.check_value = NULL;
177 field.check_mask = NULL;
178 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
181 static int ls1_sap_read_memory(struct target *target, target_addr_t address,
182 uint32_t size, uint32_t count, uint8_t *buffer)
184 LOG_DEBUG("Reading memory at physical address 0x%" TARGET_PRIxADDR
185 "; size %" PRId32 "; count %" PRId32, address, size, count);
187 if (count == 0 || buffer == NULL)
188 return ERROR_COMMAND_SYNTAX_ERROR;
190 ls1_sap_set_addr_high(target->tap, 0);
192 while (count--) {
193 ls1_sap_memory_cmd(target->tap, address, size, true);
194 ls1_sap_memory_read(target->tap, size, buffer);
195 address += size;
196 buffer += size;
199 return jtag_execute_queue();
202 static int ls1_sap_write_memory(struct target *target, target_addr_t address,
203 uint32_t size, uint32_t count,
204 const uint8_t *buffer)
206 LOG_DEBUG("Writing memory at physical address 0x%" TARGET_PRIxADDR
207 "; size %" PRId32 "; count %" PRId32, address, size, count);
210 if (count == 0 || buffer == NULL)
211 return ERROR_COMMAND_SYNTAX_ERROR;
213 ls1_sap_set_addr_high(target->tap, 0);
215 while (count--) {
216 ls1_sap_memory_cmd(target->tap, address, size, false);
217 ls1_sap_memory_write(target->tap, size, buffer);
218 address += size;
219 buffer += size;
222 return jtag_execute_queue();
225 struct target_type ls1_sap_target = {
226 .name = "ls1_sap",
228 .target_create = ls1_sap_target_create,
229 .init_target = ls1_sap_init_target,
231 .poll = ls1_sap_poll,
232 .arch_state = ls1_sap_arch_state,
234 .halt = ls1_sap_halt,
235 .resume = ls1_sap_resume,
236 .step = ls1_sap_step,
238 .assert_reset = ls1_sap_assert_reset,
239 .deassert_reset = ls1_sap_deassert_reset,
241 .read_memory = ls1_sap_read_memory,
242 .write_memory = ls1_sap_write_memory,