From 1fadd469b628e5857ff2e7a42c4c5aa394cd3229 Mon Sep 17 00:00:00 2001 From: "Bastien Curutchet (Schneider Electric)" Date: Fri, 25 Sep 2026 13:38:47 +0200 Subject: [PATCH 01/11] net: dsa: microchip: fully save the periodic output request When a periodic output is initialized, its period and start time are stored to initialize it back in case of restart. These information aren't enough if the request contains flags or targets a pin that isn't the first output of the switch. At restarts, these two arguments are always set to 0. The 'flag issue' isn't concrete so far because the driver doesn't advertise any .supported_perout_flags so any flag request gets rejected by the upper layer. I don't know if other switches use non-zero indexes but the KSZ8463 has twelve periodic outputs, so this index has to also be saved from now on. Save the full request when a periodic output is successfully started. Use the saved request when the periodic output is restarted. Signed-off-by: Bastien Curutchet (Schneider Electric) Link: https://patch.msgid.link/20260925-ksz-perout-v4-1-d675bd21ce49@bootlin.com Signed-off-by: Jakub Kicinski --- drivers/net/dsa/microchip/ksz_ptp.c | 33 ++++++++++++++++------------- drivers/net/dsa/microchip/ksz_ptp.h | 3 +-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c index 39cc70d65900..47cf397481d5 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.c +++ b/drivers/net/dsa/microchip/ksz_ptp.c @@ -189,6 +189,7 @@ static int ksz_ptp_enable_perout(struct ksz_device *dev, { struct ksz_ptp_data *ptp_data = &dev->ptp_data; u64 req_pulse_width_ns; + struct timespec64 tmp; u64 cycle_width_ns; u64 pulse_width_ns; int pin = 0; @@ -222,13 +223,9 @@ static int ksz_ptp_enable_perout(struct ksz_device *dev, return 0; } - ptp_data->perout_target_time_first.tv_sec = request->start.sec; - ptp_data->perout_target_time_first.tv_nsec = request->start.nsec; - - ptp_data->perout_period.tv_sec = request->period.sec; - ptp_data->perout_period.tv_nsec = request->period.nsec; - - cycle_width_ns = timespec64_to_ns(&ptp_data->perout_period); + tmp.tv_sec = request->period.sec; + tmp.tv_nsec = request->period.nsec; + cycle_width_ns = timespec64_to_ns(&tmp); if ((cycle_width_ns & TRIG_CYCLE_WIDTH_M) != cycle_width_ns) return -EINVAL; @@ -249,9 +246,10 @@ static int ksz_ptp_enable_perout(struct ksz_device *dev, if (ret) return ret; + tmp.tv_sec = request->start.sec; + tmp.tv_nsec = request->start.nsec; ret = ksz_ptp_configure_perout(dev, cycle_width_ns, pulse_width_ns, - &ptp_data->perout_target_time_first, - pin); + &tmp, pin); if (ret) return ret; @@ -263,6 +261,8 @@ static int ksz_ptp_enable_perout(struct ksz_device *dev, if (ret) return ret; + memcpy(&ptp_data->perout_request, request, + sizeof(struct ptp_perout_request)); ptp_data->tou_mode = KSZ_PTP_TOU_PEROUT; return 0; @@ -763,6 +763,7 @@ static int ksz_ptp_restart_perout(struct ksz_device *dev) struct ptp_perout_request request; struct timespec64 next; struct timespec64 now; + struct timespec64 tmp; unsigned int count; int ret; @@ -773,10 +774,14 @@ static int ksz_ptp_restart_perout(struct ksz_device *dev) return ret; now_ns = timespec64_to_ns(&now); - first_ns = timespec64_to_ns(&ptp_data->perout_target_time_first); + tmp.tv_sec = ptp_data->perout_request.start.sec; + tmp.tv_nsec = ptp_data->perout_request.start.nsec; + first_ns = timespec64_to_ns(&tmp); /* Calculate next perout event based on start time and period */ - period_ns = timespec64_to_ns(&ptp_data->perout_period); + tmp.tv_sec = ptp_data->perout_request.period.sec; + tmp.tv_nsec = ptp_data->perout_request.period.nsec; + period_ns = timespec64_to_ns(&tmp); if (first_ns < now_ns) { count = div_u64(now_ns - first_ns, period_ns); @@ -791,12 +796,10 @@ static int ksz_ptp_restart_perout(struct ksz_device *dev) /* Restart periodic output signal */ next = ns_to_timespec64(next_ns); + memcpy(&request, &ptp_data->perout_request, + sizeof(struct ptp_perout_request)); request.start.sec = next.tv_sec; request.start.nsec = next.tv_nsec; - request.period.sec = ptp_data->perout_period.tv_sec; - request.period.nsec = ptp_data->perout_period.tv_nsec; - request.index = 0; - request.flags = 0; return ksz_ptp_enable_perout(dev, &request, 1); } diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h index 7067ec9bd1e6..eb3827203b96 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.h +++ b/drivers/net/dsa/microchip/ksz_ptp.h @@ -29,8 +29,7 @@ struct ksz_ptp_data { spinlock_t clock_lock; struct timespec64 clock_time; enum ksz_ptp_tou_mode tou_mode; - struct timespec64 perout_target_time_first; /* start of first pulse */ - struct timespec64 perout_period; + struct ptp_perout_request perout_request; }; int ksz_ptp_clock_register(struct dsa_switch *ds); From 1599393bf3fff9394a360a77f3358df93362f7bb Mon Sep 17 00:00:00 2001 From: "Bastien Curutchet (Schneider Electric)" Date: Fri, 25 Sep 2026 13:38:48 +0200 Subject: [PATCH 02/11] net: dsa: microchip: add the number of pins to chip infos The number of periodic output pins available is hardcoded to 2 while KSZ8463 has 12 pins that can be used as periodic outputs. Add an n_pin attribute to the struct ksz_chip_data to make this setting configurable. Set it to 2 for all the PTP-capable switches. Signed-off-by: Bastien Curutchet (Schneider Electric) Link: https://patch.msgid.link/20260925-ksz-perout-v4-2-d675bd21ce49@bootlin.com Signed-off-by: Jakub Kicinski --- drivers/net/dsa/microchip/ksz_common.c | 10 ++++++++++ drivers/net/dsa/microchip/ksz_common.h | 1 + drivers/net/dsa/microchip/ksz_ptp.c | 4 ++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c index f1b53439ff58..ac9a10a91d79 100644 --- a/drivers/net/dsa/microchip/ksz_common.c +++ b/drivers/net/dsa/microchip/ksz_common.c @@ -1208,6 +1208,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .ptp_capable = true, .wr_table = &ksz8563_register_set, .rd_table = &ksz8563_register_set, + .n_pins = 2, }, [KSZ8795] = { @@ -1443,6 +1444,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .sgmii_port = 7, .wr_table = &ksz9477_register_set, .rd_table = &ksz9477_register_set, + .n_pins = 2, }, [KSZ9896] = { @@ -1608,6 +1610,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .internal_phy = {true, true, false}, .gbit_capable = {true, true, true}, .ptp_capable = true, + .n_pins = 2, }, [KSZ8567] = { @@ -1645,6 +1648,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .gbit_capable = {false, false, false, false, false, true, true}, .ptp_capable = true, + .n_pins = 2, }, [KSZ9567] = { @@ -1679,6 +1683,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { true, false, false}, .gbit_capable = {true, true, true, true, true, true, true}, .ptp_capable = true, + .n_pins = 2, }, [LAN9370] = { @@ -1710,6 +1715,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .supports_rgmii = {false, false, false, false, true}, .internal_phy = {true, true, true, true, false}, .ptp_capable = true, + .n_pins = 2, }, [LAN9371] = { @@ -1741,6 +1747,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .supports_rgmii = {false, false, false, false, true, true}, .internal_phy = {true, true, true, true, false, false}, .ptp_capable = true, + .n_pins = 2, }, [LAN9372] = { @@ -1776,6 +1783,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .internal_phy = {true, true, true, true, false, false, true, true}, .ptp_capable = true, + .n_pins = 2, }, [LAN9373] = { @@ -1811,6 +1819,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .internal_phy = {true, true, true, false, false, false, true, true}, .ptp_capable = true, + .n_pins = 2, }, [LAN9374] = { @@ -1846,6 +1855,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .internal_phy = {true, true, true, true, false, false, true, true}, .ptp_capable = true, + .n_pins = 2, }, [LAN9646] = { diff --git a/drivers/net/dsa/microchip/ksz_common.h b/drivers/net/dsa/microchip/ksz_common.h index 091cfbdc486f..9b911f9aafd9 100644 --- a/drivers/net/dsa/microchip/ksz_common.h +++ b/drivers/net/dsa/microchip/ksz_common.h @@ -139,6 +139,7 @@ struct ksz_chip_data { u8 sgmii_port; const struct regmap_access_table *wr_table; const struct regmap_access_table *rd_table; + const u8 n_pins; }; struct ksz_irq { diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c index 47cf397481d5..33416e59edc7 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.c +++ b/drivers/net/dsa/microchip/ksz_ptp.c @@ -1053,14 +1053,14 @@ int ksz_ptp_clock_register(struct dsa_switch *ds) ptp_data->caps.do_aux_work = ksz_ptp_do_aux_work; ptp_data->caps.enable = ksz_ptp_enable; ptp_data->caps.verify = ksz_ptp_verify_pin; - ptp_data->caps.n_pins = KSZ_PTP_N_GPIO; + ptp_data->caps.n_pins = dev->info->n_pins; ptp_data->caps.n_per_out = 3; ret = ksz_ptp_start_clock(dev); if (ret) return ret; - for (i = 0; i < KSZ_PTP_N_GPIO; i++) { + for (i = 0; i < dev->info->n_pins; i++) { struct ptp_pin_desc *ptp_pin = &ptp_data->pin_config[i]; snprintf(ptp_pin->name, From 7242c96652e3d2be2c9fe472f73f39c2c27bc221 Mon Sep 17 00:00:00 2001 From: "Bastien Curutchet (Schneider Electric)" Date: Fri, 25 Sep 2026 13:38:49 +0200 Subject: [PATCH 03/11] net: dsa: microchip: add the number of periodic signals to chip infos The number of periodic signals available is hardcoded to 3 while KSZ8463 can produce 12 different periodic signals. Add an n_per_out attribute to the struct ksz_chip_data to make this setting configurable. Set it to 3 for all the PTP-capable switches. Signed-off-by: Bastien Curutchet (Schneider Electric) Link: https://patch.msgid.link/20260925-ksz-perout-v4-3-d675bd21ce49@bootlin.com Signed-off-by: Jakub Kicinski --- drivers/net/dsa/microchip/ksz_common.c | 10 ++++++++++ drivers/net/dsa/microchip/ksz_common.h | 1 + drivers/net/dsa/microchip/ksz_ptp.c | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c index ac9a10a91d79..ea6db85e7e2b 100644 --- a/drivers/net/dsa/microchip/ksz_common.c +++ b/drivers/net/dsa/microchip/ksz_common.c @@ -1209,6 +1209,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .wr_table = &ksz8563_register_set, .rd_table = &ksz8563_register_set, .n_pins = 2, + .n_per_out = 3, }, [KSZ8795] = { @@ -1445,6 +1446,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .wr_table = &ksz9477_register_set, .rd_table = &ksz9477_register_set, .n_pins = 2, + .n_per_out = 3, }, [KSZ9896] = { @@ -1611,6 +1613,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .gbit_capable = {true, true, true}, .ptp_capable = true, .n_pins = 2, + .n_per_out = 3, }, [KSZ8567] = { @@ -1649,6 +1652,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { true, true}, .ptp_capable = true, .n_pins = 2, + .n_per_out = 3, }, [KSZ9567] = { @@ -1684,6 +1688,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .gbit_capable = {true, true, true, true, true, true, true}, .ptp_capable = true, .n_pins = 2, + .n_per_out = 3, }, [LAN9370] = { @@ -1716,6 +1721,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .internal_phy = {true, true, true, true, false}, .ptp_capable = true, .n_pins = 2, + .n_per_out = 3, }, [LAN9371] = { @@ -1748,6 +1754,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { .internal_phy = {true, true, true, true, false, false}, .ptp_capable = true, .n_pins = 2, + .n_per_out = 3, }, [LAN9372] = { @@ -1784,6 +1791,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { false, false, true, true}, .ptp_capable = true, .n_pins = 2, + .n_per_out = 3, }, [LAN9373] = { @@ -1820,6 +1828,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { false, false, true, true}, .ptp_capable = true, .n_pins = 2, + .n_per_out = 3, }, [LAN9374] = { @@ -1856,6 +1865,7 @@ const struct ksz_chip_data ksz_switch_chips[] = { false, false, true, true}, .ptp_capable = true, .n_pins = 2, + .n_per_out = 3, }, [LAN9646] = { diff --git a/drivers/net/dsa/microchip/ksz_common.h b/drivers/net/dsa/microchip/ksz_common.h index 9b911f9aafd9..f1c0e89e13c1 100644 --- a/drivers/net/dsa/microchip/ksz_common.h +++ b/drivers/net/dsa/microchip/ksz_common.h @@ -140,6 +140,7 @@ struct ksz_chip_data { const struct regmap_access_table *wr_table; const struct regmap_access_table *rd_table; const u8 n_pins; + const u8 n_per_out; }; struct ksz_irq { diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c index 33416e59edc7..84c799959aaa 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.c +++ b/drivers/net/dsa/microchip/ksz_ptp.c @@ -1054,7 +1054,7 @@ int ksz_ptp_clock_register(struct dsa_switch *ds) ptp_data->caps.enable = ksz_ptp_enable; ptp_data->caps.verify = ksz_ptp_verify_pin; ptp_data->caps.n_pins = dev->info->n_pins; - ptp_data->caps.n_per_out = 3; + ptp_data->caps.n_per_out = dev->info->n_per_out; ret = ksz_ptp_start_clock(dev); if (ret) From 3336fa2007184dc900fcedabca887137bbebc421 Mon Sep 17 00:00:00 2001 From: "Bastien Curutchet (Schneider Electric)" Date: Fri, 25 Sep 2026 13:38:50 +0200 Subject: [PATCH 04/11] net: dsa: microchip: use dynamic mask to check pulse width validity The pulse width validity is checked against an hardcoded mask. KSZ8463 uses a smaller register than other switches so this check won't work for it. Move the mask as a ksz_ptp_tou_pulse_verify()'s input to allow changing it for KSZ8463 case. Signed-off-by: Bastien Curutchet (Schneider Electric) Link: https://patch.msgid.link/20260925-ksz-perout-v4-4-d675bd21ce49@bootlin.com Signed-off-by: Jakub Kicinski --- drivers/net/dsa/microchip/ksz_ptp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c index 84c799959aaa..1889698691e9 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.c +++ b/drivers/net/dsa/microchip/ksz_ptp.c @@ -86,7 +86,7 @@ static int ksz_ptp_tou_reset(struct ksz_device *dev, u8 unit) 0); } -static int ksz_ptp_tou_pulse_verify(u64 pulse_ns) +static int ksz_ptp_tou_pulse_verify(u64 pulse_ns, u32 mask) { u32 data; @@ -94,7 +94,7 @@ static int ksz_ptp_tou_pulse_verify(u64 pulse_ns) return -EINVAL; data = (pulse_ns / 8); - if (!FIELD_FIT(TRIG_PULSE_WIDTH_M, data)) + if ((mask & data) != data) return -ERANGE; return 0; @@ -242,7 +242,7 @@ static int ksz_ptp_enable_perout(struct ksz_device *dev, KSZ_MAX_PULSE_WIDTH); } - ret = ksz_ptp_tou_pulse_verify(pulse_width_ns); + ret = ksz_ptp_tou_pulse_verify(pulse_width_ns, TRIG_PULSE_WIDTH_M); if (ret) return ret; From cfd0011cb5c2140cc06bcc04a477198a28ffa49e Mon Sep 17 00:00:00 2001 From: "Bastien Curutchet (Schneider Electric)" Date: Fri, 25 Sep 2026 13:38:51 +0200 Subject: [PATCH 05/11] net: dsa: microchip: extract PTP callbacks configuration from PTP registration KSZ8463 also has periodic output capabilities but the way its outputs are driven differs from the other switches. It will need its own set of PTP callbacks to implements this behaviour. Extract the PTP callbacks configuration in a dedicated function to be called before the PTP registration to ease the use of others callbacks when needed. Signed-off-by: Bastien Curutchet (Schneider Electric) Link: https://patch.msgid.link/20260925-ksz-perout-v4-5-d675bd21ce49@bootlin.com Signed-off-by: Jakub Kicinski --- drivers/net/dsa/microchip/ksz8.c | 2 ++ drivers/net/dsa/microchip/ksz9477.c | 1 + drivers/net/dsa/microchip/ksz_ptp.c | 20 ++++++++++++++------ drivers/net/dsa/microchip/ksz_ptp.h | 2 ++ drivers/net/dsa/microchip/lan937x_main.c | 1 + 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/drivers/net/dsa/microchip/ksz8.c b/drivers/net/dsa/microchip/ksz8.c index d7498132064e..37b681d9e20f 100644 --- a/drivers/net/dsa/microchip/ksz8.c +++ b/drivers/net/dsa/microchip/ksz8.c @@ -2589,6 +2589,7 @@ static int ksz8463_setup(struct dsa_switch *ds) if (ret) goto free_girq; + ksz_ptp_set_caps(ds); ret = ksz_ptp_clock_register(ds); if (ret) { dev_err(dev->dev, "Failed to register PTP clock: %d\n", @@ -2898,6 +2899,7 @@ static int ksz8_setup(struct dsa_switch *ds) } if (dev->info->ptp_capable) { + ksz_ptp_set_caps(ds); ret = ksz_ptp_clock_register(ds); if (ret) { dev_err(dev->dev, "Failed to register PTP clock: %d\n", diff --git a/drivers/net/dsa/microchip/ksz9477.c b/drivers/net/dsa/microchip/ksz9477.c index 3ee995545c57..72528a53b67d 100644 --- a/drivers/net/dsa/microchip/ksz9477.c +++ b/drivers/net/dsa/microchip/ksz9477.c @@ -1781,6 +1781,7 @@ static int ksz9477_setup(struct dsa_switch *ds) } if (dev->info->ptp_capable) { + ksz_ptp_set_caps(ds); ret = ksz_ptp_clock_register(ds); if (ret) { dev_err(dev->dev, "Failed to register PTP clock: %d\n", diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c index 1889698691e9..fd27e5851003 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.c +++ b/drivers/net/dsa/microchip/ksz_ptp.c @@ -1031,17 +1031,12 @@ static int ksz_ptp_start_clock(struct ksz_device *dev) return 0; } -int ksz_ptp_clock_register(struct dsa_switch *ds) +void ksz_ptp_set_caps(struct dsa_switch *ds) { struct ksz_device *dev = ds->priv; - const u16 *regs = dev->info->regs; struct ksz_ptp_data *ptp_data; - int ret; - u8 i; ptp_data = &dev->ptp_data; - mutex_init(&ptp_data->lock); - spin_lock_init(&ptp_data->clock_lock); ptp_data->caps.owner = THIS_MODULE; snprintf(ptp_data->caps.name, 16, "Microchip Clock"); @@ -1055,6 +1050,19 @@ int ksz_ptp_clock_register(struct dsa_switch *ds) ptp_data->caps.verify = ksz_ptp_verify_pin; ptp_data->caps.n_pins = dev->info->n_pins; ptp_data->caps.n_per_out = dev->info->n_per_out; +} + +int ksz_ptp_clock_register(struct dsa_switch *ds) +{ + struct ksz_device *dev = ds->priv; + const u16 *regs = dev->info->regs; + struct ksz_ptp_data *ptp_data; + int ret; + u8 i; + + ptp_data = &dev->ptp_data; + mutex_init(&ptp_data->lock); + spin_lock_init(&ptp_data->clock_lock); ret = ksz_ptp_start_clock(dev); if (ret) diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h index eb3827203b96..b23040f47b9c 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.h +++ b/drivers/net/dsa/microchip/ksz_ptp.h @@ -32,6 +32,7 @@ struct ksz_ptp_data { struct ptp_perout_request perout_request; }; +void ksz_ptp_set_caps(struct dsa_switch *ds); int ksz_ptp_clock_register(struct dsa_switch *ds); void ksz_ptp_clock_unregister(struct dsa_switch *ds); @@ -64,6 +65,7 @@ struct ksz_ptp_data { struct mutex lock; }; +static inline void ksz_ptp_set_caps(struct dsa_switch *ds) { } static inline int ksz_ptp_clock_register(struct dsa_switch *ds) { return 0; diff --git a/drivers/net/dsa/microchip/lan937x_main.c b/drivers/net/dsa/microchip/lan937x_main.c index 86ce3a86705f..3a209122fc7d 100644 --- a/drivers/net/dsa/microchip/lan937x_main.c +++ b/drivers/net/dsa/microchip/lan937x_main.c @@ -867,6 +867,7 @@ static int lan937x_setup(struct dsa_switch *ds) } } + ksz_ptp_set_caps(ds); ret = ksz_ptp_clock_register(ds); if (ret) { dev_err(dev->dev, "Failed to register PTP clock: %d\n", From 5b2bb536a3418fe29cd2e8e21eca3d83c833ca6b Mon Sep 17 00:00:00 2001 From: "Bastien Curutchet (Schneider Electric)" Date: Fri, 25 Sep 2026 13:38:52 +0200 Subject: [PATCH 06/11] net: dsa: microchip: extract ptp_get_pin The KSZ8463 supports periodic outputs but doesn't handle them in the same way as the other KSZ switches. To add proper support for the KSZ8463, a dedicated ksz8463_ptp_enable_perout() function needs to be created. This function will use the same algorithm to select the output pin as the common ksz_ptp_enable_perout() function. Extract the pin selection algorithm into a dedicated function so it can be used later by the KSZ8463 support. Signed-off-by: Bastien Curutchet (Schneider Electric) Link: https://patch.msgid.link/20260925-ksz-perout-v4-6-d675bd21ce49@bootlin.com Signed-off-by: Jakub Kicinski --- drivers/net/dsa/microchip/ksz_ptp.c | 31 ++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c index fd27e5851003..79520d345efc 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.c +++ b/drivers/net/dsa/microchip/ksz_ptp.c @@ -183,6 +183,26 @@ static int ksz_ptp_configure_perout(struct ksz_device *dev, return 0; } +static int ksz_ptp_get_pin(struct ksz_device *dev, + struct ptp_perout_request const *request) +{ + struct ksz_ptp_data *ptp_data = &dev->ptp_data; + int pin; + + if (request->flags & ~PTP_PEROUT_DUTY_CYCLE) + return -EOPNOTSUPP; + + if (ptp_data->tou_mode != KSZ_PTP_TOU_PEROUT && + ptp_data->tou_mode != KSZ_PTP_TOU_IDLE) + return -EBUSY; + + pin = ptp_find_pin(ptp_data->clock, PTP_PF_PEROUT, request->index); + if (pin < 0) + return -EINVAL; + + return pin; +} + static int ksz_ptp_enable_perout(struct ksz_device *dev, struct ptp_perout_request const *request, int on) @@ -196,16 +216,9 @@ static int ksz_ptp_enable_perout(struct ksz_device *dev, u32 data32; int ret; - if (request->flags & ~PTP_PEROUT_DUTY_CYCLE) - return -EOPNOTSUPP; - - if (ptp_data->tou_mode != KSZ_PTP_TOU_PEROUT && - ptp_data->tou_mode != KSZ_PTP_TOU_IDLE) - return -EBUSY; - - pin = ptp_find_pin(ptp_data->clock, PTP_PF_PEROUT, request->index); + pin = ksz_ptp_get_pin(dev, request); if (pin < 0) - return -EINVAL; + return pin; data32 = FIELD_PREP(PTP_GPIO_INDEX, pin) | FIELD_PREP(PTP_TOU_INDEX, request->index); From 8885b5406a58ad5e1bb1629a0e1e66aa26f3c818 Mon Sep 17 00:00:00 2001 From: "Bastien Curutchet (Schneider Electric)" Date: Fri, 25 Sep 2026 13:38:53 +0200 Subject: [PATCH 07/11] net: dsa: microchip: extract compute_width The KSZ8463 supports periodic outputs but doesn't handle them in the same way as the other KSZ switches. To add proper support for the KSZ8463, a dedicated ksz8463_ptp_enable_perout() function needs to be created. This function will use the same algorithm to compute the periodic cycles as the common ksz_ptp_enable_perout() function. Extract these algorithms into a dedicated function so they can be used later by the KSZ8463 support. Add some checks on the period to ensure the request is valid and will not cause divide by zero issues if the periodic output is restarted. Signed-off-by: Bastien Curutchet (Schneider Electric) Link: https://patch.msgid.link/20260925-ksz-perout-v4-7-d675bd21ce49@bootlin.com Signed-off-by: Jakub Kicinski --- drivers/net/dsa/microchip/ksz_ptp.c | 62 ++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c index 79520d345efc..4b57cf076bb4 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.c +++ b/drivers/net/dsa/microchip/ksz_ptp.c @@ -203,12 +203,50 @@ static int ksz_ptp_get_pin(struct ksz_device *dev, return pin; } +static int ksz_ptp_compute_perout_cycle(struct ksz_device *dev, + struct ptp_perout_request const *request, + u64 max_pulse_width, + u64 *cycle_width_ns, + u64 *pulse_width_ns) +{ + struct timespec64 tmp; + + if (request->period.sec < 0) + return -EINVAL; + + if (!request->period.sec && !request->period.nsec) + return -EINVAL; + + tmp.tv_sec = request->period.sec; + tmp.tv_nsec = request->period.nsec; + *cycle_width_ns = timespec64_to_ns(&tmp); + if ((*cycle_width_ns & TRIG_CYCLE_WIDTH_M) != *cycle_width_ns) { + *cycle_width_ns = 0; + *pulse_width_ns = 0; + return -EINVAL; + } + + if (request->flags & PTP_PEROUT_DUTY_CYCLE) { + *pulse_width_ns = request->on.sec * NSEC_PER_SEC + + request->on.nsec; + return 0; + } + + /* Use a duty cycle of 50%. Maximum pulse width supported by the + * hardware is a little bit more than 125 ms. + */ + *pulse_width_ns = (request->period.sec * NSEC_PER_SEC + + request->period.nsec) / 2; + *pulse_width_ns = min_t(u64, *pulse_width_ns, max_pulse_width); + + return 0; +} + static int ksz_ptp_enable_perout(struct ksz_device *dev, struct ptp_perout_request const *request, int on) { struct ksz_ptp_data *ptp_data = &dev->ptp_data; - u64 req_pulse_width_ns; struct timespec64 tmp; u64 cycle_width_ns; u64 pulse_width_ns; @@ -236,24 +274,10 @@ static int ksz_ptp_enable_perout(struct ksz_device *dev, return 0; } - tmp.tv_sec = request->period.sec; - tmp.tv_nsec = request->period.nsec; - cycle_width_ns = timespec64_to_ns(&tmp); - if ((cycle_width_ns & TRIG_CYCLE_WIDTH_M) != cycle_width_ns) - return -EINVAL; - - if (request->flags & PTP_PEROUT_DUTY_CYCLE) { - pulse_width_ns = request->on.sec * NSEC_PER_SEC + - request->on.nsec; - } else { - /* Use a duty cycle of 50%. Maximum pulse width supported by the - * hardware is a little bit more than 125 ms. - */ - req_pulse_width_ns = (request->period.sec * NSEC_PER_SEC + - request->period.nsec) / 2; - pulse_width_ns = min_t(u64, req_pulse_width_ns, - KSZ_MAX_PULSE_WIDTH); - } + ret = ksz_ptp_compute_perout_cycle(dev, request, KSZ_MAX_PULSE_WIDTH, + &cycle_width_ns, &pulse_width_ns); + if (ret) + return ret; ret = ksz_ptp_tou_pulse_verify(pulse_width_ns, TRIG_PULSE_WIDTH_M); if (ret) From ff327ef4488df10ba76a8f0e1f3711374baf3292 Mon Sep 17 00:00:00 2001 From: "Bastien Curutchet (Schneider Electric)" Date: Fri, 25 Sep 2026 13:38:54 +0200 Subject: [PATCH 08/11] net: dsa: microchip: extract prepare reset The KSZ8463 supports periodic outputs but doesn't handle them in the same way as the other KSZ switches. To add proper support for the KSZ8463, a dedicated ksz8463_ptp_restart_perout() function needs to be created. This function will use the same request initialization as the common ksz_ptp_restart_perout() function. Extract the inialization into a dedicated function so it can be used later by the KSZ8463 support. Signed-off-by: Bastien Curutchet (Schneider Electric) Link: https://patch.msgid.link/20260925-ksz-perout-v4-8-d675bd21ce49@bootlin.com Signed-off-by: Jakub Kicinski --- drivers/net/dsa/microchip/ksz_ptp.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c index 4b57cf076bb4..b627062dbaad 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.c +++ b/drivers/net/dsa/microchip/ksz_ptp.c @@ -793,11 +793,11 @@ static int ksz_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts) return ret; } -static int ksz_ptp_restart_perout(struct ksz_device *dev) +static int ksz_ptp_prepare_restart_perout(struct ksz_device *dev, + struct ptp_perout_request *request) { struct ksz_ptp_data *ptp_data = &dev->ptp_data; s64 now_ns, first_ns, period_ns, next_ns; - struct ptp_perout_request request; struct timespec64 next; struct timespec64 now; struct timespec64 tmp; @@ -833,10 +833,22 @@ static int ksz_ptp_restart_perout(struct ksz_device *dev) /* Restart periodic output signal */ next = ns_to_timespec64(next_ns); - memcpy(&request, &ptp_data->perout_request, + memcpy(request, &ptp_data->perout_request, sizeof(struct ptp_perout_request)); - request.start.sec = next.tv_sec; - request.start.nsec = next.tv_nsec; + request->start.sec = next.tv_sec; + request->start.nsec = next.tv_nsec; + + return 0; +} + +static int ksz_ptp_restart_perout(struct ksz_device *dev) +{ + struct ptp_perout_request request; + int ret; + + ret = ksz_ptp_prepare_restart_perout(dev, &request); + if (ret) + return ret; return ksz_ptp_enable_perout(dev, &request, 1); } From 7273205d31db8b66a4500b77afe8999bd104d483 Mon Sep 17 00:00:00 2001 From: "Bastien Curutchet (Schneider Electric)" Date: Fri, 25 Sep 2026 13:38:55 +0200 Subject: [PATCH 09/11] net: dsa: microchip: extract time update The KSZ8463 supports periodic outputs but doesn't handle them in the same way as the other KSZ switches. To add proper support for the KSZ8463, a dedicated ksz8463_ptp_settime() function needs to be created. This function will access the same registers than the common ksz_ptp_settime(). Extract the register accesses into a dedicated function so it can be used later by the KSZ8463 support. Signed-off-by: Bastien Curutchet (Schneider Electric) Link: https://patch.msgid.link/20260925-ksz-perout-v4-9-d675bd21ce49@bootlin.com Signed-off-by: Jakub Kicinski --- drivers/net/dsa/microchip/ksz_ptp.c | 42 ++++++++++++++++++----------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c index b627062dbaad..1244ef44df34 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.c +++ b/drivers/net/dsa/microchip/ksz_ptp.c @@ -853,30 +853,42 @@ static int ksz_ptp_restart_perout(struct ksz_device *dev) return ksz_ptp_enable_perout(dev, &request, 1); } +static int __ksz_ptp_settime(struct ksz_device *dev, + const struct timespec64 *ts) +{ + const u16 *regs = dev->info->regs; + int ret; + + /* Write to shadow registers and Load PTP clock */ + ret = ksz_write16(dev, regs[PTP_RTC_SUB_NANOSEC], PTP_RTC_0NS); + if (ret) + return ret; + + ret = ksz_write32(dev, regs[PTP_RTC_NANOSEC], ts->tv_nsec); + if (ret) + return ret; + + ret = ksz_write32(dev, regs[PTP_RTC_SEC], ts->tv_sec); + if (ret) + return ret; + + ret = ksz_rmw16(dev, regs[PTP_CLK_CTRL], PTP_LOAD_TIME, PTP_LOAD_TIME); + if (ret) + return ret; + + return 0; +} + static int ksz_ptp_settime(struct ptp_clock_info *ptp, const struct timespec64 *ts) { struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp); struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data); - const u16 *regs = dev->info->regs; int ret; mutex_lock(&ptp_data->lock); - /* Write to shadow registers and Load PTP clock */ - ret = ksz_write16(dev, regs[PTP_RTC_SUB_NANOSEC], PTP_RTC_0NS); - if (ret) - goto unlock; - - ret = ksz_write32(dev, regs[PTP_RTC_NANOSEC], ts->tv_nsec); - if (ret) - goto unlock; - - ret = ksz_write32(dev, regs[PTP_RTC_SEC], ts->tv_sec); - if (ret) - goto unlock; - - ret = ksz_rmw16(dev, regs[PTP_CLK_CTRL], PTP_LOAD_TIME, PTP_LOAD_TIME); + ret = __ksz_ptp_settime(dev, ts); if (ret) goto unlock; From a7cc3395d17e3ea9de18b9bf95392378d5eac98f Mon Sep 17 00:00:00 2001 From: "Bastien Curutchet (Schneider Electric)" Date: Fri, 25 Sep 2026 13:38:56 +0200 Subject: [PATCH 10/11] net: dsa: microchip: extract time adjustment The KSZ8463 supports periodic outputs but doesn't handle them in the same way as the other KSZ switches. To add proper support for the KSZ8463, a dedicated ksz8463_ptp_adjtime() function needs to be created. This function will access the same registers as the common ksz_ptp_adjtime() function. Extract the register accesses into a dedicated function so it can be used later by the KSZ8463 support. Signed-off-by: Bastien Curutchet (Schneider Electric) Link: https://patch.msgid.link/20260925-ksz-perout-v4-10-d675bd21ce49@bootlin.com Signed-off-by: Jakub Kicinski --- drivers/net/dsa/microchip/ksz_ptp.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c index 1244ef44df34..c1c4c781bca7 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.c +++ b/drivers/net/dsa/microchip/ksz_ptp.c @@ -954,18 +954,13 @@ static int ksz_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) return ret; } -static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) +static int __ksz_ptp_adjtime(struct ksz_device *dev, s64 delta) { - struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp); - struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data); - struct timespec64 delta64 = ns_to_timespec64(delta); const u16 *regs = dev->info->regs; s32 sec, nsec; u16 data16; int ret; - mutex_lock(&ptp_data->lock); - /* do not use ns_to_timespec64(), * both sec and nsec are subtracted by hw */ @@ -973,15 +968,15 @@ static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) ret = ksz_write32(dev, regs[PTP_RTC_NANOSEC], abs(nsec)); if (ret) - goto unlock; + return ret; ret = ksz_write32(dev, regs[PTP_RTC_SEC], abs(sec)); if (ret) - goto unlock; + return ret; ret = ksz_read16(dev, regs[PTP_CLK_CTRL], &data16); if (ret) - goto unlock; + return ret; data16 |= PTP_STEP_ADJ; @@ -992,6 +987,22 @@ static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) data16 |= PTP_STEP_DIR; ret = ksz_write16(dev, regs[PTP_CLK_CTRL], data16); + if (ret) + return ret; + + return 0; +} + +static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) +{ + struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp); + struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data); + struct timespec64 delta64 = ns_to_timespec64(delta); + int ret; + + mutex_lock(&ptp_data->lock); + + ret = __ksz_ptp_adjtime(dev, delta); if (ret) goto unlock; From 64854d9aab3a66ba2cffb87713ac157a297924bd Mon Sep 17 00:00:00 2001 From: "Bastien Curutchet (Schneider Electric)" Date: Fri, 25 Sep 2026 13:38:57 +0200 Subject: [PATCH 11/11] net: dsa: microchip: add periodic output support for the KSZ8463 KSZ8463 has several periodic outputs. The way it handles them differ from the other KSZ switches: - KSZ8463 has one set of registers for each of its 12 units while others switches share on set of configuration registers for all their units. - Maximum size of the KSZ8463 pulse width is smaller - KSZ8463 has 12 outputs while others only have 2 Add support for the KSZ8463 periodics outputs through a set of KSZ8463 specific functions. Signed-off-by: Bastien Curutchet (Schneider Electric) Link: https://patch.msgid.link/20260925-ksz-perout-v4-11-d675bd21ce49@bootlin.com Signed-off-by: Jakub Kicinski --- drivers/net/dsa/microchip/ksz8.c | 2 +- drivers/net/dsa/microchip/ksz_common.c | 2 + drivers/net/dsa/microchip/ksz_ptp.c | 261 ++++++++++++++++++++++++ drivers/net/dsa/microchip/ksz_ptp.h | 4 +- drivers/net/dsa/microchip/ksz_ptp_reg.h | 16 ++ 5 files changed, 283 insertions(+), 2 deletions(-) diff --git a/drivers/net/dsa/microchip/ksz8.c b/drivers/net/dsa/microchip/ksz8.c index 37b681d9e20f..3c96dd4581cf 100644 --- a/drivers/net/dsa/microchip/ksz8.c +++ b/drivers/net/dsa/microchip/ksz8.c @@ -2589,7 +2589,7 @@ static int ksz8463_setup(struct dsa_switch *ds) if (ret) goto free_girq; - ksz_ptp_set_caps(ds); + ksz8463_ptp_set_caps(ds); ret = ksz_ptp_clock_register(ds); if (ret) { dev_err(dev->dev, "Failed to register PTP clock: %d\n", diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c index ea6db85e7e2b..767c8fe8e819 100644 --- a/drivers/net/dsa/microchip/ksz_common.c +++ b/drivers/net/dsa/microchip/ksz_common.c @@ -1175,6 +1175,8 @@ const struct ksz_chip_data ksz_switch_chips[] = { .supports_mii = {false, false, true}, .supports_rmii = {false, false, true}, .internal_phy = {true, true, false}, + .n_pins = 12, + .n_per_out = 12, }, [KSZ8563] = { diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c index c1c4c781bca7..1c59428b159b 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.c +++ b/drivers/net/dsa/microchip/ksz_ptp.c @@ -26,6 +26,7 @@ */ #define KSZ_MAX_DRIFT_CORR 6249999 #define KSZ_MAX_PULSE_WIDTH 125000000LL +#define KSZ8463_MAX_PULSE_WIDTH 500000LL #define KSZ_PTP_INC_NS 40ULL /* HW clock is incremented every 40 ns (by 40) */ #define KSZ_PTP_SUBNS_BITS 32 @@ -63,6 +64,17 @@ static int ksz_ptp_tou_gpio(struct ksz_device *dev) LED_SRC_PTP_GPIO_1 | LED_SRC_PTP_GPIO_2); } +static int ksz8463_ptp_tou_reset(struct ksz_device *dev, u8 unit) +{ + int ret; + + ret = ksz_rmw16(dev, KSZ8463_TOU_SW_RST, BIT(unit), BIT(unit)); + if (ret) + return ret; + + return ksz_rmw16(dev, KSZ8463_TOU_SW_RST, BIT(unit), 0); +} + static int ksz_ptp_tou_reset(struct ksz_device *dev, u8 unit) { u32 data; @@ -120,6 +132,28 @@ static int ksz_ptp_tou_target_time_set(struct ksz_device *dev, return 0; } +static int ksz8463_ptp_tou_start(struct ksz_device *dev, u8 unit) +{ + u16 data; + int ret; + + ret = ksz_rmw16(dev, KSZ8463_TOU_EN, BIT(unit), BIT(unit)); + if (ret) + return ret; + + ret = ksz_read16(dev, KSZ8463_TOU_ACTIVE, &data); + if (ret) + return ret; + + if (!(data & BIT(unit))) { + dev_err(dev->dev, "%s: Trigger unit%d error!\n", __func__, + unit); + return -EIO; + } + + return 0; +} + static int ksz_ptp_tou_start(struct ksz_device *dev, u8 unit) { u32 data; @@ -147,6 +181,56 @@ static int ksz_ptp_tou_start(struct ksz_device *dev, u8 unit) return 0; } +static int ksz8463_ptp_configure_perout(struct ksz_device *dev, + struct ptp_perout_request const *request, + u32 cycle_width_ns, u32 pulse_width_ns, + u8 index) +{ + struct ptp_pin_desc *pin = &dev->ptp_data.pin_config[index]; + u16 cfg_base = KSZ8463_TRIG1_CFG + KSZ8463_TRIGN_CFG_SIZE * pin->chan; + u16 data; + int ret; + + /* Hardware has only 32 bit for the second field */ + if ((request->start.sec & 0xffffffff) != request->start.sec) + return -EINVAL; + + data = KSZ8463_NOTIFY_BIT | + FIELD_PREP(KSZ8463_PATTERN_M, TRIG_POS_PERIOD) | + pin->index; + ret = ksz_write16(dev, cfg_base + KSZ8463_PATTERN_OFF, data); + if (ret) + return ret; + + ret = ksz_write32(dev, cfg_base + KSZ8463_CYCLE_WIDTH_OFF, + cycle_width_ns); + if (ret) + return ret; + + /* Set cycle count 0 - Infinite */ + ret = ksz_write16(dev, cfg_base + KSZ8463_CYCLE_CNT_OFF, 0); + if (ret) + return ret; + + /* KSZ8463 uses a 8 ns unit value to compute the pulse width */ + data = (pulse_width_ns / 8); + ret = ksz_write16(dev, cfg_base + KSZ8463_PULSE_WIDTH_OFF, data); + if (ret) + return ret; + + ret = ksz_write32(dev, cfg_base + KSZ8463_TARGET_NSEC, + request->start.nsec); + if (ret) + return ret; + + ret = ksz_write32(dev, cfg_base + KSZ8463_TARGET_SEC, + request->start.sec); + if (ret) + return ret; + + return 0; +} + static int ksz_ptp_configure_perout(struct ksz_device *dev, u32 cycle_width_ns, u32 pulse_width_ns, struct timespec64 const *target_time, @@ -242,6 +326,61 @@ static int ksz_ptp_compute_perout_cycle(struct ksz_device *dev, return 0; } +static int ksz8463_ptp_enable_perout(struct ksz_device *dev, + struct ptp_perout_request const *request, + int on) +{ + struct ksz_ptp_data *ptp_data = &dev->ptp_data; + u64 cycle_width_ns; + u64 pulse_width_ns; + int pin; + int ret; + + pin = ksz_ptp_get_pin(dev, request); + if (pin < 0) + return pin; + + ret = ksz8463_ptp_tou_reset(dev, request->index); + if (ret) + return ret; + + if (!on) { + ptp_data->tou_mode = KSZ_PTP_TOU_IDLE; + return 0; + } + + ret = ksz_ptp_compute_perout_cycle(dev, request, + KSZ8463_MAX_PULSE_WIDTH, + &cycle_width_ns, &pulse_width_ns); + if (ret) + return ret; + + ret = ksz_ptp_tou_pulse_verify(pulse_width_ns, + KSZ8463_TRIG_PULSE_WIDTH_M); + if (ret) + return ret; + + ret = ksz8463_ptp_configure_perout(dev, request, + cycle_width_ns, pulse_width_ns, + pin); + if (ret) + return ret; + + ret = ksz_ptp_tou_gpio(dev); + if (ret) + return ret; + + ret = ksz8463_ptp_tou_start(dev, request->index); + if (ret) + return ret; + + memcpy(&ptp_data->perout_request, request, + sizeof(struct ptp_perout_request)); + ptp_data->tou_mode = KSZ_PTP_TOU_PEROUT; + + return 0; +} + static int ksz_ptp_enable_perout(struct ksz_device *dev, struct ptp_perout_request const *request, int on) @@ -853,6 +992,18 @@ static int ksz_ptp_restart_perout(struct ksz_device *dev) return ksz_ptp_enable_perout(dev, &request, 1); } +static int ksz8463_ptp_restart_perout(struct ksz_device *dev) +{ + struct ptp_perout_request request; + int ret; + + ret = ksz_ptp_prepare_restart_perout(dev, &request); + if (ret) + return ret; + + return ksz8463_ptp_enable_perout(dev, &request, 1); +} + static int __ksz_ptp_settime(struct ksz_device *dev, const struct timespec64 *ts) { @@ -879,6 +1030,41 @@ static int __ksz_ptp_settime(struct ksz_device *dev, return 0; } +static int ksz8463_ptp_settime(struct ptp_clock_info *ptp, + const struct timespec64 *ts) +{ + struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp); + struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data); + int ret; + + mutex_lock(&ptp_data->lock); + + ret = __ksz_ptp_settime(dev, ts); + if (ret) + goto unlock; + + switch (ptp_data->tou_mode) { + case KSZ_PTP_TOU_IDLE: + break; + + case KSZ_PTP_TOU_PEROUT: + ret = ksz8463_ptp_restart_perout(dev); + if (ret) + goto unlock; + + break; + } + + spin_lock_bh(&ptp_data->clock_lock); + ptp_data->clock_time = *ts; + spin_unlock_bh(&ptp_data->clock_lock); + +unlock: + mutex_unlock(&ptp_data->lock); + + return ret; +} + static int ksz_ptp_settime(struct ptp_clock_info *ptp, const struct timespec64 *ts) { @@ -993,6 +1179,40 @@ static int __ksz_ptp_adjtime(struct ksz_device *dev, s64 delta) return 0; } +static int ksz8463_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) +{ + struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp); + struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data); + struct timespec64 delta64 = ns_to_timespec64(delta); + int ret; + + mutex_lock(&ptp_data->lock); + + ret = __ksz_ptp_adjtime(dev, delta); + if (ret) + goto unlock; + + switch (ptp_data->tou_mode) { + case KSZ_PTP_TOU_IDLE: + break; + + case KSZ_PTP_TOU_PEROUT: + ret = ksz8463_ptp_restart_perout(dev); + if (ret) + goto unlock; + + break; + } + + spin_lock_bh(&ptp_data->clock_lock); + ptp_data->clock_time = timespec64_add(ptp_data->clock_time, delta64); + spin_unlock_bh(&ptp_data->clock_lock); + +unlock: + mutex_unlock(&ptp_data->lock); + return ret; +} + static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) { struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp); @@ -1027,6 +1247,26 @@ static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) return ret; } +static int ksz8463_ptp_enable(struct ptp_clock_info *ptp, + struct ptp_clock_request *req, int on) +{ + struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp); + struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data); + int ret; + + switch (req->type) { + case PTP_CLK_REQ_PEROUT: + mutex_lock(&ptp_data->lock); + ret = ksz8463_ptp_enable_perout(dev, &req->perout, on); + mutex_unlock(&ptp_data->lock); + break; + default: + return -EOPNOTSUPP; + } + + return ret; +} + static int ksz_ptp_enable(struct ptp_clock_info *ptp, struct ptp_clock_request *req, int on) { @@ -1103,6 +1343,27 @@ static int ksz_ptp_start_clock(struct ksz_device *dev) return 0; } +void ksz8463_ptp_set_caps(struct dsa_switch *ds) +{ + struct ksz_device *dev = ds->priv; + struct ksz_ptp_data *ptp_data; + + ptp_data = &dev->ptp_data; + + ptp_data->caps.owner = THIS_MODULE; + snprintf(ptp_data->caps.name, 16, "Microchip Clock"); + ptp_data->caps.max_adj = KSZ_MAX_DRIFT_CORR; + ptp_data->caps.gettime64 = ksz_ptp_gettime; + ptp_data->caps.settime64 = ksz8463_ptp_settime; + ptp_data->caps.adjfine = ksz_ptp_adjfine; + ptp_data->caps.adjtime = ksz8463_ptp_adjtime; + ptp_data->caps.do_aux_work = ksz_ptp_do_aux_work; + ptp_data->caps.enable = ksz8463_ptp_enable; + ptp_data->caps.verify = ksz_ptp_verify_pin; + ptp_data->caps.n_pins = dev->info->n_pins; + ptp_data->caps.n_per_out = dev->info->n_per_out; +} + void ksz_ptp_set_caps(struct dsa_switch *ds) { struct ksz_device *dev = ds->priv; diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h index b23040f47b9c..22898a22723c 100644 --- a/drivers/net/dsa/microchip/ksz_ptp.h +++ b/drivers/net/dsa/microchip/ksz_ptp.h @@ -12,7 +12,7 @@ #include -#define KSZ_PTP_N_GPIO 2 +#define KSZ_PTP_N_GPIO 12 enum ksz_ptp_tou_mode { KSZ_PTP_TOU_IDLE, @@ -33,6 +33,7 @@ struct ksz_ptp_data { }; void ksz_ptp_set_caps(struct dsa_switch *ds); +void ksz8463_ptp_set_caps(struct dsa_switch *ds); int ksz_ptp_clock_register(struct dsa_switch *ds); void ksz_ptp_clock_unregister(struct dsa_switch *ds); @@ -66,6 +67,7 @@ struct ksz_ptp_data { }; static inline void ksz_ptp_set_caps(struct dsa_switch *ds) { } +static inline void ksz8463_ptp_set_caps(struct dsa_switch *ds) { } static inline int ksz_ptp_clock_register(struct dsa_switch *ds) { return 0; diff --git a/drivers/net/dsa/microchip/ksz_ptp_reg.h b/drivers/net/dsa/microchip/ksz_ptp_reg.h index 65ea8577af75..16f117fa5d49 100644 --- a/drivers/net/dsa/microchip/ksz_ptp_reg.h +++ b/drivers/net/dsa/microchip/ksz_ptp_reg.h @@ -51,6 +51,22 @@ #define REG_PTP_UNIT_INDEX__4 0x0520 +#define KSZ8463_TOU_ACTIVE 0x202 +#define KSZ8463_TOU_EN 0x206 +#define KSZ8463_TOU_SW_RST 0x208 + +#define KSZ8463_TRIGN_CFG_SIZE 0x20 +#define KSZ8463_TRIG1_CFG 0x220 +#define KSZ8463_TARGET_NSEC 0x000 +#define KSZ8463_TARGET_SEC 0x004 +#define KSZ8463_PATTERN_OFF 0x008 +#define KSZ8463_NOTIFY_BIT BIT(8) +#define KSZ8463_PATTERN_M GENMASK(6, 4) +#define KSZ8463_PULSE_WIDTH_OFF 0x00A +#define KSZ8463_TRIG_PULSE_WIDTH_M GENMASK(15, 0) +#define KSZ8463_CYCLE_WIDTH_OFF 0x00C +#define KSZ8463_CYCLE_CNT_OFF 0x010 + #define PTP_GPIO_INDEX GENMASK(19, 16) #define PTP_TSI_INDEX BIT(8) #define PTP_TOU_INDEX GENMASK(1, 0)