avr32: work-in-progress
[openocd/genbsdl.git] / src / target / avr32_regs.c
blobeb283fc30882ea3d39d4a6194e2c61a099abdcae
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, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
23 #include "target.h"
24 #include "jtag/jtag.h"
25 #include "avr32_jtag.h"
26 #include "avr32_regs.h"
28 static int avr32_jtag_read_reg(struct avr32_jtag *jtag_info, int reg,
29 uint32_t *val)
31 int retval;
32 uint32_t dcsr;
34 retval = avr32_jtag_exec(jtag_info, MTDR(AVR32_OCDREG_DCCPU, reg));
35 if (retval != ERROR_OK)
36 return retval;
38 do {
39 retval = avr32_jtag_nexus_read(jtag_info,
40 AVR32_OCDREG_DCSR, &dcsr);
42 if (retval != ERROR_OK)
43 return retval;
44 } while (!(dcsr & OCDREG_DCSR_CPUD));
46 retval = avr32_jtag_nexus_read(jtag_info,
47 AVR32_OCDREG_DCCPU, val);
49 return retval;
52 static int avr32_jtag_write_reg(struct avr32_jtag *jtag_info, int reg,
53 uint32_t val)
55 int retval;
56 uint32_t dcsr;
58 /* Restore Status reg */
59 retval = avr32_jtag_nexus_write(jtag_info,
60 AVR32_OCDREG_DCEMU, val);
61 if (retval != ERROR_OK)
62 return retval;
64 retval = avr32_jtag_exec(jtag_info, MFDR(reg, AVR32_OCDREG_DCEMU));
65 if (retval != ERROR_OK)
66 return retval;
67 do {
68 retval = avr32_jtag_nexus_read(jtag_info,
69 AVR32_OCDREG_DCSR, &dcsr);
70 } while (!(dcsr & OCDREG_DCSR_EMUD) && (retval == ERROR_OK));
72 return retval;
77 int avr32_jtag_read_regs(struct avr32_jtag *jtag_info, uint32_t *regs)
79 int i, retval;
81 /* read core registers */
82 for (i = 0; i < AVR32NUMCOREREGS - 1; i++)
83 avr32_jtag_read_reg(jtag_info, i, regs + i);
85 /* read status register */
86 retval = avr32_jtag_exec(jtag_info, MFSR(0, 0));
87 if (retval != ERROR_OK)
88 return retval;
90 retval = avr32_jtag_read_reg(jtag_info, 0, regs + AVR32_REG_SR);
92 return retval;
95 int avr32_jtag_write_regs(struct avr32_jtag *jtag_info, uint32_t *regs)
97 int i, retval;
99 retval = avr32_jtag_write_reg(jtag_info, 0, regs[AVR32_REG_SR]);
100 /* Restore Status reg */
101 retval = avr32_jtag_exec(jtag_info, MTSR(0, 0));
102 if (retval != ERROR_OK)
103 return retval;
106 * And now the rest of registers
108 for (i = 0; i < AVR32NUMCOREREGS - 1; i++)
109 avr32_jtag_write_reg(jtag_info, i, regs[i]);
111 return ERROR_OK;