blob: 57dea03e64b29ff34bffdfc49bdb448e2cfeb4cc [file] [log] [blame]
Carlo Caione28b9fcd2015-06-01 13:13:55 +02001/*
2 * Copyright (c) 2015 Endless Mobile, Inc.
3 * Author: Carlo Caione <carlo@endlessm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Michael Turquette55d42c42016-04-30 12:47:36 -070018#include <linux/clk.h>
Carlo Caione28b9fcd2015-06-01 13:13:55 +020019#include <linux/clk-provider.h>
20#include <linux/kernel.h>
21#include <linux/of.h>
22#include <linux/of_address.h>
23#include <linux/slab.h>
24#include <dt-bindings/clock/meson8b-clkc.h>
25
26#include "clkc.h"
27
Michael Turquette89ef4b62016-04-28 17:18:52 -070028/*
29 * Clock controller register offsets
30 *
31 * Register offsets from the HardKernel[0] data sheet are listed in comment
32 * blocks below. Those offsets must be multiplied by 4 before adding them to
33 * the base address to get the right value
34 *
35 * [0] http://dn.odroid.com/S805/Datasheet/S805_Datasheet%20V0.8%2020150126.pdf
36 */
Michael Turquette89ef4b62016-04-28 17:18:52 -070037#define MESON8B_REG_SYS_CPU_CNTL1 0x015c /* 0x57 offset in data sheet */
38#define MESON8B_REG_HHI_MPEG 0x0174 /* 0x5d offset in data sheet */
39#define MESON8B_REG_MALI 0x01b0 /* 0x6c offset in data sheet */
Carlo Caione28b9fcd2015-06-01 13:13:55 +020040#define MESON8B_REG_PLL_FIXED 0x0280
41#define MESON8B_REG_PLL_SYS 0x0300
42#define MESON8B_REG_PLL_VID 0x0320
43
Michael Turquettec0daa3e2016-04-28 12:01:51 -070044static DEFINE_SPINLOCK(clk_lock);
45
Carlo Caione28b9fcd2015-06-01 13:13:55 +020046static const struct pll_rate_table sys_pll_rate_table[] = {
47 PLL_RATE(312000000, 52, 1, 2),
48 PLL_RATE(336000000, 56, 1, 2),
49 PLL_RATE(360000000, 60, 1, 2),
50 PLL_RATE(384000000, 64, 1, 2),
51 PLL_RATE(408000000, 68, 1, 2),
52 PLL_RATE(432000000, 72, 1, 2),
53 PLL_RATE(456000000, 76, 1, 2),
54 PLL_RATE(480000000, 80, 1, 2),
55 PLL_RATE(504000000, 84, 1, 2),
56 PLL_RATE(528000000, 88, 1, 2),
57 PLL_RATE(552000000, 92, 1, 2),
58 PLL_RATE(576000000, 96, 1, 2),
59 PLL_RATE(600000000, 50, 1, 1),
60 PLL_RATE(624000000, 52, 1, 1),
61 PLL_RATE(648000000, 54, 1, 1),
62 PLL_RATE(672000000, 56, 1, 1),
63 PLL_RATE(696000000, 58, 1, 1),
64 PLL_RATE(720000000, 60, 1, 1),
65 PLL_RATE(744000000, 62, 1, 1),
66 PLL_RATE(768000000, 64, 1, 1),
67 PLL_RATE(792000000, 66, 1, 1),
68 PLL_RATE(816000000, 68, 1, 1),
69 PLL_RATE(840000000, 70, 1, 1),
70 PLL_RATE(864000000, 72, 1, 1),
71 PLL_RATE(888000000, 74, 1, 1),
72 PLL_RATE(912000000, 76, 1, 1),
73 PLL_RATE(936000000, 78, 1, 1),
74 PLL_RATE(960000000, 80, 1, 1),
75 PLL_RATE(984000000, 82, 1, 1),
76 PLL_RATE(1008000000, 84, 1, 1),
77 PLL_RATE(1032000000, 86, 1, 1),
78 PLL_RATE(1056000000, 88, 1, 1),
79 PLL_RATE(1080000000, 90, 1, 1),
80 PLL_RATE(1104000000, 92, 1, 1),
81 PLL_RATE(1128000000, 94, 1, 1),
82 PLL_RATE(1152000000, 96, 1, 1),
83 PLL_RATE(1176000000, 98, 1, 1),
84 PLL_RATE(1200000000, 50, 1, 0),
85 PLL_RATE(1224000000, 51, 1, 0),
86 PLL_RATE(1248000000, 52, 1, 0),
87 PLL_RATE(1272000000, 53, 1, 0),
88 PLL_RATE(1296000000, 54, 1, 0),
89 PLL_RATE(1320000000, 55, 1, 0),
90 PLL_RATE(1344000000, 56, 1, 0),
91 PLL_RATE(1368000000, 57, 1, 0),
92 PLL_RATE(1392000000, 58, 1, 0),
93 PLL_RATE(1416000000, 59, 1, 0),
94 PLL_RATE(1440000000, 60, 1, 0),
95 PLL_RATE(1464000000, 61, 1, 0),
96 PLL_RATE(1488000000, 62, 1, 0),
97 PLL_RATE(1512000000, 63, 1, 0),
98 PLL_RATE(1536000000, 64, 1, 0),
99 { /* sentinel */ },
100};
101
102static const struct clk_div_table cpu_div_table[] = {
103 { .val = 1, .div = 1 },
104 { .val = 2, .div = 2 },
105 { .val = 3, .div = 3 },
106 { .val = 2, .div = 4 },
107 { .val = 3, .div = 6 },
108 { .val = 4, .div = 8 },
109 { .val = 5, .div = 10 },
110 { .val = 6, .div = 12 },
111 { .val = 7, .div = 14 },
112 { .val = 8, .div = 16 },
113 { /* sentinel */ },
114};
115
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700116static struct clk_fixed_rate meson8b_xtal = {
117 .fixed_rate = 24000000,
118 .hw.init = &(struct clk_init_data){
119 .name = "xtal",
120 .num_parents = 0,
121 .ops = &clk_fixed_rate_ops,
122 },
123};
124
Michael Turquetteec623f22016-04-28 12:01:42 -0700125static struct meson_clk_pll meson8b_fixed_pll = {
126 .m = {
127 .reg_off = MESON8B_REG_PLL_FIXED,
128 .shift = 0,
129 .width = 9,
130 },
131 .n = {
132 .reg_off = MESON8B_REG_PLL_FIXED,
133 .shift = 9,
134 .width = 5,
135 },
136 .od = {
137 .reg_off = MESON8B_REG_PLL_FIXED,
138 .shift = 16,
139 .width = 2,
140 },
141 .lock = &clk_lock,
142 .hw.init = &(struct clk_init_data){
143 .name = "fixed_pll",
144 .ops = &meson_clk_pll_ro_ops,
145 .parent_names = (const char *[]){ "xtal" },
146 .num_parents = 1,
147 .flags = CLK_GET_RATE_NOCACHE,
148 },
149};
150
151static struct meson_clk_pll meson8b_vid_pll = {
152 .m = {
153 .reg_off = MESON8B_REG_PLL_VID,
154 .shift = 0,
155 .width = 9,
156 },
157 .n = {
158 .reg_off = MESON8B_REG_PLL_VID,
159 .shift = 9,
160 .width = 5,
161 },
162 .od = {
163 .reg_off = MESON8B_REG_PLL_VID,
164 .shift = 16,
165 .width = 2,
166 },
167 .lock = &clk_lock,
168 .hw.init = &(struct clk_init_data){
169 .name = "vid_pll",
170 .ops = &meson_clk_pll_ro_ops,
171 .parent_names = (const char *[]){ "xtal" },
172 .num_parents = 1,
173 .flags = CLK_GET_RATE_NOCACHE,
174 },
175};
176
177static struct meson_clk_pll meson8b_sys_pll = {
178 .m = {
179 .reg_off = MESON8B_REG_PLL_SYS,
180 .shift = 0,
181 .width = 9,
182 },
183 .n = {
184 .reg_off = MESON8B_REG_PLL_SYS,
185 .shift = 9,
186 .width = 5,
187 },
188 .od = {
189 .reg_off = MESON8B_REG_PLL_SYS,
190 .shift = 16,
191 .width = 2,
192 },
193 .rate_table = sys_pll_rate_table,
194 .rate_count = ARRAY_SIZE(sys_pll_rate_table),
195 .lock = &clk_lock,
196 .hw.init = &(struct clk_init_data){
197 .name = "sys_pll",
198 .ops = &meson_clk_pll_ops,
199 .parent_names = (const char *[]){ "xtal" },
200 .num_parents = 1,
201 .flags = CLK_GET_RATE_NOCACHE,
202 },
203};
204
Michael Turquette6282a2d2016-04-28 12:01:58 -0700205static struct clk_fixed_factor meson8b_fclk_div2 = {
206 .mult = 1,
207 .div = 2,
208 .hw.init = &(struct clk_init_data){
209 .name = "fclk_div2",
210 .ops = &clk_fixed_factor_ops,
211 .parent_names = (const char *[]){ "fixed_pll" },
212 .num_parents = 1,
213 },
214};
215
216static struct clk_fixed_factor meson8b_fclk_div3 = {
217 .mult = 1,
218 .div = 3,
219 .hw.init = &(struct clk_init_data){
220 .name = "fclk_div3",
221 .ops = &clk_fixed_factor_ops,
222 .parent_names = (const char *[]){ "fixed_pll" },
223 .num_parents = 1,
224 },
225};
226
227static struct clk_fixed_factor meson8b_fclk_div4 = {
228 .mult = 1,
229 .div = 4,
230 .hw.init = &(struct clk_init_data){
231 .name = "fclk_div4",
232 .ops = &clk_fixed_factor_ops,
233 .parent_names = (const char *[]){ "fixed_pll" },
234 .num_parents = 1,
235 },
236};
237
238static struct clk_fixed_factor meson8b_fclk_div5 = {
239 .mult = 1,
240 .div = 5,
241 .hw.init = &(struct clk_init_data){
242 .name = "fclk_div5",
243 .ops = &clk_fixed_factor_ops,
244 .parent_names = (const char *[]){ "fixed_pll" },
245 .num_parents = 1,
246 },
247};
248
249static struct clk_fixed_factor meson8b_fclk_div7 = {
250 .mult = 1,
251 .div = 7,
252 .hw.init = &(struct clk_init_data){
253 .name = "fclk_div7",
254 .ops = &clk_fixed_factor_ops,
255 .parent_names = (const char *[]){ "fixed_pll" },
256 .num_parents = 1,
257 },
258};
259
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700260/*
261 * FIXME cpu clocks and the legacy composite clocks (e.g. clk81) are both PLL
262 * post-dividers and should be modeled with their respective PLLs via the
263 * forthcoming coordinated clock rates feature
264 */
Michael Turquette55d42c42016-04-30 12:47:36 -0700265static struct meson_clk_cpu meson8b_cpu_clk = {
266 .reg_off = MESON8B_REG_SYS_CPU_CNTL1,
267 .div_table = cpu_div_table,
268 .clk_nb.notifier_call = meson_clk_cpu_notifier_cb,
269 .hw.init = &(struct clk_init_data){
270 .name = "cpu_clk",
271 .ops = &meson_clk_cpu_ops,
272 .parent_names = (const char *[]){ "sys_pll" },
273 .num_parents = 1,
274 },
275};
276
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700277static u32 mux_table_clk81[] = { 6, 5, 7 };
278
279struct clk_mux meson8b_mpeg_clk_sel = {
280 .reg = (void *)MESON8B_REG_HHI_MPEG,
281 .mask = 0x7,
282 .shift = 12,
283 .flags = CLK_MUX_READ_ONLY,
284 .table = mux_table_clk81,
285 .lock = &clk_lock,
286 .hw.init = &(struct clk_init_data){
287 .name = "mpeg_clk_sel",
288 .ops = &clk_mux_ro_ops,
289 /*
290 * FIXME bits 14:12 selects from 8 possible parents:
291 * xtal, 1'b0 (wtf), fclk_div7, mpll_clkout1, mpll_clkout2,
292 * fclk_div4, fclk_div3, fclk_div5
293 */
294 .parent_names = (const char *[]){ "fclk_div3", "fclk_div4",
295 "fclk_div5" },
296 .num_parents = 3,
297 .flags = (CLK_SET_RATE_NO_REPARENT | CLK_IGNORE_UNUSED),
298 },
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200299};
300
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700301struct clk_divider meson8b_mpeg_clk_div = {
302 .reg = (void *)MESON8B_REG_HHI_MPEG,
303 .shift = 0,
304 .width = 7,
305 .lock = &clk_lock,
306 .hw.init = &(struct clk_init_data){
307 .name = "mpeg_clk_div",
308 .ops = &clk_divider_ops,
309 .parent_names = (const char *[]){ "mpeg_clk_sel" },
310 .num_parents = 1,
311 .flags = (CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED),
312 },
313};
314
315struct clk_gate meson8b_clk81 = {
316 .reg = (void *)MESON8B_REG_HHI_MPEG,
317 .bit_idx = 7,
318 .lock = &clk_lock,
319 .hw.init = &(struct clk_init_data){
320 .name = "clk81",
321 .ops = &clk_gate_ops,
322 .parent_names = (const char *[]){ "mpeg_clk_div" },
323 .num_parents = 1,
324 .flags = (CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED),
325 },
326};
327
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700328static struct clk_hw_onecell_data meson8b_hw_onecell_data = {
329 .hws = {
330 [CLKID_XTAL] = &meson8b_xtal.hw,
Michael Turquetteec623f22016-04-28 12:01:42 -0700331 [CLKID_PLL_FIXED] = &meson8b_fixed_pll.hw,
332 [CLKID_PLL_VID] = &meson8b_vid_pll.hw,
333 [CLKID_PLL_SYS] = &meson8b_sys_pll.hw,
Michael Turquette6282a2d2016-04-28 12:01:58 -0700334 [CLKID_FCLK_DIV2] = &meson8b_fclk_div2.hw,
335 [CLKID_FCLK_DIV3] = &meson8b_fclk_div3.hw,
336 [CLKID_FCLK_DIV4] = &meson8b_fclk_div4.hw,
337 [CLKID_FCLK_DIV5] = &meson8b_fclk_div5.hw,
338 [CLKID_FCLK_DIV7] = &meson8b_fclk_div7.hw,
Michael Turquette55d42c42016-04-30 12:47:36 -0700339 [CLKID_CPUCLK] = &meson8b_cpu_clk.hw,
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700340 [CLKID_MPEG_SEL] = &meson8b_mpeg_clk_sel.hw,
341 [CLKID_MPEG_DIV] = &meson8b_mpeg_clk_div.hw,
342 [CLKID_CLK81] = &meson8b_clk81.hw,
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700343 },
344 .num = CLK_NR_CLKS,
345};
346
Michael Turquetteec623f22016-04-28 12:01:42 -0700347static struct meson_clk_pll *const meson8b_clk_plls[] = {
348 &meson8b_fixed_pll,
349 &meson8b_vid_pll,
350 &meson8b_sys_pll,
351};
352
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200353static void __init meson8b_clkc_init(struct device_node *np)
354{
355 void __iomem *clk_base;
Michael Turquetteec623f22016-04-28 12:01:42 -0700356 int ret, clkid, i;
Michael Turquette55d42c42016-04-30 12:47:36 -0700357 struct clk_hw *parent_hw;
358 struct clk *parent_clk;
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200359
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200360 /* Generic clocks and PLLs */
361 clk_base = of_iomap(np, 1);
362 if (!clk_base) {
363 pr_err("%s: Unable to map clk base\n", __func__);
364 return;
365 }
366
Michael Turquetteec623f22016-04-28 12:01:42 -0700367 /* Populate base address for PLLs */
368 for (i = 0; i < ARRAY_SIZE(meson8b_clk_plls); i++)
369 meson8b_clk_plls[i]->base = clk_base;
370
Michael Turquette55d42c42016-04-30 12:47:36 -0700371 /* Populate the base address for CPU clk */
372 meson8b_cpu_clk.base = clk_base;
373
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700374 /* Populate the base address for the MPEG clks */
375 meson8b_mpeg_clk_sel.reg = clk_base + (u32)meson8b_mpeg_clk_sel.reg;
376 meson8b_mpeg_clk_div.reg = clk_base + (u32)meson8b_mpeg_clk_div.reg;
377 meson8b_clk81.reg = clk_base + (u32)meson8b_clk81.reg;
378
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700379 /*
380 * register all clks
381 * CLKID_UNUSED = 0, so skip it and start with CLKID_XTAL = 1
382 */
383 for (clkid = CLKID_XTAL; clkid < CLK_NR_CLKS; clkid++) {
384 /* array might be sparse */
385 if (!meson8b_hw_onecell_data.hws[clkid])
386 continue;
387
388 /* FIXME convert to devm_clk_register */
389 ret = clk_hw_register(NULL, meson8b_hw_onecell_data.hws[clkid]);
390 if (ret)
391 goto unregister;
392 }
393
Michael Turquette55d42c42016-04-30 12:47:36 -0700394 /*
395 * Register CPU clk notifier
396 *
397 * FIXME this is wrong for a lot of reasons. First, the muxes should be
398 * struct clk_hw objects. Second, we shouldn't program the muxes in
399 * notifier handlers. The tricky programming sequence will be handled
400 * by the forthcoming coordinated clock rates mechanism once that
401 * feature is released.
402 *
403 * Furthermore, looking up the parent this way is terrible. At some
404 * point we will stop allocating a default struct clk when registering
405 * a new clk_hw, and this hack will no longer work. Releasing the ccr
406 * feature before that time solves the problem :-)
407 */
408 parent_hw = clk_hw_get_parent(&meson8b_cpu_clk.hw);
409 parent_clk = parent_hw->clk;
410 ret = clk_notifier_register(parent_clk, &meson8b_cpu_clk.clk_nb);
411 if (ret) {
412 pr_err("%s: failed to register clock notifier for cpu_clk\n",
413 __func__);
414 goto unregister_clk_nb;
415 }
416
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700417 of_clk_add_hw_provider(np, of_clk_hw_onecell_get, &meson8b_hw_onecell_data);
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700418 return;
419
420/* FIXME remove after converting to platform_driver/devm_clk_register */
Michael Turquette55d42c42016-04-30 12:47:36 -0700421unregister_clk_nb:
422 clk_notifier_unregister(parent_clk, &meson8b_a5_clk.clk_nb);
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700423unregister:
424 for (clkid = CLK_NR_CLKS - 1; clkid >= 0; clkid--)
425 clk_hw_unregister(meson8b_hw_onecell_data.hws[clkid]);
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200426}
427CLK_OF_DECLARE(meson8b_clock, "amlogic,meson8b-clkc", meson8b_clkc_init);