Skip to content

Commit 6605b73

Browse files
nxpfranklidavem330
authored andcommitted
FEC: Add time stamping code and a PTP hardware clock
This patch adds a driver for the FEC(MX6) that offers time stamping and a PTP haderware clock. Because FEC\ENET(MX6) hardware frequency adjustment is complex, we have implemented this in software by changing the multiplication factor of the timecounter. Signed-off-by: Frank Li <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d6e0d9f commit 6605b73

File tree

5 files changed

+520
-1
lines changed

5 files changed

+520
-1
lines changed

drivers/net/ethernet/freescale/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,13 @@ config GIANFAR
9292
This driver supports the Gigabit TSEC on the MPC83xx, MPC85xx,
9393
and MPC86xx family of chips, and the FEC on the 8540.
9494

95+
config FEC_PTP
96+
bool "PTP Hardware Clock (PHC)"
97+
depends on FEC
98+
select PPS
99+
select PTP_1588_CLOCK
100+
--help---
101+
Say Y here if you want to use PTP Hardware Clock (PHC) in the
102+
driver. Only the basic clock operations have been implemented.
103+
95104
endif # NET_VENDOR_FREESCALE

drivers/net/ethernet/freescale/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44

55
obj-$(CONFIG_FEC) += fec.o
6+
obj-$(CONFIG_FEC_PTP) += fec_ptp.o
67
obj-$(CONFIG_FEC_MPC52xx) += fec_mpc52xx.o
78
ifeq ($(CONFIG_FEC_MPC52xx_MDIO),y)
89
obj-$(CONFIG_FEC_MPC52xx) += fec_mpc52xx_phy.o

drivers/net/ethernet/freescale/fec.c

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,17 @@ fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
280280
| BD_ENET_TX_LAST | BD_ENET_TX_TC);
281281
bdp->cbd_sc = status;
282282

283+
#ifdef CONFIG_FEC_PTP
284+
bdp->cbd_bdu = 0;
285+
if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
286+
fep->hwts_tx_en)) {
287+
bdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
288+
skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
289+
} else {
290+
291+
bdp->cbd_esc = BD_ENET_TX_INT;
292+
}
293+
#endif
283294
/* Trigger transmission start */
284295
writel(0, fep->hwp + FEC_X_DES_ACTIVE);
285296

@@ -437,10 +448,17 @@ fec_restart(struct net_device *ndev, int duplex)
437448
writel(1 << 8, fep->hwp + FEC_X_WMRK);
438449
}
439450

451+
#ifdef CONFIG_FEC_PTP
452+
ecntl |= (1 << 4);
453+
#endif
454+
440455
/* And last, enable the transmit and receive processing */
441456
writel(ecntl, fep->hwp + FEC_ECNTRL);
442457
writel(0, fep->hwp + FEC_R_DES_ACTIVE);
443458

459+
#ifdef CONFIG_FEC_PTP
460+
fec_ptp_start_cyclecounter(ndev);
461+
#endif
444462
/* Enable interrupts we wish to service */
445463
writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
446464
}
@@ -526,6 +544,19 @@ fec_enet_tx(struct net_device *ndev)
526544
ndev->stats.tx_packets++;
527545
}
528546

547+
#ifdef CONFIG_FEC_PTP
548+
if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
549+
struct skb_shared_hwtstamps shhwtstamps;
550+
unsigned long flags;
551+
552+
memset(&shhwtstamps, 0, sizeof(shhwtstamps));
553+
spin_lock_irqsave(&fep->tmreg_lock, flags);
554+
shhwtstamps.hwtstamp = ns_to_ktime(
555+
timecounter_cyc2time(&fep->tc, bdp->ts));
556+
spin_unlock_irqrestore(&fep->tmreg_lock, flags);
557+
skb_tstamp_tx(skb, &shhwtstamps);
558+
}
559+
#endif
529560
if (status & BD_ENET_TX_READY)
530561
printk("HEY! Enet xmit interrupt and TX_READY.\n");
531562

@@ -652,6 +683,21 @@ fec_enet_rx(struct net_device *ndev)
652683
skb_put(skb, pkt_len - 4); /* Make room */
653684
skb_copy_to_linear_data(skb, data, pkt_len - 4);
654685
skb->protocol = eth_type_trans(skb, ndev);
686+
#ifdef CONFIG_FEC_PTP
687+
/* Get receive timestamp from the skb */
688+
if (fep->hwts_rx_en) {
689+
struct skb_shared_hwtstamps *shhwtstamps =
690+
skb_hwtstamps(skb);
691+
unsigned long flags;
692+
693+
memset(shhwtstamps, 0, sizeof(*shhwtstamps));
694+
695+
spin_lock_irqsave(&fep->tmreg_lock, flags);
696+
shhwtstamps->hwtstamp = ns_to_ktime(
697+
timecounter_cyc2time(&fep->tc, bdp->ts));
698+
spin_unlock_irqrestore(&fep->tmreg_lock, flags);
699+
}
700+
#endif
655701
if (!skb_defer_rx_timestamp(skb))
656702
netif_rx(skb);
657703
}
@@ -666,6 +712,12 @@ fec_enet_rx(struct net_device *ndev)
666712
status |= BD_ENET_RX_EMPTY;
667713
bdp->cbd_sc = status;
668714

715+
#ifdef CONFIG_FEC_PTP
716+
bdp->cbd_esc = BD_ENET_RX_INT;
717+
bdp->cbd_prot = 0;
718+
bdp->cbd_bdu = 0;
719+
#endif
720+
669721
/* Update BD pointer to next entry */
670722
if (status & BD_ENET_RX_WRAP)
671723
bdp = fep->rx_bd_base;
@@ -1105,6 +1157,10 @@ static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
11051157
if (!phydev)
11061158
return -ENODEV;
11071159

1160+
#ifdef CONFIG_FEC_PTP
1161+
if (cmd == SIOCSHWTSTAMP)
1162+
return fec_ptp_ioctl(ndev, rq, cmd);
1163+
#endif
11081164
return phy_mii_ioctl(phydev, rq, cmd);
11091165
}
11101166

@@ -1151,6 +1207,9 @@ static int fec_enet_alloc_buffers(struct net_device *ndev)
11511207
bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
11521208
FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
11531209
bdp->cbd_sc = BD_ENET_RX_EMPTY;
1210+
#ifdef CONFIG_FEC_PTP
1211+
bdp->cbd_esc = BD_ENET_RX_INT;
1212+
#endif
11541213
bdp++;
11551214
}
11561215

@@ -1164,6 +1223,10 @@ static int fec_enet_alloc_buffers(struct net_device *ndev)
11641223

11651224
bdp->cbd_sc = 0;
11661225
bdp->cbd_bufaddr = 0;
1226+
1227+
#ifdef CONFIG_FEC_PTP
1228+
bdp->cbd_esc = BD_ENET_RX_INT;
1229+
#endif
11671230
bdp++;
11681231
}
11691232

@@ -1565,9 +1628,19 @@ fec_probe(struct platform_device *pdev)
15651628
goto failed_clk;
15661629
}
15671630

1631+
#ifdef CONFIG_FEC_PTP
1632+
fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
1633+
if (IS_ERR(fep->clk_ptp)) {
1634+
ret = PTR_ERR(fep->clk_ptp);
1635+
goto failed_clk;
1636+
}
1637+
#endif
1638+
15681639
clk_prepare_enable(fep->clk_ahb);
15691640
clk_prepare_enable(fep->clk_ipg);
1570-
1641+
#ifdef CONFIG_FEC_PTP
1642+
clk_prepare_enable(fep->clk_ptp);
1643+
#endif
15711644
reg_phy = devm_regulator_get(&pdev->dev, "phy");
15721645
if (!IS_ERR(reg_phy)) {
15731646
ret = regulator_enable(reg_phy);
@@ -1595,6 +1668,10 @@ fec_probe(struct platform_device *pdev)
15951668
if (ret)
15961669
goto failed_register;
15971670

1671+
#ifdef CONFIG_FEC_PTP
1672+
fec_ptp_init(ndev, pdev);
1673+
#endif
1674+
15981675
return 0;
15991676

16001677
failed_register:
@@ -1604,6 +1681,9 @@ fec_probe(struct platform_device *pdev)
16041681
failed_regulator:
16051682
clk_disable_unprepare(fep->clk_ahb);
16061683
clk_disable_unprepare(fep->clk_ipg);
1684+
#ifdef CONFIG_FEC_PTP
1685+
clk_disable_unprepare(fep->clk_ptp);
1686+
#endif
16071687
failed_pin:
16081688
failed_clk:
16091689
for (i = 0; i < FEC_IRQ_NUM; i++) {
@@ -1636,6 +1716,12 @@ fec_drv_remove(struct platform_device *pdev)
16361716
if (irq > 0)
16371717
free_irq(irq, ndev);
16381718
}
1719+
#ifdef CONFIG_FEC_PTP
1720+
del_timer_sync(&fep->time_keep);
1721+
clk_disable_unprepare(fep->clk_ptp);
1722+
if (fep->ptp_clock)
1723+
ptp_clock_unregister(fep->ptp_clock);
1724+
#endif
16391725
clk_disable_unprepare(fep->clk_ahb);
16401726
clk_disable_unprepare(fep->clk_ipg);
16411727
iounmap(fep->hwp);

drivers/net/ethernet/freescale/fec.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
#define FEC_H
1414
/****************************************************************************/
1515

16+
#ifdef CONFIG_FEC_PTP
17+
#include <linux/clocksource.h>
18+
#include <linux/net_tstamp.h>
19+
#include <linux/ptp_clock_kernel.h>
20+
#endif
21+
1622
#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
1723
defined(CONFIG_M520x) || defined(CONFIG_M532x) || \
1824
defined(CONFIG_ARCH_MXC) || defined(CONFIG_SOC_IMX28)
@@ -88,6 +94,13 @@ struct bufdesc {
8894
unsigned short cbd_datlen; /* Data length */
8995
unsigned short cbd_sc; /* Control and status info */
9096
unsigned long cbd_bufaddr; /* Buffer address */
97+
#ifdef CONFIG_FEC_PTP
98+
unsigned long cbd_esc;
99+
unsigned long cbd_prot;
100+
unsigned long cbd_bdu;
101+
unsigned long ts;
102+
unsigned short res0[4];
103+
#endif
91104
};
92105
#else
93106
struct bufdesc {
@@ -190,6 +203,9 @@ struct fec_enet_private {
190203

191204
struct clk *clk_ipg;
192205
struct clk *clk_ahb;
206+
#ifdef CONFIG_FEC_PTP
207+
struct clk *clk_ptp;
208+
#endif
193209

194210
/* The saved address of a sent-in-place packet/buffer, for skfree(). */
195211
unsigned char *tx_bounce[TX_RING_SIZE];
@@ -227,7 +243,29 @@ struct fec_enet_private {
227243
int full_duplex;
228244
struct completion mdio_done;
229245
int irq[FEC_IRQ_NUM];
246+
247+
#ifdef CONFIG_FEC_PTP
248+
struct ptp_clock *ptp_clock;
249+
struct ptp_clock_info ptp_caps;
250+
unsigned long last_overflow_check;
251+
spinlock_t tmreg_lock;
252+
struct cyclecounter cc;
253+
struct timecounter tc;
254+
int rx_hwtstamp_filter;
255+
u32 base_incval;
256+
u32 cycle_speed;
257+
int hwts_rx_en;
258+
int hwts_tx_en;
259+
struct timer_list time_keep;
260+
#endif
261+
230262
};
231263

264+
#ifdef CONFIG_FEC_PTP
265+
void fec_ptp_init(struct net_device *ndev, struct platform_device *pdev);
266+
void fec_ptp_start_cyclecounter(struct net_device *ndev);
267+
int fec_ptp_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd);
268+
#endif
269+
232270
/****************************************************************************/
233271
#endif /* FEC_H */

0 commit comments

Comments
 (0)