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 */
pch_eth_enable_set(struct pch_dev * chip)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
pch_systime_read(struct pch_ts_regs __iomem * regs)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
pch_systime_write(struct pch_ts_regs __iomem * regs,u64 ns)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
pch_block_reset(struct pch_dev * chip)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
pch_ch_control_write(struct pci_dev * pdev,u32 val)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
pch_ch_event_read(struct pci_dev * pdev)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
pch_ch_event_write(struct pci_dev * pdev,u32 val)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
pch_src_uuid_lo_read(struct pci_dev * pdev)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
pch_src_uuid_hi_read(struct pci_dev * pdev)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
pch_rx_snap_read(struct pci_dev * pdev)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
pch_tx_snap_read(struct pci_dev * pdev)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*/
pch_set_system_time_count(struct pch_dev * chip)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
pch_reset(struct pch_dev * chip)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 */
pch_set_station_address(u8 * addr,struct pci_dev * pdev)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 */
isr(int irq,void * priv)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
ptp_pch_adjfine(struct ptp_clock_info * ptp,long scaled_ppm)339*2e77ededSJacob Keller static int ptp_pch_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
340863d08ecSTakahiro Shimizu {
341*2e77ededSJacob Keller u32 addend;
342863d08ecSTakahiro Shimizu struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
3437d3ac5c7SSahara struct pch_ts_regs __iomem *regs = pch_dev->regs;
344863d08ecSTakahiro Shimizu
345*2e77ededSJacob Keller addend = adjust_by_scaled_ppm(DEFAULT_ADDEND, scaled_ppm);
346863d08ecSTakahiro Shimizu
347863d08ecSTakahiro Shimizu iowrite32(addend, ®s->addend);
348863d08ecSTakahiro Shimizu
349863d08ecSTakahiro Shimizu return 0;
350863d08ecSTakahiro Shimizu }
351863d08ecSTakahiro Shimizu
ptp_pch_adjtime(struct ptp_clock_info * ptp,s64 delta)352863d08ecSTakahiro Shimizu static int ptp_pch_adjtime(struct ptp_clock_info *ptp, s64 delta)
353863d08ecSTakahiro Shimizu {
354863d08ecSTakahiro Shimizu s64 now;
355863d08ecSTakahiro Shimizu unsigned long flags;
356863d08ecSTakahiro Shimizu struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
3577d3ac5c7SSahara struct pch_ts_regs __iomem *regs = pch_dev->regs;
358863d08ecSTakahiro Shimizu
359863d08ecSTakahiro Shimizu spin_lock_irqsave(&pch_dev->register_lock, flags);
360863d08ecSTakahiro Shimizu now = pch_systime_read(regs);
361863d08ecSTakahiro Shimizu now += delta;
362863d08ecSTakahiro Shimizu pch_systime_write(regs, now);
363863d08ecSTakahiro Shimizu spin_unlock_irqrestore(&pch_dev->register_lock, flags);
364863d08ecSTakahiro Shimizu
365863d08ecSTakahiro Shimizu return 0;
366863d08ecSTakahiro Shimizu }
367863d08ecSTakahiro Shimizu
ptp_pch_gettime(struct ptp_clock_info * ptp,struct timespec64 * ts)368a043a729SRichard Cochran static int ptp_pch_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
369863d08ecSTakahiro Shimizu {
370863d08ecSTakahiro Shimizu u64 ns;
371863d08ecSTakahiro Shimizu unsigned long flags;
372863d08ecSTakahiro Shimizu struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
3737d3ac5c7SSahara struct pch_ts_regs __iomem *regs = pch_dev->regs;
374863d08ecSTakahiro Shimizu
375863d08ecSTakahiro Shimizu spin_lock_irqsave(&pch_dev->register_lock, flags);
376863d08ecSTakahiro Shimizu ns = pch_systime_read(regs);
377863d08ecSTakahiro Shimizu spin_unlock_irqrestore(&pch_dev->register_lock, flags);
378863d08ecSTakahiro Shimizu
37980e95f47SYueHaibing *ts = ns_to_timespec64(ns);
380863d08ecSTakahiro Shimizu return 0;
381863d08ecSTakahiro Shimizu }
382863d08ecSTakahiro Shimizu
ptp_pch_settime(struct ptp_clock_info * ptp,const struct timespec64 * ts)383863d08ecSTakahiro Shimizu static int ptp_pch_settime(struct ptp_clock_info *ptp,
384a043a729SRichard Cochran const struct timespec64 *ts)
385863d08ecSTakahiro Shimizu {
386863d08ecSTakahiro Shimizu u64 ns;
387863d08ecSTakahiro Shimizu unsigned long flags;
388863d08ecSTakahiro Shimizu struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
3897d3ac5c7SSahara struct pch_ts_regs __iomem *regs = pch_dev->regs;
390863d08ecSTakahiro Shimizu
39180e95f47SYueHaibing ns = timespec64_to_ns(ts);
392863d08ecSTakahiro Shimizu
393863d08ecSTakahiro Shimizu spin_lock_irqsave(&pch_dev->register_lock, flags);
394863d08ecSTakahiro Shimizu pch_systime_write(regs, ns);
395863d08ecSTakahiro Shimizu spin_unlock_irqrestore(&pch_dev->register_lock, flags);
396863d08ecSTakahiro Shimizu
397863d08ecSTakahiro Shimizu return 0;
398863d08ecSTakahiro Shimizu }
399863d08ecSTakahiro Shimizu
ptp_pch_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)400863d08ecSTakahiro Shimizu static int ptp_pch_enable(struct ptp_clock_info *ptp,
401863d08ecSTakahiro Shimizu struct ptp_clock_request *rq, int on)
402863d08ecSTakahiro Shimizu {
403863d08ecSTakahiro Shimizu struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
404863d08ecSTakahiro Shimizu
405863d08ecSTakahiro Shimizu switch (rq->type) {
406863d08ecSTakahiro Shimizu case PTP_CLK_REQ_EXTTS:
407863d08ecSTakahiro Shimizu switch (rq->extts.index) {
408863d08ecSTakahiro Shimizu case 0:
409863d08ecSTakahiro Shimizu pch_dev->exts0_enabled = on ? 1 : 0;
410863d08ecSTakahiro Shimizu break;
411863d08ecSTakahiro Shimizu case 1:
412863d08ecSTakahiro Shimizu pch_dev->exts1_enabled = on ? 1 : 0;
413863d08ecSTakahiro Shimizu break;
414863d08ecSTakahiro Shimizu default:
415863d08ecSTakahiro Shimizu return -EINVAL;
416863d08ecSTakahiro Shimizu }
417863d08ecSTakahiro Shimizu return 0;
418863d08ecSTakahiro Shimizu default:
419863d08ecSTakahiro Shimizu break;
420863d08ecSTakahiro Shimizu }
421863d08ecSTakahiro Shimizu
422863d08ecSTakahiro Shimizu return -EOPNOTSUPP;
423863d08ecSTakahiro Shimizu }
424863d08ecSTakahiro Shimizu
4257d47e9a2SBhumika Goyal static const struct ptp_clock_info ptp_pch_caps = {
426863d08ecSTakahiro Shimizu .owner = THIS_MODULE,
427863d08ecSTakahiro Shimizu .name = "PCH timer",
428863d08ecSTakahiro Shimizu .max_adj = 50000000,
429863d08ecSTakahiro Shimizu .n_ext_ts = N_EXT_TS,
4304986b4f0SRichard Cochran .n_pins = 0,
431863d08ecSTakahiro Shimizu .pps = 0,
432*2e77ededSJacob Keller .adjfine = ptp_pch_adjfine,
433863d08ecSTakahiro Shimizu .adjtime = ptp_pch_adjtime,
434a043a729SRichard Cochran .gettime64 = ptp_pch_gettime,
435a043a729SRichard Cochran .settime64 = ptp_pch_settime,
436863d08ecSTakahiro Shimizu .enable = ptp_pch_enable,
437863d08ecSTakahiro Shimizu };
438863d08ecSTakahiro Shimizu
pch_remove(struct pci_dev * pdev)439b1f7c8ccSBill Pemberton static void pch_remove(struct pci_dev *pdev)
440863d08ecSTakahiro Shimizu {
441863d08ecSTakahiro Shimizu struct pch_dev *chip = pci_get_drvdata(pdev);
442863d08ecSTakahiro Shimizu
443863d08ecSTakahiro Shimizu free_irq(pdev->irq, chip);
444874f50c8SAndy Shevchenko ptp_clock_unregister(chip->ptp_clock);
445863d08ecSTakahiro Shimizu }
446863d08ecSTakahiro Shimizu
4475c0a4256SBill Pemberton static s32
pch_probe(struct pci_dev * pdev,const struct pci_device_id * id)448863d08ecSTakahiro Shimizu pch_probe(struct pci_dev *pdev, const struct pci_device_id *id)
449863d08ecSTakahiro Shimizu {
450863d08ecSTakahiro Shimizu s32 ret;
451863d08ecSTakahiro Shimizu unsigned long flags;
452863d08ecSTakahiro Shimizu struct pch_dev *chip;
453863d08ecSTakahiro Shimizu
454874f50c8SAndy Shevchenko chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
455863d08ecSTakahiro Shimizu if (chip == NULL)
456863d08ecSTakahiro Shimizu return -ENOMEM;
457863d08ecSTakahiro Shimizu
458863d08ecSTakahiro Shimizu /* enable the 1588 pci device */
459874f50c8SAndy Shevchenko ret = pcim_enable_device(pdev);
460863d08ecSTakahiro Shimizu if (ret != 0) {
461863d08ecSTakahiro Shimizu dev_err(&pdev->dev, "could not enable the pci device\n");
462874f50c8SAndy Shevchenko return ret;
463863d08ecSTakahiro Shimizu }
464863d08ecSTakahiro Shimizu
465874f50c8SAndy Shevchenko ret = pcim_iomap_regions(pdev, BIT(IO_MEM_BAR), "1588_regs");
466874f50c8SAndy Shevchenko if (ret) {
467863d08ecSTakahiro Shimizu dev_err(&pdev->dev, "could not locate IO memory address\n");
468874f50c8SAndy Shevchenko return ret;
469863d08ecSTakahiro Shimizu }
470863d08ecSTakahiro Shimizu
471863d08ecSTakahiro Shimizu /* get the virtual address to the 1588 registers */
472874f50c8SAndy Shevchenko chip->regs = pcim_iomap_table(pdev)[IO_MEM_BAR];
473863d08ecSTakahiro Shimizu chip->caps = ptp_pch_caps;
4741ef76158SRichard Cochran chip->ptp_clock = ptp_clock_register(&chip->caps, &pdev->dev);
475874f50c8SAndy Shevchenko if (IS_ERR(chip->ptp_clock))
476874f50c8SAndy Shevchenko return PTR_ERR(chip->ptp_clock);
477863d08ecSTakahiro Shimizu
478863d08ecSTakahiro Shimizu spin_lock_init(&chip->register_lock);
479863d08ecSTakahiro Shimizu
480863d08ecSTakahiro Shimizu ret = request_irq(pdev->irq, &isr, IRQF_SHARED, KBUILD_MODNAME, chip);
481863d08ecSTakahiro Shimizu if (ret != 0) {
482863d08ecSTakahiro Shimizu dev_err(&pdev->dev, "failed to get irq %d\n", pdev->irq);
483863d08ecSTakahiro Shimizu goto err_req_irq;
484863d08ecSTakahiro Shimizu }
485863d08ecSTakahiro Shimizu
486863d08ecSTakahiro Shimizu /* indicate success */
487863d08ecSTakahiro Shimizu chip->irq = pdev->irq;
488863d08ecSTakahiro Shimizu chip->pdev = pdev;
489863d08ecSTakahiro Shimizu pci_set_drvdata(pdev, chip);
490863d08ecSTakahiro Shimizu
491863d08ecSTakahiro Shimizu spin_lock_irqsave(&chip->register_lock, flags);
492863d08ecSTakahiro Shimizu /* reset the ieee1588 h/w */
493863d08ecSTakahiro Shimizu pch_reset(chip);
494863d08ecSTakahiro Shimizu
495863d08ecSTakahiro Shimizu iowrite32(DEFAULT_ADDEND, &chip->regs->addend);
4968664d49aSAndy Shevchenko iowrite64_lo_hi(1, &chip->regs->trgt_lo);
497863d08ecSTakahiro Shimizu iowrite32(PCH_TSE_TTIPEND, &chip->regs->event);
498863d08ecSTakahiro Shimizu
499863d08ecSTakahiro Shimizu pch_eth_enable_set(chip);
500863d08ecSTakahiro Shimizu
501863d08ecSTakahiro Shimizu if (strcmp(pch_param.station, "00:00:00:00:00:00") != 0) {
502863d08ecSTakahiro Shimizu if (pch_set_station_address(pch_param.station, pdev) != 0) {
503863d08ecSTakahiro Shimizu dev_err(&pdev->dev,
504863d08ecSTakahiro Shimizu "Invalid station address parameter\n"
505863d08ecSTakahiro Shimizu "Module loaded but station address not set correctly\n"
506863d08ecSTakahiro Shimizu );
507863d08ecSTakahiro Shimizu }
508863d08ecSTakahiro Shimizu }
509863d08ecSTakahiro Shimizu spin_unlock_irqrestore(&chip->register_lock, flags);
510863d08ecSTakahiro Shimizu return 0;
511863d08ecSTakahiro Shimizu
512863d08ecSTakahiro Shimizu err_req_irq:
513863d08ecSTakahiro Shimizu ptp_clock_unregister(chip->ptp_clock);
514863d08ecSTakahiro Shimizu
515863d08ecSTakahiro Shimizu dev_err(&pdev->dev, "probe failed(ret=0x%x)\n", ret);
516863d08ecSTakahiro Shimizu
517863d08ecSTakahiro Shimizu return ret;
518863d08ecSTakahiro Shimizu }
519863d08ecSTakahiro Shimizu
5209baa3c34SBenoit Taine static const struct pci_device_id pch_ieee1588_pcidev_id[] = {
521863d08ecSTakahiro Shimizu {
522863d08ecSTakahiro Shimizu .vendor = PCI_VENDOR_ID_INTEL,
523863d08ecSTakahiro Shimizu .device = PCI_DEVICE_ID_PCH_1588
524863d08ecSTakahiro Shimizu },
525863d08ecSTakahiro Shimizu {0}
526863d08ecSTakahiro Shimizu };
5277cd8b154SAndy Shevchenko MODULE_DEVICE_TABLE(pci, pch_ieee1588_pcidev_id);
528863d08ecSTakahiro Shimizu
529d8d78949SDavid S. Miller static struct pci_driver pch_driver = {
530863d08ecSTakahiro Shimizu .name = KBUILD_MODNAME,
531863d08ecSTakahiro Shimizu .id_table = pch_ieee1588_pcidev_id,
532863d08ecSTakahiro Shimizu .probe = pch_probe,
533863d08ecSTakahiro Shimizu .remove = pch_remove,
534863d08ecSTakahiro Shimizu };
5353fa66d3dSAndy Shevchenko module_pci_driver(pch_driver);
536863d08ecSTakahiro Shimizu
5377d3ac5c7SSahara module_param_string(station,
5387d3ac5c7SSahara pch_param.station, sizeof(pch_param.station), 0444);
539863d08ecSTakahiro Shimizu MODULE_PARM_DESC(station,
54055c31b5bSJiri Benc "IEEE 1588 station address to use - colon separated hex values");
541863d08ecSTakahiro Shimizu
542863d08ecSTakahiro Shimizu MODULE_AUTHOR("LAPIS SEMICONDUCTOR, <tshimizu818@gmail.com>");
543863d08ecSTakahiro Shimizu MODULE_DESCRIPTION("PTP clock using the EG20T timer");
544863d08ecSTakahiro Shimizu MODULE_LICENSE("GPL");
545