blob: eabecff9b740ac30797f02d35c98b7588133d55e [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>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020030#include <linux/if_vlan.h>
Richard Cochrancb646e22011-04-22 12:04:55 +020031#include <linux/phy.h>
32#include <linux/ptp_classify.h>
33#include <linux/ptp_clock_kernel.h>
34
35#include "dp83640_reg.h"
36
37#define DP83640_PHY_ID 0x20005ce1
38#define PAGESEL 0x13
39#define LAYER4 0x02
40#define LAYER2 0x01
Richard Cochran80288372011-08-06 21:03:04 +000041#define MAX_RXTS 64
Richard Cochran49b3fd42011-09-20 01:43:14 +000042#define N_EXT_TS 6
Stefan Sørensenad015772014-06-27 12:05:30 +020043#define N_PER_OUT 7
Richard Cochrancb646e22011-04-22 12:04:55 +020044#define PSF_PTPVER 2
45#define PSF_EVNT 0x4000
46#define PSF_RX 0x2000
47#define PSF_TX 0x1000
48#define EXT_EVENT 1
Richard Cochran49b3fd42011-09-20 01:43:14 +000049#define CAL_EVENT 7
50#define CAL_TRIGGER 7
Richard Cochran86dd3612014-03-20 22:21:58 +010051#define DP83640_N_PINS 12
Richard Cochrancb646e22011-04-22 12:04:55 +020052
Stephan Gatzka16421822012-12-04 10:21:38 +000053#define MII_DP83640_MICR 0x11
54#define MII_DP83640_MISR 0x12
55
56#define MII_DP83640_MICR_OE 0x1
57#define MII_DP83640_MICR_IE 0x2
58
59#define MII_DP83640_MISR_RHF_INT_EN 0x01
60#define MII_DP83640_MISR_FHF_INT_EN 0x02
61#define MII_DP83640_MISR_ANC_INT_EN 0x04
62#define MII_DP83640_MISR_DUP_INT_EN 0x08
63#define MII_DP83640_MISR_SPD_INT_EN 0x10
64#define MII_DP83640_MISR_LINK_INT_EN 0x20
65#define MII_DP83640_MISR_ED_INT_EN 0x40
66#define MII_DP83640_MISR_LQ_INT_EN 0x80
67
Richard Cochrancb646e22011-04-22 12:04:55 +020068/* phyter seems to miss the mark by 16 ns */
69#define ADJTIME_FIX 16
70
71#if defined(__BIG_ENDIAN)
72#define ENDIAN_FLAG 0
73#elif defined(__LITTLE_ENDIAN)
74#define ENDIAN_FLAG PSF_ENDIAN
75#endif
76
77#define SKB_PTP_TYPE(__skb) (*(unsigned int *)((__skb)->cb))
78
79struct phy_rxts {
80 u16 ns_lo; /* ns[15:0] */
81 u16 ns_hi; /* overflow[1:0], ns[29:16] */
82 u16 sec_lo; /* sec[15:0] */
83 u16 sec_hi; /* sec[31:16] */
84 u16 seqid; /* sequenceId[15:0] */
85 u16 msgtype; /* messageType[3:0], hash[11:0] */
86};
87
88struct phy_txts {
89 u16 ns_lo; /* ns[15:0] */
90 u16 ns_hi; /* overflow[1:0], ns[29:16] */
91 u16 sec_lo; /* sec[15:0] */
92 u16 sec_hi; /* sec[31:16] */
93};
94
95struct rxts {
96 struct list_head list;
97 unsigned long tmo;
98 u64 ns;
99 u16 seqid;
100 u8 msgtype;
101 u16 hash;
102};
103
104struct dp83640_clock;
105
106struct dp83640_private {
107 struct list_head list;
108 struct dp83640_clock *clock;
109 struct phy_device *phydev;
110 struct work_struct ts_work;
111 int hwts_tx_en;
112 int hwts_rx_en;
113 int layer;
114 int version;
115 /* remember state of cfg0 during calibration */
116 int cfg0;
117 /* remember the last event time stamp */
118 struct phy_txts edata;
119 /* list of rx timestamps */
120 struct list_head rxts;
121 struct list_head rxpool;
122 struct rxts rx_pool_data[MAX_RXTS];
123 /* protects above three fields from concurrent access */
124 spinlock_t rx_lock;
125 /* queues of incoming and outgoing packets */
126 struct sk_buff_head rx_queue;
127 struct sk_buff_head tx_queue;
128};
129
130struct dp83640_clock {
131 /* keeps the instance in the 'phyter_clocks' list */
132 struct list_head list;
133 /* we create one clock instance per MII bus */
134 struct mii_bus *bus;
135 /* protects extended registers from concurrent access */
136 struct mutex extreg_lock;
137 /* remembers which page was last selected */
138 int page;
139 /* our advertised capabilities */
140 struct ptp_clock_info caps;
141 /* protects the three fields below from concurrent access */
142 struct mutex clock_lock;
143 /* the one phyter from which we shall read */
144 struct dp83640_private *chosen;
145 /* list of the other attached phyters, not chosen */
146 struct list_head phylist;
147 /* reference to our PTP hardware clock */
148 struct ptp_clock *ptp_clock;
149};
150
151/* globals */
152
Richard Cochran49b3fd42011-09-20 01:43:14 +0000153enum {
154 CALIBRATE_GPIO,
155 PEROUT_GPIO,
156 EXTTS0_GPIO,
157 EXTTS1_GPIO,
158 EXTTS2_GPIO,
159 EXTTS3_GPIO,
160 EXTTS4_GPIO,
161 EXTTS5_GPIO,
162 GPIO_TABLE_SIZE
163};
164
Richard Cochrancb646e22011-04-22 12:04:55 +0200165static int chosen_phy = -1;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000166static ushort gpio_tab[GPIO_TABLE_SIZE] = {
167 1, 2, 3, 4, 8, 9, 10, 11
168};
Richard Cochrancb646e22011-04-22 12:04:55 +0200169
170module_param(chosen_phy, int, 0444);
Richard Cochran49b3fd42011-09-20 01:43:14 +0000171module_param_array(gpio_tab, ushort, NULL, 0444);
Richard Cochrancb646e22011-04-22 12:04:55 +0200172
173MODULE_PARM_DESC(chosen_phy, \
174 "The address of the PHY to use for the ancillary clock features");
Richard Cochran49b3fd42011-09-20 01:43:14 +0000175MODULE_PARM_DESC(gpio_tab, \
176 "Which GPIO line to use for which purpose: cal,perout,extts1,...,extts6");
Richard Cochrancb646e22011-04-22 12:04:55 +0200177
Richard Cochran86dd3612014-03-20 22:21:58 +0100178static void dp83640_gpio_defaults(struct ptp_pin_desc *pd)
179{
180 int i, index;
181
182 for (i = 0; i < DP83640_N_PINS; i++) {
183 snprintf(pd[i].name, sizeof(pd[i].name), "GPIO%d", 1 + i);
184 pd[i].index = i;
185 }
186
187 for (i = 0; i < GPIO_TABLE_SIZE; i++) {
188 if (gpio_tab[i] < 1 || gpio_tab[i] > DP83640_N_PINS) {
189 pr_err("gpio_tab[%d]=%hu out of range", i, gpio_tab[i]);
190 return;
191 }
192 }
193
194 index = gpio_tab[CALIBRATE_GPIO] - 1;
195 pd[index].func = PTP_PF_PHYSYNC;
196 pd[index].chan = 0;
197
198 index = gpio_tab[PEROUT_GPIO] - 1;
199 pd[index].func = PTP_PF_PEROUT;
200 pd[index].chan = 0;
201
202 for (i = EXTTS0_GPIO; i < GPIO_TABLE_SIZE; i++) {
203 index = gpio_tab[i] - 1;
204 pd[index].func = PTP_PF_EXTTS;
205 pd[index].chan = i - EXTTS0_GPIO;
206 }
207}
208
Richard Cochrancb646e22011-04-22 12:04:55 +0200209/* a list of clocks and a mutex to protect it */
210static LIST_HEAD(phyter_clocks);
211static DEFINE_MUTEX(phyter_clocks_lock);
212
213static void rx_timestamp_work(struct work_struct *work);
214
215/* extended register access functions */
216
217#define BROADCAST_ADDR 31
218
219static inline int broadcast_write(struct mii_bus *bus, u32 regnum, u16 val)
220{
221 return mdiobus_write(bus, BROADCAST_ADDR, regnum, val);
222}
223
224/* Caller must hold extreg_lock. */
225static int ext_read(struct phy_device *phydev, int page, u32 regnum)
226{
227 struct dp83640_private *dp83640 = phydev->priv;
228 int val;
229
230 if (dp83640->clock->page != page) {
231 broadcast_write(phydev->bus, PAGESEL, page);
232 dp83640->clock->page = page;
233 }
234 val = phy_read(phydev, regnum);
235
236 return val;
237}
238
239/* Caller must hold extreg_lock. */
240static void ext_write(int broadcast, struct phy_device *phydev,
241 int page, u32 regnum, u16 val)
242{
243 struct dp83640_private *dp83640 = phydev->priv;
244
245 if (dp83640->clock->page != page) {
246 broadcast_write(phydev->bus, PAGESEL, page);
247 dp83640->clock->page = page;
248 }
249 if (broadcast)
250 broadcast_write(phydev->bus, regnum, val);
251 else
252 phy_write(phydev, regnum, val);
253}
254
255/* Caller must hold extreg_lock. */
256static int tdr_write(int bc, struct phy_device *dev,
257 const struct timespec *ts, u16 cmd)
258{
259 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec & 0xffff);/* ns[15:0] */
260 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec >> 16); /* ns[31:16] */
261 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec & 0xffff); /* sec[15:0] */
262 ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec >> 16); /* sec[31:16]*/
263
264 ext_write(bc, dev, PAGE4, PTP_CTL, cmd);
265
266 return 0;
267}
268
269/* convert phy timestamps into driver timestamps */
270
271static void phy2rxts(struct phy_rxts *p, struct rxts *rxts)
272{
273 u32 sec;
274
275 sec = p->sec_lo;
276 sec |= p->sec_hi << 16;
277
278 rxts->ns = p->ns_lo;
279 rxts->ns |= (p->ns_hi & 0x3fff) << 16;
280 rxts->ns += ((u64)sec) * 1000000000ULL;
281 rxts->seqid = p->seqid;
282 rxts->msgtype = (p->msgtype >> 12) & 0xf;
283 rxts->hash = p->msgtype & 0x0fff;
Richard Cochran80288372011-08-06 21:03:04 +0000284 rxts->tmo = jiffies + 2;
Richard Cochrancb646e22011-04-22 12:04:55 +0200285}
286
287static u64 phy2txts(struct phy_txts *p)
288{
289 u64 ns;
290 u32 sec;
291
292 sec = p->sec_lo;
293 sec |= p->sec_hi << 16;
294
295 ns = p->ns_lo;
296 ns |= (p->ns_hi & 0x3fff) << 16;
297 ns += ((u64)sec) * 1000000000ULL;
298
299 return ns;
300}
301
Richard Cochran621bdec2014-03-20 22:22:00 +0100302static int periodic_output(struct dp83640_clock *clock,
Stefan Sørensenad015772014-06-27 12:05:30 +0200303 struct ptp_clock_request *clkreq, bool on,
304 int trigger)
Richard Cochran49b3fd42011-09-20 01:43:14 +0000305{
306 struct dp83640_private *dp83640 = clock->chosen;
307 struct phy_device *phydev = dp83640->phydev;
Richard Cochran564ca562014-03-20 22:21:57 +0100308 u32 sec, nsec, pwidth;
Stefan Sørensenad015772014-06-27 12:05:30 +0200309 u16 gpio, ptp_trig, val;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000310
Richard Cochran621bdec2014-03-20 22:22:00 +0100311 if (on) {
Stefan Sørensenad015772014-06-27 12:05:30 +0200312 gpio = 1 + ptp_find_pin(clock->ptp_clock, PTP_PF_PEROUT,
313 trigger);
Richard Cochran621bdec2014-03-20 22:22:00 +0100314 if (gpio < 1)
315 return -EINVAL;
316 } else {
317 gpio = 0;
318 }
319
Richard Cochran49b3fd42011-09-20 01:43:14 +0000320 ptp_trig = TRIG_WR |
321 (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT |
322 (gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT |
323 TRIG_PER |
324 TRIG_PULSE;
325
326 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
327
328 if (!on) {
329 val |= TRIG_DIS;
330 mutex_lock(&clock->extreg_lock);
331 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
332 ext_write(0, phydev, PAGE4, PTP_CTL, val);
333 mutex_unlock(&clock->extreg_lock);
Richard Cochran621bdec2014-03-20 22:22:00 +0100334 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000335 }
336
337 sec = clkreq->perout.start.sec;
338 nsec = clkreq->perout.start.nsec;
Richard Cochran564ca562014-03-20 22:21:57 +0100339 pwidth = clkreq->perout.period.sec * 1000000000UL;
340 pwidth += clkreq->perout.period.nsec;
341 pwidth /= 2;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000342
343 mutex_lock(&clock->extreg_lock);
344
345 ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
346
347 /*load trigger*/
348 val |= TRIG_LOAD;
349 ext_write(0, phydev, PAGE4, PTP_CTL, val);
350 ext_write(0, phydev, PAGE4, PTP_TDR, nsec & 0xffff); /* ns[15:0] */
351 ext_write(0, phydev, PAGE4, PTP_TDR, nsec >> 16); /* ns[31:16] */
352 ext_write(0, phydev, PAGE4, PTP_TDR, sec & 0xffff); /* sec[15:0] */
353 ext_write(0, phydev, PAGE4, PTP_TDR, sec >> 16); /* sec[31:16] */
Richard Cochran564ca562014-03-20 22:21:57 +0100354 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff); /* ns[15:0] */
355 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16); /* ns[31:16] */
Stefan Sørensen35e872a2014-06-27 12:05:29 +0200356 /* Triggers 0 and 1 has programmable pulsewidth2 */
357 if (trigger < 2) {
358 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff);
359 ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16);
360 }
Richard Cochran49b3fd42011-09-20 01:43:14 +0000361
362 /*enable trigger*/
363 val &= ~TRIG_LOAD;
364 val |= TRIG_EN;
365 ext_write(0, phydev, PAGE4, PTP_CTL, val);
366
367 mutex_unlock(&clock->extreg_lock);
Richard Cochran621bdec2014-03-20 22:22:00 +0100368 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000369}
370
Richard Cochrancb646e22011-04-22 12:04:55 +0200371/* ptp clock methods */
372
373static int ptp_dp83640_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
374{
375 struct dp83640_clock *clock =
376 container_of(ptp, struct dp83640_clock, caps);
377 struct phy_device *phydev = clock->chosen->phydev;
378 u64 rate;
379 int neg_adj = 0;
380 u16 hi, lo;
381
382 if (ppb < 0) {
383 neg_adj = 1;
384 ppb = -ppb;
385 }
386 rate = ppb;
387 rate <<= 26;
388 rate = div_u64(rate, 1953125);
389
390 hi = (rate >> 16) & PTP_RATE_HI_MASK;
391 if (neg_adj)
392 hi |= PTP_RATE_DIR;
393
394 lo = rate & 0xffff;
395
396 mutex_lock(&clock->extreg_lock);
397
398 ext_write(1, phydev, PAGE4, PTP_RATEH, hi);
399 ext_write(1, phydev, PAGE4, PTP_RATEL, lo);
400
401 mutex_unlock(&clock->extreg_lock);
402
403 return 0;
404}
405
406static int ptp_dp83640_adjtime(struct ptp_clock_info *ptp, s64 delta)
407{
408 struct dp83640_clock *clock =
409 container_of(ptp, struct dp83640_clock, caps);
410 struct phy_device *phydev = clock->chosen->phydev;
411 struct timespec ts;
412 int err;
413
414 delta += ADJTIME_FIX;
415
416 ts = ns_to_timespec(delta);
417
418 mutex_lock(&clock->extreg_lock);
419
420 err = tdr_write(1, phydev, &ts, PTP_STEP_CLK);
421
422 mutex_unlock(&clock->extreg_lock);
423
424 return err;
425}
426
427static int ptp_dp83640_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
428{
429 struct dp83640_clock *clock =
430 container_of(ptp, struct dp83640_clock, caps);
431 struct phy_device *phydev = clock->chosen->phydev;
432 unsigned int val[4];
433
434 mutex_lock(&clock->extreg_lock);
435
436 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_RD_CLK);
437
438 val[0] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[15:0] */
439 val[1] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[31:16] */
440 val[2] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[15:0] */
441 val[3] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[31:16] */
442
443 mutex_unlock(&clock->extreg_lock);
444
445 ts->tv_nsec = val[0] | (val[1] << 16);
446 ts->tv_sec = val[2] | (val[3] << 16);
447
448 return 0;
449}
450
451static int ptp_dp83640_settime(struct ptp_clock_info *ptp,
452 const struct timespec *ts)
453{
454 struct dp83640_clock *clock =
455 container_of(ptp, struct dp83640_clock, caps);
456 struct phy_device *phydev = clock->chosen->phydev;
457 int err;
458
459 mutex_lock(&clock->extreg_lock);
460
461 err = tdr_write(1, phydev, ts, PTP_LOAD_CLK);
462
463 mutex_unlock(&clock->extreg_lock);
464
465 return err;
466}
467
468static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
469 struct ptp_clock_request *rq, int on)
470{
471 struct dp83640_clock *clock =
472 container_of(ptp, struct dp83640_clock, caps);
473 struct phy_device *phydev = clock->chosen->phydev;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100474 unsigned int index;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000475 u16 evnt, event_num, gpio_num;
Richard Cochrancb646e22011-04-22 12:04:55 +0200476
477 switch (rq->type) {
478 case PTP_CLK_REQ_EXTTS:
Richard Cochran49b3fd42011-09-20 01:43:14 +0000479 index = rq->extts.index;
Richard Cochranfbf4b932014-03-20 22:21:56 +0100480 if (index >= N_EXT_TS)
Richard Cochrancb646e22011-04-22 12:04:55 +0200481 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000482 event_num = EXT_EVENT + index;
483 evnt = EVNT_WR | (event_num & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
Richard Cochrancb646e22011-04-22 12:04:55 +0200484 if (on) {
Richard Cochranfaa89712014-03-20 22:21:59 +0100485 gpio_num = 1 + ptp_find_pin(clock->ptp_clock,
486 PTP_PF_EXTTS, index);
487 if (gpio_num < 1)
488 return -EINVAL;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000489 evnt |= (gpio_num & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
Stefan Sørensen80671bd2014-02-03 15:36:50 +0100490 if (rq->extts.flags & PTP_FALLING_EDGE)
491 evnt |= EVNT_FALL;
492 else
493 evnt |= EVNT_RISE;
Richard Cochrancb646e22011-04-22 12:04:55 +0200494 }
495 ext_write(0, phydev, PAGE5, PTP_EVNT, evnt);
496 return 0;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000497
498 case PTP_CLK_REQ_PEROUT:
Stefan Sørensenad015772014-06-27 12:05:30 +0200499 if (rq->perout.index >= N_PER_OUT)
Richard Cochran49b3fd42011-09-20 01:43:14 +0000500 return -EINVAL;
Stefan Sørensenad015772014-06-27 12:05:30 +0200501 return periodic_output(clock, rq, on, rq->perout.index);
Richard Cochran49b3fd42011-09-20 01:43:14 +0000502
Richard Cochrancb646e22011-04-22 12:04:55 +0200503 default:
504 break;
505 }
506
507 return -EOPNOTSUPP;
508}
509
Richard Cochran86dd3612014-03-20 22:21:58 +0100510static int ptp_dp83640_verify(struct ptp_clock_info *ptp, unsigned int pin,
511 enum ptp_pin_function func, unsigned int chan)
512{
513 return 0;
514}
515
Richard Cochrancb646e22011-04-22 12:04:55 +0200516static u8 status_frame_dst[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 };
517static u8 status_frame_src[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F };
518
519static void enable_status_frames(struct phy_device *phydev, bool on)
520{
521 u16 cfg0 = 0, ver;
522
523 if (on)
524 cfg0 = PSF_EVNT_EN | PSF_RXTS_EN | PSF_TXTS_EN | ENDIAN_FLAG;
525
526 ver = (PSF_PTPVER & VERSIONPTP_MASK) << VERSIONPTP_SHIFT;
527
528 ext_write(0, phydev, PAGE5, PSF_CFG0, cfg0);
529 ext_write(0, phydev, PAGE6, PSF_CFG1, ver);
530
531 if (!phydev->attached_dev) {
Joe Perches8d242482012-06-09 07:49:07 +0000532 pr_warn("expected to find an attached netdevice\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200533 return;
534 }
535
536 if (on) {
537 if (dev_mc_add(phydev->attached_dev, status_frame_dst))
Joe Perches8d242482012-06-09 07:49:07 +0000538 pr_warn("failed to add mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200539 } else {
540 if (dev_mc_del(phydev->attached_dev, status_frame_dst))
Joe Perches8d242482012-06-09 07:49:07 +0000541 pr_warn("failed to delete mc address\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200542 }
543}
544
545static bool is_status_frame(struct sk_buff *skb, int type)
546{
547 struct ethhdr *h = eth_hdr(skb);
548
549 if (PTP_CLASS_V2_L2 == type &&
550 !memcmp(h->h_source, status_frame_src, sizeof(status_frame_src)))
551 return true;
552 else
553 return false;
554}
555
556static int expired(struct rxts *rxts)
557{
558 return time_after(jiffies, rxts->tmo);
559}
560
561/* Caller must hold rx_lock. */
562static void prune_rx_ts(struct dp83640_private *dp83640)
563{
564 struct list_head *this, *next;
565 struct rxts *rxts;
566
567 list_for_each_safe(this, next, &dp83640->rxts) {
568 rxts = list_entry(this, struct rxts, list);
569 if (expired(rxts)) {
570 list_del_init(&rxts->list);
571 list_add(&rxts->list, &dp83640->rxpool);
572 }
573 }
574}
575
576/* synchronize the phyters so they act as one clock */
577
578static void enable_broadcast(struct phy_device *phydev, int init_page, int on)
579{
580 int val;
581 phy_write(phydev, PAGESEL, 0);
582 val = phy_read(phydev, PHYCR2);
583 if (on)
584 val |= BC_WRITE;
585 else
586 val &= ~BC_WRITE;
587 phy_write(phydev, PHYCR2, val);
588 phy_write(phydev, PAGESEL, init_page);
589}
590
591static void recalibrate(struct dp83640_clock *clock)
592{
593 s64 now, diff;
594 struct phy_txts event_ts;
595 struct timespec ts;
596 struct list_head *this;
597 struct dp83640_private *tmp;
598 struct phy_device *master = clock->chosen->phydev;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000599 u16 cal_gpio, cfg0, evnt, ptp_trig, trigger, val;
Richard Cochrancb646e22011-04-22 12:04:55 +0200600
601 trigger = CAL_TRIGGER;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000602 cal_gpio = gpio_tab[CALIBRATE_GPIO];
Richard Cochrancb646e22011-04-22 12:04:55 +0200603
604 mutex_lock(&clock->extreg_lock);
605
606 /*
607 * enable broadcast, disable status frames, enable ptp clock
608 */
609 list_for_each(this, &clock->phylist) {
610 tmp = list_entry(this, struct dp83640_private, list);
611 enable_broadcast(tmp->phydev, clock->page, 1);
612 tmp->cfg0 = ext_read(tmp->phydev, PAGE5, PSF_CFG0);
613 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, 0);
614 ext_write(0, tmp->phydev, PAGE4, PTP_CTL, PTP_ENABLE);
615 }
616 enable_broadcast(master, clock->page, 1);
617 cfg0 = ext_read(master, PAGE5, PSF_CFG0);
618 ext_write(0, master, PAGE5, PSF_CFG0, 0);
619 ext_write(0, master, PAGE4, PTP_CTL, PTP_ENABLE);
620
621 /*
622 * enable an event timestamp
623 */
624 evnt = EVNT_WR | EVNT_RISE | EVNT_SINGLE;
625 evnt |= (CAL_EVENT & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
626 evnt |= (cal_gpio & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
627
628 list_for_each(this, &clock->phylist) {
629 tmp = list_entry(this, struct dp83640_private, list);
630 ext_write(0, tmp->phydev, PAGE5, PTP_EVNT, evnt);
631 }
632 ext_write(0, master, PAGE5, PTP_EVNT, evnt);
633
634 /*
635 * configure a trigger
636 */
637 ptp_trig = TRIG_WR | TRIG_IF_LATE | TRIG_PULSE;
638 ptp_trig |= (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT;
639 ptp_trig |= (cal_gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT;
640 ext_write(0, master, PAGE5, PTP_TRIG, ptp_trig);
641
642 /* load trigger */
643 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
644 val |= TRIG_LOAD;
645 ext_write(0, master, PAGE4, PTP_CTL, val);
646
647 /* enable trigger */
648 val &= ~TRIG_LOAD;
649 val |= TRIG_EN;
650 ext_write(0, master, PAGE4, PTP_CTL, val);
651
652 /* disable trigger */
653 val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
654 val |= TRIG_DIS;
655 ext_write(0, master, PAGE4, PTP_CTL, val);
656
657 /*
658 * read out and correct offsets
659 */
660 val = ext_read(master, PAGE4, PTP_STS);
Joe Perches8d242482012-06-09 07:49:07 +0000661 pr_info("master PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200662 val = ext_read(master, PAGE4, PTP_ESTS);
Joe Perches8d242482012-06-09 07:49:07 +0000663 pr_info("master PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200664 event_ts.ns_lo = ext_read(master, PAGE4, PTP_EDATA);
665 event_ts.ns_hi = ext_read(master, PAGE4, PTP_EDATA);
666 event_ts.sec_lo = ext_read(master, PAGE4, PTP_EDATA);
667 event_ts.sec_hi = ext_read(master, PAGE4, PTP_EDATA);
668 now = phy2txts(&event_ts);
669
670 list_for_each(this, &clock->phylist) {
671 tmp = list_entry(this, struct dp83640_private, list);
672 val = ext_read(tmp->phydev, PAGE4, PTP_STS);
Joe Perches8d242482012-06-09 07:49:07 +0000673 pr_info("slave PTP_STS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200674 val = ext_read(tmp->phydev, PAGE4, PTP_ESTS);
Joe Perches8d242482012-06-09 07:49:07 +0000675 pr_info("slave PTP_ESTS 0x%04hx\n", val);
Richard Cochrancb646e22011-04-22 12:04:55 +0200676 event_ts.ns_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
677 event_ts.ns_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
678 event_ts.sec_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
679 event_ts.sec_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
680 diff = now - (s64) phy2txts(&event_ts);
681 pr_info("slave offset %lld nanoseconds\n", diff);
682 diff += ADJTIME_FIX;
683 ts = ns_to_timespec(diff);
684 tdr_write(0, tmp->phydev, &ts, PTP_STEP_CLK);
685 }
686
687 /*
688 * restore status frames
689 */
690 list_for_each(this, &clock->phylist) {
691 tmp = list_entry(this, struct dp83640_private, list);
692 ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, tmp->cfg0);
693 }
694 ext_write(0, master, PAGE5, PSF_CFG0, cfg0);
695
696 mutex_unlock(&clock->extreg_lock);
697}
698
699/* time stamping methods */
700
Richard Cochran49b3fd42011-09-20 01:43:14 +0000701static inline u16 exts_chan_to_edata(int ch)
702{
703 return 1 << ((ch + EXT_EVENT) * 2);
704}
705
Richard Cochran23310382011-06-14 23:55:19 +0000706static int decode_evnt(struct dp83640_private *dp83640,
707 void *data, u16 ests)
Richard Cochrancb646e22011-04-22 12:04:55 +0200708{
Richard Cochran23310382011-06-14 23:55:19 +0000709 struct phy_txts *phy_txts;
Richard Cochrancb646e22011-04-22 12:04:55 +0200710 struct ptp_clock_event event;
Richard Cochran49b3fd42011-09-20 01:43:14 +0000711 int i, parsed;
Richard Cochrancb646e22011-04-22 12:04:55 +0200712 int words = (ests >> EVNT_TS_LEN_SHIFT) & EVNT_TS_LEN_MASK;
Richard Cochran23310382011-06-14 23:55:19 +0000713 u16 ext_status = 0;
714
715 if (ests & MULT_EVNT) {
716 ext_status = *(u16 *) data;
717 data += sizeof(ext_status);
718 }
719
720 phy_txts = data;
Richard Cochrancb646e22011-04-22 12:04:55 +0200721
722 switch (words) { /* fall through in every case */
723 case 3:
724 dp83640->edata.sec_hi = phy_txts->sec_hi;
725 case 2:
726 dp83640->edata.sec_lo = phy_txts->sec_lo;
727 case 1:
728 dp83640->edata.ns_hi = phy_txts->ns_hi;
729 case 0:
730 dp83640->edata.ns_lo = phy_txts->ns_lo;
731 }
732
Richard Cochran49b3fd42011-09-20 01:43:14 +0000733 if (ext_status) {
734 parsed = words + 2;
735 } else {
736 parsed = words + 1;
737 i = ((ests >> EVNT_NUM_SHIFT) & EVNT_NUM_MASK) - EXT_EVENT;
738 ext_status = exts_chan_to_edata(i);
739 }
740
Richard Cochrancb646e22011-04-22 12:04:55 +0200741 event.type = PTP_CLOCK_EXTTS;
Richard Cochrancb646e22011-04-22 12:04:55 +0200742 event.timestamp = phy2txts(&dp83640->edata);
743
Richard Cochran49b3fd42011-09-20 01:43:14 +0000744 for (i = 0; i < N_EXT_TS; i++) {
745 if (ext_status & exts_chan_to_edata(i)) {
746 event.index = i;
747 ptp_clock_event(dp83640->clock->ptp_clock, &event);
748 }
749 }
Richard Cochran23310382011-06-14 23:55:19 +0000750
Richard Cochran49b3fd42011-09-20 01:43:14 +0000751 return parsed * sizeof(u16);
Richard Cochrancb646e22011-04-22 12:04:55 +0200752}
753
754static void decode_rxts(struct dp83640_private *dp83640,
755 struct phy_rxts *phy_rxts)
756{
757 struct rxts *rxts;
758 unsigned long flags;
759
760 spin_lock_irqsave(&dp83640->rx_lock, flags);
761
762 prune_rx_ts(dp83640);
763
764 if (list_empty(&dp83640->rxpool)) {
Joe Perches8d242482012-06-09 07:49:07 +0000765 pr_debug("rx timestamp pool is empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200766 goto out;
767 }
768 rxts = list_first_entry(&dp83640->rxpool, struct rxts, list);
769 list_del_init(&rxts->list);
770 phy2rxts(phy_rxts, rxts);
771 list_add_tail(&rxts->list, &dp83640->rxts);
772out:
773 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
774}
775
776static void decode_txts(struct dp83640_private *dp83640,
777 struct phy_txts *phy_txts)
778{
779 struct skb_shared_hwtstamps shhwtstamps;
780 struct sk_buff *skb;
781 u64 ns;
782
783 /* We must already have the skb that triggered this. */
784
785 skb = skb_dequeue(&dp83640->tx_queue);
786
787 if (!skb) {
Joe Perches8d242482012-06-09 07:49:07 +0000788 pr_debug("have timestamp but tx_queue empty\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200789 return;
790 }
791 ns = phy2txts(phy_txts);
792 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
793 shhwtstamps.hwtstamp = ns_to_ktime(ns);
794 skb_complete_tx_timestamp(skb, &shhwtstamps);
795}
796
797static void decode_status_frame(struct dp83640_private *dp83640,
798 struct sk_buff *skb)
799{
800 struct phy_rxts *phy_rxts;
801 struct phy_txts *phy_txts;
802 u8 *ptr;
803 int len, size;
804 u16 ests, type;
805
806 ptr = skb->data + 2;
807
808 for (len = skb_headlen(skb) - 2; len > sizeof(type); len -= size) {
809
810 type = *(u16 *)ptr;
811 ests = type & 0x0fff;
812 type = type & 0xf000;
813 len -= sizeof(type);
814 ptr += sizeof(type);
815
816 if (PSF_RX == type && len >= sizeof(*phy_rxts)) {
817
818 phy_rxts = (struct phy_rxts *) ptr;
819 decode_rxts(dp83640, phy_rxts);
820 size = sizeof(*phy_rxts);
821
822 } else if (PSF_TX == type && len >= sizeof(*phy_txts)) {
823
824 phy_txts = (struct phy_txts *) ptr;
825 decode_txts(dp83640, phy_txts);
826 size = sizeof(*phy_txts);
827
828 } else if (PSF_EVNT == type && len >= sizeof(*phy_txts)) {
829
Richard Cochran23310382011-06-14 23:55:19 +0000830 size = decode_evnt(dp83640, ptr, ests);
Richard Cochrancb646e22011-04-22 12:04:55 +0200831
832 } else {
833 size = 0;
834 break;
835 }
836 ptr += size;
837 }
838}
839
Richard Cochrandccaa9e2011-09-20 01:43:16 +0000840static int is_sync(struct sk_buff *skb, int type)
841{
842 u8 *data = skb->data, *msgtype;
843 unsigned int offset = 0;
844
845 switch (type) {
846 case PTP_CLASS_V1_IPV4:
847 case PTP_CLASS_V2_IPV4:
848 offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
849 break;
850 case PTP_CLASS_V1_IPV6:
851 case PTP_CLASS_V2_IPV6:
852 offset = OFF_PTP6;
853 break;
854 case PTP_CLASS_V2_L2:
855 offset = ETH_HLEN;
856 break;
857 case PTP_CLASS_V2_VLAN:
858 offset = ETH_HLEN + VLAN_HLEN;
859 break;
860 default:
861 return 0;
862 }
863
864 if (type & PTP_CLASS_V1)
865 offset += OFF_PTP_CONTROL;
866
867 if (skb->len < offset + 1)
868 return 0;
869
870 msgtype = data + offset;
871
872 return (*msgtype & 0xf) == 0;
873}
874
Richard Cochrancb646e22011-04-22 12:04:55 +0200875static int match(struct sk_buff *skb, unsigned int type, struct rxts *rxts)
876{
877 u16 *seqid;
878 unsigned int offset;
879 u8 *msgtype, *data = skb_mac_header(skb);
880
881 /* check sequenceID, messageType, 12 bit hash of offset 20-29 */
882
883 switch (type) {
884 case PTP_CLASS_V1_IPV4:
885 case PTP_CLASS_V2_IPV4:
886 offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
887 break;
888 case PTP_CLASS_V1_IPV6:
889 case PTP_CLASS_V2_IPV6:
890 offset = OFF_PTP6;
891 break;
892 case PTP_CLASS_V2_L2:
893 offset = ETH_HLEN;
894 break;
895 case PTP_CLASS_V2_VLAN:
896 offset = ETH_HLEN + VLAN_HLEN;
897 break;
898 default:
899 return 0;
900 }
901
902 if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
903 return 0;
904
905 if (unlikely(type & PTP_CLASS_V1))
906 msgtype = data + offset + OFF_PTP_CONTROL;
907 else
908 msgtype = data + offset;
909
910 seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
911
Florian Fainellidd61d962013-12-17 21:38:07 -0800912 return rxts->msgtype == (*msgtype & 0xf) &&
913 rxts->seqid == ntohs(*seqid);
Richard Cochrancb646e22011-04-22 12:04:55 +0200914}
915
916static void dp83640_free_clocks(void)
917{
918 struct dp83640_clock *clock;
919 struct list_head *this, *next;
920
921 mutex_lock(&phyter_clocks_lock);
922
923 list_for_each_safe(this, next, &phyter_clocks) {
924 clock = list_entry(this, struct dp83640_clock, list);
925 if (!list_empty(&clock->phylist)) {
Joe Perches8d242482012-06-09 07:49:07 +0000926 pr_warn("phy list non-empty while unloading\n");
Richard Cochrancb646e22011-04-22 12:04:55 +0200927 BUG();
928 }
929 list_del(&clock->list);
930 mutex_destroy(&clock->extreg_lock);
931 mutex_destroy(&clock->clock_lock);
932 put_device(&clock->bus->dev);
Richard Cochran86dd3612014-03-20 22:21:58 +0100933 kfree(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +0200934 kfree(clock);
935 }
936
937 mutex_unlock(&phyter_clocks_lock);
938}
939
940static void dp83640_clock_init(struct dp83640_clock *clock, struct mii_bus *bus)
941{
942 INIT_LIST_HEAD(&clock->list);
943 clock->bus = bus;
944 mutex_init(&clock->extreg_lock);
945 mutex_init(&clock->clock_lock);
946 INIT_LIST_HEAD(&clock->phylist);
947 clock->caps.owner = THIS_MODULE;
948 sprintf(clock->caps.name, "dp83640 timer");
949 clock->caps.max_adj = 1953124;
950 clock->caps.n_alarm = 0;
951 clock->caps.n_ext_ts = N_EXT_TS;
Stefan Sørensenad015772014-06-27 12:05:30 +0200952 clock->caps.n_per_out = N_PER_OUT;
Richard Cochran86dd3612014-03-20 22:21:58 +0100953 clock->caps.n_pins = DP83640_N_PINS;
Richard Cochrancb646e22011-04-22 12:04:55 +0200954 clock->caps.pps = 0;
955 clock->caps.adjfreq = ptp_dp83640_adjfreq;
956 clock->caps.adjtime = ptp_dp83640_adjtime;
957 clock->caps.gettime = ptp_dp83640_gettime;
958 clock->caps.settime = ptp_dp83640_settime;
959 clock->caps.enable = ptp_dp83640_enable;
Richard Cochran86dd3612014-03-20 22:21:58 +0100960 clock->caps.verify = ptp_dp83640_verify;
961 /*
962 * Convert the module param defaults into a dynamic pin configuration.
963 */
964 dp83640_gpio_defaults(clock->caps.pin_config);
Richard Cochrancb646e22011-04-22 12:04:55 +0200965 /*
966 * Get a reference to this bus instance.
967 */
968 get_device(&bus->dev);
969}
970
971static int choose_this_phy(struct dp83640_clock *clock,
972 struct phy_device *phydev)
973{
974 if (chosen_phy == -1 && !clock->chosen)
975 return 1;
976
977 if (chosen_phy == phydev->addr)
978 return 1;
979
980 return 0;
981}
982
983static struct dp83640_clock *dp83640_clock_get(struct dp83640_clock *clock)
984{
985 if (clock)
986 mutex_lock(&clock->clock_lock);
987 return clock;
988}
989
990/*
991 * Look up and lock a clock by bus instance.
992 * If there is no clock for this bus, then create it first.
993 */
994static struct dp83640_clock *dp83640_clock_get_bus(struct mii_bus *bus)
995{
996 struct dp83640_clock *clock = NULL, *tmp;
997 struct list_head *this;
998
999 mutex_lock(&phyter_clocks_lock);
1000
1001 list_for_each(this, &phyter_clocks) {
1002 tmp = list_entry(this, struct dp83640_clock, list);
1003 if (tmp->bus == bus) {
1004 clock = tmp;
1005 break;
1006 }
1007 }
1008 if (clock)
1009 goto out;
1010
1011 clock = kzalloc(sizeof(struct dp83640_clock), GFP_KERNEL);
1012 if (!clock)
1013 goto out;
1014
Richard Cochran86dd3612014-03-20 22:21:58 +01001015 clock->caps.pin_config = kzalloc(sizeof(struct ptp_pin_desc) *
1016 DP83640_N_PINS, GFP_KERNEL);
1017 if (!clock->caps.pin_config) {
1018 kfree(clock);
1019 clock = NULL;
1020 goto out;
1021 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001022 dp83640_clock_init(clock, bus);
1023 list_add_tail(&phyter_clocks, &clock->list);
1024out:
1025 mutex_unlock(&phyter_clocks_lock);
1026
1027 return dp83640_clock_get(clock);
1028}
1029
1030static void dp83640_clock_put(struct dp83640_clock *clock)
1031{
1032 mutex_unlock(&clock->clock_lock);
1033}
1034
1035static int dp83640_probe(struct phy_device *phydev)
1036{
1037 struct dp83640_clock *clock;
1038 struct dp83640_private *dp83640;
1039 int err = -ENOMEM, i;
1040
1041 if (phydev->addr == BROADCAST_ADDR)
1042 return 0;
1043
1044 clock = dp83640_clock_get_bus(phydev->bus);
1045 if (!clock)
1046 goto no_clock;
1047
1048 dp83640 = kzalloc(sizeof(struct dp83640_private), GFP_KERNEL);
1049 if (!dp83640)
1050 goto no_memory;
1051
1052 dp83640->phydev = phydev;
1053 INIT_WORK(&dp83640->ts_work, rx_timestamp_work);
1054
1055 INIT_LIST_HEAD(&dp83640->rxts);
1056 INIT_LIST_HEAD(&dp83640->rxpool);
1057 for (i = 0; i < MAX_RXTS; i++)
1058 list_add(&dp83640->rx_pool_data[i].list, &dp83640->rxpool);
1059
1060 phydev->priv = dp83640;
1061
1062 spin_lock_init(&dp83640->rx_lock);
1063 skb_queue_head_init(&dp83640->rx_queue);
1064 skb_queue_head_init(&dp83640->tx_queue);
1065
1066 dp83640->clock = clock;
1067
1068 if (choose_this_phy(clock, phydev)) {
1069 clock->chosen = dp83640;
Richard Cochran1ef76152012-09-22 07:02:03 +00001070 clock->ptp_clock = ptp_clock_register(&clock->caps, &phydev->dev);
Richard Cochrancb646e22011-04-22 12:04:55 +02001071 if (IS_ERR(clock->ptp_clock)) {
1072 err = PTR_ERR(clock->ptp_clock);
1073 goto no_register;
1074 }
1075 } else
1076 list_add_tail(&dp83640->list, &clock->phylist);
1077
Richard Cochrancb646e22011-04-22 12:04:55 +02001078 dp83640_clock_put(clock);
1079 return 0;
1080
1081no_register:
1082 clock->chosen = NULL;
1083 kfree(dp83640);
1084no_memory:
1085 dp83640_clock_put(clock);
1086no_clock:
1087 return err;
1088}
1089
1090static void dp83640_remove(struct phy_device *phydev)
1091{
1092 struct dp83640_clock *clock;
1093 struct list_head *this, *next;
1094 struct dp83640_private *tmp, *dp83640 = phydev->priv;
Richard Cochran8b3408f2011-10-21 00:49:17 +00001095 struct sk_buff *skb;
Richard Cochrancb646e22011-04-22 12:04:55 +02001096
1097 if (phydev->addr == BROADCAST_ADDR)
1098 return;
1099
1100 enable_status_frames(phydev, false);
1101 cancel_work_sync(&dp83640->ts_work);
1102
Richard Cochran8b3408f2011-10-21 00:49:17 +00001103 while ((skb = skb_dequeue(&dp83640->rx_queue)) != NULL)
1104 kfree_skb(skb);
1105
1106 while ((skb = skb_dequeue(&dp83640->tx_queue)) != NULL)
1107 skb_complete_tx_timestamp(skb, NULL);
1108
Richard Cochrancb646e22011-04-22 12:04:55 +02001109 clock = dp83640_clock_get(dp83640->clock);
1110
1111 if (dp83640 == clock->chosen) {
1112 ptp_clock_unregister(clock->ptp_clock);
1113 clock->chosen = NULL;
1114 } else {
1115 list_for_each_safe(this, next, &clock->phylist) {
1116 tmp = list_entry(this, struct dp83640_private, list);
1117 if (tmp == dp83640) {
1118 list_del_init(&tmp->list);
1119 break;
1120 }
1121 }
1122 }
1123
1124 dp83640_clock_put(clock);
1125 kfree(dp83640);
1126}
1127
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001128static int dp83640_config_init(struct phy_device *phydev)
1129{
Stefan Sørensen602b1092014-02-13 15:26:57 +01001130 struct dp83640_private *dp83640 = phydev->priv;
1131 struct dp83640_clock *clock = dp83640->clock;
1132
1133 if (clock->chosen && !list_empty(&clock->phylist))
1134 recalibrate(clock);
1135 else
1136 enable_broadcast(phydev, clock->page, 1);
1137
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001138 enable_status_frames(phydev, true);
1139 ext_write(0, phydev, PAGE4, PTP_CTL, PTP_ENABLE);
1140 return 0;
1141}
1142
Stephan Gatzka16421822012-12-04 10:21:38 +00001143static int dp83640_ack_interrupt(struct phy_device *phydev)
1144{
1145 int err = phy_read(phydev, MII_DP83640_MISR);
1146
1147 if (err < 0)
1148 return err;
1149
1150 return 0;
1151}
1152
1153static int dp83640_config_intr(struct phy_device *phydev)
1154{
1155 int micr;
1156 int misr;
1157 int err;
1158
1159 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1160 misr = phy_read(phydev, MII_DP83640_MISR);
1161 if (misr < 0)
1162 return misr;
1163 misr |=
1164 (MII_DP83640_MISR_ANC_INT_EN |
1165 MII_DP83640_MISR_DUP_INT_EN |
1166 MII_DP83640_MISR_SPD_INT_EN |
1167 MII_DP83640_MISR_LINK_INT_EN);
1168 err = phy_write(phydev, MII_DP83640_MISR, misr);
1169 if (err < 0)
1170 return err;
1171
1172 micr = phy_read(phydev, MII_DP83640_MICR);
1173 if (micr < 0)
1174 return micr;
1175 micr |=
1176 (MII_DP83640_MICR_OE |
1177 MII_DP83640_MICR_IE);
1178 return phy_write(phydev, MII_DP83640_MICR, micr);
1179 } else {
1180 micr = phy_read(phydev, MII_DP83640_MICR);
1181 if (micr < 0)
1182 return micr;
1183 micr &=
1184 ~(MII_DP83640_MICR_OE |
1185 MII_DP83640_MICR_IE);
1186 err = phy_write(phydev, MII_DP83640_MICR, micr);
1187 if (err < 0)
1188 return err;
1189
1190 misr = phy_read(phydev, MII_DP83640_MISR);
1191 if (misr < 0)
1192 return misr;
1193 misr &=
1194 ~(MII_DP83640_MISR_ANC_INT_EN |
1195 MII_DP83640_MISR_DUP_INT_EN |
1196 MII_DP83640_MISR_SPD_INT_EN |
1197 MII_DP83640_MISR_LINK_INT_EN);
1198 return phy_write(phydev, MII_DP83640_MISR, misr);
1199 }
1200}
1201
Richard Cochrancb646e22011-04-22 12:04:55 +02001202static int dp83640_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
1203{
1204 struct dp83640_private *dp83640 = phydev->priv;
1205 struct hwtstamp_config cfg;
1206 u16 txcfg0, rxcfg0;
1207
1208 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1209 return -EFAULT;
1210
1211 if (cfg.flags) /* reserved for future extensions */
1212 return -EINVAL;
1213
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001214 if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ONESTEP_SYNC)
Richard Cochrancb646e22011-04-22 12:04:55 +02001215 return -ERANGE;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001216
1217 dp83640->hwts_tx_en = cfg.tx_type;
Richard Cochrancb646e22011-04-22 12:04:55 +02001218
1219 switch (cfg.rx_filter) {
1220 case HWTSTAMP_FILTER_NONE:
1221 dp83640->hwts_rx_en = 0;
1222 dp83640->layer = 0;
1223 dp83640->version = 0;
1224 break;
1225 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1226 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1227 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1228 dp83640->hwts_rx_en = 1;
1229 dp83640->layer = LAYER4;
1230 dp83640->version = 1;
1231 break;
1232 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1233 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1234 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1235 dp83640->hwts_rx_en = 1;
1236 dp83640->layer = LAYER4;
1237 dp83640->version = 2;
1238 break;
1239 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1240 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1241 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1242 dp83640->hwts_rx_en = 1;
1243 dp83640->layer = LAYER2;
1244 dp83640->version = 2;
1245 break;
1246 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1247 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1248 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1249 dp83640->hwts_rx_en = 1;
1250 dp83640->layer = LAYER4|LAYER2;
1251 dp83640->version = 2;
1252 break;
1253 default:
1254 return -ERANGE;
1255 }
1256
1257 txcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1258 rxcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
1259
1260 if (dp83640->layer & LAYER2) {
1261 txcfg0 |= TX_L2_EN;
1262 rxcfg0 |= RX_L2_EN;
1263 }
1264 if (dp83640->layer & LAYER4) {
1265 txcfg0 |= TX_IPV6_EN | TX_IPV4_EN;
1266 rxcfg0 |= RX_IPV6_EN | RX_IPV4_EN;
1267 }
1268
1269 if (dp83640->hwts_tx_en)
1270 txcfg0 |= TX_TS_EN;
1271
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001272 if (dp83640->hwts_tx_en == HWTSTAMP_TX_ONESTEP_SYNC)
1273 txcfg0 |= SYNC_1STEP | CHK_1STEP;
1274
Richard Cochrancb646e22011-04-22 12:04:55 +02001275 if (dp83640->hwts_rx_en)
1276 rxcfg0 |= RX_TS_EN;
1277
1278 mutex_lock(&dp83640->clock->extreg_lock);
1279
Richard Cochrancb646e22011-04-22 12:04:55 +02001280 ext_write(0, phydev, PAGE5, PTP_TXCFG0, txcfg0);
1281 ext_write(0, phydev, PAGE5, PTP_RXCFG0, rxcfg0);
1282
1283 mutex_unlock(&dp83640->clock->extreg_lock);
1284
1285 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1286}
1287
1288static void rx_timestamp_work(struct work_struct *work)
1289{
1290 struct dp83640_private *dp83640 =
1291 container_of(work, struct dp83640_private, ts_work);
1292 struct list_head *this, *next;
1293 struct rxts *rxts;
1294 struct skb_shared_hwtstamps *shhwtstamps;
1295 struct sk_buff *skb;
1296 unsigned int type;
1297 unsigned long flags;
1298
1299 /* Deliver each deferred packet, with or without a time stamp. */
1300
1301 while ((skb = skb_dequeue(&dp83640->rx_queue)) != NULL) {
1302 type = SKB_PTP_TYPE(skb);
1303 spin_lock_irqsave(&dp83640->rx_lock, flags);
1304 list_for_each_safe(this, next, &dp83640->rxts) {
1305 rxts = list_entry(this, struct rxts, list);
1306 if (match(skb, type, rxts)) {
1307 shhwtstamps = skb_hwtstamps(skb);
1308 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
1309 shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
1310 list_del_init(&rxts->list);
1311 list_add(&rxts->list, &dp83640->rxpool);
1312 break;
1313 }
1314 }
1315 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
Manfred Rudigier72092cc2012-01-09 23:52:15 +00001316 netif_rx_ni(skb);
Richard Cochrancb646e22011-04-22 12:04:55 +02001317 }
1318
1319 /* Clear out expired time stamps. */
1320
1321 spin_lock_irqsave(&dp83640->rx_lock, flags);
1322 prune_rx_ts(dp83640);
1323 spin_unlock_irqrestore(&dp83640->rx_lock, flags);
1324}
1325
1326static bool dp83640_rxtstamp(struct phy_device *phydev,
1327 struct sk_buff *skb, int type)
1328{
1329 struct dp83640_private *dp83640 = phydev->priv;
1330
1331 if (!dp83640->hwts_rx_en)
1332 return false;
1333
1334 if (is_status_frame(skb, type)) {
1335 decode_status_frame(dp83640, skb);
Richard Cochranae6e86b2011-06-14 23:55:20 +00001336 kfree_skb(skb);
1337 return true;
Richard Cochrancb646e22011-04-22 12:04:55 +02001338 }
1339
1340 SKB_PTP_TYPE(skb) = type;
1341 skb_queue_tail(&dp83640->rx_queue, skb);
1342 schedule_work(&dp83640->ts_work);
1343
1344 return true;
1345}
1346
1347static void dp83640_txtstamp(struct phy_device *phydev,
1348 struct sk_buff *skb, int type)
1349{
1350 struct dp83640_private *dp83640 = phydev->priv;
1351
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001352 switch (dp83640->hwts_tx_en) {
1353
1354 case HWTSTAMP_TX_ONESTEP_SYNC:
1355 if (is_sync(skb, type)) {
Richard Cochranf5ff7cd2011-10-21 00:49:16 +00001356 skb_complete_tx_timestamp(skb, NULL);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001357 return;
1358 }
1359 /* fall through */
1360 case HWTSTAMP_TX_ON:
Stefan Sørensene2e2f512014-02-03 15:36:35 +01001361 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001362 skb_queue_tail(&dp83640->tx_queue, skb);
1363 schedule_work(&dp83640->ts_work);
1364 break;
1365
1366 case HWTSTAMP_TX_OFF:
1367 default:
Richard Cochranf5ff7cd2011-10-21 00:49:16 +00001368 skb_complete_tx_timestamp(skb, NULL);
Richard Cochrandccaa9e2011-09-20 01:43:16 +00001369 break;
Richard Cochrancb646e22011-04-22 12:04:55 +02001370 }
Richard Cochrancb646e22011-04-22 12:04:55 +02001371}
1372
Richard Cochran7dff3492012-04-03 22:59:18 +00001373static int dp83640_ts_info(struct phy_device *dev, struct ethtool_ts_info *info)
1374{
1375 struct dp83640_private *dp83640 = dev->priv;
1376
1377 info->so_timestamping =
1378 SOF_TIMESTAMPING_TX_HARDWARE |
1379 SOF_TIMESTAMPING_RX_HARDWARE |
1380 SOF_TIMESTAMPING_RAW_HARDWARE;
1381 info->phc_index = ptp_clock_index(dp83640->clock->ptp_clock);
1382 info->tx_types =
1383 (1 << HWTSTAMP_TX_OFF) |
1384 (1 << HWTSTAMP_TX_ON) |
1385 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
1386 info->rx_filters =
1387 (1 << HWTSTAMP_FILTER_NONE) |
1388 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
1389 (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
1390 (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
1391 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
1392 (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
1393 (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
1394 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1395 (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
1396 (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
1397 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
1398 (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
1399 (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ);
1400 return 0;
1401}
1402
Richard Cochrancb646e22011-04-22 12:04:55 +02001403static struct phy_driver dp83640_driver = {
1404 .phy_id = DP83640_PHY_ID,
1405 .phy_id_mask = 0xfffffff0,
1406 .name = "NatSemi DP83640",
1407 .features = PHY_BASIC_FEATURES,
Stephan Gatzka16421822012-12-04 10:21:38 +00001408 .flags = PHY_HAS_INTERRUPT,
Richard Cochrancb646e22011-04-22 12:04:55 +02001409 .probe = dp83640_probe,
1410 .remove = dp83640_remove,
Stefan Sørensen62ad9682014-02-03 15:36:58 +01001411 .config_init = dp83640_config_init,
Richard Cochrancb646e22011-04-22 12:04:55 +02001412 .config_aneg = genphy_config_aneg,
1413 .read_status = genphy_read_status,
Stephan Gatzka16421822012-12-04 10:21:38 +00001414 .ack_interrupt = dp83640_ack_interrupt,
1415 .config_intr = dp83640_config_intr,
Richard Cochran7dff3492012-04-03 22:59:18 +00001416 .ts_info = dp83640_ts_info,
Richard Cochrancb646e22011-04-22 12:04:55 +02001417 .hwtstamp = dp83640_hwtstamp,
1418 .rxtstamp = dp83640_rxtstamp,
1419 .txtstamp = dp83640_txtstamp,
1420 .driver = {.owner = THIS_MODULE,}
1421};
1422
1423static int __init dp83640_init(void)
1424{
1425 return phy_driver_register(&dp83640_driver);
1426}
1427
1428static void __exit dp83640_exit(void)
1429{
1430 dp83640_free_clocks();
1431 phy_driver_unregister(&dp83640_driver);
1432}
1433
1434MODULE_DESCRIPTION("National Semiconductor DP83640 PHY driver");
Richard Cochranfbf4b932014-03-20 22:21:56 +01001435MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
Richard Cochrancb646e22011-04-22 12:04:55 +02001436MODULE_LICENSE("GPL");
1437
1438module_init(dp83640_init);
1439module_exit(dp83640_exit);
1440
John Stultz86ff9baa2011-05-23 13:32:11 -07001441static struct mdio_device_id __maybe_unused dp83640_tbl[] = {
Richard Cochrancb646e22011-04-22 12:04:55 +02001442 { DP83640_PHY_ID, 0xfffffff0 },
1443 { }
1444};
1445
1446MODULE_DEVICE_TABLE(mdio, dp83640_tbl);