flash/nor/stm32l4x: Remove redundant error messages
[openocd.git] / src / target / avr32_mem.c
blob835a501f03d090e1e2cf756ef6716ebc79e86855
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2010 by Oleksandr Tymoshenko <gonzo@bluezbox.com> *
5 ***************************************************************************/
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
11 #include "target.h"
12 #include "jtag/jtag.h"
13 #include "avr32_jtag.h"
14 #include "avr32_mem.h"
16 int avr32_jtag_read_memory32(struct avr32_jtag *jtag_info,
17 uint32_t addr, int count, uint32_t *buffer)
19 int i, retval;
20 uint32_t data;
22 for (i = 0; i < count; i++) {
23 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
24 addr + i*4, &data);
26 if (retval != ERROR_OK)
27 return retval;
29 /* XXX: Assume AVR32 is BE */
30 buffer[i] = be_to_h_u32((uint8_t *)&data);
33 return ERROR_OK;
36 int avr32_jtag_read_memory16(struct avr32_jtag *jtag_info,
37 uint32_t addr, int count, uint16_t *buffer)
39 int i, retval;
40 uint32_t data;
42 i = 0;
44 /* any unaligned half-words? */
45 if (addr & 3) {
46 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
47 addr + i*2, &data);
49 if (retval != ERROR_OK)
50 return retval;
52 /* XXX: Assume AVR32 is BE */
53 data = be_to_h_u32((uint8_t *)&data);
54 buffer[i] = (data >> 16) & 0xffff;
55 i++;
58 /* read all complete words */
59 for (; i < (count & ~1); i += 2) {
60 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
61 addr + i*2, &data);
63 if (retval != ERROR_OK)
64 return retval;
66 /* XXX: Assume AVR32 is BE */
67 data = be_to_h_u32((uint8_t *)&data);
68 buffer[i] = data & 0xffff;
69 buffer[i+1] = (data >> 16) & 0xffff;
72 /* last halfword */
73 if (i < count) {
74 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
75 addr + i*2, &data);
77 if (retval != ERROR_OK)
78 return retval;
80 /* XXX: Assume AVR32 is BE */
81 data = be_to_h_u32((uint8_t *)&data);
82 buffer[i] = data & 0xffff;
85 return ERROR_OK;
88 int avr32_jtag_read_memory8(struct avr32_jtag *jtag_info,
89 uint32_t addr, int count, uint8_t *buffer)
91 int i, j, retval;
92 uint8_t data[4];
93 i = 0;
95 /* Do we have non-aligned bytes? */
96 if (addr & 3) {
97 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
98 addr + i, (uint32_t *)(void *)data);
100 if (retval != ERROR_OK)
101 return retval;
103 for (j = addr & 3; (j < 4) && (i < count); j++, i++)
104 buffer[i] = data[3-j];
107 /* read all complete words */
108 for (; i < (count & ~3); i += 4) {
109 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
110 addr + i, (uint32_t *)(void *)data);
112 if (retval != ERROR_OK)
113 return retval;
115 for (j = 0; j < 4; j++)
116 buffer[i+j] = data[3-j];
119 /* remaining bytes */
120 if (i < count) {
121 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
122 addr + i, (uint32_t *)(void *)data);
124 if (retval != ERROR_OK)
125 return retval;
127 for (j = 0; i + j < count; j++)
128 buffer[i+j] = data[3-j];
131 return ERROR_OK;
134 int avr32_jtag_write_memory32(struct avr32_jtag *jtag_info,
135 uint32_t addr, int count, const uint32_t *buffer)
137 int i, retval;
138 uint32_t data;
140 for (i = 0; i < count; i++) {
141 /* XXX: Assume AVR32 is BE */
142 h_u32_to_be((uint8_t *)&data, buffer[i]);
143 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
144 addr + i*4, data);
146 if (retval != ERROR_OK)
147 return retval;
151 return ERROR_OK;
154 int avr32_jtag_write_memory16(struct avr32_jtag *jtag_info,
155 uint32_t addr, int count, const uint16_t *buffer)
157 int i, retval;
158 uint32_t data;
159 uint32_t data_out;
161 i = 0;
164 * Do we have any non-aligned half-words?
166 if (addr & 3) {
168 * mwa_read will read whole world, no need to fiddle
169 * with address. It will be truncated in set_addr
171 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
172 addr, &data);
174 if (retval != ERROR_OK)
175 return retval;
177 data = be_to_h_u32((uint8_t *)&data);
178 data = (buffer[i] << 16) | (data & 0xffff);
179 h_u32_to_be((uint8_t *)&data_out, data);
181 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
182 addr, data_out);
184 if (retval != ERROR_OK)
185 return retval;
187 i++;
190 /* write all complete words */
191 for (; i < (count & ~1); i += 2) {
192 /* XXX: Assume AVR32 is BE */
193 data = (buffer[i+1] << 16) | buffer[i];
194 h_u32_to_be((uint8_t *)&data_out, data);
196 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
197 addr + i*2, data_out);
199 if (retval != ERROR_OK)
200 return retval;
203 /* last halfword */
204 if (i < count) {
205 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
206 addr + i*2, &data);
208 if (retval != ERROR_OK)
209 return retval;
211 data = be_to_h_u32((uint8_t *)&data);
212 data &= ~0xffff;
213 data |= buffer[i];
214 h_u32_to_be((uint8_t *)&data_out, data);
216 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
217 addr + i*2, data_out);
219 if (retval != ERROR_OK)
220 return retval;
223 return ERROR_OK;
226 int avr32_jtag_write_memory8(struct avr32_jtag *jtag_info,
227 uint32_t addr, int count, const uint8_t *buffer)
229 int i, j, retval;
230 uint32_t data;
231 uint32_t data_out;
233 i = 0;
236 * Do we have any non-aligned bytes?
238 if (addr & 3) {
240 * mwa_read will read whole world, no need to fiddle
241 * with address. It will be truncated in set_addr
243 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
244 addr, &data);
246 if (retval != ERROR_OK)
247 return retval;
249 data = be_to_h_u32((uint8_t *)&data);
250 for (j = addr & 3; (j < 4) && (i < count); j++, i++) {
251 data &= ~(0xff << j*8);
252 data |= (buffer[i] << j*8);
255 h_u32_to_be((uint8_t *)&data_out, data);
256 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
257 addr, data_out);
259 if (retval != ERROR_OK)
260 return retval;
264 /* write all complete words */
265 for (; i < (count & ~3); i += 4) {
266 data = 0;
268 for (j = 0; j < 4; j++)
269 data |= (buffer[j+i] << j*8);
271 h_u32_to_be((uint8_t *)&data_out, data);
273 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
274 addr + i, data_out);
276 if (retval != ERROR_OK)
277 return retval;
281 * Write trailing bytes
283 if (i < count) {
284 retval = avr32_jtag_mwa_read(jtag_info, SLAVE_HSB_UNCACHED,
285 addr + i, &data);
287 if (retval != ERROR_OK)
288 return retval;
290 data = be_to_h_u32((uint8_t *)&data);
291 for (j = 0; i < count; j++, i++) {
292 data &= ~(0xff << j*8);
293 data |= (buffer[j+i] << j*8);
296 h_u32_to_be((uint8_t *)&data_out, data);
298 retval = avr32_jtag_mwa_write(jtag_info, SLAVE_HSB_UNCACHED,
299 addr+i, data_out);
301 if (retval != ERROR_OK)
302 return retval;
305 return ERROR_OK;