21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
29 #include <linux/netdevice.h>
36 #define DP83640_PHY_ID 0x20005ce1
43 #define PSF_EVNT 0x4000
52 #define ADJTIME_FIX 16
54 #if defined(__BIG_ENDIAN)
56 #elif defined(__LITTLE_ENDIAN)
57 #define ENDIAN_FLAG PSF_ENDIAN
60 #define SKB_PTP_TYPE(__skb) (*(unsigned int *)((__skb)->cb))
148 static int chosen_phy = -1;
150 1, 2, 3, 4, 8, 9, 10, 11
157 "The address of the PHY to use for the ancillary clock features");
159 "Which GPIO line to use for which purpose: cal,perout,extts1,...,extts6");
169 #define BROADCAST_ADDR 31
182 if (dp83640->
clock->page != page) {
192 static void ext_write(
int broadcast,
struct phy_device *phydev,
193 int page,
u32 regnum,
u16 val)
197 if (dp83640->
clock->page != page) {
202 broadcast_write(phydev->
bus, regnum, val);
231 rxts->
ns |= (p->
ns_hi & 0x3fff) << 16;
232 rxts->
ns += ((
u64)sec) * 1000000000ULL;
248 ns |= (p->
ns_hi & 0x3fff) << 16;
249 ns += ((
u64)sec) * 1000000000ULL;
282 sec = clkreq->
perout.start.sec;
283 nsec = clkreq->
perout.start.nsec;
284 period = clkreq->
perout.period.sec * 1000000000
UL;
285 period += clkreq->
perout.period.nsec;
326 rate = div_u64(rate, 1953125);
383 ts->
tv_nsec = val[0] | (val[1] << 16);
384 ts->
tv_sec = val[2] | (val[3] << 16);
416 case PTP_CLK_REQ_EXTTS:
417 index = rq->
extts.index;
430 case PTP_CLK_REQ_PEROUT:
431 if (rq->
perout.index != 0)
433 periodic_output(clock, rq, on);
443 static u8 status_frame_dst[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 };
444 static u8 status_frame_src[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F };
446 static void enable_status_frames(
struct phy_device *phydev,
bool on)
459 pr_warn(
"expected to find an attached netdevice\n");
465 pr_warn(
"failed to add mc address\n");
468 pr_warn(
"failed to delete mc address\n");
474 struct ethhdr *
h = eth_hdr(skb);
483 static int expired(
struct rxts *
rxts)
497 list_del_init(&rxts->
list);
505 static void enable_broadcast(
struct phy_device *phydev,
int init_page,
int on)
538 enable_broadcast(tmp->
phydev, clock->
page, 1);
543 enable_broadcast(master, clock->
page, 1);
588 pr_info(
"master PTP_STS 0x%04hx\n", val);
590 pr_info(
"master PTP_ESTS 0x%04hx\n", val);
595 now = phy2txts(&event_ts);
600 pr_info(
"slave PTP_STS 0x%04hx\n", val);
602 pr_info(
"slave PTP_ESTS 0x%04hx\n", val);
607 diff = now - (
s64) phy2txts(&event_ts);
608 pr_info(
"slave offset %lld nanoseconds\n", diff);
628 static inline u16 exts_chan_to_edata(
int ch)
643 ext_status = *(
u16 *) data;
644 data +=
sizeof(ext_status);
665 ext_status = exts_chan_to_edata(i);
669 event.timestamp = phy2txts(&dp83640->
edata);
672 if (ext_status & exts_chan_to_edata(i)) {
678 return parsed *
sizeof(
u16);
689 prune_rx_ts(dp83640);
691 if (list_empty(&dp83640->
rxpool)) {
692 pr_debug(
"rx timestamp pool is empty\n");
696 list_del_init(&rxts->
list);
697 phy2rxts(phy_rxts, rxts);
700 spin_unlock_irqrestore(&dp83640->
rx_lock, flags);
704 struct phy_txts *phy_txts)
715 pr_debug(
"have timestamp but tx_queue empty\n");
718 ns = phy2txts(phy_txts);
719 memset(&shhwtstamps, 0,
sizeof(shhwtstamps));
720 shhwtstamps.hwtstamp = ns_to_ktime(ns);
727 struct phy_rxts *phy_rxts;
728 struct phy_txts *phy_txts;
735 for (len = skb_headlen(skb) - 2; len >
sizeof(
type); len -=
size) {
738 ests = type & 0x0fff;
739 type = type & 0xf000;
743 if (
PSF_RX == type && len >=
sizeof(*phy_rxts)) {
745 phy_rxts = (
struct phy_rxts *) ptr;
746 decode_rxts(dp83640, phy_rxts);
747 size =
sizeof(*phy_rxts);
749 }
else if (
PSF_TX == type && len >=
sizeof(*phy_txts)) {
751 phy_txts = (
struct phy_txts *) ptr;
752 decode_txts(dp83640, phy_txts);
753 size =
sizeof(*phy_txts);
755 }
else if (
PSF_EVNT == type && len >=
sizeof(*phy_txts)) {
757 size = decode_evnt(dp83640, ptr, ests);
767 static int is_sync(
struct sk_buff *skb,
int type)
794 if (skb->
len < offset + 1)
802 static int match(
struct sk_buff *skb,
unsigned int type,
struct rxts *rxts)
806 u8 *
msgtype, *data = skb_mac_header(skb);
839 return (rxts->
msgtype == (*msgtype & 0xf) &&
843 static void dp83640_free_clocks(
void)
852 if (!list_empty(&clock->
phylist)) {
853 pr_warn(
"phy list non-empty while unloading\n");
868 INIT_LIST_HEAD(&clock->
list);
872 INIT_LIST_HEAD(&clock->
phylist);
875 clock->
caps.max_adj = 1953124;
876 clock->
caps.n_alarm = 0;
878 clock->
caps.n_per_out = 1;
880 clock->
caps.adjfreq = ptp_dp83640_adjfreq;
881 clock->
caps.adjtime = ptp_dp83640_adjtime;
882 clock->
caps.gettime = ptp_dp83640_gettime;
883 clock->
caps.settime = ptp_dp83640_settime;
884 clock->
caps.enable = ptp_dp83640_enable;
894 if (chosen_phy == -1 && !clock->
chosen)
897 if (chosen_phy == phydev->
addr)
923 if (tmp->bus == bus) {
935 dp83640_clock_init(clock, bus);
940 return dp83640_clock_get(clock);
948 static int dp83640_probe(
struct phy_device *phydev)
957 clock = dp83640_clock_get_bus(phydev->
bus);
968 INIT_LIST_HEAD(&dp83640->
rxts);
969 INIT_LIST_HEAD(&dp83640->
rxpool);
973 phydev->
priv = dp83640;
976 skb_queue_head_init(&dp83640->
rx_queue);
977 skb_queue_head_init(&dp83640->
tx_queue);
981 if (choose_this_phy(clock, phydev)) {
994 enable_broadcast(dp83640->
phydev, clock->
page, 1);
996 dp83640_clock_put(clock);
1003 dp83640_clock_put(clock);
1008 static void dp83640_remove(
struct phy_device *phydev)
1018 enable_status_frames(phydev,
false);
1027 clock = dp83640_clock_get(dp83640->
clock);
1029 if (dp83640 == clock->
chosen) {
1035 if (tmp == dp83640) {
1036 list_del_init(&tmp->
list);
1042 dp83640_clock_put(clock);
1046 static int dp83640_hwtstamp(
struct phy_device *phydev,
struct ifreq *ifr)
1063 switch (
cfg.rx_filter) {
1125 enable_status_frames(phydev,
true);
1146 unsigned long flags;
1155 if (
match(skb, type, rxts)) {
1156 shhwtstamps = skb_hwtstamps(skb);
1157 memset(shhwtstamps, 0,
sizeof(*shhwtstamps));
1158 shhwtstamps->
hwtstamp = ns_to_ktime(rxts->
ns);
1159 list_del_init(&rxts->
list);
1164 spin_unlock_irqrestore(&dp83640->
rx_lock, flags);
1171 prune_rx_ts(dp83640);
1172 spin_unlock_irqrestore(&dp83640->
rx_lock, flags);
1175 static bool dp83640_rxtstamp(
struct phy_device *phydev,
1176 struct sk_buff *skb,
int type)
1183 if (is_status_frame(skb, type)) {
1184 decode_status_frame(dp83640, skb);
1196 static void dp83640_txtstamp(
struct phy_device *phydev,
1197 struct sk_buff *skb,
int type)
1204 if (is_sync(skb, type)) {
1253 .phy_id_mask = 0xfffffff0,
1254 .name =
"NatSemi DP83640",
1257 .probe = dp83640_probe,
1258 .remove = dp83640_remove,
1261 .ts_info = dp83640_ts_info,
1262 .hwtstamp = dp83640_hwtstamp,
1263 .rxtstamp = dp83640_rxtstamp,
1264 .txtstamp = dp83640_txtstamp,
1268 static int __init dp83640_init(
void)
1273 static void __exit dp83640_exit(
void)
1275 dp83640_free_clocks();