blob: 6580094161a9f6730e529124a08811f5ace9ff53 [file] [log] [blame]
Andrew Lunna2443fd2019-01-21 19:05:50 +01001// SPDX-License-Identifier: GPL-2.0+
Richard Cochrancb646e22011-04-22 12:04:55 +02002/*
3 * Driver for the National Semiconductor DP83640 PHYTER
4 *
5 * Copyright (C) 2010 OMICRON electronics GmbH
Richard Cochrancb646e22011-04-22 12:04:55 +02006 */
Joe Perches8d242482012-06-09 07:49:07 +00007
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Stefan Sørensen539e44d2015-11-03 09:34:04 +010010#include <linux/crc32.h>
Richard Cochrancb646e22011-04-22 12:04:55 +020011#include <linux/ethtool.h>
12#include <linux/kernel.h>
13#include <linux/list.h>
14#include <linux/mii.h>
15#include <linux/module.h>
16#include <linux/net_tstamp.h>
17#include <linux/netdevice.h>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020018#include <linux/if_vlan.h>
Richard Cochrancb646e22011-04-22 12:04:55 +020019#include <linux/phy.h>
20#include <linux/ptp_classify.h>
21#include <linux/ptp_clock_kernel.h>
22
23#include "dp83640_reg.h"
24
25#define DP83640_PHY_ID 0x20005ce1
26#define PAGESEL 0x13
Richard Cochran80288372011-08-06 21:03:04 +000027#define MAX_RXTS 64
Richard Cochran49b3fd42011-09-20 01:43:14 +000028#define N_EXT_TS 6
Stefan Sørensenad015772014-06-27 12:05:30 +020029#define N_PER_OUT 7
Richard Cochrancb646e22011-04-22 12:04:55 +020030#define PSF_PTPVER 2
31#define PSF_EVNT 0x4000
32#define PSF_RX 0x2000
33#define PSF_TX 0x1000
34#define EXT_EVENT 1
Richard Cochran49b3fd42011-09-20 01:43:14 +000035#define CAL_EVENT 7
Richard Cochran397a253a2015-05-25 11:55:43 +020036#define CAL_TRIGGER 1
Richard Cochran86dd3612014-03-20 22:21:58 +010037#define DP83640_N_PINS 12
Richard Cochrancb646e22011-04-22 12:04:55 +020038
Stephan Gatzka16421822012-12-04 10:21:38 +000039#define MII_DP83640_MICR 0x11
40#define MII_DP83640_MISR 0x12
41
42#define MII_DP83640_MICR_OE 0x1
43#define MII_DP83640_MICR_IE 0x2
44
45#define MII_DP83640_MISR_RHF_INT_EN 0x01
46#define MII_DP83640_MISR_FHF_INT_EN 0x02
47#define MII_DP83640_MISR_ANC_INT_EN 0x04
48#define MII_DP83640_MISR_DUP_INT_EN 0x08
49#define MII_DP83640_MISR_SPD_INT_EN 0x10
50#define MII_DP83640_MISR_LINK_INT_EN 0x20
51#define MII_DP83640_MISR_ED_INT_EN 0x40
52#define MII_DP83640_MISR_LQ_INT_EN 0x80
53
Richard Cochrancb646e22011-04-22 12:04:55 +020054/* phyter seems to miss the mark by 16 ns */
55#define ADJTIME_FIX 16
56
Stefan Sørensen4b063252015-11-03 09:34:05 +010057#define SKB_TIMESTAMP_TIMEOUT 2 /* jiffies */
58
Richard Cochrancb646e22011-04-22 12:04:55 +020059#if defined(__BIG_ENDIAN)
60#define ENDIAN_FLAG 0
61#elif defined(__LITTLE_ENDIAN)
62#define ENDIAN_FLAG PSF_ENDIAN
63#endif
64
Stefan Sørensen63502b82014-07-22 15:20:45 +020065struct dp83640_skb_info {
66 int ptp_type;
67 unsigned long tmo;
68};
Richard Cochrancb646e22011-04-22 12:04:55 +020069
70struct phy_rxts {
71 u16 ns_lo; /* ns[15:0] */
72 u16 ns_hi; /* overflow[1:0], ns[29:16] */
73 u16 sec_lo; /* sec[15:0] */
74 u16 sec_hi; /* sec[31:16] */
75 u16 seqid; /* sequenceId[15:0] */
76 u16 msgtype; /* messageType[3:0], hash[11:0] */
77};
78
79struct phy_txts {
80 u16 ns_lo; /* ns[15:0] */
81 u16 ns_hi; /* overflow[1:0], ns[29:16] */
82 u16 sec_lo; /* sec[15:0] */
83 u16 sec_hi; /* sec[31:16] */
84};
85
86struct rxts {
87 struct list_head list;
88 unsigned long tmo;
89 u64 ns;
90 u16 seqid;
91 u8 msgtype;
92 u16 hash;
93};
94
95struct dp83640_clock;
96
97struct dp83640_private {
98 struct list_head list;
99 struct dp83640_clock *clock;
100 struct phy_device *phydev;
Stefan Sørensen4b063252015-11-03 09:34:05 +0100101 struct delayed_work ts_work;
Richard Cochrancb646e22011-04-22 12:04:55 +0200102 int hwts_tx_en;
103 int hwts_rx_en;
104 int layer;
105 int version;
106 /* remember state of cfg0 during calibration */
107 int cfg0;
108 /* remember the last event time stamp */
109 struct phy_txts edata;
110 /* list of rx timestamps */
111 struct list_head rxts;
112 struct list_head rxpool;
113 struct rxts rx_pool_data[MAX_RXTS];
114 /* protects above three fields from concurrent access */
115 spinlock_t rx_lock;
116 /* queues of incoming and outgoing packets */
117 struct sk_buff_head rx_queue;
118 struct sk_buff_head tx_queue;
119};
120
121struct dp83640_clock {
122 /* keeps the instance in the 'phyter_clocks' list */
123 struct list_head list;
124 /* we create one clock instance per MII bus */
125 struct mii_bus *bus;
126 /* protects extended registers from concurrent access */
127 struct mutex extreg_lock;
128 /* remembers which page was last selected */
129 int page;
130 /* our advertised capabilities */
131 struct ptp_clock_info caps;
132 /* protects the three fields below from concurrent access */
133 struct mutex clock_lock;
134 /* the one phyter from which we shall read */
135 struct dp83640_private *chosen;
136 /* list of the other attached phyters, not chosen */
137 struct list_head phylist;
138 /* reference to our PTP hardware clock */
139 struct ptp_clock *ptp_clock;
140};
141
142/* globals */
143
Richard Cochran49b3fd42011-09-20 01:43:14 +0000144enum {
145 CALIBRATE_GPIO,
146 PEROUT_GPIO,
147 EXTTS0_GPIO,
148 EXTTS1_GPIO,
149 EXTTS2_GPIO,
150 EXTTS3_GPIO,
151 EXTTS4_GPIO,
152 EXTTS5_GPIO,
153 GPIO_TABLE_SIZE
154};
155
Richard Cochrancb646e22011-04-22 12:04:55 +0200156static int chosen_phy = -1;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000157static ushort gpio_tab[GPIO_TABLE_SIZE] = {
158 1, 2, 3, 4, 8, 9, 10, 11
159};
Richard Cochrancb646e22011-04-22 12:04:55 +0200160
161module_param(chosen_phy, int, 0444);
Richard Cochran49b3fd42011-09-20 01:43:14 +0000162module_param_array(gpio_tab, ushort, NULL, 0444);
Richard Cochrancb646e22011-04-22 12:04:55 +0200163
164MODULE_PARM_DESC(chosen_phy, \
165 "The address of the PHY to use for the ancillary clock features");
Richard Cochran49b3fd42011-09-20 01:43:14 +0000166MODULE_PARM_DESC(gpio_tab, \
167 "Which GPIO line to use for which purpose: cal,perout,extts1,...,extts6");
Richard Cochrancb646e22011-04-22 12:04:55 +0200168
Richard Cochran86dd3612014-03-20 22:21:58 +0100169static void dp83640_gpio_defaults(struct ptp_pin_desc *pd)
170{
171 int i, index;
172
173 for (i = 0; i < DP83640_N_PINS; i++) {
174 snprintf(pd[i].name, sizeof(pd[i].name), "GPIO%d", 1 + i);
175 pd[i].index = i;
176 }
177
178 for (i = 0; i < GPIO_TABLE_SIZE; i++) {
179 if (gpio_tab[i] < 1 || gpio_tab[i] > DP83640_N_PINS) {
180 pr_err("gpio_tab[%d]=%hu out of range", i, gpio_tab[i]);
181 return;
182 }
183 }
184
185 index = gpio_tab[CALIBRATE_GPIO] - 1;
186 pd[index].func = PTP_PF_PHYSYNC;
187 pd[index].chan = 0;
188
189 index = gpio_tab[PEROUT_GPIO] - 1;
190 pd[index].func = PTP_PF_PEROUT;
191 pd[index].chan = 0;
192
193 for (i = EXTTS0_GPIO; i < GPIO_TABLE_SIZE; i++) {
194 index = gpio_tab[i] - 1;
195 pd[index].func = PTP_PF_EXTTS;
196 pd[index].chan = i - EXTTS0_GPIO;
197 }
198}
199
Richard Cochrancb646e22011-04-22 12:04:55 +0200200/* a list of clocks and a mutex to protect it */
201static LIST_HEAD(phyter_clocks);
202static DEFINE_MUTEX(phyter_clocks_lock);
203
204static void rx_timestamp_work(struct work_struct *work);
205
206/* extended register access functions */
207
208#define BROADCAST_ADDR 31
209
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100210static inline int broadcast_write(struct phy_device *phydev, u32 regnum,
211 u16 val)
Richard Cochrancb646e22011-04-22 12:04:55 +0200212{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100213 return mdiobus_write(phydev->mdio.bus, BROADCAST_ADDR, regnum, val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200214}
215
216/* Caller must hold extreg_lock. */
217static int ext_read(struct phy_device *phydev, int page, u32 regnum)
218{
219 struct dp83640_private *dp83640 = phydev->priv;
220 int val;
221
222 if (dp83640->clock->page != page) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100223 broadcast_write(phydev, PAGESEL, page);
Richard Cochrancb646e22011-04-22 12:04:55 +0200224 dp83640->clock->page = page;
225 }
226 val = phy_read(phydev, regnum);
227
228 return val;
229}
230
231/* Caller must hold extreg_lock. */
232static void ext_write(int broadcast, struct phy_device *phydev,
233 int page, u32 regnum, u16 val)
234{
235 struct dp83640_private *dp83640 = phydev->priv;
236
237 if (dp83640->clock->page != page) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100238 broadcast_write(phydev, PAGESEL, page);
Richard Cochrancb646e22011-04-22 12:04:55 +0200239 dp83640->clock->page = page;
240 }
241 if (broadcast)
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100242 broadcast_write(phydev, regnum, val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200243 else
244 phy_write(phydev, regnum, val);
245}
246
247/* Caller must hold extreg_lock. */
248static int tdr_write(int bc, struct phy_device *dev,
Richard Cochran41c2c182015-03-29 23:12:10 +0200249 const struct timespec64 *ts, u16 cmd)
Richard Cochrancb646e22011-04-22 12:04:55 +0200250{
251 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec & 0xffff);/* ns[15:0] */
252 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec >> 16); /* ns[31:16] */
253 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec & 0xffff); /* sec[15:0] */
254 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec >> 16); /* sec[31:16]*/
255
256 ext_write(bc, dev, PAGE4, PTP_CTL, cmd);
257
258 return 0;
259}
260
261/* convert phy timestamps into driver timestamps */
262
263static void phy2rxts(struct phy_rxts *p, struct rxts *rxts)
264{
265 u32 sec;
266
267 sec = p->sec_lo;
268 sec |= p->sec_hi << 16;
269
270 rxts->ns = p->ns_lo;
271 rxts->ns |= (p->ns_hi & 0x3fff) << 16;
272 rxts->ns += ((u64)sec) * 1000000000ULL;
273 rxts->seqid = p->seqid;
274 rxts->msgtype = (p->msgtype >> 12) & 0xf;
275 rxts->hash = p->msgtype & 0x0fff;
Stefan Sørensen4b063252015-11-03 09:34:05 +0100276 rxts->tmo = jiffies + SKB_TIMESTAMP_TIMEOUT;
Richard Cochrancb646e22011-04-22 12:04:55 +0200277}
278
279static u64 phy2txts(struct phy_txts *p)
280{
281 u64 ns;
282 u32 sec;
283
284 sec = p->sec_lo;
285 sec |= p->sec_hi << 16;
286
287 ns = p->ns_lo;
288 ns |= (p->ns_hi & 0x3fff) << 16;
289 ns += ((u64)sec) * 1000000000ULL;
290
291 return ns;
292}
293
Richard Cochran621bdec2014-03-20 22:22:00 +0100294static int periodic_output(struct dp83640_clock *clock,
Stefan Sørensenad015772014-06-27 12:05:30 +0200295 struct ptp_clock_request *clkreq, bool on,
296 int trigger)
Richard Cochran49b3fd42011-09-20 01:43:14 +0000297{
298 struct dp83640_private *dp83640 = clock->chosen;
299 struct phy_device *phydev = dp83640->phydev;
Richard Cochran564ca562014-03-20 22:21:57 +0100300 u32 sec, nsec, pwidth;
Stefan Sørensenad015772014-06-27 12:05:30 +0200301 u16 gpio, ptp_trig, val;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000302
Richard Cochran621bdec2014-03-20 22:22:00 +0100303 if (on) {
Stefan Sørensenad015772014-06-27 12:05:30 +0200304 gpio = 1 + ptp_find_pin(clock->ptp_clock, PTP_PF_PEROUT,
305 trigger);
Richard Cochran621bdec2014-03-20 22:22:00 +0100306 if (gpio < 1)
307 return -EINVAL;
308 } else {
309 gpio = 0;
310 }
311
Richard Cochran49b3fd42011-09-20 01:43:14 +0000312 ptp_trig = TRIG_WR |
313 (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT |
314 (gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT |
315 TRIG_PER |
316 TRIG_PULSE;
317
318 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
319
320 if (!on) {
321 val |= TRIG_DIS;
322 mutex_lock(&clock->extreg_lock);
323 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
324 ext_write(0, phydev, PAGE4, PTP_CTL, val);
325 mutex_unlock(&clock->extreg_lock);
Richard Cochran621bdec2014-03-20 22:22:00 +0100326 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000327 }
328
329 sec = clkreq->perout.start.sec;
330 nsec = clkreq->perout.start.nsec;
Richard Cochran564ca562014-03-20 22:21:57 +0100331 pwidth = clkreq->perout.period.sec * 1000000000UL;
332 pwidth += clkreq->perout.period.nsec;
333 pwidth /= 2;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000334
335 mutex_lock(&clock->extreg_lock);
336
337 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
338
339 /*load trigger*/
340 val |= TRIG_LOAD;
341 ext_write(0, phydev, PAGE4, PTP_CTL, val);
342 ext_write(0, phydev, PAGE4, PTP_TDR, nsec & 0xffff); /* ns[15:0] */
343 ext_write(0, phydev, PAGE4, PTP_TDR, nsec >> 16); /* ns[31:16] */
344 ext_write(0, phydev, PAGE4, PTP_TDR, sec & 0xffff); /* sec[15:0] */
345 ext_write(0, phydev, PAGE4, PTP_TDR, sec >> 16); /* sec[31:16] */
Richard Cochran564ca562014-03-20 22:21:57 +0100346 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff); /* ns[15:0] */
347 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16); /* ns[31:16] */
Stefan Sørensen35e872a2014-06-27 12:05:29 +0200348 /* Triggers 0 and 1 has programmable pulsewidth2 */
349 if (trigger < 2) {
350 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff);
351 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16);
352 }
Richard Cochran49b3fd42011-09-20 01:43:14 +0000353
354 /*enable trigger*/
355 val &= ~TRIG_LOAD;
356 val |= TRIG_EN;
357 ext_write(0, phydev, PAGE4, PTP_CTL, val);
358
359 mutex_unlock(&clock->extreg_lock);
Richard Cochran621bdec2014-03-20 22:22:00 +0100360 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000361}
362
Richard Cochrancb646e22011-04-22 12:04:55 +0200363/* ptp clock methods */
364
Richard Cochrane4788b82016-11-08 22:49:18 +0100365static int ptp_dp83640_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
Richard Cochrancb646e22011-04-22 12:04:55 +0200366{
367 struct dp83640_clock *clock =
368 container_of(ptp, struct dp83640_clock, caps);
369 struct phy_device *phydev = clock->chosen->phydev;
370 u64 rate;
371 int neg_adj = 0;
372 u16 hi, lo;
373
Richard Cochrane4788b82016-11-08 22:49:18 +0100374 if (scaled_ppm < 0) {
Richard Cochrancb646e22011-04-22 12:04:55 +0200375 neg_adj = 1;
Richard Cochrane4788b82016-11-08 22:49:18 +0100376 scaled_ppm = -scaled_ppm;
Richard Cochrancb646e22011-04-22 12:04:55 +0200377 }
Richard Cochrane4788b82016-11-08 22:49:18 +0100378 rate = scaled_ppm;
379 rate <<= 13;
380 rate = div_u64(rate, 15625);
Richard Cochrancb646e22011-04-22 12:04:55 +0200381
382 hi = (rate >> 16) & PTP_RATE_HI_MASK;
383 if (neg_adj)
384 hi |= PTP_RATE_DIR;
385
386 lo = rate & 0xffff;
387
388 mutex_lock(&clock->extreg_lock);
389
390 ext_write(1, phydev, PAGE4, PTP_RATEH, hi);
391 ext_write(1, phydev, PAGE4, PTP_RATEL, lo);
392
393 mutex_unlock(&clock->extreg_lock);
394
395 return 0;
396}
397
398static int ptp_dp83640_adjtime(struct ptp_clock_info *ptp, s64 delta)
399{
400 struct dp83640_clock *clock =
401 container_of(ptp, struct dp83640_clock, caps);
402 struct phy_device *phydev = clock->chosen->phydev;
Richard Cochran41c2c182015-03-29 23:12:10 +0200403 struct timespec64 ts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200404 int err;
405
406 delta += ADJTIME_FIX;
407
Richard Cochran41c2c182015-03-29 23:12:10 +0200408 ts = ns_to_timespec64(delta);
Richard Cochrancb646e22011-04-22 12:04:55 +0200409
410 mutex_lock(&clock->extreg_lock);
411
412 err = tdr_write(1, phydev, &ts, PTP_STEP_CLK);
413
414 mutex_unlock(&clock->extreg_lock);
415
416 return err;
417}
418
Richard Cochran41c2c182015-03-29 23:12:10 +0200419static int ptp_dp83640_gettime(struct ptp_clock_info *ptp,
420 struct timespec64 *ts)
Richard Cochrancb646e22011-04-22 12:04:55 +0200421{
422 struct dp83640_clock *clock =
423 container_of(ptp, struct dp83640_clock, caps);
424 struct phy_device *phydev = clock->chosen->phydev;
425 unsigned int val[4];
426
427 mutex_lock(&clock->extreg_lock);
428
429 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_RD_CLK);
430
431 val[0] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[15:0] */
432 val[1] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[31:16] */
433 val[2] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[15:0] */
434 val[3] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[31:16] */
435
436 mutex_unlock(&clock->extreg_lock);
437
438 ts->tv_nsec = val[0] | (val[1] << 16);
439 ts->tv_sec = val[2] | (val[3] << 16);
440
441 return 0;
442}
443
444static int ptp_dp83640_settime(struct ptp_clock_info *ptp,
Richard Cochran41c2c182015-03-29 23:12:10 +0200445 const struct timespec64 *ts)
Richard Cochrancb646e22011-04-22 12:04:55 +0200446{
447 struct dp83640_clock *clock =
448 container_of(ptp, struct dp83640_clock, caps);
449 struct phy_device *phydev = clock->chosen->phydev;
450 int err;
451
452 mutex_lock(&clock->extreg_lock);
453
454 err = tdr_write(1, phydev, ts, PTP_LOAD_CLK);
455
456 mutex_unlock(&clock->extreg_lock);
457
458 return err;
459}
460
461static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
462 struct ptp_clock_request *rq, int on)
463{
464 struct dp83640_clock *clock =
465 container_of(ptp, struct dp83640_clock, caps);
466 struct phy_device *phydev = clock->chosen->phydev;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100467 unsigned int index;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000468 u16 evnt, event_num, gpio_num;
Richard Cochrancb646e22011-04-22 12:04:55 +0200469
470 switch (rq->type) {
471 case PTP_CLK_REQ_EXTTS:
Richard Cochran49b3fd42011-09-20 01:43:14 +0000472 index = rq->extts.index;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100473 if (index >= N_EXT_TS)
Richard Cochrancb646e22011-04-22 12:04:55 +0200474 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000475 event_num = EXT_EVENT + index;
476 evnt = EVNT_WR | (event_num & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
Richard Cochrancb646e22011-04-22 12:04:55 +0200477 if (on) {
Richard Cochranfaa89712014-03-20 22:21:59 +0100478 gpio_num = 1 + ptp_find_pin(clock->ptp_clock,
479 PTP_PF_EXTTS, index);
480 if (gpio_num < 1)
481 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000482 evnt |= (gpio_num & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
Stefan Sørensen80671bd2014-02-03 15:36:50 +0100483 if (rq->extts.flags & PTP_FALLING_EDGE)
484 evnt |= EVNT_FALL;
485 else
486 evnt |= EVNT_RISE;
Richard Cochrancb646e22011-04-22 12:04:55 +0200487 }
Richard Cochrana9358652015-05-25 11:55:44 +0200488 mutex_lock(&clock->extreg_lock);
Richard Cochrancb646e22011-04-22 12:04:55 +0200489 ext_write(0, phydev, PAGE5, PTP_EVNT, evnt);
Richard Cochrana9358652015-05-25 11:55:44 +0200490 mutex_unlock(&clock->extreg_lock);
Richard Cochrancb646e22011-04-22 12:04:55 +0200491 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000492
493 case PTP_CLK_REQ_PEROUT:
Stefan Sørensenad015772014-06-27 12:05:30 +0200494 if (rq->perout.index >= N_PER_OUT)
Richard Cochran49b3fd42011-09-20 01:43:14 +0000495 return -EINVAL;
Stefan Sørensenad015772014-06-27 12:05:30 +0200496 return periodic_output(clock, rq, on, rq->perout.index);
Richard Cochran49b3fd42011-09-20 01:43:14 +0000497
Richard Cochrancb646e22011-04-22 12:04:55 +0200498 default:
499 break;
500 }
501
502 return -EOPNOTSUPP;
503}
504
Richard Cochran86dd3612014-03-20 22:21:58 +0100505static int ptp_dp83640_verify(struct ptp_clock_info *ptp, unsigned int pin,
506 enum ptp_pin_function func, unsigned int chan)
507{
Stefan Sørensen6f39eb82014-06-27 12:05:31 +0200508 struct dp83640_clock *clock =
509 container_of(ptp, struct dp83640_clock, caps);
510
511 if (clock->caps.pin_config[pin].func == PTP_PF_PHYSYNC &&
512 !list_empty(&clock->phylist))
513 return 1;
514
515 if (func == PTP_PF_PHYSYNC)
516 return 1;
517
Richard Cochran86dd3612014-03-20 22:21:58 +0100518 return 0;
519}
520
Richard Cochrancb646e22011-04-22 12:04:55 +0200521static u8 status_frame_dst[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 };
522static u8 status_frame_src[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F };
523
524static void enable_status_frames(struct phy_device *phydev, bool on)
525{
Richard Cochrana9358652015-05-25 11:55:44 +0200526 struct dp83640_private *dp83640 = phydev->priv;
527 struct dp83640_clock *clock = dp83640->clock;
Richard Cochrancb646e22011-04-22 12:04:55 +0200528 u16 cfg0 = 0, ver;
529
530 if (on)
531 cfg0 = PSF_EVNT_EN | PSF_RXTS_EN | PSF_TXTS_EN | ENDIAN_FLAG;
532
533 ver = (PSF_PTPVER & VERSIONPTP_MASK) << VERSIONPTP_SHIFT;
534
Richard Cochrana9358652015-05-25 11:55:44 +0200535 mutex_lock(&clock->extreg_lock);
536
Richard Cochrancb646e22011-04-22 12:04:55 +0200537 ext_write(0, phydev, PAGE5, PSF_CFG0, cfg0);
538 ext_write(0, phydev, PAGE6, PSF_CFG1, ver);
539
Richard Cochrana9358652015-05-25 11:55:44 +0200540 mutex_unlock(&clock->extreg_lock);
541
Richard Cochrancb646e22011-04-22 12:04:55 +0200542 if (!phydev->attached_dev) {
Andrew Lunnab2a6052018-09-29 23:04:10 +0200543 phydev_warn(phydev,
544 "expected to find an attached netdevice\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200545 return;
546 }
547
548 if (on) {
549 if (dev_mc_add(phydev->attached_dev, status_frame_dst))
Andrew Lunnab2a6052018-09-29 23:04:10 +0200550 phydev_warn(phydev, "failed to add mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200551 } else {
552 if (dev_mc_del(phydev->attached_dev, status_frame_dst))
Andrew Lunnab2a6052018-09-29 23:04:10 +0200553 phydev_warn(phydev, "failed to delete mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200554 }
555}
556
557static bool is_status_frame(struct sk_buff *skb, int type)
558{
559 struct ethhdr *h = eth_hdr(skb);
560
561 if (PTP_CLASS_V2_L2 == type &&
562 !memcmp(h->h_source, status_frame_src, sizeof(status_frame_src)))
563 return true;
564 else
565 return false;
566}
567
568static int expired(struct rxts *rxts)
569{
570 return time_after(jiffies, rxts->tmo);
571}
572
573/* Caller must hold rx_lock. */
574static void prune_rx_ts(struct dp83640_private *dp83640)
575{
576 struct list_head *this, *next;
577 struct rxts *rxts;
578
579 list_for_each_safe(this, next, &dp83640->rxts) {
580 rxts = list_entry(this, struct rxts, list);
581 if (expired(rxts)) {
582 list_del_init(&rxts->list);
583 list_add(&rxts->list, &dp83640->rxpool);
584 }
585 }
586}
587
588/* synchronize the phyters so they act as one clock */
589
590static void enable_broadcast(struct phy_device *phydev, int init_page, int on)
591{
592 int val;
593 phy_write(phydev, PAGESEL, 0);
594 val = phy_read(phydev, PHYCR2);
595 if (on)
596 val |= BC_WRITE;
597 else
598 val &= ~BC_WRITE;
599 phy_write(phydev, PHYCR2, val);
600 phy_write(phydev, PAGESEL, init_page);
601}
602
603static void recalibrate(struct dp83640_clock *clock)
604{
605 s64 now, diff;
606 struct phy_txts event_ts;
Richard Cochran41c2c182015-03-29 23:12:10 +0200607 struct timespec64 ts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200608 struct list_head *this;
609 struct dp83640_private *tmp;
610 struct phy_device *master = clock->chosen->phydev;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000611 u16 cal_gpio, cfg0, evnt, ptp_trig, trigger, val;
Richard Cochrancb646e22011-04-22 12:04:55 +0200612
613 trigger = CAL_TRIGGER;
Stefan Sørensene0155952014-06-27 12:05:32 +0200614 cal_gpio = 1 + ptp_find_pin(clock->ptp_clock, PTP_PF_PHYSYNC, 0);
615 if (cal_gpio < 1) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +0900616 pr_err("PHY calibration pin not available - PHY is not calibrated.");
Stefan Sørensene0155952014-06-27 12:05:32 +0200617 return;
618 }
Richard Cochrancb646e22011-04-22 12:04:55 +0200619
620 mutex_lock(&clock->extreg_lock);
621
622 /*
623 * enable broadcast, disable status frames, enable ptp clock
624 */
625 list_for_each(this, &clock->phylist) {
626 tmp = list_entry(this, struct dp83640_private, list);
627 enable_broadcast(tmp->phydev, clock->page, 1);
628 tmp->cfg0 = ext_read(tmp->phydev, PAGE5, PSF_CFG0);
629 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, 0);
630 ext_write(0, tmp->phydev, PAGE4, PTP_CTL, PTP_ENABLE);
631 }
632 enable_broadcast(master, clock->page, 1);
633 cfg0 = ext_read(master, PAGE5, PSF_CFG0);
634 ext_write(0, master, PAGE5, PSF_CFG0, 0);
635 ext_write(0, master, PAGE4, PTP_CTL, PTP_ENABLE);
636
637 /*
638 * enable an event timestamp
639 */
640 evnt = EVNT_WR | EVNT_RISE | EVNT_SINGLE;
641 evnt |= (CAL_EVENT & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
642 evnt |= (cal_gpio & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
643
644 list_for_each(this, &clock->phylist) {
645 tmp = list_entry(this, struct dp83640_private, list);
646 ext_write(0, tmp->phydev, PAGE5, PTP_EVNT, evnt);
647 }
648 ext_write(0, master, PAGE5, PTP_EVNT, evnt);
649
650 /*
651 * configure a trigger
652 */
653 ptp_trig = TRIG_WR | TRIG_IF_LATE | TRIG_PULSE;
654 ptp_trig |= (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT;
655 ptp_trig |= (cal_gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT;
656 ext_write(0, master, PAGE5, PTP_TRIG, ptp_trig);
657
658 /* load trigger */
659 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
660 val |= TRIG_LOAD;
661 ext_write(0, master, PAGE4, PTP_CTL, val);
662
663 /* enable trigger */
664 val &= ~TRIG_LOAD;
665 val |= TRIG_EN;
666 ext_write(0, master, PAGE4, PTP_CTL, val);
667
668 /* disable trigger */
669 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
670 val |= TRIG_DIS;
671 ext_write(0, master, PAGE4, PTP_CTL, val);
672
673 /*
674 * read out and correct offsets
675 */
676 val = ext_read(master, PAGE4, PTP_STS);
Andrew Lunnc4fabb82018-09-29 23:04:11 +0200677 phydev_info(master, "master PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200678 val = ext_read(master, PAGE4, PTP_ESTS);
Andrew Lunnc4fabb82018-09-29 23:04:11 +0200679 phydev_info(master, "master PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200680 event_ts.ns_lo = ext_read(master, PAGE4, PTP_EDATA);
681 event_ts.ns_hi = ext_read(master, PAGE4, PTP_EDATA);
682 event_ts.sec_lo = ext_read(master, PAGE4, PTP_EDATA);
683 event_ts.sec_hi = ext_read(master, PAGE4, PTP_EDATA);
684 now = phy2txts(&event_ts);
685
686 list_for_each(this, &clock->phylist) {
687 tmp = list_entry(this, struct dp83640_private, list);
688 val = ext_read(tmp->phydev, PAGE4, PTP_STS);
Andrew Lunnc4fabb82018-09-29 23:04:11 +0200689 phydev_info(tmp->phydev, "slave PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200690 val = ext_read(tmp->phydev, PAGE4, PTP_ESTS);
Andrew Lunnc4fabb82018-09-29 23:04:11 +0200691 phydev_info(tmp->phydev, "slave PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200692 event_ts.ns_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
693 event_ts.ns_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
694 event_ts.sec_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
695 event_ts.sec_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
696 diff = now - (s64) phy2txts(&event_ts);
Andrew Lunnc4fabb82018-09-29 23:04:11 +0200697 phydev_info(tmp->phydev, "slave offset %lld nanoseconds\n",
698 diff);
Richard Cochrancb646e22011-04-22 12:04:55 +0200699 diff += ADJTIME_FIX;
Richard Cochran41c2c182015-03-29 23:12:10 +0200700 ts = ns_to_timespec64(diff);
Richard Cochrancb646e22011-04-22 12:04:55 +0200701 tdr_write(0, tmp->phydev, &ts, PTP_STEP_CLK);
702 }
703
704 /*
705 * restore status frames
706 */
707 list_for_each(this, &clock->phylist) {
708 tmp = list_entry(this, struct dp83640_private, list);
709 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, tmp->cfg0);
710 }
711 ext_write(0, master, PAGE5, PSF_CFG0, cfg0);
712
713 mutex_unlock(&clock->extreg_lock);
714}
715
716/* time stamping methods */
717
Richard Cochran49b3fd42011-09-20 01:43:14 +0000718static inline u16 exts_chan_to_edata(int ch)
719{
720 return 1 << ((ch + EXT_EVENT) * 2);
721}
722
Richard Cochran23310382011-06-14 23:55:19 +0000723static int decode_evnt(struct dp83640_private *dp83640,
Christian Riesch13322f22014-08-21 15:17:04 +0200724 void *data, int len, u16 ests)
Richard Cochrancb646e22011-04-22 12:04:55 +0200725{
Richard Cochran23310382011-06-14 23:55:19 +0000726 struct phy_txts *phy_txts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200727 struct ptp_clock_event event;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000728 int i, parsed;
Richard Cochrancb646e22011-04-22 12:04:55 +0200729 int words = (ests >> EVNT_TS_LEN_SHIFT) & EVNT_TS_LEN_MASK;
Richard Cochran23310382011-06-14 23:55:19 +0000730 u16 ext_status = 0;
731
Christian Riesch13322f22014-08-21 15:17:04 +0200732 /* calculate length of the event timestamp status message */
733 if (ests & MULT_EVNT)
734 parsed = (words + 2) * sizeof(u16);
735 else
736 parsed = (words + 1) * sizeof(u16);
737
738 /* check if enough data is available */
739 if (len < parsed)
740 return len;
741
Richard Cochran23310382011-06-14 23:55:19 +0000742 if (ests & MULT_EVNT) {
743 ext_status = *(u16 *) data;
744 data += sizeof(ext_status);
745 }
746
747 phy_txts = data;
Richard Cochrancb646e22011-04-22 12:04:55 +0200748
Gustavo A. R. Silvad331e752018-08-09 10:08:24 -0500749 switch (words) {
Richard Cochrancb646e22011-04-22 12:04:55 +0200750 case 3:
751 dp83640->edata.sec_hi = phy_txts->sec_hi;
Gustavo A. R. Silvad331e752018-08-09 10:08:24 -0500752 /* fall through */
Richard Cochrancb646e22011-04-22 12:04:55 +0200753 case 2:
754 dp83640->edata.sec_lo = phy_txts->sec_lo;
Gustavo A. R. Silvad331e752018-08-09 10:08:24 -0500755 /* fall through */
Richard Cochrancb646e22011-04-22 12:04:55 +0200756 case 1:
757 dp83640->edata.ns_hi = phy_txts->ns_hi;
Gustavo A. R. Silvad331e752018-08-09 10:08:24 -0500758 /* fall through */
Richard Cochrancb646e22011-04-22 12:04:55 +0200759 case 0:
760 dp83640->edata.ns_lo = phy_txts->ns_lo;
761 }
762
Christian Riesch13322f22014-08-21 15:17:04 +0200763 if (!ext_status) {
Richard Cochran49b3fd42011-09-20 01:43:14 +0000764 i = ((ests >> EVNT_NUM_SHIFT) & EVNT_NUM_MASK) - EXT_EVENT;
765 ext_status = exts_chan_to_edata(i);
766 }
767
Richard Cochrancb646e22011-04-22 12:04:55 +0200768 event.type = PTP_CLOCK_EXTTS;
Richard Cochrancb646e22011-04-22 12:04:55 +0200769 event.timestamp = phy2txts(&dp83640->edata);
770
Stefan Sørensena0077a92014-07-11 08:18:26 +0200771 /* Compensate for input path and synchronization delays */
772 event.timestamp -= 35;
773
Richard Cochran49b3fd42011-09-20 01:43:14 +0000774 for (i = 0; i < N_EXT_TS; i++) {
775 if (ext_status & exts_chan_to_edata(i)) {
776 event.index = i;
777 ptp_clock_event(dp83640->clock->ptp_clock, &event);
778 }
779 }
Richard Cochran23310382011-06-14 23:55:19 +0000780
Christian Riesch13322f22014-08-21 15:17:04 +0200781 return parsed;
Richard Cochrancb646e22011-04-22 12:04:55 +0200782}
783
Stefan Sørensen539e44d2015-11-03 09:34:04 +0100784#define DP83640_PACKET_HASH_OFFSET 20
785#define DP83640_PACKET_HASH_LEN 10
786
Stefan Sørensen63502b82014-07-22 15:20:45 +0200787static int match(struct sk_buff *skb, unsigned int type, struct rxts *rxts)
788{
Stefan Sørensen539e44d2015-11-03 09:34:04 +0100789 u16 *seqid, hash;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200790 unsigned int offset = 0;
791 u8 *msgtype, *data = skb_mac_header(skb);
792
793 /* check sequenceID, messageType, 12 bit hash of offset 20-29 */
794
795 if (type & PTP_CLASS_VLAN)
796 offset += VLAN_HLEN;
797
798 switch (type & PTP_CLASS_PMASK) {
799 case PTP_CLASS_IPV4:
Richard Cochrancca04b22014-11-12 11:33:52 +0100800 offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200801 break;
802 case PTP_CLASS_IPV6:
803 offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
804 break;
805 case PTP_CLASS_L2:
806 offset += ETH_HLEN;
807 break;
808 default:
809 return 0;
810 }
811
812 if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
813 return 0;
814
815 if (unlikely(type & PTP_CLASS_V1))
816 msgtype = data + offset + OFF_PTP_CONTROL;
817 else
818 msgtype = data + offset;
Stefan Sørensen539e44d2015-11-03 09:34:04 +0100819 if (rxts->msgtype != (*msgtype & 0xf))
820 return 0;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200821
822 seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
Stefan Sørensen539e44d2015-11-03 09:34:04 +0100823 if (rxts->seqid != ntohs(*seqid))
824 return 0;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200825
Stefan Sørensen539e44d2015-11-03 09:34:04 +0100826 hash = ether_crc(DP83640_PACKET_HASH_LEN,
827 data + offset + DP83640_PACKET_HASH_OFFSET) >> 20;
828 if (rxts->hash != hash)
829 return 0;
830
831 return 1;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200832}
833
Richard Cochrancb646e22011-04-22 12:04:55 +0200834static void decode_rxts(struct dp83640_private *dp83640,
835 struct phy_rxts *phy_rxts)
836{
837 struct rxts *rxts;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200838 struct skb_shared_hwtstamps *shhwtstamps = NULL;
839 struct sk_buff *skb;
Richard Cochrancb646e22011-04-22 12:04:55 +0200840 unsigned long flags;
Manfred Rudigier81e8f2e2016-01-20 11:22:28 +0100841 u8 overflow;
842
843 overflow = (phy_rxts->ns_hi >> 14) & 0x3;
844 if (overflow)
845 pr_debug("rx timestamp queue overflow, count %d\n", overflow);
Richard Cochrancb646e22011-04-22 12:04:55 +0200846
847 spin_lock_irqsave(&dp83640->rx_lock, flags);
848
849 prune_rx_ts(dp83640);
850
851 if (list_empty(&dp83640->rxpool)) {
Joe Perches8d242482012-06-09 07:49:07 +0000852 pr_debug("rx timestamp pool is empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200853 goto out;
854 }
855 rxts = list_first_entry(&dp83640->rxpool, struct rxts, list);
856 list_del_init(&rxts->list);
857 phy2rxts(phy_rxts, rxts);
Stefan Sørensen63502b82014-07-22 15:20:45 +0200858
Richard Cochranadbe0882015-05-25 11:55:45 +0200859 spin_lock(&dp83640->rx_queue.lock);
Stefan Sørensen63502b82014-07-22 15:20:45 +0200860 skb_queue_walk(&dp83640->rx_queue, skb) {
861 struct dp83640_skb_info *skb_info;
862
863 skb_info = (struct dp83640_skb_info *)skb->cb;
864 if (match(skb, skb_info->ptp_type, rxts)) {
865 __skb_unlink(skb, &dp83640->rx_queue);
866 shhwtstamps = skb_hwtstamps(skb);
867 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
868 shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
Stefan Sørensen63502b82014-07-22 15:20:45 +0200869 list_add(&rxts->list, &dp83640->rxpool);
870 break;
871 }
872 }
Richard Cochranadbe0882015-05-25 11:55:45 +0200873 spin_unlock(&dp83640->rx_queue.lock);
Stefan Sørensen63502b82014-07-22 15:20:45 +0200874
875 if (!shhwtstamps)
876 list_add_tail(&rxts->list, &dp83640->rxts);
Richard Cochrancb646e22011-04-22 12:04:55 +0200877out:
878 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
Stefan Sørensend36b82b2017-08-30 08:58:47 +0200879
880 if (shhwtstamps)
881 netif_rx_ni(skb);
Richard Cochrancb646e22011-04-22 12:04:55 +0200882}
883
884static void decode_txts(struct dp83640_private *dp83640,
885 struct phy_txts *phy_txts)
886{
887 struct skb_shared_hwtstamps shhwtstamps;
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +0100888 struct dp83640_skb_info *skb_info;
Richard Cochrancb646e22011-04-22 12:04:55 +0200889 struct sk_buff *skb;
Manfred Rudigier81e8f2e2016-01-20 11:22:28 +0100890 u8 overflow;
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +0100891 u64 ns;
Richard Cochrancb646e22011-04-22 12:04:55 +0200892
893 /* We must already have the skb that triggered this. */
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +0100894again:
Richard Cochrancb646e22011-04-22 12:04:55 +0200895 skb = skb_dequeue(&dp83640->tx_queue);
Richard Cochrancb646e22011-04-22 12:04:55 +0200896 if (!skb) {
Joe Perches8d242482012-06-09 07:49:07 +0000897 pr_debug("have timestamp but tx_queue empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200898 return;
899 }
Manfred Rudigier81e8f2e2016-01-20 11:22:28 +0100900
901 overflow = (phy_txts->ns_hi >> 14) & 0x3;
902 if (overflow) {
903 pr_debug("tx timestamp queue overflow, count %d\n", overflow);
904 while (skb) {
Richard Cochrandb9d8b22017-06-23 17:51:31 +0200905 kfree_skb(skb);
Manfred Rudigier81e8f2e2016-01-20 11:22:28 +0100906 skb = skb_dequeue(&dp83640->tx_queue);
907 }
908 return;
909 }
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +0100910 skb_info = (struct dp83640_skb_info *)skb->cb;
911 if (time_after(jiffies, skb_info->tmo)) {
912 kfree_skb(skb);
913 goto again;
914 }
Manfred Rudigier81e8f2e2016-01-20 11:22:28 +0100915
Richard Cochrancb646e22011-04-22 12:04:55 +0200916 ns = phy2txts(phy_txts);
917 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
918 shhwtstamps.hwtstamp = ns_to_ktime(ns);
919 skb_complete_tx_timestamp(skb, &shhwtstamps);
920}
921
922static void decode_status_frame(struct dp83640_private *dp83640,
923 struct sk_buff *skb)
924{
925 struct phy_rxts *phy_rxts;
926 struct phy_txts *phy_txts;
927 u8 *ptr;
928 int len, size;
929 u16 ests, type;
930
931 ptr = skb->data + 2;
932
933 for (len = skb_headlen(skb) - 2; len > sizeof(type); len -= size) {
934
935 type = *(u16 *)ptr;
936 ests = type & 0x0fff;
937 type = type & 0xf000;
938 len -= sizeof(type);
939 ptr += sizeof(type);
940
941 if (PSF_RX == type && len >= sizeof(*phy_rxts)) {
942
943 phy_rxts = (struct phy_rxts *) ptr;
944 decode_rxts(dp83640, phy_rxts);
945 size = sizeof(*phy_rxts);
946
947 } else if (PSF_TX == type && len >= sizeof(*phy_txts)) {
948
949 phy_txts = (struct phy_txts *) ptr;
950 decode_txts(dp83640, phy_txts);
951 size = sizeof(*phy_txts);
952
Christian Riesch13322f22014-08-21 15:17:04 +0200953 } else if (PSF_EVNT == type) {
Richard Cochrancb646e22011-04-22 12:04:55 +0200954
Christian Riesch13322f22014-08-21 15:17:04 +0200955 size = decode_evnt(dp83640, ptr, len, ests);
Richard Cochrancb646e22011-04-22 12:04:55 +0200956
957 } else {
958 size = 0;
959 break;
960 }
961 ptr += size;
962 }
963}
964
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000965static int is_sync(struct sk_buff *skb, int type)
966{
967 u8 *data = skb->data, *msgtype;
968 unsigned int offset = 0;
969
Stefan Sørensenae5c6c62014-06-27 11:59:10 +0200970 if (type & PTP_CLASS_VLAN)
971 offset += VLAN_HLEN;
972
973 switch (type & PTP_CLASS_PMASK) {
974 case PTP_CLASS_IPV4:
Richard Cochrancca04b22014-11-12 11:33:52 +0100975 offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000976 break;
Stefan Sørensenae5c6c62014-06-27 11:59:10 +0200977 case PTP_CLASS_IPV6:
978 offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000979 break;
Stefan Sørensenae5c6c62014-06-27 11:59:10 +0200980 case PTP_CLASS_L2:
981 offset += ETH_HLEN;
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000982 break;
983 default:
984 return 0;
985 }
986
987 if (type & PTP_CLASS_V1)
988 offset += OFF_PTP_CONTROL;
989
990 if (skb->len < offset + 1)
991 return 0;
992
993 msgtype = data + offset;
994
995 return (*msgtype & 0xf) == 0;
996}
997
Richard Cochrancb646e22011-04-22 12:04:55 +0200998static void dp83640_free_clocks(void)
999{
1000 struct dp83640_clock *clock;
1001 struct list_head *this, *next;
1002
1003 mutex_lock(&phyter_clocks_lock);
1004
1005 list_for_each_safe(this, next, &phyter_clocks) {
1006 clock = list_entry(this, struct dp83640_clock, list);
1007 if (!list_empty(&clock->phylist)) {
Joe Perches8d242482012-06-09 07:49:07 +00001008 pr_warn("phy list non-empty while unloading\n");
Richard Cochrancb646e22011-04-22 12:04:55 +02001009 BUG();
1010 }
1011 list_del(&clock->list);
1012 mutex_destroy(&clock->extreg_lock);
1013 mutex_destroy(&clock->clock_lock);
1014 put_device(&clock->bus->dev);
Richard Cochran86dd3612014-03-20 22:21:58 +01001015 kfree(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +02001016 kfree(clock);
1017 }
1018
1019 mutex_unlock(&phyter_clocks_lock);
1020}
1021
1022static void dp83640_clock_init(struct dp83640_clock *clock, struct mii_bus *bus)
1023{
1024 INIT_LIST_HEAD(&clock->list);
1025 clock->bus = bus;
1026 mutex_init(&clock->extreg_lock);
1027 mutex_init(&clock->clock_lock);
1028 INIT_LIST_HEAD(&clock->phylist);
1029 clock->caps.owner = THIS_MODULE;
1030 sprintf(clock->caps.name, "dp83640 timer");
1031 clock->caps.max_adj = 1953124;
1032 clock->caps.n_alarm = 0;
1033 clock->caps.n_ext_ts = N_EXT_TS;
Stefan Sørensenad015772014-06-27 12:05:30 +02001034 clock->caps.n_per_out = N_PER_OUT;
Richard Cochran86dd3612014-03-20 22:21:58 +01001035 clock->caps.n_pins = DP83640_N_PINS;
Richard Cochrancb646e22011-04-22 12:04:55 +02001036 clock->caps.pps = 0;
Richard Cochrane4788b82016-11-08 22:49:18 +01001037 clock->caps.adjfine = ptp_dp83640_adjfine;
Richard Cochrancb646e22011-04-22 12:04:55 +02001038 clock->caps.adjtime = ptp_dp83640_adjtime;
Richard Cochran41c2c182015-03-29 23:12:10 +02001039 clock->caps.gettime64 = ptp_dp83640_gettime;
1040 clock->caps.settime64 = ptp_dp83640_settime;
Richard Cochrancb646e22011-04-22 12:04:55 +02001041 clock->caps.enable = ptp_dp83640_enable;
Richard Cochran86dd3612014-03-20 22:21:58 +01001042 clock->caps.verify = ptp_dp83640_verify;
1043 /*
1044 * Convert the module param defaults into a dynamic pin configuration.
1045 */
1046 dp83640_gpio_defaults(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +02001047 /*
1048 * Get a reference to this bus instance.
1049 */
1050 get_device(&bus->dev);
1051}
1052
1053static int choose_this_phy(struct dp83640_clock *clock,
1054 struct phy_device *phydev)
1055{
1056 if (chosen_phy == -1 && !clock->chosen)
1057 return 1;
1058
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001059 if (chosen_phy == phydev->mdio.addr)
Richard Cochrancb646e22011-04-22 12:04:55 +02001060 return 1;
1061
1062 return 0;
1063}
1064
1065static struct dp83640_clock *dp83640_clock_get(struct dp83640_clock *clock)
1066{
1067 if (clock)
1068 mutex_lock(&clock->clock_lock);
1069 return clock;
1070}
1071
1072/*
1073 * Look up and lock a clock by bus instance.
1074 * If there is no clock for this bus, then create it first.
1075 */
1076static struct dp83640_clock *dp83640_clock_get_bus(struct mii_bus *bus)
1077{
1078 struct dp83640_clock *clock = NULL, *tmp;
1079 struct list_head *this;
1080
1081 mutex_lock(&phyter_clocks_lock);
1082
1083 list_for_each(this, &phyter_clocks) {
1084 tmp = list_entry(this, struct dp83640_clock, list);
1085 if (tmp->bus == bus) {
1086 clock = tmp;
1087 break;
1088 }
1089 }
1090 if (clock)
1091 goto out;
1092
1093 clock = kzalloc(sizeof(struct dp83640_clock), GFP_KERNEL);
1094 if (!clock)
1095 goto out;
1096
Kees Cook6396bb22018-06-12 14:03:40 -07001097 clock->caps.pin_config = kcalloc(DP83640_N_PINS,
1098 sizeof(struct ptp_pin_desc),
1099 GFP_KERNEL);
Richard Cochran86dd3612014-03-20 22:21:58 +01001100 if (!clock->caps.pin_config) {
1101 kfree(clock);
1102 clock = NULL;
1103 goto out;
1104 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001105 dp83640_clock_init(clock, bus);
1106 list_add_tail(&phyter_clocks, &clock->list);
1107out:
1108 mutex_unlock(&phyter_clocks_lock);
1109
1110 return dp83640_clock_get(clock);
1111}
1112
1113static void dp83640_clock_put(struct dp83640_clock *clock)
1114{
1115 mutex_unlock(&clock->clock_lock);
1116}
1117
1118static int dp83640_probe(struct phy_device *phydev)
1119{
1120 struct dp83640_clock *clock;
1121 struct dp83640_private *dp83640;
1122 int err = -ENOMEM, i;
1123
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001124 if (phydev->mdio.addr == BROADCAST_ADDR)
Richard Cochrancb646e22011-04-22 12:04:55 +02001125 return 0;
1126
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001127 clock = dp83640_clock_get_bus(phydev->mdio.bus);
Richard Cochrancb646e22011-04-22 12:04:55 +02001128 if (!clock)
1129 goto no_clock;
1130
1131 dp83640 = kzalloc(sizeof(struct dp83640_private), GFP_KERNEL);
1132 if (!dp83640)
1133 goto no_memory;
1134
1135 dp83640->phydev = phydev;
Stefan Sørensen4b063252015-11-03 09:34:05 +01001136 INIT_DELAYED_WORK(&dp83640->ts_work, rx_timestamp_work);
Richard Cochrancb646e22011-04-22 12:04:55 +02001137
1138 INIT_LIST_HEAD(&dp83640->rxts);
1139 INIT_LIST_HEAD(&dp83640->rxpool);
1140 for (i = 0; i < MAX_RXTS; i++)
1141 list_add(&dp83640->rx_pool_data[i].list, &dp83640->rxpool);
1142
1143 phydev->priv = dp83640;
1144
1145 spin_lock_init(&dp83640->rx_lock);
1146 skb_queue_head_init(&dp83640->rx_queue);
1147 skb_queue_head_init(&dp83640->tx_queue);
1148
1149 dp83640->clock = clock;
1150
1151 if (choose_this_phy(clock, phydev)) {
1152 clock->chosen = dp83640;
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001153 clock->ptp_clock = ptp_clock_register(&clock->caps,
1154 &phydev->mdio.dev);
Richard Cochrancb646e22011-04-22 12:04:55 +02001155 if (IS_ERR(clock->ptp_clock)) {
1156 err = PTR_ERR(clock->ptp_clock);
1157 goto no_register;
1158 }
1159 } else
1160 list_add_tail(&dp83640->list, &clock->phylist);
1161
Richard Cochrancb646e22011-04-22 12:04:55 +02001162 dp83640_clock_put(clock);
1163 return 0;
1164
1165no_register:
1166 clock->chosen = NULL;
1167 kfree(dp83640);
1168no_memory:
1169 dp83640_clock_put(clock);
1170no_clock:
1171 return err;
1172}
1173
1174static void dp83640_remove(struct phy_device *phydev)
1175{
1176 struct dp83640_clock *clock;
1177 struct list_head *this, *next;
1178 struct dp83640_private *tmp, *dp83640 = phydev->priv;
1179
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001180 if (phydev->mdio.addr == BROADCAST_ADDR)
Richard Cochrancb646e22011-04-22 12:04:55 +02001181 return;
1182
1183 enable_status_frames(phydev, false);
Stefan Sørensen4b063252015-11-03 09:34:05 +01001184 cancel_delayed_work_sync(&dp83640->ts_work);
Richard Cochrancb646e22011-04-22 12:04:55 +02001185
Alexander Duyckdb91b722014-09-08 11:25:34 -04001186 skb_queue_purge(&dp83640->rx_queue);
1187 skb_queue_purge(&dp83640->tx_queue);
Richard Cochran8b3408f2011-10-21 00:49:17 +00001188
Richard Cochrancb646e22011-04-22 12:04:55 +02001189 clock = dp83640_clock_get(dp83640->clock);
1190
1191 if (dp83640 == clock->chosen) {
1192 ptp_clock_unregister(clock->ptp_clock);
1193 clock->chosen = NULL;
1194 } else {
1195 list_for_each_safe(this, next, &clock->phylist) {
1196 tmp = list_entry(this, struct dp83640_private, list);
1197 if (tmp == dp83640) {
1198 list_del_init(&tmp->list);
1199 break;
1200 }
1201 }
1202 }
1203
1204 dp83640_clock_put(clock);
1205 kfree(dp83640);
1206}
1207
Esben Haabendal76327a32018-04-08 22:17:01 +02001208static int dp83640_soft_reset(struct phy_device *phydev)
1209{
1210 int ret;
1211
1212 ret = genphy_soft_reset(phydev);
1213 if (ret < 0)
1214 return ret;
1215
1216 /* From DP83640 datasheet: "Software driver code must wait 3 us
1217 * following a software reset before allowing further serial MII
1218 * operations with the DP83640."
1219 */
1220 udelay(10); /* Taking udelay inaccuracy into account */
1221
1222 return 0;
1223}
1224
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001225static int dp83640_config_init(struct phy_device *phydev)
1226{
Stefan Sørensen602b1092014-02-13 15:26:57 +01001227 struct dp83640_private *dp83640 = phydev->priv;
1228 struct dp83640_clock *clock = dp83640->clock;
1229
1230 if (clock->chosen && !list_empty(&clock->phylist))
1231 recalibrate(clock);
Richard Cochrana9358652015-05-25 11:55:44 +02001232 else {
1233 mutex_lock(&clock->extreg_lock);
Stefan Sørensen602b1092014-02-13 15:26:57 +01001234 enable_broadcast(phydev, clock->page, 1);
Richard Cochrana9358652015-05-25 11:55:44 +02001235 mutex_unlock(&clock->extreg_lock);
1236 }
Stefan Sørensen602b1092014-02-13 15:26:57 +01001237
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001238 enable_status_frames(phydev, true);
Richard Cochrana9358652015-05-25 11:55:44 +02001239
1240 mutex_lock(&clock->extreg_lock);
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001241 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_ENABLE);
Richard Cochrana9358652015-05-25 11:55:44 +02001242 mutex_unlock(&clock->extreg_lock);
1243
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001244 return 0;
1245}
1246
Stephan Gatzka16421822012-12-04 10:21:38 +00001247static int dp83640_ack_interrupt(struct phy_device *phydev)
1248{
1249 int err = phy_read(phydev, MII_DP83640_MISR);
1250
1251 if (err < 0)
1252 return err;
1253
1254 return 0;
1255}
1256
1257static int dp83640_config_intr(struct phy_device *phydev)
1258{
1259 int micr;
1260 int misr;
1261 int err;
1262
1263 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1264 misr = phy_read(phydev, MII_DP83640_MISR);
1265 if (misr < 0)
1266 return misr;
1267 misr |=
1268 (MII_DP83640_MISR_ANC_INT_EN |
1269 MII_DP83640_MISR_DUP_INT_EN |
1270 MII_DP83640_MISR_SPD_INT_EN |
1271 MII_DP83640_MISR_LINK_INT_EN);
1272 err = phy_write(phydev, MII_DP83640_MISR, misr);
1273 if (err < 0)
1274 return err;
1275
1276 micr = phy_read(phydev, MII_DP83640_MICR);
1277 if (micr < 0)
1278 return micr;
1279 micr |=
1280 (MII_DP83640_MICR_OE |
1281 MII_DP83640_MICR_IE);
1282 return phy_write(phydev, MII_DP83640_MICR, micr);
1283 } else {
1284 micr = phy_read(phydev, MII_DP83640_MICR);
1285 if (micr < 0)
1286 return micr;
1287 micr &=
1288 ~(MII_DP83640_MICR_OE |
1289 MII_DP83640_MICR_IE);
1290 err = phy_write(phydev, MII_DP83640_MICR, micr);
1291 if (err < 0)
1292 return err;
1293
1294 misr = phy_read(phydev, MII_DP83640_MISR);
1295 if (misr < 0)
1296 return misr;
1297 misr &=
1298 ~(MII_DP83640_MISR_ANC_INT_EN |
1299 MII_DP83640_MISR_DUP_INT_EN |
1300 MII_DP83640_MISR_SPD_INT_EN |
1301 MII_DP83640_MISR_LINK_INT_EN);
1302 return phy_write(phydev, MII_DP83640_MISR, misr);
1303 }
1304}
1305
Richard Cochrancb646e22011-04-22 12:04:55 +02001306static int dp83640_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
1307{
1308 struct dp83640_private *dp83640 = phydev->priv;
1309 struct hwtstamp_config cfg;
1310 u16 txcfg0, rxcfg0;
1311
1312 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1313 return -EFAULT;
1314
1315 if (cfg.flags) /* reserved for future extensions */
1316 return -EINVAL;
1317
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001318 if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ONESTEP_SYNC)
Richard Cochrancb646e22011-04-22 12:04:55 +02001319 return -ERANGE;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001320
1321 dp83640->hwts_tx_en = cfg.tx_type;
Richard Cochrancb646e22011-04-22 12:04:55 +02001322
1323 switch (cfg.rx_filter) {
1324 case HWTSTAMP_FILTER_NONE:
1325 dp83640->hwts_rx_en = 0;
1326 dp83640->layer = 0;
1327 dp83640->version = 0;
1328 break;
1329 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1330 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1331 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1332 dp83640->hwts_rx_en = 1;
Stefan Sørensena1f87232015-11-03 09:34:08 +01001333 dp83640->layer = PTP_CLASS_L4;
1334 dp83640->version = PTP_CLASS_V1;
Richard Cochrancb646e22011-04-22 12:04:55 +02001335 break;
1336 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1337 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1338 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1339 dp83640->hwts_rx_en = 1;
Stefan Sørensena1f87232015-11-03 09:34:08 +01001340 dp83640->layer = PTP_CLASS_L4;
1341 dp83640->version = PTP_CLASS_V2;
Richard Cochrancb646e22011-04-22 12:04:55 +02001342 break;
1343 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1344 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1345 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1346 dp83640->hwts_rx_en = 1;
Stefan Sørensena1f87232015-11-03 09:34:08 +01001347 dp83640->layer = PTP_CLASS_L2;
1348 dp83640->version = PTP_CLASS_V2;
Richard Cochrancb646e22011-04-22 12:04:55 +02001349 break;
1350 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1351 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1352 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1353 dp83640->hwts_rx_en = 1;
Stefan Sørensena1f87232015-11-03 09:34:08 +01001354 dp83640->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
1355 dp83640->version = PTP_CLASS_V2;
Richard Cochrancb646e22011-04-22 12:04:55 +02001356 break;
1357 default:
1358 return -ERANGE;
1359 }
1360
1361 txcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1362 rxcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1363
Stefan Sørensena1f87232015-11-03 09:34:08 +01001364 if (dp83640->layer & PTP_CLASS_L2) {
Richard Cochrancb646e22011-04-22 12:04:55 +02001365 txcfg0 |= TX_L2_EN;
1366 rxcfg0 |= RX_L2_EN;
1367 }
Stefan Sørensena1f87232015-11-03 09:34:08 +01001368 if (dp83640->layer & PTP_CLASS_L4) {
Richard Cochrancb646e22011-04-22 12:04:55 +02001369 txcfg0 |= TX_IPV6_EN | TX_IPV4_EN;
1370 rxcfg0 |= RX_IPV6_EN | RX_IPV4_EN;
1371 }
1372
1373 if (dp83640->hwts_tx_en)
1374 txcfg0 |= TX_TS_EN;
1375
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001376 if (dp83640->hwts_tx_en == HWTSTAMP_TX_ONESTEP_SYNC)
1377 txcfg0 |= SYNC_1STEP | CHK_1STEP;
1378
Richard Cochrancb646e22011-04-22 12:04:55 +02001379 if (dp83640->hwts_rx_en)
1380 rxcfg0 |= RX_TS_EN;
1381
1382 mutex_lock(&dp83640->clock->extreg_lock);
1383
Richard Cochrancb646e22011-04-22 12:04:55 +02001384 ext_write(0, phydev, PAGE5, PTP_TXCFG0, txcfg0);
1385 ext_write(0, phydev, PAGE5, PTP_RXCFG0, rxcfg0);
1386
1387 mutex_unlock(&dp83640->clock->extreg_lock);
1388
1389 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1390}
1391
1392static void rx_timestamp_work(struct work_struct *work)
1393{
1394 struct dp83640_private *dp83640 =
Stefan Sørensen4b063252015-11-03 09:34:05 +01001395 container_of(work, struct dp83640_private, ts_work.work);
Richard Cochrancb646e22011-04-22 12:04:55 +02001396 struct sk_buff *skb;
Richard Cochrancb646e22011-04-22 12:04:55 +02001397
Stefan Sørensen63502b82014-07-22 15:20:45 +02001398 /* Deliver expired packets. */
1399 while ((skb = skb_dequeue(&dp83640->rx_queue))) {
1400 struct dp83640_skb_info *skb_info;
Richard Cochrancb646e22011-04-22 12:04:55 +02001401
Stefan Sørensen63502b82014-07-22 15:20:45 +02001402 skb_info = (struct dp83640_skb_info *)skb->cb;
1403 if (!time_after(jiffies, skb_info->tmo)) {
1404 skb_queue_head(&dp83640->rx_queue, skb);
1405 break;
Richard Cochrancb646e22011-04-22 12:04:55 +02001406 }
Stefan Sørensen63502b82014-07-22 15:20:45 +02001407
Manfred Rudigier72092cc2012-01-09 23:52:15 +00001408 netif_rx_ni(skb);
Richard Cochrancb646e22011-04-22 12:04:55 +02001409 }
1410
Stefan Sørensen63502b82014-07-22 15:20:45 +02001411 if (!skb_queue_empty(&dp83640->rx_queue))
Stefan Sørensen4b063252015-11-03 09:34:05 +01001412 schedule_delayed_work(&dp83640->ts_work, SKB_TIMESTAMP_TIMEOUT);
Richard Cochrancb646e22011-04-22 12:04:55 +02001413}
1414
1415static bool dp83640_rxtstamp(struct phy_device *phydev,
1416 struct sk_buff *skb, int type)
1417{
1418 struct dp83640_private *dp83640 = phydev->priv;
Stefan Sørensen63502b82014-07-22 15:20:45 +02001419 struct dp83640_skb_info *skb_info = (struct dp83640_skb_info *)skb->cb;
1420 struct list_head *this, *next;
1421 struct rxts *rxts;
1422 struct skb_shared_hwtstamps *shhwtstamps = NULL;
1423 unsigned long flags;
Richard Cochrancb646e22011-04-22 12:04:55 +02001424
Richard Cochrancb646e22011-04-22 12:04:55 +02001425 if (is_status_frame(skb, type)) {
1426 decode_status_frame(dp83640, skb);
Richard Cochranae6e86b2011-06-14 23:55:20 +00001427 kfree_skb(skb);
1428 return true;
Richard Cochrancb646e22011-04-22 12:04:55 +02001429 }
1430
Stefan Sørensena12f78c2014-06-25 14:40:16 +02001431 if (!dp83640->hwts_rx_en)
1432 return false;
1433
Stefan Sørensena1f87232015-11-03 09:34:08 +01001434 if ((type & dp83640->version) == 0 || (type & dp83640->layer) == 0)
1435 return false;
1436
Stefan Sørensen63502b82014-07-22 15:20:45 +02001437 spin_lock_irqsave(&dp83640->rx_lock, flags);
Stefan Sørensenccf6ee92015-11-03 09:34:06 +01001438 prune_rx_ts(dp83640);
Stefan Sørensen63502b82014-07-22 15:20:45 +02001439 list_for_each_safe(this, next, &dp83640->rxts) {
1440 rxts = list_entry(this, struct rxts, list);
1441 if (match(skb, type, rxts)) {
1442 shhwtstamps = skb_hwtstamps(skb);
1443 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
1444 shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
Stefan Sørensen63502b82014-07-22 15:20:45 +02001445 list_del_init(&rxts->list);
1446 list_add(&rxts->list, &dp83640->rxpool);
1447 break;
1448 }
1449 }
1450 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
1451
1452 if (!shhwtstamps) {
1453 skb_info->ptp_type = type;
Stefan Sørensen4b063252015-11-03 09:34:05 +01001454 skb_info->tmo = jiffies + SKB_TIMESTAMP_TIMEOUT;
Stefan Sørensen63502b82014-07-22 15:20:45 +02001455 skb_queue_tail(&dp83640->rx_queue, skb);
Stefan Sørensen4b063252015-11-03 09:34:05 +01001456 schedule_delayed_work(&dp83640->ts_work, SKB_TIMESTAMP_TIMEOUT);
Stefan Sørensend36b82b2017-08-30 08:58:47 +02001457 } else {
1458 netif_rx_ni(skb);
Stefan Sørensen63502b82014-07-22 15:20:45 +02001459 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001460
1461 return true;
1462}
1463
1464static void dp83640_txtstamp(struct phy_device *phydev,
1465 struct sk_buff *skb, int type)
1466{
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +01001467 struct dp83640_skb_info *skb_info = (struct dp83640_skb_info *)skb->cb;
Richard Cochrancb646e22011-04-22 12:04:55 +02001468 struct dp83640_private *dp83640 = phydev->priv;
1469
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001470 switch (dp83640->hwts_tx_en) {
1471
1472 case HWTSTAMP_TX_ONESTEP_SYNC:
1473 if (is_sync(skb, type)) {
Alexander Duyck62bccb82014-09-04 13:31:35 -04001474 kfree_skb(skb);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001475 return;
1476 }
1477 /* fall through */
1478 case HWTSTAMP_TX_ON:
Stefan Sørensene2e2f512014-02-03 15:36:35 +01001479 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +01001480 skb_info->tmo = jiffies + SKB_TIMESTAMP_TIMEOUT;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001481 skb_queue_tail(&dp83640->tx_queue, skb);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001482 break;
1483
1484 case HWTSTAMP_TX_OFF:
1485 default:
Alexander Duyck62bccb82014-09-04 13:31:35 -04001486 kfree_skb(skb);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001487 break;
Richard Cochrancb646e22011-04-22 12:04:55 +02001488 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001489}
1490
Richard Cochran7dff3492012-04-03 22:59:18 +00001491static int dp83640_ts_info(struct phy_device *dev, struct ethtool_ts_info *info)
1492{
1493 struct dp83640_private *dp83640 = dev->priv;
1494
1495 info->so_timestamping =
1496 SOF_TIMESTAMPING_TX_HARDWARE |
1497 SOF_TIMESTAMPING_RX_HARDWARE |
1498 SOF_TIMESTAMPING_RAW_HARDWARE;
1499 info->phc_index = ptp_clock_index(dp83640->clock->ptp_clock);
1500 info->tx_types =
1501 (1 << HWTSTAMP_TX_OFF) |
1502 (1 << HWTSTAMP_TX_ON) |
1503 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
1504 info->rx_filters =
1505 (1 << HWTSTAMP_FILTER_NONE) |
1506 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
Richard Cochran7dff3492012-04-03 22:59:18 +00001507 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
Richard Cochran7dff3492012-04-03 22:59:18 +00001508 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
Jacob Keller11b15442015-04-22 14:40:37 -07001509 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
Richard Cochran7dff3492012-04-03 22:59:18 +00001510 return 0;
1511}
1512
Richard Cochrancb646e22011-04-22 12:04:55 +02001513static struct phy_driver dp83640_driver = {
1514 .phy_id = DP83640_PHY_ID,
1515 .phy_id_mask = 0xfffffff0,
1516 .name = "NatSemi DP83640",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +02001517 /* PHY_BASIC_FEATURES */
Richard Cochrancb646e22011-04-22 12:04:55 +02001518 .probe = dp83640_probe,
1519 .remove = dp83640_remove,
Esben Haabendal76327a32018-04-08 22:17:01 +02001520 .soft_reset = dp83640_soft_reset,
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001521 .config_init = dp83640_config_init,
Stephan Gatzka16421822012-12-04 10:21:38 +00001522 .ack_interrupt = dp83640_ack_interrupt,
1523 .config_intr = dp83640_config_intr,
Richard Cochran7dff3492012-04-03 22:59:18 +00001524 .ts_info = dp83640_ts_info,
Richard Cochrancb646e22011-04-22 12:04:55 +02001525 .hwtstamp = dp83640_hwtstamp,
1526 .rxtstamp = dp83640_rxtstamp,
1527 .txtstamp = dp83640_txtstamp,
Richard Cochrancb646e22011-04-22 12:04:55 +02001528};
1529
1530static int __init dp83640_init(void)
1531{
Andrew Lunnbe01da72016-01-06 20:11:22 +01001532 return phy_driver_register(&dp83640_driver, THIS_MODULE);
Richard Cochrancb646e22011-04-22 12:04:55 +02001533}
1534
1535static void __exit dp83640_exit(void)
1536{
1537 dp83640_free_clocks();
1538 phy_driver_unregister(&dp83640_driver);
1539}
1540
1541MODULE_DESCRIPTION("National Semiconductor DP83640 PHY driver");
Richard Cochranfbf4b932014-03-20 22:21:56 +01001542MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
Richard Cochrancb646e22011-04-22 12:04:55 +02001543MODULE_LICENSE("GPL");
1544
1545module_init(dp83640_init);
1546module_exit(dp83640_exit);
1547
John Stultz86ff9baa2011-05-23 13:32:11 -07001548static struct mdio_device_id __maybe_unused dp83640_tbl[] = {
Richard Cochrancb646e22011-04-22 12:04:55 +02001549 { DP83640_PHY_ID, 0xfffffff0 },
1550 { }
1551};
1552
1553MODULE_DEVICE_TABLE(mdio, dp83640_tbl);