Merge tag 'clk-for-linus' of git://git.linaro.org/people/mturquette/linux
[~shefty/rdma-dev.git] / drivers / clk / versatile / clk-vexpress.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * Copyright (C) 2012 ARM Limited
12  */
13
14 #include <linux/clkdev.h>
15 #include <linux/clk-provider.h>
16 #include <linux/err.h>
17 #include <linux/of.h>
18 #include <linux/of_address.h>
19 #include <linux/vexpress.h>
20
21 #include <asm/hardware/sp810.h>
22
23 static struct clk *vexpress_sp810_timerclken[4];
24 static DEFINE_SPINLOCK(vexpress_sp810_lock);
25
26 static void __init vexpress_sp810_init(void __iomem *base)
27 {
28         int i;
29
30         if (WARN_ON(!base))
31                 return;
32
33         for (i = 0; i < ARRAY_SIZE(vexpress_sp810_timerclken); i++) {
34                 char name[12];
35                 const char *parents[] = {
36                         "v2m:refclk32khz", /* REFCLK */
37                         "v2m:refclk1mhz" /* TIMCLK */
38                 };
39
40                 snprintf(name, ARRAY_SIZE(name), "timerclken%d", i);
41
42                 vexpress_sp810_timerclken[i] = clk_register_mux(NULL, name,
43                                 parents, 2, 0, base + SCCTRL,
44                                 SCCTRL_TIMERENnSEL_SHIFT(i), 1,
45                                 0, &vexpress_sp810_lock);
46
47                 if (WARN_ON(IS_ERR(vexpress_sp810_timerclken[i])))
48                         break;
49         }
50 }
51
52
53 static const char * const vexpress_clk_24mhz_periphs[] __initconst = {
54         "mb:uart0", "mb:uart1", "mb:uart2", "mb:uart3",
55         "mb:mmci", "mb:kmi0", "mb:kmi1"
56 };
57
58 void __init vexpress_clk_init(void __iomem *sp810_base)
59 {
60         struct clk *clk;
61         int i;
62
63         clk = clk_register_fixed_rate(NULL, "dummy_apb_pclk", NULL,
64                         CLK_IS_ROOT, 0);
65         WARN_ON(clk_register_clkdev(clk, "apb_pclk", NULL));
66
67         clk = clk_register_fixed_rate(NULL, "v2m:clk_24mhz", NULL,
68                         CLK_IS_ROOT, 24000000);
69         for (i = 0; i < ARRAY_SIZE(vexpress_clk_24mhz_periphs); i++)
70                 WARN_ON(clk_register_clkdev(clk, NULL,
71                                 vexpress_clk_24mhz_periphs[i]));
72
73         clk = clk_register_fixed_rate(NULL, "v2m:refclk32khz", NULL,
74                         CLK_IS_ROOT, 32768);
75         WARN_ON(clk_register_clkdev(clk, NULL, "v2m:wdt"));
76
77         clk = clk_register_fixed_rate(NULL, "v2m:refclk1mhz", NULL,
78                         CLK_IS_ROOT, 1000000);
79
80         vexpress_sp810_init(sp810_base);
81
82         for (i = 0; i < ARRAY_SIZE(vexpress_sp810_timerclken); i++)
83                 WARN_ON(clk_set_parent(vexpress_sp810_timerclken[i], clk));
84
85         WARN_ON(clk_register_clkdev(vexpress_sp810_timerclken[0],
86                                 "v2m-timer0", "sp804"));
87         WARN_ON(clk_register_clkdev(vexpress_sp810_timerclken[1],
88                                 "v2m-timer1", "sp804"));
89 }
90
91 #if defined(CONFIG_OF)
92
93 struct clk *vexpress_sp810_of_get(struct of_phandle_args *clkspec, void *data)
94 {
95         if (WARN_ON(clkspec->args_count != 1 || clkspec->args[0] >
96                         ARRAY_SIZE(vexpress_sp810_timerclken)))
97                 return NULL;
98
99         return vexpress_sp810_timerclken[clkspec->args[0]];
100 }
101
102 void __init vexpress_clk_of_init(void)
103 {
104         struct device_node *node;
105         struct clk *clk;
106         struct clk *refclk, *timclk;
107
108         of_clk_init(NULL);
109
110         node = of_find_compatible_node(NULL, NULL, "arm,sp810");
111         vexpress_sp810_init(of_iomap(node, 0));
112         of_clk_add_provider(node, vexpress_sp810_of_get, NULL);
113
114         /* Select "better" (faster) parent for SP804 timers */
115         refclk = of_clk_get_by_name(node, "refclk");
116         timclk = of_clk_get_by_name(node, "timclk");
117         if (!WARN_ON(IS_ERR(refclk) || IS_ERR(timclk))) {
118                 int i = 0;
119
120                 if (clk_get_rate(refclk) > clk_get_rate(timclk))
121                         clk = refclk;
122                 else
123                         clk = timclk;
124
125                 for (i = 0; i < ARRAY_SIZE(vexpress_sp810_timerclken); i++)
126                         WARN_ON(clk_set_parent(vexpress_sp810_timerclken[i],
127                                         clk));
128         }
129
130         WARN_ON(clk_register_clkdev(vexpress_sp810_timerclken[0],
131                                 "v2m-timer0", "sp804"));
132         WARN_ON(clk_register_clkdev(vexpress_sp810_timerclken[1],
133                                 "v2m-timer1", "sp804"));
134 }
135
136 #endif