1 From dd6ffa07b42d40ff680ce9bce34da88cf8920837 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 17 Jul 2010 11:10:00 +0000
4 Subject: [PATCH] MIPS: JZ4740: Add clock API support.
6 Add support for managing the clocks found on JZ4740 SoC through the
9 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
10 Cc: linux-mips@linux-mips.org
11 Cc: linux-kernel@vger.kernel.org
12 Patchwork: https://patchwork.linux-mips.org/patch/1466/
13 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
15 arch/mips/include/asm/mach-jz4740/clock.h | 28 +
16 arch/mips/jz4740/clock-debugfs.c | 109 ++++
17 arch/mips/jz4740/clock.c | 924 +++++++++++++++++++++++++++++
18 arch/mips/jz4740/clock.h | 76 +++
19 4 files changed, 1137 insertions(+), 0 deletions(-)
20 create mode 100644 arch/mips/include/asm/mach-jz4740/clock.h
21 create mode 100644 arch/mips/jz4740/clock-debugfs.c
22 create mode 100644 arch/mips/jz4740/clock.c
23 create mode 100644 arch/mips/jz4740/clock.h
26 +++ b/arch/mips/include/asm/mach-jz4740/clock.h
29 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
31 + * This program is free software; you can redistribute it and/or modify it
32 + * under the terms of the GNU General Public License as published by the
33 + * Free Software Foundation; either version 2 of the License, or (at your
34 + * option) any later version.
36 + * You should have received a copy of the GNU General Public License along
37 + * with this program; if not, write to the Free Software Foundation, Inc.,
38 + * 675 Mass Ave, Cambridge, MA 02139, USA.
42 +#ifndef __ASM_JZ4740_CLOCK_H__
43 +#define __ASM_JZ4740_CLOCK_H__
45 +enum jz4740_wait_mode {
46 + JZ4740_WAIT_MODE_IDLE,
47 + JZ4740_WAIT_MODE_SLEEP,
50 +void jz4740_clock_set_wait_mode(enum jz4740_wait_mode mode);
52 +void jz4740_clock_udc_enable_auto_suspend(void);
53 +void jz4740_clock_udc_disable_auto_suspend(void);
57 +++ b/arch/mips/jz4740/clock-debugfs.c
60 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
61 + * JZ4740 SoC clock support debugfs entries
63 + * This program is free software; you can redistribute it and/or modify it
64 + * under the terms of the GNU General Public License as published by the
65 + * Free Software Foundation; either version 2 of the License, or (at your
66 + * option) any later version.
68 + * You should have received a copy of the GNU General Public License along
69 + * with this program; if not, write to the Free Software Foundation, Inc.,
70 + * 675 Mass Ave, Cambridge, MA 02139, USA.
74 +#include <linux/kernel.h>
75 +#include <linux/module.h>
76 +#include <linux/clk.h>
77 +#include <linux/err.h>
79 +#include <linux/debugfs.h>
80 +#include <linux/uaccess.h>
82 +#include <asm/mach-jz4740/clock.h>
85 +static struct dentry *jz4740_clock_debugfs;
87 +static int jz4740_clock_debugfs_show_enabled(void *data, uint64_t *value)
89 + struct clk *clk = data;
90 + *value = clk_is_enabled(clk);
95 +static int jz4740_clock_debugfs_set_enabled(void *data, uint64_t value)
97 + struct clk *clk = data;
100 + return clk_enable(clk);
107 +DEFINE_SIMPLE_ATTRIBUTE(jz4740_clock_debugfs_ops_enabled,
108 + jz4740_clock_debugfs_show_enabled,
109 + jz4740_clock_debugfs_set_enabled,
112 +static int jz4740_clock_debugfs_show_rate(void *data, uint64_t *value)
114 + struct clk *clk = data;
115 + *value = clk_get_rate(clk);
120 +DEFINE_SIMPLE_ATTRIBUTE(jz4740_clock_debugfs_ops_rate,
121 + jz4740_clock_debugfs_show_rate,
125 +void jz4740_clock_debugfs_add_clk(struct clk *clk)
127 + if (!jz4740_clock_debugfs)
130 + clk->debugfs_entry = debugfs_create_dir(clk->name, jz4740_clock_debugfs);
131 + debugfs_create_file("rate", S_IWUGO | S_IRUGO, clk->debugfs_entry, clk,
132 + &jz4740_clock_debugfs_ops_rate);
133 + debugfs_create_file("enabled", S_IRUGO, clk->debugfs_entry, clk,
134 + &jz4740_clock_debugfs_ops_enabled);
137 + char parent_path[100];
138 + snprintf(parent_path, 100, "../%s", clk->parent->name);
139 + clk->debugfs_parent_entry = debugfs_create_symlink("parent",
140 + clk->debugfs_entry,
146 +void jz4740_clock_debugfs_update_parent(struct clk *clk)
148 + if (clk->debugfs_parent_entry)
149 + debugfs_remove(clk->debugfs_parent_entry);
152 + char parent_path[100];
153 + snprintf(parent_path, 100, "../%s", clk->parent->name);
154 + clk->debugfs_parent_entry = debugfs_create_symlink("parent",
155 + clk->debugfs_entry,
158 + clk->debugfs_parent_entry = NULL;
162 +void jz4740_clock_debugfs_init(void)
164 + jz4740_clock_debugfs = debugfs_create_dir("jz4740-clock", NULL);
165 + if (IS_ERR(jz4740_clock_debugfs))
166 + jz4740_clock_debugfs = NULL;
169 +++ b/arch/mips/jz4740/clock.c
172 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
173 + * JZ4740 SoC clock support
175 + * This program is free software; you can redistribute it and/or modify it
176 + * under the terms of the GNU General Public License as published by the
177 + * Free Software Foundation; either version 2 of the License, or (at your
178 + * option) any later version.
180 + * You should have received a copy of the GNU General Public License along
181 + * with this program; if not, write to the Free Software Foundation, Inc.,
182 + * 675 Mass Ave, Cambridge, MA 02139, USA.
186 +#include <linux/kernel.h>
187 +#include <linux/errno.h>
188 +#include <linux/clk.h>
189 +#include <linux/spinlock.h>
190 +#include <linux/io.h>
191 +#include <linux/module.h>
192 +#include <linux/list.h>
193 +#include <linux/err.h>
195 +#include <asm/mach-jz4740/clock.h>
196 +#include <asm/mach-jz4740/base.h>
200 +#define JZ_REG_CLOCK_CTRL 0x00
201 +#define JZ_REG_CLOCK_LOW_POWER 0x04
202 +#define JZ_REG_CLOCK_PLL 0x10
203 +#define JZ_REG_CLOCK_GATE 0x20
204 +#define JZ_REG_CLOCK_SLEEP_CTRL 0x24
205 +#define JZ_REG_CLOCK_I2S 0x60
206 +#define JZ_REG_CLOCK_LCD 0x64
207 +#define JZ_REG_CLOCK_MMC 0x68
208 +#define JZ_REG_CLOCK_UHC 0x6C
209 +#define JZ_REG_CLOCK_SPI 0x74
211 +#define JZ_CLOCK_CTRL_I2S_SRC_PLL BIT(31)
212 +#define JZ_CLOCK_CTRL_KO_ENABLE BIT(30)
213 +#define JZ_CLOCK_CTRL_UDC_SRC_PLL BIT(29)
214 +#define JZ_CLOCK_CTRL_UDIV_MASK 0x1f800000
215 +#define JZ_CLOCK_CTRL_CHANGE_ENABLE BIT(22)
216 +#define JZ_CLOCK_CTRL_PLL_HALF BIT(21)
217 +#define JZ_CLOCK_CTRL_LDIV_MASK 0x001f0000
218 +#define JZ_CLOCK_CTRL_UDIV_OFFSET 23
219 +#define JZ_CLOCK_CTRL_LDIV_OFFSET 16
220 +#define JZ_CLOCK_CTRL_MDIV_OFFSET 12
221 +#define JZ_CLOCK_CTRL_PDIV_OFFSET 8
222 +#define JZ_CLOCK_CTRL_HDIV_OFFSET 4
223 +#define JZ_CLOCK_CTRL_CDIV_OFFSET 0
225 +#define JZ_CLOCK_GATE_UART0 BIT(0)
226 +#define JZ_CLOCK_GATE_TCU BIT(1)
227 +#define JZ_CLOCK_GATE_RTC BIT(2)
228 +#define JZ_CLOCK_GATE_I2C BIT(3)
229 +#define JZ_CLOCK_GATE_SPI BIT(4)
230 +#define JZ_CLOCK_GATE_AIC BIT(5)
231 +#define JZ_CLOCK_GATE_I2S BIT(6)
232 +#define JZ_CLOCK_GATE_MMC BIT(7)
233 +#define JZ_CLOCK_GATE_ADC BIT(8)
234 +#define JZ_CLOCK_GATE_CIM BIT(9)
235 +#define JZ_CLOCK_GATE_LCD BIT(10)
236 +#define JZ_CLOCK_GATE_UDC BIT(11)
237 +#define JZ_CLOCK_GATE_DMAC BIT(12)
238 +#define JZ_CLOCK_GATE_IPU BIT(13)
239 +#define JZ_CLOCK_GATE_UHC BIT(14)
240 +#define JZ_CLOCK_GATE_UART1 BIT(15)
242 +#define JZ_CLOCK_I2S_DIV_MASK 0x01ff
244 +#define JZ_CLOCK_LCD_DIV_MASK 0x01ff
246 +#define JZ_CLOCK_MMC_DIV_MASK 0x001f
248 +#define JZ_CLOCK_UHC_DIV_MASK 0x000f
250 +#define JZ_CLOCK_SPI_SRC_PLL BIT(31)
251 +#define JZ_CLOCK_SPI_DIV_MASK 0x000f
253 +#define JZ_CLOCK_PLL_M_MASK 0x01ff
254 +#define JZ_CLOCK_PLL_N_MASK 0x001f
255 +#define JZ_CLOCK_PLL_OD_MASK 0x0003
256 +#define JZ_CLOCK_PLL_STABLE BIT(10)
257 +#define JZ_CLOCK_PLL_BYPASS BIT(9)
258 +#define JZ_CLOCK_PLL_ENABLED BIT(8)
259 +#define JZ_CLOCK_PLL_STABLIZE_MASK 0x000f
260 +#define JZ_CLOCK_PLL_M_OFFSET 23
261 +#define JZ_CLOCK_PLL_N_OFFSET 18
262 +#define JZ_CLOCK_PLL_OD_OFFSET 16
264 +#define JZ_CLOCK_LOW_POWER_MODE_DOZE BIT(2)
265 +#define JZ_CLOCK_LOW_POWER_MODE_SLEEP BIT(0)
267 +#define JZ_CLOCK_SLEEP_CTRL_SUSPEND_UHC BIT(7)
268 +#define JZ_CLOCK_SLEEP_CTRL_ENABLE_UDC BIT(6)
270 +static void __iomem *jz_clock_base;
271 +static spinlock_t jz_clock_lock;
272 +static LIST_HEAD(jz_clocks);
276 + uint32_t div_offset;
279 +struct divided_clk {
287 + unsigned long rate;
290 +static uint32_t jz_clk_reg_read(int reg)
292 + return readl(jz_clock_base + reg);
295 +static void jz_clk_reg_write_mask(int reg, uint32_t val, uint32_t mask)
299 + spin_lock(&jz_clock_lock);
300 + val2 = readl(jz_clock_base + reg);
303 + writel(val2, jz_clock_base + reg);
304 + spin_unlock(&jz_clock_lock);
307 +static void jz_clk_reg_set_bits(int reg, uint32_t mask)
311 + spin_lock(&jz_clock_lock);
312 + val = readl(jz_clock_base + reg);
314 + writel(val, jz_clock_base + reg);
315 + spin_unlock(&jz_clock_lock);
318 +static void jz_clk_reg_clear_bits(int reg, uint32_t mask)
322 + spin_lock(&jz_clock_lock);
323 + val = readl(jz_clock_base + reg);
325 + writel(val, jz_clock_base + reg);
326 + spin_unlock(&jz_clock_lock);
329 +static int jz_clk_enable_gating(struct clk *clk)
331 + if (clk->gate_bit == JZ4740_CLK_NOT_GATED)
334 + jz_clk_reg_clear_bits(JZ_REG_CLOCK_GATE, clk->gate_bit);
338 +static int jz_clk_disable_gating(struct clk *clk)
340 + if (clk->gate_bit == JZ4740_CLK_NOT_GATED)
343 + jz_clk_reg_set_bits(JZ_REG_CLOCK_GATE, clk->gate_bit);
347 +static int jz_clk_is_enabled_gating(struct clk *clk)
349 + if (clk->gate_bit == JZ4740_CLK_NOT_GATED)
352 + return !(jz_clk_reg_read(JZ_REG_CLOCK_GATE) & clk->gate_bit);
355 +static unsigned long jz_clk_static_get_rate(struct clk *clk)
357 + return ((struct static_clk *)clk)->rate;
360 +static int jz_clk_ko_enable(struct clk *clk)
362 + jz_clk_reg_set_bits(JZ_REG_CLOCK_CTRL, JZ_CLOCK_CTRL_KO_ENABLE);
366 +static int jz_clk_ko_disable(struct clk *clk)
368 + jz_clk_reg_clear_bits(JZ_REG_CLOCK_CTRL, JZ_CLOCK_CTRL_KO_ENABLE);
372 +static int jz_clk_ko_is_enabled(struct clk *clk)
374 + return !!(jz_clk_reg_read(JZ_REG_CLOCK_CTRL) & JZ_CLOCK_CTRL_KO_ENABLE);
377 +static const int pllno[] = {1, 2, 2, 4};
379 +static unsigned long jz_clk_pll_get_rate(struct clk *clk)
386 + val = jz_clk_reg_read(JZ_REG_CLOCK_PLL);
388 + if (val & JZ_CLOCK_PLL_BYPASS)
389 + return clk_get_rate(clk->parent);
391 + m = ((val >> 23) & 0x1ff) + 2;
392 + n = ((val >> 18) & 0x1f) + 2;
393 + od = (val >> 16) & 0x3;
395 + return ((clk_get_rate(clk->parent) / n) * m) / pllno[od];
398 +static unsigned long jz_clk_pll_half_get_rate(struct clk *clk)
402 + reg = jz_clk_reg_read(JZ_REG_CLOCK_CTRL);
403 + if (reg & JZ_CLOCK_CTRL_PLL_HALF)
404 + return jz_clk_pll_get_rate(clk->parent);
405 + return jz_clk_pll_get_rate(clk->parent) >> 1;
408 +static const int jz_clk_main_divs[] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32};
410 +static unsigned long jz_clk_main_round_rate(struct clk *clk, unsigned long rate)
412 + unsigned long parent_rate = jz_clk_pll_get_rate(clk->parent);
415 + div = parent_rate / rate;
417 + return parent_rate / 32;
419 + return parent_rate;
421 + div &= (0x3 << (ffs(div) - 1));
423 + return parent_rate / div;
426 +static unsigned long jz_clk_main_get_rate(struct clk *clk)
428 + struct main_clk *mclk = (struct main_clk *)clk;
431 + div = jz_clk_reg_read(JZ_REG_CLOCK_CTRL);
433 + div >>= mclk->div_offset;
436 + if (div >= ARRAY_SIZE(jz_clk_main_divs))
437 + div = ARRAY_SIZE(jz_clk_main_divs) - 1;
439 + return jz_clk_pll_get_rate(clk->parent) / jz_clk_main_divs[div];
442 +static int jz_clk_main_set_rate(struct clk *clk, unsigned long rate)
444 + struct main_clk *mclk = (struct main_clk *)clk;
447 + unsigned long parent_rate = jz_clk_pll_get_rate(clk->parent);
449 + rate = jz_clk_main_round_rate(clk, rate);
451 + div = parent_rate / rate;
453 + i = (ffs(div) - 1) << 1;
454 + if (i > 0 && !(div & BIT(i-1)))
457 + jz_clk_reg_write_mask(JZ_REG_CLOCK_CTRL, i << mclk->div_offset,
458 + 0xf << mclk->div_offset);
463 +static struct clk_ops jz_clk_static_ops = {
464 + .get_rate = jz_clk_static_get_rate,
465 + .enable = jz_clk_enable_gating,
466 + .disable = jz_clk_disable_gating,
467 + .is_enabled = jz_clk_is_enabled_gating,
470 +static struct static_clk jz_clk_ext = {
473 + .gate_bit = JZ4740_CLK_NOT_GATED,
474 + .ops = &jz_clk_static_ops,
478 +static struct clk_ops jz_clk_pll_ops = {
479 + .get_rate = jz_clk_pll_get_rate,
482 +static struct clk jz_clk_pll = {
484 + .parent = &jz_clk_ext.clk,
485 + .ops = &jz_clk_pll_ops,
488 +static struct clk_ops jz_clk_pll_half_ops = {
489 + .get_rate = jz_clk_pll_half_get_rate,
492 +static struct clk jz_clk_pll_half = {
493 + .name = "pll half",
494 + .parent = &jz_clk_pll,
495 + .ops = &jz_clk_pll_half_ops,
498 +static const struct clk_ops jz_clk_main_ops = {
499 + .get_rate = jz_clk_main_get_rate,
500 + .set_rate = jz_clk_main_set_rate,
501 + .round_rate = jz_clk_main_round_rate,
504 +static struct main_clk jz_clk_cpu = {
507 + .parent = &jz_clk_pll,
508 + .ops = &jz_clk_main_ops,
510 + .div_offset = JZ_CLOCK_CTRL_CDIV_OFFSET,
513 +static struct main_clk jz_clk_memory = {
516 + .parent = &jz_clk_pll,
517 + .ops = &jz_clk_main_ops,
519 + .div_offset = JZ_CLOCK_CTRL_MDIV_OFFSET,
522 +static struct main_clk jz_clk_high_speed_peripheral = {
525 + .parent = &jz_clk_pll,
526 + .ops = &jz_clk_main_ops,
528 + .div_offset = JZ_CLOCK_CTRL_HDIV_OFFSET,
532 +static struct main_clk jz_clk_low_speed_peripheral = {
535 + .parent = &jz_clk_pll,
536 + .ops = &jz_clk_main_ops,
538 + .div_offset = JZ_CLOCK_CTRL_PDIV_OFFSET,
541 +static const struct clk_ops jz_clk_ko_ops = {
542 + .enable = jz_clk_ko_enable,
543 + .disable = jz_clk_ko_disable,
544 + .is_enabled = jz_clk_ko_is_enabled,
547 +static struct clk jz_clk_ko = {
549 + .parent = &jz_clk_memory.clk,
550 + .ops = &jz_clk_ko_ops,
553 +static int jz_clk_spi_set_parent(struct clk *clk, struct clk *parent)
555 + if (parent == &jz_clk_pll)
556 + jz_clk_reg_set_bits(JZ_CLOCK_SPI_SRC_PLL, JZ_REG_CLOCK_SPI);
557 + else if (parent == &jz_clk_ext.clk)
558 + jz_clk_reg_clear_bits(JZ_CLOCK_SPI_SRC_PLL, JZ_REG_CLOCK_SPI);
562 + clk->parent = parent;
567 +static int jz_clk_i2s_set_parent(struct clk *clk, struct clk *parent)
569 + if (parent == &jz_clk_pll_half)
570 + jz_clk_reg_set_bits(JZ_REG_CLOCK_CTRL, JZ_CLOCK_CTRL_I2S_SRC_PLL);
571 + else if (parent == &jz_clk_ext.clk)
572 + jz_clk_reg_clear_bits(JZ_REG_CLOCK_CTRL, JZ_CLOCK_CTRL_I2S_SRC_PLL);
576 + clk->parent = parent;
581 +static int jz_clk_udc_enable(struct clk *clk)
583 + jz_clk_reg_set_bits(JZ_REG_CLOCK_SLEEP_CTRL,
584 + JZ_CLOCK_SLEEP_CTRL_ENABLE_UDC);
589 +static int jz_clk_udc_disable(struct clk *clk)
591 + jz_clk_reg_clear_bits(JZ_REG_CLOCK_SLEEP_CTRL,
592 + JZ_CLOCK_SLEEP_CTRL_ENABLE_UDC);
597 +static int jz_clk_udc_is_enabled(struct clk *clk)
599 + return !!(jz_clk_reg_read(JZ_REG_CLOCK_SLEEP_CTRL) &
600 + JZ_CLOCK_SLEEP_CTRL_ENABLE_UDC);
603 +static int jz_clk_udc_set_parent(struct clk *clk, struct clk *parent)
605 + if (parent == &jz_clk_pll_half)
606 + jz_clk_reg_set_bits(JZ_REG_CLOCK_CTRL, JZ_CLOCK_CTRL_UDC_SRC_PLL);
607 + else if (parent == &jz_clk_ext.clk)
608 + jz_clk_reg_clear_bits(JZ_REG_CLOCK_CTRL, JZ_CLOCK_CTRL_UDC_SRC_PLL);
612 + clk->parent = parent;
617 +static int jz_clk_udc_set_rate(struct clk *clk, unsigned long rate)
621 + if (clk->parent == &jz_clk_ext.clk)
624 + div = clk_get_rate(clk->parent) / rate - 1;
631 + jz_clk_reg_write_mask(JZ_REG_CLOCK_CTRL, div << JZ_CLOCK_CTRL_UDIV_OFFSET,
632 + JZ_CLOCK_CTRL_UDIV_MASK);
636 +static unsigned long jz_clk_udc_get_rate(struct clk *clk)
640 + if (clk->parent == &jz_clk_ext.clk)
641 + return clk_get_rate(clk->parent);
643 + div = (jz_clk_reg_read(JZ_REG_CLOCK_CTRL) & JZ_CLOCK_CTRL_UDIV_MASK);
644 + div >>= JZ_CLOCK_CTRL_UDIV_OFFSET;
647 + return clk_get_rate(clk->parent) / div;
650 +static unsigned long jz_clk_divided_get_rate(struct clk *clk)
652 + struct divided_clk *dclk = (struct divided_clk *)clk;
655 + if (clk->parent == &jz_clk_ext.clk)
656 + return clk_get_rate(clk->parent);
658 + div = (jz_clk_reg_read(dclk->reg) & dclk->mask) + 1;
660 + return clk_get_rate(clk->parent) / div;
663 +static int jz_clk_divided_set_rate(struct clk *clk, unsigned long rate)
665 + struct divided_clk *dclk = (struct divided_clk *)clk;
668 + if (clk->parent == &jz_clk_ext.clk)
671 + div = clk_get_rate(clk->parent) / rate - 1;
675 + else if (div > dclk->mask)
678 + jz_clk_reg_write_mask(dclk->reg, div, dclk->mask);
683 +static unsigned long jz_clk_ldclk_round_rate(struct clk *clk, unsigned long rate)
686 + unsigned long parent_rate = jz_clk_pll_half_get_rate(clk->parent);
688 + if (rate > 150000000)
691 + div = parent_rate / rate;
697 + return parent_rate / div;
700 +static int jz_clk_ldclk_set_rate(struct clk *clk, unsigned long rate)
704 + if (rate > 150000000)
707 + div = jz_clk_pll_half_get_rate(clk->parent) / rate - 1;
713 + jz_clk_reg_write_mask(JZ_REG_CLOCK_CTRL, div << JZ_CLOCK_CTRL_LDIV_OFFSET,
714 + JZ_CLOCK_CTRL_LDIV_MASK);
719 +static unsigned long jz_clk_ldclk_get_rate(struct clk *clk)
723 + div = jz_clk_reg_read(JZ_REG_CLOCK_CTRL) & JZ_CLOCK_CTRL_LDIV_MASK;
724 + div >>= JZ_CLOCK_CTRL_LDIV_OFFSET;
726 + return jz_clk_pll_half_get_rate(clk->parent) / (div + 1);
729 +static const struct clk_ops jz_clk_ops_ld = {
730 + .set_rate = jz_clk_ldclk_set_rate,
731 + .get_rate = jz_clk_ldclk_get_rate,
732 + .round_rate = jz_clk_ldclk_round_rate,
733 + .enable = jz_clk_enable_gating,
734 + .disable = jz_clk_disable_gating,
735 + .is_enabled = jz_clk_is_enabled_gating,
738 +static struct clk jz_clk_ld = {
740 + .gate_bit = JZ_CLOCK_GATE_LCD,
741 + .parent = &jz_clk_pll_half,
742 + .ops = &jz_clk_ops_ld,
745 +static const struct clk_ops jz_clk_i2s_ops = {
746 + .set_rate = jz_clk_divided_set_rate,
747 + .get_rate = jz_clk_divided_get_rate,
748 + .enable = jz_clk_enable_gating,
749 + .disable = jz_clk_disable_gating,
750 + .is_enabled = jz_clk_is_enabled_gating,
751 + .set_parent = jz_clk_i2s_set_parent,
754 +static const struct clk_ops jz_clk_spi_ops = {
755 + .set_rate = jz_clk_divided_set_rate,
756 + .get_rate = jz_clk_divided_get_rate,
757 + .enable = jz_clk_enable_gating,
758 + .disable = jz_clk_disable_gating,
759 + .is_enabled = jz_clk_is_enabled_gating,
760 + .set_parent = jz_clk_spi_set_parent,
763 +static const struct clk_ops jz_clk_divided_ops = {
764 + .set_rate = jz_clk_divided_set_rate,
765 + .get_rate = jz_clk_divided_get_rate,
766 + .enable = jz_clk_enable_gating,
767 + .disable = jz_clk_disable_gating,
768 + .is_enabled = jz_clk_is_enabled_gating,
771 +static struct divided_clk jz4740_clock_divided_clks[] = {
775 + .parent = &jz_clk_ext.clk,
776 + .gate_bit = JZ_CLOCK_GATE_I2S,
777 + .ops = &jz_clk_i2s_ops,
779 + .reg = JZ_REG_CLOCK_I2S,
780 + .mask = JZ_CLOCK_I2S_DIV_MASK,
785 + .parent = &jz_clk_ext.clk,
786 + .gate_bit = JZ_CLOCK_GATE_SPI,
787 + .ops = &jz_clk_spi_ops,
789 + .reg = JZ_REG_CLOCK_SPI,
790 + .mask = JZ_CLOCK_SPI_DIV_MASK,
794 + .name = "lcd_pclk",
795 + .parent = &jz_clk_pll_half,
796 + .gate_bit = JZ4740_CLK_NOT_GATED,
797 + .ops = &jz_clk_divided_ops,
799 + .reg = JZ_REG_CLOCK_LCD,
800 + .mask = JZ_CLOCK_LCD_DIV_MASK,
805 + .parent = &jz_clk_pll_half,
806 + .gate_bit = JZ_CLOCK_GATE_MMC,
807 + .ops = &jz_clk_divided_ops,
809 + .reg = JZ_REG_CLOCK_MMC,
810 + .mask = JZ_CLOCK_MMC_DIV_MASK,
815 + .parent = &jz_clk_pll_half,
816 + .gate_bit = JZ_CLOCK_GATE_UHC,
817 + .ops = &jz_clk_divided_ops,
819 + .reg = JZ_REG_CLOCK_UHC,
820 + .mask = JZ_CLOCK_UHC_DIV_MASK,
824 +static const struct clk_ops jz_clk_udc_ops = {
825 + .set_parent = jz_clk_udc_set_parent,
826 + .set_rate = jz_clk_udc_set_rate,
827 + .get_rate = jz_clk_udc_get_rate,
828 + .enable = jz_clk_udc_enable,
829 + .disable = jz_clk_udc_disable,
830 + .is_enabled = jz_clk_udc_is_enabled,
833 +static const struct clk_ops jz_clk_simple_ops = {
834 + .enable = jz_clk_enable_gating,
835 + .disable = jz_clk_disable_gating,
836 + .is_enabled = jz_clk_is_enabled_gating,
839 +static struct clk jz4740_clock_simple_clks[] = {
842 + .parent = &jz_clk_ext.clk,
843 + .ops = &jz_clk_udc_ops,
847 + .parent = &jz_clk_ext.clk,
848 + .gate_bit = JZ_CLOCK_GATE_UART0,
849 + .ops = &jz_clk_simple_ops,
853 + .parent = &jz_clk_ext.clk,
854 + .gate_bit = JZ_CLOCK_GATE_UART1,
855 + .ops = &jz_clk_simple_ops,
859 + .parent = &jz_clk_high_speed_peripheral.clk,
860 + .gate_bit = JZ_CLOCK_GATE_UART0,
861 + .ops = &jz_clk_simple_ops,
865 + .parent = &jz_clk_high_speed_peripheral.clk,
866 + .gate_bit = JZ_CLOCK_GATE_IPU,
867 + .ops = &jz_clk_simple_ops,
871 + .parent = &jz_clk_ext.clk,
872 + .gate_bit = JZ_CLOCK_GATE_ADC,
873 + .ops = &jz_clk_simple_ops,
877 + .parent = &jz_clk_ext.clk,
878 + .gate_bit = JZ_CLOCK_GATE_I2C,
879 + .ops = &jz_clk_simple_ops,
883 + .parent = &jz_clk_ext.clk,
884 + .gate_bit = JZ_CLOCK_GATE_AIC,
885 + .ops = &jz_clk_simple_ops,
889 +static struct static_clk jz_clk_rtc = {
892 + .gate_bit = JZ_CLOCK_GATE_RTC,
893 + .ops = &jz_clk_static_ops,
898 +int clk_enable(struct clk *clk)
900 + if (!clk->ops->enable)
903 + return clk->ops->enable(clk);
905 +EXPORT_SYMBOL_GPL(clk_enable);
907 +void clk_disable(struct clk *clk)
909 + if (clk->ops->disable)
910 + clk->ops->disable(clk);
912 +EXPORT_SYMBOL_GPL(clk_disable);
914 +int clk_is_enabled(struct clk *clk)
916 + if (clk->ops->is_enabled)
917 + return clk->ops->is_enabled(clk);
922 +unsigned long clk_get_rate(struct clk *clk)
924 + if (clk->ops->get_rate)
925 + return clk->ops->get_rate(clk);
927 + return clk_get_rate(clk->parent);
931 +EXPORT_SYMBOL_GPL(clk_get_rate);
933 +int clk_set_rate(struct clk *clk, unsigned long rate)
935 + if (!clk->ops->set_rate)
937 + return clk->ops->set_rate(clk, rate);
939 +EXPORT_SYMBOL_GPL(clk_set_rate);
941 +long clk_round_rate(struct clk *clk, unsigned long rate)
943 + if (clk->ops->round_rate)
944 + return clk->ops->round_rate(clk, rate);
948 +EXPORT_SYMBOL_GPL(clk_round_rate);
950 +int clk_set_parent(struct clk *clk, struct clk *parent)
955 + if (!clk->ops->set_parent)
958 + enabled = clk_is_enabled(clk);
961 + ret = clk->ops->set_parent(clk, parent);
965 + jz4740_clock_debugfs_update_parent(clk);
969 +EXPORT_SYMBOL_GPL(clk_set_parent);
971 +struct clk *clk_get(struct device *dev, const char *name)
975 + list_for_each_entry(clk, &jz_clocks, list) {
976 + if (strcmp(clk->name, name) == 0)
979 + return ERR_PTR(-ENXIO);
981 +EXPORT_SYMBOL_GPL(clk_get);
983 +void clk_put(struct clk *clk)
986 +EXPORT_SYMBOL_GPL(clk_put);
988 +static inline void clk_add(struct clk *clk)
990 + list_add_tail(&clk->list, &jz_clocks);
992 + jz4740_clock_debugfs_add_clk(clk);
995 +static void clk_register_clks(void)
999 + clk_add(&jz_clk_ext.clk);
1000 + clk_add(&jz_clk_pll);
1001 + clk_add(&jz_clk_pll_half);
1002 + clk_add(&jz_clk_cpu.clk);
1003 + clk_add(&jz_clk_high_speed_peripheral.clk);
1004 + clk_add(&jz_clk_low_speed_peripheral.clk);
1005 + clk_add(&jz_clk_ko);
1006 + clk_add(&jz_clk_ld);
1007 + clk_add(&jz_clk_rtc.clk);
1009 + for (i = 0; i < ARRAY_SIZE(jz4740_clock_divided_clks); ++i)
1010 + clk_add(&jz4740_clock_divided_clks[i].clk);
1012 + for (i = 0; i < ARRAY_SIZE(jz4740_clock_simple_clks); ++i)
1013 + clk_add(&jz4740_clock_simple_clks[i]);
1016 +void jz4740_clock_set_wait_mode(enum jz4740_wait_mode mode)
1019 + case JZ4740_WAIT_MODE_IDLE:
1020 + jz_clk_reg_clear_bits(JZ_REG_CLOCK_LOW_POWER, JZ_CLOCK_LOW_POWER_MODE_SLEEP);
1022 + case JZ4740_WAIT_MODE_SLEEP:
1023 + jz_clk_reg_set_bits(JZ_REG_CLOCK_LOW_POWER, JZ_CLOCK_LOW_POWER_MODE_SLEEP);
1028 +void jz4740_clock_udc_disable_auto_suspend(void)
1030 + jz_clk_reg_clear_bits(JZ_REG_CLOCK_GATE, JZ_CLOCK_GATE_UDC);
1032 +EXPORT_SYMBOL_GPL(jz4740_clock_udc_disable_auto_suspend);
1034 +void jz4740_clock_udc_enable_auto_suspend(void)
1036 + jz_clk_reg_set_bits(JZ_REG_CLOCK_GATE, JZ_CLOCK_GATE_UDC);
1038 +EXPORT_SYMBOL_GPL(jz4740_clock_udc_enable_auto_suspend);
1040 +void jz4740_clock_suspend(void)
1042 + jz_clk_reg_set_bits(JZ_REG_CLOCK_GATE,
1043 + JZ_CLOCK_GATE_TCU | JZ_CLOCK_GATE_DMAC | JZ_CLOCK_GATE_UART0);
1045 + jz_clk_reg_clear_bits(JZ_REG_CLOCK_PLL, JZ_CLOCK_PLL_ENABLED);
1048 +void jz4740_clock_resume(void)
1052 + jz_clk_reg_set_bits(JZ_REG_CLOCK_PLL, JZ_CLOCK_PLL_ENABLED);
1055 + pll = jz_clk_reg_read(JZ_REG_CLOCK_PLL);
1056 + } while (!(pll & JZ_CLOCK_PLL_STABLE));
1058 + jz_clk_reg_clear_bits(JZ_REG_CLOCK_GATE,
1059 + JZ_CLOCK_GATE_TCU | JZ_CLOCK_GATE_DMAC | JZ_CLOCK_GATE_UART0);
1062 +static int jz4740_clock_init(void)
1066 + jz_clock_base = ioremap(JZ4740_CPM_BASE_ADDR, 0x100);
1067 + if (!jz_clock_base)
1070 + spin_lock_init(&jz_clock_lock);
1072 + jz_clk_ext.rate = jz4740_clock_bdata.ext_rate;
1073 + jz_clk_rtc.rate = jz4740_clock_bdata.rtc_rate;
1075 + val = jz_clk_reg_read(JZ_REG_CLOCK_SPI);
1077 + if (val & JZ_CLOCK_SPI_SRC_PLL)
1078 + jz4740_clock_divided_clks[1].clk.parent = &jz_clk_pll_half;
1080 + val = jz_clk_reg_read(JZ_REG_CLOCK_CTRL);
1082 + if (val & JZ_CLOCK_CTRL_I2S_SRC_PLL)
1083 + jz4740_clock_divided_clks[0].clk.parent = &jz_clk_pll_half;
1085 + if (val & JZ_CLOCK_CTRL_UDC_SRC_PLL)
1086 + jz4740_clock_simple_clks[0].parent = &jz_clk_pll_half;
1088 + jz4740_clock_debugfs_init();
1090 + clk_register_clks();
1094 +arch_initcall(jz4740_clock_init);
1096 +++ b/arch/mips/jz4740/clock.h
1099 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
1100 + * JZ4740 SoC clock support
1102 + * This program is free software; you can redistribute it and/or modify it
1103 + * under the terms of the GNU General Public License as published by the
1104 + * Free Software Foundation; either version 2 of the License, or (at your
1105 + * option) any later version.
1107 + * You should have received a copy of the GNU General Public License along
1108 + * with this program; if not, write to the Free Software Foundation, Inc.,
1109 + * 675 Mass Ave, Cambridge, MA 02139, USA.
1113 +#ifndef __MIPS_JZ4740_CLOCK_H__
1114 +#define __MIPS_JZ4740_CLOCK_H__
1116 +#include <linux/list.h>
1118 +struct jz4740_clock_board_data {
1119 + unsigned long ext_rate;
1120 + unsigned long rtc_rate;
1123 +extern struct jz4740_clock_board_data jz4740_clock_bdata;
1125 +void jz4740_clock_suspend(void);
1126 +void jz4740_clock_resume(void);
1131 + unsigned long (*get_rate)(struct clk *clk);
1132 + unsigned long (*round_rate)(struct clk *clk, unsigned long rate);
1133 + int (*set_rate)(struct clk *clk, unsigned long rate);
1134 + int (*enable)(struct clk *clk);
1135 + int (*disable)(struct clk *clk);
1136 + int (*is_enabled)(struct clk *clk);
1138 + int (*set_parent)(struct clk *clk, struct clk *parent);
1144 + struct clk *parent;
1146 + uint32_t gate_bit;
1148 + const struct clk_ops *ops;
1150 + struct list_head list;
1152 +#ifdef CONFIG_DEBUG_FS
1153 + struct dentry *debugfs_entry;
1154 + struct dentry *debugfs_parent_entry;
1159 +#define JZ4740_CLK_NOT_GATED ((uint32_t)-1)
1161 +int clk_is_enabled(struct clk *clk);
1163 +#ifdef CONFIG_DEBUG_FS
1164 +void jz4740_clock_debugfs_init(void);
1165 +void jz4740_clock_debugfs_add_clk(struct clk *clk);
1166 +void jz4740_clock_debugfs_update_parent(struct clk *clk);
1168 +static inline void jz4740_clock_debugfs_init(void) {};
1169 +static inline void jz4740_clock_debugfs_add_clk(struct clk *clk) {};
1170 +static inline void jz4740_clock_debugfs_update_parent(struct clk *clk) {};