I.MX6Q(TQIMX6Q/TQE9)学习笔记——新版BSP之网卡驱动移植

由于对网卡这块不是很熟悉,误以为网卡驱动也可以简单的配置下DTS就可以正常工作了,实际移植中遇到了些问题。闲话少说,下面开始tqimx6q的网卡驱动移植。

DTS编写

首先在我们的DTS中添加网卡配置,参考sabrelite的DTS,我们可以如下编写:

&fec {
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_enet_1>;
    phy-mode = "rgmii";
    status = "okay";
};
开始以为添加以上内容后编译并烧写DTB就可以正常工作了,实则不然,还需要添加PHY相关的配置代码。

PHY配置

打开arch/arm/mach-imx/mach-imx6q.c,在宏PHY_ID_AR8031附近做如下修改:

static int ar8035_phy_fixup(struct phy_device *dev)
{
    u16 val;
    
    
    /* Ar803x phy SmartEEE feature cause link status generates glitch,
    * which cause ethernet link down/up issue, so disable SmartEEE
    */
    phy_write(dev, 0xd, 0x3);
    phy_write(dev, 0xe, 0x805d);
    phy_write(dev, 0xd, 0x4003);
    
    
    val = phy_read(dev, 0xe);
    phy_write(dev, 0xe, val & ~(1 << 8));
    
    
    /*
    * Enable 125MHz clock from CLK_25M on the AR8031.  This
    * is fed in to the IMX6 on the ENET_REF_CLK (V22) pad.
    * Also, introduce a tx clock delay.
    *
    * This is the same as is the AR8031 fixup.
    */
    ar8031_phy_fixup(dev);
    
    
    /*check phy power*/
    val = phy_read(dev, 0x0);
    if (val & BMCR_PDOWN)
        phy_write(dev, 0x0, val & ~BMCR_PDOWN);
    
    
    return 0;
}

#define PHY_ID_AR8035 0x004dd072

static void __init imx6q_enet_phy_init(void)
{
    if (IS_BUILTIN(CONFIG_PHYLIB)) {
        phy_register_fixup_for_uid(PHY_ID_KSZ9021, MICREL_PHY_ID_MASK,
                ksz9021rn_phy_fixup);
        phy_register_fixup_for_uid(PHY_ID_KSZ9031, MICREL_PHY_ID_MASK,
                ksz9031rn_phy_fixup);
        phy_register_fixup_for_uid(PHY_ID_AR8031, 0xffffffff,
                ar8031_phy_fixup);
        phy_register_fixup_for_uid(PHY_ID_AR8035, 0xffffffff,
                ar8035_phy_fixup);
    }
}
即添加AR8035的ID,并初始化AR8035的寄存器,具体的含义我还没有来得及分析,以上修改参考了:

http://lxr.free-electrons.com/source/arch/arm/mach-imx/mach-imx6q.c
完成以上修改之后重新编译uImage和dtb,然后烧写开发板即可。

测试方法

寻求网卡的测试方法目标应该很明确,就是能ping通外网即可,下面是我在tqimx6q上的测试步骤。

Step1. 查看网卡信息

执行命令:

ifconfig -a
效果如下:

@tqimx6q #ifconfig -a
eth0      Link encap:Ethernet  HWaddr AA:EF:AE:8A:EA:05  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          LOOPBACK  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

sit0      Link encap:IPv6-in-IPv4  
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
Step2. 使能网卡eth0,执行命令:

ifconfig eth0 up
效果如下:
@tqimx6q #ifconfig eth0 up
fec 2188000.ethernet eth0: Freescale FEC PHY driver [Generic PHY] (mii_bus:phy_addr=2188000.ethernet:00, irq=-1)
IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready
@tqimx6q #libphy: 2188000.ethernet:00 - Link is Up - 100/Full
IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
Step3. 动态获取IP,执行指令:

udhcpc
效果如下:
@tqimx6q #udhcpc
udhcpc (v1.22.1) started
Sending discover...
Sending select for 192.168.0.106...
Lease of 192.168.0.106 obtained, lease time 86400
Step4. 将动态获取的IP设置给网卡eth0,执行指令:

ifconfig eth0 192.168.0.106
之后可以通过命令查看网卡配置信息:

ifconfig eth0
效果如下:

@tqimx6q #ifconfig eth0 192.168.0.106
@tqimx6q #ifconfig eth0
eth0      Link encap:Ethernet  HWaddr AA:EF:AE:8A:EA:05  
          inet addr:192.168.0.106  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a8ef:aeff:fe8a:ea05/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:253 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:24238 (23.6 KiB)  TX bytes:1152 (1.1 KiB)
Step5. 设置网关,执行指令:

route add default gw 192.168.0.1
Step6. 通过ping指令测试网络状态,执行指令:

ping 8.8.8.8
效果如下:

@tqimx6q #ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=43 time=77.409 ms
64 bytes from 8.8.8.8: seq=1 ttl=43 time=83.292 ms
64 bytes from 8.8.8.8: seq=2 ttl=43 time=88.236 ms
64 bytes from 8.8.8.8: seq=3 ttl=43 time=80.713 ms
64 bytes from 8.8.8.8: seq=4 ttl=43 time=74.452 ms
64 bytes from 8.8.8.8: seq=5 ttl=43 time=78.627 ms
可见,tqimx6q已经可以ping通谷歌的DNS服务器了。至此,网卡的移植工作就完成了,PHY相关的配置还没有仔细研究就来分享了,请见谅。文章编写匆忙,如有问题请帮忙指出,有疑问可以留言讨论。






本文作者:girlkoo

本文链接:http://blog.csdn.net/girlkoo/article/details/45678357

你可能感兴趣的:(嵌入式开发)