blob: 352c5e45fe9cc4477286970612a15b72a1e9dd81 [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 Cochran621bdec2014-03-20 22:22:00 +0100301static int periodic_output(struct dp83640_clock *clock,
302 struct ptp_clock_request *clkreq, bool on)
Richard Cochran49b3fd42011-09-20 01:43:14 +0000303{
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
Richard Cochran621bdec2014-03-20 22:22:00 +0100309 if (on) {
310 gpio = 1 + ptp_find_pin(clock->ptp_clock, PTP_PF_PEROUT, 0);
311 if (gpio < 1)
312 return -EINVAL;
313 } else {
314 gpio = 0;
315 }
316
Richard Cochran49b3fd42011-09-20 01:43:14 +0000317 trigger = PER_TRIGGER;
318
319 ptp_trig = TRIG_WR |
320 (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT |
321 (gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT |
322 TRIG_PER |
323 TRIG_PULSE;
324
325 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
326
327 if (!on) {
328 val |= TRIG_DIS;
329 mutex_lock(&clock->extreg_lock);
330 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
331 ext_write(0, phydev, PAGE4, PTP_CTL, val);
332 mutex_unlock(&clock->extreg_lock);
Richard Cochran621bdec2014-03-20 22:22:00 +0100333 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000334 }
335
336 sec = clkreq->perout.start.sec;
337 nsec = clkreq->perout.start.nsec;
Richard Cochran564ca562014-03-20 22:21:57 +0100338 pwidth = clkreq->perout.period.sec * 1000000000UL;
339 pwidth += clkreq->perout.period.nsec;
340 pwidth /= 2;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000341
342 mutex_lock(&clock->extreg_lock);
343
344 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
345
346 /*load trigger*/
347 val |= TRIG_LOAD;
348 ext_write(0, phydev, PAGE4, PTP_CTL, val);
349 ext_write(0, phydev, PAGE4, PTP_TDR, nsec & 0xffff); /* ns[15:0] */
350 ext_write(0, phydev, PAGE4, PTP_TDR, nsec >> 16); /* ns[31:16] */
351 ext_write(0, phydev, PAGE4, PTP_TDR, sec & 0xffff); /* sec[15:0] */
352 ext_write(0, phydev, PAGE4, PTP_TDR, sec >> 16); /* sec[31:16] */
Richard Cochran564ca562014-03-20 22:21:57 +0100353 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff); /* ns[15:0] */
354 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16); /* ns[31:16] */
Richard Cochran49b3fd42011-09-20 01:43:14 +0000355
356 /*enable trigger*/
357 val &= ~TRIG_LOAD;
358 val |= TRIG_EN;
359 ext_write(0, phydev, PAGE4, PTP_CTL, val);
360
361 mutex_unlock(&clock->extreg_lock);
Richard Cochran621bdec2014-03-20 22:22:00 +0100362 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000363}
364
Richard Cochrancb646e22011-04-22 12:04:55 +0200365/* ptp clock methods */
366
367static int ptp_dp83640_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
368{
369 struct dp83640_clock *clock =
370 container_of(ptp, struct dp83640_clock, caps);
371 struct phy_device *phydev = clock->chosen->phydev;
372 u64 rate;
373 int neg_adj = 0;
374 u16 hi, lo;
375
376 if (ppb < 0) {
377 neg_adj = 1;
378 ppb = -ppb;
379 }
380 rate = ppb;
381 rate <<= 26;
382 rate = div_u64(rate, 1953125);
383
384 hi = (rate >> 16) & PTP_RATE_HI_MASK;
385 if (neg_adj)
386 hi |= PTP_RATE_DIR;
387
388 lo = rate & 0xffff;
389
390 mutex_lock(&clock->extreg_lock);
391
392 ext_write(1, phydev, PAGE4, PTP_RATEH, hi);
393 ext_write(1, phydev, PAGE4, PTP_RATEL, lo);
394
395 mutex_unlock(&clock->extreg_lock);
396
397 return 0;
398}
399
400static int ptp_dp83640_adjtime(struct ptp_clock_info *ptp, s64 delta)
401{
402 struct dp83640_clock *clock =
403 container_of(ptp, struct dp83640_clock, caps);
404 struct phy_device *phydev = clock->chosen->phydev;
405 struct timespec ts;
406 int err;
407
408 delta += ADJTIME_FIX;
409
410 ts = ns_to_timespec(delta);
411
412 mutex_lock(&clock->extreg_lock);
413
414 err = tdr_write(1, phydev, &ts, PTP_STEP_CLK);
415
416 mutex_unlock(&clock->extreg_lock);
417
418 return err;
419}
420
421static int ptp_dp83640_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
422{
423 struct dp83640_clock *clock =
424 container_of(ptp, struct dp83640_clock, caps);
425 struct phy_device *phydev = clock->chosen->phydev;
426 unsigned int val[4];
427
428 mutex_lock(&clock->extreg_lock);
429
430 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_RD_CLK);
431
432 val[0] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[15:0] */
433 val[1] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[31:16] */
434 val[2] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[15:0] */
435 val[3] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[31:16] */
436
437 mutex_unlock(&clock->extreg_lock);
438
439 ts->tv_nsec = val[0] | (val[1] << 16);
440 ts->tv_sec = val[2] | (val[3] << 16);
441
442 return 0;
443}
444
445static int ptp_dp83640_settime(struct ptp_clock_info *ptp,
446 const struct timespec *ts)
447{
448 struct dp83640_clock *clock =
449 container_of(ptp, struct dp83640_clock, caps);
450 struct phy_device *phydev = clock->chosen->phydev;
451 int err;
452
453 mutex_lock(&clock->extreg_lock);
454
455 err = tdr_write(1, phydev, ts, PTP_LOAD_CLK);
456
457 mutex_unlock(&clock->extreg_lock);
458
459 return err;
460}
461
462static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
463 struct ptp_clock_request *rq, int on)
464{
465 struct dp83640_clock *clock =
466 container_of(ptp, struct dp83640_clock, caps);
467 struct phy_device *phydev = clock->chosen->phydev;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100468 unsigned int index;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000469 u16 evnt, event_num, gpio_num;
Richard Cochrancb646e22011-04-22 12:04:55 +0200470
471 switch (rq->type) {
472 case PTP_CLK_REQ_EXTTS:
Richard Cochran49b3fd42011-09-20 01:43:14 +0000473 index = rq->extts.index;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100474 if (index >= N_EXT_TS)
Richard Cochrancb646e22011-04-22 12:04:55 +0200475 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000476 event_num = EXT_EVENT + index;
477 evnt = EVNT_WR | (event_num & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
Richard Cochrancb646e22011-04-22 12:04:55 +0200478 if (on) {
Richard Cochranfaa89712014-03-20 22:21:59 +0100479 gpio_num = 1 + ptp_find_pin(clock->ptp_clock,
480 PTP_PF_EXTTS, index);
481 if (gpio_num < 1)
482 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000483 evnt |= (gpio_num & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
Stefan Sørensen80671bd2014-02-03 15:36:50 +0100484 if (rq->extts.flags & PTP_FALLING_EDGE)
485 evnt |= EVNT_FALL;
486 else
487 evnt |= EVNT_RISE;
Richard Cochrancb646e22011-04-22 12:04:55 +0200488 }
489 ext_write(0, phydev, PAGE5, PTP_EVNT, evnt);
490 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000491
492 case PTP_CLK_REQ_PEROUT:
493 if (rq->perout.index != 0)
494 return -EINVAL;
Richard Cochran621bdec2014-03-20 22:22:00 +0100495 return periodic_output(clock, rq, on);
Richard Cochran49b3fd42011-09-20 01:43:14 +0000496
Richard Cochrancb646e22011-04-22 12:04:55 +0200497 default:
498 break;
499 }
500
501 return -EOPNOTSUPP;
502}
503
Richard Cochran86dd3612014-03-20 22:21:58 +0100504static int ptp_dp83640_verify(struct ptp_clock_info *ptp, unsigned int pin,
505 enum ptp_pin_function func, unsigned int chan)
506{
507 return 0;
508}
509
Richard Cochrancb646e22011-04-22 12:04:55 +0200510static u8 status_frame_dst[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 };
511static u8 status_frame_src[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F };
512
513static void enable_status_frames(struct phy_device *phydev, bool on)
514{
515 u16 cfg0 = 0, ver;
516
517 if (on)
518 cfg0 = PSF_EVNT_EN | PSF_RXTS_EN | PSF_TXTS_EN | ENDIAN_FLAG;
519
520 ver = (PSF_PTPVER & VERSIONPTP_MASK) << VERSIONPTP_SHIFT;
521
522 ext_write(0, phydev, PAGE5, PSF_CFG0, cfg0);
523 ext_write(0, phydev, PAGE6, PSF_CFG1, ver);
524
525 if (!phydev->attached_dev) {
Joe Perches8d242482012-06-09 07:49:07 +0000526 pr_warn("expected to find an attached netdevice\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200527 return;
528 }
529
530 if (on) {
531 if (dev_mc_add(phydev->attached_dev, status_frame_dst))
Joe Perches8d242482012-06-09 07:49:07 +0000532 pr_warn("failed to add mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200533 } else {
534 if (dev_mc_del(phydev->attached_dev, status_frame_dst))
Joe Perches8d242482012-06-09 07:49:07 +0000535 pr_warn("failed to delete mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200536 }
537}
538
539static bool is_status_frame(struct sk_buff *skb, int type)
540{
541 struct ethhdr *h = eth_hdr(skb);
542
543 if (PTP_CLASS_V2_L2 == type &&
544 !memcmp(h->h_source, status_frame_src, sizeof(status_frame_src)))
545 return true;
546 else
547 return false;
548}
549
550static int expired(struct rxts *rxts)
551{
552 return time_after(jiffies, rxts->tmo);
553}
554
555/* Caller must hold rx_lock. */
556static void prune_rx_ts(struct dp83640_private *dp83640)
557{
558 struct list_head *this, *next;
559 struct rxts *rxts;
560
561 list_for_each_safe(this, next, &dp83640->rxts) {
562 rxts = list_entry(this, struct rxts, list);
563 if (expired(rxts)) {
564 list_del_init(&rxts->list);
565 list_add(&rxts->list, &dp83640->rxpool);
566 }
567 }
568}
569
570/* synchronize the phyters so they act as one clock */
571
572static void enable_broadcast(struct phy_device *phydev, int init_page, int on)
573{
574 int val;
575 phy_write(phydev, PAGESEL, 0);
576 val = phy_read(phydev, PHYCR2);
577 if (on)
578 val |= BC_WRITE;
579 else
580 val &= ~BC_WRITE;
581 phy_write(phydev, PHYCR2, val);
582 phy_write(phydev, PAGESEL, init_page);
583}
584
585static void recalibrate(struct dp83640_clock *clock)
586{
587 s64 now, diff;
588 struct phy_txts event_ts;
589 struct timespec ts;
590 struct list_head *this;
591 struct dp83640_private *tmp;
592 struct phy_device *master = clock->chosen->phydev;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000593 u16 cal_gpio, cfg0, evnt, ptp_trig, trigger, val;
Richard Cochrancb646e22011-04-22 12:04:55 +0200594
595 trigger = CAL_TRIGGER;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000596 cal_gpio = gpio_tab[CALIBRATE_GPIO];
Richard Cochrancb646e22011-04-22 12:04:55 +0200597
598 mutex_lock(&clock->extreg_lock);
599
600 /*
601 * enable broadcast, disable status frames, enable ptp clock
602 */
603 list_for_each(this, &clock->phylist) {
604 tmp = list_entry(this, struct dp83640_private, list);
605 enable_broadcast(tmp->phydev, clock->page, 1);
606 tmp->cfg0 = ext_read(tmp->phydev, PAGE5, PSF_CFG0);
607 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, 0);
608 ext_write(0, tmp->phydev, PAGE4, PTP_CTL, PTP_ENABLE);
609 }
610 enable_broadcast(master, clock->page, 1);
611 cfg0 = ext_read(master, PAGE5, PSF_CFG0);
612 ext_write(0, master, PAGE5, PSF_CFG0, 0);
613 ext_write(0, master, PAGE4, PTP_CTL, PTP_ENABLE);
614
615 /*
616 * enable an event timestamp
617 */
618 evnt = EVNT_WR | EVNT_RISE | EVNT_SINGLE;
619 evnt |= (CAL_EVENT & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
620 evnt |= (cal_gpio & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
621
622 list_for_each(this, &clock->phylist) {
623 tmp = list_entry(this, struct dp83640_private, list);
624 ext_write(0, tmp->phydev, PAGE5, PTP_EVNT, evnt);
625 }
626 ext_write(0, master, PAGE5, PTP_EVNT, evnt);
627
628 /*
629 * configure a trigger
630 */
631 ptp_trig = TRIG_WR | TRIG_IF_LATE | TRIG_PULSE;
632 ptp_trig |= (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT;
633 ptp_trig |= (cal_gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT;
634 ext_write(0, master, PAGE5, PTP_TRIG, ptp_trig);
635
636 /* load trigger */
637 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
638 val |= TRIG_LOAD;
639 ext_write(0, master, PAGE4, PTP_CTL, val);
640
641 /* enable trigger */
642 val &= ~TRIG_LOAD;
643 val |= TRIG_EN;
644 ext_write(0, master, PAGE4, PTP_CTL, val);
645
646 /* disable trigger */
647 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
648 val |= TRIG_DIS;
649 ext_write(0, master, PAGE4, PTP_CTL, val);
650
651 /*
652 * read out and correct offsets
653 */
654 val = ext_read(master, PAGE4, PTP_STS);
Joe Perches8d242482012-06-09 07:49:07 +0000655 pr_info("master PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200656 val = ext_read(master, PAGE4, PTP_ESTS);
Joe Perches8d242482012-06-09 07:49:07 +0000657 pr_info("master PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200658 event_ts.ns_lo = ext_read(master, PAGE4, PTP_EDATA);
659 event_ts.ns_hi = ext_read(master, PAGE4, PTP_EDATA);
660 event_ts.sec_lo = ext_read(master, PAGE4, PTP_EDATA);
661 event_ts.sec_hi = ext_read(master, PAGE4, PTP_EDATA);
662 now = phy2txts(&event_ts);
663
664 list_for_each(this, &clock->phylist) {
665 tmp = list_entry(this, struct dp83640_private, list);
666 val = ext_read(tmp->phydev, PAGE4, PTP_STS);
Joe Perches8d242482012-06-09 07:49:07 +0000667 pr_info("slave PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200668 val = ext_read(tmp->phydev, PAGE4, PTP_ESTS);
Joe Perches8d242482012-06-09 07:49:07 +0000669 pr_info("slave PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200670 event_ts.ns_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
671 event_ts.ns_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
672 event_ts.sec_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
673 event_ts.sec_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
674 diff = now - (s64) phy2txts(&event_ts);
675 pr_info("slave offset %lld nanoseconds\n", diff);
676 diff += ADJTIME_FIX;
677 ts = ns_to_timespec(diff);
678 tdr_write(0, tmp->phydev, &ts, PTP_STEP_CLK);
679 }
680
681 /*
682 * restore status frames
683 */
684 list_for_each(this, &clock->phylist) {
685 tmp = list_entry(this, struct dp83640_private, list);
686 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, tmp->cfg0);
687 }
688 ext_write(0, master, PAGE5, PSF_CFG0, cfg0);
689
690 mutex_unlock(&clock->extreg_lock);
691}
692
693/* time stamping methods */
694
Richard Cochran49b3fd42011-09-20 01:43:14 +0000695static inline u16 exts_chan_to_edata(int ch)
696{
697 return 1 << ((ch + EXT_EVENT) * 2);
698}
699
Richard Cochran23310382011-06-14 23:55:19 +0000700static int decode_evnt(struct dp83640_private *dp83640,
701 void *data, u16 ests)
Richard Cochrancb646e22011-04-22 12:04:55 +0200702{
Richard Cochran23310382011-06-14 23:55:19 +0000703 struct phy_txts *phy_txts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200704 struct ptp_clock_event event;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000705 int i, parsed;
Richard Cochrancb646e22011-04-22 12:04:55 +0200706 int words = (ests >> EVNT_TS_LEN_SHIFT) & EVNT_TS_LEN_MASK;
Richard Cochran23310382011-06-14 23:55:19 +0000707 u16 ext_status = 0;
708
709 if (ests & MULT_EVNT) {
710 ext_status = *(u16 *) data;
711 data += sizeof(ext_status);
712 }
713
714 phy_txts = data;
Richard Cochrancb646e22011-04-22 12:04:55 +0200715
716 switch (words) { /* fall through in every case */
717 case 3:
718 dp83640->edata.sec_hi = phy_txts->sec_hi;
719 case 2:
720 dp83640->edata.sec_lo = phy_txts->sec_lo;
721 case 1:
722 dp83640->edata.ns_hi = phy_txts->ns_hi;
723 case 0:
724 dp83640->edata.ns_lo = phy_txts->ns_lo;
725 }
726
Richard Cochran49b3fd42011-09-20 01:43:14 +0000727 if (ext_status) {
728 parsed = words + 2;
729 } else {
730 parsed = words + 1;
731 i = ((ests >> EVNT_NUM_SHIFT) & EVNT_NUM_MASK) - EXT_EVENT;
732 ext_status = exts_chan_to_edata(i);
733 }
734
Richard Cochrancb646e22011-04-22 12:04:55 +0200735 event.type = PTP_CLOCK_EXTTS;
Richard Cochrancb646e22011-04-22 12:04:55 +0200736 event.timestamp = phy2txts(&dp83640->edata);
737
Richard Cochran49b3fd42011-09-20 01:43:14 +0000738 for (i = 0; i < N_EXT_TS; i++) {
739 if (ext_status & exts_chan_to_edata(i)) {
740 event.index = i;
741 ptp_clock_event(dp83640->clock->ptp_clock, &event);
742 }
743 }
Richard Cochran23310382011-06-14 23:55:19 +0000744
Richard Cochran49b3fd42011-09-20 01:43:14 +0000745 return parsed * sizeof(u16);
Richard Cochrancb646e22011-04-22 12:04:55 +0200746}
747
748static void decode_rxts(struct dp83640_private *dp83640,
749 struct phy_rxts *phy_rxts)
750{
751 struct rxts *rxts;
752 unsigned long flags;
753
754 spin_lock_irqsave(&dp83640->rx_lock, flags);
755
756 prune_rx_ts(dp83640);
757
758 if (list_empty(&dp83640->rxpool)) {
Joe Perches8d242482012-06-09 07:49:07 +0000759 pr_debug("rx timestamp pool is empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200760 goto out;
761 }
762 rxts = list_first_entry(&dp83640->rxpool, struct rxts, list);
763 list_del_init(&rxts->list);
764 phy2rxts(phy_rxts, rxts);
765 list_add_tail(&rxts->list, &dp83640->rxts);
766out:
767 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
768}
769
770static void decode_txts(struct dp83640_private *dp83640,
771 struct phy_txts *phy_txts)
772{
773 struct skb_shared_hwtstamps shhwtstamps;
774 struct sk_buff *skb;
775 u64 ns;
776
777 /* We must already have the skb that triggered this. */
778
779 skb = skb_dequeue(&dp83640->tx_queue);
780
781 if (!skb) {
Joe Perches8d242482012-06-09 07:49:07 +0000782 pr_debug("have timestamp but tx_queue empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200783 return;
784 }
785 ns = phy2txts(phy_txts);
786 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
787 shhwtstamps.hwtstamp = ns_to_ktime(ns);
788 skb_complete_tx_timestamp(skb, &shhwtstamps);
789}
790
791static void decode_status_frame(struct dp83640_private *dp83640,
792 struct sk_buff *skb)
793{
794 struct phy_rxts *phy_rxts;
795 struct phy_txts *phy_txts;
796 u8 *ptr;
797 int len, size;
798 u16 ests, type;
799
800 ptr = skb->data + 2;
801
802 for (len = skb_headlen(skb) - 2; len > sizeof(type); len -= size) {
803
804 type = *(u16 *)ptr;
805 ests = type & 0x0fff;
806 type = type & 0xf000;
807 len -= sizeof(type);
808 ptr += sizeof(type);
809
810 if (PSF_RX == type && len >= sizeof(*phy_rxts)) {
811
812 phy_rxts = (struct phy_rxts *) ptr;
813 decode_rxts(dp83640, phy_rxts);
814 size = sizeof(*phy_rxts);
815
816 } else if (PSF_TX == type && len >= sizeof(*phy_txts)) {
817
818 phy_txts = (struct phy_txts *) ptr;
819 decode_txts(dp83640, phy_txts);
820 size = sizeof(*phy_txts);
821
822 } else if (PSF_EVNT == type && len >= sizeof(*phy_txts)) {
823
Richard Cochran23310382011-06-14 23:55:19 +0000824 size = decode_evnt(dp83640, ptr, ests);
Richard Cochrancb646e22011-04-22 12:04:55 +0200825
826 } else {
827 size = 0;
828 break;
829 }
830 ptr += size;
831 }
832}
833
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000834static int is_sync(struct sk_buff *skb, int type)
835{
836 u8 *data = skb->data, *msgtype;
837 unsigned int offset = 0;
838
839 switch (type) {
840 case PTP_CLASS_V1_IPV4:
841 case PTP_CLASS_V2_IPV4:
842 offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
843 break;
844 case PTP_CLASS_V1_IPV6:
845 case PTP_CLASS_V2_IPV6:
846 offset = OFF_PTP6;
847 break;
848 case PTP_CLASS_V2_L2:
849 offset = ETH_HLEN;
850 break;
851 case PTP_CLASS_V2_VLAN:
852 offset = ETH_HLEN + VLAN_HLEN;
853 break;
854 default:
855 return 0;
856 }
857
858 if (type & PTP_CLASS_V1)
859 offset += OFF_PTP_CONTROL;
860
861 if (skb->len < offset + 1)
862 return 0;
863
864 msgtype = data + offset;
865
866 return (*msgtype & 0xf) == 0;
867}
868
Richard Cochrancb646e22011-04-22 12:04:55 +0200869static int match(struct sk_buff *skb, unsigned int type, struct rxts *rxts)
870{
871 u16 *seqid;
872 unsigned int offset;
873 u8 *msgtype, *data = skb_mac_header(skb);
874
875 /* check sequenceID, messageType, 12 bit hash of offset 20-29 */
876
877 switch (type) {
878 case PTP_CLASS_V1_IPV4:
879 case PTP_CLASS_V2_IPV4:
880 offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
881 break;
882 case PTP_CLASS_V1_IPV6:
883 case PTP_CLASS_V2_IPV6:
884 offset = OFF_PTP6;
885 break;
886 case PTP_CLASS_V2_L2:
887 offset = ETH_HLEN;
888 break;
889 case PTP_CLASS_V2_VLAN:
890 offset = ETH_HLEN + VLAN_HLEN;
891 break;
892 default:
893 return 0;
894 }
895
896 if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
897 return 0;
898
899 if (unlikely(type & PTP_CLASS_V1))
900 msgtype = data + offset + OFF_PTP_CONTROL;
901 else
902 msgtype = data + offset;
903
904 seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
905
Florian Fainellidd61d962013-12-17 21:38:07 -0800906 return rxts->msgtype == (*msgtype & 0xf) &&
907 rxts->seqid == ntohs(*seqid);
Richard Cochrancb646e22011-04-22 12:04:55 +0200908}
909
910static void dp83640_free_clocks(void)
911{
912 struct dp83640_clock *clock;
913 struct list_head *this, *next;
914
915 mutex_lock(&phyter_clocks_lock);
916
917 list_for_each_safe(this, next, &phyter_clocks) {
918 clock = list_entry(this, struct dp83640_clock, list);
919 if (!list_empty(&clock->phylist)) {
Joe Perches8d242482012-06-09 07:49:07 +0000920 pr_warn("phy list non-empty while unloading\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200921 BUG();
922 }
923 list_del(&clock->list);
924 mutex_destroy(&clock->extreg_lock);
925 mutex_destroy(&clock->clock_lock);
926 put_device(&clock->bus->dev);
Richard Cochran86dd3612014-03-20 22:21:58 +0100927 kfree(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +0200928 kfree(clock);
929 }
930
931 mutex_unlock(&phyter_clocks_lock);
932}
933
934static void dp83640_clock_init(struct dp83640_clock *clock, struct mii_bus *bus)
935{
936 INIT_LIST_HEAD(&clock->list);
937 clock->bus = bus;
938 mutex_init(&clock->extreg_lock);
939 mutex_init(&clock->clock_lock);
940 INIT_LIST_HEAD(&clock->phylist);
941 clock->caps.owner = THIS_MODULE;
942 sprintf(clock->caps.name, "dp83640 timer");
943 clock->caps.max_adj = 1953124;
944 clock->caps.n_alarm = 0;
945 clock->caps.n_ext_ts = N_EXT_TS;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000946 clock->caps.n_per_out = 1;
Richard Cochran86dd3612014-03-20 22:21:58 +0100947 clock->caps.n_pins = DP83640_N_PINS;
Richard Cochrancb646e22011-04-22 12:04:55 +0200948 clock->caps.pps = 0;
949 clock->caps.adjfreq = ptp_dp83640_adjfreq;
950 clock->caps.adjtime = ptp_dp83640_adjtime;
951 clock->caps.gettime = ptp_dp83640_gettime;
952 clock->caps.settime = ptp_dp83640_settime;
953 clock->caps.enable = ptp_dp83640_enable;
Richard Cochran86dd3612014-03-20 22:21:58 +0100954 clock->caps.verify = ptp_dp83640_verify;
955 /*
956 * Convert the module param defaults into a dynamic pin configuration.
957 */
958 dp83640_gpio_defaults(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +0200959 /*
960 * Get a reference to this bus instance.
961 */
962 get_device(&bus->dev);
963}
964
965static int choose_this_phy(struct dp83640_clock *clock,
966 struct phy_device *phydev)
967{
968 if (chosen_phy == -1 && !clock->chosen)
969 return 1;
970
971 if (chosen_phy == phydev->addr)
972 return 1;
973
974 return 0;
975}
976
977static struct dp83640_clock *dp83640_clock_get(struct dp83640_clock *clock)
978{
979 if (clock)
980 mutex_lock(&clock->clock_lock);
981 return clock;
982}
983
984/*
985 * Look up and lock a clock by bus instance.
986 * If there is no clock for this bus, then create it first.
987 */
988static struct dp83640_clock *dp83640_clock_get_bus(struct mii_bus *bus)
989{
990 struct dp83640_clock *clock = NULL, *tmp;
991 struct list_head *this;
992
993 mutex_lock(&phyter_clocks_lock);
994
995 list_for_each(this, &phyter_clocks) {
996 tmp = list_entry(this, struct dp83640_clock, list);
997 if (tmp->bus == bus) {
998 clock = tmp;
999 break;
1000 }
1001 }
1002 if (clock)
1003 goto out;
1004
1005 clock = kzalloc(sizeof(struct dp83640_clock), GFP_KERNEL);
1006 if (!clock)
1007 goto out;
1008
Richard Cochran86dd3612014-03-20 22:21:58 +01001009 clock->caps.pin_config = kzalloc(sizeof(struct ptp_pin_desc) *
1010 DP83640_N_PINS, GFP_KERNEL);
1011 if (!clock->caps.pin_config) {
1012 kfree(clock);
1013 clock = NULL;
1014 goto out;
1015 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001016 dp83640_clock_init(clock, bus);
1017 list_add_tail(&phyter_clocks, &clock->list);
1018out:
1019 mutex_unlock(&phyter_clocks_lock);
1020
1021 return dp83640_clock_get(clock);
1022}
1023
1024static void dp83640_clock_put(struct dp83640_clock *clock)
1025{
1026 mutex_unlock(&clock->clock_lock);
1027}
1028
1029static int dp83640_probe(struct phy_device *phydev)
1030{
1031 struct dp83640_clock *clock;
1032 struct dp83640_private *dp83640;
1033 int err = -ENOMEM, i;
1034
1035 if (phydev->addr == BROADCAST_ADDR)
1036 return 0;
1037
1038 clock = dp83640_clock_get_bus(phydev->bus);
1039 if (!clock)
1040 goto no_clock;
1041
1042 dp83640 = kzalloc(sizeof(struct dp83640_private), GFP_KERNEL);
1043 if (!dp83640)
1044 goto no_memory;
1045
1046 dp83640->phydev = phydev;
1047 INIT_WORK(&dp83640->ts_work, rx_timestamp_work);
1048
1049 INIT_LIST_HEAD(&dp83640->rxts);
1050 INIT_LIST_HEAD(&dp83640->rxpool);
1051 for (i = 0; i < MAX_RXTS; i++)
1052 list_add(&dp83640->rx_pool_data[i].list, &dp83640->rxpool);
1053
1054 phydev->priv = dp83640;
1055
1056 spin_lock_init(&dp83640->rx_lock);
1057 skb_queue_head_init(&dp83640->rx_queue);
1058 skb_queue_head_init(&dp83640->tx_queue);
1059
1060 dp83640->clock = clock;
1061
1062 if (choose_this_phy(clock, phydev)) {
1063 clock->chosen = dp83640;
Richard Cochran1ef76152012-09-22 07:02:03 +00001064 clock->ptp_clock = ptp_clock_register(&clock->caps, &phydev->dev);
Richard Cochrancb646e22011-04-22 12:04:55 +02001065 if (IS_ERR(clock->ptp_clock)) {
1066 err = PTR_ERR(clock->ptp_clock);
1067 goto no_register;
1068 }
1069 } else
1070 list_add_tail(&dp83640->list, &clock->phylist);
1071
Richard Cochrancb646e22011-04-22 12:04:55 +02001072 dp83640_clock_put(clock);
1073 return 0;
1074
1075no_register:
1076 clock->chosen = NULL;
1077 kfree(dp83640);
1078no_memory:
1079 dp83640_clock_put(clock);
1080no_clock:
1081 return err;
1082}
1083
1084static void dp83640_remove(struct phy_device *phydev)
1085{
1086 struct dp83640_clock *clock;
1087 struct list_head *this, *next;
1088 struct dp83640_private *tmp, *dp83640 = phydev->priv;
Richard Cochran8b3408f2011-10-21 00:49:17 +00001089 struct sk_buff *skb;
Richard Cochrancb646e22011-04-22 12:04:55 +02001090
1091 if (phydev->addr == BROADCAST_ADDR)
1092 return;
1093
1094 enable_status_frames(phydev, false);
1095 cancel_work_sync(&dp83640->ts_work);
1096
Richard Cochran8b3408f2011-10-21 00:49:17 +00001097 while ((skb = skb_dequeue(&dp83640->rx_queue)) != NULL)
1098 kfree_skb(skb);
1099
1100 while ((skb = skb_dequeue(&dp83640->tx_queue)) != NULL)
1101 skb_complete_tx_timestamp(skb, NULL);
1102
Richard Cochrancb646e22011-04-22 12:04:55 +02001103 clock = dp83640_clock_get(dp83640->clock);
1104
1105 if (dp83640 == clock->chosen) {
1106 ptp_clock_unregister(clock->ptp_clock);
1107 clock->chosen = NULL;
1108 } else {
1109 list_for_each_safe(this, next, &clock->phylist) {
1110 tmp = list_entry(this, struct dp83640_private, list);
1111 if (tmp == dp83640) {
1112 list_del_init(&tmp->list);
1113 break;
1114 }
1115 }
1116 }
1117
1118 dp83640_clock_put(clock);
1119 kfree(dp83640);
1120}
1121
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001122static int dp83640_config_init(struct phy_device *phydev)
1123{
Stefan Sørensen602b1092014-02-13 15:26:57 +01001124 struct dp83640_private *dp83640 = phydev->priv;
1125 struct dp83640_clock *clock = dp83640->clock;
1126
1127 if (clock->chosen && !list_empty(&clock->phylist))
1128 recalibrate(clock);
1129 else
1130 enable_broadcast(phydev, clock->page, 1);
1131
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001132 enable_status_frames(phydev, true);
1133 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_ENABLE);
1134 return 0;
1135}
1136
Stephan Gatzka16421822012-12-04 10:21:38 +00001137static int dp83640_ack_interrupt(struct phy_device *phydev)
1138{
1139 int err = phy_read(phydev, MII_DP83640_MISR);
1140
1141 if (err < 0)
1142 return err;
1143
1144 return 0;
1145}
1146
1147static int dp83640_config_intr(struct phy_device *phydev)
1148{
1149 int micr;
1150 int misr;
1151 int err;
1152
1153 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1154 misr = phy_read(phydev, MII_DP83640_MISR);
1155 if (misr < 0)
1156 return misr;
1157 misr |=
1158 (MII_DP83640_MISR_ANC_INT_EN |
1159 MII_DP83640_MISR_DUP_INT_EN |
1160 MII_DP83640_MISR_SPD_INT_EN |
1161 MII_DP83640_MISR_LINK_INT_EN);
1162 err = phy_write(phydev, MII_DP83640_MISR, misr);
1163 if (err < 0)
1164 return err;
1165
1166 micr = phy_read(phydev, MII_DP83640_MICR);
1167 if (micr < 0)
1168 return micr;
1169 micr |=
1170 (MII_DP83640_MICR_OE |
1171 MII_DP83640_MICR_IE);
1172 return phy_write(phydev, MII_DP83640_MICR, micr);
1173 } else {
1174 micr = phy_read(phydev, MII_DP83640_MICR);
1175 if (micr < 0)
1176 return micr;
1177 micr &=
1178 ~(MII_DP83640_MICR_OE |
1179 MII_DP83640_MICR_IE);
1180 err = phy_write(phydev, MII_DP83640_MICR, micr);
1181 if (err < 0)
1182 return err;
1183
1184 misr = phy_read(phydev, MII_DP83640_MISR);
1185 if (misr < 0)
1186 return misr;
1187 misr &=
1188 ~(MII_DP83640_MISR_ANC_INT_EN |
1189 MII_DP83640_MISR_DUP_INT_EN |
1190 MII_DP83640_MISR_SPD_INT_EN |
1191 MII_DP83640_MISR_LINK_INT_EN);
1192 return phy_write(phydev, MII_DP83640_MISR, misr);
1193 }
1194}
1195
Richard Cochrancb646e22011-04-22 12:04:55 +02001196static int dp83640_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
1197{
1198 struct dp83640_private *dp83640 = phydev->priv;
1199 struct hwtstamp_config cfg;
1200 u16 txcfg0, rxcfg0;
1201
1202 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1203 return -EFAULT;
1204
1205 if (cfg.flags) /* reserved for future extensions */
1206 return -EINVAL;
1207
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001208 if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ONESTEP_SYNC)
Richard Cochrancb646e22011-04-22 12:04:55 +02001209 return -ERANGE;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001210
1211 dp83640->hwts_tx_en = cfg.tx_type;
Richard Cochrancb646e22011-04-22 12:04:55 +02001212
1213 switch (cfg.rx_filter) {
1214 case HWTSTAMP_FILTER_NONE:
1215 dp83640->hwts_rx_en = 0;
1216 dp83640->layer = 0;
1217 dp83640->version = 0;
1218 break;
1219 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1220 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1221 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1222 dp83640->hwts_rx_en = 1;
1223 dp83640->layer = LAYER4;
1224 dp83640->version = 1;
1225 break;
1226 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1227 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1228 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1229 dp83640->hwts_rx_en = 1;
1230 dp83640->layer = LAYER4;
1231 dp83640->version = 2;
1232 break;
1233 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1234 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1235 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1236 dp83640->hwts_rx_en = 1;
1237 dp83640->layer = LAYER2;
1238 dp83640->version = 2;
1239 break;
1240 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1241 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1242 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1243 dp83640->hwts_rx_en = 1;
1244 dp83640->layer = LAYER4|LAYER2;
1245 dp83640->version = 2;
1246 break;
1247 default:
1248 return -ERANGE;
1249 }
1250
1251 txcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1252 rxcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1253
1254 if (dp83640->layer & LAYER2) {
1255 txcfg0 |= TX_L2_EN;
1256 rxcfg0 |= RX_L2_EN;
1257 }
1258 if (dp83640->layer & LAYER4) {
1259 txcfg0 |= TX_IPV6_EN | TX_IPV4_EN;
1260 rxcfg0 |= RX_IPV6_EN | RX_IPV4_EN;
1261 }
1262
1263 if (dp83640->hwts_tx_en)
1264 txcfg0 |= TX_TS_EN;
1265
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001266 if (dp83640->hwts_tx_en == HWTSTAMP_TX_ONESTEP_SYNC)
1267 txcfg0 |= SYNC_1STEP | CHK_1STEP;
1268
Richard Cochrancb646e22011-04-22 12:04:55 +02001269 if (dp83640->hwts_rx_en)
1270 rxcfg0 |= RX_TS_EN;
1271
1272 mutex_lock(&dp83640->clock->extreg_lock);
1273
Richard Cochrancb646e22011-04-22 12:04:55 +02001274 ext_write(0, phydev, PAGE5, PTP_TXCFG0, txcfg0);
1275 ext_write(0, phydev, PAGE5, PTP_RXCFG0, rxcfg0);
1276
1277 mutex_unlock(&dp83640->clock->extreg_lock);
1278
1279 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1280}
1281
1282static void rx_timestamp_work(struct work_struct *work)
1283{
1284 struct dp83640_private *dp83640 =
1285 container_of(work, struct dp83640_private, ts_work);
1286 struct list_head *this, *next;
1287 struct rxts *rxts;
1288 struct skb_shared_hwtstamps *shhwtstamps;
1289 struct sk_buff *skb;
1290 unsigned int type;
1291 unsigned long flags;
1292
1293 /* Deliver each deferred packet, with or without a time stamp. */
1294
1295 while ((skb = skb_dequeue(&dp83640->rx_queue)) != NULL) {
1296 type = SKB_PTP_TYPE(skb);
1297 spin_lock_irqsave(&dp83640->rx_lock, flags);
1298 list_for_each_safe(this, next, &dp83640->rxts) {
1299 rxts = list_entry(this, struct rxts, list);
1300 if (match(skb, type, rxts)) {
1301 shhwtstamps = skb_hwtstamps(skb);
1302 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
1303 shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
1304 list_del_init(&rxts->list);
1305 list_add(&rxts->list, &dp83640->rxpool);
1306 break;
1307 }
1308 }
1309 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
Manfred Rudigier72092cc2012-01-09 23:52:15 +00001310 netif_rx_ni(skb);
Richard Cochrancb646e22011-04-22 12:04:55 +02001311 }
1312
1313 /* Clear out expired time stamps. */
1314
1315 spin_lock_irqsave(&dp83640->rx_lock, flags);
1316 prune_rx_ts(dp83640);
1317 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
1318}
1319
1320static bool dp83640_rxtstamp(struct phy_device *phydev,
1321 struct sk_buff *skb, int type)
1322{
1323 struct dp83640_private *dp83640 = phydev->priv;
1324
1325 if (!dp83640->hwts_rx_en)
1326 return false;
1327
1328 if (is_status_frame(skb, type)) {
1329 decode_status_frame(dp83640, skb);
Richard Cochranae6e86b2011-06-14 23:55:20 +00001330 kfree_skb(skb);
1331 return true;
Richard Cochrancb646e22011-04-22 12:04:55 +02001332 }
1333
1334 SKB_PTP_TYPE(skb) = type;
1335 skb_queue_tail(&dp83640->rx_queue, skb);
1336 schedule_work(&dp83640->ts_work);
1337
1338 return true;
1339}
1340
1341static void dp83640_txtstamp(struct phy_device *phydev,
1342 struct sk_buff *skb, int type)
1343{
1344 struct dp83640_private *dp83640 = phydev->priv;
1345
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001346 switch (dp83640->hwts_tx_en) {
1347
1348 case HWTSTAMP_TX_ONESTEP_SYNC:
1349 if (is_sync(skb, type)) {
Richard Cochranf5ff7cd2011-10-21 00:49:16 +00001350 skb_complete_tx_timestamp(skb, NULL);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001351 return;
1352 }
1353 /* fall through */
1354 case HWTSTAMP_TX_ON:
Stefan Sørensene2e2f512014-02-03 15:36:35 +01001355 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001356 skb_queue_tail(&dp83640->tx_queue, skb);
1357 schedule_work(&dp83640->ts_work);
1358 break;
1359
1360 case HWTSTAMP_TX_OFF:
1361 default:
Richard Cochranf5ff7cd2011-10-21 00:49:16 +00001362 skb_complete_tx_timestamp(skb, NULL);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001363 break;
Richard Cochrancb646e22011-04-22 12:04:55 +02001364 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001365}
1366
Richard Cochran7dff3492012-04-03 22:59:18 +00001367static int dp83640_ts_info(struct phy_device *dev, struct ethtool_ts_info *info)
1368{
1369 struct dp83640_private *dp83640 = dev->priv;
1370
1371 info->so_timestamping =
1372 SOF_TIMESTAMPING_TX_HARDWARE |
1373 SOF_TIMESTAMPING_RX_HARDWARE |
1374 SOF_TIMESTAMPING_RAW_HARDWARE;
1375 info->phc_index = ptp_clock_index(dp83640->clock->ptp_clock);
1376 info->tx_types =
1377 (1 << HWTSTAMP_TX_OFF) |
1378 (1 << HWTSTAMP_TX_ON) |
1379 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
1380 info->rx_filters =
1381 (1 << HWTSTAMP_FILTER_NONE) |
1382 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
1383 (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
1384 (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
1385 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
1386 (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
1387 (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
1388 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1389 (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
1390 (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
1391 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
1392 (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
1393 (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ);
1394 return 0;
1395}
1396
Richard Cochrancb646e22011-04-22 12:04:55 +02001397static struct phy_driver dp83640_driver = {
1398 .phy_id = DP83640_PHY_ID,
1399 .phy_id_mask = 0xfffffff0,
1400 .name = "NatSemi DP83640",
1401 .features = PHY_BASIC_FEATURES,
Stephan Gatzka16421822012-12-04 10:21:38 +00001402 .flags = PHY_HAS_INTERRUPT,
Richard Cochrancb646e22011-04-22 12:04:55 +02001403 .probe = dp83640_probe,
1404 .remove = dp83640_remove,
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001405 .config_init = dp83640_config_init,
Richard Cochrancb646e22011-04-22 12:04:55 +02001406 .config_aneg = genphy_config_aneg,
1407 .read_status = genphy_read_status,
Stephan Gatzka16421822012-12-04 10:21:38 +00001408 .ack_interrupt = dp83640_ack_interrupt,
1409 .config_intr = dp83640_config_intr,
Richard Cochran7dff3492012-04-03 22:59:18 +00001410 .ts_info = dp83640_ts_info,
Richard Cochrancb646e22011-04-22 12:04:55 +02001411 .hwtstamp = dp83640_hwtstamp,
1412 .rxtstamp = dp83640_rxtstamp,
1413 .txtstamp = dp83640_txtstamp,
1414 .driver = {.owner = THIS_MODULE,}
1415};
1416
1417static int __init dp83640_init(void)
1418{
1419 return phy_driver_register(&dp83640_driver);
1420}
1421
1422static void __exit dp83640_exit(void)
1423{
1424 dp83640_free_clocks();
1425 phy_driver_unregister(&dp83640_driver);
1426}
1427
1428MODULE_DESCRIPTION("National Semiconductor DP83640 PHY driver");
Richard Cochranfbf4b932014-03-20 22:21:56 +01001429MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
Richard Cochrancb646e22011-04-22 12:04:55 +02001430MODULE_LICENSE("GPL");
1431
1432module_init(dp83640_init);
1433module_exit(dp83640_exit);
1434
John Stultz86ff9baa2011-05-23 13:32:11 -07001435static struct mdio_device_id __maybe_unused dp83640_tbl[] = {
Richard Cochrancb646e22011-04-22 12:04:55 +02001436 { DP83640_PHY_ID, 0xfffffff0 },
1437 { }
1438};
1439
1440MODULE_DEVICE_TABLE(mdio, dp83640_tbl);