2 * Driver for the National Semiconductor DP83640 PHYTER
4 * Copyright (C) 2010 OMICRON electronics GmbH
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.
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.
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.
20 #include <linux/ethtool.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/mii.h>
24 #include <linux/module.h>
25 #include <linux/net_tstamp.h>
26 #include <linux/netdevice.h>
27 #include <linux/phy.h>
28 #include <linux/ptp_classify.h>
29 #include <linux/ptp_clock_kernel.h>
31 #include "dp83640_reg.h"
33 #define DP83640_PHY_ID 0x20005ce1
40 #define PSF_EVNT 0x4000
48 /* phyter seems to miss the mark by 16 ns */
49 #define ADJTIME_FIX 16
51 #if defined(__BIG_ENDIAN)
53 #elif defined(__LITTLE_ENDIAN)
54 #define ENDIAN_FLAG PSF_ENDIAN
57 #define SKB_PTP_TYPE(__skb) (*(unsigned int *)((__skb)->cb))
60 u16 ns_lo
; /* ns[15:0] */
61 u16 ns_hi
; /* overflow[1:0], ns[29:16] */
62 u16 sec_lo
; /* sec[15:0] */
63 u16 sec_hi
; /* sec[31:16] */
64 u16 seqid
; /* sequenceId[15:0] */
65 u16 msgtype
; /* messageType[3:0], hash[11:0] */
69 u16 ns_lo
; /* ns[15:0] */
70 u16 ns_hi
; /* overflow[1:0], ns[29:16] */
71 u16 sec_lo
; /* sec[15:0] */
72 u16 sec_hi
; /* sec[31:16] */
76 struct list_head list
;
86 struct dp83640_private
{
87 struct list_head list
;
88 struct dp83640_clock
*clock
;
89 struct phy_device
*phydev
;
90 struct work_struct ts_work
;
95 /* remember state of cfg0 during calibration */
97 /* remember the last event time stamp */
98 struct phy_txts edata
;
99 /* list of rx timestamps */
100 struct list_head rxts
;
101 struct list_head rxpool
;
102 struct rxts rx_pool_data
[MAX_RXTS
];
103 /* protects above three fields from concurrent access */
105 /* queues of incoming and outgoing packets */
106 struct sk_buff_head rx_queue
;
107 struct sk_buff_head tx_queue
;
110 struct dp83640_clock
{
111 /* keeps the instance in the 'phyter_clocks' list */
112 struct list_head list
;
113 /* we create one clock instance per MII bus */
115 /* protects extended registers from concurrent access */
116 struct mutex extreg_lock
;
117 /* remembers which page was last selected */
119 /* our advertised capabilities */
120 struct ptp_clock_info caps
;
121 /* protects the three fields below from concurrent access */
122 struct mutex clock_lock
;
123 /* the one phyter from which we shall read */
124 struct dp83640_private
*chosen
;
125 /* list of the other attached phyters, not chosen */
126 struct list_head phylist
;
127 /* reference to our PTP hardware clock */
128 struct ptp_clock
*ptp_clock
;
145 static int chosen_phy
= -1;
146 static ushort gpio_tab
[GPIO_TABLE_SIZE
] = {
147 1, 2, 3, 4, 8, 9, 10, 11
150 module_param(chosen_phy
, int, 0444);
151 module_param_array(gpio_tab
, ushort
, NULL
, 0444);
153 MODULE_PARM_DESC(chosen_phy
, \
154 "The address of the PHY to use for the ancillary clock features");
155 MODULE_PARM_DESC(gpio_tab
, \
156 "Which GPIO line to use for which purpose: cal,perout,extts1,...,extts6");
158 /* a list of clocks and a mutex to protect it */
159 static LIST_HEAD(phyter_clocks
);
160 static DEFINE_MUTEX(phyter_clocks_lock
);
162 static void rx_timestamp_work(struct work_struct
*work
);
164 /* extended register access functions */
166 #define BROADCAST_ADDR 31
168 static inline int broadcast_write(struct mii_bus
*bus
, u32 regnum
, u16 val
)
170 return mdiobus_write(bus
, BROADCAST_ADDR
, regnum
, val
);
173 /* Caller must hold extreg_lock. */
174 static int ext_read(struct phy_device
*phydev
, int page
, u32 regnum
)
176 struct dp83640_private
*dp83640
= phydev
->priv
;
179 if (dp83640
->clock
->page
!= page
) {
180 broadcast_write(phydev
->bus
, PAGESEL
, page
);
181 dp83640
->clock
->page
= page
;
183 val
= phy_read(phydev
, regnum
);
188 /* Caller must hold extreg_lock. */
189 static void ext_write(int broadcast
, struct phy_device
*phydev
,
190 int page
, u32 regnum
, u16 val
)
192 struct dp83640_private
*dp83640
= phydev
->priv
;
194 if (dp83640
->clock
->page
!= page
) {
195 broadcast_write(phydev
->bus
, PAGESEL
, page
);
196 dp83640
->clock
->page
= page
;
199 broadcast_write(phydev
->bus
, regnum
, val
);
201 phy_write(phydev
, regnum
, val
);
204 /* Caller must hold extreg_lock. */
205 static int tdr_write(int bc
, struct phy_device
*dev
,
206 const struct timespec
*ts
, u16 cmd
)
208 ext_write(bc
, dev
, PAGE4
, PTP_TDR
, ts
->tv_nsec
& 0xffff);/* ns[15:0] */
209 ext_write(bc
, dev
, PAGE4
, PTP_TDR
, ts
->tv_nsec
>> 16); /* ns[31:16] */
210 ext_write(bc
, dev
, PAGE4
, PTP_TDR
, ts
->tv_sec
& 0xffff); /* sec[15:0] */
211 ext_write(bc
, dev
, PAGE4
, PTP_TDR
, ts
->tv_sec
>> 16); /* sec[31:16]*/
213 ext_write(bc
, dev
, PAGE4
, PTP_CTL
, cmd
);
218 /* convert phy timestamps into driver timestamps */
220 static void phy2rxts(struct phy_rxts
*p
, struct rxts
*rxts
)
225 sec
|= p
->sec_hi
<< 16;
228 rxts
->ns
|= (p
->ns_hi
& 0x3fff) << 16;
229 rxts
->ns
+= ((u64
)sec
) * 1000000000ULL;
230 rxts
->seqid
= p
->seqid
;
231 rxts
->msgtype
= (p
->msgtype
>> 12) & 0xf;
232 rxts
->hash
= p
->msgtype
& 0x0fff;
233 rxts
->tmo
= jiffies
+ 2;
236 static u64
phy2txts(struct phy_txts
*p
)
242 sec
|= p
->sec_hi
<< 16;
245 ns
|= (p
->ns_hi
& 0x3fff) << 16;
246 ns
+= ((u64
)sec
) * 1000000000ULL;
251 static void periodic_output(struct dp83640_clock
*clock
,
252 struct ptp_clock_request
*clkreq
, bool on
)
254 struct dp83640_private
*dp83640
= clock
->chosen
;
255 struct phy_device
*phydev
= dp83640
->phydev
;
256 u32 sec
, nsec
, period
;
257 u16 gpio
, ptp_trig
, trigger
, val
;
259 gpio
= on
? gpio_tab
[PEROUT_GPIO
] : 0;
260 trigger
= PER_TRIGGER
;
263 (trigger
& TRIG_CSEL_MASK
) << TRIG_CSEL_SHIFT
|
264 (gpio
& TRIG_GPIO_MASK
) << TRIG_GPIO_SHIFT
|
268 val
= (trigger
& TRIG_SEL_MASK
) << TRIG_SEL_SHIFT
;
272 mutex_lock(&clock
->extreg_lock
);
273 ext_write(0, phydev
, PAGE5
, PTP_TRIG
, ptp_trig
);
274 ext_write(0, phydev
, PAGE4
, PTP_CTL
, val
);
275 mutex_unlock(&clock
->extreg_lock
);
279 sec
= clkreq
->perout
.start
.sec
;
280 nsec
= clkreq
->perout
.start
.nsec
;
281 period
= clkreq
->perout
.period
.sec
* 1000000000UL;
282 period
+= clkreq
->perout
.period
.nsec
;
284 mutex_lock(&clock
->extreg_lock
);
286 ext_write(0, phydev
, PAGE5
, PTP_TRIG
, ptp_trig
);
290 ext_write(0, phydev
, PAGE4
, PTP_CTL
, val
);
291 ext_write(0, phydev
, PAGE4
, PTP_TDR
, nsec
& 0xffff); /* ns[15:0] */
292 ext_write(0, phydev
, PAGE4
, PTP_TDR
, nsec
>> 16); /* ns[31:16] */
293 ext_write(0, phydev
, PAGE4
, PTP_TDR
, sec
& 0xffff); /* sec[15:0] */
294 ext_write(0, phydev
, PAGE4
, PTP_TDR
, sec
>> 16); /* sec[31:16] */
295 ext_write(0, phydev
, PAGE4
, PTP_TDR
, period
& 0xffff); /* ns[15:0] */
296 ext_write(0, phydev
, PAGE4
, PTP_TDR
, period
>> 16); /* ns[31:16] */
301 ext_write(0, phydev
, PAGE4
, PTP_CTL
, val
);
303 mutex_unlock(&clock
->extreg_lock
);
306 /* ptp clock methods */
308 static int ptp_dp83640_adjfreq(struct ptp_clock_info
*ptp
, s32 ppb
)
310 struct dp83640_clock
*clock
=
311 container_of(ptp
, struct dp83640_clock
, caps
);
312 struct phy_device
*phydev
= clock
->chosen
->phydev
;
323 rate
= div_u64(rate
, 1953125);
325 hi
= (rate
>> 16) & PTP_RATE_HI_MASK
;
331 mutex_lock(&clock
->extreg_lock
);
333 ext_write(1, phydev
, PAGE4
, PTP_RATEH
, hi
);
334 ext_write(1, phydev
, PAGE4
, PTP_RATEL
, lo
);
336 mutex_unlock(&clock
->extreg_lock
);
341 static int ptp_dp83640_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
343 struct dp83640_clock
*clock
=
344 container_of(ptp
, struct dp83640_clock
, caps
);
345 struct phy_device
*phydev
= clock
->chosen
->phydev
;
349 delta
+= ADJTIME_FIX
;
351 ts
= ns_to_timespec(delta
);
353 mutex_lock(&clock
->extreg_lock
);
355 err
= tdr_write(1, phydev
, &ts
, PTP_STEP_CLK
);
357 mutex_unlock(&clock
->extreg_lock
);
362 static int ptp_dp83640_gettime(struct ptp_clock_info
*ptp
, struct timespec
*ts
)
364 struct dp83640_clock
*clock
=
365 container_of(ptp
, struct dp83640_clock
, caps
);
366 struct phy_device
*phydev
= clock
->chosen
->phydev
;
369 mutex_lock(&clock
->extreg_lock
);
371 ext_write(0, phydev
, PAGE4
, PTP_CTL
, PTP_RD_CLK
);
373 val
[0] = ext_read(phydev
, PAGE4
, PTP_TDR
); /* ns[15:0] */
374 val
[1] = ext_read(phydev
, PAGE4
, PTP_TDR
); /* ns[31:16] */
375 val
[2] = ext_read(phydev
, PAGE4
, PTP_TDR
); /* sec[15:0] */
376 val
[3] = ext_read(phydev
, PAGE4
, PTP_TDR
); /* sec[31:16] */
378 mutex_unlock(&clock
->extreg_lock
);
380 ts
->tv_nsec
= val
[0] | (val
[1] << 16);
381 ts
->tv_sec
= val
[2] | (val
[3] << 16);
386 static int ptp_dp83640_settime(struct ptp_clock_info
*ptp
,
387 const struct timespec
*ts
)
389 struct dp83640_clock
*clock
=
390 container_of(ptp
, struct dp83640_clock
, caps
);
391 struct phy_device
*phydev
= clock
->chosen
->phydev
;
394 mutex_lock(&clock
->extreg_lock
);
396 err
= tdr_write(1, phydev
, ts
, PTP_LOAD_CLK
);
398 mutex_unlock(&clock
->extreg_lock
);
403 static int ptp_dp83640_enable(struct ptp_clock_info
*ptp
,
404 struct ptp_clock_request
*rq
, int on
)
406 struct dp83640_clock
*clock
=
407 container_of(ptp
, struct dp83640_clock
, caps
);
408 struct phy_device
*phydev
= clock
->chosen
->phydev
;
410 u16 evnt
, event_num
, gpio_num
;
413 case PTP_CLK_REQ_EXTTS
:
414 index
= rq
->extts
.index
;
415 if (index
< 0 || index
>= N_EXT_TS
)
417 event_num
= EXT_EVENT
+ index
;
418 evnt
= EVNT_WR
| (event_num
& EVNT_SEL_MASK
) << EVNT_SEL_SHIFT
;
420 gpio_num
= gpio_tab
[EXTTS0_GPIO
+ index
];
421 evnt
|= (gpio_num
& EVNT_GPIO_MASK
) << EVNT_GPIO_SHIFT
;
424 ext_write(0, phydev
, PAGE5
, PTP_EVNT
, evnt
);
427 case PTP_CLK_REQ_PEROUT
:
428 if (rq
->perout
.index
!= 0)
430 periodic_output(clock
, rq
, on
);
440 static u8 status_frame_dst
[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 };
441 static u8 status_frame_src
[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F };
443 static void enable_status_frames(struct phy_device
*phydev
, bool on
)
448 cfg0
= PSF_EVNT_EN
| PSF_RXTS_EN
| PSF_TXTS_EN
| ENDIAN_FLAG
;
450 ver
= (PSF_PTPVER
& VERSIONPTP_MASK
) << VERSIONPTP_SHIFT
;
452 ext_write(0, phydev
, PAGE5
, PSF_CFG0
, cfg0
);
453 ext_write(0, phydev
, PAGE6
, PSF_CFG1
, ver
);
455 if (!phydev
->attached_dev
) {
456 pr_warning("dp83640: expected to find an attached netdevice\n");
461 if (dev_mc_add(phydev
->attached_dev
, status_frame_dst
))
462 pr_warning("dp83640: failed to add mc address\n");
464 if (dev_mc_del(phydev
->attached_dev
, status_frame_dst
))
465 pr_warning("dp83640: failed to delete mc address\n");
469 static bool is_status_frame(struct sk_buff
*skb
, int type
)
471 struct ethhdr
*h
= eth_hdr(skb
);
473 if (PTP_CLASS_V2_L2
== type
&&
474 !memcmp(h
->h_source
, status_frame_src
, sizeof(status_frame_src
)))
480 static int expired(struct rxts
*rxts
)
482 return time_after(jiffies
, rxts
->tmo
);
485 /* Caller must hold rx_lock. */
486 static void prune_rx_ts(struct dp83640_private
*dp83640
)
488 struct list_head
*this, *next
;
491 list_for_each_safe(this, next
, &dp83640
->rxts
) {
492 rxts
= list_entry(this, struct rxts
, list
);
494 list_del_init(&rxts
->list
);
495 list_add(&rxts
->list
, &dp83640
->rxpool
);
500 /* synchronize the phyters so they act as one clock */
502 static void enable_broadcast(struct phy_device
*phydev
, int init_page
, int on
)
505 phy_write(phydev
, PAGESEL
, 0);
506 val
= phy_read(phydev
, PHYCR2
);
511 phy_write(phydev
, PHYCR2
, val
);
512 phy_write(phydev
, PAGESEL
, init_page
);
515 static void recalibrate(struct dp83640_clock
*clock
)
518 struct phy_txts event_ts
;
520 struct list_head
*this;
521 struct dp83640_private
*tmp
;
522 struct phy_device
*master
= clock
->chosen
->phydev
;
523 u16 cal_gpio
, cfg0
, evnt
, ptp_trig
, trigger
, val
;
525 trigger
= CAL_TRIGGER
;
526 cal_gpio
= gpio_tab
[CALIBRATE_GPIO
];
528 mutex_lock(&clock
->extreg_lock
);
531 * enable broadcast, disable status frames, enable ptp clock
533 list_for_each(this, &clock
->phylist
) {
534 tmp
= list_entry(this, struct dp83640_private
, list
);
535 enable_broadcast(tmp
->phydev
, clock
->page
, 1);
536 tmp
->cfg0
= ext_read(tmp
->phydev
, PAGE5
, PSF_CFG0
);
537 ext_write(0, tmp
->phydev
, PAGE5
, PSF_CFG0
, 0);
538 ext_write(0, tmp
->phydev
, PAGE4
, PTP_CTL
, PTP_ENABLE
);
540 enable_broadcast(master
, clock
->page
, 1);
541 cfg0
= ext_read(master
, PAGE5
, PSF_CFG0
);
542 ext_write(0, master
, PAGE5
, PSF_CFG0
, 0);
543 ext_write(0, master
, PAGE4
, PTP_CTL
, PTP_ENABLE
);
546 * enable an event timestamp
548 evnt
= EVNT_WR
| EVNT_RISE
| EVNT_SINGLE
;
549 evnt
|= (CAL_EVENT
& EVNT_SEL_MASK
) << EVNT_SEL_SHIFT
;
550 evnt
|= (cal_gpio
& EVNT_GPIO_MASK
) << EVNT_GPIO_SHIFT
;
552 list_for_each(this, &clock
->phylist
) {
553 tmp
= list_entry(this, struct dp83640_private
, list
);
554 ext_write(0, tmp
->phydev
, PAGE5
, PTP_EVNT
, evnt
);
556 ext_write(0, master
, PAGE5
, PTP_EVNT
, evnt
);
559 * configure a trigger
561 ptp_trig
= TRIG_WR
| TRIG_IF_LATE
| TRIG_PULSE
;
562 ptp_trig
|= (trigger
& TRIG_CSEL_MASK
) << TRIG_CSEL_SHIFT
;
563 ptp_trig
|= (cal_gpio
& TRIG_GPIO_MASK
) << TRIG_GPIO_SHIFT
;
564 ext_write(0, master
, PAGE5
, PTP_TRIG
, ptp_trig
);
567 val
= (trigger
& TRIG_SEL_MASK
) << TRIG_SEL_SHIFT
;
569 ext_write(0, master
, PAGE4
, PTP_CTL
, val
);
574 ext_write(0, master
, PAGE4
, PTP_CTL
, val
);
576 /* disable trigger */
577 val
= (trigger
& TRIG_SEL_MASK
) << TRIG_SEL_SHIFT
;
579 ext_write(0, master
, PAGE4
, PTP_CTL
, val
);
582 * read out and correct offsets
584 val
= ext_read(master
, PAGE4
, PTP_STS
);
585 pr_info("master PTP_STS 0x%04hx", val
);
586 val
= ext_read(master
, PAGE4
, PTP_ESTS
);
587 pr_info("master PTP_ESTS 0x%04hx", val
);
588 event_ts
.ns_lo
= ext_read(master
, PAGE4
, PTP_EDATA
);
589 event_ts
.ns_hi
= ext_read(master
, PAGE4
, PTP_EDATA
);
590 event_ts
.sec_lo
= ext_read(master
, PAGE4
, PTP_EDATA
);
591 event_ts
.sec_hi
= ext_read(master
, PAGE4
, PTP_EDATA
);
592 now
= phy2txts(&event_ts
);
594 list_for_each(this, &clock
->phylist
) {
595 tmp
= list_entry(this, struct dp83640_private
, list
);
596 val
= ext_read(tmp
->phydev
, PAGE4
, PTP_STS
);
597 pr_info("slave PTP_STS 0x%04hx", val
);
598 val
= ext_read(tmp
->phydev
, PAGE4
, PTP_ESTS
);
599 pr_info("slave PTP_ESTS 0x%04hx", val
);
600 event_ts
.ns_lo
= ext_read(tmp
->phydev
, PAGE4
, PTP_EDATA
);
601 event_ts
.ns_hi
= ext_read(tmp
->phydev
, PAGE4
, PTP_EDATA
);
602 event_ts
.sec_lo
= ext_read(tmp
->phydev
, PAGE4
, PTP_EDATA
);
603 event_ts
.sec_hi
= ext_read(tmp
->phydev
, PAGE4
, PTP_EDATA
);
604 diff
= now
- (s64
) phy2txts(&event_ts
);
605 pr_info("slave offset %lld nanoseconds\n", diff
);
607 ts
= ns_to_timespec(diff
);
608 tdr_write(0, tmp
->phydev
, &ts
, PTP_STEP_CLK
);
612 * restore status frames
614 list_for_each(this, &clock
->phylist
) {
615 tmp
= list_entry(this, struct dp83640_private
, list
);
616 ext_write(0, tmp
->phydev
, PAGE5
, PSF_CFG0
, tmp
->cfg0
);
618 ext_write(0, master
, PAGE5
, PSF_CFG0
, cfg0
);
620 mutex_unlock(&clock
->extreg_lock
);
623 /* time stamping methods */
625 static inline u16
exts_chan_to_edata(int ch
)
627 return 1 << ((ch
+ EXT_EVENT
) * 2);
630 static int decode_evnt(struct dp83640_private
*dp83640
,
631 void *data
, u16 ests
)
633 struct phy_txts
*phy_txts
;
634 struct ptp_clock_event event
;
636 int words
= (ests
>> EVNT_TS_LEN_SHIFT
) & EVNT_TS_LEN_MASK
;
639 if (ests
& MULT_EVNT
) {
640 ext_status
= *(u16
*) data
;
641 data
+= sizeof(ext_status
);
646 switch (words
) { /* fall through in every case */
648 dp83640
->edata
.sec_hi
= phy_txts
->sec_hi
;
650 dp83640
->edata
.sec_lo
= phy_txts
->sec_lo
;
652 dp83640
->edata
.ns_hi
= phy_txts
->ns_hi
;
654 dp83640
->edata
.ns_lo
= phy_txts
->ns_lo
;
661 i
= ((ests
>> EVNT_NUM_SHIFT
) & EVNT_NUM_MASK
) - EXT_EVENT
;
662 ext_status
= exts_chan_to_edata(i
);
665 event
.type
= PTP_CLOCK_EXTTS
;
666 event
.timestamp
= phy2txts(&dp83640
->edata
);
668 for (i
= 0; i
< N_EXT_TS
; i
++) {
669 if (ext_status
& exts_chan_to_edata(i
)) {
671 ptp_clock_event(dp83640
->clock
->ptp_clock
, &event
);
675 return parsed
* sizeof(u16
);
678 static void decode_rxts(struct dp83640_private
*dp83640
,
679 struct phy_rxts
*phy_rxts
)
684 spin_lock_irqsave(&dp83640
->rx_lock
, flags
);
686 prune_rx_ts(dp83640
);
688 if (list_empty(&dp83640
->rxpool
)) {
689 pr_debug("dp83640: rx timestamp pool is empty\n");
692 rxts
= list_first_entry(&dp83640
->rxpool
, struct rxts
, list
);
693 list_del_init(&rxts
->list
);
694 phy2rxts(phy_rxts
, rxts
);
695 list_add_tail(&rxts
->list
, &dp83640
->rxts
);
697 spin_unlock_irqrestore(&dp83640
->rx_lock
, flags
);
700 static void decode_txts(struct dp83640_private
*dp83640
,
701 struct phy_txts
*phy_txts
)
703 struct skb_shared_hwtstamps shhwtstamps
;
707 /* We must already have the skb that triggered this. */
709 skb
= skb_dequeue(&dp83640
->tx_queue
);
712 pr_debug("dp83640: have timestamp but tx_queue empty\n");
715 ns
= phy2txts(phy_txts
);
716 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
717 shhwtstamps
.hwtstamp
= ns_to_ktime(ns
);
718 skb_complete_tx_timestamp(skb
, &shhwtstamps
);
721 static void decode_status_frame(struct dp83640_private
*dp83640
,
724 struct phy_rxts
*phy_rxts
;
725 struct phy_txts
*phy_txts
;
732 for (len
= skb_headlen(skb
) - 2; len
> sizeof(type
); len
-= size
) {
735 ests
= type
& 0x0fff;
736 type
= type
& 0xf000;
740 if (PSF_RX
== type
&& len
>= sizeof(*phy_rxts
)) {
742 phy_rxts
= (struct phy_rxts
*) ptr
;
743 decode_rxts(dp83640
, phy_rxts
);
744 size
= sizeof(*phy_rxts
);
746 } else if (PSF_TX
== type
&& len
>= sizeof(*phy_txts
)) {
748 phy_txts
= (struct phy_txts
*) ptr
;
749 decode_txts(dp83640
, phy_txts
);
750 size
= sizeof(*phy_txts
);
752 } else if (PSF_EVNT
== type
&& len
>= sizeof(*phy_txts
)) {
754 size
= decode_evnt(dp83640
, ptr
, ests
);
764 static int is_sync(struct sk_buff
*skb
, int type
)
766 u8
*data
= skb
->data
, *msgtype
;
767 unsigned int offset
= 0;
770 case PTP_CLASS_V1_IPV4
:
771 case PTP_CLASS_V2_IPV4
:
772 offset
= ETH_HLEN
+ IPV4_HLEN(data
) + UDP_HLEN
;
774 case PTP_CLASS_V1_IPV6
:
775 case PTP_CLASS_V2_IPV6
:
778 case PTP_CLASS_V2_L2
:
781 case PTP_CLASS_V2_VLAN
:
782 offset
= ETH_HLEN
+ VLAN_HLEN
;
788 if (type
& PTP_CLASS_V1
)
789 offset
+= OFF_PTP_CONTROL
;
791 if (skb
->len
< offset
+ 1)
794 msgtype
= data
+ offset
;
796 return (*msgtype
& 0xf) == 0;
799 static int match(struct sk_buff
*skb
, unsigned int type
, struct rxts
*rxts
)
803 u8
*msgtype
, *data
= skb_mac_header(skb
);
805 /* check sequenceID, messageType, 12 bit hash of offset 20-29 */
808 case PTP_CLASS_V1_IPV4
:
809 case PTP_CLASS_V2_IPV4
:
810 offset
= ETH_HLEN
+ IPV4_HLEN(data
) + UDP_HLEN
;
812 case PTP_CLASS_V1_IPV6
:
813 case PTP_CLASS_V2_IPV6
:
816 case PTP_CLASS_V2_L2
:
819 case PTP_CLASS_V2_VLAN
:
820 offset
= ETH_HLEN
+ VLAN_HLEN
;
826 if (skb
->len
+ ETH_HLEN
< offset
+ OFF_PTP_SEQUENCE_ID
+ sizeof(*seqid
))
829 if (unlikely(type
& PTP_CLASS_V1
))
830 msgtype
= data
+ offset
+ OFF_PTP_CONTROL
;
832 msgtype
= data
+ offset
;
834 seqid
= (u16
*)(data
+ offset
+ OFF_PTP_SEQUENCE_ID
);
836 return (rxts
->msgtype
== (*msgtype
& 0xf) &&
837 rxts
->seqid
== ntohs(*seqid
));
840 static void dp83640_free_clocks(void)
842 struct dp83640_clock
*clock
;
843 struct list_head
*this, *next
;
845 mutex_lock(&phyter_clocks_lock
);
847 list_for_each_safe(this, next
, &phyter_clocks
) {
848 clock
= list_entry(this, struct dp83640_clock
, list
);
849 if (!list_empty(&clock
->phylist
)) {
850 pr_warning("phy list non-empty while unloading");
853 list_del(&clock
->list
);
854 mutex_destroy(&clock
->extreg_lock
);
855 mutex_destroy(&clock
->clock_lock
);
856 put_device(&clock
->bus
->dev
);
860 mutex_unlock(&phyter_clocks_lock
);
863 static void dp83640_clock_init(struct dp83640_clock
*clock
, struct mii_bus
*bus
)
865 INIT_LIST_HEAD(&clock
->list
);
867 mutex_init(&clock
->extreg_lock
);
868 mutex_init(&clock
->clock_lock
);
869 INIT_LIST_HEAD(&clock
->phylist
);
870 clock
->caps
.owner
= THIS_MODULE
;
871 sprintf(clock
->caps
.name
, "dp83640 timer");
872 clock
->caps
.max_adj
= 1953124;
873 clock
->caps
.n_alarm
= 0;
874 clock
->caps
.n_ext_ts
= N_EXT_TS
;
875 clock
->caps
.n_per_out
= 1;
877 clock
->caps
.adjfreq
= ptp_dp83640_adjfreq
;
878 clock
->caps
.adjtime
= ptp_dp83640_adjtime
;
879 clock
->caps
.gettime
= ptp_dp83640_gettime
;
880 clock
->caps
.settime
= ptp_dp83640_settime
;
881 clock
->caps
.enable
= ptp_dp83640_enable
;
883 * Get a reference to this bus instance.
885 get_device(&bus
->dev
);
888 static int choose_this_phy(struct dp83640_clock
*clock
,
889 struct phy_device
*phydev
)
891 if (chosen_phy
== -1 && !clock
->chosen
)
894 if (chosen_phy
== phydev
->addr
)
900 static struct dp83640_clock
*dp83640_clock_get(struct dp83640_clock
*clock
)
903 mutex_lock(&clock
->clock_lock
);
908 * Look up and lock a clock by bus instance.
909 * If there is no clock for this bus, then create it first.
911 static struct dp83640_clock
*dp83640_clock_get_bus(struct mii_bus
*bus
)
913 struct dp83640_clock
*clock
= NULL
, *tmp
;
914 struct list_head
*this;
916 mutex_lock(&phyter_clocks_lock
);
918 list_for_each(this, &phyter_clocks
) {
919 tmp
= list_entry(this, struct dp83640_clock
, list
);
920 if (tmp
->bus
== bus
) {
928 clock
= kzalloc(sizeof(struct dp83640_clock
), GFP_KERNEL
);
932 dp83640_clock_init(clock
, bus
);
933 list_add_tail(&phyter_clocks
, &clock
->list
);
935 mutex_unlock(&phyter_clocks_lock
);
937 return dp83640_clock_get(clock
);
940 static void dp83640_clock_put(struct dp83640_clock
*clock
)
942 mutex_unlock(&clock
->clock_lock
);
945 static int dp83640_probe(struct phy_device
*phydev
)
947 struct dp83640_clock
*clock
;
948 struct dp83640_private
*dp83640
;
949 int err
= -ENOMEM
, i
;
951 if (phydev
->addr
== BROADCAST_ADDR
)
954 clock
= dp83640_clock_get_bus(phydev
->bus
);
958 dp83640
= kzalloc(sizeof(struct dp83640_private
), GFP_KERNEL
);
962 dp83640
->phydev
= phydev
;
963 INIT_WORK(&dp83640
->ts_work
, rx_timestamp_work
);
965 INIT_LIST_HEAD(&dp83640
->rxts
);
966 INIT_LIST_HEAD(&dp83640
->rxpool
);
967 for (i
= 0; i
< MAX_RXTS
; i
++)
968 list_add(&dp83640
->rx_pool_data
[i
].list
, &dp83640
->rxpool
);
970 phydev
->priv
= dp83640
;
972 spin_lock_init(&dp83640
->rx_lock
);
973 skb_queue_head_init(&dp83640
->rx_queue
);
974 skb_queue_head_init(&dp83640
->tx_queue
);
976 dp83640
->clock
= clock
;
978 if (choose_this_phy(clock
, phydev
)) {
979 clock
->chosen
= dp83640
;
980 clock
->ptp_clock
= ptp_clock_register(&clock
->caps
);
981 if (IS_ERR(clock
->ptp_clock
)) {
982 err
= PTR_ERR(clock
->ptp_clock
);
986 list_add_tail(&dp83640
->list
, &clock
->phylist
);
988 if (clock
->chosen
&& !list_empty(&clock
->phylist
))
991 enable_broadcast(dp83640
->phydev
, clock
->page
, 1);
993 dp83640_clock_put(clock
);
997 clock
->chosen
= NULL
;
1000 dp83640_clock_put(clock
);
1005 static void dp83640_remove(struct phy_device
*phydev
)
1007 struct dp83640_clock
*clock
;
1008 struct list_head
*this, *next
;
1009 struct dp83640_private
*tmp
, *dp83640
= phydev
->priv
;
1010 struct sk_buff
*skb
;
1012 if (phydev
->addr
== BROADCAST_ADDR
)
1015 enable_status_frames(phydev
, false);
1016 cancel_work_sync(&dp83640
->ts_work
);
1018 while ((skb
= skb_dequeue(&dp83640
->rx_queue
)) != NULL
)
1021 while ((skb
= skb_dequeue(&dp83640
->tx_queue
)) != NULL
)
1022 skb_complete_tx_timestamp(skb
, NULL
);
1024 clock
= dp83640_clock_get(dp83640
->clock
);
1026 if (dp83640
== clock
->chosen
) {
1027 ptp_clock_unregister(clock
->ptp_clock
);
1028 clock
->chosen
= NULL
;
1030 list_for_each_safe(this, next
, &clock
->phylist
) {
1031 tmp
= list_entry(this, struct dp83640_private
, list
);
1032 if (tmp
== dp83640
) {
1033 list_del_init(&tmp
->list
);
1039 dp83640_clock_put(clock
);
1043 static int dp83640_hwtstamp(struct phy_device
*phydev
, struct ifreq
*ifr
)
1045 struct dp83640_private
*dp83640
= phydev
->priv
;
1046 struct hwtstamp_config cfg
;
1049 if (copy_from_user(&cfg
, ifr
->ifr_data
, sizeof(cfg
)))
1052 if (cfg
.flags
) /* reserved for future extensions */
1055 if (cfg
.tx_type
< 0 || cfg
.tx_type
> HWTSTAMP_TX_ONESTEP_SYNC
)
1058 dp83640
->hwts_tx_en
= cfg
.tx_type
;
1060 switch (cfg
.rx_filter
) {
1061 case HWTSTAMP_FILTER_NONE
:
1062 dp83640
->hwts_rx_en
= 0;
1064 dp83640
->version
= 0;
1066 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
1067 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
1068 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
1069 dp83640
->hwts_rx_en
= 1;
1070 dp83640
->layer
= LAYER4
;
1071 dp83640
->version
= 1;
1073 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
1074 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
1075 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
1076 dp83640
->hwts_rx_en
= 1;
1077 dp83640
->layer
= LAYER4
;
1078 dp83640
->version
= 2;
1080 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
1081 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
1082 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
1083 dp83640
->hwts_rx_en
= 1;
1084 dp83640
->layer
= LAYER2
;
1085 dp83640
->version
= 2;
1087 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
1088 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
1089 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
1090 dp83640
->hwts_rx_en
= 1;
1091 dp83640
->layer
= LAYER4
|LAYER2
;
1092 dp83640
->version
= 2;
1098 txcfg0
= (dp83640
->version
& TX_PTP_VER_MASK
) << TX_PTP_VER_SHIFT
;
1099 rxcfg0
= (dp83640
->version
& TX_PTP_VER_MASK
) << TX_PTP_VER_SHIFT
;
1101 if (dp83640
->layer
& LAYER2
) {
1105 if (dp83640
->layer
& LAYER4
) {
1106 txcfg0
|= TX_IPV6_EN
| TX_IPV4_EN
;
1107 rxcfg0
|= RX_IPV6_EN
| RX_IPV4_EN
;
1110 if (dp83640
->hwts_tx_en
)
1113 if (dp83640
->hwts_tx_en
== HWTSTAMP_TX_ONESTEP_SYNC
)
1114 txcfg0
|= SYNC_1STEP
| CHK_1STEP
;
1116 if (dp83640
->hwts_rx_en
)
1119 mutex_lock(&dp83640
->clock
->extreg_lock
);
1121 if (dp83640
->hwts_tx_en
|| dp83640
->hwts_rx_en
) {
1122 enable_status_frames(phydev
, true);
1123 ext_write(0, phydev
, PAGE4
, PTP_CTL
, PTP_ENABLE
);
1126 ext_write(0, phydev
, PAGE5
, PTP_TXCFG0
, txcfg0
);
1127 ext_write(0, phydev
, PAGE5
, PTP_RXCFG0
, rxcfg0
);
1129 mutex_unlock(&dp83640
->clock
->extreg_lock
);
1131 return copy_to_user(ifr
->ifr_data
, &cfg
, sizeof(cfg
)) ? -EFAULT
: 0;
1134 static void rx_timestamp_work(struct work_struct
*work
)
1136 struct dp83640_private
*dp83640
=
1137 container_of(work
, struct dp83640_private
, ts_work
);
1138 struct list_head
*this, *next
;
1140 struct skb_shared_hwtstamps
*shhwtstamps
;
1141 struct sk_buff
*skb
;
1143 unsigned long flags
;
1145 /* Deliver each deferred packet, with or without a time stamp. */
1147 while ((skb
= skb_dequeue(&dp83640
->rx_queue
)) != NULL
) {
1148 type
= SKB_PTP_TYPE(skb
);
1149 spin_lock_irqsave(&dp83640
->rx_lock
, flags
);
1150 list_for_each_safe(this, next
, &dp83640
->rxts
) {
1151 rxts
= list_entry(this, struct rxts
, list
);
1152 if (match(skb
, type
, rxts
)) {
1153 shhwtstamps
= skb_hwtstamps(skb
);
1154 memset(shhwtstamps
, 0, sizeof(*shhwtstamps
));
1155 shhwtstamps
->hwtstamp
= ns_to_ktime(rxts
->ns
);
1156 list_del_init(&rxts
->list
);
1157 list_add(&rxts
->list
, &dp83640
->rxpool
);
1161 spin_unlock_irqrestore(&dp83640
->rx_lock
, flags
);
1165 /* Clear out expired time stamps. */
1167 spin_lock_irqsave(&dp83640
->rx_lock
, flags
);
1168 prune_rx_ts(dp83640
);
1169 spin_unlock_irqrestore(&dp83640
->rx_lock
, flags
);
1172 static bool dp83640_rxtstamp(struct phy_device
*phydev
,
1173 struct sk_buff
*skb
, int type
)
1175 struct dp83640_private
*dp83640
= phydev
->priv
;
1177 if (!dp83640
->hwts_rx_en
)
1180 if (is_status_frame(skb
, type
)) {
1181 decode_status_frame(dp83640
, skb
);
1186 SKB_PTP_TYPE(skb
) = type
;
1187 skb_queue_tail(&dp83640
->rx_queue
, skb
);
1188 schedule_work(&dp83640
->ts_work
);
1193 static void dp83640_txtstamp(struct phy_device
*phydev
,
1194 struct sk_buff
*skb
, int type
)
1196 struct dp83640_private
*dp83640
= phydev
->priv
;
1198 switch (dp83640
->hwts_tx_en
) {
1200 case HWTSTAMP_TX_ONESTEP_SYNC
:
1201 if (is_sync(skb
, type
)) {
1202 skb_complete_tx_timestamp(skb
, NULL
);
1206 case HWTSTAMP_TX_ON
:
1207 skb_queue_tail(&dp83640
->tx_queue
, skb
);
1208 schedule_work(&dp83640
->ts_work
);
1211 case HWTSTAMP_TX_OFF
:
1213 skb_complete_tx_timestamp(skb
, NULL
);
1218 static struct phy_driver dp83640_driver
= {
1219 .phy_id
= DP83640_PHY_ID
,
1220 .phy_id_mask
= 0xfffffff0,
1221 .name
= "NatSemi DP83640",
1222 .features
= PHY_BASIC_FEATURES
,
1224 .probe
= dp83640_probe
,
1225 .remove
= dp83640_remove
,
1226 .config_aneg
= genphy_config_aneg
,
1227 .read_status
= genphy_read_status
,
1228 .hwtstamp
= dp83640_hwtstamp
,
1229 .rxtstamp
= dp83640_rxtstamp
,
1230 .txtstamp
= dp83640_txtstamp
,
1231 .driver
= {.owner
= THIS_MODULE
,}
1234 static int __init
dp83640_init(void)
1236 return phy_driver_register(&dp83640_driver
);
1239 static void __exit
dp83640_exit(void)
1241 dp83640_free_clocks();
1242 phy_driver_unregister(&dp83640_driver
);
1245 MODULE_DESCRIPTION("National Semiconductor DP83640 PHY driver");
1246 MODULE_AUTHOR("Richard Cochran <richard.cochran@omicron.at>");
1247 MODULE_LICENSE("GPL");
1249 module_init(dp83640_init
);
1250 module_exit(dp83640_exit
);
1252 static struct mdio_device_id __maybe_unused dp83640_tbl
[] = {
1253 { DP83640_PHY_ID
, 0xfffffff0 },
1257 MODULE_DEVICE_TABLE(mdio
, dp83640_tbl
);