1bad1eaa6SRichard Cochran // SPDX-License-Identifier: GPL-2.0
2bad1eaa6SRichard Cochran //
3bad1eaa6SRichard Cochran // Copyright (C) 2018 MOSER-BAER AG
4bad1eaa6SRichard Cochran //
5bad1eaa6SRichard Cochran
6bad1eaa6SRichard Cochran #define pr_fmt(fmt) "InES_PTP: " fmt
7bad1eaa6SRichard Cochran
8bad1eaa6SRichard Cochran #include <linux/ethtool.h>
9bad1eaa6SRichard Cochran #include <linux/export.h>
10bad1eaa6SRichard Cochran #include <linux/if_vlan.h>
11bad1eaa6SRichard Cochran #include <linux/mii_timestamper.h>
12bad1eaa6SRichard Cochran #include <linux/module.h>
13bad1eaa6SRichard Cochran #include <linux/net_tstamp.h>
14bad1eaa6SRichard Cochran #include <linux/of.h>
15bad1eaa6SRichard Cochran #include <linux/of_address.h>
16bad1eaa6SRichard Cochran #include <linux/of_irq.h>
17bad1eaa6SRichard Cochran #include <linux/phy.h>
18bad1eaa6SRichard Cochran #include <linux/platform_device.h>
19bad1eaa6SRichard Cochran #include <linux/ptp_classify.h>
20bad1eaa6SRichard Cochran #include <linux/ptp_clock_kernel.h>
21bad1eaa6SRichard Cochran #include <linux/stddef.h>
22bad1eaa6SRichard Cochran
23bad1eaa6SRichard Cochran MODULE_DESCRIPTION("Driver for the ZHAW InES PTP time stamping IP core");
24bad1eaa6SRichard Cochran MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
25bad1eaa6SRichard Cochran MODULE_VERSION("1.0");
26bad1eaa6SRichard Cochran MODULE_LICENSE("GPL");
27bad1eaa6SRichard Cochran
28bad1eaa6SRichard Cochran /* GLOBAL register */
29bad1eaa6SRichard Cochran #define MCAST_MAC_SELECT_SHIFT 2
30bad1eaa6SRichard Cochran #define MCAST_MAC_SELECT_MASK 0x3
31bad1eaa6SRichard Cochran #define IO_RESET BIT(1)
32bad1eaa6SRichard Cochran #define PTP_RESET BIT(0)
33bad1eaa6SRichard Cochran
34bad1eaa6SRichard Cochran /* VERSION register */
35bad1eaa6SRichard Cochran #define IF_MAJOR_VER_SHIFT 12
36bad1eaa6SRichard Cochran #define IF_MAJOR_VER_MASK 0xf
37bad1eaa6SRichard Cochran #define IF_MINOR_VER_SHIFT 8
38bad1eaa6SRichard Cochran #define IF_MINOR_VER_MASK 0xf
39bad1eaa6SRichard Cochran #define FPGA_MAJOR_VER_SHIFT 4
40bad1eaa6SRichard Cochran #define FPGA_MAJOR_VER_MASK 0xf
41bad1eaa6SRichard Cochran #define FPGA_MINOR_VER_SHIFT 0
42bad1eaa6SRichard Cochran #define FPGA_MINOR_VER_MASK 0xf
43bad1eaa6SRichard Cochran
44bad1eaa6SRichard Cochran /* INT_STAT register */
45bad1eaa6SRichard Cochran #define RX_INTR_STATUS_3 BIT(5)
46bad1eaa6SRichard Cochran #define RX_INTR_STATUS_2 BIT(4)
47bad1eaa6SRichard Cochran #define RX_INTR_STATUS_1 BIT(3)
48bad1eaa6SRichard Cochran #define TX_INTR_STATUS_3 BIT(2)
49bad1eaa6SRichard Cochran #define TX_INTR_STATUS_2 BIT(1)
50bad1eaa6SRichard Cochran #define TX_INTR_STATUS_1 BIT(0)
51bad1eaa6SRichard Cochran
52bad1eaa6SRichard Cochran /* INT_MSK register */
53bad1eaa6SRichard Cochran #define RX_INTR_MASK_3 BIT(5)
54bad1eaa6SRichard Cochran #define RX_INTR_MASK_2 BIT(4)
55bad1eaa6SRichard Cochran #define RX_INTR_MASK_1 BIT(3)
56bad1eaa6SRichard Cochran #define TX_INTR_MASK_3 BIT(2)
57bad1eaa6SRichard Cochran #define TX_INTR_MASK_2 BIT(1)
58bad1eaa6SRichard Cochran #define TX_INTR_MASK_1 BIT(0)
59bad1eaa6SRichard Cochran
60bad1eaa6SRichard Cochran /* BUF_STAT register */
61bad1eaa6SRichard Cochran #define RX_FIFO_NE_3 BIT(5)
62bad1eaa6SRichard Cochran #define RX_FIFO_NE_2 BIT(4)
63bad1eaa6SRichard Cochran #define RX_FIFO_NE_1 BIT(3)
64bad1eaa6SRichard Cochran #define TX_FIFO_NE_3 BIT(2)
65bad1eaa6SRichard Cochran #define TX_FIFO_NE_2 BIT(1)
66bad1eaa6SRichard Cochran #define TX_FIFO_NE_1 BIT(0)
67bad1eaa6SRichard Cochran
68bad1eaa6SRichard Cochran /* PORT_CONF register */
69bad1eaa6SRichard Cochran #define CM_ONE_STEP BIT(6)
70bad1eaa6SRichard Cochran #define PHY_SPEED_SHIFT 4
71bad1eaa6SRichard Cochran #define PHY_SPEED_MASK 0x3
72bad1eaa6SRichard Cochran #define P2P_DELAY_WR_POS_SHIFT 2
73bad1eaa6SRichard Cochran #define P2P_DELAY_WR_POS_MASK 0x3
74bad1eaa6SRichard Cochran #define PTP_MODE_SHIFT 0
75bad1eaa6SRichard Cochran #define PTP_MODE_MASK 0x3
76bad1eaa6SRichard Cochran
77bad1eaa6SRichard Cochran /* TS_STAT_TX register */
78bad1eaa6SRichard Cochran #define TS_ENABLE BIT(15)
79bad1eaa6SRichard Cochran #define DATA_READ_POS_SHIFT 8
80bad1eaa6SRichard Cochran #define DATA_READ_POS_MASK 0x1f
81bad1eaa6SRichard Cochran #define DISCARDED_EVENTS_SHIFT 4
82bad1eaa6SRichard Cochran #define DISCARDED_EVENTS_MASK 0xf
83bad1eaa6SRichard Cochran
84bad1eaa6SRichard Cochran #define INES_N_PORTS 3
85bad1eaa6SRichard Cochran #define INES_REGISTER_SIZE 0x80
86bad1eaa6SRichard Cochran #define INES_PORT_OFFSET 0x20
87bad1eaa6SRichard Cochran #define INES_PORT_SIZE 0x20
88bad1eaa6SRichard Cochran #define INES_FIFO_DEPTH 90
89bad1eaa6SRichard Cochran #define INES_MAX_EVENTS 100
90bad1eaa6SRichard Cochran
91bad1eaa6SRichard Cochran #define BC_PTP_V1 0
92bad1eaa6SRichard Cochran #define BC_PTP_V2 1
93bad1eaa6SRichard Cochran #define TC_E2E_PTP_V2 2
94bad1eaa6SRichard Cochran #define TC_P2P_PTP_V2 3
95bad1eaa6SRichard Cochran
96bad1eaa6SRichard Cochran #define PHY_SPEED_10 0
97bad1eaa6SRichard Cochran #define PHY_SPEED_100 1
98bad1eaa6SRichard Cochran #define PHY_SPEED_1000 2
99bad1eaa6SRichard Cochran
100bad1eaa6SRichard Cochran #define PORT_CONF \
101bad1eaa6SRichard Cochran ((PHY_SPEED_1000 << PHY_SPEED_SHIFT) | (BC_PTP_V2 << PTP_MODE_SHIFT))
102bad1eaa6SRichard Cochran
103bad1eaa6SRichard Cochran #define ines_read32(s, r) __raw_readl((void __iomem *)&s->regs->r)
104bad1eaa6SRichard Cochran #define ines_write32(s, v, r) __raw_writel(v, (void __iomem *)&s->regs->r)
105bad1eaa6SRichard Cochran
106bad1eaa6SRichard Cochran #define MESSAGE_TYPE_SYNC 1
107bad1eaa6SRichard Cochran #define MESSAGE_TYPE_P_DELAY_REQ 2
108bad1eaa6SRichard Cochran #define MESSAGE_TYPE_P_DELAY_RESP 3
109bad1eaa6SRichard Cochran #define MESSAGE_TYPE_DELAY_REQ 4
110bad1eaa6SRichard Cochran
111bad1eaa6SRichard Cochran static LIST_HEAD(ines_clocks);
112bad1eaa6SRichard Cochran static DEFINE_MUTEX(ines_clocks_lock);
113bad1eaa6SRichard Cochran
114bad1eaa6SRichard Cochran struct ines_global_regs {
115bad1eaa6SRichard Cochran u32 id;
116bad1eaa6SRichard Cochran u32 test;
117bad1eaa6SRichard Cochran u32 global;
118bad1eaa6SRichard Cochran u32 version;
119bad1eaa6SRichard Cochran u32 test2;
120bad1eaa6SRichard Cochran u32 int_stat;
121bad1eaa6SRichard Cochran u32 int_msk;
122bad1eaa6SRichard Cochran u32 buf_stat;
123bad1eaa6SRichard Cochran };
124bad1eaa6SRichard Cochran
125bad1eaa6SRichard Cochran struct ines_port_registers {
126bad1eaa6SRichard Cochran u32 port_conf;
127bad1eaa6SRichard Cochran u32 p_delay;
128bad1eaa6SRichard Cochran u32 ts_stat_tx;
129bad1eaa6SRichard Cochran u32 ts_stat_rx;
130bad1eaa6SRichard Cochran u32 ts_tx;
131bad1eaa6SRichard Cochran u32 ts_rx;
132bad1eaa6SRichard Cochran };
133bad1eaa6SRichard Cochran
134bad1eaa6SRichard Cochran struct ines_timestamp {
135bad1eaa6SRichard Cochran struct list_head list;
136bad1eaa6SRichard Cochran unsigned long tmo;
137bad1eaa6SRichard Cochran u16 tag;
138bad1eaa6SRichard Cochran u64 sec;
139bad1eaa6SRichard Cochran u64 nsec;
140bad1eaa6SRichard Cochran u64 clkid;
141bad1eaa6SRichard Cochran u16 portnum;
142bad1eaa6SRichard Cochran u16 seqid;
143bad1eaa6SRichard Cochran };
144bad1eaa6SRichard Cochran
145bad1eaa6SRichard Cochran struct ines_port {
146bad1eaa6SRichard Cochran struct ines_port_registers *regs;
147bad1eaa6SRichard Cochran struct mii_timestamper mii_ts;
148bad1eaa6SRichard Cochran struct ines_clock *clock;
149bad1eaa6SRichard Cochran bool rxts_enabled;
150bad1eaa6SRichard Cochran bool txts_enabled;
151bad1eaa6SRichard Cochran unsigned int index;
152bad1eaa6SRichard Cochran struct delayed_work ts_work;
153bad1eaa6SRichard Cochran /* lock protects event list and tx_skb */
154bad1eaa6SRichard Cochran spinlock_t lock;
155bad1eaa6SRichard Cochran struct sk_buff *tx_skb;
156bad1eaa6SRichard Cochran struct list_head events;
157bad1eaa6SRichard Cochran struct list_head pool;
158bad1eaa6SRichard Cochran struct ines_timestamp pool_data[INES_MAX_EVENTS];
159bad1eaa6SRichard Cochran };
160bad1eaa6SRichard Cochran
161bad1eaa6SRichard Cochran struct ines_clock {
162bad1eaa6SRichard Cochran struct ines_port port[INES_N_PORTS];
163bad1eaa6SRichard Cochran struct ines_global_regs __iomem *regs;
164bad1eaa6SRichard Cochran void __iomem *base;
165bad1eaa6SRichard Cochran struct device_node *node;
166bad1eaa6SRichard Cochran struct device *dev;
167bad1eaa6SRichard Cochran struct list_head list;
168bad1eaa6SRichard Cochran };
169bad1eaa6SRichard Cochran
170bad1eaa6SRichard Cochran static bool ines_match(struct sk_buff *skb, unsigned int ptp_class,
171bad1eaa6SRichard Cochran struct ines_timestamp *ts, struct device *dev);
172bad1eaa6SRichard Cochran static int ines_rxfifo_read(struct ines_port *port);
173bad1eaa6SRichard Cochran static u64 ines_rxts64(struct ines_port *port, unsigned int words);
174bad1eaa6SRichard Cochran static bool ines_timestamp_expired(struct ines_timestamp *ts);
175bad1eaa6SRichard Cochran static u64 ines_txts64(struct ines_port *port, unsigned int words);
176bad1eaa6SRichard Cochran static void ines_txtstamp_work(struct work_struct *work);
177bad1eaa6SRichard Cochran static bool is_sync_pdelay_resp(struct sk_buff *skb, int type);
178bad1eaa6SRichard Cochran static u8 tag_to_msgtype(u8 tag);
179bad1eaa6SRichard Cochran
ines_clock_cleanup(struct ines_clock * clock)180bad1eaa6SRichard Cochran static void ines_clock_cleanup(struct ines_clock *clock)
181bad1eaa6SRichard Cochran {
182bad1eaa6SRichard Cochran struct ines_port *port;
183bad1eaa6SRichard Cochran int i;
184bad1eaa6SRichard Cochran
185bad1eaa6SRichard Cochran for (i = 0; i < INES_N_PORTS; i++) {
186bad1eaa6SRichard Cochran port = &clock->port[i];
187bad1eaa6SRichard Cochran cancel_delayed_work_sync(&port->ts_work);
188bad1eaa6SRichard Cochran }
189bad1eaa6SRichard Cochran }
190bad1eaa6SRichard Cochran
ines_clock_init(struct ines_clock * clock,struct device * device,void __iomem * addr)191bad1eaa6SRichard Cochran static int ines_clock_init(struct ines_clock *clock, struct device *device,
192bad1eaa6SRichard Cochran void __iomem *addr)
193bad1eaa6SRichard Cochran {
194bad1eaa6SRichard Cochran struct device_node *node = device->of_node;
195bad1eaa6SRichard Cochran unsigned long port_addr;
196bad1eaa6SRichard Cochran struct ines_port *port;
197bad1eaa6SRichard Cochran int i, j;
198bad1eaa6SRichard Cochran
199bad1eaa6SRichard Cochran INIT_LIST_HEAD(&clock->list);
200bad1eaa6SRichard Cochran clock->node = node;
201bad1eaa6SRichard Cochran clock->dev = device;
202bad1eaa6SRichard Cochran clock->base = addr;
203bad1eaa6SRichard Cochran clock->regs = clock->base;
204bad1eaa6SRichard Cochran
205bad1eaa6SRichard Cochran for (i = 0; i < INES_N_PORTS; i++) {
206bad1eaa6SRichard Cochran port = &clock->port[i];
207bad1eaa6SRichard Cochran port_addr = (unsigned long) clock->base +
208bad1eaa6SRichard Cochran INES_PORT_OFFSET + i * INES_PORT_SIZE;
209bad1eaa6SRichard Cochran port->regs = (struct ines_port_registers *) port_addr;
210bad1eaa6SRichard Cochran port->clock = clock;
211bad1eaa6SRichard Cochran port->index = i;
212bad1eaa6SRichard Cochran INIT_DELAYED_WORK(&port->ts_work, ines_txtstamp_work);
213bad1eaa6SRichard Cochran spin_lock_init(&port->lock);
214bad1eaa6SRichard Cochran INIT_LIST_HEAD(&port->events);
215bad1eaa6SRichard Cochran INIT_LIST_HEAD(&port->pool);
216bad1eaa6SRichard Cochran for (j = 0; j < INES_MAX_EVENTS; j++)
217bad1eaa6SRichard Cochran list_add(&port->pool_data[j].list, &port->pool);
218bad1eaa6SRichard Cochran }
219bad1eaa6SRichard Cochran
220bad1eaa6SRichard Cochran ines_write32(clock, 0xBEEF, test);
221bad1eaa6SRichard Cochran ines_write32(clock, 0xBEEF, test2);
222bad1eaa6SRichard Cochran
223bad1eaa6SRichard Cochran dev_dbg(device, "ID 0x%x\n", ines_read32(clock, id));
224bad1eaa6SRichard Cochran dev_dbg(device, "TEST 0x%x\n", ines_read32(clock, test));
225bad1eaa6SRichard Cochran dev_dbg(device, "VERSION 0x%x\n", ines_read32(clock, version));
226bad1eaa6SRichard Cochran dev_dbg(device, "TEST2 0x%x\n", ines_read32(clock, test2));
227bad1eaa6SRichard Cochran
228bad1eaa6SRichard Cochran for (i = 0; i < INES_N_PORTS; i++) {
229bad1eaa6SRichard Cochran port = &clock->port[i];
230bad1eaa6SRichard Cochran ines_write32(port, PORT_CONF, port_conf);
231bad1eaa6SRichard Cochran }
232bad1eaa6SRichard Cochran
233bad1eaa6SRichard Cochran return 0;
234bad1eaa6SRichard Cochran }
235bad1eaa6SRichard Cochran
ines_find_port(struct device_node * node,u32 index)236bad1eaa6SRichard Cochran static struct ines_port *ines_find_port(struct device_node *node, u32 index)
237bad1eaa6SRichard Cochran {
238bad1eaa6SRichard Cochran struct ines_port *port = NULL;
239bad1eaa6SRichard Cochran struct ines_clock *clock;
240bad1eaa6SRichard Cochran struct list_head *this;
241bad1eaa6SRichard Cochran
242bad1eaa6SRichard Cochran mutex_lock(&ines_clocks_lock);
243bad1eaa6SRichard Cochran list_for_each(this, &ines_clocks) {
244bad1eaa6SRichard Cochran clock = list_entry(this, struct ines_clock, list);
245bad1eaa6SRichard Cochran if (clock->node == node) {
246bad1eaa6SRichard Cochran port = &clock->port[index];
247bad1eaa6SRichard Cochran break;
248bad1eaa6SRichard Cochran }
249bad1eaa6SRichard Cochran }
250bad1eaa6SRichard Cochran mutex_unlock(&ines_clocks_lock);
251bad1eaa6SRichard Cochran return port;
252bad1eaa6SRichard Cochran }
253bad1eaa6SRichard Cochran
ines_find_rxts(struct ines_port * port,struct sk_buff * skb,int type)254bad1eaa6SRichard Cochran static u64 ines_find_rxts(struct ines_port *port, struct sk_buff *skb, int type)
255bad1eaa6SRichard Cochran {
256bad1eaa6SRichard Cochran struct list_head *this, *next;
257bad1eaa6SRichard Cochran struct ines_timestamp *ts;
258bad1eaa6SRichard Cochran unsigned long flags;
259bad1eaa6SRichard Cochran u64 ns = 0;
260bad1eaa6SRichard Cochran
261bad1eaa6SRichard Cochran if (type == PTP_CLASS_NONE)
262bad1eaa6SRichard Cochran return 0;
263bad1eaa6SRichard Cochran
264bad1eaa6SRichard Cochran spin_lock_irqsave(&port->lock, flags);
265bad1eaa6SRichard Cochran ines_rxfifo_read(port);
266bad1eaa6SRichard Cochran list_for_each_safe(this, next, &port->events) {
267bad1eaa6SRichard Cochran ts = list_entry(this, struct ines_timestamp, list);
268bad1eaa6SRichard Cochran if (ines_timestamp_expired(ts)) {
269bad1eaa6SRichard Cochran list_del_init(&ts->list);
270bad1eaa6SRichard Cochran list_add(&ts->list, &port->pool);
271bad1eaa6SRichard Cochran continue;
272bad1eaa6SRichard Cochran }
273bad1eaa6SRichard Cochran if (ines_match(skb, type, ts, port->clock->dev)) {
274bad1eaa6SRichard Cochran ns = ts->sec * 1000000000ULL + ts->nsec;
275bad1eaa6SRichard Cochran list_del_init(&ts->list);
276bad1eaa6SRichard Cochran list_add(&ts->list, &port->pool);
277bad1eaa6SRichard Cochran break;
278bad1eaa6SRichard Cochran }
279bad1eaa6SRichard Cochran }
280bad1eaa6SRichard Cochran spin_unlock_irqrestore(&port->lock, flags);
281bad1eaa6SRichard Cochran
282bad1eaa6SRichard Cochran return ns;
283bad1eaa6SRichard Cochran }
284bad1eaa6SRichard Cochran
ines_find_txts(struct ines_port * port,struct sk_buff * skb)285bad1eaa6SRichard Cochran static u64 ines_find_txts(struct ines_port *port, struct sk_buff *skb)
286bad1eaa6SRichard Cochran {
287bad1eaa6SRichard Cochran unsigned int class = ptp_classify_raw(skb), i;
288bad1eaa6SRichard Cochran u32 data_rd_pos, buf_stat, mask, ts_stat_tx;
289bad1eaa6SRichard Cochran struct ines_timestamp ts;
290bad1eaa6SRichard Cochran unsigned long flags;
291bad1eaa6SRichard Cochran u64 ns = 0;
292bad1eaa6SRichard Cochran
293bad1eaa6SRichard Cochran mask = TX_FIFO_NE_1 << port->index;
294bad1eaa6SRichard Cochran
295bad1eaa6SRichard Cochran spin_lock_irqsave(&port->lock, flags);
296bad1eaa6SRichard Cochran
297bad1eaa6SRichard Cochran for (i = 0; i < INES_FIFO_DEPTH; i++) {
298bad1eaa6SRichard Cochran
299bad1eaa6SRichard Cochran buf_stat = ines_read32(port->clock, buf_stat);
300bad1eaa6SRichard Cochran if (!(buf_stat & mask)) {
301bad1eaa6SRichard Cochran dev_dbg(port->clock->dev,
302bad1eaa6SRichard Cochran "Tx timestamp FIFO unexpectedly empty\n");
303bad1eaa6SRichard Cochran break;
304bad1eaa6SRichard Cochran }
305bad1eaa6SRichard Cochran ts_stat_tx = ines_read32(port, ts_stat_tx);
306bad1eaa6SRichard Cochran data_rd_pos = (ts_stat_tx >> DATA_READ_POS_SHIFT) &
307bad1eaa6SRichard Cochran DATA_READ_POS_MASK;
308bad1eaa6SRichard Cochran if (data_rd_pos) {
309bad1eaa6SRichard Cochran dev_err(port->clock->dev,
310bad1eaa6SRichard Cochran "unexpected Tx read pos %u\n", data_rd_pos);
311bad1eaa6SRichard Cochran break;
312bad1eaa6SRichard Cochran }
313bad1eaa6SRichard Cochran
314bad1eaa6SRichard Cochran ts.tag = ines_read32(port, ts_tx);
315bad1eaa6SRichard Cochran ts.sec = ines_txts64(port, 3);
316bad1eaa6SRichard Cochran ts.nsec = ines_txts64(port, 2);
317bad1eaa6SRichard Cochran ts.clkid = ines_txts64(port, 4);
318bad1eaa6SRichard Cochran ts.portnum = ines_read32(port, ts_tx);
319bad1eaa6SRichard Cochran ts.seqid = ines_read32(port, ts_tx);
320bad1eaa6SRichard Cochran
321bad1eaa6SRichard Cochran if (ines_match(skb, class, &ts, port->clock->dev)) {
322bad1eaa6SRichard Cochran ns = ts.sec * 1000000000ULL + ts.nsec;
323bad1eaa6SRichard Cochran break;
324bad1eaa6SRichard Cochran }
325bad1eaa6SRichard Cochran }
326bad1eaa6SRichard Cochran
327bad1eaa6SRichard Cochran spin_unlock_irqrestore(&port->lock, flags);
328bad1eaa6SRichard Cochran return ns;
329bad1eaa6SRichard Cochran }
330bad1eaa6SRichard Cochran
ines_hwtstamp(struct mii_timestamper * mii_ts,struct kernel_hwtstamp_config * cfg,struct netlink_ext_ack * extack)331446e2305SKory Maincent static int ines_hwtstamp(struct mii_timestamper *mii_ts,
332446e2305SKory Maincent struct kernel_hwtstamp_config *cfg,
333446e2305SKory Maincent struct netlink_ext_ack *extack)
334bad1eaa6SRichard Cochran {
335bad1eaa6SRichard Cochran struct ines_port *port = container_of(mii_ts, struct ines_port, mii_ts);
336bad1eaa6SRichard Cochran u32 cm_one_step = 0, port_conf, ts_stat_rx, ts_stat_tx;
337bad1eaa6SRichard Cochran unsigned long flags;
338bad1eaa6SRichard Cochran
339446e2305SKory Maincent switch (cfg->tx_type) {
340bad1eaa6SRichard Cochran case HWTSTAMP_TX_OFF:
341bad1eaa6SRichard Cochran ts_stat_tx = 0;
342bad1eaa6SRichard Cochran break;
343bad1eaa6SRichard Cochran case HWTSTAMP_TX_ON:
344bad1eaa6SRichard Cochran ts_stat_tx = TS_ENABLE;
345bad1eaa6SRichard Cochran break;
346bad1eaa6SRichard Cochran case HWTSTAMP_TX_ONESTEP_P2P:
347bad1eaa6SRichard Cochran ts_stat_tx = TS_ENABLE;
348bad1eaa6SRichard Cochran cm_one_step = CM_ONE_STEP;
349bad1eaa6SRichard Cochran break;
350bad1eaa6SRichard Cochran default:
351bad1eaa6SRichard Cochran return -ERANGE;
352bad1eaa6SRichard Cochran }
353bad1eaa6SRichard Cochran
354446e2305SKory Maincent switch (cfg->rx_filter) {
355bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_NONE:
356bad1eaa6SRichard Cochran ts_stat_rx = 0;
357bad1eaa6SRichard Cochran break;
358bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_ALL:
359bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
360bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
361bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
362bad1eaa6SRichard Cochran return -ERANGE;
363bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
364bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
365bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
366bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
367bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
368bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
369bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V2_EVENT:
370bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V2_SYNC:
371bad1eaa6SRichard Cochran case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
372bad1eaa6SRichard Cochran ts_stat_rx = TS_ENABLE;
373446e2305SKory Maincent cfg->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
374bad1eaa6SRichard Cochran break;
375bad1eaa6SRichard Cochran default:
376bad1eaa6SRichard Cochran return -ERANGE;
377bad1eaa6SRichard Cochran }
378bad1eaa6SRichard Cochran
379bad1eaa6SRichard Cochran spin_lock_irqsave(&port->lock, flags);
380bad1eaa6SRichard Cochran
381bad1eaa6SRichard Cochran port_conf = ines_read32(port, port_conf);
382bad1eaa6SRichard Cochran port_conf &= ~CM_ONE_STEP;
383bad1eaa6SRichard Cochran port_conf |= cm_one_step;
384bad1eaa6SRichard Cochran
385bad1eaa6SRichard Cochran ines_write32(port, port_conf, port_conf);
386bad1eaa6SRichard Cochran ines_write32(port, ts_stat_rx, ts_stat_rx);
387bad1eaa6SRichard Cochran ines_write32(port, ts_stat_tx, ts_stat_tx);
388bad1eaa6SRichard Cochran
389e9a9e519SJason Yan port->rxts_enabled = ts_stat_rx == TS_ENABLE;
390e9a9e519SJason Yan port->txts_enabled = ts_stat_tx == TS_ENABLE;
391bad1eaa6SRichard Cochran
392bad1eaa6SRichard Cochran spin_unlock_irqrestore(&port->lock, flags);
393bad1eaa6SRichard Cochran
394446e2305SKory Maincent return 0;
395bad1eaa6SRichard Cochran }
396bad1eaa6SRichard Cochran
ines_link_state(struct mii_timestamper * mii_ts,struct phy_device * phydev)397bad1eaa6SRichard Cochran static void ines_link_state(struct mii_timestamper *mii_ts,
398bad1eaa6SRichard Cochran struct phy_device *phydev)
399bad1eaa6SRichard Cochran {
400bad1eaa6SRichard Cochran struct ines_port *port = container_of(mii_ts, struct ines_port, mii_ts);
401bad1eaa6SRichard Cochran u32 port_conf, speed_conf;
402bad1eaa6SRichard Cochran unsigned long flags;
403bad1eaa6SRichard Cochran
404bad1eaa6SRichard Cochran switch (phydev->speed) {
405bad1eaa6SRichard Cochran case SPEED_10:
406bad1eaa6SRichard Cochran speed_conf = PHY_SPEED_10 << PHY_SPEED_SHIFT;
407bad1eaa6SRichard Cochran break;
408bad1eaa6SRichard Cochran case SPEED_100:
409bad1eaa6SRichard Cochran speed_conf = PHY_SPEED_100 << PHY_SPEED_SHIFT;
410bad1eaa6SRichard Cochran break;
411bad1eaa6SRichard Cochran case SPEED_1000:
412bad1eaa6SRichard Cochran speed_conf = PHY_SPEED_1000 << PHY_SPEED_SHIFT;
413bad1eaa6SRichard Cochran break;
414bad1eaa6SRichard Cochran default:
415bad1eaa6SRichard Cochran dev_err(port->clock->dev, "bad speed: %d\n", phydev->speed);
416bad1eaa6SRichard Cochran return;
417bad1eaa6SRichard Cochran }
418bad1eaa6SRichard Cochran spin_lock_irqsave(&port->lock, flags);
419bad1eaa6SRichard Cochran
420bad1eaa6SRichard Cochran port_conf = ines_read32(port, port_conf);
421bad1eaa6SRichard Cochran port_conf &= ~(0x3 << PHY_SPEED_SHIFT);
422bad1eaa6SRichard Cochran port_conf |= speed_conf;
423bad1eaa6SRichard Cochran
424bad1eaa6SRichard Cochran ines_write32(port, port_conf, port_conf);
425bad1eaa6SRichard Cochran
426bad1eaa6SRichard Cochran spin_unlock_irqrestore(&port->lock, flags);
427bad1eaa6SRichard Cochran }
428bad1eaa6SRichard Cochran
ines_match(struct sk_buff * skb,unsigned int ptp_class,struct ines_timestamp * ts,struct device * dev)429bad1eaa6SRichard Cochran static bool ines_match(struct sk_buff *skb, unsigned int ptp_class,
430bad1eaa6SRichard Cochran struct ines_timestamp *ts, struct device *dev)
431bad1eaa6SRichard Cochran {
4329087da5dSKurt Kanzenbach struct ptp_header *hdr;
4339087da5dSKurt Kanzenbach u16 portn, seqid;
4349087da5dSKurt Kanzenbach u8 msgtype;
4359087da5dSKurt Kanzenbach u64 clkid;
436bad1eaa6SRichard Cochran
437bad1eaa6SRichard Cochran if (unlikely(ptp_class & PTP_CLASS_V1))
438bad1eaa6SRichard Cochran return false;
439bad1eaa6SRichard Cochran
4409087da5dSKurt Kanzenbach hdr = ptp_parse_header(skb, ptp_class);
4419087da5dSKurt Kanzenbach if (!hdr)
442bad1eaa6SRichard Cochran return false;
443bad1eaa6SRichard Cochran
4449087da5dSKurt Kanzenbach msgtype = ptp_get_msgtype(hdr, ptp_class);
4459087da5dSKurt Kanzenbach clkid = be64_to_cpup((__be64 *)&hdr->source_port_identity.clock_identity.id[0]);
4469087da5dSKurt Kanzenbach portn = be16_to_cpu(hdr->source_port_identity.port_number);
4479087da5dSKurt Kanzenbach seqid = be16_to_cpu(hdr->sequence_id);
448bad1eaa6SRichard Cochran
4499087da5dSKurt Kanzenbach if (tag_to_msgtype(ts->tag & 0x7) != msgtype) {
450bad1eaa6SRichard Cochran dev_dbg(dev, "msgtype mismatch ts %hhu != skb %hhu\n",
4519087da5dSKurt Kanzenbach tag_to_msgtype(ts->tag & 0x7), msgtype);
452bad1eaa6SRichard Cochran return false;
453bad1eaa6SRichard Cochran }
4549087da5dSKurt Kanzenbach if (ts->clkid != clkid) {
455bad1eaa6SRichard Cochran dev_dbg(dev, "clkid mismatch ts %llx != skb %llx\n",
4569087da5dSKurt Kanzenbach ts->clkid, clkid);
457bad1eaa6SRichard Cochran return false;
458bad1eaa6SRichard Cochran }
4599087da5dSKurt Kanzenbach if (ts->portnum != portn) {
460bad1eaa6SRichard Cochran dev_dbg(dev, "portn mismatch ts %hu != skb %hu\n",
4619087da5dSKurt Kanzenbach ts->portnum, portn);
462bad1eaa6SRichard Cochran return false;
463bad1eaa6SRichard Cochran }
4649087da5dSKurt Kanzenbach if (ts->seqid != seqid) {
465bad1eaa6SRichard Cochran dev_dbg(dev, "seqid mismatch ts %hu != skb %hu\n",
4669087da5dSKurt Kanzenbach ts->seqid, seqid);
467bad1eaa6SRichard Cochran return false;
468bad1eaa6SRichard Cochran }
469bad1eaa6SRichard Cochran
470bad1eaa6SRichard Cochran return true;
471bad1eaa6SRichard Cochran }
472bad1eaa6SRichard Cochran
ines_rxtstamp(struct mii_timestamper * mii_ts,struct sk_buff * skb,int type)473bad1eaa6SRichard Cochran static bool ines_rxtstamp(struct mii_timestamper *mii_ts,
474bad1eaa6SRichard Cochran struct sk_buff *skb, int type)
475bad1eaa6SRichard Cochran {
476bad1eaa6SRichard Cochran struct ines_port *port = container_of(mii_ts, struct ines_port, mii_ts);
477bad1eaa6SRichard Cochran struct skb_shared_hwtstamps *ssh;
478bad1eaa6SRichard Cochran u64 ns;
479bad1eaa6SRichard Cochran
480bad1eaa6SRichard Cochran if (!port->rxts_enabled)
481bad1eaa6SRichard Cochran return false;
482bad1eaa6SRichard Cochran
483bad1eaa6SRichard Cochran ns = ines_find_rxts(port, skb, type);
484bad1eaa6SRichard Cochran if (!ns)
485bad1eaa6SRichard Cochran return false;
486bad1eaa6SRichard Cochran
487bad1eaa6SRichard Cochran ssh = skb_hwtstamps(skb);
488bad1eaa6SRichard Cochran ssh->hwtstamp = ns_to_ktime(ns);
489bad1eaa6SRichard Cochran netif_rx(skb);
490bad1eaa6SRichard Cochran
491bad1eaa6SRichard Cochran return true;
492bad1eaa6SRichard Cochran }
493bad1eaa6SRichard Cochran
ines_rxfifo_read(struct ines_port * port)494bad1eaa6SRichard Cochran static int ines_rxfifo_read(struct ines_port *port)
495bad1eaa6SRichard Cochran {
496bad1eaa6SRichard Cochran u32 data_rd_pos, buf_stat, mask, ts_stat_rx;
497bad1eaa6SRichard Cochran struct ines_timestamp *ts;
498bad1eaa6SRichard Cochran unsigned int i;
499bad1eaa6SRichard Cochran
500bad1eaa6SRichard Cochran mask = RX_FIFO_NE_1 << port->index;
501bad1eaa6SRichard Cochran
502bad1eaa6SRichard Cochran for (i = 0; i < INES_FIFO_DEPTH; i++) {
503bad1eaa6SRichard Cochran if (list_empty(&port->pool)) {
504bad1eaa6SRichard Cochran dev_err(port->clock->dev, "event pool is empty\n");
505bad1eaa6SRichard Cochran return -1;
506bad1eaa6SRichard Cochran }
507bad1eaa6SRichard Cochran buf_stat = ines_read32(port->clock, buf_stat);
508bad1eaa6SRichard Cochran if (!(buf_stat & mask))
509bad1eaa6SRichard Cochran break;
510bad1eaa6SRichard Cochran
511bad1eaa6SRichard Cochran ts_stat_rx = ines_read32(port, ts_stat_rx);
512bad1eaa6SRichard Cochran data_rd_pos = (ts_stat_rx >> DATA_READ_POS_SHIFT) &
513bad1eaa6SRichard Cochran DATA_READ_POS_MASK;
514bad1eaa6SRichard Cochran if (data_rd_pos) {
515bad1eaa6SRichard Cochran dev_err(port->clock->dev, "unexpected Rx read pos %u\n",
516bad1eaa6SRichard Cochran data_rd_pos);
517bad1eaa6SRichard Cochran break;
518bad1eaa6SRichard Cochran }
519bad1eaa6SRichard Cochran
520bad1eaa6SRichard Cochran ts = list_first_entry(&port->pool, struct ines_timestamp, list);
521bad1eaa6SRichard Cochran ts->tmo = jiffies + HZ;
522bad1eaa6SRichard Cochran ts->tag = ines_read32(port, ts_rx);
523bad1eaa6SRichard Cochran ts->sec = ines_rxts64(port, 3);
524bad1eaa6SRichard Cochran ts->nsec = ines_rxts64(port, 2);
525bad1eaa6SRichard Cochran ts->clkid = ines_rxts64(port, 4);
526bad1eaa6SRichard Cochran ts->portnum = ines_read32(port, ts_rx);
527bad1eaa6SRichard Cochran ts->seqid = ines_read32(port, ts_rx);
528bad1eaa6SRichard Cochran
529bad1eaa6SRichard Cochran list_del_init(&ts->list);
530bad1eaa6SRichard Cochran list_add_tail(&ts->list, &port->events);
531bad1eaa6SRichard Cochran }
532bad1eaa6SRichard Cochran
533bad1eaa6SRichard Cochran return 0;
534bad1eaa6SRichard Cochran }
535bad1eaa6SRichard Cochran
ines_rxts64(struct ines_port * port,unsigned int words)536bad1eaa6SRichard Cochran static u64 ines_rxts64(struct ines_port *port, unsigned int words)
537bad1eaa6SRichard Cochran {
538bad1eaa6SRichard Cochran unsigned int i;
539bad1eaa6SRichard Cochran u64 result;
540bad1eaa6SRichard Cochran u16 word;
541bad1eaa6SRichard Cochran
542bad1eaa6SRichard Cochran word = ines_read32(port, ts_rx);
543bad1eaa6SRichard Cochran result = word;
544bad1eaa6SRichard Cochran words--;
545bad1eaa6SRichard Cochran for (i = 0; i < words; i++) {
546bad1eaa6SRichard Cochran word = ines_read32(port, ts_rx);
547bad1eaa6SRichard Cochran result <<= 16;
548bad1eaa6SRichard Cochran result |= word;
549bad1eaa6SRichard Cochran }
550bad1eaa6SRichard Cochran return result;
551bad1eaa6SRichard Cochran }
552bad1eaa6SRichard Cochran
ines_timestamp_expired(struct ines_timestamp * ts)553bad1eaa6SRichard Cochran static bool ines_timestamp_expired(struct ines_timestamp *ts)
554bad1eaa6SRichard Cochran {
555bad1eaa6SRichard Cochran return time_after(jiffies, ts->tmo);
556bad1eaa6SRichard Cochran }
557bad1eaa6SRichard Cochran
ines_ts_info(struct mii_timestamper * mii_ts,struct kernel_ethtool_ts_info * info)558bad1eaa6SRichard Cochran static int ines_ts_info(struct mii_timestamper *mii_ts,
559*2111375bSKory Maincent struct kernel_ethtool_ts_info *info)
560bad1eaa6SRichard Cochran {
561bad1eaa6SRichard Cochran info->so_timestamping =
562bad1eaa6SRichard Cochran SOF_TIMESTAMPING_TX_HARDWARE |
563bad1eaa6SRichard Cochran SOF_TIMESTAMPING_TX_SOFTWARE |
564bad1eaa6SRichard Cochran SOF_TIMESTAMPING_RX_HARDWARE |
565bad1eaa6SRichard Cochran SOF_TIMESTAMPING_RAW_HARDWARE;
566bad1eaa6SRichard Cochran
567bad1eaa6SRichard Cochran info->tx_types =
568bad1eaa6SRichard Cochran (1 << HWTSTAMP_TX_OFF) |
569bad1eaa6SRichard Cochran (1 << HWTSTAMP_TX_ON) |
570bad1eaa6SRichard Cochran (1 << HWTSTAMP_TX_ONESTEP_P2P);
571bad1eaa6SRichard Cochran
572bad1eaa6SRichard Cochran info->rx_filters =
573bad1eaa6SRichard Cochran (1 << HWTSTAMP_FILTER_NONE) |
574bad1eaa6SRichard Cochran (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
575bad1eaa6SRichard Cochran
576bad1eaa6SRichard Cochran return 0;
577bad1eaa6SRichard Cochran }
578bad1eaa6SRichard Cochran
ines_txts64(struct ines_port * port,unsigned int words)579bad1eaa6SRichard Cochran static u64 ines_txts64(struct ines_port *port, unsigned int words)
580bad1eaa6SRichard Cochran {
581bad1eaa6SRichard Cochran unsigned int i;
582bad1eaa6SRichard Cochran u64 result;
583bad1eaa6SRichard Cochran u16 word;
584bad1eaa6SRichard Cochran
585bad1eaa6SRichard Cochran word = ines_read32(port, ts_tx);
586bad1eaa6SRichard Cochran result = word;
587bad1eaa6SRichard Cochran words--;
588bad1eaa6SRichard Cochran for (i = 0; i < words; i++) {
589bad1eaa6SRichard Cochran word = ines_read32(port, ts_tx);
590bad1eaa6SRichard Cochran result <<= 16;
591bad1eaa6SRichard Cochran result |= word;
592bad1eaa6SRichard Cochran }
593bad1eaa6SRichard Cochran return result;
594bad1eaa6SRichard Cochran }
595bad1eaa6SRichard Cochran
ines_txts_onestep(struct ines_port * port,struct sk_buff * skb,int type)596bad1eaa6SRichard Cochran static bool ines_txts_onestep(struct ines_port *port, struct sk_buff *skb, int type)
597bad1eaa6SRichard Cochran {
598bad1eaa6SRichard Cochran unsigned long flags;
599bad1eaa6SRichard Cochran u32 port_conf;
600bad1eaa6SRichard Cochran
601bad1eaa6SRichard Cochran spin_lock_irqsave(&port->lock, flags);
602bad1eaa6SRichard Cochran port_conf = ines_read32(port, port_conf);
603bad1eaa6SRichard Cochran spin_unlock_irqrestore(&port->lock, flags);
604bad1eaa6SRichard Cochran
605bad1eaa6SRichard Cochran if (port_conf & CM_ONE_STEP)
606bad1eaa6SRichard Cochran return is_sync_pdelay_resp(skb, type);
607bad1eaa6SRichard Cochran
608bad1eaa6SRichard Cochran return false;
609bad1eaa6SRichard Cochran }
610bad1eaa6SRichard Cochran
ines_txtstamp(struct mii_timestamper * mii_ts,struct sk_buff * skb,int type)611bad1eaa6SRichard Cochran static void ines_txtstamp(struct mii_timestamper *mii_ts,
612bad1eaa6SRichard Cochran struct sk_buff *skb, int type)
613bad1eaa6SRichard Cochran {
614bad1eaa6SRichard Cochran struct ines_port *port = container_of(mii_ts, struct ines_port, mii_ts);
615bad1eaa6SRichard Cochran struct sk_buff *old_skb = NULL;
616bad1eaa6SRichard Cochran unsigned long flags;
617bad1eaa6SRichard Cochran
618bad1eaa6SRichard Cochran if (!port->txts_enabled || ines_txts_onestep(port, skb, type)) {
619bad1eaa6SRichard Cochran kfree_skb(skb);
620bad1eaa6SRichard Cochran return;
621bad1eaa6SRichard Cochran }
622bad1eaa6SRichard Cochran
623bad1eaa6SRichard Cochran spin_lock_irqsave(&port->lock, flags);
624bad1eaa6SRichard Cochran
625bad1eaa6SRichard Cochran if (port->tx_skb)
626bad1eaa6SRichard Cochran old_skb = port->tx_skb;
627bad1eaa6SRichard Cochran
628bad1eaa6SRichard Cochran port->tx_skb = skb;
629bad1eaa6SRichard Cochran
630bad1eaa6SRichard Cochran spin_unlock_irqrestore(&port->lock, flags);
631bad1eaa6SRichard Cochran
632bad1eaa6SRichard Cochran kfree_skb(old_skb);
633bad1eaa6SRichard Cochran
634bad1eaa6SRichard Cochran schedule_delayed_work(&port->ts_work, 1);
635bad1eaa6SRichard Cochran }
636bad1eaa6SRichard Cochran
ines_txtstamp_work(struct work_struct * work)637bad1eaa6SRichard Cochran static void ines_txtstamp_work(struct work_struct *work)
638bad1eaa6SRichard Cochran {
639bad1eaa6SRichard Cochran struct ines_port *port =
640bad1eaa6SRichard Cochran container_of(work, struct ines_port, ts_work.work);
641bad1eaa6SRichard Cochran struct skb_shared_hwtstamps ssh;
642bad1eaa6SRichard Cochran struct sk_buff *skb;
643bad1eaa6SRichard Cochran unsigned long flags;
644bad1eaa6SRichard Cochran u64 ns;
645bad1eaa6SRichard Cochran
646bad1eaa6SRichard Cochran spin_lock_irqsave(&port->lock, flags);
647bad1eaa6SRichard Cochran skb = port->tx_skb;
648bad1eaa6SRichard Cochran port->tx_skb = NULL;
649bad1eaa6SRichard Cochran spin_unlock_irqrestore(&port->lock, flags);
650bad1eaa6SRichard Cochran
651bad1eaa6SRichard Cochran ns = ines_find_txts(port, skb);
652bad1eaa6SRichard Cochran if (!ns) {
653bad1eaa6SRichard Cochran kfree_skb(skb);
654bad1eaa6SRichard Cochran return;
655bad1eaa6SRichard Cochran }
656bad1eaa6SRichard Cochran ssh.hwtstamp = ns_to_ktime(ns);
657bad1eaa6SRichard Cochran skb_complete_tx_timestamp(skb, &ssh);
658bad1eaa6SRichard Cochran }
659bad1eaa6SRichard Cochran
is_sync_pdelay_resp(struct sk_buff * skb,int type)660bad1eaa6SRichard Cochran static bool is_sync_pdelay_resp(struct sk_buff *skb, int type)
661bad1eaa6SRichard Cochran {
6629087da5dSKurt Kanzenbach struct ptp_header *hdr;
6639087da5dSKurt Kanzenbach u8 msgtype;
664bad1eaa6SRichard Cochran
6659087da5dSKurt Kanzenbach hdr = ptp_parse_header(skb, type);
6669087da5dSKurt Kanzenbach if (!hdr)
6679087da5dSKurt Kanzenbach return false;
668bad1eaa6SRichard Cochran
6699087da5dSKurt Kanzenbach msgtype = ptp_get_msgtype(hdr, type);
670bad1eaa6SRichard Cochran
67134890b30SChristian Eggers switch (msgtype) {
67234890b30SChristian Eggers case PTP_MSGTYPE_SYNC:
67334890b30SChristian Eggers case PTP_MSGTYPE_PDELAY_RESP:
674bad1eaa6SRichard Cochran return true;
675bad1eaa6SRichard Cochran default:
676bad1eaa6SRichard Cochran return false;
677bad1eaa6SRichard Cochran }
678bad1eaa6SRichard Cochran }
679bad1eaa6SRichard Cochran
tag_to_msgtype(u8 tag)680bad1eaa6SRichard Cochran static u8 tag_to_msgtype(u8 tag)
681bad1eaa6SRichard Cochran {
682bad1eaa6SRichard Cochran switch (tag) {
683bad1eaa6SRichard Cochran case MESSAGE_TYPE_SYNC:
68434890b30SChristian Eggers return PTP_MSGTYPE_SYNC;
685bad1eaa6SRichard Cochran case MESSAGE_TYPE_P_DELAY_REQ:
68634890b30SChristian Eggers return PTP_MSGTYPE_PDELAY_REQ;
687bad1eaa6SRichard Cochran case MESSAGE_TYPE_P_DELAY_RESP:
68834890b30SChristian Eggers return PTP_MSGTYPE_PDELAY_RESP;
689bad1eaa6SRichard Cochran case MESSAGE_TYPE_DELAY_REQ:
69034890b30SChristian Eggers return PTP_MSGTYPE_DELAY_REQ;
691bad1eaa6SRichard Cochran }
692bad1eaa6SRichard Cochran return 0xf;
693bad1eaa6SRichard Cochran }
694bad1eaa6SRichard Cochran
ines_ptp_probe_channel(struct device * device,unsigned int index)695bad1eaa6SRichard Cochran static struct mii_timestamper *ines_ptp_probe_channel(struct device *device,
696bad1eaa6SRichard Cochran unsigned int index)
697bad1eaa6SRichard Cochran {
698bad1eaa6SRichard Cochran struct device_node *node = device->of_node;
699bad1eaa6SRichard Cochran struct ines_port *port;
700bad1eaa6SRichard Cochran
701bad1eaa6SRichard Cochran if (index > INES_N_PORTS - 1) {
702bad1eaa6SRichard Cochran dev_err(device, "bad port index %u\n", index);
703bad1eaa6SRichard Cochran return ERR_PTR(-EINVAL);
704bad1eaa6SRichard Cochran }
705bad1eaa6SRichard Cochran port = ines_find_port(node, index);
706bad1eaa6SRichard Cochran if (!port) {
707bad1eaa6SRichard Cochran dev_err(device, "missing port index %u\n", index);
708bad1eaa6SRichard Cochran return ERR_PTR(-ENODEV);
709bad1eaa6SRichard Cochran }
710bad1eaa6SRichard Cochran port->mii_ts.rxtstamp = ines_rxtstamp;
711bad1eaa6SRichard Cochran port->mii_ts.txtstamp = ines_txtstamp;
712bad1eaa6SRichard Cochran port->mii_ts.hwtstamp = ines_hwtstamp;
713bad1eaa6SRichard Cochran port->mii_ts.link_state = ines_link_state;
714bad1eaa6SRichard Cochran port->mii_ts.ts_info = ines_ts_info;
715bad1eaa6SRichard Cochran
716bad1eaa6SRichard Cochran return &port->mii_ts;
717bad1eaa6SRichard Cochran }
718bad1eaa6SRichard Cochran
ines_ptp_release_channel(struct device * device,struct mii_timestamper * mii_ts)719bad1eaa6SRichard Cochran static void ines_ptp_release_channel(struct device *device,
720bad1eaa6SRichard Cochran struct mii_timestamper *mii_ts)
721bad1eaa6SRichard Cochran {
722bad1eaa6SRichard Cochran }
723bad1eaa6SRichard Cochran
724bad1eaa6SRichard Cochran static struct mii_timestamping_ctrl ines_ctrl = {
725bad1eaa6SRichard Cochran .probe_channel = ines_ptp_probe_channel,
726bad1eaa6SRichard Cochran .release_channel = ines_ptp_release_channel,
727bad1eaa6SRichard Cochran };
728bad1eaa6SRichard Cochran
ines_ptp_ctrl_probe(struct platform_device * pld)729bad1eaa6SRichard Cochran static int ines_ptp_ctrl_probe(struct platform_device *pld)
730bad1eaa6SRichard Cochran {
731bad1eaa6SRichard Cochran struct ines_clock *clock;
732bad1eaa6SRichard Cochran void __iomem *addr;
733bad1eaa6SRichard Cochran int err = 0;
734bad1eaa6SRichard Cochran
73500b5aac5SWei Yongjun addr = devm_platform_ioremap_resource(pld, 0);
736bad1eaa6SRichard Cochran if (IS_ERR(addr)) {
737bad1eaa6SRichard Cochran err = PTR_ERR(addr);
738bad1eaa6SRichard Cochran goto out;
739bad1eaa6SRichard Cochran }
740bad1eaa6SRichard Cochran clock = kzalloc(sizeof(*clock), GFP_KERNEL);
741bad1eaa6SRichard Cochran if (!clock) {
742bad1eaa6SRichard Cochran err = -ENOMEM;
743bad1eaa6SRichard Cochran goto out;
744bad1eaa6SRichard Cochran }
745bad1eaa6SRichard Cochran if (ines_clock_init(clock, &pld->dev, addr)) {
746bad1eaa6SRichard Cochran kfree(clock);
747bad1eaa6SRichard Cochran err = -ENOMEM;
748bad1eaa6SRichard Cochran goto out;
749bad1eaa6SRichard Cochran }
750bad1eaa6SRichard Cochran err = register_mii_tstamp_controller(&pld->dev, &ines_ctrl);
751bad1eaa6SRichard Cochran if (err) {
752bad1eaa6SRichard Cochran kfree(clock);
753bad1eaa6SRichard Cochran goto out;
754bad1eaa6SRichard Cochran }
755bad1eaa6SRichard Cochran mutex_lock(&ines_clocks_lock);
756bad1eaa6SRichard Cochran list_add_tail(&ines_clocks, &clock->list);
757bad1eaa6SRichard Cochran mutex_unlock(&ines_clocks_lock);
758bad1eaa6SRichard Cochran
759bad1eaa6SRichard Cochran dev_set_drvdata(&pld->dev, clock);
760bad1eaa6SRichard Cochran out:
761bad1eaa6SRichard Cochran return err;
762bad1eaa6SRichard Cochran }
763bad1eaa6SRichard Cochran
ines_ptp_ctrl_remove(struct platform_device * pld)764cff52369SUwe Kleine-König static void ines_ptp_ctrl_remove(struct platform_device *pld)
765bad1eaa6SRichard Cochran {
766bad1eaa6SRichard Cochran struct ines_clock *clock = dev_get_drvdata(&pld->dev);
767bad1eaa6SRichard Cochran
768bad1eaa6SRichard Cochran unregister_mii_tstamp_controller(&pld->dev);
769bad1eaa6SRichard Cochran mutex_lock(&ines_clocks_lock);
770bad1eaa6SRichard Cochran list_del(&clock->list);
771bad1eaa6SRichard Cochran mutex_unlock(&ines_clocks_lock);
772bad1eaa6SRichard Cochran ines_clock_cleanup(clock);
773bad1eaa6SRichard Cochran kfree(clock);
774bad1eaa6SRichard Cochran }
775bad1eaa6SRichard Cochran
776bad1eaa6SRichard Cochran static const struct of_device_id ines_ptp_ctrl_of_match[] = {
777bad1eaa6SRichard Cochran { .compatible = "ines,ptp-ctrl" },
778bad1eaa6SRichard Cochran { }
779bad1eaa6SRichard Cochran };
780bad1eaa6SRichard Cochran
781bad1eaa6SRichard Cochran MODULE_DEVICE_TABLE(of, ines_ptp_ctrl_of_match);
782bad1eaa6SRichard Cochran
783bad1eaa6SRichard Cochran static struct platform_driver ines_ptp_ctrl_driver = {
784bad1eaa6SRichard Cochran .probe = ines_ptp_ctrl_probe,
785cff52369SUwe Kleine-König .remove_new = ines_ptp_ctrl_remove,
786bad1eaa6SRichard Cochran .driver = {
787bad1eaa6SRichard Cochran .name = "ines_ptp_ctrl",
788543c143dSKrzysztof Kozlowski .of_match_table = ines_ptp_ctrl_of_match,
789bad1eaa6SRichard Cochran },
790bad1eaa6SRichard Cochran };
791bad1eaa6SRichard Cochran module_platform_driver(ines_ptp_ctrl_driver);
792