blob: 6e8807212aa342228ad5edb63b5bee4cc6cadd81 [file] [log] [blame]
Richard Cochrancb646e22011-04-22 12:04:55 +02001/*
2 * Driver for the National Semiconductor DP83640 PHYTER
3 *
4 * Copyright (C) 2010 OMICRON electronics GmbH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
Joe Perches8d242482012-06-09 07:49:07 +000020
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Stefan Sørensen539e44d2015-11-03 09:34:04 +010023#include <linux/crc32.h>
Richard Cochrancb646e22011-04-22 12:04:55 +020024#include <linux/ethtool.h>
25#include <linux/kernel.h>
26#include <linux/list.h>
27#include <linux/mii.h>
28#include <linux/module.h>
29#include <linux/net_tstamp.h>
30#include <linux/netdevice.h>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020031#include <linux/if_vlan.h>
Richard Cochrancb646e22011-04-22 12:04:55 +020032#include <linux/phy.h>
33#include <linux/ptp_classify.h>
34#include <linux/ptp_clock_kernel.h>
35
36#include "dp83640_reg.h"
37
38#define DP83640_PHY_ID 0x20005ce1
39#define PAGESEL 0x13
Richard Cochran80288372011-08-06 21:03:04 +000040#define MAX_RXTS 64
Richard Cochran49b3fd42011-09-20 01:43:14 +000041#define N_EXT_TS 6
Stefan Sørensenad015772014-06-27 12:05:30 +020042#define N_PER_OUT 7
Richard Cochrancb646e22011-04-22 12:04:55 +020043#define PSF_PTPVER 2
44#define PSF_EVNT 0x4000
45#define PSF_RX 0x2000
46#define PSF_TX 0x1000
47#define EXT_EVENT 1
Richard Cochran49b3fd42011-09-20 01:43:14 +000048#define CAL_EVENT 7
Richard Cochran397a2532015-05-25 11:55:43 +020049#define CAL_TRIGGER 1
Richard Cochran86dd3612014-03-20 22:21:58 +010050#define DP83640_N_PINS 12
Richard Cochrancb646e22011-04-22 12:04:55 +020051
Stephan Gatzka16421822012-12-04 10:21:38 +000052#define MII_DP83640_MICR 0x11
53#define MII_DP83640_MISR 0x12
54
55#define MII_DP83640_MICR_OE 0x1
56#define MII_DP83640_MICR_IE 0x2
57
58#define MII_DP83640_MISR_RHF_INT_EN 0x01
59#define MII_DP83640_MISR_FHF_INT_EN 0x02
60#define MII_DP83640_MISR_ANC_INT_EN 0x04
61#define MII_DP83640_MISR_DUP_INT_EN 0x08
62#define MII_DP83640_MISR_SPD_INT_EN 0x10
63#define MII_DP83640_MISR_LINK_INT_EN 0x20
64#define MII_DP83640_MISR_ED_INT_EN 0x40
65#define MII_DP83640_MISR_LQ_INT_EN 0x80
66
Richard Cochrancb646e22011-04-22 12:04:55 +020067/* phyter seems to miss the mark by 16 ns */
68#define ADJTIME_FIX 16
69
Stefan Sørensen4b063252015-11-03 09:34:05 +010070#define SKB_TIMESTAMP_TIMEOUT 2 /* jiffies */
71
Richard Cochrancb646e22011-04-22 12:04:55 +020072#if defined(__BIG_ENDIAN)
73#define ENDIAN_FLAG 0
74#elif defined(__LITTLE_ENDIAN)
75#define ENDIAN_FLAG PSF_ENDIAN
76#endif
77
Stefan Sørensen63502b82014-07-22 15:20:45 +020078struct dp83640_skb_info {
79 int ptp_type;
80 unsigned long tmo;
81};
Richard Cochrancb646e22011-04-22 12:04:55 +020082
83struct phy_rxts {
84 u16 ns_lo; /* ns[15:0] */
85 u16 ns_hi; /* overflow[1:0], ns[29:16] */
86 u16 sec_lo; /* sec[15:0] */
87 u16 sec_hi; /* sec[31:16] */
88 u16 seqid; /* sequenceId[15:0] */
89 u16 msgtype; /* messageType[3:0], hash[11:0] */
90};
91
92struct phy_txts {
93 u16 ns_lo; /* ns[15:0] */
94 u16 ns_hi; /* overflow[1:0], ns[29:16] */
95 u16 sec_lo; /* sec[15:0] */
96 u16 sec_hi; /* sec[31:16] */
97};
98
99struct rxts {
100 struct list_head list;
101 unsigned long tmo;
102 u64 ns;
103 u16 seqid;
104 u8 msgtype;
105 u16 hash;
106};
107
108struct dp83640_clock;
109
110struct dp83640_private {
111 struct list_head list;
112 struct dp83640_clock *clock;
113 struct phy_device *phydev;
Stefan Sørensen4b063252015-11-03 09:34:05 +0100114 struct delayed_work ts_work;
Richard Cochrancb646e22011-04-22 12:04:55 +0200115 int hwts_tx_en;
116 int hwts_rx_en;
117 int layer;
118 int version;
119 /* remember state of cfg0 during calibration */
120 int cfg0;
121 /* remember the last event time stamp */
122 struct phy_txts edata;
123 /* list of rx timestamps */
124 struct list_head rxts;
125 struct list_head rxpool;
126 struct rxts rx_pool_data[MAX_RXTS];
127 /* protects above three fields from concurrent access */
128 spinlock_t rx_lock;
129 /* queues of incoming and outgoing packets */
130 struct sk_buff_head rx_queue;
131 struct sk_buff_head tx_queue;
132};
133
134struct dp83640_clock {
135 /* keeps the instance in the 'phyter_clocks' list */
136 struct list_head list;
137 /* we create one clock instance per MII bus */
138 struct mii_bus *bus;
139 /* protects extended registers from concurrent access */
140 struct mutex extreg_lock;
141 /* remembers which page was last selected */
142 int page;
143 /* our advertised capabilities */
144 struct ptp_clock_info caps;
145 /* protects the three fields below from concurrent access */
146 struct mutex clock_lock;
147 /* the one phyter from which we shall read */
148 struct dp83640_private *chosen;
149 /* list of the other attached phyters, not chosen */
150 struct list_head phylist;
151 /* reference to our PTP hardware clock */
152 struct ptp_clock *ptp_clock;
153};
154
155/* globals */
156
Richard Cochran49b3fd42011-09-20 01:43:14 +0000157enum {
158 CALIBRATE_GPIO,
159 PEROUT_GPIO,
160 EXTTS0_GPIO,
161 EXTTS1_GPIO,
162 EXTTS2_GPIO,
163 EXTTS3_GPIO,
164 EXTTS4_GPIO,
165 EXTTS5_GPIO,
166 GPIO_TABLE_SIZE
167};
168
Richard Cochrancb646e22011-04-22 12:04:55 +0200169static int chosen_phy = -1;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000170static ushort gpio_tab[GPIO_TABLE_SIZE] = {
171 1, 2, 3, 4, 8, 9, 10, 11
172};
Richard Cochrancb646e22011-04-22 12:04:55 +0200173
174module_param(chosen_phy, int, 0444);
Richard Cochran49b3fd42011-09-20 01:43:14 +0000175module_param_array(gpio_tab, ushort, NULL, 0444);
Richard Cochrancb646e22011-04-22 12:04:55 +0200176
177MODULE_PARM_DESC(chosen_phy, \
178 "The address of the PHY to use for the ancillary clock features");
Richard Cochran49b3fd42011-09-20 01:43:14 +0000179MODULE_PARM_DESC(gpio_tab, \
180 "Which GPIO line to use for which purpose: cal,perout,extts1,...,extts6");
Richard Cochrancb646e22011-04-22 12:04:55 +0200181
Richard Cochran86dd3612014-03-20 22:21:58 +0100182static void dp83640_gpio_defaults(struct ptp_pin_desc *pd)
183{
184 int i, index;
185
186 for (i = 0; i < DP83640_N_PINS; i++) {
187 snprintf(pd[i].name, sizeof(pd[i].name), "GPIO%d", 1 + i);
188 pd[i].index = i;
189 }
190
191 for (i = 0; i < GPIO_TABLE_SIZE; i++) {
192 if (gpio_tab[i] < 1 || gpio_tab[i] > DP83640_N_PINS) {
193 pr_err("gpio_tab[%d]=%hu out of range", i, gpio_tab[i]);
194 return;
195 }
196 }
197
198 index = gpio_tab[CALIBRATE_GPIO] - 1;
199 pd[index].func = PTP_PF_PHYSYNC;
200 pd[index].chan = 0;
201
202 index = gpio_tab[PEROUT_GPIO] - 1;
203 pd[index].func = PTP_PF_PEROUT;
204 pd[index].chan = 0;
205
206 for (i = EXTTS0_GPIO; i < GPIO_TABLE_SIZE; i++) {
207 index = gpio_tab[i] - 1;
208 pd[index].func = PTP_PF_EXTTS;
209 pd[index].chan = i - EXTTS0_GPIO;
210 }
211}
212
Richard Cochrancb646e22011-04-22 12:04:55 +0200213/* a list of clocks and a mutex to protect it */
214static LIST_HEAD(phyter_clocks);
215static DEFINE_MUTEX(phyter_clocks_lock);
216
217static void rx_timestamp_work(struct work_struct *work);
218
219/* extended register access functions */
220
221#define BROADCAST_ADDR 31
222
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100223static inline int broadcast_write(struct phy_device *phydev, u32 regnum,
224 u16 val)
Richard Cochrancb646e22011-04-22 12:04:55 +0200225{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100226 return mdiobus_write(phydev->mdio.bus, BROADCAST_ADDR, regnum, val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200227}
228
229/* Caller must hold extreg_lock. */
230static int ext_read(struct phy_device *phydev, int page, u32 regnum)
231{
232 struct dp83640_private *dp83640 = phydev->priv;
233 int val;
234
235 if (dp83640->clock->page != page) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100236 broadcast_write(phydev, PAGESEL, page);
Richard Cochrancb646e22011-04-22 12:04:55 +0200237 dp83640->clock->page = page;
238 }
239 val = phy_read(phydev, regnum);
240
241 return val;
242}
243
244/* Caller must hold extreg_lock. */
245static void ext_write(int broadcast, struct phy_device *phydev,
246 int page, u32 regnum, u16 val)
247{
248 struct dp83640_private *dp83640 = phydev->priv;
249
250 if (dp83640->clock->page != page) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100251 broadcast_write(phydev, PAGESEL, page);
Richard Cochrancb646e22011-04-22 12:04:55 +0200252 dp83640->clock->page = page;
253 }
254 if (broadcast)
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100255 broadcast_write(phydev, regnum, val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200256 else
257 phy_write(phydev, regnum, val);
258}
259
260/* Caller must hold extreg_lock. */
261static int tdr_write(int bc, struct phy_device *dev,
Richard Cochran41c2c182015-03-29 23:12:10 +0200262 const struct timespec64 *ts, u16 cmd)
Richard Cochrancb646e22011-04-22 12:04:55 +0200263{
264 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec & 0xffff);/* ns[15:0] */
265 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec >> 16); /* ns[31:16] */
266 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec & 0xffff); /* sec[15:0] */
267 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec >> 16); /* sec[31:16]*/
268
269 ext_write(bc, dev, PAGE4, PTP_CTL, cmd);
270
271 return 0;
272}
273
274/* convert phy timestamps into driver timestamps */
275
276static void phy2rxts(struct phy_rxts *p, struct rxts *rxts)
277{
278 u32 sec;
279
280 sec = p->sec_lo;
281 sec |= p->sec_hi << 16;
282
283 rxts->ns = p->ns_lo;
284 rxts->ns |= (p->ns_hi & 0x3fff) << 16;
285 rxts->ns += ((u64)sec) * 1000000000ULL;
286 rxts->seqid = p->seqid;
287 rxts->msgtype = (p->msgtype >> 12) & 0xf;
288 rxts->hash = p->msgtype & 0x0fff;
Stefan Sørensen4b063252015-11-03 09:34:05 +0100289 rxts->tmo = jiffies + SKB_TIMESTAMP_TIMEOUT;
Richard Cochrancb646e22011-04-22 12:04:55 +0200290}
291
292static u64 phy2txts(struct phy_txts *p)
293{
294 u64 ns;
295 u32 sec;
296
297 sec = p->sec_lo;
298 sec |= p->sec_hi << 16;
299
300 ns = p->ns_lo;
301 ns |= (p->ns_hi & 0x3fff) << 16;
302 ns += ((u64)sec) * 1000000000ULL;
303
304 return ns;
305}
306
Richard Cochran621bdec2014-03-20 22:22:00 +0100307static int periodic_output(struct dp83640_clock *clock,
Stefan Sørensenad015772014-06-27 12:05:30 +0200308 struct ptp_clock_request *clkreq, bool on,
309 int trigger)
Richard Cochran49b3fd42011-09-20 01:43:14 +0000310{
311 struct dp83640_private *dp83640 = clock->chosen;
312 struct phy_device *phydev = dp83640->phydev;
Richard Cochran564ca562014-03-20 22:21:57 +0100313 u32 sec, nsec, pwidth;
Stefan Sørensenad015772014-06-27 12:05:30 +0200314 u16 gpio, ptp_trig, val;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000315
Richard Cochran621bdec2014-03-20 22:22:00 +0100316 if (on) {
Stefan Sørensenad015772014-06-27 12:05:30 +0200317 gpio = 1 + ptp_find_pin(clock->ptp_clock, PTP_PF_PEROUT,
318 trigger);
Richard Cochran621bdec2014-03-20 22:22:00 +0100319 if (gpio < 1)
320 return -EINVAL;
321 } else {
322 gpio = 0;
323 }
324
Richard Cochran49b3fd42011-09-20 01:43:14 +0000325 ptp_trig = TRIG_WR |
326 (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT |
327 (gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT |
328 TRIG_PER |
329 TRIG_PULSE;
330
331 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
332
333 if (!on) {
334 val |= TRIG_DIS;
335 mutex_lock(&clock->extreg_lock);
336 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
337 ext_write(0, phydev, PAGE4, PTP_CTL, val);
338 mutex_unlock(&clock->extreg_lock);
Richard Cochran621bdec2014-03-20 22:22:00 +0100339 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000340 }
341
342 sec = clkreq->perout.start.sec;
343 nsec = clkreq->perout.start.nsec;
Richard Cochran564ca562014-03-20 22:21:57 +0100344 pwidth = clkreq->perout.period.sec * 1000000000UL;
345 pwidth += clkreq->perout.period.nsec;
346 pwidth /= 2;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000347
348 mutex_lock(&clock->extreg_lock);
349
350 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
351
352 /*load trigger*/
353 val |= TRIG_LOAD;
354 ext_write(0, phydev, PAGE4, PTP_CTL, val);
355 ext_write(0, phydev, PAGE4, PTP_TDR, nsec & 0xffff); /* ns[15:0] */
356 ext_write(0, phydev, PAGE4, PTP_TDR, nsec >> 16); /* ns[31:16] */
357 ext_write(0, phydev, PAGE4, PTP_TDR, sec & 0xffff); /* sec[15:0] */
358 ext_write(0, phydev, PAGE4, PTP_TDR, sec >> 16); /* sec[31:16] */
Richard Cochran564ca562014-03-20 22:21:57 +0100359 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff); /* ns[15:0] */
360 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16); /* ns[31:16] */
Stefan Sørensen35e872a2014-06-27 12:05:29 +0200361 /* Triggers 0 and 1 has programmable pulsewidth2 */
362 if (trigger < 2) {
363 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff);
364 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16);
365 }
Richard Cochran49b3fd42011-09-20 01:43:14 +0000366
367 /*enable trigger*/
368 val &= ~TRIG_LOAD;
369 val |= TRIG_EN;
370 ext_write(0, phydev, PAGE4, PTP_CTL, val);
371
372 mutex_unlock(&clock->extreg_lock);
Richard Cochran621bdec2014-03-20 22:22:00 +0100373 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000374}
375
Richard Cochrancb646e22011-04-22 12:04:55 +0200376/* ptp clock methods */
377
Richard Cochrane4788b82016-11-08 22:49:18 +0100378static int ptp_dp83640_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
Richard Cochrancb646e22011-04-22 12:04:55 +0200379{
380 struct dp83640_clock *clock =
381 container_of(ptp, struct dp83640_clock, caps);
382 struct phy_device *phydev = clock->chosen->phydev;
383 u64 rate;
384 int neg_adj = 0;
385 u16 hi, lo;
386
Richard Cochrane4788b82016-11-08 22:49:18 +0100387 if (scaled_ppm < 0) {
Richard Cochrancb646e22011-04-22 12:04:55 +0200388 neg_adj = 1;
Richard Cochrane4788b82016-11-08 22:49:18 +0100389 scaled_ppm = -scaled_ppm;
Richard Cochrancb646e22011-04-22 12:04:55 +0200390 }
Richard Cochrane4788b82016-11-08 22:49:18 +0100391 rate = scaled_ppm;
392 rate <<= 13;
393 rate = div_u64(rate, 15625);
Richard Cochrancb646e22011-04-22 12:04:55 +0200394
395 hi = (rate >> 16) & PTP_RATE_HI_MASK;
396 if (neg_adj)
397 hi |= PTP_RATE_DIR;
398
399 lo = rate & 0xffff;
400
401 mutex_lock(&clock->extreg_lock);
402
403 ext_write(1, phydev, PAGE4, PTP_RATEH, hi);
404 ext_write(1, phydev, PAGE4, PTP_RATEL, lo);
405
406 mutex_unlock(&clock->extreg_lock);
407
408 return 0;
409}
410
411static int ptp_dp83640_adjtime(struct ptp_clock_info *ptp, s64 delta)
412{
413 struct dp83640_clock *clock =
414 container_of(ptp, struct dp83640_clock, caps);
415 struct phy_device *phydev = clock->chosen->phydev;
Richard Cochran41c2c182015-03-29 23:12:10 +0200416 struct timespec64 ts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200417 int err;
418
419 delta += ADJTIME_FIX;
420
Richard Cochran41c2c182015-03-29 23:12:10 +0200421 ts = ns_to_timespec64(delta);
Richard Cochrancb646e22011-04-22 12:04:55 +0200422
423 mutex_lock(&clock->extreg_lock);
424
425 err = tdr_write(1, phydev, &ts, PTP_STEP_CLK);
426
427 mutex_unlock(&clock->extreg_lock);
428
429 return err;
430}
431
Richard Cochran41c2c182015-03-29 23:12:10 +0200432static int ptp_dp83640_gettime(struct ptp_clock_info *ptp,
433 struct timespec64 *ts)
Richard Cochrancb646e22011-04-22 12:04:55 +0200434{
435 struct dp83640_clock *clock =
436 container_of(ptp, struct dp83640_clock, caps);
437 struct phy_device *phydev = clock->chosen->phydev;
438 unsigned int val[4];
439
440 mutex_lock(&clock->extreg_lock);
441
442 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_RD_CLK);
443
444 val[0] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[15:0] */
445 val[1] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[31:16] */
446 val[2] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[15:0] */
447 val[3] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[31:16] */
448
449 mutex_unlock(&clock->extreg_lock);
450
451 ts->tv_nsec = val[0] | (val[1] << 16);
452 ts->tv_sec = val[2] | (val[3] << 16);
453
454 return 0;
455}
456
457static int ptp_dp83640_settime(struct ptp_clock_info *ptp,
Richard Cochran41c2c182015-03-29 23:12:10 +0200458 const struct timespec64 *ts)
Richard Cochrancb646e22011-04-22 12:04:55 +0200459{
460 struct dp83640_clock *clock =
461 container_of(ptp, struct dp83640_clock, caps);
462 struct phy_device *phydev = clock->chosen->phydev;
463 int err;
464
465 mutex_lock(&clock->extreg_lock);
466
467 err = tdr_write(1, phydev, ts, PTP_LOAD_CLK);
468
469 mutex_unlock(&clock->extreg_lock);
470
471 return err;
472}
473
474static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
475 struct ptp_clock_request *rq, int on)
476{
477 struct dp83640_clock *clock =
478 container_of(ptp, struct dp83640_clock, caps);
479 struct phy_device *phydev = clock->chosen->phydev;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100480 unsigned int index;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000481 u16 evnt, event_num, gpio_num;
Richard Cochrancb646e22011-04-22 12:04:55 +0200482
483 switch (rq->type) {
484 case PTP_CLK_REQ_EXTTS:
Richard Cochran49b3fd42011-09-20 01:43:14 +0000485 index = rq->extts.index;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100486 if (index >= N_EXT_TS)
Richard Cochrancb646e22011-04-22 12:04:55 +0200487 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000488 event_num = EXT_EVENT + index;
489 evnt = EVNT_WR | (event_num & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
Richard Cochrancb646e22011-04-22 12:04:55 +0200490 if (on) {
Richard Cochranfaa89712014-03-20 22:21:59 +0100491 gpio_num = 1 + ptp_find_pin(clock->ptp_clock,
492 PTP_PF_EXTTS, index);
493 if (gpio_num < 1)
494 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000495 evnt |= (gpio_num & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
Stefan Sørensen80671bd2014-02-03 15:36:50 +0100496 if (rq->extts.flags & PTP_FALLING_EDGE)
497 evnt |= EVNT_FALL;
498 else
499 evnt |= EVNT_RISE;
Richard Cochrancb646e22011-04-22 12:04:55 +0200500 }
Richard Cochrana9358652015-05-25 11:55:44 +0200501 mutex_lock(&clock->extreg_lock);
Richard Cochrancb646e22011-04-22 12:04:55 +0200502 ext_write(0, phydev, PAGE5, PTP_EVNT, evnt);
Richard Cochrana9358652015-05-25 11:55:44 +0200503 mutex_unlock(&clock->extreg_lock);
Richard Cochrancb646e22011-04-22 12:04:55 +0200504 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000505
506 case PTP_CLK_REQ_PEROUT:
Stefan Sørensenad015772014-06-27 12:05:30 +0200507 if (rq->perout.index >= N_PER_OUT)
Richard Cochran49b3fd42011-09-20 01:43:14 +0000508 return -EINVAL;
Stefan Sørensenad015772014-06-27 12:05:30 +0200509 return periodic_output(clock, rq, on, rq->perout.index);
Richard Cochran49b3fd42011-09-20 01:43:14 +0000510
Richard Cochrancb646e22011-04-22 12:04:55 +0200511 default:
512 break;
513 }
514
515 return -EOPNOTSUPP;
516}
517
Richard Cochran86dd3612014-03-20 22:21:58 +0100518static int ptp_dp83640_verify(struct ptp_clock_info *ptp, unsigned int pin,
519 enum ptp_pin_function func, unsigned int chan)
520{
Stefan Sørensen6f39eb82014-06-27 12:05:31 +0200521 struct dp83640_clock *clock =
522 container_of(ptp, struct dp83640_clock, caps);
523
524 if (clock->caps.pin_config[pin].func == PTP_PF_PHYSYNC &&
525 !list_empty(&clock->phylist))
526 return 1;
527
528 if (func == PTP_PF_PHYSYNC)
529 return 1;
530
Richard Cochran86dd3612014-03-20 22:21:58 +0100531 return 0;
532}
533
Richard Cochrancb646e22011-04-22 12:04:55 +0200534static u8 status_frame_dst[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 };
535static u8 status_frame_src[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F };
536
537static void enable_status_frames(struct phy_device *phydev, bool on)
538{
Richard Cochrana9358652015-05-25 11:55:44 +0200539 struct dp83640_private *dp83640 = phydev->priv;
540 struct dp83640_clock *clock = dp83640->clock;
Richard Cochrancb646e22011-04-22 12:04:55 +0200541 u16 cfg0 = 0, ver;
542
543 if (on)
544 cfg0 = PSF_EVNT_EN | PSF_RXTS_EN | PSF_TXTS_EN | ENDIAN_FLAG;
545
546 ver = (PSF_PTPVER & VERSIONPTP_MASK) << VERSIONPTP_SHIFT;
547
Richard Cochrana9358652015-05-25 11:55:44 +0200548 mutex_lock(&clock->extreg_lock);
549
Richard Cochrancb646e22011-04-22 12:04:55 +0200550 ext_write(0, phydev, PAGE5, PSF_CFG0, cfg0);
551 ext_write(0, phydev, PAGE6, PSF_CFG1, ver);
552
Richard Cochrana9358652015-05-25 11:55:44 +0200553 mutex_unlock(&clock->extreg_lock);
554
Richard Cochrancb646e22011-04-22 12:04:55 +0200555 if (!phydev->attached_dev) {
Andrew Lunnab2a6052018-09-29 23:04:10 +0200556 phydev_warn(phydev,
557 "expected to find an attached netdevice\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200558 return;
559 }
560
561 if (on) {
562 if (dev_mc_add(phydev->attached_dev, status_frame_dst))
Andrew Lunnab2a6052018-09-29 23:04:10 +0200563 phydev_warn(phydev, "failed to add mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200564 } else {
565 if (dev_mc_del(phydev->attached_dev, status_frame_dst))
Andrew Lunnab2a6052018-09-29 23:04:10 +0200566 phydev_warn(phydev, "failed to delete mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200567 }
568}
569
570static bool is_status_frame(struct sk_buff *skb, int type)
571{
572 struct ethhdr *h = eth_hdr(skb);
573
574 if (PTP_CLASS_V2_L2 == type &&
575 !memcmp(h->h_source, status_frame_src, sizeof(status_frame_src)))
576 return true;
577 else
578 return false;
579}
580
581static int expired(struct rxts *rxts)
582{
583 return time_after(jiffies, rxts->tmo);
584}
585
586/* Caller must hold rx_lock. */
587static void prune_rx_ts(struct dp83640_private *dp83640)
588{
589 struct list_head *this, *next;
590 struct rxts *rxts;
591
592 list_for_each_safe(this, next, &dp83640->rxts) {
593 rxts = list_entry(this, struct rxts, list);
594 if (expired(rxts)) {
595 list_del_init(&rxts->list);
596 list_add(&rxts->list, &dp83640->rxpool);
597 }
598 }
599}
600
601/* synchronize the phyters so they act as one clock */
602
603static void enable_broadcast(struct phy_device *phydev, int init_page, int on)
604{
605 int val;
606 phy_write(phydev, PAGESEL, 0);
607 val = phy_read(phydev, PHYCR2);
608 if (on)
609 val |= BC_WRITE;
610 else
611 val &= ~BC_WRITE;
612 phy_write(phydev, PHYCR2, val);
613 phy_write(phydev, PAGESEL, init_page);
614}
615
616static void recalibrate(struct dp83640_clock *clock)
617{
618 s64 now, diff;
619 struct phy_txts event_ts;
Richard Cochran41c2c182015-03-29 23:12:10 +0200620 struct timespec64 ts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200621 struct list_head *this;
622 struct dp83640_private *tmp;
623 struct phy_device *master = clock->chosen->phydev;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000624 u16 cal_gpio, cfg0, evnt, ptp_trig, trigger, val;
Richard Cochrancb646e22011-04-22 12:04:55 +0200625
626 trigger = CAL_TRIGGER;
Stefan Sørensene0155952014-06-27 12:05:32 +0200627 cal_gpio = 1 + ptp_find_pin(clock->ptp_clock, PTP_PF_PHYSYNC, 0);
628 if (cal_gpio < 1) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +0900629 pr_err("PHY calibration pin not available - PHY is not calibrated.");
Stefan Sørensene0155952014-06-27 12:05:32 +0200630 return;
631 }
Richard Cochrancb646e22011-04-22 12:04:55 +0200632
633 mutex_lock(&clock->extreg_lock);
634
635 /*
636 * enable broadcast, disable status frames, enable ptp clock
637 */
638 list_for_each(this, &clock->phylist) {
639 tmp = list_entry(this, struct dp83640_private, list);
640 enable_broadcast(tmp->phydev, clock->page, 1);
641 tmp->cfg0 = ext_read(tmp->phydev, PAGE5, PSF_CFG0);
642 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, 0);
643 ext_write(0, tmp->phydev, PAGE4, PTP_CTL, PTP_ENABLE);
644 }
645 enable_broadcast(master, clock->page, 1);
646 cfg0 = ext_read(master, PAGE5, PSF_CFG0);
647 ext_write(0, master, PAGE5, PSF_CFG0, 0);
648 ext_write(0, master, PAGE4, PTP_CTL, PTP_ENABLE);
649
650 /*
651 * enable an event timestamp
652 */
653 evnt = EVNT_WR | EVNT_RISE | EVNT_SINGLE;
654 evnt |= (CAL_EVENT & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
655 evnt |= (cal_gpio & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
656
657 list_for_each(this, &clock->phylist) {
658 tmp = list_entry(this, struct dp83640_private, list);
659 ext_write(0, tmp->phydev, PAGE5, PTP_EVNT, evnt);
660 }
661 ext_write(0, master, PAGE5, PTP_EVNT, evnt);
662
663 /*
664 * configure a trigger
665 */
666 ptp_trig = TRIG_WR | TRIG_IF_LATE | TRIG_PULSE;
667 ptp_trig |= (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT;
668 ptp_trig |= (cal_gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT;
669 ext_write(0, master, PAGE5, PTP_TRIG, ptp_trig);
670
671 /* load trigger */
672 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
673 val |= TRIG_LOAD;
674 ext_write(0, master, PAGE4, PTP_CTL, val);
675
676 /* enable trigger */
677 val &= ~TRIG_LOAD;
678 val |= TRIG_EN;
679 ext_write(0, master, PAGE4, PTP_CTL, val);
680
681 /* disable trigger */
682 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
683 val |= TRIG_DIS;
684 ext_write(0, master, PAGE4, PTP_CTL, val);
685
686 /*
687 * read out and correct offsets
688 */
689 val = ext_read(master, PAGE4, PTP_STS);
Andrew Lunnc4fabb82018-09-29 23:04:11 +0200690 phydev_info(master, "master PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200691 val = ext_read(master, PAGE4, PTP_ESTS);
Andrew Lunnc4fabb82018-09-29 23:04:11 +0200692 phydev_info(master, "master PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200693 event_ts.ns_lo = ext_read(master, PAGE4, PTP_EDATA);
694 event_ts.ns_hi = ext_read(master, PAGE4, PTP_EDATA);
695 event_ts.sec_lo = ext_read(master, PAGE4, PTP_EDATA);
696 event_ts.sec_hi = ext_read(master, PAGE4, PTP_EDATA);
697 now = phy2txts(&event_ts);
698
699 list_for_each(this, &clock->phylist) {
700 tmp = list_entry(this, struct dp83640_private, list);
701 val = ext_read(tmp->phydev, PAGE4, PTP_STS);
Andrew Lunnc4fabb82018-09-29 23:04:11 +0200702 phydev_info(tmp->phydev, "slave PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200703 val = ext_read(tmp->phydev, PAGE4, PTP_ESTS);
Andrew Lunnc4fabb82018-09-29 23:04:11 +0200704 phydev_info(tmp->phydev, "slave PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200705 event_ts.ns_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
706 event_ts.ns_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
707 event_ts.sec_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
708 event_ts.sec_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
709 diff = now - (s64) phy2txts(&event_ts);
Andrew Lunnc4fabb82018-09-29 23:04:11 +0200710 phydev_info(tmp->phydev, "slave offset %lld nanoseconds\n",
711 diff);
Richard Cochrancb646e22011-04-22 12:04:55 +0200712 diff += ADJTIME_FIX;
Richard Cochran41c2c182015-03-29 23:12:10 +0200713 ts = ns_to_timespec64(diff);
Richard Cochrancb646e22011-04-22 12:04:55 +0200714 tdr_write(0, tmp->phydev, &ts, PTP_STEP_CLK);
715 }
716
717 /*
718 * restore status frames
719 */
720 list_for_each(this, &clock->phylist) {
721 tmp = list_entry(this, struct dp83640_private, list);
722 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, tmp->cfg0);
723 }
724 ext_write(0, master, PAGE5, PSF_CFG0, cfg0);
725
726 mutex_unlock(&clock->extreg_lock);
727}
728
729/* time stamping methods */
730
Richard Cochran49b3fd42011-09-20 01:43:14 +0000731static inline u16 exts_chan_to_edata(int ch)
732{
733 return 1 << ((ch + EXT_EVENT) * 2);
734}
735
Richard Cochran23310382011-06-14 23:55:19 +0000736static int decode_evnt(struct dp83640_private *dp83640,
Christian Riesch13322f22014-08-21 15:17:04 +0200737 void *data, int len, u16 ests)
Richard Cochrancb646e22011-04-22 12:04:55 +0200738{
Richard Cochran23310382011-06-14 23:55:19 +0000739 struct phy_txts *phy_txts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200740 struct ptp_clock_event event;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000741 int i, parsed;
Richard Cochrancb646e22011-04-22 12:04:55 +0200742 int words = (ests >> EVNT_TS_LEN_SHIFT) & EVNT_TS_LEN_MASK;
Richard Cochran23310382011-06-14 23:55:19 +0000743 u16 ext_status = 0;
744
Christian Riesch13322f22014-08-21 15:17:04 +0200745 /* calculate length of the event timestamp status message */
746 if (ests & MULT_EVNT)
747 parsed = (words + 2) * sizeof(u16);
748 else
749 parsed = (words + 1) * sizeof(u16);
750
751 /* check if enough data is available */
752 if (len < parsed)
753 return len;
754
Richard Cochran23310382011-06-14 23:55:19 +0000755 if (ests & MULT_EVNT) {
756 ext_status = *(u16 *) data;
757 data += sizeof(ext_status);
758 }
759
760 phy_txts = data;
Richard Cochrancb646e22011-04-22 12:04:55 +0200761
Gustavo A. R. Silvad331e752018-08-09 10:08:24 -0500762 switch (words) {
Richard Cochrancb646e22011-04-22 12:04:55 +0200763 case 3:
764 dp83640->edata.sec_hi = phy_txts->sec_hi;
Gustavo A. R. Silvad331e752018-08-09 10:08:24 -0500765 /* fall through */
Richard Cochrancb646e22011-04-22 12:04:55 +0200766 case 2:
767 dp83640->edata.sec_lo = phy_txts->sec_lo;
Gustavo A. R. Silvad331e752018-08-09 10:08:24 -0500768 /* fall through */
Richard Cochrancb646e22011-04-22 12:04:55 +0200769 case 1:
770 dp83640->edata.ns_hi = phy_txts->ns_hi;
Gustavo A. R. Silvad331e752018-08-09 10:08:24 -0500771 /* fall through */
Richard Cochrancb646e22011-04-22 12:04:55 +0200772 case 0:
773 dp83640->edata.ns_lo = phy_txts->ns_lo;
774 }
775
Christian Riesch13322f22014-08-21 15:17:04 +0200776 if (!ext_status) {
Richard Cochran49b3fd42011-09-20 01:43:14 +0000777 i = ((ests >> EVNT_NUM_SHIFT) & EVNT_NUM_MASK) - EXT_EVENT;
778 ext_status = exts_chan_to_edata(i);
779 }
780
Richard Cochrancb646e22011-04-22 12:04:55 +0200781 event.type = PTP_CLOCK_EXTTS;
Richard Cochrancb646e22011-04-22 12:04:55 +0200782 event.timestamp = phy2txts(&dp83640->edata);
783
Stefan Sørensena0077a92014-07-11 08:18:26 +0200784 /* Compensate for input path and synchronization delays */
785 event.timestamp -= 35;
786
Richard Cochran49b3fd42011-09-20 01:43:14 +0000787 for (i = 0; i < N_EXT_TS; i++) {
788 if (ext_status & exts_chan_to_edata(i)) {
789 event.index = i;
790 ptp_clock_event(dp83640->clock->ptp_clock, &event);
791 }
792 }
Richard Cochran23310382011-06-14 23:55:19 +0000793
Christian Riesch13322f22014-08-21 15:17:04 +0200794 return parsed;
Richard Cochrancb646e22011-04-22 12:04:55 +0200795}
796
Stefan Sørensen539e44d2015-11-03 09:34:04 +0100797#define DP83640_PACKET_HASH_OFFSET 20
798#define DP83640_PACKET_HASH_LEN 10
799
Stefan Sørensen63502b82014-07-22 15:20:45 +0200800static int match(struct sk_buff *skb, unsigned int type, struct rxts *rxts)
801{
Stefan Sørensen539e44d2015-11-03 09:34:04 +0100802 u16 *seqid, hash;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200803 unsigned int offset = 0;
804 u8 *msgtype, *data = skb_mac_header(skb);
805
806 /* check sequenceID, messageType, 12 bit hash of offset 20-29 */
807
808 if (type & PTP_CLASS_VLAN)
809 offset += VLAN_HLEN;
810
811 switch (type & PTP_CLASS_PMASK) {
812 case PTP_CLASS_IPV4:
Richard Cochrancca04b22014-11-12 11:33:52 +0100813 offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200814 break;
815 case PTP_CLASS_IPV6:
816 offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
817 break;
818 case PTP_CLASS_L2:
819 offset += ETH_HLEN;
820 break;
821 default:
822 return 0;
823 }
824
825 if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
826 return 0;
827
828 if (unlikely(type & PTP_CLASS_V1))
829 msgtype = data + offset + OFF_PTP_CONTROL;
830 else
831 msgtype = data + offset;
Stefan Sørensen539e44d2015-11-03 09:34:04 +0100832 if (rxts->msgtype != (*msgtype & 0xf))
833 return 0;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200834
835 seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
Stefan Sørensen539e44d2015-11-03 09:34:04 +0100836 if (rxts->seqid != ntohs(*seqid))
837 return 0;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200838
Stefan Sørensen539e44d2015-11-03 09:34:04 +0100839 hash = ether_crc(DP83640_PACKET_HASH_LEN,
840 data + offset + DP83640_PACKET_HASH_OFFSET) >> 20;
841 if (rxts->hash != hash)
842 return 0;
843
844 return 1;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200845}
846
Richard Cochrancb646e22011-04-22 12:04:55 +0200847static void decode_rxts(struct dp83640_private *dp83640,
848 struct phy_rxts *phy_rxts)
849{
850 struct rxts *rxts;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200851 struct skb_shared_hwtstamps *shhwtstamps = NULL;
852 struct sk_buff *skb;
Richard Cochrancb646e22011-04-22 12:04:55 +0200853 unsigned long flags;
Manfred Rudigier81e8f2e2016-01-20 11:22:28 +0100854 u8 overflow;
855
856 overflow = (phy_rxts->ns_hi >> 14) & 0x3;
857 if (overflow)
858 pr_debug("rx timestamp queue overflow, count %d\n", overflow);
Richard Cochrancb646e22011-04-22 12:04:55 +0200859
860 spin_lock_irqsave(&dp83640->rx_lock, flags);
861
862 prune_rx_ts(dp83640);
863
864 if (list_empty(&dp83640->rxpool)) {
Joe Perches8d242482012-06-09 07:49:07 +0000865 pr_debug("rx timestamp pool is empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200866 goto out;
867 }
868 rxts = list_first_entry(&dp83640->rxpool, struct rxts, list);
869 list_del_init(&rxts->list);
870 phy2rxts(phy_rxts, rxts);
Stefan Sørensen63502b82014-07-22 15:20:45 +0200871
Richard Cochranadbe0882015-05-25 11:55:45 +0200872 spin_lock(&dp83640->rx_queue.lock);
Stefan Sørensen63502b82014-07-22 15:20:45 +0200873 skb_queue_walk(&dp83640->rx_queue, skb) {
874 struct dp83640_skb_info *skb_info;
875
876 skb_info = (struct dp83640_skb_info *)skb->cb;
877 if (match(skb, skb_info->ptp_type, rxts)) {
878 __skb_unlink(skb, &dp83640->rx_queue);
879 shhwtstamps = skb_hwtstamps(skb);
880 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
881 shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
Stefan Sørensen63502b82014-07-22 15:20:45 +0200882 list_add(&rxts->list, &dp83640->rxpool);
883 break;
884 }
885 }
Richard Cochranadbe0882015-05-25 11:55:45 +0200886 spin_unlock(&dp83640->rx_queue.lock);
Stefan Sørensen63502b82014-07-22 15:20:45 +0200887
888 if (!shhwtstamps)
889 list_add_tail(&rxts->list, &dp83640->rxts);
Richard Cochrancb646e22011-04-22 12:04:55 +0200890out:
891 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
Stefan Sørensend36b82b2017-08-30 08:58:47 +0200892
893 if (shhwtstamps)
894 netif_rx_ni(skb);
Richard Cochrancb646e22011-04-22 12:04:55 +0200895}
896
897static void decode_txts(struct dp83640_private *dp83640,
898 struct phy_txts *phy_txts)
899{
900 struct skb_shared_hwtstamps shhwtstamps;
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +0100901 struct dp83640_skb_info *skb_info;
Richard Cochrancb646e22011-04-22 12:04:55 +0200902 struct sk_buff *skb;
Manfred Rudigier81e8f2e2016-01-20 11:22:28 +0100903 u8 overflow;
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +0100904 u64 ns;
Richard Cochrancb646e22011-04-22 12:04:55 +0200905
906 /* We must already have the skb that triggered this. */
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +0100907again:
Richard Cochrancb646e22011-04-22 12:04:55 +0200908 skb = skb_dequeue(&dp83640->tx_queue);
Richard Cochrancb646e22011-04-22 12:04:55 +0200909 if (!skb) {
Joe Perches8d242482012-06-09 07:49:07 +0000910 pr_debug("have timestamp but tx_queue empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200911 return;
912 }
Manfred Rudigier81e8f2e2016-01-20 11:22:28 +0100913
914 overflow = (phy_txts->ns_hi >> 14) & 0x3;
915 if (overflow) {
916 pr_debug("tx timestamp queue overflow, count %d\n", overflow);
917 while (skb) {
Richard Cochrandb9d8b22017-06-23 17:51:31 +0200918 kfree_skb(skb);
Manfred Rudigier81e8f2e2016-01-20 11:22:28 +0100919 skb = skb_dequeue(&dp83640->tx_queue);
920 }
921 return;
922 }
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +0100923 skb_info = (struct dp83640_skb_info *)skb->cb;
924 if (time_after(jiffies, skb_info->tmo)) {
925 kfree_skb(skb);
926 goto again;
927 }
Manfred Rudigier81e8f2e2016-01-20 11:22:28 +0100928
Richard Cochrancb646e22011-04-22 12:04:55 +0200929 ns = phy2txts(phy_txts);
930 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
931 shhwtstamps.hwtstamp = ns_to_ktime(ns);
932 skb_complete_tx_timestamp(skb, &shhwtstamps);
933}
934
935static void decode_status_frame(struct dp83640_private *dp83640,
936 struct sk_buff *skb)
937{
938 struct phy_rxts *phy_rxts;
939 struct phy_txts *phy_txts;
940 u8 *ptr;
941 int len, size;
942 u16 ests, type;
943
944 ptr = skb->data + 2;
945
946 for (len = skb_headlen(skb) - 2; len > sizeof(type); len -= size) {
947
948 type = *(u16 *)ptr;
949 ests = type & 0x0fff;
950 type = type & 0xf000;
951 len -= sizeof(type);
952 ptr += sizeof(type);
953
954 if (PSF_RX == type && len >= sizeof(*phy_rxts)) {
955
956 phy_rxts = (struct phy_rxts *) ptr;
957 decode_rxts(dp83640, phy_rxts);
958 size = sizeof(*phy_rxts);
959
960 } else if (PSF_TX == type && len >= sizeof(*phy_txts)) {
961
962 phy_txts = (struct phy_txts *) ptr;
963 decode_txts(dp83640, phy_txts);
964 size = sizeof(*phy_txts);
965
Christian Riesch13322f22014-08-21 15:17:04 +0200966 } else if (PSF_EVNT == type) {
Richard Cochrancb646e22011-04-22 12:04:55 +0200967
Christian Riesch13322f22014-08-21 15:17:04 +0200968 size = decode_evnt(dp83640, ptr, len, ests);
Richard Cochrancb646e22011-04-22 12:04:55 +0200969
970 } else {
971 size = 0;
972 break;
973 }
974 ptr += size;
975 }
976}
977
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000978static int is_sync(struct sk_buff *skb, int type)
979{
980 u8 *data = skb->data, *msgtype;
981 unsigned int offset = 0;
982
Stefan Sørensenae5c6c62014-06-27 11:59:10 +0200983 if (type & PTP_CLASS_VLAN)
984 offset += VLAN_HLEN;
985
986 switch (type & PTP_CLASS_PMASK) {
987 case PTP_CLASS_IPV4:
Richard Cochrancca04b22014-11-12 11:33:52 +0100988 offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000989 break;
Stefan Sørensenae5c6c62014-06-27 11:59:10 +0200990 case PTP_CLASS_IPV6:
991 offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000992 break;
Stefan Sørensenae5c6c62014-06-27 11:59:10 +0200993 case PTP_CLASS_L2:
994 offset += ETH_HLEN;
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000995 break;
996 default:
997 return 0;
998 }
999
1000 if (type & PTP_CLASS_V1)
1001 offset += OFF_PTP_CONTROL;
1002
1003 if (skb->len < offset + 1)
1004 return 0;
1005
1006 msgtype = data + offset;
1007
1008 return (*msgtype & 0xf) == 0;
1009}
1010
Richard Cochrancb646e22011-04-22 12:04:55 +02001011static void dp83640_free_clocks(void)
1012{
1013 struct dp83640_clock *clock;
1014 struct list_head *this, *next;
1015
1016 mutex_lock(&phyter_clocks_lock);
1017
1018 list_for_each_safe(this, next, &phyter_clocks) {
1019 clock = list_entry(this, struct dp83640_clock, list);
1020 if (!list_empty(&clock->phylist)) {
Joe Perches8d242482012-06-09 07:49:07 +00001021 pr_warn("phy list non-empty while unloading\n");
Richard Cochrancb646e22011-04-22 12:04:55 +02001022 BUG();
1023 }
1024 list_del(&clock->list);
1025 mutex_destroy(&clock->extreg_lock);
1026 mutex_destroy(&clock->clock_lock);
1027 put_device(&clock->bus->dev);
Richard Cochran86dd3612014-03-20 22:21:58 +01001028 kfree(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +02001029 kfree(clock);
1030 }
1031
1032 mutex_unlock(&phyter_clocks_lock);
1033}
1034
1035static void dp83640_clock_init(struct dp83640_clock *clock, struct mii_bus *bus)
1036{
1037 INIT_LIST_HEAD(&clock->list);
1038 clock->bus = bus;
1039 mutex_init(&clock->extreg_lock);
1040 mutex_init(&clock->clock_lock);
1041 INIT_LIST_HEAD(&clock->phylist);
1042 clock->caps.owner = THIS_MODULE;
1043 sprintf(clock->caps.name, "dp83640 timer");
1044 clock->caps.max_adj = 1953124;
1045 clock->caps.n_alarm = 0;
1046 clock->caps.n_ext_ts = N_EXT_TS;
Stefan Sørensenad015772014-06-27 12:05:30 +02001047 clock->caps.n_per_out = N_PER_OUT;
Richard Cochran86dd3612014-03-20 22:21:58 +01001048 clock->caps.n_pins = DP83640_N_PINS;
Richard Cochrancb646e22011-04-22 12:04:55 +02001049 clock->caps.pps = 0;
Richard Cochrane4788b82016-11-08 22:49:18 +01001050 clock->caps.adjfine = ptp_dp83640_adjfine;
Richard Cochrancb646e22011-04-22 12:04:55 +02001051 clock->caps.adjtime = ptp_dp83640_adjtime;
Richard Cochran41c2c182015-03-29 23:12:10 +02001052 clock->caps.gettime64 = ptp_dp83640_gettime;
1053 clock->caps.settime64 = ptp_dp83640_settime;
Richard Cochrancb646e22011-04-22 12:04:55 +02001054 clock->caps.enable = ptp_dp83640_enable;
Richard Cochran86dd3612014-03-20 22:21:58 +01001055 clock->caps.verify = ptp_dp83640_verify;
1056 /*
1057 * Convert the module param defaults into a dynamic pin configuration.
1058 */
1059 dp83640_gpio_defaults(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +02001060 /*
1061 * Get a reference to this bus instance.
1062 */
1063 get_device(&bus->dev);
1064}
1065
1066static int choose_this_phy(struct dp83640_clock *clock,
1067 struct phy_device *phydev)
1068{
1069 if (chosen_phy == -1 && !clock->chosen)
1070 return 1;
1071
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001072 if (chosen_phy == phydev->mdio.addr)
Richard Cochrancb646e22011-04-22 12:04:55 +02001073 return 1;
1074
1075 return 0;
1076}
1077
1078static struct dp83640_clock *dp83640_clock_get(struct dp83640_clock *clock)
1079{
1080 if (clock)
1081 mutex_lock(&clock->clock_lock);
1082 return clock;
1083}
1084
1085/*
1086 * Look up and lock a clock by bus instance.
1087 * If there is no clock for this bus, then create it first.
1088 */
1089static struct dp83640_clock *dp83640_clock_get_bus(struct mii_bus *bus)
1090{
1091 struct dp83640_clock *clock = NULL, *tmp;
1092 struct list_head *this;
1093
1094 mutex_lock(&phyter_clocks_lock);
1095
1096 list_for_each(this, &phyter_clocks) {
1097 tmp = list_entry(this, struct dp83640_clock, list);
1098 if (tmp->bus == bus) {
1099 clock = tmp;
1100 break;
1101 }
1102 }
1103 if (clock)
1104 goto out;
1105
1106 clock = kzalloc(sizeof(struct dp83640_clock), GFP_KERNEL);
1107 if (!clock)
1108 goto out;
1109
Kees Cook6396bb22018-06-12 14:03:40 -07001110 clock->caps.pin_config = kcalloc(DP83640_N_PINS,
1111 sizeof(struct ptp_pin_desc),
1112 GFP_KERNEL);
Richard Cochran86dd3612014-03-20 22:21:58 +01001113 if (!clock->caps.pin_config) {
1114 kfree(clock);
1115 clock = NULL;
1116 goto out;
1117 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001118 dp83640_clock_init(clock, bus);
1119 list_add_tail(&phyter_clocks, &clock->list);
1120out:
1121 mutex_unlock(&phyter_clocks_lock);
1122
1123 return dp83640_clock_get(clock);
1124}
1125
1126static void dp83640_clock_put(struct dp83640_clock *clock)
1127{
1128 mutex_unlock(&clock->clock_lock);
1129}
1130
1131static int dp83640_probe(struct phy_device *phydev)
1132{
1133 struct dp83640_clock *clock;
1134 struct dp83640_private *dp83640;
1135 int err = -ENOMEM, i;
1136
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001137 if (phydev->mdio.addr == BROADCAST_ADDR)
Richard Cochrancb646e22011-04-22 12:04:55 +02001138 return 0;
1139
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001140 clock = dp83640_clock_get_bus(phydev->mdio.bus);
Richard Cochrancb646e22011-04-22 12:04:55 +02001141 if (!clock)
1142 goto no_clock;
1143
1144 dp83640 = kzalloc(sizeof(struct dp83640_private), GFP_KERNEL);
1145 if (!dp83640)
1146 goto no_memory;
1147
1148 dp83640->phydev = phydev;
Stefan Sørensen4b063252015-11-03 09:34:05 +01001149 INIT_DELAYED_WORK(&dp83640->ts_work, rx_timestamp_work);
Richard Cochrancb646e22011-04-22 12:04:55 +02001150
1151 INIT_LIST_HEAD(&dp83640->rxts);
1152 INIT_LIST_HEAD(&dp83640->rxpool);
1153 for (i = 0; i < MAX_RXTS; i++)
1154 list_add(&dp83640->rx_pool_data[i].list, &dp83640->rxpool);
1155
1156 phydev->priv = dp83640;
1157
1158 spin_lock_init(&dp83640->rx_lock);
1159 skb_queue_head_init(&dp83640->rx_queue);
1160 skb_queue_head_init(&dp83640->tx_queue);
1161
1162 dp83640->clock = clock;
1163
1164 if (choose_this_phy(clock, phydev)) {
1165 clock->chosen = dp83640;
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001166 clock->ptp_clock = ptp_clock_register(&clock->caps,
1167 &phydev->mdio.dev);
Richard Cochrancb646e22011-04-22 12:04:55 +02001168 if (IS_ERR(clock->ptp_clock)) {
1169 err = PTR_ERR(clock->ptp_clock);
1170 goto no_register;
1171 }
1172 } else
1173 list_add_tail(&dp83640->list, &clock->phylist);
1174
Richard Cochrancb646e22011-04-22 12:04:55 +02001175 dp83640_clock_put(clock);
1176 return 0;
1177
1178no_register:
1179 clock->chosen = NULL;
1180 kfree(dp83640);
1181no_memory:
1182 dp83640_clock_put(clock);
1183no_clock:
1184 return err;
1185}
1186
1187static void dp83640_remove(struct phy_device *phydev)
1188{
1189 struct dp83640_clock *clock;
1190 struct list_head *this, *next;
1191 struct dp83640_private *tmp, *dp83640 = phydev->priv;
1192
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001193 if (phydev->mdio.addr == BROADCAST_ADDR)
Richard Cochrancb646e22011-04-22 12:04:55 +02001194 return;
1195
1196 enable_status_frames(phydev, false);
Stefan Sørensen4b063252015-11-03 09:34:05 +01001197 cancel_delayed_work_sync(&dp83640->ts_work);
Richard Cochrancb646e22011-04-22 12:04:55 +02001198
Alexander Duyckdb91b722014-09-08 11:25:34 -04001199 skb_queue_purge(&dp83640->rx_queue);
1200 skb_queue_purge(&dp83640->tx_queue);
Richard Cochran8b3408f2011-10-21 00:49:17 +00001201
Richard Cochrancb646e22011-04-22 12:04:55 +02001202 clock = dp83640_clock_get(dp83640->clock);
1203
1204 if (dp83640 == clock->chosen) {
1205 ptp_clock_unregister(clock->ptp_clock);
1206 clock->chosen = NULL;
1207 } else {
1208 list_for_each_safe(this, next, &clock->phylist) {
1209 tmp = list_entry(this, struct dp83640_private, list);
1210 if (tmp == dp83640) {
1211 list_del_init(&tmp->list);
1212 break;
1213 }
1214 }
1215 }
1216
1217 dp83640_clock_put(clock);
1218 kfree(dp83640);
1219}
1220
Esben Haabendal76327a32018-04-08 22:17:01 +02001221static int dp83640_soft_reset(struct phy_device *phydev)
1222{
1223 int ret;
1224
1225 ret = genphy_soft_reset(phydev);
1226 if (ret < 0)
1227 return ret;
1228
1229 /* From DP83640 datasheet: "Software driver code must wait 3 us
1230 * following a software reset before allowing further serial MII
1231 * operations with the DP83640."
1232 */
1233 udelay(10); /* Taking udelay inaccuracy into account */
1234
1235 return 0;
1236}
1237
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001238static int dp83640_config_init(struct phy_device *phydev)
1239{
Stefan Sørensen602b1092014-02-13 15:26:57 +01001240 struct dp83640_private *dp83640 = phydev->priv;
1241 struct dp83640_clock *clock = dp83640->clock;
1242
1243 if (clock->chosen && !list_empty(&clock->phylist))
1244 recalibrate(clock);
Richard Cochrana9358652015-05-25 11:55:44 +02001245 else {
1246 mutex_lock(&clock->extreg_lock);
Stefan Sørensen602b1092014-02-13 15:26:57 +01001247 enable_broadcast(phydev, clock->page, 1);
Richard Cochrana9358652015-05-25 11:55:44 +02001248 mutex_unlock(&clock->extreg_lock);
1249 }
Stefan Sørensen602b1092014-02-13 15:26:57 +01001250
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001251 enable_status_frames(phydev, true);
Richard Cochrana9358652015-05-25 11:55:44 +02001252
1253 mutex_lock(&clock->extreg_lock);
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001254 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_ENABLE);
Richard Cochrana9358652015-05-25 11:55:44 +02001255 mutex_unlock(&clock->extreg_lock);
1256
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001257 return 0;
1258}
1259
Stephan Gatzka16421822012-12-04 10:21:38 +00001260static int dp83640_ack_interrupt(struct phy_device *phydev)
1261{
1262 int err = phy_read(phydev, MII_DP83640_MISR);
1263
1264 if (err < 0)
1265 return err;
1266
1267 return 0;
1268}
1269
1270static int dp83640_config_intr(struct phy_device *phydev)
1271{
1272 int micr;
1273 int misr;
1274 int err;
1275
1276 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1277 misr = phy_read(phydev, MII_DP83640_MISR);
1278 if (misr < 0)
1279 return misr;
1280 misr |=
1281 (MII_DP83640_MISR_ANC_INT_EN |
1282 MII_DP83640_MISR_DUP_INT_EN |
1283 MII_DP83640_MISR_SPD_INT_EN |
1284 MII_DP83640_MISR_LINK_INT_EN);
1285 err = phy_write(phydev, MII_DP83640_MISR, misr);
1286 if (err < 0)
1287 return err;
1288
1289 micr = phy_read(phydev, MII_DP83640_MICR);
1290 if (micr < 0)
1291 return micr;
1292 micr |=
1293 (MII_DP83640_MICR_OE |
1294 MII_DP83640_MICR_IE);
1295 return phy_write(phydev, MII_DP83640_MICR, micr);
1296 } else {
1297 micr = phy_read(phydev, MII_DP83640_MICR);
1298 if (micr < 0)
1299 return micr;
1300 micr &=
1301 ~(MII_DP83640_MICR_OE |
1302 MII_DP83640_MICR_IE);
1303 err = phy_write(phydev, MII_DP83640_MICR, micr);
1304 if (err < 0)
1305 return err;
1306
1307 misr = phy_read(phydev, MII_DP83640_MISR);
1308 if (misr < 0)
1309 return misr;
1310 misr &=
1311 ~(MII_DP83640_MISR_ANC_INT_EN |
1312 MII_DP83640_MISR_DUP_INT_EN |
1313 MII_DP83640_MISR_SPD_INT_EN |
1314 MII_DP83640_MISR_LINK_INT_EN);
1315 return phy_write(phydev, MII_DP83640_MISR, misr);
1316 }
1317}
1318
Richard Cochrancb646e22011-04-22 12:04:55 +02001319static int dp83640_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
1320{
1321 struct dp83640_private *dp83640 = phydev->priv;
1322 struct hwtstamp_config cfg;
1323 u16 txcfg0, rxcfg0;
1324
1325 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1326 return -EFAULT;
1327
1328 if (cfg.flags) /* reserved for future extensions */
1329 return -EINVAL;
1330
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001331 if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ONESTEP_SYNC)
Richard Cochrancb646e22011-04-22 12:04:55 +02001332 return -ERANGE;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001333
1334 dp83640->hwts_tx_en = cfg.tx_type;
Richard Cochrancb646e22011-04-22 12:04:55 +02001335
1336 switch (cfg.rx_filter) {
1337 case HWTSTAMP_FILTER_NONE:
1338 dp83640->hwts_rx_en = 0;
1339 dp83640->layer = 0;
1340 dp83640->version = 0;
1341 break;
1342 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1343 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1344 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1345 dp83640->hwts_rx_en = 1;
Stefan Sørensena1f87232015-11-03 09:34:08 +01001346 dp83640->layer = PTP_CLASS_L4;
1347 dp83640->version = PTP_CLASS_V1;
Richard Cochrancb646e22011-04-22 12:04:55 +02001348 break;
1349 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1350 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1351 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1352 dp83640->hwts_rx_en = 1;
Stefan Sørensena1f87232015-11-03 09:34:08 +01001353 dp83640->layer = PTP_CLASS_L4;
1354 dp83640->version = PTP_CLASS_V2;
Richard Cochrancb646e22011-04-22 12:04:55 +02001355 break;
1356 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1357 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1358 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1359 dp83640->hwts_rx_en = 1;
Stefan Sørensena1f87232015-11-03 09:34:08 +01001360 dp83640->layer = PTP_CLASS_L2;
1361 dp83640->version = PTP_CLASS_V2;
Richard Cochrancb646e22011-04-22 12:04:55 +02001362 break;
1363 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1364 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1365 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1366 dp83640->hwts_rx_en = 1;
Stefan Sørensena1f87232015-11-03 09:34:08 +01001367 dp83640->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
1368 dp83640->version = PTP_CLASS_V2;
Richard Cochrancb646e22011-04-22 12:04:55 +02001369 break;
1370 default:
1371 return -ERANGE;
1372 }
1373
1374 txcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1375 rxcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1376
Stefan Sørensena1f87232015-11-03 09:34:08 +01001377 if (dp83640->layer & PTP_CLASS_L2) {
Richard Cochrancb646e22011-04-22 12:04:55 +02001378 txcfg0 |= TX_L2_EN;
1379 rxcfg0 |= RX_L2_EN;
1380 }
Stefan Sørensena1f87232015-11-03 09:34:08 +01001381 if (dp83640->layer & PTP_CLASS_L4) {
Richard Cochrancb646e22011-04-22 12:04:55 +02001382 txcfg0 |= TX_IPV6_EN | TX_IPV4_EN;
1383 rxcfg0 |= RX_IPV6_EN | RX_IPV4_EN;
1384 }
1385
1386 if (dp83640->hwts_tx_en)
1387 txcfg0 |= TX_TS_EN;
1388
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001389 if (dp83640->hwts_tx_en == HWTSTAMP_TX_ONESTEP_SYNC)
1390 txcfg0 |= SYNC_1STEP | CHK_1STEP;
1391
Richard Cochrancb646e22011-04-22 12:04:55 +02001392 if (dp83640->hwts_rx_en)
1393 rxcfg0 |= RX_TS_EN;
1394
1395 mutex_lock(&dp83640->clock->extreg_lock);
1396
Richard Cochrancb646e22011-04-22 12:04:55 +02001397 ext_write(0, phydev, PAGE5, PTP_TXCFG0, txcfg0);
1398 ext_write(0, phydev, PAGE5, PTP_RXCFG0, rxcfg0);
1399
1400 mutex_unlock(&dp83640->clock->extreg_lock);
1401
1402 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1403}
1404
1405static void rx_timestamp_work(struct work_struct *work)
1406{
1407 struct dp83640_private *dp83640 =
Stefan Sørensen4b063252015-11-03 09:34:05 +01001408 container_of(work, struct dp83640_private, ts_work.work);
Richard Cochrancb646e22011-04-22 12:04:55 +02001409 struct sk_buff *skb;
Richard Cochrancb646e22011-04-22 12:04:55 +02001410
Stefan Sørensen63502b82014-07-22 15:20:45 +02001411 /* Deliver expired packets. */
1412 while ((skb = skb_dequeue(&dp83640->rx_queue))) {
1413 struct dp83640_skb_info *skb_info;
Richard Cochrancb646e22011-04-22 12:04:55 +02001414
Stefan Sørensen63502b82014-07-22 15:20:45 +02001415 skb_info = (struct dp83640_skb_info *)skb->cb;
1416 if (!time_after(jiffies, skb_info->tmo)) {
1417 skb_queue_head(&dp83640->rx_queue, skb);
1418 break;
Richard Cochrancb646e22011-04-22 12:04:55 +02001419 }
Stefan Sørensen63502b82014-07-22 15:20:45 +02001420
Manfred Rudigier72092cc2012-01-09 23:52:15 +00001421 netif_rx_ni(skb);
Richard Cochrancb646e22011-04-22 12:04:55 +02001422 }
1423
Stefan Sørensen63502b82014-07-22 15:20:45 +02001424 if (!skb_queue_empty(&dp83640->rx_queue))
Stefan Sørensen4b063252015-11-03 09:34:05 +01001425 schedule_delayed_work(&dp83640->ts_work, SKB_TIMESTAMP_TIMEOUT);
Richard Cochrancb646e22011-04-22 12:04:55 +02001426}
1427
1428static bool dp83640_rxtstamp(struct phy_device *phydev,
1429 struct sk_buff *skb, int type)
1430{
1431 struct dp83640_private *dp83640 = phydev->priv;
Stefan Sørensen63502b82014-07-22 15:20:45 +02001432 struct dp83640_skb_info *skb_info = (struct dp83640_skb_info *)skb->cb;
1433 struct list_head *this, *next;
1434 struct rxts *rxts;
1435 struct skb_shared_hwtstamps *shhwtstamps = NULL;
1436 unsigned long flags;
Richard Cochrancb646e22011-04-22 12:04:55 +02001437
Richard Cochrancb646e22011-04-22 12:04:55 +02001438 if (is_status_frame(skb, type)) {
1439 decode_status_frame(dp83640, skb);
Richard Cochranae6e86b2011-06-14 23:55:20 +00001440 kfree_skb(skb);
1441 return true;
Richard Cochrancb646e22011-04-22 12:04:55 +02001442 }
1443
Stefan Sørensena12f78c2014-06-25 14:40:16 +02001444 if (!dp83640->hwts_rx_en)
1445 return false;
1446
Stefan Sørensena1f87232015-11-03 09:34:08 +01001447 if ((type & dp83640->version) == 0 || (type & dp83640->layer) == 0)
1448 return false;
1449
Stefan Sørensen63502b82014-07-22 15:20:45 +02001450 spin_lock_irqsave(&dp83640->rx_lock, flags);
Stefan Sørensenccf6ee92015-11-03 09:34:06 +01001451 prune_rx_ts(dp83640);
Stefan Sørensen63502b82014-07-22 15:20:45 +02001452 list_for_each_safe(this, next, &dp83640->rxts) {
1453 rxts = list_entry(this, struct rxts, list);
1454 if (match(skb, type, rxts)) {
1455 shhwtstamps = skb_hwtstamps(skb);
1456 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
1457 shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
Stefan Sørensen63502b82014-07-22 15:20:45 +02001458 list_del_init(&rxts->list);
1459 list_add(&rxts->list, &dp83640->rxpool);
1460 break;
1461 }
1462 }
1463 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
1464
1465 if (!shhwtstamps) {
1466 skb_info->ptp_type = type;
Stefan Sørensen4b063252015-11-03 09:34:05 +01001467 skb_info->tmo = jiffies + SKB_TIMESTAMP_TIMEOUT;
Stefan Sørensen63502b82014-07-22 15:20:45 +02001468 skb_queue_tail(&dp83640->rx_queue, skb);
Stefan Sørensen4b063252015-11-03 09:34:05 +01001469 schedule_delayed_work(&dp83640->ts_work, SKB_TIMESTAMP_TIMEOUT);
Stefan Sørensend36b82b2017-08-30 08:58:47 +02001470 } else {
1471 netif_rx_ni(skb);
Stefan Sørensen63502b82014-07-22 15:20:45 +02001472 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001473
1474 return true;
1475}
1476
1477static void dp83640_txtstamp(struct phy_device *phydev,
1478 struct sk_buff *skb, int type)
1479{
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +01001480 struct dp83640_skb_info *skb_info = (struct dp83640_skb_info *)skb->cb;
Richard Cochrancb646e22011-04-22 12:04:55 +02001481 struct dp83640_private *dp83640 = phydev->priv;
1482
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001483 switch (dp83640->hwts_tx_en) {
1484
1485 case HWTSTAMP_TX_ONESTEP_SYNC:
1486 if (is_sync(skb, type)) {
Alexander Duyck62bccb82014-09-04 13:31:35 -04001487 kfree_skb(skb);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001488 return;
1489 }
1490 /* fall through */
1491 case HWTSTAMP_TX_ON:
Stefan Sørensene2e2f512014-02-03 15:36:35 +01001492 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Sebastian Andrzej Siewior53bc8d22019-02-04 11:20:29 +01001493 skb_info->tmo = jiffies + SKB_TIMESTAMP_TIMEOUT;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001494 skb_queue_tail(&dp83640->tx_queue, skb);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001495 break;
1496
1497 case HWTSTAMP_TX_OFF:
1498 default:
Alexander Duyck62bccb82014-09-04 13:31:35 -04001499 kfree_skb(skb);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001500 break;
Richard Cochrancb646e22011-04-22 12:04:55 +02001501 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001502}
1503
Richard Cochran7dff3492012-04-03 22:59:18 +00001504static int dp83640_ts_info(struct phy_device *dev, struct ethtool_ts_info *info)
1505{
1506 struct dp83640_private *dp83640 = dev->priv;
1507
1508 info->so_timestamping =
1509 SOF_TIMESTAMPING_TX_HARDWARE |
1510 SOF_TIMESTAMPING_RX_HARDWARE |
1511 SOF_TIMESTAMPING_RAW_HARDWARE;
1512 info->phc_index = ptp_clock_index(dp83640->clock->ptp_clock);
1513 info->tx_types =
1514 (1 << HWTSTAMP_TX_OFF) |
1515 (1 << HWTSTAMP_TX_ON) |
1516 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
1517 info->rx_filters =
1518 (1 << HWTSTAMP_FILTER_NONE) |
1519 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
Richard Cochran7dff3492012-04-03 22:59:18 +00001520 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
Richard Cochran7dff3492012-04-03 22:59:18 +00001521 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
Jacob Keller11b15442015-04-22 14:40:37 -07001522 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
Richard Cochran7dff3492012-04-03 22:59:18 +00001523 return 0;
1524}
1525
Richard Cochrancb646e22011-04-22 12:04:55 +02001526static struct phy_driver dp83640_driver = {
1527 .phy_id = DP83640_PHY_ID,
1528 .phy_id_mask = 0xfffffff0,
1529 .name = "NatSemi DP83640",
1530 .features = PHY_BASIC_FEATURES,
Richard Cochrancb646e22011-04-22 12:04:55 +02001531 .probe = dp83640_probe,
1532 .remove = dp83640_remove,
Esben Haabendal76327a32018-04-08 22:17:01 +02001533 .soft_reset = dp83640_soft_reset,
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001534 .config_init = dp83640_config_init,
Stephan Gatzka16421822012-12-04 10:21:38 +00001535 .ack_interrupt = dp83640_ack_interrupt,
1536 .config_intr = dp83640_config_intr,
Richard Cochran7dff3492012-04-03 22:59:18 +00001537 .ts_info = dp83640_ts_info,
Richard Cochrancb646e22011-04-22 12:04:55 +02001538 .hwtstamp = dp83640_hwtstamp,
1539 .rxtstamp = dp83640_rxtstamp,
1540 .txtstamp = dp83640_txtstamp,
Richard Cochrancb646e22011-04-22 12:04:55 +02001541};
1542
1543static int __init dp83640_init(void)
1544{
Andrew Lunnbe01da72016-01-06 20:11:22 +01001545 return phy_driver_register(&dp83640_driver, THIS_MODULE);
Richard Cochrancb646e22011-04-22 12:04:55 +02001546}
1547
1548static void __exit dp83640_exit(void)
1549{
1550 dp83640_free_clocks();
1551 phy_driver_unregister(&dp83640_driver);
1552}
1553
1554MODULE_DESCRIPTION("National Semiconductor DP83640 PHY driver");
Richard Cochranfbf4b932014-03-20 22:21:56 +01001555MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
Richard Cochrancb646e22011-04-22 12:04:55 +02001556MODULE_LICENSE("GPL");
1557
1558module_init(dp83640_init);
1559module_exit(dp83640_exit);
1560
John Stultz86ff9baa2011-05-23 13:32:11 -07001561static struct mdio_device_id __maybe_unused dp83640_tbl[] = {
Richard Cochrancb646e22011-04-22 12:04:55 +02001562 { DP83640_PHY_ID, 0xfffffff0 },
1563 { }
1564};
1565
1566MODULE_DEVICE_TABLE(mdio, dp83640_tbl);