【uboot】imx6ull uboot移植LAN8720A网卡驱动

相关文章

1.《【uboot】imx6ull uboot 2020.04源码下载和编译环境配置》
2.《【Ethernet】以太网卡LAN8720A分析和使用》

1. 前言

本篇文章主要是介绍,基于IMX6ULL平台uboot来移植LAN8720A网卡驱动,代码是基于《【uboot】imx6ull uboot 2020.04源码下载和编译环境配置》这篇文章下载后的修改。

2. IMX6ULL Ethernet LAN8720A硬件连接

IMX6ULL 平台支持2个以太网口,在uboot中我们基本上使用一个uboot就足够,所以后面主要是介绍imx6ull Ethernet2如何支持LAN8720A网卡。下面是IMX6ULL 连接LAN8720A的原理图:

在这里插入图片描述

3. 支持LAN8720A修改步骤

基于IMX6ULL平台uboot中,修改支持LAN8720A网卡。

  • 修改config配置文件对LAN8720A网卡的支持,并且关闭对其它网卡的支持。
    在这里插入图片描述

    --- a/configs/mx6ull_14x14_evk_emmc_zc_defconfig
    +++ b/configs/mx6ull_14x14_evk_emmc_zc_defconfig
    @@ -1025,13 +1025,11 @@ CONFIG_PHYLIB=y
     # CONFIG_PHY_LXT is not set
     # CONFIG_PHY_MARVELL is not set
     # CONFIG_PHY_MESON_GXL is not set
    -CONFIG_PHY_MICREL=y
    -# CONFIG_PHY_MICREL_KSZ90X1 is not set
    -CONFIG_PHY_MICREL_KSZ8XXX=y
    +# CONFIG_PHY_MICREL is not set
     # CONFIG_PHY_MSCC is not set
     # CONFIG_PHY_NATSEMI is not set
     # CONFIG_PHY_REALTEK is not set
    -# CONFIG_PHY_SMSC is not set
    +CONFIG_PHY_SMSC=y
     # CONFIG_PHY_TERANETICS is not set
     # CONFIG_PHY_TI is not set
     # CONFIG_PHY_VITESSE is not set
    
  • 修改device tree文件,禁用ETH1并且修改ETH2增加对RESET GPIO的支持。
    在这里插入图片描述

    --- a/arch/arm/dts/imx6ul-14x14-evk.dtsi
    +++ b/arch/arm/dts/imx6ul-14x14-evk.dtsi
    @@ -83,7 +83,7 @@
     	pinctrl-0 = <&pinctrl_enet1>;
     	phy-mode = "rmii";
     	phy-handle = <&ethphy0>;
    -	status = "okay";
    +	status = "disabled";
     };
     
     &fec2 {
    @@ -91,14 +91,17 @@
     	pinctrl-0 = <&pinctrl_enet2>;
     	phy-mode = "rmii";
     	phy-handle = <&ethphy1>;
    +	phy-reset-gpios = <&gpio5 6 GPIO_ACTIVE_LOW>;
    +	phy-reset-duration = <15>;
    +	phy-reset-post-delay = <15>;
     	status = "okay";
    
  • 修改phy.c文件,在启用了自动协商之前,再次进行网卡的软件reset。(NOTE:至于为什么需要再次reset还不太清楚,有了解的话欢迎在下面留言。)
    在这里插入图片描述

    --- a/drivers/net/phy/phy.c
    +++ b/drivers/net/phy/phy.c
    @@ -177,6 +177,9 @@ int genphy_config_aneg(struct phy_device *phydev)
     {
     	int result;
     
    +	/* Soft Reset the PHY */
    +	phy_reset(phydev); // add by cai.zhong 2021-06-20
    +
     	if (phydev->autoneg != AUTONEG_ENABLE)
     		return genphy_setup_forced(phydev);
    

4. 验证测试

编译烧录后,通过使用ping命令ping通局域网的其它设备,说明网卡已经调试成功。打印log如下:

U-Boot 2020.04-g7a4fc484-dirty (Jun 27 2021 - 20:46:38 +0800)

CPU:   i.MX6ULL rev1.1 792 MHz (running at 396 MHz)
CPU:   Industrial temperature grade (-40C to 105C) at 51C
Reset cause: POR
Model: i.MX6 ULL 14x14 EVK Board
Board: MX6ULL 14x14 EVK
DRAM:  512 MiB
MMC:   FSL_SDHC: 0, FSL_SDHC: 1
Loading Environment from MMC... OK
[*]-Video Link 0 (480 x 272)
[0] lcdif@21c8000, video
In:    serial
Out:   serial
Err:   serial
switch to partitions #0, OK
mmc1(part 0) is current device
flash target is MMC:1
Net:   eth1: ethernet@20b4000 [PRIME]
Fastboot: Normal
Normal Boot
Hit any key to stop autoboot:  3  0 
=> 
=> dhcp
BOOTP broadcast 1
DHCP client bound to address 192.168.1.105 (4 ms)
*** ERROR: `serverip' not set
Cannot autoload with TFTPGET
=> ping 192.168.10 .10
Using ethernet@20b4000 device
host 192.168.1.10 is alive
=> 

问题1:如何确定LAN8720A网卡PHYAD地址?

PHYAD[0]引脚用于配置 SMI通信的 LAN8720A 地址,在芯片内部该引脚已经自带下拉电阻,默认认为 0(即使外部悬空不接),在系统上电时会检测该引脚获取得到 LAN8720A的地址为 0 或者 1,并保存在特殊模式寄存器(R18)的 PHYAD位中,该寄存器的 PHYAD5个位,在需要超过 2个 LAN8720A 时可以通过软件设置不同 SMI通信地址
在这里插入图片描述
根据上面的硬件连接Ethernet2使用的PHYAD = 1,所以在device tree可以再次确认mdioreg是否等于1
在这里插入图片描述

问题2:如何确定device tree中对reset gpio的定义?

一般都是driver驱动确定了device tree的编写格式,对于imx6ull uboot的ETHdriver路径如下:
PATH:imx-uboot/drivers/net/fec_mxc.c
在这里插入图片描述

在驱动绑定成功后,都会从设备树中进行平台数据的加载,调用函数fecmxc_ofdata_to_platdata()。通过它我们就知道,需要在device tree中定义phy-reset-gpiosphy-reset-durationphy-reset-post-delay这3个属性。

static int fecmxc_ofdata_to_platdata(struct udevice *dev)
{
	int ret = 0;
	struct eth_pdata *pdata = dev_get_platdata(dev);
	struct fec_priv *priv = dev_get_priv(dev);
	const char *phy_mode;

	...

#if CONFIG_IS_ENABLED(DM_GPIO)
	ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
				   &priv->phy_reset_gpio, GPIOD_IS_OUT);
	if (ret < 0)
		return 0; /* property is optional, don't return error! */

	priv->reset_delay = dev_read_u32_default(dev, "phy-reset-duration", 1);
	if (priv->reset_delay > 1000) {
		printf("FEC MXC: phy reset duration should be <= 1000ms\n");
		/* property value wrong, use default value */
		priv->reset_delay = 1;
	}

	priv->reset_post_delay = dev_read_u32_default(dev,
						      "phy-reset-post-delay",
						      0);
	if (priv->reset_post_delay > 1000) {
		printf("FEC MXC: phy reset post delay should be <= 1000ms\n");
		/* property value wrong, use default value */
		priv->reset_post_delay = 0;
	}
#endif

	return 0;
}

???那么这3个属性是怎么使用的呢???
imx-uboot/drivers/net/fec_mxc.c文件中,我们发现是通过这个GPIO硬件reset网卡,可以通过原理图查看得知网卡的reset是连接的在imx6ull GPIO5_6上,所以phy-reset-gpios就是GPIO5_6。

static void fec_gpio_reset(struct fec_priv *priv)
{
	debug("fec_gpio_reset: fec_gpio_reset(dev)\n");
	if (dm_gpio_is_valid(&priv->phy_reset_gpio)) {
		dm_gpio_set_value(&priv->phy_reset_gpio, 1);
		mdelay(priv->reset_delay);
		dm_gpio_set_value(&priv->phy_reset_gpio, 0);
		if (priv->reset_post_delay)
			mdelay(priv->reset_post_delay);
	}
}

phy-reset-durationphy-reset-post-delay就是用来延时维持io的状态,它需要延时多久我们可以通过查看LAN8720A datasheet知道,具体如下:
在这里插入图片描述

问题3:LAN8720A网卡nINTSEL是如何配置?

通过nINTSEL来配置两个模式:REF_CLK输入模式(nINT)和REF_CLK输出模式。通过该引脚的高低电平决定了nINT / REFCLKO引脚的功能。

STRAP VALUE MODEREF_CLK DESCRIPTION
nINTSEL = 0REF_CLK Out ModenINT/REFCLKO is the source of REF_CLK.
nINTSEL = 1REF_CLK In ModenINT/REFCLKO is the source of REF_CLK.

根据如下硬件连接,那么nINTSEL = 1,所以选择的是REF_CLK输入模式。即外部时钟源直接提供 50MHz时钟接入 主机MAC接口REF_CLK引脚和 LAN8720A 的 XTAL1/CLKIN 引脚,此时 nINT/REFCLKO 可用于中断功能。IMX6ULL中内部分频给MAC模块,并且输出到ENET2_TX_CLK给外部的PHY LAN8720A 使用。
在这里插入图片描述

问题4:IMX6ULL ETH是如何被初始化的?

下面是IMX6ULL ETH的初始化流程,后面有时间再详细的分析。

[board_r.c]initr_net();
	[eth-uclass.c]eth_initialize();
		[eth-uclass.c]eth_common_init();
			[miiphyutil.c]miiphy_init();
			[phy.c]phy_init();
				[smsc.c]phy_smsc_init();
					[phy.c]phy_register(&lan8710_driver); // 将lan8720a phy_driver添加到LIST_HEAD(phy_drivers)队列中
				[phy.c]genphy_init();
					[phy.c]phy_register(&genphy_driver);
		[uclass.c]uclass_first_device_check(UCLASS_ETH, &dev); // dev = ethernet@20b4000
			[device.c]device_probe(*devp); // 探测udevice ethernet@20b4000
				[device.c]device_ofdata_to_platdata(dev); // 给dev配置需要的空间,并从Devcie Tree中获取dev ethernet@20b4000的平台数据。
					[fec_mxc.c]fecmxc_ofdata_to_platdata(dev); // 1:获取eth iobase地址;2:获取phy-mode值"rmii";3:通过phy-reset-gpios值申请GPIO资源;4:通过phy-reset-duration值设置reset delay的时间;
				[pinctrl-uclass.c]pinctrl_select_state(dev, "default"); // 设置eth IO为default状态,default为eth功能pinctrl_enet1、pinctrl_enet2;
				[uclass.c]uclass_pre_probe_device(dev); // 
				[fec_mxc.c]fecmxc_probe(dev); //
					[fec_mxc.c]fec_gpio_reset(priv); // 根据device tree里面定义的reset pin,操作它让eth reset并延时。
					[fec_mxc.c]fec_get_miibus((ulong)priv->eth, dev->seq); // 获取mii总线,包括mdio。
					[fec_mxc.c]fec_phy_init(priv, dev); //
						[fec_mxc.c]device_get_phy_addr(dev); // 从device tree中获取phy reg地址,reg = <1>;
						[phy.c]phy_connect(priv->bus, addr, dev, priv->interface); // 
							[phy.c]phy_find_by_mask(bus, mask, interface);
								[phy.c]get_phy_device_by_mask(bus, phy_mask, interface); 
									[phy.c]create_phy_by_mask(bus, phy_mask, i ? i : MDIO_DEVAD_NONE, interface);
										[phy.c]get_phy_id(bus, addr, devad, &phy_id); // 读取PHY Identifier 1 Register和PHY Identifier 2 Register的值;
										[phy.c]phy_device_create(bus, addr, phy_id, is_c45, interface);// 创建一个phy_device设备
											[phy.c]get_phy_driver(dev, interface); // 遍历phy_drivers链表,通过driver定义的uid和读取的实际phy_id匹配,找到合适的driver。
											[phy.c]phy_probe(dev); // 设置phydev->advertising和phydev->supported为PHY_BASIC_FEATURES
							[phy.c]phy_connect_dev(phydev, dev); // 将给定的PHY和以太网设备绑定起来
								[phy.c]phy_reset(phydev); // 软件复位PHY
									[phy.c]phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET); // 发送reset命令给网卡lan8720a
									[phy.c]phy_read(phydev, devad, MII_BMCR); //读取网卡lan8720a Basic mode control register的reset是否完成
								[log.h]debug("%s connected to %s\n", dev->name, phydev->drv->name); // ethernet@20b4000 connected to SMSC LAN8710/LAN8720
						[phy.c]phy_config(phydev); // 
							[phy.c]board_phy_config(phydev);
								[phy.c]genphy_config_aneg(phydev); // 重启自协商或写入BMCR
									[phy.c]genphy_config_advert(phydev); // 清除和发布自动协商参数
				[uclass.c]uclass_post_probe_device(dev); // 处理一个刚刚被probed过的设备
					[eth-uclass.c]eth_post_probe(dev); // 获取mac地址
						[eth-uclass.c]eth_dev_get_mac_address(dev, pdata->enetaddr); // 从设备树中获取mac-address
						[fec_mxc.c]fecmxc_read_rom_hwaddr(dev); // 从ROM中获取mac地址
							[mac.c]imx_get_mac_from_fuse(dev_id, mac); // 从ROM中获取mac地址
						[eth-uclass.c]eth_write_hwaddr(dev); // 将mac地址写入到eth寄存器
							[fec_mxc.c]fecmxc_set_hwaddr(dev); // 将mac地址写入到eth寄存器
		[eth-uclass.c]eth_get_dev_by_name(ethprime);  // ethprime = eth1; 
		[eth-uclass.c]eth_set_dev(prime_dev); 
		[eth_common.c]eth_current_changed();
		[eth_common.c]eth_write_hwaddr(dev);
  • 8
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值