xref: /openbmc/linux/drivers/clk/imgtec/clk-boston.c (revision 2874c5fd284268364ece81a7bd936f3c8168e567)
1*2874c5fdSThomas 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 
266b0fd6c1SPaul Burton static u32 ext_field(u32 val, u32 mask)
276b0fd6c1SPaul Burton {
286b0fd6c1SPaul Burton 	return (val & mask) >> (ffs(mask) - 1);
296b0fd6c1SPaul Burton }
306b0fd6c1SPaul Burton 
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 
616b0fd6c1SPaul Burton 	onecell = kzalloc(sizeof(*onecell) +
626b0fd6c1SPaul Burton 			  (BOSTON_CLK_COUNT * sizeof(struct clk_hw *)),
636b0fd6c1SPaul Burton 			  GFP_KERNEL);
646b0fd6c1SPaul Burton 	if (!onecell)
656b0fd6c1SPaul Burton 		return;
666b0fd6c1SPaul Burton 
676b0fd6c1SPaul Burton 	onecell->num = BOSTON_CLK_COUNT;
686b0fd6c1SPaul Burton 
696b0fd6c1SPaul Burton 	hw = clk_hw_register_fixed_rate(NULL, "input", NULL, 0, in_freq);
706b0fd6c1SPaul Burton 	if (IS_ERR(hw)) {
716b0fd6c1SPaul Burton 		pr_err("failed to register input clock: %ld\n", PTR_ERR(hw));
728b627f61SYi Wang 		goto fail_input;
736b0fd6c1SPaul Burton 	}
746b0fd6c1SPaul Burton 	onecell->hws[BOSTON_CLK_INPUT] = hw;
756b0fd6c1SPaul Burton 
766b0fd6c1SPaul Burton 	hw = clk_hw_register_fixed_rate(NULL, "sys", "input", 0, sys_freq);
776b0fd6c1SPaul Burton 	if (IS_ERR(hw)) {
786b0fd6c1SPaul Burton 		pr_err("failed to register sys clock: %ld\n", PTR_ERR(hw));
798b627f61SYi Wang 		goto fail_sys;
806b0fd6c1SPaul Burton 	}
816b0fd6c1SPaul Burton 	onecell->hws[BOSTON_CLK_SYS] = hw;
826b0fd6c1SPaul Burton 
836b0fd6c1SPaul Burton 	hw = clk_hw_register_fixed_rate(NULL, "cpu", "input", 0, cpu_freq);
846b0fd6c1SPaul Burton 	if (IS_ERR(hw)) {
856b0fd6c1SPaul Burton 		pr_err("failed to register cpu clock: %ld\n", PTR_ERR(hw));
868b627f61SYi Wang 		goto fail_cpu;
876b0fd6c1SPaul Burton 	}
886b0fd6c1SPaul Burton 	onecell->hws[BOSTON_CLK_CPU] = hw;
896b0fd6c1SPaul Burton 
906b0fd6c1SPaul Burton 	err = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, onecell);
918b627f61SYi Wang 	if (err) {
926b0fd6c1SPaul Burton 		pr_err("failed to add DT provider: %d\n", err);
938b627f61SYi Wang 		goto fail_clk_add;
948b627f61SYi Wang 	}
9546fda5b5SYi Wang 
9646fda5b5SYi Wang 	return;
9746fda5b5SYi Wang 
988b627f61SYi Wang fail_clk_add:
998b627f61SYi Wang 	clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_CPU]);
1008b627f61SYi Wang fail_cpu:
1018b627f61SYi Wang 	clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_SYS]);
1028b627f61SYi Wang fail_sys:
1038b627f61SYi Wang 	clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_INPUT]);
1048b627f61SYi Wang fail_input:
10546fda5b5SYi Wang 	kfree(onecell);
1066b0fd6c1SPaul Burton }
1076b0fd6c1SPaul Burton 
1086b0fd6c1SPaul Burton /*
1096b0fd6c1SPaul Burton  * Use CLK_OF_DECLARE so that this driver is probed early enough to provide the
1106b0fd6c1SPaul Burton  * CPU frequency for use with the GIC or cop0 counters/timers.
1116b0fd6c1SPaul Burton  */
1126b0fd6c1SPaul Burton CLK_OF_DECLARE(clk_boston, "img,boston-clock", clk_boston_setup);
113