X-Git-Url: https://review.openocd.org/gitweb?a=blobdiff_plain;f=src%2Fjtag%2Fdrivers%2Flinuxgpiod.c;h=0e96d82e5471ecb5c75768e289321b2fa029f79e;hb=eab88eadd7b828f9c07007a8ac91988c37b5d036;hp=42c8a31408d6c9af1b55c3b8407ee580cc314e2c;hpb=620211bec677306854786892dcc0b6876a46a057;p=openocd.git diff --git a/src/jtag/drivers/linuxgpiod.c b/src/jtag/drivers/linuxgpiod.c index 42c8a31408..0e96d82e54 100644 --- a/src/jtag/drivers/linuxgpiod.c +++ b/src/jtag/drivers/linuxgpiod.c @@ -27,10 +27,31 @@ static int trst_gpio = -1; static int srst_gpio = -1; static int swclk_gpio = -1; static int swdio_gpio = -1; +static int swdio_dir_gpio = -1; static int led_gpio = -1; static int gpiochip = -1; +static int tck_gpiochip = -1; +static int tms_gpiochip = -1; +static int tdi_gpiochip = -1; +static int tdo_gpiochip = -1; +static int trst_gpiochip = -1; +static int srst_gpiochip = -1; +static int swclk_gpiochip = -1; +static int swdio_gpiochip = -1; +static int swdio_dir_gpiochip = -1; +static int led_gpiochip = -1; + +static struct gpiod_chip *gpiod_chip_tck; +static struct gpiod_chip *gpiod_chip_tms; +static struct gpiod_chip *gpiod_chip_tdi; +static struct gpiod_chip *gpiod_chip_tdo; +static struct gpiod_chip *gpiod_chip_trst; +static struct gpiod_chip *gpiod_chip_srst; +static struct gpiod_chip *gpiod_chip_swclk; +static struct gpiod_chip *gpiod_chip_swdio; +static struct gpiod_chip *gpiod_chip_swdio_dir; +static struct gpiod_chip *gpiod_chip_led; -static struct gpiod_chip *gpiod_chip; static struct gpiod_line *gpiod_tck; static struct gpiod_line *gpiod_tms; static struct gpiod_line *gpiod_tdi; @@ -38,6 +59,7 @@ static struct gpiod_line *gpiod_tdo; static struct gpiod_line *gpiod_trst; static struct gpiod_line *gpiod_swclk; static struct gpiod_line *gpiod_swdio; +static struct gpiod_line *gpiod_swdio_dir; static struct gpiod_line *gpiod_srst; static struct gpiod_line *gpiod_led; @@ -45,6 +67,7 @@ static int last_swclk; static int last_swdio; static bool last_stored; static bool swdio_input; +static bool swdio_dir_is_active_high = true; /* Bitbang interface read of TDO */ static bb_value_t linuxgpiod_read(void) @@ -134,6 +157,11 @@ static void linuxgpiod_swdio_drive(bool is_output) gpiod_line_release(gpiod_swdio); if (is_output) { + if (gpiod_swdio_dir) { + retval = gpiod_line_set_value(gpiod_swdio_dir, swdio_dir_is_active_high ? 1 : 0); + if (retval < 0) + LOG_WARNING("Fail set swdio_dir"); + } retval = gpiod_line_request_output(gpiod_swdio, "OpenOCD", 1); if (retval < 0) LOG_WARNING("Fail request_output line swdio"); @@ -141,6 +169,11 @@ static void linuxgpiod_swdio_drive(bool is_output) retval = gpiod_line_request_input(gpiod_swdio, "OpenOCD"); if (retval < 0) LOG_WARNING("Fail request_input line swdio"); + if (gpiod_swdio_dir) { + retval = gpiod_line_set_value(gpiod_swdio_dir, swdio_dir_is_active_high ? 0 : 1); + if (retval < 0) + LOG_WARNING("Fail set swdio_dir"); + } } last_stored = false; @@ -255,30 +288,33 @@ static bool linuxgpiod_swd_mode_possible(void) return true; } -static inline void helper_release(struct gpiod_line *line) +static inline void helper_release(struct gpiod_line *line, struct gpiod_chip *chip) { if (line) gpiod_line_release(line); + if (chip) + gpiod_chip_close(chip); } static int linuxgpiod_quit(void) { - helper_release(gpiod_led); - helper_release(gpiod_srst); - helper_release(gpiod_swdio); - helper_release(gpiod_swclk); - helper_release(gpiod_trst); - helper_release(gpiod_tms); - helper_release(gpiod_tck); - helper_release(gpiod_tdi); - helper_release(gpiod_tdo); - - gpiod_chip_close(gpiod_chip); + helper_release(gpiod_led, gpiod_chip_led); + helper_release(gpiod_srst, gpiod_chip_srst); + helper_release(gpiod_swdio, gpiod_chip_swdio); + helper_release(gpiod_swdio_dir, gpiod_chip_swdio_dir); + helper_release(gpiod_swclk, gpiod_chip_swclk); + helper_release(gpiod_trst, gpiod_chip_trst); + helper_release(gpiod_tms, gpiod_chip_tms); + helper_release(gpiod_tck, gpiod_chip_tck); + helper_release(gpiod_tdi, gpiod_chip_tdi); + helper_release(gpiod_tdo, gpiod_chip_tdo); return ERROR_OK; } -static struct gpiod_line *helper_get_input_line(const char *label, unsigned int offset) +static struct gpiod_line *helper_get_line(const char *label, + struct gpiod_chip *gpiod_chip, unsigned int offset, + int val, int dir, int flags) { struct gpiod_line *line; int retval; @@ -289,33 +325,40 @@ static struct gpiod_line *helper_get_input_line(const char *label, unsigned int return NULL; } - retval = gpiod_line_request_input(line, "OpenOCD"); + struct gpiod_line_request_config config = { + .consumer = "OpenOCD", + .request_type = dir, + .flags = flags, + }; + + retval = gpiod_line_request(line, &config, val); if (retval < 0) { - LOG_ERROR("Error request_input line %s", label); + LOG_ERROR("Error requesting gpio line %s", label); return NULL; } return line; } -static struct gpiod_line *helper_get_output_line(const char *label, unsigned int offset, int val) +static struct gpiod_line *helper_get_input_line(const char *label, + struct gpiod_chip *gpiod_chip, unsigned int offset) { - struct gpiod_line *line; - int retval; - - line = gpiod_chip_get_line(gpiod_chip, offset); - if (!line) { - LOG_ERROR("Error get line %s", label); - return NULL; - } + return helper_get_line(label, gpiod_chip, offset, 0, + GPIOD_LINE_REQUEST_DIRECTION_INPUT, 0); +} - retval = gpiod_line_request_output(line, "OpenOCD", val); - if (retval < 0) { - LOG_ERROR("Error request_output line %s", label); - return NULL; - } +static struct gpiod_line *helper_get_output_line(const char *label, + struct gpiod_chip *gpiod_chip, unsigned int offset, int val) +{ + return helper_get_line(label, gpiod_chip, offset, val, + GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, 0); +} - return line; +static struct gpiod_line *helper_get_open_drain_output_line(const char *label, + struct gpiod_chip *gpiod_chip, unsigned int offset, int val) +{ + return helper_get_line(label, gpiod_chip, offset, val, + GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN); } static int linuxgpiod_init(void) @@ -324,12 +367,6 @@ static int linuxgpiod_init(void) bitbang_interface = &linuxgpiod_bitbang; - gpiod_chip = gpiod_chip_open_by_number(gpiochip); - if (!gpiod_chip) { - LOG_ERROR("Cannot open LinuxGPIOD gpiochip %d", gpiochip); - return ERROR_JTAG_INIT_FAILED; - } - /* * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high. @@ -342,24 +379,55 @@ static int linuxgpiod_init(void) goto out_error; } - gpiod_tdo = helper_get_input_line("tdo", tdo_gpio); + gpiod_chip_tdo = gpiod_chip_open_by_number(tdo_gpiochip); + if (!gpiod_chip_tdo) { + LOG_ERROR("Cannot open LinuxGPIOD tdo_gpiochip %d", tdo_gpiochip); + goto out_error; + } + gpiod_chip_tdi = gpiod_chip_open_by_number(tdi_gpiochip); + if (!gpiod_chip_tdi) { + LOG_ERROR("Cannot open LinuxGPIOD tdi_gpiochip %d", tdi_gpiochip); + goto out_error; + } + gpiod_chip_tck = gpiod_chip_open_by_number(tck_gpiochip); + if (!gpiod_chip_tck) { + LOG_ERROR("Cannot open LinuxGPIOD tck_gpiochip %d", tck_gpiochip); + goto out_error; + } + gpiod_chip_tms = gpiod_chip_open_by_number(tms_gpiochip); + if (!gpiod_chip_tms) { + LOG_ERROR("Cannot open LinuxGPIOD tms_gpiochip %d", tms_gpiochip); + goto out_error; + } + + gpiod_tdo = helper_get_input_line("tdo", gpiod_chip_tdo, tdo_gpio); if (!gpiod_tdo) goto out_error; - gpiod_tdi = helper_get_output_line("tdi", tdi_gpio, 0); + gpiod_tdi = helper_get_output_line("tdi", gpiod_chip_tdi, tdi_gpio, 0); if (!gpiod_tdi) goto out_error; - gpiod_tck = helper_get_output_line("tck", tck_gpio, 0); + gpiod_tck = helper_get_output_line("tck", gpiod_chip_tck, tck_gpio, 0); if (!gpiod_tck) goto out_error; - gpiod_tms = helper_get_output_line("tms", tms_gpio, 1); + gpiod_tms = helper_get_output_line("tms", gpiod_chip_tms, tms_gpio, 1); if (!gpiod_tms) goto out_error; if (is_gpio_valid(trst_gpio)) { - gpiod_trst = helper_get_output_line("trst", trst_gpio, 1); + gpiod_chip_trst = gpiod_chip_open_by_number(trst_gpiochip); + if (!gpiod_chip_trst) { + LOG_ERROR("Cannot open LinuxGPIOD trst_gpiochip %d", trst_gpiochip); + goto out_error; + } + + if (jtag_get_reset_config() & RESET_TRST_OPEN_DRAIN) + gpiod_trst = helper_get_open_drain_output_line("trst", gpiod_chip_trst, trst_gpio, 1); + else + gpiod_trst = helper_get_output_line("trst", gpiod_chip_trst, trst_gpio, 1); + if (!gpiod_trst) goto out_error; } @@ -371,23 +439,66 @@ static int linuxgpiod_init(void) goto out_error; } - gpiod_swclk = helper_get_output_line("swclk", swclk_gpio, 1); + gpiod_chip_swclk = gpiod_chip_open_by_number(swclk_gpiochip); + if (!gpiod_chip_swclk) { + LOG_ERROR("Cannot open LinuxGPIOD swclk_gpiochip %d", swclk_gpiochip); + goto out_error; + } + gpiod_chip_swdio = gpiod_chip_open_by_number(swdio_gpiochip); + if (!gpiod_chip_swdio) { + LOG_ERROR("Cannot open LinuxGPIOD swdio_gpiochip %d", swdio_gpiochip); + goto out_error; + } + + if (is_gpio_valid(swdio_dir_gpio)) { + gpiod_chip_swdio_dir = gpiod_chip_open_by_number(swdio_dir_gpiochip); + if (!gpiod_chip_swdio_dir) { + LOG_ERROR("Cannot open LinuxGPIOD swdio_dir_gpiochip %d", swdio_dir_gpiochip); + goto out_error; + } + } + + gpiod_swclk = helper_get_output_line("swclk", gpiod_chip_swclk, swclk_gpio, 1); if (!gpiod_swclk) goto out_error; - gpiod_swdio = helper_get_output_line("swdio", swdio_gpio, 1); + /* Set buffer direction before making SWDIO an output */ + if (is_gpio_valid(swdio_dir_gpio)) { + gpiod_swdio_dir = helper_get_output_line("swdio_dir", gpiod_chip_swdio_dir, swdio_dir_gpio, + swdio_dir_is_active_high ? 1 : 0); + if (!gpiod_swdio_dir) + goto out_error; + } + + gpiod_swdio = helper_get_output_line("swdio", gpiod_chip_swdio, swdio_gpio, 1); if (!gpiod_swdio) goto out_error; } if (is_gpio_valid(srst_gpio)) { - gpiod_srst = helper_get_output_line("srst", srst_gpio, 1); + gpiod_chip_srst = gpiod_chip_open_by_number(srst_gpiochip); + if (!gpiod_chip_srst) { + LOG_ERROR("Cannot open LinuxGPIOD srst_gpiochip %d", srst_gpiochip); + goto out_error; + } + + if (jtag_get_reset_config() & RESET_SRST_PUSH_PULL) + gpiod_srst = helper_get_output_line("srst", gpiod_chip_srst, srst_gpio, 1); + else + gpiod_srst = helper_get_open_drain_output_line("srst", gpiod_chip_srst, srst_gpio, 1); + if (!gpiod_srst) goto out_error; } if (is_gpio_valid(led_gpio)) { - gpiod_led = helper_get_output_line("led", led_gpio, 0); + gpiod_chip_led = gpiod_chip_open_by_number(led_gpiochip); + if (!gpiod_chip_led) { + LOG_ERROR("Cannot open LinuxGPIOD led_gpiochip %d", led_gpiochip); + goto out_error; + } + + gpiod_led = helper_get_output_line("led", gpiod_chip_led, led_gpio, 0); if (!gpiod_led) goto out_error; } @@ -400,6 +511,21 @@ out_error: return ERROR_JTAG_INIT_FAILED; } +COMMAND_HELPER(linuxgpiod_helper_gpionum, const char *name, int *chip, int *line) +{ + int i = 0; + if (CMD_ARGC > 2) + return ERROR_COMMAND_SYNTAX_ERROR; + if (CMD_ARGC == 2) { + COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], *chip); + i = 1; + } + if (CMD_ARGC > 0) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[i], *line); + command_print(CMD, "LinuxGPIOD %s: chip = %d, num = %d", name, *chip, *line); + return ERROR_OK; +} + COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionums) { if (CMD_ARGC == 4) { @@ -420,56 +546,38 @@ COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionums) COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tck) { - if (CMD_ARGC == 1) - COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio); - - command_print(CMD, "LinuxGPIOD num: tck = %d", tck_gpio); - return ERROR_OK; + return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "tck", &tck_gpiochip, + &tck_gpio); } COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tms) { - if (CMD_ARGC == 1) - COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio); - - command_print(CMD, "LinuxGPIOD num: tms = %d", tms_gpio); - return ERROR_OK; + return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "tms", &tms_gpiochip, + &tms_gpio); } COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdo) { - if (CMD_ARGC == 1) - COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio); - - command_print(CMD, "LinuxGPIOD num: tdo = %d", tdo_gpio); - return ERROR_OK; + return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "tdo", &tdo_gpiochip, + &tdo_gpio); } COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdi) { - if (CMD_ARGC == 1) - COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio); - - command_print(CMD, "LinuxGPIOD num: tdi = %d", tdi_gpio); - return ERROR_OK; + return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "tdi", &tdi_gpiochip, + &tdi_gpio); } COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_srst) { - if (CMD_ARGC == 1) - COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio); - - command_print(CMD, "LinuxGPIOD num: srst = %d", srst_gpio); - return ERROR_OK; + return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "srst", &srst_gpiochip, + &srst_gpio); } COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_trst) { - if (CMD_ARGC == 1) - COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio); - - command_print(CMD, "LinuxGPIOD num: trst = %d", trst_gpio); - return ERROR_OK; + return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "trst", &trst_gpiochip, + &trst_gpio); } COMMAND_HANDLER(linuxgpiod_handle_swd_gpionums) @@ -490,35 +598,43 @@ COMMAND_HANDLER(linuxgpiod_handle_swd_gpionums) COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swclk) { - if (CMD_ARGC == 1) - COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio); - - command_print(CMD, "LinuxGPIOD num: swclk = %d", swclk_gpio); - return ERROR_OK; + return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "swclk", &swclk_gpiochip, + &swclk_gpio); } COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio) { - if (CMD_ARGC == 1) - COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio); + return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "swdio", &swdio_gpiochip, + &swdio_gpio); +} - command_print(CMD, "LinuxGPIOD num: swdio = %d", swdio_gpio); - return ERROR_OK; +COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio_dir) +{ + return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "swdio_dir", &swdio_dir_gpiochip, + &swdio_dir_gpio); } COMMAND_HANDLER(linuxgpiod_handle_gpionum_led) { - if (CMD_ARGC == 1) - COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], led_gpio); - - command_print(CMD, "LinuxGPIOD num: led = %d", led_gpio); - return ERROR_OK; + return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "led", &led_gpiochip, + &led_gpio); } COMMAND_HANDLER(linuxgpiod_handle_gpiochip) { - if (CMD_ARGC == 1) + if (CMD_ARGC == 1) { COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], gpiochip); + tck_gpiochip = gpiochip; + tms_gpiochip = gpiochip; + tdi_gpiochip = gpiochip; + tdo_gpiochip = gpiochip; + trst_gpiochip = gpiochip; + srst_gpiochip = gpiochip; + swclk_gpiochip = gpiochip; + swdio_gpiochip = gpiochip; + swdio_dir_gpiochip = gpiochip; + led_gpiochip = gpiochip; + } command_print(CMD, "LinuxGPIOD gpiochip = %d", gpiochip); return ERROR_OK; @@ -536,43 +652,43 @@ static const struct command_registration linuxgpiod_subcommand_handlers[] = { .name = "tck_num", .handler = linuxgpiod_handle_jtag_gpionum_tck, .mode = COMMAND_CONFIG, - .help = "gpio number for tck.", - .usage = "tck", + .help = "gpio chip number (optional) and gpio number for tck.", + .usage = "[chip] tck", }, { .name = "tms_num", .handler = linuxgpiod_handle_jtag_gpionum_tms, .mode = COMMAND_CONFIG, - .help = "gpio number for tms.", - .usage = "tms", + .help = "gpio chip number (optional) and gpio number for tms.", + .usage = "[chip] tms", }, { .name = "tdo_num", .handler = linuxgpiod_handle_jtag_gpionum_tdo, .mode = COMMAND_CONFIG, - .help = "gpio number for tdo.", - .usage = "tdo", + .help = "gpio chip number (optional) and gpio number for tdo.", + .usage = "[chip] tdo", }, { .name = "tdi_num", .handler = linuxgpiod_handle_jtag_gpionum_tdi, .mode = COMMAND_CONFIG, - .help = "gpio number for tdi.", - .usage = "tdi", + .help = "gpio chip number (optional) and gpio number for tdi.", + .usage = "[chip] tdi", }, { .name = "srst_num", .handler = linuxgpiod_handle_jtag_gpionum_srst, .mode = COMMAND_CONFIG, - .help = "gpio number for srst.", - .usage = "srst", + .help = "gpio chip number (optional) and gpio number for srst.", + .usage = "[chip] srst", }, { .name = "trst_num", .handler = linuxgpiod_handle_jtag_gpionum_trst, .mode = COMMAND_CONFIG, - .help = "gpio number for trst.", - .usage = "trst", + .help = "gpio chip number (optional) and gpio number for trst.", + .usage = "[chip] trst", }, { .name = "swd_nums", @@ -585,22 +701,29 @@ static const struct command_registration linuxgpiod_subcommand_handlers[] = { .name = "swclk_num", .handler = linuxgpiod_handle_swd_gpionum_swclk, .mode = COMMAND_CONFIG, - .help = "gpio number for swclk.", - .usage = "swclk", + .help = "gpio chip number (optional) and gpio number for swclk.", + .usage = "[chip] swclk", }, { .name = "swdio_num", .handler = linuxgpiod_handle_swd_gpionum_swdio, .mode = COMMAND_CONFIG, - .help = "gpio number for swdio.", - .usage = "swdio", + .help = "gpio chip number (optional) and gpio number for swdio.", + .usage = "[chip] swdio", + }, + { + .name = "swdio_dir_num", + .handler = linuxgpiod_handle_swd_gpionum_swdio_dir, + .mode = COMMAND_CONFIG, + .help = "gpio chip number (optional) and gpio number for swdio_dir.", + .usage = "[chip] swdio_dir", }, { .name = "led_num", .handler = linuxgpiod_handle_gpionum_led, .mode = COMMAND_CONFIG, - .help = "gpio number for LED.", - .usage = "led", + .help = "gpio chip number (optional) and gpio number for LED.", + .usage = "[chip] led", }, { .name = "gpiochip",