转:DM9000驱动解析

  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 <weilun_huang@davicom.com.tw>:
  33. *     Fixed phy reset.
  34. *     Added tx/rx 32 bit mode.
  35. *     Cleaned up for kernel merge.
  36. *
  37. *        03/03/2004    Sascha Hauer <s.hauer@pengutronix.de>
  38. *                      Port to 2.6 kernel
  39. *
  40. *   24-Sep-2004   Ben Dooks <ben@simtec.co.uk>
  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 <s.hauer@pengutronix.de>
  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 <ben@simtec.co.uk>
  53. *    * fixed spinlock call without pointer
  54. *    * ensure spinlock is initialised
  55. */
  56. #include <linux/module.h>
  57. #include <linux/ioport.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/etherdevice.h>
  60. #include <linux/init.h>
  61. #include <linux/skbuff.h>
  62. #include <linux/spinlock.h>
  63. #include <linux/crc32.h>
  64. #include <linux/mii.h>
  65. #include <linux/dm9000.h>
  66. #include <linux/delay.h>
  67. #include <linux/platform_device.h>
  68. #include <asm/delay.h>
  69. #include <asm/irq.h>
  70. #include <asm/io.h>
  71. #include "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(intvoid *);
  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. }
  246. /* dm9000_set_io
  247. *
  248. * select the specified set of io routines to use with the
  249. * device
  250. 根据芯片的外部总线带宽,指定对应的输入输出函数
  251. */
  252. static void dm9000_set_io(struct board_info *db, int byte_width)
  253. {
  254. /* use the size of the data resource to work out what IO
  255. * routines we want to use
  256. */
  257. switch (byte_width) {
  258. case 1:
  259.    db->dumpblk = dm9000_dumpblk_8bit;
  260.    db->outblk = dm9000_outblk_8bit;
  261.    db->inblk   = dm9000_inblk_8bit;
  262.    break;
  263. case 2:
  264.    db->dumpblk = dm9000_dumpblk_16bit;
  265.    db->outblk = dm9000_outblk_16bit;
  266.    db->inblk   = dm9000_inblk_16bit;
  267.    break;
  268. case 3:
  269.    printk(KERN_ERR PFX ": 3 byte IO, falling back to 16bit/n");
  270.    db->dumpblk = dm9000_dumpblk_16bit;
  271.    db->outblk = dm9000_outblk_16bit;
  272.    db->inblk   = dm9000_inblk_16bit;
  273.    break;
  274. case 4:
  275. default:
  276.    db->dumpblk = dm9000_dumpblk_32bit;
  277.    db->outblk = dm9000_outblk_32bit;
  278.    db->inblk   = dm9000_inblk_32bit;
  279.    break;
  280. }
  281. }
  282. /*超时处理函数*/
  283. /* Our watchdog timed out. Called by the networking layer */
  284. static void dm9000_timeout(struct net_device *dev)
  285. {
  286. board_info_t *db = (board_info_t *) dev->priv;
  287. u8 reg_save;
  288. unsigned long flags;
  289. /* Save previous register address */
  290. reg_save = readb(db->io_addr);
  291. spin_lock_irqsave(&db->lock,flags);
  292. /*停止发送报文 */
  293. netif_stop_queue(dev);
  294. /*复位DM9000芯片*/
  295. dm9000_reset(db);
  296. /*初始化DM9000芯片*/
  297. dm9000_init_dm9000(dev);
  298. /* We can accept TX packets again */
  299. /*重启发送*/
  300. dev->trans_start = jiffies;
  301. netif_wake_queue(dev);
  302. /* Restore previous register address */
  303. /*io_addr的恢复,保证了芯片的配置和初始值一致*/
  304. writeb(reg_save, db->io_addr);
  305. spin_unlock_irqrestore(&db->lock,flags);
  306. }
  307. #ifdef CONFIG_NET_POLL_CONTROLLER
  308. /*
  309. *Used by netconsole
  310. */
  311. static void dm9000_poll_controller(struct net_device *dev)
  312. {
  313. disable_irq(dev->irq);
  314. dm9000_interrupt(dev->irq,dev);
  315. enable_irq(dev->irq);
  316. }
  317. #endif
  318. /* dm9000_release_board
  319. *
  320. * release a board, and any mapped resources
  321.     释放资源
  322. */
  323. static void
  324. dm9000_release_board(struct platform_device *pdev, struct board_info *db)
  325. {
  326. if (db->data_res == NULL) {
  327.    if (db->addr_res != NULL)
  328.     release_mem_region((unsigned long)db->io_addr, 4);
  329.    return;
  330. }
  331. /* unmap our resources */
  332. iounmap(db->io_addr);
  333. iounmap(db->io_data);
  334. /* release the resources */
  335. if (db->data_req != NULL) {
  336.    release_resource(db->data_req);
  337.    kfree(db->data_req);
  338. }
  339. if (db->addr_req != NULL) {
  340.    release_resource(db->addr_req);
  341.    kfree(db->addr_req);
  342. }
  343. }
  344. #define res_size(_r) (((_r)->end - (_r)->start) + 1)
  345. /*
  346. * Search DM9000 board, allocate space and register it
  347. */
  348. static int
  349. dm9000_probe(struct platform_device *pdev)
  350. {
  351. struct dm9000_plat_data *pdata = pdev->dev.platform_data;
  352. struct board_info *db; /* Point a board information structure */
  353. struct net_device *ndev;
  354. unsigned long base;
  355. int ret = 0;
  356. int iosize;
  357. int i;
  358. u32 id_val;
  359. /* 分配eth网卡资源,私有数据区保存board_info*/
  360. ndev = alloc_etherdev(sizeof (struct board_info));
  361. if (!ndev) {
  362.    printk("%s: could not allocate device./n", CARDNAME);
  363.    return -ENOMEM;
  364. }
  365. /*忽略*/
  366. SET_MODULE_OWNER(ndev);
  367. SET_NETDEV_DEV(ndev, &pdev->dev);
  368. PRINTK2("dm9000_probe()");
  369. /* setup board info structure 
  370. bord info初始化为0 */
  371. db = (struct board_info *) ndev->priv;
  372. memset(db, 0, sizeof (*db));
  373. /*初始化spinlock*/
  374. spin_lock_init(&db->lock);
  375. /*忽略*/
  376. if (pdev->num_resources < 2) {
  377.    ret = -ENODEV;
  378.    goto out;
  379. else if (pdev->num_resources == 2) {
  380.    base = pdev->resource[0].start;
  381.    if (!request_mem_region(base, 4, ndev->name)) {
  382.     ret = -EBUSY;
  383.     goto out;
  384.    }
  385.    ndev->base_addr = base;
  386.    ndev->irq = pdev->resource[1].start;
  387.    db->io_addr = (void __iomem *)base;
  388.    db->io_data = (void __iomem *)(base + 4);
  389. else {
  390.    db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  391.    db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  392.    db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  393.    if (db->addr_res == NULL || db->data_res == NULL ||
  394.       db->irq_res == NULL) {
  395.     printk(KERN_ERR PFX "insufficient resources/n");
  396.     ret = -ENOENT;
  397.     goto out;
  398.    }
  399.    i = res_size(db->addr_res);
  400.    db->addr_req = request_mem_region(db->addr_res->start, i,
  401.         pdev->name);
  402.    if (db->addr_req == NULL) {
  403.     printk(KERN_ERR PFX "cannot claim address reg area/n");
  404.     ret = -EIO;
  405.     goto out;
  406.    }
  407.    db->io_addr = ioremap(db->addr_res->start, i);
  408.    if (db->io_addr == NULL) {
  409.     printk(KERN_ERR "failed to ioremap address reg/n");
  410.     ret = -EINVAL;
  411.     goto out;
  412.    }
  413.    iosize = res_size(db->data_res);
  414.    db->data_req = request_mem_region(db->data_res->start, iosize,
  415.         pdev->name);
  416.    if (db->data_req == NULL) {
  417.     printk(KERN_ERR PFX "cannot claim data reg area/n");
  418.     ret = -EIO;
  419.     goto out;
  420.    }
  421.    db->io_data = ioremap(db->data_res->start, iosize);
  422.    if (db->io_data == NULL) {
  423.     printk(KERN_ERR "failed to ioremap data reg/n");
  424.     ret = -EINVAL;
  425.     goto out;
  426.    }
  427.    /* fill in parameters for net-dev structure */
  428.    ndev->base_addr = (unsigned long)db->io_addr;
  429.    ndev->irq = db->irq_res->start;
  430.    /* ensure at least we have a default set of IO routines */
  431.    dm9000_set_io(db, iosize);
  432. }
  433. /* check to see if anything is being over-ridden */
  434. if (pdata != NULL) {
  435.    /* check to see if the driver wants to over-ride the
  436.    * default IO width */
  437.    if (pdata->flags & DM9000_PLATF_8BITONLY)
  438.     dm9000_set_io(db, 1);
  439.    if (pdata->flags & DM9000_PLATF_16BITONLY)
  440.     dm9000_set_io(db, 2);
  441.    if (pdata->flags & DM9000_PLATF_32BITONLY)
  442.     dm9000_set_io(db, 4);
  443.    /* check to see if there are any IO routine
  444.    * over-rides */
  445.    if (pdata->inblk != NULL)
  446.     db->inblk = pdata->inblk;
  447.    if (pdata->outblk != NULL)
  448.     db->outblk = pdata->outblk;
  449.    if (pdata->dumpblk != NULL)
  450.     db->dumpblk = pdata->dumpblk;
  451. }
  452. /*根据board info信息,复位DM9000芯片*/
  453. dm9000_reset(db);
  454. /* try two times, DM9000 sometimes gets the first read wrong */
  455. for (i = 0; i < 2; i++) {
  456.    id_val = ior(db, DM9000_VIDL);
  457.    id_val |= (u32)ior(db, DM9000_VIDH) << 8;
  458.    id_val |= (u32)ior(db, DM9000_PIDL) << 16;
  459.    id_val |= (u32)ior(db, DM9000_PIDH) << 24;
  460.    if (id_val == DM9000_ID)
  461.     break;
  462.    printk("%s: read wrong id 0x%08x/n", CARDNAME, id_val);
  463. }
  464. /*芯片的ID获取失败,驱动不匹配*/
  465. if (id_val != DM9000_ID) {
  466.    printk("%s: wrong id: 0x%08x/n", CARDNAME, id_val);
  467.    goto release;
  468. }
  469. /* from this point we assume that we have found a DM9000 */
  470. /* driver system function 
  471. 将DM9000作为以太网卡初始化内部数据结构*/
  472. ether_setup(ndev);
  473. ndev->open   = &dm9000_open;
  474. ndev->hard_start_xmit    = &dm9000_start_xmit;
  475. ndev->tx_timeout         = &dm9000_timeout;
  476. ndev->watchdog_timeo = msecs_to_jiffies(watchdog); /*超时值*/
  477. ndev->stop   = &dm9000_stop;
  478. ndev->get_stats   = &dm9000_get_stats;
  479. ndev->set_multicast_list = &dm9000_hash_table;
  480. #ifdef CONFIG_NET_POLL_CONTROLLER
  481. ndev->poll_controller = &dm9000_poll_controller;
  482. #endif
  483. #ifdef DM9000_PROGRAM_EEPROM
  484. /*开发时,需要更新EERPOM的值*/
  485. program_eeprom(db);
  486. #endif
  487. /*忽略*/
  488. db->msg_enable       = NETIF_MSG_LINK;
  489. db->mii.phy_id_mask = 0x1f;
  490. db->mii.reg_num_mask = 0x1f;
  491. db->mii.force_media = 0;
  492. db->mii.full_duplex = 0;
  493. db->mii.dev      = ndev;
  494. db->mii.mdio_read    = dm9000_phy_read; /*DM9000可以提供MII,外接PHY*/
  495. db->mii.mdio_write   = dm9000_phy_write;
  496. /* 获取网卡上EEPROM9346的内容,走EERPOM接口 */
  497. for (i = 0; i < 64; i++)
  498.    /*采用16bit读取方式*/
  499.    ((u16 *) db->srom)[i] = read_srom_word(db, i);
  500. /* Set Node Address 
  501.     EEPROM的前6个字节为MAC地址
  502. */
  503. for (i = 0; i < 6; i++)
  504.    ndev->dev_addr[i] = db->srom[i];
  505. /*验证获取的MAC地址是否合法*/
  506. if (!is_valid_ether_addr(ndev->dev_addr)) {
  507.    /* try reading from mac */
  508.    /*不合法,重新从DM9000的内部寄存器获取MAC,不走EEPROM接口*/
  509.    for (i = 0; i < 6; i++)
  510.     ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
  511. }
  512. /*两种方式都失败,提示用ifconfig,驱动没有对应接口!*/
  513. if (!is_valid_ether_addr(ndev->dev_addr))
  514.    printk("%s: Invalid ethernet MAC address. Please "
  515.          "set using ifconfig/n", ndev->name);
  516. platform_set_drvdata(pdev, ndev);
  517. /*注册接口到系统中,状态默认down,不可用*/
  518. ret = register_netdev(ndev);
  519. if (ret == 0) {
  520.    printk("%s: dm9000 at %p,%p IRQ %d MAC: ",
  521.          ndev->name, db->io_addr, db->io_data, ndev->irq);
  522.    for (i = 0; i < 5; i++)
  523.     printk("%02x:", ndev->dev_addr[i]);
  524.    printk("%02x/n", ndev->dev_addr[5]);
  525. }
  526. return 0;
  527. release:
  528. out:
  529. printk("%s: not found (%d)./n", CARDNAME, ret);
  530. /失败时,释放资源*/
  531. dm9000_release_board(pdev, db);
  532. free_netdev(ndev);
  533. return ret;
  534. }
  535. /*
  536. * Open the interface.
  537. * The interface is opened whenever "ifconfig" actives it.
  538.      当使用ifconfig激活该网络接口时调用
  539. */
  540. static int
  541. dm9000_open(struct net_device *dev)
  542. {
  543. board_info_t *db = (board_info_t *) dev->priv;
  544. PRINTK2("entering dm9000_open/n");
  545. /*申请中断资源,注册中断函数*/
  546. if (request_irq(dev->irq, &dm9000_interrupt, IRQF_SHARED, dev->name, dev))
  547.    return -EAGAIN;
  548. /* Initialize DM9000 board */
  549. /*复位DM9000芯片*/
  550. dm9000_reset(db);
  551. /*配置DM9000芯片内寄存器,使其能工作*/
  552. dm9000_init_dm9000(dev);
  553. /* Init driver variable */
  554. db->dbug_cnt = 0;
  555. /* set and active a timer process */
  556. init_timer(&db->timer);   /*初始化内核定时器*/
  557. db->timer.expires = DM9000_TIMER_WUT; /*2s唤醒一次*/
  558. db->timer.data     = (unsigned long) dev;
  559. db->timer.function = &dm9000_timer; /*内核定时器溢出回调函数*/
  560. add_timer(&db->timer); /*启动内核定时器*/
  561. /*检查mii接口状态*/
  562. mii_check_media(&db->mii, netif_msg_link(db), 1);
  563. /*启动发送队列*/
  564. netif_start_queue(dev);
  565. return 0;
  566. }
  567. /*
  568. * Initilize dm9000 board
  569.    配置DM9000芯片内部寄存器,使其能工作
  570. */
  571. static void
  572. dm9000_init_dm9000(struct net_device *dev)
  573. {
  574. board_info_t *db = (board_info_t *) dev->priv;
  575. PRINTK1("entering %s/n",__FUNCTION__);
  576. /* I/O mode
  577. 检查当前芯片的总线带宽8bit,16bit,32bit
  578. 芯片的总线带宽由硬件走线决定 */
  579. db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
  580. /* GPIO0 on pre-activate PHY 
  581. 启动PHY*/
  582. iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
  583. iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
  584. iow(db, DM9000_GPR, 0); /* Enable PHY */
  585. /* Program operating register */
  586. /*清除发送配置选项*/
  587. iow(db, DM9000_TCR, 0);         /* TX Polling clear */
  588. iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
  589. iow(db, DM9000_FCR, 0xff); /* Flow Control */
  590. iow(db, DM9000_SMCR, 0);        /* Special Mode */
  591. /* clear TX status */
  592. /*清除发送标志位*/
  593. iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
  594. /*清除中断标志位*/
  595. iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
  596. /* Set address filter table */
  597. dm9000_hash_table(dev);
  598. /* Activate DM9000 
  599.    接收包大小不能超过1.5k,CRC错误的抛弃,使能接收*/
  600. iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
  601. /* Enable TX/RX interrupt mask
  602. 使用发送,接收中断。SRAM缓冲区指针自动回0 */
  603. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
  604. /* Init Driver variable */
  605. db->tx_pkt_cnt = 0;
  606. db->queue_pkt_len = 0;
  607. dev->trans_start = 0;
  608. }
  609. /*
  610. * Hardware start transmission.
  611. * Send a packet to media from the upper layer.
  612. */
  613. static int
  614. dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
  615. {
  616. board_info_t *db = (board_info_t *) dev->priv;
  617. PRINTK3("dm9000_start_xmit/n");
  618. if (db->tx_pkt_cnt > 1)
  619.    return 1;
  620. /*停止接收*/
  621. netif_stop_queue(dev);
  622. /* Disable all interrupts 
  623. 关闭发送,接收中断*/
  624. iow(db, DM9000_IMR, IMR_PAR);
  625. /* Move data to DM9000 TX RAM */
  626. writeb(DM9000_MWCMD, db->io_addr);
  627. /*把skb的数据拷贝到DM9000的SRAM中*/
  628. (db->outblk)(db->io_data, skb->data, skb->len);
  629. /*统计值加上相应的长度*/
  630. db->stats.tx_bytes += skb->len;
  631. /* TX control: First packet immediately send, second packet queue */
  632. if (db->tx_pkt_cnt == 0) {
  633.    /* First Packet 
  634.    发送的包增加*/
  635.    db->tx_pkt_cnt++;
  636.    /* Set TX length to DM9000 
  637.    设定DM9000需要发送的包长度*/
  638.    iow(db, DM9000_TXPLL, skb->len & 0xff);
  639.    iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
  640.    /* Issue TX polling command 
  641.    设定发送请求,即启动发送*/
  642.    iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
  643.    /*启动时间*/
  644.    dev->trans_start = jiffies; /* save the time stamp */
  645. else {
  646.    /*第二个包发送,在tx_done中实现*/
  647.    /* Second packet */
  648.    db->tx_pkt_cnt++;
  649.    db->queue_pkt_len = skb->len;
  650. }
  651. /* free this SKB
  652. 发送完毕释放skb */
  653. dev_kfree_skb(skb);
  654. /* Re-enable resource check 
  655. 第一个包发送完毕,即内存拷贝完毕,不是实际总线上的
  656. 可以启动第二包拷贝到SRAM*/
  657. if (db->tx_pkt_cnt == 1)
  658.    netif_wake_queue(dev);
  659. /* Re-enable interrupt 
  660. 重新启用中断,这时DM9000若发送完成,会产生发送完毕中断*/
  661. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
  662. return 0;
  663. }
  664. static void
  665. dm9000_shutdown(struct net_device *dev)
  666. {
  667. board_info_t *db = (board_info_t *) dev->priv;
  668. /* RESET device */
  669. dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
  670. /*关闭PHY*/
  671. iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
  672. /*屏蔽所有中断*/
  673. iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
  674. /*禁用接收*/
  675. iow(db, DM9000_RCR, 0x00); /* Disable RX */
  676. }
  677. /*
  678. * Stop the interface.
  679. * The interface is stopped when it is brought.
  680. */
  681. static int
  682. dm9000_stop(struct net_device *ndev)
  683. {
  684. board_info_t *db = (board_info_t *) ndev->priv;
  685. PRINTK1("entering %s/n",__FUNCTION__);
  686. /* deleted timer 
  687. 删除内核定时器*/
  688. del_timer(&db->timer);
  689. /*通知内核队列不可用*/
  690. netif_stop_queue(ndev);
  691. /*linkstat 状态断路*/
  692. netif_carrier_off(ndev);
  693. /* free interrupt 
  694. 释放中断*/
  695. free_irq(ndev->irq, ndev);
  696. /*硬件上关闭芯片*/
  697. dm9000_shutdown(ndev);
  698. return 0;
  699. }
  700. /*
  701. * DM9000 interrupt handler
  702. * receive the packet to upper layer, free the transmitted packet
  703. */
  704. static void
  705. dm9000_tx_done(struct net_device *dev, board_info_t * db)
  706. {
  707. int tx_status = ior(db, DM9000_NSR); /* Got TX status */
  708. /*一个包已经发送完毕*/
  709. if (tx_status & (NSR_TX2END | NSR_TX1END)) {
  710.    /* One packet sent complete */
  711.    db->tx_pkt_cnt--;
  712.    db->stats.tx_packets++;
  713.    /* Queue packet check & send 
  714.    当前还有一个包未发送,且在缓冲区中,是第二个包*/
  715.    if (db->tx_pkt_cnt > 0) {
  716.     /*设定DM9000发送包的长度*/
  717.     iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
  718.     iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
  719.     /*启动发送*/
  720.     iow(db, DM9000_TCR, TCR_TXREQ);
  721.     dev->trans_start = jiffies;
  722.    }
  723.    /*通知内核*/
  724.    netif_wake_queue(dev);
  725. }
  726. }
  727. static irqreturn_t
  728. dm9000_interrupt(int irq, void *dev_id)
  729. {
  730. struct net_device *dev = dev_id;
  731. board_info_t *db;
  732. int int_status;
  733. u8 reg_save;
  734. PRINTK3("entering %s/n",__FUNCTION__);
  735. if (!dev) {
  736.    PRINTK1("dm9000_interrupt() without DEVICE arg/n");
  737.    return IRQ_HANDLED;
  738. }
  739. /* A real interrupt coming */
  740. db = (board_info_t *) dev->priv;
  741. spin_lock(&db->lock);
  742. /* Save previous register address */
  743. /*保存寄存器值*/
  744. reg_save = readb(db->io_addr);
  745. /* Disable all interrupts 
  746. 关闭所有中断*/
  747. iow(db, DM9000_IMR, IMR_PAR);
  748. /* Got DM9000 interrupt status */
  749. int_status = ior(db, DM9000_ISR); /* Got ISR */
  750. iow(db, DM9000_ISR, int_status); /* Clear ISR status */
  751. /* Received the coming packet 
  752. 接收包中断*/
  753. if (int_status & ISR_PRS)
  754.    dm9000_rx(dev);
  755. /* Trnasmit Interrupt check 
  756. 发送包中断*/
  757. if (int_status & ISR_PTS)
  758.    dm9000_tx_done(dev, db);
  759. /* Re-enable interrupt mask */
  760. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
  761. /* Restore previous register address */
  762. writeb(reg_save, db->io_addr);
  763. spin_unlock(&db->lock);
  764. return IRQ_HANDLED;
  765. }
  766. /*
  767. * Get statistics from driver.
  768. 获取网卡接口统计信息
  769. */
  770. static struct net_device_stats *
  771. dm9000_get_stats(struct net_device *dev)
  772. {
  773. board_info_t *db = (board_info_t *) dev->priv;
  774. return &db->stats;
  775. }
  776. /*
  777. * A periodic timer routine
  778. * Dynamic media sense, allocated Rx buffer...
  779. */
  780. static void
  781. dm9000_timer(unsigned long data)
  782. {
  783. struct net_device *dev = (struct net_device *) data;
  784. board_info_t *db = (board_info_t *) dev->priv;
  785. PRINTK3("dm9000_timer()/n");
  786. /*检查mii接口*/
  787. mii_check_media(&db->mii, netif_msg_link(db), 0);
  788. /* Set timer again
  789. 重新启动内核定时器,周期检查mii接口 */
  790. db->timer.expires = DM9000_TIMER_WUT;
  791. add_timer(&db->timer);
  792. }
  793. /*DM9000接收包头,芯片提供
  794. 01,status,length low,length high*/
  795. struct dm9000_rxhdr {
  796. u16 RxStatus; 
  797. u16 RxLen;
  798. } __attribute__((__packed__));
  799. /*
  800. * Received a packet and pass to upper layer
  801. */
  802. static void
  803. dm9000_rx(struct net_device *dev)
  804. {
  805. board_info_t *db = (board_info_t *) dev->priv;
  806. struct dm9000_rxhdr rxhdr;
  807. struct sk_buff *skb;
  808. u8 rxbyte, *rdptr;
  809. bool GoodPacket;
  810. int RxLen;
  811. /* Check packet ready or not */
  812. do {
  813.    ior(db, DM9000_MRCMDX); /* Dummy read */
  814.    /* Get most updated data */
  815.    rxbyte = readb(db->io_data);
  816.    /* Status check: this byte must be 0 or 1 
  817.    接收包最长为1536 */
  818.    if (rxbyte > DM9000_PKT_RDY) {
  819.     printk("status check failed: %d/n", rxbyte);
  820.     iow(db, DM9000_RCR, 0x00); /* Stop Device */
  821.     iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
  822.     return;
  823.    }
  824.    if (rxbyte != DM9000_PKT_RDY)
  825.     return;
  826.    /* A packet ready now & Get status/length */
  827.    GoodPacket = true;
  828.    writeb(DM9000_MRCMD, db->io_addr);
  829.    /*获取接收数据包头,共4字节,参考DM9000手册*/
  830.    (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
  831.    /*查看包长*/
  832.    RxLen = rxhdr.RxLen;
  833.    /* Packet Status check 
  834.    包长不能小于64字节*/
  835.    if (RxLen < 0x40) {
  836.     GoodPacket = false;
  837.     PRINTK1("Bad Packet received (runt)/n");
  838.    }
  839.    /*包长不能大于1536字节*/
  840.    if (RxLen > DM9000_PKT_MAX) {
  841.     PRINTK1("RST: RX Len:%x/n", RxLen);
  842.    }
  843.    /*接收的包有错误*/
  844.    if (rxhdr.RxStatus & 0xbf00) {
  845.     GoodPacket = false;
  846.     if (rxhdr.RxStatus & 0x100) {
  847.      PRINTK1("fifo error/n");
  848.      db->stats.rx_fifo_errors++;
  849.     }
  850.     if (rxhdr.RxStatus & 0x200) {
  851.      PRINTK1("crc error/n");
  852.      db->stats.rx_crc_errors++;
  853.     }
  854.     if (rxhdr.RxStatus & 0x8000) {
  855.      PRINTK1("length error/n");
  856.      db->stats.rx_length_errors++;
  857.     }
  858.    }
  859.    /* Move data from DM9000 
  860.    接收的包没有任何问题时*/
  861.    if (GoodPacket
  862.       && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
  863.       
  864.     skb_reserve(skb, 2);
  865.     rdptr = (u8 *) skb_put(skb, RxLen - 4);
  866.     /* Read received packet from RX SRAM */
  867.     /*数据从DM9000内存拷贝到skb*/
  868.     (db->inblk)(db->io_data, rdptr, RxLen);
  869.     /*更新接收字节数统计*/
  870.     db->stats.rx_bytes += RxLen;
  871.     /* Pass to upper layer */
  872.     skb->protocol = eth_type_trans(skb, dev);
  873.     /*通知上层有新的skb收到*/
  874.     netif_rx(skb);
  875.     /*接收包更新*/
  876.     db->stats.rx_packets++;
  877.    } else {
  878.     /* need to dump the packet's data */
  879.     (db->dumpblk)(db->io_data, RxLen);
  880.    }
  881. while (rxbyte == DM9000_PKT_RDY);
  882. }
  883. /*
  884. * Read a word data from SROM
  885.     从EEPROM9346中读取数据 16bit
  886. */
  887. static u16
  888. read_srom_word(board_info_t * db, int offset)
  889. {
  890. iow(db, DM9000_EPAR, offset);
  891. iow(db, DM9000_EPCR, EPCR_ERPRR);
  892. mdelay(8);   /* according to the datasheet 200us should be enough,
  893.        but it doesn't work */
  894. iow(db, DM9000_EPCR, 0x0);
  895. return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
  896. }
  897. #ifdef DM9000_PROGRAM_EEPROM
  898. /*
  899. * Write a word data to SROM
  900. 向EEPROM 9346中写数据 16bit
  901. */
  902. static void
  903. write_srom_word(board_info_t * db, int offset, u16 val)
  904. {
  905. iow(db, DM9000_EPAR, offset);
  906. iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
  907. iow(db, DM9000_EPDRL, (val & 0xff));
  908. iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
  909. mdelay(8);   /* same shit */
  910. iow(db, DM9000_EPCR, 0);
  911. }
  912. /*
  913. * Only for development:
  914. * Here we write static data to the eeprom in case
  915. * we don't have valid content on a new board
  916. */
  917. static void
  918. program_eeprom(board_info_t * db)
  919. {
  920. u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
  921.    0x0000,   /* Autoload: accept nothing */
  922.    0x0a46, 0x9000, /* Vendor / Product ID */
  923.    0x0000,   /* pin control */
  924.    0x0000,
  925. };    /* Wake-up mode control */
  926. int i;
  927. for (i = 0; i < 8; i++)
  928.    write_srom_word(db, i, eeprom[i]);
  929. }
  930. #endif
  931. /*
  932. * Calculate the CRC valude of the Rx packet
  933. * flag = 1 : return the reverse CRC (for the received packet CRC)
  934. *         0 : return the normal CRC (for Hash Table index)
  935.     计算接收包的crc值
  936. */
  937. static unsigned long
  938. cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
  939. {
  940.        u32 crc = ether_crc_le(Len, Data);
  941.        if (flag)
  942.                return ~crc;
  943.        return crc;
  944. }
  945. /*
  946. * Set DM9000 multicast address
  947. */
  948. static void
  949. dm9000_hash_table(struct net_device *dev)
  950. {
  951. board_info_t *db = (board_info_t *) dev->priv;
  952. struct dev_mc_list *mcptr = dev->mc_list;
  953. int mc_cnt = dev->mc_count;
  954. u32 hash_val;
  955. u16 i, oft, hash_table[4];
  956. unsigned long flags;
  957. PRINTK2("dm9000_hash_table()/n");
  958. spin_lock_irqsave(&db->lock,flags);
  959. for (i = 0, oft = 0x10; i < 6; i++, oft++)
  960.    iow(db, oft, dev->dev_addr[i]);
  961. /* Clear Hash Table */
  962. for (i = 0; i < 4; i++)
  963.    hash_table[i] = 0x0;
  964. /* broadcast address */
  965. hash_table[3] = 0x8000;
  966. /* the multicast address in Hash Table : 64 bits */
  967. for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
  968.    hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
  969.    hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
  970. }
  971. /* Write the hash table to MAC MD table */
  972. for (i = 0, oft = 0x16; i < 4; i++) {
  973.    iow(db, oft++, hash_table[i] & 0xff);
  974.    iow(db, oft++, (hash_table[i] >> 8) & 0xff);
  975. }
  976. spin_unlock_irqrestore(&db->lock,flags);
  977. }
  978. /*
  979. *   Read a word from phyxcer
  980.    从PHY读取数据16bit
  981. */
  982. static int
  983. dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
  984. {
  985. board_info_t *db = (board_info_t *) dev->priv;
  986. unsigned long flags;
  987. unsigned int reg_save;
  988. int ret;
  989. spin_lock_irqsave(&db->lock,flags);
  990. /* Save previous register address */
  991. reg_save = readb(db->io_addr);
  992. /* Fill the phyxcer register into REG_0C */
  993. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  994. iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
  995. udelay(100);   /* Wait read complete */
  996. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
  997. /* The read data keeps on REG_0D & REG_0E */
  998. ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
  999. /* restore the previous address */
  1000. writeb(reg_save, db->io_addr);
  1001. spin_unlock_irqrestore(&db->lock,flags);
  1002. return ret;
  1003. }
  1004. /*
  1005. *   Write a word to phyxcer
  1006. 向PHY写入数据16bit
  1007. */
  1008. static void
  1009. dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
  1010. {
  1011. board_info_t *db = (board_info_t *) dev->priv;
  1012. unsigned long flags;
  1013. unsigned long reg_save;
  1014. spin_lock_irqsave(&db->lock,flags);
  1015. /* Save previous register address */
  1016. reg_save = readb(db->io_addr);
  1017. /* Fill the phyxcer register into REG_0C */
  1018. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  1019. /* Fill the written data into REG_0D & REG_0E */
  1020. iow(db, DM9000_EPDRL, (value & 0xff));
  1021. iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
  1022. iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
  1023. udelay(500);   /* Wait write complete */
  1024. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
  1025. /* restore the previous address */
  1026. writeb(reg_save, db->io_addr);
  1027. spin_unlock_irqrestore(&db->lock,flags);
  1028. }
  1029. /*挂起网卡接口*/
  1030. static int
  1031. dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
  1032. {
  1033. struct net_device *ndev = platform_get_drvdata(dev);
  1034. if (ndev) {
  1035.    if (netif_running(ndev)) {
  1036.     /*内核临时删除接口*/
  1037.     netif_device_detach(ndev);
  1038.     /*关闭设备*/
  1039.     dm9000_shutdown(ndev);
  1040.    }
  1041. }
  1042. return 0;
  1043. }
  1044. static int
  1045. dm9000_drv_resume(struct platform_device *dev)
  1046. {
  1047. struct net_device *ndev = platform_get_drvdata(dev);
  1048. board_info_t *db = (board_info_t *) ndev->priv;
  1049. if (ndev) {
  1050.    if (netif_running(ndev)) {
  1051.     /*复位设备*/
  1052.     dm9000_reset(db);
  1053.     dm9000_init_dm9000(ndev);
  1054.     /*恢复接口*/
  1055.     netif_device_attach(ndev);
  1056.    }
  1057. }
  1058. return 0;
  1059. }
  1060. static int
  1061. dm9000_drv_remove(struct platform_device *pdev)
  1062. {
  1063. struct net_device *ndev = platform_get_drvdata(pdev);
  1064. platform_set_drvdata(pdev, NULL);
  1065. /*释放注册的网络接口*/
  1066. unregister_netdev(ndev);
  1067. dm9000_release_board(pdev, (board_info_t *) ndev->priv);
  1068. /*释放资源*/
  1069. free_netdev(ndev);   /* free device structure */
  1070. PRINTK1("clean_module() exit/n");
  1071. return 0;
  1072. }
  1073. static struct platform_driver dm9000_driver = {
  1074. .driver = {
  1075.    .name    = "dm9000",
  1076.    .owner = THIS_MODULE,
  1077. },
  1078. .probe   = dm9000_probe,
  1079. .remove = dm9000_drv_remove,
  1080. .suspend = dm9000_drv_suspend,
  1081. .resume = dm9000_drv_resume,
  1082. };
  1083. static int __init
  1084. dm9000_init(void)
  1085. {
  1086. printk(KERN_INFO "%s Ethernet Driver/n", CARDNAME);
  1087. return platform_driver_register(&dm9000_driver); /* search board and register */
  1088. }
  1089. static void __exit
  1090. dm9000_cleanup(void)
  1091. {
  1092. platform_driver_unregister(&dm9000_driver);
  1093. }
  1094. module_init(dm9000_init);
  1095. module_exit(dm9000_cleanup);
  1096. MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
  1097. MODULE_DESCRIPTION("Davicom DM9000 network driver");
  1098. MODULE_LICENSE("GPL");
 
附件:DM9000网卡芯片手册,大家可以对照寄存器查看
                         http://blog.21ic.com/uploadfile-/2007-8/85766303.pdf
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值