]> git.sur5r.net Git - openocd/blob - src/target/avr32_mem.c
types: write memory now uses const
[openocd] / src / target / avr32_mem.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_mem.h"
27
28 int avr32_jtag_read_memory32(struct avr32_jtag *jtag_info, 
29                 uint32_t addr, int count, uint32_t *buffer)
30 {
31         int i, retval;
32         uint32_t data;
33
34         for (i = 0; i < count; i++) 
35         {
36                 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
37                                 addr + i*4, &data);
38
39                 if (retval != ERROR_OK)
40                         return retval;
41
42                 /* XXX: Assume AVR32 is BE */
43                 buffer[i] = be_to_h_u32((uint8_t*)&data);
44         }
45
46         return ERROR_OK;
47 }
48
49 int avr32_jtag_read_memory16(struct avr32_jtag *jtag_info, 
50                 uint32_t addr, int count, uint16_t *buffer)
51 {
52         int i, retval;
53         uint32_t data;
54
55         i = 0;
56
57         /* any unaligned half-words? */
58         if (addr & 3)
59         {
60                 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
61                                 addr + i*2, &data);
62
63                 if (retval != ERROR_OK)
64                         return retval;
65
66                 /* XXX: Assume AVR32 is BE */
67                 data = be_to_h_u32((uint8_t*)&data);
68                 buffer[i] = (data >> 16) & 0xffff;
69                 i++;
70         }
71
72         /* read all complete words */
73         for (; i < (count & ~1); i+=2)
74         {
75                 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
76                                 addr + i*2, &data);
77
78                 if (retval != ERROR_OK)
79                         return retval;
80
81                 /* XXX: Assume AVR32 is BE */
82                 data = be_to_h_u32((uint8_t*)&data);
83                 buffer[i] = data & 0xffff;
84                 buffer[i+1] = (data >> 16) & 0xffff;
85         }
86
87         /* last halfword */
88         if (i < count)
89         {
90                 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
91                                 addr + i*2, &data);
92
93                 if (retval != ERROR_OK)
94                         return retval;
95
96                 /* XXX: Assume AVR32 is BE */
97                 data = be_to_h_u32((uint8_t*)&data);
98                 buffer[i] = data & 0xffff;
99         }
100
101         return ERROR_OK;
102 }
103
104 int avr32_jtag_read_memory8(struct avr32_jtag *jtag_info, 
105                 uint32_t addr, int count, uint8_t *buffer)
106 {
107         int i, j, retval;
108         uint8_t data[4];
109         i = 0;
110
111         /* Do we have non-aligned bytes? */
112         if (addr & 3)
113         {
114                 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
115                                 addr + i, (uint32_t*)(void *)data);
116
117                 if (retval != ERROR_OK)
118                         return retval;
119
120                 for (j = addr & 3; (j < 4) && (i < count); j++, i++) 
121                                 buffer[i] = data[3-j];
122         }
123
124
125         /* read all complete words */
126         for (; i < (count & ~3); i+=4)
127         {
128                 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
129                                 addr + i, (uint32_t*)(void *)data);
130
131                 if (retval != ERROR_OK)
132                         return retval;
133
134                 for (j = 0; j < 4; j++)
135                                 buffer[i+j] = data[3-j];
136         }
137
138         /* remaining bytes */
139         if (i < count)
140         {
141                 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
142                                 addr + i, (uint32_t*)(void *)data);
143
144                 if (retval != ERROR_OK)
145                         return retval;
146
147                 for (j = 0; i + j < count; j++) 
148                                 buffer[i+j] = data[3-j];
149         }
150
151         return ERROR_OK;
152 }
153
154 int avr32_jtag_write_memory32(struct avr32_jtag *jtag_info, 
155                 uint32_t addr, int count, const uint32_t *buffer)
156 {
157         int i, retval;
158         uint32_t data;
159
160         for (i = 0; i < count; i++) 
161         {
162                 /* XXX: Assume AVR32 is BE */
163                 h_u32_to_be((uint8_t*)&data, buffer[i]);
164                 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
165                                 addr + i*4, data);
166
167                 if (retval != ERROR_OK)
168                         return retval;
169
170         }
171
172         return ERROR_OK;
173 }
174
175 int avr32_jtag_write_memory16(struct avr32_jtag *jtag_info, 
176                 uint32_t addr, int count, const uint16_t *buffer)
177 {
178         int i, retval;
179         uint32_t data;
180         uint32_t data_out;
181
182         i = 0;
183
184         /*
185          * Do we have any non-aligned half-words?
186          */
187         if (addr & 3) {
188                 /* 
189                  * mwa_read will read whole world, no nead to fiddle
190                  * with address. It will be truncated in set_addr
191                  */
192                 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
193                                 addr, &data);
194
195                 if (retval != ERROR_OK)
196                         return retval;
197
198                 data = be_to_h_u32((uint8_t*)&data);
199                 data = (buffer[i] << 16) | (data & 0xffff);
200                 h_u32_to_be((uint8_t*)&data_out, data);
201
202                 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
203                                 addr, data_out);
204
205                 if (retval != ERROR_OK)
206                         return retval;
207
208                 i++;
209         }
210
211
212         /* write all complete words */
213         for (; i < (count & ~1); i+=2)
214         {
215                 /* XXX: Assume AVR32 is BE */
216                 data = (buffer[i+1] << 16) | buffer[i];
217                 h_u32_to_be((uint8_t*)&data_out, data);
218
219                 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
220                                 addr + i*2, data_out);
221
222                 if (retval != ERROR_OK)
223                         return retval;
224         }
225
226         /* last halfword */
227         if (i < count)
228         {
229                 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
230                                 addr + i*2, &data);
231
232                 if (retval != ERROR_OK)
233                         return retval;
234
235                 data = be_to_h_u32((uint8_t*)&data);
236                 data &= ~0xffff;
237                 data |= buffer[i];
238                 h_u32_to_be((uint8_t*)&data_out, data);
239
240                 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
241                                 addr + i*2, data_out);
242
243                 if (retval != ERROR_OK)
244                         return retval;
245         }
246
247         return ERROR_OK;
248 }
249
250 int avr32_jtag_write_memory8(struct avr32_jtag *jtag_info, 
251                 uint32_t addr, int count, const uint8_t *buffer)
252 {
253         int i, j, retval;
254         uint32_t data;
255         uint32_t data_out;
256
257         i = 0;
258
259         /*
260          * Do we have any non-aligned bytes?
261          */
262         if (addr & 3) {
263                 /* 
264                  * mwa_read will read whole world, no nead to fiddle
265                  * with address. It will be truncated in set_addr
266                  */
267                 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
268                                 addr, &data);
269
270                 if (retval != ERROR_OK)
271                         return retval;
272
273                 data = be_to_h_u32((uint8_t*)&data);
274                 for (j = addr & 3; (j < 4) && (i < count); j++, i++) 
275                 {
276                         data &= ~(0xff << j*8);
277                         data |= (buffer[i] << j*8);
278                 }
279
280                 h_u32_to_be((uint8_t*)&data_out, data);
281                 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
282                                 addr, data_out);
283
284                 if (retval != ERROR_OK)
285                         return retval;
286         }
287
288
289         /* write all complete words */
290         for (; i < (count & ~3); i+=4)
291         {
292                 data = 0;
293
294                 for (j = 0; j < 4; j++)
295                         data |= (buffer[j+i] << j*8);
296
297                 h_u32_to_be((uint8_t*)&data_out, data);
298
299                 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
300                                 addr + i, data_out);
301
302                 if (retval != ERROR_OK)
303                         return retval;
304         }
305
306         /*
307          * Write trailing bytes
308          */
309         if (i < count) {
310                 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
311                                 addr + i, &data);
312
313                 if (retval != ERROR_OK)
314                         return retval;
315
316                 data = be_to_h_u32((uint8_t*)&data);
317                 for (j = 0; i < count; j++, i++)
318                 {
319                         data &= ~(0xff << j*8);
320                         data |= (buffer[j+i] << j*8);
321                 }
322
323                 h_u32_to_be((uint8_t*)&data_out, data);
324
325                 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
326                                 addr+i, data_out);
327
328                 if (retval != ERROR_OK)
329                         return retval;
330         }
331
332         return ERROR_OK;
333 }