1 // SPDX-License-Identifier: GPL-2.0-only
3 * PTP 1588 clock using the EG20T PCH
5 * Copyright (C) 2010 OMICRON electronics GmbH
6 * Copyright (C) 2011-2012 LAPIS SEMICONDUCTOR Co., LTD.
8 * This code was derived from the IXP46X driver.
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/pci.h>
20 #include <linux/ptp_clock_kernel.h>
21 #include <linux/slab.h>
23 #define STATION_ADDR_LEN 20
24 #define PCI_DEVICE_ID_PCH_1588 0x8819
27 #define DEFAULT_ADDEND 0xA0000000
28 #define TICKS_NS_SHIFT 5
35 PCH_INTERRUPTMODEINUSE,
40 * struct pch_ts_regs - IEEE 1588 registers
79 #define PCH_TSC_RESET (1 << 0)
80 #define PCH_TSC_TTM_MASK (1 << 1)
81 #define PCH_TSC_ASMS_MASK (1 << 2)
82 #define PCH_TSC_AMMS_MASK (1 << 3)
83 #define PCH_TSC_PPSM_MASK (1 << 4)
84 #define PCH_TSE_TTIPEND (1 << 1)
85 #define PCH_TSE_SNS (1 << 2)
86 #define PCH_TSE_SNM (1 << 3)
87 #define PCH_TSE_PPS (1 << 4)
88 #define PCH_CC_MM (1 << 0)
89 #define PCH_CC_TA (1 << 1)
91 #define PCH_CC_MODE_SHIFT 16
92 #define PCH_CC_MODE_MASK 0x001F0000
93 #define PCH_CC_VERSION (1 << 31)
94 #define PCH_CE_TXS (1 << 0)
95 #define PCH_CE_RXS (1 << 1)
96 #define PCH_CE_OVR (1 << 0)
97 #define PCH_CE_VAL (1 << 1)
98 #define PCH_ECS_ETH (1 << 0)
100 #define PCH_ECS_CAN (1 << 1)
101 #define PCH_STATION_BYTES 6
103 #define PCH_IEEE1588_ETH (1 << 0)
104 #define PCH_IEEE1588_CAN (1 << 1)
106 * struct pch_dev - Driver private data
109 struct pch_ts_regs __iomem *regs;
110 struct ptp_clock *ptp_clock;
111 struct ptp_clock_info caps;
118 struct pci_dev *pdev;
119 spinlock_t register_lock;
123 * struct pch_params - 1588 module parameter
126 u8 station[STATION_ADDR_LEN];
129 /* structure to hold the module parameters */
130 static struct pch_params pch_param = {
135 * Register access functions
137 static inline void pch_eth_enable_set(struct pch_dev *chip)
140 /* SET the eth_enable bit */
141 val = ioread32(&chip->regs->ts_sel) | (PCH_ECS_ETH);
142 iowrite32(val, (&chip->regs->ts_sel));
145 static u64 pch_systime_read(struct pch_ts_regs __iomem *regs)
150 lo = ioread32(®s->systime_lo);
151 hi = ioread32(®s->systime_hi);
153 ns = ((u64) hi) << 32;
155 ns <<= TICKS_NS_SHIFT;
160 static void pch_systime_write(struct pch_ts_regs __iomem *regs, u64 ns)
164 ns >>= TICKS_NS_SHIFT;
166 lo = ns & 0xffffffff;
168 iowrite32(lo, ®s->systime_lo);
169 iowrite32(hi, ®s->systime_hi);
172 static inline void pch_block_reset(struct pch_dev *chip)
175 /* Reset Hardware Assist block */
176 val = ioread32(&chip->regs->control) | PCH_TSC_RESET;
177 iowrite32(val, (&chip->regs->control));
178 val = val & ~PCH_TSC_RESET;
179 iowrite32(val, (&chip->regs->control));
182 u32 pch_ch_control_read(struct pci_dev *pdev)
184 struct pch_dev *chip = pci_get_drvdata(pdev);
187 val = ioread32(&chip->regs->ch_control);
191 EXPORT_SYMBOL(pch_ch_control_read);
193 void pch_ch_control_write(struct pci_dev *pdev, u32 val)
195 struct pch_dev *chip = pci_get_drvdata(pdev);
197 iowrite32(val, (&chip->regs->ch_control));
199 EXPORT_SYMBOL(pch_ch_control_write);
201 u32 pch_ch_event_read(struct pci_dev *pdev)
203 struct pch_dev *chip = pci_get_drvdata(pdev);
206 val = ioread32(&chip->regs->ch_event);
210 EXPORT_SYMBOL(pch_ch_event_read);
212 void pch_ch_event_write(struct pci_dev *pdev, u32 val)
214 struct pch_dev *chip = pci_get_drvdata(pdev);
216 iowrite32(val, (&chip->regs->ch_event));
218 EXPORT_SYMBOL(pch_ch_event_write);
220 u32 pch_src_uuid_lo_read(struct pci_dev *pdev)
222 struct pch_dev *chip = pci_get_drvdata(pdev);
225 val = ioread32(&chip->regs->src_uuid_lo);
229 EXPORT_SYMBOL(pch_src_uuid_lo_read);
231 u32 pch_src_uuid_hi_read(struct pci_dev *pdev)
233 struct pch_dev *chip = pci_get_drvdata(pdev);
236 val = ioread32(&chip->regs->src_uuid_hi);
240 EXPORT_SYMBOL(pch_src_uuid_hi_read);
242 u64 pch_rx_snap_read(struct pci_dev *pdev)
244 struct pch_dev *chip = pci_get_drvdata(pdev);
248 lo = ioread32(&chip->regs->rx_snap_lo);
249 hi = ioread32(&chip->regs->rx_snap_hi);
251 ns = ((u64) hi) << 32;
253 ns <<= TICKS_NS_SHIFT;
257 EXPORT_SYMBOL(pch_rx_snap_read);
259 u64 pch_tx_snap_read(struct pci_dev *pdev)
261 struct pch_dev *chip = pci_get_drvdata(pdev);
265 lo = ioread32(&chip->regs->tx_snap_lo);
266 hi = ioread32(&chip->regs->tx_snap_hi);
268 ns = ((u64) hi) << 32;
270 ns <<= TICKS_NS_SHIFT;
274 EXPORT_SYMBOL(pch_tx_snap_read);
276 /* This function enables all 64 bits in system time registers [high & low].
277 This is a work-around for non continuous value in the SystemTime Register*/
278 static void pch_set_system_time_count(struct pch_dev *chip)
280 iowrite32(0x01, &chip->regs->stl_max_set_en);
281 iowrite32(0xFFFFFFFF, &chip->regs->stl_max_set);
282 iowrite32(0x00, &chip->regs->stl_max_set_en);
285 static void pch_reset(struct pch_dev *chip)
287 /* Reset Hardware Assist */
288 pch_block_reset(chip);
290 /* enable all 32 bits in system time registers */
291 pch_set_system_time_count(chip);
295 * pch_set_station_address() - This API sets the station address used by
296 * IEEE 1588 hardware when looking at PTP
297 * traffic on the ethernet interface
298 * @addr: dress which contain the column separated address to be used.
300 int pch_set_station_address(u8 *addr, struct pci_dev *pdev)
303 struct pch_dev *chip = pci_get_drvdata(pdev);
305 /* Verify the parameter */
306 if ((chip->regs == NULL) || addr == (u8 *)NULL) {
308 "invalid params returning PCH_INVALIDPARAM\n");
309 return PCH_INVALIDPARAM;
311 /* For all station address bytes */
312 for (i = 0; i < PCH_STATION_BYTES; i++) {
316 tmp = hex_to_bin(addr[i * 3]);
319 "invalid params returning PCH_INVALIDPARAM\n");
320 return PCH_INVALIDPARAM;
323 tmp = hex_to_bin(addr[(i * 3) + 1]);
326 "invalid params returning PCH_INVALIDPARAM\n");
327 return PCH_INVALIDPARAM;
330 /* Expects ':' separated addresses */
331 if ((i < 5) && (addr[(i * 3) + 2] != ':')) {
333 "invalid params returning PCH_INVALIDPARAM\n");
334 return PCH_INVALIDPARAM;
337 /* Ideally we should set the address only after validating
339 dev_dbg(&pdev->dev, "invoking pch_station_set\n");
340 iowrite32(val, &chip->regs->ts_st[i]);
344 EXPORT_SYMBOL(pch_set_station_address);
347 * Interrupt service routine
349 static irqreturn_t isr(int irq, void *priv)
351 struct pch_dev *pch_dev = priv;
352 struct pch_ts_regs __iomem *regs = pch_dev->regs;
353 struct ptp_clock_event event;
354 u32 ack = 0, lo, hi, val;
356 val = ioread32(®s->event);
358 if (val & PCH_TSE_SNS) {
360 if (pch_dev->exts0_enabled) {
361 hi = ioread32(®s->asms_hi);
362 lo = ioread32(®s->asms_lo);
363 event.type = PTP_CLOCK_EXTTS;
365 event.timestamp = ((u64) hi) << 32;
366 event.timestamp |= lo;
367 event.timestamp <<= TICKS_NS_SHIFT;
368 ptp_clock_event(pch_dev->ptp_clock, &event);
372 if (val & PCH_TSE_SNM) {
374 if (pch_dev->exts1_enabled) {
375 hi = ioread32(®s->amms_hi);
376 lo = ioread32(®s->amms_lo);
377 event.type = PTP_CLOCK_EXTTS;
379 event.timestamp = ((u64) hi) << 32;
380 event.timestamp |= lo;
381 event.timestamp <<= TICKS_NS_SHIFT;
382 ptp_clock_event(pch_dev->ptp_clock, &event);
386 if (val & PCH_TSE_TTIPEND)
387 ack |= PCH_TSE_TTIPEND; /* this bit seems to be always set */
390 iowrite32(ack, ®s->event);
397 * PTP clock operations
400 static int ptp_pch_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
405 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
406 struct pch_ts_regs __iomem *regs = pch_dev->regs;
412 addend = DEFAULT_ADDEND;
415 diff = div_u64(adj, 1000000000ULL);
417 addend = neg_adj ? addend - diff : addend + diff;
419 iowrite32(addend, ®s->addend);
424 static int ptp_pch_adjtime(struct ptp_clock_info *ptp, s64 delta)
428 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
429 struct pch_ts_regs __iomem *regs = pch_dev->regs;
431 spin_lock_irqsave(&pch_dev->register_lock, flags);
432 now = pch_systime_read(regs);
434 pch_systime_write(regs, now);
435 spin_unlock_irqrestore(&pch_dev->register_lock, flags);
440 static int ptp_pch_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
444 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
445 struct pch_ts_regs __iomem *regs = pch_dev->regs;
447 spin_lock_irqsave(&pch_dev->register_lock, flags);
448 ns = pch_systime_read(regs);
449 spin_unlock_irqrestore(&pch_dev->register_lock, flags);
451 *ts = ns_to_timespec64(ns);
455 static int ptp_pch_settime(struct ptp_clock_info *ptp,
456 const struct timespec64 *ts)
460 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
461 struct pch_ts_regs __iomem *regs = pch_dev->regs;
463 ns = timespec64_to_ns(ts);
465 spin_lock_irqsave(&pch_dev->register_lock, flags);
466 pch_systime_write(regs, ns);
467 spin_unlock_irqrestore(&pch_dev->register_lock, flags);
472 static int ptp_pch_enable(struct ptp_clock_info *ptp,
473 struct ptp_clock_request *rq, int on)
475 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
478 case PTP_CLK_REQ_EXTTS:
479 switch (rq->extts.index) {
481 pch_dev->exts0_enabled = on ? 1 : 0;
484 pch_dev->exts1_enabled = on ? 1 : 0;
497 static const struct ptp_clock_info ptp_pch_caps = {
498 .owner = THIS_MODULE,
501 .n_ext_ts = N_EXT_TS,
504 .adjfreq = ptp_pch_adjfreq,
505 .adjtime = ptp_pch_adjtime,
506 .gettime64 = ptp_pch_gettime,
507 .settime64 = ptp_pch_settime,
508 .enable = ptp_pch_enable,
513 static s32 pch_suspend(struct pci_dev *pdev, pm_message_t state)
515 pci_disable_device(pdev);
516 pci_enable_wake(pdev, PCI_D3hot, 0);
518 if (pci_save_state(pdev) != 0) {
519 dev_err(&pdev->dev, "could not save PCI config state\n");
522 pci_set_power_state(pdev, pci_choose_state(pdev, state));
527 static s32 pch_resume(struct pci_dev *pdev)
531 pci_set_power_state(pdev, PCI_D0);
532 pci_restore_state(pdev);
533 ret = pci_enable_device(pdev);
535 dev_err(&pdev->dev, "pci_enable_device failed\n");
538 pci_enable_wake(pdev, PCI_D3hot, 0);
542 #define pch_suspend NULL
543 #define pch_resume NULL
546 static void pch_remove(struct pci_dev *pdev)
548 struct pch_dev *chip = pci_get_drvdata(pdev);
550 ptp_clock_unregister(chip->ptp_clock);
551 /* free the interrupt */
553 free_irq(pdev->irq, chip);
555 /* unmap the virtual IO memory space */
556 if (chip->regs != NULL) {
560 /* release the reserved IO memory space */
561 if (chip->mem_base != 0) {
562 release_mem_region(chip->mem_base, chip->mem_size);
565 pci_disable_device(pdev);
567 dev_info(&pdev->dev, "complete\n");
571 pch_probe(struct pci_dev *pdev, const struct pci_device_id *id)
575 struct pch_dev *chip;
577 chip = kzalloc(sizeof(struct pch_dev), GFP_KERNEL);
581 /* enable the 1588 pci device */
582 ret = pci_enable_device(pdev);
584 dev_err(&pdev->dev, "could not enable the pci device\n");
588 chip->mem_base = pci_resource_start(pdev, IO_MEM_BAR);
589 if (!chip->mem_base) {
590 dev_err(&pdev->dev, "could not locate IO memory address\n");
595 /* retrieve the available length of the IO memory space */
596 chip->mem_size = pci_resource_len(pdev, IO_MEM_BAR);
598 /* allocate the memory for the device registers */
599 if (!request_mem_region(chip->mem_base, chip->mem_size, "1588_regs")) {
601 "could not allocate register memory space\n");
603 goto err_req_mem_region;
606 /* get the virtual address to the 1588 registers */
607 chip->regs = ioremap(chip->mem_base, chip->mem_size);
610 dev_err(&pdev->dev, "Could not get virtual address\n");
615 chip->caps = ptp_pch_caps;
616 chip->ptp_clock = ptp_clock_register(&chip->caps, &pdev->dev);
617 if (IS_ERR(chip->ptp_clock)) {
618 ret = PTR_ERR(chip->ptp_clock);
619 goto err_ptp_clock_reg;
622 spin_lock_init(&chip->register_lock);
624 ret = request_irq(pdev->irq, &isr, IRQF_SHARED, KBUILD_MODNAME, chip);
626 dev_err(&pdev->dev, "failed to get irq %d\n", pdev->irq);
630 /* indicate success */
631 chip->irq = pdev->irq;
633 pci_set_drvdata(pdev, chip);
635 spin_lock_irqsave(&chip->register_lock, flags);
636 /* reset the ieee1588 h/w */
639 iowrite32(DEFAULT_ADDEND, &chip->regs->addend);
640 iowrite32(1, &chip->regs->trgt_lo);
641 iowrite32(0, &chip->regs->trgt_hi);
642 iowrite32(PCH_TSE_TTIPEND, &chip->regs->event);
644 pch_eth_enable_set(chip);
646 if (strcmp(pch_param.station, "00:00:00:00:00:00") != 0) {
647 if (pch_set_station_address(pch_param.station, pdev) != 0) {
649 "Invalid station address parameter\n"
650 "Module loaded but station address not set correctly\n"
654 spin_unlock_irqrestore(&chip->register_lock, flags);
658 ptp_clock_unregister(chip->ptp_clock);
664 release_mem_region(chip->mem_base, chip->mem_size);
670 pci_disable_device(pdev);
674 dev_err(&pdev->dev, "probe failed(ret=0x%x)\n", ret);
679 static const struct pci_device_id pch_ieee1588_pcidev_id[] = {
681 .vendor = PCI_VENDOR_ID_INTEL,
682 .device = PCI_DEVICE_ID_PCH_1588
687 static struct pci_driver pch_driver = {
688 .name = KBUILD_MODNAME,
689 .id_table = pch_ieee1588_pcidev_id,
691 .remove = pch_remove,
692 .suspend = pch_suspend,
693 .resume = pch_resume,
696 static void __exit ptp_pch_exit(void)
698 pci_unregister_driver(&pch_driver);
701 static s32 __init ptp_pch_init(void)
705 /* register the driver with the pci core */
706 ret = pci_register_driver(&pch_driver);
711 module_init(ptp_pch_init);
712 module_exit(ptp_pch_exit);
714 module_param_string(station,
715 pch_param.station, sizeof(pch_param.station), 0444);
716 MODULE_PARM_DESC(station,
717 "IEEE 1588 station address to use - colon separated hex values");
720 MODULE_DESCRIPTION("PTP clock using the EG20T timer");
721 MODULE_LICENSE("GPL");