]> git.sur5r.net Git - openocd/blob - src/target/avr32_regs.c
Fix load_image for ELF with all p_paddr set to zero
[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, 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
22
23 #include "target.h"
24 #include "jtag/jtag.h"
25 #include "avr32_jtag.h"
26 #include "avr32_regs.h"
27
28 static int avr32_jtag_read_reg(struct avr32_jtag *jtag_info, int reg, 
29                 uint32_t *val)
30 {
31         int retval;
32         uint32_t dcsr;
33
34         retval = avr32_jtag_exec(jtag_info, MTDR(AVR32_OCDREG_DCCPU, reg));
35         if (retval != ERROR_OK)
36                 return retval;
37
38         do {
39                 retval = avr32_jtag_nexus_read(jtag_info, 
40                         AVR32_OCDREG_DCSR, &dcsr);
41
42                 if (retval != ERROR_OK)
43                         return retval;
44         } while (!(dcsr & OCDREG_DCSR_CPUD));
45
46         retval = avr32_jtag_nexus_read(jtag_info, 
47                         AVR32_OCDREG_DCCPU, val);
48
49         return retval;
50 }
51
52 static int avr32_jtag_write_reg(struct avr32_jtag *jtag_info, int reg, 
53                 uint32_t val)
54 {
55         int retval;
56         uint32_t dcsr;
57
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;
63
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));
71
72         return retval;
73 }
74
75
76
77 int avr32_jtag_read_regs(struct avr32_jtag *jtag_info, uint32_t *regs)
78 {
79         int i, retval;
80
81         /* read core registers */
82         for (i = 0; i < AVR32NUMCOREREGS - 1; i++) 
83                 avr32_jtag_read_reg(jtag_info, i, regs + i);
84
85         /* read status register */
86         retval = avr32_jtag_exec(jtag_info, MFSR(0, 0));
87         if (retval != ERROR_OK)
88                 return retval;
89
90         retval = avr32_jtag_read_reg(jtag_info, 0, regs + AVR32_REG_SR);
91
92         return retval;
93 }
94
95 int avr32_jtag_write_regs(struct avr32_jtag *jtag_info, uint32_t *regs)
96 {
97         int i, retval;
98
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;
104
105         /*
106          * And now the rest of registers
107          */
108         for (i = 0; i < AVR32NUMCOREREGS - 1; i++) 
109                 avr32_jtag_write_reg(jtag_info, i, regs[i]);
110
111         return ERROR_OK;
112 }