xref: /openbmc/linux/drivers/clk/berlin/berlin2-pll.c (revision 836ee0f7)
1cf8de5a7SAlexandre Belloni /*
2cf8de5a7SAlexandre Belloni  * Copyright (c) 2014 Marvell Technology Group Ltd.
3cf8de5a7SAlexandre Belloni  *
4cf8de5a7SAlexandre Belloni  * Alexandre Belloni <alexandre.belloni@free-electrons.com>
5cf8de5a7SAlexandre Belloni  * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
6cf8de5a7SAlexandre Belloni  *
7cf8de5a7SAlexandre Belloni  * This program is free software; you can redistribute it and/or modify it
8cf8de5a7SAlexandre Belloni  * under the terms and conditions of the GNU General Public License,
9cf8de5a7SAlexandre Belloni  * version 2, as published by the Free Software Foundation.
10cf8de5a7SAlexandre Belloni  *
11cf8de5a7SAlexandre Belloni  * This program is distributed in the hope it will be useful, but WITHOUT
12cf8de5a7SAlexandre Belloni  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13cf8de5a7SAlexandre Belloni  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14cf8de5a7SAlexandre Belloni  * more details.
15cf8de5a7SAlexandre Belloni  *
16cf8de5a7SAlexandre Belloni  * You should have received a copy of the GNU General Public License along with
17cf8de5a7SAlexandre Belloni  * this program.  If not, see <http://www.gnu.org/licenses/>.
18cf8de5a7SAlexandre Belloni  */
19cf8de5a7SAlexandre Belloni #include <linux/clk-provider.h>
20cf8de5a7SAlexandre Belloni #include <linux/io.h>
21cf8de5a7SAlexandre Belloni #include <linux/kernel.h>
22cf8de5a7SAlexandre Belloni #include <linux/of.h>
23cf8de5a7SAlexandre Belloni #include <linux/of_address.h>
24cf8de5a7SAlexandre Belloni #include <linux/slab.h>
25cf8de5a7SAlexandre Belloni #include <asm/div64.h>
26cf8de5a7SAlexandre Belloni 
27cf8de5a7SAlexandre Belloni #include "berlin2-div.h"
2847c18e4cSStephen Boyd #include "berlin2-pll.h"
29cf8de5a7SAlexandre Belloni 
30cf8de5a7SAlexandre Belloni struct berlin2_pll {
31cf8de5a7SAlexandre Belloni 	struct clk_hw hw;
32cf8de5a7SAlexandre Belloni 	void __iomem *base;
33cf8de5a7SAlexandre Belloni 	struct berlin2_pll_map map;
34cf8de5a7SAlexandre Belloni };
35cf8de5a7SAlexandre Belloni 
36cf8de5a7SAlexandre Belloni #define to_berlin2_pll(hw) container_of(hw, struct berlin2_pll, hw)
37cf8de5a7SAlexandre Belloni 
38cf8de5a7SAlexandre Belloni #define SPLL_CTRL0	0x00
39cf8de5a7SAlexandre Belloni #define SPLL_CTRL1	0x04
40cf8de5a7SAlexandre Belloni #define SPLL_CTRL2	0x08
41cf8de5a7SAlexandre Belloni #define SPLL_CTRL3	0x0c
42cf8de5a7SAlexandre Belloni #define SPLL_CTRL4	0x10
43cf8de5a7SAlexandre Belloni 
44cf8de5a7SAlexandre Belloni #define FBDIV_MASK	0x1ff
45cf8de5a7SAlexandre Belloni #define RFDIV_MASK	0x1f
46cf8de5a7SAlexandre Belloni #define DIVSEL_MASK	0xf
47cf8de5a7SAlexandre Belloni 
48cf8de5a7SAlexandre Belloni /*
49cf8de5a7SAlexandre Belloni  * The output frequency formula for the pll is:
50cf8de5a7SAlexandre Belloni  * clkout = fbdiv / refdiv * parent / vcodiv
51cf8de5a7SAlexandre Belloni  */
52cf8de5a7SAlexandre Belloni static unsigned long
53cf8de5a7SAlexandre Belloni berlin2_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
54cf8de5a7SAlexandre Belloni {
55cf8de5a7SAlexandre Belloni 	struct berlin2_pll *pll = to_berlin2_pll(hw);
56cf8de5a7SAlexandre Belloni 	struct berlin2_pll_map *map = &pll->map;
57cf8de5a7SAlexandre Belloni 	u32 val, fbdiv, rfdiv, vcodivsel, vcodiv;
58cf8de5a7SAlexandre Belloni 	u64 rate = parent_rate;
59cf8de5a7SAlexandre Belloni 
60cf8de5a7SAlexandre Belloni 	val = readl_relaxed(pll->base + SPLL_CTRL0);
61cf8de5a7SAlexandre Belloni 	fbdiv = (val >> map->fbdiv_shift) & FBDIV_MASK;
62cf8de5a7SAlexandre Belloni 	rfdiv = (val >> map->rfdiv_shift) & RFDIV_MASK;
63cf8de5a7SAlexandre Belloni 	if (rfdiv == 0) {
64836ee0f7SStephen Boyd 		pr_warn("%s has zero rfdiv\n", clk_hw_get_name(hw));
65cf8de5a7SAlexandre Belloni 		rfdiv = 1;
66cf8de5a7SAlexandre Belloni 	}
67cf8de5a7SAlexandre Belloni 
68cf8de5a7SAlexandre Belloni 	val = readl_relaxed(pll->base + SPLL_CTRL1);
69cf8de5a7SAlexandre Belloni 	vcodivsel = (val >> map->divsel_shift) & DIVSEL_MASK;
70cf8de5a7SAlexandre Belloni 	vcodiv = map->vcodiv[vcodivsel];
71cf8de5a7SAlexandre Belloni 	if (vcodiv == 0) {
72cf8de5a7SAlexandre Belloni 		pr_warn("%s has zero vcodiv (index %d)\n",
73836ee0f7SStephen Boyd 			clk_hw_get_name(hw), vcodivsel);
74cf8de5a7SAlexandre Belloni 		vcodiv = 1;
75cf8de5a7SAlexandre Belloni 	}
76cf8de5a7SAlexandre Belloni 
77cf8de5a7SAlexandre Belloni 	rate *= fbdiv * map->mult;
78cf8de5a7SAlexandre Belloni 	do_div(rate, rfdiv * vcodiv);
79cf8de5a7SAlexandre Belloni 
80cf8de5a7SAlexandre Belloni 	return (unsigned long)rate;
81cf8de5a7SAlexandre Belloni }
82cf8de5a7SAlexandre Belloni 
83cf8de5a7SAlexandre Belloni static const struct clk_ops berlin2_pll_ops = {
84cf8de5a7SAlexandre Belloni 	.recalc_rate	= berlin2_pll_recalc_rate,
85cf8de5a7SAlexandre Belloni };
86cf8de5a7SAlexandre Belloni 
87cf8de5a7SAlexandre Belloni struct clk * __init
88cf8de5a7SAlexandre Belloni berlin2_pll_register(const struct berlin2_pll_map *map,
89cf8de5a7SAlexandre Belloni 		     void __iomem *base, const char *name,
90cf8de5a7SAlexandre Belloni 		     const char *parent_name, unsigned long flags)
91cf8de5a7SAlexandre Belloni {
92cf8de5a7SAlexandre Belloni 	struct clk_init_data init;
93cf8de5a7SAlexandre Belloni 	struct berlin2_pll *pll;
94cf8de5a7SAlexandre Belloni 
95cf8de5a7SAlexandre Belloni 	pll = kzalloc(sizeof(*pll), GFP_KERNEL);
96cf8de5a7SAlexandre Belloni 	if (!pll)
97cf8de5a7SAlexandre Belloni 		return ERR_PTR(-ENOMEM);
98cf8de5a7SAlexandre Belloni 
99cf8de5a7SAlexandre Belloni 	/* copy pll_map to allow __initconst */
100cf8de5a7SAlexandre Belloni 	memcpy(&pll->map, map, sizeof(*map));
101cf8de5a7SAlexandre Belloni 	pll->base = base;
102cf8de5a7SAlexandre Belloni 	pll->hw.init = &init;
103cf8de5a7SAlexandre Belloni 	init.name = name;
104cf8de5a7SAlexandre Belloni 	init.ops = &berlin2_pll_ops;
105cf8de5a7SAlexandre Belloni 	init.parent_names = &parent_name;
106cf8de5a7SAlexandre Belloni 	init.num_parents = 1;
107cf8de5a7SAlexandre Belloni 	init.flags = flags;
108cf8de5a7SAlexandre Belloni 
109cf8de5a7SAlexandre Belloni 	return clk_register(NULL, &pll->hw);
110cf8de5a7SAlexandre Belloni }
111