[Linux]移植驱动(一)配置环境&移植DM9000

[Linux]移植驱动(一)配置环境&移植DM9000

个人博客 https://aeneag.xyz/

艾恩凝

2021/7/12

introduction

前段时间进行写的驱动是在内核2.6版本上的,现在移植到新内核中去,万事开头难

开发板:JZ2440

操作系统:Ubuntu16.04

编译环境:arm-linux-gcc 4.3.2

内核:Linux 3.4.2

参考资料:韦东山 Linux 开发视频,网络

配置环境

问题描述:新内核之前移植过,编译环境,根文件系统理解还没上一层楼,花了一上午到下午快两点才彻底搞完,问题引出,首先挂载网络根文件系统挂载不上,尝试重新编译内核,又引出其他问题,之前内核编译的已经遗忘,重新清理了之前内核编译,忽略了清理之后需要重新配置,最开始出现的问题,怀疑根文件系统构建的有问题,一想之前可以用,没问题 ,只能把根文件系统烧到板子上才能挂载成功,后来才明白,新内核没有dm9000网卡驱动,需要配置,修改后,重新编译,板子上运行后,竟然乱码,就开始到处检查错误,找错误,清理编译,最后怀疑改的驱动有问题,改回之前的,清理之后重新编译,不乱码,就是没法挂载,改回去之后,又乱码,也不知道最后怎么怀疑改的有问题,重新修改了dm9000驱动,终于成功了,来来回回折腾到了两点

折腾的结局就是越来越熟

问题解决:

移植DM9000到内核3.4.2

之前有一篇移植这个的文章,之前的代码适配了内核2.6的版本,在这个基础上适配3.4.2版本

--- dm9dev9000c_old.c	2021-07-12 16:37:33.638299772 +0800
+++ dm9dev9000c.c	2021-07-12 15:18:47.258451326 +0800
@@ -2,8 +2,10 @@
  /*
  * @author aen
  * @date 2021/7/4
+ * @modify support kernel 3.4.2  2021/7/12
  */
  
+ 
  /*
  
    dm9ks.c: Version 2.08 2007/02/12 
@@ -96,12 +98,13 @@
  #include <asm/hardware.h>
  #include <asm/irq.h>
  #endif
- 
+ #include <linux/interrupt.h>
  
  #include <asm/delay.h>
  #include <asm/irq.h>
  #include <asm/io.h>
- #include <asm/arch-s3c2410/regs-mem.h>
+ #include "dm9000.h"
+ //#include <asm/arch-s3c2410/regs-mem.h>
  
  /* Board/System/Debug information/definition ---------------- */
  
@@ -325,10 +328,10 @@
  static int dmfe_set_settings(struct net_device *, struct ethtool_cmd *);
  static u32 dmfe_get_link(struct net_device *);
  static int dmfe_nway_reset(struct net_device *);
- static uint32_t dmfe_get_rx_csum(struct net_device *);
- static uint32_t dmfe_get_tx_csum(struct net_device *);
- static int dmfe_set_rx_csum(struct net_device *, uint32_t );
- static int dmfe_set_tx_csum(struct net_device *, uint32_t );
+ //static uint32_t dmfe_get_rx_csum(struct net_device *);
+ //static uint32_t dmfe_get_tx_csum(struct net_device *);
+ //static int dmfe_set_rx_csum(struct net_device *, uint32_t );
+ //static int dmfe_set_tx_csum(struct net_device *, uint32_t );
  
  #ifdef DM8606
  #include "dm8606.h"
@@ -359,7 +362,7 @@
  	if(!dev)
  		return ERR_PTR(-ENOMEM);
  
-      	SET_MODULE_OWNER(dev);
+      	//SET_MODULE_OWNER(dev);
  	err = dmfe_probe1(dev);
  	if (err)
  		goto out;
@@ -379,7 +382,23 @@
  #endif
  	return ERR_PTR(err);
  }
- 
+ static const struct net_device_ops dm9000C_netdev_ops = {
+	 .ndo_open		 = dmfe_open,
+	 .ndo_stop		 = dmfe_stop,
+	 .ndo_start_xmit	 = dmfe_start_xmit,
+	 .ndo_tx_timeout	 = dmfe_timeout,
+	 .ndo_set_rx_mode	 = dm9000_hash_table,
+	 .ndo_do_ioctl		 = dmfe_do_ioctl,
+	 .ndo_change_mtu	 = eth_change_mtu,
+	 .ndo_get_stats      = dmfe_get_stats,
+	// .ndo_set_features	 = dm9000_set_features,
+	 .ndo_validate_addr  = eth_validate_addr,
+	 .ndo_set_mac_address	 = eth_mac_addr,
+#ifdef CONFIG_NET_POLL_CONTROLLER
+	 .ndo_poll_controller	 = dm9000_poll_controller,
+#endif
+ };
+
  int __init dmfe_probe1(struct net_device *dev)
  {
  	struct board_info *db;    /* Point a board information structure */
@@ -408,10 +427,11 @@
  
  			printk(KERN_ERR"<DM9KS> I/O: %x, VID: %x \n",iobase, id_val);
  			dm9000_found = TRUE;
- 
+	
+            db = netdev_priv(dev);
  			/* Allocated board information structure */
- 			memset(dev->priv, 0, sizeof(struct board_info));
- 			db = (board_info_t *)dev->priv;
+ 			memset(db, 0, sizeof(struct board_info));
+ 	
  			dmfe_dev    = dev;
  			db->io_addr  = iobase;
  			db->io_data = iobase + 4;   
@@ -420,17 +440,20 @@
  			chip_info = ior(db,0x43);
  			//if((db->chip_revision!=0x1A) || ((chip_info&(1<<5))!=0) || ((chip_info&(1<<2))!=1)) return -ENODEV;
  				
- 			/* driver system function */		
+ 			/* driver system function */
+			dev->netdev_ops	= &dm9000C_netdev_ops;
+
+	
  			dev->base_addr 		= iobase;
  			dev->irq 		= irq;
- 			dev->open 		= &dmfe_open;
- 			dev->hard_start_xmit 	= &dmfe_start_xmit;
+ 			//dev->open 		= &dmfe_open;
+ 			//dev->hard_start_xmit 	= &dmfe_start_xmit;
  			dev->watchdog_timeo	= 5*HZ;
- 			dev->tx_timeout		= dmfe_timeout;
- 			dev->stop 		= &dmfe_stop;
- 			dev->get_stats 		= &dmfe_get_stats;
- 			dev->set_multicast_list = &dm9000_hash_table;
- 			dev->do_ioctl 		= &dmfe_do_ioctl;
+ 			//dev->tx_timeout		= dmfe_timeout;
+ 			//dev->stop 		= &dmfe_stop;
+ 			 //dev->get_stats 		= &dmfe_get_stats;
+ 			//dev->set_multicast_list = &dm9000_hash_table;
+ 			//dev->do_ioctl 		= &dmfe_do_ioctl;
  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,28)
  			dev->ethtool_ops = &dmfe_ethtool_ops;
  #endif
@@ -480,7 +503,7 @@
  */
  static int dmfe_open(struct net_device *dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	u8 reg_nsr;
  	int i;
  	DMFE_DBUG(0, "dmfe_open", 0);
@@ -663,7 +686,7 @@
  */
  static void dmfe_init_dm9000(struct net_device *dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	DMFE_DBUG(0, "dmfe_init_dm9000()", 0);
  
  	spin_lock_init(&db->lock);
@@ -746,7 +769,7 @@
  */
  static int dmfe_start_xmit(struct sk_buff *skb, struct net_device *dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	char * data_ptr;
  	int i, tmplen;
  	u16 MDWAH, MDWAL;
@@ -857,7 +880,7 @@
  */
  static int dmfe_stop(struct net_device *dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	DMFE_DBUG(0, "dmfe_stop", 0);
  
  	/* deleted timer */
@@ -890,7 +913,7 @@
  static void dmfe_tx_done(unsigned long unused)
  {
  	struct net_device *dev = dmfe_dev;
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	int  nsr;
  
  	DMFE_DBUG(0, "dmfe_tx_done()", 0);
@@ -939,7 +962,7 @@
  	DMFE_DBUG(0, "dmfe_interrupt()", 0);
  
  	/* A real interrupt coming */
- 	db = (board_info_t *)dev->priv;
+ 	db = netdev_priv(dev);
  	spin_lock(&db->lock);
  
  	/* Save previous register address */
@@ -1006,7 +1029,7 @@
  */
  static struct net_device_stats * dmfe_get_stats(struct net_device *dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db =netdev_priv(dev);
  	DMFE_DBUG(0, "dmfe_get_stats", 0);
  	return &db->stats;
  }
@@ -1041,7 +1064,7 @@
  */
  static int dmfe_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7) /* for kernel 2.6.7 */
      struct mii_ioctl_data *data=(struct mii_ioctl_data *)&ifr->ifr_data; 
  	#endif
@@ -1070,7 +1093,7 @@
  /* Our watchdog timed out. Called by the networking layer */
  static void dmfe_timeout(struct net_device *dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	int i;
  
  	DMFE_DBUG(0, "dmfe_TX_timeout()", 0);
@@ -1106,7 +1129,7 @@
  
  static void dmfe_reset(struct net_device * dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	u8 reg_save;
  	int i;
  	/* Save previous register address */
@@ -1140,7 +1163,7 @@
  static void dmfe_timer(unsigned long data)
  {
  	struct net_device * dev = (struct net_device *)data;
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	DMFE_DBUG(0, "dmfe_timer()", 0);
  
  	if (db->cont_rx_pkt_cnt>=CONT_RX_PKT_CNT)
@@ -1161,7 +1184,7 @@
  */
  static void dmfe_packet_receive(struct net_device *dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	struct sk_buff *skb;
  	u8 rxbyte;
  	u16 i, GoodPacket, tmplen = 0, MDRAH, MDRAL;
@@ -1377,9 +1400,10 @@
  /*
    Set DM9000/DM9010 multicast address
  */
+ #if 0
  static void dm9000_hash_table(struct net_device *dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	struct dev_mc_list *mcptr = dev->mc_list;
  	int mc_cnt = dev->mc_count;
  	u32 hash_val;
@@ -1429,12 +1453,71 @@
  		iow(db, oft++, (hash_table[i] >> 8) & 0xff);
  	}
  }
+ #else
  
  /*
+  *  Set DM9000 multicast address
+  */
+ static void
+ dm9000_hash_table_unlocked(struct net_device *dev)
+ {
+	 board_info_t *db = netdev_priv(dev);
+	 struct netdev_hw_addr *ha;
+	 int i, oft;
+	 u32 hash_val;
+	 u16 hash_table[4];
+	 u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
+ 
+	 //dm9000_dbg(db, 1, "entering %s\n", __func__);
+ 
+	 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
+		 iow(db, oft, dev->dev_addr[i]);
+ 
+	 /* Clear Hash Table */
+	 for (i = 0; i < 4; i++)
+		 hash_table[i] = 0x0;
+ 
+	 /* broadcast address */
+	 hash_table[3] = 0x8000;
+ 
+	 if (dev->flags & IFF_PROMISC)
+		 rcr |= RCR_PRMSC;
+ 
+	 if (dev->flags & IFF_ALLMULTI)
+		 rcr |= RCR_ALL;
+ 
+	 /* the multicast address in Hash Table : 64 bits */
+	 netdev_for_each_mc_addr(ha, dev) {
+		 hash_val = ether_crc_le(6, ha->addr) & 0x3f;
+		 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
+	 }
+ 
+	 /* Write the hash table to MAC MD table */
+	 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
+		 iow(db, oft++, hash_table[i]);
+		 iow(db, oft++, hash_table[i] >> 8);
+	 }
+ 
+	 iow(db, DM9000_RCR, rcr);
+ }
+ static void
+ dm9000_hash_table(struct net_device *dev)
+ {
+	 board_info_t *db = netdev_priv(dev);
+	 unsigned long flags;
+ 
+	 spin_lock_irqsave(&db->lock, flags);
+	 dm9000_hash_table_unlocked(dev);
+	 spin_unlock_irqrestore(&db->lock, flags);
+ }
+
+ #endif
+ /*
    Calculate the CRC valude of the Rx packet
    flag = 1 : return the reverse CRC (for the received packet CRC)
           0 : return the normal CRC (for Hash Table index)
  */
+ #if 0
  static unsigned long cal_CRC(unsigned char * Data, unsigned int Len, u8 flag)
  {
  	u32 crc = ether_crc_le(Len, Data);
@@ -1444,16 +1527,16 @@
  
  	return crc;	 
  }
- 
+ #endif
  static int mdio_read(struct net_device *dev, int phy_id, int location)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	return phy_read(db, location);
  }
  
  static void mdio_write(struct net_device *dev, int phy_id, int location, int val)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	phy_write(db, location, val);
  }
  
@@ -1519,7 +1602,7 @@
  }
  static int dmfe_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	spin_lock_irq(&db->lock);
  	mii_ethtool_gset(&db->mii, cmd);
  	spin_unlock_irq(&db->lock);
@@ -1527,7 +1610,7 @@
  }
  static int dmfe_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	int rc;
  
  	spin_lock_irq(&db->lock);
@@ -1540,7 +1623,7 @@
  */
  static u32 dmfe_get_link(struct net_device *dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db =netdev_priv(dev);
  	return mii_link_ok(&db->mii);
  }
  
@@ -1549,15 +1632,16 @@
  */
  static int dmfe_nway_reset(struct net_device *dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db =netdev_priv(dev);
  	return mii_nway_restart(&db->mii);
  }
  /*
  * Get RX checksum offload state
  */
+ #if 0
  static uint32_t dmfe_get_rx_csum(struct net_device *dev)
  {
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	return db->rx_csum;
  }
  /*
@@ -1573,7 +1657,7 @@
  static int dmfe_set_rx_csum(struct net_device *dev, uint32_t data)
  {
  #ifdef CHECKSUM
- 	board_info_t *db = (board_info_t *)dev->priv;
+ 	board_info_t *db = netdev_priv(dev);
  	db->rx_csum = data;
  
  	if(netif_running(dev)) {
@@ -1586,6 +1670,7 @@
  #endif
  	return 0;
  }
+
  /* 
  * Enable/Disable TX checksum offload
  */
@@ -1602,6 +1687,7 @@
  
  	return 0;
  }
+ #endif
  //=========================================
  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,28)  /* for kernel 2.4.28 */
  static struct ethtool_ops dmfe_ethtool_ops = {
@@ -1610,10 +1696,10 @@
  	.set_settings		= dmfe_set_settings,
  	.get_link			= dmfe_get_link,
  	.nway_reset		= dmfe_nway_reset,
- 	.get_rx_csum		= dmfe_get_rx_csum,
- 	.set_rx_csum		= dmfe_set_rx_csum,
- 	.get_tx_csum		= dmfe_get_tx_csum,
- 	.set_tx_csum		= dmfe_set_tx_csum,
+ 	//.get_rx_csum		= dmfe_get_rx_csum,
+ 	//.set_rx_csum		= dmfe_set_rx_csum,
+ 	//.get_tx_csum		= dmfe_get_tx_csum,
+ 	//.set_tx_csum		= dmfe_set_tx_csum,
  };
  #endif
  
@@ -1680,8 +1766,11 @@
  	 * PMC[1:0]   : 00-正常模式
  	 *
  	 */
- 	*bankcon4 = (1<<8)|(1<<6);	/* 对于DM9000C可以设Tacc为1, 对于DM9000E,Tacc要设大一点,比如最大值7  */
- 	iounmap(bwscon);
+ //	*bankcon4 = (1<<8)|(1<<6);	/* 对于DM9000C可以设Tacc为1, 对于DM9000E,Tacc要设大一点,比如最大值7  */
+	*bankcon4 = (7<<8)|(1<<6);  // performance not good,use this.
+
+
+	iounmap(bwscon);
  	iounmap(bankcon4);

上面是补丁,可以清楚的看到与之前的区别,报错的地方很多,需要慢慢修改

driver2.png

end

今天解决错误算是解决了一天,有问题总比没问题强,希望加快速度

艾恩凝

写于大连

2021/7/12


    


公众号'艾恩凝'
个人公众号
个人微信
个人微信
    吾心信其可行,
          则移山填海之难,
                  终有成功之日!
                                  ——孙文
    评论
    0 评论
avatar

取消