1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Microchip LAN966x SoC Clock driver.
5 * Copyright (C) 2021 Microchip Technology, Inc. and its subsidiaries
7 * Author: Kavyasree Kotagiri <kavyasree.kotagiri@microchip.com>
10 #include <linux/bitfield.h>
11 #include <linux/clk-provider.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
19 #include <dt-bindings/clock/microchip,lan966x.h>
21 #define GCK_ENA BIT(0)
22 #define GCK_SRC_SEL GENMASK(9, 8)
23 #define GCK_PRESCALER GENMASK(23, 16)
27 static const char *clk_names[N_CLOCKS] = {
28 "qspi0", "qspi1", "qspi2", "sdmmc0",
29 "pi", "mcan0", "mcan1", "flexcom0",
30 "flexcom1", "flexcom2", "flexcom3",
31 "flexcom4", "timer1", "usb_refclk",
38 #define to_lan966x_gck(hw) container_of(hw, struct lan966x_gck, hw)
40 static const struct clk_parent_data lan966x_gck_pdata[] = {
41 { .fw_name = "cpu", },
42 { .fw_name = "ddr", },
43 { .fw_name = "sys", },
46 static struct clk_init_data init = {
47 .parent_data = lan966x_gck_pdata,
48 .num_parents = ARRAY_SIZE(lan966x_gck_pdata),
51 struct clk_gate_soc_desc {
56 static const struct clk_gate_soc_desc clk_gate_desc[] = {
64 static DEFINE_SPINLOCK(clk_gate_lock);
65 static void __iomem *base;
67 static int lan966x_gck_enable(struct clk_hw *hw)
69 struct lan966x_gck *gck = to_lan966x_gck(hw);
70 u32 val = readl(gck->reg);
73 writel(val, gck->reg);
78 static void lan966x_gck_disable(struct clk_hw *hw)
80 struct lan966x_gck *gck = to_lan966x_gck(hw);
81 u32 val = readl(gck->reg);
84 writel(val, gck->reg);
87 static int lan966x_gck_set_rate(struct clk_hw *hw,
89 unsigned long parent_rate)
91 struct lan966x_gck *gck = to_lan966x_gck(hw);
92 u32 div, val = readl(gck->reg);
94 if (rate == 0 || parent_rate == 0)
98 div = parent_rate / rate;
99 val &= ~GCK_PRESCALER;
100 val |= FIELD_PREP(GCK_PRESCALER, (div - 1));
101 writel(val, gck->reg);
106 static long lan966x_gck_round_rate(struct clk_hw *hw, unsigned long rate,
107 unsigned long *parent_rate)
111 if (rate == 0 || *parent_rate == 0)
114 if (rate >= *parent_rate)
117 div = DIV_ROUND_CLOSEST(*parent_rate, rate);
119 return *parent_rate / div;
122 static unsigned long lan966x_gck_recalc_rate(struct clk_hw *hw,
123 unsigned long parent_rate)
125 struct lan966x_gck *gck = to_lan966x_gck(hw);
126 u32 div, val = readl(gck->reg);
128 div = FIELD_GET(GCK_PRESCALER, val);
130 return parent_rate / (div + 1);
133 static int lan966x_gck_determine_rate(struct clk_hw *hw,
134 struct clk_rate_request *req)
136 struct clk_hw *parent;
139 for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
140 parent = clk_hw_get_parent_by_index(hw, i);
144 /* Allowed prescaler divider range is 0-255 */
145 if (clk_hw_get_rate(parent) / req->rate <= DIV_MAX) {
146 req->best_parent_hw = parent;
147 req->best_parent_rate = clk_hw_get_rate(parent);
156 static u8 lan966x_gck_get_parent(struct clk_hw *hw)
158 struct lan966x_gck *gck = to_lan966x_gck(hw);
159 u32 val = readl(gck->reg);
161 return FIELD_GET(GCK_SRC_SEL, val);
164 static int lan966x_gck_set_parent(struct clk_hw *hw, u8 index)
166 struct lan966x_gck *gck = to_lan966x_gck(hw);
167 u32 val = readl(gck->reg);
170 val |= FIELD_PREP(GCK_SRC_SEL, index);
171 writel(val, gck->reg);
176 static const struct clk_ops lan966x_gck_ops = {
177 .enable = lan966x_gck_enable,
178 .disable = lan966x_gck_disable,
179 .set_rate = lan966x_gck_set_rate,
180 .round_rate = lan966x_gck_round_rate,
181 .recalc_rate = lan966x_gck_recalc_rate,
182 .determine_rate = lan966x_gck_determine_rate,
183 .set_parent = lan966x_gck_set_parent,
184 .get_parent = lan966x_gck_get_parent,
187 static struct clk_hw *lan966x_gck_clk_register(struct device *dev, int i)
189 struct lan966x_gck *priv;
192 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
194 return ERR_PTR(-ENOMEM);
196 priv->reg = base + (i * 4);
197 priv->hw.init = &init;
198 ret = devm_clk_hw_register(dev, &priv->hw);
205 static int lan966x_gate_clk_register(struct device *dev,
206 struct clk_hw_onecell_data *hw_data,
207 void __iomem *gate_base)
211 for (i = GCK_GATE_UHPHS; i < N_CLOCKS; ++i) {
212 int idx = i - GCK_GATE_UHPHS;
215 devm_clk_hw_register_gate(dev, clk_gate_desc[idx].name,
217 clk_gate_desc[idx].bit_idx,
220 if (IS_ERR(hw_data->hws[i]))
221 return dev_err_probe(dev, PTR_ERR(hw_data->hws[i]),
222 "failed to register %s clock\n",
223 clk_gate_desc[idx].name);
229 static int lan966x_clk_probe(struct platform_device *pdev)
231 struct clk_hw_onecell_data *hw_data;
232 struct device *dev = &pdev->dev;
233 void __iomem *gate_base;
234 struct resource *res;
237 hw_data = devm_kzalloc(dev, struct_size(hw_data, hws, N_CLOCKS),
242 base = devm_platform_ioremap_resource(pdev, 0);
244 return PTR_ERR(base);
246 init.ops = &lan966x_gck_ops;
248 hw_data->num = GCK_GATE_UHPHS;
250 for (i = 0; i < GCK_GATE_UHPHS; i++) {
251 init.name = clk_names[i];
252 hw_data->hws[i] = lan966x_gck_clk_register(dev, i);
253 if (IS_ERR(hw_data->hws[i])) {
254 dev_err(dev, "failed to register %s clock\n",
256 return PTR_ERR(hw_data->hws[i]);
260 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
262 gate_base = devm_ioremap_resource(&pdev->dev, res);
263 if (IS_ERR(gate_base))
264 return PTR_ERR(gate_base);
266 hw_data->num = N_CLOCKS;
268 ret = lan966x_gate_clk_register(dev, hw_data, gate_base);
273 return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, hw_data);
276 static const struct of_device_id lan966x_clk_dt_ids[] = {
277 { .compatible = "microchip,lan966x-gck", },
280 MODULE_DEVICE_TABLE(of, lan966x_clk_dt_ids);
282 static struct platform_driver lan966x_clk_driver = {
283 .probe = lan966x_clk_probe,
285 .name = "lan966x-clk",
286 .of_match_table = lan966x_clk_dt_ids,
289 builtin_platform_driver(lan966x_clk_driver);
291 MODULE_AUTHOR("Kavyasree Kotagiri <kavyasree.kotagiri@microchip.com>");
292 MODULE_DESCRIPTION("LAN966X clock driver");
293 MODULE_LICENSE("GPL v2");