07 PTP 与硬件时间戳
源码:
stmmac_ptp.c、stmmac_hwtstamp.c、stmmac_ptp.h
核心协作:stmmac_main.c(RX/TX 时间戳提取)
1. 模块职责
| 功能 | 说明 |
|---|---|
| PTP 时钟设备 | 向内核注册 /dev/ptpX,供 ptp4l 等同步 |
| 硬件 TX/RX 时间戳 | 精确到 ns 的报文时间标记 |
| ioctl 配置 | SIOCSHWTSTAMP / SIOCGHWTSTAMP |
2. 架构
3. PTP 时钟(stmmac_ptp.c)
3.1 注册
1 | stmmac_init_ptp() |
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_ptp(stmmac_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
stmmac_enable_tx_timestamp()在 descriptor 设 TS bit- 发送完成后
get_tx_timestamp_status()检查有效 stmmac_get_tx_hwtstamp()从 descriptor 或 MAC 寄存器取时间- 填入
skb_shinfo(skb)->tx_flags/hwtstamps
5.2 RX
rx_status/ extended status 指示 PTP 报文类型stmmac_get_rx_hwtstamp()解析 RDES 时间戳字段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_stats(ptp_rx_msg_type_*)。
7. RK3588 注意点
- 需
CONFIG_PTP_1588_CLOCK内核选项 - PTP 参考时钟由平台
clk_ptp_ref/ptp_clk_freq_config提供 ethtool -T eth0查看时间戳能力