1 /*
2  * arch/sh/kernel/cpu/sh4a/clock-sh7763.c
3  *
4  * SH7763 support for the clock framework
5  *
6  *  Copyright (C) 2005  Paul Mundt
7  *  Copyright (C) 2007  Yoshihiro Shimoda
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file "COPYING" in the main directory of this archive
11  * for more details.
12  */
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/io.h>
16 #include <linux/clkdev.h>
17 #include <asm/clock.h>
18 #include <asm/freq.h>
19 #include <asm/io.h>
20 
21 static int bfc_divisors[] = { 1, 1, 1, 8, 1, 1, 1, 1 };
22 static int p0fc_divisors[] = { 1, 1, 1, 8, 1, 1, 1, 1 };
23 static int cfc_divisors[] = { 1, 1, 4, 1, 1, 1, 1, 1 };
24 
25 static void master_clk_init(struct clk *clk)
26 {
27 	clk->rate *= p0fc_divisors[(__raw_readl(FRQCR) >> 4) & 0x07];
28 }
29 
30 static struct clk_ops sh7763_master_clk_ops = {
31 	.init		= master_clk_init,
32 };
33 
34 static unsigned long module_clk_recalc(struct clk *clk)
35 {
36 	int idx = ((__raw_readl(FRQCR) >> 4) & 0x07);
37 	return clk->parent->rate / p0fc_divisors[idx];
38 }
39 
40 static struct clk_ops sh7763_module_clk_ops = {
41 	.recalc		= module_clk_recalc,
42 };
43 
44 static unsigned long bus_clk_recalc(struct clk *clk)
45 {
46 	int idx = ((__raw_readl(FRQCR) >> 16) & 0x07);
47 	return clk->parent->rate / bfc_divisors[idx];
48 }
49 
50 static struct clk_ops sh7763_bus_clk_ops = {
51 	.recalc		= bus_clk_recalc,
52 };
53 
54 static struct clk_ops sh7763_cpu_clk_ops = {
55 	.recalc		= followparent_recalc,
56 };
57 
58 static struct clk_ops *sh7763_clk_ops[] = {
59 	&sh7763_master_clk_ops,
60 	&sh7763_module_clk_ops,
61 	&sh7763_bus_clk_ops,
62 	&sh7763_cpu_clk_ops,
63 };
64 
65 void __init arch_init_clk_ops(struct clk_ops **ops, int idx)
66 {
67 	if (idx < ARRAY_SIZE(sh7763_clk_ops))
68 		*ops = sh7763_clk_ops[idx];
69 }
70 
71 static unsigned long shyway_clk_recalc(struct clk *clk)
72 {
73 	int idx = ((__raw_readl(FRQCR) >> 20) & 0x07);
74 	return clk->parent->rate / cfc_divisors[idx];
75 }
76 
77 static struct clk_ops sh7763_shyway_clk_ops = {
78 	.recalc		= shyway_clk_recalc,
79 };
80 
81 static struct clk sh7763_shyway_clk = {
82 	.flags		= CLK_ENABLE_ON_INIT,
83 	.ops		= &sh7763_shyway_clk_ops,
84 };
85 
86 /*
87  * Additional SH7763-specific on-chip clocks that aren't already part of the
88  * clock framework
89  */
90 static struct clk *sh7763_onchip_clocks[] = {
91 	&sh7763_shyway_clk,
92 };
93 
94 #define CLKDEV_CON_ID(_id, _clk) { .con_id = _id, .clk = _clk }
95 
96 static struct clk_lookup lookups[] = {
97 	/* main clocks */
98 	CLKDEV_CON_ID("shyway_clk", &sh7763_shyway_clk),
99 };
100 
101 int __init arch_clk_init(void)
102 {
103 	struct clk *clk;
104 	int i, ret = 0;
105 
106 	cpg_clk_init();
107 
108 	clk = clk_get(NULL, "master_clk");
109 	for (i = 0; i < ARRAY_SIZE(sh7763_onchip_clocks); i++) {
110 		struct clk *clkp = sh7763_onchip_clocks[i];
111 
112 		clkp->parent = clk;
113 		ret |= clk_register(clkp);
114 	}
115 
116 	clk_put(clk);
117 
118 	clkdev_add_table(lookups, ARRAY_SIZE(lookups));
119 
120 	return ret;
121 }
122