18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (C) 2013 Broadcom Corporation 38c2ecf20Sopenharmony_ci * Copyright 2013 Linaro Limited 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or 68c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License as 78c2ecf20Sopenharmony_ci * published by the Free Software Foundation version 2. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * This program is distributed "as is" WITHOUT ANY WARRANTY of any 108c2ecf20Sopenharmony_ci * kind, whether express or implied; without even the implied warranty 118c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2ecf20Sopenharmony_ci * GNU General Public License for more details. 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include "clk-kona.h" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/delay.h> 188c2ecf20Sopenharmony_ci#include <linux/io.h> 198c2ecf20Sopenharmony_ci#include <linux/kernel.h> 208c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/* 238c2ecf20Sopenharmony_ci * "Policies" affect the frequencies of bus clocks provided by a 248c2ecf20Sopenharmony_ci * CCU. (I believe these polices are named "Deep Sleep", "Economy", 258c2ecf20Sopenharmony_ci * "Normal", and "Turbo".) A lower policy number has lower power 268c2ecf20Sopenharmony_ci * consumption, and policy 2 is the default. 278c2ecf20Sopenharmony_ci */ 288c2ecf20Sopenharmony_ci#define CCU_POLICY_COUNT 4 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#define CCU_ACCESS_PASSWORD 0xA5A500 318c2ecf20Sopenharmony_ci#define CLK_GATE_DELAY_LOOP 2000 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci/* Bitfield operations */ 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* Produces a mask of set bits covering a range of a 32-bit value */ 368c2ecf20Sopenharmony_cistatic inline u32 bitfield_mask(u32 shift, u32 width) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci return ((1 << width) - 1) << shift; 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* Extract the value of a bitfield found within a given register value */ 428c2ecf20Sopenharmony_cistatic inline u32 bitfield_extract(u32 reg_val, u32 shift, u32 width) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci return (reg_val & bitfield_mask(shift, width)) >> shift; 458c2ecf20Sopenharmony_ci} 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci/* Replace the value of a bitfield found within a given register value */ 488c2ecf20Sopenharmony_cistatic inline u32 bitfield_replace(u32 reg_val, u32 shift, u32 width, u32 val) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci u32 mask = bitfield_mask(shift, width); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci return (reg_val & ~mask) | (val << shift); 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci/* Divider and scaling helpers */ 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/* Convert a divider into the scaled divisor value it represents. */ 588c2ecf20Sopenharmony_cistatic inline u64 scaled_div_value(struct bcm_clk_div *div, u32 reg_div) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci return (u64)reg_div + ((u64)1 << div->u.s.frac_width); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci/* 648c2ecf20Sopenharmony_ci * Build a scaled divider value as close as possible to the 658c2ecf20Sopenharmony_ci * given whole part (div_value) and fractional part (expressed 668c2ecf20Sopenharmony_ci * in billionths). 678c2ecf20Sopenharmony_ci */ 688c2ecf20Sopenharmony_ciu64 scaled_div_build(struct bcm_clk_div *div, u32 div_value, u32 billionths) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci u64 combined; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci BUG_ON(!div_value); 738c2ecf20Sopenharmony_ci BUG_ON(billionths >= BILLION); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci combined = (u64)div_value * BILLION + billionths; 768c2ecf20Sopenharmony_ci combined <<= div->u.s.frac_width; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci return DIV_ROUND_CLOSEST_ULL(combined, BILLION); 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci/* The scaled minimum divisor representable by a divider */ 828c2ecf20Sopenharmony_cistatic inline u64 838c2ecf20Sopenharmony_ciscaled_div_min(struct bcm_clk_div *div) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci if (divider_is_fixed(div)) 868c2ecf20Sopenharmony_ci return (u64)div->u.fixed; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci return scaled_div_value(div, 0); 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/* The scaled maximum divisor representable by a divider */ 928c2ecf20Sopenharmony_ciu64 scaled_div_max(struct bcm_clk_div *div) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci u32 reg_div; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci if (divider_is_fixed(div)) 978c2ecf20Sopenharmony_ci return (u64)div->u.fixed; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci reg_div = ((u32)1 << div->u.s.width) - 1; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci return scaled_div_value(div, reg_div); 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci/* 1058c2ecf20Sopenharmony_ci * Convert a scaled divisor into its divider representation as 1068c2ecf20Sopenharmony_ci * stored in a divider register field. 1078c2ecf20Sopenharmony_ci */ 1088c2ecf20Sopenharmony_cistatic inline u32 1098c2ecf20Sopenharmony_cidivider(struct bcm_clk_div *div, u64 scaled_div) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci BUG_ON(scaled_div < scaled_div_min(div)); 1128c2ecf20Sopenharmony_ci BUG_ON(scaled_div > scaled_div_max(div)); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci return (u32)(scaled_div - ((u64)1 << div->u.s.frac_width)); 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci/* Return a rate scaled for use when dividing by a scaled divisor. */ 1188c2ecf20Sopenharmony_cistatic inline u64 1198c2ecf20Sopenharmony_ciscale_rate(struct bcm_clk_div *div, u32 rate) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci if (divider_is_fixed(div)) 1228c2ecf20Sopenharmony_ci return (u64)rate; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci return (u64)rate << div->u.s.frac_width; 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci/* CCU access */ 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci/* Read a 32-bit register value from a CCU's address space. */ 1308c2ecf20Sopenharmony_cistatic inline u32 __ccu_read(struct ccu_data *ccu, u32 reg_offset) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci return readl(ccu->base + reg_offset); 1338c2ecf20Sopenharmony_ci} 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci/* Write a 32-bit register value into a CCU's address space. */ 1368c2ecf20Sopenharmony_cistatic inline void 1378c2ecf20Sopenharmony_ci__ccu_write(struct ccu_data *ccu, u32 reg_offset, u32 reg_val) 1388c2ecf20Sopenharmony_ci{ 1398c2ecf20Sopenharmony_ci writel(reg_val, ccu->base + reg_offset); 1408c2ecf20Sopenharmony_ci} 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic inline unsigned long ccu_lock(struct ccu_data *ccu) 1438c2ecf20Sopenharmony_ci{ 1448c2ecf20Sopenharmony_ci unsigned long flags; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci spin_lock_irqsave(&ccu->lock, flags); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return flags; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_cistatic inline void ccu_unlock(struct ccu_data *ccu, unsigned long flags) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ccu->lock, flags); 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci/* 1568c2ecf20Sopenharmony_ci * Enable/disable write access to CCU protected registers. The 1578c2ecf20Sopenharmony_ci * WR_ACCESS register for all CCUs is at offset 0. 1588c2ecf20Sopenharmony_ci */ 1598c2ecf20Sopenharmony_cistatic inline void __ccu_write_enable(struct ccu_data *ccu) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci if (ccu->write_enabled) { 1628c2ecf20Sopenharmony_ci pr_err("%s: access already enabled for %s\n", __func__, 1638c2ecf20Sopenharmony_ci ccu->name); 1648c2ecf20Sopenharmony_ci return; 1658c2ecf20Sopenharmony_ci } 1668c2ecf20Sopenharmony_ci ccu->write_enabled = true; 1678c2ecf20Sopenharmony_ci __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD | 1); 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_cistatic inline void __ccu_write_disable(struct ccu_data *ccu) 1718c2ecf20Sopenharmony_ci{ 1728c2ecf20Sopenharmony_ci if (!ccu->write_enabled) { 1738c2ecf20Sopenharmony_ci pr_err("%s: access wasn't enabled for %s\n", __func__, 1748c2ecf20Sopenharmony_ci ccu->name); 1758c2ecf20Sopenharmony_ci return; 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD); 1798c2ecf20Sopenharmony_ci ccu->write_enabled = false; 1808c2ecf20Sopenharmony_ci} 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci/* 1838c2ecf20Sopenharmony_ci * Poll a register in a CCU's address space, returning when the 1848c2ecf20Sopenharmony_ci * specified bit in that register's value is set (or clear). Delay 1858c2ecf20Sopenharmony_ci * a microsecond after each read of the register. Returns true if 1868c2ecf20Sopenharmony_ci * successful, or false if we gave up trying. 1878c2ecf20Sopenharmony_ci * 1888c2ecf20Sopenharmony_ci * Caller must ensure the CCU lock is held. 1898c2ecf20Sopenharmony_ci */ 1908c2ecf20Sopenharmony_cistatic inline bool 1918c2ecf20Sopenharmony_ci__ccu_wait_bit(struct ccu_data *ccu, u32 reg_offset, u32 bit, bool want) 1928c2ecf20Sopenharmony_ci{ 1938c2ecf20Sopenharmony_ci unsigned int tries; 1948c2ecf20Sopenharmony_ci u32 bit_mask = 1 << bit; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci for (tries = 0; tries < CLK_GATE_DELAY_LOOP; tries++) { 1978c2ecf20Sopenharmony_ci u32 val; 1988c2ecf20Sopenharmony_ci bool bit_val; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci val = __ccu_read(ccu, reg_offset); 2018c2ecf20Sopenharmony_ci bit_val = (val & bit_mask) != 0; 2028c2ecf20Sopenharmony_ci if (bit_val == want) 2038c2ecf20Sopenharmony_ci return true; 2048c2ecf20Sopenharmony_ci udelay(1); 2058c2ecf20Sopenharmony_ci } 2068c2ecf20Sopenharmony_ci pr_warn("%s: %s/0x%04x bit %u was never %s\n", __func__, 2078c2ecf20Sopenharmony_ci ccu->name, reg_offset, bit, want ? "set" : "clear"); 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci return false; 2108c2ecf20Sopenharmony_ci} 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci/* Policy operations */ 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_cistatic bool __ccu_policy_engine_start(struct ccu_data *ccu, bool sync) 2158c2ecf20Sopenharmony_ci{ 2168c2ecf20Sopenharmony_ci struct bcm_policy_ctl *control = &ccu->policy.control; 2178c2ecf20Sopenharmony_ci u32 offset; 2188c2ecf20Sopenharmony_ci u32 go_bit; 2198c2ecf20Sopenharmony_ci u32 mask; 2208c2ecf20Sopenharmony_ci bool ret; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci /* If we don't need to control policy for this CCU, we're done. */ 2238c2ecf20Sopenharmony_ci if (!policy_ctl_exists(control)) 2248c2ecf20Sopenharmony_ci return true; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci offset = control->offset; 2278c2ecf20Sopenharmony_ci go_bit = control->go_bit; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci /* Ensure we're not busy before we start */ 2308c2ecf20Sopenharmony_ci ret = __ccu_wait_bit(ccu, offset, go_bit, false); 2318c2ecf20Sopenharmony_ci if (!ret) { 2328c2ecf20Sopenharmony_ci pr_err("%s: ccu %s policy engine wouldn't go idle\n", 2338c2ecf20Sopenharmony_ci __func__, ccu->name); 2348c2ecf20Sopenharmony_ci return false; 2358c2ecf20Sopenharmony_ci } 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci /* 2388c2ecf20Sopenharmony_ci * If it's a synchronous request, we'll wait for the voltage 2398c2ecf20Sopenharmony_ci * and frequency of the active load to stabilize before 2408c2ecf20Sopenharmony_ci * returning. To do this we select the active load by 2418c2ecf20Sopenharmony_ci * setting the ATL bit. 2428c2ecf20Sopenharmony_ci * 2438c2ecf20Sopenharmony_ci * An asynchronous request instead ramps the voltage in the 2448c2ecf20Sopenharmony_ci * background, and when that process stabilizes, the target 2458c2ecf20Sopenharmony_ci * load is copied to the active load and the CCU frequency 2468c2ecf20Sopenharmony_ci * is switched. We do this by selecting the target load 2478c2ecf20Sopenharmony_ci * (ATL bit clear) and setting the request auto-copy (AC bit 2488c2ecf20Sopenharmony_ci * set). 2498c2ecf20Sopenharmony_ci * 2508c2ecf20Sopenharmony_ci * Note, we do NOT read-modify-write this register. 2518c2ecf20Sopenharmony_ci */ 2528c2ecf20Sopenharmony_ci mask = (u32)1 << go_bit; 2538c2ecf20Sopenharmony_ci if (sync) 2548c2ecf20Sopenharmony_ci mask |= 1 << control->atl_bit; 2558c2ecf20Sopenharmony_ci else 2568c2ecf20Sopenharmony_ci mask |= 1 << control->ac_bit; 2578c2ecf20Sopenharmony_ci __ccu_write(ccu, offset, mask); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci /* Wait for indication that operation is complete. */ 2608c2ecf20Sopenharmony_ci ret = __ccu_wait_bit(ccu, offset, go_bit, false); 2618c2ecf20Sopenharmony_ci if (!ret) 2628c2ecf20Sopenharmony_ci pr_err("%s: ccu %s policy engine never started\n", 2638c2ecf20Sopenharmony_ci __func__, ccu->name); 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci return ret; 2668c2ecf20Sopenharmony_ci} 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_cistatic bool __ccu_policy_engine_stop(struct ccu_data *ccu) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci struct bcm_lvm_en *enable = &ccu->policy.enable; 2718c2ecf20Sopenharmony_ci u32 offset; 2728c2ecf20Sopenharmony_ci u32 enable_bit; 2738c2ecf20Sopenharmony_ci bool ret; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci /* If we don't need to control policy for this CCU, we're done. */ 2768c2ecf20Sopenharmony_ci if (!policy_lvm_en_exists(enable)) 2778c2ecf20Sopenharmony_ci return true; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci /* Ensure we're not busy before we start */ 2808c2ecf20Sopenharmony_ci offset = enable->offset; 2818c2ecf20Sopenharmony_ci enable_bit = enable->bit; 2828c2ecf20Sopenharmony_ci ret = __ccu_wait_bit(ccu, offset, enable_bit, false); 2838c2ecf20Sopenharmony_ci if (!ret) { 2848c2ecf20Sopenharmony_ci pr_err("%s: ccu %s policy engine already stopped\n", 2858c2ecf20Sopenharmony_ci __func__, ccu->name); 2868c2ecf20Sopenharmony_ci return false; 2878c2ecf20Sopenharmony_ci } 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci /* Now set the bit to stop the engine (NO read-modify-write) */ 2908c2ecf20Sopenharmony_ci __ccu_write(ccu, offset, (u32)1 << enable_bit); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci /* Wait for indication that it has stopped. */ 2938c2ecf20Sopenharmony_ci ret = __ccu_wait_bit(ccu, offset, enable_bit, false); 2948c2ecf20Sopenharmony_ci if (!ret) 2958c2ecf20Sopenharmony_ci pr_err("%s: ccu %s policy engine never stopped\n", 2968c2ecf20Sopenharmony_ci __func__, ccu->name); 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci return ret; 2998c2ecf20Sopenharmony_ci} 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci/* 3028c2ecf20Sopenharmony_ci * A CCU has four operating conditions ("policies"), and some clocks 3038c2ecf20Sopenharmony_ci * can be disabled or enabled based on which policy is currently in 3048c2ecf20Sopenharmony_ci * effect. Such clocks have a bit in a "policy mask" register for 3058c2ecf20Sopenharmony_ci * each policy indicating whether the clock is enabled for that 3068c2ecf20Sopenharmony_ci * policy or not. The bit position for a clock is the same for all 3078c2ecf20Sopenharmony_ci * four registers, and the 32-bit registers are at consecutive 3088c2ecf20Sopenharmony_ci * addresses. 3098c2ecf20Sopenharmony_ci */ 3108c2ecf20Sopenharmony_cistatic bool policy_init(struct ccu_data *ccu, struct bcm_clk_policy *policy) 3118c2ecf20Sopenharmony_ci{ 3128c2ecf20Sopenharmony_ci u32 offset; 3138c2ecf20Sopenharmony_ci u32 mask; 3148c2ecf20Sopenharmony_ci int i; 3158c2ecf20Sopenharmony_ci bool ret; 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci if (!policy_exists(policy)) 3188c2ecf20Sopenharmony_ci return true; 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci /* 3218c2ecf20Sopenharmony_ci * We need to stop the CCU policy engine to allow update 3228c2ecf20Sopenharmony_ci * of our policy bits. 3238c2ecf20Sopenharmony_ci */ 3248c2ecf20Sopenharmony_ci if (!__ccu_policy_engine_stop(ccu)) { 3258c2ecf20Sopenharmony_ci pr_err("%s: unable to stop CCU %s policy engine\n", 3268c2ecf20Sopenharmony_ci __func__, ccu->name); 3278c2ecf20Sopenharmony_ci return false; 3288c2ecf20Sopenharmony_ci } 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci /* 3318c2ecf20Sopenharmony_ci * For now, if a clock defines its policy bit we just mark 3328c2ecf20Sopenharmony_ci * it "enabled" for all four policies. 3338c2ecf20Sopenharmony_ci */ 3348c2ecf20Sopenharmony_ci offset = policy->offset; 3358c2ecf20Sopenharmony_ci mask = (u32)1 << policy->bit; 3368c2ecf20Sopenharmony_ci for (i = 0; i < CCU_POLICY_COUNT; i++) { 3378c2ecf20Sopenharmony_ci u32 reg_val; 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci reg_val = __ccu_read(ccu, offset); 3408c2ecf20Sopenharmony_ci reg_val |= mask; 3418c2ecf20Sopenharmony_ci __ccu_write(ccu, offset, reg_val); 3428c2ecf20Sopenharmony_ci offset += sizeof(u32); 3438c2ecf20Sopenharmony_ci } 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci /* We're done updating; fire up the policy engine again. */ 3468c2ecf20Sopenharmony_ci ret = __ccu_policy_engine_start(ccu, true); 3478c2ecf20Sopenharmony_ci if (!ret) 3488c2ecf20Sopenharmony_ci pr_err("%s: unable to restart CCU %s policy engine\n", 3498c2ecf20Sopenharmony_ci __func__, ccu->name); 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci return ret; 3528c2ecf20Sopenharmony_ci} 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci/* Gate operations */ 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci/* Determine whether a clock is gated. CCU lock must be held. */ 3578c2ecf20Sopenharmony_cistatic bool 3588c2ecf20Sopenharmony_ci__is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate) 3598c2ecf20Sopenharmony_ci{ 3608c2ecf20Sopenharmony_ci u32 bit_mask; 3618c2ecf20Sopenharmony_ci u32 reg_val; 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci /* If there is no gate we can assume it's enabled. */ 3648c2ecf20Sopenharmony_ci if (!gate_exists(gate)) 3658c2ecf20Sopenharmony_ci return true; 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci bit_mask = 1 << gate->status_bit; 3688c2ecf20Sopenharmony_ci reg_val = __ccu_read(ccu, gate->offset); 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci return (reg_val & bit_mask) != 0; 3718c2ecf20Sopenharmony_ci} 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci/* Determine whether a clock is gated. */ 3748c2ecf20Sopenharmony_cistatic bool 3758c2ecf20Sopenharmony_ciis_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate) 3768c2ecf20Sopenharmony_ci{ 3778c2ecf20Sopenharmony_ci long flags; 3788c2ecf20Sopenharmony_ci bool ret; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci /* Avoid taking the lock if we can */ 3818c2ecf20Sopenharmony_ci if (!gate_exists(gate)) 3828c2ecf20Sopenharmony_ci return true; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci flags = ccu_lock(ccu); 3858c2ecf20Sopenharmony_ci ret = __is_clk_gate_enabled(ccu, gate); 3868c2ecf20Sopenharmony_ci ccu_unlock(ccu, flags); 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci return ret; 3898c2ecf20Sopenharmony_ci} 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci/* 3928c2ecf20Sopenharmony_ci * Commit our desired gate state to the hardware. 3938c2ecf20Sopenharmony_ci * Returns true if successful, false otherwise. 3948c2ecf20Sopenharmony_ci */ 3958c2ecf20Sopenharmony_cistatic bool 3968c2ecf20Sopenharmony_ci__gate_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate) 3978c2ecf20Sopenharmony_ci{ 3988c2ecf20Sopenharmony_ci u32 reg_val; 3998c2ecf20Sopenharmony_ci u32 mask; 4008c2ecf20Sopenharmony_ci bool enabled = false; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci BUG_ON(!gate_exists(gate)); 4038c2ecf20Sopenharmony_ci if (!gate_is_sw_controllable(gate)) 4048c2ecf20Sopenharmony_ci return true; /* Nothing we can change */ 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci reg_val = __ccu_read(ccu, gate->offset); 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci /* For a hardware/software gate, set which is in control */ 4098c2ecf20Sopenharmony_ci if (gate_is_hw_controllable(gate)) { 4108c2ecf20Sopenharmony_ci mask = (u32)1 << gate->hw_sw_sel_bit; 4118c2ecf20Sopenharmony_ci if (gate_is_sw_managed(gate)) 4128c2ecf20Sopenharmony_ci reg_val |= mask; 4138c2ecf20Sopenharmony_ci else 4148c2ecf20Sopenharmony_ci reg_val &= ~mask; 4158c2ecf20Sopenharmony_ci } 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci /* 4188c2ecf20Sopenharmony_ci * If software is in control, enable or disable the gate. 4198c2ecf20Sopenharmony_ci * If hardware is, clear the enabled bit for good measure. 4208c2ecf20Sopenharmony_ci * If a software controlled gate can't be disabled, we're 4218c2ecf20Sopenharmony_ci * required to write a 0 into the enable bit (but the gate 4228c2ecf20Sopenharmony_ci * will be enabled). 4238c2ecf20Sopenharmony_ci */ 4248c2ecf20Sopenharmony_ci mask = (u32)1 << gate->en_bit; 4258c2ecf20Sopenharmony_ci if (gate_is_sw_managed(gate) && (enabled = gate_is_enabled(gate)) && 4268c2ecf20Sopenharmony_ci !gate_is_no_disable(gate)) 4278c2ecf20Sopenharmony_ci reg_val |= mask; 4288c2ecf20Sopenharmony_ci else 4298c2ecf20Sopenharmony_ci reg_val &= ~mask; 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci __ccu_write(ccu, gate->offset, reg_val); 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci /* For a hardware controlled gate, we're done */ 4348c2ecf20Sopenharmony_ci if (!gate_is_sw_managed(gate)) 4358c2ecf20Sopenharmony_ci return true; 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci /* Otherwise wait for the gate to be in desired state */ 4388c2ecf20Sopenharmony_ci return __ccu_wait_bit(ccu, gate->offset, gate->status_bit, enabled); 4398c2ecf20Sopenharmony_ci} 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci/* 4428c2ecf20Sopenharmony_ci * Initialize a gate. Our desired state (hardware/software select, 4438c2ecf20Sopenharmony_ci * and if software, its enable state) is committed to hardware 4448c2ecf20Sopenharmony_ci * without the usual checks to see if it's already set up that way. 4458c2ecf20Sopenharmony_ci * Returns true if successful, false otherwise. 4468c2ecf20Sopenharmony_ci */ 4478c2ecf20Sopenharmony_cistatic bool gate_init(struct ccu_data *ccu, struct bcm_clk_gate *gate) 4488c2ecf20Sopenharmony_ci{ 4498c2ecf20Sopenharmony_ci if (!gate_exists(gate)) 4508c2ecf20Sopenharmony_ci return true; 4518c2ecf20Sopenharmony_ci return __gate_commit(ccu, gate); 4528c2ecf20Sopenharmony_ci} 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci/* 4558c2ecf20Sopenharmony_ci * Set a gate to enabled or disabled state. Does nothing if the 4568c2ecf20Sopenharmony_ci * gate is not currently under software control, or if it is already 4578c2ecf20Sopenharmony_ci * in the requested state. Returns true if successful, false 4588c2ecf20Sopenharmony_ci * otherwise. CCU lock must be held. 4598c2ecf20Sopenharmony_ci */ 4608c2ecf20Sopenharmony_cistatic bool 4618c2ecf20Sopenharmony_ci__clk_gate(struct ccu_data *ccu, struct bcm_clk_gate *gate, bool enable) 4628c2ecf20Sopenharmony_ci{ 4638c2ecf20Sopenharmony_ci bool ret; 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci if (!gate_exists(gate) || !gate_is_sw_managed(gate)) 4668c2ecf20Sopenharmony_ci return true; /* Nothing to do */ 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci if (!enable && gate_is_no_disable(gate)) { 4698c2ecf20Sopenharmony_ci pr_warn("%s: invalid gate disable request (ignoring)\n", 4708c2ecf20Sopenharmony_ci __func__); 4718c2ecf20Sopenharmony_ci return true; 4728c2ecf20Sopenharmony_ci } 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci if (enable == gate_is_enabled(gate)) 4758c2ecf20Sopenharmony_ci return true; /* No change */ 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci gate_flip_enabled(gate); 4788c2ecf20Sopenharmony_ci ret = __gate_commit(ccu, gate); 4798c2ecf20Sopenharmony_ci if (!ret) 4808c2ecf20Sopenharmony_ci gate_flip_enabled(gate); /* Revert the change */ 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci return ret; 4838c2ecf20Sopenharmony_ci} 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci/* Enable or disable a gate. Returns 0 if successful, -EIO otherwise */ 4868c2ecf20Sopenharmony_cistatic int clk_gate(struct ccu_data *ccu, const char *name, 4878c2ecf20Sopenharmony_ci struct bcm_clk_gate *gate, bool enable) 4888c2ecf20Sopenharmony_ci{ 4898c2ecf20Sopenharmony_ci unsigned long flags; 4908c2ecf20Sopenharmony_ci bool success; 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci /* 4938c2ecf20Sopenharmony_ci * Avoid taking the lock if we can. We quietly ignore 4948c2ecf20Sopenharmony_ci * requests to change state that don't make sense. 4958c2ecf20Sopenharmony_ci */ 4968c2ecf20Sopenharmony_ci if (!gate_exists(gate) || !gate_is_sw_managed(gate)) 4978c2ecf20Sopenharmony_ci return 0; 4988c2ecf20Sopenharmony_ci if (!enable && gate_is_no_disable(gate)) 4998c2ecf20Sopenharmony_ci return 0; 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci flags = ccu_lock(ccu); 5028c2ecf20Sopenharmony_ci __ccu_write_enable(ccu); 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci success = __clk_gate(ccu, gate, enable); 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci __ccu_write_disable(ccu); 5078c2ecf20Sopenharmony_ci ccu_unlock(ccu, flags); 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci if (success) 5108c2ecf20Sopenharmony_ci return 0; 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci pr_err("%s: failed to %s gate for %s\n", __func__, 5138c2ecf20Sopenharmony_ci enable ? "enable" : "disable", name); 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci return -EIO; 5168c2ecf20Sopenharmony_ci} 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci/* Hysteresis operations */ 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci/* 5218c2ecf20Sopenharmony_ci * If a clock gate requires a turn-off delay it will have 5228c2ecf20Sopenharmony_ci * "hysteresis" register bits defined. The first, if set, enables 5238c2ecf20Sopenharmony_ci * the delay; and if enabled, the second bit determines whether the 5248c2ecf20Sopenharmony_ci * delay is "low" or "high" (1 means high). For now, if it's 5258c2ecf20Sopenharmony_ci * defined for a clock, we set it. 5268c2ecf20Sopenharmony_ci */ 5278c2ecf20Sopenharmony_cistatic bool hyst_init(struct ccu_data *ccu, struct bcm_clk_hyst *hyst) 5288c2ecf20Sopenharmony_ci{ 5298c2ecf20Sopenharmony_ci u32 offset; 5308c2ecf20Sopenharmony_ci u32 reg_val; 5318c2ecf20Sopenharmony_ci u32 mask; 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci if (!hyst_exists(hyst)) 5348c2ecf20Sopenharmony_ci return true; 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci offset = hyst->offset; 5378c2ecf20Sopenharmony_ci mask = (u32)1 << hyst->en_bit; 5388c2ecf20Sopenharmony_ci mask |= (u32)1 << hyst->val_bit; 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci reg_val = __ccu_read(ccu, offset); 5418c2ecf20Sopenharmony_ci reg_val |= mask; 5428c2ecf20Sopenharmony_ci __ccu_write(ccu, offset, reg_val); 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci return true; 5458c2ecf20Sopenharmony_ci} 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci/* Trigger operations */ 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci/* 5508c2ecf20Sopenharmony_ci * Caller must ensure CCU lock is held and access is enabled. 5518c2ecf20Sopenharmony_ci * Returns true if successful, false otherwise. 5528c2ecf20Sopenharmony_ci */ 5538c2ecf20Sopenharmony_cistatic bool __clk_trigger(struct ccu_data *ccu, struct bcm_clk_trig *trig) 5548c2ecf20Sopenharmony_ci{ 5558c2ecf20Sopenharmony_ci /* Trigger the clock and wait for it to finish */ 5568c2ecf20Sopenharmony_ci __ccu_write(ccu, trig->offset, 1 << trig->bit); 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci return __ccu_wait_bit(ccu, trig->offset, trig->bit, false); 5598c2ecf20Sopenharmony_ci} 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci/* Divider operations */ 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci/* Read a divider value and return the scaled divisor it represents. */ 5648c2ecf20Sopenharmony_cistatic u64 divider_read_scaled(struct ccu_data *ccu, struct bcm_clk_div *div) 5658c2ecf20Sopenharmony_ci{ 5668c2ecf20Sopenharmony_ci unsigned long flags; 5678c2ecf20Sopenharmony_ci u32 reg_val; 5688c2ecf20Sopenharmony_ci u32 reg_div; 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_ci if (divider_is_fixed(div)) 5718c2ecf20Sopenharmony_ci return (u64)div->u.fixed; 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci flags = ccu_lock(ccu); 5748c2ecf20Sopenharmony_ci reg_val = __ccu_read(ccu, div->u.s.offset); 5758c2ecf20Sopenharmony_ci ccu_unlock(ccu, flags); 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci /* Extract the full divider field from the register value */ 5788c2ecf20Sopenharmony_ci reg_div = bitfield_extract(reg_val, div->u.s.shift, div->u.s.width); 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_ci /* Return the scaled divisor value it represents */ 5818c2ecf20Sopenharmony_ci return scaled_div_value(div, reg_div); 5828c2ecf20Sopenharmony_ci} 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci/* 5858c2ecf20Sopenharmony_ci * Convert a divider's scaled divisor value into its recorded form 5868c2ecf20Sopenharmony_ci * and commit it into the hardware divider register. 5878c2ecf20Sopenharmony_ci * 5888c2ecf20Sopenharmony_ci * Returns 0 on success. Returns -EINVAL for invalid arguments. 5898c2ecf20Sopenharmony_ci * Returns -ENXIO if gating failed, and -EIO if a trigger failed. 5908c2ecf20Sopenharmony_ci */ 5918c2ecf20Sopenharmony_cistatic int __div_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate, 5928c2ecf20Sopenharmony_ci struct bcm_clk_div *div, struct bcm_clk_trig *trig) 5938c2ecf20Sopenharmony_ci{ 5948c2ecf20Sopenharmony_ci bool enabled; 5958c2ecf20Sopenharmony_ci u32 reg_div; 5968c2ecf20Sopenharmony_ci u32 reg_val; 5978c2ecf20Sopenharmony_ci int ret = 0; 5988c2ecf20Sopenharmony_ci 5998c2ecf20Sopenharmony_ci BUG_ON(divider_is_fixed(div)); 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ci /* 6028c2ecf20Sopenharmony_ci * If we're just initializing the divider, and no initial 6038c2ecf20Sopenharmony_ci * state was defined in the device tree, we just find out 6048c2ecf20Sopenharmony_ci * what its current value is rather than updating it. 6058c2ecf20Sopenharmony_ci */ 6068c2ecf20Sopenharmony_ci if (div->u.s.scaled_div == BAD_SCALED_DIV_VALUE) { 6078c2ecf20Sopenharmony_ci reg_val = __ccu_read(ccu, div->u.s.offset); 6088c2ecf20Sopenharmony_ci reg_div = bitfield_extract(reg_val, div->u.s.shift, 6098c2ecf20Sopenharmony_ci div->u.s.width); 6108c2ecf20Sopenharmony_ci div->u.s.scaled_div = scaled_div_value(div, reg_div); 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ci return 0; 6138c2ecf20Sopenharmony_ci } 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci /* Convert the scaled divisor to the value we need to record */ 6168c2ecf20Sopenharmony_ci reg_div = divider(div, div->u.s.scaled_div); 6178c2ecf20Sopenharmony_ci 6188c2ecf20Sopenharmony_ci /* Clock needs to be enabled before changing the rate */ 6198c2ecf20Sopenharmony_ci enabled = __is_clk_gate_enabled(ccu, gate); 6208c2ecf20Sopenharmony_ci if (!enabled && !__clk_gate(ccu, gate, true)) { 6218c2ecf20Sopenharmony_ci ret = -ENXIO; 6228c2ecf20Sopenharmony_ci goto out; 6238c2ecf20Sopenharmony_ci } 6248c2ecf20Sopenharmony_ci 6258c2ecf20Sopenharmony_ci /* Replace the divider value and record the result */ 6268c2ecf20Sopenharmony_ci reg_val = __ccu_read(ccu, div->u.s.offset); 6278c2ecf20Sopenharmony_ci reg_val = bitfield_replace(reg_val, div->u.s.shift, div->u.s.width, 6288c2ecf20Sopenharmony_ci reg_div); 6298c2ecf20Sopenharmony_ci __ccu_write(ccu, div->u.s.offset, reg_val); 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_ci /* If the trigger fails we still want to disable the gate */ 6328c2ecf20Sopenharmony_ci if (!__clk_trigger(ccu, trig)) 6338c2ecf20Sopenharmony_ci ret = -EIO; 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci /* Disable the clock again if it was disabled to begin with */ 6368c2ecf20Sopenharmony_ci if (!enabled && !__clk_gate(ccu, gate, false)) 6378c2ecf20Sopenharmony_ci ret = ret ? ret : -ENXIO; /* return first error */ 6388c2ecf20Sopenharmony_ciout: 6398c2ecf20Sopenharmony_ci return ret; 6408c2ecf20Sopenharmony_ci} 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ci/* 6438c2ecf20Sopenharmony_ci * Initialize a divider by committing our desired state to hardware 6448c2ecf20Sopenharmony_ci * without the usual checks to see if it's already set up that way. 6458c2ecf20Sopenharmony_ci * Returns true if successful, false otherwise. 6468c2ecf20Sopenharmony_ci */ 6478c2ecf20Sopenharmony_cistatic bool div_init(struct ccu_data *ccu, struct bcm_clk_gate *gate, 6488c2ecf20Sopenharmony_ci struct bcm_clk_div *div, struct bcm_clk_trig *trig) 6498c2ecf20Sopenharmony_ci{ 6508c2ecf20Sopenharmony_ci if (!divider_exists(div) || divider_is_fixed(div)) 6518c2ecf20Sopenharmony_ci return true; 6528c2ecf20Sopenharmony_ci return !__div_commit(ccu, gate, div, trig); 6538c2ecf20Sopenharmony_ci} 6548c2ecf20Sopenharmony_ci 6558c2ecf20Sopenharmony_cistatic int divider_write(struct ccu_data *ccu, struct bcm_clk_gate *gate, 6568c2ecf20Sopenharmony_ci struct bcm_clk_div *div, struct bcm_clk_trig *trig, 6578c2ecf20Sopenharmony_ci u64 scaled_div) 6588c2ecf20Sopenharmony_ci{ 6598c2ecf20Sopenharmony_ci unsigned long flags; 6608c2ecf20Sopenharmony_ci u64 previous; 6618c2ecf20Sopenharmony_ci int ret; 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci BUG_ON(divider_is_fixed(div)); 6648c2ecf20Sopenharmony_ci 6658c2ecf20Sopenharmony_ci previous = div->u.s.scaled_div; 6668c2ecf20Sopenharmony_ci if (previous == scaled_div) 6678c2ecf20Sopenharmony_ci return 0; /* No change */ 6688c2ecf20Sopenharmony_ci 6698c2ecf20Sopenharmony_ci div->u.s.scaled_div = scaled_div; 6708c2ecf20Sopenharmony_ci 6718c2ecf20Sopenharmony_ci flags = ccu_lock(ccu); 6728c2ecf20Sopenharmony_ci __ccu_write_enable(ccu); 6738c2ecf20Sopenharmony_ci 6748c2ecf20Sopenharmony_ci ret = __div_commit(ccu, gate, div, trig); 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_ci __ccu_write_disable(ccu); 6778c2ecf20Sopenharmony_ci ccu_unlock(ccu, flags); 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_ci if (ret) 6808c2ecf20Sopenharmony_ci div->u.s.scaled_div = previous; /* Revert the change */ 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_ci return ret; 6838c2ecf20Sopenharmony_ci 6848c2ecf20Sopenharmony_ci} 6858c2ecf20Sopenharmony_ci 6868c2ecf20Sopenharmony_ci/* Common clock rate helpers */ 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci/* 6898c2ecf20Sopenharmony_ci * Implement the common clock framework recalc_rate method, taking 6908c2ecf20Sopenharmony_ci * into account a divider and an optional pre-divider. The 6918c2ecf20Sopenharmony_ci * pre-divider register pointer may be NULL. 6928c2ecf20Sopenharmony_ci */ 6938c2ecf20Sopenharmony_cistatic unsigned long clk_recalc_rate(struct ccu_data *ccu, 6948c2ecf20Sopenharmony_ci struct bcm_clk_div *div, struct bcm_clk_div *pre_div, 6958c2ecf20Sopenharmony_ci unsigned long parent_rate) 6968c2ecf20Sopenharmony_ci{ 6978c2ecf20Sopenharmony_ci u64 scaled_parent_rate; 6988c2ecf20Sopenharmony_ci u64 scaled_div; 6998c2ecf20Sopenharmony_ci u64 result; 7008c2ecf20Sopenharmony_ci 7018c2ecf20Sopenharmony_ci if (!divider_exists(div)) 7028c2ecf20Sopenharmony_ci return parent_rate; 7038c2ecf20Sopenharmony_ci 7048c2ecf20Sopenharmony_ci if (parent_rate > (unsigned long)LONG_MAX) 7058c2ecf20Sopenharmony_ci return 0; /* actually this would be a caller bug */ 7068c2ecf20Sopenharmony_ci 7078c2ecf20Sopenharmony_ci /* 7088c2ecf20Sopenharmony_ci * If there is a pre-divider, divide the scaled parent rate 7098c2ecf20Sopenharmony_ci * by the pre-divider value first. In this case--to improve 7108c2ecf20Sopenharmony_ci * accuracy--scale the parent rate by *both* the pre-divider 7118c2ecf20Sopenharmony_ci * value and the divider before actually computing the 7128c2ecf20Sopenharmony_ci * result of the pre-divider. 7138c2ecf20Sopenharmony_ci * 7148c2ecf20Sopenharmony_ci * If there's only one divider, just scale the parent rate. 7158c2ecf20Sopenharmony_ci */ 7168c2ecf20Sopenharmony_ci if (pre_div && divider_exists(pre_div)) { 7178c2ecf20Sopenharmony_ci u64 scaled_rate; 7188c2ecf20Sopenharmony_ci 7198c2ecf20Sopenharmony_ci scaled_rate = scale_rate(pre_div, parent_rate); 7208c2ecf20Sopenharmony_ci scaled_rate = scale_rate(div, scaled_rate); 7218c2ecf20Sopenharmony_ci scaled_div = divider_read_scaled(ccu, pre_div); 7228c2ecf20Sopenharmony_ci scaled_parent_rate = DIV_ROUND_CLOSEST_ULL(scaled_rate, 7238c2ecf20Sopenharmony_ci scaled_div); 7248c2ecf20Sopenharmony_ci } else { 7258c2ecf20Sopenharmony_ci scaled_parent_rate = scale_rate(div, parent_rate); 7268c2ecf20Sopenharmony_ci } 7278c2ecf20Sopenharmony_ci 7288c2ecf20Sopenharmony_ci /* 7298c2ecf20Sopenharmony_ci * Get the scaled divisor value, and divide the scaled 7308c2ecf20Sopenharmony_ci * parent rate by that to determine this clock's resulting 7318c2ecf20Sopenharmony_ci * rate. 7328c2ecf20Sopenharmony_ci */ 7338c2ecf20Sopenharmony_ci scaled_div = divider_read_scaled(ccu, div); 7348c2ecf20Sopenharmony_ci result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, scaled_div); 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_ci return (unsigned long)result; 7378c2ecf20Sopenharmony_ci} 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_ci/* 7408c2ecf20Sopenharmony_ci * Compute the output rate produced when a given parent rate is fed 7418c2ecf20Sopenharmony_ci * into two dividers. The pre-divider can be NULL, and even if it's 7428c2ecf20Sopenharmony_ci * non-null it may be nonexistent. It's also OK for the divider to 7438c2ecf20Sopenharmony_ci * be nonexistent, and in that case the pre-divider is also ignored. 7448c2ecf20Sopenharmony_ci * 7458c2ecf20Sopenharmony_ci * If scaled_div is non-null, it is used to return the scaled divisor 7468c2ecf20Sopenharmony_ci * value used by the (downstream) divider to produce that rate. 7478c2ecf20Sopenharmony_ci */ 7488c2ecf20Sopenharmony_cistatic long round_rate(struct ccu_data *ccu, struct bcm_clk_div *div, 7498c2ecf20Sopenharmony_ci struct bcm_clk_div *pre_div, 7508c2ecf20Sopenharmony_ci unsigned long rate, unsigned long parent_rate, 7518c2ecf20Sopenharmony_ci u64 *scaled_div) 7528c2ecf20Sopenharmony_ci{ 7538c2ecf20Sopenharmony_ci u64 scaled_parent_rate; 7548c2ecf20Sopenharmony_ci u64 min_scaled_div; 7558c2ecf20Sopenharmony_ci u64 max_scaled_div; 7568c2ecf20Sopenharmony_ci u64 best_scaled_div; 7578c2ecf20Sopenharmony_ci u64 result; 7588c2ecf20Sopenharmony_ci 7598c2ecf20Sopenharmony_ci BUG_ON(!divider_exists(div)); 7608c2ecf20Sopenharmony_ci BUG_ON(!rate); 7618c2ecf20Sopenharmony_ci BUG_ON(parent_rate > (u64)LONG_MAX); 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ci /* 7648c2ecf20Sopenharmony_ci * If there is a pre-divider, divide the scaled parent rate 7658c2ecf20Sopenharmony_ci * by the pre-divider value first. In this case--to improve 7668c2ecf20Sopenharmony_ci * accuracy--scale the parent rate by *both* the pre-divider 7678c2ecf20Sopenharmony_ci * value and the divider before actually computing the 7688c2ecf20Sopenharmony_ci * result of the pre-divider. 7698c2ecf20Sopenharmony_ci * 7708c2ecf20Sopenharmony_ci * If there's only one divider, just scale the parent rate. 7718c2ecf20Sopenharmony_ci * 7728c2ecf20Sopenharmony_ci * For simplicity we treat the pre-divider as fixed (for now). 7738c2ecf20Sopenharmony_ci */ 7748c2ecf20Sopenharmony_ci if (divider_exists(pre_div)) { 7758c2ecf20Sopenharmony_ci u64 scaled_rate; 7768c2ecf20Sopenharmony_ci u64 scaled_pre_div; 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci scaled_rate = scale_rate(pre_div, parent_rate); 7798c2ecf20Sopenharmony_ci scaled_rate = scale_rate(div, scaled_rate); 7808c2ecf20Sopenharmony_ci scaled_pre_div = divider_read_scaled(ccu, pre_div); 7818c2ecf20Sopenharmony_ci scaled_parent_rate = DIV_ROUND_CLOSEST_ULL(scaled_rate, 7828c2ecf20Sopenharmony_ci scaled_pre_div); 7838c2ecf20Sopenharmony_ci } else { 7848c2ecf20Sopenharmony_ci scaled_parent_rate = scale_rate(div, parent_rate); 7858c2ecf20Sopenharmony_ci } 7868c2ecf20Sopenharmony_ci 7878c2ecf20Sopenharmony_ci /* 7888c2ecf20Sopenharmony_ci * Compute the best possible divider and ensure it is in 7898c2ecf20Sopenharmony_ci * range. A fixed divider can't be changed, so just report 7908c2ecf20Sopenharmony_ci * the best we can do. 7918c2ecf20Sopenharmony_ci */ 7928c2ecf20Sopenharmony_ci if (!divider_is_fixed(div)) { 7938c2ecf20Sopenharmony_ci best_scaled_div = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, 7948c2ecf20Sopenharmony_ci rate); 7958c2ecf20Sopenharmony_ci min_scaled_div = scaled_div_min(div); 7968c2ecf20Sopenharmony_ci max_scaled_div = scaled_div_max(div); 7978c2ecf20Sopenharmony_ci if (best_scaled_div > max_scaled_div) 7988c2ecf20Sopenharmony_ci best_scaled_div = max_scaled_div; 7998c2ecf20Sopenharmony_ci else if (best_scaled_div < min_scaled_div) 8008c2ecf20Sopenharmony_ci best_scaled_div = min_scaled_div; 8018c2ecf20Sopenharmony_ci } else { 8028c2ecf20Sopenharmony_ci best_scaled_div = divider_read_scaled(ccu, div); 8038c2ecf20Sopenharmony_ci } 8048c2ecf20Sopenharmony_ci 8058c2ecf20Sopenharmony_ci /* OK, figure out the resulting rate */ 8068c2ecf20Sopenharmony_ci result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, best_scaled_div); 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_ci if (scaled_div) 8098c2ecf20Sopenharmony_ci *scaled_div = best_scaled_div; 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_ci return (long)result; 8128c2ecf20Sopenharmony_ci} 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_ci/* Common clock parent helpers */ 8158c2ecf20Sopenharmony_ci 8168c2ecf20Sopenharmony_ci/* 8178c2ecf20Sopenharmony_ci * For a given parent selector (register field) value, find the 8188c2ecf20Sopenharmony_ci * index into a selector's parent_sel array that contains it. 8198c2ecf20Sopenharmony_ci * Returns the index, or BAD_CLK_INDEX if it's not found. 8208c2ecf20Sopenharmony_ci */ 8218c2ecf20Sopenharmony_cistatic u8 parent_index(struct bcm_clk_sel *sel, u8 parent_sel) 8228c2ecf20Sopenharmony_ci{ 8238c2ecf20Sopenharmony_ci u8 i; 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ci BUG_ON(sel->parent_count > (u32)U8_MAX); 8268c2ecf20Sopenharmony_ci for (i = 0; i < sel->parent_count; i++) 8278c2ecf20Sopenharmony_ci if (sel->parent_sel[i] == parent_sel) 8288c2ecf20Sopenharmony_ci return i; 8298c2ecf20Sopenharmony_ci return BAD_CLK_INDEX; 8308c2ecf20Sopenharmony_ci} 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ci/* 8338c2ecf20Sopenharmony_ci * Fetch the current value of the selector, and translate that into 8348c2ecf20Sopenharmony_ci * its corresponding index in the parent array we registered with 8358c2ecf20Sopenharmony_ci * the clock framework. 8368c2ecf20Sopenharmony_ci * 8378c2ecf20Sopenharmony_ci * Returns parent array index that corresponds with the value found, 8388c2ecf20Sopenharmony_ci * or BAD_CLK_INDEX if the found value is out of range. 8398c2ecf20Sopenharmony_ci */ 8408c2ecf20Sopenharmony_cistatic u8 selector_read_index(struct ccu_data *ccu, struct bcm_clk_sel *sel) 8418c2ecf20Sopenharmony_ci{ 8428c2ecf20Sopenharmony_ci unsigned long flags; 8438c2ecf20Sopenharmony_ci u32 reg_val; 8448c2ecf20Sopenharmony_ci u32 parent_sel; 8458c2ecf20Sopenharmony_ci u8 index; 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_ci /* If there's no selector, there's only one parent */ 8488c2ecf20Sopenharmony_ci if (!selector_exists(sel)) 8498c2ecf20Sopenharmony_ci return 0; 8508c2ecf20Sopenharmony_ci 8518c2ecf20Sopenharmony_ci /* Get the value in the selector register */ 8528c2ecf20Sopenharmony_ci flags = ccu_lock(ccu); 8538c2ecf20Sopenharmony_ci reg_val = __ccu_read(ccu, sel->offset); 8548c2ecf20Sopenharmony_ci ccu_unlock(ccu, flags); 8558c2ecf20Sopenharmony_ci 8568c2ecf20Sopenharmony_ci parent_sel = bitfield_extract(reg_val, sel->shift, sel->width); 8578c2ecf20Sopenharmony_ci 8588c2ecf20Sopenharmony_ci /* Look up that selector's parent array index and return it */ 8598c2ecf20Sopenharmony_ci index = parent_index(sel, parent_sel); 8608c2ecf20Sopenharmony_ci if (index == BAD_CLK_INDEX) 8618c2ecf20Sopenharmony_ci pr_err("%s: out-of-range parent selector %u (%s 0x%04x)\n", 8628c2ecf20Sopenharmony_ci __func__, parent_sel, ccu->name, sel->offset); 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci return index; 8658c2ecf20Sopenharmony_ci} 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_ci/* 8688c2ecf20Sopenharmony_ci * Commit our desired selector value to the hardware. 8698c2ecf20Sopenharmony_ci * 8708c2ecf20Sopenharmony_ci * Returns 0 on success. Returns -EINVAL for invalid arguments. 8718c2ecf20Sopenharmony_ci * Returns -ENXIO if gating failed, and -EIO if a trigger failed. 8728c2ecf20Sopenharmony_ci */ 8738c2ecf20Sopenharmony_cistatic int 8748c2ecf20Sopenharmony_ci__sel_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate, 8758c2ecf20Sopenharmony_ci struct bcm_clk_sel *sel, struct bcm_clk_trig *trig) 8768c2ecf20Sopenharmony_ci{ 8778c2ecf20Sopenharmony_ci u32 parent_sel; 8788c2ecf20Sopenharmony_ci u32 reg_val; 8798c2ecf20Sopenharmony_ci bool enabled; 8808c2ecf20Sopenharmony_ci int ret = 0; 8818c2ecf20Sopenharmony_ci 8828c2ecf20Sopenharmony_ci BUG_ON(!selector_exists(sel)); 8838c2ecf20Sopenharmony_ci 8848c2ecf20Sopenharmony_ci /* 8858c2ecf20Sopenharmony_ci * If we're just initializing the selector, and no initial 8868c2ecf20Sopenharmony_ci * state was defined in the device tree, we just find out 8878c2ecf20Sopenharmony_ci * what its current value is rather than updating it. 8888c2ecf20Sopenharmony_ci */ 8898c2ecf20Sopenharmony_ci if (sel->clk_index == BAD_CLK_INDEX) { 8908c2ecf20Sopenharmony_ci u8 index; 8918c2ecf20Sopenharmony_ci 8928c2ecf20Sopenharmony_ci reg_val = __ccu_read(ccu, sel->offset); 8938c2ecf20Sopenharmony_ci parent_sel = bitfield_extract(reg_val, sel->shift, sel->width); 8948c2ecf20Sopenharmony_ci index = parent_index(sel, parent_sel); 8958c2ecf20Sopenharmony_ci if (index == BAD_CLK_INDEX) 8968c2ecf20Sopenharmony_ci return -EINVAL; 8978c2ecf20Sopenharmony_ci sel->clk_index = index; 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_ci return 0; 9008c2ecf20Sopenharmony_ci } 9018c2ecf20Sopenharmony_ci 9028c2ecf20Sopenharmony_ci BUG_ON((u32)sel->clk_index >= sel->parent_count); 9038c2ecf20Sopenharmony_ci parent_sel = sel->parent_sel[sel->clk_index]; 9048c2ecf20Sopenharmony_ci 9058c2ecf20Sopenharmony_ci /* Clock needs to be enabled before changing the parent */ 9068c2ecf20Sopenharmony_ci enabled = __is_clk_gate_enabled(ccu, gate); 9078c2ecf20Sopenharmony_ci if (!enabled && !__clk_gate(ccu, gate, true)) 9088c2ecf20Sopenharmony_ci return -ENXIO; 9098c2ecf20Sopenharmony_ci 9108c2ecf20Sopenharmony_ci /* Replace the selector value and record the result */ 9118c2ecf20Sopenharmony_ci reg_val = __ccu_read(ccu, sel->offset); 9128c2ecf20Sopenharmony_ci reg_val = bitfield_replace(reg_val, sel->shift, sel->width, parent_sel); 9138c2ecf20Sopenharmony_ci __ccu_write(ccu, sel->offset, reg_val); 9148c2ecf20Sopenharmony_ci 9158c2ecf20Sopenharmony_ci /* If the trigger fails we still want to disable the gate */ 9168c2ecf20Sopenharmony_ci if (!__clk_trigger(ccu, trig)) 9178c2ecf20Sopenharmony_ci ret = -EIO; 9188c2ecf20Sopenharmony_ci 9198c2ecf20Sopenharmony_ci /* Disable the clock again if it was disabled to begin with */ 9208c2ecf20Sopenharmony_ci if (!enabled && !__clk_gate(ccu, gate, false)) 9218c2ecf20Sopenharmony_ci ret = ret ? ret : -ENXIO; /* return first error */ 9228c2ecf20Sopenharmony_ci 9238c2ecf20Sopenharmony_ci return ret; 9248c2ecf20Sopenharmony_ci} 9258c2ecf20Sopenharmony_ci 9268c2ecf20Sopenharmony_ci/* 9278c2ecf20Sopenharmony_ci * Initialize a selector by committing our desired state to hardware 9288c2ecf20Sopenharmony_ci * without the usual checks to see if it's already set up that way. 9298c2ecf20Sopenharmony_ci * Returns true if successful, false otherwise. 9308c2ecf20Sopenharmony_ci */ 9318c2ecf20Sopenharmony_cistatic bool sel_init(struct ccu_data *ccu, struct bcm_clk_gate *gate, 9328c2ecf20Sopenharmony_ci struct bcm_clk_sel *sel, struct bcm_clk_trig *trig) 9338c2ecf20Sopenharmony_ci{ 9348c2ecf20Sopenharmony_ci if (!selector_exists(sel)) 9358c2ecf20Sopenharmony_ci return true; 9368c2ecf20Sopenharmony_ci return !__sel_commit(ccu, gate, sel, trig); 9378c2ecf20Sopenharmony_ci} 9388c2ecf20Sopenharmony_ci 9398c2ecf20Sopenharmony_ci/* 9408c2ecf20Sopenharmony_ci * Write a new value into a selector register to switch to a 9418c2ecf20Sopenharmony_ci * different parent clock. Returns 0 on success, or an error code 9428c2ecf20Sopenharmony_ci * (from __sel_commit()) otherwise. 9438c2ecf20Sopenharmony_ci */ 9448c2ecf20Sopenharmony_cistatic int selector_write(struct ccu_data *ccu, struct bcm_clk_gate *gate, 9458c2ecf20Sopenharmony_ci struct bcm_clk_sel *sel, struct bcm_clk_trig *trig, 9468c2ecf20Sopenharmony_ci u8 index) 9478c2ecf20Sopenharmony_ci{ 9488c2ecf20Sopenharmony_ci unsigned long flags; 9498c2ecf20Sopenharmony_ci u8 previous; 9508c2ecf20Sopenharmony_ci int ret; 9518c2ecf20Sopenharmony_ci 9528c2ecf20Sopenharmony_ci previous = sel->clk_index; 9538c2ecf20Sopenharmony_ci if (previous == index) 9548c2ecf20Sopenharmony_ci return 0; /* No change */ 9558c2ecf20Sopenharmony_ci 9568c2ecf20Sopenharmony_ci sel->clk_index = index; 9578c2ecf20Sopenharmony_ci 9588c2ecf20Sopenharmony_ci flags = ccu_lock(ccu); 9598c2ecf20Sopenharmony_ci __ccu_write_enable(ccu); 9608c2ecf20Sopenharmony_ci 9618c2ecf20Sopenharmony_ci ret = __sel_commit(ccu, gate, sel, trig); 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci __ccu_write_disable(ccu); 9648c2ecf20Sopenharmony_ci ccu_unlock(ccu, flags); 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci if (ret) 9678c2ecf20Sopenharmony_ci sel->clk_index = previous; /* Revert the change */ 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci return ret; 9708c2ecf20Sopenharmony_ci} 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci/* Clock operations */ 9738c2ecf20Sopenharmony_ci 9748c2ecf20Sopenharmony_cistatic int kona_peri_clk_enable(struct clk_hw *hw) 9758c2ecf20Sopenharmony_ci{ 9768c2ecf20Sopenharmony_ci struct kona_clk *bcm_clk = to_kona_clk(hw); 9778c2ecf20Sopenharmony_ci struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate; 9788c2ecf20Sopenharmony_ci 9798c2ecf20Sopenharmony_ci return clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, true); 9808c2ecf20Sopenharmony_ci} 9818c2ecf20Sopenharmony_ci 9828c2ecf20Sopenharmony_cistatic void kona_peri_clk_disable(struct clk_hw *hw) 9838c2ecf20Sopenharmony_ci{ 9848c2ecf20Sopenharmony_ci struct kona_clk *bcm_clk = to_kona_clk(hw); 9858c2ecf20Sopenharmony_ci struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate; 9868c2ecf20Sopenharmony_ci 9878c2ecf20Sopenharmony_ci (void)clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, false); 9888c2ecf20Sopenharmony_ci} 9898c2ecf20Sopenharmony_ci 9908c2ecf20Sopenharmony_cistatic int kona_peri_clk_is_enabled(struct clk_hw *hw) 9918c2ecf20Sopenharmony_ci{ 9928c2ecf20Sopenharmony_ci struct kona_clk *bcm_clk = to_kona_clk(hw); 9938c2ecf20Sopenharmony_ci struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate; 9948c2ecf20Sopenharmony_ci 9958c2ecf20Sopenharmony_ci return is_clk_gate_enabled(bcm_clk->ccu, gate) ? 1 : 0; 9968c2ecf20Sopenharmony_ci} 9978c2ecf20Sopenharmony_ci 9988c2ecf20Sopenharmony_cistatic unsigned long kona_peri_clk_recalc_rate(struct clk_hw *hw, 9998c2ecf20Sopenharmony_ci unsigned long parent_rate) 10008c2ecf20Sopenharmony_ci{ 10018c2ecf20Sopenharmony_ci struct kona_clk *bcm_clk = to_kona_clk(hw); 10028c2ecf20Sopenharmony_ci struct peri_clk_data *data = bcm_clk->u.peri; 10038c2ecf20Sopenharmony_ci 10048c2ecf20Sopenharmony_ci return clk_recalc_rate(bcm_clk->ccu, &data->div, &data->pre_div, 10058c2ecf20Sopenharmony_ci parent_rate); 10068c2ecf20Sopenharmony_ci} 10078c2ecf20Sopenharmony_ci 10088c2ecf20Sopenharmony_cistatic long kona_peri_clk_round_rate(struct clk_hw *hw, unsigned long rate, 10098c2ecf20Sopenharmony_ci unsigned long *parent_rate) 10108c2ecf20Sopenharmony_ci{ 10118c2ecf20Sopenharmony_ci struct kona_clk *bcm_clk = to_kona_clk(hw); 10128c2ecf20Sopenharmony_ci struct bcm_clk_div *div = &bcm_clk->u.peri->div; 10138c2ecf20Sopenharmony_ci 10148c2ecf20Sopenharmony_ci if (!divider_exists(div)) 10158c2ecf20Sopenharmony_ci return clk_hw_get_rate(hw); 10168c2ecf20Sopenharmony_ci 10178c2ecf20Sopenharmony_ci /* Quietly avoid a zero rate */ 10188c2ecf20Sopenharmony_ci return round_rate(bcm_clk->ccu, div, &bcm_clk->u.peri->pre_div, 10198c2ecf20Sopenharmony_ci rate ? rate : 1, *parent_rate, NULL); 10208c2ecf20Sopenharmony_ci} 10218c2ecf20Sopenharmony_ci 10228c2ecf20Sopenharmony_cistatic int kona_peri_clk_determine_rate(struct clk_hw *hw, 10238c2ecf20Sopenharmony_ci struct clk_rate_request *req) 10248c2ecf20Sopenharmony_ci{ 10258c2ecf20Sopenharmony_ci struct kona_clk *bcm_clk = to_kona_clk(hw); 10268c2ecf20Sopenharmony_ci struct clk_hw *current_parent; 10278c2ecf20Sopenharmony_ci unsigned long parent_rate; 10288c2ecf20Sopenharmony_ci unsigned long best_delta; 10298c2ecf20Sopenharmony_ci unsigned long best_rate; 10308c2ecf20Sopenharmony_ci u32 parent_count; 10318c2ecf20Sopenharmony_ci long rate; 10328c2ecf20Sopenharmony_ci u32 which; 10338c2ecf20Sopenharmony_ci 10348c2ecf20Sopenharmony_ci /* 10358c2ecf20Sopenharmony_ci * If there is no other parent to choose, use the current one. 10368c2ecf20Sopenharmony_ci * Note: We don't honor (or use) CLK_SET_RATE_NO_REPARENT. 10378c2ecf20Sopenharmony_ci */ 10388c2ecf20Sopenharmony_ci WARN_ON_ONCE(bcm_clk->init_data.flags & CLK_SET_RATE_NO_REPARENT); 10398c2ecf20Sopenharmony_ci parent_count = (u32)bcm_clk->init_data.num_parents; 10408c2ecf20Sopenharmony_ci if (parent_count < 2) { 10418c2ecf20Sopenharmony_ci rate = kona_peri_clk_round_rate(hw, req->rate, 10428c2ecf20Sopenharmony_ci &req->best_parent_rate); 10438c2ecf20Sopenharmony_ci if (rate < 0) 10448c2ecf20Sopenharmony_ci return rate; 10458c2ecf20Sopenharmony_ci 10468c2ecf20Sopenharmony_ci req->rate = rate; 10478c2ecf20Sopenharmony_ci return 0; 10488c2ecf20Sopenharmony_ci } 10498c2ecf20Sopenharmony_ci 10508c2ecf20Sopenharmony_ci /* Unless we can do better, stick with current parent */ 10518c2ecf20Sopenharmony_ci current_parent = clk_hw_get_parent(hw); 10528c2ecf20Sopenharmony_ci parent_rate = clk_hw_get_rate(current_parent); 10538c2ecf20Sopenharmony_ci best_rate = kona_peri_clk_round_rate(hw, req->rate, &parent_rate); 10548c2ecf20Sopenharmony_ci best_delta = abs(best_rate - req->rate); 10558c2ecf20Sopenharmony_ci 10568c2ecf20Sopenharmony_ci /* Check whether any other parent clock can produce a better result */ 10578c2ecf20Sopenharmony_ci for (which = 0; which < parent_count; which++) { 10588c2ecf20Sopenharmony_ci struct clk_hw *parent = clk_hw_get_parent_by_index(hw, which); 10598c2ecf20Sopenharmony_ci unsigned long delta; 10608c2ecf20Sopenharmony_ci unsigned long other_rate; 10618c2ecf20Sopenharmony_ci 10628c2ecf20Sopenharmony_ci BUG_ON(!parent); 10638c2ecf20Sopenharmony_ci if (parent == current_parent) 10648c2ecf20Sopenharmony_ci continue; 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_ci /* We don't support CLK_SET_RATE_PARENT */ 10678c2ecf20Sopenharmony_ci parent_rate = clk_hw_get_rate(parent); 10688c2ecf20Sopenharmony_ci other_rate = kona_peri_clk_round_rate(hw, req->rate, 10698c2ecf20Sopenharmony_ci &parent_rate); 10708c2ecf20Sopenharmony_ci delta = abs(other_rate - req->rate); 10718c2ecf20Sopenharmony_ci if (delta < best_delta) { 10728c2ecf20Sopenharmony_ci best_delta = delta; 10738c2ecf20Sopenharmony_ci best_rate = other_rate; 10748c2ecf20Sopenharmony_ci req->best_parent_hw = parent; 10758c2ecf20Sopenharmony_ci req->best_parent_rate = parent_rate; 10768c2ecf20Sopenharmony_ci } 10778c2ecf20Sopenharmony_ci } 10788c2ecf20Sopenharmony_ci 10798c2ecf20Sopenharmony_ci req->rate = best_rate; 10808c2ecf20Sopenharmony_ci return 0; 10818c2ecf20Sopenharmony_ci} 10828c2ecf20Sopenharmony_ci 10838c2ecf20Sopenharmony_cistatic int kona_peri_clk_set_parent(struct clk_hw *hw, u8 index) 10848c2ecf20Sopenharmony_ci{ 10858c2ecf20Sopenharmony_ci struct kona_clk *bcm_clk = to_kona_clk(hw); 10868c2ecf20Sopenharmony_ci struct peri_clk_data *data = bcm_clk->u.peri; 10878c2ecf20Sopenharmony_ci struct bcm_clk_sel *sel = &data->sel; 10888c2ecf20Sopenharmony_ci struct bcm_clk_trig *trig; 10898c2ecf20Sopenharmony_ci int ret; 10908c2ecf20Sopenharmony_ci 10918c2ecf20Sopenharmony_ci BUG_ON(index >= sel->parent_count); 10928c2ecf20Sopenharmony_ci 10938c2ecf20Sopenharmony_ci /* If there's only one parent we don't require a selector */ 10948c2ecf20Sopenharmony_ci if (!selector_exists(sel)) 10958c2ecf20Sopenharmony_ci return 0; 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_ci /* 10988c2ecf20Sopenharmony_ci * The regular trigger is used by default, but if there's a 10998c2ecf20Sopenharmony_ci * pre-trigger we want to use that instead. 11008c2ecf20Sopenharmony_ci */ 11018c2ecf20Sopenharmony_ci trig = trigger_exists(&data->pre_trig) ? &data->pre_trig 11028c2ecf20Sopenharmony_ci : &data->trig; 11038c2ecf20Sopenharmony_ci 11048c2ecf20Sopenharmony_ci ret = selector_write(bcm_clk->ccu, &data->gate, sel, trig, index); 11058c2ecf20Sopenharmony_ci if (ret == -ENXIO) { 11068c2ecf20Sopenharmony_ci pr_err("%s: gating failure for %s\n", __func__, 11078c2ecf20Sopenharmony_ci bcm_clk->init_data.name); 11088c2ecf20Sopenharmony_ci ret = -EIO; /* Don't proliferate weird errors */ 11098c2ecf20Sopenharmony_ci } else if (ret == -EIO) { 11108c2ecf20Sopenharmony_ci pr_err("%s: %strigger failed for %s\n", __func__, 11118c2ecf20Sopenharmony_ci trig == &data->pre_trig ? "pre-" : "", 11128c2ecf20Sopenharmony_ci bcm_clk->init_data.name); 11138c2ecf20Sopenharmony_ci } 11148c2ecf20Sopenharmony_ci 11158c2ecf20Sopenharmony_ci return ret; 11168c2ecf20Sopenharmony_ci} 11178c2ecf20Sopenharmony_ci 11188c2ecf20Sopenharmony_cistatic u8 kona_peri_clk_get_parent(struct clk_hw *hw) 11198c2ecf20Sopenharmony_ci{ 11208c2ecf20Sopenharmony_ci struct kona_clk *bcm_clk = to_kona_clk(hw); 11218c2ecf20Sopenharmony_ci struct peri_clk_data *data = bcm_clk->u.peri; 11228c2ecf20Sopenharmony_ci u8 index; 11238c2ecf20Sopenharmony_ci 11248c2ecf20Sopenharmony_ci index = selector_read_index(bcm_clk->ccu, &data->sel); 11258c2ecf20Sopenharmony_ci 11268c2ecf20Sopenharmony_ci /* Not all callers would handle an out-of-range value gracefully */ 11278c2ecf20Sopenharmony_ci return index == BAD_CLK_INDEX ? 0 : index; 11288c2ecf20Sopenharmony_ci} 11298c2ecf20Sopenharmony_ci 11308c2ecf20Sopenharmony_cistatic int kona_peri_clk_set_rate(struct clk_hw *hw, unsigned long rate, 11318c2ecf20Sopenharmony_ci unsigned long parent_rate) 11328c2ecf20Sopenharmony_ci{ 11338c2ecf20Sopenharmony_ci struct kona_clk *bcm_clk = to_kona_clk(hw); 11348c2ecf20Sopenharmony_ci struct peri_clk_data *data = bcm_clk->u.peri; 11358c2ecf20Sopenharmony_ci struct bcm_clk_div *div = &data->div; 11368c2ecf20Sopenharmony_ci u64 scaled_div = 0; 11378c2ecf20Sopenharmony_ci int ret; 11388c2ecf20Sopenharmony_ci 11398c2ecf20Sopenharmony_ci if (parent_rate > (unsigned long)LONG_MAX) 11408c2ecf20Sopenharmony_ci return -EINVAL; 11418c2ecf20Sopenharmony_ci 11428c2ecf20Sopenharmony_ci if (rate == clk_hw_get_rate(hw)) 11438c2ecf20Sopenharmony_ci return 0; 11448c2ecf20Sopenharmony_ci 11458c2ecf20Sopenharmony_ci if (!divider_exists(div)) 11468c2ecf20Sopenharmony_ci return rate == parent_rate ? 0 : -EINVAL; 11478c2ecf20Sopenharmony_ci 11488c2ecf20Sopenharmony_ci /* 11498c2ecf20Sopenharmony_ci * A fixed divider can't be changed. (Nor can a fixed 11508c2ecf20Sopenharmony_ci * pre-divider be, but for now we never actually try to 11518c2ecf20Sopenharmony_ci * change that.) Tolerate a request for a no-op change. 11528c2ecf20Sopenharmony_ci */ 11538c2ecf20Sopenharmony_ci if (divider_is_fixed(&data->div)) 11548c2ecf20Sopenharmony_ci return rate == parent_rate ? 0 : -EINVAL; 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_ci /* 11578c2ecf20Sopenharmony_ci * Get the scaled divisor value needed to achieve a clock 11588c2ecf20Sopenharmony_ci * rate as close as possible to what was requested, given 11598c2ecf20Sopenharmony_ci * the parent clock rate supplied. 11608c2ecf20Sopenharmony_ci */ 11618c2ecf20Sopenharmony_ci (void)round_rate(bcm_clk->ccu, div, &data->pre_div, 11628c2ecf20Sopenharmony_ci rate ? rate : 1, parent_rate, &scaled_div); 11638c2ecf20Sopenharmony_ci 11648c2ecf20Sopenharmony_ci /* 11658c2ecf20Sopenharmony_ci * We aren't updating any pre-divider at this point, so 11668c2ecf20Sopenharmony_ci * we'll use the regular trigger. 11678c2ecf20Sopenharmony_ci */ 11688c2ecf20Sopenharmony_ci ret = divider_write(bcm_clk->ccu, &data->gate, &data->div, 11698c2ecf20Sopenharmony_ci &data->trig, scaled_div); 11708c2ecf20Sopenharmony_ci if (ret == -ENXIO) { 11718c2ecf20Sopenharmony_ci pr_err("%s: gating failure for %s\n", __func__, 11728c2ecf20Sopenharmony_ci bcm_clk->init_data.name); 11738c2ecf20Sopenharmony_ci ret = -EIO; /* Don't proliferate weird errors */ 11748c2ecf20Sopenharmony_ci } else if (ret == -EIO) { 11758c2ecf20Sopenharmony_ci pr_err("%s: trigger failed for %s\n", __func__, 11768c2ecf20Sopenharmony_ci bcm_clk->init_data.name); 11778c2ecf20Sopenharmony_ci } 11788c2ecf20Sopenharmony_ci 11798c2ecf20Sopenharmony_ci return ret; 11808c2ecf20Sopenharmony_ci} 11818c2ecf20Sopenharmony_ci 11828c2ecf20Sopenharmony_cistruct clk_ops kona_peri_clk_ops = { 11838c2ecf20Sopenharmony_ci .enable = kona_peri_clk_enable, 11848c2ecf20Sopenharmony_ci .disable = kona_peri_clk_disable, 11858c2ecf20Sopenharmony_ci .is_enabled = kona_peri_clk_is_enabled, 11868c2ecf20Sopenharmony_ci .recalc_rate = kona_peri_clk_recalc_rate, 11878c2ecf20Sopenharmony_ci .determine_rate = kona_peri_clk_determine_rate, 11888c2ecf20Sopenharmony_ci .set_parent = kona_peri_clk_set_parent, 11898c2ecf20Sopenharmony_ci .get_parent = kona_peri_clk_get_parent, 11908c2ecf20Sopenharmony_ci .set_rate = kona_peri_clk_set_rate, 11918c2ecf20Sopenharmony_ci}; 11928c2ecf20Sopenharmony_ci 11938c2ecf20Sopenharmony_ci/* Put a peripheral clock into its initial state */ 11948c2ecf20Sopenharmony_cistatic bool __peri_clk_init(struct kona_clk *bcm_clk) 11958c2ecf20Sopenharmony_ci{ 11968c2ecf20Sopenharmony_ci struct ccu_data *ccu = bcm_clk->ccu; 11978c2ecf20Sopenharmony_ci struct peri_clk_data *peri = bcm_clk->u.peri; 11988c2ecf20Sopenharmony_ci const char *name = bcm_clk->init_data.name; 11998c2ecf20Sopenharmony_ci struct bcm_clk_trig *trig; 12008c2ecf20Sopenharmony_ci 12018c2ecf20Sopenharmony_ci BUG_ON(bcm_clk->type != bcm_clk_peri); 12028c2ecf20Sopenharmony_ci 12038c2ecf20Sopenharmony_ci if (!policy_init(ccu, &peri->policy)) { 12048c2ecf20Sopenharmony_ci pr_err("%s: error initializing policy for %s\n", 12058c2ecf20Sopenharmony_ci __func__, name); 12068c2ecf20Sopenharmony_ci return false; 12078c2ecf20Sopenharmony_ci } 12088c2ecf20Sopenharmony_ci if (!gate_init(ccu, &peri->gate)) { 12098c2ecf20Sopenharmony_ci pr_err("%s: error initializing gate for %s\n", __func__, name); 12108c2ecf20Sopenharmony_ci return false; 12118c2ecf20Sopenharmony_ci } 12128c2ecf20Sopenharmony_ci if (!hyst_init(ccu, &peri->hyst)) { 12138c2ecf20Sopenharmony_ci pr_err("%s: error initializing hyst for %s\n", __func__, name); 12148c2ecf20Sopenharmony_ci return false; 12158c2ecf20Sopenharmony_ci } 12168c2ecf20Sopenharmony_ci if (!div_init(ccu, &peri->gate, &peri->div, &peri->trig)) { 12178c2ecf20Sopenharmony_ci pr_err("%s: error initializing divider for %s\n", __func__, 12188c2ecf20Sopenharmony_ci name); 12198c2ecf20Sopenharmony_ci return false; 12208c2ecf20Sopenharmony_ci } 12218c2ecf20Sopenharmony_ci 12228c2ecf20Sopenharmony_ci /* 12238c2ecf20Sopenharmony_ci * For the pre-divider and selector, the pre-trigger is used 12248c2ecf20Sopenharmony_ci * if it's present, otherwise we just use the regular trigger. 12258c2ecf20Sopenharmony_ci */ 12268c2ecf20Sopenharmony_ci trig = trigger_exists(&peri->pre_trig) ? &peri->pre_trig 12278c2ecf20Sopenharmony_ci : &peri->trig; 12288c2ecf20Sopenharmony_ci 12298c2ecf20Sopenharmony_ci if (!div_init(ccu, &peri->gate, &peri->pre_div, trig)) { 12308c2ecf20Sopenharmony_ci pr_err("%s: error initializing pre-divider for %s\n", __func__, 12318c2ecf20Sopenharmony_ci name); 12328c2ecf20Sopenharmony_ci return false; 12338c2ecf20Sopenharmony_ci } 12348c2ecf20Sopenharmony_ci 12358c2ecf20Sopenharmony_ci if (!sel_init(ccu, &peri->gate, &peri->sel, trig)) { 12368c2ecf20Sopenharmony_ci pr_err("%s: error initializing selector for %s\n", __func__, 12378c2ecf20Sopenharmony_ci name); 12388c2ecf20Sopenharmony_ci return false; 12398c2ecf20Sopenharmony_ci } 12408c2ecf20Sopenharmony_ci 12418c2ecf20Sopenharmony_ci return true; 12428c2ecf20Sopenharmony_ci} 12438c2ecf20Sopenharmony_ci 12448c2ecf20Sopenharmony_cistatic bool __kona_clk_init(struct kona_clk *bcm_clk) 12458c2ecf20Sopenharmony_ci{ 12468c2ecf20Sopenharmony_ci switch (bcm_clk->type) { 12478c2ecf20Sopenharmony_ci case bcm_clk_peri: 12488c2ecf20Sopenharmony_ci return __peri_clk_init(bcm_clk); 12498c2ecf20Sopenharmony_ci default: 12508c2ecf20Sopenharmony_ci BUG(); 12518c2ecf20Sopenharmony_ci } 12528c2ecf20Sopenharmony_ci return false; 12538c2ecf20Sopenharmony_ci} 12548c2ecf20Sopenharmony_ci 12558c2ecf20Sopenharmony_ci/* Set a CCU and all its clocks into their desired initial state */ 12568c2ecf20Sopenharmony_cibool __init kona_ccu_init(struct ccu_data *ccu) 12578c2ecf20Sopenharmony_ci{ 12588c2ecf20Sopenharmony_ci unsigned long flags; 12598c2ecf20Sopenharmony_ci unsigned int which; 12608c2ecf20Sopenharmony_ci struct kona_clk *kona_clks = ccu->kona_clks; 12618c2ecf20Sopenharmony_ci bool success = true; 12628c2ecf20Sopenharmony_ci 12638c2ecf20Sopenharmony_ci flags = ccu_lock(ccu); 12648c2ecf20Sopenharmony_ci __ccu_write_enable(ccu); 12658c2ecf20Sopenharmony_ci 12668c2ecf20Sopenharmony_ci for (which = 0; which < ccu->clk_num; which++) { 12678c2ecf20Sopenharmony_ci struct kona_clk *bcm_clk = &kona_clks[which]; 12688c2ecf20Sopenharmony_ci 12698c2ecf20Sopenharmony_ci if (!bcm_clk->ccu) 12708c2ecf20Sopenharmony_ci continue; 12718c2ecf20Sopenharmony_ci 12728c2ecf20Sopenharmony_ci success &= __kona_clk_init(bcm_clk); 12738c2ecf20Sopenharmony_ci } 12748c2ecf20Sopenharmony_ci 12758c2ecf20Sopenharmony_ci __ccu_write_disable(ccu); 12768c2ecf20Sopenharmony_ci ccu_unlock(ccu, flags); 12778c2ecf20Sopenharmony_ci return success; 12788c2ecf20Sopenharmony_ci} 1279