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_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
342 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
343 struct pch_ts_regs __iomem *regs = pch_dev->regs;
345 addend = adjust_by_scaled_ppm(DEFAULT_ADDEND, scaled_ppm);
347 iowrite32(addend, ®s->addend);
352 static int ptp_pch_adjtime(struct ptp_clock_info *ptp, s64 delta)
356 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
357 struct pch_ts_regs __iomem *regs = pch_dev->regs;
359 spin_lock_irqsave(&pch_dev->register_lock, flags);
360 now = pch_systime_read(regs);
362 pch_systime_write(regs, now);
363 spin_unlock_irqrestore(&pch_dev->register_lock, flags);
368 static int ptp_pch_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
372 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
373 struct pch_ts_regs __iomem *regs = pch_dev->regs;
375 spin_lock_irqsave(&pch_dev->register_lock, flags);
376 ns = pch_systime_read(regs);
377 spin_unlock_irqrestore(&pch_dev->register_lock, flags);
379 *ts = ns_to_timespec64(ns);
383 static int ptp_pch_settime(struct ptp_clock_info *ptp,
384 const struct timespec64 *ts)
388 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
389 struct pch_ts_regs __iomem *regs = pch_dev->regs;
391 ns = timespec64_to_ns(ts);
393 spin_lock_irqsave(&pch_dev->register_lock, flags);
394 pch_systime_write(regs, ns);
395 spin_unlock_irqrestore(&pch_dev->register_lock, flags);
400 static int ptp_pch_enable(struct ptp_clock_info *ptp,
401 struct ptp_clock_request *rq, int on)
403 struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
406 case PTP_CLK_REQ_EXTTS:
407 switch (rq->extts.index) {
409 pch_dev->exts0_enabled = on ? 1 : 0;
412 pch_dev->exts1_enabled = on ? 1 : 0;
425 static const struct ptp_clock_info ptp_pch_caps = {
426 .owner = THIS_MODULE,
429 .n_ext_ts = N_EXT_TS,
432 .adjfine = ptp_pch_adjfine,
433 .adjtime = ptp_pch_adjtime,
434 .gettime64 = ptp_pch_gettime,
435 .settime64 = ptp_pch_settime,
436 .enable = ptp_pch_enable,
439 static void pch_remove(struct pci_dev *pdev)
441 struct pch_dev *chip = pci_get_drvdata(pdev);
443 free_irq(pdev->irq, chip);
444 ptp_clock_unregister(chip->ptp_clock);
448 pch_probe(struct pci_dev *pdev, const struct pci_device_id *id)
452 struct pch_dev *chip;
454 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
458 /* enable the 1588 pci device */
459 ret = pcim_enable_device(pdev);
461 dev_err(&pdev->dev, "could not enable the pci device\n");
465 ret = pcim_iomap_regions(pdev, BIT(IO_MEM_BAR), "1588_regs");
467 dev_err(&pdev->dev, "could not locate IO memory address\n");
471 /* get the virtual address to the 1588 registers */
472 chip->regs = pcim_iomap_table(pdev)[IO_MEM_BAR];
473 chip->caps = ptp_pch_caps;
474 chip->ptp_clock = ptp_clock_register(&chip->caps, &pdev->dev);
475 if (IS_ERR(chip->ptp_clock))
476 return PTR_ERR(chip->ptp_clock);
478 spin_lock_init(&chip->register_lock);
480 ret = request_irq(pdev->irq, &isr, IRQF_SHARED, KBUILD_MODNAME, chip);
482 dev_err(&pdev->dev, "failed to get irq %d\n", pdev->irq);
486 /* indicate success */
487 chip->irq = pdev->irq;
489 pci_set_drvdata(pdev, chip);
491 spin_lock_irqsave(&chip->register_lock, flags);
492 /* reset the ieee1588 h/w */
495 iowrite32(DEFAULT_ADDEND, &chip->regs->addend);
496 iowrite64_lo_hi(1, &chip->regs->trgt_lo);
497 iowrite32(PCH_TSE_TTIPEND, &chip->regs->event);
499 pch_eth_enable_set(chip);
501 if (strcmp(pch_param.station, "00:00:00:00:00:00") != 0) {
502 if (pch_set_station_address(pch_param.station, pdev) != 0) {
504 "Invalid station address parameter\n"
505 "Module loaded but station address not set correctly\n"
509 spin_unlock_irqrestore(&chip->register_lock, flags);
513 ptp_clock_unregister(chip->ptp_clock);
515 dev_err(&pdev->dev, "probe failed(ret=0x%x)\n", ret);
520 static const struct pci_device_id pch_ieee1588_pcidev_id[] = {
522 .vendor = PCI_VENDOR_ID_INTEL,
523 .device = PCI_DEVICE_ID_PCH_1588
527 MODULE_DEVICE_TABLE(pci, pch_ieee1588_pcidev_id);
529 static struct pci_driver pch_driver = {
530 .name = KBUILD_MODNAME,
531 .id_table = pch_ieee1588_pcidev_id,
533 .remove = pch_remove,
535 module_pci_driver(pch_driver);
537 module_param_string(station,
538 pch_param.station, sizeof(pch_param.station), 0444);
539 MODULE_PARM_DESC(station,
540 "IEEE 1588 station address to use - colon separated hex values");
542 MODULE_AUTHOR("LAPIS SEMICONDUCTOR, <tshimizu818@gmail.com>");
543 MODULE_DESCRIPTION("PTP clock using the EG20T timer");
544 MODULE_LICENSE("GPL");