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/interrupt.h>
15 #include <linux/io-64-nonatomic-lo-hi.h>
16 #include <linux/io-64-nonatomic-hi-lo.h>
17 #include <linux/irq.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/ptp_clock_kernel.h>
22 #include <linux/ptp_pch.h>
23 #include <linux/slab.h>
25 #define STATION_ADDR_LEN 20
26 #define PCI_DEVICE_ID_PCH_1588 0x8819
29 #define DEFAULT_ADDEND 0xA0000000
30 #define TICKS_NS_SHIFT 5
37 PCH_INTERRUPTMODEINUSE,
43 * struct pch_ts_regs - IEEE 1588 registers
82 #define PCH_TSC_RESET (1 << 0)
83 #define PCH_TSC_TTM_MASK (1 << 1)
84 #define PCH_TSC_ASMS_MASK (1 << 2)
85 #define PCH_TSC_AMMS_MASK (1 << 3)
86 #define PCH_TSC_PPSM_MASK (1 << 4)
87 #define PCH_TSE_TTIPEND (1 << 1)
88 #define PCH_TSE_SNS (1 << 2)
89 #define PCH_TSE_SNM (1 << 3)
90 #define PCH_TSE_PPS (1 << 4)
91 #define PCH_CC_MM (1 << 0)
92 #define PCH_CC_TA (1 << 1)
94 #define PCH_CC_MODE_SHIFT 16
95 #define PCH_CC_MODE_MASK 0x001F0000
96 #define PCH_CC_VERSION (1 << 31)
97 #define PCH_CE_TXS (1 << 0)
98 #define PCH_CE_RXS (1 << 1)
99 #define PCH_CE_OVR (1 << 0)
100 #define PCH_CE_VAL (1 << 1)
101 #define PCH_ECS_ETH (1 << 0)
103 #define PCH_ECS_CAN (1 << 1)
105 #define PCH_IEEE1588_ETH (1 << 0)
106 #define PCH_IEEE1588_CAN (1 << 1)
109 * struct pch_dev - Driver private data
112 struct pch_ts_regs __iomem *regs;
113 struct ptp_clock *ptp_clock;
114 struct ptp_clock_info caps;
119 struct pci_dev *pdev;
120 spinlock_t register_lock;
124 * struct pch_params - 1588 module parameter
127 u8 station[STATION_ADDR_LEN];
130 /* structure to hold the module parameters */
131 static struct pch_params pch_param = {
136 * Register access functions
138 static inline void pch_eth_enable_set(struct pch_dev *chip)
141 /* SET the eth_enable bit */
142 val = ioread32(&chip->regs->ts_sel) | (PCH_ECS_ETH);
143 iowrite32(val, (&chip->regs->ts_sel));
146 static u64 pch_systime_read(struct pch_ts_regs __iomem *regs)
150 ns = ioread64_lo_hi(®s->systime_lo);
152 return ns << TICKS_NS_SHIFT;
155 static void pch_systime_write(struct pch_ts_regs __iomem *regs, u64 ns)
157 iowrite64_lo_hi(ns >> TICKS_NS_SHIFT, ®s->systime_lo);
160 static inline void pch_block_reset(struct pch_dev *chip)
163 /* Reset Hardware Assist block */
164 val = ioread32(&chip->regs->control) | PCH_TSC_RESET;
165 iowrite32(val, (&chip->regs->control));
166 val = val & ~PCH_TSC_RESET;
167 iowrite32(val, (&chip->regs->control));
170 void pch_ch_control_write(struct pci_dev *pdev, u32 val)
172 struct pch_dev *chip = pci_get_drvdata(pdev);
174 iowrite32(val, (&chip->regs->ch_control));
176 EXPORT_SYMBOL(pch_ch_control_write);
178 u32 pch_ch_event_read(struct pci_dev *pdev)
180 struct pch_dev *chip = pci_get_drvdata(pdev);
183 val = ioread32(&chip->regs->ch_event);
187 EXPORT_SYMBOL(pch_ch_event_read);
189 void pch_ch_event_write(struct pci_dev *pdev, u32 val)
191 struct pch_dev *chip = pci_get_drvdata(pdev);
193 iowrite32(val, (&chip->regs->ch_event));
195 EXPORT_SYMBOL(pch_ch_event_write);
197 u32 pch_src_uuid_lo_read(struct pci_dev *pdev)
199 struct pch_dev *chip = pci_get_drvdata(pdev);
202 val = ioread32(&chip->regs->src_uuid_lo);
206 EXPORT_SYMBOL(pch_src_uuid_lo_read);
208 u32 pch_src_uuid_hi_read(struct pci_dev *pdev)
210 struct pch_dev *chip = pci_get_drvdata(pdev);
213 val = ioread32(&chip->regs->src_uuid_hi);
217 EXPORT_SYMBOL(pch_src_uuid_hi_read);
219 u64 pch_rx_snap_read(struct pci_dev *pdev)
221 struct pch_dev *chip = pci_get_drvdata(pdev);
224 ns = ioread64_lo_hi(&chip->regs->rx_snap_lo);
226 return ns << TICKS_NS_SHIFT;
228 EXPORT_SYMBOL(pch_rx_snap_read);
230 u64 pch_tx_snap_read(struct pci_dev *pdev)
232 struct pch_dev *chip = pci_get_drvdata(pdev);
235 ns = ioread64_lo_hi(&chip->regs->tx_snap_lo);
237 return ns << TICKS_NS_SHIFT;
239 EXPORT_SYMBOL(pch_tx_snap_read);
241 /* This function enables all 64 bits in system time registers [high & low].
242 This is a work-around for non continuous value in the SystemTime Register*/
243 static void pch_set_system_time_count(struct pch_dev *chip)
245 iowrite32(0x01, &chip->regs->stl_max_set_en);
246 iowrite32(0xFFFFFFFF, &chip->regs->stl_max_set);
247 iowrite32(0x00, &chip->regs->stl_max_set_en);
250 static void pch_reset(struct pch_dev *chip)
252 /* Reset Hardware Assist */
253 pch_block_reset(chip);
255 /* enable all 32 bits in system time registers */
256 pch_set_system_time_count(chip);
260 * pch_set_station_address() - This API sets the station address used by
261 * IEEE 1588 hardware when looking at PTP
262 * traffic on the ethernet interface
263 * @addr: dress which contain the column separated address to be used.
266 int pch_set_station_address(u8 *addr, struct pci_dev *pdev)
268 struct pch_dev *chip = pci_get_drvdata(pdev);
272 /* Verify the parameter */
273 if ((chip->regs == NULL) || addr == (u8 *)NULL) {
275 "invalid params returning PCH_INVALIDPARAM\n");
276 return PCH_INVALIDPARAM;
279 valid = mac_pton(addr, (u8 *)&mac);
281 dev_err(&pdev->dev, "invalid params returning PCH_INVALIDPARAM\n");
282 return PCH_INVALIDPARAM;
285 dev_dbg(&pdev->dev, "invoking pch_station_set\n");
286 iowrite64_lo_hi(mac, &chip->regs->ts_st);
289 EXPORT_SYMBOL(pch_set_station_address);
292 * Interrupt service routine
294 static irqreturn_t isr(int irq, void *priv)
296 struct pch_dev *pch_dev = priv;
297 struct pch_ts_regs __iomem *regs = pch_dev->regs;
298 struct ptp_clock_event event;
301 val = ioread32(®s->event);
303 if (val & PCH_TSE_SNS) {
305 if (pch_dev->exts0_enabled) {
306 event.type = PTP_CLOCK_EXTTS;
308 event.timestamp = ioread64_hi_lo(®s->asms_hi);
309 event.timestamp <<= TICKS_NS_SHIFT;
310 ptp_clock_event(pch_dev->ptp_clock, &event);
314 if (val & PCH_TSE_SNM) {
316 if (pch_dev->exts1_enabled) {
317 event.type = PTP_CLOCK_EXTTS;
319 event.timestamp = ioread64_hi_lo(®s->asms_hi);
320 event.timestamp <<= TICKS_NS_SHIFT;
321 ptp_clock_event(pch_dev->ptp_clock, &event);
325 if (val & PCH_TSE_TTIPEND)
326 ack |= PCH_TSE_TTIPEND; /* this bit seems to be always set */
329 iowrite32(ack, ®s->event);
336 * PTP clock operations
339 static int ptp_pch_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
344 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
345 struct pch_ts_regs __iomem *regs = pch_dev->regs;
351 addend = DEFAULT_ADDEND;
354 diff = div_u64(adj, 1000000000ULL);
356 addend = neg_adj ? addend - diff : addend + diff;
358 iowrite32(addend, ®s->addend);
363 static int ptp_pch_adjtime(struct ptp_clock_info *ptp, s64 delta)
367 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
368 struct pch_ts_regs __iomem *regs = pch_dev->regs;
370 spin_lock_irqsave(&pch_dev->register_lock, flags);
371 now = pch_systime_read(regs);
373 pch_systime_write(regs, now);
374 spin_unlock_irqrestore(&pch_dev->register_lock, flags);
379 static int ptp_pch_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
383 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
384 struct pch_ts_regs __iomem *regs = pch_dev->regs;
386 spin_lock_irqsave(&pch_dev->register_lock, flags);
387 ns = pch_systime_read(regs);
388 spin_unlock_irqrestore(&pch_dev->register_lock, flags);
390 *ts = ns_to_timespec64(ns);
394 static int ptp_pch_settime(struct ptp_clock_info *ptp,
395 const struct timespec64 *ts)
399 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
400 struct pch_ts_regs __iomem *regs = pch_dev->regs;
402 ns = timespec64_to_ns(ts);
404 spin_lock_irqsave(&pch_dev->register_lock, flags);
405 pch_systime_write(regs, ns);
406 spin_unlock_irqrestore(&pch_dev->register_lock, flags);
411 static int ptp_pch_enable(struct ptp_clock_info *ptp,
412 struct ptp_clock_request *rq, int on)
414 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
417 case PTP_CLK_REQ_EXTTS:
418 switch (rq->extts.index) {
420 pch_dev->exts0_enabled = on ? 1 : 0;
423 pch_dev->exts1_enabled = on ? 1 : 0;
436 static const struct ptp_clock_info ptp_pch_caps = {
437 .owner = THIS_MODULE,
440 .n_ext_ts = N_EXT_TS,
443 .adjfreq = ptp_pch_adjfreq,
444 .adjtime = ptp_pch_adjtime,
445 .gettime64 = ptp_pch_gettime,
446 .settime64 = ptp_pch_settime,
447 .enable = ptp_pch_enable,
450 static void pch_remove(struct pci_dev *pdev)
452 struct pch_dev *chip = pci_get_drvdata(pdev);
454 free_irq(pdev->irq, chip);
455 ptp_clock_unregister(chip->ptp_clock);
459 pch_probe(struct pci_dev *pdev, const struct pci_device_id *id)
463 struct pch_dev *chip;
465 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
469 /* enable the 1588 pci device */
470 ret = pcim_enable_device(pdev);
472 dev_err(&pdev->dev, "could not enable the pci device\n");
476 ret = pcim_iomap_regions(pdev, BIT(IO_MEM_BAR), "1588_regs");
478 dev_err(&pdev->dev, "could not locate IO memory address\n");
482 /* get the virtual address to the 1588 registers */
483 chip->regs = pcim_iomap_table(pdev)[IO_MEM_BAR];
484 chip->caps = ptp_pch_caps;
485 chip->ptp_clock = ptp_clock_register(&chip->caps, &pdev->dev);
486 if (IS_ERR(chip->ptp_clock))
487 return PTR_ERR(chip->ptp_clock);
489 spin_lock_init(&chip->register_lock);
491 ret = request_irq(pdev->irq, &isr, IRQF_SHARED, KBUILD_MODNAME, chip);
493 dev_err(&pdev->dev, "failed to get irq %d\n", pdev->irq);
497 /* indicate success */
498 chip->irq = pdev->irq;
500 pci_set_drvdata(pdev, chip);
502 spin_lock_irqsave(&chip->register_lock, flags);
503 /* reset the ieee1588 h/w */
506 iowrite32(DEFAULT_ADDEND, &chip->regs->addend);
507 iowrite64_lo_hi(1, &chip->regs->trgt_lo);
508 iowrite32(PCH_TSE_TTIPEND, &chip->regs->event);
510 pch_eth_enable_set(chip);
512 if (strcmp(pch_param.station, "00:00:00:00:00:00") != 0) {
513 if (pch_set_station_address(pch_param.station, pdev) != 0) {
515 "Invalid station address parameter\n"
516 "Module loaded but station address not set correctly\n"
520 spin_unlock_irqrestore(&chip->register_lock, flags);
524 ptp_clock_unregister(chip->ptp_clock);
526 dev_err(&pdev->dev, "probe failed(ret=0x%x)\n", ret);
531 static const struct pci_device_id pch_ieee1588_pcidev_id[] = {
533 .vendor = PCI_VENDOR_ID_INTEL,
534 .device = PCI_DEVICE_ID_PCH_1588
538 MODULE_DEVICE_TABLE(pci, pch_ieee1588_pcidev_id);
540 static struct pci_driver pch_driver = {
541 .name = KBUILD_MODNAME,
542 .id_table = pch_ieee1588_pcidev_id,
544 .remove = pch_remove,
546 module_pci_driver(pch_driver);
548 module_param_string(station,
549 pch_param.station, sizeof(pch_param.station), 0444);
550 MODULE_PARM_DESC(station,
551 "IEEE 1588 station address to use - colon separated hex values");
553 MODULE_AUTHOR("LAPIS SEMICONDUCTOR, <tshimizu818@gmail.com>");
554 MODULE_DESCRIPTION("PTP clock using the EG20T timer");
555 MODULE_LICENSE("GPL");