2626#include <zephyr/logging/log.h>
2727LOG_MODULE_REGISTER (swdp , CONFIG_DP_DRIVER_LOG_LEVEL );
2828
29- #define CLOCK_DELAY ( swclk_freq , port_write_cycles ) \
30- ((CPU_CLOCK / 2 / swclk_freq) - port_write_cycles )
29+ #define MAX_SWJ_CLOCK ( delay_cycles , port_write_cycles ) \
30+ ((CPU_CLOCK / 2U) / (port_write_cycles + delay_cycles) )
3131
3232/*
3333 * Default SWCLK frequency in Hz.
3434 * sw_clock can be used to overwrite this default value.
3535 */
3636#define SWDP_DEFAULT_SWCLK_FREQUENCY 1000000U
3737
38+ #define DELAY_FAST_CYCLES 2U
3839#define DELAY_SLOW_CYCLES 3U
3940
4041struct sw_config {
@@ -528,14 +529,19 @@ static int sw_set_clock(const struct device *dev, const uint32_t clock)
528529 struct sw_cfg_data * sw_data = dev -> data ;
529530 uint32_t delay ;
530531
531- sw_data -> fast_clock = false;
532- delay = ((CPU_CLOCK / 2U ) + (clock - 1U )) / clock ;
533-
534- if (delay > config -> port_write_cycles ) {
535- delay -= config -> port_write_cycles ;
536- delay = (delay + (DELAY_SLOW_CYCLES - 1U )) / DELAY_SLOW_CYCLES ;
537- } else {
532+ if (clock >= MAX_SWJ_CLOCK (DELAY_FAST_CYCLES , config -> port_write_cycles )) {
533+ sw_data -> fast_clock = true;
538534 delay = 1U ;
535+ } else {
536+ sw_data -> fast_clock = false;
537+
538+ delay = ((CPU_CLOCK / 2U ) + (clock - 1U )) / clock ;
539+ if (delay > config -> port_write_cycles ) {
540+ delay -= config -> port_write_cycles ;
541+ delay = (delay + (DELAY_SLOW_CYCLES - 1U )) / DELAY_SLOW_CYCLES ;
542+ } else {
543+ delay = 1U ;
544+ }
539545 }
540546
541547 sw_data -> clock_delay = delay ;
@@ -562,112 +568,128 @@ static int sw_configure(const struct device *dev,
562568static int sw_port_on (const struct device * dev )
563569{
564570 const struct sw_config * config = dev -> config ;
565-
566- gpio_pin_set_dt (& config -> clk , 1 );
571+ int ret ;
567572
568573 if (config -> dnoe .port ) {
569- gpio_pin_set_dt (& config -> dnoe , 1 );
574+ ret = gpio_pin_configure_dt (& config -> dnoe , GPIO_OUTPUT_ACTIVE );
575+ if (ret ) {
576+ return ret ;
577+ }
570578 }
571579
572580 if (config -> dout .port ) {
573- gpio_pin_set_dt (& config -> dout , 1 );
574- } else {
575- int ret ;
576-
577- ret = gpio_pin_configure_dt (& config -> dio , GPIO_OUTPUT_ACTIVE );
581+ ret = gpio_pin_configure_dt (& config -> dout , GPIO_OUTPUT_ACTIVE );
578582 if (ret ) {
579583 return ret ;
580584 }
581585 }
582586
583- if (config -> noe .port ) {
584- gpio_pin_set_dt (& config -> noe , 1 );
585- }
586- if (config -> reset .port ) {
587- gpio_pin_set_dt (& config -> reset , 1 );
588- }
589-
590- return 0 ;
591- }
592-
593- static int sw_port_off (const struct device * dev )
594- {
595- const struct sw_config * config = dev -> config ;
596-
597- if (config -> dnoe .port ) {
598- gpio_pin_set_dt (& config -> dnoe , 0 );
587+ ret = gpio_pin_configure_dt (& config -> dio , GPIO_INPUT );
588+ if (ret ) {
589+ return ret ;
599590 }
600591
601- if (config -> dout .port ) {
602- gpio_pin_set_dt (& config -> dout , 0 );
603- } else {
604- int ret ;
605-
606- ret = gpio_pin_configure_dt (& config -> dio , GPIO_INPUT );
592+ if (config -> noe .port ) {
593+ ret = gpio_pin_configure_dt (& config -> noe , GPIO_OUTPUT_ACTIVE );
607594 if (ret ) {
608595 return ret ;
609596 }
610597 }
611598
612- if (config -> noe .port ) {
613- gpio_pin_set_dt (& config -> noe , 0 );
599+ ret = gpio_pin_configure_dt (& config -> clk , GPIO_OUTPUT_ACTIVE );
600+ if (ret ) {
601+ return ret ;
614602 }
615- if (config -> reset .port ) {
616- gpio_pin_set_dt (& config -> reset , 1 );
603+
604+ ret = gpio_pin_configure_dt (& config -> reset , GPIO_OUTPUT_ACTIVE );
605+ if (ret ) {
606+ return ret ;
617607 }
618608
619609 return 0 ;
620610}
621611
622- static int sw_gpio_init (const struct device * dev )
612+ static int sw_port_off (const struct device * dev )
623613{
624614 const struct sw_config * config = dev -> config ;
625- struct sw_cfg_data * sw_data = dev -> data ;
626615 int ret ;
627616
628- ret = gpio_pin_configure_dt (& config -> clk , GPIO_OUTPUT_ACTIVE );
629- if (ret ) {
630- return ret ;
631- }
617+ /* If there is a transceiver connected to IO, pins should always be driven. */
618+ if (config -> dnoe .port ) {
619+ ret = gpio_pin_configure_dt (& config -> dnoe , GPIO_OUTPUT_INACTIVE );
620+ if (ret ) {
621+ return ret ;
622+ }
632623
633- ret = gpio_pin_configure_dt (& config -> dio , GPIO_INPUT );
634- if (ret ) {
635- return ret ;
636- }
624+ if (config -> dout .port ) {
625+ ret = gpio_pin_configure_dt (& config -> dout , GPIO_OUTPUT_ACTIVE );
626+ if (ret ) {
627+ return ret ;
628+ }
629+ }
637630
638- if (config -> dout .port ) {
639- ret = gpio_pin_configure_dt (& config -> dout , GPIO_OUTPUT_ACTIVE );
631+ ret = gpio_pin_configure_dt (& config -> dio , GPIO_INPUT );
640632 if (ret ) {
641633 return ret ;
642634 }
643- }
635+ } else {
636+ if (config -> dout .port ) {
637+ ret = gpio_pin_configure_dt (& config -> dout , GPIO_DISCONNECTED );
638+ if (ret ) {
639+ return ret ;
640+ }
641+ }
644642
645- if (config -> dnoe .port ) {
646- ret = gpio_pin_configure_dt (& config -> dnoe , GPIO_OUTPUT_INACTIVE );
643+ ret = gpio_pin_configure_dt (& config -> dio , GPIO_DISCONNECTED );
647644 if (ret ) {
648645 return ret ;
649646 }
650647 }
651648
649+ /* If there is a transceiver connected to CLK, pins should always be driven. */
652650 if (config -> noe .port ) {
653651 ret = gpio_pin_configure_dt (& config -> noe , GPIO_OUTPUT_INACTIVE );
654652 if (ret ) {
655653 return ret ;
656654 }
655+
656+ ret = gpio_pin_configure_dt (& config -> clk , GPIO_OUTPUT_ACTIVE );
657+ if (ret ) {
658+ return ret ;
659+ }
660+
661+ } else {
662+ ret = gpio_pin_configure_dt (& config -> clk , GPIO_DISCONNECTED );
663+ if (ret ) {
664+ return ret ;
665+ }
657666 }
658667
659668 if (config -> reset .port ) {
660- ret = gpio_pin_configure_dt (& config -> reset , GPIO_OUTPUT_ACTIVE );
669+ ret = gpio_pin_configure_dt (& config -> reset , GPIO_DISCONNECTED );
661670 if (ret ) {
662671 return ret ;
663672 }
664673 }
665674
675+ return 0 ;
676+ }
677+
678+ static int sw_gpio_init (const struct device * dev )
679+ {
680+ struct sw_cfg_data * sw_data = dev -> data ;
681+ int ret ;
682+
683+ /* start with the port turned off */
684+ ret = sw_port_off (dev );
685+ if (ret ) {
686+ return ret ;
687+ }
688+
666689 sw_data -> turnaround = 1U ;
667690 sw_data -> data_phase = false;
668691 sw_data -> fast_clock = false;
669- sw_data -> clock_delay = CLOCK_DELAY (SWDP_DEFAULT_SWCLK_FREQUENCY ,
670- config -> port_write_cycles );
692+ sw_set_clock (dev , SWDP_DEFAULT_SWCLK_FREQUENCY );
671693
672694 return 0 ;
673695}
0 commit comments