07 PTP 与硬件时间戳

07 PTP 与硬件时间戳

源码:stmmac_ptp.cstmmac_hwtstamp.cstmmac_ptp.h
核心协作:stmmac_main.c(RX/TX 时间戳提取)

1. 模块职责

功能 说明
PTP 时钟设备 向内核注册 /dev/ptpX,供 ptp4l 等同步
硬件 TX/RX 时间戳 精确到 ns 的报文时间标记
ioctl 配置 SIOCSHWTSTAMP / SIOCGHWTSTAMP

2. 架构

ptp4l / chrony/dev/ptpXptp_clock_opsPTP 寄存器 via ptp opsskb hwtstampsstmmac_get_rx/tx_hwtstampdescriptor timestamp 域

3. PTP 时钟(stmmac_ptp.c)

3.1 注册

1
2
3
stmmac_init_ptp()
→ stmmac_init_tstamp_counter()
→ ptp_clock_register(&priv->ptp_clock_ops)

priv->ptp_clock 为内核 struct ptp_clock *

3.2 ptp_clock_ops

回调 作用
adjfreq stmmac_adjust_freq() — 调整 addend 微调频率
adjtime 步进调整系统时间
gettime64 读硬件时间
settime64 设置硬件时间
enable PPS 输出等

3.3 频率调整

通过 stmmac_config_addend() 修改 sub-nanosecond increment 寄存器,实现 ppb 级频率校正。

4. 硬件时间戳 ops

stmmac_hwtstamp.c 提供 stmmac_ptpstmmac_hwtimestamp 结构):

方法 说明
config_hw_tstamping 使能 TX/RX timestamp
config_sub_second_increment 亚秒增量
init_systime 初始化系统时间
adjust_systime 加减时间
get_systime 读取 64 位时间

PTP 寄存器基址:priv->ptpaddr = ioaddr + PTP_GMAC4_OFFSET(GMAC4)。

5. TX/RX 报文时间戳

5.1 TX

  1. stmmac_enable_tx_timestamp() 在 descriptor 设 TS bit
  2. 发送完成后 get_tx_timestamp_status() 检查有效
  3. stmmac_get_tx_hwtstamp() 从 descriptor 或 MAC 寄存器取时间
  4. 填入 skb_shinfo(skb)->tx_flags / hwtstamps

5.2 RX

  1. rx_status / extended status 指示 PTP 报文类型
  2. stmmac_get_rx_hwtstamp() 解析 RDES 时间戳字段
  3. skb_hwtstamps 交给上层

5.3 ioctl

stmmac_hwtstamp_set/get() 处理 struct hwtstamp_config

  • HWTSTAMP_FILTER_*:哪些报文打戳
  • tx_type / rx_filter:one-step / two-step 等

6. 与描述符关系

GMAC4 extended descriptor 含:

  • PTP 报文类型(Sync、Follow_Up、Delay_Req 等)
  • Timestamp valid / dropped 标志

统计计数在 stmmac_extra_statsptp_rx_msg_type_*)。

7. RK3588 注意点

  • CONFIG_PTP_1588_CLOCK 内核选项
  • PTP 参考时钟由平台 clk_ptp_ref / ptp_clk_freq_config 提供
  • ethtool -T eth0 查看时间戳能力

8. 相关文档

文章互动

阅读 --

留言

0 条留言

正在加载留言…