blob: 93db564389158c971c5d14fb7b9835888a244325 [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>
30#include <linux/phy.h>
31#include <linux/ptp_classify.h>
32#include <linux/ptp_clock_kernel.h>
33
34#include "dp83640_reg.h"
35
36#define DP83640_PHY_ID 0x20005ce1
37#define PAGESEL 0x13
38#define LAYER4 0x02
39#define LAYER2 0x01
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
Richard Cochrancb646e22011-04-22 12:04:55 +020042#define PSF_PTPVER 2
43#define PSF_EVNT 0x4000
44#define PSF_RX 0x2000
45#define PSF_TX 0x1000
46#define EXT_EVENT 1
Richard Cochran49b3fd42011-09-20 01:43:14 +000047#define CAL_EVENT 7
48#define CAL_TRIGGER 7
49#define PER_TRIGGER 6
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
70#if defined(__BIG_ENDIAN)
71#define ENDIAN_FLAG 0
72#elif defined(__LITTLE_ENDIAN)
73#define ENDIAN_FLAG PSF_ENDIAN
74#endif
75
76#define SKB_PTP_TYPE(__skb) (*(unsigned int *)((__skb)->cb))
77
78struct phy_rxts {
79 u16 ns_lo; /* ns[15:0] */
80 u16 ns_hi; /* overflow[1:0], ns[29:16] */
81 u16 sec_lo; /* sec[15:0] */
82 u16 sec_hi; /* sec[31:16] */
83 u16 seqid; /* sequenceId[15:0] */
84 u16 msgtype; /* messageType[3:0], hash[11:0] */
85};
86
87struct phy_txts {
88 u16 ns_lo; /* ns[15:0] */
89 u16 ns_hi; /* overflow[1:0], ns[29:16] */
90 u16 sec_lo; /* sec[15:0] */
91 u16 sec_hi; /* sec[31:16] */
92};
93
94struct rxts {
95 struct list_head list;
96 unsigned long tmo;
97 u64 ns;
98 u16 seqid;
99 u8 msgtype;
100 u16 hash;
101};
102
103struct dp83640_clock;
104
105struct dp83640_private {
106 struct list_head list;
107 struct dp83640_clock *clock;
108 struct phy_device *phydev;
109 struct work_struct ts_work;
110 int hwts_tx_en;
111 int hwts_rx_en;
112 int layer;
113 int version;
114 /* remember state of cfg0 during calibration */
115 int cfg0;
116 /* remember the last event time stamp */
117 struct phy_txts edata;
118 /* list of rx timestamps */
119 struct list_head rxts;
120 struct list_head rxpool;
121 struct rxts rx_pool_data[MAX_RXTS];
122 /* protects above three fields from concurrent access */
123 spinlock_t rx_lock;
124 /* queues of incoming and outgoing packets */
125 struct sk_buff_head rx_queue;
126 struct sk_buff_head tx_queue;
127};
128
129struct dp83640_clock {
130 /* keeps the instance in the 'phyter_clocks' list */
131 struct list_head list;
132 /* we create one clock instance per MII bus */
133 struct mii_bus *bus;
134 /* protects extended registers from concurrent access */
135 struct mutex extreg_lock;
136 /* remembers which page was last selected */
137 int page;
138 /* our advertised capabilities */
139 struct ptp_clock_info caps;
140 /* protects the three fields below from concurrent access */
141 struct mutex clock_lock;
142 /* the one phyter from which we shall read */
143 struct dp83640_private *chosen;
144 /* list of the other attached phyters, not chosen */
145 struct list_head phylist;
146 /* reference to our PTP hardware clock */
147 struct ptp_clock *ptp_clock;
148};
149
150/* globals */
151
Richard Cochran49b3fd42011-09-20 01:43:14 +0000152enum {
153 CALIBRATE_GPIO,
154 PEROUT_GPIO,
155 EXTTS0_GPIO,
156 EXTTS1_GPIO,
157 EXTTS2_GPIO,
158 EXTTS3_GPIO,
159 EXTTS4_GPIO,
160 EXTTS5_GPIO,
161 GPIO_TABLE_SIZE
162};
163
Richard Cochrancb646e22011-04-22 12:04:55 +0200164static int chosen_phy = -1;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000165static ushort gpio_tab[GPIO_TABLE_SIZE] = {
166 1, 2, 3, 4, 8, 9, 10, 11
167};
Richard Cochrancb646e22011-04-22 12:04:55 +0200168
169module_param(chosen_phy, int, 0444);
Richard Cochran49b3fd42011-09-20 01:43:14 +0000170module_param_array(gpio_tab, ushort, NULL, 0444);
Richard Cochrancb646e22011-04-22 12:04:55 +0200171
172MODULE_PARM_DESC(chosen_phy, \
173 "The address of the PHY to use for the ancillary clock features");
Richard Cochran49b3fd42011-09-20 01:43:14 +0000174MODULE_PARM_DESC(gpio_tab, \
175 "Which GPIO line to use for which purpose: cal,perout,extts1,...,extts6");
Richard Cochrancb646e22011-04-22 12:04:55 +0200176
Richard Cochran86dd3612014-03-20 22:21:58 +0100177static void dp83640_gpio_defaults(struct ptp_pin_desc *pd)
178{
179 int i, index;
180
181 for (i = 0; i < DP83640_N_PINS; i++) {
182 snprintf(pd[i].name, sizeof(pd[i].name), "GPIO%d", 1 + i);
183 pd[i].index = i;
184 }
185
186 for (i = 0; i < GPIO_TABLE_SIZE; i++) {
187 if (gpio_tab[i] < 1 || gpio_tab[i] > DP83640_N_PINS) {
188 pr_err("gpio_tab[%d]=%hu out of range", i, gpio_tab[i]);
189 return;
190 }
191 }
192
193 index = gpio_tab[CALIBRATE_GPIO] - 1;
194 pd[index].func = PTP_PF_PHYSYNC;
195 pd[index].chan = 0;
196
197 index = gpio_tab[PEROUT_GPIO] - 1;
198 pd[index].func = PTP_PF_PEROUT;
199 pd[index].chan = 0;
200
201 for (i = EXTTS0_GPIO; i < GPIO_TABLE_SIZE; i++) {
202 index = gpio_tab[i] - 1;
203 pd[index].func = PTP_PF_EXTTS;
204 pd[index].chan = i - EXTTS0_GPIO;
205 }
206}
207
Richard Cochrancb646e22011-04-22 12:04:55 +0200208/* a list of clocks and a mutex to protect it */
209static LIST_HEAD(phyter_clocks);
210static DEFINE_MUTEX(phyter_clocks_lock);
211
212static void rx_timestamp_work(struct work_struct *work);
213
214/* extended register access functions */
215
216#define BROADCAST_ADDR 31
217
218static inline int broadcast_write(struct mii_bus *bus, u32 regnum, u16 val)
219{
220 return mdiobus_write(bus, BROADCAST_ADDR, regnum, val);
221}
222
223/* Caller must hold extreg_lock. */
224static int ext_read(struct phy_device *phydev, int page, u32 regnum)
225{
226 struct dp83640_private *dp83640 = phydev->priv;
227 int val;
228
229 if (dp83640->clock->page != page) {
230 broadcast_write(phydev->bus, PAGESEL, page);
231 dp83640->clock->page = page;
232 }
233 val = phy_read(phydev, regnum);
234
235 return val;
236}
237
238/* Caller must hold extreg_lock. */
239static void ext_write(int broadcast, struct phy_device *phydev,
240 int page, u32 regnum, u16 val)
241{
242 struct dp83640_private *dp83640 = phydev->priv;
243
244 if (dp83640->clock->page != page) {
245 broadcast_write(phydev->bus, PAGESEL, page);
246 dp83640->clock->page = page;
247 }
248 if (broadcast)
249 broadcast_write(phydev->bus, regnum, val);
250 else
251 phy_write(phydev, regnum, val);
252}
253
254/* Caller must hold extreg_lock. */
255static int tdr_write(int bc, struct phy_device *dev,
256 const struct timespec *ts, u16 cmd)
257{
258 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec & 0xffff);/* ns[15:0] */
259 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec >> 16); /* ns[31:16] */
260 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec & 0xffff); /* sec[15:0] */
261 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec >> 16); /* sec[31:16]*/
262
263 ext_write(bc, dev, PAGE4, PTP_CTL, cmd);
264
265 return 0;
266}
267
268/* convert phy timestamps into driver timestamps */
269
270static void phy2rxts(struct phy_rxts *p, struct rxts *rxts)
271{
272 u32 sec;
273
274 sec = p->sec_lo;
275 sec |= p->sec_hi << 16;
276
277 rxts->ns = p->ns_lo;
278 rxts->ns |= (p->ns_hi & 0x3fff) << 16;
279 rxts->ns += ((u64)sec) * 1000000000ULL;
280 rxts->seqid = p->seqid;
281 rxts->msgtype = (p->msgtype >> 12) & 0xf;
282 rxts->hash = p->msgtype & 0x0fff;
Richard Cochran80288372011-08-06 21:03:04 +0000283 rxts->tmo = jiffies + 2;
Richard Cochrancb646e22011-04-22 12:04:55 +0200284}
285
286static u64 phy2txts(struct phy_txts *p)
287{
288 u64 ns;
289 u32 sec;
290
291 sec = p->sec_lo;
292 sec |= p->sec_hi << 16;
293
294 ns = p->ns_lo;
295 ns |= (p->ns_hi & 0x3fff) << 16;
296 ns += ((u64)sec) * 1000000000ULL;
297
298 return ns;
299}
300
Richard Cochran49b3fd42011-09-20 01:43:14 +0000301static void periodic_output(struct dp83640_clock *clock,
302 struct ptp_clock_request *clkreq, bool on)
303{
304 struct dp83640_private *dp83640 = clock->chosen;
305 struct phy_device *phydev = dp83640->phydev;
Richard Cochran564ca562014-03-20 22:21:57 +0100306 u32 sec, nsec, pwidth;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000307 u16 gpio, ptp_trig, trigger, val;
308
309 gpio = on ? gpio_tab[PEROUT_GPIO] : 0;
310 trigger = PER_TRIGGER;
311
312 ptp_trig = TRIG_WR |
313 (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT |
314 (gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT |
315 TRIG_PER |
316 TRIG_PULSE;
317
318 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
319
320 if (!on) {
321 val |= TRIG_DIS;
322 mutex_lock(&clock->extreg_lock);
323 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
324 ext_write(0, phydev, PAGE4, PTP_CTL, val);
325 mutex_unlock(&clock->extreg_lock);
326 return;
327 }
328
329 sec = clkreq->perout.start.sec;
330 nsec = clkreq->perout.start.nsec;
Richard Cochran564ca562014-03-20 22:21:57 +0100331 pwidth = clkreq->perout.period.sec * 1000000000UL;
332 pwidth += clkreq->perout.period.nsec;
333 pwidth /= 2;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000334
335 mutex_lock(&clock->extreg_lock);
336
337 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
338
339 /*load trigger*/
340 val |= TRIG_LOAD;
341 ext_write(0, phydev, PAGE4, PTP_CTL, val);
342 ext_write(0, phydev, PAGE4, PTP_TDR, nsec & 0xffff); /* ns[15:0] */
343 ext_write(0, phydev, PAGE4, PTP_TDR, nsec >> 16); /* ns[31:16] */
344 ext_write(0, phydev, PAGE4, PTP_TDR, sec & 0xffff); /* sec[15:0] */
345 ext_write(0, phydev, PAGE4, PTP_TDR, sec >> 16); /* sec[31:16] */
Richard Cochran564ca562014-03-20 22:21:57 +0100346 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff); /* ns[15:0] */
347 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16); /* ns[31:16] */
Richard Cochran49b3fd42011-09-20 01:43:14 +0000348
349 /*enable trigger*/
350 val &= ~TRIG_LOAD;
351 val |= TRIG_EN;
352 ext_write(0, phydev, PAGE4, PTP_CTL, val);
353
354 mutex_unlock(&clock->extreg_lock);
355}
356
Richard Cochrancb646e22011-04-22 12:04:55 +0200357/* ptp clock methods */
358
359static int ptp_dp83640_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
360{
361 struct dp83640_clock *clock =
362 container_of(ptp, struct dp83640_clock, caps);
363 struct phy_device *phydev = clock->chosen->phydev;
364 u64 rate;
365 int neg_adj = 0;
366 u16 hi, lo;
367
368 if (ppb < 0) {
369 neg_adj = 1;
370 ppb = -ppb;
371 }
372 rate = ppb;
373 rate <<= 26;
374 rate = div_u64(rate, 1953125);
375
376 hi = (rate >> 16) & PTP_RATE_HI_MASK;
377 if (neg_adj)
378 hi |= PTP_RATE_DIR;
379
380 lo = rate & 0xffff;
381
382 mutex_lock(&clock->extreg_lock);
383
384 ext_write(1, phydev, PAGE4, PTP_RATEH, hi);
385 ext_write(1, phydev, PAGE4, PTP_RATEL, lo);
386
387 mutex_unlock(&clock->extreg_lock);
388
389 return 0;
390}
391
392static int ptp_dp83640_adjtime(struct ptp_clock_info *ptp, s64 delta)
393{
394 struct dp83640_clock *clock =
395 container_of(ptp, struct dp83640_clock, caps);
396 struct phy_device *phydev = clock->chosen->phydev;
397 struct timespec ts;
398 int err;
399
400 delta += ADJTIME_FIX;
401
402 ts = ns_to_timespec(delta);
403
404 mutex_lock(&clock->extreg_lock);
405
406 err = tdr_write(1, phydev, &ts, PTP_STEP_CLK);
407
408 mutex_unlock(&clock->extreg_lock);
409
410 return err;
411}
412
413static int ptp_dp83640_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
414{
415 struct dp83640_clock *clock =
416 container_of(ptp, struct dp83640_clock, caps);
417 struct phy_device *phydev = clock->chosen->phydev;
418 unsigned int val[4];
419
420 mutex_lock(&clock->extreg_lock);
421
422 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_RD_CLK);
423
424 val[0] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[15:0] */
425 val[1] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[31:16] */
426 val[2] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[15:0] */
427 val[3] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[31:16] */
428
429 mutex_unlock(&clock->extreg_lock);
430
431 ts->tv_nsec = val[0] | (val[1] << 16);
432 ts->tv_sec = val[2] | (val[3] << 16);
433
434 return 0;
435}
436
437static int ptp_dp83640_settime(struct ptp_clock_info *ptp,
438 const struct timespec *ts)
439{
440 struct dp83640_clock *clock =
441 container_of(ptp, struct dp83640_clock, caps);
442 struct phy_device *phydev = clock->chosen->phydev;
443 int err;
444
445 mutex_lock(&clock->extreg_lock);
446
447 err = tdr_write(1, phydev, ts, PTP_LOAD_CLK);
448
449 mutex_unlock(&clock->extreg_lock);
450
451 return err;
452}
453
454static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
455 struct ptp_clock_request *rq, int on)
456{
457 struct dp83640_clock *clock =
458 container_of(ptp, struct dp83640_clock, caps);
459 struct phy_device *phydev = clock->chosen->phydev;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100460 unsigned int index;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000461 u16 evnt, event_num, gpio_num;
Richard Cochrancb646e22011-04-22 12:04:55 +0200462
463 switch (rq->type) {
464 case PTP_CLK_REQ_EXTTS:
Richard Cochran49b3fd42011-09-20 01:43:14 +0000465 index = rq->extts.index;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100466 if (index >= N_EXT_TS)
Richard Cochrancb646e22011-04-22 12:04:55 +0200467 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000468 event_num = EXT_EVENT + index;
469 evnt = EVNT_WR | (event_num & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
Richard Cochrancb646e22011-04-22 12:04:55 +0200470 if (on) {
Richard Cochranfaa89712014-03-20 22:21:59 +0100471 gpio_num = 1 + ptp_find_pin(clock->ptp_clock,
472 PTP_PF_EXTTS, index);
473 if (gpio_num < 1)
474 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000475 evnt |= (gpio_num & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
Stefan Sørensen80671bd2014-02-03 15:36:50 +0100476 if (rq->extts.flags & PTP_FALLING_EDGE)
477 evnt |= EVNT_FALL;
478 else
479 evnt |= EVNT_RISE;
Richard Cochrancb646e22011-04-22 12:04:55 +0200480 }
481 ext_write(0, phydev, PAGE5, PTP_EVNT, evnt);
482 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000483
484 case PTP_CLK_REQ_PEROUT:
485 if (rq->perout.index != 0)
486 return -EINVAL;
487 periodic_output(clock, rq, on);
488 return 0;
489
Richard Cochrancb646e22011-04-22 12:04:55 +0200490 default:
491 break;
492 }
493
494 return -EOPNOTSUPP;
495}
496
Richard Cochran86dd3612014-03-20 22:21:58 +0100497static int ptp_dp83640_verify(struct ptp_clock_info *ptp, unsigned int pin,
498 enum ptp_pin_function func, unsigned int chan)
499{
500 return 0;
501}
502
Richard Cochrancb646e22011-04-22 12:04:55 +0200503static u8 status_frame_dst[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 };
504static u8 status_frame_src[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F };
505
506static void enable_status_frames(struct phy_device *phydev, bool on)
507{
508 u16 cfg0 = 0, ver;
509
510 if (on)
511 cfg0 = PSF_EVNT_EN | PSF_RXTS_EN | PSF_TXTS_EN | ENDIAN_FLAG;
512
513 ver = (PSF_PTPVER & VERSIONPTP_MASK) << VERSIONPTP_SHIFT;
514
515 ext_write(0, phydev, PAGE5, PSF_CFG0, cfg0);
516 ext_write(0, phydev, PAGE6, PSF_CFG1, ver);
517
518 if (!phydev->attached_dev) {
Joe Perches8d242482012-06-09 07:49:07 +0000519 pr_warn("expected to find an attached netdevice\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200520 return;
521 }
522
523 if (on) {
524 if (dev_mc_add(phydev->attached_dev, status_frame_dst))
Joe Perches8d242482012-06-09 07:49:07 +0000525 pr_warn("failed to add mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200526 } else {
527 if (dev_mc_del(phydev->attached_dev, status_frame_dst))
Joe Perches8d242482012-06-09 07:49:07 +0000528 pr_warn("failed to delete mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200529 }
530}
531
532static bool is_status_frame(struct sk_buff *skb, int type)
533{
534 struct ethhdr *h = eth_hdr(skb);
535
536 if (PTP_CLASS_V2_L2 == type &&
537 !memcmp(h->h_source, status_frame_src, sizeof(status_frame_src)))
538 return true;
539 else
540 return false;
541}
542
543static int expired(struct rxts *rxts)
544{
545 return time_after(jiffies, rxts->tmo);
546}
547
548/* Caller must hold rx_lock. */
549static void prune_rx_ts(struct dp83640_private *dp83640)
550{
551 struct list_head *this, *next;
552 struct rxts *rxts;
553
554 list_for_each_safe(this, next, &dp83640->rxts) {
555 rxts = list_entry(this, struct rxts, list);
556 if (expired(rxts)) {
557 list_del_init(&rxts->list);
558 list_add(&rxts->list, &dp83640->rxpool);
559 }
560 }
561}
562
563/* synchronize the phyters so they act as one clock */
564
565static void enable_broadcast(struct phy_device *phydev, int init_page, int on)
566{
567 int val;
568 phy_write(phydev, PAGESEL, 0);
569 val = phy_read(phydev, PHYCR2);
570 if (on)
571 val |= BC_WRITE;
572 else
573 val &= ~BC_WRITE;
574 phy_write(phydev, PHYCR2, val);
575 phy_write(phydev, PAGESEL, init_page);
576}
577
578static void recalibrate(struct dp83640_clock *clock)
579{
580 s64 now, diff;
581 struct phy_txts event_ts;
582 struct timespec ts;
583 struct list_head *this;
584 struct dp83640_private *tmp;
585 struct phy_device *master = clock->chosen->phydev;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000586 u16 cal_gpio, cfg0, evnt, ptp_trig, trigger, val;
Richard Cochrancb646e22011-04-22 12:04:55 +0200587
588 trigger = CAL_TRIGGER;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000589 cal_gpio = gpio_tab[CALIBRATE_GPIO];
Richard Cochrancb646e22011-04-22 12:04:55 +0200590
591 mutex_lock(&clock->extreg_lock);
592
593 /*
594 * enable broadcast, disable status frames, enable ptp clock
595 */
596 list_for_each(this, &clock->phylist) {
597 tmp = list_entry(this, struct dp83640_private, list);
598 enable_broadcast(tmp->phydev, clock->page, 1);
599 tmp->cfg0 = ext_read(tmp->phydev, PAGE5, PSF_CFG0);
600 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, 0);
601 ext_write(0, tmp->phydev, PAGE4, PTP_CTL, PTP_ENABLE);
602 }
603 enable_broadcast(master, clock->page, 1);
604 cfg0 = ext_read(master, PAGE5, PSF_CFG0);
605 ext_write(0, master, PAGE5, PSF_CFG0, 0);
606 ext_write(0, master, PAGE4, PTP_CTL, PTP_ENABLE);
607
608 /*
609 * enable an event timestamp
610 */
611 evnt = EVNT_WR | EVNT_RISE | EVNT_SINGLE;
612 evnt |= (CAL_EVENT & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
613 evnt |= (cal_gpio & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
614
615 list_for_each(this, &clock->phylist) {
616 tmp = list_entry(this, struct dp83640_private, list);
617 ext_write(0, tmp->phydev, PAGE5, PTP_EVNT, evnt);
618 }
619 ext_write(0, master, PAGE5, PTP_EVNT, evnt);
620
621 /*
622 * configure a trigger
623 */
624 ptp_trig = TRIG_WR | TRIG_IF_LATE | TRIG_PULSE;
625 ptp_trig |= (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT;
626 ptp_trig |= (cal_gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT;
627 ext_write(0, master, PAGE5, PTP_TRIG, ptp_trig);
628
629 /* load trigger */
630 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
631 val |= TRIG_LOAD;
632 ext_write(0, master, PAGE4, PTP_CTL, val);
633
634 /* enable trigger */
635 val &= ~TRIG_LOAD;
636 val |= TRIG_EN;
637 ext_write(0, master, PAGE4, PTP_CTL, val);
638
639 /* disable trigger */
640 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
641 val |= TRIG_DIS;
642 ext_write(0, master, PAGE4, PTP_CTL, val);
643
644 /*
645 * read out and correct offsets
646 */
647 val = ext_read(master, PAGE4, PTP_STS);
Joe Perches8d242482012-06-09 07:49:07 +0000648 pr_info("master PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200649 val = ext_read(master, PAGE4, PTP_ESTS);
Joe Perches8d242482012-06-09 07:49:07 +0000650 pr_info("master PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200651 event_ts.ns_lo = ext_read(master, PAGE4, PTP_EDATA);
652 event_ts.ns_hi = ext_read(master, PAGE4, PTP_EDATA);
653 event_ts.sec_lo = ext_read(master, PAGE4, PTP_EDATA);
654 event_ts.sec_hi = ext_read(master, PAGE4, PTP_EDATA);
655 now = phy2txts(&event_ts);
656
657 list_for_each(this, &clock->phylist) {
658 tmp = list_entry(this, struct dp83640_private, list);
659 val = ext_read(tmp->phydev, PAGE4, PTP_STS);
Joe Perches8d242482012-06-09 07:49:07 +0000660 pr_info("slave PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200661 val = ext_read(tmp->phydev, PAGE4, PTP_ESTS);
Joe Perches8d242482012-06-09 07:49:07 +0000662 pr_info("slave PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200663 event_ts.ns_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
664 event_ts.ns_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
665 event_ts.sec_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
666 event_ts.sec_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
667 diff = now - (s64) phy2txts(&event_ts);
668 pr_info("slave offset %lld nanoseconds\n", diff);
669 diff += ADJTIME_FIX;
670 ts = ns_to_timespec(diff);
671 tdr_write(0, tmp->phydev, &ts, PTP_STEP_CLK);
672 }
673
674 /*
675 * restore status frames
676 */
677 list_for_each(this, &clock->phylist) {
678 tmp = list_entry(this, struct dp83640_private, list);
679 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, tmp->cfg0);
680 }
681 ext_write(0, master, PAGE5, PSF_CFG0, cfg0);
682
683 mutex_unlock(&clock->extreg_lock);
684}
685
686/* time stamping methods */
687
Richard Cochran49b3fd42011-09-20 01:43:14 +0000688static inline u16 exts_chan_to_edata(int ch)
689{
690 return 1 << ((ch + EXT_EVENT) * 2);
691}
692
Richard Cochran23310382011-06-14 23:55:19 +0000693static int decode_evnt(struct dp83640_private *dp83640,
694 void *data, u16 ests)
Richard Cochrancb646e22011-04-22 12:04:55 +0200695{
Richard Cochran23310382011-06-14 23:55:19 +0000696 struct phy_txts *phy_txts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200697 struct ptp_clock_event event;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000698 int i, parsed;
Richard Cochrancb646e22011-04-22 12:04:55 +0200699 int words = (ests >> EVNT_TS_LEN_SHIFT) & EVNT_TS_LEN_MASK;
Richard Cochran23310382011-06-14 23:55:19 +0000700 u16 ext_status = 0;
701
702 if (ests & MULT_EVNT) {
703 ext_status = *(u16 *) data;
704 data += sizeof(ext_status);
705 }
706
707 phy_txts = data;
Richard Cochrancb646e22011-04-22 12:04:55 +0200708
709 switch (words) { /* fall through in every case */
710 case 3:
711 dp83640->edata.sec_hi = phy_txts->sec_hi;
712 case 2:
713 dp83640->edata.sec_lo = phy_txts->sec_lo;
714 case 1:
715 dp83640->edata.ns_hi = phy_txts->ns_hi;
716 case 0:
717 dp83640->edata.ns_lo = phy_txts->ns_lo;
718 }
719
Richard Cochran49b3fd42011-09-20 01:43:14 +0000720 if (ext_status) {
721 parsed = words + 2;
722 } else {
723 parsed = words + 1;
724 i = ((ests >> EVNT_NUM_SHIFT) & EVNT_NUM_MASK) - EXT_EVENT;
725 ext_status = exts_chan_to_edata(i);
726 }
727
Richard Cochrancb646e22011-04-22 12:04:55 +0200728 event.type = PTP_CLOCK_EXTTS;
Richard Cochrancb646e22011-04-22 12:04:55 +0200729 event.timestamp = phy2txts(&dp83640->edata);
730
Richard Cochran49b3fd42011-09-20 01:43:14 +0000731 for (i = 0; i < N_EXT_TS; i++) {
732 if (ext_status & exts_chan_to_edata(i)) {
733 event.index = i;
734 ptp_clock_event(dp83640->clock->ptp_clock, &event);
735 }
736 }
Richard Cochran23310382011-06-14 23:55:19 +0000737
Richard Cochran49b3fd42011-09-20 01:43:14 +0000738 return parsed * sizeof(u16);
Richard Cochrancb646e22011-04-22 12:04:55 +0200739}
740
741static void decode_rxts(struct dp83640_private *dp83640,
742 struct phy_rxts *phy_rxts)
743{
744 struct rxts *rxts;
745 unsigned long flags;
746
747 spin_lock_irqsave(&dp83640->rx_lock, flags);
748
749 prune_rx_ts(dp83640);
750
751 if (list_empty(&dp83640->rxpool)) {
Joe Perches8d242482012-06-09 07:49:07 +0000752 pr_debug("rx timestamp pool is empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200753 goto out;
754 }
755 rxts = list_first_entry(&dp83640->rxpool, struct rxts, list);
756 list_del_init(&rxts->list);
757 phy2rxts(phy_rxts, rxts);
758 list_add_tail(&rxts->list, &dp83640->rxts);
759out:
760 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
761}
762
763static void decode_txts(struct dp83640_private *dp83640,
764 struct phy_txts *phy_txts)
765{
766 struct skb_shared_hwtstamps shhwtstamps;
767 struct sk_buff *skb;
768 u64 ns;
769
770 /* We must already have the skb that triggered this. */
771
772 skb = skb_dequeue(&dp83640->tx_queue);
773
774 if (!skb) {
Joe Perches8d242482012-06-09 07:49:07 +0000775 pr_debug("have timestamp but tx_queue empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200776 return;
777 }
778 ns = phy2txts(phy_txts);
779 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
780 shhwtstamps.hwtstamp = ns_to_ktime(ns);
781 skb_complete_tx_timestamp(skb, &shhwtstamps);
782}
783
784static void decode_status_frame(struct dp83640_private *dp83640,
785 struct sk_buff *skb)
786{
787 struct phy_rxts *phy_rxts;
788 struct phy_txts *phy_txts;
789 u8 *ptr;
790 int len, size;
791 u16 ests, type;
792
793 ptr = skb->data + 2;
794
795 for (len = skb_headlen(skb) - 2; len > sizeof(type); len -= size) {
796
797 type = *(u16 *)ptr;
798 ests = type & 0x0fff;
799 type = type & 0xf000;
800 len -= sizeof(type);
801 ptr += sizeof(type);
802
803 if (PSF_RX == type && len >= sizeof(*phy_rxts)) {
804
805 phy_rxts = (struct phy_rxts *) ptr;
806 decode_rxts(dp83640, phy_rxts);
807 size = sizeof(*phy_rxts);
808
809 } else if (PSF_TX == type && len >= sizeof(*phy_txts)) {
810
811 phy_txts = (struct phy_txts *) ptr;
812 decode_txts(dp83640, phy_txts);
813 size = sizeof(*phy_txts);
814
815 } else if (PSF_EVNT == type && len >= sizeof(*phy_txts)) {
816
Richard Cochran23310382011-06-14 23:55:19 +0000817 size = decode_evnt(dp83640, ptr, ests);
Richard Cochrancb646e22011-04-22 12:04:55 +0200818
819 } else {
820 size = 0;
821 break;
822 }
823 ptr += size;
824 }
825}
826
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000827static int is_sync(struct sk_buff *skb, int type)
828{
829 u8 *data = skb->data, *msgtype;
830 unsigned int offset = 0;
831
832 switch (type) {
833 case PTP_CLASS_V1_IPV4:
834 case PTP_CLASS_V2_IPV4:
835 offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
836 break;
837 case PTP_CLASS_V1_IPV6:
838 case PTP_CLASS_V2_IPV6:
839 offset = OFF_PTP6;
840 break;
841 case PTP_CLASS_V2_L2:
842 offset = ETH_HLEN;
843 break;
844 case PTP_CLASS_V2_VLAN:
845 offset = ETH_HLEN + VLAN_HLEN;
846 break;
847 default:
848 return 0;
849 }
850
851 if (type & PTP_CLASS_V1)
852 offset += OFF_PTP_CONTROL;
853
854 if (skb->len < offset + 1)
855 return 0;
856
857 msgtype = data + offset;
858
859 return (*msgtype & 0xf) == 0;
860}
861
Richard Cochrancb646e22011-04-22 12:04:55 +0200862static int match(struct sk_buff *skb, unsigned int type, struct rxts *rxts)
863{
864 u16 *seqid;
865 unsigned int offset;
866 u8 *msgtype, *data = skb_mac_header(skb);
867
868 /* check sequenceID, messageType, 12 bit hash of offset 20-29 */
869
870 switch (type) {
871 case PTP_CLASS_V1_IPV4:
872 case PTP_CLASS_V2_IPV4:
873 offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
874 break;
875 case PTP_CLASS_V1_IPV6:
876 case PTP_CLASS_V2_IPV6:
877 offset = OFF_PTP6;
878 break;
879 case PTP_CLASS_V2_L2:
880 offset = ETH_HLEN;
881 break;
882 case PTP_CLASS_V2_VLAN:
883 offset = ETH_HLEN + VLAN_HLEN;
884 break;
885 default:
886 return 0;
887 }
888
889 if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
890 return 0;
891
892 if (unlikely(type & PTP_CLASS_V1))
893 msgtype = data + offset + OFF_PTP_CONTROL;
894 else
895 msgtype = data + offset;
896
897 seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
898
Florian Fainellidd61d962013-12-17 21:38:07 -0800899 return rxts->msgtype == (*msgtype & 0xf) &&
900 rxts->seqid == ntohs(*seqid);
Richard Cochrancb646e22011-04-22 12:04:55 +0200901}
902
903static void dp83640_free_clocks(void)
904{
905 struct dp83640_clock *clock;
906 struct list_head *this, *next;
907
908 mutex_lock(&phyter_clocks_lock);
909
910 list_for_each_safe(this, next, &phyter_clocks) {
911 clock = list_entry(this, struct dp83640_clock, list);
912 if (!list_empty(&clock->phylist)) {
Joe Perches8d242482012-06-09 07:49:07 +0000913 pr_warn("phy list non-empty while unloading\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200914 BUG();
915 }
916 list_del(&clock->list);
917 mutex_destroy(&clock->extreg_lock);
918 mutex_destroy(&clock->clock_lock);
919 put_device(&clock->bus->dev);
Richard Cochran86dd3612014-03-20 22:21:58 +0100920 kfree(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +0200921 kfree(clock);
922 }
923
924 mutex_unlock(&phyter_clocks_lock);
925}
926
927static void dp83640_clock_init(struct dp83640_clock *clock, struct mii_bus *bus)
928{
929 INIT_LIST_HEAD(&clock->list);
930 clock->bus = bus;
931 mutex_init(&clock->extreg_lock);
932 mutex_init(&clock->clock_lock);
933 INIT_LIST_HEAD(&clock->phylist);
934 clock->caps.owner = THIS_MODULE;
935 sprintf(clock->caps.name, "dp83640 timer");
936 clock->caps.max_adj = 1953124;
937 clock->caps.n_alarm = 0;
938 clock->caps.n_ext_ts = N_EXT_TS;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000939 clock->caps.n_per_out = 1;
Richard Cochran86dd3612014-03-20 22:21:58 +0100940 clock->caps.n_pins = DP83640_N_PINS;
Richard Cochrancb646e22011-04-22 12:04:55 +0200941 clock->caps.pps = 0;
942 clock->caps.adjfreq = ptp_dp83640_adjfreq;
943 clock->caps.adjtime = ptp_dp83640_adjtime;
944 clock->caps.gettime = ptp_dp83640_gettime;
945 clock->caps.settime = ptp_dp83640_settime;
946 clock->caps.enable = ptp_dp83640_enable;
Richard Cochran86dd3612014-03-20 22:21:58 +0100947 clock->caps.verify = ptp_dp83640_verify;
948 /*
949 * Convert the module param defaults into a dynamic pin configuration.
950 */
951 dp83640_gpio_defaults(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +0200952 /*
953 * Get a reference to this bus instance.
954 */
955 get_device(&bus->dev);
956}
957
958static int choose_this_phy(struct dp83640_clock *clock,
959 struct phy_device *phydev)
960{
961 if (chosen_phy == -1 && !clock->chosen)
962 return 1;
963
964 if (chosen_phy == phydev->addr)
965 return 1;
966
967 return 0;
968}
969
970static struct dp83640_clock *dp83640_clock_get(struct dp83640_clock *clock)
971{
972 if (clock)
973 mutex_lock(&clock->clock_lock);
974 return clock;
975}
976
977/*
978 * Look up and lock a clock by bus instance.
979 * If there is no clock for this bus, then create it first.
980 */
981static struct dp83640_clock *dp83640_clock_get_bus(struct mii_bus *bus)
982{
983 struct dp83640_clock *clock = NULL, *tmp;
984 struct list_head *this;
985
986 mutex_lock(&phyter_clocks_lock);
987
988 list_for_each(this, &phyter_clocks) {
989 tmp = list_entry(this, struct dp83640_clock, list);
990 if (tmp->bus == bus) {
991 clock = tmp;
992 break;
993 }
994 }
995 if (clock)
996 goto out;
997
998 clock = kzalloc(sizeof(struct dp83640_clock), GFP_KERNEL);
999 if (!clock)
1000 goto out;
1001
Richard Cochran86dd3612014-03-20 22:21:58 +01001002 clock->caps.pin_config = kzalloc(sizeof(struct ptp_pin_desc) *
1003 DP83640_N_PINS, GFP_KERNEL);
1004 if (!clock->caps.pin_config) {
1005 kfree(clock);
1006 clock = NULL;
1007 goto out;
1008 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001009 dp83640_clock_init(clock, bus);
1010 list_add_tail(&phyter_clocks, &clock->list);
1011out:
1012 mutex_unlock(&phyter_clocks_lock);
1013
1014 return dp83640_clock_get(clock);
1015}
1016
1017static void dp83640_clock_put(struct dp83640_clock *clock)
1018{
1019 mutex_unlock(&clock->clock_lock);
1020}
1021
1022static int dp83640_probe(struct phy_device *phydev)
1023{
1024 struct dp83640_clock *clock;
1025 struct dp83640_private *dp83640;
1026 int err = -ENOMEM, i;
1027
1028 if (phydev->addr == BROADCAST_ADDR)
1029 return 0;
1030
1031 clock = dp83640_clock_get_bus(phydev->bus);
1032 if (!clock)
1033 goto no_clock;
1034
1035 dp83640 = kzalloc(sizeof(struct dp83640_private), GFP_KERNEL);
1036 if (!dp83640)
1037 goto no_memory;
1038
1039 dp83640->phydev = phydev;
1040 INIT_WORK(&dp83640->ts_work, rx_timestamp_work);
1041
1042 INIT_LIST_HEAD(&dp83640->rxts);
1043 INIT_LIST_HEAD(&dp83640->rxpool);
1044 for (i = 0; i < MAX_RXTS; i++)
1045 list_add(&dp83640->rx_pool_data[i].list, &dp83640->rxpool);
1046
1047 phydev->priv = dp83640;
1048
1049 spin_lock_init(&dp83640->rx_lock);
1050 skb_queue_head_init(&dp83640->rx_queue);
1051 skb_queue_head_init(&dp83640->tx_queue);
1052
1053 dp83640->clock = clock;
1054
1055 if (choose_this_phy(clock, phydev)) {
1056 clock->chosen = dp83640;
Richard Cochran1ef76152012-09-22 07:02:03 +00001057 clock->ptp_clock = ptp_clock_register(&clock->caps, &phydev->dev);
Richard Cochrancb646e22011-04-22 12:04:55 +02001058 if (IS_ERR(clock->ptp_clock)) {
1059 err = PTR_ERR(clock->ptp_clock);
1060 goto no_register;
1061 }
1062 } else
1063 list_add_tail(&dp83640->list, &clock->phylist);
1064
Richard Cochrancb646e22011-04-22 12:04:55 +02001065 dp83640_clock_put(clock);
1066 return 0;
1067
1068no_register:
1069 clock->chosen = NULL;
1070 kfree(dp83640);
1071no_memory:
1072 dp83640_clock_put(clock);
1073no_clock:
1074 return err;
1075}
1076
1077static void dp83640_remove(struct phy_device *phydev)
1078{
1079 struct dp83640_clock *clock;
1080 struct list_head *this, *next;
1081 struct dp83640_private *tmp, *dp83640 = phydev->priv;
Richard Cochran8b3408f2011-10-21 00:49:17 +00001082 struct sk_buff *skb;
Richard Cochrancb646e22011-04-22 12:04:55 +02001083
1084 if (phydev->addr == BROADCAST_ADDR)
1085 return;
1086
1087 enable_status_frames(phydev, false);
1088 cancel_work_sync(&dp83640->ts_work);
1089
Richard Cochran8b3408f2011-10-21 00:49:17 +00001090 while ((skb = skb_dequeue(&dp83640->rx_queue)) != NULL)
1091 kfree_skb(skb);
1092
1093 while ((skb = skb_dequeue(&dp83640->tx_queue)) != NULL)
1094 skb_complete_tx_timestamp(skb, NULL);
1095
Richard Cochrancb646e22011-04-22 12:04:55 +02001096 clock = dp83640_clock_get(dp83640->clock);
1097
1098 if (dp83640 == clock->chosen) {
1099 ptp_clock_unregister(clock->ptp_clock);
1100 clock->chosen = NULL;
1101 } else {
1102 list_for_each_safe(this, next, &clock->phylist) {
1103 tmp = list_entry(this, struct dp83640_private, list);
1104 if (tmp == dp83640) {
1105 list_del_init(&tmp->list);
1106 break;
1107 }
1108 }
1109 }
1110
1111 dp83640_clock_put(clock);
1112 kfree(dp83640);
1113}
1114
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001115static int dp83640_config_init(struct phy_device *phydev)
1116{
Stefan Sørensen602b1092014-02-13 15:26:57 +01001117 struct dp83640_private *dp83640 = phydev->priv;
1118 struct dp83640_clock *clock = dp83640->clock;
1119
1120 if (clock->chosen && !list_empty(&clock->phylist))
1121 recalibrate(clock);
1122 else
1123 enable_broadcast(phydev, clock->page, 1);
1124
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001125 enable_status_frames(phydev, true);
1126 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_ENABLE);
1127 return 0;
1128}
1129
Stephan Gatzka16421822012-12-04 10:21:38 +00001130static int dp83640_ack_interrupt(struct phy_device *phydev)
1131{
1132 int err = phy_read(phydev, MII_DP83640_MISR);
1133
1134 if (err < 0)
1135 return err;
1136
1137 return 0;
1138}
1139
1140static int dp83640_config_intr(struct phy_device *phydev)
1141{
1142 int micr;
1143 int misr;
1144 int err;
1145
1146 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1147 misr = phy_read(phydev, MII_DP83640_MISR);
1148 if (misr < 0)
1149 return misr;
1150 misr |=
1151 (MII_DP83640_MISR_ANC_INT_EN |
1152 MII_DP83640_MISR_DUP_INT_EN |
1153 MII_DP83640_MISR_SPD_INT_EN |
1154 MII_DP83640_MISR_LINK_INT_EN);
1155 err = phy_write(phydev, MII_DP83640_MISR, misr);
1156 if (err < 0)
1157 return err;
1158
1159 micr = phy_read(phydev, MII_DP83640_MICR);
1160 if (micr < 0)
1161 return micr;
1162 micr |=
1163 (MII_DP83640_MICR_OE |
1164 MII_DP83640_MICR_IE);
1165 return phy_write(phydev, MII_DP83640_MICR, micr);
1166 } else {
1167 micr = phy_read(phydev, MII_DP83640_MICR);
1168 if (micr < 0)
1169 return micr;
1170 micr &=
1171 ~(MII_DP83640_MICR_OE |
1172 MII_DP83640_MICR_IE);
1173 err = phy_write(phydev, MII_DP83640_MICR, micr);
1174 if (err < 0)
1175 return err;
1176
1177 misr = phy_read(phydev, MII_DP83640_MISR);
1178 if (misr < 0)
1179 return misr;
1180 misr &=
1181 ~(MII_DP83640_MISR_ANC_INT_EN |
1182 MII_DP83640_MISR_DUP_INT_EN |
1183 MII_DP83640_MISR_SPD_INT_EN |
1184 MII_DP83640_MISR_LINK_INT_EN);
1185 return phy_write(phydev, MII_DP83640_MISR, misr);
1186 }
1187}
1188
Richard Cochrancb646e22011-04-22 12:04:55 +02001189static int dp83640_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
1190{
1191 struct dp83640_private *dp83640 = phydev->priv;
1192 struct hwtstamp_config cfg;
1193 u16 txcfg0, rxcfg0;
1194
1195 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1196 return -EFAULT;
1197
1198 if (cfg.flags) /* reserved for future extensions */
1199 return -EINVAL;
1200
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001201 if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ONESTEP_SYNC)
Richard Cochrancb646e22011-04-22 12:04:55 +02001202 return -ERANGE;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001203
1204 dp83640->hwts_tx_en = cfg.tx_type;
Richard Cochrancb646e22011-04-22 12:04:55 +02001205
1206 switch (cfg.rx_filter) {
1207 case HWTSTAMP_FILTER_NONE:
1208 dp83640->hwts_rx_en = 0;
1209 dp83640->layer = 0;
1210 dp83640->version = 0;
1211 break;
1212 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1213 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1214 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1215 dp83640->hwts_rx_en = 1;
1216 dp83640->layer = LAYER4;
1217 dp83640->version = 1;
1218 break;
1219 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1220 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1221 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1222 dp83640->hwts_rx_en = 1;
1223 dp83640->layer = LAYER4;
1224 dp83640->version = 2;
1225 break;
1226 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1227 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1228 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1229 dp83640->hwts_rx_en = 1;
1230 dp83640->layer = LAYER2;
1231 dp83640->version = 2;
1232 break;
1233 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1234 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1235 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1236 dp83640->hwts_rx_en = 1;
1237 dp83640->layer = LAYER4|LAYER2;
1238 dp83640->version = 2;
1239 break;
1240 default:
1241 return -ERANGE;
1242 }
1243
1244 txcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1245 rxcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1246
1247 if (dp83640->layer & LAYER2) {
1248 txcfg0 |= TX_L2_EN;
1249 rxcfg0 |= RX_L2_EN;
1250 }
1251 if (dp83640->layer & LAYER4) {
1252 txcfg0 |= TX_IPV6_EN | TX_IPV4_EN;
1253 rxcfg0 |= RX_IPV6_EN | RX_IPV4_EN;
1254 }
1255
1256 if (dp83640->hwts_tx_en)
1257 txcfg0 |= TX_TS_EN;
1258
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001259 if (dp83640->hwts_tx_en == HWTSTAMP_TX_ONESTEP_SYNC)
1260 txcfg0 |= SYNC_1STEP | CHK_1STEP;
1261
Richard Cochrancb646e22011-04-22 12:04:55 +02001262 if (dp83640->hwts_rx_en)
1263 rxcfg0 |= RX_TS_EN;
1264
1265 mutex_lock(&dp83640->clock->extreg_lock);
1266
Richard Cochrancb646e22011-04-22 12:04:55 +02001267 ext_write(0, phydev, PAGE5, PTP_TXCFG0, txcfg0);
1268 ext_write(0, phydev, PAGE5, PTP_RXCFG0, rxcfg0);
1269
1270 mutex_unlock(&dp83640->clock->extreg_lock);
1271
1272 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1273}
1274
1275static void rx_timestamp_work(struct work_struct *work)
1276{
1277 struct dp83640_private *dp83640 =
1278 container_of(work, struct dp83640_private, ts_work);
1279 struct list_head *this, *next;
1280 struct rxts *rxts;
1281 struct skb_shared_hwtstamps *shhwtstamps;
1282 struct sk_buff *skb;
1283 unsigned int type;
1284 unsigned long flags;
1285
1286 /* Deliver each deferred packet, with or without a time stamp. */
1287
1288 while ((skb = skb_dequeue(&dp83640->rx_queue)) != NULL) {
1289 type = SKB_PTP_TYPE(skb);
1290 spin_lock_irqsave(&dp83640->rx_lock, flags);
1291 list_for_each_safe(this, next, &dp83640->rxts) {
1292 rxts = list_entry(this, struct rxts, list);
1293 if (match(skb, type, rxts)) {
1294 shhwtstamps = skb_hwtstamps(skb);
1295 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
1296 shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
1297 list_del_init(&rxts->list);
1298 list_add(&rxts->list, &dp83640->rxpool);
1299 break;
1300 }
1301 }
1302 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
Manfred Rudigier72092cc2012-01-09 23:52:15 +00001303 netif_rx_ni(skb);
Richard Cochrancb646e22011-04-22 12:04:55 +02001304 }
1305
1306 /* Clear out expired time stamps. */
1307
1308 spin_lock_irqsave(&dp83640->rx_lock, flags);
1309 prune_rx_ts(dp83640);
1310 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
1311}
1312
1313static bool dp83640_rxtstamp(struct phy_device *phydev,
1314 struct sk_buff *skb, int type)
1315{
1316 struct dp83640_private *dp83640 = phydev->priv;
1317
1318 if (!dp83640->hwts_rx_en)
1319 return false;
1320
1321 if (is_status_frame(skb, type)) {
1322 decode_status_frame(dp83640, skb);
Richard Cochranae6e86b2011-06-14 23:55:20 +00001323 kfree_skb(skb);
1324 return true;
Richard Cochrancb646e22011-04-22 12:04:55 +02001325 }
1326
1327 SKB_PTP_TYPE(skb) = type;
1328 skb_queue_tail(&dp83640->rx_queue, skb);
1329 schedule_work(&dp83640->ts_work);
1330
1331 return true;
1332}
1333
1334static void dp83640_txtstamp(struct phy_device *phydev,
1335 struct sk_buff *skb, int type)
1336{
1337 struct dp83640_private *dp83640 = phydev->priv;
1338
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001339 switch (dp83640->hwts_tx_en) {
1340
1341 case HWTSTAMP_TX_ONESTEP_SYNC:
1342 if (is_sync(skb, type)) {
Richard Cochranf5ff7cd2011-10-21 00:49:16 +00001343 skb_complete_tx_timestamp(skb, NULL);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001344 return;
1345 }
1346 /* fall through */
1347 case HWTSTAMP_TX_ON:
Stefan Sørensene2e2f512014-02-03 15:36:35 +01001348 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001349 skb_queue_tail(&dp83640->tx_queue, skb);
1350 schedule_work(&dp83640->ts_work);
1351 break;
1352
1353 case HWTSTAMP_TX_OFF:
1354 default:
Richard Cochranf5ff7cd2011-10-21 00:49:16 +00001355 skb_complete_tx_timestamp(skb, NULL);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001356 break;
Richard Cochrancb646e22011-04-22 12:04:55 +02001357 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001358}
1359
Richard Cochran7dff3492012-04-03 22:59:18 +00001360static int dp83640_ts_info(struct phy_device *dev, struct ethtool_ts_info *info)
1361{
1362 struct dp83640_private *dp83640 = dev->priv;
1363
1364 info->so_timestamping =
1365 SOF_TIMESTAMPING_TX_HARDWARE |
1366 SOF_TIMESTAMPING_RX_HARDWARE |
1367 SOF_TIMESTAMPING_RAW_HARDWARE;
1368 info->phc_index = ptp_clock_index(dp83640->clock->ptp_clock);
1369 info->tx_types =
1370 (1 << HWTSTAMP_TX_OFF) |
1371 (1 << HWTSTAMP_TX_ON) |
1372 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
1373 info->rx_filters =
1374 (1 << HWTSTAMP_FILTER_NONE) |
1375 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
1376 (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
1377 (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
1378 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
1379 (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
1380 (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
1381 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1382 (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
1383 (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
1384 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
1385 (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
1386 (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ);
1387 return 0;
1388}
1389
Richard Cochrancb646e22011-04-22 12:04:55 +02001390static struct phy_driver dp83640_driver = {
1391 .phy_id = DP83640_PHY_ID,
1392 .phy_id_mask = 0xfffffff0,
1393 .name = "NatSemi DP83640",
1394 .features = PHY_BASIC_FEATURES,
Stephan Gatzka16421822012-12-04 10:21:38 +00001395 .flags = PHY_HAS_INTERRUPT,
Richard Cochrancb646e22011-04-22 12:04:55 +02001396 .probe = dp83640_probe,
1397 .remove = dp83640_remove,
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001398 .config_init = dp83640_config_init,
Richard Cochrancb646e22011-04-22 12:04:55 +02001399 .config_aneg = genphy_config_aneg,
1400 .read_status = genphy_read_status,
Stephan Gatzka16421822012-12-04 10:21:38 +00001401 .ack_interrupt = dp83640_ack_interrupt,
1402 .config_intr = dp83640_config_intr,
Richard Cochran7dff3492012-04-03 22:59:18 +00001403 .ts_info = dp83640_ts_info,
Richard Cochrancb646e22011-04-22 12:04:55 +02001404 .hwtstamp = dp83640_hwtstamp,
1405 .rxtstamp = dp83640_rxtstamp,
1406 .txtstamp = dp83640_txtstamp,
1407 .driver = {.owner = THIS_MODULE,}
1408};
1409
1410static int __init dp83640_init(void)
1411{
1412 return phy_driver_register(&dp83640_driver);
1413}
1414
1415static void __exit dp83640_exit(void)
1416{
1417 dp83640_free_clocks();
1418 phy_driver_unregister(&dp83640_driver);
1419}
1420
1421MODULE_DESCRIPTION("National Semiconductor DP83640 PHY driver");
Richard Cochranfbf4b932014-03-20 22:21:56 +01001422MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
Richard Cochrancb646e22011-04-22 12:04:55 +02001423MODULE_LICENSE("GPL");
1424
1425module_init(dp83640_init);
1426module_exit(dp83640_exit);
1427
John Stultz86ff9baa2011-05-23 13:32:11 -07001428static struct mdio_device_id __maybe_unused dp83640_tbl[] = {
Richard Cochrancb646e22011-04-22 12:04:55 +02001429 { DP83640_PHY_ID, 0xfffffff0 },
1430 { }
1431};
1432
1433MODULE_DEVICE_TABLE(mdio, dp83640_tbl);