]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/clk/tegra/clk.c
clk: tegra: Share clk and rst register defines with Tegra clock driver
[linux.git] / drivers / clk / tegra / clk.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
4  */
5
6 #include <linux/clkdev.h>
7 #include <linux/clk.h>
8 #include <linux/clk-provider.h>
9 #include <linux/delay.h>
10 #include <linux/io.h>
11 #include <linux/of.h>
12 #include <linux/clk/tegra.h>
13 #include <linux/reset-controller.h>
14
15 #include <soc/tegra/fuse.h>
16
17 #include "clk.h"
18
19 /* Global data of Tegra CPU CAR ops */
20 static struct tegra_cpu_car_ops dummy_car_ops;
21 struct tegra_cpu_car_ops *tegra_cpu_car_ops = &dummy_car_ops;
22
23 int *periph_clk_enb_refcnt;
24 static int periph_banks;
25 static struct clk **clks;
26 static int clk_num;
27 static struct clk_onecell_data clk_data;
28
29 /* Handlers for SoC-specific reset lines */
30 static int (*special_reset_assert)(unsigned long);
31 static int (*special_reset_deassert)(unsigned long);
32 static unsigned int num_special_reset;
33
34 static const struct tegra_clk_periph_regs periph_regs[] = {
35         [0] = {
36                 .enb_reg = CLK_OUT_ENB_L,
37                 .enb_set_reg = CLK_OUT_ENB_SET_L,
38                 .enb_clr_reg = CLK_OUT_ENB_CLR_L,
39                 .rst_reg = RST_DEVICES_L,
40                 .rst_set_reg = RST_DEVICES_SET_L,
41                 .rst_clr_reg = RST_DEVICES_CLR_L,
42         },
43         [1] = {
44                 .enb_reg = CLK_OUT_ENB_H,
45                 .enb_set_reg = CLK_OUT_ENB_SET_H,
46                 .enb_clr_reg = CLK_OUT_ENB_CLR_H,
47                 .rst_reg = RST_DEVICES_H,
48                 .rst_set_reg = RST_DEVICES_SET_H,
49                 .rst_clr_reg = RST_DEVICES_CLR_H,
50         },
51         [2] = {
52                 .enb_reg = CLK_OUT_ENB_U,
53                 .enb_set_reg = CLK_OUT_ENB_SET_U,
54                 .enb_clr_reg = CLK_OUT_ENB_CLR_U,
55                 .rst_reg = RST_DEVICES_U,
56                 .rst_set_reg = RST_DEVICES_SET_U,
57                 .rst_clr_reg = RST_DEVICES_CLR_U,
58         },
59         [3] = {
60                 .enb_reg = CLK_OUT_ENB_V,
61                 .enb_set_reg = CLK_OUT_ENB_SET_V,
62                 .enb_clr_reg = CLK_OUT_ENB_CLR_V,
63                 .rst_reg = RST_DEVICES_V,
64                 .rst_set_reg = RST_DEVICES_SET_V,
65                 .rst_clr_reg = RST_DEVICES_CLR_V,
66         },
67         [4] = {
68                 .enb_reg = CLK_OUT_ENB_W,
69                 .enb_set_reg = CLK_OUT_ENB_SET_W,
70                 .enb_clr_reg = CLK_OUT_ENB_CLR_W,
71                 .rst_reg = RST_DEVICES_W,
72                 .rst_set_reg = RST_DEVICES_SET_W,
73                 .rst_clr_reg = RST_DEVICES_CLR_W,
74         },
75         [5] = {
76                 .enb_reg = CLK_OUT_ENB_X,
77                 .enb_set_reg = CLK_OUT_ENB_SET_X,
78                 .enb_clr_reg = CLK_OUT_ENB_CLR_X,
79                 .rst_reg = RST_DEVICES_X,
80                 .rst_set_reg = RST_DEVICES_SET_X,
81                 .rst_clr_reg = RST_DEVICES_CLR_X,
82         },
83         [6] = {
84                 .enb_reg = CLK_OUT_ENB_Y,
85                 .enb_set_reg = CLK_OUT_ENB_SET_Y,
86                 .enb_clr_reg = CLK_OUT_ENB_CLR_Y,
87                 .rst_reg = RST_DEVICES_Y,
88                 .rst_set_reg = RST_DEVICES_SET_Y,
89                 .rst_clr_reg = RST_DEVICES_CLR_Y,
90         },
91 };
92
93 static void __iomem *clk_base;
94
95 static int tegra_clk_rst_assert(struct reset_controller_dev *rcdev,
96                 unsigned long id)
97 {
98         /*
99          * If peripheral is on the APB bus then we must read the APB bus to
100          * flush the write operation in apb bus. This will avoid peripheral
101          * access after disabling clock. Since the reset driver has no
102          * knowledge of which reset IDs represent which devices, simply do
103          * this all the time.
104          */
105         tegra_read_chipid();
106
107         if (id < periph_banks * 32) {
108                 writel_relaxed(BIT(id % 32),
109                                clk_base + periph_regs[id / 32].rst_set_reg);
110                 return 0;
111         } else if (id < periph_banks * 32 + num_special_reset) {
112                 return special_reset_assert(id);
113         }
114
115         return -EINVAL;
116 }
117
118 static int tegra_clk_rst_deassert(struct reset_controller_dev *rcdev,
119                 unsigned long id)
120 {
121         if (id < periph_banks * 32) {
122                 writel_relaxed(BIT(id % 32),
123                                clk_base + periph_regs[id / 32].rst_clr_reg);
124                 return 0;
125         } else if (id < periph_banks * 32 + num_special_reset) {
126                 return special_reset_deassert(id);
127         }
128
129         return -EINVAL;
130 }
131
132 static int tegra_clk_rst_reset(struct reset_controller_dev *rcdev,
133                 unsigned long id)
134 {
135         int err;
136
137         err = tegra_clk_rst_assert(rcdev, id);
138         if (err)
139                 return err;
140
141         udelay(1);
142
143         return tegra_clk_rst_deassert(rcdev, id);
144 }
145
146 const struct tegra_clk_periph_regs *get_reg_bank(int clkid)
147 {
148         int reg_bank = clkid / 32;
149
150         if (reg_bank < periph_banks)
151                 return &periph_regs[reg_bank];
152         else {
153                 WARN_ON(1);
154                 return NULL;
155         }
156 }
157
158 void tegra_clk_set_pllp_out_cpu(bool enable)
159 {
160         u32 val;
161
162         val = readl_relaxed(clk_base + CLK_OUT_ENB_Y);
163         if (enable)
164                 val |= CLK_ENB_PLLP_OUT_CPU;
165         else
166                 val &= ~CLK_ENB_PLLP_OUT_CPU;
167
168         writel_relaxed(val, clk_base + CLK_OUT_ENB_Y);
169 }
170
171 struct clk ** __init tegra_clk_init(void __iomem *regs, int num, int banks)
172 {
173         clk_base = regs;
174
175         if (WARN_ON(banks > ARRAY_SIZE(periph_regs)))
176                 return NULL;
177
178         periph_clk_enb_refcnt = kcalloc(32 * banks,
179                                         sizeof(*periph_clk_enb_refcnt),
180                                         GFP_KERNEL);
181         if (!periph_clk_enb_refcnt)
182                 return NULL;
183
184         periph_banks = banks;
185
186         clks = kcalloc(num, sizeof(struct clk *), GFP_KERNEL);
187         if (!clks)
188                 kfree(periph_clk_enb_refcnt);
189
190         clk_num = num;
191
192         return clks;
193 }
194
195 void __init tegra_init_dup_clks(struct tegra_clk_duplicate *dup_list,
196                                 struct clk *clks[], int clk_max)
197 {
198         struct clk *clk;
199
200         for (; dup_list->clk_id < clk_max; dup_list++) {
201                 clk = clks[dup_list->clk_id];
202                 dup_list->lookup.clk = clk;
203                 clkdev_add(&dup_list->lookup);
204         }
205 }
206
207 void __init tegra_init_from_table(struct tegra_clk_init_table *tbl,
208                                   struct clk *clks[], int clk_max)
209 {
210         struct clk *clk;
211
212         for (; tbl->clk_id < clk_max; tbl++) {
213                 clk = clks[tbl->clk_id];
214                 if (IS_ERR_OR_NULL(clk)) {
215                         pr_err("%s: invalid entry %ld in clks array for id %d\n",
216                                __func__, PTR_ERR(clk), tbl->clk_id);
217                         WARN_ON(1);
218
219                         continue;
220                 }
221
222                 if (tbl->parent_id < clk_max) {
223                         struct clk *parent = clks[tbl->parent_id];
224                         if (clk_set_parent(clk, parent)) {
225                                 pr_err("%s: Failed to set parent %s of %s\n",
226                                        __func__, __clk_get_name(parent),
227                                        __clk_get_name(clk));
228                                 WARN_ON(1);
229                         }
230                 }
231
232                 if (tbl->rate)
233                         if (clk_set_rate(clk, tbl->rate)) {
234                                 pr_err("%s: Failed to set rate %lu of %s\n",
235                                        __func__, tbl->rate,
236                                        __clk_get_name(clk));
237                                 WARN_ON(1);
238                         }
239
240                 if (tbl->state)
241                         if (clk_prepare_enable(clk)) {
242                                 pr_err("%s: Failed to enable %s\n", __func__,
243                                        __clk_get_name(clk));
244                                 WARN_ON(1);
245                         }
246         }
247 }
248
249 static const struct reset_control_ops rst_ops = {
250         .assert = tegra_clk_rst_assert,
251         .deassert = tegra_clk_rst_deassert,
252         .reset = tegra_clk_rst_reset,
253 };
254
255 static struct reset_controller_dev rst_ctlr = {
256         .ops = &rst_ops,
257         .owner = THIS_MODULE,
258         .of_reset_n_cells = 1,
259 };
260
261 void __init tegra_add_of_provider(struct device_node *np,
262                                   void *clk_src_onecell_get)
263 {
264         int i;
265
266         for (i = 0; i < clk_num; i++) {
267                 if (IS_ERR(clks[i])) {
268                         pr_err
269                             ("Tegra clk %d: register failed with %ld\n",
270                              i, PTR_ERR(clks[i]));
271                 }
272                 if (!clks[i])
273                         clks[i] = ERR_PTR(-EINVAL);
274         }
275
276         clk_data.clks = clks;
277         clk_data.clk_num = clk_num;
278         of_clk_add_provider(np, clk_src_onecell_get, &clk_data);
279
280         rst_ctlr.of_node = np;
281         rst_ctlr.nr_resets = periph_banks * 32 + num_special_reset;
282         reset_controller_register(&rst_ctlr);
283 }
284
285 void __init tegra_init_special_resets(unsigned int num,
286                                       int (*assert)(unsigned long),
287                                       int (*deassert)(unsigned long))
288 {
289         num_special_reset = num;
290         special_reset_assert = assert;
291         special_reset_deassert = deassert;
292 }
293
294 void __init tegra_register_devclks(struct tegra_devclk *dev_clks, int num)
295 {
296         int i;
297
298         for (i = 0; i < num; i++, dev_clks++)
299                 clk_register_clkdev(clks[dev_clks->dt_id], dev_clks->con_id,
300                                 dev_clks->dev_id);
301
302         for (i = 0; i < clk_num; i++) {
303                 if (!IS_ERR_OR_NULL(clks[i]))
304                         clk_register_clkdev(clks[i], __clk_get_name(clks[i]),
305                                 "tegra-clk-debug");
306         }
307 }
308
309 struct clk ** __init tegra_lookup_dt_id(int clk_id,
310                                         struct tegra_clk *tegra_clk)
311 {
312         if (tegra_clk[clk_id].present)
313                 return &clks[tegra_clk[clk_id].dt_id];
314         else
315                 return NULL;
316 }
317
318 tegra_clk_apply_init_table_func tegra_clk_apply_init_table;
319
320 static int __init tegra_clocks_apply_init_table(void)
321 {
322         if (!tegra_clk_apply_init_table)
323                 return 0;
324
325         tegra_clk_apply_init_table();
326
327         return 0;
328 }
329 arch_initcall(tegra_clocks_apply_init_table);