[brcm63xx] fixes for the bcm6338 clocks, thanks Maxime
[openwrt.git] / target / linux / brcm63xx / files / arch / mips / bcm63xx / clk.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7  */
8
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/err.h>
12 #include <linux/clk.h>
13 #include <bcm63xx_cpu.h>
14 #include <bcm63xx_io.h>
15 #include <bcm63xx_regs.h>
16 #include <bcm63xx_clk.h>
17
18 DEFINE_MUTEX(clocks_mutex);
19
20
21 static void clk_enable_unlocked(struct clk *clk)
22 {
23         if (clk->set && (clk->usage++) == 0)
24                 clk->set(clk, 1);
25 }
26
27 static void clk_disable_unlocked(struct clk *clk)
28 {
29         if (clk->set && (--clk->usage) == 0)
30                 clk->set(clk, 0);
31 }
32
33 static void bcm_hwclock_set(u32 mask, int enable)
34 {
35         u32 reg;
36
37         reg = bcm_perf_readl(PERF_CKCTL_REG);
38         if (enable)
39                 reg |= mask;
40         else
41                 reg &= ~mask;
42         bcm_perf_writel(reg, PERF_CKCTL_REG);
43 }
44
45 /*
46  * Ethernet MAC "misc" clock: dma clocks and main clock on 6348
47  */
48 static void enet_misc_set(struct clk *clk, int enable)
49 {
50         u32 mask;
51
52         if (BCMCPU_IS_6338())
53                 mask = CKCTL_6338_ENET_EN;
54         else if (BCMCPU_IS_6348())
55                 mask = CKCTL_6348_ENET_EN;
56         else
57                 /* BCMCPU_IS_6358 */
58                 mask = CKCTL_6358_EMUSB_EN;
59         bcm_hwclock_set(mask, enable);
60 }
61
62 static struct clk clk_enet_misc = {
63         .set    = enet_misc_set,
64 };
65
66 /*
67  * Ethernet MAC clocks: only revelant on 6358, silently enable misc
68  * clocks
69  */
70 static void enetx_set(struct clk *clk, int enable)
71 {
72         if (enable)
73                 clk_enable_unlocked(&clk_enet_misc);
74         else
75                 clk_disable_unlocked(&clk_enet_misc);
76
77         if (BCMCPU_IS_6358()) {
78                 u32 mask;
79
80                 if (clk->id == 0)
81                         mask = CKCTL_6358_ENET0_EN;
82                 else
83                         mask = CKCTL_6358_ENET1_EN;
84                 bcm_hwclock_set(mask, enable);
85         }
86 }
87
88 static struct clk clk_enet0 = {
89         .id     = 0,
90         .set    = enetx_set,
91 };
92
93 static struct clk clk_enet1 = {
94         .id     = 1,
95         .set    = enetx_set,
96 };
97
98 /*
99  * Ethernet PHY clock
100  */
101 static void ephy_set(struct clk *clk, int enable)
102 {
103         if (!BCMCPU_IS_6358())
104                 return;
105         bcm_hwclock_set(CKCTL_6358_EPHY_EN, enable);
106 }
107
108
109 static struct clk clk_ephy = {
110         .set    = ephy_set,
111 };
112
113 /*
114  * PCM clock
115  */
116 static void pcm_set(struct clk *clk, int enable)
117 {
118         if (!BCMCPU_IS_6358())
119                 return;
120         bcm_hwclock_set(CKCTL_6358_PCM_EN, enable);
121 }
122
123 static struct clk clk_pcm = {
124         .set    = pcm_set,
125 };
126
127 /*
128  * USB host clock
129  */
130 static void usbh_set(struct clk *clk, int enable)
131 {
132         if (!BCMCPU_IS_6348())
133                 return;
134         bcm_hwclock_set(CKCTL_6348_USBH_EN, enable);
135 }
136
137 static struct clk clk_usbh = {
138         .set    = usbh_set,
139 };
140
141 /*
142  * USB slave clock
143  */
144 static void usbs_set(struct clk *clk, int enable)
145 {
146         u32 mask;
147
148         switch(bcm63xx_get_cpu_id()) {
149         case BCM6338_CPU_ID: mask = CKCTL_6338_USBS_EN; break;
150         case BCM6348_CPU_ID: mask = CKCTL_6348_USBS_EN; break;
151         default:
152                 return;
153         }
154         bcm_hwclock_set(mask, enable);
155 }
156
157 static struct clk clk_usbs = {
158         .set    = usbs_set,
159 };
160
161 /*
162  * SPI clock
163  */
164 static void spi_set(struct clk *clk, int enable)
165 {
166         u32 mask;
167
168         if (BCMCPU_IS_6338())
169                 mask = CKCTL_6338_SPI_EN;
170         else if (BCMCPU_IS_6348())
171                 mask = CKCTL_6348_SPI_EN;
172         else
173                 /* BCMCPU_IS_6358 */
174                 mask = CKCTL_6358_SPI_EN;
175         bcm_hwclock_set(mask, enable);
176 }
177
178 static struct clk clk_spi = {
179         .set    = spi_set,
180 };
181
182 /*
183  * Internal peripheral clock
184  */
185 static struct clk clk_periph = {
186         .rate   = (50 * 1000 * 1000),
187 };
188
189
190 /*
191  * Linux clock API implementation
192  */
193 int clk_enable(struct clk *clk)
194 {
195         mutex_lock(&clocks_mutex);
196         clk_enable_unlocked(clk);
197         mutex_unlock(&clocks_mutex);
198         return 0;
199 }
200
201 EXPORT_SYMBOL(clk_enable);
202
203 void clk_disable(struct clk *clk)
204 {
205         mutex_lock(&clocks_mutex);
206         clk_disable_unlocked(clk);
207         mutex_unlock(&clocks_mutex);
208 }
209
210 EXPORT_SYMBOL(clk_disable);
211
212 unsigned long clk_get_rate(struct clk *clk)
213 {
214         return clk->rate;
215 }
216
217 EXPORT_SYMBOL(clk_get_rate);
218
219 struct clk *clk_get(struct device *dev, const char *id)
220 {
221         if (!strcmp(id, "enet0"))
222                 return &clk_enet0;
223         if (!strcmp(id, "enet1"))
224                 return &clk_enet1;
225         if (!strcmp(id, "ephy"))
226                 return &clk_ephy;
227         if (!strcmp(id, "usbh"))
228                 return &clk_usbh;
229         if (!strcmp(id, "usbs"))
230                 return &clk_usbs;
231         if (!strcmp(id, "spi"))
232                 return &clk_spi;
233         if (!strcmp(id, "periph"))
234                 return &clk_periph;
235         if (BCMCPU_IS_6358() && !strcmp(id, "pcm"))
236                 return &clk_pcm;
237         return ERR_PTR(-ENOENT);
238 }
239
240 EXPORT_SYMBOL(clk_get);
241
242 void clk_put(struct clk *clk)
243 {
244 }
245
246 EXPORT_SYMBOL(clk_put);