emac驱动
emac驱动作为以太网的mac驱动,会做一些工具的初始化,以及接发数据的接口,和mdio总线的注册
emac_probe
ndev = alloc_etherdev
ndev->ethtool_ops = &emac_ethtool_ops; // 供上层ethtool工具使用
ndev->netdev_ops = &emac_netdev_ops; //里面有发送数据的接口
.ndo_start_xmit = emac_start_xmit, //核心调用这个发送数据时,发送完成后触发中断,去解除dma映射
emac_mdio_init
register_netdev
netif_napi_add(ndev, &priv->napi, emac_rx_poll, 32); //通过napi来接受数据,在中断处理函数中接受到中断后,就会关掉中断,调度一次emac_rx_poll,emac_rx_poll直到rx_buffer空了,再开中断
mdio总线
简单看下mdio的初始化干了什么,主要就是实现mdio的读写函数,以及对挂在mdio总线的设备就,比如phy,switch设备进行创建和注册
emac_mdio_init
mii->write = emac_mii_write
of_mdiobus_register
mdiobus_register
of_mdiobus_register_phy
比如这个emac_mdio_init,去到emac节点里找到“mdio-bus”这个属性,然后申请一个mii成员,将emac驱动里面实现的成员函数赋值给mii的成员,然后再通过of_mdiobus_register,将拿到的这个mdio-bus节点,去注册总线和挂在上面的设备
static int emac_mdio_init(struct emac_priv *priv)
{
struct device_node *mii_np;
struct device *dev = &priv->pdev->dev;
int ret;
mii_np = of_get_child_by_name(dev->of_node, "mdio-bus");
if (!mii_np) {
dev_err(dev, "no %s child node found", "mdio-bus");
return -ENODEV;
}
if (!of_device_is_available(mii_np)) {
ret = -ENODEV;
goto err_put_node;
}
priv->mii = mdiobus_alloc();//devm_mdiobus_alloc(dev);
if (!priv->mii) {
ret = -ENOMEM;
goto err_put_node;
}
priv->mii->priv = priv;
priv->mii->irq = priv->mdio_irqs;
priv->mii->name = "emac mii";
priv->mii->reset = emac_mii_reset;
priv->mii->read = emac_mii_read;
priv->mii->write = emac_mii_write;
snprintf(priv->mii->id, MII_BUS_ID_SIZE, "%pOFn",
mii_np);
priv->mii->parent = dev;
priv->mii->phy_mask = 0xffffffff;
ret = of_mdiobus_register(priv->mii, mii_np);
err_put_node:
of_node_put(mii_np);
return ret;
}
写操作其实就是对应的一些MDIO相关寄存器的读写操作
static int emac_mii_write(struct mii_bus *bus, int phy_addr, int regnum,
u16 value)
{
struct emac_priv *priv = bus->priv;
u32 cmd = 0;
u32 val;
emac_wr(priv, MAC_MDIO_DATA, value);
cmd |= phy_addr & 0x1F;
cmd |= (regnum & 0x1F) << 5;
cmd |= MREGBIT_START_MDIO_TRANS;
/*
* MDC/MDIO clock is from AXI, add qos to avoid MDC frequency
* change during MDIO read/write
*/
pm_qos_update_request(&priv->pm_ddr_qos, INT_MAX);
emac_wr(priv, MAC_MDIO_CONTROL, cmd);
val = emac_rd(priv, MAC_MDIO_CONTROL);
val = val >> 15 & 0x01;
while (val != 0) {
val = emac_rd(priv, MAC_MDIO_CONTROL);
val = val >> 15 & 0x01;
}
pm_qos_update_request(&priv->pm_ddr_qos, PM_QOS_DEFAULT_VALUE);
return 0;
}
将成员函数填充后,生成一个mdio控制器,可以由mac驱动或者bitbang去调用生成,由于下面phy_mask = ~0; 所以mdiobus_register里面就不会去扫描所有phy了
int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
{
struct device_node *child;
bool scanphys = false;
int addr, rc;
if (!np)
return mdiobus_register(mdio);
/* Do not continue if the node is disabled */
if (!of_device_is_available(np))
return -ENODEV;
/* Mask out all PHYs from auto probing. Instead the PHYs listed in
* the device tree are populated after the bus has been registered */
mdio->phy_mask = ~0;
mdio->dev.of_node = np;
mdio->dev.fwnode = of_fwnode_handle(np);
/* Get bus level PHY reset GPIO details */
mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
/* Register the MDIO bus */
rc = mdiobus_register(mdio);
if (rc)
return rc;
/* Loop over the child nodes and register a phy_device for each phy */
for_each_available_child_of_node(np, child) {
addr = of_mdio_parse_addr(&mdio->dev, child);
if (addr < 0) {
scanphys = true;
continue;
}
if (of_mdiobus_child_is_phy(child))
rc = of_mdiobus_register_phy(mdio, child, addr); //会将mdio节点的子节点进行解析注册
else
rc = of_mdiobus_register_device(mdio, child, addr);
if (rc == -ENODEV)
dev_err(&mdio->dev,
"MDIO device at address %d is missing.\n",
addr);
else if (rc)
goto unregister;
}
if (!scanphys)
return 0;
/* auto scan for PHYs with empty reg property */
for_each_available_child_of_node(np, child) {
/* Skip PHYs with reg property set */
if (of_find_property(child, "reg", NULL))
continue;
for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
/* skip already registered PHYs */
if (mdiobus_is_registered_device(mdio, addr))
continue;
/* be noisy to encourage people to set reg property */
dev_info(&mdio->dev, "scan phy %pOFn at address %i\n",
child, addr);
if (of_mdiobus_child_is_phy(child)) {
/* -ENODEV is the return code that PHYLIB has
* standardized on to indicate that bus
* scanning should continue.
*/
rc = of_mdiobus_register_phy(mdio, child, addr);
if (!rc)
break;
if (rc != -ENODEV)
goto unregister;
}
}
}
return 0;
unregister:
of_node_put(child);
mdiobus_unregister(mdio);
return rc;
}
EXPORT_SYMBOL(of_mdiobus_register);
注册mdio总线,后续的phy设备都会在这个总线下面
#define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
int __mdiobus_register(struct mii_bus *bus, struct module *owner)
{
struct mdio_device *mdiodev;
int i, err;
struct gpio_desc *gpiod;
if (NULL == bus || NULL == bus->name ||
NULL == bus->read || NULL == bus->write)
return -EINVAL;
BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
bus->state != MDIOBUS_UNREGISTERED);
bus->owner = owner;
bus->dev.parent = bus->parent;
bus->dev.class = &mdio_bus_class;
bus->dev.groups = NULL;
dev_set_name(&bus->dev, "%s", bus->id);
/* We need to set state to MDIOBUS_UNREGISTERED to correctly release
* the device in mdiobus_free()
*
* State will be updated later in this function in case of success
*/
bus->state = MDIOBUS_UNREGISTERED;
err = device_register(&bus->dev);
if (err) {
pr_err("mii_bus %s failed to register\n", bus->id);
return -EINVAL;
}
mutex_init(&bus->mdio_lock);
/* de-assert bus level PHY GPIO reset */
gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_LOW);
if (IS_ERR(gpiod)) {
dev_err(&bus->dev, "mii_bus %s couldn't get reset GPIO\n",
bus->id);
device_del(&bus->dev);
return PTR_ERR(gpiod);
} else if (gpiod) {
bus->reset_gpiod = gpiod;
gpiod_set_value_cansleep(gpiod, 1);
udelay(bus->reset_delay_us);
gpiod_set_value_cansleep(gpiod, 0);
}
if (bus->reset)
bus->reset(bus);
for (i = 0; i < PHY_MAX_ADDR; i++) {
if ((bus->phy_mask & BIT(i)) == 0) {
struct phy_device *phydev;
phydev = mdiobus_scan(bus, i);
if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
err = PTR_ERR(phydev);
goto error;
}
}
}
mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
bus->state = MDIOBUS_REGISTERED;
dev_dbg(&bus->dev, "probed\n");
return 0;
error:
while (--i >= 0) {
mdiodev = bus->mdio_map[i];
if (!mdiodev)
continue;
mdiodev->device_remove(mdiodev);
mdiodev->device_free(mdiodev);
}
/* Put PHYs in RESET to save power */
if (bus->reset_gpiod)
gpiod_set_value_cansleep(bus->reset_gpiod, 1);
device_del(&bus->dev);
return err;
}
EXPORT_SYMBOL(__mdiobus_register);
phy设备
如果直接走mdiobus_register来注册,就会去轮询执行mdiobbus_sacn来扫描phy设备
struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
{
struct phy_device *phydev;
int err;
phydev = get_phy_device(bus, addr, false); //get_phy_id -> mdiobus_read-> mii_bus->read -> emac_mii_read
if (IS_ERR(phydev))
return phydev;
/*
* For DT, see if the auto-probed phy has a correspoding child
* in the bus node, and set the of_node pointer in this case.
*/
of_mdiobus_link_mdiodev(bus, &phydev->mdio);
err = phy_device_register(phydev);
if (err) {
phy_device_free(phydev);
return ERR_PTR(-ENODEV);
}
return phydev;
}
注册phy设备,switch走phy_driver的话,就去设备树添加对应mdio的子节点
(1). 先通过of_get_phy_id找看看mdio子节点的compatible属性,有没有id字符串,有的话直接创建设备
(2). 没有id字符串,就去通过get_phy_id去根据子节点的地址属性,去读id;读到id则创建phy设备;否则返回失败
static int of_mdiobus_register_phy(struct mii_bus *mdio,
struct device_node *child, u32 addr)
{
struct phy_device *phy;
bool is_c45;
int rc;
u32 phy_id;
is_c45 = of_device_is_compatible(child,
"ethernet-phy-ieee802.3-c45");
if (!is_c45 && !of_get_phy_id(child, &phy_id))
phy = phy_device_create(mdio, addr, phy_id, 0, NULL); //switch一般走这:比如compatible属性有这种"ethernet-phy-id001c.c943"
else
phy = get_phy_device(mdio, addr, is_c45); //phy一般走这
if (IS_ERR(phy))
return PTR_ERR(phy);
rc = of_irq_get(child, 0);
if (rc == -EPROBE_DEFER) {
phy_device_free(phy);
return rc;
}
if (rc > 0) {
phy->irq = rc;
mdio->irq[addr] = rc;
} else {
phy->irq = mdio->irq[addr];
}
if (of_property_read_bool(child, "broken-turn-around"))
mdio->phy_ignore_ta_mask |= 1 << addr;
of_property_read_u32(child, "reset-assert-us",
&phy->mdio.reset_assert_delay);
of_property_read_u32(child, "reset-deassert-us",
&phy->mdio.reset_deassert_delay);
/* Associate the OF node with the device structure so it
* can be looked up later */
of_node_get(child);
phy->mdio.dev.of_node = child;
phy->mdio.dev.fwnode = of_fwnode_handle(child);
/* All data is now stored in the phy struct;
* register it */
rc = phy_device_register(phy);
if (rc) {
phy_device_free(phy);
of_node_put(child);
return rc;
}
dev_dbg(&mdio->dev, "registered phy %pOFn at address %i\n",
child, addr);
return 0;
}
按设备树里的phy子节点生成phy设备的ID
static int of_get_phy_id(struct device_node *device, u32 *phy_id)
{
struct property *prop;
const char *cp;
unsigned int upper, lower;
of_property_for_each_string(device, "compatible", prop, cp) {
if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {
*phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
return 0;
}
}
return -EINVAL;
}
以下是三种方式生成mdio子设备
1.比如添加一个platform设备树节点,在paltfoem驱动中去register_switch,然后通过swconfig去配置vlan
switch: rtk_switch@0 {
compatible = "realtek,rtl8304";
mii-bus = <&mdio>;
rtl,ethernet = <ð0>;
status = "okay";
};
2.手动去创建,注册一个phy_device,匹配phy驱动去初始化switch,在上层通过vconfig去配置vlan
phy_device_create(priv->mii,29,0x001cc943,0,NULL);
phy_device_register(phydev);if( ret)
3.phy设备节点添加一个phy节点
/* enable fix link for ethernet switch */
fixed-link {
speed = <1000>;
full-duplex;
phy-mode = "rgmii";
};
mdio: mdio-bus {
#address-cells = <0x1>;
#size-cells = <0x0>;
/* YT8521 10M/100M/100OM 1.8V RGMII PHY */
phy0: phy@0 {
compatible = "ethernet-phy-ieee802.3-c22";
device_type = "ethernet-phy";
reg = <0x0>; /* set phy address*/
phy-mode = "rgmii";
tx_rx_delay = <0x0 0x0>; /* 150ps per step*/
};
/* rtl8367 RGMII switch */
phy29: phy@29 {
compatible = "ethernet-phy-id001c.c943","ethernet-phy-ieee802.3-c22";
device_type = "ethernet-phy";
reg = <0x1d>; /* set phy address*/
phy-mode = "rgmii";
tx_rx_delay = <0x1 0x3>; /* 150ps per step*/
}
}
设备跟驱动的匹配
设备注册后,就是跟驱动的匹配了
static int mdio_bus_match(struct device *dev, struct device_driver *drv)
{
struct mdio_device *mdio = to_mdio_device(dev);
if (of_driver_match_device(dev, drv))
return 1;
if (mdio->bus_match)
return mdio->bus_match(dev, drv);
return 0;
}
//一般就是phy_device的phy_id跟phy_driver的phy_id去比较
static int phy_bus_match(struct device *dev, struct device_driver *drv)
{
struct phy_device *phydev = to_phy_device(dev);
struct phy_driver *phydrv = to_phy_driver(drv);
const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
int i;
if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
return 0;
if (phydrv->match_phy_device)
return phydrv->match_phy_device(phydev);
if (phydev->is_c45) {
for (i = 1; i < num_ids; i++) {
if (phydev->c45_ids.device_ids[i] == 0xffffffff)
continue;
if ((phydrv->phy_id & phydrv->phy_id_mask) ==
(phydev->c45_ids.device_ids[i] &
phydrv->phy_id_mask))
return 1;
}
return 0;
} else {
return (phydrv->phy_id & phydrv->phy_id_mask) ==
(phydev->phy_id & phydrv->phy_id_mask);
}
}
phy驱动
再给一个phy_driver的实例
#define PHY_ID_RTL836X 0x001cc943
static struct phy_driver r8367_driver[] = {
{
PHY_ID_MATCH_MODEL(PHY_ID_RTL836X),
.name = "Realtek RTL836X",
.phy_id_mask = 0xffffffff,
.probe = r836x_probe,
.soft_reset = r836x_soft_reset,
.config_init = r836x_config_init,
.features = PHY_GBIT_FEATURES,
.flags = 0,
.config_aneg = r836x_config_aneg,
.read_status = r836x_read_status,
.aneg_done = r836x_aneg_done,
.remove = r836x_remove,
}};
module_phy_driver(r8367_driver);
调试步骤
最后给个简单的调试步骤
1.mdio总线不通:
量信号,如果没有信号,判断pinctrl的工作状态,是否被选择到rgmii接口了
2.chip id读不出:
cpu这端有信号,但读不出id;判断switch的供电是否正常,确认各引脚的上下拉选择的功能,比如SMI_SEL
3.switch模式不通:
一般上电后,cpu跟switch的所有port都是接通的,查看硬件原因,比如变压器
4.rgmii不通:
tx,rx都有信号;环回模式,rx没信号;怀疑delay时序不对
5.vlan:
swconfig或者vconfig