内核启动时,会调用s3c24xx_register_clock函数注册很多时钟,所谓注册,就是在一个链表中保存各种"struct clk*"结构指针,这些"struct clk"结构有:clk_f(表示FCLK),clk_h(表示HCLK),clk_p(表示PCLK)等。然后可以通过clk_get_rate函数获得获得某类时钟频率。下面到内核中分析一下源码,这里跟踪内核启动时clock system的初始化过程:
- asmlinkage void __init start_kernel(void)
- {
- ............
- setup_arch(&command_line);
- ............
- }
- void __init setup_arch(char **cmdline_p)
- {
- ..........
- paging_init(mdesc);
- ..........
- }
- void __init paging_init(struct machine_desc *mdesc){
- .............
- devicemaps_init(mdesc);
- .............
- }
- static void __init devicemaps_init(struct machine_desc *mdesc)
- {
- ....................
- /*这里调用具体体系结构的map_io函数*/
- if (mdesc->map_io)
- mdesc->map_io();
- ...................
- }
- static void __init smdk2440_map_io(void)
- {
- s3c24xx_init_io(smdk2440_iodesc, ARRAY_SIZE(smdk2440_iodesc));
- s3c24xx_init_clocks(12000000);
- s3c24xx_init_uarts(smdk2440_uartcfgs, ARRAY_SIZE(smdk2440_uartcfgs));
- }
- /* s3c24xx_init_clocks
- *
- * Initialise the clock subsystem and associated information from the
- * given master crystal value.
- *
- * xtal = 0 -> use default PLL crystal value (normally 12MHz)
- * != 0 -> PLL crystal value in Hz
- */
- void __init s3c24xx_init_clocks(int xtal)
- {
- if (xtal == 0)
- xtal = 12*1000*1000;
- if (cpu == NULL)
- panic("s3c24xx_init_clocks: no cpu setup?/n");
- if (cpu->init_clocks == NULL)
- panic("s3c24xx_init_clocks: cpu has no clock init/n");
- else
- (cpu->init_clocks)(xtal);
- }
- static struct cpu_table cpu_ids[] __initdata = {
- ......................
- {
- .idcode = 0x32440001,
- .idmask = 0xffffffff,
- .map_io = s3c244x_map_io,
- .init_clocks = s3c244x_init_clocks,
- .init_uarts = s3c244x_init_uarts,
- .init = s3c2440_init,
- .name = name_s3c2440a
- },
- .....................
- };
- void __init s3c244x_init_clocks(int xtal)
- {
- /* initialise the clocks here, to allow other things like the
- * console to use them, and to add new ones after the initialisation
- */
- s3c24xx_register_baseclocks(xtal);
- s3c244x_setup_clocks();
- s3c2410_baseclk_add();
- }
- int __init s3c24xx_register_baseclocks(unsigned long xtal)
- {
- printk(KERN_INFO "S3C24XX Clocks, (c) 2004 Simtec Electronics/n");
- clk_xtal.rate = xtal;
- /* register our clocks */
- if (s3c24xx_register_clock(&clk_xtal) < 0)
- printk(KERN_ERR "failed to register master xtal/n");
- if (s3c24xx_register_clock(&clk_mpll) < 0)
- printk(KERN_ERR "failed to register mpll clock/n");
- if (s3c24xx_register_clock(&clk_upll) < 0)
- printk(KERN_ERR "failed to register upll clock/n");
- if (s3c24xx_register_clock(&clk_f) < 0)
- printk(KERN_ERR "failed to register cpu fclk/n");
- if (s3c24xx_register_clock(&clk_h) < 0)
- printk(KERN_ERR "failed to register cpu hclk/n");
- if (s3c24xx_register_clock(&clk_p) < 0)
- printk(KERN_ERR "failed to register cpu pclk/n");
- return 0;
- }
static LIST_HEAD(clocks);
这个是链表的头的注册函数。注册的struct clk结构体都要添加到这个clocks链表中。
- int s3c24xx_register_clock(struct clk *clk)
- {
- .........
- list_add(&clk->list, &clocks);
- .........
- }
- void __init_or_cpufreq s3c244x_setup_clocks(void)
- {
- .................
- s3c24xx_setup_clocks(fclk, hclk, pclk);
- }
- /* initalise all the clocks */
- void __init_or_cpufreq s3c24xx_setup_clocks(unsigned long fclk,
- unsigned long hclk,
- unsigned long pclk)
- {
- clk_upll.rate = s3c24xx_get_pll(__raw_readl(S3C2410_UPLLCON),
- clk_xtal.rate);
- clk_mpll.rate = fclk;
- clk_h.rate = hclk;
- clk_p.rate = pclk;
- clk_f.rate = fclk;
- }
下面来看第三个函数,这个主要就是对外设的struct clk进行注册。这个函数一共分两部分,有两个数组,一个是init_clocks,也就是在boot时需要提供时钟的,一个是init_clocks_disable,这里的每个成员都是在boot的时候需要disable时钟的。这两个数组分别进行注册,但是注册init_clocks_disable数组中成员的for循环中调用了s3c2410_clkcon_enable(clkp, 0);也就是将相应的clk disable掉。
- int __init s3c2410_baseclk_add(void)
- {
- unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
- unsigned long clkcon = __raw_readl(S3C2410_CLKCON);
- struct clk *clkp;
- struct clk *xtal;
- int ret;
- int ptr;
- clk_upll.enable = s3c2410_upll_enable;
- if (s3c24xx_register_clock(&clk_usb_bus) < 0)
- printk(KERN_ERR "failed to register usb bus clock/n");
- /* register clocks from clock array */
- clkp = init_clocks;
- for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
- /* ensure that we note the clock state */
- clkp->usage = clkcon & clkp->ctrlbit ? 1 : 0;
- ret = s3c24xx_register_clock(clkp);
- if (ret < 0) {
- printk(KERN_ERR "Failed to register clock %s (%d)/n",
- clkp->name, ret);
- }
- }
- /* We must be careful disabling the clocks we are not intending to
- * be using at boot time, as subsystems such as the LCD which do
- * their own DMA requests to the bus can cause the system to lockup
- * if they where in the middle of requesting bus access.
- *
- * Disabling the LCD clock if the LCD is active is very dangerous,
- * and therefore the bootloader should be careful to not enable
- * the LCD clock if it is not needed.
- */
- /* install (and disable) the clocks we do not need immediately */
- clkp = init_clocks_disable;
- for (ptr = 0; ptr < ARRAY_SIZE(init_clocks_disable); ptr++, clkp++) {
- ret = s3c24xx_register_clock(clkp);
- if (ret < 0) {
- printk(KERN_ERR "Failed to register clock %s (%d)/n",
- clkp->name, ret);
- }
- s3c2410_clkcon_enable(clkp, 0);
- }
- /* show the clock-slow value */
- xtal = clk_get(NULL, "xtal");
- printk("CLOCK: Slow mode (%ld.%ld MHz), %s, MPLL %s, UPLL %s/n",
- print_mhz(clk_get_rate(xtal) /
- ( 2 * S3C2410_CLKSLOW_GET_SLOWVAL(clkslow))),
- (clkslow & S3C2410_CLKSLOW_SLOW) ? "slow" : "fast",
- (clkslow & S3C2410_CLKSLOW_MPLL_OFF) ? "off" : "on",
- (clkslow & S3C2410_CLKSLOW_UCLK_OFF) ? "off" : "on");
- s3c_pwmclk_init();
- return 0;
- }
- /*则加模块的引用计数*/
- struct clk *clk_get(struct device *dev, const char *id);
- /*减少模块的引用计数*/
- void clk_put(struct clk *clk);
- /*使能某个模块的时钟,比如ADC模块等*/
- int clk_enable(struct clk *clk);
- /*禁止模块的时钟*/
- void clk_disable(struct clk *clk);
- /*获得某类时钟频率*/
- unsigned long clk_get_rate(struct clk *clk);
- /*设置某类部件的时钟(比如设置CAM接口时钟)*/
- int clk_set_rate(struct clk *clk, unsigned long rate);
- /*获得父clk*/
- struct clk *clk_get_parent(struct clk *clk);
- /*设置父clk*/
- int clk_set_parent(struct clk *clk, struct clk *parent);