18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * R-Car Gen3 Clock Pulse Generator 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2015-2018 Glider bvba 68c2ecf20Sopenharmony_ci * Copyright (C) 2019 Renesas Electronics Corp. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Based on clk-rcar-gen3.c 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Copyright (C) 2015 Renesas Electronics Corp. 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/bug.h> 148c2ecf20Sopenharmony_ci#include <linux/bitfield.h> 158c2ecf20Sopenharmony_ci#include <linux/clk.h> 168c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 178c2ecf20Sopenharmony_ci#include <linux/device.h> 188c2ecf20Sopenharmony_ci#include <linux/err.h> 198c2ecf20Sopenharmony_ci#include <linux/init.h> 208c2ecf20Sopenharmony_ci#include <linux/io.h> 218c2ecf20Sopenharmony_ci#include <linux/pm.h> 228c2ecf20Sopenharmony_ci#include <linux/slab.h> 238c2ecf20Sopenharmony_ci#include <linux/sys_soc.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#include "renesas-cpg-mssr.h" 268c2ecf20Sopenharmony_ci#include "rcar-gen3-cpg.h" 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define CPG_PLL0CR 0x00d8 298c2ecf20Sopenharmony_ci#define CPG_PLL2CR 0x002c 308c2ecf20Sopenharmony_ci#define CPG_PLL4CR 0x01f4 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define CPG_RCKCR_CKSEL BIT(15) /* RCLK Clock Source Select */ 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic spinlock_t cpg_lock; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic void cpg_reg_modify(void __iomem *reg, u32 clear, u32 set) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci unsigned long flags; 398c2ecf20Sopenharmony_ci u32 val; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci spin_lock_irqsave(&cpg_lock, flags); 428c2ecf20Sopenharmony_ci val = readl(reg); 438c2ecf20Sopenharmony_ci val &= ~clear; 448c2ecf20Sopenharmony_ci val |= set; 458c2ecf20Sopenharmony_ci writel(val, reg); 468c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&cpg_lock, flags); 478c2ecf20Sopenharmony_ci}; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistruct cpg_simple_notifier { 508c2ecf20Sopenharmony_ci struct notifier_block nb; 518c2ecf20Sopenharmony_ci void __iomem *reg; 528c2ecf20Sopenharmony_ci u32 saved; 538c2ecf20Sopenharmony_ci}; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic int cpg_simple_notifier_call(struct notifier_block *nb, 568c2ecf20Sopenharmony_ci unsigned long action, void *data) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct cpg_simple_notifier *csn = 598c2ecf20Sopenharmony_ci container_of(nb, struct cpg_simple_notifier, nb); 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci switch (action) { 628c2ecf20Sopenharmony_ci case PM_EVENT_SUSPEND: 638c2ecf20Sopenharmony_ci csn->saved = readl(csn->reg); 648c2ecf20Sopenharmony_ci return NOTIFY_OK; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci case PM_EVENT_RESUME: 678c2ecf20Sopenharmony_ci writel(csn->saved, csn->reg); 688c2ecf20Sopenharmony_ci return NOTIFY_OK; 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci return NOTIFY_DONE; 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic void cpg_simple_notifier_register(struct raw_notifier_head *notifiers, 748c2ecf20Sopenharmony_ci struct cpg_simple_notifier *csn) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci csn->nb.notifier_call = cpg_simple_notifier_call; 778c2ecf20Sopenharmony_ci raw_notifier_chain_register(notifiers, &csn->nb); 788c2ecf20Sopenharmony_ci} 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci/* 818c2ecf20Sopenharmony_ci * Z Clock & Z2 Clock 828c2ecf20Sopenharmony_ci * 838c2ecf20Sopenharmony_ci * Traits of this clock: 848c2ecf20Sopenharmony_ci * prepare - clk_prepare only ensures that parents are prepared 858c2ecf20Sopenharmony_ci * enable - clk_enable only ensures that parents are enabled 868c2ecf20Sopenharmony_ci * rate - rate is adjustable. clk->rate = (parent->rate * mult / 32 ) / 2 878c2ecf20Sopenharmony_ci * parent - fixed parent. No clk_set_parent support 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_ci#define CPG_FRQCRB 0x00000004 908c2ecf20Sopenharmony_ci#define CPG_FRQCRB_KICK BIT(31) 918c2ecf20Sopenharmony_ci#define CPG_FRQCRC 0x000000e0 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistruct cpg_z_clk { 948c2ecf20Sopenharmony_ci struct clk_hw hw; 958c2ecf20Sopenharmony_ci void __iomem *reg; 968c2ecf20Sopenharmony_ci void __iomem *kick_reg; 978c2ecf20Sopenharmony_ci unsigned long mask; 988c2ecf20Sopenharmony_ci unsigned int fixed_div; 998c2ecf20Sopenharmony_ci}; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci#define to_z_clk(_hw) container_of(_hw, struct cpg_z_clk, hw) 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cistatic unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw, 1048c2ecf20Sopenharmony_ci unsigned long parent_rate) 1058c2ecf20Sopenharmony_ci{ 1068c2ecf20Sopenharmony_ci struct cpg_z_clk *zclk = to_z_clk(hw); 1078c2ecf20Sopenharmony_ci unsigned int mult; 1088c2ecf20Sopenharmony_ci u32 val; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci val = readl(zclk->reg) & zclk->mask; 1118c2ecf20Sopenharmony_ci mult = 32 - (val >> __ffs(zclk->mask)); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, 1148c2ecf20Sopenharmony_ci 32 * zclk->fixed_div); 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic int cpg_z_clk_determine_rate(struct clk_hw *hw, 1188c2ecf20Sopenharmony_ci struct clk_rate_request *req) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci struct cpg_z_clk *zclk = to_z_clk(hw); 1218c2ecf20Sopenharmony_ci unsigned int min_mult, max_mult, mult; 1228c2ecf20Sopenharmony_ci unsigned long prate; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci prate = req->best_parent_rate / zclk->fixed_div; 1258c2ecf20Sopenharmony_ci min_mult = max(div64_ul(req->min_rate * 32ULL, prate), 1ULL); 1268c2ecf20Sopenharmony_ci max_mult = min(div64_ul(req->max_rate * 32ULL, prate), 32ULL); 1278c2ecf20Sopenharmony_ci if (max_mult < min_mult) 1288c2ecf20Sopenharmony_ci return -EINVAL; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci mult = div64_ul(req->rate * 32ULL, prate); 1318c2ecf20Sopenharmony_ci mult = clamp(mult, min_mult, max_mult); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci req->rate = div_u64((u64)prate * mult, 32); 1348c2ecf20Sopenharmony_ci return 0; 1358c2ecf20Sopenharmony_ci} 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_cistatic int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate, 1388c2ecf20Sopenharmony_ci unsigned long parent_rate) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci struct cpg_z_clk *zclk = to_z_clk(hw); 1418c2ecf20Sopenharmony_ci unsigned int mult; 1428c2ecf20Sopenharmony_ci unsigned int i; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci mult = DIV64_U64_ROUND_CLOSEST(rate * 32ULL * zclk->fixed_div, 1458c2ecf20Sopenharmony_ci parent_rate); 1468c2ecf20Sopenharmony_ci mult = clamp(mult, 1U, 32U); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci if (readl(zclk->kick_reg) & CPG_FRQCRB_KICK) 1498c2ecf20Sopenharmony_ci return -EBUSY; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci cpg_reg_modify(zclk->reg, zclk->mask, 1528c2ecf20Sopenharmony_ci ((32 - mult) << __ffs(zclk->mask)) & zclk->mask); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci /* 1558c2ecf20Sopenharmony_ci * Set KICK bit in FRQCRB to update hardware setting and wait for 1568c2ecf20Sopenharmony_ci * clock change completion. 1578c2ecf20Sopenharmony_ci */ 1588c2ecf20Sopenharmony_ci cpg_reg_modify(zclk->kick_reg, 0, CPG_FRQCRB_KICK); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci /* 1618c2ecf20Sopenharmony_ci * Note: There is no HW information about the worst case latency. 1628c2ecf20Sopenharmony_ci * 1638c2ecf20Sopenharmony_ci * Using experimental measurements, it seems that no more than 1648c2ecf20Sopenharmony_ci * ~10 iterations are needed, independently of the CPU rate. 1658c2ecf20Sopenharmony_ci * Since this value might be dependent of external xtal rate, pll1 1668c2ecf20Sopenharmony_ci * rate or even the other emulation clocks rate, use 1000 as a 1678c2ecf20Sopenharmony_ci * "super" safe value. 1688c2ecf20Sopenharmony_ci */ 1698c2ecf20Sopenharmony_ci for (i = 1000; i; i--) { 1708c2ecf20Sopenharmony_ci if (!(readl(zclk->kick_reg) & CPG_FRQCRB_KICK)) 1718c2ecf20Sopenharmony_ci return 0; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci cpu_relax(); 1748c2ecf20Sopenharmony_ci } 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci return -ETIMEDOUT; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistatic const struct clk_ops cpg_z_clk_ops = { 1808c2ecf20Sopenharmony_ci .recalc_rate = cpg_z_clk_recalc_rate, 1818c2ecf20Sopenharmony_ci .determine_rate = cpg_z_clk_determine_rate, 1828c2ecf20Sopenharmony_ci .set_rate = cpg_z_clk_set_rate, 1838c2ecf20Sopenharmony_ci}; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_cistatic struct clk * __init cpg_z_clk_register(const char *name, 1868c2ecf20Sopenharmony_ci const char *parent_name, 1878c2ecf20Sopenharmony_ci void __iomem *reg, 1888c2ecf20Sopenharmony_ci unsigned int div, 1898c2ecf20Sopenharmony_ci unsigned int offset) 1908c2ecf20Sopenharmony_ci{ 1918c2ecf20Sopenharmony_ci struct clk_init_data init; 1928c2ecf20Sopenharmony_ci struct cpg_z_clk *zclk; 1938c2ecf20Sopenharmony_ci struct clk *clk; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci zclk = kzalloc(sizeof(*zclk), GFP_KERNEL); 1968c2ecf20Sopenharmony_ci if (!zclk) 1978c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci init.name = name; 2008c2ecf20Sopenharmony_ci init.ops = &cpg_z_clk_ops; 2018c2ecf20Sopenharmony_ci init.flags = 0; 2028c2ecf20Sopenharmony_ci init.parent_names = &parent_name; 2038c2ecf20Sopenharmony_ci init.num_parents = 1; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci zclk->reg = reg + CPG_FRQCRC; 2068c2ecf20Sopenharmony_ci zclk->kick_reg = reg + CPG_FRQCRB; 2078c2ecf20Sopenharmony_ci zclk->hw.init = &init; 2088c2ecf20Sopenharmony_ci zclk->mask = GENMASK(offset + 4, offset); 2098c2ecf20Sopenharmony_ci zclk->fixed_div = div; /* PLLVCO x 1/div x SYS-CPU divider */ 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci clk = clk_register(NULL, &zclk->hw); 2128c2ecf20Sopenharmony_ci if (IS_ERR(clk)) 2138c2ecf20Sopenharmony_ci kfree(zclk); 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci return clk; 2168c2ecf20Sopenharmony_ci} 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci/* 2198c2ecf20Sopenharmony_ci * SDn Clock 2208c2ecf20Sopenharmony_ci */ 2218c2ecf20Sopenharmony_ci#define CPG_SD_STP_HCK BIT(9) 2228c2ecf20Sopenharmony_ci#define CPG_SD_STP_CK BIT(8) 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci#define CPG_SD_STP_MASK (CPG_SD_STP_HCK | CPG_SD_STP_CK) 2258c2ecf20Sopenharmony_ci#define CPG_SD_FC_MASK (0x7 << 2 | 0x3 << 0) 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci#define CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) \ 2288c2ecf20Sopenharmony_ci{ \ 2298c2ecf20Sopenharmony_ci .val = ((stp_hck) ? CPG_SD_STP_HCK : 0) | \ 2308c2ecf20Sopenharmony_ci ((stp_ck) ? CPG_SD_STP_CK : 0) | \ 2318c2ecf20Sopenharmony_ci ((sd_srcfc) << 2) | \ 2328c2ecf20Sopenharmony_ci ((sd_fc) << 0), \ 2338c2ecf20Sopenharmony_ci .div = (sd_div), \ 2348c2ecf20Sopenharmony_ci} 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_cistruct sd_div_table { 2378c2ecf20Sopenharmony_ci u32 val; 2388c2ecf20Sopenharmony_ci unsigned int div; 2398c2ecf20Sopenharmony_ci}; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_cistruct sd_clock { 2428c2ecf20Sopenharmony_ci struct clk_hw hw; 2438c2ecf20Sopenharmony_ci const struct sd_div_table *div_table; 2448c2ecf20Sopenharmony_ci struct cpg_simple_notifier csn; 2458c2ecf20Sopenharmony_ci unsigned int div_num; 2468c2ecf20Sopenharmony_ci unsigned int cur_div_idx; 2478c2ecf20Sopenharmony_ci}; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci/* SDn divider 2508c2ecf20Sopenharmony_ci * sd_srcfc sd_fc div 2518c2ecf20Sopenharmony_ci * stp_hck stp_ck (div) (div) = sd_srcfc x sd_fc 2528c2ecf20Sopenharmony_ci *------------------------------------------------------------------- 2538c2ecf20Sopenharmony_ci * 0 0 0 (1) 1 (4) 4 : SDR104 / HS200 / HS400 (8 TAP) 2548c2ecf20Sopenharmony_ci * 0 0 1 (2) 1 (4) 8 : SDR50 2558c2ecf20Sopenharmony_ci * 1 0 2 (4) 1 (4) 16 : HS / SDR25 2568c2ecf20Sopenharmony_ci * 1 0 3 (8) 1 (4) 32 : NS / SDR12 2578c2ecf20Sopenharmony_ci * 1 0 4 (16) 1 (4) 64 2588c2ecf20Sopenharmony_ci * 0 0 0 (1) 0 (2) 2 2598c2ecf20Sopenharmony_ci * 0 0 1 (2) 0 (2) 4 : SDR104 / HS200 / HS400 (4 TAP) 2608c2ecf20Sopenharmony_ci * 1 0 2 (4) 0 (2) 8 2618c2ecf20Sopenharmony_ci * 1 0 3 (8) 0 (2) 16 2628c2ecf20Sopenharmony_ci * 1 0 4 (16) 0 (2) 32 2638c2ecf20Sopenharmony_ci * 2648c2ecf20Sopenharmony_ci * NOTE: There is a quirk option to ignore the first row of the dividers 2658c2ecf20Sopenharmony_ci * table when searching for suitable settings. This is because HS400 on 2668c2ecf20Sopenharmony_ci * early ES versions of H3 and M3-W requires a specific setting to work. 2678c2ecf20Sopenharmony_ci */ 2688c2ecf20Sopenharmony_cistatic const struct sd_div_table cpg_sd_div_table[] = { 2698c2ecf20Sopenharmony_ci/* CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) */ 2708c2ecf20Sopenharmony_ci CPG_SD_DIV_TABLE_DATA(0, 0, 0, 1, 4), 2718c2ecf20Sopenharmony_ci CPG_SD_DIV_TABLE_DATA(0, 0, 1, 1, 8), 2728c2ecf20Sopenharmony_ci CPG_SD_DIV_TABLE_DATA(1, 0, 2, 1, 16), 2738c2ecf20Sopenharmony_ci CPG_SD_DIV_TABLE_DATA(1, 0, 3, 1, 32), 2748c2ecf20Sopenharmony_ci CPG_SD_DIV_TABLE_DATA(1, 0, 4, 1, 64), 2758c2ecf20Sopenharmony_ci CPG_SD_DIV_TABLE_DATA(0, 0, 0, 0, 2), 2768c2ecf20Sopenharmony_ci CPG_SD_DIV_TABLE_DATA(0, 0, 1, 0, 4), 2778c2ecf20Sopenharmony_ci CPG_SD_DIV_TABLE_DATA(1, 0, 2, 0, 8), 2788c2ecf20Sopenharmony_ci CPG_SD_DIV_TABLE_DATA(1, 0, 3, 0, 16), 2798c2ecf20Sopenharmony_ci CPG_SD_DIV_TABLE_DATA(1, 0, 4, 0, 32), 2808c2ecf20Sopenharmony_ci}; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci#define to_sd_clock(_hw) container_of(_hw, struct sd_clock, hw) 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_cistatic int cpg_sd_clock_enable(struct clk_hw *hw) 2858c2ecf20Sopenharmony_ci{ 2868c2ecf20Sopenharmony_ci struct sd_clock *clock = to_sd_clock(hw); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci cpg_reg_modify(clock->csn.reg, CPG_SD_STP_MASK, 2898c2ecf20Sopenharmony_ci clock->div_table[clock->cur_div_idx].val & 2908c2ecf20Sopenharmony_ci CPG_SD_STP_MASK); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci return 0; 2938c2ecf20Sopenharmony_ci} 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_cistatic void cpg_sd_clock_disable(struct clk_hw *hw) 2968c2ecf20Sopenharmony_ci{ 2978c2ecf20Sopenharmony_ci struct sd_clock *clock = to_sd_clock(hw); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci cpg_reg_modify(clock->csn.reg, 0, CPG_SD_STP_MASK); 3008c2ecf20Sopenharmony_ci} 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_cistatic int cpg_sd_clock_is_enabled(struct clk_hw *hw) 3038c2ecf20Sopenharmony_ci{ 3048c2ecf20Sopenharmony_ci struct sd_clock *clock = to_sd_clock(hw); 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci return !(readl(clock->csn.reg) & CPG_SD_STP_MASK); 3078c2ecf20Sopenharmony_ci} 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_cistatic unsigned long cpg_sd_clock_recalc_rate(struct clk_hw *hw, 3108c2ecf20Sopenharmony_ci unsigned long parent_rate) 3118c2ecf20Sopenharmony_ci{ 3128c2ecf20Sopenharmony_ci struct sd_clock *clock = to_sd_clock(hw); 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci return DIV_ROUND_CLOSEST(parent_rate, 3158c2ecf20Sopenharmony_ci clock->div_table[clock->cur_div_idx].div); 3168c2ecf20Sopenharmony_ci} 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_cistatic int cpg_sd_clock_determine_rate(struct clk_hw *hw, 3198c2ecf20Sopenharmony_ci struct clk_rate_request *req) 3208c2ecf20Sopenharmony_ci{ 3218c2ecf20Sopenharmony_ci unsigned long best_rate = ULONG_MAX, diff_min = ULONG_MAX; 3228c2ecf20Sopenharmony_ci struct sd_clock *clock = to_sd_clock(hw); 3238c2ecf20Sopenharmony_ci unsigned long calc_rate, diff; 3248c2ecf20Sopenharmony_ci unsigned int i; 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci for (i = 0; i < clock->div_num; i++) { 3278c2ecf20Sopenharmony_ci calc_rate = DIV_ROUND_CLOSEST(req->best_parent_rate, 3288c2ecf20Sopenharmony_ci clock->div_table[i].div); 3298c2ecf20Sopenharmony_ci if (calc_rate < req->min_rate || calc_rate > req->max_rate) 3308c2ecf20Sopenharmony_ci continue; 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci diff = calc_rate > req->rate ? calc_rate - req->rate 3338c2ecf20Sopenharmony_ci : req->rate - calc_rate; 3348c2ecf20Sopenharmony_ci if (diff < diff_min) { 3358c2ecf20Sopenharmony_ci best_rate = calc_rate; 3368c2ecf20Sopenharmony_ci diff_min = diff; 3378c2ecf20Sopenharmony_ci } 3388c2ecf20Sopenharmony_ci } 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci if (best_rate == ULONG_MAX) 3418c2ecf20Sopenharmony_ci return -EINVAL; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci req->rate = best_rate; 3448c2ecf20Sopenharmony_ci return 0; 3458c2ecf20Sopenharmony_ci} 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_cistatic int cpg_sd_clock_set_rate(struct clk_hw *hw, unsigned long rate, 3488c2ecf20Sopenharmony_ci unsigned long parent_rate) 3498c2ecf20Sopenharmony_ci{ 3508c2ecf20Sopenharmony_ci struct sd_clock *clock = to_sd_clock(hw); 3518c2ecf20Sopenharmony_ci unsigned int i; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci for (i = 0; i < clock->div_num; i++) 3548c2ecf20Sopenharmony_ci if (rate == DIV_ROUND_CLOSEST(parent_rate, 3558c2ecf20Sopenharmony_ci clock->div_table[i].div)) 3568c2ecf20Sopenharmony_ci break; 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci if (i >= clock->div_num) 3598c2ecf20Sopenharmony_ci return -EINVAL; 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci clock->cur_div_idx = i; 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci cpg_reg_modify(clock->csn.reg, CPG_SD_STP_MASK | CPG_SD_FC_MASK, 3648c2ecf20Sopenharmony_ci clock->div_table[i].val & 3658c2ecf20Sopenharmony_ci (CPG_SD_STP_MASK | CPG_SD_FC_MASK)); 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci return 0; 3688c2ecf20Sopenharmony_ci} 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_cistatic const struct clk_ops cpg_sd_clock_ops = { 3718c2ecf20Sopenharmony_ci .enable = cpg_sd_clock_enable, 3728c2ecf20Sopenharmony_ci .disable = cpg_sd_clock_disable, 3738c2ecf20Sopenharmony_ci .is_enabled = cpg_sd_clock_is_enabled, 3748c2ecf20Sopenharmony_ci .recalc_rate = cpg_sd_clock_recalc_rate, 3758c2ecf20Sopenharmony_ci .determine_rate = cpg_sd_clock_determine_rate, 3768c2ecf20Sopenharmony_ci .set_rate = cpg_sd_clock_set_rate, 3778c2ecf20Sopenharmony_ci}; 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_cistatic u32 cpg_quirks __initdata; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci#define PLL_ERRATA BIT(0) /* Missing PLL0/2/4 post-divider */ 3828c2ecf20Sopenharmony_ci#define RCKCR_CKSEL BIT(1) /* Manual RCLK parent selection */ 3838c2ecf20Sopenharmony_ci#define SD_SKIP_FIRST BIT(2) /* Skip first clock in SD table */ 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_cistatic struct clk * __init cpg_sd_clk_register(const char *name, 3868c2ecf20Sopenharmony_ci void __iomem *base, unsigned int offset, const char *parent_name, 3878c2ecf20Sopenharmony_ci struct raw_notifier_head *notifiers) 3888c2ecf20Sopenharmony_ci{ 3898c2ecf20Sopenharmony_ci struct clk_init_data init; 3908c2ecf20Sopenharmony_ci struct sd_clock *clock; 3918c2ecf20Sopenharmony_ci struct clk *clk; 3928c2ecf20Sopenharmony_ci u32 val; 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci clock = kzalloc(sizeof(*clock), GFP_KERNEL); 3958c2ecf20Sopenharmony_ci if (!clock) 3968c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci init.name = name; 3998c2ecf20Sopenharmony_ci init.ops = &cpg_sd_clock_ops; 4008c2ecf20Sopenharmony_ci init.flags = CLK_SET_RATE_PARENT; 4018c2ecf20Sopenharmony_ci init.parent_names = &parent_name; 4028c2ecf20Sopenharmony_ci init.num_parents = 1; 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci clock->csn.reg = base + offset; 4058c2ecf20Sopenharmony_ci clock->hw.init = &init; 4068c2ecf20Sopenharmony_ci clock->div_table = cpg_sd_div_table; 4078c2ecf20Sopenharmony_ci clock->div_num = ARRAY_SIZE(cpg_sd_div_table); 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci if (cpg_quirks & SD_SKIP_FIRST) { 4108c2ecf20Sopenharmony_ci clock->div_table++; 4118c2ecf20Sopenharmony_ci clock->div_num--; 4128c2ecf20Sopenharmony_ci } 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci val = readl(clock->csn.reg) & ~CPG_SD_FC_MASK; 4158c2ecf20Sopenharmony_ci val |= CPG_SD_STP_MASK | (clock->div_table[0].val & CPG_SD_FC_MASK); 4168c2ecf20Sopenharmony_ci writel(val, clock->csn.reg); 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci clk = clk_register(NULL, &clock->hw); 4198c2ecf20Sopenharmony_ci if (IS_ERR(clk)) 4208c2ecf20Sopenharmony_ci goto free_clock; 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci cpg_simple_notifier_register(notifiers, &clock->csn); 4238c2ecf20Sopenharmony_ci return clk; 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_cifree_clock: 4268c2ecf20Sopenharmony_ci kfree(clock); 4278c2ecf20Sopenharmony_ci return clk; 4288c2ecf20Sopenharmony_ci} 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_cistruct rpc_clock { 4318c2ecf20Sopenharmony_ci struct clk_divider div; 4328c2ecf20Sopenharmony_ci struct clk_gate gate; 4338c2ecf20Sopenharmony_ci /* 4348c2ecf20Sopenharmony_ci * One notifier covers both RPC and RPCD2 clocks as they are both 4358c2ecf20Sopenharmony_ci * controlled by the same RPCCKCR register... 4368c2ecf20Sopenharmony_ci */ 4378c2ecf20Sopenharmony_ci struct cpg_simple_notifier csn; 4388c2ecf20Sopenharmony_ci}; 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_cistatic const struct clk_div_table cpg_rpcsrc_div_table[] = { 4418c2ecf20Sopenharmony_ci { 2, 5 }, { 3, 6 }, { 0, 0 }, 4428c2ecf20Sopenharmony_ci}; 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_cistatic const struct clk_div_table cpg_rpc_div_table[] = { 4458c2ecf20Sopenharmony_ci { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 0, 0 }, 4468c2ecf20Sopenharmony_ci}; 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_cistatic struct clk * __init cpg_rpc_clk_register(const char *name, 4498c2ecf20Sopenharmony_ci void __iomem *base, const char *parent_name, 4508c2ecf20Sopenharmony_ci struct raw_notifier_head *notifiers) 4518c2ecf20Sopenharmony_ci{ 4528c2ecf20Sopenharmony_ci struct rpc_clock *rpc; 4538c2ecf20Sopenharmony_ci struct clk *clk; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci rpc = kzalloc(sizeof(*rpc), GFP_KERNEL); 4568c2ecf20Sopenharmony_ci if (!rpc) 4578c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci rpc->div.reg = base + CPG_RPCCKCR; 4608c2ecf20Sopenharmony_ci rpc->div.width = 3; 4618c2ecf20Sopenharmony_ci rpc->div.table = cpg_rpc_div_table; 4628c2ecf20Sopenharmony_ci rpc->div.lock = &cpg_lock; 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci rpc->gate.reg = base + CPG_RPCCKCR; 4658c2ecf20Sopenharmony_ci rpc->gate.bit_idx = 8; 4668c2ecf20Sopenharmony_ci rpc->gate.flags = CLK_GATE_SET_TO_DISABLE; 4678c2ecf20Sopenharmony_ci rpc->gate.lock = &cpg_lock; 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci rpc->csn.reg = base + CPG_RPCCKCR; 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci clk = clk_register_composite(NULL, name, &parent_name, 1, NULL, NULL, 4728c2ecf20Sopenharmony_ci &rpc->div.hw, &clk_divider_ops, 4738c2ecf20Sopenharmony_ci &rpc->gate.hw, &clk_gate_ops, 4748c2ecf20Sopenharmony_ci CLK_SET_RATE_PARENT); 4758c2ecf20Sopenharmony_ci if (IS_ERR(clk)) { 4768c2ecf20Sopenharmony_ci kfree(rpc); 4778c2ecf20Sopenharmony_ci return clk; 4788c2ecf20Sopenharmony_ci } 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci cpg_simple_notifier_register(notifiers, &rpc->csn); 4818c2ecf20Sopenharmony_ci return clk; 4828c2ecf20Sopenharmony_ci} 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_cistruct rpcd2_clock { 4858c2ecf20Sopenharmony_ci struct clk_fixed_factor fixed; 4868c2ecf20Sopenharmony_ci struct clk_gate gate; 4878c2ecf20Sopenharmony_ci}; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_cistatic struct clk * __init cpg_rpcd2_clk_register(const char *name, 4908c2ecf20Sopenharmony_ci void __iomem *base, 4918c2ecf20Sopenharmony_ci const char *parent_name) 4928c2ecf20Sopenharmony_ci{ 4938c2ecf20Sopenharmony_ci struct rpcd2_clock *rpcd2; 4948c2ecf20Sopenharmony_ci struct clk *clk; 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ci rpcd2 = kzalloc(sizeof(*rpcd2), GFP_KERNEL); 4978c2ecf20Sopenharmony_ci if (!rpcd2) 4988c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci rpcd2->fixed.mult = 1; 5018c2ecf20Sopenharmony_ci rpcd2->fixed.div = 2; 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci rpcd2->gate.reg = base + CPG_RPCCKCR; 5048c2ecf20Sopenharmony_ci rpcd2->gate.bit_idx = 9; 5058c2ecf20Sopenharmony_ci rpcd2->gate.flags = CLK_GATE_SET_TO_DISABLE; 5068c2ecf20Sopenharmony_ci rpcd2->gate.lock = &cpg_lock; 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci clk = clk_register_composite(NULL, name, &parent_name, 1, NULL, NULL, 5098c2ecf20Sopenharmony_ci &rpcd2->fixed.hw, &clk_fixed_factor_ops, 5108c2ecf20Sopenharmony_ci &rpcd2->gate.hw, &clk_gate_ops, 5118c2ecf20Sopenharmony_ci CLK_SET_RATE_PARENT); 5128c2ecf20Sopenharmony_ci if (IS_ERR(clk)) 5138c2ecf20Sopenharmony_ci kfree(rpcd2); 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci return clk; 5168c2ecf20Sopenharmony_ci} 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_cistatic const struct rcar_gen3_cpg_pll_config *cpg_pll_config __initdata; 5208c2ecf20Sopenharmony_cistatic unsigned int cpg_clk_extalr __initdata; 5218c2ecf20Sopenharmony_cistatic u32 cpg_mode __initdata; 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_cistatic const struct soc_device_attribute cpg_quirks_match[] __initconst = { 5248c2ecf20Sopenharmony_ci { 5258c2ecf20Sopenharmony_ci .soc_id = "r8a7795", .revision = "ES1.0", 5268c2ecf20Sopenharmony_ci .data = (void *)(PLL_ERRATA | RCKCR_CKSEL | SD_SKIP_FIRST), 5278c2ecf20Sopenharmony_ci }, 5288c2ecf20Sopenharmony_ci { 5298c2ecf20Sopenharmony_ci .soc_id = "r8a7795", .revision = "ES1.*", 5308c2ecf20Sopenharmony_ci .data = (void *)(RCKCR_CKSEL | SD_SKIP_FIRST), 5318c2ecf20Sopenharmony_ci }, 5328c2ecf20Sopenharmony_ci { 5338c2ecf20Sopenharmony_ci .soc_id = "r8a7795", .revision = "ES2.0", 5348c2ecf20Sopenharmony_ci .data = (void *)SD_SKIP_FIRST, 5358c2ecf20Sopenharmony_ci }, 5368c2ecf20Sopenharmony_ci { 5378c2ecf20Sopenharmony_ci .soc_id = "r8a7796", .revision = "ES1.0", 5388c2ecf20Sopenharmony_ci .data = (void *)(RCKCR_CKSEL | SD_SKIP_FIRST), 5398c2ecf20Sopenharmony_ci }, 5408c2ecf20Sopenharmony_ci { 5418c2ecf20Sopenharmony_ci .soc_id = "r8a7796", .revision = "ES1.1", 5428c2ecf20Sopenharmony_ci .data = (void *)SD_SKIP_FIRST, 5438c2ecf20Sopenharmony_ci }, 5448c2ecf20Sopenharmony_ci { /* sentinel */ } 5458c2ecf20Sopenharmony_ci}; 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_cistruct clk * __init rcar_gen3_cpg_clk_register(struct device *dev, 5488c2ecf20Sopenharmony_ci const struct cpg_core_clk *core, const struct cpg_mssr_info *info, 5498c2ecf20Sopenharmony_ci struct clk **clks, void __iomem *base, 5508c2ecf20Sopenharmony_ci struct raw_notifier_head *notifiers) 5518c2ecf20Sopenharmony_ci{ 5528c2ecf20Sopenharmony_ci const struct clk *parent; 5538c2ecf20Sopenharmony_ci unsigned int mult = 1; 5548c2ecf20Sopenharmony_ci unsigned int div = 1; 5558c2ecf20Sopenharmony_ci u32 value; 5568c2ecf20Sopenharmony_ci 5578c2ecf20Sopenharmony_ci parent = clks[core->parent & 0xffff]; /* some types use high bits */ 5588c2ecf20Sopenharmony_ci if (IS_ERR(parent)) 5598c2ecf20Sopenharmony_ci return ERR_CAST(parent); 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci switch (core->type) { 5628c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_MAIN: 5638c2ecf20Sopenharmony_ci div = cpg_pll_config->extal_div; 5648c2ecf20Sopenharmony_ci break; 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_PLL0: 5678c2ecf20Sopenharmony_ci /* 5688c2ecf20Sopenharmony_ci * PLL0 is a configurable multiplier clock. Register it as a 5698c2ecf20Sopenharmony_ci * fixed factor clock for now as there's no generic multiplier 5708c2ecf20Sopenharmony_ci * clock implementation and we currently have no need to change 5718c2ecf20Sopenharmony_ci * the multiplier value. 5728c2ecf20Sopenharmony_ci */ 5738c2ecf20Sopenharmony_ci value = readl(base + CPG_PLL0CR); 5748c2ecf20Sopenharmony_ci mult = (((value >> 24) & 0x7f) + 1) * 2; 5758c2ecf20Sopenharmony_ci if (cpg_quirks & PLL_ERRATA) 5768c2ecf20Sopenharmony_ci mult *= 2; 5778c2ecf20Sopenharmony_ci break; 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_PLL1: 5808c2ecf20Sopenharmony_ci mult = cpg_pll_config->pll1_mult; 5818c2ecf20Sopenharmony_ci div = cpg_pll_config->pll1_div; 5828c2ecf20Sopenharmony_ci break; 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_PLL2: 5858c2ecf20Sopenharmony_ci /* 5868c2ecf20Sopenharmony_ci * PLL2 is a configurable multiplier clock. Register it as a 5878c2ecf20Sopenharmony_ci * fixed factor clock for now as there's no generic multiplier 5888c2ecf20Sopenharmony_ci * clock implementation and we currently have no need to change 5898c2ecf20Sopenharmony_ci * the multiplier value. 5908c2ecf20Sopenharmony_ci */ 5918c2ecf20Sopenharmony_ci value = readl(base + CPG_PLL2CR); 5928c2ecf20Sopenharmony_ci mult = (((value >> 24) & 0x7f) + 1) * 2; 5938c2ecf20Sopenharmony_ci if (cpg_quirks & PLL_ERRATA) 5948c2ecf20Sopenharmony_ci mult *= 2; 5958c2ecf20Sopenharmony_ci break; 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_PLL3: 5988c2ecf20Sopenharmony_ci mult = cpg_pll_config->pll3_mult; 5998c2ecf20Sopenharmony_ci div = cpg_pll_config->pll3_div; 6008c2ecf20Sopenharmony_ci break; 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_PLL4: 6038c2ecf20Sopenharmony_ci /* 6048c2ecf20Sopenharmony_ci * PLL4 is a configurable multiplier clock. Register it as a 6058c2ecf20Sopenharmony_ci * fixed factor clock for now as there's no generic multiplier 6068c2ecf20Sopenharmony_ci * clock implementation and we currently have no need to change 6078c2ecf20Sopenharmony_ci * the multiplier value. 6088c2ecf20Sopenharmony_ci */ 6098c2ecf20Sopenharmony_ci value = readl(base + CPG_PLL4CR); 6108c2ecf20Sopenharmony_ci mult = (((value >> 24) & 0x7f) + 1) * 2; 6118c2ecf20Sopenharmony_ci if (cpg_quirks & PLL_ERRATA) 6128c2ecf20Sopenharmony_ci mult *= 2; 6138c2ecf20Sopenharmony_ci break; 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_SD: 6168c2ecf20Sopenharmony_ci return cpg_sd_clk_register(core->name, base, core->offset, 6178c2ecf20Sopenharmony_ci __clk_get_name(parent), notifiers); 6188c2ecf20Sopenharmony_ci 6198c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_R: 6208c2ecf20Sopenharmony_ci if (cpg_quirks & RCKCR_CKSEL) { 6218c2ecf20Sopenharmony_ci struct cpg_simple_notifier *csn; 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci csn = kzalloc(sizeof(*csn), GFP_KERNEL); 6248c2ecf20Sopenharmony_ci if (!csn) 6258c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_ci csn->reg = base + CPG_RCKCR; 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci /* 6308c2ecf20Sopenharmony_ci * RINT is default. 6318c2ecf20Sopenharmony_ci * Only if EXTALR is populated, we switch to it. 6328c2ecf20Sopenharmony_ci */ 6338c2ecf20Sopenharmony_ci value = readl(csn->reg) & 0x3f; 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci if (clk_get_rate(clks[cpg_clk_extalr])) { 6368c2ecf20Sopenharmony_ci parent = clks[cpg_clk_extalr]; 6378c2ecf20Sopenharmony_ci value |= CPG_RCKCR_CKSEL; 6388c2ecf20Sopenharmony_ci } 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci writel(value, csn->reg); 6418c2ecf20Sopenharmony_ci cpg_simple_notifier_register(notifiers, csn); 6428c2ecf20Sopenharmony_ci break; 6438c2ecf20Sopenharmony_ci } 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_ci /* Select parent clock of RCLK by MD28 */ 6468c2ecf20Sopenharmony_ci if (cpg_mode & BIT(28)) 6478c2ecf20Sopenharmony_ci parent = clks[cpg_clk_extalr]; 6488c2ecf20Sopenharmony_ci break; 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_MDSEL: 6518c2ecf20Sopenharmony_ci /* 6528c2ecf20Sopenharmony_ci * Clock selectable between two parents and two fixed dividers 6538c2ecf20Sopenharmony_ci * using a mode pin 6548c2ecf20Sopenharmony_ci */ 6558c2ecf20Sopenharmony_ci if (cpg_mode & BIT(core->offset)) { 6568c2ecf20Sopenharmony_ci div = core->div & 0xffff; 6578c2ecf20Sopenharmony_ci } else { 6588c2ecf20Sopenharmony_ci parent = clks[core->parent >> 16]; 6598c2ecf20Sopenharmony_ci if (IS_ERR(parent)) 6608c2ecf20Sopenharmony_ci return ERR_CAST(parent); 6618c2ecf20Sopenharmony_ci div = core->div >> 16; 6628c2ecf20Sopenharmony_ci } 6638c2ecf20Sopenharmony_ci mult = 1; 6648c2ecf20Sopenharmony_ci break; 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_Z: 6678c2ecf20Sopenharmony_ci return cpg_z_clk_register(core->name, __clk_get_name(parent), 6688c2ecf20Sopenharmony_ci base, core->div, core->offset); 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_OSC: 6718c2ecf20Sopenharmony_ci /* 6728c2ecf20Sopenharmony_ci * Clock combining OSC EXTAL predivider and a fixed divider 6738c2ecf20Sopenharmony_ci */ 6748c2ecf20Sopenharmony_ci div = cpg_pll_config->osc_prediv * core->div; 6758c2ecf20Sopenharmony_ci break; 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_RCKSEL: 6788c2ecf20Sopenharmony_ci /* 6798c2ecf20Sopenharmony_ci * Clock selectable between two parents and two fixed dividers 6808c2ecf20Sopenharmony_ci * using RCKCR.CKSEL 6818c2ecf20Sopenharmony_ci */ 6828c2ecf20Sopenharmony_ci if (readl(base + CPG_RCKCR) & CPG_RCKCR_CKSEL) { 6838c2ecf20Sopenharmony_ci div = core->div & 0xffff; 6848c2ecf20Sopenharmony_ci } else { 6858c2ecf20Sopenharmony_ci parent = clks[core->parent >> 16]; 6868c2ecf20Sopenharmony_ci if (IS_ERR(parent)) 6878c2ecf20Sopenharmony_ci return ERR_CAST(parent); 6888c2ecf20Sopenharmony_ci div = core->div >> 16; 6898c2ecf20Sopenharmony_ci } 6908c2ecf20Sopenharmony_ci break; 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_RPCSRC: 6938c2ecf20Sopenharmony_ci return clk_register_divider_table(NULL, core->name, 6948c2ecf20Sopenharmony_ci __clk_get_name(parent), 0, 6958c2ecf20Sopenharmony_ci base + CPG_RPCCKCR, 3, 2, 0, 6968c2ecf20Sopenharmony_ci cpg_rpcsrc_div_table, 6978c2ecf20Sopenharmony_ci &cpg_lock); 6988c2ecf20Sopenharmony_ci 6998c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_RPC: 7008c2ecf20Sopenharmony_ci return cpg_rpc_clk_register(core->name, base, 7018c2ecf20Sopenharmony_ci __clk_get_name(parent), notifiers); 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci case CLK_TYPE_GEN3_RPCD2: 7048c2ecf20Sopenharmony_ci return cpg_rpcd2_clk_register(core->name, base, 7058c2ecf20Sopenharmony_ci __clk_get_name(parent)); 7068c2ecf20Sopenharmony_ci 7078c2ecf20Sopenharmony_ci default: 7088c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 7098c2ecf20Sopenharmony_ci } 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci return clk_register_fixed_factor(NULL, core->name, 7128c2ecf20Sopenharmony_ci __clk_get_name(parent), 0, mult, div); 7138c2ecf20Sopenharmony_ci} 7148c2ecf20Sopenharmony_ci 7158c2ecf20Sopenharmony_ciint __init rcar_gen3_cpg_init(const struct rcar_gen3_cpg_pll_config *config, 7168c2ecf20Sopenharmony_ci unsigned int clk_extalr, u32 mode) 7178c2ecf20Sopenharmony_ci{ 7188c2ecf20Sopenharmony_ci const struct soc_device_attribute *attr; 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci cpg_pll_config = config; 7218c2ecf20Sopenharmony_ci cpg_clk_extalr = clk_extalr; 7228c2ecf20Sopenharmony_ci cpg_mode = mode; 7238c2ecf20Sopenharmony_ci attr = soc_device_match(cpg_quirks_match); 7248c2ecf20Sopenharmony_ci if (attr) 7258c2ecf20Sopenharmony_ci cpg_quirks = (uintptr_t)attr->data; 7268c2ecf20Sopenharmony_ci pr_debug("%s: mode = 0x%x quirks = 0x%x\n", __func__, mode, cpg_quirks); 7278c2ecf20Sopenharmony_ci 7288c2ecf20Sopenharmony_ci spin_lock_init(&cpg_lock); 7298c2ecf20Sopenharmony_ci 7308c2ecf20Sopenharmony_ci return 0; 7318c2ecf20Sopenharmony_ci} 732