blob: e357d55818abaa7ee5d0b04efe7ca0e0506f0914 [file] [log] [blame]
Maxime Bizone7300d02009-08-18 13:23:37 +01001/*
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>
Maxime Bizon04712f32011-11-04 19:09:35 +010013#include <linux/delay.h>
Maxime Bizone7300d02009-08-18 13:23:37 +010014#include <bcm63xx_cpu.h>
15#include <bcm63xx_io.h>
16#include <bcm63xx_regs.h>
Jonas Gorskiba00e2e2012-10-28 12:17:55 +000017#include <bcm63xx_reset.h>
Maxime Bizone7300d02009-08-18 13:23:37 +010018#include <bcm63xx_clk.h>
19
20static DEFINE_MUTEX(clocks_mutex);
21
22
23static void clk_enable_unlocked(struct clk *clk)
24{
25 if (clk->set && (clk->usage++) == 0)
26 clk->set(clk, 1);
27}
28
29static void clk_disable_unlocked(struct clk *clk)
30{
31 if (clk->set && (--clk->usage) == 0)
32 clk->set(clk, 0);
33}
34
35static void bcm_hwclock_set(u32 mask, int enable)
36{
37 u32 reg;
38
39 reg = bcm_perf_readl(PERF_CKCTL_REG);
40 if (enable)
41 reg |= mask;
42 else
43 reg &= ~mask;
44 bcm_perf_writel(reg, PERF_CKCTL_REG);
45}
46
47/*
48 * Ethernet MAC "misc" clock: dma clocks and main clock on 6348
49 */
50static void enet_misc_set(struct clk *clk, int enable)
51{
52 u32 mask;
53
54 if (BCMCPU_IS_6338())
55 mask = CKCTL_6338_ENET_EN;
56 else if (BCMCPU_IS_6345())
57 mask = CKCTL_6345_ENET_EN;
58 else if (BCMCPU_IS_6348())
59 mask = CKCTL_6348_ENET_EN;
60 else
61 /* BCMCPU_IS_6358 */
62 mask = CKCTL_6358_EMUSB_EN;
63 bcm_hwclock_set(mask, enable);
64}
65
66static struct clk clk_enet_misc = {
67 .set = enet_misc_set,
68};
69
70/*
71 * Ethernet MAC clocks: only revelant on 6358, silently enable misc
72 * clocks
73 */
74static void enetx_set(struct clk *clk, int enable)
75{
76 if (enable)
77 clk_enable_unlocked(&clk_enet_misc);
78 else
79 clk_disable_unlocked(&clk_enet_misc);
80
81 if (BCMCPU_IS_6358()) {
82 u32 mask;
83
84 if (clk->id == 0)
85 mask = CKCTL_6358_ENET0_EN;
86 else
87 mask = CKCTL_6358_ENET1_EN;
88 bcm_hwclock_set(mask, enable);
89 }
90}
91
92static struct clk clk_enet0 = {
93 .id = 0,
94 .set = enetx_set,
95};
96
97static struct clk clk_enet1 = {
98 .id = 1,
99 .set = enetx_set,
100};
101
102/*
103 * Ethernet PHY clock
104 */
105static void ephy_set(struct clk *clk, int enable)
106{
107 if (!BCMCPU_IS_6358())
108 return;
109 bcm_hwclock_set(CKCTL_6358_EPHY_EN, enable);
110}
111
112
113static struct clk clk_ephy = {
114 .set = ephy_set,
115};
116
117/*
Maxime Bizon04712f32011-11-04 19:09:35 +0100118 * Ethernet switch clock
119 */
120static void enetsw_set(struct clk *clk, int enable)
121{
122 if (!BCMCPU_IS_6368())
123 return;
Florian Fainellid9831a42012-07-04 16:57:09 +0200124 bcm_hwclock_set(CKCTL_6368_ROBOSW_EN |
Maxime Bizon04712f32011-11-04 19:09:35 +0100125 CKCTL_6368_SWPKT_USB_EN |
126 CKCTL_6368_SWPKT_SAR_EN, enable);
127 if (enable) {
Maxime Bizon04712f32011-11-04 19:09:35 +0100128 /* reset switch core afer clock change */
Jonas Gorskiba00e2e2012-10-28 12:17:55 +0000129 bcm63xx_core_set_reset(BCM63XX_RESET_ENETSW, 1);
Maxime Bizon04712f32011-11-04 19:09:35 +0100130 msleep(10);
Jonas Gorskiba00e2e2012-10-28 12:17:55 +0000131 bcm63xx_core_set_reset(BCM63XX_RESET_ENETSW, 0);
Maxime Bizon04712f32011-11-04 19:09:35 +0100132 msleep(10);
133 }
134}
135
136static struct clk clk_enetsw = {
137 .set = enetsw_set,
138};
139
140/*
Maxime Bizone7300d02009-08-18 13:23:37 +0100141 * PCM clock
142 */
143static void pcm_set(struct clk *clk, int enable)
144{
145 if (!BCMCPU_IS_6358())
146 return;
147 bcm_hwclock_set(CKCTL_6358_PCM_EN, enable);
148}
149
150static struct clk clk_pcm = {
151 .set = pcm_set,
152};
153
154/*
155 * USB host clock
156 */
157static void usbh_set(struct clk *clk, int enable)
158{
Kevin Cernekeedd89d602012-06-23 04:14:51 +0000159 if (BCMCPU_IS_6328())
160 bcm_hwclock_set(CKCTL_6328_USBH_EN, enable);
161 else if (BCMCPU_IS_6348())
Maxime Bizon04712f32011-11-04 19:09:35 +0100162 bcm_hwclock_set(CKCTL_6348_USBH_EN, enable);
163 else if (BCMCPU_IS_6368())
Florian Fainellid9831a42012-07-04 16:57:09 +0200164 bcm_hwclock_set(CKCTL_6368_USBH_EN, enable);
Maxime Bizone7300d02009-08-18 13:23:37 +0100165}
166
167static struct clk clk_usbh = {
168 .set = usbh_set,
169};
170
171/*
Kevin Cernekeedd89d602012-06-23 04:14:51 +0000172 * USB device clock
173 */
174static void usbd_set(struct clk *clk, int enable)
175{
176 if (BCMCPU_IS_6328())
177 bcm_hwclock_set(CKCTL_6328_USBD_EN, enable);
178 else if (BCMCPU_IS_6368())
179 bcm_hwclock_set(CKCTL_6368_USBD_EN, enable);
180}
181
182static struct clk clk_usbd = {
183 .set = usbd_set,
184};
185
186/*
Maxime Bizone7300d02009-08-18 13:23:37 +0100187 * SPI clock
188 */
189static void spi_set(struct clk *clk, int enable)
190{
191 u32 mask;
192
193 if (BCMCPU_IS_6338())
194 mask = CKCTL_6338_SPI_EN;
195 else if (BCMCPU_IS_6348())
196 mask = CKCTL_6348_SPI_EN;
Florian Fainelli19372b22012-07-04 16:58:30 +0200197 else if (BCMCPU_IS_6358())
Maxime Bizone7300d02009-08-18 13:23:37 +0100198 mask = CKCTL_6358_SPI_EN;
Jonas Gorski08a41d12013-03-21 14:03:18 +0000199 else if (BCMCPU_IS_6362())
200 mask = CKCTL_6362_SPI_EN;
Florian Fainelli19372b22012-07-04 16:58:30 +0200201 else
202 /* BCMCPU_IS_6368 */
203 mask = CKCTL_6368_SPI_EN;
Maxime Bizone7300d02009-08-18 13:23:37 +0100204 bcm_hwclock_set(mask, enable);
205}
206
207static struct clk clk_spi = {
208 .set = spi_set,
209};
210
211/*
Maxime Bizon04712f32011-11-04 19:09:35 +0100212 * XTM clock
213 */
214static void xtm_set(struct clk *clk, int enable)
215{
216 if (!BCMCPU_IS_6368())
217 return;
218
Florian Fainellid9831a42012-07-04 16:57:09 +0200219 bcm_hwclock_set(CKCTL_6368_SAR_EN |
Maxime Bizon04712f32011-11-04 19:09:35 +0100220 CKCTL_6368_SWPKT_SAR_EN, enable);
221
222 if (enable) {
Maxime Bizon04712f32011-11-04 19:09:35 +0100223 /* reset sar core afer clock change */
Jonas Gorskiba00e2e2012-10-28 12:17:55 +0000224 bcm63xx_core_set_reset(BCM63XX_RESET_SAR, 1);
Maxime Bizon04712f32011-11-04 19:09:35 +0100225 mdelay(1);
Jonas Gorskiba00e2e2012-10-28 12:17:55 +0000226 bcm63xx_core_set_reset(BCM63XX_RESET_SAR, 0);
Maxime Bizon04712f32011-11-04 19:09:35 +0100227 mdelay(1);
228 }
229}
230
231
232static struct clk clk_xtm = {
233 .set = xtm_set,
234};
235
236/*
Florian Fainelli0b555612012-07-24 16:33:09 +0200237 * IPsec clock
238 */
239static void ipsec_set(struct clk *clk, int enable)
240{
241 bcm_hwclock_set(CKCTL_6368_IPSEC_EN, enable);
242}
243
244static struct clk clk_ipsec = {
245 .set = ipsec_set,
246};
247
248/*
Jonas Gorskif2d10352012-10-28 11:49:53 +0000249 * PCIe clock
250 */
251
252static void pcie_set(struct clk *clk, int enable)
253{
254 bcm_hwclock_set(CKCTL_6328_PCIE_EN, enable);
255}
256
257static struct clk clk_pcie = {
258 .set = pcie_set,
259};
260
261/*
Maxime Bizone7300d02009-08-18 13:23:37 +0100262 * Internal peripheral clock
263 */
264static struct clk clk_periph = {
265 .rate = (50 * 1000 * 1000),
266};
267
268
269/*
270 * Linux clock API implementation
271 */
272int clk_enable(struct clk *clk)
273{
274 mutex_lock(&clocks_mutex);
275 clk_enable_unlocked(clk);
276 mutex_unlock(&clocks_mutex);
277 return 0;
278}
279
280EXPORT_SYMBOL(clk_enable);
281
282void clk_disable(struct clk *clk)
283{
284 mutex_lock(&clocks_mutex);
285 clk_disable_unlocked(clk);
286 mutex_unlock(&clocks_mutex);
287}
288
289EXPORT_SYMBOL(clk_disable);
290
291unsigned long clk_get_rate(struct clk *clk)
292{
293 return clk->rate;
294}
295
296EXPORT_SYMBOL(clk_get_rate);
297
298struct clk *clk_get(struct device *dev, const char *id)
299{
300 if (!strcmp(id, "enet0"))
301 return &clk_enet0;
302 if (!strcmp(id, "enet1"))
303 return &clk_enet1;
Maxime Bizon04712f32011-11-04 19:09:35 +0100304 if (!strcmp(id, "enetsw"))
305 return &clk_enetsw;
Maxime Bizone7300d02009-08-18 13:23:37 +0100306 if (!strcmp(id, "ephy"))
307 return &clk_ephy;
308 if (!strcmp(id, "usbh"))
309 return &clk_usbh;
Kevin Cernekeedd89d602012-06-23 04:14:51 +0000310 if (!strcmp(id, "usbd"))
311 return &clk_usbd;
Maxime Bizone7300d02009-08-18 13:23:37 +0100312 if (!strcmp(id, "spi"))
313 return &clk_spi;
Maxime Bizon04712f32011-11-04 19:09:35 +0100314 if (!strcmp(id, "xtm"))
315 return &clk_xtm;
Maxime Bizone7300d02009-08-18 13:23:37 +0100316 if (!strcmp(id, "periph"))
317 return &clk_periph;
318 if (BCMCPU_IS_6358() && !strcmp(id, "pcm"))
319 return &clk_pcm;
Florian Fainelli0b555612012-07-24 16:33:09 +0200320 if (BCMCPU_IS_6368() && !strcmp(id, "ipsec"))
321 return &clk_ipsec;
Jonas Gorskif2d10352012-10-28 11:49:53 +0000322 if (BCMCPU_IS_6328() && !strcmp(id, "pcie"))
323 return &clk_pcie;
Maxime Bizone7300d02009-08-18 13:23:37 +0100324 return ERR_PTR(-ENOENT);
325}
326
327EXPORT_SYMBOL(clk_get);
328
329void clk_put(struct clk *clk)
330{
331}
332
333EXPORT_SYMBOL(clk_put);