You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

244 lines
6.1 KiB

  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. ***************************************************************************/
  15. #ifdef HAVE_CONFIG_H
  16. #include "config.h"
  17. #endif
  18. #include "target.h"
  19. #include "target_type.h"
  20. #include <jtag/jtag.h>
  21. struct ls1_sap {
  22. struct jtag_tap *tap;
  23. };
  24. static int ls1_sap_target_create(struct target *target, Jim_Interp *interp)
  25. {
  26. struct ls1_sap *ls1_sap = calloc(1, sizeof(struct ls1_sap));
  27. ls1_sap->tap = target->tap;
  28. target->arch_info = ls1_sap;
  29. return ERROR_OK;
  30. }
  31. static int ls1_sap_init_target(struct command_context *cmd_ctx, struct target *target)
  32. {
  33. LOG_DEBUG("%s", __func__);
  34. return ERROR_OK;
  35. }
  36. static int ls1_sap_arch_state(struct target *target)
  37. {
  38. LOG_DEBUG("%s", __func__);
  39. return ERROR_OK;
  40. }
  41. static int ls1_sap_poll(struct target *target)
  42. {
  43. if ((target->state == TARGET_UNKNOWN) ||
  44. (target->state == TARGET_RUNNING) ||
  45. (target->state == TARGET_DEBUG_RUNNING))
  46. target->state = TARGET_HALTED;
  47. return ERROR_OK;
  48. }
  49. static int ls1_sap_halt(struct target *target)
  50. {
  51. LOG_DEBUG("%s", __func__);
  52. return ERROR_OK;
  53. }
  54. static int ls1_sap_resume(struct target *target, int current, target_addr_t address,
  55. int handle_breakpoints, int debug_execution)
  56. {
  57. LOG_DEBUG("%s", __func__);
  58. return ERROR_OK;
  59. }
  60. static int ls1_sap_step(struct target *target, int current, target_addr_t address,
  61. int handle_breakpoints)
  62. {
  63. LOG_DEBUG("%s", __func__);
  64. return ERROR_OK;
  65. }
  66. static int ls1_sap_assert_reset(struct target *target)
  67. {
  68. target->state = TARGET_RESET;
  69. LOG_DEBUG("%s", __func__);
  70. return ERROR_OK;
  71. }
  72. static int ls1_sap_deassert_reset(struct target *target)
  73. {
  74. target->state = TARGET_RUNNING;
  75. LOG_DEBUG("%s", __func__);
  76. return ERROR_OK;
  77. }
  78. static void ls1_sap_set_instr(struct jtag_tap *tap, uint32_t new_instr)
  79. {
  80. struct scan_field field;
  81. if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) == new_instr)
  82. return;
  83. field.num_bits = tap->ir_length;
  84. uint8_t *t = calloc(DIV_ROUND_UP(field.num_bits, 8), 1);
  85. field.out_value = t;
  86. buf_set_u32(t, 0, field.num_bits, new_instr);
  87. field.in_value = NULL;
  88. jtag_add_ir_scan(tap, &field, TAP_IDLE);
  89. free(t);
  90. }
  91. static void ls1_sap_set_addr_high(struct jtag_tap *tap, uint16_t addr_high)
  92. {
  93. struct scan_field field;
  94. uint8_t buf[2] = { 0 };
  95. ls1_sap_set_instr(tap, 0x21);
  96. field.num_bits = 16;
  97. field.out_value = buf;
  98. buf_set_u32(buf, 0, 16, addr_high);
  99. field.in_value = NULL;
  100. field.check_value = NULL;
  101. field.check_mask = NULL;
  102. jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
  103. }
  104. static void ls1_sap_memory_cmd(struct jtag_tap *tap, uint32_t address,
  105. int32_t size, bool rnw)
  106. {
  107. struct scan_field field;
  108. uint8_t cmd[8] = { 0 };
  109. ls1_sap_set_instr(tap, 0x24);
  110. field.num_bits = 64;
  111. field.out_value = cmd;
  112. buf_set_u64(cmd, 0, 9, 0);
  113. buf_set_u64(cmd, 9, 3, size);
  114. buf_set_u64(cmd, 12, 1, rnw);
  115. buf_set_u64(cmd, 13, 3, 0);
  116. buf_set_u64(cmd, 16, 32, address);
  117. buf_set_u64(cmd, 48, 16, 0);
  118. field.in_value = NULL;
  119. field.check_value = NULL;
  120. field.check_mask = NULL;
  121. jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
  122. }
  123. static void ls1_sap_memory_read(struct jtag_tap *tap, uint32_t size,
  124. uint8_t *value)
  125. {
  126. struct scan_field field;
  127. ls1_sap_set_instr(tap, 0x25);
  128. field.num_bits = 8 * size;
  129. field.out_value = NULL;
  130. field.in_value = value;
  131. field.check_value = NULL;
  132. field.check_mask = NULL;
  133. jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
  134. }
  135. static void ls1_sap_memory_write(struct jtag_tap *tap, uint32_t size,
  136. const uint8_t *value)
  137. {
  138. struct scan_field field;
  139. ls1_sap_set_instr(tap, 0x25);
  140. field.num_bits = 8 * size;
  141. field.out_value = value;
  142. field.in_value = NULL;
  143. field.check_value = NULL;
  144. field.check_mask = NULL;
  145. jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
  146. }
  147. static int ls1_sap_read_memory(struct target *target, target_addr_t address,
  148. uint32_t size, uint32_t count, uint8_t *buffer)
  149. {
  150. LOG_DEBUG("Reading memory at physical address 0x%" TARGET_PRIxADDR
  151. "; size %" PRIu32 "; count %" PRIu32, address, size, count);
  152. if (count == 0 || !buffer)
  153. return ERROR_COMMAND_SYNTAX_ERROR;
  154. ls1_sap_set_addr_high(target->tap, 0);
  155. while (count--) {
  156. ls1_sap_memory_cmd(target->tap, address, size, true);
  157. ls1_sap_memory_read(target->tap, size, buffer);
  158. address += size;
  159. buffer += size;
  160. }
  161. return jtag_execute_queue();
  162. }
  163. static int ls1_sap_write_memory(struct target *target, target_addr_t address,
  164. uint32_t size, uint32_t count,
  165. const uint8_t *buffer)
  166. {
  167. LOG_DEBUG("Writing memory at physical address 0x%" TARGET_PRIxADDR
  168. "; size %" PRIu32 "; count %" PRIu32, address, size, count);
  169. if (count == 0 || !buffer)
  170. return ERROR_COMMAND_SYNTAX_ERROR;
  171. ls1_sap_set_addr_high(target->tap, 0);
  172. while (count--) {
  173. ls1_sap_memory_cmd(target->tap, address, size, false);
  174. ls1_sap_memory_write(target->tap, size, buffer);
  175. address += size;
  176. buffer += size;
  177. }
  178. return jtag_execute_queue();
  179. }
  180. struct target_type ls1_sap_target = {
  181. .name = "ls1_sap",
  182. .target_create = ls1_sap_target_create,
  183. .init_target = ls1_sap_init_target,
  184. .poll = ls1_sap_poll,
  185. .arch_state = ls1_sap_arch_state,
  186. .halt = ls1_sap_halt,
  187. .resume = ls1_sap_resume,
  188. .step = ls1_sap_step,
  189. .assert_reset = ls1_sap_assert_reset,
  190. .deassert_reset = ls1_sap_deassert_reset,
  191. .read_memory = ls1_sap_read_memory,
  192. .write_memory = ls1_sap_write_memory,
  193. };