1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2010 by Oleksandr Tymoshenko <gonzo@bluezbox.com> *
5 ***************************************************************************/
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
)
22 for (i
= 0; i
< count
; i
++) {
23 retval
= avr32_jtag_mwa_read(jtag_info
, SLAVE_HSB_UNCACHED
,
26 if (retval
!= ERROR_OK
)
29 /* XXX: Assume AVR32 is BE */
30 buffer
[i
] = be_to_h_u32((uint8_t *)&data
);
36 int avr32_jtag_read_memory16(struct avr32_jtag
*jtag_info
,
37 uint32_t addr
, int count
, uint16_t *buffer
)
44 /* any unaligned half-words? */
46 retval
= avr32_jtag_mwa_read(jtag_info
, SLAVE_HSB_UNCACHED
,
49 if (retval
!= ERROR_OK
)
52 /* XXX: Assume AVR32 is BE */
53 data
= be_to_h_u32((uint8_t *)&data
);
54 buffer
[i
] = (data
>> 16) & 0xffff;
58 /* read all complete words */
59 for (; i
< (count
& ~1); i
+= 2) {
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
& 0xffff;
69 buffer
[i
+1] = (data
>> 16) & 0xffff;
74 retval
= avr32_jtag_mwa_read(jtag_info
, SLAVE_HSB_UNCACHED
,
77 if (retval
!= ERROR_OK
)
80 /* XXX: Assume AVR32 is BE */
81 data
= be_to_h_u32((uint8_t *)&data
);
82 buffer
[i
] = data
& 0xffff;
88 int avr32_jtag_read_memory8(struct avr32_jtag
*jtag_info
,
89 uint32_t addr
, int count
, uint8_t *buffer
)
95 /* Do we have non-aligned bytes? */
97 retval
= avr32_jtag_mwa_read(jtag_info
, SLAVE_HSB_UNCACHED
,
98 addr
+ i
, (uint32_t *)(void *)data
);
100 if (retval
!= ERROR_OK
)
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
)
115 for (j
= 0; j
< 4; j
++)
116 buffer
[i
+j
] = data
[3-j
];
119 /* remaining bytes */
121 retval
= avr32_jtag_mwa_read(jtag_info
, SLAVE_HSB_UNCACHED
,
122 addr
+ i
, (uint32_t *)(void *)data
);
124 if (retval
!= ERROR_OK
)
127 for (j
= 0; i
+ j
< count
; j
++)
128 buffer
[i
+j
] = data
[3-j
];
134 int avr32_jtag_write_memory32(struct avr32_jtag
*jtag_info
,
135 uint32_t addr
, int count
, const uint32_t *buffer
)
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
,
146 if (retval
!= ERROR_OK
)
154 int avr32_jtag_write_memory16(struct avr32_jtag
*jtag_info
,
155 uint32_t addr
, int count
, const uint16_t *buffer
)
164 * Do we have any non-aligned half-words?
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
,
174 if (retval
!= ERROR_OK
)
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
,
184 if (retval
!= ERROR_OK
)
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
)
205 retval
= avr32_jtag_mwa_read(jtag_info
, SLAVE_HSB_UNCACHED
,
208 if (retval
!= ERROR_OK
)
211 data
= be_to_h_u32((uint8_t *)&data
);
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
)
226 int avr32_jtag_write_memory8(struct avr32_jtag
*jtag_info
,
227 uint32_t addr
, int count
, const uint8_t *buffer
)
236 * Do we have any non-aligned bytes?
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
,
246 if (retval
!= ERROR_OK
)
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
,
259 if (retval
!= ERROR_OK
)
264 /* write all complete words */
265 for (; i
< (count
& ~3); i
+= 4) {
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
,
276 if (retval
!= ERROR_OK
)
281 * Write trailing bytes
284 retval
= avr32_jtag_mwa_read(jtag_info
, SLAVE_HSB_UNCACHED
,
287 if (retval
!= ERROR_OK
)
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
,
301 if (retval
!= ERROR_OK
)