时钟相关基本知识可先参考这篇博文:http://blog.csdn.net/yyplc/article/details/7085034
下面直接从总线时钟初始化函数开始分析:
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(); }
1.初始化s3c2440的总线时钟。通过s3c24xx_register_baseclocks(xtal)和s3c244x_setup_clocks实现
2.想系统注册外设时钟。通过s3c2410_baseclk_add()实现
首先分析初始化系统时钟
int __init s3c24xx_register_baseclocks(unsigned long xtal) { printk(KERN_INFO "S3C24XX Clocks, Copyright 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; }依次向注册clk_xtal、clk_mpll、clk_upll... ...clk_p等时钟,clk_p等的定义如下:
struct clk clk_p = { .name = "pclk", .id = -1, .rate = 0, .parent = NULL, .ctrlbit = 0, .ops = &clk_ops_def_setrate, };注册成功后然后通过s3c244x_setup_clocks->s3c24xx_setup_clocks(fclk, hclk, pclk)来初始化前面注册的
各个时钟。
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; }可以发现此处已经将clk_p.rate修改为pclk了,默认值为0。
调用函数s3c2410_baseclk_add()注册外设时钟的过程和
注册总线时钟的过程类似,也是将各个定义好的外设时钟如:
struct clk init_clocks_off[] ={ { ... ... },{ .name = "i2c", .id = -1, .parent = &clk_p, .enable = s3c2410_clkcon_enable, .ctrlbit = S3C2410_CLKCON_IIC, },{ ... ... } }
下面分析i2c部分有关时钟的初始化设置
i2c中设置时钟相关的部分通过函数s3c24xx_i2c_probe->s3c24xx_i2c_init->s3c24xx_i2c_clockrate来实现
static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) { struct s3c2410_platform_i2c *pdata = i2c->dev->platform_data; unsigned long clkin = clk_get_rate(i2c->clk); unsigned int divs, div1; unsigned long target_frequency; u32 iiccon; int freq; i2c->clkrate = clkin; clkin /= 1000; /* clkin now in KHz */ dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency); printk("clkin = %d\n", clkin); target_frequency = pdata->frequency ? pdata->frequency : 100000; target_frequency /= 1000; /* Target frequency now in KHz */ freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs); ... ... }
unsigned long clk_get_rate(struct clk *clk) { if (IS_ERR(clk)) return 0; if (clk->rate != 0) return clk->rate; if (clk->ops != NULL && clk->ops->get_rate != NULL) return (clk->ops->get_rate)(clk); if (clk->parent != NULL) return clk_get_rate(clk->parent); return clk->rate; }
而,之前clk_p时钟在之前的初始化中已近被设置为pclk,默认情况下pclk即外设时钟频率为50M。
所以此处clkin数值为50000000。