03 HWIF 硬件抽象层
源码:
hwif.c、hwif.h
关联:common.h(mac_device_info)
1. 模块职责
HWIF(Hardware Interface)层解决 Synopsys DWMAC 多版本共存 问题:
- 统一 MAC/DMA/描述符/PTP/TC/MMC 操作接口
- 根据芯片 ID 自动绑定对应 ops 实现
- 提供宏封装,核心代码无需
#ifdef版本分支
2. ops 结构体体系
定义于 hwif.h:
| 结构体 | 主要方法示例 |
|---|---|
stmmac_desc_ops |
init_rx_desc, prepare_tx_desc, rx_status, set_mss |
stmmac_dma_ops |
reset, start_tx/rx, set_tx_tail_ptr, enable_dma_irq |
stmmac_ops |
core_init, set_filter, flow_ctrl, rss_configure |
stmmac_hwtimestamp |
init_systime, adjust_systime, config_hw_tstamping |
stmmac_mode_ops |
init, refill_desc3, clean_desc3(ring/chain) |
stmmac_tc_ops |
setup_cls_u32, setup_cbs, setup_taprio |
stmmac_mmc_ops |
ctrl, read |
3. 宏调用机制
1 |
示例:
1 | stmmac_start_tx(priv, priv->ioaddr, chan); |
若 ops 或函数指针为 NULL,返回 -EINVAL,避免空指针崩溃。
4. 版本匹配表 stmmac_hw[]
hwif.c 中 stmmac_hwif_init() 遍历 stmmac_hw[](从新到旧匹配):
| 条目 | min_id | gmac4 | xgmac | MAC ops | DMA ops |
|---|---|---|---|---|---|
| DWXLGMAC2 | 0x20 | — | ✓ | dwxlgmac2_ops | dwxgmac210_dma_ops |
| DWXGMAC2.10 | 0x21 | — | ✓ | dwxgmac210_ops | dwxgmac210_dma_ops |
| DWMAC5.10 | 0x51 | ✓ | — | dwmac510_ops | dwmac410_dma_ops |
| DWMAC4.10+ | 0x41 | ✓ | — | dwmac410_ops | dwmac410_dma_ops |
| DWMAC4.00 | 0x40 | ✓ | — | dwmac4_ops | dwmac4_dma_ops |
| DWMAC1000 | 0x00 | — | — | dwmac1000_ops | dwmac1000_dma_ops |
| DWMAC100 | 0x00 | — | — | dwmac100_ops | dwmac100_dma_ops |
4.1 匹配条件
1 | needs_gmac ^ entry->gmac → skip |
平台通过 plat->has_gmac4 / has_xgmac 声明 IP 类型;RK3588 为 has_gmac4=true。
4.2 寄存器偏移
每条目含 stmmac_regs_off:
ptp_off:PTP 子模块相对基址偏移mmc_off:MMC 计数器偏移
GMAC4 使用 PTP_GMAC4_OFFSET / MMC_GMAC4_OFFSET。
5. stmmac_hwif_init() 流程
5.1 setup 函数
| setup | 文件 | 作用 |
|---|---|---|
dwmac4_setup |
dwmac4_lib.c | 填充 GMAC4 寄存器地址、过滤容量 |
dwmac1000_setup |
dwmac1000_core.c | GMAC3 配置 |
dwxgmac2_setup |
dwxgmac2_core.c | XGMAC 配置 |
5.2 quirks
| quirks | 作用 |
|---|---|
stmmac_dwmac1_quirks |
选择 norm/enh desc、ring/chain |
stmmac_dwmac4_quirks |
ring/chain 模式 |
stmmac_dwxlgmac_quirks |
设置 xlgmac 标志 |
6. mac_device_info 聚合
1 | struct mac_device_info { |
priv->hw 指向该结构,全驱动共享。
7. 与 stmmac_hw_init 协作
stmmac_hw_init()(stmmac_main.c)在 stmmac_hwif_init() 之后:
stmmac_get_hw_features()读 DMA capability 寄存器- 根据 capability 修正
plat->tx_coe、rx_coe、enh_desc - 执行
hwif_quirks()(若存在) - 决定是否启用 RIWT(RX 中断 watchdog)
8. 扩展新版本 IP
- 实现
*_ops结构体(mac/dma/desc) - 实现
*_setup()初始化mac_device_info - 在
stmmac_hw[]末尾追加条目(注释要求新版本放表尾) - 在
Makefile的CONFIG_STMMAC_FULL块加入.o文件
9. 调试
probe 日志示例:
1 | User ID: 0x0, Synopsys ID: 0x51 |
Synopsys ID 0x41 = Core 4.10,0x51 = Core 5.10。
正在加载留言…