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, see <http://www.gnu.org/licenses/>. *
16 ***************************************************************************/
23 #include "jtag/jtag.h"
24 #include "avr32_jtag.h"
25 #include "avr32_mem.h"
27 int avr32_jtag_read_memory32(struct avr32_jtag *jtag_info,
28 uint32_t addr, int count, uint32_t *buffer)
33 for (i = 0; i < count; i++) {
34 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
37 if (retval != ERROR_OK)
40 /* XXX: Assume AVR32 is BE */
41 buffer[i] = be_to_h_u32((uint8_t *)&data);
47 int avr32_jtag_read_memory16(struct avr32_jtag *jtag_info,
48 uint32_t addr, int count, uint16_t *buffer)
55 /* any unaligned half-words? */
57 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
60 if (retval != ERROR_OK)
63 /* XXX: Assume AVR32 is BE */
64 data = be_to_h_u32((uint8_t *)&data);
65 buffer[i] = (data >> 16) & 0xffff;
69 /* read all complete words */
70 for (; i < (count & ~1); i += 2) {
71 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
74 if (retval != ERROR_OK)
77 /* XXX: Assume AVR32 is BE */
78 data = be_to_h_u32((uint8_t *)&data);
79 buffer[i] = data & 0xffff;
80 buffer[i+1] = (data >> 16) & 0xffff;
85 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
88 if (retval != ERROR_OK)
91 /* XXX: Assume AVR32 is BE */
92 data = be_to_h_u32((uint8_t *)&data);
93 buffer[i] = data & 0xffff;
99 int avr32_jtag_read_memory8(struct avr32_jtag *jtag_info,
100 uint32_t addr, int count, uint8_t *buffer)
106 /* Do we have non-aligned bytes? */
108 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
109 addr + i, (uint32_t *)(void *)data);
111 if (retval != ERROR_OK)
114 for (j = addr & 3; (j < 4) && (i < count); j++, i++)
115 buffer[i] = data[3-j];
118 /* read all complete words */
119 for (; i < (count & ~3); i += 4) {
120 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
121 addr + i, (uint32_t *)(void *)data);
123 if (retval != ERROR_OK)
126 for (j = 0; j < 4; j++)
127 buffer[i+j] = data[3-j];
130 /* remaining bytes */
132 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
133 addr + i, (uint32_t *)(void *)data);
135 if (retval != ERROR_OK)
138 for (j = 0; i + j < count; j++)
139 buffer[i+j] = data[3-j];
145 int avr32_jtag_write_memory32(struct avr32_jtag *jtag_info,
146 uint32_t addr, int count, const uint32_t *buffer)
151 for (i = 0; i < count; i++) {
152 /* XXX: Assume AVR32 is BE */
153 h_u32_to_be((uint8_t *)&data, buffer[i]);
154 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
157 if (retval != ERROR_OK)
165 int avr32_jtag_write_memory16(struct avr32_jtag *jtag_info,
166 uint32_t addr, int count, const uint16_t *buffer)
175 * Do we have any non-aligned half-words?
179 * mwa_read will read whole world, no nead to fiddle
180 * with address. It will be truncated in set_addr
182 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
185 if (retval != ERROR_OK)
188 data = be_to_h_u32((uint8_t *)&data);
189 data = (buffer[i] << 16) | (data & 0xffff);
190 h_u32_to_be((uint8_t *)&data_out, data);
192 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
195 if (retval != ERROR_OK)
201 /* write all complete words */
202 for (; i < (count & ~1); i += 2) {
203 /* XXX: Assume AVR32 is BE */
204 data = (buffer[i+1] << 16) | buffer[i];
205 h_u32_to_be((uint8_t *)&data_out, data);
207 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
208 addr + i*2, data_out);
210 if (retval != ERROR_OK)
216 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
219 if (retval != ERROR_OK)
222 data = be_to_h_u32((uint8_t *)&data);
225 h_u32_to_be((uint8_t *)&data_out, data);
227 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
228 addr + i*2, data_out);
230 if (retval != ERROR_OK)
237 int avr32_jtag_write_memory8(struct avr32_jtag *jtag_info,
238 uint32_t addr, int count, const uint8_t *buffer)
247 * Do we have any non-aligned bytes?
251 * mwa_read will read whole world, no nead to fiddle
252 * with address. It will be truncated in set_addr
254 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
257 if (retval != ERROR_OK)
260 data = be_to_h_u32((uint8_t *)&data);
261 for (j = addr & 3; (j < 4) && (i < count); j++, i++) {
262 data &= ~(0xff << j*8);
263 data |= (buffer[i] << j*8);
266 h_u32_to_be((uint8_t *)&data_out, data);
267 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
270 if (retval != ERROR_OK)
275 /* write all complete words */
276 for (; i < (count & ~3); i += 4) {
279 for (j = 0; j < 4; j++)
280 data |= (buffer[j+i] << j*8);
282 h_u32_to_be((uint8_t *)&data_out, data);
284 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
287 if (retval != ERROR_OK)
292 * Write trailing bytes
295 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
298 if (retval != ERROR_OK)
301 data = be_to_h_u32((uint8_t *)&data);
302 for (j = 0; i < count; j++, i++) {
303 data &= ~(0xff << j*8);
304 data |= (buffer[j+i] << j*8);
307 h_u32_to_be((uint8_t *)&data_out, data);
309 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
312 if (retval != ERROR_OK)