19e288cefSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
2b3a33077SSimon Horman /*
3b3a33077SSimon Horman * r8a7740 Core CPG Clocks
4b3a33077SSimon Horman *
5b3a33077SSimon Horman * Copyright (C) 2014 Ulrich Hecht
6b3a33077SSimon Horman */
7b3a33077SSimon Horman
8b3a33077SSimon Horman #include <linux/clk-provider.h>
909c32427SSimon Horman #include <linux/clk/renesas.h>
10b3a33077SSimon Horman #include <linux/init.h>
1162e59c4eSStephen Boyd #include <linux/io.h>
12b3a33077SSimon Horman #include <linux/kernel.h>
13b3a33077SSimon Horman #include <linux/slab.h>
14b3a33077SSimon Horman #include <linux/of.h>
15b3a33077SSimon Horman #include <linux/of_address.h>
16b3a33077SSimon Horman #include <linux/spinlock.h>
17b3a33077SSimon Horman
18b3a33077SSimon Horman struct r8a7740_cpg {
19b3a33077SSimon Horman struct clk_onecell_data data;
20b3a33077SSimon Horman spinlock_t lock;
21b3a33077SSimon Horman };
22b3a33077SSimon Horman
23b3a33077SSimon Horman #define CPG_FRQCRA 0x00
24b3a33077SSimon Horman #define CPG_FRQCRB 0x04
25b3a33077SSimon Horman #define CPG_PLLC2CR 0x2c
26b3a33077SSimon Horman #define CPG_USBCKCR 0x8c
27b3a33077SSimon Horman #define CPG_FRQCRC 0xe0
28b3a33077SSimon Horman
29b3a33077SSimon Horman #define CLK_ENABLE_ON_INIT BIT(0)
30b3a33077SSimon Horman
31b3a33077SSimon Horman struct div4_clk {
32b3a33077SSimon Horman const char *name;
33b3a33077SSimon Horman unsigned int reg;
34b3a33077SSimon Horman unsigned int shift;
35b3a33077SSimon Horman int flags;
36b3a33077SSimon Horman };
37b3a33077SSimon Horman
38b3a33077SSimon Horman static struct div4_clk div4_clks[] = {
39b3a33077SSimon Horman { "i", CPG_FRQCRA, 20, CLK_ENABLE_ON_INIT },
40b3a33077SSimon Horman { "zg", CPG_FRQCRA, 16, CLK_ENABLE_ON_INIT },
41b3a33077SSimon Horman { "b", CPG_FRQCRA, 8, CLK_ENABLE_ON_INIT },
42b3a33077SSimon Horman { "m1", CPG_FRQCRA, 4, CLK_ENABLE_ON_INIT },
43b3a33077SSimon Horman { "hp", CPG_FRQCRB, 4, 0 },
44b3a33077SSimon Horman { "hpp", CPG_FRQCRC, 20, 0 },
45b3a33077SSimon Horman { "usbp", CPG_FRQCRC, 16, 0 },
46b3a33077SSimon Horman { "s", CPG_FRQCRC, 12, 0 },
47b3a33077SSimon Horman { "zb", CPG_FRQCRC, 8, 0 },
48b3a33077SSimon Horman { "m3", CPG_FRQCRC, 4, 0 },
49b3a33077SSimon Horman { "cp", CPG_FRQCRC, 0, 0 },
50b3a33077SSimon Horman { NULL, 0, 0, 0 },
51b3a33077SSimon Horman };
52b3a33077SSimon Horman
53b3a33077SSimon Horman static const struct clk_div_table div4_div_table[] = {
54b3a33077SSimon Horman { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 }, { 4, 8 }, { 5, 12 },
55b3a33077SSimon Horman { 6, 16 }, { 7, 18 }, { 8, 24 }, { 9, 32 }, { 10, 36 }, { 11, 48 },
56b3a33077SSimon Horman { 13, 72 }, { 14, 96 }, { 0, 0 }
57b3a33077SSimon Horman };
58b3a33077SSimon Horman
59b3a33077SSimon Horman static u32 cpg_mode __initdata;
60b3a33077SSimon Horman
61b3a33077SSimon Horman static struct clk * __init
r8a7740_cpg_register_clock(struct device_node * np,struct r8a7740_cpg * cpg,void __iomem * base,const char * name)62b3a33077SSimon Horman r8a7740_cpg_register_clock(struct device_node *np, struct r8a7740_cpg *cpg,
63*a00d077aSGeert Uytterhoeven void __iomem *base, const char *name)
64b3a33077SSimon Horman {
65b3a33077SSimon Horman const struct clk_div_table *table = NULL;
66b3a33077SSimon Horman const char *parent_name;
67b3a33077SSimon Horman unsigned int shift, reg;
68b3a33077SSimon Horman unsigned int mult = 1;
69b3a33077SSimon Horman unsigned int div = 1;
70b3a33077SSimon Horman
71b3a33077SSimon Horman if (!strcmp(name, "r")) {
72b3a33077SSimon Horman switch (cpg_mode & (BIT(2) | BIT(1))) {
73b3a33077SSimon Horman case BIT(1) | BIT(2):
74b3a33077SSimon Horman /* extal1 */
75b3a33077SSimon Horman parent_name = of_clk_get_parent_name(np, 0);
76b3a33077SSimon Horman div = 2048;
77b3a33077SSimon Horman break;
78b3a33077SSimon Horman case BIT(2):
79b3a33077SSimon Horman /* extal1 */
80b3a33077SSimon Horman parent_name = of_clk_get_parent_name(np, 0);
81b3a33077SSimon Horman div = 1024;
82b3a33077SSimon Horman break;
83b3a33077SSimon Horman default:
84b3a33077SSimon Horman /* extalr */
85b3a33077SSimon Horman parent_name = of_clk_get_parent_name(np, 2);
86b3a33077SSimon Horman break;
87b3a33077SSimon Horman }
88b3a33077SSimon Horman } else if (!strcmp(name, "system")) {
89b3a33077SSimon Horman parent_name = of_clk_get_parent_name(np, 0);
90b3a33077SSimon Horman if (cpg_mode & BIT(1))
91b3a33077SSimon Horman div = 2;
92b3a33077SSimon Horman } else if (!strcmp(name, "pllc0")) {
93b3a33077SSimon Horman /* PLLC0/1 are configurable multiplier clocks. Register them as
94b3a33077SSimon Horman * fixed factor clocks for now as there's no generic multiplier
95b3a33077SSimon Horman * clock implementation and we currently have no need to change
96b3a33077SSimon Horman * the multiplier value.
97b3a33077SSimon Horman */
98*a00d077aSGeert Uytterhoeven u32 value = readl(base + CPG_FRQCRC);
99b3a33077SSimon Horman parent_name = "system";
100b3a33077SSimon Horman mult = ((value >> 24) & 0x7f) + 1;
101b3a33077SSimon Horman } else if (!strcmp(name, "pllc1")) {
102*a00d077aSGeert Uytterhoeven u32 value = readl(base + CPG_FRQCRA);
103b3a33077SSimon Horman parent_name = "system";
104b3a33077SSimon Horman mult = ((value >> 24) & 0x7f) + 1;
105b3a33077SSimon Horman div = 2;
106b3a33077SSimon Horman } else if (!strcmp(name, "pllc2")) {
107*a00d077aSGeert Uytterhoeven u32 value = readl(base + CPG_PLLC2CR);
108b3a33077SSimon Horman parent_name = "system";
109b3a33077SSimon Horman mult = ((value >> 24) & 0x3f) + 1;
110b3a33077SSimon Horman } else if (!strcmp(name, "usb24s")) {
111*a00d077aSGeert Uytterhoeven u32 value = readl(base + CPG_USBCKCR);
112b3a33077SSimon Horman if (value & BIT(7))
113b3a33077SSimon Horman /* extal2 */
114b3a33077SSimon Horman parent_name = of_clk_get_parent_name(np, 1);
115b3a33077SSimon Horman else
116b3a33077SSimon Horman parent_name = "system";
117b3a33077SSimon Horman if (!(value & BIT(6)))
118b3a33077SSimon Horman div = 2;
119b3a33077SSimon Horman } else {
120b3a33077SSimon Horman struct div4_clk *c;
121b3a33077SSimon Horman for (c = div4_clks; c->name; c++) {
122b3a33077SSimon Horman if (!strcmp(name, c->name)) {
123b3a33077SSimon Horman parent_name = "pllc1";
124b3a33077SSimon Horman table = div4_div_table;
125b3a33077SSimon Horman reg = c->reg;
126b3a33077SSimon Horman shift = c->shift;
127b3a33077SSimon Horman break;
128b3a33077SSimon Horman }
129b3a33077SSimon Horman }
130b3a33077SSimon Horman if (!c->name)
131b3a33077SSimon Horman return ERR_PTR(-EINVAL);
132b3a33077SSimon Horman }
133b3a33077SSimon Horman
134b3a33077SSimon Horman if (!table) {
135b3a33077SSimon Horman return clk_register_fixed_factor(NULL, name, parent_name, 0,
136b3a33077SSimon Horman mult, div);
137b3a33077SSimon Horman } else {
138b3a33077SSimon Horman return clk_register_divider_table(NULL, name, parent_name, 0,
139*a00d077aSGeert Uytterhoeven base + reg, shift, 4, 0,
140b3a33077SSimon Horman table, &cpg->lock);
141b3a33077SSimon Horman }
142b3a33077SSimon Horman }
143b3a33077SSimon Horman
r8a7740_cpg_clocks_init(struct device_node * np)144b3a33077SSimon Horman static void __init r8a7740_cpg_clocks_init(struct device_node *np)
145b3a33077SSimon Horman {
146b3a33077SSimon Horman struct r8a7740_cpg *cpg;
147*a00d077aSGeert Uytterhoeven void __iomem *base;
148b3a33077SSimon Horman struct clk **clks;
149b3a33077SSimon Horman unsigned int i;
150b3a33077SSimon Horman int num_clks;
151b3a33077SSimon Horman
152b3a33077SSimon Horman if (of_property_read_u32(np, "renesas,mode", &cpg_mode))
153b3a33077SSimon Horman pr_warn("%s: missing renesas,mode property\n", __func__);
154b3a33077SSimon Horman
155b3a33077SSimon Horman num_clks = of_property_count_strings(np, "clock-output-names");
156b3a33077SSimon Horman if (num_clks < 0) {
157b3a33077SSimon Horman pr_err("%s: failed to count clocks\n", __func__);
158b3a33077SSimon Horman return;
159b3a33077SSimon Horman }
160b3a33077SSimon Horman
161b3a33077SSimon Horman cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
1626396bb22SKees Cook clks = kcalloc(num_clks, sizeof(*clks), GFP_KERNEL);
163b3a33077SSimon Horman if (cpg == NULL || clks == NULL) {
164b3a33077SSimon Horman /* We're leaking memory on purpose, there's no point in cleaning
165b3a33077SSimon Horman * up as the system won't boot anyway.
166b3a33077SSimon Horman */
167b3a33077SSimon Horman return;
168b3a33077SSimon Horman }
169b3a33077SSimon Horman
170b3a33077SSimon Horman spin_lock_init(&cpg->lock);
171b3a33077SSimon Horman
172b3a33077SSimon Horman cpg->data.clks = clks;
173b3a33077SSimon Horman cpg->data.clk_num = num_clks;
174b3a33077SSimon Horman
175*a00d077aSGeert Uytterhoeven base = of_iomap(np, 0);
176*a00d077aSGeert Uytterhoeven if (WARN_ON(base == NULL))
177b3a33077SSimon Horman return;
178b3a33077SSimon Horman
179b3a33077SSimon Horman for (i = 0; i < num_clks; ++i) {
180b3a33077SSimon Horman const char *name;
181b3a33077SSimon Horman struct clk *clk;
182b3a33077SSimon Horman
183b3a33077SSimon Horman of_property_read_string_index(np, "clock-output-names", i,
184b3a33077SSimon Horman &name);
185b3a33077SSimon Horman
186*a00d077aSGeert Uytterhoeven clk = r8a7740_cpg_register_clock(np, cpg, base, name);
187b3a33077SSimon Horman if (IS_ERR(clk))
188e665f029SRob Herring pr_err("%s: failed to register %pOFn %s clock (%ld)\n",
189e665f029SRob Herring __func__, np, name, PTR_ERR(clk));
190b3a33077SSimon Horman else
191b3a33077SSimon Horman cpg->data.clks[i] = clk;
192b3a33077SSimon Horman }
193b3a33077SSimon Horman
194b3a33077SSimon Horman of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
195b3a33077SSimon Horman }
196b3a33077SSimon Horman CLK_OF_DECLARE(r8a7740_cpg_clks, "renesas,r8a7740-cpg-clocks",
197b3a33077SSimon Horman r8a7740_cpg_clocks_init);
198