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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
18 ***************************************************************************/
25 #include "jtag/jtag.h"
26 #include "avr32_jtag.h"
27 #include "avr32_mem.h"
29 int avr32_jtag_read_memory32(struct avr32_jtag *jtag_info,
30 uint32_t addr, int count, uint32_t *buffer)
35 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? */
59 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
62 if (retval != ERROR_OK)
65 /* XXX: Assume AVR32 is BE */
66 data = be_to_h_u32((uint8_t *)&data);
67 buffer[i] = (data >> 16) & 0xffff;
71 /* read all complete words */
72 for (; i < (count & ~1); i += 2) {
73 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
76 if (retval != ERROR_OK)
79 /* XXX: Assume AVR32 is BE */
80 data = be_to_h_u32((uint8_t *)&data);
81 buffer[i] = data & 0xffff;
82 buffer[i+1] = (data >> 16) & 0xffff;
87 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
90 if (retval != ERROR_OK)
93 /* XXX: Assume AVR32 is BE */
94 data = be_to_h_u32((uint8_t *)&data);
95 buffer[i] = data & 0xffff;
101 int avr32_jtag_read_memory8(struct avr32_jtag *jtag_info,
102 uint32_t addr, int count, uint8_t *buffer)
108 /* Do we have non-aligned bytes? */
110 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
111 addr + i, (uint32_t *)(void *)data);
113 if (retval != ERROR_OK)
116 for (j = addr & 3; (j < 4) && (i < count); j++, i++)
117 buffer[i] = data[3-j];
120 /* read all complete words */
121 for (; i < (count & ~3); i += 4) {
122 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
123 addr + i, (uint32_t *)(void *)data);
125 if (retval != ERROR_OK)
128 for (j = 0; j < 4; j++)
129 buffer[i+j] = data[3-j];
132 /* remaining bytes */
134 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
135 addr + i, (uint32_t *)(void *)data);
137 if (retval != ERROR_OK)
140 for (j = 0; i + j < count; j++)
141 buffer[i+j] = data[3-j];
147 int avr32_jtag_write_memory32(struct avr32_jtag *jtag_info,
148 uint32_t addr, int count, const uint32_t *buffer)
153 for (i = 0; i < count; i++) {
154 /* XXX: Assume AVR32 is BE */
155 h_u32_to_be((uint8_t *)&data, buffer[i]);
156 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
159 if (retval != ERROR_OK)
167 int avr32_jtag_write_memory16(struct avr32_jtag *jtag_info,
168 uint32_t addr, int count, const uint16_t *buffer)
177 * Do we have any non-aligned half-words?
181 * mwa_read will read whole world, no nead to fiddle
182 * with address. It will be truncated in set_addr
184 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
187 if (retval != ERROR_OK)
190 data = be_to_h_u32((uint8_t *)&data);
191 data = (buffer[i] << 16) | (data & 0xffff);
192 h_u32_to_be((uint8_t *)&data_out, data);
194 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
197 if (retval != ERROR_OK)
203 /* write all complete words */
204 for (; i < (count & ~1); i += 2) {
205 /* XXX: Assume AVR32 is BE */
206 data = (buffer[i+1] << 16) | buffer[i];
207 h_u32_to_be((uint8_t *)&data_out, data);
209 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
210 addr + i*2, data_out);
212 if (retval != ERROR_OK)
218 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
221 if (retval != ERROR_OK)
224 data = be_to_h_u32((uint8_t *)&data);
227 h_u32_to_be((uint8_t *)&data_out, data);
229 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
230 addr + i*2, data_out);
232 if (retval != ERROR_OK)
239 int avr32_jtag_write_memory8(struct avr32_jtag *jtag_info,
240 uint32_t addr, int count, const uint8_t *buffer)
249 * Do we have any non-aligned bytes?
253 * mwa_read will read whole world, no nead to fiddle
254 * with address. It will be truncated in set_addr
256 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
259 if (retval != ERROR_OK)
262 data = be_to_h_u32((uint8_t *)&data);
263 for (j = addr & 3; (j < 4) && (i < count); j++, i++) {
264 data &= ~(0xff << j*8);
265 data |= (buffer[i] << j*8);
268 h_u32_to_be((uint8_t *)&data_out, data);
269 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
272 if (retval != ERROR_OK)
277 /* write all complete words */
278 for (; i < (count & ~3); i += 4) {
281 for (j = 0; j < 4; j++)
282 data |= (buffer[j+i] << j*8);
284 h_u32_to_be((uint8_t *)&data_out, data);
286 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
289 if (retval != ERROR_OK)
294 * Write trailing bytes
297 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
300 if (retval != ERROR_OK)
303 data = be_to_h_u32((uint8_t *)&data);
304 for (j = 0; i < count; j++, i++) {
305 data &= ~(0xff << j*8);
306 data |= (buffer[j+i] << j*8);
309 h_u32_to_be((uint8_t *)&data_out, data);
311 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
314 if (retval != ERROR_OK)