dm9000a框架原理图:
EEPROM Interface接口用于存放mac地址,InternalSRAM用于存放收发数据,MII部分把MAC部分与PHY部分连接起来通信,AUTO-MDIX用于自适应10/100M网络,在物理层上,MAC在PHY之下。
由dm9000a驱动可知,dm9000a驱动是用platform模型编写的,分析一个驱动源码都是从模块加载函数module_init()开始,而dm9000a加载函数是module_init(dm9000_init).
继而调用:
static int __init dm9000_init(void) { printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION); return platform_driver_register(&dm9000_driver); }
dm9000_driver结构体:
static struct platform_driver dm9000_driver = { .driver = { .name = "dm9000",//名字 .owner = THIS_MODULE, }, .probe = dm9000_probe,//模块加载后,调用probe函数 .remove = __devexit_p(dm9000_drv_remove), .suspend = dm9000_drv_suspend, .resume = dm9000_drv_resume, };模块加载之后,调用probe函数,如下:
/* * Search DM9000 board, allocate space and register it */ static int __devinit dm9000_probe(struct platform_device *pdev) { struct dm9000_plat_data *pdata = pdev->dev.platform_data; struct board_info *db; /* Point a board information structure */ struct net_device *ndev; const unsigned char *mac_src; int ret = 0; int iosize; int i; u32 id_val; unsigned char ne_def_eth_mac_addr[]={0x00,0x12,0x34,0x56,0x80,0x49}; /* ------------------------------------------------------------------------ */ static void *bwscon; static void *gpfcon; static void *extint0; static void *intmsk; #define BWSCON (0x48000000) #define GPFCON (0x56000050) #define EXTINT0 (0x56000088) #define INTMSK (0x4A000008) bwscon=ioremap_nocache(BWSCON,0x0000004); gpfcon=ioremap_nocache(GPFCON,0x0000004); extint0=ioremap_nocache(EXTINT0,0x0000004); intmsk=ioremap_nocache(INTMSK,0x0000004); writel(readl(bwscon)|0xc0000,bwscon); writel( (readl(gpfcon) & ~(0x3 << 14)) | (0x2 << 14), gpfcon); writel( readl(gpfcon) | (0x1 << 7), gpfcon); // Disable pull-up writel( (readl(extint0) & ~(0xf << 28)) | (0x4 << 28), extint0); //rising edge writel( (readl(intmsk)) & ~0x80, intmsk); /* ------------------------------------------------------------------------ */ /* Init network device */ /* 分配eth网卡资源,私有数据区保存board_info*/ ndev = alloc_etherdev(sizeof(struct board_info)); if (!ndev) { dev_err(&pdev->dev, "could not allocate device.\n"); return -ENOMEM; } SET_NETDEV_DEV(ndev, &pdev->dev); dev_dbg(&pdev->dev, "dm9000_probe()\n"); /* setup board info structure 初始化为0*/ db = ndev->priv; memset(db, 0, sizeof(*db)); db->dev = &pdev->dev; db->ndev = ndev; /*初始化spinlock*/ spin_lock_init(&db->lock); mutex_init(&db->addr_lock); /*提交一个任务给一个工作队列,你需要填充一个work_struct结构db->phy_poll*/ INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work); /*获取IO内存和中断资源*/ db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1); db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); if (db->addr_res == NULL || db->data_res == NULL || db->irq_res == NULL) { dev_err(db->dev, "insufficient resources\n"); ret = -ENOENT; goto out; } /*映射到内核,并获得IO内存的虚拟地址,ioremap完成页表的建立, 不同于vmalloc,但是,它实际上不分配内存*/ iosize = res_size(db->addr_res); db->addr_req = request_mem_region(db->addr_res->start, iosize, pdev->name); if (db->addr_req == NULL) { dev_err(db->dev, "cannot claim address reg area\n"); ret = -EIO; goto out; } db->io_addr = ioremap(db->addr_res->start, iosize); if (db->io_addr == NULL) { dev_err(db->dev, "failed to ioremap address reg\n"); ret = -EINVAL; goto out; } iosize = res_size(db->data_res); db->data_req = request_mem_region(db->data_res->start, iosize, pdev->name); if (db->data_req == NULL) { dev_err(db->dev, "cannot claim data reg area\n"); ret = -EIO; goto out; } db->io_data = ioremap(db->data_res->start, iosize); if (db->io_data == NULL) { dev_err(db->dev, "failed to ioremap data reg\n"); ret = -EINVAL; goto out; } /* fill in parameters for net-dev structure */ /*获得网络设备的基地址*/ ndev->base_addr = (unsigned long)db->io_addr; /*获得网络设备的中断号*/ ndev->irq = db->irq_res->start; /* ensure at least we have a default set of IO routines */ /*设置默认的IO函数*/ dm9000_set_io(db, iosize); /*如果平台数据不为空 */ if (pdata != NULL) { /* check to see if the driver wants to over-ride the * default IO width */ if (pdata->flags & DM9000_PLATF_8BITONLY) dm9000_set_io(db, 1); if (pdata->flags & DM9000_PLATF_16BITONLY) dm9000_set_io(db, 2); if (pdata->flags & DM9000_PLATF_32BITONLY) dm9000_set_io(db, 4); /* check to see if there are any IO routine * over-rides */ if (pdata->inblk != NULL) db->inblk = pdata->inblk; if (pdata->outblk != NULL) db->outblk = pdata->outblk; if (pdata->dumpblk != NULL) db->dumpblk = pdata->dumpblk; db->flags = pdata->flags; } #ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL db->flags |= DM9000_PLATF_SIMPLE_PHY; #endif /*根据board info信息,复位DM9000芯片*/ dm9000_reset(db); /*读取Vendor ID Register,Product ID Register中的值,与0x90000A46比较,如果相等,则说明是DM9000*/ /* try multiple times, DM9000 sometimes gets the read wrong */ for (i = 0; i < 8; i++) { id_val = ior(db, DM9000_VIDL); id_val |= (u32)ior(db, DM9000_VIDH) << 8; id_val |= (u32)ior(db, DM9000_PIDL) << 16; id_val |= (u32)ior(db, DM9000_PIDH) << 24; if (id_val == DM9000_ID) break; dev_err(db->dev, "read wrong id 0x%08x\n", id_val); } /*芯片的ID获取失败,驱动不匹配*/ if (id_val != DM9000_ID) { dev_err(db->dev, "wrong id: 0x%08x\n", id_val); ret = -ENODEV; goto out; } /* Identify what type of DM9000 we are working on */ /*读取Chip Revision Register中的值*/ id_val = ior(db, DM9000_CHIPR); dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val); switch (id_val) { case CHIPR_DM9000A: db->type = TYPE_DM9000A; break; case CHIPR_DM9000B: db->type = TYPE_DM9000B; break; default: dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val); db->type = TYPE_DM9000E; } /* from this point we assume that we have found a DM9000 */ /* driver system function */ /*设置部分net_device字段*/ ether_setup(ndev); ndev->open = &dm9000_open; ndev->hard_start_xmit = &dm9000_start_xmit; ndev->tx_timeout = &dm9000_timeout; ndev->watchdog_timeo = msecs_to_jiffies(watchdog); ndev->stop = &dm9000_stop; ndev->set_multicast_list = &dm9000_hash_table; /*对ethtool支持的相关声明可在<linux/ethtool.h>中找到。 它的核心是一个ethtool_ops类型的结构,里边包含一个全部 的24个不同的方法来支持ethtool*/ ndev->ethtool_ops = &dm9000_ethtool_ops; ndev->do_ioctl = &dm9000_ioctl; #ifdef CONFIG_NET_POLL_CONTROLLER ndev->poll_controller = &dm9000_poll_controller; #endif db->msg_enable = NETIF_MSG_LINK; db->mii.phy_id_mask = 0x1f; db->mii.reg_num_mask = 0x1f; db->mii.force_media = 0; db->mii.full_duplex = 0; db->mii.dev = ndev; db->mii.mdio_read = dm9000_phy_read; db->mii.mdio_write = dm9000_phy_write; /*MAC地址的源是eeprom*/ mac_src = "eeprom"; /* try reading the node address from the attached EEPROM */ for (i = 0; i < 6; i += 2) dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i); /*如果从eeprom中读取的地址无效,并且私有数据不为空,从platform_device的私有数据中获取dev_addr*/ if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) { mac_src = "platform data"; memcpy(ndev->dev_addr, pdata->dev_addr, 6); } /*如果地址依然无效,从PAR:物理地址(MAC)寄存器(Physical Address Register)中读取*/ if (!is_valid_ether_addr(ndev->dev_addr)) { /* try reading from mac */ mac_src = "chip"; for (i = 0; i < 6; i++) //ndev->dev_addr[i] = ior(db, i+DM9000_PAR); // by bai ndev->dev_addr[i] = ne_def_eth_mac_addr[i]; } /*查看以太网网卡设备地址是否有效*/ if (!is_valid_ether_addr(ndev->dev_addr)) dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please " "set using ifconfig\n", ndev->name); /*将ndev保存到pdev->dev->driver_data中*/ platform_set_drvdata(pdev, ndev); /*一切都初始化好后,注册网络设备*/ ret = register_netdev(ndev); if (ret == 0) { DECLARE_MAC_BUF(mac); printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %s (%s)\n", ndev->name, dm9000_type_to_char(db->type), db->io_addr, db->io_data, ndev->irq, print_mac(mac, ndev->dev_addr), mac_src); } return 0; out: dev_err(db->dev, "not found (%d).\n", ret); /*失败时,释放资源*/ dm9000_release_board(pdev, db); free_netdev(ndev); return ret; }