net: dsa: microchip: PTP support for KSZ956x
Subsystems (6)
v5 cover net: dsa: microchip: PTP support for KSZ956x
[1]
http://ww1.microchip.com/downloads/en/DeviceDoc/KSZ9563R-Data-Sheet-DS00002419D.pdf
Changes from v4 --> v5
------------------------
[8/9] - Fix compile error reported by kernel test robot
(NET_DSA_TAG_KSZ must select NET_PTP_CLASSIFY)
Changes from v3 --> v4
------------------------
The first 2 patches of v3 have been applied.
[ 5/12]-->[ 3/9] - Reviewed-by: Vladimir Oltean <[email protected]>
[ 6/12]-->[ 4/9] - s/low active/active low/
- Reviewed-by: Vladimir Oltean <[email protected]>
[ 7/12]-->[ 5/9] - Reviewed-by: Vladimir Oltean <[email protected]>
[ 9/12]-->[ 7/9] - Remove useless case statement
- Reviewed-by: Vladimir Oltean <[email protected]>
[10/12]-->[ 8/9] - s/low active/active low/
- 80 chars per line
- Use IEEE 802.1AS mode (to suppress forwarding of PDelay messages)
- Enable/disable hardware timestaping at runtime (port_hwtstamp_set)
- Use mutex in port_hwtstamp_set
- Don't use port specific struct hwtstamp_config
- removed #ifdefs from tag_ksz.c
- Set port's tx_latency and rx_latency to 0
- added include/linux/dsa/ksz_common.h to MAINTAINERS
[11/12] - removed Patch 11/12 (PPS support)
[12/12]-->[ 9/9] - 80 chars per line
- reverse christmas tree
- Set default pulse width for perout pulse to 50% (max. 125ms)
- reject unsupported flags for perout_request
Changes from v2 --> v3
------------------------
Applied all changes requested by Vladimir Oltean. v3 depends on my other netdev patches from 2020-11-18:
- net: ptp: introduce common defines for PTP message types
- net: dsa: avoid potential use-after-free error
[1/11]-->[1/12] - dts: remove " OR BSD-2-Clause" from SPDX-License-Identifier
- dts: add "additionalProperties"
- dts: remove quotes
[2/11]-->[2/12] - Reviewed-by: Vladimir Oltean <[email protected]> [3/11] - [Patch removed] (split ksz_common.h) [4/11]-->[3/12] - Reviewed-by: Vladimir Oltean <[email protected]>
- fixed summary
[5/11]-->[4/12] - Use "interrupts-extended" syntax [6/11]-->[5+6/12] - Split up patch
- style fixes
- use GENMASK()
- IRQF_ONESHOT|IRQF_SHARED
[7/11]-->[7/12] - Remove "default n" from Kconfig
- use mutex in adjfine()
- style fixes
[8/11]-->[8/12] - Be more verbose in commit message
- Rename helper
- provide correction instead of t2
- simplify location of UDP header
[9/11]-->[9+10/12] - Split up patch
- Update commmit messages
- don't use OR operator on irqreturn_t
- spin_lock_irqsave() --> spin_lock_bh()
- style fixes
- remove rx_filter cases for DELAY_REQ
- use new PTP_MSGTYPE_* defines
- inline ksz9477_ptp_should_tstamp()
- ksz9477_tstamp_to_clock() --> ksz9477_tstamp_reconstruct()
- use shared data in include/linux/net/dsa/ksz_common.h
- wait for tx time stamp (within sleepable context)
- use boolean for tx time stamp enable
- move t2 from correction to tail tag (again)
- ...
Changes from RFC --> v2
------------------------
I think that all open questions regarding the RFC version could be solved.
dts: referenced to dsa.yaml
dts: changed node name to "switch" in example
dts: changed "ports" subnode to "ethernet-ports"ksz_common: support "ethernet-ports" subnodetag_ksz: fix usage of correction field (32 bit ns + 16 bit sub-ns)tag_ksz: use cached PTP header from device's .port_txtstamp functiontag_ksz: refactored ksz9477_tstamp_to_clock()tag_ksz: pdelay_req: only subtract 2 bit seconds from the correction fieldtag_ksz: pdelay_resp: don't move (negative) correction to the egress tail tagptp_classify: add ptp_onestep_p2p_move_t2_to_correction helperksz9477_ptp: removed E2E support (as suggested by Vladimir)ksz9477_ptp: removed master/slave sysfs attributes (nacked by Richard)ksz9477_ptp: refactored ksz9477_ptp_port_txtstampksz9477_ptp: removed "pulse" attribute
kconfig: depend on PTP_1588_CLOCK (instead of "imply")
PATCH v5 1/9 net: dsa: microchip: rename ksz9477.c to ksz9477_main.c
PTP functionality will be built into a separate source file (ksz9477_ptp.c).
---
drivers/net/dsa/microchip/Makefile | 1 +
drivers/net/dsa/microchip/{ksz9477.c => ksz9477_main.c} | 0
2 files changed, 1 insertion(+)
rename drivers/net/dsa/microchip/{ksz9477.c => ksz9477_main.c} (100%)drivers/net/dsa/microchip/Makefile +1 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON) += ksz_common.o obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477) += ksz9477.o +ksz9477-objs := ksz9477_main.o obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_I2C) += ksz9477_i2c.o obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_SPI) += ksz9477_spi.o obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ8795) += ksz8795.o
drivers/net/dsa/microchip/ksz9477_main.c renamed from drivers/net/dsa/microchip/ksz9477.c +0 -0
Signature · 8 lines
Christian Eggers Embedded software developer Arnold & Richter Cine Technik GmbH & Co. Betriebs KG Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918 Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477 Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler
PATCH v5 2/9 dt-bindings: net: dsa: microchip,ksz: add interrupt property
In reply to Christian EggersThe devices have an optional interrupt line.
--- .../devicetree/bindings/net/dsa/microchip,ksz.yaml | 7 +++++++ 1 file changed, 7 insertions(+)
Documentation/devicetree/bindings/net/dsa/microchip,ksz.yaml +7 -0
@@ -35,6 +35,11 @@ properties: Should be a gpio specifier for a reset line. maxItems: 1 + interrupts: + description: + Interrupt specifier for the INTRP_N line from the device. + maxItems: 1 + microchip,synclko-125: $ref: /schemas/types.yaml#/definitions/flag description:
@@ -49,6 +54,7 @@ unevaluatedProperties: false examples: - | #include <dt-bindings/gpio/gpio.h> + #include <dt-bindings/interrupt-controller/irq.h> // Ethernet switch connected via SPI to the host, CPU port wired to eth0: eth0 {
@@ -70,6 +76,7 @@ examples: compatible = "microchip,ksz9477"; reg = <0>; reset-gpios = <&gpio5 0 GPIO_ACTIVE_LOW>; + interrupts-extended = <&gpio5 1 IRQ_TYPE_LEVEL_LOW>; /* INTRP_N line */ spi-max-frequency = <44000000>;
Signature · 8 lines
Christian Eggers Embedded software developer Arnold & Richter Cine Technik GmbH & Co. Betriebs KG Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918 Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477 Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler
Re: [PATCH net-next v5 2/9] dt-bindings: net: dsa: microchip,ksz: add interrupt property
Quoted 7 lines · Christian Eggers
The devices have an optional interrupt line.
Signed-off-by Christian Eggers <[email protected]>--- .../devicetree/bindings/net/dsa/microchip,ksz.yaml | 7 +++++++ 1 file changed, 7 insertions(+)
PATCH v5 3/9 net: dsa: microchip: ksz9477: move chip reset to ksz9477_switch_init()
In reply to Christian EggersThe next patch will add basic interrupt support. Chip reset must be performed before requesting the IRQ, so move this from ksz9477_setup() to ksz9477_init().
--- drivers/net/dsa/microchip/ksz9477_main.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/net/dsa/microchip/ksz9477_main.c +7 -8
@@ -1343,19 +1343,12 @@ static void ksz9477_config_cpu_port(struct dsa_switch *ds)static int ksz9477_setup (struct dsa_switch *ds) {struct ksz_device *dev = ds->priv; -int ret =0 ; dev->vlan_cache =devm_kcalloc (dev->dev,sizeof (struct vlan_table ), dev->num_vlans,GFP_KERNEL );if (!dev->vlan_cache)return -ENOMEM ; - ret =ksz9477_reset_switch (dev); -if (ret) { -dev_err (ds->dev,"failed to reset switch\n" ); -return ret; - } -/* Required for port partitioning. */ ksz9477_cfg32 (dev,REG_SW_QM_CTRL__4 ,UNICAST_VLAN_BOUNDARY , true);
@@ -1533,10 +1526,16 @@ static const struct ksz_chip_data ksz9477_switch_chips[] = {static int ksz9477_switch_init (struct ksz_device *dev) { -int i; +int i, ret; dev->ds->ops = &ksz9477_switch_ops; + ret =ksz9477_reset_switch (dev); +if (ret) { +dev_err (dev->dev,"failed to reset switch\n" ); +return ret; + } +for (i =0 ; i <ARRAY_SIZE (ksz9477_switch_chips); i++) {const struct ksz_chip_data *chip = &ksz9477_switch_chips[i];
Signature · 8 lines
Christian Eggers Embedded software developer Arnold & Richter Cine Technik GmbH & Co. Betriebs KG Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918 Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477 Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler
PATCH v5 4/9 net: dsa: microchip: ksz9477: basic interrupt support
In reply to Christian EggersInterrupts are required for TX time stamping. Probably they could also be used for PHY connection status.
This patch only adds the basic infrastructure for interrupts, no interrupts are finally enabled nor handled.
---
Changes in v4:
--------------
- s/low active/active low/
- Reviewed-by: Vladimir Oltean <[email protected]>
drivers/net/dsa/microchip/ksz9477_i2c.c | 2 + drivers/net/dsa/microchip/ksz9477_main.c | 68 ++++++++++++++++++++++++ drivers/net/dsa/microchip/ksz9477_spi.c | 2 + drivers/net/dsa/microchip/ksz_common.h | 1 + 4 files changed, 73 insertions(+)
drivers/net/dsa/microchip/ksz9477_i2c.c +2 -0
@@ -41,6 +41,8 @@ static int ksz9477_i2c_probe(struct i2c_client *i2c,if (i2c->dev.platform_data) dev->pdata = i2c->dev.platform_data; + dev->irq = i2c->irq; + ret =ksz9477_switch_register (dev);/* Main DSA driver may not be started yet. */
drivers/net/dsa/microchip/ksz9477_main.c +68 -0
95 lines not on this page · raw message
drivers/net/dsa/microchip/ksz9477_spi.c +2 -0
@@ -48,6 +48,8 @@ static int ksz9477_spi_probe(struct spi_device *spi)if (spi->dev.platform_data) dev->pdata = spi->dev.platform_data; + dev->irq = spi->irq; +/* setup spi */ spi->mode =SPI_MODE_3 ; ret =spi_setup (spi);
drivers/net/dsa/microchip/ksz_common.h +1 -0
@@ -55,6 +55,7 @@ struct ksz_device {struct device *dev;struct regmap *regmap[3 ]; +int irq;void *priv;
Signature · 8 lines
Christian Eggers Embedded software developer Arnold & Richter Cine Technik GmbH & Co. Betriebs KG Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918 Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477 Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler
PATCH v5 5/9 net: dsa: microchip: ksz9477: add Posix clock support for chip PTP clock
In reply to Christian EggersImplement routines (adjfine, adjtime, gettime and settime) for manipulating the chip's PTP clock.
--- drivers/net/dsa/microchip/Kconfig | 8 + drivers/net/dsa/microchip/Makefile | 1 + drivers/net/dsa/microchip/ksz9477_i2c.c | 2 +- drivers/net/dsa/microchip/ksz9477_main.c | 17 ++ drivers/net/dsa/microchip/ksz9477_ptp.c | 308 +++++++++++++++++++++++ drivers/net/dsa/microchip/ksz9477_ptp.h | 27 ++ drivers/net/dsa/microchip/ksz9477_spi.c | 2 +- drivers/net/dsa/microchip/ksz_common.h | 8 + 8 files changed, 371 insertions(+), 2 deletions(-) create mode 100644 drivers/net/dsa/microchip/ksz9477_ptp.c create mode 100644 drivers/net/dsa/microchip/ksz9477_ptp.h
drivers/net/dsa/microchip/Kconfig +8 -0
@@ -24,6 +24,14 @@ config NET_DSA_MICROCHIP_KSZ9477_SPI help Select to enable support for registering switches configured through SPI. +config NET_DSA_MICROCHIP_KSZ9477_PTP + bool "PTP support for Microchip KSZ9477 series" + depends on NET_DSA_MICROCHIP_KSZ9477 + depends on PTP_1588_CLOCK + help + Say Y to enable PTP hardware timestamping on Microchip KSZ switch + chips that support it. + menuconfig NET_DSA_MICROCHIP_KSZ8795 tristate "Microchip KSZ8795 series switch support" depends on NET_DSA
drivers/net/dsa/microchip/Makefile +1 -0
@@ -2,6 +2,7 @@ obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON) += ksz_common.o obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477) += ksz9477.o ksz9477-objs := ksz9477_main.o +ksz9477-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_PTP) += ksz9477_ptp.o obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_I2C) += ksz9477_i2c.o obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_SPI) += ksz9477_spi.o obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ8795) += ksz8795.o
drivers/net/dsa/microchip/ksz9477_i2c.c +1 -1
@@ -58,7 +58,7 @@ static int ksz9477_i2c_remove(struct i2c_client *i2c) {struct ksz_device *dev =i2c_get_clientdata (i2c); -ksz_switch_remove (dev); +ksz9477_switch_remove (dev);return 0 ; }
drivers/net/dsa/microchip/ksz9477_main.c +17 -0
@@ -19,6 +19,7 @@#include "ksz9477_reg.h" #include "ksz_common.h" +#include "ksz9477_ptp.h" /* Used with variable features to indicate capabilities. */ #define GBIT_SUPPORT BIT(0)
@@ -1695,10 +1696,26 @@ int ksz9477_switch_register(struct ksz_device *dev)phy_remove_link_mode (phydev, ETHTOOL_LINK_MODE_1000baseT_Full_BIT); } + + ret =ksz9477_ptp_init (dev); +if (ret) + goto error_switch_unregister; + +return 0 ; + +error_switch_unregister: +ksz_switch_remove (dev);return ret; }EXPORT_SYMBOL (ksz9477_switch_register); +void ksz9477_switch_remove (struct ksz_device *dev) +{ +ksz9477_ptp_deinit (dev); +ksz_switch_remove (dev); +} +EXPORT_SYMBOL (ksz9477_switch_remove); +MODULE_AUTHOR ("Woojung Huh <[email protected]>" );MODULE_DESCRIPTION ("Microchip KSZ9477 Series Switch DSA Driver" );MODULE_LICENSE ("GPL" );
drivers/net/dsa/microchip/ksz9477_ptp.c new file +308 -0
308 lines not on this page · raw message
drivers/net/dsa/microchip/ksz9477_ptp.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 +* +* Microchip KSZ9477 switch driver PTP routines +* +* Author: Christian Eggers <[email protected]> +* +* Copyright (c) 2020 ARRI Lighting +*/ + +#ifndef DRIVERS_NET_DSA_MICROCHIP_KSZ9477_PTP_H_ +#define DRIVERS_NET_DSA_MICROCHIP_KSZ9477_PTP_H_ + +#include "ksz_common.h" + +#if IS_ENABLED (CONFIG_NET_DSA_MICROCHIP_KSZ9477_PTP ) + +int ksz9477_ptp_init (struct ksz_device *dev); +void ksz9477_ptp_deinit (struct ksz_device *dev); + +#else + +static inline int ksz9477_ptp_init (struct ksz_device *dev) {return 0 ; } +static inline void ksz9477_ptp_deinit (struct ksz_device *dev) {} + +#endif + +#endif /* DRIVERS_NET_DSA_MICROCHIP_KSZ9477_PTP_H_ */
drivers/net/dsa/microchip/ksz9477_spi.c +1 -1
@@ -72,7 +72,7 @@ static int ksz9477_spi_remove(struct spi_device *spi)struct ksz_device *dev =spi_get_drvdata (spi);if (dev) -ksz_switch_remove (dev); +ksz9477_switch_remove (dev);return 0 ; }
drivers/net/dsa/microchip/ksz_common.h +8 -0
@@ -11,6 +11,7 @@#include <linux/kernel.h> #include <linux/mutex.h> #include <linux/phy.h> +#include <linux/ptp_clock_kernel.h> #include <linux/regmap.h> #include <net/dsa.h>
@@ -90,6 +91,12 @@ struct ksz_device {u32 overrides;/* chip functions set by user */ u16 host_mask;u16 port_mask; + +#if IS_ENABLED (CONFIG_NET_DSA_MICROCHIP_KSZ9477_PTP ) +struct ptp_clock *ptp_clock; +struct ptp_clock_info ptp_caps; +struct mutex ptp_mutex;/* protects PTP related hardware */ +#endif };struct alu_struct {
@@ -145,6 +152,7 @@ void ksz_switch_remove(struct ksz_device *dev);int ksz8795_switch_register (struct ksz_device *dev);int ksz9477_switch_register (struct ksz_device *dev); +void ksz9477_switch_remove (struct ksz_device *dev);void ksz_update_port_member (struct ksz_device *dev,int port);void ksz_init_mib_timer (struct ksz_device *dev);
Signature · 8 lines
Christian Eggers Embedded software developer Arnold & Richter Cine Technik GmbH & Co. Betriebs KG Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918 Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477 Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler
PATCH v5 6/9 net: ptp: add helper for one-step P2P clocks
In reply to Christian EggersFor P2P delay measurement, the ingress time stamp of the PDelay_Req is required for the correction field of the PDelay_Resp. The application echoes back the correction field of the PDelay_Req when sending the PDelay_Resp.
Some hardware (like the ZHAW InES PTP time stamping IP core) subtracts the ingress timestamp autonomously from the correction field, so that the hardware only needs to add the egress timestamp on tx. Other hardware (like the Microchip KSZ9563) reports the ingress time stamp via an interrupt and requires that the software provides this time stamp via tail-tag on tx.
In order to avoid introducing a further application interface for this, the driver can simply emulate the behavior of the InES device and subtract the ingress time stamp in software from the correction field.
On egress, the correction field can either be kept as it is (and the time stamp field in the tail-tag is set to zero) or move the value from the correction field back to the tail-tag.
Changing the correction field requires updating the UDP checksum (if UDP is used as transport).
--- include/linux/ptp_classify.h | 73 ++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+)
include/linux/ptp_classify.h +73 -0
@@ -10,8 +10,12 @@#ifndef _PTP_CLASSIFY_H_#define _PTP_CLASSIFY_H_ +#include <asm/unaligned.h> #include <linux/ip.h> +#include <linux/ktime.h> #include <linux/skbuff.h> +#include <linux/udp.h> +#include <net/checksum.h> #define PTP_CLASS_NONE 0x00/* not a PTP event message */ #define PTP_CLASS_V1 0x01/* protocol version 1 */
@@ -123,6 +127,67 @@ static inline u8 ptp_get_msgtype(const struct ptp_header *hdr, return msgtype; } +/** +* ptp_check_diff8 - Computes new checksum (when altering a 64-bit field) +* @old: old field value +* @new: new field value +* @oldsum: previous checksum +* +* This function can be used to calculate a new checksum when only a single +* field is changed. Similar as ip_vs_check_diff*() in ip_vs.h. +* +* Return: Updated checksum +*/ +static inline __wsum ptp_check_diff8 (__be64 old,__be64 new,__wsum oldsum) +{ +__be64 diff[2 ] = { ~old, new }; + +return csum_partial (diff,sizeof (diff), oldsum); +} + +/** +* ptp_header_update_correction - Update PTP header's correction field +* @skb: packet buffer +* @type: type of the packet (see ptp_classify_raw()) +* @hdr: ptp header +* @correction: new correction value +* +* This updates the correction field of a PTP header and updates the UDP +* checksum (if UDP is used as transport). It is needed for hardware capable of +* one-step P2P that does not already modify the correction field of Pdelay_Req +* event messages on ingress. +*/ +static inline +void ptp_header_update_correction (struct sk_buff *skb,unsigned int type, +struct ptp_header *hdr,s64 correction) +{ +__be64 correction_old; +struct udphdr *uhdr; + +/* previous correction value is required for checksum update. */ +memcpy (&correction_old, &hdr->correction,sizeof (correction_old)); + +/* write new correction value */ +put_unaligned_be64 ((u64 )correction, &hdr->correction); + +switch (type &PTP_CLASS_PMASK ) { +case PTP_CLASS_IPV4 : +case PTP_CLASS_IPV6 : +/* locate udp header */ + uhdr = (struct udphdr *)((char *)hdr -sizeof (struct udphdr )); +break ; +default : +return ; + } + +/* update checksum */ + uhdr->check =csum_fold (ptp_check_diff8 (correction_old, + hdr->correction, + ~csum_unfold (uhdr->check))); +if (!uhdr->check) + uhdr->check =CSUM_MANGLED_0 ; +} +void __init ptp_classifier_init (void );#else static inline void ptp_classifier_init (void )
@@ -145,5 +210,13 @@ static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,*/ return PTP_MSGTYPE_SYNC ; } + +static inline +void ptp_onestep_p2p_move_t2_to_correction (struct sk_buff *skb, +unsigned int type, +struct ptp_header *hdr, +ktime_t t2) +{ +}#endif #endif /* _PTP_CLASSIFY_H_ */
Signature · 8 lines
Christian Eggers Embedded software developer Arnold & Richter Cine Technik GmbH & Co. Betriebs KG Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918 Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477 Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler
PATCH v5 7/9 net: dsa: microchip: ksz9477: initial hardware time stamping support
In reply to Christian EggersAdd control routines required for TX hardware time stamping.
The KSZ9563 only supports one step time stamping (HWTSTAMP_TX_ONESTEP_P2P), which requires linuxptp-2.0 or later.
Currently, only P2P delay measurement is supported. See patchwork discussion and comments in ksz9477_ptp_init() for details:
https://patchwork.ozlabs.org/project/netdev/patch/[email protected]/
---
Changes in v4:
--------------
- Remove useless case statement
- Reviewed-by: Vladimir Oltean <[email protected]>
drivers/net/dsa/microchip/ksz9477_main.c | 6 + drivers/net/dsa/microchip/ksz9477_ptp.c | 186 +++++++++++++++++++++++ drivers/net/dsa/microchip/ksz9477_ptp.h | 21 +++ drivers/net/dsa/microchip/ksz_common.h | 4 + 4 files changed, 217 insertions(+)
drivers/net/dsa/microchip/ksz9477_main.c +6 -0
@@ -1387,6 +1387,7 @@ static const struct dsa_switch_ops ksz9477_switch_ops = { .phy_read = ksz9477_phy_read16, .phy_write = ksz9477_phy_write16, .phylink_mac_link_down = ksz_mac_link_down, + .get_ts_info = ksz9477_ptp_get_ts_info, .port_enable = ksz_enable_port, .get_strings = ksz9477_get_strings, .get_ethtool_stats = ksz_get_ethtool_stats,
@@ -1407,6 +1408,11 @@ static const struct dsa_switch_ops ksz9477_switch_ops = { .port_mdb_del = ksz9477_port_mdb_del, .port_mirror_add = ksz9477_port_mirror_add, .port_mirror_del = ksz9477_port_mirror_del, + .port_hwtstamp_get = ksz9477_ptp_port_hwtstamp_get, + .port_hwtstamp_set = ksz9477_ptp_port_hwtstamp_set, + .port_txtstamp = NULL, + /* never defer rx delivery, tstamping is done via tail tagging */ + .port_rxtstamp = NULL, };static u32 ksz9477_get_port_addr (int port,int offset)
drivers/net/dsa/microchip/ksz9477_ptp.c +186 -0
215 lines not on this page · raw message
drivers/net/dsa/microchip/ksz9477_ptp.h +21 -0
@@ -10,6 +10,8 @@#ifndef DRIVERS_NET_DSA_MICROCHIP_KSZ9477_PTP_H_ #define DRIVERS_NET_DSA_MICROCHIP_KSZ9477_PTP_H_ +#include <linux/types.h> +#include "ksz_common.h" #if IS_ENABLED (CONFIG_NET_DSA_MICROCHIP_KSZ9477_PTP )
@@ -17,11 +19,30 @@int ksz9477_ptp_init (struct ksz_device *dev);void ksz9477_ptp_deinit (struct ksz_device *dev); +int ksz9477_ptp_get_ts_info (struct dsa_switch *ds,int port, +struct ethtool_ts_info *ts); +int ksz9477_ptp_port_hwtstamp_get (struct dsa_switch *ds,int port, +struct ifreq *ifr); +int ksz9477_ptp_port_hwtstamp_set (struct dsa_switch *ds,int port, +struct ifreq *ifr); +#else static inline int ksz9477_ptp_init (struct ksz_device *dev) {return 0 ; }static inline void ksz9477_ptp_deinit (struct ksz_device *dev) {} +static inline int ksz9477_ptp_get_ts_info (struct dsa_switch *ds,int port, +struct ethtool_ts_info *ts) +{return -EOPNOTSUPP ; } + +static inline int ksz9477_ptp_port_hwtstamp_get (struct dsa_switch *ds,int port, +struct ifreq *ifr) +{return -EOPNOTSUPP ; } + +static inline int ksz9477_ptp_port_hwtstamp_set (struct dsa_switch *ds,int port, +struct ifreq *ifr) +{return -EOPNOTSUPP ; } +#endif #endif /* DRIVERS_NET_DSA_MICROCHIP_KSZ9477_PTP_H_ */
drivers/net/dsa/microchip/ksz_common.h +4 -0
@@ -41,6 +41,10 @@ struct ksz_port {struct ksz_port_mib mib;phy_interface_t interface; +#if IS_ENABLED (CONFIG_NET_DSA_MICROCHIP_KSZ9477_PTP ) +struct hwtstamp_config tstamp_config; +bool hwts_tx_en; +#endif };struct ksz_device {
Signature · 8 lines
Christian Eggers Embedded software developer Arnold & Richter Cine Technik GmbH & Co. Betriebs KG Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918 Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477 Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler
PATCH v5 8/9 net: dsa: microchip: ksz9477: remaining hardware time stamping support
In reply to Christian EggersAdd data path routines required for TX hardware time stamping.
PTP mode is enabled depending on the filter setup (changes tail tag). TX time stamps are reported via an interrupt / device registers whilst RX time stamps are reported via an additional tail tag.
One step TX time stamping of PDelay_Resp requires the RX time stamp from the associated PDelay_Req message. The user space PTP stack assumes that the RX time stamp has already been subtracted from the PDelay_Req correction field (as done by the ZHAW InES PTP time stamping core). It will echo back the value of the correction field in the PDelay_Resp message.
In order to be compatible to this already established interface, the KSZ9563 code emulates this behavior. When processing the PDelay_Resp message, the time stamp is moved back from the correction field to the tail tag, as the hardware generates an invalid UDP checksum if this field is negative.
Of course, the UDP checksums (if any) have to be corrected after this (for both directions).
Everything has been tested on a Microchip KSZ9563 switch.
---
Changes in v5:
--------------
- Fix compile error reported by kernel test robot
(NET_DSA_TAG_KSZ must select NET_PTP_CLASSIFY)
Changes in v4:
--------------
- s/low active/active low/
- 80 chars per line
- Use IEEE 802.1AS mode (to suppress forwarding of PDelay messages)
- Enable/disable hardware timestaping at runtime (port_hwtstamp_set)
- Use mutex in port_hwtstamp_set
- Don't use port specific struct hwtstamp_config
- removed #ifdefs from tag_ksz.c
- Set port's tx_latency and rx_latency to 0
- added include/linux/dsa/ksz_common.h to MAINTAINERS
Quoted 5 lines · Vladimir Oltean
If you don't like the #ifdef's, I am not in love with them either. But maybe Christian is just optimizing too aggressively, and doesn't actually need to put those #ifdef's there and provide stub implementations, but could actually just leave the
ksz9477_rcv_timestampandksz9477_xmit_timestampalways compiled-in, and "dead at runtime" in the case there is no PTP.
I removed the #ifdefs.
Quoted 12 lines
[...]
The thing is, ptp4l already has ingressLatency and egressLatency settings, and I would not be surprised if those config options would get extended to cover values at multiple link speeds.In the general case, the ksz9477 MAC could be attached to any external PHY, having its own propagation delay characteristics, or any number of other things that cause clock domain crossings. I'm not sure how feasible it is for the kernel to abstract this away completely, and adjust timestamps automatically based on any and all combinations of MAC and PHY. Maybe this is just wishful thinking.
Quoted 9 lines
Oh, and by the way, Christian, I'm not even sure if you aren't in fact just beating around the bush with these
tstamp_rx_latency_nsandtstamp_tx_latency_nsvalues? I mean, the switch adds the latency value to the timestamps. And you, from the driver, read the value of the register, so you can subtract the value from the timestamp, to compensate for its correction. So, all in all, there is no net latency compensation seen by the outside world?! If that is the case, can't you just set the latency registers to zero, do your compensation from the application stack and call it a day?
At first I thought that I have to move these values to ptp4l.conf. But after setting the hardware registers to zero, it turned out, that I also have to use zero values in ptp4l.conf. So you are right.
Quoted 11 lines · Vladimir Oltean
Quoted 3 lines · Christian Eggers
please let me know, how I shall finally implement this. Enabling the PTP mode on the switch and sending the extra 4 byte tail on tx must be done in sync. Currently, both simply depends on the PTP define.
I, too, would prefer that the reconfiguration is done at ioctl time. Distributions typically enable whatever kernel config options they can. However, for users, the behavior should not change. Therefore the tail tag should remain small even though the PTP kernel config option is enabled, as long as hardware timestamping has not been explicitly enabled.
I moved this to port_hwtstamp_set. But I am not sure whether enabling PTP mode should depend on tx_type or rx_filter.
Quoted 4 lines
[...]
When forwarding what packet? What profile are you testing with? What commands do you run?
A P2P capable switch should not forward Peer delay messages.
With the 802.1AS settings, no SYNC/Announce messages are forwarded anymore. Peer delay messages have never been forwarded.
MAINTAINERS | 1 + drivers/net/dsa/microchip/ksz9477_main.c | 12 +- drivers/net/dsa/microchip/ksz9477_ptp.c | 391 ++++++++++++++++++++++- drivers/net/dsa/microchip/ksz9477_ptp.h | 14 + drivers/net/dsa/microchip/ksz_common.h | 9 +- include/linux/dsa/ksz_common.h | 70 ++++ net/dsa/Kconfig | 1 + net/dsa/tag_ksz.c | 205 +++++++++++- 8 files changed, 679 insertions(+), 24 deletions(-) create mode 100644 include/linux/dsa/ksz_common.h
MAINTAINERS +1 -0
@@ -11491,6 +11491,7 @@ L: [email protected] S: Maintained F: Documentation/devicetree/bindings/net/dsa/microchip,ksz.yaml F: drivers/net/dsa/microchip/* +F: include/linux/dsa/ksz_common.h F: include/linux/platform_data/microchip-ksz.h F: net/dsa/tag_ksz.c
drivers/net/dsa/microchip/ksz9477_main.c +8 -4
@@ -1410,7 +1410,7 @@ static const struct dsa_switch_ops ksz9477_switch_ops = { .port_mirror_del = ksz9477_port_mirror_del, .port_hwtstamp_get = ksz9477_ptp_port_hwtstamp_get, .port_hwtstamp_set = ksz9477_ptp_port_hwtstamp_set, - .port_txtstamp = NULL, + .port_txtstamp = ksz9477_ptp_port_txtstamp, /* never defer rx delivery, tstamping is done via tail tagging */ .port_rxtstamp = NULL, };
@@ -1537,6 +1537,7 @@ static const struct ksz_chip_data ksz9477_switch_chips[] = {static irqreturn_t ksz9477_switch_irq_thread (int irq,void *dev_id) {struct ksz_device *dev = dev_id; +irqreturn_t result =IRQ_NONE ;u32 data;int port;int ret;
@@ -1556,12 +1557,15 @@ static irqreturn_t ksz9477_switch_irq_thread(int irq, void *dev_id) ret =ksz_read8 (dev,PORT_CTRL_ADDR (port,REG_PORT_INT_STATUS ), &data8);if (ret) -return IRQ_NONE ; +return result; -/* ToDo: Add specific handling of port interrupts */ +if (data8 &PORT_PTP_INT ) { +if (ksz9477_ptp_port_interrupt (dev, port) !=IRQ_NONE ) + result =IRQ_HANDLED ; + } } -return IRQ_NONE ; +return result; }static int ksz9477_enable_port_interrupts (struct ksz_device *dev,bool enable)
drivers/net/dsa/microchip/ksz9477_ptp.c +381 -10
518 lines not on this page · raw message
drivers/net/dsa/microchip/ksz9477_ptp.h +14 -0
@@ -10,6 +10,7 @@#ifndef DRIVERS_NET_DSA_MICROCHIP_KSZ9477_PTP_H_ #define DRIVERS_NET_DSA_MICROCHIP_KSZ9477_PTP_H_ +#include <linux/irqreturn.h> #include <linux/types.h> #include "ksz_common.h"
@@ -19,18 +20,26 @@int ksz9477_ptp_init (struct ksz_device *dev);void ksz9477_ptp_deinit (struct ksz_device *dev); +irqreturn_t ksz9477_ptp_port_interrupt (struct ksz_device *dev,int port); +int ksz9477_ptp_get_ts_info (struct dsa_switch *ds,int port,struct ethtool_ts_info *ts);int ksz9477_ptp_port_hwtstamp_get (struct dsa_switch *ds,int port,struct ifreq *ifr);int ksz9477_ptp_port_hwtstamp_set (struct dsa_switch *ds,int port,struct ifreq *ifr); +bool ksz9477_ptp_port_txtstamp (struct dsa_switch *ds,int port, +struct sk_buff *clone,unsigned int type);#else static inline int ksz9477_ptp_init (struct ksz_device *dev) {return 0 ; }static inline void ksz9477_ptp_deinit (struct ksz_device *dev) {} +static inline irqreturn_t ksz9477_ptp_port_interrupt (struct ksz_device *dev, +int port) +{return IRQ_NONE ; } +static inline int ksz9477_ptp_get_ts_info (struct dsa_switch *ds,int port,struct ethtool_ts_info *ts) {return -EOPNOTSUPP ; }
@@ -43,6 +52,11 @@ static inline int ksz9477_ptp_port_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr) {return -EOPNOTSUPP ; } +static inline bool ksz9477_ptp_port_txtstamp (struct dsa_switch *ds,int port, +struct sk_buff *clone, +unsigned int type) +{return false; } +#endif #endif /* DRIVERS_NET_DSA_MICROCHIP_KSZ9477_PTP_H_ */
drivers/net/dsa/microchip/ksz_common.h +8 -1
@@ -7,8 +7,11 @@#ifndef __KSZ_COMMON_H#define __KSZ_COMMON_H +#include <linux/completion.h> +#include <linux/dsa/ksz_common.h> #include <linux/etherdevice.h> #include <linux/kernel.h> +#include <linux/ktime.h> #include <linux/mutex.h> #include <linux/phy.h> #include <linux/ptp_clock_kernel.h>
@@ -42,7 +45,9 @@ struct ksz_port {struct ksz_port_mib mib;phy_interface_t interface;#if IS_ENABLED (CONFIG_NET_DSA_MICROCHIP_KSZ9477_PTP ) -struct hwtstamp_config tstamp_config; +struct ksz_port_ptp_shared ptp_shared; +ktime_t tstamp_xdelay; +struct completion tstamp_completion;bool hwts_tx_en;#endif };
@@ -99,7 +104,9 @@ struct ksz_device {#if IS_ENABLED (CONFIG_NET_DSA_MICROCHIP_KSZ9477_PTP )struct ptp_clock *ptp_clock;struct ptp_clock_info ptp_caps; +struct hwtstamp_config tstamp_config;struct mutex ptp_mutex;/* protects PTP related hardware */ +struct ksz_device_ptp_shared ptp_shared;#endif };
include/linux/dsa/ksz_common.h new file +70 -0
70 lines not on this page · raw message
net/dsa/Kconfig +1 -0
@@ -93,6 +93,7 @@ config NET_DSA_TAG_MTK config NET_DSA_TAG_KSZ tristate "Tag driver for Microchip 8795/9477/9893 families of switches" + select NET_PTP_CLASSIFY help Say Y if you want to enable support for tagging frames for the Microchip 8795/9477/9893 families of switches.
net/dsa/tag_ksz.c +196 -9
287 lines not on this page · raw message
Signature · 8 lines
Christian Eggers Embedded software developer Arnold & Richter Cine Technik GmbH & Co. Betriebs KG Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918 Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477 Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler
PATCH v5 9/9 net: dsa: microchip: ksz9477: add periodic output support
In reply to Christian EggersThe KSZ9563 has a Trigger Output Unit (TOU) which can be used to generate periodic signals.
The pulse length can be altered via a device attribute.
Tested on a Microchip KSZ9563 switch.
---
Changes in v4:
--------------
- 80 chars per line
- reverse christmas tree
- Set default pulse width for perout pulse to 50% (max. 125ms)
- reject unsupported flags for perout_request
Quoted 7 lines · Vladimir Oltean
Quoted 5 lines · Christian Eggers
static int
ksz9477_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)(quoted diff) +0
-0@@ -241,6 +256,15 @@ static int ksz9477_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) case KSZ_PTP_TOU_IDLE: break;
Quoted 5 lines
Review static int ksz9477_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) · line 260
Quoted 2 lines
(quoted diff) +2
-0+ case KSZ_PTP_TOU_PEROUT: + dev_info(dev->dev, "Restarting periodic output signal\n");Isn't this a bit too verbose, or is there something for the user to be concerned about?
In my setup this message appears only once after ptp4l has been started. Shall I remove it?
Quoted 1 line
Watch out for 80 characters, please!
A few long line are left (for instance ksz9477_ptp_enable_perout). Please let me know how I shall wrap them.
Quoted 6 lines
Quoted 6 lines
(quoted diff) +6
-0+ if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) { + u64 value = perout_request->on.sec * NSEC_PER_SEC + + perout_request->on.nsec; + + ret = ksz9477_ptp_tou_pulse_verify(value); + if (ret)
Quoted 8 lines
Review static int ksz9477_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) · line 270
Quoted 4 lines
(quoted diff) +4
-0+ return ret; + + dev->ptp_perout_pulse_width_ns = value; + }It is not guaranteed that user space will set this flag. Shouldn't you assign a default value for the pulse width? I don't know, half the period should be a good default.
Yes I should. The hardware support a maximum "duty cycle" of a little bit more than 125 ms. As PPS signals often have a 125 ms high period, I use this duration as the maximum.
Quoted 11 lines
Review static int ksz9477_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
Quoted 8 lines
(quoted diff) +8
-0+ /* Check error flag: + * - the ACTIVE flag is NOT cleared an error! + */ + ret = ksz_read32(dev, REG_PTP_TRIG_STATUS__4, &gpio_stat0); + if (ret) + return ret; + + if (gpio_stat0 & (1 << (0 + TRIG_ERROR_S))) {What is the role of this "0 +" term here?
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ dev_err(dev->dev, "%s: Trigger unit0 error!\n", __func__);
This is the index of the used trigger unit.
drivers/net/dsa/microchip/ksz9477_ptp.c | 357 +++++++++++++++++++++++- drivers/net/dsa/microchip/ksz9477_ptp.h | 4 + drivers/net/dsa/microchip/ksz_common.h | 10 + 3 files changed, 369 insertions(+), 2 deletions(-)
drivers/net/dsa/microchip/ksz9477_ptp.c +355 -2
@@ -19,6 +19,140 @@#define KSZ_PTP_INC_NS 40/* HW clock is incremented every 40 ns (by 40) */ #define KSZ_PTP_SUBNS_BITS 32/* Number of bits in sub-nanoseconds counter */ +/* Shared register access routines (Trigger Output Unit) */ + +static int ksz9477_ptp_tou_reset (struct ksz_device *dev,unsigned int unit) +{ +u32 ctrl_stat, data; +int ret; + +/* Reset trigger unit (clears TRIGGER_EN, but not GPIOSTATx) */ + ret =ksz_read32 (dev,REG_PTP_CTRL_STAT__4 , &ctrl_stat); +if (ret) +return ret; + + ctrl_stat |=TRIG_RESET ; + + ret =ksz_write32 (dev,REG_PTP_CTRL_STAT__4 , ctrl_stat); +if (ret) +return ret; + +/* Clear DONE */ + data =1 << (unit +TRIG_DONE_S ); + ret =ksz_write32 (dev,REG_PTP_TRIG_STATUS__4 , data); +if (ret) +return ret; + +/* Clear IRQ */ + data =1 << (unit +TRIG_INT_S ); + ret =ksz_write32 (dev,REG_PTP_INT_STATUS__4 , data); +if (ret) +return ret; + +/* Clear reset and set GPIO direction */ + ctrl_stat &= ~TRIG_ENABLE ;/* clear cached bit :-) */ + ctrl_stat &= ~TRIG_RESET ; + + ret =ksz_write32 (dev,REG_PTP_CTRL_STAT__4 , ctrl_stat); +if (ret) +return ret; + +return 0 ; +} + +static int ksz9477_ptp_tou_cycle_width_set (struct ksz_device *dev,u32 width_ns) +{ +int ret; + + ret =ksz_write32 (dev,REG_TRIG_CYCLE_WIDTH , width_ns); +if (ret) +return ret; + +return 0 ; +} + +static int ksz9477_ptp_tou_cycle_count_set (struct ksz_device *dev,u16 count) +{ +u32 data; +int ret; + + ret =ksz_read32 (dev,REG_TRIG_CYCLE_CNT , &data); +if (ret) +return ret; + + data &= ~(TRIG_CYCLE_CNT_M <<TRIG_CYCLE_CNT_S ); + data |= (count &TRIG_CYCLE_CNT_M ) <<TRIG_CYCLE_CNT_S ; + + ret =ksz_write32 (dev,REG_TRIG_CYCLE_CNT , data); +if (ret) +return ret; + +return 0 ; +} + +static int ksz9477_ptp_tou_pulse_verify (u64 pulse_ns) +{ +u32 data; + +if (pulse_ns &0x3 ) +return -EINVAL ; + + data = (pulse_ns /8 ); +if (data != (data &TRIG_PULSE_WIDTH_M )) +return -ERANGE ; + +return 0 ; +} + +static int ksz9477_ptp_tou_pulse_set (struct ksz_device *dev,u32 pulse_ns) +{ +u32 data; + + data = (pulse_ns /8 ); + +return ksz_write32 (dev,REG_TRIG_PULSE_WIDTH__4 , data); +} + +static int ksz9477_ptp_tou_target_time_set (struct ksz_device *dev,struct timespec64 const *ts) +{ +int ret; + +/* Hardware has only 32 bit */ +if ((ts->tv_sec &0xffffffff ) != ts->tv_sec) +return -EINVAL ; + + ret =ksz_write32 (dev,REG_TRIG_TARGET_NANOSEC , ts->tv_nsec); +if (ret) +return ret; + + ret =ksz_write32 (dev,REG_TRIG_TARGET_SEC , ts->tv_sec); +if (ret) +return ret; + +return 0 ; +} + +static int ksz9477_ptp_tou_start (struct ksz_device *dev,u32 *ctrl_stat_) +{ +u32 ctrl_stat; +int ret; + + ret =ksz_read32 (dev,REG_PTP_CTRL_STAT__4 , &ctrl_stat); +if (ret) +return ret; + + ctrl_stat |=TRIG_ENABLE ; + + ret =ksz_write32 (dev,REG_PTP_CTRL_STAT__4 , ctrl_stat); +if (ret) +return ret; + +if (ctrl_stat_) + *ctrl_stat_ = ctrl_stat; + +return 0 ; +} +/* Posix clock support */ static int ksz9477_ptp_adjfine (struct ptp_clock_info *ptp,long scaled_ppm)
@@ -76,6 +210,8 @@ static int ksz9477_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)return ret; } +static int ksz9477_ptp_restart_perout (struct ksz_device *dev); +static int ksz9477_ptp_adjtime (struct ptp_clock_info *ptp,s64 delta) { struct ksz_device *dev = container_of(ptp, struct ksz_device, ptp_caps);
@@ -115,6 +251,20 @@ static int ksz9477_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)if (ret) goto error_return; +switch (dev->ptp_tou_mode) { +case KSZ_PTP_TOU_IDLE : +break ; + +case KSZ_PTP_TOU_PEROUT : +dev_info (dev->dev,"Restarting periodic output signal\n" ); + + ret =ksz9477_ptp_restart_perout (dev); +if (ret) + goto error_return; + +break ; + } +spin_lock_bh (&ptp_shared->ptp_clock_lock); ptp_shared->ptp_clock_time =timespec64_add (ptp_shared->ptp_clock_time, delta64);
@@ -218,6 +368,20 @@ static int ksz9477_ptp_settime(struct ptp_clock_info *ptp,if (ret) goto error_return; +switch (dev->ptp_tou_mode) { +case KSZ_PTP_TOU_IDLE : +break ; + +case KSZ_PTP_TOU_PEROUT : +dev_info (dev->dev,"Restarting periodic output signal\n" ); + + ret =ksz9477_ptp_restart_perout (dev); +if (ret) + goto error_return; + +break ; + } +spin_lock_bh (&ptp_shared->ptp_clock_lock); ptp_shared->ptp_clock_time = *ts;spin_unlock_bh (&ptp_shared->ptp_clock_lock);
@@ -227,10 +391,199 @@ static int ksz9477_ptp_settime(struct ptp_clock_info *ptp,return ret; } +static int ksz9477_ptp_configure_perout (struct ksz_device *dev, +u32 cycle_width_ns,u16 cycle_count, +u32 pulse_width_ns, +struct timespec64 const *target_time) +{ +u32 trig_ctrl; +int ret; + +/* Enable notify, set rising edge, set periodic pattern */ + trig_ctrl =TRIG_NOTIFY | (TRIG_POS_PERIOD <<TRIG_PATTERN_S ); + ret =ksz_write32 (dev,REG_TRIG_CTRL__4 , trig_ctrl); +if (ret) +return ret; + + ret =ksz9477_ptp_tou_cycle_width_set (dev, cycle_width_ns); +if (ret) +return ret; + +ksz9477_ptp_tou_cycle_count_set (dev, cycle_count); +if (ret) +return ret; + + ret =ksz9477_ptp_tou_pulse_set (dev, pulse_width_ns); +if (ret) +return ret; + + ret =ksz9477_ptp_tou_target_time_set (dev, target_time); +if (ret) +return ret; + +return 0 ; +} + +#define KSZ9477_PEROUT_VALID_FLAGS ( \ + PTP_PEROUT_DUTY_CYCLE \ +) + +static int ksz9477_ptp_enable_perout (struct ksz_device *dev, +struct ptp_perout_request const *perout_request, +int on) +{ +u64 cycle_width_ns; +u64 pulse_width_ns; +u32 gpio_stat0; +int ret; + +if (perout_request->flags & ~KSZ9477_PEROUT_VALID_FLAGS ) +return -EINVAL ; + +if (dev->ptp_tou_mode !=KSZ_PTP_TOU_PEROUT && + dev->ptp_tou_mode !=KSZ_PTP_TOU_IDLE ) +return -EBUSY ; + + ret =ksz9477_ptp_tou_reset (dev,0 ); +if (ret) +return ret; + +if (!on) { + dev->ptp_tou_mode =KSZ_PTP_TOU_IDLE ; +return 0 ;/* success */ + } + + dev->ptp_perout_target_time_first.tv_sec = perout_request->start.sec; + dev->ptp_perout_target_time_first.tv_nsec = perout_request->start.nsec; + + dev->ptp_perout_period.tv_sec = perout_request->period.sec; + dev->ptp_perout_period.tv_nsec = perout_request->period.nsec; + + cycle_width_ns =timespec64_to_ns (&dev->ptp_perout_period); +if ((cycle_width_ns &GENMASK (31 ,0 )) != cycle_width_ns) +return -EINVAL ; + +if (perout_request->flags &PTP_PEROUT_DUTY_CYCLE ) + pulse_width_ns = perout_request->on.sec *NSEC_PER_SEC + + perout_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. +*/ + pulse_width_ns =min_t (u64, + (perout_request->period.sec *NSEC_PER_SEC + + perout_request->period.nsec) /2 + /8 *8 , +125000000LL ); + + ret =ksz9477_ptp_tou_pulse_verify (pulse_width_ns); +if (ret) +return ret; + + dev->ptp_perout_pulse_width_ns = pulse_width_ns; + + ret =ksz9477_ptp_configure_perout (dev, cycle_width_ns, + dev->ptp_perout_cycle_count, + dev->ptp_perout_pulse_width_ns, + &dev->ptp_perout_target_time_first); +if (ret) +return ret; + +/* Activate trigger unit */ + ret =ksz9477_ptp_tou_start (dev,NULL ); +if (ret) +return ret; + +/* Check error flag: +* - the ACTIVE flag is NOT cleared an error! +*/ + ret =ksz_read32 (dev,REG_PTP_TRIG_STATUS__4 , &gpio_stat0); +if (ret) +return ret; + +if (gpio_stat0 & (1 << (0 +TRIG_ERROR_S ))) { +dev_err (dev->dev,"%s: Trigger unit0 error!\n" , __func__); + ret = -EIO ; +/* Unit will be reset on next access */ +return ret; + } + + dev->ptp_tou_mode =KSZ_PTP_TOU_PEROUT ; +return 0 ; +} + +static int ksz9477_ptp_restart_perout (struct ksz_device *dev) +{ +s64 now_ns, first_ns, period_ns, next_ns; +struct timespec64 now; +unsigned int count; +int ret; + + ret =_ksz9477_ptp_gettime (dev, &now); +if (ret) +return ret; + + now_ns =timespec64_to_ns (&now); + first_ns =timespec64_to_ns (&dev->ptp_perout_target_time_first); + +/* Calculate next perout event based on start time and period */ + period_ns =timespec64_to_ns (&dev->ptp_perout_period); + +if (first_ns < now_ns) { + count =div_u64 (now_ns - first_ns, period_ns); + next_ns = first_ns + count * period_ns; + }else { + next_ns = first_ns; + } + +/* Ensure 100 ms guard time prior next event */ +while (next_ns < now_ns +100000000 ) + next_ns += period_ns; + +/* Restart periodic output signal */ + { +struct timespec64 next =ns_to_timespec64 (next_ns); +struct ptp_perout_request perout_request = { + .start = { + .sec = next.tv_sec, + .nsec = next.tv_nsec + }, + .period = { + .sec = dev->ptp_perout_period.tv_sec, + .nsec = dev->ptp_perout_period.tv_nsec + }, + .index =0 , + .flags =0 ,/* keep current values */ + }; + ret =ksz9477_ptp_enable_perout (dev, &perout_request,1 ); +if (ret) +return ret; + } + +return 0 ; +} +static int ksz9477_ptp_enable (struct ptp_clock_info *ptp,struct ptp_clock_request *req,int on) { -return -EOPNOTSUPP ; + struct ksz_device *dev = container_of(ptp, struct ksz_device, ptp_caps); +int ret; + +switch (req->type) { +case PTP_CLK_REQ_PEROUT : { +struct ptp_perout_request const *perout_request = &req->perout; + +mutex_lock (&dev->ptp_mutex); + ret =ksz9477_ptp_enable_perout (dev, perout_request, on); +mutex_unlock (&dev->ptp_mutex); +return ret; + } +default : +return -EINVAL ; + } + +return 0 ; }static long ksz9477_ptp_do_aux_work (struct ptp_clock_info *ptp)
@@ -592,7 +945,7 @@ int ksz9477_ptp_init(struct ksz_device *dev) dev->ptp_caps.max_adj =6249999 ; dev->ptp_caps.n_alarm =0 ; dev->ptp_caps.n_ext_ts =0 ;/* currently not implemented */ - dev->ptp_caps.n_per_out =0 ; + dev->ptp_caps.n_per_out =1 ; dev->ptp_caps.pps =0 ; dev->ptp_caps.adjfine = ksz9477_ptp_adjfine; dev->ptp_caps.adjtime = ksz9477_ptp_adjtime;
drivers/net/dsa/microchip/ksz9477_ptp.h +4 -0
@@ -20,6 +20,7 @@int ksz9477_ptp_init (struct ksz_device *dev);void ksz9477_ptp_deinit (struct ksz_device *dev); +irqreturn_t ksz9477_ptp_interrupt (struct ksz_device *dev);irqreturn_t ksz9477_ptp_port_interrupt (struct ksz_device *dev,int port); int ksz9477_ptp_get_ts_info(struct dsa_switch *ds, int port,
@@ -36,6 +37,9 @@ bool ksz9477_ptp_port_txtstamp(struct dsa_switch *ds, int port,static inline int ksz9477_ptp_init (struct ksz_device *dev) {return 0 ; }static inline void ksz9477_ptp_deinit (struct ksz_device *dev) {} +static inline irqreturn_t ksz9477_ptp_interrupt (struct ksz_device *dev) +{return IRQ_NONE ; } +static inline irqreturn_t ksz9477_ptp_port_interrupt (struct ksz_device *dev,int port) {return IRQ_NONE ; }
drivers/net/dsa/microchip/ksz_common.h +10 -0
@@ -52,6 +52,11 @@ struct ksz_port {#endif }; +enum ksz_ptp_tou_mode { +KSZ_PTP_TOU_IDLE , +KSZ_PTP_TOU_PEROUT , +}; +struct ksz_device {struct dsa_switch *ds;struct ksz_platform_data *pdata;
@@ -107,6 +112,11 @@ struct ksz_device {struct hwtstamp_config tstamp_config;struct mutex ptp_mutex;/* protects PTP related hardware */ struct ksz_device_ptp_shared ptp_shared; +enum ksz_ptp_tou_mode ptp_tou_mode; +struct timespec64 ptp_perout_target_time_first;/* start of first perout pulse */ +struct timespec64 ptp_perout_period; +u32 ptp_perout_pulse_width_ns; +u16 ptp_perout_cycle_count;#endif };
Signature · 8 lines
Christian Eggers Embedded software developer Arnold & Richter Cine Technik GmbH & Co. Betriebs KG Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918 Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477 Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler
Richard Cochran
maintainerRe: [PATCH net-next v5 9/9] net: dsa: microchip: ksz9477: add periodic output support
Quoted 4 lines · Christian Eggers
The KSZ9563 has a Trigger Output Unit (TOU) which can be used to generate periodic signals.
The pulse length can be altered via a device attribute.
Device tree is the wrong place for that.
Aren't you using PTP_PEROUT_DUTY_CYCLE anyhow?
Thanks,
Richard
Re: [PATCH net-next v5 9/9] net: dsa: microchip: ksz9477: add periodic output support
Quoted 7 lines · Richard Cochran
Quoted 4 lines · Christian Eggers
The KSZ9563 has a Trigger Output Unit (TOU) which can be used to generate periodic signals.
The pulse length can be altered via a device attribute.
Device tree is the wrong place for that.
I meant a device specfic sysfs attribute./sys/class/ptp/ptp2/device/pulse
But this is not true anymore, as I moved the sysfs attribute to a later patch (not submitted yet).
Quoted 1 line
Aren't you using
PTP_PEROUT_DUTY_CYCLEanyhow?
I also use PTP_PEROUT_DUTY_CYCLE. But this cannot be set via a shell script. The existing "period" sysfs attribute doesn't allow to set the pulse length, so I added a device specific attribute for this.
Should ptp_sysfs be extended with a "pulse" attribute with calls enable() with only PTP_PEROUT_DUTY_CYCLE set?
Quoted 5 lines
Thanks,
Richard
regards
Christian
Vladimir Oltean
maintainerRe: [PATCH net-next v5 9/9] net: dsa: microchip: ksz9477: add periodic output support
Quoted 2 lines · Christian Eggers
Should
ptp_sysfsbe extended with a "pulse" attribute with callsenable()with onlyPTP_PEROUT_DUTY_CYCLEset?
Use tools/testing/selftests/ptp/testptp.
Re: [PATCH net-next v5 9/9] net: dsa: microchip: ksz9477: add periodic output support
Quoted 5 lines · Vladimir Oltean
Quoted 2 lines · Christian Eggers
Should
ptp_sysfsbe extended with a "pulse" attribute with callsenable()with onlyPTP_PEROUT_DUTY_CYCLEset?Use
tools/testing/selftests/ptp/testptp.
Thanks for the hint. Last time I looked at it (5.4), it used to have no "pulse" feature (as the ioctl() interface).
Maybe I should also add support for PTP_PEROUT_PHASE to the KSZ driver.
regards
Christian
Richard Cochran
maintainerRe: [PATCH net-next v5 9/9] net: dsa: microchip: ksz9477: add periodic output support
In reply to Christian EggersQuoted 2 lines · Christian Eggers
Should
ptp_sysfsbe extended with a "pulse" attribute with callsenable()with onlyPTP_PEROUT_DUTY_CYCLEset?
Yes, that would make sense. It would bring sysfs back to feature parity with the ioctls.
Thanks,
Richard
Vladimir Oltean
maintainerRe: [PATCH net-next v5 9/9] net: dsa: microchip: ksz9477: add periodic output support
Quoted 6 lines · Richard Cochran
Quoted 2 lines · Christian Eggers
Should
ptp_sysfsbe extended with a "pulse" attribute with callsenable()with onlyPTP_PEROUT_DUTY_CYCLEset?Yes, that would make sense. It would bring sysfs back to feature parity with the ioctls.
Which is a good thing?
Anyway, Christian, if you do decide to do that, here's some context why I didn't do it when I added the additional knobs for periodic output:
https://www.mail-archive.com/[email protected]/msg04150.html
Richard Cochran
maintainerRe: [PATCH net-next v5 9/9] net: dsa: microchip: ksz9477: add periodic output support
Quoted 5 lines · Vladimir Oltean
Quoted 2 lines · Richard Cochran
Yes, that would make sense. It would bring sysfs back to feature parity with the ioctls.
Which is a good thing?
Yes, of course it is. I'm sorry I didn't insist on it in the first place!
Quoted 3 lines
Anyway, Christian, if you do decide to do that, here's some context why I didn't do it when I added the additional knobs for periodic output:
https://www.mail-archive.com/[email protected]/msg04150.html
I think Christian is proposing a different sysfs file, not a flag in the existing ones. That makes sense to me.
Thanks,
Richard