Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'net-ethernet-ti-k3-introduce-common-platform-time-sync-driver-cpts'

Grygorii Strashko says:

====================
net: ethernet: ti: k3: introduce common platform time sync driver - cpts

This series introduced support for significantly upgraded TI A65x/J721E Common
platform time sync (CPTS) modules which are part of AM65xx Time Synchronization
Architecture [1].
The TI A65x/J721E now contain more than one CPTS instance:
- MCU CPSW CPTS (IEEE 1588 compliant)
- Main NAVSS CPTS (central)
- PCIe CPTS(s) (PTM compliant)
- J721E: Main CPSW9g CPTS (IEEE 1588 compliant)
which can work as separately as interact to each other through Time Sync Router
(TSR) and Compare Event Router (CER). In addition there are also ICSS-G IEP
blocks which can perform similar timsync functions, but require FW support.
More info also available in TRM [2][3]. Not all above modules are available
to the Linux by as of now as some of them are reserved for RTOS/FW purposes.

The scope of this submission is TI A65x/J721E CPSW CPTS and Main NAVSS CPTS,
and TSR was used for testing purposes.
+---------------------------+
| MCU CPSW |
+-------------------+ +------------------------+ | TS |
| Main Navss CPTS | | Time Sync Router (TSR) | | +-------------+ |
| | | | | | | |
| HW1_TS +<----------+ | | +--------v-----+ +--+--+
| | | | | | CPTS | |Port |
| ... | | | X+-->HW1_TS | | |
| HW8_TS <------------<---------+ | X|-->HW2_TS | +--^--+
| | | | +--------------->HW3_TS | | |
| | | | +--------------->HW4_TS | | |
| | | | | | | | | |
| | | | | | | | | |
| Genf0 +-----------> (A)---------+ +<--------------+Genf0 | | |
| | | | | | | | | |
| ... | | +-----------> <---------------+Genf1 ESTf+-------+ |
| | | | | | | |
| | | | | +--------------+ |
| Genf8 +---------->+ | | |
| | | SYNC0 ... SYNC3 | | |
+-------------------+ +------+------------+----+ +---------------------------+
+ +
X X
(A) shows possible routing path for MCU CPSW CPTS Genf0 signal as an example.

Main features of the new TI A65x/J721E CPTS modules are:
- 64-bit timestamp/counter mode support in ns by using add_val
- implemented in HW PPM and nudge adjustment.
- control of time sync events via interrupt or polling
- selection of multiple external reference clock sources
- hardware timestamp of ext. inputs events (HWx_TS_PUSH)
- periodic generator function outputs (TS_GENFx)
- (CPSW only) Ethernet Enhanced Scheduled Traffic Operations (CPTS_ESTFn),
which drives TSN schedule
- timestamping of all RX packets bypassing CPTS FIFO

Patch 1 - DT bindings
Patch 2 - the AM65x/J721E driver
Patch 3 - enables packet timestamping support in TI AM65x/J721E MCU CPSW driver.
Patches 4-7 - DT updates.

=== PTP Testing:

phc2sys -s CLOCK_REALTIME -c eth0 -m -O 0 -u30
phc2sys[627.331]: eth0 rms 409912446712787392 max 1587584079521858304 freq -6665 +/- 35040 delay 832 +/- 27
phc2sys[657.335]: eth0 rms 33 max 66 freq -0 +/- 28 delay 820 +/- 30
phc2sys[687.339]: eth0 rms 37 max 70 freq -1 +/- 32 delay 830 +/- 29
phc2sys[717.343]: eth0 rms 33 max 71 freq -0 +/- 29 delay 828 +/- 23
phc2sys[747.346]: eth0 rms 35 max 75 freq -0 +/- 31 delay 829 +/- 26
phc2sys[777.350]: eth0 rms 37 max 68 freq -1 +/- 32 delay 825 +/- 25
phc2sys[807.354]: eth0 rms 28 max 57 freq -1 +/- 25 delay 824 +/- 21
phc2sys[837.358]: eth0 rms 43 max 81 freq -1 +/- 37 delay 836 +/- 23
phc2sys[867.361]: eth0 rms 33 max 74 freq +0 +/- 29 delay 828 +/- 24
phc2sys[897.365]: eth0 rms 35 max 77 freq -2 +/- 30 delay 824 +/- 25
phc2sys[927.369]: eth0 rms 28 max 50 freq +0 +/- 25 delay 825 +/- 25

ptp4l -P -2 -H -i eth0 -l 6 -m -q -p /dev/ptp1 -f ptp.cfg -s
ptp4l[22095.754]: port 1: MASTER to UNCALIBRATED on RS_SLAVE
ptp4l[22097.754]: port 1: UNCALIBRATED to SLAVE on MASTER_CLOCK_SELECTED
ptp4l[22159.757]: rms 317 max 1418 freq +79 +/- 186 delay 410 +/- 1
ptp4l[22223.760]: rms 9 max 24 freq +42 +/- 12 delay 409 +/- 1
ptp4l[22287.763]: rms 10 max 28 freq +41 +/- 11 delay 410 +/- 1
ptp4l[22351.767]: rms 10 max 26 freq +34 +/- 12 delay 410 +/- 1
ptp4l[22415.770]: rms 10 max 26 freq +49 +/- 14 delay 410 +/- 1

=== Ext. HW_TS and Genf testing:

For testing purposes Time Sync Router (TSR) can be modeled in DT as pin controller
+ timesync_router: timesync_router@A40000 {
+ compatible = "pinctrl-single";
+ reg = <0x0 0xA40000 0x0 0x800>;
+ #address-cells = <1>;
+ #size-cells = <0>;
+ #pinctrl-cells = <1>;
+ pinctrl-single,register-width = <32>;
+ pinctrl-single,function-mask = <0x800007ff>;
+ };

then signals routing can be done in board file, for example:
+#define TS_OFFSET(pa, val) (0x4+(pa)*4) (0x80000000 | val)
+
+&timesync_router {
+ pinctrl-names = "default";
+ pinctrl-0 = <&mcu_cpts>;
+
+ /* Example of the timesync routing */
+ mcu_cpts: mcu_cpts {
+ pinctrl-single,pins = <
+ /* [cpts genf1] in13 -> out25 [cpts hw4_push] */
+ TS_OFFSET(25, 13)
+ /* [cpts genf1] in13 -> out0 [main cpts hw1_push] */
+ TS_OFFSET(0, 13)
+ /* [main cpts genf0] in4 -> out1 [main cpts hw2_push] */
+ TS_OFFSET(1, 4)
+ /* [main cpts genf0] in4 -> out24 [cpts hw3_push] */
+ TS_OFFSET(24, 4)
+ >;
+ };
+};

will create link:
cpsw cpts Genf1 -> main cpts hw1_push
-> cpsw cpts hw4_push

main cpts Genf0 -> main cpts hw2_push
-> cpsw cpts hw3_push

testptp -d /dev/ptp0 -i 0 -p 1000000000
periodic output request okay
testptp -d /dev/ptp0 -i 1 -e 5
external time stamp request okay
event index 1 at 22583.000000025
event index 1 at 22584.000000025
event index 1 at 22585.000000025
event index 1 at 22586.000000025
event index 1 at 22587.000000025
testptp -d /dev/ptp1 -i 2 -e 5
external time stamp request okay
event index 2 at 1587606764.249304554
event index 2 at 1587606765.249304467
event index 2 at 1587606766.249304380
event index 2 at 1587606767.249304293
event index 2 at 1587606768.249304206

[1] https://www.ti.com/lit/pdf/spracp7
[2] https://www.ti.com/lit/pdf/sprz452
[3] https://www.ti.com/lit/pdf/spruil1
====================

Signed-off-by: David S. Miller <davem@davemloft.net>

+1528 -2
+7
Documentation/devicetree/bindings/net/ti,k3-am654-cpsw-nuss.yaml
··· 144 144 description: 145 145 CPSW MDIO bus. 146 146 147 + "^cpts$": 148 + type: object 149 + allOf: 150 + - $ref: "ti,am654-cpts.yaml#" 151 + description: 152 + CPSW Common Platform Time Sync (CPTS) module. 153 + 147 154 required: 148 155 - compatible 149 156 - reg
+152
Documentation/devicetree/bindings/net/ti,k3-am654-cpts.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/net/ti,am654-cpts.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: The TI AM654x/J721E Common Platform Time Sync (CPTS) module Device Tree Bindings 8 + 9 + maintainers: 10 + - Grygorii Strashko <grygorii.strashko@ti.com> 11 + - Sekhar Nori <nsekhar@ti.com> 12 + 13 + description: |+ 14 + The TI AM654x/J721E CPTS module is used to facilitate host control of time 15 + sync operations. 16 + Main features of CPTS module are 17 + - selection of multiple external clock sources 18 + - Software control of time sync events via interrupt or polling 19 + - 64-bit timestamp mode in ns with PPM and nudge adjustment. 20 + - hardware timestamp push inputs (HWx_TS_PUSH) 21 + - timestamp counter compare output (TS_COMP) 22 + - timestamp counter bit output (TS_SYNC) 23 + - periodic Generator function outputs (TS_GENFx) 24 + - Ethernet Enhanced Scheduled Traffic Operations (CPTS_ESTFn) (TSN) 25 + - external hardware timestamp push inputs (HWx_TS_PUSH) timestamping 26 + 27 + Depending on integration it enables compliance with the IEEE 1588-2008 28 + standard for a precision clock synchronization protocol, Ethernet Enhanced 29 + Scheduled Traffic Operations (CPTS_ESTFn) and PCIe Subsystem Precision Time 30 + Measurement (PTM). 31 + 32 + TI AM654x/J721E SoCs has several similar CPTS modules integrated into the 33 + different parts of the system which could be synchronized with each other 34 + - Main CPTS 35 + - MCU CPSW CPTS with IEEE 1588-2008 support 36 + - PCIe subsystem CPTS for PTM support 37 + 38 + Depending on CPTS module integration and when CPTS is integral part of 39 + another module (MCU CPSW for example) "compatible" and "reg" can 40 + be omitted - parent module is fully responsible for CPTS enabling and 41 + configuration. 42 + 43 + properties: 44 + $nodename: 45 + pattern: "^cpts(@.*|-[0-9a-f])*$" 46 + 47 + compatible: 48 + oneOf: 49 + - const: ti,am65-cpts 50 + - const: ti,j721e-cpts 51 + 52 + reg: 53 + maxItems: 1 54 + description: 55 + The physical base address and size of CPTS IO range 56 + 57 + reg-names: 58 + items: 59 + - const: cpts 60 + 61 + clocks: 62 + description: CPTS reference clock 63 + 64 + clock-names: 65 + items: 66 + - const: cpts 67 + 68 + interrupts-extended: 69 + items: 70 + - description: CPTS events interrupt 71 + 72 + interrupt-names: 73 + items: 74 + - const: "cpts" 75 + 76 + ti,cpts-ext-ts-inputs: 77 + allOf: 78 + - $ref: /schemas/types.yaml#/definitions/uint32 79 + maximum: 8 80 + description: 81 + Number of hardware timestamp push inputs (HWx_TS_PUSH) 82 + 83 + ti,cpts-periodic-outputs: 84 + allOf: 85 + - $ref: /schemas/types.yaml#/definitions/uint32 86 + maximum: 8 87 + description: 88 + Number of timestamp Generator function outputs (TS_GENFx) 89 + 90 + refclk-mux: 91 + type: object 92 + description: CPTS reference clock multiplexer clock 93 + properties: 94 + '#clock-cells': 95 + const: 0 96 + 97 + clocks: 98 + maxItems: 8 99 + 100 + assigned-clocks: 101 + maxItems: 1 102 + 103 + assigned-clocks-parents: 104 + maxItems: 1 105 + 106 + required: 107 + - clocks 108 + 109 + required: 110 + - clocks 111 + - clock-names 112 + - interrupts-extended 113 + - interrupt-names 114 + 115 + additionalProperties: false 116 + 117 + examples: 118 + - | 119 + #include <dt-bindings/interrupt-controller/irq.h> 120 + #include <dt-bindings/interrupt-controller/arm-gic.h> 121 + 122 + cpts@310d0000 { 123 + compatible = "ti,am65-cpts"; 124 + reg = <0x0 0x310d0000 0x0 0x400>; 125 + reg-names = "cpts"; 126 + clocks = <&main_cpts_mux>; 127 + clock-names = "cpts"; 128 + interrupts-extended = <&k3_irq 163 0 IRQ_TYPE_LEVEL_HIGH>; 129 + interrupt-names = "cpts"; 130 + ti,cpts-periodic-outputs = <6>; 131 + ti,cpts-ext-ts-inputs = <8>; 132 + 133 + main_cpts_mux: refclk-mux { 134 + #clock-cells = <0>; 135 + clocks = <&k3_clks 118 5>, <&k3_clks 118 11>, 136 + <&k3_clks 157 91>, <&k3_clks 157 77>, 137 + <&k3_clks 157 102>, <&k3_clks 157 80>, 138 + <&k3_clks 120 3>, <&k3_clks 121 3>; 139 + assigned-clocks = <&main_cpts_mux>; 140 + assigned-clock-parents = <&k3_clks 118 11>; 141 + }; 142 + }; 143 + - | 144 + 145 + cpts { 146 + clocks = <&k3_clks 18 2>; 147 + clock-names = "cpts"; 148 + interrupts-extended = <&gic500 GIC_SPI 858 IRQ_TYPE_LEVEL_HIGH>; 149 + interrupt-names = "cpts"; 150 + ti,cpts-ext-ts-inputs = <4>; 151 + ti,cpts-periodic-outputs = <2>; 152 + };
+22
arch/arm64/boot/dts/ti/k3-am65-main.dtsi
··· 570 570 <0x5>; /* RX_CHAN */ 571 571 ti,sci-rm-range-rflow = <0x6>; /* GP RFLOW */ 572 572 }; 573 + 574 + cpts@310d0000 { 575 + compatible = "ti,am65-cpts"; 576 + reg = <0x0 0x310d0000 0x0 0x400>; 577 + reg-names = "cpts"; 578 + clocks = <&main_cpts_mux>; 579 + clock-names = "cpts"; 580 + interrupts-extended = <&intr_main_navss 163 0>; 581 + interrupt-names = "cpts"; 582 + ti,cpts-periodic-outputs = <6>; 583 + ti,cpts-ext-ts-inputs = <8>; 584 + 585 + main_cpts_mux: refclk-mux { 586 + #clock-cells = <0>; 587 + clocks = <&k3_clks 118 5>, <&k3_clks 118 11>, 588 + <&k3_clks 118 6>, <&k3_clks 118 3>, 589 + <&k3_clks 118 8>, <&k3_clks 118 14>, 590 + <&k3_clks 120 3>, <&k3_clks 121 3>; 591 + assigned-clocks = <&main_cpts_mux>; 592 + assigned-clock-parents = <&k3_clks 118 5>; 593 + }; 594 + }; 573 595 }; 574 596 575 597 main_gpio0: main_gpio0@600000 {
+19
arch/arm64/boot/dts/ti/k3-am65-mcu.dtsi
··· 247 247 clock-names = "fck"; 248 248 bus_freq = <1000000>; 249 249 }; 250 + 251 + cpts { 252 + clocks = <&mcu_cpsw_cpts_mux>; 253 + clock-names = "cpts"; 254 + interrupts-extended = <&gic500 GIC_SPI 570 IRQ_TYPE_LEVEL_HIGH>; 255 + interrupt-names = "cpts"; 256 + ti,cpts-ext-ts-inputs = <4>; 257 + ti,cpts-periodic-outputs = <2>; 258 + 259 + mcu_cpsw_cpts_mux: refclk-mux { 260 + #clock-cells = <0>; 261 + clocks = <&k3_clks 118 5>, <&k3_clks 118 11>, 262 + <&k3_clks 118 6>, <&k3_clks 118 3>, 263 + <&k3_clks 118 8>, <&k3_clks 118 14>, 264 + <&k3_clks 120 3>, <&k3_clks 121 3>; 265 + assigned-clocks = <&mcu_cpsw_cpts_mux>; 266 + assigned-clock-parents = <&k3_clks 118 5>; 267 + }; 268 + }; 250 269 }; 251 270 };
+12
arch/arm64/boot/dts/ti/k3-j721e-main.dtsi
··· 254 254 <0x0c>; /* RX_UHCHAN */ 255 255 ti,sci-rm-range-rflow = <0x00>; /* GP RFLOW */ 256 256 }; 257 + 258 + cpts@310d0000 { 259 + compatible = "ti,j721e-cpts"; 260 + reg = <0x0 0x310d0000 0x0 0x400>; 261 + reg-names = "cpts"; 262 + clocks = <&k3_clks 201 1>; 263 + clock-names = "cpts"; 264 + interrupts-extended = <&main_navss_intr 201 0>; 265 + interrupt-names = "cpts"; 266 + ti,cpts-periodic-outputs = <6>; 267 + ti,cpts-ext-ts-inputs = <8>; 268 + }; 257 269 }; 258 270 259 271 main_pmx0: pinmux@11c000 {
+9
arch/arm64/boot/dts/ti/k3-j721e-mcu-wakeup.dtsi
··· 338 338 clock-names = "fck"; 339 339 bus_freq = <1000000>; 340 340 }; 341 + 342 + cpts { 343 + clocks = <&k3_clks 18 2>; 344 + clock-names = "cpts"; 345 + interrupts-extended = <&gic500 GIC_SPI 858 IRQ_TYPE_LEVEL_HIGH>; 346 + interrupt-names = "cpts"; 347 + ti,cpts-ext-ts-inputs = <4>; 348 + ti,cpts-periodic-outputs = <2>; 349 + }; 341 350 }; 342 351 };
+15
drivers/net/ethernet/ti/Kconfig
··· 100 100 depends on ARCH_K3 && OF && TI_K3_UDMA_GLUE_LAYER 101 101 select TI_DAVINCI_MDIO 102 102 imply PHY_TI_GMII_SEL 103 + imply TI_AM65_CPTS 103 104 help 104 105 This driver supports TI K3 AM654/J721E CPSW2G Ethernet SubSystem. 105 106 The two-port Gigabit Ethernet MAC (MCU_CPSW0) subsystem provides ··· 110 109 111 110 To compile this driver as a module, choose M here: the module 112 111 will be called ti-am65-cpsw-nuss. 112 + 113 + config TI_K3_AM65_CPTS 114 + tristate "TI K3 AM65x CPTS" 115 + depends on ARCH_K3 && OF && PTP_1588_CLOCK 116 + depends on PTP_1588_CLOCK 117 + select NET_PTP_CLASSIFY 118 + help 119 + Say y here to support the TI K3 AM65x CPTS with 1588 features such as 120 + PTP hardware clock for each CPTS device and network packets 121 + timestamping where applicable. 122 + Depending on integration CPTS blocks enable compliance with 123 + the IEEE 1588-2008 standard for a precision clock synchronization 124 + protocol, Ethernet Enhanced Scheduled Traffic Operations (CPTS_ESTFn) 125 + and PCIe Subsystem Precision Time Measurement (PTM). 113 126 114 127 config TI_KEYSTONE_NETCP 115 128 tristate "TI Keystone NETCP Core Support"
+1
drivers/net/ethernet/ti/Makefile
··· 26 26 27 27 obj-$(CONFIG_TI_K3_AM65_CPSW_NUSS) += ti-am65-cpsw-nuss.o 28 28 ti-am65-cpsw-nuss-y := am65-cpsw-nuss.o cpsw_sl.o am65-cpsw-ethtool.o cpsw_ale.o k3-cppi-desc-pool.o 29 + obj-$(CONFIG_TI_K3_AM65_CPTS) += am65-cpts.o
+23 -1
drivers/net/ethernet/ti/am65-cpsw-ethtool.c
··· 12 12 13 13 #include "am65-cpsw-nuss.h" 14 14 #include "cpsw_ale.h" 15 + #include "am65-cpts.h" 15 16 16 17 #define AM65_CPSW_REGDUMP_VER 0x1 17 18 ··· 695 694 hw_stats[i].offset); 696 695 } 697 696 697 + static int am65_cpsw_get_ethtool_ts_info(struct net_device *ndev, 698 + struct ethtool_ts_info *info) 699 + { 700 + struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 701 + 702 + if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 703 + return ethtool_op_get_ts_info(ndev, info); 704 + 705 + info->so_timestamping = 706 + SOF_TIMESTAMPING_TX_HARDWARE | 707 + SOF_TIMESTAMPING_TX_SOFTWARE | 708 + SOF_TIMESTAMPING_RX_HARDWARE | 709 + SOF_TIMESTAMPING_RX_SOFTWARE | 710 + SOF_TIMESTAMPING_SOFTWARE | 711 + SOF_TIMESTAMPING_RAW_HARDWARE; 712 + info->phc_index = am65_cpts_phc_index(common->cpts); 713 + info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON); 714 + info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL); 715 + return 0; 716 + } 717 + 698 718 static u32 am65_cpsw_get_ethtool_priv_flags(struct net_device *ndev) 699 719 { 700 720 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); ··· 752 730 .get_sset_count = am65_cpsw_get_sset_count, 753 731 .get_strings = am65_cpsw_get_strings, 754 732 .get_ethtool_stats = am65_cpsw_get_ethtool_stats, 755 - .get_ts_info = ethtool_op_get_ts_info, 733 + .get_ts_info = am65_cpsw_get_ethtool_ts_info, 756 734 .get_priv_flags = am65_cpsw_get_ethtool_priv_flags, 757 735 .set_priv_flags = am65_cpsw_set_ethtool_priv_flags, 758 736
+172
drivers/net/ethernet/ti/am65-cpsw-nuss.c
··· 30 30 #include "cpsw_sl.h" 31 31 #include "am65-cpsw-nuss.h" 32 32 #include "k3-cppi-desc-pool.h" 33 + #include "am65-cpts.h" 33 34 34 35 #define AM65_CPSW_SS_BASE 0x0 35 36 #define AM65_CPSW_SGMII_BASE 0x100 ··· 669 668 dev_kfree_skb_any(skb); 670 669 } 671 670 671 + static void am65_cpsw_nuss_rx_ts(struct sk_buff *skb, u32 *psdata) 672 + { 673 + struct skb_shared_hwtstamps *ssh; 674 + u64 ns; 675 + 676 + ns = ((u64)psdata[1] << 32) | psdata[0]; 677 + 678 + ssh = skb_hwtstamps(skb); 679 + memset(ssh, 0, sizeof(*ssh)); 680 + ssh->hwtstamp = ns_to_ktime(ns); 681 + } 682 + 672 683 /* RX psdata[2] word format - checksum information */ 673 684 #define AM65_CPSW_RX_PSD_CSUM_ADD GENMASK(15, 0) 674 685 #define AM65_CPSW_RX_PSD_CSUM_ERR BIT(16) ··· 758 745 skb->dev = ndev; 759 746 760 747 psdata = cppi5_hdesc_get_psdata(desc_rx); 748 + /* add RX timestamp */ 749 + if (port->rx_ts_enabled) 750 + am65_cpsw_nuss_rx_ts(skb, psdata); 761 751 csum_info = psdata[2]; 762 752 dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info); 763 753 ··· 920 904 921 905 ndev = skb->dev; 922 906 907 + am65_cpts_tx_timestamp(common->cpts, skb); 908 + 923 909 ndev_priv = netdev_priv(ndev); 924 910 stats = this_cpu_ptr(ndev_priv->stats); 925 911 u64_stats_update_begin(&stats->syncp); ··· 1012 994 1013 995 /* padding enabled in hw */ 1014 996 pkt_len = skb_headlen(skb); 997 + 998 + /* SKB TX timestamp */ 999 + if (port->tx_ts_enabled) 1000 + am65_cpts_prep_tx_timestamp(common->cpts, skb); 1015 1001 1016 1002 q_idx = skb_get_queue_mapping(skb); 1017 1003 dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx); ··· 1180 1158 return 0; 1181 1159 } 1182 1160 1161 + static int am65_cpsw_nuss_hwtstamp_set(struct net_device *ndev, 1162 + struct ifreq *ifr) 1163 + { 1164 + struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 1165 + struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1166 + u32 ts_ctrl, seq_id, ts_ctrl_ltype2, ts_vlan_ltype; 1167 + struct hwtstamp_config cfg; 1168 + 1169 + if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1170 + return -EOPNOTSUPP; 1171 + 1172 + if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 1173 + return -EFAULT; 1174 + 1175 + /* TX HW timestamp */ 1176 + switch (cfg.tx_type) { 1177 + case HWTSTAMP_TX_OFF: 1178 + case HWTSTAMP_TX_ON: 1179 + break; 1180 + default: 1181 + return -ERANGE; 1182 + } 1183 + 1184 + switch (cfg.rx_filter) { 1185 + case HWTSTAMP_FILTER_NONE: 1186 + port->rx_ts_enabled = false; 1187 + break; 1188 + case HWTSTAMP_FILTER_ALL: 1189 + case HWTSTAMP_FILTER_SOME: 1190 + case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 1191 + case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 1192 + case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 1193 + case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 1194 + case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 1195 + case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 1196 + case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 1197 + case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 1198 + case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 1199 + case HWTSTAMP_FILTER_PTP_V2_EVENT: 1200 + case HWTSTAMP_FILTER_PTP_V2_SYNC: 1201 + case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 1202 + case HWTSTAMP_FILTER_NTP_ALL: 1203 + port->rx_ts_enabled = true; 1204 + cfg.rx_filter = HWTSTAMP_FILTER_ALL; 1205 + break; 1206 + default: 1207 + return -ERANGE; 1208 + } 1209 + 1210 + port->tx_ts_enabled = (cfg.tx_type == HWTSTAMP_TX_ON); 1211 + 1212 + /* cfg TX timestamp */ 1213 + seq_id = (AM65_CPSW_TS_SEQ_ID_OFFSET << 1214 + AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT) | ETH_P_1588; 1215 + 1216 + ts_vlan_ltype = ETH_P_8021Q; 1217 + 1218 + ts_ctrl_ltype2 = ETH_P_1588 | 1219 + AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 | 1220 + AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 | 1221 + AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 | 1222 + AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 | 1223 + AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 | 1224 + AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 | 1225 + AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 | 1226 + AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO; 1227 + 1228 + ts_ctrl = AM65_CPSW_TS_EVENT_MSG_TYPE_BITS << 1229 + AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT; 1230 + 1231 + if (port->tx_ts_enabled) 1232 + ts_ctrl |= AM65_CPSW_TS_TX_ANX_ALL_EN | 1233 + AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN; 1234 + 1235 + writel(seq_id, port->port_base + AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG); 1236 + writel(ts_vlan_ltype, port->port_base + 1237 + AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG); 1238 + writel(ts_ctrl_ltype2, port->port_base + 1239 + AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2); 1240 + writel(ts_ctrl, port->port_base + AM65_CPSW_PORTN_REG_TS_CTL); 1241 + 1242 + /* en/dis RX timestamp */ 1243 + am65_cpts_rx_enable(common->cpts, port->rx_ts_enabled); 1244 + 1245 + return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1246 + } 1247 + 1248 + static int am65_cpsw_nuss_hwtstamp_get(struct net_device *ndev, 1249 + struct ifreq *ifr) 1250 + { 1251 + struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1252 + struct hwtstamp_config cfg; 1253 + 1254 + if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1255 + return -EOPNOTSUPP; 1256 + 1257 + cfg.flags = 0; 1258 + cfg.tx_type = port->tx_ts_enabled ? 1259 + HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 1260 + cfg.rx_filter = port->rx_ts_enabled ? 1261 + HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE; 1262 + 1263 + return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1264 + } 1265 + 1183 1266 static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev, 1184 1267 struct ifreq *req, int cmd) 1185 1268 { ··· 1292 1165 1293 1166 if (!netif_running(ndev)) 1294 1167 return -EINVAL; 1168 + 1169 + switch (cmd) { 1170 + case SIOCSHWTSTAMP: 1171 + return am65_cpsw_nuss_hwtstamp_set(ndev, req); 1172 + case SIOCGHWTSTAMP: 1173 + return am65_cpsw_nuss_hwtstamp_get(ndev, req); 1174 + } 1295 1175 1296 1176 if (!port->slave.phy) 1297 1177 return -EOPNOTSUPP; ··· 1661 1527 mac_addr[3] = (mac_lo >> 16) & 0xff; 1662 1528 mac_addr[4] = (mac_lo >> 8) & 0xff; 1663 1529 mac_addr[5] = mac_lo & 0xff; 1530 + 1531 + return 0; 1532 + } 1533 + 1534 + static int am65_cpsw_init_cpts(struct am65_cpsw_common *common) 1535 + { 1536 + struct device *dev = common->dev; 1537 + struct device_node *node; 1538 + struct am65_cpts *cpts; 1539 + void __iomem *reg_base; 1540 + 1541 + if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1542 + return 0; 1543 + 1544 + node = of_get_child_by_name(dev->of_node, "cpts"); 1545 + if (!node) { 1546 + dev_err(dev, "%s cpts not found\n", __func__); 1547 + return -ENOENT; 1548 + } 1549 + 1550 + reg_base = common->cpsw_base + AM65_CPSW_NU_CPTS_BASE; 1551 + cpts = am65_cpts_create(dev, reg_base, node); 1552 + if (IS_ERR(cpts)) { 1553 + int ret = PTR_ERR(cpts); 1554 + 1555 + if (ret == -EOPNOTSUPP) { 1556 + dev_info(dev, "cpts disabled\n"); 1557 + return 0; 1558 + } 1559 + 1560 + dev_err(dev, "cpts create err %d\n", ret); 1561 + return ret; 1562 + } 1563 + common->cpts = cpts; 1664 1564 1665 1565 return 0; 1666 1566 } ··· 2066 1898 dev_err(dev, "error initializing ale engine\n"); 2067 1899 goto err_of_clear; 2068 1900 } 1901 + 1902 + ret = am65_cpsw_init_cpts(common); 1903 + if (ret) 1904 + goto err_of_clear; 2069 1905 2070 1906 /* init ports */ 2071 1907 for (i = 0; i < common->port_num; i++)
+5 -1
drivers/net/ethernet/ti/am65-cpsw-nuss.h
··· 10 10 #include <linux/module.h> 11 11 #include <linux/netdevice.h> 12 12 13 + struct am65_cpts; 14 + 13 15 #define HOST_PORT_NUM 0 14 16 15 17 #define AM65_CPSW_MAX_TX_QUEUES 8 ··· 39 37 void __iomem *stat_base; 40 38 bool disabled; 41 39 struct am65_cpsw_slave_data slave; 40 + bool tx_ts_enabled; 41 + bool rx_ts_enabled; 42 42 }; 43 43 44 44 struct am65_cpsw_host { ··· 100 96 101 97 u32 nuss_ver; 102 98 u32 cpsw_ver; 103 - 104 99 bool pf_p0_rx_ptype_rrobin; 100 + struct am65_cpts *cpts; 105 101 }; 106 102 107 103 struct am65_cpsw_ndev_stats {
+1041
drivers/net/ethernet/ti/am65-cpts.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* TI K3 AM65x Common Platform Time Sync 3 + * 4 + * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com 5 + * 6 + */ 7 + 8 + #include <linux/clk.h> 9 + #include <linux/clk-provider.h> 10 + #include <linux/err.h> 11 + #include <linux/if_vlan.h> 12 + #include <linux/interrupt.h> 13 + #include <linux/module.h> 14 + #include <linux/netdevice.h> 15 + #include <linux/net_tstamp.h> 16 + #include <linux/of.h> 17 + #include <linux/of_irq.h> 18 + #include <linux/platform_device.h> 19 + #include <linux/pm_runtime.h> 20 + #include <linux/ptp_classify.h> 21 + #include <linux/ptp_clock_kernel.h> 22 + 23 + #include "am65-cpts.h" 24 + 25 + struct am65_genf_regs { 26 + u32 comp_lo; /* Comparison Low Value 0:31 */ 27 + u32 comp_hi; /* Comparison High Value 32:63 */ 28 + u32 control; /* control */ 29 + u32 length; /* Length */ 30 + u32 ppm_low; /* PPM Load Low Value 0:31 */ 31 + u32 ppm_hi; /* PPM Load High Value 32:63 */ 32 + u32 ts_nudge; /* Nudge value */ 33 + } __aligned(32) __packed; 34 + 35 + #define AM65_CPTS_GENF_MAX_NUM 9 36 + #define AM65_CPTS_ESTF_MAX_NUM 8 37 + 38 + struct am65_cpts_regs { 39 + u32 idver; /* Identification and version */ 40 + u32 control; /* Time sync control */ 41 + u32 rftclk_sel; /* Reference Clock Select Register */ 42 + u32 ts_push; /* Time stamp event push */ 43 + u32 ts_load_val_lo; /* Time Stamp Load Low Value 0:31 */ 44 + u32 ts_load_en; /* Time stamp load enable */ 45 + u32 ts_comp_lo; /* Time Stamp Comparison Low Value 0:31 */ 46 + u32 ts_comp_length; /* Time Stamp Comparison Length */ 47 + u32 intstat_raw; /* Time sync interrupt status raw */ 48 + u32 intstat_masked; /* Time sync interrupt status masked */ 49 + u32 int_enable; /* Time sync interrupt enable */ 50 + u32 ts_comp_nudge; /* Time Stamp Comparison Nudge Value */ 51 + u32 event_pop; /* Event interrupt pop */ 52 + u32 event_0; /* Event Time Stamp lo 0:31 */ 53 + u32 event_1; /* Event Type Fields */ 54 + u32 event_2; /* Event Type Fields domain */ 55 + u32 event_3; /* Event Time Stamp hi 32:63 */ 56 + u32 ts_load_val_hi; /* Time Stamp Load High Value 32:63 */ 57 + u32 ts_comp_hi; /* Time Stamp Comparison High Value 32:63 */ 58 + u32 ts_add_val; /* Time Stamp Add value */ 59 + u32 ts_ppm_low; /* Time Stamp PPM Load Low Value 0:31 */ 60 + u32 ts_ppm_hi; /* Time Stamp PPM Load High Value 32:63 */ 61 + u32 ts_nudge; /* Time Stamp Nudge value */ 62 + u32 reserv[33]; 63 + struct am65_genf_regs genf[AM65_CPTS_GENF_MAX_NUM]; 64 + struct am65_genf_regs estf[AM65_CPTS_ESTF_MAX_NUM]; 65 + }; 66 + 67 + /* CONTROL_REG */ 68 + #define AM65_CPTS_CONTROL_EN BIT(0) 69 + #define AM65_CPTS_CONTROL_INT_TEST BIT(1) 70 + #define AM65_CPTS_CONTROL_TS_COMP_POLARITY BIT(2) 71 + #define AM65_CPTS_CONTROL_TSTAMP_EN BIT(3) 72 + #define AM65_CPTS_CONTROL_SEQUENCE_EN BIT(4) 73 + #define AM65_CPTS_CONTROL_64MODE BIT(5) 74 + #define AM65_CPTS_CONTROL_TS_COMP_TOG BIT(6) 75 + #define AM65_CPTS_CONTROL_TS_PPM_DIR BIT(7) 76 + #define AM65_CPTS_CONTROL_HW1_TS_PUSH_EN BIT(8) 77 + #define AM65_CPTS_CONTROL_HW2_TS_PUSH_EN BIT(9) 78 + #define AM65_CPTS_CONTROL_HW3_TS_PUSH_EN BIT(10) 79 + #define AM65_CPTS_CONTROL_HW4_TS_PUSH_EN BIT(11) 80 + #define AM65_CPTS_CONTROL_HW5_TS_PUSH_EN BIT(12) 81 + #define AM65_CPTS_CONTROL_HW6_TS_PUSH_EN BIT(13) 82 + #define AM65_CPTS_CONTROL_HW7_TS_PUSH_EN BIT(14) 83 + #define AM65_CPTS_CONTROL_HW8_TS_PUSH_EN BIT(15) 84 + #define AM65_CPTS_CONTROL_HW1_TS_PUSH_OFFSET (8) 85 + 86 + #define AM65_CPTS_CONTROL_TS_SYNC_SEL_MASK (0xF) 87 + #define AM65_CPTS_CONTROL_TS_SYNC_SEL_SHIFT (28) 88 + 89 + /* RFTCLK_SEL_REG */ 90 + #define AM65_CPTS_RFTCLK_SEL_MASK (0x1F) 91 + 92 + /* TS_PUSH_REG */ 93 + #define AM65_CPTS_TS_PUSH BIT(0) 94 + 95 + /* TS_LOAD_EN_REG */ 96 + #define AM65_CPTS_TS_LOAD_EN BIT(0) 97 + 98 + /* INTSTAT_RAW_REG */ 99 + #define AM65_CPTS_INTSTAT_RAW_TS_PEND BIT(0) 100 + 101 + /* INTSTAT_MASKED_REG */ 102 + #define AM65_CPTS_INTSTAT_MASKED_TS_PEND BIT(0) 103 + 104 + /* INT_ENABLE_REG */ 105 + #define AM65_CPTS_INT_ENABLE_TS_PEND_EN BIT(0) 106 + 107 + /* TS_COMP_NUDGE_REG */ 108 + #define AM65_CPTS_TS_COMP_NUDGE_MASK (0xFF) 109 + 110 + /* EVENT_POP_REG */ 111 + #define AM65_CPTS_EVENT_POP BIT(0) 112 + 113 + /* EVENT_1_REG */ 114 + #define AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK GENMASK(15, 0) 115 + 116 + #define AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK GENMASK(19, 16) 117 + #define AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT (16) 118 + 119 + #define AM65_CPTS_EVENT_1_EVENT_TYPE_MASK GENMASK(23, 20) 120 + #define AM65_CPTS_EVENT_1_EVENT_TYPE_SHIFT (20) 121 + 122 + #define AM65_CPTS_EVENT_1_PORT_NUMBER_MASK GENMASK(28, 24) 123 + #define AM65_CPTS_EVENT_1_PORT_NUMBER_SHIFT (24) 124 + 125 + /* EVENT_2_REG */ 126 + #define AM65_CPTS_EVENT_2_REG_DOMAIN_MASK (0xFF) 127 + #define AM65_CPTS_EVENT_2_REG_DOMAIN_SHIFT (0) 128 + 129 + enum { 130 + AM65_CPTS_EV_PUSH, /* Time Stamp Push Event */ 131 + AM65_CPTS_EV_ROLL, /* Time Stamp Rollover Event */ 132 + AM65_CPTS_EV_HALF, /* Time Stamp Half Rollover Event */ 133 + AM65_CPTS_EV_HW, /* Hardware Time Stamp Push Event */ 134 + AM65_CPTS_EV_RX, /* Ethernet Receive Event */ 135 + AM65_CPTS_EV_TX, /* Ethernet Transmit Event */ 136 + AM65_CPTS_EV_TS_COMP, /* Time Stamp Compare Event */ 137 + AM65_CPTS_EV_HOST, /* Host Transmit Event */ 138 + }; 139 + 140 + struct am65_cpts_event { 141 + struct list_head list; 142 + unsigned long tmo; 143 + u32 event1; 144 + u32 event2; 145 + u64 timestamp; 146 + }; 147 + 148 + #define AM65_CPTS_FIFO_DEPTH (16) 149 + #define AM65_CPTS_MAX_EVENTS (32) 150 + #define AM65_CPTS_EVENT_RX_TX_TIMEOUT (20) /* ms */ 151 + #define AM65_CPTS_SKB_TX_WORK_TIMEOUT 1 /* jiffies */ 152 + #define AM65_CPTS_MIN_PPM 0x400 153 + 154 + struct am65_cpts { 155 + struct device *dev; 156 + struct am65_cpts_regs __iomem *reg; 157 + struct ptp_clock_info ptp_info; 158 + struct ptp_clock *ptp_clock; 159 + int phc_index; 160 + struct clk_hw *clk_mux_hw; 161 + struct device_node *clk_mux_np; 162 + struct clk *refclk; 163 + u32 refclk_freq; 164 + struct list_head events; 165 + struct list_head pool; 166 + struct am65_cpts_event pool_data[AM65_CPTS_MAX_EVENTS]; 167 + spinlock_t lock; /* protects events lists*/ 168 + u32 ext_ts_inputs; 169 + u32 genf_num; 170 + u32 ts_add_val; 171 + int irq; 172 + struct mutex ptp_clk_lock; /* PHC access sync */ 173 + u64 timestamp; 174 + u32 genf_enable; 175 + u32 hw_ts_enable; 176 + struct sk_buff_head txq; 177 + }; 178 + 179 + struct am65_cpts_skb_cb_data { 180 + unsigned long tmo; 181 + u32 skb_mtype_seqid; 182 + }; 183 + 184 + #define am65_cpts_write32(c, v, r) writel(v, &(c)->reg->r) 185 + #define am65_cpts_read32(c, r) readl(&(c)->reg->r) 186 + 187 + static void am65_cpts_settime(struct am65_cpts *cpts, u64 start_tstamp) 188 + { 189 + u32 val; 190 + 191 + val = upper_32_bits(start_tstamp); 192 + am65_cpts_write32(cpts, val, ts_load_val_hi); 193 + val = lower_32_bits(start_tstamp); 194 + am65_cpts_write32(cpts, val, ts_load_val_lo); 195 + 196 + am65_cpts_write32(cpts, AM65_CPTS_TS_LOAD_EN, ts_load_en); 197 + } 198 + 199 + static void am65_cpts_set_add_val(struct am65_cpts *cpts) 200 + { 201 + /* select coefficient according to the rate */ 202 + cpts->ts_add_val = (NSEC_PER_SEC / cpts->refclk_freq - 1) & 0x7; 203 + 204 + am65_cpts_write32(cpts, cpts->ts_add_val, ts_add_val); 205 + } 206 + 207 + static void am65_cpts_disable(struct am65_cpts *cpts) 208 + { 209 + am65_cpts_write32(cpts, 0, control); 210 + am65_cpts_write32(cpts, 0, int_enable); 211 + } 212 + 213 + static int am65_cpts_event_get_port(struct am65_cpts_event *event) 214 + { 215 + return (event->event1 & AM65_CPTS_EVENT_1_PORT_NUMBER_MASK) >> 216 + AM65_CPTS_EVENT_1_PORT_NUMBER_SHIFT; 217 + } 218 + 219 + static int am65_cpts_event_get_type(struct am65_cpts_event *event) 220 + { 221 + return (event->event1 & AM65_CPTS_EVENT_1_EVENT_TYPE_MASK) >> 222 + AM65_CPTS_EVENT_1_EVENT_TYPE_SHIFT; 223 + } 224 + 225 + static int am65_cpts_cpts_purge_events(struct am65_cpts *cpts) 226 + { 227 + struct list_head *this, *next; 228 + struct am65_cpts_event *event; 229 + int removed = 0; 230 + 231 + list_for_each_safe(this, next, &cpts->events) { 232 + event = list_entry(this, struct am65_cpts_event, list); 233 + if (time_after(jiffies, event->tmo)) { 234 + list_del_init(&event->list); 235 + list_add(&event->list, &cpts->pool); 236 + ++removed; 237 + } 238 + } 239 + 240 + if (removed) 241 + dev_dbg(cpts->dev, "event pool cleaned up %d\n", removed); 242 + return removed ? 0 : -1; 243 + } 244 + 245 + static bool am65_cpts_fifo_pop_event(struct am65_cpts *cpts, 246 + struct am65_cpts_event *event) 247 + { 248 + u32 r = am65_cpts_read32(cpts, intstat_raw); 249 + 250 + if (r & AM65_CPTS_INTSTAT_RAW_TS_PEND) { 251 + event->timestamp = am65_cpts_read32(cpts, event_0); 252 + event->event1 = am65_cpts_read32(cpts, event_1); 253 + event->event2 = am65_cpts_read32(cpts, event_2); 254 + event->timestamp |= (u64)am65_cpts_read32(cpts, event_3) << 32; 255 + am65_cpts_write32(cpts, AM65_CPTS_EVENT_POP, event_pop); 256 + return false; 257 + } 258 + return true; 259 + } 260 + 261 + static int am65_cpts_fifo_read(struct am65_cpts *cpts) 262 + { 263 + struct ptp_clock_event pevent; 264 + struct am65_cpts_event *event; 265 + bool schedule = false; 266 + int i, type, ret = 0; 267 + unsigned long flags; 268 + 269 + spin_lock_irqsave(&cpts->lock, flags); 270 + for (i = 0; i < AM65_CPTS_FIFO_DEPTH; i++) { 271 + event = list_first_entry_or_null(&cpts->pool, 272 + struct am65_cpts_event, list); 273 + 274 + if (!event) { 275 + if (am65_cpts_cpts_purge_events(cpts)) { 276 + dev_err(cpts->dev, "cpts: event pool empty\n"); 277 + ret = -1; 278 + goto out; 279 + } 280 + continue; 281 + } 282 + 283 + if (am65_cpts_fifo_pop_event(cpts, event)) 284 + break; 285 + 286 + type = am65_cpts_event_get_type(event); 287 + switch (type) { 288 + case AM65_CPTS_EV_PUSH: 289 + cpts->timestamp = event->timestamp; 290 + dev_dbg(cpts->dev, "AM65_CPTS_EV_PUSH t:%llu\n", 291 + cpts->timestamp); 292 + break; 293 + case AM65_CPTS_EV_RX: 294 + case AM65_CPTS_EV_TX: 295 + event->tmo = jiffies + 296 + msecs_to_jiffies(AM65_CPTS_EVENT_RX_TX_TIMEOUT); 297 + 298 + list_del_init(&event->list); 299 + list_add_tail(&event->list, &cpts->events); 300 + 301 + dev_dbg(cpts->dev, 302 + "AM65_CPTS_EV_TX e1:%08x e2:%08x t:%lld\n", 303 + event->event1, event->event2, 304 + event->timestamp); 305 + schedule = true; 306 + break; 307 + case AM65_CPTS_EV_HW: 308 + pevent.index = am65_cpts_event_get_port(event) - 1; 309 + pevent.timestamp = event->timestamp; 310 + pevent.type = PTP_CLOCK_EXTTS; 311 + dev_dbg(cpts->dev, "AM65_CPTS_EV_HW p:%d t:%llu\n", 312 + pevent.index, event->timestamp); 313 + 314 + ptp_clock_event(cpts->ptp_clock, &pevent); 315 + break; 316 + case AM65_CPTS_EV_HOST: 317 + break; 318 + case AM65_CPTS_EV_ROLL: 319 + case AM65_CPTS_EV_HALF: 320 + case AM65_CPTS_EV_TS_COMP: 321 + dev_dbg(cpts->dev, 322 + "AM65_CPTS_EVT: %d e1:%08x e2:%08x t:%lld\n", 323 + type, 324 + event->event1, event->event2, 325 + event->timestamp); 326 + break; 327 + default: 328 + dev_err(cpts->dev, "cpts: unknown event type\n"); 329 + ret = -1; 330 + goto out; 331 + } 332 + } 333 + 334 + out: 335 + spin_unlock_irqrestore(&cpts->lock, flags); 336 + 337 + if (schedule) 338 + ptp_schedule_worker(cpts->ptp_clock, 0); 339 + 340 + return ret; 341 + } 342 + 343 + static u64 am65_cpts_gettime(struct am65_cpts *cpts, 344 + struct ptp_system_timestamp *sts) 345 + { 346 + unsigned long flags; 347 + u64 val = 0; 348 + 349 + /* temporarily disable cpts interrupt to avoid intentional 350 + * doubled read. Interrupt can be in-flight - it's Ok. 351 + */ 352 + am65_cpts_write32(cpts, 0, int_enable); 353 + 354 + /* use spin_lock_irqsave() here as it has to run very fast */ 355 + spin_lock_irqsave(&cpts->lock, flags); 356 + ptp_read_system_prets(sts); 357 + am65_cpts_write32(cpts, AM65_CPTS_TS_PUSH, ts_push); 358 + am65_cpts_read32(cpts, ts_push); 359 + ptp_read_system_postts(sts); 360 + spin_unlock_irqrestore(&cpts->lock, flags); 361 + 362 + am65_cpts_fifo_read(cpts); 363 + 364 + am65_cpts_write32(cpts, AM65_CPTS_INT_ENABLE_TS_PEND_EN, int_enable); 365 + 366 + val = cpts->timestamp; 367 + 368 + return val; 369 + } 370 + 371 + static irqreturn_t am65_cpts_interrupt(int irq, void *dev_id) 372 + { 373 + struct am65_cpts *cpts = dev_id; 374 + 375 + if (am65_cpts_fifo_read(cpts)) 376 + dev_dbg(cpts->dev, "cpts: unable to obtain a time stamp\n"); 377 + 378 + return IRQ_HANDLED; 379 + } 380 + 381 + /* PTP clock operations */ 382 + static int am65_cpts_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb) 383 + { 384 + struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 385 + int neg_adj = 0; 386 + u64 adj_period; 387 + u32 val; 388 + 389 + if (ppb < 0) { 390 + neg_adj = 1; 391 + ppb = -ppb; 392 + } 393 + 394 + /* base freq = 1GHz = 1 000 000 000 395 + * ppb_norm = ppb * base_freq / clock_freq; 396 + * ppm_norm = ppb_norm / 1000 397 + * adj_period = 1 000 000 / ppm_norm 398 + * adj_period = 1 000 000 000 / ppb_norm 399 + * adj_period = 1 000 000 000 / (ppb * base_freq / clock_freq) 400 + * adj_period = (1 000 000 000 * clock_freq) / (ppb * base_freq) 401 + * adj_period = clock_freq / ppb 402 + */ 403 + adj_period = div_u64(cpts->refclk_freq, ppb); 404 + 405 + mutex_lock(&cpts->ptp_clk_lock); 406 + 407 + val = am65_cpts_read32(cpts, control); 408 + if (neg_adj) 409 + val |= AM65_CPTS_CONTROL_TS_PPM_DIR; 410 + else 411 + val &= ~AM65_CPTS_CONTROL_TS_PPM_DIR; 412 + am65_cpts_write32(cpts, val, control); 413 + 414 + val = upper_32_bits(adj_period) & 0x3FF; 415 + am65_cpts_write32(cpts, val, ts_ppm_hi); 416 + val = lower_32_bits(adj_period); 417 + am65_cpts_write32(cpts, val, ts_ppm_low); 418 + 419 + mutex_unlock(&cpts->ptp_clk_lock); 420 + 421 + return 0; 422 + } 423 + 424 + static int am65_cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) 425 + { 426 + struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 427 + s64 ns; 428 + 429 + mutex_lock(&cpts->ptp_clk_lock); 430 + ns = am65_cpts_gettime(cpts, NULL); 431 + ns += delta; 432 + am65_cpts_settime(cpts, ns); 433 + mutex_unlock(&cpts->ptp_clk_lock); 434 + 435 + return 0; 436 + } 437 + 438 + static int am65_cpts_ptp_gettimex(struct ptp_clock_info *ptp, 439 + struct timespec64 *ts, 440 + struct ptp_system_timestamp *sts) 441 + { 442 + struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 443 + u64 ns; 444 + 445 + mutex_lock(&cpts->ptp_clk_lock); 446 + ns = am65_cpts_gettime(cpts, sts); 447 + mutex_unlock(&cpts->ptp_clk_lock); 448 + *ts = ns_to_timespec64(ns); 449 + 450 + return 0; 451 + } 452 + 453 + static int am65_cpts_ptp_settime(struct ptp_clock_info *ptp, 454 + const struct timespec64 *ts) 455 + { 456 + struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 457 + u64 ns; 458 + 459 + ns = timespec64_to_ns(ts); 460 + mutex_lock(&cpts->ptp_clk_lock); 461 + am65_cpts_settime(cpts, ns); 462 + mutex_unlock(&cpts->ptp_clk_lock); 463 + 464 + return 0; 465 + } 466 + 467 + static void am65_cpts_extts_enable_hw(struct am65_cpts *cpts, u32 index, int on) 468 + { 469 + u32 v; 470 + 471 + v = am65_cpts_read32(cpts, control); 472 + if (on) { 473 + v |= BIT(AM65_CPTS_CONTROL_HW1_TS_PUSH_OFFSET + index); 474 + cpts->hw_ts_enable |= BIT(index); 475 + } else { 476 + v &= ~BIT(AM65_CPTS_CONTROL_HW1_TS_PUSH_OFFSET + index); 477 + cpts->hw_ts_enable &= ~BIT(index); 478 + } 479 + am65_cpts_write32(cpts, v, control); 480 + } 481 + 482 + static int am65_cpts_extts_enable(struct am65_cpts *cpts, u32 index, int on) 483 + { 484 + if (!!(cpts->hw_ts_enable & BIT(index)) == !!on) 485 + return 0; 486 + 487 + mutex_lock(&cpts->ptp_clk_lock); 488 + am65_cpts_extts_enable_hw(cpts, index, on); 489 + mutex_unlock(&cpts->ptp_clk_lock); 490 + 491 + dev_dbg(cpts->dev, "%s: ExtTS:%u %s\n", 492 + __func__, index, on ? "enabled" : "disabled"); 493 + 494 + return 0; 495 + } 496 + 497 + static void am65_cpts_perout_enable_hw(struct am65_cpts *cpts, 498 + struct ptp_perout_request *req, int on) 499 + { 500 + u64 ns_period, ns_start, cycles; 501 + struct timespec64 ts; 502 + u32 val; 503 + 504 + if (on) { 505 + ts.tv_sec = req->period.sec; 506 + ts.tv_nsec = req->period.nsec; 507 + ns_period = timespec64_to_ns(&ts); 508 + 509 + cycles = (ns_period * cpts->refclk_freq) / NSEC_PER_SEC; 510 + 511 + ts.tv_sec = req->start.sec; 512 + ts.tv_nsec = req->start.nsec; 513 + ns_start = timespec64_to_ns(&ts); 514 + 515 + val = upper_32_bits(ns_start); 516 + am65_cpts_write32(cpts, val, genf[req->index].comp_hi); 517 + val = lower_32_bits(ns_start); 518 + am65_cpts_write32(cpts, val, genf[req->index].comp_lo); 519 + val = lower_32_bits(cycles); 520 + am65_cpts_write32(cpts, val, genf[req->index].length); 521 + 522 + cpts->genf_enable |= BIT(req->index); 523 + } else { 524 + am65_cpts_write32(cpts, 0, genf[req->index].length); 525 + 526 + cpts->genf_enable &= ~BIT(req->index); 527 + } 528 + } 529 + 530 + static int am65_cpts_perout_enable(struct am65_cpts *cpts, 531 + struct ptp_perout_request *req, int on) 532 + { 533 + if (!!(cpts->genf_enable & BIT(req->index)) == !!on) 534 + return 0; 535 + 536 + mutex_lock(&cpts->ptp_clk_lock); 537 + am65_cpts_perout_enable_hw(cpts, req, on); 538 + mutex_unlock(&cpts->ptp_clk_lock); 539 + 540 + dev_dbg(cpts->dev, "%s: GenF:%u %s\n", 541 + __func__, req->index, on ? "enabled" : "disabled"); 542 + 543 + return 0; 544 + } 545 + 546 + static int am65_cpts_ptp_enable(struct ptp_clock_info *ptp, 547 + struct ptp_clock_request *rq, int on) 548 + { 549 + struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 550 + 551 + switch (rq->type) { 552 + case PTP_CLK_REQ_EXTTS: 553 + return am65_cpts_extts_enable(cpts, rq->extts.index, on); 554 + case PTP_CLK_REQ_PEROUT: 555 + return am65_cpts_perout_enable(cpts, &rq->perout, on); 556 + default: 557 + break; 558 + } 559 + 560 + return -EOPNOTSUPP; 561 + } 562 + 563 + static long am65_cpts_ts_work(struct ptp_clock_info *ptp); 564 + 565 + static struct ptp_clock_info am65_ptp_info = { 566 + .owner = THIS_MODULE, 567 + .name = "CTPS timer", 568 + .adjfreq = am65_cpts_ptp_adjfreq, 569 + .adjtime = am65_cpts_ptp_adjtime, 570 + .gettimex64 = am65_cpts_ptp_gettimex, 571 + .settime64 = am65_cpts_ptp_settime, 572 + .enable = am65_cpts_ptp_enable, 573 + .do_aux_work = am65_cpts_ts_work, 574 + }; 575 + 576 + static bool am65_cpts_match_tx_ts(struct am65_cpts *cpts, 577 + struct am65_cpts_event *event) 578 + { 579 + struct sk_buff_head txq_list; 580 + struct sk_buff *skb, *tmp; 581 + unsigned long flags; 582 + bool found = false; 583 + u32 mtype_seqid; 584 + 585 + mtype_seqid = event->event1 & 586 + (AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK | 587 + AM65_CPTS_EVENT_1_EVENT_TYPE_MASK | 588 + AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK); 589 + 590 + __skb_queue_head_init(&txq_list); 591 + 592 + spin_lock_irqsave(&cpts->txq.lock, flags); 593 + skb_queue_splice_init(&cpts->txq, &txq_list); 594 + spin_unlock_irqrestore(&cpts->txq.lock, flags); 595 + 596 + /* no need to grab txq.lock as access is always done under cpts->lock */ 597 + skb_queue_walk_safe(&txq_list, skb, tmp) { 598 + struct skb_shared_hwtstamps ssh; 599 + struct am65_cpts_skb_cb_data *skb_cb = 600 + (struct am65_cpts_skb_cb_data *)skb->cb; 601 + 602 + if (mtype_seqid == skb_cb->skb_mtype_seqid) { 603 + u64 ns = event->timestamp; 604 + 605 + memset(&ssh, 0, sizeof(ssh)); 606 + ssh.hwtstamp = ns_to_ktime(ns); 607 + skb_tstamp_tx(skb, &ssh); 608 + found = true; 609 + __skb_unlink(skb, &txq_list); 610 + dev_consume_skb_any(skb); 611 + dev_dbg(cpts->dev, 612 + "match tx timestamp mtype_seqid %08x\n", 613 + mtype_seqid); 614 + break; 615 + } 616 + 617 + if (time_after(jiffies, skb_cb->tmo)) { 618 + /* timeout any expired skbs over 100 ms */ 619 + dev_dbg(cpts->dev, 620 + "expiring tx timestamp mtype_seqid %08x\n", 621 + mtype_seqid); 622 + __skb_unlink(skb, &txq_list); 623 + dev_consume_skb_any(skb); 624 + } 625 + } 626 + 627 + spin_lock_irqsave(&cpts->txq.lock, flags); 628 + skb_queue_splice(&txq_list, &cpts->txq); 629 + spin_unlock_irqrestore(&cpts->txq.lock, flags); 630 + 631 + return found; 632 + } 633 + 634 + static void am65_cpts_find_ts(struct am65_cpts *cpts) 635 + { 636 + struct am65_cpts_event *event; 637 + struct list_head *this, *next; 638 + LIST_HEAD(events_free); 639 + unsigned long flags; 640 + LIST_HEAD(events); 641 + 642 + spin_lock_irqsave(&cpts->lock, flags); 643 + list_splice_init(&cpts->events, &events); 644 + spin_unlock_irqrestore(&cpts->lock, flags); 645 + 646 + list_for_each_safe(this, next, &events) { 647 + event = list_entry(this, struct am65_cpts_event, list); 648 + if (am65_cpts_match_tx_ts(cpts, event) || 649 + time_after(jiffies, event->tmo)) { 650 + list_del_init(&event->list); 651 + list_add(&event->list, &events_free); 652 + } 653 + } 654 + 655 + spin_lock_irqsave(&cpts->lock, flags); 656 + list_splice_tail(&events, &cpts->events); 657 + list_splice_tail(&events_free, &cpts->pool); 658 + spin_unlock_irqrestore(&cpts->lock, flags); 659 + } 660 + 661 + static long am65_cpts_ts_work(struct ptp_clock_info *ptp) 662 + { 663 + struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 664 + unsigned long flags; 665 + long delay = -1; 666 + 667 + am65_cpts_find_ts(cpts); 668 + 669 + spin_lock_irqsave(&cpts->txq.lock, flags); 670 + if (!skb_queue_empty(&cpts->txq)) 671 + delay = AM65_CPTS_SKB_TX_WORK_TIMEOUT; 672 + spin_unlock_irqrestore(&cpts->txq.lock, flags); 673 + 674 + return delay; 675 + } 676 + 677 + /** 678 + * am65_cpts_rx_enable - enable rx timestamping 679 + * @cpts: cpts handle 680 + * @skb: packet 681 + * 682 + * This functions enables rx packets timestamping. The CPTS can timestamp all 683 + * rx packets. 684 + */ 685 + void am65_cpts_rx_enable(struct am65_cpts *cpts, bool en) 686 + { 687 + u32 val; 688 + 689 + mutex_lock(&cpts->ptp_clk_lock); 690 + val = am65_cpts_read32(cpts, control); 691 + if (en) 692 + val |= AM65_CPTS_CONTROL_TSTAMP_EN; 693 + else 694 + val &= ~AM65_CPTS_CONTROL_TSTAMP_EN; 695 + am65_cpts_write32(cpts, val, control); 696 + mutex_unlock(&cpts->ptp_clk_lock); 697 + } 698 + EXPORT_SYMBOL_GPL(am65_cpts_rx_enable); 699 + 700 + static int am65_skb_get_mtype_seqid(struct sk_buff *skb, u32 *mtype_seqid) 701 + { 702 + unsigned int ptp_class = ptp_classify_raw(skb); 703 + u8 *msgtype, *data = skb->data; 704 + unsigned int offset = 0; 705 + __be16 *seqid; 706 + 707 + if (ptp_class == PTP_CLASS_NONE) 708 + return 0; 709 + 710 + if (ptp_class & PTP_CLASS_VLAN) 711 + offset += VLAN_HLEN; 712 + 713 + switch (ptp_class & PTP_CLASS_PMASK) { 714 + case PTP_CLASS_IPV4: 715 + offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN; 716 + break; 717 + case PTP_CLASS_IPV6: 718 + offset += ETH_HLEN + IP6_HLEN + UDP_HLEN; 719 + break; 720 + case PTP_CLASS_L2: 721 + offset += ETH_HLEN; 722 + break; 723 + default: 724 + return 0; 725 + } 726 + 727 + if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid)) 728 + return 0; 729 + 730 + if (unlikely(ptp_class & PTP_CLASS_V1)) 731 + msgtype = data + offset + OFF_PTP_CONTROL; 732 + else 733 + msgtype = data + offset; 734 + 735 + seqid = (__be16 *)(data + offset + OFF_PTP_SEQUENCE_ID); 736 + *mtype_seqid = (*msgtype << AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT) & 737 + AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK; 738 + *mtype_seqid |= (ntohs(*seqid) & AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK); 739 + 740 + return 1; 741 + } 742 + 743 + /** 744 + * am65_cpts_tx_timestamp - save tx packet for timestamping 745 + * @cpts: cpts handle 746 + * @skb: packet 747 + * 748 + * This functions saves tx packet for timestamping if packet can be timestamped. 749 + * The future processing is done in from PTP auxiliary worker. 750 + */ 751 + void am65_cpts_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb) 752 + { 753 + struct am65_cpts_skb_cb_data *skb_cb = (void *)skb->cb; 754 + 755 + if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) 756 + return; 757 + 758 + /* add frame to queue for processing later. 759 + * The periodic FIFO check will handle this. 760 + */ 761 + skb_get(skb); 762 + /* get the timestamp for timeouts */ 763 + skb_cb->tmo = jiffies + msecs_to_jiffies(100); 764 + skb_queue_tail(&cpts->txq, skb); 765 + ptp_schedule_worker(cpts->ptp_clock, 0); 766 + } 767 + EXPORT_SYMBOL_GPL(am65_cpts_tx_timestamp); 768 + 769 + /** 770 + * am65_cpts_prep_tx_timestamp - check and prepare tx packet for timestamping 771 + * @cpts: cpts handle 772 + * @skb: packet 773 + * 774 + * This functions should be called from .xmit(). 775 + * It checks if packet can be timestamped, fills internal cpts data 776 + * in skb-cb and marks packet as SKBTX_IN_PROGRESS. 777 + */ 778 + void am65_cpts_prep_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb) 779 + { 780 + struct am65_cpts_skb_cb_data *skb_cb = (void *)skb->cb; 781 + int ret; 782 + 783 + if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) 784 + return; 785 + 786 + ret = am65_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid); 787 + if (!ret) 788 + return; 789 + skb_cb->skb_mtype_seqid |= (AM65_CPTS_EV_TX << 790 + AM65_CPTS_EVENT_1_EVENT_TYPE_SHIFT); 791 + 792 + skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 793 + } 794 + EXPORT_SYMBOL_GPL(am65_cpts_prep_tx_timestamp); 795 + 796 + int am65_cpts_phc_index(struct am65_cpts *cpts) 797 + { 798 + return cpts->phc_index; 799 + } 800 + EXPORT_SYMBOL_GPL(am65_cpts_phc_index); 801 + 802 + static void cpts_free_clk_mux(void *data) 803 + { 804 + struct am65_cpts *cpts = data; 805 + 806 + of_clk_del_provider(cpts->clk_mux_np); 807 + clk_hw_unregister_mux(cpts->clk_mux_hw); 808 + of_node_put(cpts->clk_mux_np); 809 + } 810 + 811 + static int cpts_of_mux_clk_setup(struct am65_cpts *cpts, 812 + struct device_node *node) 813 + { 814 + unsigned int num_parents; 815 + const char **parent_names; 816 + char *clk_mux_name; 817 + void __iomem *reg; 818 + int ret = -EINVAL; 819 + 820 + cpts->clk_mux_np = of_get_child_by_name(node, "refclk-mux"); 821 + if (!cpts->clk_mux_np) 822 + return 0; 823 + 824 + num_parents = of_clk_get_parent_count(cpts->clk_mux_np); 825 + if (num_parents < 1) { 826 + dev_err(cpts->dev, "mux-clock %pOF must have parents\n", 827 + cpts->clk_mux_np); 828 + goto mux_fail; 829 + } 830 + 831 + parent_names = devm_kcalloc(cpts->dev, sizeof(char *), num_parents, 832 + GFP_KERNEL); 833 + if (!parent_names) { 834 + ret = -ENOMEM; 835 + goto mux_fail; 836 + } 837 + 838 + of_clk_parent_fill(cpts->clk_mux_np, parent_names, num_parents); 839 + 840 + clk_mux_name = devm_kasprintf(cpts->dev, GFP_KERNEL, "%s.%pOFn", 841 + dev_name(cpts->dev), cpts->clk_mux_np); 842 + if (!clk_mux_name) { 843 + ret = -ENOMEM; 844 + goto mux_fail; 845 + } 846 + 847 + reg = &cpts->reg->rftclk_sel; 848 + /* dev must be NULL to avoid recursive incrementing 849 + * of module refcnt 850 + */ 851 + cpts->clk_mux_hw = clk_hw_register_mux(NULL, clk_mux_name, 852 + parent_names, num_parents, 853 + 0, reg, 0, 5, 0, NULL); 854 + if (IS_ERR(cpts->clk_mux_hw)) { 855 + ret = PTR_ERR(cpts->clk_mux_hw); 856 + goto mux_fail; 857 + } 858 + 859 + ret = of_clk_add_hw_provider(cpts->clk_mux_np, of_clk_hw_simple_get, 860 + cpts->clk_mux_hw); 861 + if (ret) 862 + goto clk_hw_register; 863 + 864 + ret = devm_add_action_or_reset(cpts->dev, cpts_free_clk_mux, cpts); 865 + if (ret) 866 + dev_err(cpts->dev, "failed to add clkmux reset action %d", ret); 867 + 868 + return ret; 869 + 870 + clk_hw_register: 871 + clk_hw_unregister_mux(cpts->clk_mux_hw); 872 + mux_fail: 873 + of_node_put(cpts->clk_mux_np); 874 + return ret; 875 + } 876 + 877 + static int am65_cpts_of_parse(struct am65_cpts *cpts, struct device_node *node) 878 + { 879 + u32 prop[2]; 880 + 881 + if (!of_property_read_u32(node, "ti,cpts-ext-ts-inputs", &prop[0])) 882 + cpts->ext_ts_inputs = prop[0]; 883 + 884 + if (!of_property_read_u32(node, "ti,cpts-periodic-outputs", &prop[0])) 885 + cpts->genf_num = prop[0]; 886 + 887 + return cpts_of_mux_clk_setup(cpts, node); 888 + } 889 + 890 + static void am65_cpts_release(void *data) 891 + { 892 + struct am65_cpts *cpts = data; 893 + 894 + ptp_clock_unregister(cpts->ptp_clock); 895 + am65_cpts_disable(cpts); 896 + clk_disable_unprepare(cpts->refclk); 897 + } 898 + 899 + struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs, 900 + struct device_node *node) 901 + { 902 + struct am65_cpts *cpts; 903 + int ret, i; 904 + 905 + cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL); 906 + if (!cpts) 907 + return ERR_PTR(-ENOMEM); 908 + 909 + cpts->dev = dev; 910 + cpts->reg = (struct am65_cpts_regs __iomem *)regs; 911 + 912 + cpts->irq = of_irq_get_byname(node, "cpts"); 913 + if (cpts->irq <= 0) { 914 + ret = cpts->irq ?: -ENXIO; 915 + if (ret != -EPROBE_DEFER) 916 + dev_err(dev, "Failed to get IRQ number (err = %d)\n", 917 + ret); 918 + return ERR_PTR(ret); 919 + } 920 + 921 + ret = am65_cpts_of_parse(cpts, node); 922 + if (ret) 923 + return ERR_PTR(ret); 924 + 925 + mutex_init(&cpts->ptp_clk_lock); 926 + INIT_LIST_HEAD(&cpts->events); 927 + INIT_LIST_HEAD(&cpts->pool); 928 + spin_lock_init(&cpts->lock); 929 + skb_queue_head_init(&cpts->txq); 930 + 931 + for (i = 0; i < AM65_CPTS_MAX_EVENTS; i++) 932 + list_add(&cpts->pool_data[i].list, &cpts->pool); 933 + 934 + cpts->refclk = devm_get_clk_from_child(dev, node, "cpts"); 935 + if (IS_ERR(cpts->refclk)) { 936 + ret = PTR_ERR(cpts->refclk); 937 + if (ret != -EPROBE_DEFER) 938 + dev_err(dev, "Failed to get refclk %d\n", ret); 939 + return ERR_PTR(ret); 940 + } 941 + 942 + ret = clk_prepare_enable(cpts->refclk); 943 + if (ret) { 944 + dev_err(dev, "Failed to enable refclk %d\n", ret); 945 + return ERR_PTR(ret); 946 + } 947 + 948 + cpts->refclk_freq = clk_get_rate(cpts->refclk); 949 + 950 + am65_ptp_info.max_adj = cpts->refclk_freq / AM65_CPTS_MIN_PPM; 951 + cpts->ptp_info = am65_ptp_info; 952 + 953 + if (cpts->ext_ts_inputs) 954 + cpts->ptp_info.n_ext_ts = cpts->ext_ts_inputs; 955 + if (cpts->genf_num) 956 + cpts->ptp_info.n_per_out = cpts->genf_num; 957 + 958 + am65_cpts_set_add_val(cpts); 959 + 960 + am65_cpts_write32(cpts, AM65_CPTS_CONTROL_EN | AM65_CPTS_CONTROL_64MODE, 961 + control); 962 + am65_cpts_write32(cpts, AM65_CPTS_INT_ENABLE_TS_PEND_EN, int_enable); 963 + 964 + /* set time to the current system time */ 965 + am65_cpts_settime(cpts, ktime_to_ns(ktime_get_real())); 966 + 967 + cpts->ptp_clock = ptp_clock_register(&cpts->ptp_info, cpts->dev); 968 + if (IS_ERR_OR_NULL(cpts->ptp_clock)) { 969 + dev_err(dev, "Failed to register ptp clk %ld\n", 970 + PTR_ERR(cpts->ptp_clock)); 971 + if (!cpts->ptp_clock) 972 + ret = -ENODEV; 973 + goto refclk_disable; 974 + } 975 + cpts->phc_index = ptp_clock_index(cpts->ptp_clock); 976 + 977 + ret = devm_add_action_or_reset(dev, am65_cpts_release, cpts); 978 + if (ret) { 979 + dev_err(dev, "failed to add ptpclk reset action %d", ret); 980 + return ERR_PTR(ret); 981 + } 982 + 983 + ret = devm_request_threaded_irq(dev, cpts->irq, NULL, 984 + am65_cpts_interrupt, 985 + IRQF_ONESHOT, dev_name(dev), cpts); 986 + if (ret < 0) { 987 + dev_err(cpts->dev, "error attaching irq %d\n", ret); 988 + return ERR_PTR(ret); 989 + } 990 + 991 + dev_info(dev, "CPTS ver 0x%08x, freq:%u, add_val:%u\n", 992 + am65_cpts_read32(cpts, idver), 993 + cpts->refclk_freq, cpts->ts_add_val); 994 + 995 + return cpts; 996 + 997 + refclk_disable: 998 + clk_disable_unprepare(cpts->refclk); 999 + return ERR_PTR(ret); 1000 + } 1001 + EXPORT_SYMBOL_GPL(am65_cpts_create); 1002 + 1003 + static int am65_cpts_probe(struct platform_device *pdev) 1004 + { 1005 + struct device_node *node = pdev->dev.of_node; 1006 + struct device *dev = &pdev->dev; 1007 + struct am65_cpts *cpts; 1008 + struct resource *res; 1009 + void __iomem *base; 1010 + 1011 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpts"); 1012 + base = devm_ioremap_resource(dev, res); 1013 + if (IS_ERR(base)) 1014 + return PTR_ERR(base); 1015 + 1016 + cpts = am65_cpts_create(dev, base, node); 1017 + if (IS_ERR(cpts)) 1018 + return PTR_ERR(cpts); 1019 + 1020 + return 0; 1021 + } 1022 + 1023 + static const struct of_device_id am65_cpts_of_match[] = { 1024 + { .compatible = "ti,am65-cpts", }, 1025 + { .compatible = "ti,j721e-cpts", }, 1026 + {}, 1027 + }; 1028 + MODULE_DEVICE_TABLE(of, am65_cpts_of_match); 1029 + 1030 + static struct platform_driver am65_cpts_driver = { 1031 + .probe = am65_cpts_probe, 1032 + .driver = { 1033 + .name = "am65-cpts", 1034 + .of_match_table = am65_cpts_of_match, 1035 + }, 1036 + }; 1037 + module_platform_driver(am65_cpts_driver); 1038 + 1039 + MODULE_LICENSE("GPL v2"); 1040 + MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>"); 1041 + MODULE_DESCRIPTION("TI K3 AM65 CPTS driver");
+50
drivers/net/ethernet/ti/am65-cpts.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0+ */ 2 + /* TI K3 AM65 CPTS driver interface 3 + * 4 + * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com 5 + */ 6 + 7 + #ifndef K3_CPTS_H_ 8 + #define K3_CPTS_H_ 9 + 10 + #include <linux/device.h> 11 + #include <linux/of.h> 12 + 13 + struct am65_cpts; 14 + 15 + #if IS_ENABLED(CONFIG_TI_K3_AM65_CPTS) 16 + struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs, 17 + struct device_node *node); 18 + int am65_cpts_phc_index(struct am65_cpts *cpts); 19 + void am65_cpts_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb); 20 + void am65_cpts_prep_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb); 21 + void am65_cpts_rx_enable(struct am65_cpts *cpts, bool en); 22 + #else 23 + static inline struct am65_cpts *am65_cpts_create(struct device *dev, 24 + void __iomem *regs, 25 + struct device_node *node) 26 + { 27 + return ERR_PTR(-EOPNOTSUPP); 28 + } 29 + 30 + static inline int am65_cpts_phc_index(struct am65_cpts *cpts) 31 + { 32 + return -1; 33 + } 34 + 35 + static inline void am65_cpts_tx_timestamp(struct am65_cpts *cpts, 36 + struct sk_buff *skb) 37 + { 38 + } 39 + 40 + static inline void am65_cpts_prep_tx_timestamp(struct am65_cpts *cpts, 41 + struct sk_buff *skb) 42 + { 43 + } 44 + 45 + static inline void am65_cpts_rx_enable(struct am65_cpts *cpts, bool en) 46 + { 47 + } 48 + #endif 49 + 50 + #endif /* K3_CPTS_H_ */