Skip to content

Commit d096459

Browse files
HoratiuVulturdavem330
authored andcommitted
net: lan966x: Add support for ptp clocks
The lan966x has 3 PHC. Enable each of them, for now all the timestamping is happening on the first PHC. Acked-by: Richard Cochran <[email protected]> Signed-off-by: Horatiu Vultur <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d700dff commit d096459

File tree

4 files changed

+317
-1
lines changed

4 files changed

+317
-1
lines changed

drivers/net/ethernet/microchip/lan966x/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ obj-$(CONFIG_LAN966X_SWITCH) += lan966x-switch.o
77

88
lan966x-switch-objs := lan966x_main.o lan966x_phylink.o lan966x_port.o \
99
lan966x_mac.o lan966x_ethtool.o lan966x_switchdev.o \
10-
lan966x_vlan.o lan966x_fdb.o lan966x_mdb.o
10+
lan966x_vlan.o lan966x_fdb.o lan966x_mdb.o \
11+
lan966x_ptp.o

drivers/net/ethernet/microchip/lan966x/lan966x_main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,8 +932,15 @@ static int lan966x_probe(struct platform_device *pdev)
932932
if (err)
933933
goto cleanup_ports;
934934

935+
err = lan966x_ptp_init(lan966x);
936+
if (err)
937+
goto cleanup_fdb;
938+
935939
return 0;
936940

941+
cleanup_fdb:
942+
lan966x_fdb_deinit(lan966x);
943+
937944
cleanup_ports:
938945
fwnode_handle_put(portnp);
939946

@@ -959,6 +966,7 @@ static int lan966x_remove(struct platform_device *pdev)
959966
lan966x_mac_purge_entries(lan966x);
960967
lan966x_mdb_deinit(lan966x);
961968
lan966x_fdb_deinit(lan966x);
969+
lan966x_ptp_deinit(lan966x);
962970

963971
return 0;
964972
}

drivers/net/ethernet/microchip/lan966x/lan966x_main.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/jiffies.h>
99
#include <linux/phy.h>
1010
#include <linux/phylink.h>
11+
#include <linux/ptp_clock_kernel.h>
1112
#include <net/switchdev.h>
1213

1314
#include "lan966x_regs.h"
@@ -50,6 +51,9 @@
5051
#define LAN966X_SPEED_100 2
5152
#define LAN966X_SPEED_10 3
5253

54+
#define LAN966X_PHC_COUNT 3
55+
#define LAN966X_PHC_PORT 0
56+
5357
/* MAC table entry types.
5458
* ENTRYTYPE_NORMAL is subject to aging.
5559
* ENTRYTYPE_LOCKED is not subject to aging.
@@ -70,6 +74,14 @@ struct lan966x_stat_layout {
7074
char name[ETH_GSTRING_LEN];
7175
};
7276

77+
struct lan966x_phc {
78+
struct ptp_clock *clock;
79+
struct ptp_clock_info info;
80+
struct hwtstamp_config hwtstamp_config;
81+
struct lan966x *lan966x;
82+
u8 index;
83+
};
84+
7385
struct lan966x {
7486
struct device *dev;
7587

@@ -113,6 +125,11 @@ struct lan966x {
113125
/* mdb */
114126
struct list_head mdb_entries;
115127
struct list_head pgid_entries;
128+
129+
/* ptp */
130+
bool ptp;
131+
struct lan966x_phc phc[LAN966X_PHC_COUNT];
132+
spinlock_t ptp_clock_lock; /* lock for phc */
116133
};
117134

118135
struct lan966x_port_config {
@@ -228,6 +245,9 @@ int lan966x_handle_port_mdb_del(struct lan966x_port *port,
228245
void lan966x_mdb_erase_entries(struct lan966x *lan966x, u16 vid);
229246
void lan966x_mdb_write_entries(struct lan966x *lan966x, u16 vid);
230247

248+
int lan966x_ptp_init(struct lan966x *lan966x);
249+
void lan966x_ptp_deinit(struct lan966x *lan966x);
250+
231251
static inline void __iomem *lan_addr(void __iomem *base[],
232252
int id, int tinst, int tcnt,
233253
int gbase, int ginst,
Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
3+
#include <linux/ptp_classify.h>
4+
5+
#include "lan966x_main.h"
6+
7+
#define LAN966X_MAX_PTP_ID 512
8+
9+
/* Represents 1ppm adjustment in 2^59 format with 6.037735849ns as reference
10+
* The value is calculated as following: (1/1000000)/((2^-59)/6.037735849)
11+
*/
12+
#define LAN966X_1PPM_FORMAT 3480517749723LL
13+
14+
/* Represents 1ppb adjustment in 2^29 format with 6.037735849ns as reference
15+
* The value is calculated as following: (1/1000000000)/((2^59)/6.037735849)
16+
*/
17+
#define LAN966X_1PPB_FORMAT 3480517749LL
18+
19+
#define TOD_ACC_PIN 0x5
20+
21+
enum {
22+
PTP_PIN_ACTION_IDLE = 0,
23+
PTP_PIN_ACTION_LOAD,
24+
PTP_PIN_ACTION_SAVE,
25+
PTP_PIN_ACTION_CLOCK,
26+
PTP_PIN_ACTION_DELTA,
27+
PTP_PIN_ACTION_TOD
28+
};
29+
30+
static u64 lan966x_ptp_get_nominal_value(void)
31+
{
32+
u64 res = 0x304d2df1;
33+
34+
res <<= 32;
35+
return res;
36+
}
37+
38+
static int lan966x_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
39+
{
40+
struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
41+
struct lan966x *lan966x = phc->lan966x;
42+
unsigned long flags;
43+
bool neg_adj = 0;
44+
u64 tod_inc;
45+
u64 ref;
46+
47+
if (!scaled_ppm)
48+
return 0;
49+
50+
if (scaled_ppm < 0) {
51+
neg_adj = 1;
52+
scaled_ppm = -scaled_ppm;
53+
}
54+
55+
tod_inc = lan966x_ptp_get_nominal_value();
56+
57+
/* The multiplication is split in 2 separate additions because of
58+
* overflow issues. If scaled_ppm with 16bit fractional part was bigger
59+
* than 20ppm then we got overflow.
60+
*/
61+
ref = LAN966X_1PPM_FORMAT * (scaled_ppm >> 16);
62+
ref += (LAN966X_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
63+
tod_inc = neg_adj ? tod_inc - ref : tod_inc + ref;
64+
65+
spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
66+
67+
lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(1 << BIT(phc->index)),
68+
PTP_DOM_CFG_CLKCFG_DIS,
69+
lan966x, PTP_DOM_CFG);
70+
71+
lan_wr((u32)tod_inc & 0xFFFFFFFF, lan966x,
72+
PTP_CLK_PER_CFG(phc->index, 0));
73+
lan_wr((u32)(tod_inc >> 32), lan966x,
74+
PTP_CLK_PER_CFG(phc->index, 1));
75+
76+
lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0),
77+
PTP_DOM_CFG_CLKCFG_DIS,
78+
lan966x, PTP_DOM_CFG);
79+
80+
spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
81+
82+
return 0;
83+
}
84+
85+
static int lan966x_ptp_settime64(struct ptp_clock_info *ptp,
86+
const struct timespec64 *ts)
87+
{
88+
struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
89+
struct lan966x *lan966x = phc->lan966x;
90+
unsigned long flags;
91+
92+
spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
93+
94+
/* Must be in IDLE mode before the time can be loaded */
95+
lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) |
96+
PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
97+
PTP_PIN_CFG_PIN_SYNC_SET(0),
98+
PTP_PIN_CFG_PIN_ACTION |
99+
PTP_PIN_CFG_PIN_DOM |
100+
PTP_PIN_CFG_PIN_SYNC,
101+
lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
102+
103+
/* Set new value */
104+
lan_wr(PTP_TOD_SEC_MSB_TOD_SEC_MSB_SET(upper_32_bits(ts->tv_sec)),
105+
lan966x, PTP_TOD_SEC_MSB(TOD_ACC_PIN));
106+
lan_wr(lower_32_bits(ts->tv_sec),
107+
lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN));
108+
lan_wr(ts->tv_nsec, lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
109+
110+
/* Apply new values */
111+
lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_LOAD) |
112+
PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
113+
PTP_PIN_CFG_PIN_SYNC_SET(0),
114+
PTP_PIN_CFG_PIN_ACTION |
115+
PTP_PIN_CFG_PIN_DOM |
116+
PTP_PIN_CFG_PIN_SYNC,
117+
lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
118+
119+
spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
120+
121+
return 0;
122+
}
123+
124+
static int lan966x_ptp_gettime64(struct ptp_clock_info *ptp,
125+
struct timespec64 *ts)
126+
{
127+
struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
128+
struct lan966x *lan966x = phc->lan966x;
129+
unsigned long flags;
130+
time64_t s;
131+
s64 ns;
132+
133+
spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
134+
135+
lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_SAVE) |
136+
PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
137+
PTP_PIN_CFG_PIN_SYNC_SET(0),
138+
PTP_PIN_CFG_PIN_ACTION |
139+
PTP_PIN_CFG_PIN_DOM |
140+
PTP_PIN_CFG_PIN_SYNC,
141+
lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
142+
143+
s = lan_rd(lan966x, PTP_TOD_SEC_MSB(TOD_ACC_PIN));
144+
s <<= 32;
145+
s |= lan_rd(lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN));
146+
ns = lan_rd(lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
147+
ns &= PTP_TOD_NSEC_TOD_NSEC;
148+
149+
spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
150+
151+
/* Deal with negative values */
152+
if ((ns & 0xFFFFFFF0) == 0x3FFFFFF0) {
153+
s--;
154+
ns &= 0xf;
155+
ns += 999999984;
156+
}
157+
158+
set_normalized_timespec64(ts, s, ns);
159+
return 0;
160+
}
161+
162+
static int lan966x_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
163+
{
164+
struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
165+
struct lan966x *lan966x = phc->lan966x;
166+
167+
if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
168+
unsigned long flags;
169+
170+
spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
171+
172+
/* Must be in IDLE mode before the time can be loaded */
173+
lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) |
174+
PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
175+
PTP_PIN_CFG_PIN_SYNC_SET(0),
176+
PTP_PIN_CFG_PIN_ACTION |
177+
PTP_PIN_CFG_PIN_DOM |
178+
PTP_PIN_CFG_PIN_SYNC,
179+
lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
180+
181+
lan_wr(PTP_TOD_NSEC_TOD_NSEC_SET(delta),
182+
lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
183+
184+
/* Adjust time with the value of PTP_TOD_NSEC */
185+
lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_DELTA) |
186+
PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
187+
PTP_PIN_CFG_PIN_SYNC_SET(0),
188+
PTP_PIN_CFG_PIN_ACTION |
189+
PTP_PIN_CFG_PIN_DOM |
190+
PTP_PIN_CFG_PIN_SYNC,
191+
lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
192+
193+
spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
194+
} else {
195+
/* Fall back using lan966x_ptp_settime64 which is not exact */
196+
struct timespec64 ts;
197+
u64 now;
198+
199+
lan966x_ptp_gettime64(ptp, &ts);
200+
201+
now = ktime_to_ns(timespec64_to_ktime(ts));
202+
ts = ns_to_timespec64(now + delta);
203+
204+
lan966x_ptp_settime64(ptp, &ts);
205+
}
206+
207+
return 0;
208+
}
209+
210+
static struct ptp_clock_info lan966x_ptp_clock_info = {
211+
.owner = THIS_MODULE,
212+
.name = "lan966x ptp",
213+
.max_adj = 200000,
214+
.gettime64 = lan966x_ptp_gettime64,
215+
.settime64 = lan966x_ptp_settime64,
216+
.adjtime = lan966x_ptp_adjtime,
217+
.adjfine = lan966x_ptp_adjfine,
218+
};
219+
220+
static int lan966x_ptp_phc_init(struct lan966x *lan966x,
221+
int index,
222+
struct ptp_clock_info *clock_info)
223+
{
224+
struct lan966x_phc *phc = &lan966x->phc[index];
225+
226+
phc->info = *clock_info;
227+
phc->clock = ptp_clock_register(&phc->info, lan966x->dev);
228+
if (IS_ERR(phc->clock))
229+
return PTR_ERR(phc->clock);
230+
231+
phc->index = index;
232+
phc->lan966x = lan966x;
233+
234+
/* PTP Rx stamping is always enabled. */
235+
phc->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
236+
237+
return 0;
238+
}
239+
240+
int lan966x_ptp_init(struct lan966x *lan966x)
241+
{
242+
u64 tod_adj = lan966x_ptp_get_nominal_value();
243+
int err, i;
244+
245+
if (!lan966x->ptp)
246+
return 0;
247+
248+
for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
249+
err = lan966x_ptp_phc_init(lan966x, i, &lan966x_ptp_clock_info);
250+
if (err)
251+
return err;
252+
}
253+
254+
spin_lock_init(&lan966x->ptp_clock_lock);
255+
256+
/* Disable master counters */
257+
lan_wr(PTP_DOM_CFG_ENA_SET(0), lan966x, PTP_DOM_CFG);
258+
259+
/* Configure the nominal TOD increment per clock cycle */
260+
lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0x7),
261+
PTP_DOM_CFG_CLKCFG_DIS,
262+
lan966x, PTP_DOM_CFG);
263+
264+
for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
265+
lan_wr((u32)tod_adj & 0xFFFFFFFF, lan966x,
266+
PTP_CLK_PER_CFG(i, 0));
267+
lan_wr((u32)(tod_adj >> 32), lan966x,
268+
PTP_CLK_PER_CFG(i, 1));
269+
}
270+
271+
lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0),
272+
PTP_DOM_CFG_CLKCFG_DIS,
273+
lan966x, PTP_DOM_CFG);
274+
275+
/* Enable master counters */
276+
lan_wr(PTP_DOM_CFG_ENA_SET(0x7), lan966x, PTP_DOM_CFG);
277+
278+
return 0;
279+
}
280+
281+
void lan966x_ptp_deinit(struct lan966x *lan966x)
282+
{
283+
int i;
284+
285+
for (i = 0; i < LAN966X_PHC_COUNT; ++i)
286+
ptp_clock_unregister(lan966x->phc[i].clock);
287+
}

0 commit comments

Comments
 (0)