1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#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
40#define MAX_RXTS 64
41#define N_EXT_TS 6
42#define PSF_PTPVER 2
43#define PSF_EVNT 0x4000
44#define PSF_RX 0x2000
45#define PSF_TX 0x1000
46#define EXT_EVENT 1
47#define CAL_EVENT 7
48#define CAL_TRIGGER 7
49#define PER_TRIGGER 6
50
51#define MII_DP83640_MICR 0x11
52#define MII_DP83640_MISR 0x12
53
54#define MII_DP83640_MICR_OE 0x1
55#define MII_DP83640_MICR_IE 0x2
56
57#define MII_DP83640_MISR_RHF_INT_EN 0x01
58#define MII_DP83640_MISR_FHF_INT_EN 0x02
59#define MII_DP83640_MISR_ANC_INT_EN 0x04
60#define MII_DP83640_MISR_DUP_INT_EN 0x08
61#define MII_DP83640_MISR_SPD_INT_EN 0x10
62#define MII_DP83640_MISR_LINK_INT_EN 0x20
63#define MII_DP83640_MISR_ED_INT_EN 0x40
64#define MII_DP83640_MISR_LQ_INT_EN 0x80
65
66
67#define ADJTIME_FIX 16
68
69#if defined(__BIG_ENDIAN)
70#define ENDIAN_FLAG 0
71#elif defined(__LITTLE_ENDIAN)
72#define ENDIAN_FLAG PSF_ENDIAN
73#endif
74
75#define SKB_PTP_TYPE(__skb) (*(unsigned int *)((__skb)->cb))
76
77struct phy_rxts {
78 u16 ns_lo;
79 u16 ns_hi;
80 u16 sec_lo;
81 u16 sec_hi;
82 u16 seqid;
83 u16 msgtype;
84};
85
86struct phy_txts {
87 u16 ns_lo;
88 u16 ns_hi;
89 u16 sec_lo;
90 u16 sec_hi;
91};
92
93struct rxts {
94 struct list_head list;
95 unsigned long tmo;
96 u64 ns;
97 u16 seqid;
98 u8 msgtype;
99 u16 hash;
100};
101
102struct dp83640_clock;
103
104struct dp83640_private {
105 struct list_head list;
106 struct dp83640_clock *clock;
107 struct phy_device *phydev;
108 struct work_struct ts_work;
109 int hwts_tx_en;
110 int hwts_rx_en;
111 int layer;
112 int version;
113
114 int cfg0;
115
116 struct phy_txts edata;
117
118 struct list_head rxts;
119 struct list_head rxpool;
120 struct rxts rx_pool_data[MAX_RXTS];
121
122 spinlock_t rx_lock;
123
124 struct sk_buff_head rx_queue;
125 struct sk_buff_head tx_queue;
126};
127
128struct dp83640_clock {
129
130 struct list_head list;
131
132 struct mii_bus *bus;
133
134 struct mutex extreg_lock;
135
136 int page;
137
138 struct ptp_clock_info caps;
139
140 struct mutex clock_lock;
141
142 struct dp83640_private *chosen;
143
144 struct list_head phylist;
145
146 struct ptp_clock *ptp_clock;
147};
148
149
150
151enum {
152 CALIBRATE_GPIO,
153 PEROUT_GPIO,
154 EXTTS0_GPIO,
155 EXTTS1_GPIO,
156 EXTTS2_GPIO,
157 EXTTS3_GPIO,
158 EXTTS4_GPIO,
159 EXTTS5_GPIO,
160 GPIO_TABLE_SIZE
161};
162
163static int chosen_phy = -1;
164static ushort gpio_tab[GPIO_TABLE_SIZE] = {
165 1, 2, 3, 4, 8, 9, 10, 11
166};
167
168module_param(chosen_phy, int, 0444);
169module_param_array(gpio_tab, ushort, NULL, 0444);
170
171MODULE_PARM_DESC(chosen_phy, \
172 "The address of the PHY to use for the ancillary clock features");
173MODULE_PARM_DESC(gpio_tab, \
174 "Which GPIO line to use for which purpose: cal,perout,extts1,...,extts6");
175
176
177static LIST_HEAD(phyter_clocks);
178static DEFINE_MUTEX(phyter_clocks_lock);
179
180static void rx_timestamp_work(struct work_struct *work);
181
182
183
184#define BROADCAST_ADDR 31
185
186static inline int broadcast_write(struct phy_device *phydev, u32 regnum,
187 u16 val)
188{
189 return mdiobus_write(phydev->mdio_bus, BROADCAST_ADDR, regnum, val);
190}
191
192
193static int ext_read(struct phy_device *phydev, int page, u32 regnum)
194{
195 struct dp83640_private *dp83640 = phydev->priv;
196 int val;
197
198 if (dp83640->clock->page != page) {
199 broadcast_write(phydev, PAGESEL, page);
200 dp83640->clock->page = page;
201 }
202 val = phy_read(phydev, regnum);
203
204 return val;
205}
206
207
208static void ext_write(int broadcast, struct phy_device *phydev,
209 int page, u32 regnum, u16 val)
210{
211 struct dp83640_private *dp83640 = phydev->priv;
212
213 if (dp83640->clock->page != page) {
214 broadcast_write(phydev, PAGESEL, page);
215 dp83640->clock->page = page;
216 }
217 if (broadcast)
218 broadcast_write(phydev, regnum, val);
219 else
220 phy_write(phydev, regnum, val);
221}
222
223
224static int tdr_write(int bc, struct phy_device *dev,
225 const struct timespec *ts, u16 cmd)
226{
227 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec & 0xffff);
228 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec >> 16);
229 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec & 0xffff);
230 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec >> 16);
231
232 ext_write(bc, dev, PAGE4, PTP_CTL, cmd);
233
234 return 0;
235}
236
237
238
239static void phy2rxts(struct phy_rxts *p, struct rxts *rxts)
240{
241 u32 sec;
242
243 sec = p->sec_lo;
244 sec |= p->sec_hi << 16;
245
246 rxts->ns = p->ns_lo;
247 rxts->ns |= (p->ns_hi & 0x3fff) << 16;
248 rxts->ns += ((u64)sec) * 1000000000ULL;
249 rxts->seqid = p->seqid;
250 rxts->msgtype = (p->msgtype >> 12) & 0xf;
251 rxts->hash = p->msgtype & 0x0fff;
252 rxts->tmo = jiffies + 2;
253}
254
255static u64 phy2txts(struct phy_txts *p)
256{
257 u64 ns;
258 u32 sec;
259
260 sec = p->sec_lo;
261 sec |= p->sec_hi << 16;
262
263 ns = p->ns_lo;
264 ns |= (p->ns_hi & 0x3fff) << 16;
265 ns += ((u64)sec) * 1000000000ULL;
266
267 return ns;
268}
269
270static void periodic_output(struct dp83640_clock *clock,
271 struct ptp_clock_request *clkreq, bool on)
272{
273 struct dp83640_private *dp83640 = clock->chosen;
274 struct phy_device *phydev = dp83640->phydev;
275 u32 sec, nsec, period;
276 u16 gpio, ptp_trig, trigger, val;
277
278 gpio = on ? gpio_tab[PEROUT_GPIO] : 0;
279 trigger = PER_TRIGGER;
280
281 ptp_trig = TRIG_WR |
282 (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT |
283 (gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT |
284 TRIG_PER |
285 TRIG_PULSE;
286
287 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
288
289 if (!on) {
290 val |= TRIG_DIS;
291 mutex_lock(&clock->extreg_lock);
292 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
293 ext_write(0, phydev, PAGE4, PTP_CTL, val);
294 mutex_unlock(&clock->extreg_lock);
295 return;
296 }
297
298 sec = clkreq->perout.start.sec;
299 nsec = clkreq->perout.start.nsec;
300 period = clkreq->perout.period.sec * 1000000000UL;
301 period += clkreq->perout.period.nsec;
302
303 mutex_lock(&clock->extreg_lock);
304
305 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
306
307
308 val |= TRIG_LOAD;
309 ext_write(0, phydev, PAGE4, PTP_CTL, val);
310 ext_write(0, phydev, PAGE4, PTP_TDR, nsec & 0xffff);
311 ext_write(0, phydev, PAGE4, PTP_TDR, nsec >> 16);
312 ext_write(0, phydev, PAGE4, PTP_TDR, sec & 0xffff);
313 ext_write(0, phydev, PAGE4, PTP_TDR, sec >> 16);
314 ext_write(0, phydev, PAGE4, PTP_TDR, period & 0xffff);
315 ext_write(0, phydev, PAGE4, PTP_TDR, period >> 16);
316
317
318 val &= ~TRIG_LOAD;
319 val |= TRIG_EN;
320 ext_write(0, phydev, PAGE4, PTP_CTL, val);
321
322 mutex_unlock(&clock->extreg_lock);
323}
324
325
326
327static int ptp_dp83640_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
328{
329 struct dp83640_clock *clock =
330 container_of(ptp, struct dp83640_clock, caps);
331 struct phy_device *phydev = clock->chosen->phydev;
332 u64 rate;
333 int neg_adj = 0;
334 u16 hi, lo;
335
336 if (ppb < 0) {
337 neg_adj = 1;
338 ppb = -ppb;
339 }
340 rate = ppb;
341 rate <<= 26;
342 rate = div_u64(rate, 1953125);
343
344 hi = (rate >> 16) & PTP_RATE_HI_MASK;
345 if (neg_adj)
346 hi |= PTP_RATE_DIR;
347
348 lo = rate & 0xffff;
349
350 mutex_lock(&clock->extreg_lock);
351
352 ext_write(1, phydev, PAGE4, PTP_RATEH, hi);
353 ext_write(1, phydev, PAGE4, PTP_RATEL, lo);
354
355 mutex_unlock(&clock->extreg_lock);
356
357 return 0;
358}
359
360static int ptp_dp83640_adjtime(struct ptp_clock_info *ptp, s64 delta)
361{
362 struct dp83640_clock *clock =
363 container_of(ptp, struct dp83640_clock, caps);
364 struct phy_device *phydev = clock->chosen->phydev;
365 struct timespec ts;
366 int err;
367
368 delta += ADJTIME_FIX;
369
370 ts = ns_to_timespec(delta);
371
372 mutex_lock(&clock->extreg_lock);
373
374 err = tdr_write(1, phydev, &ts, PTP_STEP_CLK);
375
376 mutex_unlock(&clock->extreg_lock);
377
378 return err;
379}
380
381static int ptp_dp83640_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
382{
383 struct dp83640_clock *clock =
384 container_of(ptp, struct dp83640_clock, caps);
385 struct phy_device *phydev = clock->chosen->phydev;
386 unsigned int val[4];
387
388 mutex_lock(&clock->extreg_lock);
389
390 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_RD_CLK);
391
392 val[0] = ext_read(phydev, PAGE4, PTP_TDR);
393 val[1] = ext_read(phydev, PAGE4, PTP_TDR);
394 val[2] = ext_read(phydev, PAGE4, PTP_TDR);
395 val[3] = ext_read(phydev, PAGE4, PTP_TDR);
396
397 mutex_unlock(&clock->extreg_lock);
398
399 ts->tv_nsec = val[0] | (val[1] << 16);
400 ts->tv_sec = val[2] | (val[3] << 16);
401
402 return 0;
403}
404
405static int ptp_dp83640_settime(struct ptp_clock_info *ptp,
406 const struct timespec *ts)
407{
408 struct dp83640_clock *clock =
409 container_of(ptp, struct dp83640_clock, caps);
410 struct phy_device *phydev = clock->chosen->phydev;
411 int err;
412
413 mutex_lock(&clock->extreg_lock);
414
415 err = tdr_write(1, phydev, ts, PTP_LOAD_CLK);
416
417 mutex_unlock(&clock->extreg_lock);
418
419 return err;
420}
421
422static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
423 struct ptp_clock_request *rq, int on)
424{
425 struct dp83640_clock *clock =
426 container_of(ptp, struct dp83640_clock, caps);
427 struct phy_device *phydev = clock->chosen->phydev;
428 int index;
429 u16 evnt, event_num, gpio_num;
430
431 switch (rq->type) {
432 case PTP_CLK_REQ_EXTTS:
433 index = rq->extts.index;
434 if (index < 0 || index >= N_EXT_TS)
435 return -EINVAL;
436 event_num = EXT_EVENT + index;
437 evnt = EVNT_WR | (event_num & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
438 if (on) {
439 gpio_num = gpio_tab[EXTTS0_GPIO + index];
440 evnt |= (gpio_num & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
441 evnt |= EVNT_RISE;
442 }
443 ext_write(0, phydev, PAGE5, PTP_EVNT, evnt);
444 return 0;
445
446 case PTP_CLK_REQ_PEROUT:
447 if (rq->perout.index != 0)
448 return -EINVAL;
449 periodic_output(clock, rq, on);
450 return 0;
451
452 default:
453 break;
454 }
455
456 return -EOPNOTSUPP;
457}
458
459static u8 status_frame_dst[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 };
460static u8 status_frame_src[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F };
461
462static void enable_status_frames(struct phy_device *phydev, bool on)
463{
464 u16 cfg0 = 0, ver;
465
466 if (on)
467 cfg0 = PSF_EVNT_EN | PSF_RXTS_EN | PSF_TXTS_EN | ENDIAN_FLAG;
468
469 ver = (PSF_PTPVER & VERSIONPTP_MASK) << VERSIONPTP_SHIFT;
470
471 ext_write(0, phydev, PAGE5, PSF_CFG0, cfg0);
472 ext_write(0, phydev, PAGE6, PSF_CFG1, ver);
473
474 if (!phydev->attached_dev) {
475 pr_warn("expected to find an attached netdevice\n");
476 return;
477 }
478
479 if (on) {
480 if (dev_mc_add(phydev->attached_dev, status_frame_dst))
481 pr_warn("failed to add mc address\n");
482 } else {
483 if (dev_mc_del(phydev->attached_dev, status_frame_dst))
484 pr_warn("failed to delete mc address\n");
485 }
486}
487
488static bool is_status_frame(struct sk_buff *skb, int type)
489{
490 struct ethhdr *h = eth_hdr(skb);
491
492 if (PTP_CLASS_V2_L2 == type &&
493 !memcmp(h->h_source, status_frame_src, sizeof(status_frame_src)))
494 return true;
495 else
496 return false;
497}
498
499static int expired(struct rxts *rxts)
500{
501 return time_after(jiffies, rxts->tmo);
502}
503
504
505static void prune_rx_ts(struct dp83640_private *dp83640)
506{
507 struct list_head *this, *next;
508 struct rxts *rxts;
509
510 list_for_each_safe(this, next, &dp83640->rxts) {
511 rxts = list_entry(this, struct rxts, list);
512 if (expired(rxts)) {
513 list_del_init(&rxts->list);
514 list_add(&rxts->list, &dp83640->rxpool);
515 }
516 }
517}
518
519
520
521static void enable_broadcast(struct phy_device *phydev, int init_page, int on)
522{
523 int val;
524 phy_write(phydev, PAGESEL, 0);
525 val = phy_read(phydev, PHYCR2);
526 if (on)
527 val |= BC_WRITE;
528 else
529 val &= ~BC_WRITE;
530 phy_write(phydev, PHYCR2, val);
531 phy_write(phydev, PAGESEL, init_page);
532}
533
534static void recalibrate(struct dp83640_clock *clock)
535{
536 s64 now, diff;
537 struct phy_txts event_ts;
538 struct timespec ts;
539 struct list_head *this;
540 struct dp83640_private *tmp;
541 struct phy_device *master = clock->chosen->phydev;
542 u16 cal_gpio, cfg0, evnt, ptp_trig, trigger, val;
543
544 trigger = CAL_TRIGGER;
545 cal_gpio = gpio_tab[CALIBRATE_GPIO];
546
547 mutex_lock(&clock->extreg_lock);
548
549
550
551
552 list_for_each(this, &clock->phylist) {
553 tmp = list_entry(this, struct dp83640_private, list);
554 enable_broadcast(tmp->phydev, clock->page, 1);
555 tmp->cfg0 = ext_read(tmp->phydev, PAGE5, PSF_CFG0);
556 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, 0);
557 ext_write(0, tmp->phydev, PAGE4, PTP_CTL, PTP_ENABLE);
558 }
559 enable_broadcast(master, clock->page, 1);
560 cfg0 = ext_read(master, PAGE5, PSF_CFG0);
561 ext_write(0, master, PAGE5, PSF_CFG0, 0);
562 ext_write(0, master, PAGE4, PTP_CTL, PTP_ENABLE);
563
564
565
566
567 evnt = EVNT_WR | EVNT_RISE | EVNT_SINGLE;
568 evnt |= (CAL_EVENT & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
569 evnt |= (cal_gpio & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
570
571 list_for_each(this, &clock->phylist) {
572 tmp = list_entry(this, struct dp83640_private, list);
573 ext_write(0, tmp->phydev, PAGE5, PTP_EVNT, evnt);
574 }
575 ext_write(0, master, PAGE5, PTP_EVNT, evnt);
576
577
578
579
580 ptp_trig = TRIG_WR | TRIG_IF_LATE | TRIG_PULSE;
581 ptp_trig |= (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT;
582 ptp_trig |= (cal_gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT;
583 ext_write(0, master, PAGE5, PTP_TRIG, ptp_trig);
584
585
586 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
587 val |= TRIG_LOAD;
588 ext_write(0, master, PAGE4, PTP_CTL, val);
589
590
591 val &= ~TRIG_LOAD;
592 val |= TRIG_EN;
593 ext_write(0, master, PAGE4, PTP_CTL, val);
594
595
596 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
597 val |= TRIG_DIS;
598 ext_write(0, master, PAGE4, PTP_CTL, val);
599
600
601
602
603 val = ext_read(master, PAGE4, PTP_STS);
604 pr_info("master PTP_STS 0x%04hx\n", val);
605 val = ext_read(master, PAGE4, PTP_ESTS);
606 pr_info("master PTP_ESTS 0x%04hx\n", val);
607 event_ts.ns_lo = ext_read(master, PAGE4, PTP_EDATA);
608 event_ts.ns_hi = ext_read(master, PAGE4, PTP_EDATA);
609 event_ts.sec_lo = ext_read(master, PAGE4, PTP_EDATA);
610 event_ts.sec_hi = ext_read(master, PAGE4, PTP_EDATA);
611 now = phy2txts(&event_ts);
612
613 list_for_each(this, &clock->phylist) {
614 tmp = list_entry(this, struct dp83640_private, list);
615 val = ext_read(tmp->phydev, PAGE4, PTP_STS);
616 pr_info("slave PTP_STS 0x%04hx\n", val);
617 val = ext_read(tmp->phydev, PAGE4, PTP_ESTS);
618 pr_info("slave PTP_ESTS 0x%04hx\n", val);
619 event_ts.ns_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
620 event_ts.ns_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
621 event_ts.sec_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
622 event_ts.sec_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
623 diff = now - (s64) phy2txts(&event_ts);
624 pr_info("slave offset %lld nanoseconds\n", diff);
625 diff += ADJTIME_FIX;
626 ts = ns_to_timespec(diff);
627 tdr_write(0, tmp->phydev, &ts, PTP_STEP_CLK);
628 }
629
630
631
632
633 list_for_each(this, &clock->phylist) {
634 tmp = list_entry(this, struct dp83640_private, list);
635 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, tmp->cfg0);
636 }
637 ext_write(0, master, PAGE5, PSF_CFG0, cfg0);
638
639 mutex_unlock(&clock->extreg_lock);
640}
641
642
643
644static inline u16 exts_chan_to_edata(int ch)
645{
646 return 1 << ((ch + EXT_EVENT) * 2);
647}
648
649static int decode_evnt(struct dp83640_private *dp83640,
650 void *data, u16 ests)
651{
652 struct phy_txts *phy_txts;
653 struct ptp_clock_event event;
654 int i, parsed;
655 int words = (ests >> EVNT_TS_LEN_SHIFT) & EVNT_TS_LEN_MASK;
656 u16 ext_status = 0;
657
658 if (ests & MULT_EVNT) {
659 ext_status = *(u16 *) data;
660 data += sizeof(ext_status);
661 }
662
663 phy_txts = data;
664
665 switch (words) {
666 case 3:
667 dp83640->edata.sec_hi = phy_txts->sec_hi;
668 case 2:
669 dp83640->edata.sec_lo = phy_txts->sec_lo;
670 case 1:
671 dp83640->edata.ns_hi = phy_txts->ns_hi;
672 case 0:
673 dp83640->edata.ns_lo = phy_txts->ns_lo;
674 }
675
676 if (ext_status) {
677 parsed = words + 2;
678 } else {
679 parsed = words + 1;
680 i = ((ests >> EVNT_NUM_SHIFT) & EVNT_NUM_MASK) - EXT_EVENT;
681 ext_status = exts_chan_to_edata(i);
682 }
683
684 event.type = PTP_CLOCK_EXTTS;
685 event.timestamp = phy2txts(&dp83640->edata);
686
687 for (i = 0; i < N_EXT_TS; i++) {
688 if (ext_status & exts_chan_to_edata(i)) {
689 event.index = i;
690 ptp_clock_event(dp83640->clock->ptp_clock, &event);
691 }
692 }
693
694 return parsed * sizeof(u16);
695}
696
697static void decode_rxts(struct dp83640_private *dp83640,
698 struct phy_rxts *phy_rxts)
699{
700 struct rxts *rxts;
701 unsigned long flags;
702
703 spin_lock_irqsave(&dp83640->rx_lock, flags);
704
705 prune_rx_ts(dp83640);
706
707 if (list_empty(&dp83640->rxpool)) {
708 pr_debug("rx timestamp pool is empty\n");
709 goto out;
710 }
711 rxts = list_first_entry(&dp83640->rxpool, struct rxts, list);
712 list_del_init(&rxts->list);
713 phy2rxts(phy_rxts, rxts);
714 list_add_tail(&rxts->list, &dp83640->rxts);
715out:
716 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
717}
718
719static void decode_txts(struct dp83640_private *dp83640,
720 struct phy_txts *phy_txts)
721{
722 struct skb_shared_hwtstamps shhwtstamps;
723 struct sk_buff *skb;
724 u64 ns;
725
726
727
728 skb = skb_dequeue(&dp83640->tx_queue);
729
730 if (!skb) {
731 pr_debug("have timestamp but tx_queue empty\n");
732 return;
733 }
734 ns = phy2txts(phy_txts);
735 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
736 shhwtstamps.hwtstamp = ns_to_ktime(ns);
737 skb_complete_tx_timestamp(skb, &shhwtstamps);
738}
739
740static void decode_status_frame(struct dp83640_private *dp83640,
741 struct sk_buff *skb)
742{
743 struct phy_rxts *phy_rxts;
744 struct phy_txts *phy_txts;
745 u8 *ptr;
746 int len, size;
747 u16 ests, type;
748
749 ptr = skb->data + 2;
750
751 for (len = skb_headlen(skb) - 2; len > sizeof(type); len -= size) {
752
753 type = *(u16 *)ptr;
754 ests = type & 0x0fff;
755 type = type & 0xf000;
756 len -= sizeof(type);
757 ptr += sizeof(type);
758
759 if (PSF_RX == type && len >= sizeof(*phy_rxts)) {
760
761 phy_rxts = (struct phy_rxts *) ptr;
762 decode_rxts(dp83640, phy_rxts);
763 size = sizeof(*phy_rxts);
764
765 } else if (PSF_TX == type && len >= sizeof(*phy_txts)) {
766
767 phy_txts = (struct phy_txts *) ptr;
768 decode_txts(dp83640, phy_txts);
769 size = sizeof(*phy_txts);
770
771 } else if (PSF_EVNT == type && len >= sizeof(*phy_txts)) {
772
773 size = decode_evnt(dp83640, ptr, ests);
774
775 } else {
776 size = 0;
777 break;
778 }
779 ptr += size;
780 }
781}
782
783static int is_sync(struct sk_buff *skb, int type)
784{
785 u8 *data = skb->data, *msgtype;
786 unsigned int offset = 0;
787
788 switch (type) {
789 case PTP_CLASS_V1_IPV4:
790 case PTP_CLASS_V2_IPV4:
791 offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
792 break;
793 case PTP_CLASS_V1_IPV6:
794 case PTP_CLASS_V2_IPV6:
795 offset = OFF_PTP6;
796 break;
797 case PTP_CLASS_V2_L2:
798 offset = ETH_HLEN;
799 break;
800 case PTP_CLASS_V2_VLAN:
801 offset = ETH_HLEN + VLAN_HLEN;
802 break;
803 default:
804 return 0;
805 }
806
807 if (type & PTP_CLASS_V1)
808 offset += OFF_PTP_CONTROL;
809
810 if (skb->len < offset + 1)
811 return 0;
812
813 msgtype = data + offset;
814
815 return (*msgtype & 0xf) == 0;
816}
817
818static int match(struct sk_buff *skb, unsigned int type, struct rxts *rxts)
819{
820 u16 *seqid;
821 unsigned int offset;
822 u8 *msgtype, *data = skb_mac_header(skb);
823
824
825
826 switch (type) {
827 case PTP_CLASS_V1_IPV4:
828 case PTP_CLASS_V2_IPV4:
829 offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
830 break;
831 case PTP_CLASS_V1_IPV6:
832 case PTP_CLASS_V2_IPV6:
833 offset = OFF_PTP6;
834 break;
835 case PTP_CLASS_V2_L2:
836 offset = ETH_HLEN;
837 break;
838 case PTP_CLASS_V2_VLAN:
839 offset = ETH_HLEN + VLAN_HLEN;
840 break;
841 default:
842 return 0;
843 }
844
845 if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
846 return 0;
847
848 if (unlikely(type & PTP_CLASS_V1))
849 msgtype = data + offset + OFF_PTP_CONTROL;
850 else
851 msgtype = data + offset;
852
853 seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
854
855 return (rxts->msgtype == (*msgtype & 0xf) &&
856 rxts->seqid == ntohs(*seqid));
857}
858
859static void dp83640_free_clocks(void)
860{
861 struct dp83640_clock *clock;
862 struct list_head *this, *next;
863
864 mutex_lock(&phyter_clocks_lock);
865
866 list_for_each_safe(this, next, &phyter_clocks) {
867 clock = list_entry(this, struct dp83640_clock, list);
868 if (!list_empty(&clock->phylist)) {
869 pr_warn("phy list non-empty while unloading\n");
870 BUG();
871 }
872 list_del(&clock->list);
873 mutex_destroy(&clock->extreg_lock);
874 mutex_destroy(&clock->clock_lock);
875 put_device(&clock->bus->dev);
876 kfree(clock);
877 }
878
879 mutex_unlock(&phyter_clocks_lock);
880}
881
882static void dp83640_clock_init(struct dp83640_clock *clock, struct mii_bus *bus)
883{
884 INIT_LIST_HEAD(&clock->list);
885 clock->bus = bus;
886 mutex_init(&clock->extreg_lock);
887 mutex_init(&clock->clock_lock);
888 INIT_LIST_HEAD(&clock->phylist);
889 clock->caps.owner = THIS_MODULE;
890 sprintf(clock->caps.name, "dp83640 timer");
891 clock->caps.max_adj = 1953124;
892 clock->caps.n_alarm = 0;
893 clock->caps.n_ext_ts = N_EXT_TS;
894 clock->caps.n_per_out = 1;
895 clock->caps.pps = 0;
896 clock->caps.adjfreq = ptp_dp83640_adjfreq;
897 clock->caps.adjtime = ptp_dp83640_adjtime;
898 clock->caps.gettime = ptp_dp83640_gettime;
899 clock->caps.settime = ptp_dp83640_settime;
900 clock->caps.enable = ptp_dp83640_enable;
901
902
903
904 get_device(&bus->dev);
905}
906
907static int choose_this_phy(struct dp83640_clock *clock,
908 struct phy_device *phydev)
909{
910 if (chosen_phy == -1 && !clock->chosen)
911 return 1;
912
913 if (chosen_phy == phydev->mdio_addr)
914 return 1;
915
916 return 0;
917}
918
919static struct dp83640_clock *dp83640_clock_get(struct dp83640_clock *clock)
920{
921 if (clock)
922 mutex_lock(&clock->clock_lock);
923 return clock;
924}
925
926
927
928
929
930static struct dp83640_clock *dp83640_clock_get_bus(struct mii_bus *bus)
931{
932 struct dp83640_clock *clock = NULL, *tmp;
933 struct list_head *this;
934
935 mutex_lock(&phyter_clocks_lock);
936
937 list_for_each(this, &phyter_clocks) {
938 tmp = list_entry(this, struct dp83640_clock, list);
939 if (tmp->bus == bus) {
940 clock = tmp;
941 break;
942 }
943 }
944 if (clock)
945 goto out;
946
947 clock = kzalloc(sizeof(struct dp83640_clock), GFP_KERNEL);
948 if (!clock)
949 goto out;
950
951 dp83640_clock_init(clock, bus);
952 list_add_tail(&phyter_clocks, &clock->list);
953out:
954 mutex_unlock(&phyter_clocks_lock);
955
956 return dp83640_clock_get(clock);
957}
958
959static void dp83640_clock_put(struct dp83640_clock *clock)
960{
961 mutex_unlock(&clock->clock_lock);
962}
963
964static int dp83640_probe(struct phy_device *phydev)
965{
966 struct dp83640_clock *clock;
967 struct dp83640_private *dp83640;
968 int err = -ENOMEM, i;
969
970 if (phydev->mdio_addr == BROADCAST_ADDR)
971 return 0;
972
973 clock = dp83640_clock_get_bus(phydev->mdio_bus);
974 if (!clock)
975 goto no_clock;
976
977 dp83640 = kzalloc(sizeof(struct dp83640_private), GFP_KERNEL);
978 if (!dp83640)
979 goto no_memory;
980
981 dp83640->phydev = phydev;
982 INIT_WORK(&dp83640->ts_work, rx_timestamp_work);
983
984 INIT_LIST_HEAD(&dp83640->rxts);
985 INIT_LIST_HEAD(&dp83640->rxpool);
986 for (i = 0; i < MAX_RXTS; i++)
987 list_add(&dp83640->rx_pool_data[i].list, &dp83640->rxpool);
988
989 phydev->priv = dp83640;
990
991 spin_lock_init(&dp83640->rx_lock);
992 skb_queue_head_init(&dp83640->rx_queue);
993 skb_queue_head_init(&dp83640->tx_queue);
994
995 dp83640->clock = clock;
996
997 if (choose_this_phy(clock, phydev)) {
998 clock->chosen = dp83640;
999 clock->ptp_clock = ptp_clock_register(&clock->caps,
1000 &phydev->mdio_dev);
1001 if (IS_ERR(clock->ptp_clock)) {
1002 err = PTR_ERR(clock->ptp_clock);
1003 goto no_register;
1004 }
1005 } else
1006 list_add_tail(&dp83640->list, &clock->phylist);
1007
1008 if (clock->chosen && !list_empty(&clock->phylist))
1009 recalibrate(clock);
1010 else
1011 enable_broadcast(dp83640->phydev, clock->page, 1);
1012
1013 dp83640_clock_put(clock);
1014 return 0;
1015
1016no_register:
1017 clock->chosen = NULL;
1018 kfree(dp83640);
1019no_memory:
1020 dp83640_clock_put(clock);
1021no_clock:
1022 return err;
1023}
1024
1025static void dp83640_remove(struct phy_device *phydev)
1026{
1027 struct dp83640_clock *clock;
1028 struct list_head *this, *next;
1029 struct dp83640_private *tmp, *dp83640 = phydev->priv;
1030 struct sk_buff *skb;
1031
1032 if (phydev->mdio_addr == BROADCAST_ADDR)
1033 return;
1034
1035 enable_status_frames(phydev, false);
1036 cancel_work_sync(&dp83640->ts_work);
1037
1038 while ((skb = skb_dequeue(&dp83640->rx_queue)) != NULL)
1039 kfree_skb(skb);
1040
1041 while ((skb = skb_dequeue(&dp83640->tx_queue)) != NULL)
1042 skb_complete_tx_timestamp(skb, NULL);
1043
1044 clock = dp83640_clock_get(dp83640->clock);
1045
1046 if (dp83640 == clock->chosen) {
1047 ptp_clock_unregister(clock->ptp_clock);
1048 clock->chosen = NULL;
1049 } else {
1050 list_for_each_safe(this, next, &clock->phylist) {
1051 tmp = list_entry(this, struct dp83640_private, list);
1052 if (tmp == dp83640) {
1053 list_del_init(&tmp->list);
1054 break;
1055 }
1056 }
1057 }
1058
1059 dp83640_clock_put(clock);
1060 kfree(dp83640);
1061}
1062
1063static int dp83640_ack_interrupt(struct phy_device *phydev)
1064{
1065 int err = phy_read(phydev, MII_DP83640_MISR);
1066
1067 if (err < 0)
1068 return err;
1069
1070 return 0;
1071}
1072
1073static int dp83640_config_intr(struct phy_device *phydev)
1074{
1075 int micr;
1076 int misr;
1077 int err;
1078
1079 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1080 misr = phy_read(phydev, MII_DP83640_MISR);
1081 if (misr < 0)
1082 return misr;
1083 misr |=
1084 (MII_DP83640_MISR_ANC_INT_EN |
1085 MII_DP83640_MISR_DUP_INT_EN |
1086 MII_DP83640_MISR_SPD_INT_EN |
1087 MII_DP83640_MISR_LINK_INT_EN);
1088 err = phy_write(phydev, MII_DP83640_MISR, misr);
1089 if (err < 0)
1090 return err;
1091
1092 micr = phy_read(phydev, MII_DP83640_MICR);
1093 if (micr < 0)
1094 return micr;
1095 micr |=
1096 (MII_DP83640_MICR_OE |
1097 MII_DP83640_MICR_IE);
1098 return phy_write(phydev, MII_DP83640_MICR, micr);
1099 } else {
1100 micr = phy_read(phydev, MII_DP83640_MICR);
1101 if (micr < 0)
1102 return micr;
1103 micr &=
1104 ~(MII_DP83640_MICR_OE |
1105 MII_DP83640_MICR_IE);
1106 err = phy_write(phydev, MII_DP83640_MICR, micr);
1107 if (err < 0)
1108 return err;
1109
1110 misr = phy_read(phydev, MII_DP83640_MISR);
1111 if (misr < 0)
1112 return misr;
1113 misr &=
1114 ~(MII_DP83640_MISR_ANC_INT_EN |
1115 MII_DP83640_MISR_DUP_INT_EN |
1116 MII_DP83640_MISR_SPD_INT_EN |
1117 MII_DP83640_MISR_LINK_INT_EN);
1118 return phy_write(phydev, MII_DP83640_MISR, misr);
1119 }
1120}
1121
1122static int dp83640_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
1123{
1124 struct dp83640_private *dp83640 = phydev->priv;
1125 struct hwtstamp_config cfg;
1126 u16 txcfg0, rxcfg0;
1127
1128 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1129 return -EFAULT;
1130
1131 if (cfg.flags)
1132 return -EINVAL;
1133
1134 if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ONESTEP_SYNC)
1135 return -ERANGE;
1136
1137 dp83640->hwts_tx_en = cfg.tx_type;
1138
1139 switch (cfg.rx_filter) {
1140 case HWTSTAMP_FILTER_NONE:
1141 dp83640->hwts_rx_en = 0;
1142 dp83640->layer = 0;
1143 dp83640->version = 0;
1144 break;
1145 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1146 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1147 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1148 dp83640->hwts_rx_en = 1;
1149 dp83640->layer = LAYER4;
1150 dp83640->version = 1;
1151 break;
1152 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1153 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1154 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1155 dp83640->hwts_rx_en = 1;
1156 dp83640->layer = LAYER4;
1157 dp83640->version = 2;
1158 break;
1159 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1160 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1161 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1162 dp83640->hwts_rx_en = 1;
1163 dp83640->layer = LAYER2;
1164 dp83640->version = 2;
1165 break;
1166 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1167 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1168 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1169 dp83640->hwts_rx_en = 1;
1170 dp83640->layer = LAYER4|LAYER2;
1171 dp83640->version = 2;
1172 break;
1173 default:
1174 return -ERANGE;
1175 }
1176
1177 txcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1178 rxcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1179
1180 if (dp83640->layer & LAYER2) {
1181 txcfg0 |= TX_L2_EN;
1182 rxcfg0 |= RX_L2_EN;
1183 }
1184 if (dp83640->layer & LAYER4) {
1185 txcfg0 |= TX_IPV6_EN | TX_IPV4_EN;
1186 rxcfg0 |= RX_IPV6_EN | RX_IPV4_EN;
1187 }
1188
1189 if (dp83640->hwts_tx_en)
1190 txcfg0 |= TX_TS_EN;
1191
1192 if (dp83640->hwts_tx_en == HWTSTAMP_TX_ONESTEP_SYNC)
1193 txcfg0 |= SYNC_1STEP | CHK_1STEP;
1194
1195 if (dp83640->hwts_rx_en)
1196 rxcfg0 |= RX_TS_EN;
1197
1198 mutex_lock(&dp83640->clock->extreg_lock);
1199
1200 if (dp83640->hwts_tx_en || dp83640->hwts_rx_en) {
1201 enable_status_frames(phydev, true);
1202 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_ENABLE);
1203 }
1204
1205 ext_write(0, phydev, PAGE5, PTP_TXCFG0, txcfg0);
1206 ext_write(0, phydev, PAGE5, PTP_RXCFG0, rxcfg0);
1207
1208 mutex_unlock(&dp83640->clock->extreg_lock);
1209
1210 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1211}
1212
1213static void rx_timestamp_work(struct work_struct *work)
1214{
1215 struct dp83640_private *dp83640 =
1216 container_of(work, struct dp83640_private, ts_work);
1217 struct list_head *this, *next;
1218 struct rxts *rxts;
1219 struct skb_shared_hwtstamps *shhwtstamps;
1220 struct sk_buff *skb;
1221 unsigned int type;
1222 unsigned long flags;
1223
1224
1225
1226 while ((skb = skb_dequeue(&dp83640->rx_queue)) != NULL) {
1227 type = SKB_PTP_TYPE(skb);
1228 spin_lock_irqsave(&dp83640->rx_lock, flags);
1229 list_for_each_safe(this, next, &dp83640->rxts) {
1230 rxts = list_entry(this, struct rxts, list);
1231 if (match(skb, type, rxts)) {
1232 shhwtstamps = skb_hwtstamps(skb);
1233 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
1234 shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
1235 list_del_init(&rxts->list);
1236 list_add(&rxts->list, &dp83640->rxpool);
1237 break;
1238 }
1239 }
1240 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
1241 netif_rx_ni(skb);
1242 }
1243
1244
1245
1246 spin_lock_irqsave(&dp83640->rx_lock, flags);
1247 prune_rx_ts(dp83640);
1248 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
1249}
1250
1251static bool dp83640_rxtstamp(struct phy_device *phydev,
1252 struct sk_buff *skb, int type)
1253{
1254 struct dp83640_private *dp83640 = phydev->priv;
1255
1256 if (!dp83640->hwts_rx_en)
1257 return false;
1258
1259 if (is_status_frame(skb, type)) {
1260 decode_status_frame(dp83640, skb);
1261 kfree_skb(skb);
1262 return true;
1263 }
1264
1265 SKB_PTP_TYPE(skb) = type;
1266 skb_queue_tail(&dp83640->rx_queue, skb);
1267 schedule_work(&dp83640->ts_work);
1268
1269 return true;
1270}
1271
1272static void dp83640_txtstamp(struct phy_device *phydev,
1273 struct sk_buff *skb, int type)
1274{
1275 struct dp83640_private *dp83640 = phydev->priv;
1276
1277 switch (dp83640->hwts_tx_en) {
1278
1279 case HWTSTAMP_TX_ONESTEP_SYNC:
1280 if (is_sync(skb, type)) {
1281 skb_complete_tx_timestamp(skb, NULL);
1282 return;
1283 }
1284
1285 case HWTSTAMP_TX_ON:
1286 skb_queue_tail(&dp83640->tx_queue, skb);
1287 schedule_work(&dp83640->ts_work);
1288 break;
1289
1290 case HWTSTAMP_TX_OFF:
1291 default:
1292 skb_complete_tx_timestamp(skb, NULL);
1293 break;
1294 }
1295}
1296
1297static int dp83640_ts_info(struct phy_device *dev, struct ethtool_ts_info *info)
1298{
1299 struct dp83640_private *dp83640 = dev->priv;
1300
1301 info->so_timestamping =
1302 SOF_TIMESTAMPING_TX_HARDWARE |
1303 SOF_TIMESTAMPING_RX_HARDWARE |
1304 SOF_TIMESTAMPING_RAW_HARDWARE;
1305 info->phc_index = ptp_clock_index(dp83640->clock->ptp_clock);
1306 info->tx_types =
1307 (1 << HWTSTAMP_TX_OFF) |
1308 (1 << HWTSTAMP_TX_ON) |
1309 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
1310 info->rx_filters =
1311 (1 << HWTSTAMP_FILTER_NONE) |
1312 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
1313 (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
1314 (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
1315 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
1316 (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
1317 (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
1318 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1319 (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
1320 (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
1321 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
1322 (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
1323 (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ);
1324 return 0;
1325}
1326
1327static struct phy_driver dp83640_driver = {
1328 .phy_id = DP83640_PHY_ID,
1329 .phy_id_mask = 0xfffffff0,
1330 .name = "NatSemi DP83640",
1331 .features = PHY_BASIC_FEATURES,
1332 .flags = PHY_HAS_INTERRUPT,
1333 .probe = dp83640_probe,
1334 .remove = dp83640_remove,
1335 .config_aneg = genphy_config_aneg,
1336 .read_status = genphy_read_status,
1337 .ack_interrupt = dp83640_ack_interrupt,
1338 .config_intr = dp83640_config_intr,
1339 .ts_info = dp83640_ts_info,
1340 .hwtstamp = dp83640_hwtstamp,
1341 .rxtstamp = dp83640_rxtstamp,
1342 .txtstamp = dp83640_txtstamp,
1343 .driver = {.owner = THIS_MODULE,}
1344};
1345
1346static int __init dp83640_init(void)
1347{
1348 return phy_driver_register(&dp83640_driver);
1349}
1350
1351static void __exit dp83640_exit(void)
1352{
1353 dp83640_free_clocks();
1354 phy_driver_unregister(&dp83640_driver);
1355}
1356
1357MODULE_DESCRIPTION("National Semiconductor DP83640 PHY driver");
1358MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.at>");
1359MODULE_LICENSE("GPL");
1360
1361module_init(dp83640_init);
1362module_exit(dp83640_exit);
1363
1364static struct mdio_device_id __maybe_unused dp83640_tbl[] = {
1365 { DP83640_PHY_ID, 0xfffffff0 },
1366 { }
1367};
1368
1369MODULE_DEVICE_TABLE(mdio, dp83640_tbl);
1370