1 /***************************************************************************
2 * Copyright (C) 2008 by John McCarthy *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * Copyright (C) 2008 by David T.L. Wong *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
24 ***************************************************************************/
29 #include "mips32_dmaacc.h"
31 static int mips32_dmaacc_read_mem8(struct mips_ejtag
*ejtag_info
,
32 uint32_t addr
, int count
, uint8_t *buf
);
33 static int mips32_dmaacc_read_mem16(struct mips_ejtag
*ejtag_info
,
34 uint32_t addr
, int count
, uint16_t *buf
);
35 static int mips32_dmaacc_read_mem32(struct mips_ejtag
*ejtag_info
,
36 uint32_t addr
, int count
, uint32_t *buf
);
38 static int mips32_dmaacc_write_mem8(struct mips_ejtag
*ejtag_info
,
39 uint32_t addr
, int count
, uint8_t *buf
);
40 static int mips32_dmaacc_write_mem16(struct mips_ejtag
*ejtag_info
,
41 uint32_t addr
, int count
, uint16_t *buf
);
42 static int mips32_dmaacc_write_mem32(struct mips_ejtag
*ejtag_info
,
43 uint32_t addr
, int count
, uint32_t *buf
);
46 * The following logic shamelessly cloned from HairyDairyMaid's wrt54g_debrick
47 * to support the Broadcom BCM5352 SoC in the Linksys WRT54GL wireless router
48 * (and any others that support EJTAG DMA transfers).
49 * Note: This only supports memory read/write. Since the BCM5352 doesn't
50 * appear to support PRACC accesses, all debug functions except halt
51 * do not work. Still, this does allow erasing/writing flash as well as
52 * displaying/modifying memory and memory mapped registers.
55 static int ejtag_dma_read(struct mips_ejtag
*ejtag_info
, uint32_t addr
, uint32_t *data
)
59 int retries
= RETRY_ATTEMPTS
;
65 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_ADDRESS
);
66 mips_ejtag_drscan_32(ejtag_info
, &v
);
68 /* Initiate DMA Read & set DSTRT */
69 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
70 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| EJTAG_CTRL_DRWN
| EJTAG_CTRL_DMA_WORD
| EJTAG_CTRL_DSTRT
| ejtag_info
->ejtag_ctrl
;
71 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
73 /* Wait for DSTRT to Clear */
75 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| ejtag_info
->ejtag_ctrl
;
76 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
77 } while (ejtag_ctrl
& EJTAG_CTRL_DSTRT
);
80 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_DATA
);
81 mips_ejtag_drscan_32(ejtag_info
, data
);
83 /* Clear DMA & Check DERR */
84 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
85 ejtag_ctrl
= ejtag_info
->ejtag_ctrl
;
86 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
87 if (ejtag_ctrl
& EJTAG_CTRL_DERR
)
90 LOG_ERROR("DMA Read Addr = %08" PRIx32
" Data = ERROR ON READ (retrying)\n", addr
);
91 goto begin_ejtag_dma_read
;
94 LOG_ERROR("DMA Read Addr = %08" PRIx32
" Data = ERROR ON READ\n", addr
);
95 return ERROR_JTAG_DEVICE_ERROR
;
101 static int ejtag_dma_read_h(struct mips_ejtag
*ejtag_info
, uint32_t addr
, uint16_t *data
)
105 int retries
= RETRY_ATTEMPTS
;
107 begin_ejtag_dma_read_h
:
111 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_ADDRESS
);
112 mips_ejtag_drscan_32(ejtag_info
, &v
);
114 /* Initiate DMA Read & set DSTRT */
115 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
116 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| EJTAG_CTRL_DRWN
| EJTAG_CTRL_DMA_HALFWORD
| EJTAG_CTRL_DSTRT
| ejtag_info
->ejtag_ctrl
;
117 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
119 /* Wait for DSTRT to Clear */
121 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| ejtag_info
->ejtag_ctrl
;
122 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
123 } while (ejtag_ctrl
& EJTAG_CTRL_DSTRT
);
126 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_DATA
);
127 mips_ejtag_drscan_32(ejtag_info
, &v
);
129 /* Clear DMA & Check DERR */
130 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
131 ejtag_ctrl
= ejtag_info
->ejtag_ctrl
;
132 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
133 if (ejtag_ctrl
& EJTAG_CTRL_DERR
)
136 LOG_ERROR("DMA Read Addr = %08" PRIx32
" Data = ERROR ON READ (retrying)\n", addr
);
137 goto begin_ejtag_dma_read_h
;
140 LOG_ERROR("DMA Read Addr = %08" PRIx32
" Data = ERROR ON READ\n", addr
);
141 return ERROR_JTAG_DEVICE_ERROR
;
144 /* Handle the bigendian/littleendian */
146 *data
= (v
>> 16) & 0xffff;
148 *data
= (v
& 0x0000ffff);
153 static int ejtag_dma_read_b(struct mips_ejtag
*ejtag_info
, uint32_t addr
, uint8_t *data
)
157 int retries
= RETRY_ATTEMPTS
;
159 begin_ejtag_dma_read_b
:
163 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_ADDRESS
);
164 mips_ejtag_drscan_32(ejtag_info
, &v
);
166 /* Initiate DMA Read & set DSTRT */
167 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
168 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| EJTAG_CTRL_DRWN
| EJTAG_CTRL_DMA_BYTE
| EJTAG_CTRL_DSTRT
| ejtag_info
->ejtag_ctrl
;
169 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
171 /* Wait for DSTRT to Clear */
173 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| ejtag_info
->ejtag_ctrl
;
174 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
175 } while (ejtag_ctrl
& EJTAG_CTRL_DSTRT
);
178 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_DATA
);
179 mips_ejtag_drscan_32(ejtag_info
, &v
);
181 /* Clear DMA & Check DERR */
182 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
183 ejtag_ctrl
= ejtag_info
->ejtag_ctrl
;
184 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
185 if (ejtag_ctrl
& EJTAG_CTRL_DERR
)
188 LOG_ERROR("DMA Read Addr = %08" PRIx32
" Data = ERROR ON READ (retrying)\n", addr
);
189 goto begin_ejtag_dma_read_b
;
192 LOG_ERROR("DMA Read Addr = %08" PRIx32
" Data = ERROR ON READ\n", addr
);
193 return ERROR_JTAG_DEVICE_ERROR
;
196 /* Handle the bigendian/littleendian */
197 switch (addr
& 0x3) {
202 *data
= (v
>> 8) & 0xff;
205 *data
= (v
>> 16) & 0xff;
208 *data
= (v
>> 24) & 0xff;
215 static int ejtag_dma_write(struct mips_ejtag
*ejtag_info
, uint32_t addr
, uint32_t data
)
219 int retries
= RETRY_ATTEMPTS
;
221 begin_ejtag_dma_write
:
225 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_ADDRESS
);
226 mips_ejtag_drscan_32(ejtag_info
, &v
);
230 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_DATA
);
231 mips_ejtag_drscan_32(ejtag_info
, &v
);
233 /* Initiate DMA Write & set DSTRT */
234 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
235 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| EJTAG_CTRL_DMA_WORD
| EJTAG_CTRL_DSTRT
| ejtag_info
->ejtag_ctrl
;
236 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
238 /* Wait for DSTRT to Clear */
240 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| ejtag_info
->ejtag_ctrl
;
241 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
242 } while (ejtag_ctrl
& EJTAG_CTRL_DSTRT
);
244 /* Clear DMA & Check DERR */
245 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
246 ejtag_ctrl
= ejtag_info
->ejtag_ctrl
;
247 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
248 if (ejtag_ctrl
& EJTAG_CTRL_DERR
)
251 LOG_ERROR("DMA Write Addr = %08" PRIx32
" Data = ERROR ON WRITE (retrying)\n", addr
);
252 goto begin_ejtag_dma_write
;
255 LOG_ERROR("DMA Write Addr = %08" PRIx32
" Data = ERROR ON WRITE\n", addr
);
256 return ERROR_JTAG_DEVICE_ERROR
;
262 static int ejtag_dma_write_h(struct mips_ejtag
*ejtag_info
, uint32_t addr
, uint32_t data
)
266 int retries
= RETRY_ATTEMPTS
;
268 /* Handle the bigendian/littleendian */
272 begin_ejtag_dma_write_h
:
276 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_ADDRESS
);
277 mips_ejtag_drscan_32(ejtag_info
, &v
);
281 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_DATA
);
282 mips_ejtag_drscan_32(ejtag_info
, &v
);
284 /* Initiate DMA Write & set DSTRT */
285 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
286 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| EJTAG_CTRL_DMA_HALFWORD
| EJTAG_CTRL_DSTRT
| ejtag_info
->ejtag_ctrl
;
287 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
289 /* Wait for DSTRT to Clear */
291 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| ejtag_info
->ejtag_ctrl
;
292 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
293 } while (ejtag_ctrl
& EJTAG_CTRL_DSTRT
);
295 /* Clear DMA & Check DERR */
296 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
297 ejtag_ctrl
= ejtag_info
->ejtag_ctrl
;
298 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
299 if (ejtag_ctrl
& EJTAG_CTRL_DERR
)
302 LOG_ERROR("DMA Write Addr = %08" PRIx32
" Data = ERROR ON WRITE (retrying)\n", addr
);
303 goto begin_ejtag_dma_write_h
;
306 LOG_ERROR("DMA Write Addr = %08" PRIx32
" Data = ERROR ON WRITE\n", addr
);
307 return ERROR_JTAG_DEVICE_ERROR
;
313 static int ejtag_dma_write_b(struct mips_ejtag
*ejtag_info
, uint32_t addr
, uint32_t data
)
317 int retries
= RETRY_ATTEMPTS
;
319 /* Handle the bigendian/littleendian */
324 begin_ejtag_dma_write_b
:
328 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_ADDRESS
);
329 mips_ejtag_drscan_32(ejtag_info
, &v
);
333 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_DATA
);
334 mips_ejtag_drscan_32(ejtag_info
, &v
);
336 /* Initiate DMA Write & set DSTRT */
337 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
338 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| EJTAG_CTRL_DMA_BYTE
| EJTAG_CTRL_DSTRT
| ejtag_info
->ejtag_ctrl
;
339 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
341 /* Wait for DSTRT to Clear */
343 ejtag_ctrl
= EJTAG_CTRL_DMAACC
| ejtag_info
->ejtag_ctrl
;
344 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
345 } while (ejtag_ctrl
& EJTAG_CTRL_DSTRT
);
347 /* Clear DMA & Check DERR */
348 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
349 ejtag_ctrl
= ejtag_info
->ejtag_ctrl
;
350 mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
351 if (ejtag_ctrl
& EJTAG_CTRL_DERR
)
354 LOG_ERROR("DMA Write Addr = %08" PRIx32
" Data = ERROR ON WRITE (retrying)\n", addr
);
355 goto begin_ejtag_dma_write_b
;
358 LOG_ERROR("DMA Write Addr = %08" PRIx32
" Data = ERROR ON WRITE\n", addr
);
359 return ERROR_JTAG_DEVICE_ERROR
;
365 int mips32_dmaacc_read_mem(struct mips_ejtag
*ejtag_info
, uint32_t addr
, int size
, int count
, void *buf
)
370 return mips32_dmaacc_read_mem8(ejtag_info
, addr
, count
, (uint8_t*)buf
);
372 return mips32_dmaacc_read_mem16(ejtag_info
, addr
, count
, (uint16_t*)buf
);
374 return mips32_dmaacc_read_mem32(ejtag_info
, addr
, count
, (uint32_t*)buf
);
380 static int mips32_dmaacc_read_mem32(struct mips_ejtag
*ejtag_info
, uint32_t addr
, int count
, uint32_t *buf
)
385 for (i
= 0; i
< count
; i
++) {
386 if ((retval
= ejtag_dma_read(ejtag_info
, addr
+ i
*sizeof(*buf
), &buf
[i
])) != ERROR_OK
)
393 static int mips32_dmaacc_read_mem16(struct mips_ejtag
*ejtag_info
, uint32_t addr
, int count
, uint16_t *buf
)
398 for (i
= 0; i
< count
; i
++) {
399 if ((retval
= ejtag_dma_read_h(ejtag_info
, addr
+ i
*sizeof(*buf
), &buf
[i
])) != ERROR_OK
)
406 static int mips32_dmaacc_read_mem8(struct mips_ejtag
*ejtag_info
, uint32_t addr
, int count
, uint8_t *buf
)
411 for (i
= 0; i
< count
; i
++) {
412 if ((retval
= ejtag_dma_read_b(ejtag_info
, addr
+ i
*sizeof(*buf
), &buf
[i
])) != ERROR_OK
)
419 int mips32_dmaacc_write_mem(struct mips_ejtag
*ejtag_info
, uint32_t addr
, int size
, int count
, void *buf
)
424 return mips32_dmaacc_write_mem8(ejtag_info
, addr
, count
, (uint8_t*)buf
);
426 return mips32_dmaacc_write_mem16(ejtag_info
, addr
, count
,(uint16_t*)buf
);
428 return mips32_dmaacc_write_mem32(ejtag_info
, addr
, count
, (uint32_t*)buf
);
434 static int mips32_dmaacc_write_mem32(struct mips_ejtag
*ejtag_info
, uint32_t addr
, int count
, uint32_t *buf
)
439 for (i
= 0; i
< count
; i
++) {
440 if ((retval
= ejtag_dma_write(ejtag_info
, addr
+ i
*sizeof(*buf
), buf
[i
])) != ERROR_OK
)
447 static int mips32_dmaacc_write_mem16(struct mips_ejtag
*ejtag_info
, uint32_t addr
, int count
, uint16_t *buf
)
452 for (i
= 0; i
< count
; i
++) {
453 if ((retval
= ejtag_dma_write_h(ejtag_info
, addr
+ i
*sizeof(*buf
), buf
[i
])) != ERROR_OK
)
460 static int mips32_dmaacc_write_mem8(struct mips_ejtag
*ejtag_info
, uint32_t addr
, int count
, uint8_t *buf
)
465 for (i
= 0; i
< count
; i
++) {
466 if ((retval
= ejtag_dma_write_b(ejtag_info
, addr
+ i
*sizeof(*buf
), buf
[i
])) != ERROR_OK
)