linux enc28j60網卡驅動移植(硬體spi和類比spi),enc28j60spi

來源:互聯網
上載者:User

linux enc28j60網卡驅動移植(硬體spi和類比spi),enc28j60spi

本來想移植DM9000網卡的驅動,無奈硬體出了點問題,通過杜邦線連結開發板和DM9000網卡模組,系統上電,還沒載入網卡驅動就直接崩潰了,找不到原因。。。剛好手上有一個enc28j60的網卡模組,於是就著手移植enc28j60的驅動。

其實移植enc28j60的驅動也十分簡單,網上有現成的,只需要分配一些硬體資源即可。

由於我的核心版本老到掉牙,沒有內建enc28j60的驅動,只能在網上找一個:

enc28j60.c

http://git.ti.com/ti-linux-kernel/ti-linux-kernel/blobs/7dac6f8df607929e51f4fd598d80bd009c45a9f8/drivers/net/enc28j60.c

enc28j60_hw.h

http://git.ti.com/ti-linux-kernel/ti-linux-kernel/blobs/7dac6f8df607929e51f4fd598d80bd009c45a9f8/drivers/net/enc28j60_hw.h

由於這個驅動是支援較新的核心,移植到2.6.22.6,只要改動3個地方好了。

 1 ... ... 2  3 static int enc28j60_set_hw_macaddr(struct net_device *ndev) 4 { 5     ... ... 6      7     if (!priv->hw_enable) { 8         if (netif_msg_drv(priv)) { 9             /* [cgw]: 屏蔽一下幾行 */10             //DECLARE_MAC_BUF(mac);11             //printk(KERN_INFO DRV_NAME12             //    ": %s: Setting MAC address to %s\n",13             //    ndev->name, print_mac(mac, ndev->dev_addr));14         }15     }16 17     ... ...18 }19 20 ... ...21 22 static void dump_packet(const char *msg, int len, const char *data)23 {24     printk(KERN_DEBUG DRV_NAME ": %s - packet len:%d\n", msg, len);25     /* [cgw]: 屏蔽一下幾行 */26     //print_hex_dump(KERN_DEBUG, "pk data: ", DUMP_PREFIX_OFFSET, 16, 1,27     //        data, len, true);28 }29 30 ... ...31     32 static int enc28j60_net_open(struct net_device *dev)33 {34     ... ...35 36     if (!is_valid_ether_addr(dev->dev_addr)) {37         if (netif_msg_ifup(priv)) {38             /* [cgw]: 屏蔽一下幾行 */39             //DECLARE_MAC_BUF(mac);40             //dev_err(&dev->dev, "invalid MAC address %s\n",41             //    print_mac(mac, dev->dev_addr));42         }43         return -EADDRNOTAVAIL;44     }45 46     ... ...47 }48 49 ... ...


都是些列印相關的東西,屏蔽掉就好。

spi的架構可以參考這裡:http://www.cnblogs.com/hackfun/p/6082489.html

這裡只列出配置spi硬體資源的代碼,只需要寫一個spi_platform_dev.c檔案就行了。類比spi的模式下,spi_platform_dev.c和http://www.cnblogs.com/hackfun/p/6082489.html這裡的spi_platform_dev.c檔案相似,只需要增加一個外部中斷入口給enc28j60用於接收中斷,和更改spi的模式等。

類比spi的模式下的spi_platform_dev.c

  1 #include <linux/module.h>  2 #include <linux/version.h>  3   4 #include <linux/init.h>  5   6 #include <linux/kernel.h>  7 #include <linux/types.h>  8 #include <linux/interrupt.h>  9 #include <linux/list.h> 10 #include <linux/timer.h> 11 #include <linux/init.h> 12 #include <linux/serial_core.h> 13 #include <linux/platform_device.h> 14 #include <linux/irq.h> 15  16 #include <asm/gpio.h> 17 #include <asm/io.h> 18 #include <asm/arch/regs-gpio.h> 19  20 #include <linux/spi/spi.h> 21 #include <linux/spi/spi_bitbang.h> 22  23 #include <asm/arch/regs-spi.h> 24 #include <asm/arch/spi.h> 25 #include <asm/arch/spi-gpio.h> 26  27  28 static struct spi_board_info board_info[1] = { 29     { 30     .modalias = "enc28j60",    /* [cgw]: spi裝置名稱,和裝置驅動名對應 */ 31     .bus_num = 0,                 /* [cgw]: spi匯流排號,即spi0 */ 32     .chip_select = 2,             /* [cgw]: spi匯流排上的裝置號,即spi0.2 */ 33     .max_speed_hz    = 50000,     /* [cgw]: spi時鐘 */ 34     .mode = SPI_MODE_0,           /* [cgw]: spi資料模式 */ 35     .irq = IRQ_EINT2, 36     }, 37 }; 38  39  40 static void enc28j60_chip_select(struct s3c2410_spigpio_info *spi, int cs) 41 { 42     /* [cgw]: 選中裝置號為2的spi裝置 */ 43     if (spi->board_info->chip_select == 2) { 44         s3c2410_gpio_cfgpin(S3C2410_GPG2, S3C2410_GPIO_OUTPUT); 45         /* [cgw]: 選中裝置 */ 46         if (BITBANG_CS_ACTIVE == cs) { 47             s3c2410_gpio_setpin(S3C2410_GPG2, 0); 48         /* [cgw]: 釋放裝置 */ 49         } else if (BITBANG_CS_INACTIVE == cs) { 50             s3c2410_gpio_setpin(S3C2410_GPG2, 1); 51         } 52     } 53 } 54  55 /* [cgw]:  */ 56 static struct s3c2410_spigpio_info spi_dev = { 57     .pin_clk = S3C2410_GPG7, 58     .pin_mosi = S3C2410_GPG6, 59     .pin_miso = S3C2410_GPG5, 60     .board_size = 1,                    /* [cgw]: 設定板上spi介面數量為1 */ 61     .board_info = &board_info[0], 62     .chip_select = enc28j60_chip_select 63 }; 64  65 static void spi_dev_release(struct device * dev) 66 { 67     printk("spi_dev_release! \n"); 68 } 69  70 /* [cgw]: 分配一個平台裝置 */ 71 static struct platform_device spi_platform_dev = { 72     .name         = "s3c24xx-spi-gpio",        /* [cgw]: 設定平台裝置名稱,和平台驅動名對應 */ 73     .id           = -1, 74     .dev = {  75         .release = spi_dev_release, 76         .platform_data = (void *)&spi_dev,      /* [cgw]: 通過platform_data傳遞spi_dev給平台驅動 77                                                 * 平台驅動可以訪問spi_dev 78                                                 */ 79     }, 80 }; 81  82  83 static int spi_dev_init(void) 84 { 85     s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_EINT2); 86  87     /* [cgw]: 註冊spi_platform_dev平台裝置 */ 88     platform_device_register(&spi_platform_dev); 89     return 0; 90 } 91  92 static void spi_dev_exit(void) 93 { 94     /* [cgw]: 登出spi_platform_dev平台裝置 */ 95     platform_device_unregister(&spi_platform_dev); 96 } 97  98 module_init(spi_dev_init); 99 module_exit(spi_dev_exit);100 101 MODULE_LICENSE("GPL");

 

makefile:

KERN_DIR = /work/system/linux-2.6.22.6all:    make -C $(KERN_DIR) M=`pwd` modules clean:    make -C $(KERN_DIR) M=`pwd` modules clean    rm -rf modules.orderobj-m    += spi_platform_dev.oobj-m    += spi_s3c24xx_gpio.oobj-m    += spi_bitbang.oobj-m    += enc28j60.o

 

硬體spi的模式下的spi_platform_dev.c

  1 #include <linux/module.h>  2 #include <linux/version.h>  3   4 #include <linux/init.h>  5   6 #include <linux/kernel.h>  7 #include <linux/types.h>  8 #include <linux/interrupt.h>  9 #include <linux/list.h> 10 #include <linux/timer.h> 11 #include <linux/init.h> 12 #include <linux/serial_core.h> 13 #include <linux/platform_device.h> 14 #include <linux/irq.h> 15  16 #include <asm/gpio.h> 17 #include <asm/io.h> 18 #include <asm/arch/regs-gpio.h> 19  20 #include <linux/spi/spi.h> 21 #include <linux/spi/spi_bitbang.h> 22  23 #include <asm/arch/regs-spi.h> 24 #include <asm/arch/spi.h> 25 #include <asm/arch/spi-gpio.h> 26  27  28 /* SPI (1) */ 29  30 static struct resource s3c_spi1_resource[] = { 31     [0] = { 32         .start = S3C2410_PA_SPI + S3C2410_SPI1, 33         .end   = S3C2410_PA_SPI + S3C2410_SPI1 + 0x1f, 34         .flags = IORESOURCE_MEM, 35     }, 36     [1] = { 37         .start = IRQ_SPI1, 38         .end   = IRQ_SPI1, 39         .flags = IORESOURCE_IRQ, 40     } 41  42 }; 43  44  45 static struct spi_board_info board_info[1] = { 46     { 47     .modalias = "enc28j60",    /* [cgw]: spi裝置名稱,和裝置驅動名對應 */ 48     .bus_num = 0,                 /* [cgw]: spi匯流排號,即spi0 */ 49     .chip_select = 2,             /* [cgw]: spi匯流排上的裝置號,即spi0.2 */ 50     .max_speed_hz    = 50000,     /* [cgw]: spi時鐘 */ 51     .mode = SPI_MODE_0,           /* [cgw]: spi資料模式 */ 52     .irq = IRQ_EINT2, 53     }, 54 }; 55  56 static struct s3c2410_spi_info spi_info = { 57     .pin_cs = S3C2410_GPG2,    /* simple gpio cs */ 58     .board_size = ARRAY_SIZE(board_info), 59     .board_info = &board_info[0], 60     .set_cs = NULL 61 }; 62  63  64 static void spi_dev_release(struct device * dev) 65 { 66     printk("spi_dev_release! \n"); 67 } 68  69 /* [cgw]: 分配一個平台裝置 */ 70 static struct platform_device spi_platform_dev = { 71     .name         = "s3c2410-spi",        /* [cgw]: 設定平台裝置名稱,和平台驅動名對應 */ 72     .id           = 1, 73     .num_resources      = ARRAY_SIZE(s3c_spi1_resource), 74     .resource      = s3c_spi1_resource, 75     .dev = {  76         .release = spi_dev_release, 77         .platform_data = &spi_info, 78         //.dma_mask = &s3c_device_spi1_dmamask, 79         //.coherent_dma_mask = 0xffffffffUL 80     }, 81 }; 82  83  84 static int spi_dev_init(void) 85 { 86     /* [cgw]: 註冊spi_platform_dev平台裝置 */ 87     platform_device_register(&spi_platform_dev); 88     return 0; 89 } 90  91 static void spi_dev_exit(void) 92 { 93     /* [cgw]: 登出spi_platform_dev平台裝置 */ 94     platform_device_unregister(&spi_platform_dev); 95 } 96  97 module_init(spi_dev_init); 98 module_exit(spi_dev_exit); 99 100 MODULE_LICENSE("GPL");

 

makefile:

KERN_DIR = /work/system/linux-2.6.22.6all:    make -C $(KERN_DIR) M=`pwd` modules clean:    make -C $(KERN_DIR) M=`pwd` modules clean    rm -rf modules.orderobj-m    += spi_platform_dev.oobj-m    += spi_s3c24xx.oobj-m    += spi_bitbang.oobj-m    += enc28j60.o

 


載入spi平台裝置時(platform_device),應注意類比spi時應載入spi_s3c24xx_gpio.c,硬體spi時應載入spi_s3c24xx.c

如:

類比spi:

1 # insmod spi_bitbang.ko2 # insmod spi_platform_dev.ko3 # insmod spi_s3c24xx_gpio.ko4 # insmod enc28j60.ko


硬體spi:

1 # insmod spi_bitbang.ko2 # insmod spi_platform_dev.ko3 # insmod spi_s3c24xx.ko4 # insmod enc28j60.ko


其中spi_bitbang.c , spi_s3c24xx_gpio.c , spi_s3c24xx.c為核心原生源檔案,不需要改動。

 

硬體spi時,載入驅動的執行個體:

 

謝謝!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.