blob: e570036275e26aa6ff7ad60762b69ec95ecf3153 [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
Richard Cochrancb646e22011-04-22 12:04:55 +020023#include <linux/ethtool.h>
24#include <linux/kernel.h>
25#include <linux/list.h>
26#include <linux/mii.h>
27#include <linux/module.h>
28#include <linux/net_tstamp.h>
29#include <linux/netdevice.h>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020030#include <linux/if_vlan.h>
Richard Cochrancb646e22011-04-22 12:04:55 +020031#include <linux/phy.h>
32#include <linux/ptp_classify.h>
33#include <linux/ptp_clock_kernel.h>
34
35#include "dp83640_reg.h"
36
37#define DP83640_PHY_ID 0x20005ce1
38#define PAGESEL 0x13
39#define LAYER4 0x02
40#define LAYER2 0x01
Richard Cochran80288372011-08-06 21:03:04 +000041#define MAX_RXTS 64
Richard Cochran49b3fd42011-09-20 01:43:14 +000042#define N_EXT_TS 6
Stefan Sørensenad015772014-06-27 12:05:30 +020043#define N_PER_OUT 7
Richard Cochrancb646e22011-04-22 12:04:55 +020044#define PSF_PTPVER 2
45#define PSF_EVNT 0x4000
46#define PSF_RX 0x2000
47#define PSF_TX 0x1000
48#define EXT_EVENT 1
Richard Cochran49b3fd42011-09-20 01:43:14 +000049#define CAL_EVENT 7
Richard Cochran397a2532015-05-25 11:55:43 +020050#define CAL_TRIGGER 1
Richard Cochran86dd3612014-03-20 22:21:58 +010051#define DP83640_N_PINS 12
Richard Cochrancb646e22011-04-22 12:04:55 +020052
Stephan Gatzka16421822012-12-04 10:21:38 +000053#define MII_DP83640_MICR 0x11
54#define MII_DP83640_MISR 0x12
55
56#define MII_DP83640_MICR_OE 0x1
57#define MII_DP83640_MICR_IE 0x2
58
59#define MII_DP83640_MISR_RHF_INT_EN 0x01
60#define MII_DP83640_MISR_FHF_INT_EN 0x02
61#define MII_DP83640_MISR_ANC_INT_EN 0x04
62#define MII_DP83640_MISR_DUP_INT_EN 0x08
63#define MII_DP83640_MISR_SPD_INT_EN 0x10
64#define MII_DP83640_MISR_LINK_INT_EN 0x20
65#define MII_DP83640_MISR_ED_INT_EN 0x40
66#define MII_DP83640_MISR_LQ_INT_EN 0x80
67
Richard Cochrancb646e22011-04-22 12:04:55 +020068/* phyter seems to miss the mark by 16 ns */
69#define ADJTIME_FIX 16
70
71#if defined(__BIG_ENDIAN)
72#define ENDIAN_FLAG 0
73#elif defined(__LITTLE_ENDIAN)
74#define ENDIAN_FLAG PSF_ENDIAN
75#endif
76
Stefan Sørensen63502b82014-07-22 15:20:45 +020077struct dp83640_skb_info {
78 int ptp_type;
79 unsigned long tmo;
80};
Richard Cochrancb646e22011-04-22 12:04:55 +020081
82struct phy_rxts {
83 u16 ns_lo; /* ns[15:0] */
84 u16 ns_hi; /* overflow[1:0], ns[29:16] */
85 u16 sec_lo; /* sec[15:0] */
86 u16 sec_hi; /* sec[31:16] */
87 u16 seqid; /* sequenceId[15:0] */
88 u16 msgtype; /* messageType[3:0], hash[11:0] */
89};
90
91struct phy_txts {
92 u16 ns_lo; /* ns[15:0] */
93 u16 ns_hi; /* overflow[1:0], ns[29:16] */
94 u16 sec_lo; /* sec[15:0] */
95 u16 sec_hi; /* sec[31:16] */
96};
97
98struct rxts {
99 struct list_head list;
100 unsigned long tmo;
101 u64 ns;
102 u16 seqid;
103 u8 msgtype;
104 u16 hash;
105};
106
107struct dp83640_clock;
108
109struct dp83640_private {
110 struct list_head list;
111 struct dp83640_clock *clock;
112 struct phy_device *phydev;
113 struct work_struct ts_work;
114 int hwts_tx_en;
115 int hwts_rx_en;
116 int layer;
117 int version;
118 /* remember state of cfg0 during calibration */
119 int cfg0;
120 /* remember the last event time stamp */
121 struct phy_txts edata;
122 /* list of rx timestamps */
123 struct list_head rxts;
124 struct list_head rxpool;
125 struct rxts rx_pool_data[MAX_RXTS];
126 /* protects above three fields from concurrent access */
127 spinlock_t rx_lock;
128 /* queues of incoming and outgoing packets */
129 struct sk_buff_head rx_queue;
130 struct sk_buff_head tx_queue;
131};
132
133struct dp83640_clock {
134 /* keeps the instance in the 'phyter_clocks' list */
135 struct list_head list;
136 /* we create one clock instance per MII bus */
137 struct mii_bus *bus;
138 /* protects extended registers from concurrent access */
139 struct mutex extreg_lock;
140 /* remembers which page was last selected */
141 int page;
142 /* our advertised capabilities */
143 struct ptp_clock_info caps;
144 /* protects the three fields below from concurrent access */
145 struct mutex clock_lock;
146 /* the one phyter from which we shall read */
147 struct dp83640_private *chosen;
148 /* list of the other attached phyters, not chosen */
149 struct list_head phylist;
150 /* reference to our PTP hardware clock */
151 struct ptp_clock *ptp_clock;
152};
153
154/* globals */
155
Richard Cochran49b3fd42011-09-20 01:43:14 +0000156enum {
157 CALIBRATE_GPIO,
158 PEROUT_GPIO,
159 EXTTS0_GPIO,
160 EXTTS1_GPIO,
161 EXTTS2_GPIO,
162 EXTTS3_GPIO,
163 EXTTS4_GPIO,
164 EXTTS5_GPIO,
165 GPIO_TABLE_SIZE
166};
167
Richard Cochrancb646e22011-04-22 12:04:55 +0200168static int chosen_phy = -1;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000169static ushort gpio_tab[GPIO_TABLE_SIZE] = {
170 1, 2, 3, 4, 8, 9, 10, 11
171};
Richard Cochrancb646e22011-04-22 12:04:55 +0200172
173module_param(chosen_phy, int, 0444);
Richard Cochran49b3fd42011-09-20 01:43:14 +0000174module_param_array(gpio_tab, ushort, NULL, 0444);
Richard Cochrancb646e22011-04-22 12:04:55 +0200175
176MODULE_PARM_DESC(chosen_phy, \
177 "The address of the PHY to use for the ancillary clock features");
Richard Cochran49b3fd42011-09-20 01:43:14 +0000178MODULE_PARM_DESC(gpio_tab, \
179 "Which GPIO line to use for which purpose: cal,perout,extts1,...,extts6");
Richard Cochrancb646e22011-04-22 12:04:55 +0200180
Richard Cochran86dd3612014-03-20 22:21:58 +0100181static void dp83640_gpio_defaults(struct ptp_pin_desc *pd)
182{
183 int i, index;
184
185 for (i = 0; i < DP83640_N_PINS; i++) {
186 snprintf(pd[i].name, sizeof(pd[i].name), "GPIO%d", 1 + i);
187 pd[i].index = i;
188 }
189
190 for (i = 0; i < GPIO_TABLE_SIZE; i++) {
191 if (gpio_tab[i] < 1 || gpio_tab[i] > DP83640_N_PINS) {
192 pr_err("gpio_tab[%d]=%hu out of range", i, gpio_tab[i]);
193 return;
194 }
195 }
196
197 index = gpio_tab[CALIBRATE_GPIO] - 1;
198 pd[index].func = PTP_PF_PHYSYNC;
199 pd[index].chan = 0;
200
201 index = gpio_tab[PEROUT_GPIO] - 1;
202 pd[index].func = PTP_PF_PEROUT;
203 pd[index].chan = 0;
204
205 for (i = EXTTS0_GPIO; i < GPIO_TABLE_SIZE; i++) {
206 index = gpio_tab[i] - 1;
207 pd[index].func = PTP_PF_EXTTS;
208 pd[index].chan = i - EXTTS0_GPIO;
209 }
210}
211
Richard Cochrancb646e22011-04-22 12:04:55 +0200212/* a list of clocks and a mutex to protect it */
213static LIST_HEAD(phyter_clocks);
214static DEFINE_MUTEX(phyter_clocks_lock);
215
216static void rx_timestamp_work(struct work_struct *work);
217
218/* extended register access functions */
219
220#define BROADCAST_ADDR 31
221
222static inline int broadcast_write(struct mii_bus *bus, u32 regnum, u16 val)
223{
224 return mdiobus_write(bus, BROADCAST_ADDR, regnum, val);
225}
226
227/* Caller must hold extreg_lock. */
228static int ext_read(struct phy_device *phydev, int page, u32 regnum)
229{
230 struct dp83640_private *dp83640 = phydev->priv;
231 int val;
232
233 if (dp83640->clock->page != page) {
234 broadcast_write(phydev->bus, PAGESEL, page);
235 dp83640->clock->page = page;
236 }
237 val = phy_read(phydev, regnum);
238
239 return val;
240}
241
242/* Caller must hold extreg_lock. */
243static void ext_write(int broadcast, struct phy_device *phydev,
244 int page, u32 regnum, u16 val)
245{
246 struct dp83640_private *dp83640 = phydev->priv;
247
248 if (dp83640->clock->page != page) {
249 broadcast_write(phydev->bus, PAGESEL, page);
250 dp83640->clock->page = page;
251 }
252 if (broadcast)
253 broadcast_write(phydev->bus, regnum, val);
254 else
255 phy_write(phydev, regnum, val);
256}
257
258/* Caller must hold extreg_lock. */
259static int tdr_write(int bc, struct phy_device *dev,
Richard Cochran41c2c182015-03-29 23:12:10 +0200260 const struct timespec64 *ts, u16 cmd)
Richard Cochrancb646e22011-04-22 12:04:55 +0200261{
262 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec & 0xffff);/* ns[15:0] */
263 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec >> 16); /* ns[31:16] */
264 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec & 0xffff); /* sec[15:0] */
265 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec >> 16); /* sec[31:16]*/
266
267 ext_write(bc, dev, PAGE4, PTP_CTL, cmd);
268
269 return 0;
270}
271
272/* convert phy timestamps into driver timestamps */
273
274static void phy2rxts(struct phy_rxts *p, struct rxts *rxts)
275{
276 u32 sec;
277
278 sec = p->sec_lo;
279 sec |= p->sec_hi << 16;
280
281 rxts->ns = p->ns_lo;
282 rxts->ns |= (p->ns_hi & 0x3fff) << 16;
283 rxts->ns += ((u64)sec) * 1000000000ULL;
284 rxts->seqid = p->seqid;
285 rxts->msgtype = (p->msgtype >> 12) & 0xf;
286 rxts->hash = p->msgtype & 0x0fff;
Richard Cochran80288372011-08-06 21:03:04 +0000287 rxts->tmo = jiffies + 2;
Richard Cochrancb646e22011-04-22 12:04:55 +0200288}
289
290static u64 phy2txts(struct phy_txts *p)
291{
292 u64 ns;
293 u32 sec;
294
295 sec = p->sec_lo;
296 sec |= p->sec_hi << 16;
297
298 ns = p->ns_lo;
299 ns |= (p->ns_hi & 0x3fff) << 16;
300 ns += ((u64)sec) * 1000000000ULL;
301
302 return ns;
303}
304
Richard Cochran621bdec2014-03-20 22:22:00 +0100305static int periodic_output(struct dp83640_clock *clock,
Stefan Sørensenad015772014-06-27 12:05:30 +0200306 struct ptp_clock_request *clkreq, bool on,
307 int trigger)
Richard Cochran49b3fd42011-09-20 01:43:14 +0000308{
309 struct dp83640_private *dp83640 = clock->chosen;
310 struct phy_device *phydev = dp83640->phydev;
Richard Cochran564ca562014-03-20 22:21:57 +0100311 u32 sec, nsec, pwidth;
Stefan Sørensenad015772014-06-27 12:05:30 +0200312 u16 gpio, ptp_trig, val;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000313
Richard Cochran621bdec2014-03-20 22:22:00 +0100314 if (on) {
Stefan Sørensenad015772014-06-27 12:05:30 +0200315 gpio = 1 + ptp_find_pin(clock->ptp_clock, PTP_PF_PEROUT,
316 trigger);
Richard Cochran621bdec2014-03-20 22:22:00 +0100317 if (gpio < 1)
318 return -EINVAL;
319 } else {
320 gpio = 0;
321 }
322
Richard Cochran49b3fd42011-09-20 01:43:14 +0000323 ptp_trig = TRIG_WR |
324 (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT |
325 (gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT |
326 TRIG_PER |
327 TRIG_PULSE;
328
329 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
330
331 if (!on) {
332 val |= TRIG_DIS;
333 mutex_lock(&clock->extreg_lock);
334 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
335 ext_write(0, phydev, PAGE4, PTP_CTL, val);
336 mutex_unlock(&clock->extreg_lock);
Richard Cochran621bdec2014-03-20 22:22:00 +0100337 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000338 }
339
340 sec = clkreq->perout.start.sec;
341 nsec = clkreq->perout.start.nsec;
Richard Cochran564ca562014-03-20 22:21:57 +0100342 pwidth = clkreq->perout.period.sec * 1000000000UL;
343 pwidth += clkreq->perout.period.nsec;
344 pwidth /= 2;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000345
346 mutex_lock(&clock->extreg_lock);
347
348 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
349
350 /*load trigger*/
351 val |= TRIG_LOAD;
352 ext_write(0, phydev, PAGE4, PTP_CTL, val);
353 ext_write(0, phydev, PAGE4, PTP_TDR, nsec & 0xffff); /* ns[15:0] */
354 ext_write(0, phydev, PAGE4, PTP_TDR, nsec >> 16); /* ns[31:16] */
355 ext_write(0, phydev, PAGE4, PTP_TDR, sec & 0xffff); /* sec[15:0] */
356 ext_write(0, phydev, PAGE4, PTP_TDR, sec >> 16); /* sec[31:16] */
Richard Cochran564ca562014-03-20 22:21:57 +0100357 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff); /* ns[15:0] */
358 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16); /* ns[31:16] */
Stefan Sørensen35e872a2014-06-27 12:05:29 +0200359 /* Triggers 0 and 1 has programmable pulsewidth2 */
360 if (trigger < 2) {
361 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff);
362 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16);
363 }
Richard Cochran49b3fd42011-09-20 01:43:14 +0000364
365 /*enable trigger*/
366 val &= ~TRIG_LOAD;
367 val |= TRIG_EN;
368 ext_write(0, phydev, PAGE4, PTP_CTL, val);
369
370 mutex_unlock(&clock->extreg_lock);
Richard Cochran621bdec2014-03-20 22:22:00 +0100371 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000372}
373
Richard Cochrancb646e22011-04-22 12:04:55 +0200374/* ptp clock methods */
375
376static int ptp_dp83640_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
377{
378 struct dp83640_clock *clock =
379 container_of(ptp, struct dp83640_clock, caps);
380 struct phy_device *phydev = clock->chosen->phydev;
381 u64 rate;
382 int neg_adj = 0;
383 u16 hi, lo;
384
385 if (ppb < 0) {
386 neg_adj = 1;
387 ppb = -ppb;
388 }
389 rate = ppb;
390 rate <<= 26;
391 rate = div_u64(rate, 1953125);
392
393 hi = (rate >> 16) & PTP_RATE_HI_MASK;
394 if (neg_adj)
395 hi |= PTP_RATE_DIR;
396
397 lo = rate & 0xffff;
398
399 mutex_lock(&clock->extreg_lock);
400
401 ext_write(1, phydev, PAGE4, PTP_RATEH, hi);
402 ext_write(1, phydev, PAGE4, PTP_RATEL, lo);
403
404 mutex_unlock(&clock->extreg_lock);
405
406 return 0;
407}
408
409static int ptp_dp83640_adjtime(struct ptp_clock_info *ptp, s64 delta)
410{
411 struct dp83640_clock *clock =
412 container_of(ptp, struct dp83640_clock, caps);
413 struct phy_device *phydev = clock->chosen->phydev;
Richard Cochran41c2c182015-03-29 23:12:10 +0200414 struct timespec64 ts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200415 int err;
416
417 delta += ADJTIME_FIX;
418
Richard Cochran41c2c182015-03-29 23:12:10 +0200419 ts = ns_to_timespec64(delta);
Richard Cochrancb646e22011-04-22 12:04:55 +0200420
421 mutex_lock(&clock->extreg_lock);
422
423 err = tdr_write(1, phydev, &ts, PTP_STEP_CLK);
424
425 mutex_unlock(&clock->extreg_lock);
426
427 return err;
428}
429
Richard Cochran41c2c182015-03-29 23:12:10 +0200430static int ptp_dp83640_gettime(struct ptp_clock_info *ptp,
431 struct timespec64 *ts)
Richard Cochrancb646e22011-04-22 12:04:55 +0200432{
433 struct dp83640_clock *clock =
434 container_of(ptp, struct dp83640_clock, caps);
435 struct phy_device *phydev = clock->chosen->phydev;
436 unsigned int val[4];
437
438 mutex_lock(&clock->extreg_lock);
439
440 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_RD_CLK);
441
442 val[0] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[15:0] */
443 val[1] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[31:16] */
444 val[2] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[15:0] */
445 val[3] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[31:16] */
446
447 mutex_unlock(&clock->extreg_lock);
448
449 ts->tv_nsec = val[0] | (val[1] << 16);
450 ts->tv_sec = val[2] | (val[3] << 16);
451
452 return 0;
453}
454
455static int ptp_dp83640_settime(struct ptp_clock_info *ptp,
Richard Cochran41c2c182015-03-29 23:12:10 +0200456 const struct timespec64 *ts)
Richard Cochrancb646e22011-04-22 12:04:55 +0200457{
458 struct dp83640_clock *clock =
459 container_of(ptp, struct dp83640_clock, caps);
460 struct phy_device *phydev = clock->chosen->phydev;
461 int err;
462
463 mutex_lock(&clock->extreg_lock);
464
465 err = tdr_write(1, phydev, ts, PTP_LOAD_CLK);
466
467 mutex_unlock(&clock->extreg_lock);
468
469 return err;
470}
471
472static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
473 struct ptp_clock_request *rq, int on)
474{
475 struct dp83640_clock *clock =
476 container_of(ptp, struct dp83640_clock, caps);
477 struct phy_device *phydev = clock->chosen->phydev;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100478 unsigned int index;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000479 u16 evnt, event_num, gpio_num;
Richard Cochrancb646e22011-04-22 12:04:55 +0200480
481 switch (rq->type) {
482 case PTP_CLK_REQ_EXTTS:
Richard Cochran49b3fd42011-09-20 01:43:14 +0000483 index = rq->extts.index;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100484 if (index >= N_EXT_TS)
Richard Cochrancb646e22011-04-22 12:04:55 +0200485 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000486 event_num = EXT_EVENT + index;
487 evnt = EVNT_WR | (event_num & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
Richard Cochrancb646e22011-04-22 12:04:55 +0200488 if (on) {
Richard Cochranfaa89712014-03-20 22:21:59 +0100489 gpio_num = 1 + ptp_find_pin(clock->ptp_clock,
490 PTP_PF_EXTTS, index);
491 if (gpio_num < 1)
492 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000493 evnt |= (gpio_num & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
Stefan Sørensen80671bd2014-02-03 15:36:50 +0100494 if (rq->extts.flags & PTP_FALLING_EDGE)
495 evnt |= EVNT_FALL;
496 else
497 evnt |= EVNT_RISE;
Richard Cochrancb646e22011-04-22 12:04:55 +0200498 }
Richard Cochrana9358652015-05-25 11:55:44 +0200499 mutex_lock(&clock->extreg_lock);
Richard Cochrancb646e22011-04-22 12:04:55 +0200500 ext_write(0, phydev, PAGE5, PTP_EVNT, evnt);
Richard Cochrana9358652015-05-25 11:55:44 +0200501 mutex_unlock(&clock->extreg_lock);
Richard Cochrancb646e22011-04-22 12:04:55 +0200502 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000503
504 case PTP_CLK_REQ_PEROUT:
Stefan Sørensenad015772014-06-27 12:05:30 +0200505 if (rq->perout.index >= N_PER_OUT)
Richard Cochran49b3fd42011-09-20 01:43:14 +0000506 return -EINVAL;
Stefan Sørensenad015772014-06-27 12:05:30 +0200507 return periodic_output(clock, rq, on, rq->perout.index);
Richard Cochran49b3fd42011-09-20 01:43:14 +0000508
Richard Cochrancb646e22011-04-22 12:04:55 +0200509 default:
510 break;
511 }
512
513 return -EOPNOTSUPP;
514}
515
Richard Cochran86dd3612014-03-20 22:21:58 +0100516static int ptp_dp83640_verify(struct ptp_clock_info *ptp, unsigned int pin,
517 enum ptp_pin_function func, unsigned int chan)
518{
Stefan Sørensen6f39eb82014-06-27 12:05:31 +0200519 struct dp83640_clock *clock =
520 container_of(ptp, struct dp83640_clock, caps);
521
522 if (clock->caps.pin_config[pin].func == PTP_PF_PHYSYNC &&
523 !list_empty(&clock->phylist))
524 return 1;
525
526 if (func == PTP_PF_PHYSYNC)
527 return 1;
528
Richard Cochran86dd3612014-03-20 22:21:58 +0100529 return 0;
530}
531
Richard Cochrancb646e22011-04-22 12:04:55 +0200532static u8 status_frame_dst[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 };
533static u8 status_frame_src[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F };
534
535static void enable_status_frames(struct phy_device *phydev, bool on)
536{
Richard Cochrana9358652015-05-25 11:55:44 +0200537 struct dp83640_private *dp83640 = phydev->priv;
538 struct dp83640_clock *clock = dp83640->clock;
Richard Cochrancb646e22011-04-22 12:04:55 +0200539 u16 cfg0 = 0, ver;
540
541 if (on)
542 cfg0 = PSF_EVNT_EN | PSF_RXTS_EN | PSF_TXTS_EN | ENDIAN_FLAG;
543
544 ver = (PSF_PTPVER & VERSIONPTP_MASK) << VERSIONPTP_SHIFT;
545
Richard Cochrana9358652015-05-25 11:55:44 +0200546 mutex_lock(&clock->extreg_lock);
547
Richard Cochrancb646e22011-04-22 12:04:55 +0200548 ext_write(0, phydev, PAGE5, PSF_CFG0, cfg0);
549 ext_write(0, phydev, PAGE6, PSF_CFG1, ver);
550
Richard Cochrana9358652015-05-25 11:55:44 +0200551 mutex_unlock(&clock->extreg_lock);
552
Richard Cochrancb646e22011-04-22 12:04:55 +0200553 if (!phydev->attached_dev) {
Joe Perches8d242482012-06-09 07:49:07 +0000554 pr_warn("expected to find an attached netdevice\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200555 return;
556 }
557
558 if (on) {
559 if (dev_mc_add(phydev->attached_dev, status_frame_dst))
Joe Perches8d242482012-06-09 07:49:07 +0000560 pr_warn("failed to add mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200561 } else {
562 if (dev_mc_del(phydev->attached_dev, status_frame_dst))
Joe Perches8d242482012-06-09 07:49:07 +0000563 pr_warn("failed to delete mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200564 }
565}
566
567static bool is_status_frame(struct sk_buff *skb, int type)
568{
569 struct ethhdr *h = eth_hdr(skb);
570
571 if (PTP_CLASS_V2_L2 == type &&
572 !memcmp(h->h_source, status_frame_src, sizeof(status_frame_src)))
573 return true;
574 else
575 return false;
576}
577
578static int expired(struct rxts *rxts)
579{
580 return time_after(jiffies, rxts->tmo);
581}
582
583/* Caller must hold rx_lock. */
584static void prune_rx_ts(struct dp83640_private *dp83640)
585{
586 struct list_head *this, *next;
587 struct rxts *rxts;
588
589 list_for_each_safe(this, next, &dp83640->rxts) {
590 rxts = list_entry(this, struct rxts, list);
591 if (expired(rxts)) {
592 list_del_init(&rxts->list);
593 list_add(&rxts->list, &dp83640->rxpool);
594 }
595 }
596}
597
598/* synchronize the phyters so they act as one clock */
599
600static void enable_broadcast(struct phy_device *phydev, int init_page, int on)
601{
602 int val;
603 phy_write(phydev, PAGESEL, 0);
604 val = phy_read(phydev, PHYCR2);
605 if (on)
606 val |= BC_WRITE;
607 else
608 val &= ~BC_WRITE;
609 phy_write(phydev, PHYCR2, val);
610 phy_write(phydev, PAGESEL, init_page);
611}
612
613static void recalibrate(struct dp83640_clock *clock)
614{
615 s64 now, diff;
616 struct phy_txts event_ts;
Richard Cochran41c2c182015-03-29 23:12:10 +0200617 struct timespec64 ts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200618 struct list_head *this;
619 struct dp83640_private *tmp;
620 struct phy_device *master = clock->chosen->phydev;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000621 u16 cal_gpio, cfg0, evnt, ptp_trig, trigger, val;
Richard Cochrancb646e22011-04-22 12:04:55 +0200622
623 trigger = CAL_TRIGGER;
Stefan Sørensene0155952014-06-27 12:05:32 +0200624 cal_gpio = 1 + ptp_find_pin(clock->ptp_clock, PTP_PF_PHYSYNC, 0);
625 if (cal_gpio < 1) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +0900626 pr_err("PHY calibration pin not available - PHY is not calibrated.");
Stefan Sørensene0155952014-06-27 12:05:32 +0200627 return;
628 }
Richard Cochrancb646e22011-04-22 12:04:55 +0200629
630 mutex_lock(&clock->extreg_lock);
631
632 /*
633 * enable broadcast, disable status frames, enable ptp clock
634 */
635 list_for_each(this, &clock->phylist) {
636 tmp = list_entry(this, struct dp83640_private, list);
637 enable_broadcast(tmp->phydev, clock->page, 1);
638 tmp->cfg0 = ext_read(tmp->phydev, PAGE5, PSF_CFG0);
639 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, 0);
640 ext_write(0, tmp->phydev, PAGE4, PTP_CTL, PTP_ENABLE);
641 }
642 enable_broadcast(master, clock->page, 1);
643 cfg0 = ext_read(master, PAGE5, PSF_CFG0);
644 ext_write(0, master, PAGE5, PSF_CFG0, 0);
645 ext_write(0, master, PAGE4, PTP_CTL, PTP_ENABLE);
646
647 /*
648 * enable an event timestamp
649 */
650 evnt = EVNT_WR | EVNT_RISE | EVNT_SINGLE;
651 evnt |= (CAL_EVENT & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
652 evnt |= (cal_gpio & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
653
654 list_for_each(this, &clock->phylist) {
655 tmp = list_entry(this, struct dp83640_private, list);
656 ext_write(0, tmp->phydev, PAGE5, PTP_EVNT, evnt);
657 }
658 ext_write(0, master, PAGE5, PTP_EVNT, evnt);
659
660 /*
661 * configure a trigger
662 */
663 ptp_trig = TRIG_WR | TRIG_IF_LATE | TRIG_PULSE;
664 ptp_trig |= (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT;
665 ptp_trig |= (cal_gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT;
666 ext_write(0, master, PAGE5, PTP_TRIG, ptp_trig);
667
668 /* load trigger */
669 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
670 val |= TRIG_LOAD;
671 ext_write(0, master, PAGE4, PTP_CTL, val);
672
673 /* enable trigger */
674 val &= ~TRIG_LOAD;
675 val |= TRIG_EN;
676 ext_write(0, master, PAGE4, PTP_CTL, val);
677
678 /* disable trigger */
679 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
680 val |= TRIG_DIS;
681 ext_write(0, master, PAGE4, PTP_CTL, val);
682
683 /*
684 * read out and correct offsets
685 */
686 val = ext_read(master, PAGE4, PTP_STS);
Joe Perches8d242482012-06-09 07:49:07 +0000687 pr_info("master PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200688 val = ext_read(master, PAGE4, PTP_ESTS);
Joe Perches8d242482012-06-09 07:49:07 +0000689 pr_info("master PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200690 event_ts.ns_lo = ext_read(master, PAGE4, PTP_EDATA);
691 event_ts.ns_hi = ext_read(master, PAGE4, PTP_EDATA);
692 event_ts.sec_lo = ext_read(master, PAGE4, PTP_EDATA);
693 event_ts.sec_hi = ext_read(master, PAGE4, PTP_EDATA);
694 now = phy2txts(&event_ts);
695
696 list_for_each(this, &clock->phylist) {
697 tmp = list_entry(this, struct dp83640_private, list);
698 val = ext_read(tmp->phydev, PAGE4, PTP_STS);
Joe Perches8d242482012-06-09 07:49:07 +0000699 pr_info("slave PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200700 val = ext_read(tmp->phydev, PAGE4, PTP_ESTS);
Joe Perches8d242482012-06-09 07:49:07 +0000701 pr_info("slave PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200702 event_ts.ns_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
703 event_ts.ns_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
704 event_ts.sec_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
705 event_ts.sec_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
706 diff = now - (s64) phy2txts(&event_ts);
707 pr_info("slave offset %lld nanoseconds\n", diff);
708 diff += ADJTIME_FIX;
Richard Cochran41c2c182015-03-29 23:12:10 +0200709 ts = ns_to_timespec64(diff);
Richard Cochrancb646e22011-04-22 12:04:55 +0200710 tdr_write(0, tmp->phydev, &ts, PTP_STEP_CLK);
711 }
712
713 /*
714 * restore status frames
715 */
716 list_for_each(this, &clock->phylist) {
717 tmp = list_entry(this, struct dp83640_private, list);
718 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, tmp->cfg0);
719 }
720 ext_write(0, master, PAGE5, PSF_CFG0, cfg0);
721
722 mutex_unlock(&clock->extreg_lock);
723}
724
725/* time stamping methods */
726
Richard Cochran49b3fd42011-09-20 01:43:14 +0000727static inline u16 exts_chan_to_edata(int ch)
728{
729 return 1 << ((ch + EXT_EVENT) * 2);
730}
731
Richard Cochran23310382011-06-14 23:55:19 +0000732static int decode_evnt(struct dp83640_private *dp83640,
Christian Riesch13322f22014-08-21 15:17:04 +0200733 void *data, int len, u16 ests)
Richard Cochrancb646e22011-04-22 12:04:55 +0200734{
Richard Cochran23310382011-06-14 23:55:19 +0000735 struct phy_txts *phy_txts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200736 struct ptp_clock_event event;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000737 int i, parsed;
Richard Cochrancb646e22011-04-22 12:04:55 +0200738 int words = (ests >> EVNT_TS_LEN_SHIFT) & EVNT_TS_LEN_MASK;
Richard Cochran23310382011-06-14 23:55:19 +0000739 u16 ext_status = 0;
740
Christian Riesch13322f22014-08-21 15:17:04 +0200741 /* calculate length of the event timestamp status message */
742 if (ests & MULT_EVNT)
743 parsed = (words + 2) * sizeof(u16);
744 else
745 parsed = (words + 1) * sizeof(u16);
746
747 /* check if enough data is available */
748 if (len < parsed)
749 return len;
750
Richard Cochran23310382011-06-14 23:55:19 +0000751 if (ests & MULT_EVNT) {
752 ext_status = *(u16 *) data;
753 data += sizeof(ext_status);
754 }
755
756 phy_txts = data;
Richard Cochrancb646e22011-04-22 12:04:55 +0200757
758 switch (words) { /* fall through in every case */
759 case 3:
760 dp83640->edata.sec_hi = phy_txts->sec_hi;
761 case 2:
762 dp83640->edata.sec_lo = phy_txts->sec_lo;
763 case 1:
764 dp83640->edata.ns_hi = phy_txts->ns_hi;
765 case 0:
766 dp83640->edata.ns_lo = phy_txts->ns_lo;
767 }
768
Christian Riesch13322f22014-08-21 15:17:04 +0200769 if (!ext_status) {
Richard Cochran49b3fd42011-09-20 01:43:14 +0000770 i = ((ests >> EVNT_NUM_SHIFT) & EVNT_NUM_MASK) - EXT_EVENT;
771 ext_status = exts_chan_to_edata(i);
772 }
773
Richard Cochrancb646e22011-04-22 12:04:55 +0200774 event.type = PTP_CLOCK_EXTTS;
Richard Cochrancb646e22011-04-22 12:04:55 +0200775 event.timestamp = phy2txts(&dp83640->edata);
776
Stefan Sørensena0077a92014-07-11 08:18:26 +0200777 /* Compensate for input path and synchronization delays */
778 event.timestamp -= 35;
779
Richard Cochran49b3fd42011-09-20 01:43:14 +0000780 for (i = 0; i < N_EXT_TS; i++) {
781 if (ext_status & exts_chan_to_edata(i)) {
782 event.index = i;
783 ptp_clock_event(dp83640->clock->ptp_clock, &event);
784 }
785 }
Richard Cochran23310382011-06-14 23:55:19 +0000786
Christian Riesch13322f22014-08-21 15:17:04 +0200787 return parsed;
Richard Cochrancb646e22011-04-22 12:04:55 +0200788}
789
Stefan Sørensen63502b82014-07-22 15:20:45 +0200790static int match(struct sk_buff *skb, unsigned int type, struct rxts *rxts)
791{
792 u16 *seqid;
793 unsigned int offset = 0;
794 u8 *msgtype, *data = skb_mac_header(skb);
795
796 /* check sequenceID, messageType, 12 bit hash of offset 20-29 */
797
798 if (type & PTP_CLASS_VLAN)
799 offset += VLAN_HLEN;
800
801 switch (type & PTP_CLASS_PMASK) {
802 case PTP_CLASS_IPV4:
Richard Cochrancca04b22014-11-12 11:33:52 +0100803 offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200804 break;
805 case PTP_CLASS_IPV6:
806 offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
807 break;
808 case PTP_CLASS_L2:
809 offset += ETH_HLEN;
810 break;
811 default:
812 return 0;
813 }
814
815 if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
816 return 0;
817
818 if (unlikely(type & PTP_CLASS_V1))
819 msgtype = data + offset + OFF_PTP_CONTROL;
820 else
821 msgtype = data + offset;
822
823 seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
824
825 return rxts->msgtype == (*msgtype & 0xf) &&
826 rxts->seqid == ntohs(*seqid);
827}
828
Richard Cochrancb646e22011-04-22 12:04:55 +0200829static void decode_rxts(struct dp83640_private *dp83640,
830 struct phy_rxts *phy_rxts)
831{
832 struct rxts *rxts;
Stefan Sørensen63502b82014-07-22 15:20:45 +0200833 struct skb_shared_hwtstamps *shhwtstamps = NULL;
834 struct sk_buff *skb;
Richard Cochrancb646e22011-04-22 12:04:55 +0200835 unsigned long flags;
836
837 spin_lock_irqsave(&dp83640->rx_lock, flags);
838
839 prune_rx_ts(dp83640);
840
841 if (list_empty(&dp83640->rxpool)) {
Joe Perches8d242482012-06-09 07:49:07 +0000842 pr_debug("rx timestamp pool is empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200843 goto out;
844 }
845 rxts = list_first_entry(&dp83640->rxpool, struct rxts, list);
846 list_del_init(&rxts->list);
847 phy2rxts(phy_rxts, rxts);
Stefan Sørensen63502b82014-07-22 15:20:45 +0200848
849 spin_lock_irqsave(&dp83640->rx_queue.lock, flags);
850 skb_queue_walk(&dp83640->rx_queue, skb) {
851 struct dp83640_skb_info *skb_info;
852
853 skb_info = (struct dp83640_skb_info *)skb->cb;
854 if (match(skb, skb_info->ptp_type, rxts)) {
855 __skb_unlink(skb, &dp83640->rx_queue);
856 shhwtstamps = skb_hwtstamps(skb);
857 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
858 shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
859 netif_rx_ni(skb);
860 list_add(&rxts->list, &dp83640->rxpool);
861 break;
862 }
863 }
864 spin_unlock_irqrestore(&dp83640->rx_queue.lock, flags);
865
866 if (!shhwtstamps)
867 list_add_tail(&rxts->list, &dp83640->rxts);
Richard Cochrancb646e22011-04-22 12:04:55 +0200868out:
869 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
870}
871
872static void decode_txts(struct dp83640_private *dp83640,
873 struct phy_txts *phy_txts)
874{
875 struct skb_shared_hwtstamps shhwtstamps;
876 struct sk_buff *skb;
877 u64 ns;
878
879 /* We must already have the skb that triggered this. */
880
881 skb = skb_dequeue(&dp83640->tx_queue);
882
883 if (!skb) {
Joe Perches8d242482012-06-09 07:49:07 +0000884 pr_debug("have timestamp but tx_queue empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200885 return;
886 }
887 ns = phy2txts(phy_txts);
888 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
889 shhwtstamps.hwtstamp = ns_to_ktime(ns);
890 skb_complete_tx_timestamp(skb, &shhwtstamps);
891}
892
893static void decode_status_frame(struct dp83640_private *dp83640,
894 struct sk_buff *skb)
895{
896 struct phy_rxts *phy_rxts;
897 struct phy_txts *phy_txts;
898 u8 *ptr;
899 int len, size;
900 u16 ests, type;
901
902 ptr = skb->data + 2;
903
904 for (len = skb_headlen(skb) - 2; len > sizeof(type); len -= size) {
905
906 type = *(u16 *)ptr;
907 ests = type & 0x0fff;
908 type = type & 0xf000;
909 len -= sizeof(type);
910 ptr += sizeof(type);
911
912 if (PSF_RX == type && len >= sizeof(*phy_rxts)) {
913
914 phy_rxts = (struct phy_rxts *) ptr;
915 decode_rxts(dp83640, phy_rxts);
916 size = sizeof(*phy_rxts);
917
918 } else if (PSF_TX == type && len >= sizeof(*phy_txts)) {
919
920 phy_txts = (struct phy_txts *) ptr;
921 decode_txts(dp83640, phy_txts);
922 size = sizeof(*phy_txts);
923
Christian Riesch13322f22014-08-21 15:17:04 +0200924 } else if (PSF_EVNT == type) {
Richard Cochrancb646e22011-04-22 12:04:55 +0200925
Christian Riesch13322f22014-08-21 15:17:04 +0200926 size = decode_evnt(dp83640, ptr, len, ests);
Richard Cochrancb646e22011-04-22 12:04:55 +0200927
928 } else {
929 size = 0;
930 break;
931 }
932 ptr += size;
933 }
934}
935
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000936static int is_sync(struct sk_buff *skb, int type)
937{
938 u8 *data = skb->data, *msgtype;
939 unsigned int offset = 0;
940
Stefan Sørensenae5c6c62014-06-27 11:59:10 +0200941 if (type & PTP_CLASS_VLAN)
942 offset += VLAN_HLEN;
943
944 switch (type & PTP_CLASS_PMASK) {
945 case PTP_CLASS_IPV4:
Richard Cochrancca04b22014-11-12 11:33:52 +0100946 offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000947 break;
Stefan Sørensenae5c6c62014-06-27 11:59:10 +0200948 case PTP_CLASS_IPV6:
949 offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000950 break;
Stefan Sørensenae5c6c62014-06-27 11:59:10 +0200951 case PTP_CLASS_L2:
952 offset += ETH_HLEN;
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000953 break;
954 default:
955 return 0;
956 }
957
958 if (type & PTP_CLASS_V1)
959 offset += OFF_PTP_CONTROL;
960
961 if (skb->len < offset + 1)
962 return 0;
963
964 msgtype = data + offset;
965
966 return (*msgtype & 0xf) == 0;
967}
968
Richard Cochrancb646e22011-04-22 12:04:55 +0200969static void dp83640_free_clocks(void)
970{
971 struct dp83640_clock *clock;
972 struct list_head *this, *next;
973
974 mutex_lock(&phyter_clocks_lock);
975
976 list_for_each_safe(this, next, &phyter_clocks) {
977 clock = list_entry(this, struct dp83640_clock, list);
978 if (!list_empty(&clock->phylist)) {
Joe Perches8d242482012-06-09 07:49:07 +0000979 pr_warn("phy list non-empty while unloading\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200980 BUG();
981 }
982 list_del(&clock->list);
983 mutex_destroy(&clock->extreg_lock);
984 mutex_destroy(&clock->clock_lock);
985 put_device(&clock->bus->dev);
Richard Cochran86dd3612014-03-20 22:21:58 +0100986 kfree(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +0200987 kfree(clock);
988 }
989
990 mutex_unlock(&phyter_clocks_lock);
991}
992
993static void dp83640_clock_init(struct dp83640_clock *clock, struct mii_bus *bus)
994{
995 INIT_LIST_HEAD(&clock->list);
996 clock->bus = bus;
997 mutex_init(&clock->extreg_lock);
998 mutex_init(&clock->clock_lock);
999 INIT_LIST_HEAD(&clock->phylist);
1000 clock->caps.owner = THIS_MODULE;
1001 sprintf(clock->caps.name, "dp83640 timer");
1002 clock->caps.max_adj = 1953124;
1003 clock->caps.n_alarm = 0;
1004 clock->caps.n_ext_ts = N_EXT_TS;
Stefan Sørensenad015772014-06-27 12:05:30 +02001005 clock->caps.n_per_out = N_PER_OUT;
Richard Cochran86dd3612014-03-20 22:21:58 +01001006 clock->caps.n_pins = DP83640_N_PINS;
Richard Cochrancb646e22011-04-22 12:04:55 +02001007 clock->caps.pps = 0;
1008 clock->caps.adjfreq = ptp_dp83640_adjfreq;
1009 clock->caps.adjtime = ptp_dp83640_adjtime;
Richard Cochran41c2c182015-03-29 23:12:10 +02001010 clock->caps.gettime64 = ptp_dp83640_gettime;
1011 clock->caps.settime64 = ptp_dp83640_settime;
Richard Cochrancb646e22011-04-22 12:04:55 +02001012 clock->caps.enable = ptp_dp83640_enable;
Richard Cochran86dd3612014-03-20 22:21:58 +01001013 clock->caps.verify = ptp_dp83640_verify;
1014 /*
1015 * Convert the module param defaults into a dynamic pin configuration.
1016 */
1017 dp83640_gpio_defaults(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +02001018 /*
1019 * Get a reference to this bus instance.
1020 */
1021 get_device(&bus->dev);
1022}
1023
1024static int choose_this_phy(struct dp83640_clock *clock,
1025 struct phy_device *phydev)
1026{
1027 if (chosen_phy == -1 && !clock->chosen)
1028 return 1;
1029
1030 if (chosen_phy == phydev->addr)
1031 return 1;
1032
1033 return 0;
1034}
1035
1036static struct dp83640_clock *dp83640_clock_get(struct dp83640_clock *clock)
1037{
1038 if (clock)
1039 mutex_lock(&clock->clock_lock);
1040 return clock;
1041}
1042
1043/*
1044 * Look up and lock a clock by bus instance.
1045 * If there is no clock for this bus, then create it first.
1046 */
1047static struct dp83640_clock *dp83640_clock_get_bus(struct mii_bus *bus)
1048{
1049 struct dp83640_clock *clock = NULL, *tmp;
1050 struct list_head *this;
1051
1052 mutex_lock(&phyter_clocks_lock);
1053
1054 list_for_each(this, &phyter_clocks) {
1055 tmp = list_entry(this, struct dp83640_clock, list);
1056 if (tmp->bus == bus) {
1057 clock = tmp;
1058 break;
1059 }
1060 }
1061 if (clock)
1062 goto out;
1063
1064 clock = kzalloc(sizeof(struct dp83640_clock), GFP_KERNEL);
1065 if (!clock)
1066 goto out;
1067
Richard Cochran86dd3612014-03-20 22:21:58 +01001068 clock->caps.pin_config = kzalloc(sizeof(struct ptp_pin_desc) *
1069 DP83640_N_PINS, GFP_KERNEL);
1070 if (!clock->caps.pin_config) {
1071 kfree(clock);
1072 clock = NULL;
1073 goto out;
1074 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001075 dp83640_clock_init(clock, bus);
1076 list_add_tail(&phyter_clocks, &clock->list);
1077out:
1078 mutex_unlock(&phyter_clocks_lock);
1079
1080 return dp83640_clock_get(clock);
1081}
1082
1083static void dp83640_clock_put(struct dp83640_clock *clock)
1084{
1085 mutex_unlock(&clock->clock_lock);
1086}
1087
1088static int dp83640_probe(struct phy_device *phydev)
1089{
1090 struct dp83640_clock *clock;
1091 struct dp83640_private *dp83640;
1092 int err = -ENOMEM, i;
1093
1094 if (phydev->addr == BROADCAST_ADDR)
1095 return 0;
1096
1097 clock = dp83640_clock_get_bus(phydev->bus);
1098 if (!clock)
1099 goto no_clock;
1100
1101 dp83640 = kzalloc(sizeof(struct dp83640_private), GFP_KERNEL);
1102 if (!dp83640)
1103 goto no_memory;
1104
1105 dp83640->phydev = phydev;
1106 INIT_WORK(&dp83640->ts_work, rx_timestamp_work);
1107
1108 INIT_LIST_HEAD(&dp83640->rxts);
1109 INIT_LIST_HEAD(&dp83640->rxpool);
1110 for (i = 0; i < MAX_RXTS; i++)
1111 list_add(&dp83640->rx_pool_data[i].list, &dp83640->rxpool);
1112
1113 phydev->priv = dp83640;
1114
1115 spin_lock_init(&dp83640->rx_lock);
1116 skb_queue_head_init(&dp83640->rx_queue);
1117 skb_queue_head_init(&dp83640->tx_queue);
1118
1119 dp83640->clock = clock;
1120
1121 if (choose_this_phy(clock, phydev)) {
1122 clock->chosen = dp83640;
Richard Cochran1ef76152012-09-22 07:02:03 +00001123 clock->ptp_clock = ptp_clock_register(&clock->caps, &phydev->dev);
Richard Cochrancb646e22011-04-22 12:04:55 +02001124 if (IS_ERR(clock->ptp_clock)) {
1125 err = PTR_ERR(clock->ptp_clock);
1126 goto no_register;
1127 }
1128 } else
1129 list_add_tail(&dp83640->list, &clock->phylist);
1130
Richard Cochrancb646e22011-04-22 12:04:55 +02001131 dp83640_clock_put(clock);
1132 return 0;
1133
1134no_register:
1135 clock->chosen = NULL;
1136 kfree(dp83640);
1137no_memory:
1138 dp83640_clock_put(clock);
1139no_clock:
1140 return err;
1141}
1142
1143static void dp83640_remove(struct phy_device *phydev)
1144{
1145 struct dp83640_clock *clock;
1146 struct list_head *this, *next;
1147 struct dp83640_private *tmp, *dp83640 = phydev->priv;
1148
1149 if (phydev->addr == BROADCAST_ADDR)
1150 return;
1151
1152 enable_status_frames(phydev, false);
1153 cancel_work_sync(&dp83640->ts_work);
1154
Alexander Duyckdb91b722014-09-08 11:25:34 -04001155 skb_queue_purge(&dp83640->rx_queue);
1156 skb_queue_purge(&dp83640->tx_queue);
Richard Cochran8b3408f2011-10-21 00:49:17 +00001157
Richard Cochrancb646e22011-04-22 12:04:55 +02001158 clock = dp83640_clock_get(dp83640->clock);
1159
1160 if (dp83640 == clock->chosen) {
1161 ptp_clock_unregister(clock->ptp_clock);
1162 clock->chosen = NULL;
1163 } else {
1164 list_for_each_safe(this, next, &clock->phylist) {
1165 tmp = list_entry(this, struct dp83640_private, list);
1166 if (tmp == dp83640) {
1167 list_del_init(&tmp->list);
1168 break;
1169 }
1170 }
1171 }
1172
1173 dp83640_clock_put(clock);
1174 kfree(dp83640);
1175}
1176
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001177static int dp83640_config_init(struct phy_device *phydev)
1178{
Stefan Sørensen602b1092014-02-13 15:26:57 +01001179 struct dp83640_private *dp83640 = phydev->priv;
1180 struct dp83640_clock *clock = dp83640->clock;
1181
1182 if (clock->chosen && !list_empty(&clock->phylist))
1183 recalibrate(clock);
Richard Cochrana9358652015-05-25 11:55:44 +02001184 else {
1185 mutex_lock(&clock->extreg_lock);
Stefan Sørensen602b1092014-02-13 15:26:57 +01001186 enable_broadcast(phydev, clock->page, 1);
Richard Cochrana9358652015-05-25 11:55:44 +02001187 mutex_unlock(&clock->extreg_lock);
1188 }
Stefan Sørensen602b1092014-02-13 15:26:57 +01001189
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001190 enable_status_frames(phydev, true);
Richard Cochrana9358652015-05-25 11:55:44 +02001191
1192 mutex_lock(&clock->extreg_lock);
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001193 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_ENABLE);
Richard Cochrana9358652015-05-25 11:55:44 +02001194 mutex_unlock(&clock->extreg_lock);
1195
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001196 return 0;
1197}
1198
Stephan Gatzka16421822012-12-04 10:21:38 +00001199static int dp83640_ack_interrupt(struct phy_device *phydev)
1200{
1201 int err = phy_read(phydev, MII_DP83640_MISR);
1202
1203 if (err < 0)
1204 return err;
1205
1206 return 0;
1207}
1208
1209static int dp83640_config_intr(struct phy_device *phydev)
1210{
1211 int micr;
1212 int misr;
1213 int err;
1214
1215 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1216 misr = phy_read(phydev, MII_DP83640_MISR);
1217 if (misr < 0)
1218 return misr;
1219 misr |=
1220 (MII_DP83640_MISR_ANC_INT_EN |
1221 MII_DP83640_MISR_DUP_INT_EN |
1222 MII_DP83640_MISR_SPD_INT_EN |
1223 MII_DP83640_MISR_LINK_INT_EN);
1224 err = phy_write(phydev, MII_DP83640_MISR, misr);
1225 if (err < 0)
1226 return err;
1227
1228 micr = phy_read(phydev, MII_DP83640_MICR);
1229 if (micr < 0)
1230 return micr;
1231 micr |=
1232 (MII_DP83640_MICR_OE |
1233 MII_DP83640_MICR_IE);
1234 return phy_write(phydev, MII_DP83640_MICR, micr);
1235 } else {
1236 micr = phy_read(phydev, MII_DP83640_MICR);
1237 if (micr < 0)
1238 return micr;
1239 micr &=
1240 ~(MII_DP83640_MICR_OE |
1241 MII_DP83640_MICR_IE);
1242 err = phy_write(phydev, MII_DP83640_MICR, micr);
1243 if (err < 0)
1244 return err;
1245
1246 misr = phy_read(phydev, MII_DP83640_MISR);
1247 if (misr < 0)
1248 return misr;
1249 misr &=
1250 ~(MII_DP83640_MISR_ANC_INT_EN |
1251 MII_DP83640_MISR_DUP_INT_EN |
1252 MII_DP83640_MISR_SPD_INT_EN |
1253 MII_DP83640_MISR_LINK_INT_EN);
1254 return phy_write(phydev, MII_DP83640_MISR, misr);
1255 }
1256}
1257
Richard Cochrancb646e22011-04-22 12:04:55 +02001258static int dp83640_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
1259{
1260 struct dp83640_private *dp83640 = phydev->priv;
1261 struct hwtstamp_config cfg;
1262 u16 txcfg0, rxcfg0;
1263
1264 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1265 return -EFAULT;
1266
1267 if (cfg.flags) /* reserved for future extensions */
1268 return -EINVAL;
1269
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001270 if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ONESTEP_SYNC)
Richard Cochrancb646e22011-04-22 12:04:55 +02001271 return -ERANGE;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001272
1273 dp83640->hwts_tx_en = cfg.tx_type;
Richard Cochrancb646e22011-04-22 12:04:55 +02001274
1275 switch (cfg.rx_filter) {
1276 case HWTSTAMP_FILTER_NONE:
1277 dp83640->hwts_rx_en = 0;
1278 dp83640->layer = 0;
1279 dp83640->version = 0;
1280 break;
1281 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1282 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1283 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1284 dp83640->hwts_rx_en = 1;
1285 dp83640->layer = LAYER4;
1286 dp83640->version = 1;
1287 break;
1288 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1289 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1290 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1291 dp83640->hwts_rx_en = 1;
1292 dp83640->layer = LAYER4;
1293 dp83640->version = 2;
1294 break;
1295 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1296 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1297 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1298 dp83640->hwts_rx_en = 1;
1299 dp83640->layer = LAYER2;
1300 dp83640->version = 2;
1301 break;
1302 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1303 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1304 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1305 dp83640->hwts_rx_en = 1;
1306 dp83640->layer = LAYER4|LAYER2;
1307 dp83640->version = 2;
1308 break;
1309 default:
1310 return -ERANGE;
1311 }
1312
1313 txcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1314 rxcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1315
1316 if (dp83640->layer & LAYER2) {
1317 txcfg0 |= TX_L2_EN;
1318 rxcfg0 |= RX_L2_EN;
1319 }
1320 if (dp83640->layer & LAYER4) {
1321 txcfg0 |= TX_IPV6_EN | TX_IPV4_EN;
1322 rxcfg0 |= RX_IPV6_EN | RX_IPV4_EN;
1323 }
1324
1325 if (dp83640->hwts_tx_en)
1326 txcfg0 |= TX_TS_EN;
1327
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001328 if (dp83640->hwts_tx_en == HWTSTAMP_TX_ONESTEP_SYNC)
1329 txcfg0 |= SYNC_1STEP | CHK_1STEP;
1330
Richard Cochrancb646e22011-04-22 12:04:55 +02001331 if (dp83640->hwts_rx_en)
1332 rxcfg0 |= RX_TS_EN;
1333
1334 mutex_lock(&dp83640->clock->extreg_lock);
1335
Richard Cochrancb646e22011-04-22 12:04:55 +02001336 ext_write(0, phydev, PAGE5, PTP_TXCFG0, txcfg0);
1337 ext_write(0, phydev, PAGE5, PTP_RXCFG0, rxcfg0);
1338
1339 mutex_unlock(&dp83640->clock->extreg_lock);
1340
1341 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1342}
1343
1344static void rx_timestamp_work(struct work_struct *work)
1345{
1346 struct dp83640_private *dp83640 =
1347 container_of(work, struct dp83640_private, ts_work);
Richard Cochrancb646e22011-04-22 12:04:55 +02001348 struct sk_buff *skb;
Richard Cochrancb646e22011-04-22 12:04:55 +02001349
Stefan Sørensen63502b82014-07-22 15:20:45 +02001350 /* Deliver expired packets. */
1351 while ((skb = skb_dequeue(&dp83640->rx_queue))) {
1352 struct dp83640_skb_info *skb_info;
Richard Cochrancb646e22011-04-22 12:04:55 +02001353
Stefan Sørensen63502b82014-07-22 15:20:45 +02001354 skb_info = (struct dp83640_skb_info *)skb->cb;
1355 if (!time_after(jiffies, skb_info->tmo)) {
1356 skb_queue_head(&dp83640->rx_queue, skb);
1357 break;
Richard Cochrancb646e22011-04-22 12:04:55 +02001358 }
Stefan Sørensen63502b82014-07-22 15:20:45 +02001359
Manfred Rudigier72092cc2012-01-09 23:52:15 +00001360 netif_rx_ni(skb);
Richard Cochrancb646e22011-04-22 12:04:55 +02001361 }
1362
Stefan Sørensen63502b82014-07-22 15:20:45 +02001363 if (!skb_queue_empty(&dp83640->rx_queue))
1364 schedule_work(&dp83640->ts_work);
Richard Cochrancb646e22011-04-22 12:04:55 +02001365}
1366
1367static bool dp83640_rxtstamp(struct phy_device *phydev,
1368 struct sk_buff *skb, int type)
1369{
1370 struct dp83640_private *dp83640 = phydev->priv;
Stefan Sørensen63502b82014-07-22 15:20:45 +02001371 struct dp83640_skb_info *skb_info = (struct dp83640_skb_info *)skb->cb;
1372 struct list_head *this, *next;
1373 struct rxts *rxts;
1374 struct skb_shared_hwtstamps *shhwtstamps = NULL;
1375 unsigned long flags;
Richard Cochrancb646e22011-04-22 12:04:55 +02001376
Richard Cochrancb646e22011-04-22 12:04:55 +02001377 if (is_status_frame(skb, type)) {
1378 decode_status_frame(dp83640, skb);
Richard Cochranae6e86b2011-06-14 23:55:20 +00001379 kfree_skb(skb);
1380 return true;
Richard Cochrancb646e22011-04-22 12:04:55 +02001381 }
1382
Stefan Sørensena12f78c2014-06-25 14:40:16 +02001383 if (!dp83640->hwts_rx_en)
1384 return false;
1385
Stefan Sørensen63502b82014-07-22 15:20:45 +02001386 spin_lock_irqsave(&dp83640->rx_lock, flags);
1387 list_for_each_safe(this, next, &dp83640->rxts) {
1388 rxts = list_entry(this, struct rxts, list);
1389 if (match(skb, type, rxts)) {
1390 shhwtstamps = skb_hwtstamps(skb);
1391 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
1392 shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
1393 netif_rx_ni(skb);
1394 list_del_init(&rxts->list);
1395 list_add(&rxts->list, &dp83640->rxpool);
1396 break;
1397 }
1398 }
1399 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
1400
1401 if (!shhwtstamps) {
1402 skb_info->ptp_type = type;
1403 skb_info->tmo = jiffies + 2;
1404 skb_queue_tail(&dp83640->rx_queue, skb);
1405 schedule_work(&dp83640->ts_work);
1406 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001407
1408 return true;
1409}
1410
1411static void dp83640_txtstamp(struct phy_device *phydev,
1412 struct sk_buff *skb, int type)
1413{
1414 struct dp83640_private *dp83640 = phydev->priv;
1415
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001416 switch (dp83640->hwts_tx_en) {
1417
1418 case HWTSTAMP_TX_ONESTEP_SYNC:
1419 if (is_sync(skb, type)) {
Alexander Duyck62bccb82014-09-04 13:31:35 -04001420 kfree_skb(skb);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001421 return;
1422 }
1423 /* fall through */
1424 case HWTSTAMP_TX_ON:
Stefan Sørensene2e2f512014-02-03 15:36:35 +01001425 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001426 skb_queue_tail(&dp83640->tx_queue, skb);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001427 break;
1428
1429 case HWTSTAMP_TX_OFF:
1430 default:
Alexander Duyck62bccb82014-09-04 13:31:35 -04001431 kfree_skb(skb);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001432 break;
Richard Cochrancb646e22011-04-22 12:04:55 +02001433 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001434}
1435
Richard Cochran7dff3492012-04-03 22:59:18 +00001436static int dp83640_ts_info(struct phy_device *dev, struct ethtool_ts_info *info)
1437{
1438 struct dp83640_private *dp83640 = dev->priv;
1439
1440 info->so_timestamping =
1441 SOF_TIMESTAMPING_TX_HARDWARE |
1442 SOF_TIMESTAMPING_RX_HARDWARE |
1443 SOF_TIMESTAMPING_RAW_HARDWARE;
1444 info->phc_index = ptp_clock_index(dp83640->clock->ptp_clock);
1445 info->tx_types =
1446 (1 << HWTSTAMP_TX_OFF) |
1447 (1 << HWTSTAMP_TX_ON) |
1448 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
1449 info->rx_filters =
1450 (1 << HWTSTAMP_FILTER_NONE) |
1451 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
1452 (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
1453 (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
1454 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
1455 (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
1456 (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
1457 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1458 (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
1459 (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
1460 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
1461 (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
1462 (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ);
1463 return 0;
1464}
1465
Richard Cochrancb646e22011-04-22 12:04:55 +02001466static struct phy_driver dp83640_driver = {
1467 .phy_id = DP83640_PHY_ID,
1468 .phy_id_mask = 0xfffffff0,
1469 .name = "NatSemi DP83640",
1470 .features = PHY_BASIC_FEATURES,
Stephan Gatzka16421822012-12-04 10:21:38 +00001471 .flags = PHY_HAS_INTERRUPT,
Richard Cochrancb646e22011-04-22 12:04:55 +02001472 .probe = dp83640_probe,
1473 .remove = dp83640_remove,
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001474 .config_init = dp83640_config_init,
Richard Cochrancb646e22011-04-22 12:04:55 +02001475 .config_aneg = genphy_config_aneg,
1476 .read_status = genphy_read_status,
Stephan Gatzka16421822012-12-04 10:21:38 +00001477 .ack_interrupt = dp83640_ack_interrupt,
1478 .config_intr = dp83640_config_intr,
Richard Cochran7dff3492012-04-03 22:59:18 +00001479 .ts_info = dp83640_ts_info,
Richard Cochrancb646e22011-04-22 12:04:55 +02001480 .hwtstamp = dp83640_hwtstamp,
1481 .rxtstamp = dp83640_rxtstamp,
1482 .txtstamp = dp83640_txtstamp,
1483 .driver = {.owner = THIS_MODULE,}
1484};
1485
1486static int __init dp83640_init(void)
1487{
1488 return phy_driver_register(&dp83640_driver);
1489}
1490
1491static void __exit dp83640_exit(void)
1492{
1493 dp83640_free_clocks();
1494 phy_driver_unregister(&dp83640_driver);
1495}
1496
1497MODULE_DESCRIPTION("National Semiconductor DP83640 PHY driver");
Richard Cochranfbf4b932014-03-20 22:21:56 +01001498MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
Richard Cochrancb646e22011-04-22 12:04:55 +02001499MODULE_LICENSE("GPL");
1500
1501module_init(dp83640_init);
1502module_exit(dp83640_exit);
1503
John Stultz86ff9baa2011-05-23 13:32:11 -07001504static struct mdio_device_id __maybe_unused dp83640_tbl[] = {
Richard Cochrancb646e22011-04-22 12:04:55 +02001505 { DP83640_PHY_ID, 0xfffffff0 },
1506 { }
1507};
1508
1509MODULE_DEVICE_TABLE(mdio, dp83640_tbl);