2 * Nomadik clock implementation
3 * Copyright (C) 2013 ST-Ericsson AB
4 * License terms: GNU General Public License (GPL) version 2
8 #define pr_fmt(fmt) "Nomadik SRC clocks: " fmt
10 #include <linux/bitops.h>
11 #include <linux/clk.h>
12 #include <linux/clkdev.h>
13 #include <linux/err.h>
15 #include <linux/clk-provider.h>
17 #include <linux/of_address.h>
18 #include <linux/debugfs.h>
19 #include <linux/seq_file.h>
20 #include <linux/spinlock.h>
21 #include <linux/reboot.h>
24 * The Nomadik clock tree is described in the STN8815A12 DB V4.2
25 * reference manual for the chip, page 94 ff.
26 * Clock IDs are in the STn8815 Reference Manual table 3, page 27.
30 #define SRC_CR_T0_ENSEL BIT(15)
31 #define SRC_CR_T1_ENSEL BIT(17)
32 #define SRC_CR_T2_ENSEL BIT(19)
33 #define SRC_CR_T3_ENSEL BIT(21)
34 #define SRC_CR_T4_ENSEL BIT(23)
35 #define SRC_CR_T5_ENSEL BIT(25)
36 #define SRC_CR_T6_ENSEL BIT(27)
37 #define SRC_CR_T7_ENSEL BIT(29)
38 #define SRC_XTALCR 0x0CU
39 #define SRC_XTALCR_XTALTIMEN BIT(20)
40 #define SRC_XTALCR_SXTALDIS BIT(19)
41 #define SRC_XTALCR_MXTALSTAT BIT(2)
42 #define SRC_XTALCR_MXTALEN BIT(1)
43 #define SRC_XTALCR_MXTALOVER BIT(0)
44 #define SRC_PLLCR 0x10U
45 #define SRC_PLLCR_PLLTIMEN BIT(29)
46 #define SRC_PLLCR_PLL2EN BIT(28)
47 #define SRC_PLLCR_PLL1STAT BIT(2)
48 #define SRC_PLLCR_PLL1EN BIT(1)
49 #define SRC_PLLCR_PLL1OVER BIT(0)
50 #define SRC_PLLFR 0x14U
51 #define SRC_PCKEN0 0x24U
52 #define SRC_PCKDIS0 0x28U
53 #define SRC_PCKENSR0 0x2CU
54 #define SRC_PCKSR0 0x30U
55 #define SRC_PCKEN1 0x34U
56 #define SRC_PCKDIS1 0x38U
57 #define SRC_PCKENSR1 0x3CU
58 #define SRC_PCKSR1 0x40U
60 /* Lock protecting the SRC_CR register */
61 static DEFINE_SPINLOCK(src_lock);
62 /* Base address of the SRC */
63 static void __iomem *src_base;
65 static int nomadik_clk_reboot_handler(struct notifier_block *this,
71 /* The main chrystal need to be enabled for reboot to work */
72 val = readl(src_base + SRC_XTALCR);
73 val &= ~SRC_XTALCR_MXTALOVER;
74 val |= SRC_XTALCR_MXTALEN;
75 pr_crit("force-enabling MXTALO\n");
76 writel(val, src_base + SRC_XTALCR);
80 static struct notifier_block nomadik_clk_reboot_notifier = {
81 .notifier_call = nomadik_clk_reboot_handler,
84 static const struct of_device_id nomadik_src_match[] __initconst = {
85 { .compatible = "stericsson,nomadik-src" },
89 static void __init nomadik_src_init(void)
91 struct device_node *np;
94 np = of_find_matching_node(NULL, nomadik_src_match);
96 pr_crit("no matching node for SRC, aborting clock init\n");
99 src_base = of_iomap(np, 0);
101 pr_err("%s: must have src parent node with REGS (%s)\n",
106 /* Set all timers to use the 2.4 MHz TIMCLK */
107 val = readl(src_base + SRC_CR);
108 val |= SRC_CR_T0_ENSEL;
109 val |= SRC_CR_T1_ENSEL;
110 val |= SRC_CR_T2_ENSEL;
111 val |= SRC_CR_T3_ENSEL;
112 val |= SRC_CR_T4_ENSEL;
113 val |= SRC_CR_T5_ENSEL;
114 val |= SRC_CR_T6_ENSEL;
115 val |= SRC_CR_T7_ENSEL;
116 writel(val, src_base + SRC_CR);
118 val = readl(src_base + SRC_XTALCR);
119 pr_info("SXTALO is %s\n",
120 (val & SRC_XTALCR_SXTALDIS) ? "disabled" : "enabled");
121 pr_info("MXTAL is %s\n",
122 (val & SRC_XTALCR_MXTALSTAT) ? "enabled" : "disabled");
123 if (of_property_read_bool(np, "disable-sxtalo")) {
124 /* The machine uses an external oscillator circuit */
125 val |= SRC_XTALCR_SXTALDIS;
126 pr_info("disabling SXTALO\n");
128 if (of_property_read_bool(np, "disable-mxtalo")) {
129 /* Disable this too: also run by external oscillator */
130 val |= SRC_XTALCR_MXTALOVER;
131 val &= ~SRC_XTALCR_MXTALEN;
132 pr_info("disabling MXTALO\n");
134 writel(val, src_base + SRC_XTALCR);
135 register_reboot_notifier(&nomadik_clk_reboot_notifier);
139 * struct clk_pll1 - Nomadik PLL1 clock
140 * @hw: corresponding clock hardware entry
141 * @id: PLL instance: 1 or 2
149 * struct clk_src - Nomadik src clock
150 * @hw: corresponding clock hardware entry
152 * @group1: true if the clock is in group1, else it is in group0
153 * @clkbit: bit 0...31 corresponding to the clock in each clock register
162 #define to_pll(_hw) container_of(_hw, struct clk_pll, hw)
163 #define to_src(_hw) container_of(_hw, struct clk_src, hw)
165 static int pll_clk_enable(struct clk_hw *hw)
167 struct clk_pll *pll = to_pll(hw);
170 spin_lock(&src_lock);
171 val = readl(src_base + SRC_PLLCR);
173 if (val & SRC_PLLCR_PLL1OVER) {
174 val |= SRC_PLLCR_PLL1EN;
175 writel(val, src_base + SRC_PLLCR);
177 } else if (pll->id == 2) {
178 val |= SRC_PLLCR_PLL2EN;
179 writel(val, src_base + SRC_PLLCR);
181 spin_unlock(&src_lock);
185 static void pll_clk_disable(struct clk_hw *hw)
187 struct clk_pll *pll = to_pll(hw);
190 spin_lock(&src_lock);
191 val = readl(src_base + SRC_PLLCR);
193 if (val & SRC_PLLCR_PLL1OVER) {
194 val &= ~SRC_PLLCR_PLL1EN;
195 writel(val, src_base + SRC_PLLCR);
197 } else if (pll->id == 2) {
198 val &= ~SRC_PLLCR_PLL2EN;
199 writel(val, src_base + SRC_PLLCR);
201 spin_unlock(&src_lock);
204 static int pll_clk_is_enabled(struct clk_hw *hw)
206 struct clk_pll *pll = to_pll(hw);
209 val = readl(src_base + SRC_PLLCR);
211 if (val & SRC_PLLCR_PLL1OVER)
212 return !!(val & SRC_PLLCR_PLL1EN);
213 } else if (pll->id == 2) {
214 return !!(val & SRC_PLLCR_PLL2EN);
219 static unsigned long pll_clk_recalc_rate(struct clk_hw *hw,
220 unsigned long parent_rate)
222 struct clk_pll *pll = to_pll(hw);
225 val = readl(src_base + SRC_PLLFR);
231 mul = (val >> 8) & 0x3FU;
234 return (parent_rate * mul) >> div;
240 mul = (val >> 24) & 0x3FU;
242 return (parent_rate * mul);
250 static const struct clk_ops pll_clk_ops = {
251 .enable = pll_clk_enable,
252 .disable = pll_clk_disable,
253 .is_enabled = pll_clk_is_enabled,
254 .recalc_rate = pll_clk_recalc_rate,
257 static struct clk * __init
258 pll_clk_register(struct device *dev, const char *name,
259 const char *parent_name, u32 id)
263 struct clk_init_data init;
265 if (id != 1 && id != 2) {
266 pr_err("%s: the Nomadik has only PLL 1 & 2\n", __func__);
267 return ERR_PTR(-EINVAL);
270 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
272 pr_err("%s: could not allocate PLL clk\n", __func__);
273 return ERR_PTR(-ENOMEM);
277 init.ops = &pll_clk_ops;
278 init.parent_names = (parent_name ? &parent_name : NULL);
279 init.num_parents = (parent_name ? 1 : 0);
280 pll->hw.init = &init;
283 pr_debug("register PLL1 clock \"%s\"\n", name);
285 clk = clk_register(dev, &pll->hw);
293 * The Nomadik SRC clocks are gated, but not in the sense that
294 * you read-modify-write a register. Instead there are separate
295 * clock enable and clock disable registers. Writing a '1' bit in
296 * the enable register for a certain clock ungates that clock without
297 * affecting the other clocks. The disable register works the opposite
301 static int src_clk_enable(struct clk_hw *hw)
303 struct clk_src *sclk = to_src(hw);
304 u32 enreg = sclk->group1 ? SRC_PCKEN1 : SRC_PCKEN0;
305 u32 sreg = sclk->group1 ? SRC_PCKSR1 : SRC_PCKSR0;
307 writel(sclk->clkbit, src_base + enreg);
308 /* spin until enabled */
309 while (!(readl(src_base + sreg) & sclk->clkbit))
314 static void src_clk_disable(struct clk_hw *hw)
316 struct clk_src *sclk = to_src(hw);
317 u32 disreg = sclk->group1 ? SRC_PCKDIS1 : SRC_PCKDIS0;
318 u32 sreg = sclk->group1 ? SRC_PCKSR1 : SRC_PCKSR0;
320 writel(sclk->clkbit, src_base + disreg);
321 /* spin until disabled */
322 while (readl(src_base + sreg) & sclk->clkbit)
326 static int src_clk_is_enabled(struct clk_hw *hw)
328 struct clk_src *sclk = to_src(hw);
329 u32 sreg = sclk->group1 ? SRC_PCKSR1 : SRC_PCKSR0;
330 u32 val = readl(src_base + sreg);
332 return !!(val & sclk->clkbit);
336 src_clk_recalc_rate(struct clk_hw *hw,
337 unsigned long parent_rate)
342 static const struct clk_ops src_clk_ops = {
343 .enable = src_clk_enable,
344 .disable = src_clk_disable,
345 .is_enabled = src_clk_is_enabled,
346 .recalc_rate = src_clk_recalc_rate,
349 static struct clk * __init
350 src_clk_register(struct device *dev, const char *name,
351 const char *parent_name, u8 id)
354 struct clk_src *sclk;
355 struct clk_init_data init;
357 sclk = kzalloc(sizeof(*sclk), GFP_KERNEL);
359 pr_err("could not allocate SRC clock %s\n",
361 return ERR_PTR(-ENOMEM);
364 init.ops = &src_clk_ops;
365 /* Do not force-disable the static SDRAM controller */
367 init.flags = CLK_IGNORE_UNUSED;
370 init.parent_names = (parent_name ? &parent_name : NULL);
371 init.num_parents = (parent_name ? 1 : 0);
372 sclk->hw.init = &init;
374 sclk->group1 = (id > 31);
375 sclk->clkbit = BIT(id & 0x1f);
377 pr_debug("register clock \"%s\" ID: %d group: %d bits: %08x\n",
378 name, id, sclk->group1, sclk->clkbit);
380 clk = clk_register(dev, &sclk->hw);
387 #ifdef CONFIG_DEBUG_FS
389 static u32 src_pcksr0_boot;
390 static u32 src_pcksr1_boot;
392 static const char * const src_clk_names[] = {
459 static int nomadik_src_clk_show(struct seq_file *s, void *what)
462 u32 src_pcksr0 = readl(src_base + SRC_PCKSR0);
463 u32 src_pcksr1 = readl(src_base + SRC_PCKSR1);
464 u32 src_pckensr0 = readl(src_base + SRC_PCKENSR0);
465 u32 src_pckensr1 = readl(src_base + SRC_PCKENSR1);
467 seq_printf(s, "Clock: Boot: Now: Request: ASKED:\n");
468 for (i = 0; i < ARRAY_SIZE(src_clk_names); i++) {
469 u32 pcksrb = (i < 0x20) ? src_pcksr0_boot : src_pcksr1_boot;
470 u32 pcksr = (i < 0x20) ? src_pcksr0 : src_pcksr1;
471 u32 pckreq = (i < 0x20) ? src_pckensr0 : src_pckensr1;
472 u32 mask = BIT(i & 0x1f);
474 seq_printf(s, "%s %s %s %s\n",
476 (pcksrb & mask) ? "on " : "off",
477 (pcksr & mask) ? "on " : "off",
478 (pckreq & mask) ? "on " : "off");
483 static int nomadik_src_clk_open(struct inode *inode, struct file *file)
485 return single_open(file, nomadik_src_clk_show, NULL);
488 static const struct file_operations nomadik_src_clk_debugfs_ops = {
489 .open = nomadik_src_clk_open,
492 .release = single_release,
495 static int __init nomadik_src_clk_init_debugfs(void)
497 /* Vital for multiplatform */
500 src_pcksr0_boot = readl(src_base + SRC_PCKSR0);
501 src_pcksr1_boot = readl(src_base + SRC_PCKSR1);
502 debugfs_create_file("nomadik-src-clk", S_IFREG | S_IRUGO,
503 NULL, NULL, &nomadik_src_clk_debugfs_ops);
507 module_init(nomadik_src_clk_init_debugfs);
511 static void __init of_nomadik_pll_setup(struct device_node *np)
513 struct clk *clk = ERR_PTR(-EINVAL);
514 const char *clk_name = np->name;
515 const char *parent_name;
521 if (of_property_read_u32(np, "pll-id", &pll_id)) {
522 pr_err("%s: PLL \"%s\" missing pll-id property\n",
526 parent_name = of_clk_get_parent_name(np, 0);
527 clk = pll_clk_register(NULL, clk_name, parent_name, pll_id);
529 of_clk_add_provider(np, of_clk_src_simple_get, clk);
531 CLK_OF_DECLARE(nomadik_pll_clk,
532 "st,nomadik-pll-clock", of_nomadik_pll_setup);
534 static void __init of_nomadik_hclk_setup(struct device_node *np)
536 struct clk *clk = ERR_PTR(-EINVAL);
537 const char *clk_name = np->name;
538 const char *parent_name;
543 parent_name = of_clk_get_parent_name(np, 0);
545 * The HCLK divides PLL1 with 1 (passthru), 2, 3 or 4.
547 clk = clk_register_divider(NULL, clk_name, parent_name,
548 0, src_base + SRC_CR,
550 CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
553 of_clk_add_provider(np, of_clk_src_simple_get, clk);
555 CLK_OF_DECLARE(nomadik_hclk_clk,
556 "st,nomadik-hclk-clock", of_nomadik_hclk_setup);
558 static void __init of_nomadik_src_clk_setup(struct device_node *np)
560 struct clk *clk = ERR_PTR(-EINVAL);
561 const char *clk_name = np->name;
562 const char *parent_name;
568 if (of_property_read_u32(np, "clock-id", &clk_id)) {
569 pr_err("%s: SRC clock \"%s\" missing clock-id property\n",
573 parent_name = of_clk_get_parent_name(np, 0);
574 clk = src_clk_register(NULL, clk_name, parent_name, clk_id);
576 of_clk_add_provider(np, of_clk_src_simple_get, clk);
578 CLK_OF_DECLARE(nomadik_src_clk,
579 "st,nomadik-src-clock", of_nomadik_src_clk_setup);