162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2016-2017 Imagination Technologies 462306a36Sopenharmony_ci * Author: Paul Burton <paul.burton@mips.com> 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#define pr_fmt(fmt) "clk-boston: " fmt 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <linux/clk-provider.h> 1062306a36Sopenharmony_ci#include <linux/kernel.h> 1162306a36Sopenharmony_ci#include <linux/of.h> 1262306a36Sopenharmony_ci#include <linux/regmap.h> 1362306a36Sopenharmony_ci#include <linux/slab.h> 1462306a36Sopenharmony_ci#include <linux/mfd/syscon.h> 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci#include <dt-bindings/clock/boston-clock.h> 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci#define BOSTON_PLAT_MMCMDIV 0x30 1962306a36Sopenharmony_ci# define BOSTON_PLAT_MMCMDIV_CLK0DIV (0xff << 0) 2062306a36Sopenharmony_ci# define BOSTON_PLAT_MMCMDIV_INPUT (0xff << 8) 2162306a36Sopenharmony_ci# define BOSTON_PLAT_MMCMDIV_MUL (0xff << 16) 2262306a36Sopenharmony_ci# define BOSTON_PLAT_MMCMDIV_CLK1DIV (0xff << 24) 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci#define BOSTON_CLK_COUNT 3 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_cistatic u32 ext_field(u32 val, u32 mask) 2762306a36Sopenharmony_ci{ 2862306a36Sopenharmony_ci return (val & mask) >> (ffs(mask) - 1); 2962306a36Sopenharmony_ci} 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_cistatic void __init clk_boston_setup(struct device_node *np) 3262306a36Sopenharmony_ci{ 3362306a36Sopenharmony_ci unsigned long in_freq, cpu_freq, sys_freq; 3462306a36Sopenharmony_ci uint mmcmdiv, mul, cpu_div, sys_div; 3562306a36Sopenharmony_ci struct clk_hw_onecell_data *onecell; 3662306a36Sopenharmony_ci struct regmap *regmap; 3762306a36Sopenharmony_ci struct clk_hw *hw; 3862306a36Sopenharmony_ci int err; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci regmap = syscon_node_to_regmap(np->parent); 4162306a36Sopenharmony_ci if (IS_ERR(regmap)) { 4262306a36Sopenharmony_ci pr_err("failed to find regmap\n"); 4362306a36Sopenharmony_ci return; 4462306a36Sopenharmony_ci } 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci err = regmap_read(regmap, BOSTON_PLAT_MMCMDIV, &mmcmdiv); 4762306a36Sopenharmony_ci if (err) { 4862306a36Sopenharmony_ci pr_err("failed to read mmcm_div register: %d\n", err); 4962306a36Sopenharmony_ci return; 5062306a36Sopenharmony_ci } 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci in_freq = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_INPUT) * 1000000; 5362306a36Sopenharmony_ci mul = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_MUL); 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci sys_div = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_CLK0DIV); 5662306a36Sopenharmony_ci sys_freq = mult_frac(in_freq, mul, sys_div); 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci cpu_div = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_CLK1DIV); 5962306a36Sopenharmony_ci cpu_freq = mult_frac(in_freq, mul, cpu_div); 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci onecell = kzalloc(struct_size(onecell, hws, BOSTON_CLK_COUNT), 6262306a36Sopenharmony_ci GFP_KERNEL); 6362306a36Sopenharmony_ci if (!onecell) 6462306a36Sopenharmony_ci return; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci onecell->num = BOSTON_CLK_COUNT; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci hw = clk_hw_register_fixed_rate(NULL, "input", NULL, 0, in_freq); 6962306a36Sopenharmony_ci if (IS_ERR(hw)) { 7062306a36Sopenharmony_ci pr_err("failed to register input clock: %ld\n", PTR_ERR(hw)); 7162306a36Sopenharmony_ci goto fail_input; 7262306a36Sopenharmony_ci } 7362306a36Sopenharmony_ci onecell->hws[BOSTON_CLK_INPUT] = hw; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci hw = clk_hw_register_fixed_rate(NULL, "sys", "input", 0, sys_freq); 7662306a36Sopenharmony_ci if (IS_ERR(hw)) { 7762306a36Sopenharmony_ci pr_err("failed to register sys clock: %ld\n", PTR_ERR(hw)); 7862306a36Sopenharmony_ci goto fail_sys; 7962306a36Sopenharmony_ci } 8062306a36Sopenharmony_ci onecell->hws[BOSTON_CLK_SYS] = hw; 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci hw = clk_hw_register_fixed_rate(NULL, "cpu", "input", 0, cpu_freq); 8362306a36Sopenharmony_ci if (IS_ERR(hw)) { 8462306a36Sopenharmony_ci pr_err("failed to register cpu clock: %ld\n", PTR_ERR(hw)); 8562306a36Sopenharmony_ci goto fail_cpu; 8662306a36Sopenharmony_ci } 8762306a36Sopenharmony_ci onecell->hws[BOSTON_CLK_CPU] = hw; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci err = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, onecell); 9062306a36Sopenharmony_ci if (err) { 9162306a36Sopenharmony_ci pr_err("failed to add DT provider: %d\n", err); 9262306a36Sopenharmony_ci goto fail_clk_add; 9362306a36Sopenharmony_ci } 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci return; 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_cifail_clk_add: 9862306a36Sopenharmony_ci clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_CPU]); 9962306a36Sopenharmony_cifail_cpu: 10062306a36Sopenharmony_ci clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_SYS]); 10162306a36Sopenharmony_cifail_sys: 10262306a36Sopenharmony_ci clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_INPUT]); 10362306a36Sopenharmony_cifail_input: 10462306a36Sopenharmony_ci kfree(onecell); 10562306a36Sopenharmony_ci} 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci/* 10862306a36Sopenharmony_ci * Use CLK_OF_DECLARE so that this driver is probed early enough to provide the 10962306a36Sopenharmony_ci * CPU frequency for use with the GIC or cop0 counters/timers. 11062306a36Sopenharmony_ci */ 11162306a36Sopenharmony_ciCLK_OF_DECLARE(clk_boston, "img,boston-clock", clk_boston_setup); 112