xref: /openbmc/linux/drivers/clk/imgtec/clk-boston.c (revision c95baf12f5077419db01313ab61c2aac007d40cd)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
26b0fd6c1SPaul Burton /*
36b0fd6c1SPaul Burton  * Copyright (C) 2016-2017 Imagination Technologies
4fb615d61SPaul Burton  * Author: Paul Burton <paul.burton@mips.com>
56b0fd6c1SPaul Burton  */
66b0fd6c1SPaul Burton 
76b0fd6c1SPaul Burton #define pr_fmt(fmt) "clk-boston: " fmt
86b0fd6c1SPaul Burton 
96b0fd6c1SPaul Burton #include <linux/clk-provider.h>
106b0fd6c1SPaul Burton #include <linux/kernel.h>
116b0fd6c1SPaul Burton #include <linux/of.h>
126b0fd6c1SPaul Burton #include <linux/regmap.h>
136b0fd6c1SPaul Burton #include <linux/slab.h>
146b0fd6c1SPaul Burton #include <linux/mfd/syscon.h>
156b0fd6c1SPaul Burton 
166b0fd6c1SPaul Burton #include <dt-bindings/clock/boston-clock.h>
176b0fd6c1SPaul Burton 
186b0fd6c1SPaul Burton #define BOSTON_PLAT_MMCMDIV		0x30
196b0fd6c1SPaul Burton # define BOSTON_PLAT_MMCMDIV_CLK0DIV	(0xff << 0)
206b0fd6c1SPaul Burton # define BOSTON_PLAT_MMCMDIV_INPUT	(0xff << 8)
216b0fd6c1SPaul Burton # define BOSTON_PLAT_MMCMDIV_MUL	(0xff << 16)
226b0fd6c1SPaul Burton # define BOSTON_PLAT_MMCMDIV_CLK1DIV	(0xff << 24)
236b0fd6c1SPaul Burton 
246b0fd6c1SPaul Burton #define BOSTON_CLK_COUNT 3
256b0fd6c1SPaul Burton 
ext_field(u32 val,u32 mask)266b0fd6c1SPaul Burton static u32 ext_field(u32 val, u32 mask)
276b0fd6c1SPaul Burton {
286b0fd6c1SPaul Burton 	return (val & mask) >> (ffs(mask) - 1);
296b0fd6c1SPaul Burton }
306b0fd6c1SPaul Burton 
clk_boston_setup(struct device_node * np)316b0fd6c1SPaul Burton static void __init clk_boston_setup(struct device_node *np)
326b0fd6c1SPaul Burton {
336b0fd6c1SPaul Burton 	unsigned long in_freq, cpu_freq, sys_freq;
346b0fd6c1SPaul Burton 	uint mmcmdiv, mul, cpu_div, sys_div;
356b0fd6c1SPaul Burton 	struct clk_hw_onecell_data *onecell;
366b0fd6c1SPaul Burton 	struct regmap *regmap;
376b0fd6c1SPaul Burton 	struct clk_hw *hw;
386b0fd6c1SPaul Burton 	int err;
396b0fd6c1SPaul Burton 
406b0fd6c1SPaul Burton 	regmap = syscon_node_to_regmap(np->parent);
416b0fd6c1SPaul Burton 	if (IS_ERR(regmap)) {
426b0fd6c1SPaul Burton 		pr_err("failed to find regmap\n");
436b0fd6c1SPaul Burton 		return;
446b0fd6c1SPaul Burton 	}
456b0fd6c1SPaul Burton 
466b0fd6c1SPaul Burton 	err = regmap_read(regmap, BOSTON_PLAT_MMCMDIV, &mmcmdiv);
476b0fd6c1SPaul Burton 	if (err) {
486b0fd6c1SPaul Burton 		pr_err("failed to read mmcm_div register: %d\n", err);
496b0fd6c1SPaul Burton 		return;
506b0fd6c1SPaul Burton 	}
516b0fd6c1SPaul Burton 
526b0fd6c1SPaul Burton 	in_freq = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_INPUT) * 1000000;
536b0fd6c1SPaul Burton 	mul = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_MUL);
546b0fd6c1SPaul Burton 
556b0fd6c1SPaul Burton 	sys_div = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_CLK0DIV);
566b0fd6c1SPaul Burton 	sys_freq = mult_frac(in_freq, mul, sys_div);
576b0fd6c1SPaul Burton 
586b0fd6c1SPaul Burton 	cpu_div = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_CLK1DIV);
596b0fd6c1SPaul Burton 	cpu_freq = mult_frac(in_freq, mul, cpu_div);
606b0fd6c1SPaul Burton 
61*e620a1e0SStephen Kitt 	onecell = kzalloc(struct_size(onecell, hws, BOSTON_CLK_COUNT),
626b0fd6c1SPaul Burton 			  GFP_KERNEL);
636b0fd6c1SPaul Burton 	if (!onecell)
646b0fd6c1SPaul Burton 		return;
656b0fd6c1SPaul Burton 
666b0fd6c1SPaul Burton 	onecell->num = BOSTON_CLK_COUNT;
676b0fd6c1SPaul Burton 
686b0fd6c1SPaul Burton 	hw = clk_hw_register_fixed_rate(NULL, "input", NULL, 0, in_freq);
696b0fd6c1SPaul Burton 	if (IS_ERR(hw)) {
706b0fd6c1SPaul Burton 		pr_err("failed to register input clock: %ld\n", PTR_ERR(hw));
718b627f61SYi Wang 		goto fail_input;
726b0fd6c1SPaul Burton 	}
736b0fd6c1SPaul Burton 	onecell->hws[BOSTON_CLK_INPUT] = hw;
746b0fd6c1SPaul Burton 
756b0fd6c1SPaul Burton 	hw = clk_hw_register_fixed_rate(NULL, "sys", "input", 0, sys_freq);
766b0fd6c1SPaul Burton 	if (IS_ERR(hw)) {
776b0fd6c1SPaul Burton 		pr_err("failed to register sys clock: %ld\n", PTR_ERR(hw));
788b627f61SYi Wang 		goto fail_sys;
796b0fd6c1SPaul Burton 	}
806b0fd6c1SPaul Burton 	onecell->hws[BOSTON_CLK_SYS] = hw;
816b0fd6c1SPaul Burton 
826b0fd6c1SPaul Burton 	hw = clk_hw_register_fixed_rate(NULL, "cpu", "input", 0, cpu_freq);
836b0fd6c1SPaul Burton 	if (IS_ERR(hw)) {
846b0fd6c1SPaul Burton 		pr_err("failed to register cpu clock: %ld\n", PTR_ERR(hw));
858b627f61SYi Wang 		goto fail_cpu;
866b0fd6c1SPaul Burton 	}
876b0fd6c1SPaul Burton 	onecell->hws[BOSTON_CLK_CPU] = hw;
886b0fd6c1SPaul Burton 
896b0fd6c1SPaul Burton 	err = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, onecell);
908b627f61SYi Wang 	if (err) {
916b0fd6c1SPaul Burton 		pr_err("failed to add DT provider: %d\n", err);
928b627f61SYi Wang 		goto fail_clk_add;
938b627f61SYi Wang 	}
9446fda5b5SYi Wang 
9546fda5b5SYi Wang 	return;
9646fda5b5SYi Wang 
978b627f61SYi Wang fail_clk_add:
988b627f61SYi Wang 	clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_CPU]);
998b627f61SYi Wang fail_cpu:
1008b627f61SYi Wang 	clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_SYS]);
1018b627f61SYi Wang fail_sys:
1028b627f61SYi Wang 	clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_INPUT]);
1038b627f61SYi Wang fail_input:
10446fda5b5SYi Wang 	kfree(onecell);
1056b0fd6c1SPaul Burton }
1066b0fd6c1SPaul Burton 
1076b0fd6c1SPaul Burton /*
1086b0fd6c1SPaul Burton  * Use CLK_OF_DECLARE so that this driver is probed early enough to provide the
1096b0fd6c1SPaul Burton  * CPU frequency for use with the GIC or cop0 counters/timers.
1106b0fd6c1SPaul Burton  */
1116b0fd6c1SPaul Burton CLK_OF_DECLARE(clk_boston, "img,boston-clock", clk_boston_setup);
112