1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 #include <linux/clkdev.h>
8 #include <linux/clk-provider.h>
9 #include <linux/delay.h>
12 #include <linux/clk/tegra.h>
13 #include <linux/reset-controller.h>
15 #include <soc/tegra/fuse.h>
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;
23 int *periph_clk_enb_refcnt;
24 static int periph_banks;
25 static struct clk **clks;
27 static struct clk_onecell_data clk_data;
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;
34 static const struct tegra_clk_periph_regs periph_regs[] = {
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,
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,
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,
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,
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,
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,
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,
93 static void __iomem *clk_base;
95 static int tegra_clk_rst_assert(struct reset_controller_dev *rcdev,
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
107 if (id < periph_banks * 32) {
108 writel_relaxed(BIT(id % 32),
109 clk_base + periph_regs[id / 32].rst_set_reg);
111 } else if (id < periph_banks * 32 + num_special_reset) {
112 return special_reset_assert(id);
118 static int tegra_clk_rst_deassert(struct reset_controller_dev *rcdev,
121 if (id < periph_banks * 32) {
122 writel_relaxed(BIT(id % 32),
123 clk_base + periph_regs[id / 32].rst_clr_reg);
125 } else if (id < periph_banks * 32 + num_special_reset) {
126 return special_reset_deassert(id);
132 static int tegra_clk_rst_reset(struct reset_controller_dev *rcdev,
137 err = tegra_clk_rst_assert(rcdev, id);
143 return tegra_clk_rst_deassert(rcdev, id);
146 const struct tegra_clk_periph_regs *get_reg_bank(int clkid)
148 int reg_bank = clkid / 32;
150 if (reg_bank < periph_banks)
151 return &periph_regs[reg_bank];
158 void tegra_clk_set_pllp_out_cpu(bool enable)
162 val = readl_relaxed(clk_base + CLK_OUT_ENB_Y);
164 val |= CLK_ENB_PLLP_OUT_CPU;
166 val &= ~CLK_ENB_PLLP_OUT_CPU;
168 writel_relaxed(val, clk_base + CLK_OUT_ENB_Y);
171 struct clk ** __init tegra_clk_init(void __iomem *regs, int num, int banks)
175 if (WARN_ON(banks > ARRAY_SIZE(periph_regs)))
178 periph_clk_enb_refcnt = kcalloc(32 * banks,
179 sizeof(*periph_clk_enb_refcnt),
181 if (!periph_clk_enb_refcnt)
184 periph_banks = banks;
186 clks = kcalloc(num, sizeof(struct clk *), GFP_KERNEL);
188 kfree(periph_clk_enb_refcnt);
195 void __init tegra_init_dup_clks(struct tegra_clk_duplicate *dup_list,
196 struct clk *clks[], int clk_max)
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);
207 void __init tegra_init_from_table(struct tegra_clk_init_table *tbl,
208 struct clk *clks[], int clk_max)
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);
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));
233 if (clk_set_rate(clk, tbl->rate)) {
234 pr_err("%s: Failed to set rate %lu of %s\n",
236 __clk_get_name(clk));
241 if (clk_prepare_enable(clk)) {
242 pr_err("%s: Failed to enable %s\n", __func__,
243 __clk_get_name(clk));
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,
255 static struct reset_controller_dev rst_ctlr = {
257 .owner = THIS_MODULE,
258 .of_reset_n_cells = 1,
261 void __init tegra_add_of_provider(struct device_node *np,
262 void *clk_src_onecell_get)
266 for (i = 0; i < clk_num; i++) {
267 if (IS_ERR(clks[i])) {
269 ("Tegra clk %d: register failed with %ld\n",
270 i, PTR_ERR(clks[i]));
273 clks[i] = ERR_PTR(-EINVAL);
276 clk_data.clks = clks;
277 clk_data.clk_num = clk_num;
278 of_clk_add_provider(np, clk_src_onecell_get, &clk_data);
280 rst_ctlr.of_node = np;
281 rst_ctlr.nr_resets = periph_banks * 32 + num_special_reset;
282 reset_controller_register(&rst_ctlr);
285 void __init tegra_init_special_resets(unsigned int num,
286 int (*assert)(unsigned long),
287 int (*deassert)(unsigned long))
289 num_special_reset = num;
290 special_reset_assert = assert;
291 special_reset_deassert = deassert;
294 void __init tegra_register_devclks(struct tegra_devclk *dev_clks, int num)
298 for (i = 0; i < num; i++, dev_clks++)
299 clk_register_clkdev(clks[dev_clks->dt_id], dev_clks->con_id,
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]),
309 struct clk ** __init tegra_lookup_dt_id(int clk_id,
310 struct tegra_clk *tegra_clk)
312 if (tegra_clk[clk_id].present)
313 return &clks[tegra_clk[clk_id].dt_id];
318 tegra_clk_apply_init_table_func tegra_clk_apply_init_table;
320 static int __init tegra_clocks_apply_init_table(void)
322 if (!tegra_clk_apply_init_table)
325 tegra_clk_apply_init_table();
329 arch_initcall(tegra_clocks_apply_init_table);