8442|10

55

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

[转贴]DM9000 linux2.6 驱动解析 [复制链接]

DM9000 linux2.6 驱动解析,网络上面找的,感觉还不错转帖给需要朋友。



  1. /*
  2. *   dm9000.c: Version 1.2 03/18/2003
  3. *
  4. *         A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
  5. *  Copyright (C) 1997  Sten Wang
  6. *
  7. *  This program is free software; you can redistribute it and/or
  8. *  modify it under the terms of the GNU General Public License
  9. *  as published by the Free Software Foundation; either version 2
  10. *  of the License, or (at your option) any later version.
  11. *
  12. *  This program is distributed in the hope that it will be useful,
  13. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. *  GNU General Public License for more details.
  16. *
  17. *   (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
  18. *
  19. * V0.11 06/20/2001 REG_0A bit3=1, default enable BP with DA match
  20. *  06/22/2001  Support DM9801 progrmming
  21. *      E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000
  22. *     E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200
  23. *          R17 = (R17 & 0xfff0) | NF + 3
  24. *     E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200
  25. *          R17 = (R17 & 0xfff0) | NF
  26. *
  27. * v1.00                modify by simon 2001.9.5
  28. *                         change for kernel 2.4.x
  29. *
  30. * v1.1   11/09/2001       fix force mode bug
  31. *
  32. * v1.2   03/18/2003       Weilun Huang :
  33. *    Fixed phy reset.
  34. *    Added tx/rx 32 bit mode.
  35. *    Cleaned up for kernel merge.
  36. *
  37. *        03/03/2004    Sascha Hauer
  38. *                      Port to 2.6 kernel
  39. *
  40. *   24-Sep-2004   Ben Dooks
  41. *   Cleanup of code to remove ifdefs
  42. *   Allowed platform device data to influence access width
  43. *   Reformatting areas of code
  44. *
  45. *        17-Mar-2005   Sascha Hauer
  46. *                      * removed 2.4 style module parameters
  47. *                      * removed removed unused stat counter and fixed
  48. *                        net_device_stats
  49. *                      * introduced tx_timeout function
  50. *                      * reworked locking
  51. *
  52. *   01-Jul-2005   Ben Dooks
  53. *   * fixed spinlock call without pointer
  54. *   * ensure spinlock is initialised
  55. */

  56. #i nclude
  57. #i nclude
  58. #i nclude
  59. #i nclude
  60. #i nclude
  61. #i nclude
  62. #i nclude
  63. #i nclude
  64. #i nclude
  65. #i nclude
  66. #i nclude
  67. #i nclude

  68. #i nclude
  69. #i nclude
  70. #i nclude

  71. #i nclude "dm9000.h"

  72. /* Board/System/Debug information/definition ---------------- */

  73. #define DM9000_PHY  0x40 /* PHY address 0x01 */

  74. #define CARDNAME "dm9000"
  75. #define PFX CARDNAME ": "

  76. #define DM9000_TIMER_WUT  jiffies+(HZ*2) /* timer wakeup time : 2 second */

  77. #define DM9000_DEBUG 0

  78. #if DM9000_DEBUG > 2
  79. #define PRINTK3(args...)  printk(CARDNAME ": " args)
  80. #else
  81. #define PRINTK3(args...)  do { } while(0)
  82. #endif

  83. #if DM9000_DEBUG > 1
  84. #define PRINTK2(args...)  printk(CARDNAME ": " args)
  85. #else
  86. #define PRINTK2(args...)  do { } while(0)
  87. #endif

  88. #if DM9000_DEBUG > 0
  89. #define PRINTK1(args...)  printk(CARDNAME ": " args)
  90. #define PRINTK(args...)   printk(CARDNAME ": " args)
  91. #else
  92. #define PRINTK1(args...)  do { } while(0)
  93. #define PRINTK(args...)   printk(KERN_DEBUG args)
  94. #endif

  95. /*
  96. * Transmit timeout, default 5 seconds.
  97. */
  98. static int watchdog = 5000;
  99. module_param(watchdog, int, 0400);
  100. MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");

  101. /*DM9000 含有16K SRAM作为FIFO buffer,3K用于transmitt,13k用于receive*/

  102. /* Structure/enum declaration ------------------------------- */
  103. typedef struct board_info {
  104. /*命令地址寄存器地址*/
  105. void __iomem *io_addr; /* Register I/O base address */
  106. /*数据寄存器地址*/
  107. void __iomem *io_data; /* Data I/O address */
  108. u16 irq;  /* IRQ */

  109. u16 tx_pkt_cnt;
  110. u16 queue_pkt_len;
  111. u16 queue_start_addr;
  112. u16 dbug_cnt;
  113. u8 io_mode;  /* 0:word, 2:byte */
  114. u8 phy_addr;

  115. void (*inblk)(void __iomem *port, void *data, int length);
  116. void (*outblk)(void __iomem *port, void *data, int length);
  117. void (*dumpblk)(void __iomem *port, int length);

  118. struct resource *addr_res;   /* resources found */
  119. struct resource *data_res;
  120. struct resource *addr_req;   /* resources requested */
  121. struct resource *data_req;
  122. struct resource *irq_res;

  123. struct timer_list timer;
  124. struct net_device_stats stats;
  125. unsigned char srom[128];    /*网卡上EERPOM的内容*/
  126. spinlock_t lock;

  127. struct mii_if_info mii;  /*调试可能会用到*/
  128. u32 msg_enable;
  129. } board_info_t;

  130. /* function declaration ------------------------------------- */
  131. static int dm9000_probe(struct platform_device *);
  132. static int dm9000_open(struct net_device *);
  133. static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
  134. static int dm9000_stop(struct net_device *);


  135. static void dm9000_timer(unsigned long);
  136. static void dm9000_init_dm9000(struct net_device *);

  137. static struct net_device_stats *dm9000_get_stats(struct net_device *);

  138. static irqreturn_t dm9000_interrupt(int, void *);

  139. static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
  140. static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
  141.       int value);
  142. static u16 read_srom_word(board_info_t *, int);
  143. static void dm9000_rx(struct net_device *);
  144. static void dm9000_hash_table(struct net_device *);

  145. //#define DM9000_PROGRAM_EEPROM
  146. #ifdef DM9000_PROGRAM_EEPROM
  147. static void program_eeprom(board_info_t * db);
  148. #endif
  149. /* DM9000 network board routine ---------------------------- */

  150. static void
  151. dm9000_reset(board_info_t * db)
  152. {
  153. PRINTK1("dm9000x: resetting\n");
  154. /* RESET device */
  155. /*指定DM9000当前的命令寄存器是NCR*/
  156. writeb(DM9000_NCR, db->io_addr);
  157. udelay(200);
  158. /*向NCR寄存器写入复位标志,芯片复位*/
  159. writeb(NCR_RST, db->io_data);
  160. udelay(200);
  161. }

  162. /*
  163. *   Read a byte from I/O port
  164.      读取寄存器的值,reg表明寄存器的偏移量
  165.      这里用宏名表示
  166. */

  167. static u8
  168. ior(board_info_t * db, int reg)
  169. {
  170. writeb(reg, db->io_addr);
  171. return readb(db->io_data);
  172. }

  173. /*
  174. *   Write a byte to I/O port
  175.     写寄存器的值,reg表明寄存器的偏移量
  176.      这里用宏名表示,value是写入数据
  177. */

  178. static void
  179. iow(board_info_t * db, int reg, int value)
  180. {
  181. writeb(reg, db->io_addr);
  182. writeb(value, db->io_data);
  183. }

  184. /* routines for sending block to chip */
  185. /*类似于内存拷贝
  186. 简单的理解为memcpy(reg,data,count)
  187. count是以字节为单位的!
  188. */
  189. static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
  190. {
  191. writesb(reg, data, count);
  192. }

  193. static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
  194. {
  195. writesw(reg, data, (count+1) >> 1);
  196. }

  197. static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
  198. {
  199. writesl(reg, data, (count+3) >> 2);
  200. }

  201. /* input block from chip to memory */
  202. /*类似于内存拷贝
  203. 简单的理解为memcpy(data,reg,count)
  204. count是以字节为单位的!
  205. */
  206. static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
  207. {
  208. readsb(reg, data, count);
  209. }


  210. static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
  211. {
  212. readsw(reg, data, (count+1) >> 1);
  213. }

  214. static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
  215. {
  216. readsl(reg, data, (count+3) >> 2);
  217. }

  218. /* dump block from chip to null */
  219. /*类似于内存清理,将reg指向的count清空。
  220.   多用于设备的接收缓存区清空
  221. count是以字节为单位的!
  222. */
  223. static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
  224. {
  225. int i;
  226. int tmp;

  227. for (i = 0; i < count; i++)
  228.   tmp = readb(reg);
  229. }

  230. static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
  231. {
  232. int i;
  233. int tmp;

  234. count = (count + 1) >> 1;

  235. for (i = 0; i < count; i++)
  236.   tmp = readw(reg);
  237. }

  238. static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
  239. {
  240. int i;
  241. int tmp;

  242. count = (count + 3) >> 2;

  243. for (i = 0; i < count; i++)
  244.   tmp = readl(reg);
  245. }



复制代码

最新回复

mark  详情 回复 发表于 2010-4-21 13:06
点赞 关注

回复
举报

78

帖子

0

TA的资源

一粒金砂(初级)

沙发
 

  1. /* dm9000_set_io
  2. *
  3. * select the specified set of io routines to use with the
  4. * device
  5. 根据芯片的外部总线带宽,指定对应的输入输出函数
  6. */

  7. static void dm9000_set_io(struct board_info *db, int byte_width)
  8. {
  9. /* use the size of the data resource to work out what IO
  10.   * routines we want to use
  11.   */

  12. switch (byte_width) {
  13. case 1:
  14.   db->dumpblk = dm9000_dumpblk_8bit;
  15.   db->outblk  = dm9000_outblk_8bit;
  16.   db->inblk   = dm9000_inblk_8bit;
  17.   break;

  18. case 2:
  19.   db->dumpblk = dm9000_dumpblk_16bit;
  20.   db->outblk  = dm9000_outblk_16bit;
  21.   db->inblk   = dm9000_inblk_16bit;
  22.   break;

  23. case 3:
  24.   printk(KERN_ERR PFX ": 3 byte IO, falling back to 16bit\n");
  25.   db->dumpblk = dm9000_dumpblk_16bit;
  26.   db->outblk  = dm9000_outblk_16bit;
  27.   db->inblk   = dm9000_inblk_16bit;
  28.   break;

  29. case 4:
  30. default:
  31.   db->dumpblk = dm9000_dumpblk_32bit;
  32.   db->outblk  = dm9000_outblk_32bit;
  33.   db->inblk   = dm9000_inblk_32bit;
  34.   break;
  35. }
  36. }

  37. /*超时处理函数*/
  38. /* Our watchdog timed out. Called by the networking layer */
  39. static void dm9000_timeout(struct net_device *dev)
  40. {
  41. board_info_t *db = (board_info_t *) dev->priv;
  42. u8 reg_save;
  43. unsigned long flags;

  44. /* Save previous register address */
  45. reg_save = readb(db->io_addr);
  46. spin_lock_irqsave(&db->lock,flags);
  47. /*停止发送报文 */
  48. netif_stop_queue(dev);
  49. /*复位DM9000芯片*/
  50. dm9000_reset(db);
  51. /*初始化DM9000芯片*/
  52. dm9000_init_dm9000(dev);
  53. /* We can accept TX packets again */
  54. /*重启发送*/
  55. dev->trans_start = jiffies;
  56. netif_wake_queue(dev);

  57. /* Restore previous register address */
  58. /*io_addr的恢复,保证了芯片的配置和初始值一致*/
  59. writeb(reg_save, db->io_addr);
  60. spin_unlock_irqrestore(&db->lock,flags);
  61. }

  62. #ifdef CONFIG_NET_POLL_CONTROLLER
  63. /*
  64. *Used by netconsole
  65. */
  66. static void dm9000_poll_controller(struct net_device *dev)
  67. {
  68. disable_irq(dev->irq);
  69. dm9000_interrupt(dev->irq,dev);
  70. enable_irq(dev->irq);
  71. }
  72. #endif

  73. /* dm9000_release_board
  74. *
  75. * release a board, and any mapped resources
  76.     释放资源
  77. */

  78. static void
  79. dm9000_release_board(struct platform_device *pdev, struct board_info *db)
  80. {
  81. if (db->data_res == NULL) {
  82.   if (db->addr_res != NULL)
  83.    release_mem_region((unsigned long)db->io_addr, 4);
  84.   return;
  85. }

  86. /* unmap our resources */

  87. iounmap(db->io_addr);
  88. iounmap(db->io_data);

  89. /* release the resources */

  90. if (db->data_req != NULL) {
  91.   release_resource(db->data_req);
  92.   kfree(db->data_req);
  93. }

  94. if (db->addr_req != NULL) {
  95.   release_resource(db->addr_req);
  96.   kfree(db->addr_req);
  97. }
  98. }

  99. #define res_size(_r) (((_r)->end - (_r)->start) + 1)

  100. /*
  101. * Search DM9000 board, allocate space and register it
  102. */
  103. static int
  104. dm9000_probe(struct platform_device *pdev)
  105. {
  106. struct dm9000_plat_data *pdata = pdev->dev.platform_data;
  107. struct board_info *db; /* Point a board information structure */
  108. struct net_device *ndev;
  109. unsigned long base;
  110. int ret = 0;
  111. int iosize;
  112. int i;
  113. u32 id_val;

  114. /* 分配eth网卡资源,私有数据区保存board_info*/
  115. ndev = alloc_etherdev(sizeof (struct board_info));
  116. if (!ndev) {
  117.   printk("%s: could not allocate device.\n", CARDNAME);
  118.   return -ENOMEM;
  119. }
  120. /*忽略*/
  121. SET_MODULE_OWNER(ndev);
  122. SET_NETDEV_DEV(ndev, &pdev->dev);

  123. PRINTK2("dm9000_probe()");

  124. /* setup board info structure
  125. bord info初始化为0  */
  126. db = (struct board_info *) ndev->priv;
  127. memset(db, 0, sizeof (*db));
  128. /*初始化spinlock*/
  129. spin_lock_init(&db->lock);

  130. /*忽略*/
  131. if (pdev->num_resources < 2) {
  132.   ret = -ENODEV;
  133.   goto out;
  134. } else if (pdev->num_resources == 2) {
  135.   base = pdev->resource[0].start;

  136.   if (!request_mem_region(base, 4, ndev->name)) {
  137.    ret = -EBUSY;
  138.    goto out;
  139.   }

  140.   ndev->base_addr = base;
  141.   ndev->irq = pdev->resource[1].start;
  142.   db->io_addr = (void __iomem *)base;
  143.   db->io_data = (void __iomem *)(base + 4);

  144. } else {
  145.   db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  146.   db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  147.   db->irq_res  = platform_get_resource(pdev, IORESOURCE_IRQ, 0);

  148.   if (db->addr_res == NULL || db->data_res == NULL ||
  149.       db->irq_res == NULL) {
  150.    printk(KERN_ERR PFX "insufficient resources\n");
  151.    ret = -ENOENT;
  152.    goto out;
  153.   }

  154.   i = res_size(db->addr_res);
  155.   db->addr_req = request_mem_region(db->addr_res->start, i,
  156.         pdev->name);

  157.   if (db->addr_req == NULL) {
  158.    printk(KERN_ERR PFX "cannot claim address reg area\n");
  159.    ret = -EIO;
  160.    goto out;
  161.   }

  162.   db->io_addr = ioremap(db->addr_res->start, i);

  163.   if (db->io_addr == NULL) {
  164.    printk(KERN_ERR "failed to ioremap address reg\n");
  165.    ret = -EINVAL;
  166.    goto out;
  167.   }

  168.   iosize = res_size(db->data_res);
  169.   db->data_req = request_mem_region(db->data_res->start, iosize,
  170.         pdev->name);

  171.   if (db->data_req == NULL) {
  172.    printk(KERN_ERR PFX "cannot claim data reg area\n");
  173.    ret = -EIO;
  174.    goto out;
  175.   }

  176. db->io_data = ioremap(db->data_res->start, iosize);

  177.   if (db->io_data == NULL) {
  178.    printk(KERN_ERR "failed to ioremap data reg\n");
  179.    ret = -EINVAL;
  180.    goto out;
  181.   }

  182.   /* fill in parameters for net-dev structure */

  183.   ndev->base_addr = (unsigned long)db->io_addr;
  184.   ndev->irq = db->irq_res->start;

  185.   /* ensure at least we have a default set of IO routines */
  186.   dm9000_set_io(db, iosize);
  187. }

  188. /* check to see if anything is being over-ridden */
  189. if (pdata != NULL) {
  190.   /* check to see if the driver wants to over-ride the
  191.    * default IO width */

  192.   if (pdata->flags & DM9000_PLATF_8BITONLY)
  193.    dm9000_set_io(db, 1);

  194.   if (pdata->flags & DM9000_PLATF_16BITONLY)
  195.    dm9000_set_io(db, 2);

  196.   if (pdata->flags & DM9000_PLATF_32BITONLY)
  197.    dm9000_set_io(db, 4);

  198.   /* check to see if there are any IO routine
  199.    * over-rides */

  200.   if (pdata->inblk != NULL)
  201.    db->inblk = pdata->inblk;

  202.   if (pdata->outblk != NULL)
  203.    db->outblk = pdata->outblk;

  204.   if (pdata->dumpblk != NULL)
  205.    db->dumpblk = pdata->dumpblk;
  206. }
  207. /*根据board info信息,复位DM9000芯片*/
  208. dm9000_reset(db);

  209. /* try two times, DM9000 sometimes gets the first read wrong */
  210. for (i = 0; i < 2; i++) {
  211.   id_val  = ior(db, DM9000_VIDL);
  212.   id_val |= (u32)ior(db, DM9000_VIDH) << 8;
  213.   id_val |= (u32)ior(db, DM9000_PIDL) << 16;
  214.   id_val |= (u32)ior(db, DM9000_PIDH) << 24;

  215.   if (id_val == DM9000_ID)
  216.    break;
  217.   printk("%s: read wrong id 0x%08x\n", CARDNAME, id_val);
  218. }
  219. /*芯片的ID获取失败,驱动不匹配*/
  220. if (id_val != DM9000_ID) {
  221.   printk("%s: wrong id: 0x%08x\n", CARDNAME, id_val);
  222.   goto release;
  223. }

  224. /* from this point we assume that we have found a DM9000 */

  225. /* driver system function
  226. 将DM9000作为以太网卡初始化内部数据结构*/
  227. ether_setup(ndev);

  228. ndev->open   = &dm9000_open;
  229. ndev->hard_start_xmit    = &dm9000_start_xmit;
  230. ndev->tx_timeout         = &dm9000_timeout;
  231. ndev->watchdog_timeo = msecs_to_jiffies(watchdog); /*超时值*/
  232. ndev->stop   = &dm9000_stop;
  233. ndev->get_stats   = &dm9000_get_stats;
  234. ndev->set_multicast_list = &dm9000_hash_table;
  235. #ifdef CONFIG_NET_POLL_CONTROLLER
  236. ndev->poll_controller  = &dm9000_poll_controller;
  237. #endif

  238. #ifdef DM9000_PROGRAM_EEPROM
  239. /*开发时,需要更新EERPOM的值*/
  240. program_eeprom(db);
  241. #endif
  242. /*忽略*/
  243. db->msg_enable       = NETIF_MSG_LINK;
  244. db->mii.phy_id_mask  = 0x1f;
  245. db->mii.reg_num_mask = 0x1f;
  246. db->mii.force_media  = 0;
  247. db->mii.full_duplex  = 0;
  248. db->mii.dev      = ndev;
  249. db->mii.mdio_read    = dm9000_phy_read;  /*DM9000可以提供MII,外接PHY*/
  250. db->mii.mdio_write   = dm9000_phy_write;

  251. /* 获取网卡上EEPROM9346的内容,走EERPOM接口 */
  252. for (i = 0; i < 64; i++)
  253.   /*采用16bit读取方式*/
  254.   ((u16 *) db->srom)[i] = read_srom_word(db, i);

  255. /* Set Node Address
  256.     EEPROM的前6个字节为MAC地址
  257. */
  258. for (i = 0; i < 6; i++)
  259.   ndev->dev_addr[i] = db->srom[i];
  260. /*验证获取的MAC地址是否合法*/
  261. if (!is_valid_ether_addr(ndev->dev_addr)) {
  262.   /* try reading from mac */
  263.   /*不合法,重新从DM9000的内部寄存器获取MAC,不走EEPROM接口*/
  264.   for (i = 0; i < 6; i++)
  265.    ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
  266. }
  267. /*两种方式都失败,提示用ifconfig,驱动没有对应接口!*/
  268. if (!is_valid_ether_addr(ndev->dev_addr))
  269.   printk("%s: Invalid ethernet MAC address.  Please "
  270.          "set using ifconfig\n", ndev->name);

  271. platform_set_drvdata(pdev, ndev);
  272. /*注册接口到系统中,状态默认down,不可用*/
  273. ret = register_netdev(ndev);

  274. if (ret == 0) {
  275.   printk("%s: dm9000 at %p,%p IRQ %d MAC: ",
  276.          ndev->name,  db->io_addr, db->io_data, ndev->irq);
  277.   for (i = 0; i < 5; i++)
  278.    printk("%02x:", ndev->dev_addr[i]);
  279.   printk("%02x\n", ndev->dev_addr[5]);
  280. }
  281. return 0;

  282. release:
  283. out:
  284. printk("%s: not found (%d).\n", CARDNAME, ret);
  285. /失败时,释放资源*/
  286. dm9000_release_board(pdev, db);
  287. free_netdev(ndev);

  288. return ret;
  289. }

复制代码
 
 

回复

66

帖子

0

TA的资源

一粒金砂(初级)

板凳
 

  1. /*
  2. *  Open the interface.
  3. *  The interface is opened whenever "ifconfig" actives it.
  4.      当使用ifconfig激活该网络接口时调用
  5. */
  6. static int
  7. dm9000_open(struct net_device *dev)
  8. {
  9. board_info_t *db = (board_info_t *) dev->priv;

  10. PRINTK2("entering dm9000_open\n");
  11. /*申请中断资源,注册中断函数*/
  12. if (request_irq(dev->irq, &dm9000_interrupt, IRQF_SHARED, dev->name, dev))
  13.   return -EAGAIN;

  14. /* Initialize DM9000 board */
  15. /*复位DM9000芯片*/
  16. dm9000_reset(db);
  17. /*配置DM9000芯片内寄存器,使其能工作*/
  18. dm9000_init_dm9000(dev);

  19. /* Init driver variable */
  20. db->dbug_cnt = 0;

  21. /* set and active a timer process */
  22. init_timer(&db->timer);   /*初始化内核定时器*/
  23. db->timer.expires  = DM9000_TIMER_WUT; /*2s唤醒一次*/
  24. db->timer.data     = (unsigned long) dev;
  25. db->timer.function = &dm9000_timer; /*内核定时器溢出回调函数*/
  26. add_timer(&db->timer);  /*启动内核定时器*/
  27. /*检查mii接口状态*/
  28. mii_check_media(&db->mii, netif_msg_link(db), 1);
  29. /*启动发送队列*/
  30. netif_start_queue(dev);

  31. return 0;
  32. }





  33. /*
  34. * Initilize dm9000 board
  35.    配置DM9000芯片内部寄存器,使其能工作
  36. */
  37. static void
  38. dm9000_init_dm9000(struct net_device *dev)
  39. {
  40. board_info_t *db = (board_info_t *) dev->priv;

  41. PRINTK1("entering %s\n",__FUNCTION__);

  42. /* I/O mode
  43. 检查当前芯片的总线带宽8bit,16bit,32bit
  44. 芯片的总线带宽由硬件走线决定 */
  45. db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */

  46. /* GPIO0 on pre-activate PHY
  47. 启动PHY*/
  48. iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
  49. iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
  50. iow(db, DM9000_GPR, 0); /* Enable PHY */

  51. /* Program operating register */
  52. /*清除发送配置选项*/
  53. iow(db, DM9000_TCR, 0);         /* TX Polling clear */
  54. iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
  55. iow(db, DM9000_FCR, 0xff); /* Flow Control */
  56. iow(db, DM9000_SMCR, 0);        /* Special Mode */
  57. /* clear TX status */
  58. /*清除发送标志位*/
  59. iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
  60. /*清除中断标志位*/
  61. iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */

  62. /* Set address filter table */
  63. dm9000_hash_table(dev);

  64. /* Activate DM9000
  65.    接收包大小不能超过1.5k,CRC错误的抛弃,使能接收*/
  66. iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
  67. /* Enable TX/RX interrupt mask
  68.   使用发送,接收中断。SRAM缓冲区指针自动回0 */
  69. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);

  70. /* Init Driver variable */
  71. db->tx_pkt_cnt = 0;
  72. db->queue_pkt_len = 0;
  73. dev->trans_start = 0;
  74. }

  75. /*
  76. *  Hardware start transmission.
  77. *  Send a packet to media from the upper layer.
  78. */
  79. static int
  80. dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
  81. {
  82. board_info_t *db = (board_info_t *) dev->priv;

  83. PRINTK3("dm9000_start_xmit\n");

  84. if (db->tx_pkt_cnt > 1)
  85.   return 1;
  86. /*停止接收*/
  87. netif_stop_queue(dev);

  88. /* Disable all interrupts
  89. 关闭发送,接收中断*/
  90. iow(db, DM9000_IMR, IMR_PAR);

  91. /* Move data to DM9000 TX RAM */
  92. writeb(DM9000_MWCMD, db->io_addr);
  93. /*把skb的数据拷贝到DM9000的SRAM中*/
  94. (db->outblk)(db->io_data, skb->data, skb->len);
  95. /*统计值加上相应的长度*/
  96. db->stats.tx_bytes += skb->len;

  97. /* TX control: First packet immediately send, second packet queue */
  98. if (db->tx_pkt_cnt == 0) {

  99.   /* First Packet
  100.   发送的包增加*/
  101.   db->tx_pkt_cnt++;

  102.   /* Set TX length to DM9000
  103.   设定DM9000需要发送的包长度*/
  104.   iow(db, DM9000_TXPLL, skb->len & 0xff);
  105.   iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);

  106.   /* Issue TX polling command
  107.   设定发送请求,即启动发送*/
  108.   iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
  109.   /*启动时间*/
  110.   dev->trans_start = jiffies; /* save the time stamp */

  111. } else {
  112.   /*第二个包发送,在tx_done中实现*/
  113.   /* Second packet */
  114.   db->tx_pkt_cnt++;
  115.   db->queue_pkt_len = skb->len;
  116. }

  117. /* free this SKB
  118. 发送完毕释放skb */
  119. dev_kfree_skb(skb);

  120. /* Re-enable resource check
  121. 第一个包发送完毕,即内存拷贝完毕,不是实际总线上的
  122. 可以启动第二包拷贝到SRAM*/
  123. if (db->tx_pkt_cnt == 1)
  124.   netif_wake_queue(dev);

  125. /* Re-enable interrupt
  126. 重新启用中断,这时DM9000若发送完成,会产生发送完毕中断*/
  127. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);

  128. return 0;
  129. }

  130. static void
  131. dm9000_shutdown(struct net_device *dev)
  132. {
  133. board_info_t *db = (board_info_t *) dev->priv;

  134. /* RESET device */
  135. dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
  136. /*关闭PHY*/
  137. iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
  138. /*屏蔽所有中断*/
  139. iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
  140. /*禁用接收*/
  141. iow(db, DM9000_RCR, 0x00); /* Disable RX */
  142. }

  143. /*
  144. * Stop the interface.
  145. * The interface is stopped when it is brought.
  146. */
  147. static int
  148. dm9000_stop(struct net_device *ndev)
  149. {
  150. board_info_t *db = (board_info_t *) ndev->priv;

  151. PRINTK1("entering %s\n",__FUNCTION__);

  152. /* deleted timer
  153. 删除内核定时器*/
  154. del_timer(&db->timer);
  155. /*通知内核队列不可用*/
  156. netif_stop_queue(ndev);
  157. /*linkstat 状态断路*/
  158. netif_carrier_off(ndev);

  159. /* free interrupt
  160. 释放中断*/
  161. free_irq(ndev->irq, ndev);
  162. /*硬件上关闭芯片*/
  163. dm9000_shutdown(ndev);

  164. return 0;
  165. }

  166. /*
  167. * DM9000 interrupt handler
  168. * receive the packet to upper layer, free the transmitted packet
  169. */

  170. static void
  171. dm9000_tx_done(struct net_device *dev, board_info_t * db)
  172. {
  173. int tx_status = ior(db,

  174. DM9000_NSR); /* Got TX status */

  175. /*一个包已经发送完毕*/
  176. if (tx_status & (NSR_TX2END | NSR_TX1END)) {
  177.   /* One packet sent complete */
  178.   db->tx_pkt_cnt--;
  179.   db->stats.tx_packets++;

  180.   /* Queue packet check & send
  181.   当前还有一个包未发送,且在缓冲区中,是第二个包*/
  182.   if (db->tx_pkt_cnt > 0) {
  183.    /*设定DM9000发送包的长度*/
  184.    iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
  185.    iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
  186.    /*启动发送*/
  187.    iow(db, DM9000_TCR, TCR_TXREQ);
  188.    dev->trans_start = jiffies;
  189.   }
  190.   /*通知内核*/
  191.   netif_wake_queue(dev);
  192. }
  193. }

  194. static irqreturn_t
  195. dm9000_interrupt(int irq, void *dev_id)
  196. {
  197. struct net_device *dev = dev_id;
  198. board_info_t *db;
  199. int int_status;
  200. u8 reg_save;

  201. PRINTK3("entering %s\n",__FUNCTION__);

  202. if (!dev) {
  203.   PRINTK1("dm9000_interrupt() without DEVICE arg\n");
  204.   return IRQ_HANDLED;
  205. }

  206. /* A real interrupt coming */
  207. db = (board_info_t *) dev->priv;
  208. spin_lock(&db->lock);

  209. /* Save previous register address */
  210. /*保存寄存器值*/
  211. reg_save = readb(db->io_addr);

  212. /* Disable all interrupts
  213. 关闭所有中断*/
  214. iow(db, DM9000_IMR, IMR_PAR);

  215. /* Got DM9000 interrupt status */
  216. int_status = ior(db, DM9000_ISR); /* Got ISR */
  217. iow(db, DM9000_ISR, int_status); /* Clear ISR status */

  218. /* Received the coming packet
  219. 接收包中断*/
  220. if (int_status & ISR_PRS)
  221.   dm9000_rx(dev);

  222. /* Trnasmit Interrupt check
  223. 发送包中断*/
  224. if (int_status & ISR_PTS)
  225.   dm9000_tx_done(dev, db);

  226. /* Re-enable interrupt mask */
  227. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);

  228. /* Restore previous register address */
  229. writeb(reg_save, db->io_addr);

  230. spin_unlock(&db->lock);

  231. return IRQ_HANDLED;
  232. }

  233. /*
  234. *  Get statistics from driver.
  235. 获取网卡接口统计信息
  236. */
  237. static struct net_device_stats *
  238. dm9000_get_stats(struct net_device *dev)
  239. {
  240. board_info_t *db = (board_info_t *) dev->priv;
  241. return &db->stats;
  242. }


  243. /*
  244. *  A periodic timer routine
  245. *  Dynamic media sense, allocated Rx buffer...
  246. */
  247. static void
  248. dm9000_timer(unsigned long data)
  249. {
  250. struct net_device *dev = (struct net_device *) data;
  251. board_info_t *db = (board_info_t *) dev->priv;

  252. PRINTK3("dm9000_timer()\n");
  253. /*检查mii接口*/
  254. mii_check_media(&db->mii, netif_msg_link(db), 0);

  255. /* Set timer again
  256. 重新启动内核定时器,周期检查mii接口 */
  257. db->timer.expires = DM9000_TIMER_WUT;
  258. add_timer(&db->timer);
  259. }
  260. /*DM9000接收包头,芯片提供
  261.   01,status,length low,length high*/
  262. struct dm9000_rxhdr {
  263. u16 RxStatus;  
  264. u16 RxLen;
  265. } __attribute__((__packed__));


复制代码
 
 
 

回复

85

帖子

0

TA的资源

一粒金砂(初级)

4
 

  1. /*
  2. *  Open the interface.
  3. *  The interface is opened whenever "ifconfig" actives it.
  4.      当使用ifconfig激活该网络接口时调用
  5. */
  6. static int
  7. dm9000_open(struct net_device *dev)
  8. {
  9. board_info_t *db = (board_info_t *) dev->priv;

  10. PRINTK2("entering dm9000_open\n");
  11. /*申请中断资源,注册中断函数*/
  12. if (request_irq(dev->irq, &dm9000_interrupt, IRQF_SHARED, dev->name, dev))
  13.   return -EAGAIN;

  14. /* Initialize DM9000 board */
  15. /*复位DM9000芯片*/
  16. dm9000_reset(db);
  17. /*配置DM9000芯片内寄存器,使其能工作*/
  18. dm9000_init_dm9000(dev);

  19. /* Init driver variable */
  20. db->dbug_cnt = 0;

  21. /* set and active a timer process */
  22. init_timer(&db->timer);   /*初始化内核定时器*/
  23. db->timer.expires  = DM9000_TIMER_WUT; /*2s唤醒一次*/
  24. db->timer.data     = (unsigned long) dev;
  25. db->timer.function = &dm9000_timer; /*内核定时器溢出回调函数*/
  26. add_timer(&db->timer);  /*启动内核定时器*/
  27. /*检查mii接口状态*/
  28. mii_check_media(&db->mii, netif_msg_link(db), 1);
  29. /*启动发送队列*/
  30. netif_start_queue(dev);

  31. return 0;
  32. }





  33. /*
  34. * Initilize dm9000 board
  35.    配置DM9000芯片内部寄存器,使其能工作
  36. */
  37. static void
  38. dm9000_init_dm9000(struct net_device *dev)
  39. {
  40. board_info_t *db = (board_info_t *) dev->priv;

  41. PRINTK1("entering %s\n",__FUNCTION__);

  42. /* I/O mode
  43. 检查当前芯片的总线带宽8bit,16bit,32bit
  44. 芯片的总线带宽由硬件走线决定 */
  45. db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */

  46. /* GPIO0 on pre-activate PHY
  47. 启动PHY*/
  48. iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
  49. iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
  50. iow(db, DM9000_GPR, 0); /* Enable PHY */

  51. /* Program operating register */
  52. /*清除发送配置选项*/
  53. iow(db, DM9000_TCR, 0);         /* TX Polling clear */
  54. iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
  55. iow(db, DM9000_FCR, 0xff); /* Flow Control */
  56. iow(db, DM9000_SMCR, 0);        /* Special Mode */
  57. /* clear TX status */
  58. /*清除发送标志位*/
  59. iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
  60. /*清除中断标志位*/
  61. iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */

  62. /* Set address filter table */
  63. dm9000_hash_table(dev);

  64. /* Activate DM9000
  65.    接收包大小不能超过1.5k,CRC错误的抛弃,使能接收*/
  66. iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
  67. /* Enable TX/RX interrupt mask
  68.   使用发送,接收中断。SRAM缓冲区指针自动回0 */
  69. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);

  70. /* Init Driver variable */
  71. db->tx_pkt_cnt = 0;
  72. db->queue_pkt_len = 0;
  73. dev->trans_start = 0;
  74. }

  75. /*
  76. *  Hardware start transmission.
  77. *  Send a packet to media from the upper layer.
  78. */
  79. static int
  80. dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
  81. {
  82. board_info_t *db = (board_info_t *) dev->priv;

  83. PRINTK3("dm9000_start_xmit\n");

  84. if (db->tx_pkt_cnt > 1)
  85.   return 1;
  86. /*停止接收*/
  87. netif_stop_queue(dev);

  88. /* Disable all interrupts
  89. 关闭发送,接收中断*/
  90. iow(db, DM9000_IMR, IMR_PAR);

  91. /* Move data to DM9000 TX RAM */
  92. writeb(DM9000_MWCMD, db->io_addr);
  93. /*把skb的数据拷贝到DM9000的SRAM中*/
  94. (db->outblk)(db->io_data, skb->data, skb->len);
  95. /*统计值加上相应的长度*/
  96. db->stats.tx_bytes += skb->len;

  97. /* TX control: First packet immediately send, second packet queue */
  98. if (db->tx_pkt_cnt == 0) {

  99.   /* First Packet
  100.   发送的包增加*/
  101.   db->tx_pkt_cnt++;

  102.   /* Set TX length to DM9000
  103.   设定DM9000需要发送的包长度*/
  104.   iow(db, DM9000_TXPLL, skb->len & 0xff);
  105.   iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);

  106.   /* Issue TX polling command
  107.   设定发送请求,即启动发送*/
  108.   iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
  109.   /*启动时间*/
  110.   dev->trans_start = jiffies; /* save the time stamp */

  111. } else {
  112.   /*第二个包发送,在tx_done中实现*/
  113.   /* Second packet */
  114.   db->tx_pkt_cnt++;
  115.   db->queue_pkt_len = skb->len;
  116. }

  117. /* free this SKB
  118. 发送完毕释放skb */
  119. dev_kfree_skb(skb);

  120. /* Re-enable resource check
  121. 第一个包发送完毕,即内存拷贝完毕,不是实际总线上的
  122. 可以启动第二包拷贝到SRAM*/
  123. if (db->tx_pkt_cnt == 1)
  124.   netif_wake_queue(dev);

  125. /* Re-enable interrupt
  126. 重新启用中断,这时DM9000若发送完成,会产生发送完毕中断*/
  127. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);

  128. return 0;
  129. }

  130. static void
  131. dm9000_shutdown(struct net_device *dev)
  132. {
  133. board_info_t *db = (board_info_t *) dev->priv;

  134. /* RESET device */
  135. dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
  136. /*关闭PHY*/
  137. iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
  138. /*屏蔽所有中断*/
  139. iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
  140. /*禁用接收*/
  141. iow(db, DM9000_RCR, 0x00); /* Disable RX */
  142. }

  143. /*
  144. * Stop the interface.
  145. * The interface is stopped when it is brought.
  146. */
  147. static int
  148. dm9000_stop(struct net_device *ndev)
  149. {
  150. board_info_t *db = (board_info_t *) ndev->priv;

  151. PRINTK1("entering %s\n",__FUNCTION__);

  152. /* deleted timer
  153. 删除内核定时器*/
  154. del_timer(&db->timer);
  155. /*通知内核队列不可用*/
  156. netif_stop_queue(ndev);
  157. /*linkstat 状态断路*/
  158. netif_carrier_off(ndev);

  159. /* free interrupt
  160. 释放中断*/
  161. free_irq(ndev->irq, ndev);
  162. /*硬件上关闭芯片*/
  163. dm9000_shutdown(ndev);

  164. return 0;
  165. }

  166. /*
  167. * DM9000 interrupt handler
  168. * receive the packet to upper layer, free the transmitted packet
  169. */

  170. static void
  171. dm9000_tx_done(struct net_device *dev, board_info_t * db)
  172. {
  173. int tx_status = ior(db,

  174. DM9000_NSR); /* Got TX status */

  175. /*一个包已经发送完毕*/
  176. if (tx_status & (NSR_TX2END | NSR_TX1END)) {
  177.   /* One packet sent complete */
  178.   db->tx_pkt_cnt--;
  179.   db->stats.tx_packets++;

  180.   /* Queue packet check & send
  181.   当前还有一个包未发送,且在缓冲区中,是第二个包*/
  182.   if (db->tx_pkt_cnt > 0) {
  183.    /*设定DM9000发送包的长度*/
  184.    iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
  185.    iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
  186.    /*启动发送*/
  187.    iow(db, DM9000_TCR, TCR_TXREQ);
  188.    dev->trans_start = jiffies;
  189.   }
  190.   /*通知内核*/
  191.   netif_wake_queue(dev);
  192. }
  193. }

  194. static irqreturn_t
  195. dm9000_interrupt(int irq, void *dev_id)
  196. {
  197. struct net_device *dev = dev_id;
  198. board_info_t *db;
  199. int int_status;
  200. u8 reg_save;

  201. PRINTK3("entering %s\n",__FUNCTION__);

  202. if (!dev) {
  203.   PRINTK1("dm9000_interrupt() without DEVICE arg\n");
  204.   return IRQ_HANDLED;
  205. }

  206. /* A real interrupt coming */
  207. db = (board_info_t *) dev->priv;
  208. spin_lock(&db->lock);

  209. /* Save previous register address */
  210. /*保存寄存器值*/
  211. reg_save = readb(db->io_addr);

  212. /* Disable all interrupts
  213. 关闭所有中断*/
  214. iow(db, DM9000_IMR, IMR_PAR);

  215. /* Got DM9000 interrupt status */
  216. int_status = ior(db, DM9000_ISR); /* Got ISR */
  217. iow(db, DM9000_ISR, int_status); /* Clear ISR status */

  218. /* Received the coming packet
  219. 接收包中断*/
  220. if (int_status & ISR_PRS)
  221.   dm9000_rx(dev);

  222. /* Trnasmit Interrupt check
  223. 发送包中断*/
  224. if (int_status & ISR_PTS)
  225.   dm9000_tx_done(dev, db);

  226. /* Re-enable interrupt mask */
  227. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);

  228. /* Restore previous register address */
  229. writeb(reg_save, db->io_addr);

  230. spin_unlock(&db->lock);

  231. return IRQ_HANDLED;
  232. }


复制代码
 
 
 

回复

66

帖子

0

TA的资源

一粒金砂(初级)

5
 
uping
 
 
 

回复

84

帖子

0

TA的资源

一粒金砂(初级)

6
 

  1. /*
  2. *  Get statistics from driver.
  3. 获取网卡接口统计信息
  4. */
  5. static struct net_device_stats *
  6. dm9000_get_stats(struct net_device *dev)
  7. {
  8. board_info_t *db = (board_info_t *) dev->priv;
  9. return &db->stats;
  10. }


  11. /*
  12. *  A periodic timer routine
  13. *  Dynamic media sense, allocated Rx buffer...
  14. */
  15. static void
  16. dm9000_timer(unsigned long data)
  17. {
  18. struct net_device *dev = (struct net_device *) data;
  19. board_info_t *db = (board_info_t *) dev->priv;

  20. PRINTK3("dm9000_timer()\n");
  21. /*检查mii接口*/
  22. mii_check_media(&db->mii, netif_msg_link(db), 0);

  23. /* Set timer again
  24. 重新启动内核定时器,周期检查mii接口 */
  25. db->timer.expires = DM9000_TIMER_WUT;
  26. add_timer(&db->timer);
  27. }
  28. /*DM9000接收包头,芯片提供
  29.   01,status,length low,length high*/
  30. struct dm9000_rxhdr {
  31. u16 RxStatus;  
  32. u16 RxLen;
  33. } __attribute__((__packed__));

  34. /*
  35. *  Received a packet and pass to upper layer
  36. */
  37. static void
  38. dm9000_rx(struct net_device *dev)
  39. {
  40. board_info_t *db = (board_info_t *) dev->priv;
  41. struct dm9000_rxhdr rxhdr;
  42. struct sk_buff *skb;
  43. u8 rxbyte, *rdptr;
  44. bool GoodPacket;
  45. int RxLen;

  46. /* Check packet ready or not */
  47. do {
  48.   ior(db, DM9000_MRCMDX); /* Dummy read */

  49.   /* Get most updated data */
  50.   rxbyte = readb(db->io_data);

  51.   /* Status check: this byte must be 0 or 1
  52.   接收包最长为1536 */
  53.   if (rxbyte > DM9000_PKT_RDY) {
  54.    printk("status check failed: %d\n", rxbyte);
  55.    iow(db, DM9000_RCR, 0x00); /* Stop Device */
  56.    iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
  57.    return;
  58.   }

  59.   if (rxbyte != DM9000_PKT_RDY)
  60.    return;

  61.   /* A packet ready now  & Get status/length */
  62.   GoodPacket = true;
  63.   writeb(DM9000_MRCMD, db->io_addr);
  64.   /*获取接收数据包头,共4字节,参考DM9000手册*/
  65.   (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
  66.   /*查看包长*/
  67.   RxLen = rxhdr.RxLen;

  68.   /* Packet Status check
  69.    包长不能小于64字节*/
  70.   if (RxLen < 0x40) {
  71.    GoodPacket = false;
  72.    PRINTK1("Bad Packet received (runt)\n");
  73.   }
  74.   /*包长不能大于1536字节*/
  75.   if (RxLen > DM9000_PKT_MAX) {
  76.    PRINTK1("RST: RX Len:%x\n", RxLen);
  77.   }
  78.   /*接收的包有错误*/
  79.   if (rxhdr.RxStatus & 0xbf00) {
  80.    GoodPacket = false;
  81.    if (rxhdr.RxStatus & 0x100) {
  82.     PRINTK1("fifo error\n");
  83.     db->stats.rx_fifo_errors++;
  84.    }
  85.    if (rxhdr.RxStatus & 0x200) {
  86.     PRINTK1("crc error\n");
  87.     db->stats.rx_crc_errors++;
  88.    }
  89.    if (rxhdr.RxStatus & 0x8000) {
  90.     PRINTK1("length error\n");
  91.     db->stats.rx_length_errors++;
  92.    }
  93.   }

  94.   /* Move data from DM9000
  95.   接收的包没有任何问题时*/
  96.   if (GoodPacket
  97.       && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {

  98. >      
  99.    skb_reserve(skb, 2);
  100.    rdptr = (u8 *) skb_put(skb, RxLen - 4);

  101.    /* Read received packet from RX SRAM */
  102.    /*数据从DM9000内存拷贝到skb*/
  103.    (db->inblk)(db->io_data, rdptr, RxLen);
  104.    /*更新接收字节数统计*/
  105.    db->stats.rx_bytes += RxLen;

  106.    /* Pass to upper layer */
  107.    skb->protocol = eth_type_trans(skb, dev);
  108.    /*通知上层有新的skb收到*/
  109.    netif_rx(skb);
  110.    /*接收包更新*/
  111.    db->stats.rx_packets++;

  112.   } else {
  113.    /* need to dump the packet's data */

  114.    (db->dumpblk)(db->io_data, RxLen);
  115.   }
  116. } while (rxbyte == DM9000_PKT_RDY);
  117. }

  118. /*
  119. *  Read a word data from SROM
  120.     从EEPROM9346中读取数据 16bit
  121. */
  122. static u16
  123. read_srom_word(board_info_t * db, int offset)
  124. {
  125. iow(db, DM9000_EPAR, offset);
  126. iow(db, DM9000_EPCR, EPCR_ERPRR);
  127. mdelay(8);  /* according to the datasheet 200us should be enough,
  128.        but it doesn't work */
  129. iow(db, DM9000_EPCR, 0x0);
  130. return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
  131. }

  132. #ifdef DM9000_PROGRAM_EEPROM
  133. /*
  134. * Write a word data to SROM
  135.   向EEPROM 9346中写数据 16bit
  136. */
  137. static void
  138. write_srom_word(board_info_t * db, int offset, u16 val)
  139. {
  140. iow(db, DM9000_EPAR, offset);
  141. iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
  142. iow(db, DM9000_EPDRL, (val & 0xff));
  143. iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
  144. mdelay(8);  /* same shit */
  145. iow(db, DM9000_EPCR, 0);
  146. }

  147. /*
  148. * Only for development:
  149. * Here we write static data to the eeprom in case
  150. * we don't have valid content on a new board
  151. */
  152. static void
  153. program_eeprom(board_info_t * db)
  154. {
  155. u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
  156.   0x0000,  /* Autoload: accept nothing */
  157.   0x0a46, 0x9000, /* Vendor / Product ID */
  158.   0x0000,  /* pin control */
  159.   0x0000,
  160. };   /* Wake-up mode control */
  161. int i;
  162. for (i = 0; i < 8; i++)
  163.   write_srom_word(db, i, eeprom[i]);
  164. }
  165. #endif


  166. /*
  167. *  Calculate the CRC valude of the Rx packet
  168. *  flag = 1 : return the reverse CRC (for the received packet CRC)
  169. *         0 : return the normal CRC (for Hash Table index)
  170.     计算接收包的crc值
  171. */

  172. static unsigned long
  173. cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
  174. {

  175.        u32 crc = ether_crc_le(Len, Data);

  176.        if (flag)
  177.                return ~crc;

  178.        return crc;
  179. }

  180. /*
  181. *  Set DM9000 multicast address
  182. */
  183. static void
  184. dm9000_hash_table(struct net_device *dev)
  185. {
  186. board_info_t *db = (board_info_t *) dev->priv;
  187. struct dev_mc_list *mcptr = dev->mc_list;
  188. int mc_cnt = dev->mc_count;
  189. u32 hash_val;
  190. u16 i, oft, hash_table[4];
  191. unsigned long flags;

  192. PRINTK2("dm9000_hash_table()\n");

  193. spin_lock_irqsave(&db->lock,flags);

  194. for (i = 0, oft = 0x10; i < 6; i++, oft++)
  195.   iow(db, oft, dev->dev_addr[i]);

  196. /* Clear Hash Table */
  197. for (i = 0; i < 4; i++)
  198.   hash_table[i] = 0x0;

  199. /* broadcast address */
  200. hash_table[3] = 0x8000;

  201. /* the multicast address in Hash Table : 64 bits */
  202. for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
  203.   hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
  204.   hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
  205. }

  206. /* Write the hash table to MAC MD table */
  207. for (i = 0, oft = 0x16; i < 4; i++) {
  208.   iow(db, oft++, hash_table[i] & 0xff);
  209.   iow(db, oft++, (hash_table[i] >> 8) & 0xff);
  210. }

  211. spin_unlock_irqrestore(&db->lock,flags);
  212. }


  213. /*
  214. *   Read a word from phyxcer
  215.    从PHY读取数据16bit
  216. */
  217. static int
  218. dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
  219. {
  220. board_info_t *db = (board_info_t *) dev->priv;
  221. unsigned long flags;
  222. unsigned int reg_save;
  223. int ret;

  224. spin_lock_irqsave(&db->lock,flags);

  225. /* Save previous register address */
  226. reg_save = readb(db->io_addr);

  227. /* Fill the phyxcer register into REG_0C */
  228. iow(db, DM9000_EPAR, DM9000_PHY | reg);

  229. iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
  230. udelay(100);  /* Wait read complete */
  231. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */

  232. /* The read data keeps on REG_0D & REG_0E */
  233. ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);

  234. /* restore the previous address */
  235. writeb(reg_save, db->io_addr);

  236. spin_unlock_irqrestore(&db->lock,flags);

  237. return ret;
  238. }

  239. /*
  240. *   Write a word to phyxcer
  241. 向PHY写入数据16bit
  242. */
  243. static void
  244. dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,

  245. int value)
  246. {
  247. board_info_t *db = (board_info_t *) dev->priv;
  248. unsigned long flags;
  249. unsigned long reg_save;

  250. spin_lock_irqsave(&db->lock,flags);

  251. /* Save previous register address */
  252. reg_save = readb(db->io_addr);

  253. /* Fill the phyxcer register into REG_0C */
  254. iow(db, DM9000_EPAR, DM9000_PHY | reg);

  255. /* Fill the written data into REG_0D & REG_0E */
  256. iow(db, DM9000_EPDRL, (value & 0xff));
  257. iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));

  258. iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
  259. udelay(500);  /* Wait write complete */
  260. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */

  261. /* restore the previous address */
  262. writeb(reg_save, db->io_addr);

  263. spin_unlock_irqrestore(&db->lock,flags);
  264. }

  265. /*挂起网卡接口*/
  266. static int
  267. dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
  268. {
  269. struct net_device *ndev = platform_get_drvdata(dev);

  270. if (ndev) {
  271.   if (netif_running(ndev)) {
  272.    /*内核临时删除接口*/
  273.    netif_device_detach(ndev);
  274.    /*关闭设备*/
  275.    dm9000_shutdown(ndev);
  276.   }
  277. }
  278. return 0;
  279. }

  280. static int
  281. dm9000_drv_resume(struct platform_device *dev)
  282. {
  283. struct net_device *ndev = platform_get_drvdata(dev);
  284. board_info_t *db = (board_info_t *) ndev->priv;

  285. if (ndev) {

  286.   if (netif_running(ndev)) {
  287.    /*复位设备*/
  288.    dm9000_reset(db);
  289.    dm9000_init_dm9000(ndev);
  290.    /*恢复接口*/
  291.    netif_device_attach(ndev);
  292.   }
  293. }
  294. return 0;
  295. }

  296. static int
  297. dm9000_drv_remove(struct platform_device *pdev)
  298. {
  299. struct net_device *ndev = platform_get_drvdata(pdev);

  300. platform_set_drvdata(pdev, NULL);
  301. /*释放注册的网络接口*/
  302. unregister_netdev(ndev);
  303. dm9000_release_board(pdev, (board_info_t *) ndev->priv);
  304. /*释放资源*/
  305. free_netdev(ndev);  /* free device structure */

  306. PRINTK1("clean_module() exit\n");

  307. return 0;
  308. }

  309. static struct platform_driver dm9000_driver = {
  310. .driver = {
  311.   .name    = "dm9000",
  312.   .owner  = THIS_MODULE,
  313. },
  314. .probe   = dm9000_probe,
  315. .remove  = dm9000_drv_remove,
  316. .suspend = dm9000_drv_suspend,
  317. .resume  = dm9000_drv_resume,
  318. };

  319. static int __init
  320. dm9000_init(void)
  321. {
  322. printk(KERN_INFO "%s Ethernet Driver\n", CARDNAME);

  323. return platform_driver_register(&dm9000_driver); /* search board and register */
  324. }

  325. static void __exit
  326. dm9000_cleanup(void)
  327. {
  328. platform_driver_unregister(&dm9000_driver);
  329. }

  330. module_init(dm9000_init);
  331. module_exit(dm9000_cleanup);


  332. MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
  333. MODULE_DESCRIPTION("Davicom DM9000 network driver");
  334. MODULE_LICENSE("GPL");
复制代码
 
 
 

回复

61

帖子

0

TA的资源

一粒金砂(初级)

7
 
您好,不知道前面各位的问题解了没,你们说的问题小弟并不太懂,但我们目前是divacom在中国区域的总代理,从03年来一直专注于做DM9000及DIVACOM全线产品,对于研发来说,首先是产品质量有保证(为当年全新原装最新版本),然后通过我们可以找到原厂的工程师做技术支持,当然,是免费提供的,说不定我能帮得上大家的忙,以下是我的联系方式    QQ:1259987438 电话:027-8765163610 (0)13554272382 王洪武
 
 
 

回复

68

帖子

0

TA的资源

一粒金砂(中级)

8
 
您好,不知道前面各位的问题解了没,你们说的问题小弟并不太懂,但我们目前是divacom在中国区域的总代理,从03年来一直专注于做DM9000及DIVACOM全线产品,对于研发来说,首先是产品质量有保证(为当年全新原装最新版本),然后通过我们可以找到原厂的工程师做技术支持,当然,是免费提供的,说不定我能帮得上大家的忙,以下是我的联系方式    QQ:1259987438 电话:027-87163610 (0)13554272382 王洪武
 
 
 

回复

86

帖子

0

TA的资源

一粒金砂(初级)

9
 
真不知道,这个有什么好的,就是源码喔,根本没什么解释!
 
 
 

回复

72

帖子

0

TA的资源

一粒金砂(初级)

10
 
您好,我是爱欣文科技的江练锋。
您提的DM芯片的问题,我可以帮助你解答。
关于DM9000AEP,DM9161AEP,数据手册,驱动,请联系我!
我们是DM芯片大陆的总代理商,有强大的技术支持。
我的联系方式:axwjiang@163.com   15827601937
 
 
 

回复

66

帖子

0

TA的资源

一粒金砂(初级)

11
 
mark
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表