Commit 6605b730 authored by Frank Li's avatar Frank Li Committed by David S. Miller

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: default avatarFrank Li <Frank.Li@freescale.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent d6e0d9fc
...@@ -92,4 +92,13 @@ config GIANFAR ...@@ -92,4 +92,13 @@ config GIANFAR
This driver supports the Gigabit TSEC on the MPC83xx, MPC85xx, This driver supports the Gigabit TSEC on the MPC83xx, MPC85xx,
and MPC86xx family of chips, and the FEC on the 8540. and MPC86xx family of chips, and the FEC on the 8540.
config FEC_PTP
bool "PTP Hardware Clock (PHC)"
depends on FEC
select PPS
select PTP_1588_CLOCK
--help---
Say Y here if you want to use PTP Hardware Clock (PHC) in the
driver. Only the basic clock operations have been implemented.
endif # NET_VENDOR_FREESCALE endif # NET_VENDOR_FREESCALE
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# #
obj-$(CONFIG_FEC) += fec.o obj-$(CONFIG_FEC) += fec.o
obj-$(CONFIG_FEC_PTP) += fec_ptp.o
obj-$(CONFIG_FEC_MPC52xx) += fec_mpc52xx.o obj-$(CONFIG_FEC_MPC52xx) += fec_mpc52xx.o
ifeq ($(CONFIG_FEC_MPC52xx_MDIO),y) ifeq ($(CONFIG_FEC_MPC52xx_MDIO),y)
obj-$(CONFIG_FEC_MPC52xx) += fec_mpc52xx_phy.o obj-$(CONFIG_FEC_MPC52xx) += fec_mpc52xx_phy.o
......
...@@ -280,6 +280,17 @@ fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev) ...@@ -280,6 +280,17 @@ fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
| BD_ENET_TX_LAST | BD_ENET_TX_TC); | BD_ENET_TX_LAST | BD_ENET_TX_TC);
bdp->cbd_sc = status; bdp->cbd_sc = status;
#ifdef CONFIG_FEC_PTP
bdp->cbd_bdu = 0;
if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
fep->hwts_tx_en)) {
bdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
} else {
bdp->cbd_esc = BD_ENET_TX_INT;
}
#endif
/* Trigger transmission start */ /* Trigger transmission start */
writel(0, fep->hwp + FEC_X_DES_ACTIVE); writel(0, fep->hwp + FEC_X_DES_ACTIVE);
...@@ -437,10 +448,17 @@ fec_restart(struct net_device *ndev, int duplex) ...@@ -437,10 +448,17 @@ fec_restart(struct net_device *ndev, int duplex)
writel(1 << 8, fep->hwp + FEC_X_WMRK); writel(1 << 8, fep->hwp + FEC_X_WMRK);
} }
#ifdef CONFIG_FEC_PTP
ecntl |= (1 << 4);
#endif
/* And last, enable the transmit and receive processing */ /* And last, enable the transmit and receive processing */
writel(ecntl, fep->hwp + FEC_ECNTRL); writel(ecntl, fep->hwp + FEC_ECNTRL);
writel(0, fep->hwp + FEC_R_DES_ACTIVE); writel(0, fep->hwp + FEC_R_DES_ACTIVE);
#ifdef CONFIG_FEC_PTP
fec_ptp_start_cyclecounter(ndev);
#endif
/* Enable interrupts we wish to service */ /* Enable interrupts we wish to service */
writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
} }
...@@ -526,6 +544,19 @@ fec_enet_tx(struct net_device *ndev) ...@@ -526,6 +544,19 @@ fec_enet_tx(struct net_device *ndev)
ndev->stats.tx_packets++; ndev->stats.tx_packets++;
} }
#ifdef CONFIG_FEC_PTP
if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
struct skb_shared_hwtstamps shhwtstamps;
unsigned long flags;
memset(&shhwtstamps, 0, sizeof(shhwtstamps));
spin_lock_irqsave(&fep->tmreg_lock, flags);
shhwtstamps.hwtstamp = ns_to_ktime(
timecounter_cyc2time(&fep->tc, bdp->ts));
spin_unlock_irqrestore(&fep->tmreg_lock, flags);
skb_tstamp_tx(skb, &shhwtstamps);
}
#endif
if (status & BD_ENET_TX_READY) if (status & BD_ENET_TX_READY)
printk("HEY! Enet xmit interrupt and TX_READY.\n"); printk("HEY! Enet xmit interrupt and TX_READY.\n");
...@@ -652,6 +683,21 @@ fec_enet_rx(struct net_device *ndev) ...@@ -652,6 +683,21 @@ fec_enet_rx(struct net_device *ndev)
skb_put(skb, pkt_len - 4); /* Make room */ skb_put(skb, pkt_len - 4); /* Make room */
skb_copy_to_linear_data(skb, data, pkt_len - 4); skb_copy_to_linear_data(skb, data, pkt_len - 4);
skb->protocol = eth_type_trans(skb, ndev); skb->protocol = eth_type_trans(skb, ndev);
#ifdef CONFIG_FEC_PTP
/* Get receive timestamp from the skb */
if (fep->hwts_rx_en) {
struct skb_shared_hwtstamps *shhwtstamps =
skb_hwtstamps(skb);
unsigned long flags;
memset(shhwtstamps, 0, sizeof(*shhwtstamps));
spin_lock_irqsave(&fep->tmreg_lock, flags);
shhwtstamps->hwtstamp = ns_to_ktime(
timecounter_cyc2time(&fep->tc, bdp->ts));
spin_unlock_irqrestore(&fep->tmreg_lock, flags);
}
#endif
if (!skb_defer_rx_timestamp(skb)) if (!skb_defer_rx_timestamp(skb))
netif_rx(skb); netif_rx(skb);
} }
...@@ -666,6 +712,12 @@ fec_enet_rx(struct net_device *ndev) ...@@ -666,6 +712,12 @@ fec_enet_rx(struct net_device *ndev)
status |= BD_ENET_RX_EMPTY; status |= BD_ENET_RX_EMPTY;
bdp->cbd_sc = status; bdp->cbd_sc = status;
#ifdef CONFIG_FEC_PTP
bdp->cbd_esc = BD_ENET_RX_INT;
bdp->cbd_prot = 0;
bdp->cbd_bdu = 0;
#endif
/* Update BD pointer to next entry */ /* Update BD pointer to next entry */
if (status & BD_ENET_RX_WRAP) if (status & BD_ENET_RX_WRAP)
bdp = fep->rx_bd_base; bdp = fep->rx_bd_base;
...@@ -1105,6 +1157,10 @@ static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) ...@@ -1105,6 +1157,10 @@ static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
if (!phydev) if (!phydev)
return -ENODEV; return -ENODEV;
#ifdef CONFIG_FEC_PTP
if (cmd == SIOCSHWTSTAMP)
return fec_ptp_ioctl(ndev, rq, cmd);
#endif
return phy_mii_ioctl(phydev, rq, cmd); return phy_mii_ioctl(phydev, rq, cmd);
} }
...@@ -1151,6 +1207,9 @@ static int fec_enet_alloc_buffers(struct net_device *ndev) ...@@ -1151,6 +1207,9 @@ static int fec_enet_alloc_buffers(struct net_device *ndev)
bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data, bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE); FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
bdp->cbd_sc = BD_ENET_RX_EMPTY; bdp->cbd_sc = BD_ENET_RX_EMPTY;
#ifdef CONFIG_FEC_PTP
bdp->cbd_esc = BD_ENET_RX_INT;
#endif
bdp++; bdp++;
} }
...@@ -1164,6 +1223,10 @@ static int fec_enet_alloc_buffers(struct net_device *ndev) ...@@ -1164,6 +1223,10 @@ static int fec_enet_alloc_buffers(struct net_device *ndev)
bdp->cbd_sc = 0; bdp->cbd_sc = 0;
bdp->cbd_bufaddr = 0; bdp->cbd_bufaddr = 0;
#ifdef CONFIG_FEC_PTP
bdp->cbd_esc = BD_ENET_RX_INT;
#endif
bdp++; bdp++;
} }
...@@ -1565,9 +1628,19 @@ fec_probe(struct platform_device *pdev) ...@@ -1565,9 +1628,19 @@ fec_probe(struct platform_device *pdev)
goto failed_clk; goto failed_clk;
} }
#ifdef CONFIG_FEC_PTP
fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
if (IS_ERR(fep->clk_ptp)) {
ret = PTR_ERR(fep->clk_ptp);
goto failed_clk;
}
#endif
clk_prepare_enable(fep->clk_ahb); clk_prepare_enable(fep->clk_ahb);
clk_prepare_enable(fep->clk_ipg); clk_prepare_enable(fep->clk_ipg);
#ifdef CONFIG_FEC_PTP
clk_prepare_enable(fep->clk_ptp);
#endif
reg_phy = devm_regulator_get(&pdev->dev, "phy"); reg_phy = devm_regulator_get(&pdev->dev, "phy");
if (!IS_ERR(reg_phy)) { if (!IS_ERR(reg_phy)) {
ret = regulator_enable(reg_phy); ret = regulator_enable(reg_phy);
...@@ -1595,6 +1668,10 @@ fec_probe(struct platform_device *pdev) ...@@ -1595,6 +1668,10 @@ fec_probe(struct platform_device *pdev)
if (ret) if (ret)
goto failed_register; goto failed_register;
#ifdef CONFIG_FEC_PTP
fec_ptp_init(ndev, pdev);
#endif
return 0; return 0;
failed_register: failed_register:
...@@ -1604,6 +1681,9 @@ fec_probe(struct platform_device *pdev) ...@@ -1604,6 +1681,9 @@ fec_probe(struct platform_device *pdev)
failed_regulator: failed_regulator:
clk_disable_unprepare(fep->clk_ahb); clk_disable_unprepare(fep->clk_ahb);
clk_disable_unprepare(fep->clk_ipg); clk_disable_unprepare(fep->clk_ipg);
#ifdef CONFIG_FEC_PTP
clk_disable_unprepare(fep->clk_ptp);
#endif
failed_pin: failed_pin:
failed_clk: failed_clk:
for (i = 0; i < FEC_IRQ_NUM; i++) { for (i = 0; i < FEC_IRQ_NUM; i++) {
...@@ -1636,6 +1716,12 @@ fec_drv_remove(struct platform_device *pdev) ...@@ -1636,6 +1716,12 @@ fec_drv_remove(struct platform_device *pdev)
if (irq > 0) if (irq > 0)
free_irq(irq, ndev); free_irq(irq, ndev);
} }
#ifdef CONFIG_FEC_PTP
del_timer_sync(&fep->time_keep);
clk_disable_unprepare(fep->clk_ptp);
if (fep->ptp_clock)
ptp_clock_unregister(fep->ptp_clock);
#endif
clk_disable_unprepare(fep->clk_ahb); clk_disable_unprepare(fep->clk_ahb);
clk_disable_unprepare(fep->clk_ipg); clk_disable_unprepare(fep->clk_ipg);
iounmap(fep->hwp); iounmap(fep->hwp);
......
...@@ -13,6 +13,12 @@ ...@@ -13,6 +13,12 @@
#define FEC_H #define FEC_H
/****************************************************************************/ /****************************************************************************/
#ifdef CONFIG_FEC_PTP
#include <linux/clocksource.h>
#include <linux/net_tstamp.h>
#include <linux/ptp_clock_kernel.h>
#endif
#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
defined(CONFIG_M520x) || defined(CONFIG_M532x) || \ defined(CONFIG_M520x) || defined(CONFIG_M532x) || \
defined(CONFIG_ARCH_MXC) || defined(CONFIG_SOC_IMX28) defined(CONFIG_ARCH_MXC) || defined(CONFIG_SOC_IMX28)
...@@ -88,6 +94,13 @@ struct bufdesc { ...@@ -88,6 +94,13 @@ struct bufdesc {
unsigned short cbd_datlen; /* Data length */ unsigned short cbd_datlen; /* Data length */
unsigned short cbd_sc; /* Control and status info */ unsigned short cbd_sc; /* Control and status info */
unsigned long cbd_bufaddr; /* Buffer address */ unsigned long cbd_bufaddr; /* Buffer address */
#ifdef CONFIG_FEC_PTP
unsigned long cbd_esc;
unsigned long cbd_prot;
unsigned long cbd_bdu;
unsigned long ts;
unsigned short res0[4];
#endif
}; };
#else #else
struct bufdesc { struct bufdesc {
...@@ -190,6 +203,9 @@ struct fec_enet_private { ...@@ -190,6 +203,9 @@ struct fec_enet_private {
struct clk *clk_ipg; struct clk *clk_ipg;
struct clk *clk_ahb; struct clk *clk_ahb;
#ifdef CONFIG_FEC_PTP
struct clk *clk_ptp;
#endif
/* The saved address of a sent-in-place packet/buffer, for skfree(). */ /* The saved address of a sent-in-place packet/buffer, for skfree(). */
unsigned char *tx_bounce[TX_RING_SIZE]; unsigned char *tx_bounce[TX_RING_SIZE];
...@@ -227,7 +243,29 @@ struct fec_enet_private { ...@@ -227,7 +243,29 @@ struct fec_enet_private {
int full_duplex; int full_duplex;
struct completion mdio_done; struct completion mdio_done;
int irq[FEC_IRQ_NUM]; int irq[FEC_IRQ_NUM];
#ifdef CONFIG_FEC_PTP
struct ptp_clock *ptp_clock;
struct ptp_clock_info ptp_caps;
unsigned long last_overflow_check;
spinlock_t tmreg_lock;
struct cyclecounter cc;
struct timecounter tc;
int rx_hwtstamp_filter;
u32 base_incval;
u32 cycle_speed;
int hwts_rx_en;
int hwts_tx_en;
struct timer_list time_keep;
#endif
}; };
#ifdef CONFIG_FEC_PTP
void fec_ptp_init(struct net_device *ndev, struct platform_device *pdev);
void fec_ptp_start_cyclecounter(struct net_device *ndev);
int fec_ptp_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd);
#endif
/****************************************************************************/ /****************************************************************************/
#endif /* FEC_H */ #endif /* FEC_H */
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment