1873e65bcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2863d08ecSTakahiro Shimizu /* 3863d08ecSTakahiro Shimizu * PTP 1588 clock using the EG20T PCH 4863d08ecSTakahiro Shimizu * 5863d08ecSTakahiro Shimizu * Copyright (C) 2010 OMICRON electronics GmbH 6863d08ecSTakahiro Shimizu * Copyright (C) 2011-2012 LAPIS SEMICONDUCTOR Co., LTD. 7863d08ecSTakahiro Shimizu * 8863d08ecSTakahiro Shimizu * This code was derived from the IXP46X driver. 9863d08ecSTakahiro Shimizu */ 10863d08ecSTakahiro Shimizu 11863d08ecSTakahiro Shimizu #include <linux/device.h> 12863d08ecSTakahiro Shimizu #include <linux/err.h> 13863d08ecSTakahiro Shimizu #include <linux/interrupt.h> 14863d08ecSTakahiro Shimizu #include <linux/io.h> 158664d49aSAndy Shevchenko #include <linux/io-64-nonatomic-lo-hi.h> 16d09adf61SAndy Shevchenko #include <linux/io-64-nonatomic-hi-lo.h> 17863d08ecSTakahiro Shimizu #include <linux/irq.h> 18863d08ecSTakahiro Shimizu #include <linux/kernel.h> 19863d08ecSTakahiro Shimizu #include <linux/module.h> 20863d08ecSTakahiro Shimizu #include <linux/pci.h> 21863d08ecSTakahiro Shimizu #include <linux/ptp_clock_kernel.h> 22f90fc37fSLee Jones #include <linux/ptp_pch.h> 23769b0dafSGeert Uytterhoeven #include <linux/slab.h> 24863d08ecSTakahiro Shimizu 25863d08ecSTakahiro Shimizu #define STATION_ADDR_LEN 20 26863d08ecSTakahiro Shimizu #define PCI_DEVICE_ID_PCH_1588 0x8819 27863d08ecSTakahiro Shimizu #define IO_MEM_BAR 1 28863d08ecSTakahiro Shimizu 29863d08ecSTakahiro Shimizu #define DEFAULT_ADDEND 0xA0000000 30863d08ecSTakahiro Shimizu #define TICKS_NS_SHIFT 5 31863d08ecSTakahiro Shimizu #define N_EXT_TS 2 32863d08ecSTakahiro Shimizu 33863d08ecSTakahiro Shimizu enum pch_status { 34863d08ecSTakahiro Shimizu PCH_SUCCESS, 35863d08ecSTakahiro Shimizu PCH_INVALIDPARAM, 36863d08ecSTakahiro Shimizu PCH_NOTIMESTAMP, 37863d08ecSTakahiro Shimizu PCH_INTERRUPTMODEINUSE, 38863d08ecSTakahiro Shimizu PCH_FAILED, 39863d08ecSTakahiro Shimizu PCH_UNSUPPORTED, 40863d08ecSTakahiro Shimizu }; 41287f93deSLee Jones 42287f93deSLee Jones /* 43863d08ecSTakahiro Shimizu * struct pch_ts_regs - IEEE 1588 registers 44863d08ecSTakahiro Shimizu */ 45863d08ecSTakahiro Shimizu struct pch_ts_regs { 46863d08ecSTakahiro Shimizu u32 control; 47863d08ecSTakahiro Shimizu u32 event; 48863d08ecSTakahiro Shimizu u32 addend; 49863d08ecSTakahiro Shimizu u32 accum; 50863d08ecSTakahiro Shimizu u32 test; 51863d08ecSTakahiro Shimizu u32 ts_compare; 52863d08ecSTakahiro Shimizu u32 rsystime_lo; 53863d08ecSTakahiro Shimizu u32 rsystime_hi; 54863d08ecSTakahiro Shimizu u32 systime_lo; 55863d08ecSTakahiro Shimizu u32 systime_hi; 56863d08ecSTakahiro Shimizu u32 trgt_lo; 57863d08ecSTakahiro Shimizu u32 trgt_hi; 58863d08ecSTakahiro Shimizu u32 asms_lo; 59863d08ecSTakahiro Shimizu u32 asms_hi; 60863d08ecSTakahiro Shimizu u32 amms_lo; 61863d08ecSTakahiro Shimizu u32 amms_hi; 62863d08ecSTakahiro Shimizu u32 ch_control; 63863d08ecSTakahiro Shimizu u32 ch_event; 64863d08ecSTakahiro Shimizu u32 tx_snap_lo; 65863d08ecSTakahiro Shimizu u32 tx_snap_hi; 66863d08ecSTakahiro Shimizu u32 rx_snap_lo; 67863d08ecSTakahiro Shimizu u32 rx_snap_hi; 68863d08ecSTakahiro Shimizu u32 src_uuid_lo; 69863d08ecSTakahiro Shimizu u32 src_uuid_hi; 70863d08ecSTakahiro Shimizu u32 can_status; 71863d08ecSTakahiro Shimizu u32 can_snap_lo; 72863d08ecSTakahiro Shimizu u32 can_snap_hi; 73863d08ecSTakahiro Shimizu u32 ts_sel; 74863d08ecSTakahiro Shimizu u32 ts_st[6]; 75863d08ecSTakahiro Shimizu u32 reserve1[14]; 76863d08ecSTakahiro Shimizu u32 stl_max_set_en; 77863d08ecSTakahiro Shimizu u32 stl_max_set; 78863d08ecSTakahiro Shimizu u32 reserve2[13]; 79863d08ecSTakahiro Shimizu u32 srst; 80863d08ecSTakahiro Shimizu }; 81863d08ecSTakahiro Shimizu 82863d08ecSTakahiro Shimizu #define PCH_TSC_RESET (1 << 0) 83863d08ecSTakahiro Shimizu #define PCH_TSC_TTM_MASK (1 << 1) 84863d08ecSTakahiro Shimizu #define PCH_TSC_ASMS_MASK (1 << 2) 85863d08ecSTakahiro Shimizu #define PCH_TSC_AMMS_MASK (1 << 3) 86863d08ecSTakahiro Shimizu #define PCH_TSC_PPSM_MASK (1 << 4) 87863d08ecSTakahiro Shimizu #define PCH_TSE_TTIPEND (1 << 1) 88863d08ecSTakahiro Shimizu #define PCH_TSE_SNS (1 << 2) 89863d08ecSTakahiro Shimizu #define PCH_TSE_SNM (1 << 3) 90863d08ecSTakahiro Shimizu #define PCH_TSE_PPS (1 << 4) 91863d08ecSTakahiro Shimizu #define PCH_CC_MM (1 << 0) 92863d08ecSTakahiro Shimizu #define PCH_CC_TA (1 << 1) 93863d08ecSTakahiro Shimizu 94863d08ecSTakahiro Shimizu #define PCH_CC_MODE_SHIFT 16 95863d08ecSTakahiro Shimizu #define PCH_CC_MODE_MASK 0x001F0000 96863d08ecSTakahiro Shimizu #define PCH_CC_VERSION (1 << 31) 97863d08ecSTakahiro Shimizu #define PCH_CE_TXS (1 << 0) 98863d08ecSTakahiro Shimizu #define PCH_CE_RXS (1 << 1) 99863d08ecSTakahiro Shimizu #define PCH_CE_OVR (1 << 0) 100863d08ecSTakahiro Shimizu #define PCH_CE_VAL (1 << 1) 101863d08ecSTakahiro Shimizu #define PCH_ECS_ETH (1 << 0) 102863d08ecSTakahiro Shimizu 103863d08ecSTakahiro Shimizu #define PCH_ECS_CAN (1 << 1) 104863d08ecSTakahiro Shimizu 105863d08ecSTakahiro Shimizu #define PCH_IEEE1588_ETH (1 << 0) 106863d08ecSTakahiro Shimizu #define PCH_IEEE1588_CAN (1 << 1) 107287f93deSLee Jones 108287f93deSLee Jones /* 109863d08ecSTakahiro Shimizu * struct pch_dev - Driver private data 110863d08ecSTakahiro Shimizu */ 111863d08ecSTakahiro Shimizu struct pch_dev { 1127d3ac5c7SSahara struct pch_ts_regs __iomem *regs; 113863d08ecSTakahiro Shimizu struct ptp_clock *ptp_clock; 114863d08ecSTakahiro Shimizu struct ptp_clock_info caps; 115863d08ecSTakahiro Shimizu int exts0_enabled; 116863d08ecSTakahiro Shimizu int exts1_enabled; 117863d08ecSTakahiro Shimizu 118863d08ecSTakahiro Shimizu u32 irq; 119863d08ecSTakahiro Shimizu struct pci_dev *pdev; 120863d08ecSTakahiro Shimizu spinlock_t register_lock; 121863d08ecSTakahiro Shimizu }; 122863d08ecSTakahiro Shimizu 123287f93deSLee Jones /* 124863d08ecSTakahiro Shimizu * struct pch_params - 1588 module parameter 125863d08ecSTakahiro Shimizu */ 126863d08ecSTakahiro Shimizu struct pch_params { 127863d08ecSTakahiro Shimizu u8 station[STATION_ADDR_LEN]; 128863d08ecSTakahiro Shimizu }; 129863d08ecSTakahiro Shimizu 130863d08ecSTakahiro Shimizu /* structure to hold the module parameters */ 131863d08ecSTakahiro Shimizu static struct pch_params pch_param = { 132863d08ecSTakahiro Shimizu "00:00:00:00:00:00" 133863d08ecSTakahiro Shimizu }; 134863d08ecSTakahiro Shimizu 135863d08ecSTakahiro Shimizu /* 136863d08ecSTakahiro Shimizu * Register access functions 137863d08ecSTakahiro Shimizu */ 138863d08ecSTakahiro Shimizu static inline void pch_eth_enable_set(struct pch_dev *chip) 139863d08ecSTakahiro Shimizu { 140863d08ecSTakahiro Shimizu u32 val; 141863d08ecSTakahiro Shimizu /* SET the eth_enable bit */ 142863d08ecSTakahiro Shimizu val = ioread32(&chip->regs->ts_sel) | (PCH_ECS_ETH); 143863d08ecSTakahiro Shimizu iowrite32(val, (&chip->regs->ts_sel)); 144863d08ecSTakahiro Shimizu } 145863d08ecSTakahiro Shimizu 1467d3ac5c7SSahara static u64 pch_systime_read(struct pch_ts_regs __iomem *regs) 147863d08ecSTakahiro Shimizu { 148863d08ecSTakahiro Shimizu u64 ns; 149863d08ecSTakahiro Shimizu 1508664d49aSAndy Shevchenko ns = ioread64_lo_hi(®s->systime_lo); 151863d08ecSTakahiro Shimizu 1528664d49aSAndy Shevchenko return ns << TICKS_NS_SHIFT; 153863d08ecSTakahiro Shimizu } 154863d08ecSTakahiro Shimizu 1557d3ac5c7SSahara static void pch_systime_write(struct pch_ts_regs __iomem *regs, u64 ns) 156863d08ecSTakahiro Shimizu { 1578664d49aSAndy Shevchenko iowrite64_lo_hi(ns >> TICKS_NS_SHIFT, ®s->systime_lo); 158863d08ecSTakahiro Shimizu } 159863d08ecSTakahiro Shimizu 160863d08ecSTakahiro Shimizu static inline void pch_block_reset(struct pch_dev *chip) 161863d08ecSTakahiro Shimizu { 162863d08ecSTakahiro Shimizu u32 val; 163863d08ecSTakahiro Shimizu /* Reset Hardware Assist block */ 164863d08ecSTakahiro Shimizu val = ioread32(&chip->regs->control) | PCH_TSC_RESET; 165863d08ecSTakahiro Shimizu iowrite32(val, (&chip->regs->control)); 166863d08ecSTakahiro Shimizu val = val & ~PCH_TSC_RESET; 167863d08ecSTakahiro Shimizu iowrite32(val, (&chip->regs->control)); 168863d08ecSTakahiro Shimizu } 169863d08ecSTakahiro Shimizu 170863d08ecSTakahiro Shimizu void pch_ch_control_write(struct pci_dev *pdev, u32 val) 171863d08ecSTakahiro Shimizu { 172863d08ecSTakahiro Shimizu struct pch_dev *chip = pci_get_drvdata(pdev); 173863d08ecSTakahiro Shimizu 174863d08ecSTakahiro Shimizu iowrite32(val, (&chip->regs->ch_control)); 175863d08ecSTakahiro Shimizu } 176863d08ecSTakahiro Shimizu EXPORT_SYMBOL(pch_ch_control_write); 177863d08ecSTakahiro Shimizu 178863d08ecSTakahiro Shimizu u32 pch_ch_event_read(struct pci_dev *pdev) 179863d08ecSTakahiro Shimizu { 180863d08ecSTakahiro Shimizu struct pch_dev *chip = pci_get_drvdata(pdev); 181863d08ecSTakahiro Shimizu u32 val; 182863d08ecSTakahiro Shimizu 183863d08ecSTakahiro Shimizu val = ioread32(&chip->regs->ch_event); 184863d08ecSTakahiro Shimizu 185863d08ecSTakahiro Shimizu return val; 186863d08ecSTakahiro Shimizu } 187863d08ecSTakahiro Shimizu EXPORT_SYMBOL(pch_ch_event_read); 188863d08ecSTakahiro Shimizu 189863d08ecSTakahiro Shimizu void pch_ch_event_write(struct pci_dev *pdev, u32 val) 190863d08ecSTakahiro Shimizu { 191863d08ecSTakahiro Shimizu struct pch_dev *chip = pci_get_drvdata(pdev); 192863d08ecSTakahiro Shimizu 193863d08ecSTakahiro Shimizu iowrite32(val, (&chip->regs->ch_event)); 194863d08ecSTakahiro Shimizu } 195863d08ecSTakahiro Shimizu EXPORT_SYMBOL(pch_ch_event_write); 196863d08ecSTakahiro Shimizu 197863d08ecSTakahiro Shimizu u32 pch_src_uuid_lo_read(struct pci_dev *pdev) 198863d08ecSTakahiro Shimizu { 199863d08ecSTakahiro Shimizu struct pch_dev *chip = pci_get_drvdata(pdev); 200863d08ecSTakahiro Shimizu u32 val; 201863d08ecSTakahiro Shimizu 202863d08ecSTakahiro Shimizu val = ioread32(&chip->regs->src_uuid_lo); 203863d08ecSTakahiro Shimizu 204863d08ecSTakahiro Shimizu return val; 205863d08ecSTakahiro Shimizu } 206863d08ecSTakahiro Shimizu EXPORT_SYMBOL(pch_src_uuid_lo_read); 207863d08ecSTakahiro Shimizu 208863d08ecSTakahiro Shimizu u32 pch_src_uuid_hi_read(struct pci_dev *pdev) 209863d08ecSTakahiro Shimizu { 210863d08ecSTakahiro Shimizu struct pch_dev *chip = pci_get_drvdata(pdev); 211863d08ecSTakahiro Shimizu u32 val; 212863d08ecSTakahiro Shimizu 213863d08ecSTakahiro Shimizu val = ioread32(&chip->regs->src_uuid_hi); 214863d08ecSTakahiro Shimizu 215863d08ecSTakahiro Shimizu return val; 216863d08ecSTakahiro Shimizu } 217863d08ecSTakahiro Shimizu EXPORT_SYMBOL(pch_src_uuid_hi_read); 218863d08ecSTakahiro Shimizu 219863d08ecSTakahiro Shimizu u64 pch_rx_snap_read(struct pci_dev *pdev) 220863d08ecSTakahiro Shimizu { 221863d08ecSTakahiro Shimizu struct pch_dev *chip = pci_get_drvdata(pdev); 222863d08ecSTakahiro Shimizu u64 ns; 223863d08ecSTakahiro Shimizu 2248664d49aSAndy Shevchenko ns = ioread64_lo_hi(&chip->regs->rx_snap_lo); 225863d08ecSTakahiro Shimizu 2268664d49aSAndy Shevchenko return ns << TICKS_NS_SHIFT; 227863d08ecSTakahiro Shimizu } 228863d08ecSTakahiro Shimizu EXPORT_SYMBOL(pch_rx_snap_read); 229863d08ecSTakahiro Shimizu 230863d08ecSTakahiro Shimizu u64 pch_tx_snap_read(struct pci_dev *pdev) 231863d08ecSTakahiro Shimizu { 232863d08ecSTakahiro Shimizu struct pch_dev *chip = pci_get_drvdata(pdev); 233863d08ecSTakahiro Shimizu u64 ns; 234863d08ecSTakahiro Shimizu 2358664d49aSAndy Shevchenko ns = ioread64_lo_hi(&chip->regs->tx_snap_lo); 236863d08ecSTakahiro Shimizu 2378664d49aSAndy Shevchenko return ns << TICKS_NS_SHIFT; 238863d08ecSTakahiro Shimizu } 239863d08ecSTakahiro Shimizu EXPORT_SYMBOL(pch_tx_snap_read); 240863d08ecSTakahiro Shimizu 241863d08ecSTakahiro Shimizu /* This function enables all 64 bits in system time registers [high & low]. 242863d08ecSTakahiro Shimizu This is a work-around for non continuous value in the SystemTime Register*/ 243863d08ecSTakahiro Shimizu static void pch_set_system_time_count(struct pch_dev *chip) 244863d08ecSTakahiro Shimizu { 245863d08ecSTakahiro Shimizu iowrite32(0x01, &chip->regs->stl_max_set_en); 246863d08ecSTakahiro Shimizu iowrite32(0xFFFFFFFF, &chip->regs->stl_max_set); 247863d08ecSTakahiro Shimizu iowrite32(0x00, &chip->regs->stl_max_set_en); 248863d08ecSTakahiro Shimizu } 249863d08ecSTakahiro Shimizu 250863d08ecSTakahiro Shimizu static void pch_reset(struct pch_dev *chip) 251863d08ecSTakahiro Shimizu { 252863d08ecSTakahiro Shimizu /* Reset Hardware Assist */ 253863d08ecSTakahiro Shimizu pch_block_reset(chip); 254863d08ecSTakahiro Shimizu 255863d08ecSTakahiro Shimizu /* enable all 32 bits in system time registers */ 256863d08ecSTakahiro Shimizu pch_set_system_time_count(chip); 257863d08ecSTakahiro Shimizu } 258863d08ecSTakahiro Shimizu 259863d08ecSTakahiro Shimizu /** 260863d08ecSTakahiro Shimizu * pch_set_station_address() - This API sets the station address used by 261863d08ecSTakahiro Shimizu * IEEE 1588 hardware when looking at PTP 262863d08ecSTakahiro Shimizu * traffic on the ethernet interface 263863d08ecSTakahiro Shimizu * @addr: dress which contain the column separated address to be used. 264287f93deSLee Jones * @pdev: PCI device. 265863d08ecSTakahiro Shimizu */ 26617cdedf3STakahiro Shimizu int pch_set_station_address(u8 *addr, struct pci_dev *pdev) 267863d08ecSTakahiro Shimizu { 268863d08ecSTakahiro Shimizu struct pch_dev *chip = pci_get_drvdata(pdev); 2694e76b5c1SAndy Shevchenko bool valid; 2704e76b5c1SAndy Shevchenko u64 mac; 271863d08ecSTakahiro Shimizu 272863d08ecSTakahiro Shimizu /* Verify the parameter */ 2737d3ac5c7SSahara if ((chip->regs == NULL) || addr == (u8 *)NULL) { 274863d08ecSTakahiro Shimizu dev_err(&pdev->dev, 275863d08ecSTakahiro Shimizu "invalid params returning PCH_INVALIDPARAM\n"); 276863d08ecSTakahiro Shimizu return PCH_INVALIDPARAM; 277863d08ecSTakahiro Shimizu } 278863d08ecSTakahiro Shimizu 2794e76b5c1SAndy Shevchenko valid = mac_pton(addr, (u8 *)&mac); 2804e76b5c1SAndy Shevchenko if (!valid) { 2814e76b5c1SAndy Shevchenko dev_err(&pdev->dev, "invalid params returning PCH_INVALIDPARAM\n"); 282863d08ecSTakahiro Shimizu return PCH_INVALIDPARAM; 283863d08ecSTakahiro Shimizu } 284863d08ecSTakahiro Shimizu 285863d08ecSTakahiro Shimizu dev_dbg(&pdev->dev, "invoking pch_station_set\n"); 2868664d49aSAndy Shevchenko iowrite64_lo_hi(mac, &chip->regs->ts_st); 287863d08ecSTakahiro Shimizu return 0; 288863d08ecSTakahiro Shimizu } 28917cdedf3STakahiro Shimizu EXPORT_SYMBOL(pch_set_station_address); 290863d08ecSTakahiro Shimizu 291863d08ecSTakahiro Shimizu /* 292863d08ecSTakahiro Shimizu * Interrupt service routine 293863d08ecSTakahiro Shimizu */ 294863d08ecSTakahiro Shimizu static irqreturn_t isr(int irq, void *priv) 295863d08ecSTakahiro Shimizu { 296863d08ecSTakahiro Shimizu struct pch_dev *pch_dev = priv; 2977d3ac5c7SSahara struct pch_ts_regs __iomem *regs = pch_dev->regs; 298863d08ecSTakahiro Shimizu struct ptp_clock_event event; 299d09adf61SAndy Shevchenko u32 ack = 0, val; 300863d08ecSTakahiro Shimizu 301863d08ecSTakahiro Shimizu val = ioread32(®s->event); 302863d08ecSTakahiro Shimizu 303863d08ecSTakahiro Shimizu if (val & PCH_TSE_SNS) { 304863d08ecSTakahiro Shimizu ack |= PCH_TSE_SNS; 305863d08ecSTakahiro Shimizu if (pch_dev->exts0_enabled) { 306863d08ecSTakahiro Shimizu event.type = PTP_CLOCK_EXTTS; 307863d08ecSTakahiro Shimizu event.index = 0; 308d09adf61SAndy Shevchenko event.timestamp = ioread64_hi_lo(®s->asms_hi); 309863d08ecSTakahiro Shimizu event.timestamp <<= TICKS_NS_SHIFT; 310863d08ecSTakahiro Shimizu ptp_clock_event(pch_dev->ptp_clock, &event); 311863d08ecSTakahiro Shimizu } 312863d08ecSTakahiro Shimizu } 313863d08ecSTakahiro Shimizu 314863d08ecSTakahiro Shimizu if (val & PCH_TSE_SNM) { 315863d08ecSTakahiro Shimizu ack |= PCH_TSE_SNM; 316863d08ecSTakahiro Shimizu if (pch_dev->exts1_enabled) { 317863d08ecSTakahiro Shimizu event.type = PTP_CLOCK_EXTTS; 318863d08ecSTakahiro Shimizu event.index = 1; 319d09adf61SAndy Shevchenko event.timestamp = ioread64_hi_lo(®s->asms_hi); 320863d08ecSTakahiro Shimizu event.timestamp <<= TICKS_NS_SHIFT; 321863d08ecSTakahiro Shimizu ptp_clock_event(pch_dev->ptp_clock, &event); 322863d08ecSTakahiro Shimizu } 323863d08ecSTakahiro Shimizu } 324863d08ecSTakahiro Shimizu 325863d08ecSTakahiro Shimizu if (val & PCH_TSE_TTIPEND) 326863d08ecSTakahiro Shimizu ack |= PCH_TSE_TTIPEND; /* this bit seems to be always set */ 327863d08ecSTakahiro Shimizu 328863d08ecSTakahiro Shimizu if (ack) { 329863d08ecSTakahiro Shimizu iowrite32(ack, ®s->event); 330863d08ecSTakahiro Shimizu return IRQ_HANDLED; 331863d08ecSTakahiro Shimizu } else 332863d08ecSTakahiro Shimizu return IRQ_NONE; 333863d08ecSTakahiro Shimizu } 334863d08ecSTakahiro Shimizu 335863d08ecSTakahiro Shimizu /* 336863d08ecSTakahiro Shimizu * PTP clock operations 337863d08ecSTakahiro Shimizu */ 338863d08ecSTakahiro Shimizu 339863d08ecSTakahiro Shimizu static int ptp_pch_adjfreq(struct ptp_clock_info *ptp, s32 ppb) 340863d08ecSTakahiro Shimizu { 341863d08ecSTakahiro Shimizu u64 adj; 342863d08ecSTakahiro Shimizu u32 diff, addend; 343863d08ecSTakahiro Shimizu int neg_adj = 0; 344863d08ecSTakahiro Shimizu struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps); 3457d3ac5c7SSahara struct pch_ts_regs __iomem *regs = pch_dev->regs; 346863d08ecSTakahiro Shimizu 347863d08ecSTakahiro Shimizu if (ppb < 0) { 348863d08ecSTakahiro Shimizu neg_adj = 1; 349863d08ecSTakahiro Shimizu ppb = -ppb; 350863d08ecSTakahiro Shimizu } 351863d08ecSTakahiro Shimizu addend = DEFAULT_ADDEND; 352863d08ecSTakahiro Shimizu adj = addend; 353863d08ecSTakahiro Shimizu adj *= ppb; 354863d08ecSTakahiro Shimizu diff = div_u64(adj, 1000000000ULL); 355863d08ecSTakahiro Shimizu 356863d08ecSTakahiro Shimizu addend = neg_adj ? addend - diff : addend + diff; 357863d08ecSTakahiro Shimizu 358863d08ecSTakahiro Shimizu iowrite32(addend, ®s->addend); 359863d08ecSTakahiro Shimizu 360863d08ecSTakahiro Shimizu return 0; 361863d08ecSTakahiro Shimizu } 362863d08ecSTakahiro Shimizu 363863d08ecSTakahiro Shimizu static int ptp_pch_adjtime(struct ptp_clock_info *ptp, s64 delta) 364863d08ecSTakahiro Shimizu { 365863d08ecSTakahiro Shimizu s64 now; 366863d08ecSTakahiro Shimizu unsigned long flags; 367863d08ecSTakahiro Shimizu struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps); 3687d3ac5c7SSahara struct pch_ts_regs __iomem *regs = pch_dev->regs; 369863d08ecSTakahiro Shimizu 370863d08ecSTakahiro Shimizu spin_lock_irqsave(&pch_dev->register_lock, flags); 371863d08ecSTakahiro Shimizu now = pch_systime_read(regs); 372863d08ecSTakahiro Shimizu now += delta; 373863d08ecSTakahiro Shimizu pch_systime_write(regs, now); 374863d08ecSTakahiro Shimizu spin_unlock_irqrestore(&pch_dev->register_lock, flags); 375863d08ecSTakahiro Shimizu 376863d08ecSTakahiro Shimizu return 0; 377863d08ecSTakahiro Shimizu } 378863d08ecSTakahiro Shimizu 379a043a729SRichard Cochran static int ptp_pch_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts) 380863d08ecSTakahiro Shimizu { 381863d08ecSTakahiro Shimizu u64 ns; 382863d08ecSTakahiro Shimizu unsigned long flags; 383863d08ecSTakahiro Shimizu struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps); 3847d3ac5c7SSahara struct pch_ts_regs __iomem *regs = pch_dev->regs; 385863d08ecSTakahiro Shimizu 386863d08ecSTakahiro Shimizu spin_lock_irqsave(&pch_dev->register_lock, flags); 387863d08ecSTakahiro Shimizu ns = pch_systime_read(regs); 388863d08ecSTakahiro Shimizu spin_unlock_irqrestore(&pch_dev->register_lock, flags); 389863d08ecSTakahiro Shimizu 39080e95f47SYueHaibing *ts = ns_to_timespec64(ns); 391863d08ecSTakahiro Shimizu return 0; 392863d08ecSTakahiro Shimizu } 393863d08ecSTakahiro Shimizu 394863d08ecSTakahiro Shimizu static int ptp_pch_settime(struct ptp_clock_info *ptp, 395a043a729SRichard Cochran const struct timespec64 *ts) 396863d08ecSTakahiro Shimizu { 397863d08ecSTakahiro Shimizu u64 ns; 398863d08ecSTakahiro Shimizu unsigned long flags; 399863d08ecSTakahiro Shimizu struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps); 4007d3ac5c7SSahara struct pch_ts_regs __iomem *regs = pch_dev->regs; 401863d08ecSTakahiro Shimizu 40280e95f47SYueHaibing ns = timespec64_to_ns(ts); 403863d08ecSTakahiro Shimizu 404863d08ecSTakahiro Shimizu spin_lock_irqsave(&pch_dev->register_lock, flags); 405863d08ecSTakahiro Shimizu pch_systime_write(regs, ns); 406863d08ecSTakahiro Shimizu spin_unlock_irqrestore(&pch_dev->register_lock, flags); 407863d08ecSTakahiro Shimizu 408863d08ecSTakahiro Shimizu return 0; 409863d08ecSTakahiro Shimizu } 410863d08ecSTakahiro Shimizu 411863d08ecSTakahiro Shimizu static int ptp_pch_enable(struct ptp_clock_info *ptp, 412863d08ecSTakahiro Shimizu struct ptp_clock_request *rq, int on) 413863d08ecSTakahiro Shimizu { 414863d08ecSTakahiro Shimizu struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps); 415863d08ecSTakahiro Shimizu 416863d08ecSTakahiro Shimizu switch (rq->type) { 417863d08ecSTakahiro Shimizu case PTP_CLK_REQ_EXTTS: 418863d08ecSTakahiro Shimizu switch (rq->extts.index) { 419863d08ecSTakahiro Shimizu case 0: 420863d08ecSTakahiro Shimizu pch_dev->exts0_enabled = on ? 1 : 0; 421863d08ecSTakahiro Shimizu break; 422863d08ecSTakahiro Shimizu case 1: 423863d08ecSTakahiro Shimizu pch_dev->exts1_enabled = on ? 1 : 0; 424863d08ecSTakahiro Shimizu break; 425863d08ecSTakahiro Shimizu default: 426863d08ecSTakahiro Shimizu return -EINVAL; 427863d08ecSTakahiro Shimizu } 428863d08ecSTakahiro Shimizu return 0; 429863d08ecSTakahiro Shimizu default: 430863d08ecSTakahiro Shimizu break; 431863d08ecSTakahiro Shimizu } 432863d08ecSTakahiro Shimizu 433863d08ecSTakahiro Shimizu return -EOPNOTSUPP; 434863d08ecSTakahiro Shimizu } 435863d08ecSTakahiro Shimizu 4367d47e9a2SBhumika Goyal static const struct ptp_clock_info ptp_pch_caps = { 437863d08ecSTakahiro Shimizu .owner = THIS_MODULE, 438863d08ecSTakahiro Shimizu .name = "PCH timer", 439863d08ecSTakahiro Shimizu .max_adj = 50000000, 440863d08ecSTakahiro Shimizu .n_ext_ts = N_EXT_TS, 4414986b4f0SRichard Cochran .n_pins = 0, 442863d08ecSTakahiro Shimizu .pps = 0, 443863d08ecSTakahiro Shimizu .adjfreq = ptp_pch_adjfreq, 444863d08ecSTakahiro Shimizu .adjtime = ptp_pch_adjtime, 445a043a729SRichard Cochran .gettime64 = ptp_pch_gettime, 446a043a729SRichard Cochran .settime64 = ptp_pch_settime, 447863d08ecSTakahiro Shimizu .enable = ptp_pch_enable, 448863d08ecSTakahiro Shimizu }; 449863d08ecSTakahiro Shimizu 450863d08ecSTakahiro Shimizu #define pch_suspend NULL 451863d08ecSTakahiro Shimizu #define pch_resume NULL 452863d08ecSTakahiro Shimizu 453b1f7c8ccSBill Pemberton static void pch_remove(struct pci_dev *pdev) 454863d08ecSTakahiro Shimizu { 455863d08ecSTakahiro Shimizu struct pch_dev *chip = pci_get_drvdata(pdev); 456863d08ecSTakahiro Shimizu 457863d08ecSTakahiro Shimizu free_irq(pdev->irq, chip); 458*874f50c8SAndy Shevchenko ptp_clock_unregister(chip->ptp_clock); 459863d08ecSTakahiro Shimizu } 460863d08ecSTakahiro Shimizu 4615c0a4256SBill Pemberton static s32 462863d08ecSTakahiro Shimizu pch_probe(struct pci_dev *pdev, const struct pci_device_id *id) 463863d08ecSTakahiro Shimizu { 464863d08ecSTakahiro Shimizu s32 ret; 465863d08ecSTakahiro Shimizu unsigned long flags; 466863d08ecSTakahiro Shimizu struct pch_dev *chip; 467863d08ecSTakahiro Shimizu 468*874f50c8SAndy Shevchenko chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); 469863d08ecSTakahiro Shimizu if (chip == NULL) 470863d08ecSTakahiro Shimizu return -ENOMEM; 471863d08ecSTakahiro Shimizu 472863d08ecSTakahiro Shimizu /* enable the 1588 pci device */ 473*874f50c8SAndy Shevchenko ret = pcim_enable_device(pdev); 474863d08ecSTakahiro Shimizu if (ret != 0) { 475863d08ecSTakahiro Shimizu dev_err(&pdev->dev, "could not enable the pci device\n"); 476*874f50c8SAndy Shevchenko return ret; 477863d08ecSTakahiro Shimizu } 478863d08ecSTakahiro Shimizu 479*874f50c8SAndy Shevchenko ret = pcim_iomap_regions(pdev, BIT(IO_MEM_BAR), "1588_regs"); 480*874f50c8SAndy Shevchenko if (ret) { 481863d08ecSTakahiro Shimizu dev_err(&pdev->dev, "could not locate IO memory address\n"); 482*874f50c8SAndy Shevchenko return ret; 483863d08ecSTakahiro Shimizu } 484863d08ecSTakahiro Shimizu 485863d08ecSTakahiro Shimizu /* get the virtual address to the 1588 registers */ 486*874f50c8SAndy Shevchenko chip->regs = pcim_iomap_table(pdev)[IO_MEM_BAR]; 487863d08ecSTakahiro Shimizu chip->caps = ptp_pch_caps; 4881ef76158SRichard Cochran chip->ptp_clock = ptp_clock_register(&chip->caps, &pdev->dev); 489*874f50c8SAndy Shevchenko if (IS_ERR(chip->ptp_clock)) 490*874f50c8SAndy Shevchenko return PTR_ERR(chip->ptp_clock); 491863d08ecSTakahiro Shimizu 492863d08ecSTakahiro Shimizu spin_lock_init(&chip->register_lock); 493863d08ecSTakahiro Shimizu 494863d08ecSTakahiro Shimizu ret = request_irq(pdev->irq, &isr, IRQF_SHARED, KBUILD_MODNAME, chip); 495863d08ecSTakahiro Shimizu if (ret != 0) { 496863d08ecSTakahiro Shimizu dev_err(&pdev->dev, "failed to get irq %d\n", pdev->irq); 497863d08ecSTakahiro Shimizu goto err_req_irq; 498863d08ecSTakahiro Shimizu } 499863d08ecSTakahiro Shimizu 500863d08ecSTakahiro Shimizu /* indicate success */ 501863d08ecSTakahiro Shimizu chip->irq = pdev->irq; 502863d08ecSTakahiro Shimizu chip->pdev = pdev; 503863d08ecSTakahiro Shimizu pci_set_drvdata(pdev, chip); 504863d08ecSTakahiro Shimizu 505863d08ecSTakahiro Shimizu spin_lock_irqsave(&chip->register_lock, flags); 506863d08ecSTakahiro Shimizu /* reset the ieee1588 h/w */ 507863d08ecSTakahiro Shimizu pch_reset(chip); 508863d08ecSTakahiro Shimizu 509863d08ecSTakahiro Shimizu iowrite32(DEFAULT_ADDEND, &chip->regs->addend); 5108664d49aSAndy Shevchenko iowrite64_lo_hi(1, &chip->regs->trgt_lo); 511863d08ecSTakahiro Shimizu iowrite32(PCH_TSE_TTIPEND, &chip->regs->event); 512863d08ecSTakahiro Shimizu 513863d08ecSTakahiro Shimizu pch_eth_enable_set(chip); 514863d08ecSTakahiro Shimizu 515863d08ecSTakahiro Shimizu if (strcmp(pch_param.station, "00:00:00:00:00:00") != 0) { 516863d08ecSTakahiro Shimizu if (pch_set_station_address(pch_param.station, pdev) != 0) { 517863d08ecSTakahiro Shimizu dev_err(&pdev->dev, 518863d08ecSTakahiro Shimizu "Invalid station address parameter\n" 519863d08ecSTakahiro Shimizu "Module loaded but station address not set correctly\n" 520863d08ecSTakahiro Shimizu ); 521863d08ecSTakahiro Shimizu } 522863d08ecSTakahiro Shimizu } 523863d08ecSTakahiro Shimizu spin_unlock_irqrestore(&chip->register_lock, flags); 524863d08ecSTakahiro Shimizu return 0; 525863d08ecSTakahiro Shimizu 526863d08ecSTakahiro Shimizu err_req_irq: 527863d08ecSTakahiro Shimizu ptp_clock_unregister(chip->ptp_clock); 528863d08ecSTakahiro Shimizu 529863d08ecSTakahiro Shimizu dev_err(&pdev->dev, "probe failed(ret=0x%x)\n", ret); 530863d08ecSTakahiro Shimizu 531863d08ecSTakahiro Shimizu return ret; 532863d08ecSTakahiro Shimizu } 533863d08ecSTakahiro Shimizu 5349baa3c34SBenoit Taine static const struct pci_device_id pch_ieee1588_pcidev_id[] = { 535863d08ecSTakahiro Shimizu { 536863d08ecSTakahiro Shimizu .vendor = PCI_VENDOR_ID_INTEL, 537863d08ecSTakahiro Shimizu .device = PCI_DEVICE_ID_PCH_1588 538863d08ecSTakahiro Shimizu }, 539863d08ecSTakahiro Shimizu {0} 540863d08ecSTakahiro Shimizu }; 5417cd8b154SAndy Shevchenko MODULE_DEVICE_TABLE(pci, pch_ieee1588_pcidev_id); 542863d08ecSTakahiro Shimizu 5434b88b9ceSVaibhav Gupta static SIMPLE_DEV_PM_OPS(pch_pm_ops, pch_suspend, pch_resume); 5444b88b9ceSVaibhav Gupta 545d8d78949SDavid S. Miller static struct pci_driver pch_driver = { 546863d08ecSTakahiro Shimizu .name = KBUILD_MODNAME, 547863d08ecSTakahiro Shimizu .id_table = pch_ieee1588_pcidev_id, 548863d08ecSTakahiro Shimizu .probe = pch_probe, 549863d08ecSTakahiro Shimizu .remove = pch_remove, 5504b88b9ceSVaibhav Gupta .driver.pm = &pch_pm_ops, 551863d08ecSTakahiro Shimizu }; 5523fa66d3dSAndy Shevchenko module_pci_driver(pch_driver); 553863d08ecSTakahiro Shimizu 5547d3ac5c7SSahara module_param_string(station, 5557d3ac5c7SSahara pch_param.station, sizeof(pch_param.station), 0444); 556863d08ecSTakahiro Shimizu MODULE_PARM_DESC(station, 55755c31b5bSJiri Benc "IEEE 1588 station address to use - colon separated hex values"); 558863d08ecSTakahiro Shimizu 559863d08ecSTakahiro Shimizu MODULE_AUTHOR("LAPIS SEMICONDUCTOR, <tshimizu818@gmail.com>"); 560863d08ecSTakahiro Shimizu MODULE_DESCRIPTION("PTP clock using the EG20T timer"); 561863d08ecSTakahiro Shimizu MODULE_LICENSE("GPL"); 562