1 /***************************************************************************
2 * Copyright (C) 2010 by Oleksandr Tymoshenko <gonzo@bluezbox.com> *
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. *
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. *
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 ***************************************************************************/
24 #include "jtag/jtag.h"
25 #include "avr32_jtag.h"
26 #include "avr32_mem.h"
28 int avr32_jtag_read_memory32(struct avr32_jtag *jtag_info,
29 uint32_t addr, int count, uint32_t *buffer)
34 for (i = 0; i < count; i++)
36 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
39 if (retval != ERROR_OK)
42 /* XXX: Assume AVR32 is BE */
43 buffer[i] = be_to_h_u32((uint8_t*)&data);
49 int avr32_jtag_read_memory16(struct avr32_jtag *jtag_info,
50 uint32_t addr, int count, uint16_t *buffer)
57 /* any unaligned half-words? */
60 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
63 if (retval != ERROR_OK)
66 /* XXX: Assume AVR32 is BE */
67 data = be_to_h_u32((uint8_t*)&data);
68 buffer[i] = (data >> 16) & 0xffff;
72 /* read all complete words */
73 for (; i < (count & ~1); i+=2)
75 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
78 if (retval != ERROR_OK)
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;
90 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
93 if (retval != ERROR_OK)
96 /* XXX: Assume AVR32 is BE */
97 data = be_to_h_u32((uint8_t*)&data);
98 buffer[i] = data & 0xffff;
104 int avr32_jtag_read_memory8(struct avr32_jtag *jtag_info,
105 uint32_t addr, int count, uint8_t *buffer)
111 /* Do we have non-aligned bytes? */
114 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
115 addr + i, (uint32_t*)(void *)data);
117 if (retval != ERROR_OK)
120 for (j = addr & 3; (j < 4) && (i < count); j++, i++)
121 buffer[i] = data[3-j];
125 /* read all complete words */
126 for (; i < (count & ~3); i+=4)
128 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
129 addr + i, (uint32_t*)(void *)data);
131 if (retval != ERROR_OK)
134 for (j = 0; j < 4; j++)
135 buffer[i+j] = data[3-j];
138 /* remaining bytes */
141 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
142 addr + i, (uint32_t*)(void *)data);
144 if (retval != ERROR_OK)
147 for (j = 0; i + j < count; j++)
148 buffer[i+j] = data[3-j];
154 int avr32_jtag_write_memory32(struct avr32_jtag *jtag_info,
155 uint32_t addr, int count, const uint32_t *buffer)
160 for (i = 0; i < count; i++)
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,
167 if (retval != ERROR_OK)
175 int avr32_jtag_write_memory16(struct avr32_jtag *jtag_info,
176 uint32_t addr, int count, const uint16_t *buffer)
185 * Do we have any non-aligned half-words?
189 * mwa_read will read whole world, no nead to fiddle
190 * with address. It will be truncated in set_addr
192 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
195 if (retval != ERROR_OK)
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);
202 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
205 if (retval != ERROR_OK)
212 /* write all complete words */
213 for (; i < (count & ~1); i+=2)
215 /* XXX: Assume AVR32 is BE */
216 data = (buffer[i+1] << 16) | buffer[i];
217 h_u32_to_be((uint8_t*)&data_out, data);
219 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
220 addr + i*2, data_out);
222 if (retval != ERROR_OK)
229 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
232 if (retval != ERROR_OK)
235 data = be_to_h_u32((uint8_t*)&data);
238 h_u32_to_be((uint8_t*)&data_out, data);
240 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
241 addr + i*2, data_out);
243 if (retval != ERROR_OK)
250 int avr32_jtag_write_memory8(struct avr32_jtag *jtag_info,
251 uint32_t addr, int count, const uint8_t *buffer)
260 * Do we have any non-aligned bytes?
264 * mwa_read will read whole world, no nead to fiddle
265 * with address. It will be truncated in set_addr
267 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
270 if (retval != ERROR_OK)
273 data = be_to_h_u32((uint8_t*)&data);
274 for (j = addr & 3; (j < 4) && (i < count); j++, i++)
276 data &= ~(0xff << j*8);
277 data |= (buffer[i] << j*8);
280 h_u32_to_be((uint8_t*)&data_out, data);
281 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
284 if (retval != ERROR_OK)
289 /* write all complete words */
290 for (; i < (count & ~3); i+=4)
294 for (j = 0; j < 4; j++)
295 data |= (buffer[j+i] << j*8);
297 h_u32_to_be((uint8_t*)&data_out, data);
299 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
302 if (retval != ERROR_OK)
307 * Write trailing bytes
310 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
313 if (retval != ERROR_OK)
316 data = be_to_h_u32((uint8_t*)&data);
317 for (j = 0; i < count; j++, i++)
319 data &= ~(0xff << j*8);
320 data |= (buffer[j+i] << j*8);
323 h_u32_to_be((uint8_t*)&data_out, data);
325 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
328 if (retval != ERROR_OK)