DM9000是開發板經采用的網絡芯片,是一種高度集成而且功耗很低的高速網絡控制器,可以和CPU直連,支持10/100M以太網連接,芯片內部自帶16K SARM(3KB用來發送,13KB用來接收).
1.模塊初始化
- static struct platform_driver dm9000_driver = {
- .driver = {
- .name = "dm9000",
- .owner = THIS_MODULE,
- },
- .probe = dm9000_probe,
- .remove = __devexit_p(dm9000_drv_remove),
- .suspend = dm9000_drv_suspend,
- .resume = dm9000_drv_resume,
- };
-
- static int __init
- dm9000_init(void)
- {
- printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
-
- return platform_driver_register(&dm9000_driver);
- }
模塊初始化完成了基於platfrom平台的DM9000網卡驅動的注冊,當DM9000網卡找到其對應的能處理的platform設備後調用probe函數。
2.DM9000網卡初始化
在probe函數中完成了對DM9000網卡的初始化
DM9000的特性:DM9000地址信號和數據信號復用使用CMD引腳區分它們(CMD為低是讀寫DM900地址寄存器,CMD為高時讀寫DM9000數據寄存器),訪問DM9000內部寄存器時,先將CMD置低,寫DM900地址寄存器,然後將CMD置高,讀寫DM9000數據寄存器。
- 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;
-
- /* Init network device */
- //申請net_device結構
- ndev = alloc_etherdev(sizeof(struct board_info));
- if (!ndev) {
- dev_err(&pdev->dev, "could not allocate device.\n");
- return -ENOMEM;
- }
-
-
- //將net_device的parent指針指向platform_device對象,表示該設備掛載platform設備上。
- SET_NETDEV_DEV(ndev, &pdev->dev);
-
- dev_dbg(&pdev->dev, "dm9000_probe()\n");
-
- /* setup board info structure */
- //獲取net_device私有數據結構指針
- db = netdev_priv(ndev);
- memset(db, 0, sizeof(*db));
-
- //設置相關設備
- db->dev = &pdev->dev;
- db->ndevndev = ndev;
-
- spin_lock_init(&db->lock);
- mutex_init(&db->addr_lock);
-
- INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
-
- //獲取平台設備資源。包括DM9000地址寄存器地址,DM9000數據寄存器地址,和DM900所占用的中斷號
-
- 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內存區域並映射
- 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;
- }
-
- //申請數據寄存器IO內存區域並映射
- 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 */
- dm9000_set_io(db, iosize);
-
- /* check to see if anything is being over-ridden */
- 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
-
- //復位網卡芯片
- dm9000_reset(db);
-
- //讀取設備ID,判斷是否是驅動能夠處理的網卡芯片
- /* 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);
- }
-
- 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 */
-
- 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 */
- 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;
- 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_src = "eeprom";
-
- //從EEPROM中讀取MAC地址填充dev_addr
- /* 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);
-
- if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
- mac_src = "platform data";
- memcpy(ndev->dev_addr, pdata->dev_addr, 6);
- }
-
- 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);
- }
-
- if (!is_valid_ether_addr(ndev->dev_addr))
- dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
- "set using ifconfig\n", ndev->name);
-
- //設置平台設備驅動的dev成員為ndev。
- platform_set_drvdata(pdev, ndev);
-
- //注冊網絡設備驅動
- ret = register_netdev(ndev);
-
- if (ret == 0)
- printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
- ndev->name, dm9000_type_to_char(db->type),
- db->io_addr, db->io_data, ndev->irq,
- 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;
- }
我們在來看看讀寫網卡寄存器所用的ior和iow
- static u8
- ior(board_info_t * db, int reg)
- {
- writeb(reg, db->io_addr);
- return readb(db->io_data);
- }
-
- static void
- iow(board_info_t * db, int reg, int value)
- {
- writeb(reg, db->io_addr);
- writeb(value, db->io_data);
- }
可以看得出是先將要訪問的寄存器地址寫入到地址寄存器,然後在將數據寫入到數據寄存器。地址。