分类: LINUX
2010-01-13 22:40:01
一、Linux 驱动的基本理论
理解linux驱动,最重要的是要区分device和driver这两个概念,要搞清device和driver之间的联系。 device 描述了某个设备所占用的硬件资源(地址、中断),可以理解为硬件方面描述。而driver则是描述了使用和操作该设备的方法、流程、逻辑,可以理解为软件方面的描述。这二者之间的对应联系是一个设备名。
我们来看一下两个结构体的定义:
struct platform_device {
const char* name; /* 连接device和driver的名字,系统内唯一!*/
int id;
struct device dev;
u32 num_resources;
struct resource * resource;
struct platform_device_id * id_entry;
struct pdev_archdata archdata;
};
在arch/arm/mach-s
struct device {
struct device *parent;
struct device_private *p;
struct kobject kobj;
const char *init_name; /* initial name of the device */
struct device_type *type;
struct semaphore sem; /* semaphore to synchronize calls to its driver. */
struct bus_type *bus; /* type of bus device is on */
struct device_driver *driver; /* which driver has allocated this device */
void *platform_data; /* Platform specific data, device core doesn't touch it */
struct dev_pm_info power;
#ifdef CONFIG_NUMA
int numa_node; /* NUMA node this device is close to */
#endif
u64 *dma_mask; /* dma mask (if dma'able device) */
u64 coherent_dma_mask;
struct device_dma_parameters *dma_parms;
struct list_head dma_pools; /* dma pools (if dma'ble) */
struct dma_coherent_mem *dma_mem; /* internal for coherent mem override */
struct dev_archdata archdata; /* arch specific additions */
dev_t devt; /* dev_t, creates the sysfs "dev" */
spinlock_t devres_lock;
struct list_head devres_head;
struct klist_node knode_class;
struct class *class;
const struct attribute_group **groups; /* optional groups */
void (*release)(struct device *dev);
};
struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver;
struct platform_device_id *id_table;
};
在drivers/serial/8250.c中初始化
struct device_driver {
const char *name; /* 连接device和driver的名字,系统内唯一!*/
struct bus_type *bus;
struct module *owner;
const char *mod_name; /* used for built-in modules */
bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
int (*probe) (struct device *dev);
int (*remove) (struct device *dev);
void (*shutdown) (struct device *dev);
int (*suspend) (struct device *dev, pm_message_t state);
int (*resume) (struct device *dev);
const struct attribute_group **groups;
const struct dev_pm_ops *pm;
struct driver_private *p;
};
二、ST
st
三、移植过程
硬件平台:mini2440 系统:linux-
红色部分为添加的语句。
1. 修改arch/arm/mach-s
添加头文件
#ifdef CONFIG_SERIAL_8250_MINI2440_ST
#include
#endif
初始化st
/* STC
#define PORT(_base,_irq) \
{ //ARM体系结构中将IO和MEMORY统一编址,
//因此这里使用的是Memory地址
.mapbase = (unsigned long)_base, \
.irq = _irq, \
.uartclk = 1843200, \
.iotype = UPIO_MEM32, \
.flags = (UPF_BOOT_AUTOCONF | UPF_IOREMAP), \
.regshift = 0, \
}
static struct plat_serial8250_port mini2440_st
PORT(S
PORT(S
PORT(S
PORT(S
{ },
};
static struct platform_device mini2440_device_st
.name = "serial8250",
.id = PLAT8250_DEV_EXAR_ST
.dev = {
.platform_data = &mini2440_st
},
};
将st
/* devices we initialise */
static struct platform_device *mini2440_devices[] __initdata = {
&s
&s
&s
&s
&s
&s
&mini2440_device_eth,
//#ifdef CONFIG_SERIAL_8250_MINI2440_ST
&mini2440_device_st
//#endif
&s
&s
&s
&s
};
2.修改 drivers/serial/8250.c
添加头文件
#ifdef CONFIG_SERIAL_8250_MINI2440_ST
#include
#endif
修改S
static int __init serial8250_init(void)
{
int ret;
if (nr_uarts > UART_NR)
nr_uarts = UART_NR;
printk(KERN_INFO "Serial: 8250/16550 driver, "
"%d ports, IRQ sharing %sabled\n", nr_uarts,
share_irqs ? "en" : "dis");
#ifdef CONFIG_SERIAL_8250_MINI2440_ST
/* Set the 8bit bus width of bank1,bank2,bank3,bank5*/
*((volatile unsigned int *)S
((*((volatile unsigned int *)S
| S
| S
| S
| S
/* Set the interface timing between s
// *((volatile unsigned int *)S
// *((volatile unsigned int *)S
// *((volatile unsigned int *)S
// *((volatile unsigned int *)S
#endif
#ifdef CONFIG_SPARC
ret = sunserial_register_minors(&serial8250_reg, UART_NR);
#else
serial8250_reg.nr = UART_NR;
ret = uart_register_driver(&serial8250_reg);
#endif
if (ret)
goto out;
#ifdef CONFIG_8250_MINI2440_ST
serial8250_isa_devs =
platform_device_alloc("serial8250", PLAT8250_DEV_EXAR_ST
#else
serial8250_isa_devs = platform_device_alloc("serial8250",
PLAT8250_DEV_LEGACY);
#endif
修改中断信号的类型为下降沿触发
if (i->head) {
list_add(&up->list, i->head);
spin_unlock_irq(&i->lock);
ret = 0;
} else {
INIT_LIST_HEAD(&up->list);
i->head = &up->list;
spin_unlock_irq(&i->lock);
#ifdef CONFIG_SERIAL_8250_MINI2440_ST
irq_flags |= IRQF_TRIGGER_RISING;
#else
irq_flags |= up->port.irqflags;
#endif
ret = request_irq(up->port.irq, serial8250_interrupt,
irq_flags, "serial", i);
if (ret < 0)
serial_do_unlink(i, up);
}
3.修改 drivers/serial/Kconfig
添加一个编译配置选项
config SERIAL_8250_MINI2440_ST
bool "Support MINI2440 Extend ST
depends on SERIAL_8250
help
A chip of ST
To compile this driver as a module, choose M here: the module will be called 8250_mini2440_st
4.重新编译内核
> make menucofig
Device Drivers à
Character Devices à
Serial Drivers à
<*> 8250/
[*] Support MINI2440 Extend ST
保存.config文件
> make zImage
这样驱动就添加好了,如果你的根文件系统使用了mdev,那么不用做任何修改,mdev会自动地将四个新串口添加在 /dev/serial/tty目录下面,分别为 ttyS0, ttyS1, ttyS2, ttyS3。
查看更详细的信息
> cat /proc/tty/driver/serial 将显示四个串口的物理地址和虚拟地址
四.碰到的问题
内核启动过程中,报错
Unable to handle kernel NULL pointer dereference at virtual address
产生这个错误有两种可能:
(1) 地址指针错误,比如在初始化平台设备结构体时丢掉了&符号。
static struct platform_device mini2440_device_st
.name = "serial8250",
.id = PLAT8250_DEV_EXAR_ST
.dev = {
.platform_data = &mini2440_st
},
};
(2) 在为ST
#define PORT(_base,_irq) \
{ \
.mapbase = (unsigned long)_base, \
.irq = _irq, \
.uartclk = 1843200, \
.iotype = UPIO_MEM32, \
.flags = (UPF_BOOT_AUTOCONF | UPF_IOREMAP), \
.regshift = 0, \
}
在 driver/serial/8250_exar_st
#define PORT(_base,_irq) \
{ \
.iobase = _base, \
.irq = _irq, \
.uartclk = 1843200, \
.iotype = UPIO_PORT, \
.flags = UPF_BOOT_AUTOCONF, \
}
chinaunix网友2010-03-24 10:40:20
你好,看了这个贴觉得很受益 我也是在arm平台移植16c554驱动,请问你在硬件上配置为4个独立中断还是4路串口中断并为一路给CPU? 谢谢~~