Chinaunix首页 | 论坛 | 博客
  • 博客访问: 393827
  • 博文数量: 138
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1620
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-10 16:55
个人简介

当你比别人优秀一点点,别人会嫉妒你。当你比别人优秀很多,别人会羡慕你。

文章分类

全部博文(138)

文章存档

2016年(2)

2015年(2)

2014年(15)

2013年(119)

我的朋友

分类: LINUX

2013-07-07 15:31:46


    内核提供了一个重要的结构体struct machine_desc ,这个结构体在内核移植中起到相当重要的作用,内核通过machine_desc结构体来控制系统体系架构相关部分的初始化。machine_desc结构体的成员包含了体系架构相关部分的几个最重要的初始化函数,包括map_io,init_irq, init_machine以及phys_io , timer成员等。

machine_desc结构体定义如下:

struct machine_desc {
            /*
                * Note! The first four elements are used
                * by assembler code in head-armv.S
                */
                unsigned int                  nr;         /* architecture number         */
                unsigned int                  phys_io;          /* start of physical io         */
                unsigned int                  io_pg_offst; /* byte offset for io
                                                * page tabe entry         */
                const char                 *name;          /* architecture name          */
                unsigned long                     ;boot_params;          /* tagged list          */
                unsigned int                  video_start;         /* start of video RAM          */
                unsigned int                  video_end;         /* end of video RAM          */
                unsigned int                 reserve_lp0 :1;          /* never has lp0          */
                unsigned int                 reserve_lp1 :1;          /* never has lp1          */
                unsigned int                  reserve_lp2 :1;          /* never has lp2          */
                unsigned int                  soft_reboot :1;          /* soft reboot          */
                void                          (*fixup)(struct machine_desc *,
                                                        struct tag *, char **,
                                                        struct meminfo *);
                void                         (*map_io)(void);/* IO mapping function          */
                void                         (*init_irq)(void);
                struct sys_timer          *timer;          /* system tick timer          */
                void                         (*init_machine)(void);
        };

machine_desc结构体通过MACHINE_START宏来初始化,这里以s3c2410平台为例:

s3c2410 machine_desc结构体定义如下:
        /* arch/arm/mach-s3c2410/mach-smdk2410.c */
        MACHINE_START(SMDK2410, "SMDK2410") /* @TODO: request a new identifier and switch
                                        * to SMDK2410 */
                /* Maintainer: Jonas Dietsche */
                .phys_io = S3C2410_PA_UART,
                .io_pg_offst = (((u32)S3C24XX_VA_UART) >> 18) & 0xfffc,
                .boot_params = S3C2410_SDRAM_PA + 0x100,
                .map_io = smdk2410_map_io,
                .init_irq = s3c24xx_init_irq,
                .init_machine = smdk2410_init,
                .timer = &s3c24xx_timer,
        MACHINE_END

其中的宏MACHINE_START和MACHINE_END定义如下:
        /* 
          * Set of macros to define architecture features. This is built into
          * a table by the linker.
          */
        #define MACHINE_START(_type,_name) \
        const struct machine_desc __mach_desc_##_type \
          __attribute__((__section__(".arch.info.init"))) = { \
                .nr = MACH_TYPE_##_type, \
                .name = _name,

#define MACHINE_END \
        };

其中MACH_TYPE_##_type 为GCC扩展语法中的字符拼接标识,在预编译的时候会用真正的字符代替,比如我们这里就是MACH_TYPE_SMDK2410

MACHINE_START的使用及各个成员函数的的放置位置以及调用过程如下:
        MACH_TYPE_SMDK2410这个值是目标板的类型值,定义在arch/include/asm-arm/mach-types.h内,值为193.

/* arch/include/asm-arm/mach-types.h */
        #define MACH_TYPE_SMDK2410 193

由上发现,MACHINE_START主要是定义了"struct machine_desc"的类型,放在 section(".arch.info.init"),是初始化数据,其所占用的内存在内核起来之后将会被释放。

这里的map_io成员即内核提供给用户的创建外设I/O资源到内核虚拟地址静态映射表的接口函数。map_io成员函数会在系统初始化过程中被调用,流程如下:
        start_kernel -> setup_arch() --> paging_init()中被调用
        struct machine_desc 结构体的各个成员函数在不同时期被调用:
        1. .init_machine 在 arch/arm/kernel/setup.c 中被 customize_machine 调用,放在 arch_initcall( ) 段里面,会自动按顺序被调用(另外博客分析,敬请关注)。
        2. init_irq在start_kernel( ) --> init_IRQ( ) --> init_arch_irq( ) 被调用
        3. map_io 在 setup_arch( ) --> paging_init( )被调用

其他主要都在 setup_arch() 中用到。

用户可以在定义machine_desc结构体时指定map_io的接口函数,我们也正是这样做的。

接下来我们继续分析smdk2410_map_io的执行过程,流程如下:

smdk2410_map_io-> s3c24xx_init_io(smdk2410_iodesc, ARRAY_SIZE(smdk2410_iodesc))

下面来看一下s3c24xx_init_io函数:

void __init s3c24xx_init_io(struct map_desc *mach_desc, int mach_size)
        {
                /* register our io-tables */
                iotable_init(s3c_iodesc, ARRAY_SIZE(s3c_iodesc));
                ……
        }

iotable_init内核提供,定义如下:

/*
        * Create the architecture specific mappings
        */
        void __init iotable_init(struct map_desc *io_desc, int nr)
        {
                int i;
                for (i = 0; i nr; i++)
                create_mapping(io_desc + i);
        }

由上知道,smdk2410_map_io最终调用iotable_init建立映射表。

iotable_init函数的参数有两个:一个是map_desc类型的结构体,另一个是该结构体的数量nr。这里最关键的就是struct map_desc。map_desc结构体定义如下:

/* include/asm-arm/mach/map.h */
                struct map_desc {
                unsigned long virtual;
                unsigned long physical;
                unsigned long length;
                unsigned int type;
        };

create_mapping( )函数就是通过map_desc提供的信息创建线性映射表的。

这样的话我们就知道了创建I/O映射表的大致流程为:只要定义相应I/O资源的map_desc结构体,并将该结构体传给iotable_init函数执行,就可以创建相应的I/O资源到内核虚拟地址空间的映射表了。

我们来看看s3c2410是怎么定义map_desc结构体的(即上面iotable_init()函数内的s3c_iodesc)。

[arch/arm/mach-s3c2410/cpu.c]
        /* minimal IO mapping */
        static struct map_desc s3c_iodesc[] __initdata = {
                IODESC_ENT(GPIO),
                IODESC_ENT(IRQ),
                IODESC_ENT(MEMCTRL),
                IODESC_ENT(UART)
        };

IODESC_ENT宏如下:

#define IODESC_ENT(x) { (unsigned long)S3C24XX_VA_##x, S3C2410_PA_##x, S3C24XX_SZ_##x, MT_DEVICE }

展开后等价于:

static struct map_desc s3c_iodesc[] __initdata = {
                {
                        .virtual = S3C24XX_VA_GPIO,
                        .physical = S3C24XX_PA_GPIO,
                        .length = S3C24XX_SZ_GPIO,
                        .type = MT_DEVICE
                },
                ……
        };

至此,我们可以比较清晰看到GPIO被静态映射的过程,由于我们在前面的静态映射中已经做好了GPIO的映射,也就是我们写GPIO相关驱动的时候可以如下配置引脚的原因:
        s3c2410_gpio_cfgpin(xxx,xxx);

其实,s3c2410_gpio_cfgpin定义如下:

void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int function)
        {
                void __iomem *base = S3C2410_GPIO_BASE(pin);
                unsigned long mask;
                unsigned long con;
                unsigned long flags;

        if (pin < S3C2410_GPIO_BANKB) {
                        mask = 1 << S3C2410_GPIO_OFFSET(pin);
                } else {
                        mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
                }

        local_irq_save(flags);

        con = __raw_readl(base + 0x00);
                con &= ~mask;
                con |= function;

        __raw_writel(con, base + 0x00);

        local_irq_restore(flags);
        }

其中,比较关键的一个地方:
        void __iomem *base = S3C2410_GPIO_BASE(pin);
        这一行中,S3C2410_GPIO_BASE定义如下:

#define S3C2410_GPIO_BASE(pin) ((((pin) & ~31) >> 1) + S3C24XX_VA_GPIO)

至此,GPIO的静态映射就看得很明白了。

下面来看其他外设的静态映射:

在s3c24xx_init_io()函数中,除了iotable_init()以为,还会在最后调用,
        (cpu->map_io)(mach_desc, size);

而CPU的这个map_io在arch/arm/mach-s3c2410/cpu.c里面定义如下:

static struct cpu_table cpu_ids[] __initdata = {
                {
                        .idcode = 0x32410000,
                        .idmask = 0xffffffff,
                        .map_io = s3c2410_map_io,
                        .init_clocks = s3c2410_init_clocks,
                        .init_uarts = s3c2410_init_uarts,
                        .init = s3c2410_init,
                        .name = name_s3c2410
                },
                        ...
         }

再查看s3c2410_map_io(),函数代码如下:

void __init s3c2410_map_io(struct map_desc *mach_desc, int mach_size)
        {
                /* register our io-tables */

        iotable_init(s3c2410_iodesc, ARRAY_SIZE(s3c2410_iodesc));
                iotable_init(mach_desc, mach_size);
        }

接下来看结构s3c2410_iodesc [arch/arm/mach-s3c2410/s3c2410.c],代码如下,

/* Initial IO mappings */
        static struct map_desc s3c2410_iodesc[] __initdata = {
                IODESC_ENT(USBHOST),
                IODESC_ENT(USBDEV),
                IODESC_ENT(CLKPWR),
                IODESC_ENT(LCD),
                IODESC_ENT(TIMER),
                IODESC_ENT(ADC),
                IODESC_ENT(WATCHDOG),
        };

赫然发现IODESC_ENT(TIMER)这一行,结合之前GPIO的类似分析,IODESC_ENT宏如下:
        #define IODESC_ENT(x) { (unsigned long)S3C24XX_VA_##x, S3C2410_PA_##x, S3C24XX_SZ_##x, MT_DEVICE }

至此,TIMER, USBHOST,USBDEV,lcd,adc,watchdog等的静态映射都看得很明白了。    

阅读(588) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~