]> git.sur5r.net Git - openocd/blob - src/target/avr32_regs.c
tcl/target/stm32f0x: Allow overriding the Flash bank size
[openocd] / src / target / avr32_regs.c
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
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include "target.h"
23 #include "jtag/jtag.h"
24 #include "avr32_jtag.h"
25 #include "avr32_regs.h"
26
27 static int avr32_jtag_read_reg(struct avr32_jtag *jtag_info, int reg,
28                 uint32_t *val)
29 {
30         int retval;
31         uint32_t dcsr;
32
33         retval = avr32_jtag_exec(jtag_info, MTDR(AVR32_OCDREG_DCCPU, reg));
34         if (retval != ERROR_OK)
35                 return retval;
36
37         do {
38                 retval = avr32_jtag_nexus_read(jtag_info,
39                         AVR32_OCDREG_DCSR, &dcsr);
40
41                 if (retval != ERROR_OK)
42                         return retval;
43         } while (!(dcsr & OCDREG_DCSR_CPUD));
44
45         retval = avr32_jtag_nexus_read(jtag_info,
46                         AVR32_OCDREG_DCCPU, val);
47
48         return retval;
49 }
50
51 static int avr32_jtag_write_reg(struct avr32_jtag *jtag_info, int reg,
52                 uint32_t val)
53 {
54         int retval;
55         uint32_t dcsr;
56
57         /* Restore Status reg */
58         retval = avr32_jtag_nexus_write(jtag_info,
59                                 AVR32_OCDREG_DCEMU, val);
60         if (retval != ERROR_OK)
61                 return retval;
62
63         retval = avr32_jtag_exec(jtag_info, MFDR(reg, AVR32_OCDREG_DCEMU));
64         if (retval != ERROR_OK)
65                 return retval;
66         do {
67                 retval = avr32_jtag_nexus_read(jtag_info,
68                         AVR32_OCDREG_DCSR, &dcsr);
69         } while (!(dcsr & OCDREG_DCSR_EMUD) && (retval == ERROR_OK));
70
71         return retval;
72 }
73
74
75
76 int avr32_jtag_read_regs(struct avr32_jtag *jtag_info, uint32_t *regs)
77 {
78         int i, retval;
79
80         /* read core registers */
81         for (i = 0; i < AVR32NUMCOREREGS - 1; i++)
82                 avr32_jtag_read_reg(jtag_info, i, regs + i);
83
84         /* read status register */
85         retval = avr32_jtag_exec(jtag_info, MFSR(0, 0));
86         if (retval != ERROR_OK)
87                 return retval;
88
89         retval = avr32_jtag_read_reg(jtag_info, 0, regs + AVR32_REG_SR);
90
91         return retval;
92 }
93
94 int avr32_jtag_write_regs(struct avr32_jtag *jtag_info, uint32_t *regs)
95 {
96         int i, retval;
97
98         retval = avr32_jtag_write_reg(jtag_info, 0, regs[AVR32_REG_SR]);
99         if (retval != ERROR_OK)
100                 return retval;
101
102         /* Restore Status reg */
103         retval = avr32_jtag_exec(jtag_info, MTSR(0, 0));
104         if (retval != ERROR_OK)
105                 return retval;
106
107         /*
108          * And now the rest of registers
109          */
110         for (i = 0; i < AVR32NUMCOREREGS - 1; i++)
111                 avr32_jtag_write_reg(jtag_info, i, regs[i]);
112
113         return ERROR_OK;
114 }