1 /***************************************************************************
2 * Copyright (C) 2013 by Paul Fertser, fercerpav@gmail.com *
4 * Copyright (C) 2012 by Creative Product Design, marc @ cpdesign.com.au *
5 * Based on at91rm9200.c (c) Anders Larsen *
6 * and RPi GPIO examples by Gert van Loo & Dom *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
26 #include <jtag/interface.h>
31 uint32_t bcm2835_peri_base
= 0x20000000;
32 #define BCM2835_GPIO_BASE (bcm2835_peri_base + 0x200000) /* GPIO controller */
34 #define BCM2835_PADS_GPIO_0_27 (bcm2835_peri_base + 0x100000)
35 #define BCM2835_PADS_GPIO_0_27_OFFSET (0x2c / 4)
37 /* GPIO setup macros */
38 #define MODE_GPIO(g) (*(pio_base+((g)/10))>>(((g)%10)*3) & 7)
39 #define INP_GPIO(g) do { *(pio_base+((g)/10)) &= ~(7<<(((g)%10)*3)); } while (0)
40 #define SET_MODE_GPIO(g, m) do { /* clear the mode bits first, then set as necessary */ \
42 *(pio_base+((g)/10)) |= ((m)<<(((g)%10)*3)); } while (0)
43 #define OUT_GPIO(g) SET_MODE_GPIO(g, 1)
45 #define GPIO_SET (*(pio_base+7)) /* sets bits which are 1, ignores bits which are 0 */
46 #define GPIO_CLR (*(pio_base+10)) /* clears bits which are 1, ignores bits which are 0 */
47 #define GPIO_LEV (*(pio_base+13)) /* current level of the pin */
49 static int dev_mem_fd
;
50 static volatile uint32_t *pio_base
;
52 static int bcm2835gpio_read(void);
53 static void bcm2835gpio_write(int tck
, int tms
, int tdi
);
54 static void bcm2835gpio_reset(int trst
, int srst
);
56 static int bcm2835_swdio_read(void);
57 static void bcm2835_swdio_drive(bool is_output
);
59 static int bcm2835gpio_init(void);
60 static int bcm2835gpio_quit(void);
62 static struct bitbang_interface bcm2835gpio_bitbang
= {
63 .read
= bcm2835gpio_read
,
64 .write
= bcm2835gpio_write
,
65 .reset
= bcm2835gpio_reset
,
66 .swdio_read
= bcm2835_swdio_read
,
67 .swdio_drive
= bcm2835_swdio_drive
,
71 /* GPIO numbers for each signal. Negative values are invalid */
72 static int tck_gpio
= -1;
73 static int tck_gpio_mode
;
74 static int tms_gpio
= -1;
75 static int tms_gpio_mode
;
76 static int tdi_gpio
= -1;
77 static int tdi_gpio_mode
;
78 static int tdo_gpio
= -1;
79 static int tdo_gpio_mode
;
80 static int trst_gpio
= -1;
81 static int trst_gpio_mode
;
82 static int srst_gpio
= -1;
83 static int srst_gpio_mode
;
84 static int swclk_gpio
= -1;
85 static int swclk_gpio_mode
;
86 static int swdio_gpio
= -1;
87 static int swdio_gpio_mode
;
89 /* Transition delay coefficients */
90 static int speed_coeff
= 113714;
91 static int speed_offset
= 28;
92 static unsigned int jtag_delay
;
94 static int bcm2835gpio_read(void)
96 return !!(GPIO_LEV
& 1<<tdo_gpio
);
99 static void bcm2835gpio_write(int tck
, int tms
, int tdi
)
101 uint32_t set
= tck
<<tck_gpio
| tms
<<tms_gpio
| tdi
<<tdi_gpio
;
102 uint32_t clear
= !tck
<<tck_gpio
| !tms
<<tms_gpio
| !tdi
<<tdi_gpio
;
107 for (unsigned int i
= 0; i
< jtag_delay
; i
++)
111 static void bcm2835gpio_swd_write(int tck
, int tms
, int tdi
)
113 uint32_t set
= tck
<<swclk_gpio
| tdi
<<swdio_gpio
;
114 uint32_t clear
= !tck
<<swclk_gpio
| !tdi
<<swdio_gpio
;
119 for (unsigned int i
= 0; i
< jtag_delay
; i
++)
123 /* (1) assert or (0) deassert reset lines */
124 static void bcm2835gpio_reset(int trst
, int srst
)
130 set
|= !trst
<<trst_gpio
;
131 clear
|= trst
<<trst_gpio
;
135 set
|= !srst
<<srst_gpio
;
136 clear
|= srst
<<srst_gpio
;
143 static void bcm2835_swdio_drive(bool is_output
)
146 OUT_GPIO(swdio_gpio
);
148 INP_GPIO(swdio_gpio
);
151 static int bcm2835_swdio_read(void)
153 return !!(GPIO_LEV
& 1 << swdio_gpio
);
156 static int bcm2835gpio_khz(int khz
, int *jtag_speed
)
159 LOG_DEBUG("RCLK not supported");
162 *jtag_speed
= speed_coeff
/khz
- speed_offset
;
168 static int bcm2835gpio_speed_div(int speed
, int *khz
)
170 *khz
= speed_coeff
/(speed
+ speed_offset
);
174 static int bcm2835gpio_speed(int speed
)
180 static int is_gpio_valid(int gpio
)
182 return gpio
>= 0 && gpio
<= 53;
185 COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionums
)
188 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], tck_gpio
);
189 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], tms_gpio
);
190 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[2], tdi_gpio
);
191 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[3], tdo_gpio
);
192 } else if (CMD_ARGC
!= 0) {
193 return ERROR_COMMAND_SYNTAX_ERROR
;
196 command_print(CMD_CTX
,
197 "BCM2835 GPIO config: tck = %d, tms = %d, tdi = %d, tdo = %d",
198 tck_gpio
, tms_gpio
, tdi_gpio
, tdo_gpio
);
203 COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_tck
)
206 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], tck_gpio
);
208 command_print(CMD_CTX
, "BCM2835 GPIO config: tck = %d", tck_gpio
);
212 COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_tms
)
215 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], tms_gpio
);
217 command_print(CMD_CTX
, "BCM2835 GPIO config: tms = %d", tms_gpio
);
221 COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_tdo
)
224 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], tdo_gpio
);
226 command_print(CMD_CTX
, "BCM2835 GPIO config: tdo = %d", tdo_gpio
);
230 COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_tdi
)
233 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], tdi_gpio
);
235 command_print(CMD_CTX
, "BCM2835 GPIO config: tdi = %d", tdi_gpio
);
239 COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_srst
)
242 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], srst_gpio
);
244 command_print(CMD_CTX
, "BCM2835 GPIO config: srst = %d", srst_gpio
);
248 COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_trst
)
251 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], trst_gpio
);
253 command_print(CMD_CTX
, "BCM2835 GPIO config: trst = %d", trst_gpio
);
257 COMMAND_HANDLER(bcm2835gpio_handle_swd_gpionums
)
260 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], swclk_gpio
);
261 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], swdio_gpio
);
262 } else if (CMD_ARGC
!= 0) {
263 return ERROR_COMMAND_SYNTAX_ERROR
;
266 command_print(CMD_CTX
,
267 "BCM2835 GPIO nums: swclk = %d, swdio = %d",
268 swclk_gpio
, swdio_gpio
);
273 COMMAND_HANDLER(bcm2835gpio_handle_swd_gpionum_swclk
)
276 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], swclk_gpio
);
278 command_print(CMD_CTX
, "BCM2835 num: swclk = %d", swclk_gpio
);
282 COMMAND_HANDLER(bcm2835gpio_handle_swd_gpionum_swdio
)
285 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], swdio_gpio
);
287 command_print(CMD_CTX
, "BCM2835 num: swdio = %d", swdio_gpio
);
291 COMMAND_HANDLER(bcm2835gpio_handle_speed_coeffs
)
294 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], speed_coeff
);
295 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], speed_offset
);
300 COMMAND_HANDLER(bcm2835gpio_handle_peripheral_base
)
303 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], bcm2835_peri_base
);
307 static const struct command_registration bcm2835gpio_command_handlers
[] = {
309 .name
= "bcm2835gpio_jtag_nums",
310 .handler
= &bcm2835gpio_handle_jtag_gpionums
,
311 .mode
= COMMAND_CONFIG
,
312 .help
= "gpio numbers for tck, tms, tdi, tdo. (in that order)",
313 .usage
= "(tck tms tdi tdo)* ",
316 .name
= "bcm2835gpio_tck_num",
317 .handler
= &bcm2835gpio_handle_jtag_gpionum_tck
,
318 .mode
= COMMAND_CONFIG
,
319 .help
= "gpio number for tck.",
322 .name
= "bcm2835gpio_tms_num",
323 .handler
= &bcm2835gpio_handle_jtag_gpionum_tms
,
324 .mode
= COMMAND_CONFIG
,
325 .help
= "gpio number for tms.",
328 .name
= "bcm2835gpio_tdo_num",
329 .handler
= &bcm2835gpio_handle_jtag_gpionum_tdo
,
330 .mode
= COMMAND_CONFIG
,
331 .help
= "gpio number for tdo.",
334 .name
= "bcm2835gpio_tdi_num",
335 .handler
= &bcm2835gpio_handle_jtag_gpionum_tdi
,
336 .mode
= COMMAND_CONFIG
,
337 .help
= "gpio number for tdi.",
340 .name
= "bcm2835gpio_swd_nums",
341 .handler
= &bcm2835gpio_handle_swd_gpionums
,
342 .mode
= COMMAND_CONFIG
,
343 .help
= "gpio numbers for swclk, swdio. (in that order)",
344 .usage
= "(swclk swdio)* ",
347 .name
= "bcm2835gpio_swclk_num",
348 .handler
= &bcm2835gpio_handle_swd_gpionum_swclk
,
349 .mode
= COMMAND_CONFIG
,
350 .help
= "gpio number for swclk.",
353 .name
= "bcm2835gpio_swdio_num",
354 .handler
= &bcm2835gpio_handle_swd_gpionum_swdio
,
355 .mode
= COMMAND_CONFIG
,
356 .help
= "gpio number for swdio.",
359 .name
= "bcm2835gpio_srst_num",
360 .handler
= &bcm2835gpio_handle_jtag_gpionum_srst
,
361 .mode
= COMMAND_CONFIG
,
362 .help
= "gpio number for srst.",
365 .name
= "bcm2835gpio_trst_num",
366 .handler
= &bcm2835gpio_handle_jtag_gpionum_trst
,
367 .mode
= COMMAND_CONFIG
,
368 .help
= "gpio number for trst.",
371 .name
= "bcm2835gpio_speed_coeffs",
372 .handler
= &bcm2835gpio_handle_speed_coeffs
,
373 .mode
= COMMAND_CONFIG
,
374 .help
= "SPEED_COEFF and SPEED_OFFSET for delay calculations.",
377 .name
= "bcm2835gpio_peripheral_base",
378 .handler
= &bcm2835gpio_handle_peripheral_base
,
379 .mode
= COMMAND_CONFIG
,
380 .help
= "peripheral base to access GPIOs (RPi1 0x20000000, RPi2 0x3F000000).",
383 COMMAND_REGISTRATION_DONE
386 static const char * const bcm2835_transports
[] = { "jtag", "swd", NULL
};
388 struct jtag_interface bcm2835gpio_interface
= {
389 .name
= "bcm2835gpio",
390 .supported
= DEBUG_CAP_TMS_SEQ
,
391 .execute_queue
= bitbang_execute_queue
,
392 .transports
= bcm2835_transports
,
394 .speed
= bcm2835gpio_speed
,
395 .khz
= bcm2835gpio_khz
,
396 .speed_div
= bcm2835gpio_speed_div
,
397 .commands
= bcm2835gpio_command_handlers
,
398 .init
= bcm2835gpio_init
,
399 .quit
= bcm2835gpio_quit
,
402 static bool bcm2835gpio_jtag_mode_possible(void)
404 if (!is_gpio_valid(tck_gpio
))
406 if (!is_gpio_valid(tms_gpio
))
408 if (!is_gpio_valid(tdi_gpio
))
410 if (!is_gpio_valid(tdo_gpio
))
415 static bool bcm2835gpio_swd_mode_possible(void)
417 if (!is_gpio_valid(swclk_gpio
))
419 if (!is_gpio_valid(swdio_gpio
))
424 static int bcm2835gpio_init(void)
426 bitbang_interface
= &bcm2835gpio_bitbang
;
428 LOG_INFO("BCM2835 GPIO JTAG/SWD bitbang driver");
430 if (bcm2835gpio_jtag_mode_possible()) {
431 if (bcm2835gpio_swd_mode_possible())
432 LOG_INFO("JTAG and SWD modes enabled");
434 LOG_INFO("JTAG only mode enabled (specify swclk and swdio gpio to add SWD mode)");
435 } else if (bcm2835gpio_swd_mode_possible()) {
436 LOG_INFO("SWD only mode enabled (specify tck, tms, tdi and tdo gpios to add JTAG mode)");
438 LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode and/or swclk and swdio gpio for SWD mode");
439 return ERROR_JTAG_INIT_FAILED
;
442 dev_mem_fd
= open("/dev/mem", O_RDWR
| O_SYNC
);
443 if (dev_mem_fd
< 0) {
445 return ERROR_JTAG_INIT_FAILED
;
448 pio_base
= mmap(NULL
, sysconf(_SC_PAGE_SIZE
), PROT_READ
| PROT_WRITE
,
449 MAP_SHARED
, dev_mem_fd
, BCM2835_GPIO_BASE
);
451 if (pio_base
== MAP_FAILED
) {
454 return ERROR_JTAG_INIT_FAILED
;
457 static volatile uint32_t *pads_base
;
458 pads_base
= mmap(NULL
, sysconf(_SC_PAGE_SIZE
), PROT_READ
| PROT_WRITE
,
459 MAP_SHARED
, dev_mem_fd
, BCM2835_PADS_GPIO_0_27
);
461 if (pads_base
== MAP_FAILED
) {
464 return ERROR_JTAG_INIT_FAILED
;
467 /* set 4mA drive strength, slew rate limited, hysteresis on */
468 pads_base
[BCM2835_PADS_GPIO_0_27_OFFSET
] = 0x5a000008 + 1;
470 tdo_gpio_mode
= MODE_GPIO(tdo_gpio
);
471 tdi_gpio_mode
= MODE_GPIO(tdi_gpio
);
472 tck_gpio_mode
= MODE_GPIO(tck_gpio
);
473 tms_gpio_mode
= MODE_GPIO(tms_gpio
);
474 swclk_gpio_mode
= MODE_GPIO(swclk_gpio
);
475 swdio_gpio_mode
= MODE_GPIO(swdio_gpio
);
477 * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
478 * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
482 GPIO_CLR
= 1<<tdi_gpio
| 1<<tck_gpio
| 1<<swdio_gpio
| 1<<swclk_gpio
;
483 GPIO_SET
= 1<<tms_gpio
;
488 OUT_GPIO(swclk_gpio
);
489 OUT_GPIO(swdio_gpio
);
490 if (trst_gpio
!= -1) {
491 trst_gpio_mode
= MODE_GPIO(trst_gpio
);
492 GPIO_SET
= 1 << trst_gpio
;
495 if (srst_gpio
!= -1) {
496 srst_gpio_mode
= MODE_GPIO(srst_gpio
);
497 GPIO_SET
= 1 << srst_gpio
;
501 LOG_DEBUG("saved pinmux settings: tck %d tms %d tdi %d "
502 "tdo %d trst %d srst %d", tck_gpio_mode
, tms_gpio_mode
,
503 tdi_gpio_mode
, tdo_gpio_mode
, trst_gpio_mode
, srst_gpio_mode
);
506 bcm2835gpio_bitbang
.write
= bcm2835gpio_swd_write
;
507 bitbang_switch_to_swd();
513 static int bcm2835gpio_quit(void)
515 SET_MODE_GPIO(tdo_gpio
, tdo_gpio_mode
);
516 SET_MODE_GPIO(tdi_gpio
, tdi_gpio_mode
);
517 SET_MODE_GPIO(tck_gpio
, tck_gpio_mode
);
518 SET_MODE_GPIO(tms_gpio
, tms_gpio_mode
);
519 SET_MODE_GPIO(swclk_gpio
, swclk_gpio_mode
);
520 SET_MODE_GPIO(swdio_gpio
, swdio_gpio_mode
);
522 SET_MODE_GPIO(trst_gpio
, trst_gpio_mode
);
524 SET_MODE_GPIO(srst_gpio
, srst_gpio_mode
);
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)