ARM-Linux驱动--DMA驱动分析(一)

来源:互联网 发布:python 创建多个空字典 编辑:程序博客网 时间:2024/06/05 09:56
 

1、DMA的功能和工作原理这里就不多说了,可以查看s3c2440的手册

2、在正式分析DMA驱动之前,我们先来看一下DMA的注册和初始化过程

系统设备:(翻译自源码注释)

系统设备和系统模型有点不同,它不需要动态绑定驱动,不能被探测(probe),不归结为任何的系统总线,所以要区分对待。对待系统设备我们仍然要有设备驱动的观念,因为我们需要对设备进行基本的操作。

定义系统设备,在./arch/arm/mach-s3c2440/s3c244x.c中

view plaincopy to clipboardprint?
  1. /* 定义系统设备类 */  
  2. struct sysdev_class s3c2440_sysclass = {  
  3.     .name       = "s3c2440-core",  
  4.     .suspend    = s3c244x_suspend,  
  5.     .resume     = s3c244x_resume  
  6. };  
注册系统设备类,在真正注册设备之前,确保已经注册了初始化了的系统设备类

view plaincopy to clipboardprint?
  1. static int __init s3c2440_core_init(void)  
  2. {  
  3.     return sysdev_class_register(&s3c2440_sysclass);  
  4. }  

下面就是系统设备类的注册函数,在./drivers/base/sys.c中

view plaincopy to clipboardprint?
  1. int sysdev_class_register(struct sysdev_class *cls)  
  2. {  
  3.     int retval;  
  4.   
  5.     pr_debug("Registering sysdev class '%s'\n", cls->name);  
  6.   
  7.     INIT_LIST_HEAD(&cls->drivers);  
  8.     memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));  
  9.     cls->kset.kobj.parent = &system_kset->kobj;  
  10.     cls->kset.kobj.ktype = &ktype_sysdev_class;  
  11.     cls->kset.kobj.kset = system_kset;  
  12.   
  13.     retval = kobject_set_name(&cls->kset.kobj, "%s", cls->name);  
  14.     if (retval)  
  15.         return retval;  
  16.   
  17.     retval = kset_register(&cls->kset);  
  18.     if (!retval && cls->attrs)  
  19.         retval = sysfs_create_files(&cls->kset.kobj,  
  20.                         (const struct attribute **)cls->attrs);  
  21.     return retval;  
  22. }  

view plaincopy to clipboardprint?
  1. /* 定义DMA系统设备驱动 */  
  2. static struct sysdev_driver s3c2440_dma_driver = {  
  3.     .add    = s3c2440_dma_add,/* 添加add函数 */  
  4. };  
下面是add函数,就是调用三个函数

view plaincopy to clipboardprint?
  1. static int __init s3c2440_dma_add(struct sys_device *sysdev)  
  2. {  
  3.     s3c2410_dma_init();  
  4.     s3c24xx_dma_order_set(&s3c2440_dma_order);  
  5.     return s3c24xx_dma_init_map(&s3c2440_dma_sel);  
  6. }  
注册DMA驱动到系统设备

view plaincopy to clipboardprint?
  1. static int __init s3c2440_dma_init(void)  
  2. {  
  3.     return sysdev_driver_register(&s3c2440_sysclass, &s3c2440_dma_driver);  
  4. }  
下面就是系统设备驱动的注册函数

view plaincopy to clipboardprint?
  1. /** 
  2.  *  sysdev_driver_register - Register auxillary driver 
  3.  *  @cls:   Device class driver belongs to. 
  4.  *  @drv:   Driver. 
  5.  * 
  6.  *  @drv is inserted into @cls->drivers to be 
  7.  *  called on each operation on devices of that class. The refcount 
  8.  *  of @cls is incremented. 
  9.  */  
  10.   
  11. int sysdev_driver_register(struct sysdev_class *cls, struct sysdev_driver *drv)  
  12. {  
  13.     int err = 0;  
  14.   
  15.     if (!cls) {  
  16.         WARN(1, KERN_WARNING "sysdev: invalid class passed to "  
  17.             "sysdev_driver_register!\n");  
  18.         return -EINVAL;  
  19.     }  
  20.   
  21.     /* Check whether this driver has already been added to a class. */  
  22.     if (drv->entry.next && !list_empty(&drv->entry))  
  23.         WARN(1, KERN_WARNING "sysdev: class %s: driver (%p) has already"  
  24.             " been registered to a class, something is wrong, but "  
  25.             "will forge on!\n", cls->name, drv);  
  26.   
  27.     mutex_lock(&sysdev_drivers_lock);  
  28.     if (cls && kset_get(&cls->kset)) {  
  29.         list_add_tail(&drv->entry, &cls->drivers);/* 将设备驱动添加到系统设备类的链表中 */  
  30.   
  31.         /* If devices of this class already exist, tell the driver */  
  32.         if (drv->add) {  
  33.             struct sys_device *dev;  
  34.             list_for_each_entry(dev, &cls->kset.list, kobj.entry)  
  35.                 drv->add(dev);  
  36.         }  
  37.     } else {  
  38.         err = -EINVAL;  
  39.         WARN(1, KERN_ERR "%s: invalid device class\n", __func__);  
  40.     }  
  41.     mutex_unlock(&sysdev_drivers_lock);  
  42.     return err;  
  43. }  
在./arch/arm/mach-s3c2440/s3c2440.c中定义s3c2440的系统设备和注册

view plaincopy to clipboardprint?
  1. static struct sys_device s3c2440_sysdev = {  
  2.     .cls        = &s3c2440_sysclass,/* 定义系统设备的所属系统设备类,用于系统设备注册到指定设备类 */  
  3. };  
  4. /* S3C2440初始化 */  
  5. int __init s3c2440_init(void)  
  6. {  
  7.     printk("S3C2440: Initialising architecture\n");  
  8.   
  9.     s3c24xx_gpiocfg_default.set_pull = s3c_gpio_setpull_1up;  
  10.     s3c24xx_gpiocfg_default.get_pull = s3c_gpio_getpull_1up;  
  11.   
  12.     /* change irq for watchdog */  
  13.   
  14.     s3c_device_wdt.resource[1].start = IRQ_S3C2440_WDT;  
  15.     s3c_device_wdt.resource[1].end   = IRQ_S3C2440_WDT;  
  16.   
  17.     /* register our system device for everything else */  
  18.   
  19.     return sysdev_register(&s3c2440_sysdev);/* 注册s3c2440的系统设备 */  
  20. }  
接下来是系统设备的注册函数

view plaincopy to clipboardprint?
  1. /** 
  2.  *  sysdev_register - add a system device to the tree 
  3.  *  @sysdev:    device in question 
  4.  * 
  5.  */  
  6.  /* 系统设备的注册 */  
  7. int sysdev_register(struct sys_device *sysdev)  
  8. {  
  9.     int error;  
  10.     struct sysdev_class *cls = sysdev->cls;/* 所属的系统设备类 */  
  11.   
  12.     if (!cls)  
  13.         return -EINVAL;  
  14.   
  15.     pr_debug("Registering sys device of class '%s'\n",  
  16.          kobject_name(&cls->kset.kobj));  
  17.   
  18.     /* initialize the kobject to 0, in case it had previously been used */  
  19.     memset(&sysdev->kobj, 0x00, sizeof(struct kobject));  
  20.   
  21.     /* Make sure the kset is set */  
  22.     sysdev->kobj.kset = &cls->kset;  
  23.   
  24.     /* Register the object */  
  25.     error = kobject_init_and_add(&sysdev->kobj, &ktype_sysdev, NULL,  
  26.                      "%s%d", kobject_name(&cls->kset.kobj),  
  27.                      sysdev->id);  
  28.   
  29.     if (!error) {  
  30.         struct sysdev_driver *drv;  
  31.   
  32.         pr_debug("Registering sys device '%s'\n",  
  33.              kobject_name(&sysdev->kobj));  
  34.   
  35.         mutex_lock(&sysdev_drivers_lock);  
  36.         /* Generic notification is implicit, because it's that 
  37.          * code that should have called us. 
  38.          */  
  39.   
  40.         /* Notify class auxillary drivers */  
  41.         list_for_each_entry(drv, &cls->drivers, entry) {  
  42.             if (drv->add)  
  43.                 drv->add(sysdev);/* 遍历该设备所属同一个设备类的所有设备,并执行相应的add函数 */  
  44.         }  
  45.         mutex_unlock(&sysdev_drivers_lock);  
  46.         kobject_uevent(&sysdev->kobj, KOBJ_ADD);  
  47.     }  
  48.   
  49.     return error;  
  50. }  
那DMA系统设备驱动中的add函数中到底是什么呢?

(1)首先看第一个函数int __init s3c2410_dma_init(void),在./arch/arm/plat-s3c24xx/dma.c

view plaincopy to clipboardprint?
  1. int __init s3c2410_dma_init(void)  
  2. {  
  3.     return s3c24xx_dma_init(4, IRQ_DMA0, 0x40);  
  4. }  
实际上就是初始化DMA为4通道,设置中断号,设置寄存器的覆盖范围

下面是该函数的实现

view plaincopy to clipboardprint?
  1. int __init s3c24xx_dma_init(unsigned int channels, unsigned int irq,  
  2.                 unsigned int stride)/* 参数分别为通道个数、中断号、寄存器的覆盖范围 */  
  3. {  
  4.     struct s3c2410_dma_chan *cp;/* 通道的结构体表示 */  
  5.     int channel;  
  6.     int ret;  
  7.   
  8.     printk("S3C24XX DMA Driver, Copyright 2003-2006 Simtec Electronics\n");  
  9.   
  10.     dma_channels = channels;  
  11.   
  12.     dma_base = ioremap(S3C24XX_PA_DMA, stride * channels);  
  13.     if (dma_base == NULL) {  
  14.         printk(KERN_ERR "dma failed to remap register block\n");  
  15.         return -ENOMEM;  
  16.     }  
  17.       
  18.     /* 分配DMA告诉缓冲区 */  
  19.     dma_kmem = kmem_cache_create("dma_desc",  
  20.                      sizeof(struct s3c2410_dma_buf), 0,  
  21.                      SLAB_HWCACHE_ALIGN,  
  22.                      s3c2410_dma_cache_ctor);  
  23.   
  24.     if (dma_kmem == NULL) {  
  25.         printk(KERN_ERR "dma failed to make kmem cache\n");  
  26.         ret = -ENOMEM;  
  27.         goto err;  
  28.     }  
  29.   
  30.     for (channel = 0; channel < channels;  channel++) {  
  31.         cp = &s3c2410_chans[channel];  
  32.   
  33.         memset(cp, 0, sizeof(struct s3c2410_dma_chan));  
  34.   
  35.         /* dma channel irqs are in order.. */  
  36.         cp->number = channel;  
  37.         cp->irq    = channel + irq;  
  38.         cp->regs   = dma_base + (channel * stride);  
  39.   
  40.         /* point current stats somewhere */  
  41.         cp->stats  = &cp->stats_store;  
  42.         cp->stats_store.timeout_shortest = LONG_MAX;  
  43.   
  44.         /* basic channel configuration */  
  45.   
  46.         cp->load_timeout = 1<<18;  
  47.   
  48.         printk("DMA channel %d at %p, irq %d\n",  
  49.                cp->number, cp->regs, cp->irq);  
  50.     }  
  51.   
  52.     return 0;  
  53.       
  54. /* 异常处理 */  
  55.  err:  
  56.     kmem_cache_destroy(dma_kmem);  
  57.     iounmap(dma_base);  
  58.     dma_base = NULL;  
  59.     return ret;  
  60. }  

(2)然后是函数s3c24xx_dma_order_set(&s3c2440_dma_order);

view plaincopy to clipboardprint?
  1. int __init s3c24xx_dma_order_set(struct s3c24xx_dma_order *ord)  
  2. {  
  3.     struct s3c24xx_dma_order *nord = dma_order;  
  4.   
  5.     if (nord == NULL)  
  6.         nord = kmalloc(sizeof(struct s3c24xx_dma_order), GFP_KERNEL);  
  7.   
  8.     if (nord == NULL) {  
  9.         printk(KERN_ERR "no memory to store dma channel order\n");  
  10.         return -ENOMEM;  
  11.     }  
  12.   
  13.     dma_order = nord;  
  14.     memcpy(nord, ord, sizeof(struct s3c24xx_dma_order));  
  15.     return 0;  
  16. }  
我们注意到函数中使用了kmalloc给结构体重新分配了内存,这是由于__initdata修饰的变量表示初始化用的变量,初始化完毕后空间自动释放,所以需要将其存储起来。

(3)最后一个函数s3c24xx_dma_init_map(&s3c2440_dma_sel)

该函数功能是建立DMA源与硬件通道的映射图

view plaincopy to clipboardprint?
  1. int __init s3c24xx_dma_init_map(struct s3c24xx_dma_selection *sel)  
  2. {  
  3.     struct s3c24xx_dma_map *nmap;  
  4.     size_t map_sz = sizeof(*nmap) * sel->map_size;  
  5.     int ptr;  
  6.   
  7.     nmap = kmalloc(map_sz, GFP_KERNEL);  
  8.     if (nmap == NULL)  
  9.         return -ENOMEM;  
  10.   
  11.     memcpy(nmap, sel->map, map_sz);  
  12.     memcpy(&dma_sel, sel, sizeof(*sel));  
  13.   
  14.     dma_sel.map = nmap;  
  15.   
  16.     for (ptr = 0; ptr < sel->map_size; ptr++)  
  17.         s3c24xx_dma_check_entry(nmap+ptr, ptr);  
  18.   
  19.     return 0;  
  20. }  
这里的kmalloc函数的作用同上面的作用一样。

注:由于内核实在是太深了,这里只是表面上按流程大体了解了子同设备的注册和系统设备驱动的注册以及DMA设备的注册和初始化,函数中有很多细节有待进一步研究。

原创粉丝点击