Linux enc28j60网卡驱动移植(硬件spi和模拟spi)
本来想移植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个地方好了。
... ...
static int enc28j60_set_hw_macaddr(struct net_device *ndev)
{
... ...
if (!priv->hw_enable) {
if (netif_msg_drv(priv)) {
/* [cgw]: 屏蔽一下几行 */
//DECLARE_MAC_BUF(mac);
//printk(KERN_INFO DRV_NAME
// ": %s: Setting MAC address to %s\n",
// ndev->name, print_mac(mac, ndev->dev_addr));
}
}
... ...
}
... ...
static void dump_packet(const char *msg, int len, const char *data)
{
printk(KERN_DEBUG DRV_NAME ": %s - packet len:%d\n", msg, len);
/* [cgw]: 屏蔽一下几行 */
//print_hex_dump(KERN_DEBUG, "pk data: ", DUMP_PREFIX_OFFSET, 16, 1,
// data, len, true);
}
... ...
static int enc28j60_net_open(struct net_device *dev)
{
... ...
if (!is_valid_ether_addr(dev->dev_addr)) {
if (netif_msg_ifup(priv)) {
/* [cgw]: 屏蔽一下几行 */
//DECLARE_MAC_BUF(mac);
//dev_err(&dev->dev, "invalid MAC address %s\n",
// print_mac(mac, dev->dev_addr));
}
return -EADDRNOTAVAIL;
}
... ...
}
... ...
都是些打印相关的东西,屏蔽掉就好。
模拟spi的模式下的spi_platform_dev.c
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/platform_device.h>
#include <linux/irq.h>
#include <asm/gpio.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <linux/spi/spi.h>
#include <linux/spi/spi_bitbang.h>
#include <asm/arch/regs-spi.h>
#include <asm/arch/spi.h>
#include <asm/arch/spi-gpio.h>
static struct spi_board_info board_info[1] = {
{
.modalias = "enc28j60", /* [cgw]: spi设备名,和设备驱动名对应 */
.bus_num = 0, /* [cgw]: spi总线号,即spi0 */
.chip_select = 2, /* [cgw]: spi总线上的设备号,即spi0.2 */
.max_speed_hz = 50000, /* [cgw]: spi时钟 */
.mode = SPI_MODE_0, /* [cgw]: spi数据模式 */
.irq = IRQ_EINT2,
},
};
static void enc28j60_chip_select(struct s3c2410_spigpio_info *spi, int cs)
{
/* [cgw]: 选中设备号为2的spi设备 */
if (spi->board_info->chip_select == 2) {
s3c2410_gpio_cfgpin(S3C2410_GPG2, S3C2410_GPIO_OUTPUT);
/* [cgw]: 选中设备 */
if (BITBANG_CS_ACTIVE == cs) {
s3c2410_gpio_setpin(S3C2410_GPG2, 0);
/* [cgw]: 释放设备 */
} else if (BITBANG_CS_INACTIVE == cs) {
s3c2410_gpio_setpin(S3C2410_GPG2, 1);
}
}
}
/* [cgw]: */
static struct s3c2410_spigpio_info spi_dev = {
.pin_clk = S3C2410_GPG7,
.pin_mosi = S3C2410_GPG6,
.pin_miso = S3C2410_GPG5,
.board_size = 1, /* [cgw]: 设置板上spi接口数量为1 */
.board_info = &board_info[0],
.chip_select = enc28j60_chip_select
};
static void spi_dev_release(struct device * dev)
{
printk("spi_dev_release! \n");
}
/* [cgw]: 分配一个平台设备 */
static struct platform_device spi_platform_dev = {
.name = "s3c24xx-spi-gpio", /* [cgw]: 设置平台设备名,和平台驱动名对应 */
.id = -1,
.dev = {
.release = spi_dev_release,
.platform_data = (void *)&spi_dev, /* [cgw]: 通过platform_data传递spi_dev给平台驱动
* 平台驱动可以访问spi_dev
*/
},
};
static int spi_dev_init(void)
{
s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_EINT2);
/* [cgw]: 注册spi_platform_dev平台设备 */
platform_device_register(&spi_platform_dev);
return 0;
}
static void spi_dev_exit(void)
{
/* [cgw]: 注销spi_platform_dev平台设备 */
platform_device_unregister(&spi_platform_dev);
}
module_init(spi_dev_init);
module_exit(spi_dev_exit);
MODULE_LICENSE("GPL");
makefile:
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += spi_platform_dev.o
obj-m += spi_s3c24xx_gpio.o
obj-m += spi_bitbang.o
obj-m += enc28j60.o
硬件spi的模式下的spi_platform_dev.c
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/platform_device.h>
#include <linux/irq.h>
#include <asm/gpio.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <linux/spi/spi.h>
#include <linux/spi/spi_bitbang.h>
#include <asm/arch/regs-spi.h>
#include <asm/arch/spi.h>
#include <asm/arch/spi-gpio.h>
/* SPI (1) */
static struct resource s3c_spi1_resource[] = {
[0] = {
.start = S3C2410_PA_SPI + S3C2410_SPI1,
.end = S3C2410_PA_SPI + S3C2410_SPI1 + 0x1f,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = IRQ_SPI1,
.end = IRQ_SPI1,
.flags = IORESOURCE_IRQ,
}
};
static struct spi_board_info board_info[1] = {
{
.modalias = "enc28j60", /* [cgw]: spi设备名,和设备驱动名对应 */
.bus_num = 0, /* [cgw]: spi总线号,即spi0 */
.chip_select = 2, /* [cgw]: spi总线上的设备号,即spi0.2 */
.max_speed_hz = 50000, /* [cgw]: spi时钟 */
.mode = SPI_MODE_0, /* [cgw]: spi数据模式 */
.irq = IRQ_EINT2,
},
};
static struct s3c2410_spi_info spi_info = {
.pin_cs = S3C2410_GPG2, /* simple gpio cs */
.board_size = ARRAY_SIZE(board_info),
.board_info = &board_info[0],
.set_cs = NULL
};
static void spi_dev_release(struct device * dev)
{
printk("spi_dev_release! \n");
}
/* [cgw]: 分配一个平台设备 */
static struct platform_device spi_platform_dev = {
.name = "s3c2410-spi", /* [cgw]: 设置平台设备名,和平台驱动名对应 */
.id = 1,
.num_resources = ARRAY_SIZE(s3c_spi1_resource),
.resource = s3c_spi1_resource,
.dev = {
.release = spi_dev_release,
.platform_data = &spi_info,
//.dma_mask = &s3c_device_spi1_dmamask,
//.coherent_dma_mask = 0xffffffffUL
},
};
static int spi_dev_init(void)
{
/* [cgw]: 注册spi_platform_dev平台设备 */
platform_device_register(&spi_platform_dev);
return 0;
}
static void spi_dev_exit(void)
{
/* [cgw]: 注销spi_platform_dev平台设备 */
platform_device_unregister(&spi_platform_dev);
}
module_init(spi_dev_init);
module_exit(spi_dev_exit);
MODULE_LICENSE("GPL");
makefile:
复制代码
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += spi_platform_dev.o
obj-m += spi_s3c24xx.o
obj-m += spi_bitbang.o
obj-m += enc28j60.o
加载spi平台设备时(platform_device),应注意模拟spi时应加载spi_s3c24xx_gpio.c,硬件spi时应加载spi_s3c24xx.c
如:
模拟spi:
1 # insmod spi_bitbang.ko
2 # insmod spi_platform_dev.ko
3 # insmod spi_s3c24xx_gpio.ko
4 # insmod enc28j60.ko
硬件spi:
1 # insmod spi_bitbang.ko
2 # insmod spi_platform_dev.ko
3 # insmod spi_s3c24xx.ko
4 # insmod enc28j60.ko
其中spi_bitbang.c , spi_s3c24xx_gpio.c , spi_s3c24xx.c为内核原生源文件,不需要改动。
硬件spi时,加载驱动的实例:
谢谢!