18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * OMAP4-specific DPLL control functions 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2011 Texas Instruments, Inc. 68c2ecf20Sopenharmony_ci * Rajendra Nayak 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/kernel.h> 108c2ecf20Sopenharmony_ci#include <linux/errno.h> 118c2ecf20Sopenharmony_ci#include <linux/clk.h> 128c2ecf20Sopenharmony_ci#include <linux/io.h> 138c2ecf20Sopenharmony_ci#include <linux/bitops.h> 148c2ecf20Sopenharmony_ci#include <linux/clk/ti.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include "clock.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* 198c2ecf20Sopenharmony_ci * Maximum DPLL input frequency (FINT) and output frequency (FOUT) that 208c2ecf20Sopenharmony_ci * can supported when using the DPLL low-power mode. Frequencies are 218c2ecf20Sopenharmony_ci * defined in OMAP4430/60 Public TRM section 3.6.3.3.2 "Enable Control, 228c2ecf20Sopenharmony_ci * Status, and Low-Power Operation Mode". 238c2ecf20Sopenharmony_ci */ 248c2ecf20Sopenharmony_ci#define OMAP4_DPLL_LP_FINT_MAX 1000000 258c2ecf20Sopenharmony_ci#define OMAP4_DPLL_LP_FOUT_MAX 100000000 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci/* 288c2ecf20Sopenharmony_ci * Bitfield declarations 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ci#define OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK BIT(8) 318c2ecf20Sopenharmony_ci#define OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK BIT(10) 328c2ecf20Sopenharmony_ci#define OMAP4430_DPLL_REGM4XEN_MASK BIT(11) 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* Static rate multiplier for OMAP4 REGM4XEN clocks */ 358c2ecf20Sopenharmony_ci#define OMAP4430_REGM4XEN_MULT 4 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistatic void omap4_dpllmx_allow_gatectrl(struct clk_hw_omap *clk) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci u32 v; 408c2ecf20Sopenharmony_ci u32 mask; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci if (!clk) 438c2ecf20Sopenharmony_ci return; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci mask = clk->flags & CLOCK_CLKOUTX2 ? 468c2ecf20Sopenharmony_ci OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK : 478c2ecf20Sopenharmony_ci OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci v = ti_clk_ll_ops->clk_readl(&clk->clksel_reg); 508c2ecf20Sopenharmony_ci /* Clear the bit to allow gatectrl */ 518c2ecf20Sopenharmony_ci v &= ~mask; 528c2ecf20Sopenharmony_ci ti_clk_ll_ops->clk_writel(v, &clk->clksel_reg); 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic void omap4_dpllmx_deny_gatectrl(struct clk_hw_omap *clk) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci u32 v; 588c2ecf20Sopenharmony_ci u32 mask; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci if (!clk) 618c2ecf20Sopenharmony_ci return; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci mask = clk->flags & CLOCK_CLKOUTX2 ? 648c2ecf20Sopenharmony_ci OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK : 658c2ecf20Sopenharmony_ci OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci v = ti_clk_ll_ops->clk_readl(&clk->clksel_reg); 688c2ecf20Sopenharmony_ci /* Set the bit to deny gatectrl */ 698c2ecf20Sopenharmony_ci v |= mask; 708c2ecf20Sopenharmony_ci ti_clk_ll_ops->clk_writel(v, &clk->clksel_reg); 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ciconst struct clk_hw_omap_ops clkhwops_omap4_dpllmx = { 748c2ecf20Sopenharmony_ci .allow_idle = omap4_dpllmx_allow_gatectrl, 758c2ecf20Sopenharmony_ci .deny_idle = omap4_dpllmx_deny_gatectrl, 768c2ecf20Sopenharmony_ci}; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci/** 798c2ecf20Sopenharmony_ci * omap4_dpll_lpmode_recalc - compute DPLL low-power setting 808c2ecf20Sopenharmony_ci * @dd: pointer to the dpll data structure 818c2ecf20Sopenharmony_ci * 828c2ecf20Sopenharmony_ci * Calculates if low-power mode can be enabled based upon the last 838c2ecf20Sopenharmony_ci * multiplier and divider values calculated. If low-power mode can be 848c2ecf20Sopenharmony_ci * enabled, then the bit to enable low-power mode is stored in the 858c2ecf20Sopenharmony_ci * last_rounded_lpmode variable. This implementation is based upon the 868c2ecf20Sopenharmony_ci * criteria for enabling low-power mode as described in the OMAP4430/60 878c2ecf20Sopenharmony_ci * Public TRM section 3.6.3.3.2 "Enable Control, Status, and Low-Power 888c2ecf20Sopenharmony_ci * Operation Mode". 898c2ecf20Sopenharmony_ci */ 908c2ecf20Sopenharmony_cistatic void omap4_dpll_lpmode_recalc(struct dpll_data *dd) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci long fint, fout; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci fint = clk_hw_get_rate(dd->clk_ref) / (dd->last_rounded_n + 1); 958c2ecf20Sopenharmony_ci fout = fint * dd->last_rounded_m; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci if ((fint < OMAP4_DPLL_LP_FINT_MAX) && (fout < OMAP4_DPLL_LP_FOUT_MAX)) 988c2ecf20Sopenharmony_ci dd->last_rounded_lpmode = 1; 998c2ecf20Sopenharmony_ci else 1008c2ecf20Sopenharmony_ci dd->last_rounded_lpmode = 0; 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci/** 1048c2ecf20Sopenharmony_ci * omap4_dpll_regm4xen_recalc - compute DPLL rate, considering REGM4XEN bit 1058c2ecf20Sopenharmony_ci * @clk: struct clk * of the DPLL to compute the rate for 1068c2ecf20Sopenharmony_ci * 1078c2ecf20Sopenharmony_ci * Compute the output rate for the OMAP4 DPLL represented by @clk. 1088c2ecf20Sopenharmony_ci * Takes the REGM4XEN bit into consideration, which is needed for the 1098c2ecf20Sopenharmony_ci * OMAP4 ABE DPLL. Returns the DPLL's output rate (before M-dividers) 1108c2ecf20Sopenharmony_ci * upon success, or 0 upon error. 1118c2ecf20Sopenharmony_ci */ 1128c2ecf20Sopenharmony_ciunsigned long omap4_dpll_regm4xen_recalc(struct clk_hw *hw, 1138c2ecf20Sopenharmony_ci unsigned long parent_rate) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci struct clk_hw_omap *clk = to_clk_hw_omap(hw); 1168c2ecf20Sopenharmony_ci u32 v; 1178c2ecf20Sopenharmony_ci unsigned long rate; 1188c2ecf20Sopenharmony_ci struct dpll_data *dd; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci if (!clk || !clk->dpll_data) 1218c2ecf20Sopenharmony_ci return 0; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci dd = clk->dpll_data; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci rate = omap2_get_dpll_rate(clk); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci /* regm4xen adds a multiplier of 4 to DPLL calculations */ 1288c2ecf20Sopenharmony_ci v = ti_clk_ll_ops->clk_readl(&dd->control_reg); 1298c2ecf20Sopenharmony_ci if (v & OMAP4430_DPLL_REGM4XEN_MASK) 1308c2ecf20Sopenharmony_ci rate *= OMAP4430_REGM4XEN_MULT; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci return rate; 1338c2ecf20Sopenharmony_ci} 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci/** 1368c2ecf20Sopenharmony_ci * omap4_dpll_regm4xen_round_rate - round DPLL rate, considering REGM4XEN bit 1378c2ecf20Sopenharmony_ci * @clk: struct clk * of the DPLL to round a rate for 1388c2ecf20Sopenharmony_ci * @target_rate: the desired rate of the DPLL 1398c2ecf20Sopenharmony_ci * 1408c2ecf20Sopenharmony_ci * Compute the rate that would be programmed into the DPLL hardware 1418c2ecf20Sopenharmony_ci * for @clk if set_rate() were to be provided with the rate 1428c2ecf20Sopenharmony_ci * @target_rate. Takes the REGM4XEN bit into consideration, which is 1438c2ecf20Sopenharmony_ci * needed for the OMAP4 ABE DPLL. Returns the rounded rate (before 1448c2ecf20Sopenharmony_ci * M-dividers) upon success, -EINVAL if @clk is null or not a DPLL, or 1458c2ecf20Sopenharmony_ci * ~0 if an error occurred in omap2_dpll_round_rate(). 1468c2ecf20Sopenharmony_ci */ 1478c2ecf20Sopenharmony_cilong omap4_dpll_regm4xen_round_rate(struct clk_hw *hw, 1488c2ecf20Sopenharmony_ci unsigned long target_rate, 1498c2ecf20Sopenharmony_ci unsigned long *parent_rate) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci struct clk_hw_omap *clk = to_clk_hw_omap(hw); 1528c2ecf20Sopenharmony_ci struct dpll_data *dd; 1538c2ecf20Sopenharmony_ci long r; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci if (!clk || !clk->dpll_data) 1568c2ecf20Sopenharmony_ci return -EINVAL; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci dd = clk->dpll_data; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci dd->last_rounded_m4xen = 0; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci /* 1638c2ecf20Sopenharmony_ci * First try to compute the DPLL configuration for 1648c2ecf20Sopenharmony_ci * target rate without using the 4X multiplier. 1658c2ecf20Sopenharmony_ci */ 1668c2ecf20Sopenharmony_ci r = omap2_dpll_round_rate(hw, target_rate, NULL); 1678c2ecf20Sopenharmony_ci if (r != ~0) 1688c2ecf20Sopenharmony_ci goto out; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci /* 1718c2ecf20Sopenharmony_ci * If we did not find a valid DPLL configuration, try again, but 1728c2ecf20Sopenharmony_ci * this time see if using the 4X multiplier can help. Enabling the 1738c2ecf20Sopenharmony_ci * 4X multiplier is equivalent to dividing the target rate by 4. 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_ci r = omap2_dpll_round_rate(hw, target_rate / OMAP4430_REGM4XEN_MULT, 1768c2ecf20Sopenharmony_ci NULL); 1778c2ecf20Sopenharmony_ci if (r == ~0) 1788c2ecf20Sopenharmony_ci return r; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci dd->last_rounded_rate *= OMAP4430_REGM4XEN_MULT; 1818c2ecf20Sopenharmony_ci dd->last_rounded_m4xen = 1; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ciout: 1848c2ecf20Sopenharmony_ci omap4_dpll_lpmode_recalc(dd); 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci return dd->last_rounded_rate; 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci/** 1908c2ecf20Sopenharmony_ci * omap4_dpll_regm4xen_determine_rate - determine rate for a DPLL 1918c2ecf20Sopenharmony_ci * @hw: pointer to the clock to determine rate for 1928c2ecf20Sopenharmony_ci * @req: target rate request 1938c2ecf20Sopenharmony_ci * 1948c2ecf20Sopenharmony_ci * Determines which DPLL mode to use for reaching a desired rate. 1958c2ecf20Sopenharmony_ci * Checks whether the DPLL shall be in bypass or locked mode, and if 1968c2ecf20Sopenharmony_ci * locked, calculates the M,N values for the DPLL via round-rate. 1978c2ecf20Sopenharmony_ci * Returns 0 on success and a negative error value otherwise. 1988c2ecf20Sopenharmony_ci */ 1998c2ecf20Sopenharmony_ciint omap4_dpll_regm4xen_determine_rate(struct clk_hw *hw, 2008c2ecf20Sopenharmony_ci struct clk_rate_request *req) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci struct clk_hw_omap *clk = to_clk_hw_omap(hw); 2038c2ecf20Sopenharmony_ci struct dpll_data *dd; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci if (!req->rate) 2068c2ecf20Sopenharmony_ci return -EINVAL; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci dd = clk->dpll_data; 2098c2ecf20Sopenharmony_ci if (!dd) 2108c2ecf20Sopenharmony_ci return -EINVAL; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci if (clk_hw_get_rate(dd->clk_bypass) == req->rate && 2138c2ecf20Sopenharmony_ci (dd->modes & (1 << DPLL_LOW_POWER_BYPASS))) { 2148c2ecf20Sopenharmony_ci req->best_parent_hw = dd->clk_bypass; 2158c2ecf20Sopenharmony_ci } else { 2168c2ecf20Sopenharmony_ci req->rate = omap4_dpll_regm4xen_round_rate(hw, req->rate, 2178c2ecf20Sopenharmony_ci &req->best_parent_rate); 2188c2ecf20Sopenharmony_ci req->best_parent_hw = dd->clk_ref; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci req->best_parent_rate = req->rate; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci return 0; 2248c2ecf20Sopenharmony_ci} 225