blob: 8145c4efc381ffa211da406ca57ce124aec982c4 [file] [log] [blame]
Sebastian Hesselbartha4518402013-05-11 03:08:02 +02001/*
2 * Marvell EBU SoC common clock handling
3 *
4 * Copyright (C) 2012 Marvell
5 *
6 * Gregory CLEMENT <gregory.clement@free-electrons.com>
7 * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
8 * Andrew Lunn <andrew@lunn.ch>
9 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 */
14
15#include <linux/kernel.h>
16#include <linux/clk.h>
17#include <linux/clkdev.h>
18#include <linux/clk-provider.h>
19#include <linux/io.h>
20#include <linux/of.h>
21#include <linux/of_address.h>
22
23#include "common.h"
24
25/*
26 * Core Clocks
27 */
28
29static struct clk_onecell_data clk_data;
30
31void __init mvebu_coreclk_setup(struct device_node *np,
32 const struct coreclk_soc_desc *desc)
33{
34 const char *tclk_name = "tclk";
35 const char *cpuclk_name = "cpuclk";
36 void __iomem *base;
37 unsigned long rate;
38 int n;
39
40 base = of_iomap(np, 0);
41 if (WARN_ON(!base))
42 return;
43
44 /* Allocate struct for TCLK, cpu clk, and core ratio clocks */
45 clk_data.clk_num = 2 + desc->num_ratios;
46 clk_data.clks = kzalloc(clk_data.clk_num * sizeof(struct clk *),
47 GFP_KERNEL);
Jisheng Zhangf98d0072013-08-23 10:34:01 +080048 if (WARN_ON(!clk_data.clks)) {
49 iounmap(base);
Sebastian Hesselbartha4518402013-05-11 03:08:02 +020050 return;
Jisheng Zhangf98d0072013-08-23 10:34:01 +080051 }
Sebastian Hesselbartha4518402013-05-11 03:08:02 +020052
53 /* Register TCLK */
54 of_property_read_string_index(np, "clock-output-names", 0,
55 &tclk_name);
56 rate = desc->get_tclk_freq(base);
57 clk_data.clks[0] = clk_register_fixed_rate(NULL, tclk_name, NULL,
58 CLK_IS_ROOT, rate);
59 WARN_ON(IS_ERR(clk_data.clks[0]));
60
61 /* Register CPU clock */
62 of_property_read_string_index(np, "clock-output-names", 1,
63 &cpuclk_name);
64 rate = desc->get_cpu_freq(base);
65 clk_data.clks[1] = clk_register_fixed_rate(NULL, cpuclk_name, NULL,
66 CLK_IS_ROOT, rate);
67 WARN_ON(IS_ERR(clk_data.clks[1]));
68
69 /* Register fixed-factor clocks derived from CPU clock */
70 for (n = 0; n < desc->num_ratios; n++) {
71 const char *rclk_name = desc->ratios[n].name;
72 int mult, div;
73
74 of_property_read_string_index(np, "clock-output-names",
75 2+n, &rclk_name);
76 desc->get_clk_ratio(base, desc->ratios[n].id, &mult, &div);
77 clk_data.clks[2+n] = clk_register_fixed_factor(NULL, rclk_name,
78 cpuclk_name, 0, mult, div);
79 WARN_ON(IS_ERR(clk_data.clks[2+n]));
80 };
81
82 /* SAR register isn't needed anymore */
83 iounmap(base);
84
85 of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
86}
87
88/*
89 * Clock Gating Control
90 */
91
Mike Turquette87e39212014-08-27 15:36:37 -070092DEFINE_SPINLOCK(ctrl_gating_lock);
93
Sebastian Hesselbartha4518402013-05-11 03:08:02 +020094struct clk_gating_ctrl {
Mike Turquette87e39212014-08-27 15:36:37 -070095 spinlock_t *lock;
Sebastian Hesselbartha4518402013-05-11 03:08:02 +020096 struct clk **gates;
97 int num_gates;
98};
99
100#define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
101
102static struct clk *clk_gating_get_src(
103 struct of_phandle_args *clkspec, void *data)
104{
105 struct clk_gating_ctrl *ctrl = (struct clk_gating_ctrl *)data;
106 int n;
107
108 if (clkspec->args_count < 1)
109 return ERR_PTR(-EINVAL);
110
111 for (n = 0; n < ctrl->num_gates; n++) {
112 struct clk_gate *gate =
113 to_clk_gate(__clk_get_hw(ctrl->gates[n]));
114 if (clkspec->args[0] == gate->bit_idx)
115 return ctrl->gates[n];
116 }
117 return ERR_PTR(-ENODEV);
118}
119
120void __init mvebu_clk_gating_setup(struct device_node *np,
121 const struct clk_gating_soc_desc *desc)
122{
123 struct clk_gating_ctrl *ctrl;
124 struct clk *clk;
125 void __iomem *base;
126 const char *default_parent = NULL;
127 int n;
128
129 base = of_iomap(np, 0);
130 if (WARN_ON(!base))
131 return;
132
133 clk = of_clk_get(np, 0);
134 if (!IS_ERR(clk)) {
135 default_parent = __clk_get_name(clk);
136 clk_put(clk);
137 }
138
139 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
140 if (WARN_ON(!ctrl))
Jisheng Zhangf98d0072013-08-23 10:34:01 +0800141 goto ctrl_out;
Sebastian Hesselbartha4518402013-05-11 03:08:02 +0200142
Mike Turquette87e39212014-08-27 15:36:37 -0700143 /* lock must already be initialized */
144 ctrl->lock = &ctrl_gating_lock;
Sebastian Hesselbartha4518402013-05-11 03:08:02 +0200145
146 /* Count, allocate, and register clock gates */
147 for (n = 0; desc[n].name;)
148 n++;
149
150 ctrl->num_gates = n;
151 ctrl->gates = kzalloc(ctrl->num_gates * sizeof(struct clk *),
152 GFP_KERNEL);
Jisheng Zhangf98d0072013-08-23 10:34:01 +0800153 if (WARN_ON(!ctrl->gates))
154 goto gates_out;
Sebastian Hesselbartha4518402013-05-11 03:08:02 +0200155
156 for (n = 0; n < ctrl->num_gates; n++) {
157 const char *parent =
158 (desc[n].parent) ? desc[n].parent : default_parent;
159 ctrl->gates[n] = clk_register_gate(NULL, desc[n].name, parent,
160 desc[n].flags, base, desc[n].bit_idx,
Mike Turquette87e39212014-08-27 15:36:37 -0700161 0, ctrl->lock);
Sebastian Hesselbartha4518402013-05-11 03:08:02 +0200162 WARN_ON(IS_ERR(ctrl->gates[n]));
163 }
164
165 of_clk_add_provider(np, clk_gating_get_src, ctrl);
Jisheng Zhangf98d0072013-08-23 10:34:01 +0800166
167 return;
168gates_out:
169 kfree(ctrl);
170ctrl_out:
171 iounmap(base);
Sebastian Hesselbartha4518402013-05-11 03:08:02 +0200172}