cleanup: rename armv4_5 to arm for readability
[fw/openocd] / src / target / dsp563xx_once.c
1 /***************************************************************************
2  *   Copyright (C) 2009 by Mathias Kuester                                 *
3  *   mkdorg@users.sourceforge.net                                          *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <jim.h>
25
26 #include "target.h"
27 #include "target_type.h"
28 #include "register.h"
29 #include "dsp563xx.h"
30 #include "dsp563xx_once.h"
31
32 #define JTAG_STATUS_STATIC_MASK         0x03
33 #define JTAG_STATUS_STATIC_VALUE        0x01
34
35 #define JTAG_STATUS_NORMAL              0x01
36 #define JTAG_STATUS_STOPWAIT            0x05
37 #define JTAG_STATUS_BUSY                0x09
38 #define JTAG_STATUS_DEBUG               0x0d
39
40 #define JTAG_INSTR_EXTEST               0x00
41 #define JTAG_INSTR_SAMPLE_PRELOAD       0x01
42 #define JTAG_INSTR_IDCODE               0x02
43 #define JTAG_INSTR_HIZ                  0x04
44 #define JTAG_INSTR_CLAMP                0x05
45 #define JTAG_INSTR_ENABLE_ONCE          0x06
46 #define JTAG_INSTR_DEBUG_REQUEST        0x07
47 #define JTAG_INSTR_BYPASS               0x0F
48
49 /** */
50 static inline int dsp563xx_write_dr(struct jtag_tap *tap, uint8_t * dr_in, uint8_t * dr_out, int dr_len, int rti)
51 {
52         jtag_add_plain_dr_scan(dr_len, dr_out, dr_in, TAP_IDLE);
53
54         return ERROR_OK;
55 }
56
57 /** */
58 static inline int dsp563xx_write_dr_u8(struct jtag_tap *tap, uint8_t * dr_in, uint8_t dr_out, int dr_len, int rti)
59 {
60         return dsp563xx_write_dr(tap, dr_in, &dr_out, dr_len, rti);
61 }
62
63 /** */
64 static inline int dsp563xx_write_dr_u32(struct jtag_tap *tap, uint32_t * dr_in, uint32_t dr_out, int dr_len, int rti)
65 {
66         return dsp563xx_write_dr(tap, (uint8_t *) dr_in, (uint8_t *) & dr_out, dr_len, rti);
67 }
68
69 /** single word instruction */
70 static inline int dsp563xx_once_ir_exec(struct jtag_tap *tap, int flush, uint8_t instr, uint8_t rw, uint8_t go, uint8_t ex)
71 {
72         int err;
73
74         if ((err = dsp563xx_write_dr_u8(tap, 0, instr | (ex << 5) | (go << 6) | (rw << 7), 8, 0)) != ERROR_OK)
75                 return err;
76         if ( flush )
77                 err = jtag_execute_queue();
78         return err;
79 }
80
81 /* IR and DR functions */
82 static inline int dsp563xx_write_ir(struct jtag_tap *tap, uint8_t * ir_in, uint8_t * ir_out, int ir_len, int rti)
83 {
84         jtag_add_plain_ir_scan(tap->ir_length, ir_out, ir_in, TAP_IDLE);
85
86         return ERROR_OK;
87 }
88
89 static inline int dsp563xx_write_ir_u8(struct jtag_tap *tap, uint8_t * ir_in, uint8_t ir_out, int ir_len, int rti)
90 {
91         return dsp563xx_write_ir(tap, ir_in, &ir_out, ir_len, rti);
92 }
93
94 static inline int dsp563xx_jtag_sendinstr(struct jtag_tap *tap, uint8_t * ir_in, uint8_t ir_out)
95 {
96         return dsp563xx_write_ir_u8(tap, ir_in, ir_out, tap->ir_length, 1);
97 }
98
99 /** */
100 int dsp563xx_once_target_status(struct jtag_tap *tap)
101 {
102         int err;
103         uint8_t jtag_status;
104
105         if ((err = dsp563xx_jtag_sendinstr(tap, &jtag_status, JTAG_INSTR_ENABLE_ONCE)) != ERROR_OK)
106                 return TARGET_UNKNOWN;
107         if ((err = jtag_execute_queue()) != ERROR_OK)
108                 return TARGET_UNKNOWN;
109
110         /* verify correct static status pattern */
111         if ( (jtag_status & JTAG_STATUS_STATIC_MASK) != JTAG_STATUS_STATIC_VALUE )
112                 return TARGET_UNKNOWN;
113
114         if (jtag_status != JTAG_STATUS_DEBUG)
115                 return TARGET_RUNNING;
116
117         return TARGET_HALTED;
118 }
119
120 /** */
121 int dsp563xx_once_request_debug(struct jtag_tap *tap, int reset_state)
122 {
123         int err;
124         uint8_t ir_in = 0, pattern = 0;
125         uint32_t retry = 0;
126
127         /* in reset state we only get a ACK
128          * from the interface */
129         if (reset_state)
130         {
131                 pattern = 1;
132         }
133         else
134         {
135                 pattern = JTAG_STATUS_DEBUG;
136         }
137
138         /* wait until we get the ack */
139         while (ir_in != pattern)
140         {
141                 if ((err = dsp563xx_jtag_sendinstr(tap, &ir_in, JTAG_INSTR_DEBUG_REQUEST)) != ERROR_OK)
142                         return err;
143                 if ((err = jtag_execute_queue()) != ERROR_OK)
144                         return err;
145
146                 LOG_DEBUG("debug request: %02X", ir_in);
147
148                 if (retry++ == 100)
149                 {
150                         return ERROR_TARGET_FAILURE;
151                 }
152         }
153
154         /* we cant enable the once in reset state */
155         if (pattern == 1)
156         {
157                 return ERROR_OK;
158         }
159
160         /* try to enable once */
161         retry = 0;
162         ir_in = 0;
163         while (ir_in != pattern)
164         {
165                 if ((err = dsp563xx_jtag_sendinstr(tap, &ir_in, JTAG_INSTR_ENABLE_ONCE)) != ERROR_OK)
166                         return err;
167                 if ((err = jtag_execute_queue()) != ERROR_OK)
168                         return err;
169
170                 LOG_DEBUG("enable once: %02X", ir_in);
171
172                 if (retry++ == 100)
173                 {
174                         LOG_DEBUG("error");
175                         return ERROR_TARGET_FAILURE;
176                 }
177         }
178
179         if (ir_in != JTAG_STATUS_DEBUG)
180         {
181                 return ERROR_TARGET_FAILURE;
182         }
183
184         return ERROR_OK;
185 }
186
187 /** once read registers */
188 int dsp563xx_once_read_register(struct jtag_tap *tap, int flush, struct once_reg *regs, int len)
189 {
190         int i;
191         int err = ERROR_OK;
192
193         for (i = 0; i < len; i++)
194         {
195                 if ((err = dsp563xx_once_reg_read_ex(tap, flush, regs[i].addr, regs[i].len, &regs[i].reg)) != ERROR_OK)
196                         return err;
197         }
198
199         if ( flush )
200                 err = jtag_execute_queue();
201         return err;
202 }
203
204 /** once read register with register len */
205 int dsp563xx_once_reg_read_ex(struct jtag_tap *tap, int flush, uint8_t reg, uint8_t len, uint32_t * data)
206 {
207         int err;
208
209         if ((err = dsp563xx_once_ir_exec(tap, 1, reg, 1, 0, 0)) != ERROR_OK)
210                 return err;
211         if ((err = dsp563xx_write_dr_u32(tap, data, 0x00, len, 0)) != ERROR_OK)
212                 return err;
213         if ( flush )
214                 err = jtag_execute_queue();
215         return err;
216 }
217
218 /** once read register */
219 int dsp563xx_once_reg_read(struct jtag_tap *tap, int flush, uint8_t reg, uint32_t * data)
220 {
221         int err;
222
223         if ((err = dsp563xx_once_ir_exec(tap, flush, reg, 1, 0, 0)) != ERROR_OK)
224                 return err;
225         if ((err = dsp563xx_write_dr_u32(tap, data, 0x00, 24, 0)) != ERROR_OK)
226                 return err;
227         if ( flush )
228                 err = jtag_execute_queue();
229         return err;
230 }
231
232 /** once write register */
233 int dsp563xx_once_reg_write(struct jtag_tap *tap, int flush, uint8_t reg, uint32_t data)
234 {
235         int err;
236
237         if ((err = dsp563xx_once_ir_exec(tap, flush, reg, 0, 0, 0)) != ERROR_OK)
238                 return err;
239         if ((err = dsp563xx_write_dr_u32(tap, 0x00, data, 24, 0)) != ERROR_OK)
240                 return err;
241         if ( flush )
242                 err = jtag_execute_queue();
243         return err;
244 }
245
246 /** single word instruction */
247 int dsp563xx_once_execute_sw_ir(struct jtag_tap *tap, int flush, uint32_t opcode)
248 {
249         int err;
250
251         if ((err = dsp563xx_once_ir_exec(tap, flush, DSP563XX_ONCE_OPDBR, 0, 1, 0)) != ERROR_OK)
252                 return err;
253         if ((err = dsp563xx_write_dr_u32(tap, 0, opcode, 24, 0)) != ERROR_OK)
254                 return err;
255         if ( flush )
256                 err = jtag_execute_queue();
257         return err;
258 }
259
260 /** double word instruction */
261 int dsp563xx_once_execute_dw_ir(struct jtag_tap *tap, int flush, uint32_t opcode, uint32_t operand)
262 {
263         int err;
264
265         if ((err = dsp563xx_once_ir_exec(tap, flush, DSP563XX_ONCE_OPDBR, 0, 0, 0)) != ERROR_OK)
266                 return err;
267         if ((err = dsp563xx_write_dr_u32(tap, 0, opcode, 24, 0)) != ERROR_OK)
268                 return err;
269         if ( flush )
270                 if ((err = jtag_execute_queue()) != ERROR_OK)
271                         return err;
272
273         if ((err = dsp563xx_once_ir_exec(tap, flush, DSP563XX_ONCE_OPDBR, 0, 1, 0)) != ERROR_OK)
274                 return err;
275         if ((err = dsp563xx_write_dr_u32(tap, 0, operand, 24, 0)) != ERROR_OK)
276                 return err;
277         if ( flush )
278                 if ((err = jtag_execute_queue()) != ERROR_OK)
279                         return err;
280
281         return ERROR_OK;
282 }