1 /***************************************************************************
2 * Copyright (C) 2015 by Esben Haabendal *
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. *
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 ***************************************************************************/
21 #include "target_type.h"
23 #include <jtag/jtag.h>
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;
39 static int ls1_sap_init_target(struct command_context *cmd_ctx, struct target *target)
41 LOG_DEBUG("%s", __func__);
45 static int ls1_sap_arch_state(struct target *target)
47 LOG_DEBUG("%s", __func__);
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;
61 static int ls1_sap_halt(struct target *target)
63 LOG_DEBUG("%s", __func__);
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__);
74 static int ls1_sap_step(struct target *target, int current, target_addr_t address,
75 int handle_breakpoints)
77 LOG_DEBUG("%s", __func__);
81 static int ls1_sap_assert_reset(struct target *target)
83 target->state = TARGET_RESET;
85 LOG_DEBUG("%s", __func__);
89 static int ls1_sap_deassert_reset(struct target *target)
91 target->state = TARGET_RUNNING;
93 LOG_DEBUG("%s", __func__);
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)
104 field.num_bits = tap->ir_length;
105 uint8_t *t = calloc(DIV_ROUND_UP(field.num_bits, 8), 1);
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);
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] = { 0 };
118 ls1_sap_set_instr(tap, 0x21);
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] = { 0 };
135 ls1_sap_set_instr(tap, 0x24);
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,
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 %" PRIu32 "; count %" PRIu32, address, size, count);
187 if (count == 0 || buffer == NULL)
188 return ERROR_COMMAND_SYNTAX_ERROR;
190 ls1_sap_set_addr_high(target->tap, 0);
193 ls1_sap_memory_cmd(target->tap, address, size, true);
194 ls1_sap_memory_read(target->tap, size, buffer);
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 %" PRIu32 "; count %" PRIu32, address, size, count);
210 if (count == 0 || buffer == NULL)
211 return ERROR_COMMAND_SYNTAX_ERROR;
213 ls1_sap_set_addr_high(target->tap, 0);
216 ls1_sap_memory_cmd(target->tap, address, size, false);
217 ls1_sap_memory_write(target->tap, size, buffer);
222 return jtag_execute_queue();
225 struct target_type ls1_sap_target = {
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,