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.
 
 
 
 
 
 

372 lines
8.8 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2010 by Oleksandr Tymoshenko <gonzo@bluezbox.com> *
  3. * *
  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. *
  8. * *
  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. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  16. ***************************************************************************/
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "target.h"
  21. #include "jtag/jtag.h"
  22. #include "avr32_jtag.h"
  23. static int avr32_jtag_set_instr(struct avr32_jtag *jtag_info, int new_instr)
  24. {
  25. struct jtag_tap *tap;
  26. int busy = 0;
  27. tap = jtag_info->tap;
  28. if (!tap)
  29. return ERROR_FAIL;
  30. if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr) {
  31. do {
  32. struct scan_field field;
  33. uint8_t t[4] = { 0 };
  34. uint8_t ret[4];
  35. field.num_bits = tap->ir_length;
  36. field.out_value = t;
  37. buf_set_u32(t, 0, field.num_bits, new_instr);
  38. field.in_value = ret;
  39. jtag_add_ir_scan(tap, &field, TAP_IDLE);
  40. if (jtag_execute_queue() != ERROR_OK) {
  41. LOG_ERROR("%s: setting address failed", __func__);
  42. return ERROR_FAIL;
  43. }
  44. busy = buf_get_u32(ret, 2, 1);
  45. } while (busy); /* check for busy bit */
  46. }
  47. return ERROR_OK;
  48. }
  49. static int avr32_jtag_nexus_set_address(struct avr32_jtag *jtag_info,
  50. uint32_t addr, int mode)
  51. {
  52. struct scan_field fields[2];
  53. uint8_t addr_buf[4];
  54. uint8_t busy_buf[4];
  55. int busy;
  56. memset(fields, 0, sizeof(fields));
  57. do {
  58. memset(addr_buf, 0, sizeof(addr_buf));
  59. memset(busy_buf, 0, sizeof(busy_buf));
  60. buf_set_u32(addr_buf, 0, 1, mode);
  61. buf_set_u32(addr_buf, 1, 7, addr);
  62. fields[0].num_bits = 26;
  63. fields[0].in_value = NULL;
  64. fields[0].out_value = NULL;
  65. fields[1].num_bits = 8;
  66. fields[1].in_value = busy_buf;
  67. fields[1].out_value = addr_buf;
  68. jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
  69. if (jtag_execute_queue() != ERROR_OK) {
  70. LOG_ERROR("%s: setting address failed", __func__);
  71. return ERROR_FAIL;
  72. }
  73. busy = buf_get_u32(busy_buf, 6, 1);
  74. } while (busy);
  75. return ERROR_OK;
  76. }
  77. static int avr32_jtag_nexus_read_data(struct avr32_jtag *jtag_info,
  78. uint32_t *pdata)
  79. {
  80. struct scan_field fields[2];
  81. uint8_t data_buf[4];
  82. uint8_t busy_buf[4];
  83. int busy;
  84. do {
  85. memset(data_buf, 0, sizeof(data_buf));
  86. memset(busy_buf, 0, sizeof(busy_buf));
  87. fields[0].num_bits = 32;
  88. fields[0].out_value = NULL;
  89. fields[0].in_value = data_buf;
  90. fields[1].num_bits = 2;
  91. fields[1].in_value = busy_buf;
  92. fields[1].out_value = NULL;
  93. jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
  94. if (jtag_execute_queue() != ERROR_OK) {
  95. LOG_ERROR("%s: reading data failed", __func__);
  96. return ERROR_FAIL;
  97. }
  98. busy = buf_get_u32(busy_buf, 0, 1);
  99. } while (busy);
  100. *pdata = buf_get_u32(data_buf, 0, 32);
  101. return ERROR_OK;
  102. }
  103. static int avr32_jtag_nexus_write_data(struct avr32_jtag *jtag_info,
  104. uint32_t data)
  105. {
  106. struct scan_field fields[2];
  107. uint8_t data_buf[4];
  108. uint8_t busy_buf[4];
  109. uint8_t dummy_buf[4];
  110. int busy;
  111. do {
  112. memset(data_buf, 0, sizeof(data_buf));
  113. memset(busy_buf, 0, sizeof(busy_buf));
  114. memset(dummy_buf, 0, sizeof(dummy_buf));
  115. fields[0].num_bits = 2;
  116. fields[0].in_value = busy_buf;
  117. fields[0].out_value = dummy_buf;
  118. buf_set_u32(data_buf, 0, 32, data);
  119. fields[1].num_bits = 32;
  120. fields[1].in_value = NULL;
  121. fields[1].out_value = data_buf;
  122. jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
  123. if (jtag_execute_queue() != ERROR_OK) {
  124. LOG_ERROR("%s: reading data failed", __func__);
  125. return ERROR_FAIL;
  126. }
  127. busy = buf_get_u32(busy_buf, 0, 0);
  128. } while (busy);
  129. return ERROR_OK;
  130. }
  131. int avr32_jtag_nexus_read(struct avr32_jtag *jtag_info,
  132. uint32_t addr, uint32_t *value)
  133. {
  134. avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
  135. avr32_jtag_nexus_set_address(jtag_info, addr, MODE_READ);
  136. return avr32_jtag_nexus_read_data(jtag_info, value);
  137. }
  138. int avr32_jtag_nexus_write(struct avr32_jtag *jtag_info,
  139. uint32_t addr, uint32_t value)
  140. {
  141. avr32_jtag_set_instr(jtag_info, AVR32_INST_NEXUS_ACCESS);
  142. avr32_jtag_nexus_set_address(jtag_info, addr, MODE_WRITE);
  143. return avr32_jtag_nexus_write_data(jtag_info, value);
  144. }
  145. static int avr32_jtag_mwa_set_address(struct avr32_jtag *jtag_info, int slave,
  146. uint32_t addr, int mode)
  147. {
  148. struct scan_field fields[2];
  149. uint8_t addr_buf[4];
  150. uint8_t slave_buf[4];
  151. uint8_t busy_buf[4];
  152. int busy;
  153. memset(fields, 0, sizeof(fields));
  154. do {
  155. memset(addr_buf, 0, sizeof(addr_buf));
  156. memset(busy_buf, 0, sizeof(busy_buf));
  157. memset(slave_buf, 0, sizeof(slave_buf));
  158. buf_set_u32(slave_buf, 0, 4, slave);
  159. buf_set_u32(addr_buf, 0, 1, mode);
  160. buf_set_u32(addr_buf, 1, 30, addr >> 2);
  161. fields[0].num_bits = 31;
  162. fields[0].in_value = NULL;
  163. fields[0].out_value = addr_buf;
  164. fields[1].num_bits = 4;
  165. fields[1].in_value = busy_buf;
  166. fields[1].out_value = slave_buf;
  167. jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
  168. if (jtag_execute_queue() != ERROR_OK) {
  169. LOG_ERROR("%s: setting address failed", __func__);
  170. return ERROR_FAIL;
  171. }
  172. busy = buf_get_u32(busy_buf, 1, 1);
  173. } while (busy);
  174. return ERROR_OK;
  175. }
  176. static int avr32_jtag_mwa_read_data(struct avr32_jtag *jtag_info,
  177. uint32_t *pdata)
  178. {
  179. struct scan_field fields[2];
  180. uint8_t data_buf[4];
  181. uint8_t busy_buf[4];
  182. int busy;
  183. do {
  184. memset(data_buf, 0, sizeof(data_buf));
  185. memset(busy_buf, 0, sizeof(busy_buf));
  186. fields[0].num_bits = 32;
  187. fields[0].out_value = NULL;
  188. fields[0].in_value = data_buf;
  189. fields[1].num_bits = 3;
  190. fields[1].in_value = busy_buf;
  191. fields[1].out_value = NULL;
  192. jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
  193. if (jtag_execute_queue() != ERROR_OK) {
  194. LOG_ERROR("%s: reading data failed", __func__);
  195. return ERROR_FAIL;
  196. }
  197. busy = buf_get_u32(busy_buf, 0, 1);
  198. } while (busy);
  199. *pdata = buf_get_u32(data_buf, 0, 32);
  200. return ERROR_OK;
  201. }
  202. static int avr32_jtag_mwa_write_data(struct avr32_jtag *jtag_info,
  203. uint32_t data)
  204. {
  205. struct scan_field fields[2];
  206. uint8_t data_buf[4];
  207. uint8_t busy_buf[4];
  208. uint8_t zero_buf[4];
  209. int busy;
  210. do {
  211. memset(data_buf, 0, sizeof(data_buf));
  212. memset(busy_buf, 0, sizeof(busy_buf));
  213. memset(zero_buf, 0, sizeof(zero_buf));
  214. buf_set_u32(data_buf, 0, 32, data);
  215. fields[0].num_bits = 3;
  216. fields[0].in_value = busy_buf;
  217. fields[0].out_value = zero_buf;
  218. fields[1].num_bits = 32;
  219. fields[1].out_value = data_buf;
  220. fields[1].in_value = NULL;
  221. jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
  222. if (jtag_execute_queue() != ERROR_OK) {
  223. LOG_ERROR("%s: reading data failed", __func__);
  224. return ERROR_FAIL;
  225. }
  226. busy = buf_get_u32(busy_buf, 0, 1);
  227. } while (busy);
  228. return ERROR_OK;
  229. }
  230. int avr32_jtag_mwa_read(struct avr32_jtag *jtag_info, int slave,
  231. uint32_t addr, uint32_t *value)
  232. {
  233. avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
  234. avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_READ);
  235. avr32_jtag_mwa_read_data(jtag_info, value);
  236. return ERROR_OK;
  237. }
  238. int avr32_jtag_mwa_write(struct avr32_jtag *jtag_info, int slave,
  239. uint32_t addr, uint32_t value)
  240. {
  241. avr32_jtag_set_instr(jtag_info, AVR32_INST_MW_ACCESS);
  242. avr32_jtag_mwa_set_address(jtag_info, slave, addr, MODE_WRITE);
  243. avr32_jtag_mwa_write_data(jtag_info, value);
  244. return ERROR_OK;
  245. }
  246. int avr32_jtag_exec(struct avr32_jtag *jtag_info, uint32_t inst)
  247. {
  248. int retval;
  249. uint32_t ds;
  250. retval = avr32_jtag_nexus_write(jtag_info, AVR32_OCDREG_DINST, inst);
  251. if (retval != ERROR_OK)
  252. return retval;
  253. do {
  254. retval = avr32_jtag_nexus_read(jtag_info, AVR32_OCDREG_DS, &ds);
  255. if (retval != ERROR_OK)
  256. return retval;
  257. } while ((ds & OCDREG_DS_DBA) && !(ds & OCDREG_DS_INC));
  258. return ERROR_OK;
  259. }
  260. int avr32_ocd_setbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
  261. {
  262. uint32_t value;
  263. int res;
  264. res = avr32_jtag_nexus_read(jtag, reg, &value);
  265. if (res)
  266. return res;
  267. value |= bits;
  268. res = avr32_jtag_nexus_write(jtag, reg, value);
  269. if (res)
  270. return res;
  271. return ERROR_OK;
  272. }
  273. int avr32_ocd_clearbits(struct avr32_jtag *jtag, int reg, uint32_t bits)
  274. {
  275. uint32_t value;
  276. int res;
  277. res = avr32_jtag_nexus_read(jtag, reg, &value);
  278. if (res)
  279. return res;
  280. value &= ~bits;
  281. res = avr32_jtag_nexus_write(jtag, reg, value);
  282. if (res)
  283. return res;
  284. return ERROR_OK;
  285. }