blob: cbabde2b476bf38db29d228c1fdf0ed415b1acd5 [file] [log] [blame]
Dinh Nguyen07afb8d2018-03-21 09:20:12 -05001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2017, Intel Corporation
4 */
5#include <linux/slab.h>
6#include <linux/clk-provider.h>
Stephen Boyd62e59c42019-04-18 15:20:22 -07007#include <linux/io.h>
Dinh Nguyen07afb8d2018-03-21 09:20:12 -05008
9#include "stratix10-clk.h"
10#include "clk.h"
11
12#define CLK_MGR_FREE_SHIFT 16
13#define CLK_MGR_FREE_MASK 0x7
14#define SWCTRLBTCLKSEN_SHIFT 8
15
16#define to_periph_clk(p) container_of(p, struct socfpga_periph_clk, hw.hw)
17
Dinh Nguyena0f98192021-02-12 08:30:59 -060018static unsigned long n5x_clk_peri_c_clk_recalc_rate(struct clk_hw *hwclk,
19 unsigned long parent_rate)
20{
21 struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk);
22 unsigned long div;
23 unsigned long shift = socfpgaclk->shift;
24 u32 val;
25
26 val = readl(socfpgaclk->hw.reg);
27 val &= (0x1f << shift);
28 div = (val >> shift) + 1;
29
30 return parent_rate / div;
31}
32
Dinh Nguyen07afb8d2018-03-21 09:20:12 -050033static unsigned long clk_peri_c_clk_recalc_rate(struct clk_hw *hwclk,
34 unsigned long parent_rate)
35{
36 struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk);
37 unsigned long div = 1;
38 u32 val;
39
40 val = readl(socfpgaclk->hw.reg);
41 val &= GENMASK(SWCTRLBTCLKSEN_SHIFT - 1, 0);
42 parent_rate /= val;
43
44 return parent_rate / div;
45}
46
47static unsigned long clk_peri_cnt_clk_recalc_rate(struct clk_hw *hwclk,
48 unsigned long parent_rate)
49{
50 struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk);
51 unsigned long div = 1;
52
53 if (socfpgaclk->fixed_div) {
54 div = socfpgaclk->fixed_div;
55 } else {
Dinh Nguyenc7ec75e2019-08-14 10:30:14 -050056 if (socfpgaclk->hw.reg)
Dinh Nguyen07afb8d2018-03-21 09:20:12 -050057 div = ((readl(socfpgaclk->hw.reg) & 0x7ff) + 1);
58 }
59
60 return parent_rate / div;
61}
62
63static u8 clk_periclk_get_parent(struct clk_hw *hwclk)
64{
65 struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk);
66 u32 clk_src, mask;
Dinh Nguyendfd14272021-06-10 21:52:01 -050067 u8 parent = 0;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -050068
Dinh Nguyendfd14272021-06-10 21:52:01 -050069 /* handle the bypass first */
Dinh Nguyen07afb8d2018-03-21 09:20:12 -050070 if (socfpgaclk->bypass_reg) {
71 mask = (0x1 << socfpgaclk->bypass_shift);
72 parent = ((readl(socfpgaclk->bypass_reg) & mask) >>
73 socfpgaclk->bypass_shift);
Dinh Nguyendfd14272021-06-10 21:52:01 -050074 if (parent)
75 return parent;
76 }
77
78 if (socfpgaclk->hw.reg) {
Dinh Nguyen07afb8d2018-03-21 09:20:12 -050079 clk_src = readl(socfpgaclk->hw.reg);
80 parent = (clk_src >> CLK_MGR_FREE_SHIFT) &
Dinh Nguyendfd14272021-06-10 21:52:01 -050081 CLK_MGR_FREE_MASK;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -050082 }
83 return parent;
84}
85
Dinh Nguyena0f98192021-02-12 08:30:59 -060086static const struct clk_ops n5x_peri_c_clk_ops = {
87 .recalc_rate = n5x_clk_peri_c_clk_recalc_rate,
88 .get_parent = clk_periclk_get_parent,
89};
90
Dinh Nguyen07afb8d2018-03-21 09:20:12 -050091static const struct clk_ops peri_c_clk_ops = {
92 .recalc_rate = clk_peri_c_clk_recalc_rate,
93 .get_parent = clk_periclk_get_parent,
94};
95
96static const struct clk_ops peri_cnt_clk_ops = {
97 .recalc_rate = clk_peri_cnt_clk_recalc_rate,
98 .get_parent = clk_periclk_get_parent,
99};
100
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600101struct clk_hw *s10_register_periph(const struct stratix10_perip_c_clock *clks,
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600102 void __iomem *reg)
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500103{
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600104 struct clk_hw *hw_clk;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500105 struct socfpga_periph_clk *periph_clk;
106 struct clk_init_data init;
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600107 const char *name = clks->name;
108 const char *parent_name = clks->parent_name;
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600109 int ret;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500110
111 periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL);
112 if (WARN_ON(!periph_clk))
113 return NULL;
114
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600115 periph_clk->hw.reg = reg + clks->offset;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500116
117 init.name = name;
118 init.ops = &peri_c_clk_ops;
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600119 init.flags = clks->flags;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500120
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600121 init.num_parents = clks->num_parents;
Dinh Nguyen762d9612020-05-12 13:16:43 -0500122 init.parent_names = parent_name ? &parent_name : NULL;
123 if (init.parent_names == NULL)
124 init.parent_data = clks->parent_data;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500125
126 periph_clk->hw.hw.init = &init;
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600127 hw_clk = &periph_clk->hw.hw;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500128
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600129 ret = clk_hw_register(NULL, hw_clk);
130 if (ret) {
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500131 kfree(periph_clk);
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600132 return ERR_PTR(ret);
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500133 }
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600134 return hw_clk;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500135}
136
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600137struct clk_hw *n5x_register_periph(const struct n5x_perip_c_clock *clks,
Dinh Nguyena0f98192021-02-12 08:30:59 -0600138 void __iomem *regbase)
139{
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600140 struct clk_hw *hw_clk;
Dinh Nguyena0f98192021-02-12 08:30:59 -0600141 struct socfpga_periph_clk *periph_clk;
142 struct clk_init_data init;
143 const char *name = clks->name;
144 const char *parent_name = clks->parent_name;
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600145 int ret;
Dinh Nguyena0f98192021-02-12 08:30:59 -0600146
147 periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL);
148 if (WARN_ON(!periph_clk))
149 return NULL;
150
151 periph_clk->hw.reg = regbase + clks->offset;
152 periph_clk->shift = clks->shift;
153
154 init.name = name;
155 init.ops = &n5x_peri_c_clk_ops;
156 init.flags = clks->flags;
157
158 init.num_parents = clks->num_parents;
159 init.parent_names = parent_name ? &parent_name : NULL;
160
161 periph_clk->hw.hw.init = &init;
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600162 hw_clk = &periph_clk->hw.hw;
Dinh Nguyena0f98192021-02-12 08:30:59 -0600163
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600164 ret = clk_hw_register(NULL, hw_clk);
165 if (ret) {
Dinh Nguyena0f98192021-02-12 08:30:59 -0600166 kfree(periph_clk);
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600167 return ERR_PTR(ret);
Dinh Nguyena0f98192021-02-12 08:30:59 -0600168 }
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600169 return hw_clk;
Dinh Nguyena0f98192021-02-12 08:30:59 -0600170}
171
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600172struct clk_hw *s10_register_cnt_periph(const struct stratix10_perip_cnt_clock *clks,
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600173 void __iomem *regbase)
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500174{
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600175 struct clk_hw *hw_clk;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500176 struct socfpga_periph_clk *periph_clk;
177 struct clk_init_data init;
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600178 const char *name = clks->name;
179 const char *parent_name = clks->parent_name;
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600180 int ret;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500181
182 periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL);
183 if (WARN_ON(!periph_clk))
184 return NULL;
185
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600186 if (clks->offset)
187 periph_clk->hw.reg = regbase + clks->offset;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500188 else
189 periph_clk->hw.reg = NULL;
190
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600191 if (clks->bypass_reg)
192 periph_clk->bypass_reg = regbase + clks->bypass_reg;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500193 else
194 periph_clk->bypass_reg = NULL;
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600195 periph_clk->bypass_shift = clks->bypass_shift;
196 periph_clk->fixed_div = clks->fixed_divider;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500197
198 init.name = name;
199 init.ops = &peri_cnt_clk_ops;
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600200 init.flags = clks->flags;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500201
Dinh Nguyen8c0e7832020-01-14 10:07:26 -0600202 init.num_parents = clks->num_parents;
Dinh Nguyen762d9612020-05-12 13:16:43 -0500203 init.parent_names = parent_name ? &parent_name : NULL;
204 if (init.parent_names == NULL)
205 init.parent_data = clks->parent_data;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500206
207 periph_clk->hw.hw.init = &init;
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600208 hw_clk = &periph_clk->hw.hw;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500209
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600210 ret = clk_hw_register(NULL, hw_clk);
211 if (ret) {
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500212 kfree(periph_clk);
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600213 return ERR_PTR(ret);
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500214 }
Dinh Nguyenba7e2582021-03-02 15:41:51 -0600215 return hw_clk;
Dinh Nguyen07afb8d2018-03-21 09:20:12 -0500216}