avr32: work-in-progress
[openocd/cederom.git] / src / target / avr32_ap7k.c
bloba5cdbe47bf5d6b174bb743c69dddf6909d783a30
1 /***************************************************************************
2 * Copyright (C) 2010 by Oleksandr Tymoshenko <gonzo@bluezbox.com> *
3 * Based on mips_m4k code: *
4 * Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
5 * Copyright (C) 2008 by David T.L. Wong *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include "jtag/jtag.h"
27 #include "register.h"
28 #include "algorithm.h"
29 #include "target.h"
30 #include "breakpoints.h"
31 #include "target_type.h"
32 #include "avr32_jtag.h"
33 #include "avr32_mem.h"
34 #include "avr32_regs.h"
35 #include "avr32_ap7k.h"
37 static char* avr32_core_reg_list[] =
39 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8",
40 "r9", "r10", "r11", "r12", "sp", "lr", "pc", "sr"
43 static struct avr32_core_reg
44 avr32_core_reg_list_arch_info[AVR32NUMCOREREGS] =
46 {0, NULL, NULL},
47 {1, NULL, NULL},
48 {2, NULL, NULL},
49 {3, NULL, NULL},
50 {4, NULL, NULL},
51 {5, NULL, NULL},
52 {6, NULL, NULL},
53 {7, NULL, NULL},
54 {8, NULL, NULL},
55 {9, NULL, NULL},
56 {10, NULL, NULL},
57 {11, NULL, NULL},
58 {12, NULL, NULL},
59 {13, NULL, NULL},
60 {14, NULL, NULL},
61 {15, NULL, NULL},
62 {16, NULL, NULL},
66 static int avr32_read_core_reg(struct target *target, int num);
67 static int avr32_write_core_reg(struct target *target, int num);
69 int avr32_ap7k_save_context(struct target *target)
71 int retval, i;
72 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
74 retval = avr32_jtag_read_regs(&ap7k->jtag, ap7k->core_regs);
75 if (retval != ERROR_OK)
76 return retval;
78 for (i = 0; i < AVR32NUMCOREREGS; i++)
80 if (!ap7k->core_cache->reg_list[i].valid)
82 avr32_read_core_reg(target, i);
86 return ERROR_OK;
89 int avr32_ap7k_restore_context(struct target *target)
91 int i;
93 /* get pointers to arch-specific information */
94 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
96 for (i = 0; i < AVR32NUMCOREREGS; i++)
98 if (ap7k->core_cache->reg_list[i].dirty)
100 avr32_write_core_reg(target, i);
104 /* write core regs */
105 avr32_jtag_write_regs(&ap7k->jtag, ap7k->core_regs);
107 return ERROR_OK;
110 static int avr32_read_core_reg(struct target *target, int num)
112 uint32_t reg_value;
113 struct avr32_core_reg *mips_core_reg;
115 /* get pointers to arch-specific information */
116 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
118 if ((num < 0) || (num >= AVR32NUMCOREREGS))
119 return ERROR_INVALID_ARGUMENTS;
121 mips_core_reg = ap7k->core_cache->reg_list[num].arch_info;
122 reg_value = ap7k->core_regs[num];
123 buf_set_u32(ap7k->core_cache->reg_list[num].value, 0, 32, reg_value);
124 ap7k->core_cache->reg_list[num].valid = 1;
125 ap7k->core_cache->reg_list[num].dirty = 0;
127 return ERROR_OK;
130 static int avr32_write_core_reg(struct target *target, int num)
132 uint32_t reg_value;
133 struct avr32_core_reg *mips_core_reg;
135 /* get pointers to arch-specific information */
136 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
138 if ((num < 0) || (num >= AVR32NUMCOREREGS))
139 return ERROR_INVALID_ARGUMENTS;
141 reg_value = buf_get_u32(ap7k->core_cache->reg_list[num].value, 0, 32);
142 mips_core_reg = ap7k->core_cache->reg_list[num].arch_info;
143 ap7k->core_regs[num] = reg_value;
144 LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", num , reg_value);
145 ap7k->core_cache->reg_list[num].valid = 1;
146 ap7k->core_cache->reg_list[num].dirty = 0;
148 return ERROR_OK;
151 static int avr32_get_core_reg(struct reg *reg)
153 int retval;
154 struct avr32_core_reg *avr32_reg = reg->arch_info;
155 struct target *target = avr32_reg->target;
157 if (target->state != TARGET_HALTED)
159 return ERROR_TARGET_NOT_HALTED;
162 retval = avr32_read_core_reg(target, avr32_reg->num);
164 return retval;
167 static int avr32_set_core_reg(struct reg *reg, uint8_t *buf)
169 struct avr32_core_reg *avr32_reg = reg->arch_info;
170 struct target *target = avr32_reg->target;
171 uint32_t value = buf_get_u32(buf, 0, 32);
173 if (target->state != TARGET_HALTED)
175 return ERROR_TARGET_NOT_HALTED;
178 buf_set_u32(reg->value, 0, 32, value);
179 reg->dirty = 1;
180 reg->valid = 1;
182 return ERROR_OK;
185 static const struct reg_arch_type avr32_reg_type = {
186 .get = avr32_get_core_reg,
187 .set = avr32_set_core_reg,
190 static struct reg_cache *avr32_build_reg_cache(struct target *target)
192 int num_regs = AVR32NUMCOREREGS;
193 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
194 struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
195 struct reg_cache *cache = malloc(sizeof(struct reg_cache));
196 struct reg *reg_list = malloc(sizeof(struct reg) * num_regs);
197 struct avr32_core_reg *arch_info =
198 malloc(sizeof(struct avr32_core_reg) * num_regs);
199 int i;
201 /* Build the process context cache */
202 cache->name = "avr32 registers";
203 cache->next = NULL;
204 cache->reg_list = reg_list;
205 cache->num_regs = num_regs;
206 (*cache_p) = cache;
207 ap7k->core_cache = cache;
209 for (i = 0; i < num_regs; i++)
211 arch_info[i] = avr32_core_reg_list_arch_info[i];
212 arch_info[i].target = target;
213 arch_info[i].avr32_common = ap7k;
214 reg_list[i].name = avr32_core_reg_list[i];
215 reg_list[i].size = 32;
216 reg_list[i].value = calloc(1, 4);
217 reg_list[i].dirty = 0;
218 reg_list[i].valid = 0;
219 reg_list[i].type = &avr32_reg_type;
220 reg_list[i].arch_info = &arch_info[i];
223 return cache;
226 static int avr32_ap7k_debug_entry(struct target *target)
229 uint32_t dpc, dinst;
230 int retval;
231 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
233 retval = avr32_jtag_nexus_read(&ap7k->jtag, AVR32_OCDREG_DPC, &dpc);
234 if (retval != ERROR_OK)
235 return retval;
237 retval = avr32_jtag_nexus_read(&ap7k->jtag, AVR32_OCDREG_DINST, &dinst);
238 if (retval != ERROR_OK)
239 return retval;
241 ap7k->jtag.dpc = dpc;
243 avr32_ap7k_save_context(target);
245 return ERROR_OK;
249 static int avr32_ap7k_poll(struct target *target)
251 uint32_t ds;
252 int retval;
253 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
255 retval = avr32_jtag_nexus_read(&ap7k->jtag, AVR32_OCDREG_DS, &ds);
256 if (retval != ERROR_OK)
257 return retval;
259 /* check for processor halted */
260 if (ds & OCDREG_DS_DBA)
262 if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET))
264 target->state = TARGET_HALTED;
266 if ((retval = avr32_ap7k_debug_entry(target)) != ERROR_OK)
267 return retval;
269 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
271 else if (target->state == TARGET_DEBUG_RUNNING)
273 target->state = TARGET_HALTED;
275 if ((retval = avr32_ap7k_debug_entry(target)) != ERROR_OK)
276 return retval;
278 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
281 else
283 target->state = TARGET_RUNNING;
287 return ERROR_OK;
290 static int avr32_ap7k_halt(struct target *target)
292 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
294 LOG_DEBUG("target->state: %s",
295 target_state_name(target));
297 if (target->state == TARGET_HALTED)
299 LOG_DEBUG("target was already halted");
300 return ERROR_OK;
303 if (target->state == TARGET_UNKNOWN)
305 LOG_WARNING("target was in unknown state when halt was requested");
308 if (target->state == TARGET_RESET)
310 if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst())
312 LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
313 return ERROR_TARGET_FAILURE;
315 else
317 target->debug_reason = DBG_REASON_DBGRQ;
319 return ERROR_OK;
324 avr32_ocd_setbits(&ap7k->jtag, AVR32_OCDREG_DC, OCDREG_DC_DBR);
325 target->debug_reason = DBG_REASON_DBGRQ;
327 return ERROR_OK;
330 static int avr32_ap7k_assert_reset(struct target *target)
332 LOG_ERROR("%s: implement me", __func__);
334 return ERROR_OK;
337 static int avr32_ap7k_deassert_reset(struct target *target)
339 LOG_ERROR("%s: implement me", __func__);
341 return ERROR_OK;
344 static int avr32_ap7k_soft_reset_halt(struct target *target)
346 LOG_ERROR("%s: implement me", __func__);
348 return ERROR_OK;
351 static int avr32_ap7k_resume(struct target *target, int current,
352 uint32_t address, int handle_breakpoints, int debug_execution)
354 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
355 struct breakpoint *breakpoint = NULL;
356 uint32_t resume_pc;
357 int retval;
359 if (target->state != TARGET_HALTED)
361 LOG_WARNING("target not halted");
362 return ERROR_TARGET_NOT_HALTED;
365 if (!debug_execution)
367 target_free_all_working_areas(target);
369 avr32_ap7k_enable_breakpoints(target);
370 avr32_ap7k_enable_watchpoints(target);
374 /* current = 1: continue on current pc, otherwise continue at <address> */
375 if (!current)
377 #if 0
378 if (retval != ERROR_OK)
379 return retval;
380 #endif
383 resume_pc =
384 buf_get_u32(ap7k->core_cache->reg_list[AVR32_REG_PC].value, 0, 32);
385 avr32_ap7k_restore_context(target);
387 /* the front-end may request us not to handle breakpoints */
388 if (handle_breakpoints)
390 /* Single step past breakpoint at current address */
391 if ((breakpoint = breakpoint_find(target, resume_pc)))
393 LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
394 #if 0
395 avr32_ap7k_unset_breakpoint(target, breakpoint);
396 avr32_ap7k_single_step_core(target);
397 avr32_ap7k_set_breakpoint(target, breakpoint);
398 #endif
402 #if 0
403 /* enable interrupts if we are running */
404 avr32_ap7k_enable_interrupts(target, !debug_execution);
406 /* exit debug mode */
407 mips_ejtag_exit_debug(ejtag_info);
408 #endif
411 retval = avr32_ocd_clearbits(&ap7k->jtag, AVR32_OCDREG_DC,
412 OCDREG_DC_DBR);
413 if (retval != ERROR_OK)
414 return retval;
416 retval = avr32_jtag_exec(&ap7k->jtag, RETD);
417 if (retval != ERROR_OK)
418 return retval;
420 target->debug_reason = DBG_REASON_NOTHALTED;
422 /* registers are now invalid */
423 register_cache_invalidate(ap7k->core_cache);
425 if (!debug_execution)
427 target->state = TARGET_RUNNING;
428 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
429 LOG_DEBUG("target resumed at 0x%" PRIx32 "", resume_pc);
431 else
433 target->state = TARGET_DEBUG_RUNNING;
434 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
435 LOG_DEBUG("target debug resumed at 0x%" PRIx32 "", resume_pc);
438 return ERROR_OK;
441 static int avr32_ap7k_step(struct target *target, int current,
442 uint32_t address, int handle_breakpoints)
444 LOG_ERROR("%s: implement me", __func__);
446 return ERROR_OK;
449 static int avr32_ap7k_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
451 LOG_ERROR("%s: implement me", __func__);
453 return ERROR_OK;
456 static int avr32_ap7k_remove_breakpoint(struct target *target,
457 struct breakpoint *breakpoint)
459 LOG_ERROR("%s: implement me", __func__);
461 return ERROR_OK;
464 static int avr32_ap7k_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
466 LOG_ERROR("%s: implement me", __func__);
468 return ERROR_OK;
471 static int avr32_ap7k_remove_watchpoint(struct target *target,
472 struct watchpoint *watchpoint)
474 LOG_ERROR("%s: implement me", __func__);
476 return ERROR_OK;
479 static int avr32_ap7k_read_memory(struct target *target, uint32_t address,
480 uint32_t size, uint32_t count, uint8_t *buffer)
482 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
484 LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, size, count);
486 if (target->state != TARGET_HALTED)
488 LOG_WARNING("target not halted");
489 return ERROR_TARGET_NOT_HALTED;
492 /* sanitize arguments */
493 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
494 return ERROR_INVALID_ARGUMENTS;
496 if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
497 return ERROR_TARGET_UNALIGNED_ACCESS;
499 switch (size)
501 case 4:
502 return avr32_jtag_read_memory32(&ap7k->jtag, address, count, (uint32_t*)buffer);
503 break;
504 case 2:
505 return avr32_jtag_read_memory16(&ap7k->jtag, address, count, (uint16_t*)buffer);
506 break;
507 case 1:
508 return avr32_jtag_read_memory8(&ap7k->jtag, address, count, buffer);
509 break;
510 default:
511 break;
514 return ERROR_OK;
517 static int avr32_ap7k_write_memory(struct target *target, uint32_t address,
518 uint32_t size, uint32_t count, uint8_t *buffer)
520 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
522 LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, size, count);
524 if (target->state != TARGET_HALTED)
526 LOG_WARNING("target not halted");
527 return ERROR_TARGET_NOT_HALTED;
530 /* sanitize arguments */
531 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
532 return ERROR_INVALID_ARGUMENTS;
534 if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
535 return ERROR_TARGET_UNALIGNED_ACCESS;
537 switch (size)
539 case 4:
540 return avr32_jtag_write_memory32(&ap7k->jtag, address, count, (uint32_t*)buffer);
541 break;
542 case 2:
543 return avr32_jtag_write_memory16(&ap7k->jtag, address, count, (uint16_t*)buffer);
544 break;
545 case 1:
546 return avr32_jtag_write_memory8(&ap7k->jtag, address, count, buffer);
547 break;
548 default:
549 break;
552 return ERROR_OK;
555 static int avr32_ap7k_init_target(struct command_context *cmd_ctx,
556 struct target *target)
558 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
560 ap7k->jtag.tap = target->tap;
561 avr32_build_reg_cache(target);
562 return ERROR_OK;
565 static int avr32_ap7k_target_create(struct target *target, Jim_Interp *interp)
567 struct avr32_ap7k_common *ap7k = calloc(1, sizeof(struct
568 avr32_ap7k_common));
570 ap7k->common_magic = AP7k_COMMON_MAGIC;
571 target->arch_info = ap7k;
573 return ERROR_OK;
576 static int avr32_ap7k_examine(struct target *target)
578 uint32_t devid, ds;
579 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
581 if (!target_was_examined(target))
583 target_set_examined(target);
584 avr32_jtag_nexus_read(&ap7k->jtag, AVR32_OCDREG_DID, &devid);
585 LOG_INFO("device id: %08x", devid);
586 avr32_ocd_setbits(&ap7k->jtag, AVR32_OCDREG_DC,OCDREG_DC_DBE);
587 avr32_jtag_nexus_read(&ap7k->jtag, AVR32_OCDREG_DS, &ds);
589 /* check for processor halted */
590 if (ds & OCDREG_DS_DBA)
592 LOG_INFO("target is halted");
593 target->state = TARGET_HALTED;
595 else
596 target->state = TARGET_RUNNING;
599 return ERROR_OK;
602 static int avr32_ap7k_bulk_write_memory(struct target *target, uint32_t address,
603 uint32_t count, uint8_t *buffer)
605 LOG_ERROR("%s: implement me", __func__);
607 return ERROR_OK;
611 int avr32_ap7k_arch_state(struct target *target)
613 struct avr32_ap7k_common *ap7k = target_to_ap7k(target);
615 LOG_USER("target halted due to %s, pc: 0x%8.8" PRIx32 "",
616 debug_reason_name(target), ap7k->jtag.dpc);
618 return ERROR_OK;
621 int avr32_ap7k_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size)
623 #if 0
624 /* get pointers to arch-specific information */
625 int i;
627 /* include floating point registers */
628 *reg_list_size = AVR32NUMCOREREGS + AVR32NUMFPREGS;
629 *reg_list = malloc(sizeof(struct reg*) * (*reg_list_size));
631 for (i = 0; i < AVR32NUMCOREREGS; i++)
633 (*reg_list)[i] = &mips32->core_cache->reg_list[i];
636 /* add dummy floating points regs */
637 for (i = AVR32NUMCOREREGS; i < (AVR32NUMCOREREGS + AVR32NUMFPREGS); i++)
639 (*reg_list)[i] = &avr32_ap7k_gdb_dummy_fp_reg;
641 #endif
643 LOG_ERROR("%s: implement me", __func__);
644 return ERROR_FAIL;
649 struct target_type avr32_ap7k_target =
651 .name = "avr32_ap7k",
653 .poll = avr32_ap7k_poll,
654 .arch_state = avr32_ap7k_arch_state,
656 .target_request_data = NULL,
658 .halt = avr32_ap7k_halt,
659 .resume = avr32_ap7k_resume,
660 .step = avr32_ap7k_step,
662 .assert_reset = avr32_ap7k_assert_reset,
663 .deassert_reset = avr32_ap7k_deassert_reset,
664 .soft_reset_halt = avr32_ap7k_soft_reset_halt,
666 .get_gdb_reg_list = avr32_ap7k_get_gdb_reg_list,
668 .read_memory = avr32_ap7k_read_memory,
669 .write_memory = avr32_ap7k_write_memory,
670 .bulk_write_memory = avr32_ap7k_bulk_write_memory,
671 // .checksum_memory = avr32_ap7k_checksum_memory,
672 // .blank_check_memory = avr32_ap7k_blank_check_memory,
674 // .run_algorithm = avr32_ap7k_run_algorithm,
676 .add_breakpoint = avr32_ap7k_add_breakpoint,
677 .remove_breakpoint = avr32_ap7k_remove_breakpoint,
678 .add_watchpoint = avr32_ap7k_add_watchpoint,
679 .remove_watchpoint = avr32_ap7k_remove_watchpoint,
681 .target_create = avr32_ap7k_target_create,
682 .init_target = avr32_ap7k_init_target,
683 .examine = avr32_ap7k_examine,