1b050f2f1SRadu Pirea (NXP OSS) // SPDX-License-Identifier: GPL-2.0 2b050f2f1SRadu Pirea (NXP OSS) /* NXP C45 PHY driver 3b050f2f1SRadu Pirea (NXP OSS) * Copyright (C) 2021 NXP 4b050f2f1SRadu Pirea (NXP OSS) * Author: Radu Pirea <radu-nicolae.pirea@oss.nxp.com> 5b050f2f1SRadu Pirea (NXP OSS) */ 6b050f2f1SRadu Pirea (NXP OSS) 7b050f2f1SRadu Pirea (NXP OSS) #include <linux/delay.h> 8b050f2f1SRadu Pirea (NXP OSS) #include <linux/ethtool.h> 9b050f2f1SRadu Pirea (NXP OSS) #include <linux/ethtool_netlink.h> 10b050f2f1SRadu Pirea (NXP OSS) #include <linux/kernel.h> 11b050f2f1SRadu Pirea (NXP OSS) #include <linux/mii.h> 12b050f2f1SRadu Pirea (NXP OSS) #include <linux/module.h> 13b050f2f1SRadu Pirea (NXP OSS) #include <linux/phy.h> 14b050f2f1SRadu Pirea (NXP OSS) #include <linux/processor.h> 15b050f2f1SRadu Pirea (NXP OSS) #include <linux/property.h> 16514def5dSRadu Pirea (NXP OSS) #include <linux/ptp_classify.h> 17514def5dSRadu Pirea (NXP OSS) #include <linux/ptp_clock_kernel.h> 18514def5dSRadu Pirea (NXP OSS) #include <linux/net_tstamp.h> 19b050f2f1SRadu Pirea (NXP OSS) 20b050f2f1SRadu Pirea (NXP OSS) #define PHY_ID_TJA_1103 0x001BB010 21b050f2f1SRadu Pirea (NXP OSS) 22b050f2f1SRadu Pirea (NXP OSS) #define VEND1_DEVICE_CONTROL 0x0040 23b050f2f1SRadu Pirea (NXP OSS) #define DEVICE_CONTROL_RESET BIT(15) 24b050f2f1SRadu Pirea (NXP OSS) #define DEVICE_CONTROL_CONFIG_GLOBAL_EN BIT(14) 25b050f2f1SRadu Pirea (NXP OSS) #define DEVICE_CONTROL_CONFIG_ALL_EN BIT(13) 26b050f2f1SRadu Pirea (NXP OSS) 27b2f0ca00SRadu Pirea (NXP OSS) #define VEND1_PHY_IRQ_ACK 0x80A0 28b2f0ca00SRadu Pirea (NXP OSS) #define VEND1_PHY_IRQ_EN 0x80A1 29b2f0ca00SRadu Pirea (NXP OSS) #define VEND1_PHY_IRQ_STATUS 0x80A2 30b2f0ca00SRadu Pirea (NXP OSS) #define PHY_IRQ_LINK_EVENT BIT(1) 31b2f0ca00SRadu Pirea (NXP OSS) 32b050f2f1SRadu Pirea (NXP OSS) #define VEND1_PHY_CONTROL 0x8100 33b050f2f1SRadu Pirea (NXP OSS) #define PHY_CONFIG_EN BIT(14) 34b050f2f1SRadu Pirea (NXP OSS) #define PHY_START_OP BIT(0) 35b050f2f1SRadu Pirea (NXP OSS) 36b050f2f1SRadu Pirea (NXP OSS) #define VEND1_PHY_CONFIG 0x8108 37b050f2f1SRadu Pirea (NXP OSS) #define PHY_CONFIG_AUTO BIT(0) 38b050f2f1SRadu Pirea (NXP OSS) 39b050f2f1SRadu Pirea (NXP OSS) #define VEND1_SIGNAL_QUALITY 0x8320 40b050f2f1SRadu Pirea (NXP OSS) #define SQI_VALID BIT(14) 41b050f2f1SRadu Pirea (NXP OSS) #define SQI_MASK GENMASK(2, 0) 42b050f2f1SRadu Pirea (NXP OSS) #define MAX_SQI SQI_MASK 43b050f2f1SRadu Pirea (NXP OSS) 44b050f2f1SRadu Pirea (NXP OSS) #define CABLE_TEST_ENABLE BIT(15) 45b050f2f1SRadu Pirea (NXP OSS) #define CABLE_TEST_START BIT(14) 46b050f2f1SRadu Pirea (NXP OSS) #define CABLE_TEST_OK 0x00 47b050f2f1SRadu Pirea (NXP OSS) #define CABLE_TEST_SHORTED 0x01 48b050f2f1SRadu Pirea (NXP OSS) #define CABLE_TEST_OPEN 0x02 49b050f2f1SRadu Pirea (NXP OSS) #define CABLE_TEST_UNKNOWN 0x07 50b050f2f1SRadu Pirea (NXP OSS) 51b050f2f1SRadu Pirea (NXP OSS) #define VEND1_PORT_CONTROL 0x8040 52b050f2f1SRadu Pirea (NXP OSS) #define PORT_CONTROL_EN BIT(14) 53b050f2f1SRadu Pirea (NXP OSS) 54514def5dSRadu Pirea (NXP OSS) #define VEND1_PORT_ABILITIES 0x8046 55514def5dSRadu Pirea (NXP OSS) #define PTP_ABILITY BIT(3) 56514def5dSRadu Pirea (NXP OSS) 57b050f2f1SRadu Pirea (NXP OSS) #define VEND1_PORT_INFRA_CONTROL 0xAC00 58b050f2f1SRadu Pirea (NXP OSS) #define PORT_INFRA_CONTROL_EN BIT(14) 59b050f2f1SRadu Pirea (NXP OSS) 60b050f2f1SRadu Pirea (NXP OSS) #define VEND1_RXID 0xAFCC 61b050f2f1SRadu Pirea (NXP OSS) #define VEND1_TXID 0xAFCD 62b050f2f1SRadu Pirea (NXP OSS) #define ID_ENABLE BIT(15) 63b050f2f1SRadu Pirea (NXP OSS) 64b050f2f1SRadu Pirea (NXP OSS) #define VEND1_ABILITIES 0xAFC4 65b050f2f1SRadu Pirea (NXP OSS) #define RGMII_ID_ABILITY BIT(15) 66b050f2f1SRadu Pirea (NXP OSS) #define RGMII_ABILITY BIT(14) 67b050f2f1SRadu Pirea (NXP OSS) #define RMII_ABILITY BIT(10) 68b050f2f1SRadu Pirea (NXP OSS) #define REVMII_ABILITY BIT(9) 69b050f2f1SRadu Pirea (NXP OSS) #define MII_ABILITY BIT(8) 70b050f2f1SRadu Pirea (NXP OSS) #define SGMII_ABILITY BIT(0) 71b050f2f1SRadu Pirea (NXP OSS) 72b050f2f1SRadu Pirea (NXP OSS) #define VEND1_MII_BASIC_CONFIG 0xAFC6 738ba57205SRadu Pirea (OSS) #define MII_BASIC_CONFIG_REV BIT(4) 74b050f2f1SRadu Pirea (NXP OSS) #define MII_BASIC_CONFIG_SGMII 0x9 75b050f2f1SRadu Pirea (NXP OSS) #define MII_BASIC_CONFIG_RGMII 0x7 76b050f2f1SRadu Pirea (NXP OSS) #define MII_BASIC_CONFIG_RMII 0x5 77b050f2f1SRadu Pirea (NXP OSS) #define MII_BASIC_CONFIG_MII 0x4 78b050f2f1SRadu Pirea (NXP OSS) 79b050f2f1SRadu Pirea (NXP OSS) #define VEND1_SYMBOL_ERROR_COUNTER 0x8350 80b050f2f1SRadu Pirea (NXP OSS) #define VEND1_LINK_DROP_COUNTER 0x8352 81b050f2f1SRadu Pirea (NXP OSS) #define VEND1_LINK_LOSSES_AND_FAILURES 0x8353 82b050f2f1SRadu Pirea (NXP OSS) #define VEND1_RX_PREAMBLE_COUNT 0xAFCE 83b050f2f1SRadu Pirea (NXP OSS) #define VEND1_TX_PREAMBLE_COUNT 0xAFCF 84b050f2f1SRadu Pirea (NXP OSS) #define VEND1_RX_IPG_LENGTH 0xAFD0 85b050f2f1SRadu Pirea (NXP OSS) #define VEND1_TX_IPG_LENGTH 0xAFD1 86b050f2f1SRadu Pirea (NXP OSS) #define COUNTER_EN BIT(15) 87b050f2f1SRadu Pirea (NXP OSS) 887a71c8aaSRadu Pirea (NXP OSS) #define VEND1_PTP_CONFIG 0x1102 897a71c8aaSRadu Pirea (NXP OSS) #define EXT_TRG_EDGE BIT(1) 907a71c8aaSRadu Pirea (NXP OSS) 91514def5dSRadu Pirea (NXP OSS) #define CLK_RATE_ADJ_LD BIT(15) 92514def5dSRadu Pirea (NXP OSS) #define CLK_RATE_ADJ_DIR BIT(14) 93514def5dSRadu Pirea (NXP OSS) 94514def5dSRadu Pirea (NXP OSS) #define VEND1_RX_TS_INSRT_CTRL 0x114D 956c0c85daSRadu Pirea (NXP OSS) #define TJA1103_RX_TS_INSRT_MODE2 0x02 96514def5dSRadu Pirea (NXP OSS) 97514def5dSRadu Pirea (NXP OSS) #define VEND1_EGR_RING_DATA_0 0x114E 98514def5dSRadu Pirea (NXP OSS) #define VEND1_EGR_RING_CTRL 0x1154 99514def5dSRadu Pirea (NXP OSS) 100514def5dSRadu Pirea (NXP OSS) #define RING_DATA_0_TS_VALID BIT(15) 101514def5dSRadu Pirea (NXP OSS) 102514def5dSRadu Pirea (NXP OSS) #define RING_DONE BIT(0) 103514def5dSRadu Pirea (NXP OSS) 104514def5dSRadu Pirea (NXP OSS) #define TS_SEC_MASK GENMASK(1, 0) 105514def5dSRadu Pirea (NXP OSS) 106514def5dSRadu Pirea (NXP OSS) #define VEND1_PORT_FUNC_ENABLES 0x8048 107514def5dSRadu Pirea (NXP OSS) #define PTP_ENABLE BIT(3) 108514def5dSRadu Pirea (NXP OSS) 109514def5dSRadu Pirea (NXP OSS) #define VEND1_PORT_PTP_CONTROL 0x9000 110514def5dSRadu Pirea (NXP OSS) #define PORT_PTP_CONTROL_BYPASS BIT(11) 111514def5dSRadu Pirea (NXP OSS) 112514def5dSRadu Pirea (NXP OSS) #define PTP_CLK_PERIOD_100BT1 15ULL 113514def5dSRadu Pirea (NXP OSS) 114514def5dSRadu Pirea (NXP OSS) #define EVENT_MSG_FILT_ALL 0x0F 115514def5dSRadu Pirea (NXP OSS) #define EVENT_MSG_FILT_NONE 0x00 116514def5dSRadu Pirea (NXP OSS) 1177a71c8aaSRadu Pirea (NXP OSS) #define VEND1_GPIO_FUNC_CONFIG_BASE 0x2C40 1187a71c8aaSRadu Pirea (NXP OSS) #define GPIO_FUNC_EN BIT(15) 1197a71c8aaSRadu Pirea (NXP OSS) #define GPIO_FUNC_PTP BIT(6) 1207a71c8aaSRadu Pirea (NXP OSS) #define GPIO_SIGNAL_PTP_TRIGGER 0x01 1217a71c8aaSRadu Pirea (NXP OSS) #define GPIO_SIGNAL_PPS_OUT 0x12 1227a71c8aaSRadu Pirea (NXP OSS) #define GPIO_DISABLE 0 1237a71c8aaSRadu Pirea (NXP OSS) #define GPIO_PPS_OUT_CFG (GPIO_FUNC_EN | GPIO_FUNC_PTP | \ 1247a71c8aaSRadu Pirea (NXP OSS) GPIO_SIGNAL_PPS_OUT) 1257a71c8aaSRadu Pirea (NXP OSS) #define GPIO_EXTTS_OUT_CFG (GPIO_FUNC_EN | GPIO_FUNC_PTP | \ 1267a71c8aaSRadu Pirea (NXP OSS) GPIO_SIGNAL_PTP_TRIGGER) 1277a71c8aaSRadu Pirea (NXP OSS) 128b050f2f1SRadu Pirea (NXP OSS) #define RGMII_PERIOD_PS 8000U 129b050f2f1SRadu Pirea (NXP OSS) #define PS_PER_DEGREE div_u64(RGMII_PERIOD_PS, 360) 130b050f2f1SRadu Pirea (NXP OSS) #define MIN_ID_PS 1644U 131b050f2f1SRadu Pirea (NXP OSS) #define MAX_ID_PS 2260U 132b050f2f1SRadu Pirea (NXP OSS) #define DEFAULT_ID_PS 2000U 133b050f2f1SRadu Pirea (NXP OSS) 1346c0c85daSRadu Pirea (NXP OSS) #define PPM_TO_SUBNS_INC(ppb, ptp_clk_period) div_u64(GENMASK_ULL(31, 0) * \ 1356c0c85daSRadu Pirea (NXP OSS) (ppb) * (ptp_clk_period), NSEC_PER_SEC) 136514def5dSRadu Pirea (NXP OSS) 137514def5dSRadu Pirea (NXP OSS) #define NXP_C45_SKB_CB(skb) ((struct nxp_c45_skb_cb *)(skb)->cb) 138514def5dSRadu Pirea (NXP OSS) 139514def5dSRadu Pirea (NXP OSS) struct nxp_c45_skb_cb { 140514def5dSRadu Pirea (NXP OSS) struct ptp_header *header; 141514def5dSRadu Pirea (NXP OSS) unsigned int type; 142514def5dSRadu Pirea (NXP OSS) }; 143514def5dSRadu Pirea (NXP OSS) 1446c0c85daSRadu Pirea (NXP OSS) #define NXP_C45_REG_FIELD(_reg, _devad, _offset, _size) \ 1456c0c85daSRadu Pirea (NXP OSS) ((struct nxp_c45_reg_field) { \ 1466c0c85daSRadu Pirea (NXP OSS) .reg = _reg, \ 1476c0c85daSRadu Pirea (NXP OSS) .devad = _devad, \ 1486c0c85daSRadu Pirea (NXP OSS) .offset = _offset, \ 1496c0c85daSRadu Pirea (NXP OSS) .size = _size, \ 1506c0c85daSRadu Pirea (NXP OSS) }) 1516c0c85daSRadu Pirea (NXP OSS) 1526c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field { 1536c0c85daSRadu Pirea (NXP OSS) u16 reg; 1546c0c85daSRadu Pirea (NXP OSS) u8 devad; 1556c0c85daSRadu Pirea (NXP OSS) u8 offset; 1566c0c85daSRadu Pirea (NXP OSS) u8 size; 1576c0c85daSRadu Pirea (NXP OSS) }; 1586c0c85daSRadu Pirea (NXP OSS) 159514def5dSRadu Pirea (NXP OSS) struct nxp_c45_hwts { 160514def5dSRadu Pirea (NXP OSS) u32 nsec; 161514def5dSRadu Pirea (NXP OSS) u32 sec; 162514def5dSRadu Pirea (NXP OSS) u8 domain_number; 163514def5dSRadu Pirea (NXP OSS) u16 sequence_id; 164514def5dSRadu Pirea (NXP OSS) u8 msg_type; 165514def5dSRadu Pirea (NXP OSS) }; 166514def5dSRadu Pirea (NXP OSS) 1676c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_regmap { 1686c0c85daSRadu Pirea (NXP OSS) /* PTP config regs. */ 1696c0c85daSRadu Pirea (NXP OSS) u16 vend1_ptp_clk_period; 1706c0c85daSRadu Pirea (NXP OSS) u16 vend1_event_msg_filt; 1716c0c85daSRadu Pirea (NXP OSS) 1726c0c85daSRadu Pirea (NXP OSS) /* LTC bits and regs. */ 1736c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field ltc_read; 1746c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field ltc_write; 1756c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field ltc_lock_ctrl; 1766c0c85daSRadu Pirea (NXP OSS) u16 vend1_ltc_wr_nsec_0; 1776c0c85daSRadu Pirea (NXP OSS) u16 vend1_ltc_wr_nsec_1; 1786c0c85daSRadu Pirea (NXP OSS) u16 vend1_ltc_wr_sec_0; 1796c0c85daSRadu Pirea (NXP OSS) u16 vend1_ltc_wr_sec_1; 1806c0c85daSRadu Pirea (NXP OSS) u16 vend1_ltc_rd_nsec_0; 1816c0c85daSRadu Pirea (NXP OSS) u16 vend1_ltc_rd_nsec_1; 1826c0c85daSRadu Pirea (NXP OSS) u16 vend1_ltc_rd_sec_0; 1836c0c85daSRadu Pirea (NXP OSS) u16 vend1_ltc_rd_sec_1; 1846c0c85daSRadu Pirea (NXP OSS) u16 vend1_rate_adj_subns_0; 1856c0c85daSRadu Pirea (NXP OSS) u16 vend1_rate_adj_subns_1; 1866c0c85daSRadu Pirea (NXP OSS) 1876c0c85daSRadu Pirea (NXP OSS) /* External trigger reg fields. */ 1886c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field irq_egr_ts_en; 1896c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field irq_egr_ts_status; 1906c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field domain_number; 1916c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field msg_type; 1926c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field sequence_id; 1936c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field sec_1_0; 1946c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field sec_4_2; 1956c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field nsec_15_0; 1966c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field nsec_29_16; 1976c0c85daSRadu Pirea (NXP OSS) 1986c0c85daSRadu Pirea (NXP OSS) /* PPS and EXT Trigger bits and regs. */ 1996c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field pps_enable; 2006c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field pps_polarity; 2016c0c85daSRadu Pirea (NXP OSS) u16 vend1_ext_trg_data_0; 2026c0c85daSRadu Pirea (NXP OSS) u16 vend1_ext_trg_data_1; 2036c0c85daSRadu Pirea (NXP OSS) u16 vend1_ext_trg_data_2; 2046c0c85daSRadu Pirea (NXP OSS) u16 vend1_ext_trg_data_3; 2056c0c85daSRadu Pirea (NXP OSS) u16 vend1_ext_trg_ctrl; 2066c0c85daSRadu Pirea (NXP OSS) 2076c0c85daSRadu Pirea (NXP OSS) /* Cable test reg fields. */ 2086c0c85daSRadu Pirea (NXP OSS) u16 cable_test; 2096c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field cable_test_valid; 2106c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_reg_field cable_test_result; 2116c0c85daSRadu Pirea (NXP OSS) }; 2126c0c85daSRadu Pirea (NXP OSS) 2136c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_phy_stats { 2146c0c85daSRadu Pirea (NXP OSS) const char *name; 2156c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_reg_field counter; 2166c0c85daSRadu Pirea (NXP OSS) }; 2176c0c85daSRadu Pirea (NXP OSS) 2186c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_phy_data { 2196c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_regmap *regmap; 2206c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_phy_stats *stats; 2216c0c85daSRadu Pirea (NXP OSS) int n_stats; 2226c0c85daSRadu Pirea (NXP OSS) u8 ptp_clk_period; 2236c0c85daSRadu Pirea (NXP OSS) void (*counters_enable)(struct phy_device *phydev); 2246c0c85daSRadu Pirea (NXP OSS) void (*ptp_init)(struct phy_device *phydev); 2256c0c85daSRadu Pirea (NXP OSS) void (*ptp_enable)(struct phy_device *phydev, bool enable); 2266c0c85daSRadu Pirea (NXP OSS) }; 2276c0c85daSRadu Pirea (NXP OSS) 228b050f2f1SRadu Pirea (NXP OSS) struct nxp_c45_phy { 2296c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_phy_data *phy_data; 230514def5dSRadu Pirea (NXP OSS) struct phy_device *phydev; 231514def5dSRadu Pirea (NXP OSS) struct mii_timestamper mii_ts; 232514def5dSRadu Pirea (NXP OSS) struct ptp_clock *ptp_clock; 233514def5dSRadu Pirea (NXP OSS) struct ptp_clock_info caps; 234514def5dSRadu Pirea (NXP OSS) struct sk_buff_head tx_queue; 235514def5dSRadu Pirea (NXP OSS) struct sk_buff_head rx_queue; 236514def5dSRadu Pirea (NXP OSS) /* used to access the PTP registers atomic */ 237514def5dSRadu Pirea (NXP OSS) struct mutex ptp_lock; 238514def5dSRadu Pirea (NXP OSS) int hwts_tx; 239514def5dSRadu Pirea (NXP OSS) int hwts_rx; 240b050f2f1SRadu Pirea (NXP OSS) u32 tx_delay; 241b050f2f1SRadu Pirea (NXP OSS) u32 rx_delay; 2427a71c8aaSRadu Pirea (NXP OSS) struct timespec64 extts_ts; 2437a71c8aaSRadu Pirea (NXP OSS) int extts_index; 2447a71c8aaSRadu Pirea (NXP OSS) bool extts; 245b050f2f1SRadu Pirea (NXP OSS) }; 246b050f2f1SRadu Pirea (NXP OSS) 2476c0c85daSRadu Pirea (NXP OSS) static const 2486c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_phy_data *nxp_c45_get_data(struct phy_device *phydev) 2496c0c85daSRadu Pirea (NXP OSS) { 2506c0c85daSRadu Pirea (NXP OSS) return phydev->drv->driver_data; 2516c0c85daSRadu Pirea (NXP OSS) } 2526c0c85daSRadu Pirea (NXP OSS) 2536c0c85daSRadu Pirea (NXP OSS) static const 2546c0c85daSRadu Pirea (NXP OSS) struct nxp_c45_regmap *nxp_c45_get_regmap(struct phy_device *phydev) 2556c0c85daSRadu Pirea (NXP OSS) { 2566c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_phy_data *phy_data = nxp_c45_get_data(phydev); 2576c0c85daSRadu Pirea (NXP OSS) 2586c0c85daSRadu Pirea (NXP OSS) return phy_data->regmap; 2596c0c85daSRadu Pirea (NXP OSS) } 2606c0c85daSRadu Pirea (NXP OSS) 2616c0c85daSRadu Pirea (NXP OSS) static int nxp_c45_read_reg_field(struct phy_device *phydev, 2626c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_reg_field *reg_field) 2636c0c85daSRadu Pirea (NXP OSS) { 264b050f2f1SRadu Pirea (NXP OSS) u16 mask; 2656c0c85daSRadu Pirea (NXP OSS) int ret; 2666c0c85daSRadu Pirea (NXP OSS) 2676c0c85daSRadu Pirea (NXP OSS) if (reg_field->size == 0) { 2686c0c85daSRadu Pirea (NXP OSS) phydev_err(phydev, "Trying to read a reg field of size 0.\n"); 2696c0c85daSRadu Pirea (NXP OSS) return -EINVAL; 2706c0c85daSRadu Pirea (NXP OSS) } 2716c0c85daSRadu Pirea (NXP OSS) 2726c0c85daSRadu Pirea (NXP OSS) ret = phy_read_mmd(phydev, reg_field->devad, reg_field->reg); 2736c0c85daSRadu Pirea (NXP OSS) if (ret < 0) 2746c0c85daSRadu Pirea (NXP OSS) return ret; 2756c0c85daSRadu Pirea (NXP OSS) 2766c0c85daSRadu Pirea (NXP OSS) mask = reg_field->size == 1 ? BIT(reg_field->offset) : 2776c0c85daSRadu Pirea (NXP OSS) GENMASK(reg_field->offset + reg_field->size - 1, 2786c0c85daSRadu Pirea (NXP OSS) reg_field->offset); 2796c0c85daSRadu Pirea (NXP OSS) ret &= mask; 2806c0c85daSRadu Pirea (NXP OSS) ret >>= reg_field->offset; 2816c0c85daSRadu Pirea (NXP OSS) 2826c0c85daSRadu Pirea (NXP OSS) return ret; 2836c0c85daSRadu Pirea (NXP OSS) } 2846c0c85daSRadu Pirea (NXP OSS) 2856c0c85daSRadu Pirea (NXP OSS) static int nxp_c45_write_reg_field(struct phy_device *phydev, 2866c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_reg_field *reg_field, 2876c0c85daSRadu Pirea (NXP OSS) u16 val) 2886c0c85daSRadu Pirea (NXP OSS) { 2896c0c85daSRadu Pirea (NXP OSS) u16 mask; 2906c0c85daSRadu Pirea (NXP OSS) u16 set; 2916c0c85daSRadu Pirea (NXP OSS) 2926c0c85daSRadu Pirea (NXP OSS) if (reg_field->size == 0) { 2936c0c85daSRadu Pirea (NXP OSS) phydev_err(phydev, "Trying to write a reg field of size 0.\n"); 2946c0c85daSRadu Pirea (NXP OSS) return -EINVAL; 2956c0c85daSRadu Pirea (NXP OSS) } 2966c0c85daSRadu Pirea (NXP OSS) 2976c0c85daSRadu Pirea (NXP OSS) mask = reg_field->size == 1 ? BIT(reg_field->offset) : 2986c0c85daSRadu Pirea (NXP OSS) GENMASK(reg_field->offset + reg_field->size - 1, 2996c0c85daSRadu Pirea (NXP OSS) reg_field->offset); 3006c0c85daSRadu Pirea (NXP OSS) set = val << reg_field->offset; 3016c0c85daSRadu Pirea (NXP OSS) 3026c0c85daSRadu Pirea (NXP OSS) return phy_modify_mmd_changed(phydev, reg_field->devad, 3036c0c85daSRadu Pirea (NXP OSS) reg_field->reg, mask, set); 3046c0c85daSRadu Pirea (NXP OSS) } 3056c0c85daSRadu Pirea (NXP OSS) 3066c0c85daSRadu Pirea (NXP OSS) static int nxp_c45_set_reg_field(struct phy_device *phydev, 3076c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_reg_field *reg_field) 3086c0c85daSRadu Pirea (NXP OSS) { 3096c0c85daSRadu Pirea (NXP OSS) if (reg_field->size != 1) { 3106c0c85daSRadu Pirea (NXP OSS) phydev_err(phydev, "Trying to set a reg field of size different than 1.\n"); 3116c0c85daSRadu Pirea (NXP OSS) return -EINVAL; 3126c0c85daSRadu Pirea (NXP OSS) } 3136c0c85daSRadu Pirea (NXP OSS) 3146c0c85daSRadu Pirea (NXP OSS) return nxp_c45_write_reg_field(phydev, reg_field, 1); 3156c0c85daSRadu Pirea (NXP OSS) } 3166c0c85daSRadu Pirea (NXP OSS) 3176c0c85daSRadu Pirea (NXP OSS) static int nxp_c45_clear_reg_field(struct phy_device *phydev, 3186c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_reg_field *reg_field) 3196c0c85daSRadu Pirea (NXP OSS) { 3206c0c85daSRadu Pirea (NXP OSS) if (reg_field->size != 1) { 3216c0c85daSRadu Pirea (NXP OSS) phydev_err(phydev, "Trying to set a reg field of size different than 1.\n"); 3226c0c85daSRadu Pirea (NXP OSS) return -EINVAL; 3236c0c85daSRadu Pirea (NXP OSS) } 3246c0c85daSRadu Pirea (NXP OSS) 3256c0c85daSRadu Pirea (NXP OSS) return nxp_c45_write_reg_field(phydev, reg_field, 0); 3266c0c85daSRadu Pirea (NXP OSS) } 327b050f2f1SRadu Pirea (NXP OSS) 328514def5dSRadu Pirea (NXP OSS) static bool nxp_c45_poll_txts(struct phy_device *phydev) 329514def5dSRadu Pirea (NXP OSS) { 330514def5dSRadu Pirea (NXP OSS) return phydev->irq <= 0; 331514def5dSRadu Pirea (NXP OSS) } 332514def5dSRadu Pirea (NXP OSS) 333514def5dSRadu Pirea (NXP OSS) static int _nxp_c45_ptp_gettimex64(struct ptp_clock_info *ptp, 334514def5dSRadu Pirea (NXP OSS) struct timespec64 *ts, 335514def5dSRadu Pirea (NXP OSS) struct ptp_system_timestamp *sts) 336514def5dSRadu Pirea (NXP OSS) { 337514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(ptp, struct nxp_c45_phy, caps); 3386c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_regmap *regmap = nxp_c45_get_regmap(priv->phydev); 339514def5dSRadu Pirea (NXP OSS) 3406c0c85daSRadu Pirea (NXP OSS) nxp_c45_set_reg_field(priv->phydev, ®map->ltc_read); 341514def5dSRadu Pirea (NXP OSS) ts->tv_nsec = phy_read_mmd(priv->phydev, MDIO_MMD_VEND1, 3426c0c85daSRadu Pirea (NXP OSS) regmap->vend1_ltc_rd_nsec_0); 343514def5dSRadu Pirea (NXP OSS) ts->tv_nsec |= phy_read_mmd(priv->phydev, MDIO_MMD_VEND1, 3446c0c85daSRadu Pirea (NXP OSS) regmap->vend1_ltc_rd_nsec_1) << 16; 345514def5dSRadu Pirea (NXP OSS) ts->tv_sec = phy_read_mmd(priv->phydev, MDIO_MMD_VEND1, 3466c0c85daSRadu Pirea (NXP OSS) regmap->vend1_ltc_rd_sec_0); 347514def5dSRadu Pirea (NXP OSS) ts->tv_sec |= phy_read_mmd(priv->phydev, MDIO_MMD_VEND1, 3486c0c85daSRadu Pirea (NXP OSS) regmap->vend1_ltc_rd_sec_1) << 16; 349514def5dSRadu Pirea (NXP OSS) 350514def5dSRadu Pirea (NXP OSS) return 0; 351514def5dSRadu Pirea (NXP OSS) } 352514def5dSRadu Pirea (NXP OSS) 353514def5dSRadu Pirea (NXP OSS) static int nxp_c45_ptp_gettimex64(struct ptp_clock_info *ptp, 354514def5dSRadu Pirea (NXP OSS) struct timespec64 *ts, 355514def5dSRadu Pirea (NXP OSS) struct ptp_system_timestamp *sts) 356514def5dSRadu Pirea (NXP OSS) { 357514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(ptp, struct nxp_c45_phy, caps); 358514def5dSRadu Pirea (NXP OSS) 359514def5dSRadu Pirea (NXP OSS) mutex_lock(&priv->ptp_lock); 360514def5dSRadu Pirea (NXP OSS) _nxp_c45_ptp_gettimex64(ptp, ts, sts); 361514def5dSRadu Pirea (NXP OSS) mutex_unlock(&priv->ptp_lock); 362514def5dSRadu Pirea (NXP OSS) 363514def5dSRadu Pirea (NXP OSS) return 0; 364514def5dSRadu Pirea (NXP OSS) } 365514def5dSRadu Pirea (NXP OSS) 366514def5dSRadu Pirea (NXP OSS) static int _nxp_c45_ptp_settime64(struct ptp_clock_info *ptp, 367514def5dSRadu Pirea (NXP OSS) const struct timespec64 *ts) 368514def5dSRadu Pirea (NXP OSS) { 369514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(ptp, struct nxp_c45_phy, caps); 3706c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_regmap *regmap = nxp_c45_get_regmap(priv->phydev); 371514def5dSRadu Pirea (NXP OSS) 3726c0c85daSRadu Pirea (NXP OSS) phy_write_mmd(priv->phydev, MDIO_MMD_VEND1, regmap->vend1_ltc_wr_nsec_0, 373514def5dSRadu Pirea (NXP OSS) ts->tv_nsec); 3746c0c85daSRadu Pirea (NXP OSS) phy_write_mmd(priv->phydev, MDIO_MMD_VEND1, regmap->vend1_ltc_wr_nsec_1, 375514def5dSRadu Pirea (NXP OSS) ts->tv_nsec >> 16); 3766c0c85daSRadu Pirea (NXP OSS) phy_write_mmd(priv->phydev, MDIO_MMD_VEND1, regmap->vend1_ltc_wr_sec_0, 377514def5dSRadu Pirea (NXP OSS) ts->tv_sec); 3786c0c85daSRadu Pirea (NXP OSS) phy_write_mmd(priv->phydev, MDIO_MMD_VEND1, regmap->vend1_ltc_wr_sec_1, 379514def5dSRadu Pirea (NXP OSS) ts->tv_sec >> 16); 3806c0c85daSRadu Pirea (NXP OSS) nxp_c45_set_reg_field(priv->phydev, ®map->ltc_write); 381514def5dSRadu Pirea (NXP OSS) 382514def5dSRadu Pirea (NXP OSS) return 0; 383514def5dSRadu Pirea (NXP OSS) } 384514def5dSRadu Pirea (NXP OSS) 385514def5dSRadu Pirea (NXP OSS) static int nxp_c45_ptp_settime64(struct ptp_clock_info *ptp, 386514def5dSRadu Pirea (NXP OSS) const struct timespec64 *ts) 387514def5dSRadu Pirea (NXP OSS) { 388514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(ptp, struct nxp_c45_phy, caps); 389514def5dSRadu Pirea (NXP OSS) 390514def5dSRadu Pirea (NXP OSS) mutex_lock(&priv->ptp_lock); 391514def5dSRadu Pirea (NXP OSS) _nxp_c45_ptp_settime64(ptp, ts); 392514def5dSRadu Pirea (NXP OSS) mutex_unlock(&priv->ptp_lock); 393514def5dSRadu Pirea (NXP OSS) 394514def5dSRadu Pirea (NXP OSS) return 0; 395514def5dSRadu Pirea (NXP OSS) } 396514def5dSRadu Pirea (NXP OSS) 397514def5dSRadu Pirea (NXP OSS) static int nxp_c45_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) 398514def5dSRadu Pirea (NXP OSS) { 399514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(ptp, struct nxp_c45_phy, caps); 4006c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_phy_data *data = nxp_c45_get_data(priv->phydev); 4016c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_regmap *regmap = data->regmap; 402514def5dSRadu Pirea (NXP OSS) s32 ppb = scaled_ppm_to_ppb(scaled_ppm); 403514def5dSRadu Pirea (NXP OSS) u64 subns_inc_val; 404514def5dSRadu Pirea (NXP OSS) bool inc; 405514def5dSRadu Pirea (NXP OSS) 406514def5dSRadu Pirea (NXP OSS) mutex_lock(&priv->ptp_lock); 407514def5dSRadu Pirea (NXP OSS) inc = ppb >= 0; 408514def5dSRadu Pirea (NXP OSS) ppb = abs(ppb); 409514def5dSRadu Pirea (NXP OSS) 4106c0c85daSRadu Pirea (NXP OSS) subns_inc_val = PPM_TO_SUBNS_INC(ppb, data->ptp_clk_period); 411514def5dSRadu Pirea (NXP OSS) 4126c0c85daSRadu Pirea (NXP OSS) phy_write_mmd(priv->phydev, MDIO_MMD_VEND1, 4136c0c85daSRadu Pirea (NXP OSS) regmap->vend1_rate_adj_subns_0, 414514def5dSRadu Pirea (NXP OSS) subns_inc_val); 415514def5dSRadu Pirea (NXP OSS) subns_inc_val >>= 16; 416514def5dSRadu Pirea (NXP OSS) subns_inc_val |= CLK_RATE_ADJ_LD; 417514def5dSRadu Pirea (NXP OSS) if (inc) 418514def5dSRadu Pirea (NXP OSS) subns_inc_val |= CLK_RATE_ADJ_DIR; 419514def5dSRadu Pirea (NXP OSS) 4206c0c85daSRadu Pirea (NXP OSS) phy_write_mmd(priv->phydev, MDIO_MMD_VEND1, 4216c0c85daSRadu Pirea (NXP OSS) regmap->vend1_rate_adj_subns_1, 422514def5dSRadu Pirea (NXP OSS) subns_inc_val); 423514def5dSRadu Pirea (NXP OSS) mutex_unlock(&priv->ptp_lock); 424514def5dSRadu Pirea (NXP OSS) 425514def5dSRadu Pirea (NXP OSS) return 0; 426514def5dSRadu Pirea (NXP OSS) } 427514def5dSRadu Pirea (NXP OSS) 428514def5dSRadu Pirea (NXP OSS) static int nxp_c45_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) 429514def5dSRadu Pirea (NXP OSS) { 430514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(ptp, struct nxp_c45_phy, caps); 431514def5dSRadu Pirea (NXP OSS) struct timespec64 now, then; 432514def5dSRadu Pirea (NXP OSS) 433514def5dSRadu Pirea (NXP OSS) mutex_lock(&priv->ptp_lock); 434514def5dSRadu Pirea (NXP OSS) then = ns_to_timespec64(delta); 435514def5dSRadu Pirea (NXP OSS) _nxp_c45_ptp_gettimex64(ptp, &now, NULL); 436514def5dSRadu Pirea (NXP OSS) now = timespec64_add(now, then); 437514def5dSRadu Pirea (NXP OSS) _nxp_c45_ptp_settime64(ptp, &now); 438514def5dSRadu Pirea (NXP OSS) mutex_unlock(&priv->ptp_lock); 439514def5dSRadu Pirea (NXP OSS) 440514def5dSRadu Pirea (NXP OSS) return 0; 441514def5dSRadu Pirea (NXP OSS) } 442514def5dSRadu Pirea (NXP OSS) 443514def5dSRadu Pirea (NXP OSS) static void nxp_c45_reconstruct_ts(struct timespec64 *ts, 444514def5dSRadu Pirea (NXP OSS) struct nxp_c45_hwts *hwts) 445514def5dSRadu Pirea (NXP OSS) { 446514def5dSRadu Pirea (NXP OSS) ts->tv_nsec = hwts->nsec; 447514def5dSRadu Pirea (NXP OSS) if ((ts->tv_sec & TS_SEC_MASK) < (hwts->sec & TS_SEC_MASK)) 448661fef56SVladimir Oltean ts->tv_sec -= TS_SEC_MASK + 1; 449514def5dSRadu Pirea (NXP OSS) ts->tv_sec &= ~TS_SEC_MASK; 450514def5dSRadu Pirea (NXP OSS) ts->tv_sec |= hwts->sec & TS_SEC_MASK; 451514def5dSRadu Pirea (NXP OSS) } 452514def5dSRadu Pirea (NXP OSS) 453514def5dSRadu Pirea (NXP OSS) static bool nxp_c45_match_ts(struct ptp_header *header, 454514def5dSRadu Pirea (NXP OSS) struct nxp_c45_hwts *hwts, 455514def5dSRadu Pirea (NXP OSS) unsigned int type) 456514def5dSRadu Pirea (NXP OSS) { 457514def5dSRadu Pirea (NXP OSS) return ntohs(header->sequence_id) == hwts->sequence_id && 458514def5dSRadu Pirea (NXP OSS) ptp_get_msgtype(header, type) == hwts->msg_type && 459514def5dSRadu Pirea (NXP OSS) header->domain_number == hwts->domain_number; 460514def5dSRadu Pirea (NXP OSS) } 461514def5dSRadu Pirea (NXP OSS) 4627a71c8aaSRadu Pirea (NXP OSS) static void nxp_c45_get_extts(struct nxp_c45_phy *priv, 4637a71c8aaSRadu Pirea (NXP OSS) struct timespec64 *extts) 4647a71c8aaSRadu Pirea (NXP OSS) { 4656c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_regmap *regmap = nxp_c45_get_regmap(priv->phydev); 4666c0c85daSRadu Pirea (NXP OSS) 4677a71c8aaSRadu Pirea (NXP OSS) extts->tv_nsec = phy_read_mmd(priv->phydev, MDIO_MMD_VEND1, 4686c0c85daSRadu Pirea (NXP OSS) regmap->vend1_ext_trg_data_0); 4697a71c8aaSRadu Pirea (NXP OSS) extts->tv_nsec |= phy_read_mmd(priv->phydev, MDIO_MMD_VEND1, 4706c0c85daSRadu Pirea (NXP OSS) regmap->vend1_ext_trg_data_1) << 16; 4717a71c8aaSRadu Pirea (NXP OSS) extts->tv_sec = phy_read_mmd(priv->phydev, MDIO_MMD_VEND1, 4726c0c85daSRadu Pirea (NXP OSS) regmap->vend1_ext_trg_data_2); 4737a71c8aaSRadu Pirea (NXP OSS) extts->tv_sec |= phy_read_mmd(priv->phydev, MDIO_MMD_VEND1, 4746c0c85daSRadu Pirea (NXP OSS) regmap->vend1_ext_trg_data_3) << 16; 4756c0c85daSRadu Pirea (NXP OSS) phy_write_mmd(priv->phydev, MDIO_MMD_VEND1, 4766c0c85daSRadu Pirea (NXP OSS) regmap->vend1_ext_trg_ctrl, RING_DONE); 4777a71c8aaSRadu Pirea (NXP OSS) } 4787a71c8aaSRadu Pirea (NXP OSS) 479514def5dSRadu Pirea (NXP OSS) static bool nxp_c45_get_hwtxts(struct nxp_c45_phy *priv, 480514def5dSRadu Pirea (NXP OSS) struct nxp_c45_hwts *hwts) 481514def5dSRadu Pirea (NXP OSS) { 4826c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_regmap *regmap = nxp_c45_get_regmap(priv->phydev); 4836c0c85daSRadu Pirea (NXP OSS) struct phy_device *phydev = priv->phydev; 484514def5dSRadu Pirea (NXP OSS) bool valid; 485514def5dSRadu Pirea (NXP OSS) u16 reg; 486514def5dSRadu Pirea (NXP OSS) 487514def5dSRadu Pirea (NXP OSS) mutex_lock(&priv->ptp_lock); 488514def5dSRadu Pirea (NXP OSS) phy_write_mmd(priv->phydev, MDIO_MMD_VEND1, VEND1_EGR_RING_CTRL, 489514def5dSRadu Pirea (NXP OSS) RING_DONE); 490514def5dSRadu Pirea (NXP OSS) reg = phy_read_mmd(priv->phydev, MDIO_MMD_VEND1, VEND1_EGR_RING_DATA_0); 491514def5dSRadu Pirea (NXP OSS) valid = !!(reg & RING_DATA_0_TS_VALID); 492514def5dSRadu Pirea (NXP OSS) if (!valid) 493514def5dSRadu Pirea (NXP OSS) goto nxp_c45_get_hwtxts_out; 494514def5dSRadu Pirea (NXP OSS) 4956c0c85daSRadu Pirea (NXP OSS) hwts->domain_number = 4966c0c85daSRadu Pirea (NXP OSS) nxp_c45_read_reg_field(phydev, ®map->domain_number); 4976c0c85daSRadu Pirea (NXP OSS) hwts->msg_type = 4986c0c85daSRadu Pirea (NXP OSS) nxp_c45_read_reg_field(phydev, ®map->msg_type); 4996c0c85daSRadu Pirea (NXP OSS) hwts->sequence_id = 5006c0c85daSRadu Pirea (NXP OSS) nxp_c45_read_reg_field(phydev, ®map->sequence_id); 5016c0c85daSRadu Pirea (NXP OSS) hwts->nsec = 5026c0c85daSRadu Pirea (NXP OSS) nxp_c45_read_reg_field(phydev, ®map->nsec_15_0); 5036c0c85daSRadu Pirea (NXP OSS) hwts->nsec |= 5046c0c85daSRadu Pirea (NXP OSS) nxp_c45_read_reg_field(phydev, ®map->nsec_29_16) << 16; 5056c0c85daSRadu Pirea (NXP OSS) hwts->sec = nxp_c45_read_reg_field(phydev, ®map->sec_1_0); 5066c0c85daSRadu Pirea (NXP OSS) hwts->sec |= nxp_c45_read_reg_field(phydev, ®map->sec_4_2) << 2; 507514def5dSRadu Pirea (NXP OSS) 508514def5dSRadu Pirea (NXP OSS) nxp_c45_get_hwtxts_out: 509514def5dSRadu Pirea (NXP OSS) mutex_unlock(&priv->ptp_lock); 510514def5dSRadu Pirea (NXP OSS) return valid; 511514def5dSRadu Pirea (NXP OSS) } 512514def5dSRadu Pirea (NXP OSS) 513514def5dSRadu Pirea (NXP OSS) static void nxp_c45_process_txts(struct nxp_c45_phy *priv, 514514def5dSRadu Pirea (NXP OSS) struct nxp_c45_hwts *txts) 515514def5dSRadu Pirea (NXP OSS) { 516514def5dSRadu Pirea (NXP OSS) struct sk_buff *skb, *tmp, *skb_match = NULL; 517514def5dSRadu Pirea (NXP OSS) struct skb_shared_hwtstamps shhwtstamps; 518514def5dSRadu Pirea (NXP OSS) struct timespec64 ts; 519514def5dSRadu Pirea (NXP OSS) unsigned long flags; 520514def5dSRadu Pirea (NXP OSS) bool ts_match; 521514def5dSRadu Pirea (NXP OSS) s64 ts_ns; 522514def5dSRadu Pirea (NXP OSS) 523514def5dSRadu Pirea (NXP OSS) spin_lock_irqsave(&priv->tx_queue.lock, flags); 524514def5dSRadu Pirea (NXP OSS) skb_queue_walk_safe(&priv->tx_queue, skb, tmp) { 525514def5dSRadu Pirea (NXP OSS) ts_match = nxp_c45_match_ts(NXP_C45_SKB_CB(skb)->header, txts, 526514def5dSRadu Pirea (NXP OSS) NXP_C45_SKB_CB(skb)->type); 527514def5dSRadu Pirea (NXP OSS) if (!ts_match) 528514def5dSRadu Pirea (NXP OSS) continue; 529514def5dSRadu Pirea (NXP OSS) skb_match = skb; 530514def5dSRadu Pirea (NXP OSS) __skb_unlink(skb, &priv->tx_queue); 531514def5dSRadu Pirea (NXP OSS) break; 532514def5dSRadu Pirea (NXP OSS) } 533514def5dSRadu Pirea (NXP OSS) spin_unlock_irqrestore(&priv->tx_queue.lock, flags); 534514def5dSRadu Pirea (NXP OSS) 535514def5dSRadu Pirea (NXP OSS) if (skb_match) { 536514def5dSRadu Pirea (NXP OSS) nxp_c45_ptp_gettimex64(&priv->caps, &ts, NULL); 537514def5dSRadu Pirea (NXP OSS) nxp_c45_reconstruct_ts(&ts, txts); 538514def5dSRadu Pirea (NXP OSS) memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 539514def5dSRadu Pirea (NXP OSS) ts_ns = timespec64_to_ns(&ts); 540514def5dSRadu Pirea (NXP OSS) shhwtstamps.hwtstamp = ns_to_ktime(ts_ns); 541514def5dSRadu Pirea (NXP OSS) skb_complete_tx_timestamp(skb_match, &shhwtstamps); 542514def5dSRadu Pirea (NXP OSS) } else { 543514def5dSRadu Pirea (NXP OSS) phydev_warn(priv->phydev, 544514def5dSRadu Pirea (NXP OSS) "the tx timestamp doesn't match with any skb\n"); 545514def5dSRadu Pirea (NXP OSS) } 546514def5dSRadu Pirea (NXP OSS) } 547514def5dSRadu Pirea (NXP OSS) 548514def5dSRadu Pirea (NXP OSS) static long nxp_c45_do_aux_work(struct ptp_clock_info *ptp) 549514def5dSRadu Pirea (NXP OSS) { 550514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(ptp, struct nxp_c45_phy, caps); 551514def5dSRadu Pirea (NXP OSS) bool poll_txts = nxp_c45_poll_txts(priv->phydev); 552514def5dSRadu Pirea (NXP OSS) struct skb_shared_hwtstamps *shhwtstamps_rx; 5537a71c8aaSRadu Pirea (NXP OSS) struct ptp_clock_event event; 554514def5dSRadu Pirea (NXP OSS) struct nxp_c45_hwts hwts; 555514def5dSRadu Pirea (NXP OSS) bool reschedule = false; 556514def5dSRadu Pirea (NXP OSS) struct timespec64 ts; 557514def5dSRadu Pirea (NXP OSS) struct sk_buff *skb; 558514def5dSRadu Pirea (NXP OSS) bool txts_valid; 559514def5dSRadu Pirea (NXP OSS) u32 ts_raw; 560514def5dSRadu Pirea (NXP OSS) 561514def5dSRadu Pirea (NXP OSS) while (!skb_queue_empty_lockless(&priv->tx_queue) && poll_txts) { 562514def5dSRadu Pirea (NXP OSS) txts_valid = nxp_c45_get_hwtxts(priv, &hwts); 563514def5dSRadu Pirea (NXP OSS) if (unlikely(!txts_valid)) { 564514def5dSRadu Pirea (NXP OSS) /* Still more skbs in the queue */ 565514def5dSRadu Pirea (NXP OSS) reschedule = true; 566514def5dSRadu Pirea (NXP OSS) break; 567514def5dSRadu Pirea (NXP OSS) } 568514def5dSRadu Pirea (NXP OSS) 569514def5dSRadu Pirea (NXP OSS) nxp_c45_process_txts(priv, &hwts); 570514def5dSRadu Pirea (NXP OSS) } 571514def5dSRadu Pirea (NXP OSS) 572514def5dSRadu Pirea (NXP OSS) while ((skb = skb_dequeue(&priv->rx_queue)) != NULL) { 573109258edSVladimir Oltean nxp_c45_ptp_gettimex64(&priv->caps, &ts, NULL); 574514def5dSRadu Pirea (NXP OSS) ts_raw = __be32_to_cpu(NXP_C45_SKB_CB(skb)->header->reserved2); 575514def5dSRadu Pirea (NXP OSS) hwts.sec = ts_raw >> 30; 576514def5dSRadu Pirea (NXP OSS) hwts.nsec = ts_raw & GENMASK(29, 0); 577514def5dSRadu Pirea (NXP OSS) nxp_c45_reconstruct_ts(&ts, &hwts); 578514def5dSRadu Pirea (NXP OSS) shhwtstamps_rx = skb_hwtstamps(skb); 579514def5dSRadu Pirea (NXP OSS) shhwtstamps_rx->hwtstamp = ns_to_ktime(timespec64_to_ns(&ts)); 580514def5dSRadu Pirea (NXP OSS) NXP_C45_SKB_CB(skb)->header->reserved2 = 0; 581a3d73e15SSebastian Andrzej Siewior netif_rx(skb); 582514def5dSRadu Pirea (NXP OSS) } 583514def5dSRadu Pirea (NXP OSS) 5847a71c8aaSRadu Pirea (NXP OSS) if (priv->extts) { 5857a71c8aaSRadu Pirea (NXP OSS) nxp_c45_get_extts(priv, &ts); 5867a71c8aaSRadu Pirea (NXP OSS) if (timespec64_compare(&ts, &priv->extts_ts) != 0) { 5877a71c8aaSRadu Pirea (NXP OSS) priv->extts_ts = ts; 5887a71c8aaSRadu Pirea (NXP OSS) event.index = priv->extts_index; 5897a71c8aaSRadu Pirea (NXP OSS) event.type = PTP_CLOCK_EXTTS; 5907a71c8aaSRadu Pirea (NXP OSS) event.timestamp = ns_to_ktime(timespec64_to_ns(&ts)); 5917a71c8aaSRadu Pirea (NXP OSS) ptp_clock_event(priv->ptp_clock, &event); 5927a71c8aaSRadu Pirea (NXP OSS) } 5937a71c8aaSRadu Pirea (NXP OSS) reschedule = true; 5947a71c8aaSRadu Pirea (NXP OSS) } 5957a71c8aaSRadu Pirea (NXP OSS) 596514def5dSRadu Pirea (NXP OSS) return reschedule ? 1 : -1; 597514def5dSRadu Pirea (NXP OSS) } 598514def5dSRadu Pirea (NXP OSS) 5997a71c8aaSRadu Pirea (NXP OSS) static void nxp_c45_gpio_config(struct nxp_c45_phy *priv, 6007a71c8aaSRadu Pirea (NXP OSS) int pin, u16 pin_cfg) 6017a71c8aaSRadu Pirea (NXP OSS) { 6027a71c8aaSRadu Pirea (NXP OSS) struct phy_device *phydev = priv->phydev; 6037a71c8aaSRadu Pirea (NXP OSS) 6047a71c8aaSRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, 6057a71c8aaSRadu Pirea (NXP OSS) VEND1_GPIO_FUNC_CONFIG_BASE + pin, pin_cfg); 6067a71c8aaSRadu Pirea (NXP OSS) } 6077a71c8aaSRadu Pirea (NXP OSS) 6087a71c8aaSRadu Pirea (NXP OSS) static int nxp_c45_perout_enable(struct nxp_c45_phy *priv, 6097a71c8aaSRadu Pirea (NXP OSS) struct ptp_perout_request *perout, int on) 6107a71c8aaSRadu Pirea (NXP OSS) { 6116c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_regmap *regmap = nxp_c45_get_regmap(priv->phydev); 6127a71c8aaSRadu Pirea (NXP OSS) struct phy_device *phydev = priv->phydev; 6137a71c8aaSRadu Pirea (NXP OSS) int pin; 6147a71c8aaSRadu Pirea (NXP OSS) 6157a71c8aaSRadu Pirea (NXP OSS) if (perout->flags & ~PTP_PEROUT_PHASE) 6167a71c8aaSRadu Pirea (NXP OSS) return -EOPNOTSUPP; 6177a71c8aaSRadu Pirea (NXP OSS) 6187a71c8aaSRadu Pirea (NXP OSS) pin = ptp_find_pin(priv->ptp_clock, PTP_PF_PEROUT, perout->index); 6197a71c8aaSRadu Pirea (NXP OSS) if (pin < 0) 6207a71c8aaSRadu Pirea (NXP OSS) return pin; 6217a71c8aaSRadu Pirea (NXP OSS) 6227a71c8aaSRadu Pirea (NXP OSS) if (!on) { 6236c0c85daSRadu Pirea (NXP OSS) nxp_c45_clear_reg_field(priv->phydev, 6246c0c85daSRadu Pirea (NXP OSS) ®map->pps_enable); 6256c0c85daSRadu Pirea (NXP OSS) nxp_c45_clear_reg_field(priv->phydev, 6266c0c85daSRadu Pirea (NXP OSS) ®map->pps_polarity); 6277a71c8aaSRadu Pirea (NXP OSS) 6287a71c8aaSRadu Pirea (NXP OSS) nxp_c45_gpio_config(priv, pin, GPIO_DISABLE); 6297a71c8aaSRadu Pirea (NXP OSS) 6307a71c8aaSRadu Pirea (NXP OSS) return 0; 6317a71c8aaSRadu Pirea (NXP OSS) } 6327a71c8aaSRadu Pirea (NXP OSS) 6337a71c8aaSRadu Pirea (NXP OSS) /* The PPS signal is fixed to 1 second and is always generated when the 6347a71c8aaSRadu Pirea (NXP OSS) * seconds counter is incremented. The start time is not configurable. 6357a71c8aaSRadu Pirea (NXP OSS) * If the clock is adjusted, the PPS signal is automatically readjusted. 6367a71c8aaSRadu Pirea (NXP OSS) */ 6377a71c8aaSRadu Pirea (NXP OSS) if (perout->period.sec != 1 || perout->period.nsec != 0) { 6387a71c8aaSRadu Pirea (NXP OSS) phydev_warn(phydev, "The period can be set only to 1 second."); 6397a71c8aaSRadu Pirea (NXP OSS) return -EINVAL; 6407a71c8aaSRadu Pirea (NXP OSS) } 6417a71c8aaSRadu Pirea (NXP OSS) 6427a71c8aaSRadu Pirea (NXP OSS) if (!(perout->flags & PTP_PEROUT_PHASE)) { 6437a71c8aaSRadu Pirea (NXP OSS) if (perout->start.sec != 0 || perout->start.nsec != 0) { 6447a71c8aaSRadu Pirea (NXP OSS) phydev_warn(phydev, "The start time is not configurable. Should be set to 0 seconds and 0 nanoseconds."); 6457a71c8aaSRadu Pirea (NXP OSS) return -EINVAL; 6467a71c8aaSRadu Pirea (NXP OSS) } 6477a71c8aaSRadu Pirea (NXP OSS) } else { 6487a71c8aaSRadu Pirea (NXP OSS) if (perout->phase.nsec != 0 && 6497a71c8aaSRadu Pirea (NXP OSS) perout->phase.nsec != (NSEC_PER_SEC >> 1)) { 6507a71c8aaSRadu Pirea (NXP OSS) phydev_warn(phydev, "The phase can be set only to 0 or 500000000 nanoseconds."); 6517a71c8aaSRadu Pirea (NXP OSS) return -EINVAL; 6527a71c8aaSRadu Pirea (NXP OSS) } 6537a71c8aaSRadu Pirea (NXP OSS) 6547a71c8aaSRadu Pirea (NXP OSS) if (perout->phase.nsec == 0) 6556c0c85daSRadu Pirea (NXP OSS) nxp_c45_clear_reg_field(priv->phydev, 6566c0c85daSRadu Pirea (NXP OSS) ®map->pps_polarity); 6577a71c8aaSRadu Pirea (NXP OSS) else 6586c0c85daSRadu Pirea (NXP OSS) nxp_c45_set_reg_field(priv->phydev, 6596c0c85daSRadu Pirea (NXP OSS) ®map->pps_polarity); 6607a71c8aaSRadu Pirea (NXP OSS) } 6617a71c8aaSRadu Pirea (NXP OSS) 6627a71c8aaSRadu Pirea (NXP OSS) nxp_c45_gpio_config(priv, pin, GPIO_PPS_OUT_CFG); 6637a71c8aaSRadu Pirea (NXP OSS) 6646c0c85daSRadu Pirea (NXP OSS) nxp_c45_set_reg_field(priv->phydev, ®map->pps_enable); 6657a71c8aaSRadu Pirea (NXP OSS) 6667a71c8aaSRadu Pirea (NXP OSS) return 0; 6677a71c8aaSRadu Pirea (NXP OSS) } 6687a71c8aaSRadu Pirea (NXP OSS) 6697a71c8aaSRadu Pirea (NXP OSS) static int nxp_c45_extts_enable(struct nxp_c45_phy *priv, 6707a71c8aaSRadu Pirea (NXP OSS) struct ptp_extts_request *extts, int on) 6717a71c8aaSRadu Pirea (NXP OSS) { 6727a71c8aaSRadu Pirea (NXP OSS) int pin; 6737a71c8aaSRadu Pirea (NXP OSS) 6747a71c8aaSRadu Pirea (NXP OSS) if (extts->flags & ~(PTP_ENABLE_FEATURE | 6757a71c8aaSRadu Pirea (NXP OSS) PTP_RISING_EDGE | 6767a71c8aaSRadu Pirea (NXP OSS) PTP_FALLING_EDGE | 6777a71c8aaSRadu Pirea (NXP OSS) PTP_STRICT_FLAGS)) 6787a71c8aaSRadu Pirea (NXP OSS) return -EOPNOTSUPP; 6797a71c8aaSRadu Pirea (NXP OSS) 6807a71c8aaSRadu Pirea (NXP OSS) /* Sampling on both edges is not supported */ 6817a71c8aaSRadu Pirea (NXP OSS) if ((extts->flags & PTP_RISING_EDGE) && 6827a71c8aaSRadu Pirea (NXP OSS) (extts->flags & PTP_FALLING_EDGE)) 6837a71c8aaSRadu Pirea (NXP OSS) return -EOPNOTSUPP; 6847a71c8aaSRadu Pirea (NXP OSS) 6857a71c8aaSRadu Pirea (NXP OSS) pin = ptp_find_pin(priv->ptp_clock, PTP_PF_EXTTS, extts->index); 6867a71c8aaSRadu Pirea (NXP OSS) if (pin < 0) 6877a71c8aaSRadu Pirea (NXP OSS) return pin; 6887a71c8aaSRadu Pirea (NXP OSS) 6897a71c8aaSRadu Pirea (NXP OSS) if (!on) { 6907a71c8aaSRadu Pirea (NXP OSS) nxp_c45_gpio_config(priv, pin, GPIO_DISABLE); 6917a71c8aaSRadu Pirea (NXP OSS) priv->extts = false; 6927a71c8aaSRadu Pirea (NXP OSS) 6937a71c8aaSRadu Pirea (NXP OSS) return 0; 6947a71c8aaSRadu Pirea (NXP OSS) } 6957a71c8aaSRadu Pirea (NXP OSS) 6967a71c8aaSRadu Pirea (NXP OSS) if (extts->flags & PTP_RISING_EDGE) 6977a71c8aaSRadu Pirea (NXP OSS) phy_clear_bits_mmd(priv->phydev, MDIO_MMD_VEND1, 6987a71c8aaSRadu Pirea (NXP OSS) VEND1_PTP_CONFIG, EXT_TRG_EDGE); 6997a71c8aaSRadu Pirea (NXP OSS) 7007a71c8aaSRadu Pirea (NXP OSS) if (extts->flags & PTP_FALLING_EDGE) 7017a71c8aaSRadu Pirea (NXP OSS) phy_set_bits_mmd(priv->phydev, MDIO_MMD_VEND1, 7027a71c8aaSRadu Pirea (NXP OSS) VEND1_PTP_CONFIG, EXT_TRG_EDGE); 7037a71c8aaSRadu Pirea (NXP OSS) 7047a71c8aaSRadu Pirea (NXP OSS) nxp_c45_gpio_config(priv, pin, GPIO_EXTTS_OUT_CFG); 7057a71c8aaSRadu Pirea (NXP OSS) priv->extts = true; 7067a71c8aaSRadu Pirea (NXP OSS) priv->extts_index = extts->index; 7077a71c8aaSRadu Pirea (NXP OSS) ptp_schedule_worker(priv->ptp_clock, 0); 7087a71c8aaSRadu Pirea (NXP OSS) 7097a71c8aaSRadu Pirea (NXP OSS) return 0; 7107a71c8aaSRadu Pirea (NXP OSS) } 7117a71c8aaSRadu Pirea (NXP OSS) 7127a71c8aaSRadu Pirea (NXP OSS) static int nxp_c45_ptp_enable(struct ptp_clock_info *ptp, 7137a71c8aaSRadu Pirea (NXP OSS) struct ptp_clock_request *req, int on) 7147a71c8aaSRadu Pirea (NXP OSS) { 7157a71c8aaSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(ptp, struct nxp_c45_phy, caps); 7167a71c8aaSRadu Pirea (NXP OSS) 7177a71c8aaSRadu Pirea (NXP OSS) switch (req->type) { 7187a71c8aaSRadu Pirea (NXP OSS) case PTP_CLK_REQ_EXTTS: 7197a71c8aaSRadu Pirea (NXP OSS) return nxp_c45_extts_enable(priv, &req->extts, on); 7207a71c8aaSRadu Pirea (NXP OSS) case PTP_CLK_REQ_PEROUT: 7217a71c8aaSRadu Pirea (NXP OSS) return nxp_c45_perout_enable(priv, &req->perout, on); 7227a71c8aaSRadu Pirea (NXP OSS) default: 7237a71c8aaSRadu Pirea (NXP OSS) return -EOPNOTSUPP; 7247a71c8aaSRadu Pirea (NXP OSS) } 7257a71c8aaSRadu Pirea (NXP OSS) } 7267a71c8aaSRadu Pirea (NXP OSS) 7277a71c8aaSRadu Pirea (NXP OSS) static struct ptp_pin_desc nxp_c45_ptp_pins[] = { 7287a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio0", 0, PTP_PF_NONE}, 7297a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio1", 1, PTP_PF_NONE}, 7307a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio2", 2, PTP_PF_NONE}, 7317a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio3", 3, PTP_PF_NONE}, 7327a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio4", 4, PTP_PF_NONE}, 7337a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio5", 5, PTP_PF_NONE}, 7347a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio6", 6, PTP_PF_NONE}, 7357a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio7", 7, PTP_PF_NONE}, 7367a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio8", 8, PTP_PF_NONE}, 7377a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio9", 9, PTP_PF_NONE}, 7387a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio10", 10, PTP_PF_NONE}, 7397a71c8aaSRadu Pirea (NXP OSS) { "nxp_c45_gpio11", 11, PTP_PF_NONE}, 7407a71c8aaSRadu Pirea (NXP OSS) }; 7417a71c8aaSRadu Pirea (NXP OSS) 7427a71c8aaSRadu Pirea (NXP OSS) static int nxp_c45_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin, 7437a71c8aaSRadu Pirea (NXP OSS) enum ptp_pin_function func, unsigned int chan) 7447a71c8aaSRadu Pirea (NXP OSS) { 7457a71c8aaSRadu Pirea (NXP OSS) if (pin >= ARRAY_SIZE(nxp_c45_ptp_pins)) 7467a71c8aaSRadu Pirea (NXP OSS) return -EINVAL; 7477a71c8aaSRadu Pirea (NXP OSS) 7487a71c8aaSRadu Pirea (NXP OSS) switch (func) { 7497a71c8aaSRadu Pirea (NXP OSS) case PTP_PF_NONE: 7507a71c8aaSRadu Pirea (NXP OSS) case PTP_PF_PEROUT: 7517a71c8aaSRadu Pirea (NXP OSS) case PTP_PF_EXTTS: 7527a71c8aaSRadu Pirea (NXP OSS) break; 7537a71c8aaSRadu Pirea (NXP OSS) default: 7547a71c8aaSRadu Pirea (NXP OSS) return -EOPNOTSUPP; 7557a71c8aaSRadu Pirea (NXP OSS) } 7567a71c8aaSRadu Pirea (NXP OSS) 7577a71c8aaSRadu Pirea (NXP OSS) return 0; 7587a71c8aaSRadu Pirea (NXP OSS) } 7597a71c8aaSRadu Pirea (NXP OSS) 760514def5dSRadu Pirea (NXP OSS) static int nxp_c45_init_ptp_clock(struct nxp_c45_phy *priv) 761514def5dSRadu Pirea (NXP OSS) { 762514def5dSRadu Pirea (NXP OSS) priv->caps = (struct ptp_clock_info) { 763514def5dSRadu Pirea (NXP OSS) .owner = THIS_MODULE, 764514def5dSRadu Pirea (NXP OSS) .name = "NXP C45 PHC", 765514def5dSRadu Pirea (NXP OSS) .max_adj = 16666666, 766514def5dSRadu Pirea (NXP OSS) .adjfine = nxp_c45_ptp_adjfine, 767514def5dSRadu Pirea (NXP OSS) .adjtime = nxp_c45_ptp_adjtime, 768514def5dSRadu Pirea (NXP OSS) .gettimex64 = nxp_c45_ptp_gettimex64, 769514def5dSRadu Pirea (NXP OSS) .settime64 = nxp_c45_ptp_settime64, 7707a71c8aaSRadu Pirea (NXP OSS) .enable = nxp_c45_ptp_enable, 7717a71c8aaSRadu Pirea (NXP OSS) .verify = nxp_c45_ptp_verify_pin, 772514def5dSRadu Pirea (NXP OSS) .do_aux_work = nxp_c45_do_aux_work, 7737a71c8aaSRadu Pirea (NXP OSS) .pin_config = nxp_c45_ptp_pins, 7747a71c8aaSRadu Pirea (NXP OSS) .n_pins = ARRAY_SIZE(nxp_c45_ptp_pins), 7757a71c8aaSRadu Pirea (NXP OSS) .n_ext_ts = 1, 7767a71c8aaSRadu Pirea (NXP OSS) .n_per_out = 1, 777514def5dSRadu Pirea (NXP OSS) }; 778514def5dSRadu Pirea (NXP OSS) 779514def5dSRadu Pirea (NXP OSS) priv->ptp_clock = ptp_clock_register(&priv->caps, 780514def5dSRadu Pirea (NXP OSS) &priv->phydev->mdio.dev); 781514def5dSRadu Pirea (NXP OSS) 782514def5dSRadu Pirea (NXP OSS) if (IS_ERR(priv->ptp_clock)) 783514def5dSRadu Pirea (NXP OSS) return PTR_ERR(priv->ptp_clock); 784514def5dSRadu Pirea (NXP OSS) 785514def5dSRadu Pirea (NXP OSS) if (!priv->ptp_clock) 786514def5dSRadu Pirea (NXP OSS) return -ENOMEM; 787514def5dSRadu Pirea (NXP OSS) 788514def5dSRadu Pirea (NXP OSS) return 0; 789514def5dSRadu Pirea (NXP OSS) } 790514def5dSRadu Pirea (NXP OSS) 791514def5dSRadu Pirea (NXP OSS) static void nxp_c45_txtstamp(struct mii_timestamper *mii_ts, 792514def5dSRadu Pirea (NXP OSS) struct sk_buff *skb, int type) 793514def5dSRadu Pirea (NXP OSS) { 794514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(mii_ts, struct nxp_c45_phy, 795514def5dSRadu Pirea (NXP OSS) mii_ts); 796514def5dSRadu Pirea (NXP OSS) 797514def5dSRadu Pirea (NXP OSS) switch (priv->hwts_tx) { 798514def5dSRadu Pirea (NXP OSS) case HWTSTAMP_TX_ON: 799514def5dSRadu Pirea (NXP OSS) NXP_C45_SKB_CB(skb)->type = type; 800514def5dSRadu Pirea (NXP OSS) NXP_C45_SKB_CB(skb)->header = ptp_parse_header(skb, type); 801514def5dSRadu Pirea (NXP OSS) skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 802514def5dSRadu Pirea (NXP OSS) skb_queue_tail(&priv->tx_queue, skb); 803514def5dSRadu Pirea (NXP OSS) if (nxp_c45_poll_txts(priv->phydev)) 804514def5dSRadu Pirea (NXP OSS) ptp_schedule_worker(priv->ptp_clock, 0); 805514def5dSRadu Pirea (NXP OSS) break; 806514def5dSRadu Pirea (NXP OSS) case HWTSTAMP_TX_OFF: 807514def5dSRadu Pirea (NXP OSS) default: 808514def5dSRadu Pirea (NXP OSS) kfree_skb(skb); 809514def5dSRadu Pirea (NXP OSS) break; 810514def5dSRadu Pirea (NXP OSS) } 811514def5dSRadu Pirea (NXP OSS) } 812514def5dSRadu Pirea (NXP OSS) 813514def5dSRadu Pirea (NXP OSS) static bool nxp_c45_rxtstamp(struct mii_timestamper *mii_ts, 814514def5dSRadu Pirea (NXP OSS) struct sk_buff *skb, int type) 815514def5dSRadu Pirea (NXP OSS) { 816514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(mii_ts, struct nxp_c45_phy, 817514def5dSRadu Pirea (NXP OSS) mii_ts); 818514def5dSRadu Pirea (NXP OSS) struct ptp_header *header = ptp_parse_header(skb, type); 819514def5dSRadu Pirea (NXP OSS) 820514def5dSRadu Pirea (NXP OSS) if (!header) 821514def5dSRadu Pirea (NXP OSS) return false; 822514def5dSRadu Pirea (NXP OSS) 823514def5dSRadu Pirea (NXP OSS) if (!priv->hwts_rx) 824514def5dSRadu Pirea (NXP OSS) return false; 825514def5dSRadu Pirea (NXP OSS) 826514def5dSRadu Pirea (NXP OSS) NXP_C45_SKB_CB(skb)->header = header; 827514def5dSRadu Pirea (NXP OSS) skb_queue_tail(&priv->rx_queue, skb); 828514def5dSRadu Pirea (NXP OSS) ptp_schedule_worker(priv->ptp_clock, 0); 829514def5dSRadu Pirea (NXP OSS) 830514def5dSRadu Pirea (NXP OSS) return true; 831514def5dSRadu Pirea (NXP OSS) } 832514def5dSRadu Pirea (NXP OSS) 833514def5dSRadu Pirea (NXP OSS) static int nxp_c45_hwtstamp(struct mii_timestamper *mii_ts, 834514def5dSRadu Pirea (NXP OSS) struct ifreq *ifreq) 835514def5dSRadu Pirea (NXP OSS) { 836514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(mii_ts, struct nxp_c45_phy, 837514def5dSRadu Pirea (NXP OSS) mii_ts); 838514def5dSRadu Pirea (NXP OSS) struct phy_device *phydev = priv->phydev; 8396c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_phy_data *data; 840514def5dSRadu Pirea (NXP OSS) struct hwtstamp_config cfg; 841514def5dSRadu Pirea (NXP OSS) 842514def5dSRadu Pirea (NXP OSS) if (copy_from_user(&cfg, ifreq->ifr_data, sizeof(cfg))) 843514def5dSRadu Pirea (NXP OSS) return -EFAULT; 844514def5dSRadu Pirea (NXP OSS) 845514def5dSRadu Pirea (NXP OSS) if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ON) 846514def5dSRadu Pirea (NXP OSS) return -ERANGE; 847514def5dSRadu Pirea (NXP OSS) 8486c0c85daSRadu Pirea (NXP OSS) data = nxp_c45_get_data(phydev); 849514def5dSRadu Pirea (NXP OSS) priv->hwts_tx = cfg.tx_type; 850514def5dSRadu Pirea (NXP OSS) 851514def5dSRadu Pirea (NXP OSS) switch (cfg.rx_filter) { 852514def5dSRadu Pirea (NXP OSS) case HWTSTAMP_FILTER_NONE: 853514def5dSRadu Pirea (NXP OSS) priv->hwts_rx = 0; 854514def5dSRadu Pirea (NXP OSS) break; 855514def5dSRadu Pirea (NXP OSS) case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 856514def5dSRadu Pirea (NXP OSS) case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 857514def5dSRadu Pirea (NXP OSS) case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 858514def5dSRadu Pirea (NXP OSS) priv->hwts_rx = 1; 859514def5dSRadu Pirea (NXP OSS) cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT; 860514def5dSRadu Pirea (NXP OSS) break; 861514def5dSRadu Pirea (NXP OSS) default: 862514def5dSRadu Pirea (NXP OSS) return -ERANGE; 863514def5dSRadu Pirea (NXP OSS) } 864514def5dSRadu Pirea (NXP OSS) 865514def5dSRadu Pirea (NXP OSS) if (priv->hwts_rx || priv->hwts_tx) { 8666c0c85daSRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, 8676c0c85daSRadu Pirea (NXP OSS) data->regmap->vend1_event_msg_filt, 868514def5dSRadu Pirea (NXP OSS) EVENT_MSG_FILT_ALL); 8696c0c85daSRadu Pirea (NXP OSS) data->ptp_enable(phydev, true); 870514def5dSRadu Pirea (NXP OSS) } else { 8716c0c85daSRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, 8726c0c85daSRadu Pirea (NXP OSS) data->regmap->vend1_event_msg_filt, 873514def5dSRadu Pirea (NXP OSS) EVENT_MSG_FILT_NONE); 8746c0c85daSRadu Pirea (NXP OSS) data->ptp_enable(phydev, false); 875514def5dSRadu Pirea (NXP OSS) } 876514def5dSRadu Pirea (NXP OSS) 877514def5dSRadu Pirea (NXP OSS) if (nxp_c45_poll_txts(priv->phydev)) 878514def5dSRadu Pirea (NXP OSS) goto nxp_c45_no_ptp_irq; 879514def5dSRadu Pirea (NXP OSS) 880514def5dSRadu Pirea (NXP OSS) if (priv->hwts_tx) 8816c0c85daSRadu Pirea (NXP OSS) nxp_c45_set_reg_field(phydev, &data->regmap->irq_egr_ts_en); 882514def5dSRadu Pirea (NXP OSS) else 8836c0c85daSRadu Pirea (NXP OSS) nxp_c45_clear_reg_field(phydev, &data->regmap->irq_egr_ts_en); 884514def5dSRadu Pirea (NXP OSS) 885514def5dSRadu Pirea (NXP OSS) nxp_c45_no_ptp_irq: 886514def5dSRadu Pirea (NXP OSS) return copy_to_user(ifreq->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 887514def5dSRadu Pirea (NXP OSS) } 888514def5dSRadu Pirea (NXP OSS) 889514def5dSRadu Pirea (NXP OSS) static int nxp_c45_ts_info(struct mii_timestamper *mii_ts, 890514def5dSRadu Pirea (NXP OSS) struct ethtool_ts_info *ts_info) 891514def5dSRadu Pirea (NXP OSS) { 892514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = container_of(mii_ts, struct nxp_c45_phy, 893514def5dSRadu Pirea (NXP OSS) mii_ts); 894514def5dSRadu Pirea (NXP OSS) 895514def5dSRadu Pirea (NXP OSS) ts_info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE | 896514def5dSRadu Pirea (NXP OSS) SOF_TIMESTAMPING_RX_HARDWARE | 897514def5dSRadu Pirea (NXP OSS) SOF_TIMESTAMPING_RAW_HARDWARE; 898514def5dSRadu Pirea (NXP OSS) ts_info->phc_index = ptp_clock_index(priv->ptp_clock); 899514def5dSRadu Pirea (NXP OSS) ts_info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON); 900514def5dSRadu Pirea (NXP OSS) ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | 901514def5dSRadu Pirea (NXP OSS) (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) | 902514def5dSRadu Pirea (NXP OSS) (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) | 903514def5dSRadu Pirea (NXP OSS) (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT); 904514def5dSRadu Pirea (NXP OSS) 905514def5dSRadu Pirea (NXP OSS) return 0; 906514def5dSRadu Pirea (NXP OSS) } 907514def5dSRadu Pirea (NXP OSS) 9086c0c85daSRadu Pirea (NXP OSS) static const struct nxp_c45_phy_stats common_hw_stats[] = { 9096c0c85daSRadu Pirea (NXP OSS) { "phy_link_status_drop_cnt", 9106c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x8352, MDIO_MMD_VEND1, 8, 6), }, 9116c0c85daSRadu Pirea (NXP OSS) { "phy_link_availability_drop_cnt", 9126c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x8352, MDIO_MMD_VEND1, 0, 6), }, 9136c0c85daSRadu Pirea (NXP OSS) { "phy_link_loss_cnt", 9146c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x8353, MDIO_MMD_VEND1, 10, 6), }, 9156c0c85daSRadu Pirea (NXP OSS) { "phy_link_failure_cnt", 9166c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x8353, MDIO_MMD_VEND1, 0, 10), }, 9176c0c85daSRadu Pirea (NXP OSS) { "phy_symbol_error_cnt", 9186c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x8350, MDIO_MMD_VEND1, 0, 16) }, 9196c0c85daSRadu Pirea (NXP OSS) }; 9206c0c85daSRadu Pirea (NXP OSS) 9216c0c85daSRadu Pirea (NXP OSS) static const struct nxp_c45_phy_stats tja1103_hw_stats[] = { 9226c0c85daSRadu Pirea (NXP OSS) { "rx_preamble_count", 9236c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0xAFCE, MDIO_MMD_VEND1, 0, 6), }, 9246c0c85daSRadu Pirea (NXP OSS) { "tx_preamble_count", 9256c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0xAFCF, MDIO_MMD_VEND1, 0, 6), }, 9266c0c85daSRadu Pirea (NXP OSS) { "rx_ipg_length", 9276c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0xAFD0, MDIO_MMD_VEND1, 0, 9), }, 9286c0c85daSRadu Pirea (NXP OSS) { "tx_ipg_length", 9296c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0xAFD1, MDIO_MMD_VEND1, 0, 9), }, 930b050f2f1SRadu Pirea (NXP OSS) }; 931b050f2f1SRadu Pirea (NXP OSS) 932b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_get_sset_count(struct phy_device *phydev) 933b050f2f1SRadu Pirea (NXP OSS) { 9346c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_phy_data *phy_data = nxp_c45_get_data(phydev); 9356c0c85daSRadu Pirea (NXP OSS) 9366c0c85daSRadu Pirea (NXP OSS) return ARRAY_SIZE(common_hw_stats) + (phy_data ? phy_data->n_stats : 0); 937b050f2f1SRadu Pirea (NXP OSS) } 938b050f2f1SRadu Pirea (NXP OSS) 939b050f2f1SRadu Pirea (NXP OSS) static void nxp_c45_get_strings(struct phy_device *phydev, u8 *data) 940b050f2f1SRadu Pirea (NXP OSS) { 9416c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_phy_data *phy_data = nxp_c45_get_data(phydev); 9426c0c85daSRadu Pirea (NXP OSS) size_t count = nxp_c45_get_sset_count(phydev); 9436c0c85daSRadu Pirea (NXP OSS) size_t idx; 944b050f2f1SRadu Pirea (NXP OSS) size_t i; 945b050f2f1SRadu Pirea (NXP OSS) 9466c0c85daSRadu Pirea (NXP OSS) for (i = 0; i < count; i++) { 9476c0c85daSRadu Pirea (NXP OSS) if (i < ARRAY_SIZE(common_hw_stats)) { 9486c0c85daSRadu Pirea (NXP OSS) strscpy(data + i * ETH_GSTRING_LEN, 9496c0c85daSRadu Pirea (NXP OSS) common_hw_stats[i].name, ETH_GSTRING_LEN); 9506c0c85daSRadu Pirea (NXP OSS) continue; 9516c0c85daSRadu Pirea (NXP OSS) } 9526c0c85daSRadu Pirea (NXP OSS) idx = i - ARRAY_SIZE(common_hw_stats); 9536c0c85daSRadu Pirea (NXP OSS) strscpy(data + i * ETH_GSTRING_LEN, 9546c0c85daSRadu Pirea (NXP OSS) phy_data->stats[idx].name, ETH_GSTRING_LEN); 955b050f2f1SRadu Pirea (NXP OSS) } 956b050f2f1SRadu Pirea (NXP OSS) } 957b050f2f1SRadu Pirea (NXP OSS) 958b050f2f1SRadu Pirea (NXP OSS) static void nxp_c45_get_stats(struct phy_device *phydev, 959b050f2f1SRadu Pirea (NXP OSS) struct ethtool_stats *stats, u64 *data) 960b050f2f1SRadu Pirea (NXP OSS) { 9616c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_phy_data *phy_data = nxp_c45_get_data(phydev); 9626c0c85daSRadu Pirea (NXP OSS) size_t count = nxp_c45_get_sset_count(phydev); 9636c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_reg_field *reg_field; 9646c0c85daSRadu Pirea (NXP OSS) size_t idx; 965b050f2f1SRadu Pirea (NXP OSS) size_t i; 966b050f2f1SRadu Pirea (NXP OSS) int ret; 967b050f2f1SRadu Pirea (NXP OSS) 9686c0c85daSRadu Pirea (NXP OSS) for (i = 0; i < count; i++) { 9696c0c85daSRadu Pirea (NXP OSS) if (i < ARRAY_SIZE(common_hw_stats)) { 9706c0c85daSRadu Pirea (NXP OSS) reg_field = &common_hw_stats[i].counter; 971b050f2f1SRadu Pirea (NXP OSS) } else { 9726c0c85daSRadu Pirea (NXP OSS) idx = i - ARRAY_SIZE(common_hw_stats); 9736c0c85daSRadu Pirea (NXP OSS) reg_field = &phy_data->stats[idx].counter; 974b050f2f1SRadu Pirea (NXP OSS) } 9756c0c85daSRadu Pirea (NXP OSS) 9766c0c85daSRadu Pirea (NXP OSS) ret = nxp_c45_read_reg_field(phydev, reg_field); 9776c0c85daSRadu Pirea (NXP OSS) if (ret < 0) 9786c0c85daSRadu Pirea (NXP OSS) data[i] = U64_MAX; 9796c0c85daSRadu Pirea (NXP OSS) else 9806c0c85daSRadu Pirea (NXP OSS) data[i] = ret; 981b050f2f1SRadu Pirea (NXP OSS) } 982b050f2f1SRadu Pirea (NXP OSS) } 983b050f2f1SRadu Pirea (NXP OSS) 984b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_config_enable(struct phy_device *phydev) 985b050f2f1SRadu Pirea (NXP OSS) { 986b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_DEVICE_CONTROL, 987b050f2f1SRadu Pirea (NXP OSS) DEVICE_CONTROL_CONFIG_GLOBAL_EN | 988b050f2f1SRadu Pirea (NXP OSS) DEVICE_CONTROL_CONFIG_ALL_EN); 989b050f2f1SRadu Pirea (NXP OSS) usleep_range(400, 450); 990b050f2f1SRadu Pirea (NXP OSS) 991b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_PORT_CONTROL, 992b050f2f1SRadu Pirea (NXP OSS) PORT_CONTROL_EN); 993b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_PHY_CONTROL, 994b050f2f1SRadu Pirea (NXP OSS) PHY_CONFIG_EN); 995b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_PORT_INFRA_CONTROL, 996b050f2f1SRadu Pirea (NXP OSS) PORT_INFRA_CONTROL_EN); 997b050f2f1SRadu Pirea (NXP OSS) 998b050f2f1SRadu Pirea (NXP OSS) return 0; 999b050f2f1SRadu Pirea (NXP OSS) } 1000b050f2f1SRadu Pirea (NXP OSS) 1001b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_start_op(struct phy_device *phydev) 1002b050f2f1SRadu Pirea (NXP OSS) { 1003b050f2f1SRadu Pirea (NXP OSS) return phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_PHY_CONTROL, 1004b050f2f1SRadu Pirea (NXP OSS) PHY_START_OP); 1005b050f2f1SRadu Pirea (NXP OSS) } 1006b050f2f1SRadu Pirea (NXP OSS) 1007b2f0ca00SRadu Pirea (NXP OSS) static int nxp_c45_config_intr(struct phy_device *phydev) 1008b2f0ca00SRadu Pirea (NXP OSS) { 1009b2f0ca00SRadu Pirea (NXP OSS) if (phydev->interrupts == PHY_INTERRUPT_ENABLED) 1010b2f0ca00SRadu Pirea (NXP OSS) return phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, 1011b2f0ca00SRadu Pirea (NXP OSS) VEND1_PHY_IRQ_EN, PHY_IRQ_LINK_EVENT); 1012b2f0ca00SRadu Pirea (NXP OSS) else 1013b2f0ca00SRadu Pirea (NXP OSS) return phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, 1014b2f0ca00SRadu Pirea (NXP OSS) VEND1_PHY_IRQ_EN, PHY_IRQ_LINK_EVENT); 1015b2f0ca00SRadu Pirea (NXP OSS) } 1016b2f0ca00SRadu Pirea (NXP OSS) 1017b2f0ca00SRadu Pirea (NXP OSS) static irqreturn_t nxp_c45_handle_interrupt(struct phy_device *phydev) 1018b2f0ca00SRadu Pirea (NXP OSS) { 10196c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_phy_data *data = nxp_c45_get_data(phydev); 1020514def5dSRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = phydev->priv; 1021b2f0ca00SRadu Pirea (NXP OSS) irqreturn_t ret = IRQ_NONE; 1022514def5dSRadu Pirea (NXP OSS) struct nxp_c45_hwts hwts; 1023b2f0ca00SRadu Pirea (NXP OSS) int irq; 1024b2f0ca00SRadu Pirea (NXP OSS) 1025b2f0ca00SRadu Pirea (NXP OSS) irq = phy_read_mmd(phydev, MDIO_MMD_VEND1, VEND1_PHY_IRQ_STATUS); 1026b2f0ca00SRadu Pirea (NXP OSS) if (irq & PHY_IRQ_LINK_EVENT) { 1027b2f0ca00SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_PHY_IRQ_ACK, 1028b2f0ca00SRadu Pirea (NXP OSS) PHY_IRQ_LINK_EVENT); 1029b2f0ca00SRadu Pirea (NXP OSS) phy_trigger_machine(phydev); 1030b2f0ca00SRadu Pirea (NXP OSS) ret = IRQ_HANDLED; 1031b2f0ca00SRadu Pirea (NXP OSS) } 1032b2f0ca00SRadu Pirea (NXP OSS) 1033514def5dSRadu Pirea (NXP OSS) /* There is no need for ACK. 1034514def5dSRadu Pirea (NXP OSS) * The irq signal will be asserted until the EGR TS FIFO will be 1035514def5dSRadu Pirea (NXP OSS) * emptied. 1036514def5dSRadu Pirea (NXP OSS) */ 10376c0c85daSRadu Pirea (NXP OSS) irq = nxp_c45_read_reg_field(phydev, &data->regmap->irq_egr_ts_status); 10386c0c85daSRadu Pirea (NXP OSS) if (irq) { 1039514def5dSRadu Pirea (NXP OSS) while (nxp_c45_get_hwtxts(priv, &hwts)) 1040514def5dSRadu Pirea (NXP OSS) nxp_c45_process_txts(priv, &hwts); 1041514def5dSRadu Pirea (NXP OSS) 1042514def5dSRadu Pirea (NXP OSS) ret = IRQ_HANDLED; 1043514def5dSRadu Pirea (NXP OSS) } 1044514def5dSRadu Pirea (NXP OSS) 1045b2f0ca00SRadu Pirea (NXP OSS) return ret; 1046b2f0ca00SRadu Pirea (NXP OSS) } 1047b2f0ca00SRadu Pirea (NXP OSS) 1048b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_soft_reset(struct phy_device *phydev) 1049b050f2f1SRadu Pirea (NXP OSS) { 1050b050f2f1SRadu Pirea (NXP OSS) int ret; 1051b050f2f1SRadu Pirea (NXP OSS) 1052b050f2f1SRadu Pirea (NXP OSS) ret = phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_DEVICE_CONTROL, 1053b050f2f1SRadu Pirea (NXP OSS) DEVICE_CONTROL_RESET); 1054b050f2f1SRadu Pirea (NXP OSS) if (ret) 1055b050f2f1SRadu Pirea (NXP OSS) return ret; 1056b050f2f1SRadu Pirea (NXP OSS) 1057b050f2f1SRadu Pirea (NXP OSS) return phy_read_mmd_poll_timeout(phydev, MDIO_MMD_VEND1, 1058b050f2f1SRadu Pirea (NXP OSS) VEND1_DEVICE_CONTROL, ret, 1059b050f2f1SRadu Pirea (NXP OSS) !(ret & DEVICE_CONTROL_RESET), 20000, 1060b050f2f1SRadu Pirea (NXP OSS) 240000, false); 1061b050f2f1SRadu Pirea (NXP OSS) } 1062b050f2f1SRadu Pirea (NXP OSS) 1063b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_cable_test_start(struct phy_device *phydev) 1064b050f2f1SRadu Pirea (NXP OSS) { 10656c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_regmap *regmap = nxp_c45_get_regmap(phydev); 10666c0c85daSRadu Pirea (NXP OSS) 10676c0c85daSRadu Pirea (NXP OSS) return phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, regmap->cable_test, 1068b050f2f1SRadu Pirea (NXP OSS) CABLE_TEST_ENABLE | CABLE_TEST_START); 1069b050f2f1SRadu Pirea (NXP OSS) } 1070b050f2f1SRadu Pirea (NXP OSS) 1071b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_cable_test_get_status(struct phy_device *phydev, 1072b050f2f1SRadu Pirea (NXP OSS) bool *finished) 1073b050f2f1SRadu Pirea (NXP OSS) { 10746c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_regmap *regmap = nxp_c45_get_regmap(phydev); 1075b050f2f1SRadu Pirea (NXP OSS) int ret; 1076b050f2f1SRadu Pirea (NXP OSS) u8 cable_test_result; 1077b050f2f1SRadu Pirea (NXP OSS) 10786c0c85daSRadu Pirea (NXP OSS) ret = nxp_c45_read_reg_field(phydev, ®map->cable_test_valid); 10796c0c85daSRadu Pirea (NXP OSS) if (!ret) { 1080b050f2f1SRadu Pirea (NXP OSS) *finished = false; 1081b050f2f1SRadu Pirea (NXP OSS) return 0; 1082b050f2f1SRadu Pirea (NXP OSS) } 1083b050f2f1SRadu Pirea (NXP OSS) 1084b050f2f1SRadu Pirea (NXP OSS) *finished = true; 10856c0c85daSRadu Pirea (NXP OSS) cable_test_result = nxp_c45_read_reg_field(phydev, 10866c0c85daSRadu Pirea (NXP OSS) ®map->cable_test_result); 1087b050f2f1SRadu Pirea (NXP OSS) 1088b050f2f1SRadu Pirea (NXP OSS) switch (cable_test_result) { 1089b050f2f1SRadu Pirea (NXP OSS) case CABLE_TEST_OK: 1090b050f2f1SRadu Pirea (NXP OSS) ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A, 1091b050f2f1SRadu Pirea (NXP OSS) ETHTOOL_A_CABLE_RESULT_CODE_OK); 1092b050f2f1SRadu Pirea (NXP OSS) break; 1093b050f2f1SRadu Pirea (NXP OSS) case CABLE_TEST_SHORTED: 1094b050f2f1SRadu Pirea (NXP OSS) ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A, 1095b050f2f1SRadu Pirea (NXP OSS) ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT); 1096b050f2f1SRadu Pirea (NXP OSS) break; 1097b050f2f1SRadu Pirea (NXP OSS) case CABLE_TEST_OPEN: 1098b050f2f1SRadu Pirea (NXP OSS) ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A, 1099b050f2f1SRadu Pirea (NXP OSS) ETHTOOL_A_CABLE_RESULT_CODE_OPEN); 1100b050f2f1SRadu Pirea (NXP OSS) break; 1101b050f2f1SRadu Pirea (NXP OSS) default: 1102b050f2f1SRadu Pirea (NXP OSS) ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A, 1103b050f2f1SRadu Pirea (NXP OSS) ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC); 1104b050f2f1SRadu Pirea (NXP OSS) } 1105b050f2f1SRadu Pirea (NXP OSS) 11066c0c85daSRadu Pirea (NXP OSS) phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, regmap->cable_test, 1107b050f2f1SRadu Pirea (NXP OSS) CABLE_TEST_ENABLE); 1108b050f2f1SRadu Pirea (NXP OSS) 1109b050f2f1SRadu Pirea (NXP OSS) return nxp_c45_start_op(phydev); 1110b050f2f1SRadu Pirea (NXP OSS) } 1111b050f2f1SRadu Pirea (NXP OSS) 1112b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_get_sqi(struct phy_device *phydev) 1113b050f2f1SRadu Pirea (NXP OSS) { 1114b050f2f1SRadu Pirea (NXP OSS) int reg; 1115b050f2f1SRadu Pirea (NXP OSS) 1116b050f2f1SRadu Pirea (NXP OSS) reg = phy_read_mmd(phydev, MDIO_MMD_VEND1, VEND1_SIGNAL_QUALITY); 1117b050f2f1SRadu Pirea (NXP OSS) if (!(reg & SQI_VALID)) 1118b050f2f1SRadu Pirea (NXP OSS) return -EINVAL; 1119b050f2f1SRadu Pirea (NXP OSS) 1120b050f2f1SRadu Pirea (NXP OSS) reg &= SQI_MASK; 1121b050f2f1SRadu Pirea (NXP OSS) 1122b050f2f1SRadu Pirea (NXP OSS) return reg; 1123b050f2f1SRadu Pirea (NXP OSS) } 1124b050f2f1SRadu Pirea (NXP OSS) 1125b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_get_sqi_max(struct phy_device *phydev) 1126b050f2f1SRadu Pirea (NXP OSS) { 1127b050f2f1SRadu Pirea (NXP OSS) return MAX_SQI; 1128b050f2f1SRadu Pirea (NXP OSS) } 1129b050f2f1SRadu Pirea (NXP OSS) 1130b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_check_delay(struct phy_device *phydev, u32 delay) 1131b050f2f1SRadu Pirea (NXP OSS) { 1132b050f2f1SRadu Pirea (NXP OSS) if (delay < MIN_ID_PS) { 1133b050f2f1SRadu Pirea (NXP OSS) phydev_err(phydev, "delay value smaller than %u\n", MIN_ID_PS); 1134b050f2f1SRadu Pirea (NXP OSS) return -EINVAL; 1135b050f2f1SRadu Pirea (NXP OSS) } 1136b050f2f1SRadu Pirea (NXP OSS) 1137b050f2f1SRadu Pirea (NXP OSS) if (delay > MAX_ID_PS) { 1138b050f2f1SRadu Pirea (NXP OSS) phydev_err(phydev, "delay value higher than %u\n", MAX_ID_PS); 1139b050f2f1SRadu Pirea (NXP OSS) return -EINVAL; 1140b050f2f1SRadu Pirea (NXP OSS) } 1141b050f2f1SRadu Pirea (NXP OSS) 1142b050f2f1SRadu Pirea (NXP OSS) return 0; 1143b050f2f1SRadu Pirea (NXP OSS) } 1144b050f2f1SRadu Pirea (NXP OSS) 11456c0c85daSRadu Pirea (NXP OSS) static void nxp_c45_counters_enable(struct phy_device *phydev) 11466c0c85daSRadu Pirea (NXP OSS) { 11476c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_phy_data *data = nxp_c45_get_data(phydev); 11486c0c85daSRadu Pirea (NXP OSS) 11496c0c85daSRadu Pirea (NXP OSS) phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_LINK_DROP_COUNTER, 11506c0c85daSRadu Pirea (NXP OSS) COUNTER_EN); 11516c0c85daSRadu Pirea (NXP OSS) 11526c0c85daSRadu Pirea (NXP OSS) data->counters_enable(phydev); 11536c0c85daSRadu Pirea (NXP OSS) } 11546c0c85daSRadu Pirea (NXP OSS) 11556c0c85daSRadu Pirea (NXP OSS) static void nxp_c45_ptp_init(struct phy_device *phydev) 11566c0c85daSRadu Pirea (NXP OSS) { 11576c0c85daSRadu Pirea (NXP OSS) const struct nxp_c45_phy_data *data = nxp_c45_get_data(phydev); 11586c0c85daSRadu Pirea (NXP OSS) 11596c0c85daSRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, 11606c0c85daSRadu Pirea (NXP OSS) data->regmap->vend1_ptp_clk_period, 11616c0c85daSRadu Pirea (NXP OSS) data->ptp_clk_period); 11626c0c85daSRadu Pirea (NXP OSS) nxp_c45_clear_reg_field(phydev, &data->regmap->ltc_lock_ctrl); 11636c0c85daSRadu Pirea (NXP OSS) 11646c0c85daSRadu Pirea (NXP OSS) data->ptp_init(phydev); 11656c0c85daSRadu Pirea (NXP OSS) } 11666c0c85daSRadu Pirea (NXP OSS) 1167b050f2f1SRadu Pirea (NXP OSS) static u64 nxp_c45_get_phase_shift(u64 phase_offset_raw) 1168b050f2f1SRadu Pirea (NXP OSS) { 1169b050f2f1SRadu Pirea (NXP OSS) /* The delay in degree phase is 73.8 + phase_offset_raw * 0.9. 1170b050f2f1SRadu Pirea (NXP OSS) * To avoid floating point operations we'll multiply by 10 1171b050f2f1SRadu Pirea (NXP OSS) * and get 1 decimal point precision. 1172b050f2f1SRadu Pirea (NXP OSS) */ 1173b050f2f1SRadu Pirea (NXP OSS) phase_offset_raw *= 10; 11746b3a6310SRadu Pirea (NXP OSS) phase_offset_raw -= 738; 1175b050f2f1SRadu Pirea (NXP OSS) return div_u64(phase_offset_raw, 9); 1176b050f2f1SRadu Pirea (NXP OSS) } 1177b050f2f1SRadu Pirea (NXP OSS) 1178b050f2f1SRadu Pirea (NXP OSS) static void nxp_c45_disable_delays(struct phy_device *phydev) 1179b050f2f1SRadu Pirea (NXP OSS) { 1180b050f2f1SRadu Pirea (NXP OSS) phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_TXID, ID_ENABLE); 1181b050f2f1SRadu Pirea (NXP OSS) phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_RXID, ID_ENABLE); 1182b050f2f1SRadu Pirea (NXP OSS) } 1183b050f2f1SRadu Pirea (NXP OSS) 1184b050f2f1SRadu Pirea (NXP OSS) static void nxp_c45_set_delays(struct phy_device *phydev) 1185b050f2f1SRadu Pirea (NXP OSS) { 1186b050f2f1SRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = phydev->priv; 1187b050f2f1SRadu Pirea (NXP OSS) u64 tx_delay = priv->tx_delay; 1188b050f2f1SRadu Pirea (NXP OSS) u64 rx_delay = priv->rx_delay; 1189b050f2f1SRadu Pirea (NXP OSS) u64 degree; 1190b050f2f1SRadu Pirea (NXP OSS) 1191b050f2f1SRadu Pirea (NXP OSS) if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 1192b050f2f1SRadu Pirea (NXP OSS) phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) { 1193b050f2f1SRadu Pirea (NXP OSS) degree = div_u64(tx_delay, PS_PER_DEGREE); 1194b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_TXID, 1195b050f2f1SRadu Pirea (NXP OSS) ID_ENABLE | nxp_c45_get_phase_shift(degree)); 1196b050f2f1SRadu Pirea (NXP OSS) } else { 1197b050f2f1SRadu Pirea (NXP OSS) phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_TXID, 1198b050f2f1SRadu Pirea (NXP OSS) ID_ENABLE); 1199b050f2f1SRadu Pirea (NXP OSS) } 1200b050f2f1SRadu Pirea (NXP OSS) 1201b050f2f1SRadu Pirea (NXP OSS) if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 1202b050f2f1SRadu Pirea (NXP OSS) phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) { 1203b050f2f1SRadu Pirea (NXP OSS) degree = div_u64(rx_delay, PS_PER_DEGREE); 1204b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_RXID, 1205b050f2f1SRadu Pirea (NXP OSS) ID_ENABLE | nxp_c45_get_phase_shift(degree)); 1206b050f2f1SRadu Pirea (NXP OSS) } else { 1207b050f2f1SRadu Pirea (NXP OSS) phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_RXID, 1208b050f2f1SRadu Pirea (NXP OSS) ID_ENABLE); 1209b050f2f1SRadu Pirea (NXP OSS) } 1210b050f2f1SRadu Pirea (NXP OSS) } 1211b050f2f1SRadu Pirea (NXP OSS) 1212b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_get_delays(struct phy_device *phydev) 1213b050f2f1SRadu Pirea (NXP OSS) { 1214b050f2f1SRadu Pirea (NXP OSS) struct nxp_c45_phy *priv = phydev->priv; 1215b050f2f1SRadu Pirea (NXP OSS) int ret; 1216b050f2f1SRadu Pirea (NXP OSS) 1217b050f2f1SRadu Pirea (NXP OSS) if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 1218b050f2f1SRadu Pirea (NXP OSS) phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) { 1219b050f2f1SRadu Pirea (NXP OSS) ret = device_property_read_u32(&phydev->mdio.dev, 1220b050f2f1SRadu Pirea (NXP OSS) "tx-internal-delay-ps", 1221b050f2f1SRadu Pirea (NXP OSS) &priv->tx_delay); 1222b050f2f1SRadu Pirea (NXP OSS) if (ret) 1223b050f2f1SRadu Pirea (NXP OSS) priv->tx_delay = DEFAULT_ID_PS; 1224b050f2f1SRadu Pirea (NXP OSS) 1225b050f2f1SRadu Pirea (NXP OSS) ret = nxp_c45_check_delay(phydev, priv->tx_delay); 1226b050f2f1SRadu Pirea (NXP OSS) if (ret) { 1227b050f2f1SRadu Pirea (NXP OSS) phydev_err(phydev, 1228b050f2f1SRadu Pirea (NXP OSS) "tx-internal-delay-ps invalid value\n"); 1229b050f2f1SRadu Pirea (NXP OSS) return ret; 1230b050f2f1SRadu Pirea (NXP OSS) } 1231b050f2f1SRadu Pirea (NXP OSS) } 1232b050f2f1SRadu Pirea (NXP OSS) 1233b050f2f1SRadu Pirea (NXP OSS) if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 1234b050f2f1SRadu Pirea (NXP OSS) phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) { 1235b050f2f1SRadu Pirea (NXP OSS) ret = device_property_read_u32(&phydev->mdio.dev, 1236b050f2f1SRadu Pirea (NXP OSS) "rx-internal-delay-ps", 1237b050f2f1SRadu Pirea (NXP OSS) &priv->rx_delay); 1238b050f2f1SRadu Pirea (NXP OSS) if (ret) 1239b050f2f1SRadu Pirea (NXP OSS) priv->rx_delay = DEFAULT_ID_PS; 1240b050f2f1SRadu Pirea (NXP OSS) 1241b050f2f1SRadu Pirea (NXP OSS) ret = nxp_c45_check_delay(phydev, priv->rx_delay); 1242b050f2f1SRadu Pirea (NXP OSS) if (ret) { 1243b050f2f1SRadu Pirea (NXP OSS) phydev_err(phydev, 1244b050f2f1SRadu Pirea (NXP OSS) "rx-internal-delay-ps invalid value\n"); 1245b050f2f1SRadu Pirea (NXP OSS) return ret; 1246b050f2f1SRadu Pirea (NXP OSS) } 1247b050f2f1SRadu Pirea (NXP OSS) } 1248b050f2f1SRadu Pirea (NXP OSS) 1249b050f2f1SRadu Pirea (NXP OSS) return 0; 1250b050f2f1SRadu Pirea (NXP OSS) } 1251b050f2f1SRadu Pirea (NXP OSS) 1252b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_set_phy_mode(struct phy_device *phydev) 1253b050f2f1SRadu Pirea (NXP OSS) { 1254b050f2f1SRadu Pirea (NXP OSS) int ret; 1255b050f2f1SRadu Pirea (NXP OSS) 1256b050f2f1SRadu Pirea (NXP OSS) ret = phy_read_mmd(phydev, MDIO_MMD_VEND1, VEND1_ABILITIES); 1257b050f2f1SRadu Pirea (NXP OSS) phydev_dbg(phydev, "Clause 45 managed PHY abilities 0x%x\n", ret); 1258b050f2f1SRadu Pirea (NXP OSS) 1259b050f2f1SRadu Pirea (NXP OSS) switch (phydev->interface) { 1260b050f2f1SRadu Pirea (NXP OSS) case PHY_INTERFACE_MODE_RGMII: 1261b050f2f1SRadu Pirea (NXP OSS) if (!(ret & RGMII_ABILITY)) { 1262b050f2f1SRadu Pirea (NXP OSS) phydev_err(phydev, "rgmii mode not supported\n"); 1263b050f2f1SRadu Pirea (NXP OSS) return -EINVAL; 1264b050f2f1SRadu Pirea (NXP OSS) } 1265b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_MII_BASIC_CONFIG, 1266b050f2f1SRadu Pirea (NXP OSS) MII_BASIC_CONFIG_RGMII); 1267b050f2f1SRadu Pirea (NXP OSS) nxp_c45_disable_delays(phydev); 1268b050f2f1SRadu Pirea (NXP OSS) break; 1269b050f2f1SRadu Pirea (NXP OSS) case PHY_INTERFACE_MODE_RGMII_ID: 1270b050f2f1SRadu Pirea (NXP OSS) case PHY_INTERFACE_MODE_RGMII_TXID: 1271b050f2f1SRadu Pirea (NXP OSS) case PHY_INTERFACE_MODE_RGMII_RXID: 1272b050f2f1SRadu Pirea (NXP OSS) if (!(ret & RGMII_ID_ABILITY)) { 1273b050f2f1SRadu Pirea (NXP OSS) phydev_err(phydev, "rgmii-id, rgmii-txid, rgmii-rxid modes are not supported\n"); 1274b050f2f1SRadu Pirea (NXP OSS) return -EINVAL; 1275b050f2f1SRadu Pirea (NXP OSS) } 1276b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_MII_BASIC_CONFIG, 1277b050f2f1SRadu Pirea (NXP OSS) MII_BASIC_CONFIG_RGMII); 1278b050f2f1SRadu Pirea (NXP OSS) ret = nxp_c45_get_delays(phydev); 1279b050f2f1SRadu Pirea (NXP OSS) if (ret) 1280b050f2f1SRadu Pirea (NXP OSS) return ret; 1281b050f2f1SRadu Pirea (NXP OSS) 1282b050f2f1SRadu Pirea (NXP OSS) nxp_c45_set_delays(phydev); 1283b050f2f1SRadu Pirea (NXP OSS) break; 1284b050f2f1SRadu Pirea (NXP OSS) case PHY_INTERFACE_MODE_MII: 1285b050f2f1SRadu Pirea (NXP OSS) if (!(ret & MII_ABILITY)) { 1286b050f2f1SRadu Pirea (NXP OSS) phydev_err(phydev, "mii mode not supported\n"); 1287b050f2f1SRadu Pirea (NXP OSS) return -EINVAL; 1288b050f2f1SRadu Pirea (NXP OSS) } 1289b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_MII_BASIC_CONFIG, 1290b050f2f1SRadu Pirea (NXP OSS) MII_BASIC_CONFIG_MII); 1291b050f2f1SRadu Pirea (NXP OSS) break; 1292b050f2f1SRadu Pirea (NXP OSS) case PHY_INTERFACE_MODE_REVMII: 1293b050f2f1SRadu Pirea (NXP OSS) if (!(ret & REVMII_ABILITY)) { 1294b050f2f1SRadu Pirea (NXP OSS) phydev_err(phydev, "rev-mii mode not supported\n"); 1295b050f2f1SRadu Pirea (NXP OSS) return -EINVAL; 1296b050f2f1SRadu Pirea (NXP OSS) } 1297b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_MII_BASIC_CONFIG, 1298b050f2f1SRadu Pirea (NXP OSS) MII_BASIC_CONFIG_MII | MII_BASIC_CONFIG_REV); 1299b050f2f1SRadu Pirea (NXP OSS) break; 1300b050f2f1SRadu Pirea (NXP OSS) case PHY_INTERFACE_MODE_RMII: 1301b050f2f1SRadu Pirea (NXP OSS) if (!(ret & RMII_ABILITY)) { 1302b050f2f1SRadu Pirea (NXP OSS) phydev_err(phydev, "rmii mode not supported\n"); 1303b050f2f1SRadu Pirea (NXP OSS) return -EINVAL; 1304b050f2f1SRadu Pirea (NXP OSS) } 1305b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_MII_BASIC_CONFIG, 1306b050f2f1SRadu Pirea (NXP OSS) MII_BASIC_CONFIG_RMII); 1307b050f2f1SRadu Pirea (NXP OSS) break; 1308b050f2f1SRadu Pirea (NXP OSS) case PHY_INTERFACE_MODE_SGMII: 1309b050f2f1SRadu Pirea (NXP OSS) if (!(ret & SGMII_ABILITY)) { 1310b050f2f1SRadu Pirea (NXP OSS) phydev_err(phydev, "sgmii mode not supported\n"); 1311b050f2f1SRadu Pirea (NXP OSS) return -EINVAL; 1312b050f2f1SRadu Pirea (NXP OSS) } 1313b050f2f1SRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_MII_BASIC_CONFIG, 1314b050f2f1SRadu Pirea (NXP OSS) MII_BASIC_CONFIG_SGMII); 1315b050f2f1SRadu Pirea (NXP OSS) break; 1316b050f2f1SRadu Pirea (NXP OSS) case PHY_INTERFACE_MODE_INTERNAL: 1317b050f2f1SRadu Pirea (NXP OSS) break; 1318b050f2f1SRadu Pirea (NXP OSS) default: 1319b050f2f1SRadu Pirea (NXP OSS) return -EINVAL; 1320b050f2f1SRadu Pirea (NXP OSS) } 1321b050f2f1SRadu Pirea (NXP OSS) 1322b050f2f1SRadu Pirea (NXP OSS) return 0; 1323b050f2f1SRadu Pirea (NXP OSS) } 1324b050f2f1SRadu Pirea (NXP OSS) 1325b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_config_init(struct phy_device *phydev) 1326b050f2f1SRadu Pirea (NXP OSS) { 1327b050f2f1SRadu Pirea (NXP OSS) int ret; 1328b050f2f1SRadu Pirea (NXP OSS) 1329b050f2f1SRadu Pirea (NXP OSS) ret = nxp_c45_config_enable(phydev); 1330b050f2f1SRadu Pirea (NXP OSS) if (ret) { 1331b050f2f1SRadu Pirea (NXP OSS) phydev_err(phydev, "Failed to enable config\n"); 1332b050f2f1SRadu Pirea (NXP OSS) return ret; 1333b050f2f1SRadu Pirea (NXP OSS) } 1334b050f2f1SRadu Pirea (NXP OSS) 13350b5f0f29SVladimir Oltean /* Bug workaround for SJA1110 rev B: enable write access 13360b5f0f29SVladimir Oltean * to MDIO_MMD_PMAPMD 13370b5f0f29SVladimir Oltean */ 13380b5f0f29SVladimir Oltean phy_write_mmd(phydev, MDIO_MMD_VEND1, 0x01F8, 1); 13390b5f0f29SVladimir Oltean phy_write_mmd(phydev, MDIO_MMD_VEND1, 0x01F9, 2); 13400b5f0f29SVladimir Oltean 1341b050f2f1SRadu Pirea (NXP OSS) phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_PHY_CONFIG, 1342b050f2f1SRadu Pirea (NXP OSS) PHY_CONFIG_AUTO); 1343b050f2f1SRadu Pirea (NXP OSS) 1344b050f2f1SRadu Pirea (NXP OSS) ret = nxp_c45_set_phy_mode(phydev); 1345b050f2f1SRadu Pirea (NXP OSS) if (ret) 1346b050f2f1SRadu Pirea (NXP OSS) return ret; 1347b050f2f1SRadu Pirea (NXP OSS) 1348b050f2f1SRadu Pirea (NXP OSS) phydev->autoneg = AUTONEG_DISABLE; 1349b050f2f1SRadu Pirea (NXP OSS) 13506c0c85daSRadu Pirea (NXP OSS) nxp_c45_counters_enable(phydev); 13516c0c85daSRadu Pirea (NXP OSS) nxp_c45_ptp_init(phydev); 1352514def5dSRadu Pirea (NXP OSS) 1353b050f2f1SRadu Pirea (NXP OSS) return nxp_c45_start_op(phydev); 1354b050f2f1SRadu Pirea (NXP OSS) } 1355b050f2f1SRadu Pirea (NXP OSS) 1356*369da333SRadu Pirea (NXP OSS) static int nxp_c45_get_features(struct phy_device *phydev) 1357*369da333SRadu Pirea (NXP OSS) { 1358*369da333SRadu Pirea (NXP OSS) linkmode_set_bit(ETHTOOL_LINK_MODE_TP_BIT, phydev->supported); 1359*369da333SRadu Pirea (NXP OSS) linkmode_set_bit(ETHTOOL_LINK_MODE_MII_BIT, phydev->supported); 1360*369da333SRadu Pirea (NXP OSS) 1361*369da333SRadu Pirea (NXP OSS) return genphy_c45_pma_read_abilities(phydev); 1362*369da333SRadu Pirea (NXP OSS) } 1363*369da333SRadu Pirea (NXP OSS) 1364b050f2f1SRadu Pirea (NXP OSS) static int nxp_c45_probe(struct phy_device *phydev) 1365b050f2f1SRadu Pirea (NXP OSS) { 1366b050f2f1SRadu Pirea (NXP OSS) struct nxp_c45_phy *priv; 1367514def5dSRadu Pirea (NXP OSS) int ptp_ability; 1368514def5dSRadu Pirea (NXP OSS) int ret = 0; 1369b050f2f1SRadu Pirea (NXP OSS) 1370b050f2f1SRadu Pirea (NXP OSS) priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL); 1371b050f2f1SRadu Pirea (NXP OSS) if (!priv) 1372b050f2f1SRadu Pirea (NXP OSS) return -ENOMEM; 1373b050f2f1SRadu Pirea (NXP OSS) 1374514def5dSRadu Pirea (NXP OSS) skb_queue_head_init(&priv->tx_queue); 1375514def5dSRadu Pirea (NXP OSS) skb_queue_head_init(&priv->rx_queue); 1376514def5dSRadu Pirea (NXP OSS) 1377514def5dSRadu Pirea (NXP OSS) priv->phydev = phydev; 1378514def5dSRadu Pirea (NXP OSS) 1379b050f2f1SRadu Pirea (NXP OSS) phydev->priv = priv; 1380b050f2f1SRadu Pirea (NXP OSS) 1381514def5dSRadu Pirea (NXP OSS) mutex_init(&priv->ptp_lock); 1382514def5dSRadu Pirea (NXP OSS) 1383514def5dSRadu Pirea (NXP OSS) ptp_ability = phy_read_mmd(phydev, MDIO_MMD_VEND1, 1384514def5dSRadu Pirea (NXP OSS) VEND1_PORT_ABILITIES); 1385514def5dSRadu Pirea (NXP OSS) ptp_ability = !!(ptp_ability & PTP_ABILITY); 1386514def5dSRadu Pirea (NXP OSS) if (!ptp_ability) { 1387565c6d8cSVladimir Oltean phydev_dbg(phydev, "the phy does not support PTP"); 1388514def5dSRadu Pirea (NXP OSS) goto no_ptp_support; 1389514def5dSRadu Pirea (NXP OSS) } 1390514def5dSRadu Pirea (NXP OSS) 1391514def5dSRadu Pirea (NXP OSS) if (IS_ENABLED(CONFIG_PTP_1588_CLOCK) && 1392514def5dSRadu Pirea (NXP OSS) IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING)) { 1393514def5dSRadu Pirea (NXP OSS) priv->mii_ts.rxtstamp = nxp_c45_rxtstamp; 1394514def5dSRadu Pirea (NXP OSS) priv->mii_ts.txtstamp = nxp_c45_txtstamp; 1395514def5dSRadu Pirea (NXP OSS) priv->mii_ts.hwtstamp = nxp_c45_hwtstamp; 1396514def5dSRadu Pirea (NXP OSS) priv->mii_ts.ts_info = nxp_c45_ts_info; 1397514def5dSRadu Pirea (NXP OSS) phydev->mii_ts = &priv->mii_ts; 1398514def5dSRadu Pirea (NXP OSS) ret = nxp_c45_init_ptp_clock(priv); 1399514def5dSRadu Pirea (NXP OSS) } else { 1400514def5dSRadu Pirea (NXP OSS) phydev_dbg(phydev, "PTP support not enabled even if the phy supports it"); 1401514def5dSRadu Pirea (NXP OSS) } 1402514def5dSRadu Pirea (NXP OSS) 1403514def5dSRadu Pirea (NXP OSS) no_ptp_support: 1404514def5dSRadu Pirea (NXP OSS) 1405514def5dSRadu Pirea (NXP OSS) return ret; 1406b050f2f1SRadu Pirea (NXP OSS) } 1407b050f2f1SRadu Pirea (NXP OSS) 1408a4506722SRadu Pirea (OSS) static void nxp_c45_remove(struct phy_device *phydev) 1409a4506722SRadu Pirea (OSS) { 1410a4506722SRadu Pirea (OSS) struct nxp_c45_phy *priv = phydev->priv; 1411a4506722SRadu Pirea (OSS) 1412a4506722SRadu Pirea (OSS) if (priv->ptp_clock) 1413a4506722SRadu Pirea (OSS) ptp_clock_unregister(priv->ptp_clock); 1414a4506722SRadu Pirea (OSS) 1415a4506722SRadu Pirea (OSS) skb_queue_purge(&priv->tx_queue); 1416a4506722SRadu Pirea (OSS) skb_queue_purge(&priv->rx_queue); 1417a4506722SRadu Pirea (OSS) } 1418a4506722SRadu Pirea (OSS) 14196c0c85daSRadu Pirea (NXP OSS) static void tja1103_counters_enable(struct phy_device *phydev) 14206c0c85daSRadu Pirea (NXP OSS) { 14216c0c85daSRadu Pirea (NXP OSS) phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_RX_PREAMBLE_COUNT, 14226c0c85daSRadu Pirea (NXP OSS) COUNTER_EN); 14236c0c85daSRadu Pirea (NXP OSS) phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_TX_PREAMBLE_COUNT, 14246c0c85daSRadu Pirea (NXP OSS) COUNTER_EN); 14256c0c85daSRadu Pirea (NXP OSS) phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_RX_IPG_LENGTH, 14266c0c85daSRadu Pirea (NXP OSS) COUNTER_EN); 14276c0c85daSRadu Pirea (NXP OSS) phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_TX_IPG_LENGTH, 14286c0c85daSRadu Pirea (NXP OSS) COUNTER_EN); 14296c0c85daSRadu Pirea (NXP OSS) } 14306c0c85daSRadu Pirea (NXP OSS) 14316c0c85daSRadu Pirea (NXP OSS) static void tja1103_ptp_init(struct phy_device *phydev) 14326c0c85daSRadu Pirea (NXP OSS) { 14336c0c85daSRadu Pirea (NXP OSS) phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_RX_TS_INSRT_CTRL, 14346c0c85daSRadu Pirea (NXP OSS) TJA1103_RX_TS_INSRT_MODE2); 14356c0c85daSRadu Pirea (NXP OSS) phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_PORT_FUNC_ENABLES, 14366c0c85daSRadu Pirea (NXP OSS) PTP_ENABLE); 14376c0c85daSRadu Pirea (NXP OSS) } 14386c0c85daSRadu Pirea (NXP OSS) 14396c0c85daSRadu Pirea (NXP OSS) static void tja1103_ptp_enable(struct phy_device *phydev, bool enable) 14406c0c85daSRadu Pirea (NXP OSS) { 14416c0c85daSRadu Pirea (NXP OSS) if (enable) 14426c0c85daSRadu Pirea (NXP OSS) phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, 14436c0c85daSRadu Pirea (NXP OSS) VEND1_PORT_PTP_CONTROL, 14446c0c85daSRadu Pirea (NXP OSS) PORT_PTP_CONTROL_BYPASS); 14456c0c85daSRadu Pirea (NXP OSS) else 14466c0c85daSRadu Pirea (NXP OSS) phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, 14476c0c85daSRadu Pirea (NXP OSS) VEND1_PORT_PTP_CONTROL, 14486c0c85daSRadu Pirea (NXP OSS) PORT_PTP_CONTROL_BYPASS); 14496c0c85daSRadu Pirea (NXP OSS) } 14506c0c85daSRadu Pirea (NXP OSS) 14516c0c85daSRadu Pirea (NXP OSS) static const struct nxp_c45_regmap tja1103_regmap = { 14526c0c85daSRadu Pirea (NXP OSS) .vend1_ptp_clk_period = 0x1104, 14536c0c85daSRadu Pirea (NXP OSS) .vend1_event_msg_filt = 0x1148, 14546c0c85daSRadu Pirea (NXP OSS) .pps_enable = 14556c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x1102, MDIO_MMD_VEND1, 3, 1), 14566c0c85daSRadu Pirea (NXP OSS) .pps_polarity = 14576c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x1102, MDIO_MMD_VEND1, 2, 1), 14586c0c85daSRadu Pirea (NXP OSS) .ltc_lock_ctrl = 14596c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x1115, MDIO_MMD_VEND1, 0, 1), 14606c0c85daSRadu Pirea (NXP OSS) .ltc_read = 14616c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x1105, MDIO_MMD_VEND1, 2, 1), 14626c0c85daSRadu Pirea (NXP OSS) .ltc_write = 14636c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x1105, MDIO_MMD_VEND1, 0, 1), 14646c0c85daSRadu Pirea (NXP OSS) .vend1_ltc_wr_nsec_0 = 0x1106, 14656c0c85daSRadu Pirea (NXP OSS) .vend1_ltc_wr_nsec_1 = 0x1107, 14666c0c85daSRadu Pirea (NXP OSS) .vend1_ltc_wr_sec_0 = 0x1108, 14676c0c85daSRadu Pirea (NXP OSS) .vend1_ltc_wr_sec_1 = 0x1109, 14686c0c85daSRadu Pirea (NXP OSS) .vend1_ltc_rd_nsec_0 = 0x110A, 14696c0c85daSRadu Pirea (NXP OSS) .vend1_ltc_rd_nsec_1 = 0x110B, 14706c0c85daSRadu Pirea (NXP OSS) .vend1_ltc_rd_sec_0 = 0x110C, 14716c0c85daSRadu Pirea (NXP OSS) .vend1_ltc_rd_sec_1 = 0x110D, 14726c0c85daSRadu Pirea (NXP OSS) .vend1_rate_adj_subns_0 = 0x110F, 14736c0c85daSRadu Pirea (NXP OSS) .vend1_rate_adj_subns_1 = 0x1110, 14746c0c85daSRadu Pirea (NXP OSS) .irq_egr_ts_en = 14756c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x1131, MDIO_MMD_VEND1, 0, 1), 14766c0c85daSRadu Pirea (NXP OSS) .irq_egr_ts_status = 14776c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x1132, MDIO_MMD_VEND1, 0, 1), 14786c0c85daSRadu Pirea (NXP OSS) .domain_number = 14796c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x114E, MDIO_MMD_VEND1, 0, 8), 14806c0c85daSRadu Pirea (NXP OSS) .msg_type = 14816c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x114E, MDIO_MMD_VEND1, 8, 4), 14826c0c85daSRadu Pirea (NXP OSS) .sequence_id = 14836c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x114F, MDIO_MMD_VEND1, 0, 16), 14846c0c85daSRadu Pirea (NXP OSS) .sec_1_0 = 14856c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x1151, MDIO_MMD_VEND1, 14, 2), 14866c0c85daSRadu Pirea (NXP OSS) .sec_4_2 = 14876c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x114E, MDIO_MMD_VEND1, 12, 3), 14886c0c85daSRadu Pirea (NXP OSS) .nsec_15_0 = 14896c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x1150, MDIO_MMD_VEND1, 0, 16), 14906c0c85daSRadu Pirea (NXP OSS) .nsec_29_16 = 14916c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x1151, MDIO_MMD_VEND1, 0, 14), 14926c0c85daSRadu Pirea (NXP OSS) .vend1_ext_trg_data_0 = 0x1121, 14936c0c85daSRadu Pirea (NXP OSS) .vend1_ext_trg_data_1 = 0x1122, 14946c0c85daSRadu Pirea (NXP OSS) .vend1_ext_trg_data_2 = 0x1123, 14956c0c85daSRadu Pirea (NXP OSS) .vend1_ext_trg_data_3 = 0x1124, 14966c0c85daSRadu Pirea (NXP OSS) .vend1_ext_trg_ctrl = 0x1126, 14976c0c85daSRadu Pirea (NXP OSS) .cable_test = 0x8330, 14986c0c85daSRadu Pirea (NXP OSS) .cable_test_valid = 14996c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x8330, MDIO_MMD_VEND1, 13, 1), 15006c0c85daSRadu Pirea (NXP OSS) .cable_test_result = 15016c0c85daSRadu Pirea (NXP OSS) NXP_C45_REG_FIELD(0x8330, MDIO_MMD_VEND1, 0, 3), 15026c0c85daSRadu Pirea (NXP OSS) }; 15036c0c85daSRadu Pirea (NXP OSS) 15046c0c85daSRadu Pirea (NXP OSS) static const struct nxp_c45_phy_data tja1103_phy_data = { 15056c0c85daSRadu Pirea (NXP OSS) .regmap = &tja1103_regmap, 15066c0c85daSRadu Pirea (NXP OSS) .stats = tja1103_hw_stats, 15076c0c85daSRadu Pirea (NXP OSS) .n_stats = ARRAY_SIZE(tja1103_hw_stats), 15086c0c85daSRadu Pirea (NXP OSS) .ptp_clk_period = PTP_CLK_PERIOD_100BT1, 15096c0c85daSRadu Pirea (NXP OSS) .counters_enable = tja1103_counters_enable, 15106c0c85daSRadu Pirea (NXP OSS) .ptp_init = tja1103_ptp_init, 15116c0c85daSRadu Pirea (NXP OSS) .ptp_enable = tja1103_ptp_enable, 15126c0c85daSRadu Pirea (NXP OSS) }; 15136c0c85daSRadu Pirea (NXP OSS) 1514b050f2f1SRadu Pirea (NXP OSS) static struct phy_driver nxp_c45_driver[] = { 1515b050f2f1SRadu Pirea (NXP OSS) { 1516b050f2f1SRadu Pirea (NXP OSS) PHY_ID_MATCH_MODEL(PHY_ID_TJA_1103), 1517b050f2f1SRadu Pirea (NXP OSS) .name = "NXP C45 TJA1103", 1518*369da333SRadu Pirea (NXP OSS) .get_features = nxp_c45_get_features, 15196c0c85daSRadu Pirea (NXP OSS) .driver_data = &tja1103_phy_data, 1520b050f2f1SRadu Pirea (NXP OSS) .probe = nxp_c45_probe, 1521b050f2f1SRadu Pirea (NXP OSS) .soft_reset = nxp_c45_soft_reset, 1522ac0687e8SRadu Pirea (NXP OSS) .config_aneg = genphy_c45_config_aneg, 1523b050f2f1SRadu Pirea (NXP OSS) .config_init = nxp_c45_config_init, 1524b2f0ca00SRadu Pirea (NXP OSS) .config_intr = nxp_c45_config_intr, 1525b2f0ca00SRadu Pirea (NXP OSS) .handle_interrupt = nxp_c45_handle_interrupt, 1526ac0687e8SRadu Pirea (NXP OSS) .read_status = genphy_c45_read_status, 1527b050f2f1SRadu Pirea (NXP OSS) .suspend = genphy_c45_pma_suspend, 1528b050f2f1SRadu Pirea (NXP OSS) .resume = genphy_c45_pma_resume, 1529b050f2f1SRadu Pirea (NXP OSS) .get_sset_count = nxp_c45_get_sset_count, 1530b050f2f1SRadu Pirea (NXP OSS) .get_strings = nxp_c45_get_strings, 1531b050f2f1SRadu Pirea (NXP OSS) .get_stats = nxp_c45_get_stats, 1532b050f2f1SRadu Pirea (NXP OSS) .cable_test_start = nxp_c45_cable_test_start, 1533b050f2f1SRadu Pirea (NXP OSS) .cable_test_get_status = nxp_c45_cable_test_get_status, 1534b050f2f1SRadu Pirea (NXP OSS) .set_loopback = genphy_c45_loopback, 1535b050f2f1SRadu Pirea (NXP OSS) .get_sqi = nxp_c45_get_sqi, 1536b050f2f1SRadu Pirea (NXP OSS) .get_sqi_max = nxp_c45_get_sqi_max, 1537a4506722SRadu Pirea (OSS) .remove = nxp_c45_remove, 1538b050f2f1SRadu Pirea (NXP OSS) }, 1539b050f2f1SRadu Pirea (NXP OSS) }; 1540b050f2f1SRadu Pirea (NXP OSS) 1541b050f2f1SRadu Pirea (NXP OSS) module_phy_driver(nxp_c45_driver); 1542b050f2f1SRadu Pirea (NXP OSS) 1543b050f2f1SRadu Pirea (NXP OSS) static struct mdio_device_id __maybe_unused nxp_c45_tbl[] = { 1544b050f2f1SRadu Pirea (NXP OSS) { PHY_ID_MATCH_MODEL(PHY_ID_TJA_1103) }, 1545b050f2f1SRadu Pirea (NXP OSS) { /*sentinel*/ }, 1546b050f2f1SRadu Pirea (NXP OSS) }; 1547b050f2f1SRadu Pirea (NXP OSS) 1548b050f2f1SRadu Pirea (NXP OSS) MODULE_DEVICE_TABLE(mdio, nxp_c45_tbl); 1549b050f2f1SRadu Pirea (NXP OSS) 1550b050f2f1SRadu Pirea (NXP OSS) MODULE_AUTHOR("Radu Pirea <radu-nicolae.pirea@oss.nxp.com>"); 1551b050f2f1SRadu Pirea (NXP OSS) MODULE_DESCRIPTION("NXP C45 PHY driver"); 1552b050f2f1SRadu Pirea (NXP OSS) MODULE_LICENSE("GPL v2"); 1553