03 HWIF 硬件抽象层

03 HWIF 硬件抽象层

源码:hwif.chwif.h
关联:common.hmac_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
2
3
4
5
6
7
#define stmmac_do_callback(__priv, __module, __cname, __arg0, __args...) \
({ \
int __result = -EINVAL; \
if ((__priv)->hw->__module && (__priv)->hw->__module->__cname) \
__result = (__priv)->hw->__module->__cname((__arg0), ##__args); \
__result; \
})

示例:

1
2
stmmac_start_tx(priv, priv->ioaddr, chan);
// → priv->hw->dma->start_tx(priv->ioaddr, chan)

若 ops 或函数指针为 NULL,返回 -EINVAL,避免空指针崩溃。

4. 版本匹配表 stmmac_hw[]

hwif.cstmmac_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
2
3
4
5
needs_gmac   ^ entry->gmac    → skip
needs_gmac4 ^ entry->gmac4 → skip
needs_xgmac ^ entry->xgmac → skip
priv->synopsys_id < entry->min_id → skip
needs_xgmac && (dev_id ^ entry->dev_id) → 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() 流程

读 GMAC4_VERSION保存 synopsys_id"plat->setup?"平台自定义 mac_device_infodevm_kzalloc mac_device_info遍历 stmmac_hw[]绑定 desc/dma/mac/ptp/tc/mmcentry->setup(priv)保存 hwif_quirks

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
2
3
4
5
6
7
8
9
10
struct mac_device_info {
const struct stmmac_ops *mac;
const struct stmmac_desc_ops *desc;
const struct stmmac_dma_ops *dma;
const struct stmmac_mode_ops *mode;
const struct stmmac_hwtimestamp *ptp;
const struct stmmac_tc_ops *tc;
const struct stmmac_mmc_ops *mmc;
// 运行时字段:mii、link、vlan_filter、xpcs...
};

priv->hw 指向该结构,全驱动共享。

7. 与 stmmac_hw_init 协作

stmmac_hw_init()(stmmac_main.c)在 stmmac_hwif_init() 之后:

  1. stmmac_get_hw_features() 读 DMA capability 寄存器
  2. 根据 capability 修正 plat->tx_coerx_coeenh_desc
  3. 执行 hwif_quirks()(若存在)
  4. 决定是否启用 RIWT(RX 中断 watchdog)

8. 扩展新版本 IP

  1. 实现 *_ops 结构体(mac/dma/desc)
  2. 实现 *_setup() 初始化 mac_device_info
  3. stmmac_hw[] 末尾追加条目(注释要求新版本放表尾)
  4. MakefileCONFIG_STMMAC_FULL 块加入 .o 文件

9. 调试

probe 日志示例:

1
2
User ID: 0x0, Synopsys ID: 0x51
DMA HW capability register supported

Synopsys ID 0x41 = Core 4.10,0x51 = Core 5.10。

10. 相关文档

文章互动

阅读 --

留言

0 条留言

正在加载留言…