106c16d89SJacob Keller /* SPDX-License-Identifier: GPL-2.0 */ 206c16d89SJacob Keller /* Copyright (C) 2021, Intel Corporation. */ 306c16d89SJacob Keller 406c16d89SJacob Keller #ifndef _ICE_PTP_H_ 506c16d89SJacob Keller #define _ICE_PTP_H_ 606c16d89SJacob Keller 706c16d89SJacob Keller #include <linux/ptp_clock_kernel.h> 877a78115SJacob Keller #include <linux/kthread.h> 906c16d89SJacob Keller 1006c16d89SJacob Keller #include "ice_ptp_hw.h" 1106c16d89SJacob Keller 12325b2064SMaciej Machnikowski enum ice_ptp_pin_e810 { 13172db5f9SMaciej Machnikowski GPIO_20 = 0, 14172db5f9SMaciej Machnikowski GPIO_21, 15172db5f9SMaciej Machnikowski GPIO_22, 16172db5f9SMaciej Machnikowski GPIO_23, 17325b2064SMaciej Machnikowski NUM_PTP_PIN_E810 18325b2064SMaciej Machnikowski }; 19325b2064SMaciej Machnikowski 20325b2064SMaciej Machnikowski enum ice_ptp_pin_e810t { 21325b2064SMaciej Machnikowski GNSS = 0, 22325b2064SMaciej Machnikowski SMA1, 23325b2064SMaciej Machnikowski UFL1, 24325b2064SMaciej Machnikowski SMA2, 25325b2064SMaciej Machnikowski UFL2, 26325b2064SMaciej Machnikowski NUM_PTP_PINS_E810T 27172db5f9SMaciej Machnikowski }; 28172db5f9SMaciej Machnikowski 29172db5f9SMaciej Machnikowski struct ice_perout_channel { 30172db5f9SMaciej Machnikowski bool ena; 31172db5f9SMaciej Machnikowski u32 gpio_pin; 32172db5f9SMaciej Machnikowski u64 period; 33172db5f9SMaciej Machnikowski u64 start_time; 34172db5f9SMaciej Machnikowski }; 35172db5f9SMaciej Machnikowski 36ea9b847cSJacob Keller /* The ice hardware captures Tx hardware timestamps in the PHY. The timestamp 37ea9b847cSJacob Keller * is stored in a buffer of registers. Depending on the specific hardware, 38ea9b847cSJacob Keller * this buffer might be shared across multiple PHY ports. 39ea9b847cSJacob Keller * 40ea9b847cSJacob Keller * On transmit of a packet to be timestamped, software is responsible for 41ea9b847cSJacob Keller * selecting an open index. Hardware makes no attempt to lock or prevent 42ea9b847cSJacob Keller * re-use of an index for multiple packets. 43ea9b847cSJacob Keller * 44ea9b847cSJacob Keller * To handle this, timestamp indexes must be tracked by software to ensure 45ea9b847cSJacob Keller * that an index is not re-used for multiple transmitted packets. The 46ea9b847cSJacob Keller * structures and functions declared in this file track the available Tx 47ea9b847cSJacob Keller * register indexes, as well as provide storage for the SKB pointers. 48ea9b847cSJacob Keller * 49ea9b847cSJacob Keller * To allow multiple ports to access the shared register block independently, 50ea9b847cSJacob Keller * the blocks are split up so that indexes are assigned to each port based on 51ea9b847cSJacob Keller * hardware logical port number. 5271a579f0SMichal Michalik * 5371a579f0SMichal Michalik * The timestamp blocks are handled differently for E810- and E822-based 5471a579f0SMichal Michalik * devices. In E810 devices, each port has its own block of timestamps, while in 5571a579f0SMichal Michalik * E822 there is a need to logically break the block of registers into smaller 5671a579f0SMichal Michalik * chunks based on the port number to avoid collisions. 5771a579f0SMichal Michalik * 5871a579f0SMichal Michalik * Example for port 5 in E810: 5971a579f0SMichal Michalik * +--------+--------+--------+--------+--------+--------+--------+--------+ 6071a579f0SMichal Michalik * |register|register|register|register|register|register|register|register| 6171a579f0SMichal Michalik * | block | block | block | block | block | block | block | block | 6271a579f0SMichal Michalik * | for | for | for | for | for | for | for | for | 6371a579f0SMichal Michalik * | port 0 | port 1 | port 2 | port 3 | port 4 | port 5 | port 6 | port 7 | 6471a579f0SMichal Michalik * +--------+--------+--------+--------+--------+--------+--------+--------+ 6571a579f0SMichal Michalik * ^^ 6671a579f0SMichal Michalik * || 6771a579f0SMichal Michalik * |--- quad offset is always 0 6871a579f0SMichal Michalik * ---- quad number 6971a579f0SMichal Michalik * 7071a579f0SMichal Michalik * Example for port 5 in E822: 7171a579f0SMichal Michalik * +-----------------------------+-----------------------------+ 7271a579f0SMichal Michalik * | register block for quad 0 | register block for quad 1 | 7371a579f0SMichal Michalik * |+------+------+------+------+|+------+------+------+------+| 7471a579f0SMichal Michalik * ||port 0|port 1|port 2|port 3|||port 0|port 1|port 2|port 3|| 7571a579f0SMichal Michalik * |+------+------+------+------+|+------+------+------+------+| 7671a579f0SMichal Michalik * +-----------------------------+-------^---------------------+ 7771a579f0SMichal Michalik * ^ | 7871a579f0SMichal Michalik * | --- quad offset* 7971a579f0SMichal Michalik * ---- quad number 8071a579f0SMichal Michalik * 8171a579f0SMichal Michalik * * PHY port 5 is port 1 in quad 1 8271a579f0SMichal Michalik * 83ea9b847cSJacob Keller */ 84ea9b847cSJacob Keller 85ea9b847cSJacob Keller /** 86ea9b847cSJacob Keller * struct ice_tx_tstamp - Tracking for a single Tx timestamp 87ea9b847cSJacob Keller * @skb: pointer to the SKB for this timestamp request 88ea9b847cSJacob Keller * @start: jiffies when the timestamp was first requested 8937e738b6SKarol Kolacinski * @cached_tstamp: last read timestamp 90ea9b847cSJacob Keller * 91ea9b847cSJacob Keller * This structure tracks a single timestamp request. The SKB pointer is 92ea9b847cSJacob Keller * provided when initiating a request. The start time is used to ensure that 93ea9b847cSJacob Keller * we discard old requests that were not fulfilled within a 2 second time 94ea9b847cSJacob Keller * window. 9537e738b6SKarol Kolacinski * Timestamp values in the PHY are read only and do not get cleared except at 9637e738b6SKarol Kolacinski * hardware reset or when a new timestamp value is captured. The cached_tstamp 9737e738b6SKarol Kolacinski * field is used to detect the case where a new timestamp has not yet been 9837e738b6SKarol Kolacinski * captured, ensuring that we avoid sending stale timestamp data to the stack. 99ea9b847cSJacob Keller */ 100ea9b847cSJacob Keller struct ice_tx_tstamp { 101ea9b847cSJacob Keller struct sk_buff *skb; 102ea9b847cSJacob Keller unsigned long start; 10337e738b6SKarol Kolacinski u64 cached_tstamp; 104ea9b847cSJacob Keller }; 105ea9b847cSJacob Keller 106ea9b847cSJacob Keller /** 107ea9b847cSJacob Keller * struct ice_ptp_tx - Tracking structure for all Tx timestamp requests on a port 108ea9b847cSJacob Keller * @lock: lock to prevent concurrent write to in_use bitmap 109ea9b847cSJacob Keller * @tstamps: array of len to store outstanding requests 110ea9b847cSJacob Keller * @in_use: bitmap of len to indicate which slots are in use 1116b5cbc8cSSergey Temerkhanov * @block: which memory block (quad or port) the timestamps are captured in 1126b5cbc8cSSergey Temerkhanov * @offset: offset into timestamp block to get the real index 113ea9b847cSJacob Keller * @len: length of the tstamps and in_use fields. 114ea9b847cSJacob Keller * @init: if true, the tracker is initialized; 1153a749623SJacob Keller * @calibrating: if true, the PHY is calibrating the Tx offset. During this 1163a749623SJacob Keller * window, timestamps are temporarily disabled. 117ea9b847cSJacob Keller */ 118ea9b847cSJacob Keller struct ice_ptp_tx { 119ea9b847cSJacob Keller spinlock_t lock; /* lock protecting in_use bitmap */ 120ea9b847cSJacob Keller struct ice_tx_tstamp *tstamps; 121ea9b847cSJacob Keller unsigned long *in_use; 1226b5cbc8cSSergey Temerkhanov u8 block; 1236b5cbc8cSSergey Temerkhanov u8 offset; 124ea9b847cSJacob Keller u8 len; 125ea9b847cSJacob Keller u8 init; 1263a749623SJacob Keller u8 calibrating; 127ea9b847cSJacob Keller }; 128ea9b847cSJacob Keller 129ea9b847cSJacob Keller /* Quad and port information for initializing timestamp blocks */ 130ea9b847cSJacob Keller #define INDEX_PER_QUAD 64 1316b5cbc8cSSergey Temerkhanov #define INDEX_PER_PORT_E822 16 1326b5cbc8cSSergey Temerkhanov #define INDEX_PER_PORT_E810 64 133ea9b847cSJacob Keller 134ea9b847cSJacob Keller /** 135ea9b847cSJacob Keller * struct ice_ptp_port - data used to initialize an external port for PTP 136ea9b847cSJacob Keller * 1373a749623SJacob Keller * This structure contains data indicating whether a single external port is 1383a749623SJacob Keller * ready for PTP functionality. It is used to track the port initialization 1393a749623SJacob Keller * and determine when the port's PHY offset is valid. 140ea9b847cSJacob Keller * 141ea9b847cSJacob Keller * @tx: Tx timestamp tracking for this port 142a69f1cb6SJacob Keller * @ov_work: delayed work task for tracking when PHY offset is valid 1433a749623SJacob Keller * @ps_lock: mutex used to protect the overall PTP PHY start procedure 1443a749623SJacob Keller * @link_up: indicates whether the link is up 145a69f1cb6SJacob Keller * @tx_fifo_busy_cnt: number of times the Tx FIFO was busy 1463a749623SJacob Keller * @port_num: the port number this structure represents 147ea9b847cSJacob Keller */ 148ea9b847cSJacob Keller struct ice_ptp_port { 149ea9b847cSJacob Keller struct ice_ptp_tx tx; 150a69f1cb6SJacob Keller struct kthread_delayed_work ov_work; 1513a749623SJacob Keller struct mutex ps_lock; /* protects overall PTP PHY start procedure */ 1523a749623SJacob Keller bool link_up; 153a69f1cb6SJacob Keller u8 tx_fifo_busy_cnt; 1543a749623SJacob Keller u8 port_num; 155ea9b847cSJacob Keller }; 156ea9b847cSJacob Keller 157172db5f9SMaciej Machnikowski #define GLTSYN_TGT_H_IDX_MAX 4 158172db5f9SMaciej Machnikowski 15906c16d89SJacob Keller /** 16006c16d89SJacob Keller * struct ice_ptp - data used for integrating with CONFIG_PTP_1588_CLOCK 161ea9b847cSJacob Keller * @port: data for the PHY port initialization procedure 16277a78115SJacob Keller * @work: delayed work function for periodic tasks 163172db5f9SMaciej Machnikowski * @extts_work: work function for handling external Tx timestamps 16477a78115SJacob Keller * @cached_phc_time: a cached copy of the PHC time for timestamp extension 165cd25507aSJacob Keller * @cached_phc_jiffies: jiffies when cached_phc_time was last updated 166172db5f9SMaciej Machnikowski * @ext_ts_chan: the external timestamp channel in use 167172db5f9SMaciej Machnikowski * @ext_ts_irq: the external timestamp IRQ in use 16877a78115SJacob Keller * @kworker: kwork thread for handling periodic work 169172db5f9SMaciej Machnikowski * @perout_channels: periodic output data 17006c16d89SJacob Keller * @info: structure defining PTP hardware capabilities 17106c16d89SJacob Keller * @clock: pointer to registered PTP clock device 17277a78115SJacob Keller * @tstamp_config: hardware timestamping configuration 17348096710SKarol Kolacinski * @reset_time: kernel time after clock stop on reset 174f020481bSJacob Keller * @tx_hwtstamp_skipped: number of Tx time stamp requests skipped 175f020481bSJacob Keller * @tx_hwtstamp_timeouts: number of Tx skbs discarded with no time stamp 176f020481bSJacob Keller * @tx_hwtstamp_flushed: number of Tx skbs flushed due to interface closed 177cd25507aSJacob Keller * @tx_hwtstamp_discarded: number of Tx skbs discarded due to cached PHC time 178cd25507aSJacob Keller * being too old to correctly extend timestamp 179cd25507aSJacob Keller * @late_cached_phc_updates: number of times cached PHC update is late 18006c16d89SJacob Keller */ 18106c16d89SJacob Keller struct ice_ptp { 182ea9b847cSJacob Keller struct ice_ptp_port port; 18377a78115SJacob Keller struct kthread_delayed_work work; 184172db5f9SMaciej Machnikowski struct kthread_work extts_work; 18577a78115SJacob Keller u64 cached_phc_time; 186cd25507aSJacob Keller unsigned long cached_phc_jiffies; 187172db5f9SMaciej Machnikowski u8 ext_ts_chan; 188172db5f9SMaciej Machnikowski u8 ext_ts_irq; 18977a78115SJacob Keller struct kthread_worker *kworker; 190172db5f9SMaciej Machnikowski struct ice_perout_channel perout_channels[GLTSYN_TGT_H_IDX_MAX]; 19106c16d89SJacob Keller struct ptp_clock_info info; 19206c16d89SJacob Keller struct ptp_clock *clock; 19377a78115SJacob Keller struct hwtstamp_config tstamp_config; 19448096710SKarol Kolacinski u64 reset_time; 195f020481bSJacob Keller u32 tx_hwtstamp_skipped; 196f020481bSJacob Keller u32 tx_hwtstamp_timeouts; 197f020481bSJacob Keller u32 tx_hwtstamp_flushed; 198cd25507aSJacob Keller u32 tx_hwtstamp_discarded; 199cd25507aSJacob Keller u32 late_cached_phc_updates; 20006c16d89SJacob Keller }; 20106c16d89SJacob Keller 202ea9b847cSJacob Keller #define __ptp_port_to_ptp(p) \ 203ea9b847cSJacob Keller container_of((p), struct ice_ptp, port) 204ea9b847cSJacob Keller #define ptp_port_to_pf(p) \ 205ea9b847cSJacob Keller container_of(__ptp_port_to_ptp((p)), struct ice_pf, ptp) 206ea9b847cSJacob Keller 20706c16d89SJacob Keller #define __ptp_info_to_ptp(i) \ 20806c16d89SJacob Keller container_of((i), struct ice_ptp, info) 20906c16d89SJacob Keller #define ptp_info_to_pf(i) \ 21006c16d89SJacob Keller container_of(__ptp_info_to_ptp((i)), struct ice_pf, ptp) 21106c16d89SJacob Keller 2123a749623SJacob Keller #define PFTSYN_SEM_BYTES 4 21306c16d89SJacob Keller #define PTP_SHARED_CLK_IDX_VALID BIT(31) 2143a749623SJacob Keller #define TS_CMD_MASK 0xF 2153a749623SJacob Keller #define SYNC_EXEC_CMD 0x3 21677a78115SJacob Keller #define ICE_PTP_TS_VALID BIT(0) 21706c16d89SJacob Keller 2183a749623SJacob Keller #define FIFO_EMPTY BIT(2) 2193a749623SJacob Keller #define FIFO_OK 0xFF 2203a749623SJacob Keller #define ICE_PTP_FIFO_NUM_CHECKS 5 221172db5f9SMaciej Machnikowski /* Per-channel register definitions */ 222172db5f9SMaciej Machnikowski #define GLTSYN_AUX_OUT(_chan, _idx) (GLTSYN_AUX_OUT_0(_idx) + ((_chan) * 8)) 223172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN(_chan, _idx) (GLTSYN_AUX_IN_0(_idx) + ((_chan) * 8)) 224172db5f9SMaciej Machnikowski #define GLTSYN_CLKO(_chan, _idx) (GLTSYN_CLKO_0(_idx) + ((_chan) * 8)) 225172db5f9SMaciej Machnikowski #define GLTSYN_TGT_L(_chan, _idx) (GLTSYN_TGT_L_0(_idx) + ((_chan) * 16)) 226172db5f9SMaciej Machnikowski #define GLTSYN_TGT_H(_chan, _idx) (GLTSYN_TGT_H_0(_idx) + ((_chan) * 16)) 227172db5f9SMaciej Machnikowski #define GLTSYN_EVNT_L(_chan, _idx) (GLTSYN_EVNT_L_0(_idx) + ((_chan) * 16)) 228172db5f9SMaciej Machnikowski #define GLTSYN_EVNT_H(_chan, _idx) (GLTSYN_EVNT_H_0(_idx) + ((_chan) * 16)) 229172db5f9SMaciej Machnikowski #define GLTSYN_EVNT_H_IDX_MAX 3 230172db5f9SMaciej Machnikowski 231172db5f9SMaciej Machnikowski /* Pin definitions for PTP PPS out */ 232172db5f9SMaciej Machnikowski #define PPS_CLK_GEN_CHAN 3 233172db5f9SMaciej Machnikowski #define PPS_CLK_SRC_CHAN 2 234172db5f9SMaciej Machnikowski #define PPS_PIN_INDEX 5 235172db5f9SMaciej Machnikowski #define TIME_SYNC_PIN_INDEX 4 236325b2064SMaciej Machnikowski #define N_EXT_TS_E810 3 237325b2064SMaciej Machnikowski #define N_PER_OUT_E810 4 238325b2064SMaciej Machnikowski #define N_PER_OUT_E810T 3 23943c4958aSArkadiusz Kubalewski #define N_PER_OUT_NO_SMA_E810T 2 24043c4958aSArkadiusz Kubalewski #define N_EXT_TS_NO_SMA_E810T 2 2413a749623SJacob Keller #define ETH_GLTSYN_ENA(_i) (0x03000348 + ((_i) * 4)) 242172db5f9SMaciej Machnikowski 24306c16d89SJacob Keller #if IS_ENABLED(CONFIG_PTP_1588_CLOCK) 24406c16d89SJacob Keller struct ice_pf; 24577a78115SJacob Keller int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr); 24677a78115SJacob Keller int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr); 24748096710SKarol Kolacinski void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena); 24867569a7fSJacob Keller int ice_get_ptp_clock_index(struct ice_pf *pf); 249ea9b847cSJacob Keller 250ea9b847cSJacob Keller s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb); 2511229b339SKarol Kolacinski bool ice_ptp_process_ts(struct ice_pf *pf); 252ea9b847cSJacob Keller 25377a78115SJacob Keller void 254e72bba21SMaciej Fijalkowski ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring, 25577a78115SJacob Keller union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb); 25648096710SKarol Kolacinski void ice_ptp_reset(struct ice_pf *pf); 25748096710SKarol Kolacinski void ice_ptp_prepare_for_reset(struct ice_pf *pf); 25806c16d89SJacob Keller void ice_ptp_init(struct ice_pf *pf); 25906c16d89SJacob Keller void ice_ptp_release(struct ice_pf *pf); 260*6b1ff5d3SJacob Keller void ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup); 26106c16d89SJacob Keller #else /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */ 26277a78115SJacob Keller static inline int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr) 26377a78115SJacob Keller { 26477a78115SJacob Keller return -EOPNOTSUPP; 26577a78115SJacob Keller } 26677a78115SJacob Keller 26777a78115SJacob Keller static inline int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr) 26877a78115SJacob Keller { 26977a78115SJacob Keller return -EOPNOTSUPP; 27077a78115SJacob Keller } 27177a78115SJacob Keller 27248096710SKarol Kolacinski static inline void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena) { } 27367569a7fSJacob Keller static inline int ice_get_ptp_clock_index(struct ice_pf *pf) 27467569a7fSJacob Keller { 27567569a7fSJacob Keller return -1; 27667569a7fSJacob Keller } 27777a78115SJacob Keller 2784d7f75feSLorenzo Bianconi static inline s8 279ea9b847cSJacob Keller ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb) 280ea9b847cSJacob Keller { 281ea9b847cSJacob Keller return -1; 282ea9b847cSJacob Keller } 283ea9b847cSJacob Keller 2841229b339SKarol Kolacinski static inline bool ice_ptp_process_ts(struct ice_pf *pf) 2851229b339SKarol Kolacinski { 2861229b339SKarol Kolacinski return true; 2871229b339SKarol Kolacinski } 28877a78115SJacob Keller static inline void 289e72bba21SMaciej Fijalkowski ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring, 29077a78115SJacob Keller union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) { } 29148096710SKarol Kolacinski static inline void ice_ptp_reset(struct ice_pf *pf) { } 29248096710SKarol Kolacinski static inline void ice_ptp_prepare_for_reset(struct ice_pf *pf) { } 29306c16d89SJacob Keller static inline void ice_ptp_init(struct ice_pf *pf) { } 29406c16d89SJacob Keller static inline void ice_ptp_release(struct ice_pf *pf) { } 295*6b1ff5d3SJacob Keller static inline void ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup) 296*6b1ff5d3SJacob Keller { 297*6b1ff5d3SJacob Keller } 29806c16d89SJacob Keller #endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */ 29906c16d89SJacob Keller #endif /* _ICE_PTP_H_ */ 300