enum phy_state {
PHY_DOWN=0, //down 如关闭网卡,ifconfig eth0 down
PHY_STARTING, //1
PHY_READY, //2 phy设备注册成功
PHY_PENDING, //3 phy芯片挂起
PHY_UP, //4 开启网卡,ifconfig eth0 up
PHY_AN, //5 网卡自动协商
PHY_RUNNING, //6 网卡上已插入网线、并建立物理连接,同时会从这个状态切换到PHY_CHANGELINK
PHY_NOLINK, //7 断网,如拔掉网线
PHY_FORCING, //8 自动协商标识未被使能,就强制执行自动协商(读取phy寄存器、并设置通讯速率、半双工或全双工模式、等)
PHY_CHANGELINK, //9 当连接时,会换到PHY_RUNNING,当断网时,会切到PHY_NOLINK
PHY_HALTED, //10 在执行网卡关闭时(ifconfig eth0 down)会执行到这个状态,即phy挂起
PHY_RESUMING //11 在执行网卡开启时(ifconfig eth0 up)会执行到这个状态,即phy恢复
};
nuc970_ether_probe //探测函数驱动
nuc970_mii_setup
mdiobus_register
mdiobus_scan
get_phy_device
get_phy_id
phy_device_create
dev->autoneg = AUTONEG_ENABLE; //设置phy 自动协商使能
dev->state = PHY_DOWN; //设置phy的状态 ----1-----
INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine); //初始化一个中断顶版本的延时工作队列,用来处理phy状态机
phy_device_register //phy设备注册
device_add //这里是通过堆栈信息打印的,详见下面的 堆栈信息
bus_probe_device
device_attach
bus_for_each_drv
__device_attach
driver_probe_device
phy_probe //非常重要的一个探测接口
phydev->supported = phydrv->features; //phydrv->features = PHY_BASIC_FEATURES | SUPPORTED_Pause | SUPPORTED_Asym_Pause
phydev->advertising = phydrv->features;
phydev->state = PHY_READY; //设置phy的状态 ----2-----
phy_connect(dev, dev_name(&phydev->dev), &adjust_link, PHY_INTERFACE_MODE_RMII);
phy_connect_direct
phy_prepare_link
phydev->adjust_link = handler; //handler = adjust_link
phy_start_machine
phydev->adjust_state = handler; //handler = NULL
schedule_delayed_work(&phydev->state_queue, HZ); //启动上面注册的延时工作队列状态机 phy_state_machine
总结:上面已经将phy的状态设置成了 phydev->state = PHY_READY,同时完成了emac接口下的phy设备驱动注册,接下来将分析如何使用该phy设备驱动。
ifconfig eth0 up----->
nuc970_ether_open
phy_start
//......
switch (phydev->state) { //上面已经 phydev->state = PHY_READY
case PHY_STARTING:
phydev->state = PHY_PENDING;
break;
case PHY_READY:
phydev->state = PHY_UP; //所以到这里, 设置phy的状态为 PHY_UP ----3-----
break;
case PHY_HALTED:
phydev->state = PHY_RESUMING;
default:
break;
}
//......
总结:上面在执行 ifconfig eth0 up 后,已将phydev->state = PHY_UP
phy_state_machine
switch(phydev->state) { //关于该phydev->state的各执行状态详见本“第一”小节的介绍
case PHY_DOWN:
case PHY_STARTING:
case PHY_READY:
case PHY_PENDING:
break;
case PHY_UP:
needs_aneg = 1;
phydev->link_timeout = PHY_AN_TIMEOUT;
break;
.....
phy_start_aneg //启动自动协商
phydev->drv->config_aneg(phydev); //这里将调用 genphy_config_aneg
genphy_config_aneg
genphy_config_advert //自动协商声明,关于其源码详见如下
phydev->state = PHY_AN; //设置为自动协商,设置phy的状态为 PHY_AN ----4-----
//此时函数返回,又重新进入到上面的状态机 phy_state_machine,不过此时的状态已经变为 PHY_AN
case PHY_AN: //自动协商
phy_read_status //读取phy的状态
phydev->drv->read_status(phydev); //.read_status = genphy_read_status
genphy_read_status
genphy_update_link
phy_read(phydev, MII_BMSR); //读取phy的状态寄存器
if ((status & BMSR_LSTATUS) == 0) //phy 的链路状态
phydev->link = 0; //设置网卡的状态处于断开状态
else
phydev->link = 1; //设置网卡的状态处于连接状态
if (!phydev->link) { //链路状态
phydev->state = PHY_NOLINK; //协商失败,未建立连接,设置phy的状态为 PHY_NOLINK ----5-----
netif_carrier_off(phydev->attached_dev);
phydev->adjust_link(phydev->attached_dev);
break;
}
err = phy_aneg_done(phydev); //判断自动协商是否完成
/* If AN is done, we're running */
if (err > 0) { //自动协商完成
phydev->state = PHY_RUNNING; //协商成功,建立连接,设置phy的状态为 PHY_RUNNING ----6-----
netif_carrier_on(phydev->attached_dev);
phydev->adjust_link(phydev->attached_dev); //adjust_link = adjust_link 调用nuc970_ether0里面的接口函数
} else if (0 == phydev->link_timeout--) { //自动协商未完成,将根据 link_timeout 次数继续尝试自动协商
needs_aneg = 1;
/* If we have the magic_aneg bit,
* we try again */
if (phydev->drv->flags & PHY_HAS_MAGICANEG)
break;
}
case PHY_RUNNING:
/* Only register a CHANGE if we are
* polling */
if (PHY_POLL == phydev->irq)
phydev->state = PHY_CHANGELINK; //正常运行,设置phy的状态为 PHY_CHANGELINK ----7-----
break;
case PHY_CHANGELINK:
err = phy_read_status(phydev); //再次获取 phy 链路状态
if (err)
break;
if (phydev->link) {
phydev->state = PHY_RUNNING; //协商成功,建立连接,设置phy的状态为 PHY_RUNNING ----8-----
netif_carrier_on(phydev->attached_dev);
} else {
phydev->state = PHY_NOLINK; //协商失败,未建立连接,设置phy的状态为 PHY_NOLINK ----9-----
netif_carrier_off(phydev->attached_dev);
}
phydev->adjust_link(phydev->attached_dev); //adjust_link = adjust_link 调用nuc970_ether0里面的接口函数
if (PHY_POLL != phydev->irq)
err = phy_config_interrupt(phydev,
PHY_INTERRUPT_ENABLED);
break;
emac接口驱动配置phy
int genphy_config_aneg(struct phy_device *phydev)
{
int result;
if (AUTONEG_ENABLE != phydev->autoneg) //在phy创建的时候,初始了ev->autoneg = AUTONEG_ENABLE; //设置phy 自动协商使能
return genphy_setup_forced(phydev);
result = genphy_config_advert(phydev);
if (result < 0) /* error */
return result;
if (result == 0) {
/* Advertisement hasn't changed, but maybe aneg was never on to
* begin with? Or maybe phy was isolated? */
int ctl = phy_read(phydev, MII_BMCR); //获取模式控制寄存器,详见该链接对该寄存器的定义:https://wenku.baidu.com/view/b8704335ee06eff9aef807bd.html
if (ctl < 0)
return ctl;
//不能自动协商 || 自动协商复位
if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) //BMCR_ANENABLE: 自动协商使能 ||
result = 1; /* do restart aneg */
}
/* Only restart aneg if we are advertising something different
* than we were before. */
if (result > 0)
result = genphy_restart_aneg(phydev); //重启自动协商
return result;
}
获取自动协商配置
static int genphy_config_advert(struct phy_device *phydev)
{
u32 advertise;
int oldadv, adv;
int err, changed = 0;
/* Only allow advertising what
* this PHY supports */
/*
phy 设备在注册时初始化为如下:
phydev->supported = phydrv->features; //phydrv->features = PHY_BASIC_FEATURES | SUPPORTED_Pause | SUPPORTED_Asym_Pause
phydev->advertising = phydrv->features;
*/
phydev->advertising &= phydev->supported;
advertise = phydev->advertising;
/* Setup standard advertisement */
oldadv = adv = phy_read(phydev, MII_ADVERTISE); //自动协商声明
if (adv < 0)
return adv;
adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
ADVERTISE_PAUSE_ASYM);
adv |= ethtool_adv_to_mii_adv_t(advertise);
if (adv != oldadv) { //自动协商结果不一致就重新设置
err = phy_write(phydev, MII_ADVERTISE, adv);
if (err < 0)
return err;
changed = 1;
}
/* Configure gigabit if it's supported */
if (phydev->supported & (SUPPORTED_1000baseT_Half |
SUPPORTED_1000baseT_Full)) {
oldadv = adv = phy_read(phydev, MII_CTRL1000);
if (adv < 0)
return adv;
adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
if (adv != oldadv) {
err = phy_write(phydev, MII_CTRL1000, adv);
if (err < 0)
return err;
changed = 1;
}
}
return changed;
}
重启自动协商
int genphy_restart_aneg(struct phy_device *phydev)
{
int ctl;
ctl = phy_read(phydev, MII_BMCR);
if (ctl < 0)
return ctl;
ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
/* Don't isolate the PHY if we're negotiating */
ctl &= ~(BMCR_ISOLATE);
ctl = phy_write(phydev, MII_BMCR, ctl); //设置自动协商
return ctl;
}
总结:上面源码只要是完成emac接口与phy之间的自动协商、链路状态检测等
===========================堆栈信息===========================
Backtrace:
[] (dump_backtrace+0x0/0x10c) from [] (show_stack+0x18/0x1c)
r7:c39c8374 r6:c39c8208 r5:c0474914 r4:c39c8200
[] (show_stack+0x0/0x1c) from [] (dump_stack+0x20/0x2c)
[] (dump_stack+0x0/0x2c) from [] (phy_probe+0x58/0x88)
[] (phy_probe+0x0/0x88) from [] (driver_probe_device+0x9c/0x238)
r7:c047496c r6:00000000 r5:c39c8208 r4:c047496c
[] (driver_probe_device+0x0/0x238) from [] (__device_attach+0x44/0x48)
[] (__device_attach+0x0/0x48) from [] (bus_for_each_drv+0x68/0x94)
r5:c3843cd8 r4:00000000
[] (bus_for_each_drv+0x0/0x94) from [] (device_attach+0x88/0xa0)
r7:00000001 r6:c39c823c r5:c0474820 r4:c39c8208
[] (device_attach+0x0/0xa0) from [] (bus_probe_device+0x8c/0xb4)
r7:00000001 r6:c39c8208 r5:c0474820 r4:c04b84f8
[] (bus_probe_device+0x0/0xb4) from [] (device_add+0x310/0x648)
r7:00000001 r6:00000000 r5:c39c8208 r4:c04b84f8
[] (device_add+0x0/0x648) from [] (phy_device_register+0x44/0x70)
[] (phy_device_register+0x0/0x70) from [] (mdiobus_scan+0x2c/0x4c)
r7:00000001 r6:00000000 r5:00000001 r4:c39c8200
[] (mdiobus_scan+0x0/0x4c) from [] (mdiobus_register+0xec/0x184)
r5:00000001 r4:c39c8000
[] (mdiobus_register+0x0/0x184) from [] (nuc970_ether_probe+0x2d8/0x4b4)
[] (nuc970_ether_probe+0x0/0x4b4) from [] (platform_drv_probe+0x20/0x24)
[] (platform_drv_probe+0x0/0x24) from [] (driver_probe_device+0x9c/0x238)
[] (driver_probe_device+0x0/0x238) from [] (__driver_attach+0x94/0x98)
[] (__driver_attach+0x0/0x98) from [] (bus_for_each_dev+0x7c/0xa0)
r7:c01f6b80 r6:c0474a58 r5:c3843e78 r4:00000000
[] (bus_for_each_dev+0x0/0xa0) from [] (driver_attach+0x20/0x28)
r7:c046fc68 r6:c0474a58 r5:c0474a58 r4:c39a69c0
[] (driver_attach+0x0/0x28) from [] (bus_add_driver+0xec/0x240)
[] (bus_add_driver+0x0/0x240) from [] (driver_register+0x60/0x150)
r7:c3842000 r6:00000000 r5:c0474a58 r4:c044396c
[] (driver_register+0x0/0x150) from [] (platform_driver_register+0x4c/0x60)
r7:c3842000 r6:00000000 r5:c043f414 r4:c044396c
[] (platform_driver_register+0x0/0x60) from [] (nuc970_ether_init+0x14/0x1c)
[] (nuc970_ether_init+0x0/0x1c) from [] (do_one_initcall+0x38/0x150)
[] (do_one_initcall+0x0/0x150) from [] (kernel_init_freeable+0x14c/0x224)
[] (kernel_init_freeable+0x0/0x224) from [] (kernel_init+0x10/0x15c)
[] (kernel_init+0x0/0x15c) from [] (ret_from_fork+0x14/0x24)
r5:c0324518 r4:00000000
libphy: nuc970_rmii0: probed
/* Generic MII registers. */
//控制寄存器
#define MII_BMCR 0x00 /* Basic mode control register */
//状态寄存器
#define MII_BMSR 0x01 /* Basic mode status register */
//PHY 标志
#define MII_PHYSID1 0x02 /* PHYS ID 1 */
#define MII_PHYSID2 0x03 /* PHYS ID 2 */
//4~8寄存器是用于自动协商的
#define MII_ADVERTISE 0x04 /* Advertisement control reg 自动协商声明*/
#define MII_LPA 0x05 /* Link partner ability reg */
#define MII_EXPANSION 0x06 /* Expansion register */
//保留
#define MII_CTRL1000 0x09 /* 1000BASE-T control */
#define MII_STAT1000 0x0a /* 1000BASE-T status */
#define MII_MMD_CTRL 0x0d /* MMD Access Control Register */
#define MII_MMD_DATA 0x0e /* MMD Access Data Register */
#define MII_ESTATUS 0x0f /* Extended Status */
//制造商指定寄存器
#define MII_DCOUNTER 0x12 /* Disconnect counter */
#define MII_FCSCOUNTER 0x13 /* False carrier counter */
#define MII_NWAYTEST 0x14 /* N-way auto-neg test reg */
#define MII_RERRCOUNTER 0x15 /* Receive error counter */
#define MII_SREVISION 0x16 /* Silicon revision */
#define MII_RESV1 0x17 /* Reserved... */
#define MII_LBRERROR 0x18 /* Lpback, rx, bypass error */
#define MII_PHYADDR 0x19 /* PHY address */
#define MII_RESV2 0x1a /* Reserved... */
#define MII_TPISTATUS 0x1b /* TPI status for 10mbps */
#define MII_NCONFIG 0x1c /* Network interface config */
关于phy的寄存器定义详见:https://wenku.baidu.com/view/b8704335ee06eff9aef807bd.html 第5页