18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2014 Marvell Technology Group Ltd. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Alexandre Belloni <alexandre.belloni@free-electrons.com> 68c2ecf20Sopenharmony_ci * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 98c2ecf20Sopenharmony_ci#include <linux/io.h> 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/of.h> 128c2ecf20Sopenharmony_ci#include <linux/of_address.h> 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci#include <asm/div64.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include "berlin2-div.h" 178c2ecf20Sopenharmony_ci#include "berlin2-pll.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cistruct berlin2_pll { 208c2ecf20Sopenharmony_ci struct clk_hw hw; 218c2ecf20Sopenharmony_ci void __iomem *base; 228c2ecf20Sopenharmony_ci struct berlin2_pll_map map; 238c2ecf20Sopenharmony_ci}; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#define to_berlin2_pll(hw) container_of(hw, struct berlin2_pll, hw) 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define SPLL_CTRL0 0x00 288c2ecf20Sopenharmony_ci#define SPLL_CTRL1 0x04 298c2ecf20Sopenharmony_ci#define SPLL_CTRL2 0x08 308c2ecf20Sopenharmony_ci#define SPLL_CTRL3 0x0c 318c2ecf20Sopenharmony_ci#define SPLL_CTRL4 0x10 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define FBDIV_MASK 0x1ff 348c2ecf20Sopenharmony_ci#define RFDIV_MASK 0x1f 358c2ecf20Sopenharmony_ci#define DIVSEL_MASK 0xf 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/* 388c2ecf20Sopenharmony_ci * The output frequency formula for the pll is: 398c2ecf20Sopenharmony_ci * clkout = fbdiv / refdiv * parent / vcodiv 408c2ecf20Sopenharmony_ci */ 418c2ecf20Sopenharmony_cistatic unsigned long 428c2ecf20Sopenharmony_ciberlin2_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci struct berlin2_pll *pll = to_berlin2_pll(hw); 458c2ecf20Sopenharmony_ci struct berlin2_pll_map *map = &pll->map; 468c2ecf20Sopenharmony_ci u32 val, fbdiv, rfdiv, vcodivsel, vcodiv; 478c2ecf20Sopenharmony_ci u64 rate = parent_rate; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci val = readl_relaxed(pll->base + SPLL_CTRL0); 508c2ecf20Sopenharmony_ci fbdiv = (val >> map->fbdiv_shift) & FBDIV_MASK; 518c2ecf20Sopenharmony_ci rfdiv = (val >> map->rfdiv_shift) & RFDIV_MASK; 528c2ecf20Sopenharmony_ci if (rfdiv == 0) { 538c2ecf20Sopenharmony_ci pr_warn("%s has zero rfdiv\n", clk_hw_get_name(hw)); 548c2ecf20Sopenharmony_ci rfdiv = 1; 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci val = readl_relaxed(pll->base + SPLL_CTRL1); 588c2ecf20Sopenharmony_ci vcodivsel = (val >> map->divsel_shift) & DIVSEL_MASK; 598c2ecf20Sopenharmony_ci vcodiv = map->vcodiv[vcodivsel]; 608c2ecf20Sopenharmony_ci if (vcodiv == 0) { 618c2ecf20Sopenharmony_ci pr_warn("%s has zero vcodiv (index %d)\n", 628c2ecf20Sopenharmony_ci clk_hw_get_name(hw), vcodivsel); 638c2ecf20Sopenharmony_ci vcodiv = 1; 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci rate *= fbdiv * map->mult; 678c2ecf20Sopenharmony_ci do_div(rate, rfdiv * vcodiv); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci return (unsigned long)rate; 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic const struct clk_ops berlin2_pll_ops = { 738c2ecf20Sopenharmony_ci .recalc_rate = berlin2_pll_recalc_rate, 748c2ecf20Sopenharmony_ci}; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ciint __init 778c2ecf20Sopenharmony_ciberlin2_pll_register(const struct berlin2_pll_map *map, 788c2ecf20Sopenharmony_ci void __iomem *base, const char *name, 798c2ecf20Sopenharmony_ci const char *parent_name, unsigned long flags) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci struct clk_init_data init; 828c2ecf20Sopenharmony_ci struct berlin2_pll *pll; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci pll = kzalloc(sizeof(*pll), GFP_KERNEL); 858c2ecf20Sopenharmony_ci if (!pll) 868c2ecf20Sopenharmony_ci return -ENOMEM; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci /* copy pll_map to allow __initconst */ 898c2ecf20Sopenharmony_ci memcpy(&pll->map, map, sizeof(*map)); 908c2ecf20Sopenharmony_ci pll->base = base; 918c2ecf20Sopenharmony_ci pll->hw.init = &init; 928c2ecf20Sopenharmony_ci init.name = name; 938c2ecf20Sopenharmony_ci init.ops = &berlin2_pll_ops; 948c2ecf20Sopenharmony_ci init.parent_names = &parent_name; 958c2ecf20Sopenharmony_ci init.num_parents = 1; 968c2ecf20Sopenharmony_ci init.flags = flags; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci return clk_hw_register(NULL, &pll->hw); 998c2ecf20Sopenharmony_ci} 100