dm9000a驱动源码分析

2021/4/16 14:26:33

本文主要是介绍dm9000a驱动源码分析,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

dm9000a框架原理图:

 

EEPROM Interface接口用于存放mac地址,InternalSRAM用于存放收发数据,MII部分把MAC部分与PHY部分连接起来通信,AUTO-MDIX用于自适应10/100M网络,在物理层上,MAC在PHY之下。 

 

由dm9000a驱动可知,dm9000a驱动是用platform模型编写的,分析一个驱动源码都是从模块加载函数module_init()开始,而dm9000a加载函数是module_init(dm9000_init).

 

继而调用:

 
  1. static int __init

  2. dm9000_init(void)

  3. {

  4. printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);

  5. return platform_driver_register(&dm9000_driver);

  6. }

 

dm9000_driver结构体:

 
  1. static struct platform_driver dm9000_driver = {

  2. .driver = {

  3. .name = "dm9000",//名字

  4. .owner = THIS_MODULE,

  5. },

  6. .probe = dm9000_probe,//模块加载后,调用probe函数

  7. .remove = __devexit_p(dm9000_drv_remove),

  8. .suspend = dm9000_drv_suspend,

  9. .resume = dm9000_drv_resume,

  10. };

模块加载之后,调用probe函数,如下:

 

 
  1. /*

  2. * Search DM9000 board, allocate space and register it

  3. */

  4. static int __devinit

  5. dm9000_probe(struct platform_device *pdev)

  6. {

  7. struct dm9000_plat_data *pdata = pdev->dev.platform_data;

  8. struct board_info *db; /* Point a board information structure */

  9. struct net_device *ndev;

  10. const unsigned char *mac_src;

  11. int ret = 0;

  12. int iosize;

  13. int i;

  14. u32 id_val;

  15.  
  16. unsigned char ne_def_eth_mac_addr[]={0x00,0x12,0x34,0x56,0x80,0x49};

  17.  
  18. /* ------------------------------------------------------------------------ */

  19. static void *bwscon;

  20. static void *gpfcon;

  21. static void *extint0;

  22. static void *intmsk;

  23. #define BWSCON (0x48000000)

  24. #define GPFCON (0x56000050)

  25. #define EXTINT0 (0x56000088)

  26. #define INTMSK (0x4A000008)

  27.  
  28. bwscon=ioremap_nocache(BWSCON,0x0000004);

  29. gpfcon=ioremap_nocache(GPFCON,0x0000004);

  30. extint0=ioremap_nocache(EXTINT0,0x0000004);

  31. intmsk=ioremap_nocache(INTMSK,0x0000004);

  32.  
  33. writel(readl(bwscon)|0xc0000,bwscon);

  34. writel( (readl(gpfcon) & ~(0x3 << 14)) | (0x2 << 14), gpfcon);

  35. writel( readl(gpfcon) | (0x1 << 7), gpfcon); // Disable pull-up

  36. writel( (readl(extint0) & ~(0xf << 28)) | (0x4 << 28), extint0); //rising edge

  37. writel( (readl(intmsk)) & ~0x80, intmsk);

  38. /* ------------------------------------------------------------------------ */

  39.  
  40. /* Init network device */

  41. /* 分配eth网卡资源,私有数据区保存board_info*/

  42. ndev = alloc_etherdev(sizeof(struct board_info));

  43. if (!ndev) {

  44. dev_err(&pdev->dev, "could not allocate device.\n");

  45. return -ENOMEM;

  46. }

  47.  
  48. SET_NETDEV_DEV(ndev, &pdev->dev);

  49.  
  50. dev_dbg(&pdev->dev, "dm9000_probe()\n");

  51.  
  52. /* setup board info structure 初始化为0*/

  53. db = ndev->priv;

  54. memset(db, 0, sizeof(*db));

  55.  
  56. db->dev = &pdev->dev;

  57. db->ndev = ndev;

  58. /*初始化spinlock*/

  59. spin_lock_init(&db->lock);

  60. mutex_init(&db->addr_lock);

  61. /*提交一个任务给一个工作队列,你需要填充一个work_struct结构db->phy_poll*/

  62. INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);

  63. /*获取IO内存和中断资源*/

  64. db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

  65. db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);

  66. db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);

  67.  
  68. if (db->addr_res == NULL || db->data_res == NULL ||

  69. db->irq_res == NULL) {

  70. dev_err(db->dev, "insufficient resources\n");

  71. ret = -ENOENT;

  72. goto out;

  73. }

  74. /*映射到内核,并获得IO内存的虚拟地址,ioremap完成页表的建立,

  75. 不同于vmalloc,但是,它实际上不分配内存*/

  76. iosize = res_size(db->addr_res);

  77. db->addr_req = request_mem_region(db->addr_res->start, iosize,

  78. pdev->name);

  79.  
  80. if (db->addr_req == NULL) {

  81. dev_err(db->dev, "cannot claim address reg area\n");

  82. ret = -EIO;

  83. goto out;

  84. }

  85.  
  86. db->io_addr = ioremap(db->addr_res->start, iosize);

  87.  
  88. if (db->io_addr == NULL) {

  89. dev_err(db->dev, "failed to ioremap address reg\n");

  90. ret = -EINVAL;

  91. goto out;

  92. }

  93.  
  94. iosize = res_size(db->data_res);

  95. db->data_req = request_mem_region(db->data_res->start, iosize,

  96. pdev->name);

  97.  
  98. if (db->data_req == NULL) {

  99. dev_err(db->dev, "cannot claim data reg area\n");

  100. ret = -EIO;

  101. goto out;

  102. }

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

  105.  
  106. if (db->io_data == NULL) {

  107. dev_err(db->dev, "failed to ioremap data reg\n");

  108. ret = -EINVAL;

  109. goto out;

  110. }

  111.  
  112. /* fill in parameters for net-dev structure */

  113. /*获得网络设备的基地址*/

  114. ndev->base_addr = (unsigned long)db->io_addr;

  115. /*获得网络设备的中断号*/

  116. ndev->irq = db->irq_res->start;

  117.  
  118. /* ensure at least we have a default set of IO routines */

  119. /*设置默认的IO函数*/

  120. dm9000_set_io(db, iosize);

  121.  
  122. /*如果平台数据不为空 */

  123. if (pdata != NULL) {

  124. /* check to see if the driver wants to over-ride the

  125. * default IO width */

  126.  
  127. if (pdata->flags & DM9000_PLATF_8BITONLY)

  128. dm9000_set_io(db, 1);

  129.  
  130. if (pdata->flags & DM9000_PLATF_16BITONLY)

  131. dm9000_set_io(db, 2);

  132.  
  133. if (pdata->flags & DM9000_PLATF_32BITONLY)

  134. dm9000_set_io(db, 4);

  135.  
  136. /* check to see if there are any IO routine

  137. * over-rides */

  138.  
  139. if (pdata->inblk != NULL)

  140. db->inblk = pdata->inblk;

  141.  
  142. if (pdata->outblk != NULL)

  143. db->outblk = pdata->outblk;

  144.  
  145. if (pdata->dumpblk != NULL)

  146. db->dumpblk = pdata->dumpblk;

  147.  
  148. db->flags = pdata->flags;

  149. }

  150.  
  151. #ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL

  152. db->flags |= DM9000_PLATF_SIMPLE_PHY;

  153. #endif

  154. /*根据board info信息,复位DM9000芯片*/

  155. dm9000_reset(db);

  156. /*读取Vendor ID Register,Product ID Register中的值,与0x90000A46比较,如果相等,则说明是DM9000*/

  157. /* try multiple times, DM9000 sometimes gets the read wrong */

  158. for (i = 0; i < 8; i++) {

  159. id_val = ior(db, DM9000_VIDL);

  160. id_val |= (u32)ior(db, DM9000_VIDH) << 8;

  161. id_val |= (u32)ior(db, DM9000_PIDL) << 16;

  162. id_val |= (u32)ior(db, DM9000_PIDH) << 24;

  163.  
  164. if (id_val == DM9000_ID)

  165. break;

  166. dev_err(db->dev, "read wrong id 0x%08x\n", id_val);

  167. }

  168. /*芯片的ID获取失败,驱动不匹配*/

  169. if (id_val != DM9000_ID) {

  170. dev_err(db->dev, "wrong id: 0x%08x\n", id_val);

  171. ret = -ENODEV;

  172. goto out;

  173. }

  174.  
  175. /* Identify what type of DM9000 we are working on */

  176. /*读取Chip Revision Register中的值*/

  177. id_val = ior(db, DM9000_CHIPR);

  178. dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);

  179.  
  180. switch (id_val) {

  181. case CHIPR_DM9000A:

  182. db->type = TYPE_DM9000A;

  183. break;

  184. case CHIPR_DM9000B:

  185. db->type = TYPE_DM9000B;

  186. break;

  187. default:

  188. dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);

  189. db->type = TYPE_DM9000E;

  190. }

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

  193.  
  194. /* driver system function */

  195. /*设置部分net_device字段*/

  196. ether_setup(ndev);

  197.  
  198. ndev->open = &dm9000_open;

  199. ndev->hard_start_xmit = &dm9000_start_xmit;

  200. ndev->tx_timeout = &dm9000_timeout;

  201. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);

  202. ndev->stop = &dm9000_stop;

  203. ndev->set_multicast_list = &dm9000_hash_table;

  204. /*对ethtool支持的相关声明可在<linux/ethtool.h>中找到。

  205. 它的核心是一个ethtool_ops类型的结构,里边包含一个全部

  206. 的24个不同的方法来支持ethtool*/

  207. ndev->ethtool_ops = &dm9000_ethtool_ops;

  208. ndev->do_ioctl = &dm9000_ioctl;

  209.  
  210. #ifdef CONFIG_NET_POLL_CONTROLLER

  211. ndev->poll_controller = &dm9000_poll_controller;

  212. #endif

  213.  
  214. db->msg_enable = NETIF_MSG_LINK;

  215. db->mii.phy_id_mask = 0x1f;

  216. db->mii.reg_num_mask = 0x1f;

  217. db->mii.force_media = 0;

  218. db->mii.full_duplex = 0;

  219. db->mii.dev = ndev;

  220. db->mii.mdio_read = dm9000_phy_read;

  221. db->mii.mdio_write = dm9000_phy_write;

  222. /*MAC地址的源是eeprom*/

  223. mac_src = "eeprom";

  224.  
  225. /* try reading the node address from the attached EEPROM */

  226. for (i = 0; i < 6; i += 2)

  227. dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);

  228. /*如果从eeprom中读取的地址无效,并且私有数据不为空,从platform_device的私有数据中获取dev_addr*/

  229. if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {

  230. mac_src = "platform data";

  231. memcpy(ndev->dev_addr, pdata->dev_addr, 6);

  232. }

  233. /*如果地址依然无效,从PAR:物理地址(MAC)寄存器(Physical Address Register)中读取*/

  234. if (!is_valid_ether_addr(ndev->dev_addr)) {

  235. /* try reading from mac */

  236.  
  237. mac_src = "chip";

  238. for (i = 0; i < 6; i++)

  239. //ndev->dev_addr[i] = ior(db, i+DM9000_PAR); // by bai

  240. ndev->dev_addr[i] = ne_def_eth_mac_addr[i];

  241. }

  242. /*查看以太网网卡设备地址是否有效*/

  243. if (!is_valid_ether_addr(ndev->dev_addr))

  244. dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "

  245. "set using ifconfig\n", ndev->name);

  246. /*将ndev保存到pdev->dev->driver_data中*/

  247. platform_set_drvdata(pdev, ndev);

  248. /*一切都初始化好后,注册网络设备*/

  249. ret = register_netdev(ndev);

  250.  
  251. if (ret == 0) {

  252. DECLARE_MAC_BUF(mac);

  253. printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %s (%s)\n",

  254. ndev->name, dm9000_type_to_char(db->type),

  255. db->io_addr, db->io_data, ndev->irq,

  256. print_mac(mac, ndev->dev_addr), mac_src);

  257. }

  258. return 0;

  259.  
  260. out:

  261. dev_err(db->dev, "not found (%d).\n", ret);

  262. /*失败时,释放资源*/

  263. dm9000_release_board(pdev, db);

  264. free_netdev(ndev);

  265.  
  266. return ret;

  267. }



这篇关于dm9000a驱动源码分析的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程