18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2013 DENX Software Engineering 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Gerhard Sittig, <gsi@denx.de> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * common clock driver support for the MPC512x platform 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/bitops.h> 118c2ecf20Sopenharmony_ci#include <linux/clk.h> 128c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 138c2ecf20Sopenharmony_ci#include <linux/clkdev.h> 148c2ecf20Sopenharmony_ci#include <linux/device.h> 158c2ecf20Sopenharmony_ci#include <linux/errno.h> 168c2ecf20Sopenharmony_ci#include <linux/io.h> 178c2ecf20Sopenharmony_ci#include <linux/of.h> 188c2ecf20Sopenharmony_ci#include <linux/of_address.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include <asm/mpc5121.h> 218c2ecf20Sopenharmony_ci#include <dt-bindings/clock/mpc512x-clock.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#include "mpc512x.h" /* our public mpc5121_clk_init() API */ 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* helpers to keep the MCLK intermediates "somewhere" in our table */ 268c2ecf20Sopenharmony_cienum { 278c2ecf20Sopenharmony_ci MCLK_IDX_MUX0, 288c2ecf20Sopenharmony_ci MCLK_IDX_EN0, 298c2ecf20Sopenharmony_ci MCLK_IDX_DIV0, 308c2ecf20Sopenharmony_ci MCLK_MAX_IDX, 318c2ecf20Sopenharmony_ci}; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define NR_PSCS 12 348c2ecf20Sopenharmony_ci#define NR_MSCANS 4 358c2ecf20Sopenharmony_ci#define NR_SPDIFS 1 368c2ecf20Sopenharmony_ci#define NR_OUTCLK 4 378c2ecf20Sopenharmony_ci#define NR_MCLKS (NR_PSCS + NR_MSCANS + NR_SPDIFS + NR_OUTCLK) 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/* extend the public set of clocks by adding internal slots for management */ 408c2ecf20Sopenharmony_cienum { 418c2ecf20Sopenharmony_ci /* arrange for adjacent numbers after the public set */ 428c2ecf20Sopenharmony_ci MPC512x_CLK_START_PRIVATE = MPC512x_CLK_LAST_PUBLIC, 438c2ecf20Sopenharmony_ci /* clocks which aren't announced to the public */ 448c2ecf20Sopenharmony_ci MPC512x_CLK_DDR, 458c2ecf20Sopenharmony_ci MPC512x_CLK_MEM, 468c2ecf20Sopenharmony_ci MPC512x_CLK_IIM, 478c2ecf20Sopenharmony_ci /* intermediates in div+gate combos or fractional dividers */ 488c2ecf20Sopenharmony_ci MPC512x_CLK_DDR_UG, 498c2ecf20Sopenharmony_ci MPC512x_CLK_SDHC_x4, 508c2ecf20Sopenharmony_ci MPC512x_CLK_SDHC_UG, 518c2ecf20Sopenharmony_ci MPC512x_CLK_SDHC2_UG, 528c2ecf20Sopenharmony_ci MPC512x_CLK_DIU_x4, 538c2ecf20Sopenharmony_ci MPC512x_CLK_DIU_UG, 548c2ecf20Sopenharmony_ci MPC512x_CLK_MBX_BUS_UG, 558c2ecf20Sopenharmony_ci MPC512x_CLK_MBX_UG, 568c2ecf20Sopenharmony_ci MPC512x_CLK_MBX_3D_UG, 578c2ecf20Sopenharmony_ci MPC512x_CLK_PCI_UG, 588c2ecf20Sopenharmony_ci MPC512x_CLK_NFC_UG, 598c2ecf20Sopenharmony_ci MPC512x_CLK_LPC_UG, 608c2ecf20Sopenharmony_ci MPC512x_CLK_SPDIF_TX_IN, 618c2ecf20Sopenharmony_ci /* intermediates for the mux+gate+div+mux MCLK generation */ 628c2ecf20Sopenharmony_ci MPC512x_CLK_MCLKS_FIRST, 638c2ecf20Sopenharmony_ci MPC512x_CLK_MCLKS_LAST = MPC512x_CLK_MCLKS_FIRST 648c2ecf20Sopenharmony_ci + NR_MCLKS * MCLK_MAX_IDX, 658c2ecf20Sopenharmony_ci /* internal, symbolic spec for the number of slots */ 668c2ecf20Sopenharmony_ci MPC512x_CLK_LAST_PRIVATE, 678c2ecf20Sopenharmony_ci}; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci/* data required for the OF clock provider registration */ 708c2ecf20Sopenharmony_cistatic struct clk *clks[MPC512x_CLK_LAST_PRIVATE]; 718c2ecf20Sopenharmony_cistatic struct clk_onecell_data clk_data; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/* CCM register access */ 748c2ecf20Sopenharmony_cistatic struct mpc512x_ccm __iomem *clkregs; 758c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(clklock); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci/* SoC variants {{{ */ 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci/* 808c2ecf20Sopenharmony_ci * tell SoC variants apart as they are rather similar yet not identical, 818c2ecf20Sopenharmony_ci * cache the result in an enum to not repeatedly run the expensive OF test 828c2ecf20Sopenharmony_ci * 838c2ecf20Sopenharmony_ci * MPC5123 is an MPC5121 without the MBX graphics accelerator 848c2ecf20Sopenharmony_ci * 858c2ecf20Sopenharmony_ci * MPC5125 has many more differences: no MBX, no AXE, no VIU, no SPDIF, 868c2ecf20Sopenharmony_ci * no PATA, no SATA, no PCI, two FECs (of different compatibility name), 878c2ecf20Sopenharmony_ci * only 10 PSCs (of different compatibility name), two SDHCs, different 888c2ecf20Sopenharmony_ci * NFC IP block, output clocks, system PLL status query, different CPMF 898c2ecf20Sopenharmony_ci * interpretation, no CFM, different fourth PSC/CAN mux0 input -- yet 908c2ecf20Sopenharmony_ci * those differences can get folded into this clock provider support 918c2ecf20Sopenharmony_ci * code and don't warrant a separate highly redundant implementation 928c2ecf20Sopenharmony_ci */ 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cistatic enum soc_type { 958c2ecf20Sopenharmony_ci MPC512x_SOC_MPC5121, 968c2ecf20Sopenharmony_ci MPC512x_SOC_MPC5123, 978c2ecf20Sopenharmony_ci MPC512x_SOC_MPC5125, 988c2ecf20Sopenharmony_ci} soc; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic void mpc512x_clk_determine_soc(void) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci if (of_machine_is_compatible("fsl,mpc5121")) { 1038c2ecf20Sopenharmony_ci soc = MPC512x_SOC_MPC5121; 1048c2ecf20Sopenharmony_ci return; 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci if (of_machine_is_compatible("fsl,mpc5123")) { 1078c2ecf20Sopenharmony_ci soc = MPC512x_SOC_MPC5123; 1088c2ecf20Sopenharmony_ci return; 1098c2ecf20Sopenharmony_ci } 1108c2ecf20Sopenharmony_ci if (of_machine_is_compatible("fsl,mpc5125")) { 1118c2ecf20Sopenharmony_ci soc = MPC512x_SOC_MPC5125; 1128c2ecf20Sopenharmony_ci return; 1138c2ecf20Sopenharmony_ci } 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic bool soc_has_mbx(void) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5121) 1198c2ecf20Sopenharmony_ci return true; 1208c2ecf20Sopenharmony_ci return false; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic bool soc_has_axe(void) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 1268c2ecf20Sopenharmony_ci return false; 1278c2ecf20Sopenharmony_ci return true; 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic bool soc_has_viu(void) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 1338c2ecf20Sopenharmony_ci return false; 1348c2ecf20Sopenharmony_ci return true; 1358c2ecf20Sopenharmony_ci} 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_cistatic bool soc_has_spdif(void) 1388c2ecf20Sopenharmony_ci{ 1398c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 1408c2ecf20Sopenharmony_ci return false; 1418c2ecf20Sopenharmony_ci return true; 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic bool soc_has_pata(void) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 1478c2ecf20Sopenharmony_ci return false; 1488c2ecf20Sopenharmony_ci return true; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic bool soc_has_sata(void) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 1548c2ecf20Sopenharmony_ci return false; 1558c2ecf20Sopenharmony_ci return true; 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic bool soc_has_pci(void) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 1618c2ecf20Sopenharmony_ci return false; 1628c2ecf20Sopenharmony_ci return true; 1638c2ecf20Sopenharmony_ci} 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cistatic bool soc_has_fec2(void) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 1688c2ecf20Sopenharmony_ci return true; 1698c2ecf20Sopenharmony_ci return false; 1708c2ecf20Sopenharmony_ci} 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_cistatic int soc_max_pscnum(void) 1738c2ecf20Sopenharmony_ci{ 1748c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 1758c2ecf20Sopenharmony_ci return 10; 1768c2ecf20Sopenharmony_ci return 12; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistatic bool soc_has_sdhc2(void) 1808c2ecf20Sopenharmony_ci{ 1818c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 1828c2ecf20Sopenharmony_ci return true; 1838c2ecf20Sopenharmony_ci return false; 1848c2ecf20Sopenharmony_ci} 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_cistatic bool soc_has_nfc_5125(void) 1878c2ecf20Sopenharmony_ci{ 1888c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 1898c2ecf20Sopenharmony_ci return true; 1908c2ecf20Sopenharmony_ci return false; 1918c2ecf20Sopenharmony_ci} 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic bool soc_has_outclk(void) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 1968c2ecf20Sopenharmony_ci return true; 1978c2ecf20Sopenharmony_ci return false; 1988c2ecf20Sopenharmony_ci} 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_cistatic bool soc_has_cpmf_0_bypass(void) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 2038c2ecf20Sopenharmony_ci return true; 2048c2ecf20Sopenharmony_ci return false; 2058c2ecf20Sopenharmony_ci} 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_cistatic bool soc_has_mclk_mux0_canin(void) 2088c2ecf20Sopenharmony_ci{ 2098c2ecf20Sopenharmony_ci if (soc == MPC512x_SOC_MPC5125) 2108c2ecf20Sopenharmony_ci return true; 2118c2ecf20Sopenharmony_ci return false; 2128c2ecf20Sopenharmony_ci} 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci/* }}} SoC variants */ 2158c2ecf20Sopenharmony_ci/* common clk API wrappers {{{ */ 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci/* convenience wrappers around the common clk API */ 2188c2ecf20Sopenharmony_cistatic inline struct clk *mpc512x_clk_fixed(const char *name, int rate) 2198c2ecf20Sopenharmony_ci{ 2208c2ecf20Sopenharmony_ci return clk_register_fixed_rate(NULL, name, NULL, 0, rate); 2218c2ecf20Sopenharmony_ci} 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_cistatic inline struct clk *mpc512x_clk_factor( 2248c2ecf20Sopenharmony_ci const char *name, const char *parent_name, 2258c2ecf20Sopenharmony_ci int mul, int div) 2268c2ecf20Sopenharmony_ci{ 2278c2ecf20Sopenharmony_ci int clkflags; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci clkflags = CLK_SET_RATE_PARENT; 2308c2ecf20Sopenharmony_ci return clk_register_fixed_factor(NULL, name, parent_name, clkflags, 2318c2ecf20Sopenharmony_ci mul, div); 2328c2ecf20Sopenharmony_ci} 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_cistatic inline struct clk *mpc512x_clk_divider( 2358c2ecf20Sopenharmony_ci const char *name, const char *parent_name, u8 clkflags, 2368c2ecf20Sopenharmony_ci u32 __iomem *reg, u8 pos, u8 len, int divflags) 2378c2ecf20Sopenharmony_ci{ 2388c2ecf20Sopenharmony_ci divflags |= CLK_DIVIDER_BIG_ENDIAN; 2398c2ecf20Sopenharmony_ci return clk_register_divider(NULL, name, parent_name, clkflags, 2408c2ecf20Sopenharmony_ci reg, pos, len, divflags, &clklock); 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_cistatic inline struct clk *mpc512x_clk_divtable( 2448c2ecf20Sopenharmony_ci const char *name, const char *parent_name, 2458c2ecf20Sopenharmony_ci u32 __iomem *reg, u8 pos, u8 len, 2468c2ecf20Sopenharmony_ci const struct clk_div_table *divtab) 2478c2ecf20Sopenharmony_ci{ 2488c2ecf20Sopenharmony_ci u8 divflags; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci divflags = CLK_DIVIDER_BIG_ENDIAN; 2518c2ecf20Sopenharmony_ci return clk_register_divider_table(NULL, name, parent_name, 0, 2528c2ecf20Sopenharmony_ci reg, pos, len, divflags, 2538c2ecf20Sopenharmony_ci divtab, &clklock); 2548c2ecf20Sopenharmony_ci} 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_cistatic inline struct clk *mpc512x_clk_gated( 2578c2ecf20Sopenharmony_ci const char *name, const char *parent_name, 2588c2ecf20Sopenharmony_ci u32 __iomem *reg, u8 pos) 2598c2ecf20Sopenharmony_ci{ 2608c2ecf20Sopenharmony_ci int clkflags; 2618c2ecf20Sopenharmony_ci u8 gateflags; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci clkflags = CLK_SET_RATE_PARENT; 2648c2ecf20Sopenharmony_ci gateflags = CLK_GATE_BIG_ENDIAN; 2658c2ecf20Sopenharmony_ci return clk_register_gate(NULL, name, parent_name, clkflags, 2668c2ecf20Sopenharmony_ci reg, pos, gateflags, &clklock); 2678c2ecf20Sopenharmony_ci} 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistatic inline struct clk *mpc512x_clk_muxed(const char *name, 2708c2ecf20Sopenharmony_ci const char **parent_names, int parent_count, 2718c2ecf20Sopenharmony_ci u32 __iomem *reg, u8 pos, u8 len) 2728c2ecf20Sopenharmony_ci{ 2738c2ecf20Sopenharmony_ci int clkflags; 2748c2ecf20Sopenharmony_ci u8 muxflags; 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci clkflags = CLK_SET_RATE_PARENT; 2778c2ecf20Sopenharmony_ci muxflags = CLK_MUX_BIG_ENDIAN; 2788c2ecf20Sopenharmony_ci return clk_register_mux(NULL, name, 2798c2ecf20Sopenharmony_ci parent_names, parent_count, clkflags, 2808c2ecf20Sopenharmony_ci reg, pos, len, muxflags, &clklock); 2818c2ecf20Sopenharmony_ci} 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci/* }}} common clk API wrappers */ 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci/* helper to isolate a bit field from a register */ 2868c2ecf20Sopenharmony_cistatic inline int get_bit_field(uint32_t __iomem *reg, uint8_t pos, uint8_t len) 2878c2ecf20Sopenharmony_ci{ 2888c2ecf20Sopenharmony_ci uint32_t val; 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci val = in_be32(reg); 2918c2ecf20Sopenharmony_ci val >>= pos; 2928c2ecf20Sopenharmony_ci val &= (1 << len) - 1; 2938c2ecf20Sopenharmony_ci return val; 2948c2ecf20Sopenharmony_ci} 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci/* get the SPMF and translate it into the "sys pll" multiplier */ 2978c2ecf20Sopenharmony_cistatic int get_spmf_mult(void) 2988c2ecf20Sopenharmony_ci{ 2998c2ecf20Sopenharmony_ci static int spmf_to_mult[] = { 3008c2ecf20Sopenharmony_ci 68, 1, 12, 16, 20, 24, 28, 32, 3018c2ecf20Sopenharmony_ci 36, 40, 44, 48, 52, 56, 60, 64, 3028c2ecf20Sopenharmony_ci }; 3038c2ecf20Sopenharmony_ci int spmf; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci spmf = get_bit_field(&clkregs->spmr, 24, 4); 3068c2ecf20Sopenharmony_ci return spmf_to_mult[spmf]; 3078c2ecf20Sopenharmony_ci} 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci/* 3108c2ecf20Sopenharmony_ci * get the SYS_DIV value and translate it into a divide factor 3118c2ecf20Sopenharmony_ci * 3128c2ecf20Sopenharmony_ci * values returned from here are a multiple of the real factor since the 3138c2ecf20Sopenharmony_ci * divide ratio is fractional 3148c2ecf20Sopenharmony_ci */ 3158c2ecf20Sopenharmony_cistatic int get_sys_div_x2(void) 3168c2ecf20Sopenharmony_ci{ 3178c2ecf20Sopenharmony_ci static int sysdiv_code_to_x2[] = { 3188c2ecf20Sopenharmony_ci 4, 5, 6, 7, 8, 9, 10, 14, 3198c2ecf20Sopenharmony_ci 12, 16, 18, 22, 20, 24, 26, 30, 3208c2ecf20Sopenharmony_ci 28, 32, 34, 38, 36, 40, 42, 46, 3218c2ecf20Sopenharmony_ci 44, 48, 50, 54, 52, 56, 58, 62, 3228c2ecf20Sopenharmony_ci 60, 64, 66, 3238c2ecf20Sopenharmony_ci }; 3248c2ecf20Sopenharmony_ci int divcode; 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci divcode = get_bit_field(&clkregs->scfr2, 26, 6); 3278c2ecf20Sopenharmony_ci return sysdiv_code_to_x2[divcode]; 3288c2ecf20Sopenharmony_ci} 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci/* 3318c2ecf20Sopenharmony_ci * get the CPMF value and translate it into a multiplier factor 3328c2ecf20Sopenharmony_ci * 3338c2ecf20Sopenharmony_ci * values returned from here are a multiple of the real factor since the 3348c2ecf20Sopenharmony_ci * multiplier ratio is fractional 3358c2ecf20Sopenharmony_ci */ 3368c2ecf20Sopenharmony_cistatic int get_cpmf_mult_x2(void) 3378c2ecf20Sopenharmony_ci{ 3388c2ecf20Sopenharmony_ci static int cpmf_to_mult_x36[] = { 3398c2ecf20Sopenharmony_ci /* 0b000 is "times 36" */ 3408c2ecf20Sopenharmony_ci 72, 2, 2, 3, 4, 5, 6, 7, 3418c2ecf20Sopenharmony_ci }; 3428c2ecf20Sopenharmony_ci static int cpmf_to_mult_0by[] = { 3438c2ecf20Sopenharmony_ci /* 0b000 is "bypass" */ 3448c2ecf20Sopenharmony_ci 2, 2, 2, 3, 4, 5, 6, 7, 3458c2ecf20Sopenharmony_ci }; 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci int *cpmf_to_mult; 3488c2ecf20Sopenharmony_ci int cpmf; 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci cpmf = get_bit_field(&clkregs->spmr, 16, 4); 3518c2ecf20Sopenharmony_ci if (soc_has_cpmf_0_bypass()) 3528c2ecf20Sopenharmony_ci cpmf_to_mult = cpmf_to_mult_0by; 3538c2ecf20Sopenharmony_ci else 3548c2ecf20Sopenharmony_ci cpmf_to_mult = cpmf_to_mult_x36; 3558c2ecf20Sopenharmony_ci return cpmf_to_mult[cpmf]; 3568c2ecf20Sopenharmony_ci} 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci/* 3598c2ecf20Sopenharmony_ci * some of the clock dividers do scale in a linear way, yet not all of 3608c2ecf20Sopenharmony_ci * their bit combinations are legal; use a divider table to get a 3618c2ecf20Sopenharmony_ci * resulting set of applicable divider values 3628c2ecf20Sopenharmony_ci */ 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci/* applies to the IPS_DIV, and PCI_DIV values */ 3658c2ecf20Sopenharmony_cistatic const struct clk_div_table divtab_2346[] = { 3668c2ecf20Sopenharmony_ci { .val = 2, .div = 2, }, 3678c2ecf20Sopenharmony_ci { .val = 3, .div = 3, }, 3688c2ecf20Sopenharmony_ci { .val = 4, .div = 4, }, 3698c2ecf20Sopenharmony_ci { .val = 6, .div = 6, }, 3708c2ecf20Sopenharmony_ci { .div = 0, }, 3718c2ecf20Sopenharmony_ci}; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci/* applies to the MBX_DIV, LPC_DIV, and NFC_DIV values */ 3748c2ecf20Sopenharmony_cistatic const struct clk_div_table divtab_1234[] = { 3758c2ecf20Sopenharmony_ci { .val = 1, .div = 1, }, 3768c2ecf20Sopenharmony_ci { .val = 2, .div = 2, }, 3778c2ecf20Sopenharmony_ci { .val = 3, .div = 3, }, 3788c2ecf20Sopenharmony_ci { .val = 4, .div = 4, }, 3798c2ecf20Sopenharmony_ci { .div = 0, }, 3808c2ecf20Sopenharmony_ci}; 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_cistatic int get_freq_from_dt(char *propname) 3838c2ecf20Sopenharmony_ci{ 3848c2ecf20Sopenharmony_ci struct device_node *np; 3858c2ecf20Sopenharmony_ci const unsigned int *prop; 3868c2ecf20Sopenharmony_ci int val; 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci val = 0; 3898c2ecf20Sopenharmony_ci np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-immr"); 3908c2ecf20Sopenharmony_ci if (np) { 3918c2ecf20Sopenharmony_ci prop = of_get_property(np, propname, NULL); 3928c2ecf20Sopenharmony_ci if (prop) 3938c2ecf20Sopenharmony_ci val = *prop; 3948c2ecf20Sopenharmony_ci of_node_put(np); 3958c2ecf20Sopenharmony_ci } 3968c2ecf20Sopenharmony_ci return val; 3978c2ecf20Sopenharmony_ci} 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_cistatic void mpc512x_clk_preset_data(void) 4008c2ecf20Sopenharmony_ci{ 4018c2ecf20Sopenharmony_ci size_t i; 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(clks); i++) 4048c2ecf20Sopenharmony_ci clks[i] = ERR_PTR(-ENODEV); 4058c2ecf20Sopenharmony_ci} 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci/* 4088c2ecf20Sopenharmony_ci * - receives the "bus frequency" from the caller (that's the IPS clock 4098c2ecf20Sopenharmony_ci * rate, the historical source of clock information) 4108c2ecf20Sopenharmony_ci * - fetches the system PLL multiplier and divider values as well as the 4118c2ecf20Sopenharmony_ci * IPS divider value from hardware 4128c2ecf20Sopenharmony_ci * - determines the REF clock rate either from the XTAL/OSC spec (if 4138c2ecf20Sopenharmony_ci * there is a device tree node describing the oscillator) or from the 4148c2ecf20Sopenharmony_ci * IPS bus clock (supported for backwards compatibility, such that 4158c2ecf20Sopenharmony_ci * setups without XTAL/OSC specs keep working) 4168c2ecf20Sopenharmony_ci * - creates the "ref" clock item in the clock tree, such that 4178c2ecf20Sopenharmony_ci * subsequent code can create the remainder of the hierarchy (REF -> 4188c2ecf20Sopenharmony_ci * SYS -> CSB -> IPS) from the REF clock rate and the returned mul/div 4198c2ecf20Sopenharmony_ci * values 4208c2ecf20Sopenharmony_ci */ 4218c2ecf20Sopenharmony_cistatic void mpc512x_clk_setup_ref_clock(struct device_node *np, int bus_freq, 4228c2ecf20Sopenharmony_ci int *sys_mul, int *sys_div, 4238c2ecf20Sopenharmony_ci int *ips_div) 4248c2ecf20Sopenharmony_ci{ 4258c2ecf20Sopenharmony_ci struct clk *osc_clk; 4268c2ecf20Sopenharmony_ci int calc_freq; 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci /* fetch mul/div factors from the hardware */ 4298c2ecf20Sopenharmony_ci *sys_mul = get_spmf_mult(); 4308c2ecf20Sopenharmony_ci *sys_mul *= 2; /* compensate for the fractional divider */ 4318c2ecf20Sopenharmony_ci *sys_div = get_sys_div_x2(); 4328c2ecf20Sopenharmony_ci *ips_div = get_bit_field(&clkregs->scfr1, 23, 3); 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci /* lookup the oscillator clock for its rate */ 4358c2ecf20Sopenharmony_ci osc_clk = of_clk_get_by_name(np, "osc"); 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci /* 4388c2ecf20Sopenharmony_ci * either descend from OSC to REF (and in bypassing verify the 4398c2ecf20Sopenharmony_ci * IPS rate), or backtrack from IPS and multiplier values that 4408c2ecf20Sopenharmony_ci * were fetched from hardware to REF and thus to the OSC value 4418c2ecf20Sopenharmony_ci * 4428c2ecf20Sopenharmony_ci * in either case the REF clock gets created here and the 4438c2ecf20Sopenharmony_ci * remainder of the clock tree can get spanned from there 4448c2ecf20Sopenharmony_ci */ 4458c2ecf20Sopenharmony_ci if (!IS_ERR(osc_clk)) { 4468c2ecf20Sopenharmony_ci clks[MPC512x_CLK_REF] = mpc512x_clk_factor("ref", "osc", 1, 1); 4478c2ecf20Sopenharmony_ci calc_freq = clk_get_rate(clks[MPC512x_CLK_REF]); 4488c2ecf20Sopenharmony_ci calc_freq *= *sys_mul; 4498c2ecf20Sopenharmony_ci calc_freq /= *sys_div; 4508c2ecf20Sopenharmony_ci calc_freq /= 2; 4518c2ecf20Sopenharmony_ci calc_freq /= *ips_div; 4528c2ecf20Sopenharmony_ci if (bus_freq && calc_freq != bus_freq) 4538c2ecf20Sopenharmony_ci pr_warn("calc rate %d != OF spec %d\n", 4548c2ecf20Sopenharmony_ci calc_freq, bus_freq); 4558c2ecf20Sopenharmony_ci } else { 4568c2ecf20Sopenharmony_ci calc_freq = bus_freq; /* start with IPS */ 4578c2ecf20Sopenharmony_ci calc_freq *= *ips_div; /* IPS -> CSB */ 4588c2ecf20Sopenharmony_ci calc_freq *= 2; /* CSB -> SYS */ 4598c2ecf20Sopenharmony_ci calc_freq *= *sys_div; /* SYS -> PLL out */ 4608c2ecf20Sopenharmony_ci calc_freq /= *sys_mul; /* PLL out -> REF == OSC */ 4618c2ecf20Sopenharmony_ci clks[MPC512x_CLK_REF] = mpc512x_clk_fixed("ref", calc_freq); 4628c2ecf20Sopenharmony_ci } 4638c2ecf20Sopenharmony_ci} 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci/* MCLK helpers {{{ */ 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci/* 4688c2ecf20Sopenharmony_ci * helper code for the MCLK subtree setup 4698c2ecf20Sopenharmony_ci * 4708c2ecf20Sopenharmony_ci * the overview in section 5.2.4 of the MPC5121e Reference Manual rev4 4718c2ecf20Sopenharmony_ci * suggests that all instances of the "PSC clock generation" are equal, 4728c2ecf20Sopenharmony_ci * and that one might re-use the PSC setup for MSCAN clock generation 4738c2ecf20Sopenharmony_ci * (section 5.2.5) as well, at least the logic if not the data for 4748c2ecf20Sopenharmony_ci * description 4758c2ecf20Sopenharmony_ci * 4768c2ecf20Sopenharmony_ci * the details (starting at page 5-20) show differences in the specific 4778c2ecf20Sopenharmony_ci * inputs of the first mux stage ("can clk in", "spdif tx"), and the 4788c2ecf20Sopenharmony_ci * factual non-availability of the second mux stage (it's present yet 4798c2ecf20Sopenharmony_ci * only one input is valid) 4808c2ecf20Sopenharmony_ci * 4818c2ecf20Sopenharmony_ci * the MSCAN clock related registers (starting at page 5-35) all 4828c2ecf20Sopenharmony_ci * reference "spdif clk" at the first mux stage and don't mention any 4838c2ecf20Sopenharmony_ci * "can clk" at all, which somehow is unexpected 4848c2ecf20Sopenharmony_ci * 4858c2ecf20Sopenharmony_ci * TODO re-check the document, and clarify whether the RM is correct in 4868c2ecf20Sopenharmony_ci * the overview or in the details, and whether the difference is a 4878c2ecf20Sopenharmony_ci * clipboard induced error or results from chip revisions 4888c2ecf20Sopenharmony_ci * 4898c2ecf20Sopenharmony_ci * it turns out that the RM rev4 as of 2012-06 talks about "can" for the 4908c2ecf20Sopenharmony_ci * PSCs while RM rev3 as of 2008-10 talks about "spdif", so I guess that 4918c2ecf20Sopenharmony_ci * first a doc update is required which better reflects reality in the 4928c2ecf20Sopenharmony_ci * SoC before the implementation should follow while no questions remain 4938c2ecf20Sopenharmony_ci */ 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci/* 4968c2ecf20Sopenharmony_ci * note that this declaration raises a checkpatch warning, but 4978c2ecf20Sopenharmony_ci * it's the very data type dictated by <linux/clk-provider.h>, 4988c2ecf20Sopenharmony_ci * "fixing" this warning will break compilation 4998c2ecf20Sopenharmony_ci */ 5008c2ecf20Sopenharmony_cistatic const char *parent_names_mux0_spdif[] = { 5018c2ecf20Sopenharmony_ci "sys", "ref", "psc-mclk-in", "spdif-tx", 5028c2ecf20Sopenharmony_ci}; 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_cistatic const char *parent_names_mux0_canin[] = { 5058c2ecf20Sopenharmony_ci "sys", "ref", "psc-mclk-in", "can-clk-in", 5068c2ecf20Sopenharmony_ci}; 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_cienum mclk_type { 5098c2ecf20Sopenharmony_ci MCLK_TYPE_PSC, 5108c2ecf20Sopenharmony_ci MCLK_TYPE_MSCAN, 5118c2ecf20Sopenharmony_ci MCLK_TYPE_SPDIF, 5128c2ecf20Sopenharmony_ci MCLK_TYPE_OUTCLK, 5138c2ecf20Sopenharmony_ci}; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_cistruct mclk_setup_data { 5168c2ecf20Sopenharmony_ci enum mclk_type type; 5178c2ecf20Sopenharmony_ci bool has_mclk1; 5188c2ecf20Sopenharmony_ci const char *name_mux0; 5198c2ecf20Sopenharmony_ci const char *name_en0; 5208c2ecf20Sopenharmony_ci const char *name_div0; 5218c2ecf20Sopenharmony_ci const char *parent_names_mux1[2]; 5228c2ecf20Sopenharmony_ci const char *name_mclk; 5238c2ecf20Sopenharmony_ci}; 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci#define MCLK_SETUP_DATA_PSC(id) { \ 5268c2ecf20Sopenharmony_ci MCLK_TYPE_PSC, 0, \ 5278c2ecf20Sopenharmony_ci "psc" #id "-mux0", \ 5288c2ecf20Sopenharmony_ci "psc" #id "-en0", \ 5298c2ecf20Sopenharmony_ci "psc" #id "_mclk_div", \ 5308c2ecf20Sopenharmony_ci { "psc" #id "_mclk_div", "dummy", }, \ 5318c2ecf20Sopenharmony_ci "psc" #id "_mclk", \ 5328c2ecf20Sopenharmony_ci} 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci#define MCLK_SETUP_DATA_MSCAN(id) { \ 5358c2ecf20Sopenharmony_ci MCLK_TYPE_MSCAN, 0, \ 5368c2ecf20Sopenharmony_ci "mscan" #id "-mux0", \ 5378c2ecf20Sopenharmony_ci "mscan" #id "-en0", \ 5388c2ecf20Sopenharmony_ci "mscan" #id "_mclk_div", \ 5398c2ecf20Sopenharmony_ci { "mscan" #id "_mclk_div", "dummy", }, \ 5408c2ecf20Sopenharmony_ci "mscan" #id "_mclk", \ 5418c2ecf20Sopenharmony_ci} 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci#define MCLK_SETUP_DATA_SPDIF { \ 5448c2ecf20Sopenharmony_ci MCLK_TYPE_SPDIF, 1, \ 5458c2ecf20Sopenharmony_ci "spdif-mux0", \ 5468c2ecf20Sopenharmony_ci "spdif-en0", \ 5478c2ecf20Sopenharmony_ci "spdif_mclk_div", \ 5488c2ecf20Sopenharmony_ci { "spdif_mclk_div", "spdif-rx", }, \ 5498c2ecf20Sopenharmony_ci "spdif_mclk", \ 5508c2ecf20Sopenharmony_ci} 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci#define MCLK_SETUP_DATA_OUTCLK(id) { \ 5538c2ecf20Sopenharmony_ci MCLK_TYPE_OUTCLK, 0, \ 5548c2ecf20Sopenharmony_ci "out" #id "-mux0", \ 5558c2ecf20Sopenharmony_ci "out" #id "-en0", \ 5568c2ecf20Sopenharmony_ci "out" #id "_mclk_div", \ 5578c2ecf20Sopenharmony_ci { "out" #id "_mclk_div", "dummy", }, \ 5588c2ecf20Sopenharmony_ci "out" #id "_clk", \ 5598c2ecf20Sopenharmony_ci} 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_cistatic struct mclk_setup_data mclk_psc_data[] = { 5628c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(0), 5638c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(1), 5648c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(2), 5658c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(3), 5668c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(4), 5678c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(5), 5688c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(6), 5698c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(7), 5708c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(8), 5718c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(9), 5728c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(10), 5738c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_PSC(11), 5748c2ecf20Sopenharmony_ci}; 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_cistatic struct mclk_setup_data mclk_mscan_data[] = { 5778c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_MSCAN(0), 5788c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_MSCAN(1), 5798c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_MSCAN(2), 5808c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_MSCAN(3), 5818c2ecf20Sopenharmony_ci}; 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_cistatic struct mclk_setup_data mclk_spdif_data[] = { 5848c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_SPDIF, 5858c2ecf20Sopenharmony_ci}; 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_cistatic struct mclk_setup_data mclk_outclk_data[] = { 5888c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_OUTCLK(0), 5898c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_OUTCLK(1), 5908c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_OUTCLK(2), 5918c2ecf20Sopenharmony_ci MCLK_SETUP_DATA_OUTCLK(3), 5928c2ecf20Sopenharmony_ci}; 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci/* setup the MCLK clock subtree of an individual PSC/MSCAN/SPDIF */ 5958c2ecf20Sopenharmony_cistatic void mpc512x_clk_setup_mclk(struct mclk_setup_data *entry, size_t idx) 5968c2ecf20Sopenharmony_ci{ 5978c2ecf20Sopenharmony_ci size_t clks_idx_pub, clks_idx_int; 5988c2ecf20Sopenharmony_ci u32 __iomem *mccr_reg; /* MCLK control register (mux, en, div) */ 5998c2ecf20Sopenharmony_ci int div; 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ci /* derive a few parameters from the component type and index */ 6028c2ecf20Sopenharmony_ci switch (entry->type) { 6038c2ecf20Sopenharmony_ci case MCLK_TYPE_PSC: 6048c2ecf20Sopenharmony_ci clks_idx_pub = MPC512x_CLK_PSC0_MCLK + idx; 6058c2ecf20Sopenharmony_ci clks_idx_int = MPC512x_CLK_MCLKS_FIRST 6068c2ecf20Sopenharmony_ci + (idx) * MCLK_MAX_IDX; 6078c2ecf20Sopenharmony_ci mccr_reg = &clkregs->psc_ccr[idx]; 6088c2ecf20Sopenharmony_ci break; 6098c2ecf20Sopenharmony_ci case MCLK_TYPE_MSCAN: 6108c2ecf20Sopenharmony_ci clks_idx_pub = MPC512x_CLK_MSCAN0_MCLK + idx; 6118c2ecf20Sopenharmony_ci clks_idx_int = MPC512x_CLK_MCLKS_FIRST 6128c2ecf20Sopenharmony_ci + (NR_PSCS + idx) * MCLK_MAX_IDX; 6138c2ecf20Sopenharmony_ci mccr_reg = &clkregs->mscan_ccr[idx]; 6148c2ecf20Sopenharmony_ci break; 6158c2ecf20Sopenharmony_ci case MCLK_TYPE_SPDIF: 6168c2ecf20Sopenharmony_ci clks_idx_pub = MPC512x_CLK_SPDIF_MCLK; 6178c2ecf20Sopenharmony_ci clks_idx_int = MPC512x_CLK_MCLKS_FIRST 6188c2ecf20Sopenharmony_ci + (NR_PSCS + NR_MSCANS) * MCLK_MAX_IDX; 6198c2ecf20Sopenharmony_ci mccr_reg = &clkregs->spccr; 6208c2ecf20Sopenharmony_ci break; 6218c2ecf20Sopenharmony_ci case MCLK_TYPE_OUTCLK: 6228c2ecf20Sopenharmony_ci clks_idx_pub = MPC512x_CLK_OUT0_CLK + idx; 6238c2ecf20Sopenharmony_ci clks_idx_int = MPC512x_CLK_MCLKS_FIRST 6248c2ecf20Sopenharmony_ci + (NR_PSCS + NR_MSCANS + NR_SPDIFS + idx) 6258c2ecf20Sopenharmony_ci * MCLK_MAX_IDX; 6268c2ecf20Sopenharmony_ci mccr_reg = &clkregs->out_ccr[idx]; 6278c2ecf20Sopenharmony_ci break; 6288c2ecf20Sopenharmony_ci default: 6298c2ecf20Sopenharmony_ci return; 6308c2ecf20Sopenharmony_ci } 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ci /* 6338c2ecf20Sopenharmony_ci * this was grabbed from the PPC_CLOCK implementation, which 6348c2ecf20Sopenharmony_ci * enforced a specific MCLK divider while the clock was gated 6358c2ecf20Sopenharmony_ci * during setup (that's a documented hardware requirement) 6368c2ecf20Sopenharmony_ci * 6378c2ecf20Sopenharmony_ci * the PPC_CLOCK implementation might even have violated the 6388c2ecf20Sopenharmony_ci * "MCLK <= IPS" constraint, the fixed divider value of 1 6398c2ecf20Sopenharmony_ci * results in a divider of 2 and thus MCLK = SYS/2 which equals 6408c2ecf20Sopenharmony_ci * CSB which is greater than IPS; the serial port setup may have 6418c2ecf20Sopenharmony_ci * adjusted the divider which the clock setup might have left in 6428c2ecf20Sopenharmony_ci * an undesirable state 6438c2ecf20Sopenharmony_ci * 6448c2ecf20Sopenharmony_ci * initial setup is: 6458c2ecf20Sopenharmony_ci * - MCLK 0 from SYS 6468c2ecf20Sopenharmony_ci * - MCLK DIV such to not exceed the IPS clock 6478c2ecf20Sopenharmony_ci * - MCLK 0 enabled 6488c2ecf20Sopenharmony_ci * - MCLK 1 from MCLK DIV 6498c2ecf20Sopenharmony_ci */ 6508c2ecf20Sopenharmony_ci div = clk_get_rate(clks[MPC512x_CLK_SYS]); 6518c2ecf20Sopenharmony_ci div /= clk_get_rate(clks[MPC512x_CLK_IPS]); 6528c2ecf20Sopenharmony_ci out_be32(mccr_reg, (0 << 16)); 6538c2ecf20Sopenharmony_ci out_be32(mccr_reg, (0 << 16) | ((div - 1) << 17)); 6548c2ecf20Sopenharmony_ci out_be32(mccr_reg, (1 << 16) | ((div - 1) << 17)); 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci /* 6578c2ecf20Sopenharmony_ci * create the 'struct clk' items of the MCLK's clock subtree 6588c2ecf20Sopenharmony_ci * 6598c2ecf20Sopenharmony_ci * note that by design we always create all nodes and won't take 6608c2ecf20Sopenharmony_ci * shortcuts here, because 6618c2ecf20Sopenharmony_ci * - the "internal" MCLK_DIV and MCLK_OUT signal in turn are 6628c2ecf20Sopenharmony_ci * selectable inputs to the CFM while those who "actually use" 6638c2ecf20Sopenharmony_ci * the PSC/MSCAN/SPDIF (serial drivers et al) need the MCLK 6648c2ecf20Sopenharmony_ci * for their bitrate 6658c2ecf20Sopenharmony_ci * - in the absence of "aliases" for clocks we need to create 6668c2ecf20Sopenharmony_ci * individial 'struct clk' items for whatever might get 6678c2ecf20Sopenharmony_ci * referenced or looked up, even if several of those items are 6688c2ecf20Sopenharmony_ci * identical from the logical POV (their rate value) 6698c2ecf20Sopenharmony_ci * - for easier future maintenance and for better reflection of 6708c2ecf20Sopenharmony_ci * the SoC's documentation, it appears appropriate to generate 6718c2ecf20Sopenharmony_ci * clock items even for those muxers which actually are NOPs 6728c2ecf20Sopenharmony_ci * (those with two inputs of which one is reserved) 6738c2ecf20Sopenharmony_ci */ 6748c2ecf20Sopenharmony_ci clks[clks_idx_int + MCLK_IDX_MUX0] = mpc512x_clk_muxed( 6758c2ecf20Sopenharmony_ci entry->name_mux0, 6768c2ecf20Sopenharmony_ci soc_has_mclk_mux0_canin() 6778c2ecf20Sopenharmony_ci ? &parent_names_mux0_canin[0] 6788c2ecf20Sopenharmony_ci : &parent_names_mux0_spdif[0], 6798c2ecf20Sopenharmony_ci ARRAY_SIZE(parent_names_mux0_spdif), 6808c2ecf20Sopenharmony_ci mccr_reg, 14, 2); 6818c2ecf20Sopenharmony_ci clks[clks_idx_int + MCLK_IDX_EN0] = mpc512x_clk_gated( 6828c2ecf20Sopenharmony_ci entry->name_en0, entry->name_mux0, 6838c2ecf20Sopenharmony_ci mccr_reg, 16); 6848c2ecf20Sopenharmony_ci clks[clks_idx_int + MCLK_IDX_DIV0] = mpc512x_clk_divider( 6858c2ecf20Sopenharmony_ci entry->name_div0, 6868c2ecf20Sopenharmony_ci entry->name_en0, CLK_SET_RATE_GATE, 6878c2ecf20Sopenharmony_ci mccr_reg, 17, 15, 0); 6888c2ecf20Sopenharmony_ci if (entry->has_mclk1) { 6898c2ecf20Sopenharmony_ci clks[clks_idx_pub] = mpc512x_clk_muxed( 6908c2ecf20Sopenharmony_ci entry->name_mclk, 6918c2ecf20Sopenharmony_ci &entry->parent_names_mux1[0], 6928c2ecf20Sopenharmony_ci ARRAY_SIZE(entry->parent_names_mux1), 6938c2ecf20Sopenharmony_ci mccr_reg, 7, 1); 6948c2ecf20Sopenharmony_ci } else { 6958c2ecf20Sopenharmony_ci clks[clks_idx_pub] = mpc512x_clk_factor( 6968c2ecf20Sopenharmony_ci entry->name_mclk, 6978c2ecf20Sopenharmony_ci entry->parent_names_mux1[0], 6988c2ecf20Sopenharmony_ci 1, 1); 6998c2ecf20Sopenharmony_ci } 7008c2ecf20Sopenharmony_ci} 7018c2ecf20Sopenharmony_ci 7028c2ecf20Sopenharmony_ci/* }}} MCLK helpers */ 7038c2ecf20Sopenharmony_ci 7048c2ecf20Sopenharmony_cistatic void mpc512x_clk_setup_clock_tree(struct device_node *np, int busfreq) 7058c2ecf20Sopenharmony_ci{ 7068c2ecf20Sopenharmony_ci int sys_mul, sys_div, ips_div; 7078c2ecf20Sopenharmony_ci int mul, div; 7088c2ecf20Sopenharmony_ci size_t mclk_idx; 7098c2ecf20Sopenharmony_ci int freq; 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci /* 7128c2ecf20Sopenharmony_ci * developer's notes: 7138c2ecf20Sopenharmony_ci * - consider whether to handle clocks which have both gates and 7148c2ecf20Sopenharmony_ci * dividers via intermediates or by means of composites 7158c2ecf20Sopenharmony_ci * - fractional dividers appear to not map well to composites 7168c2ecf20Sopenharmony_ci * since they can be seen as a fixed multiplier and an 7178c2ecf20Sopenharmony_ci * adjustable divider, while composites can only combine at 7188c2ecf20Sopenharmony_ci * most one of a mux, div, and gate each into one 'struct clk' 7198c2ecf20Sopenharmony_ci * item 7208c2ecf20Sopenharmony_ci * - PSC/MSCAN/SPDIF clock generation OTOH already is very 7218c2ecf20Sopenharmony_ci * specific and cannot get mapped to composites (at least not 7228c2ecf20Sopenharmony_ci * a single one, maybe two of them, but then some of these 7238c2ecf20Sopenharmony_ci * intermediate clock signals get referenced elsewhere (e.g. 7248c2ecf20Sopenharmony_ci * in the clock frequency measurement, CFM) and thus need 7258c2ecf20Sopenharmony_ci * publicly available names 7268c2ecf20Sopenharmony_ci * - the current source layout appropriately reflects the 7278c2ecf20Sopenharmony_ci * hardware setup, and it works, so it's questionable whether 7288c2ecf20Sopenharmony_ci * further changes will result in big enough a benefit 7298c2ecf20Sopenharmony_ci */ 7308c2ecf20Sopenharmony_ci 7318c2ecf20Sopenharmony_ci /* regardless of whether XTAL/OSC exists, have REF created */ 7328c2ecf20Sopenharmony_ci mpc512x_clk_setup_ref_clock(np, busfreq, &sys_mul, &sys_div, &ips_div); 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_ci /* now setup the REF -> SYS -> CSB -> IPS hierarchy */ 7358c2ecf20Sopenharmony_ci clks[MPC512x_CLK_SYS] = mpc512x_clk_factor("sys", "ref", 7368c2ecf20Sopenharmony_ci sys_mul, sys_div); 7378c2ecf20Sopenharmony_ci clks[MPC512x_CLK_CSB] = mpc512x_clk_factor("csb", "sys", 1, 2); 7388c2ecf20Sopenharmony_ci clks[MPC512x_CLK_IPS] = mpc512x_clk_divtable("ips", "csb", 7398c2ecf20Sopenharmony_ci &clkregs->scfr1, 23, 3, 7408c2ecf20Sopenharmony_ci divtab_2346); 7418c2ecf20Sopenharmony_ci /* now setup anything below SYS and CSB and IPS */ 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_ci clks[MPC512x_CLK_DDR_UG] = mpc512x_clk_factor("ddr-ug", "sys", 1, 2); 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_ci /* 7468c2ecf20Sopenharmony_ci * the Reference Manual discusses that for SDHC only even divide 7478c2ecf20Sopenharmony_ci * ratios are supported because clock domain synchronization 7488c2ecf20Sopenharmony_ci * between 'per' and 'ipg' is broken; 7498c2ecf20Sopenharmony_ci * keep the divider's bit 0 cleared (per reset value), and only 7508c2ecf20Sopenharmony_ci * allow to setup the divider's bits 7:1, which results in that 7518c2ecf20Sopenharmony_ci * only even divide ratios can get configured upon rate changes; 7528c2ecf20Sopenharmony_ci * keep the "x4" name because this bit shift hack is an internal 7538c2ecf20Sopenharmony_ci * implementation detail, the "fractional divider with quarters" 7548c2ecf20Sopenharmony_ci * semantics remains 7558c2ecf20Sopenharmony_ci */ 7568c2ecf20Sopenharmony_ci clks[MPC512x_CLK_SDHC_x4] = mpc512x_clk_factor("sdhc-x4", "csb", 2, 1); 7578c2ecf20Sopenharmony_ci clks[MPC512x_CLK_SDHC_UG] = mpc512x_clk_divider("sdhc-ug", "sdhc-x4", 0, 7588c2ecf20Sopenharmony_ci &clkregs->scfr2, 1, 7, 7598c2ecf20Sopenharmony_ci CLK_DIVIDER_ONE_BASED); 7608c2ecf20Sopenharmony_ci if (soc_has_sdhc2()) { 7618c2ecf20Sopenharmony_ci clks[MPC512x_CLK_SDHC2_UG] = mpc512x_clk_divider( 7628c2ecf20Sopenharmony_ci "sdhc2-ug", "sdhc-x4", 0, &clkregs->scfr2, 7638c2ecf20Sopenharmony_ci 9, 7, CLK_DIVIDER_ONE_BASED); 7648c2ecf20Sopenharmony_ci } 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci clks[MPC512x_CLK_DIU_x4] = mpc512x_clk_factor("diu-x4", "csb", 4, 1); 7678c2ecf20Sopenharmony_ci clks[MPC512x_CLK_DIU_UG] = mpc512x_clk_divider("diu-ug", "diu-x4", 0, 7688c2ecf20Sopenharmony_ci &clkregs->scfr1, 0, 8, 7698c2ecf20Sopenharmony_ci CLK_DIVIDER_ONE_BASED); 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci /* 7728c2ecf20Sopenharmony_ci * the "power architecture PLL" was setup from data which was 7738c2ecf20Sopenharmony_ci * sampled from the reset config word, at this point in time the 7748c2ecf20Sopenharmony_ci * configuration can be considered fixed and read only (i.e. no 7758c2ecf20Sopenharmony_ci * longer adjustable, or no longer in need of adjustment), which 7768c2ecf20Sopenharmony_ci * is why we don't register a PLL here but assume fixed factors 7778c2ecf20Sopenharmony_ci */ 7788c2ecf20Sopenharmony_ci mul = get_cpmf_mult_x2(); 7798c2ecf20Sopenharmony_ci div = 2; /* compensate for the fractional factor */ 7808c2ecf20Sopenharmony_ci clks[MPC512x_CLK_E300] = mpc512x_clk_factor("e300", "csb", mul, div); 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci if (soc_has_mbx()) { 7838c2ecf20Sopenharmony_ci clks[MPC512x_CLK_MBX_BUS_UG] = mpc512x_clk_factor( 7848c2ecf20Sopenharmony_ci "mbx-bus-ug", "csb", 1, 2); 7858c2ecf20Sopenharmony_ci clks[MPC512x_CLK_MBX_UG] = mpc512x_clk_divtable( 7868c2ecf20Sopenharmony_ci "mbx-ug", "mbx-bus-ug", &clkregs->scfr1, 7878c2ecf20Sopenharmony_ci 14, 3, divtab_1234); 7888c2ecf20Sopenharmony_ci clks[MPC512x_CLK_MBX_3D_UG] = mpc512x_clk_factor( 7898c2ecf20Sopenharmony_ci "mbx-3d-ug", "mbx-ug", 1, 1); 7908c2ecf20Sopenharmony_ci } 7918c2ecf20Sopenharmony_ci if (soc_has_pci()) { 7928c2ecf20Sopenharmony_ci clks[MPC512x_CLK_PCI_UG] = mpc512x_clk_divtable( 7938c2ecf20Sopenharmony_ci "pci-ug", "csb", &clkregs->scfr1, 7948c2ecf20Sopenharmony_ci 20, 3, divtab_2346); 7958c2ecf20Sopenharmony_ci } 7968c2ecf20Sopenharmony_ci if (soc_has_nfc_5125()) { 7978c2ecf20Sopenharmony_ci /* 7988c2ecf20Sopenharmony_ci * XXX TODO implement 5125 NFC clock setup logic, 7998c2ecf20Sopenharmony_ci * with high/low period counters in clkregs->scfr3, 8008c2ecf20Sopenharmony_ci * currently there are no users so it's ENOIMPL 8018c2ecf20Sopenharmony_ci */ 8028c2ecf20Sopenharmony_ci clks[MPC512x_CLK_NFC_UG] = ERR_PTR(-ENOTSUPP); 8038c2ecf20Sopenharmony_ci } else { 8048c2ecf20Sopenharmony_ci clks[MPC512x_CLK_NFC_UG] = mpc512x_clk_divtable( 8058c2ecf20Sopenharmony_ci "nfc-ug", "ips", &clkregs->scfr1, 8068c2ecf20Sopenharmony_ci 8, 3, divtab_1234); 8078c2ecf20Sopenharmony_ci } 8088c2ecf20Sopenharmony_ci clks[MPC512x_CLK_LPC_UG] = mpc512x_clk_divtable("lpc-ug", "ips", 8098c2ecf20Sopenharmony_ci &clkregs->scfr1, 11, 3, 8108c2ecf20Sopenharmony_ci divtab_1234); 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci clks[MPC512x_CLK_LPC] = mpc512x_clk_gated("lpc", "lpc-ug", 8138c2ecf20Sopenharmony_ci &clkregs->sccr1, 30); 8148c2ecf20Sopenharmony_ci clks[MPC512x_CLK_NFC] = mpc512x_clk_gated("nfc", "nfc-ug", 8158c2ecf20Sopenharmony_ci &clkregs->sccr1, 29); 8168c2ecf20Sopenharmony_ci if (soc_has_pata()) { 8178c2ecf20Sopenharmony_ci clks[MPC512x_CLK_PATA] = mpc512x_clk_gated( 8188c2ecf20Sopenharmony_ci "pata", "ips", &clkregs->sccr1, 28); 8198c2ecf20Sopenharmony_ci } 8208c2ecf20Sopenharmony_ci /* for PSCs there is a "registers" gate and a bitrate MCLK subtree */ 8218c2ecf20Sopenharmony_ci for (mclk_idx = 0; mclk_idx < soc_max_pscnum(); mclk_idx++) { 8228c2ecf20Sopenharmony_ci char name[12]; 8238c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "psc%d", mclk_idx); 8248c2ecf20Sopenharmony_ci clks[MPC512x_CLK_PSC0 + mclk_idx] = mpc512x_clk_gated( 8258c2ecf20Sopenharmony_ci name, "ips", &clkregs->sccr1, 27 - mclk_idx); 8268c2ecf20Sopenharmony_ci mpc512x_clk_setup_mclk(&mclk_psc_data[mclk_idx], mclk_idx); 8278c2ecf20Sopenharmony_ci } 8288c2ecf20Sopenharmony_ci clks[MPC512x_CLK_PSC_FIFO] = mpc512x_clk_gated("psc-fifo", "ips", 8298c2ecf20Sopenharmony_ci &clkregs->sccr1, 15); 8308c2ecf20Sopenharmony_ci if (soc_has_sata()) { 8318c2ecf20Sopenharmony_ci clks[MPC512x_CLK_SATA] = mpc512x_clk_gated( 8328c2ecf20Sopenharmony_ci "sata", "ips", &clkregs->sccr1, 14); 8338c2ecf20Sopenharmony_ci } 8348c2ecf20Sopenharmony_ci clks[MPC512x_CLK_FEC] = mpc512x_clk_gated("fec", "ips", 8358c2ecf20Sopenharmony_ci &clkregs->sccr1, 13); 8368c2ecf20Sopenharmony_ci if (soc_has_pci()) { 8378c2ecf20Sopenharmony_ci clks[MPC512x_CLK_PCI] = mpc512x_clk_gated( 8388c2ecf20Sopenharmony_ci "pci", "pci-ug", &clkregs->sccr1, 11); 8398c2ecf20Sopenharmony_ci } 8408c2ecf20Sopenharmony_ci clks[MPC512x_CLK_DDR] = mpc512x_clk_gated("ddr", "ddr-ug", 8418c2ecf20Sopenharmony_ci &clkregs->sccr1, 10); 8428c2ecf20Sopenharmony_ci if (soc_has_fec2()) { 8438c2ecf20Sopenharmony_ci clks[MPC512x_CLK_FEC2] = mpc512x_clk_gated( 8448c2ecf20Sopenharmony_ci "fec2", "ips", &clkregs->sccr1, 9); 8458c2ecf20Sopenharmony_ci } 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_ci clks[MPC512x_CLK_DIU] = mpc512x_clk_gated("diu", "diu-ug", 8488c2ecf20Sopenharmony_ci &clkregs->sccr2, 31); 8498c2ecf20Sopenharmony_ci if (soc_has_axe()) { 8508c2ecf20Sopenharmony_ci clks[MPC512x_CLK_AXE] = mpc512x_clk_gated( 8518c2ecf20Sopenharmony_ci "axe", "csb", &clkregs->sccr2, 30); 8528c2ecf20Sopenharmony_ci } 8538c2ecf20Sopenharmony_ci clks[MPC512x_CLK_MEM] = mpc512x_clk_gated("mem", "ips", 8548c2ecf20Sopenharmony_ci &clkregs->sccr2, 29); 8558c2ecf20Sopenharmony_ci clks[MPC512x_CLK_USB1] = mpc512x_clk_gated("usb1", "csb", 8568c2ecf20Sopenharmony_ci &clkregs->sccr2, 28); 8578c2ecf20Sopenharmony_ci clks[MPC512x_CLK_USB2] = mpc512x_clk_gated("usb2", "csb", 8588c2ecf20Sopenharmony_ci &clkregs->sccr2, 27); 8598c2ecf20Sopenharmony_ci clks[MPC512x_CLK_I2C] = mpc512x_clk_gated("i2c", "ips", 8608c2ecf20Sopenharmony_ci &clkregs->sccr2, 26); 8618c2ecf20Sopenharmony_ci /* MSCAN differs from PSC with just one gate for multiple components */ 8628c2ecf20Sopenharmony_ci clks[MPC512x_CLK_BDLC] = mpc512x_clk_gated("bdlc", "ips", 8638c2ecf20Sopenharmony_ci &clkregs->sccr2, 25); 8648c2ecf20Sopenharmony_ci for (mclk_idx = 0; mclk_idx < ARRAY_SIZE(mclk_mscan_data); mclk_idx++) 8658c2ecf20Sopenharmony_ci mpc512x_clk_setup_mclk(&mclk_mscan_data[mclk_idx], mclk_idx); 8668c2ecf20Sopenharmony_ci clks[MPC512x_CLK_SDHC] = mpc512x_clk_gated("sdhc", "sdhc-ug", 8678c2ecf20Sopenharmony_ci &clkregs->sccr2, 24); 8688c2ecf20Sopenharmony_ci /* there is only one SPDIF component, which shares MCLK support code */ 8698c2ecf20Sopenharmony_ci if (soc_has_spdif()) { 8708c2ecf20Sopenharmony_ci clks[MPC512x_CLK_SPDIF] = mpc512x_clk_gated( 8718c2ecf20Sopenharmony_ci "spdif", "ips", &clkregs->sccr2, 23); 8728c2ecf20Sopenharmony_ci mpc512x_clk_setup_mclk(&mclk_spdif_data[0], 0); 8738c2ecf20Sopenharmony_ci } 8748c2ecf20Sopenharmony_ci if (soc_has_mbx()) { 8758c2ecf20Sopenharmony_ci clks[MPC512x_CLK_MBX_BUS] = mpc512x_clk_gated( 8768c2ecf20Sopenharmony_ci "mbx-bus", "mbx-bus-ug", &clkregs->sccr2, 22); 8778c2ecf20Sopenharmony_ci clks[MPC512x_CLK_MBX] = mpc512x_clk_gated( 8788c2ecf20Sopenharmony_ci "mbx", "mbx-ug", &clkregs->sccr2, 21); 8798c2ecf20Sopenharmony_ci clks[MPC512x_CLK_MBX_3D] = mpc512x_clk_gated( 8808c2ecf20Sopenharmony_ci "mbx-3d", "mbx-3d-ug", &clkregs->sccr2, 20); 8818c2ecf20Sopenharmony_ci } 8828c2ecf20Sopenharmony_ci clks[MPC512x_CLK_IIM] = mpc512x_clk_gated("iim", "csb", 8838c2ecf20Sopenharmony_ci &clkregs->sccr2, 19); 8848c2ecf20Sopenharmony_ci if (soc_has_viu()) { 8858c2ecf20Sopenharmony_ci clks[MPC512x_CLK_VIU] = mpc512x_clk_gated( 8868c2ecf20Sopenharmony_ci "viu", "csb", &clkregs->sccr2, 18); 8878c2ecf20Sopenharmony_ci } 8888c2ecf20Sopenharmony_ci if (soc_has_sdhc2()) { 8898c2ecf20Sopenharmony_ci clks[MPC512x_CLK_SDHC2] = mpc512x_clk_gated( 8908c2ecf20Sopenharmony_ci "sdhc-2", "sdhc2-ug", &clkregs->sccr2, 17); 8918c2ecf20Sopenharmony_ci } 8928c2ecf20Sopenharmony_ci 8938c2ecf20Sopenharmony_ci if (soc_has_outclk()) { 8948c2ecf20Sopenharmony_ci size_t idx; /* used as mclk_idx, just to trim line length */ 8958c2ecf20Sopenharmony_ci for (idx = 0; idx < ARRAY_SIZE(mclk_outclk_data); idx++) 8968c2ecf20Sopenharmony_ci mpc512x_clk_setup_mclk(&mclk_outclk_data[idx], idx); 8978c2ecf20Sopenharmony_ci } 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_ci /* 9008c2ecf20Sopenharmony_ci * externally provided clocks (when implemented in hardware, 9018c2ecf20Sopenharmony_ci * device tree may specify values which otherwise were unknown) 9028c2ecf20Sopenharmony_ci */ 9038c2ecf20Sopenharmony_ci freq = get_freq_from_dt("psc_mclk_in"); 9048c2ecf20Sopenharmony_ci if (!freq) 9058c2ecf20Sopenharmony_ci freq = 25000000; 9068c2ecf20Sopenharmony_ci clks[MPC512x_CLK_PSC_MCLK_IN] = mpc512x_clk_fixed("psc_mclk_in", freq); 9078c2ecf20Sopenharmony_ci if (soc_has_mclk_mux0_canin()) { 9088c2ecf20Sopenharmony_ci freq = get_freq_from_dt("can_clk_in"); 9098c2ecf20Sopenharmony_ci clks[MPC512x_CLK_CAN_CLK_IN] = mpc512x_clk_fixed( 9108c2ecf20Sopenharmony_ci "can_clk_in", freq); 9118c2ecf20Sopenharmony_ci } else { 9128c2ecf20Sopenharmony_ci freq = get_freq_from_dt("spdif_tx_in"); 9138c2ecf20Sopenharmony_ci clks[MPC512x_CLK_SPDIF_TX_IN] = mpc512x_clk_fixed( 9148c2ecf20Sopenharmony_ci "spdif_tx_in", freq); 9158c2ecf20Sopenharmony_ci freq = get_freq_from_dt("spdif_rx_in"); 9168c2ecf20Sopenharmony_ci clks[MPC512x_CLK_SPDIF_TX_IN] = mpc512x_clk_fixed( 9178c2ecf20Sopenharmony_ci "spdif_rx_in", freq); 9188c2ecf20Sopenharmony_ci } 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_ci /* fixed frequency for AC97, always 24.567MHz */ 9218c2ecf20Sopenharmony_ci clks[MPC512x_CLK_AC97] = mpc512x_clk_fixed("ac97", 24567000); 9228c2ecf20Sopenharmony_ci 9238c2ecf20Sopenharmony_ci /* 9248c2ecf20Sopenharmony_ci * pre-enable those "internal" clock items which never get 9258c2ecf20Sopenharmony_ci * claimed by any peripheral driver, to not have the clock 9268c2ecf20Sopenharmony_ci * subsystem disable them late at startup 9278c2ecf20Sopenharmony_ci */ 9288c2ecf20Sopenharmony_ci clk_prepare_enable(clks[MPC512x_CLK_DUMMY]); 9298c2ecf20Sopenharmony_ci clk_prepare_enable(clks[MPC512x_CLK_E300]); /* PowerPC CPU */ 9308c2ecf20Sopenharmony_ci clk_prepare_enable(clks[MPC512x_CLK_DDR]); /* DRAM */ 9318c2ecf20Sopenharmony_ci clk_prepare_enable(clks[MPC512x_CLK_MEM]); /* SRAM */ 9328c2ecf20Sopenharmony_ci clk_prepare_enable(clks[MPC512x_CLK_IPS]); /* SoC periph */ 9338c2ecf20Sopenharmony_ci clk_prepare_enable(clks[MPC512x_CLK_LPC]); /* boot media */ 9348c2ecf20Sopenharmony_ci} 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci/* 9378c2ecf20Sopenharmony_ci * registers the set of public clocks (those listed in the dt-bindings/ 9388c2ecf20Sopenharmony_ci * header file) for OF lookups, keeps the intermediates private to us 9398c2ecf20Sopenharmony_ci */ 9408c2ecf20Sopenharmony_cistatic void mpc5121_clk_register_of_provider(struct device_node *np) 9418c2ecf20Sopenharmony_ci{ 9428c2ecf20Sopenharmony_ci clk_data.clks = clks; 9438c2ecf20Sopenharmony_ci clk_data.clk_num = MPC512x_CLK_LAST_PUBLIC + 1; /* _not_ ARRAY_SIZE() */ 9448c2ecf20Sopenharmony_ci of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data); 9458c2ecf20Sopenharmony_ci} 9468c2ecf20Sopenharmony_ci 9478c2ecf20Sopenharmony_ci/* 9488c2ecf20Sopenharmony_ci * temporary support for the period of time between introduction of CCF 9498c2ecf20Sopenharmony_ci * support and the adjustment of peripheral drivers to OF based lookups 9508c2ecf20Sopenharmony_ci */ 9518c2ecf20Sopenharmony_cistatic void mpc5121_clk_provide_migration_support(void) 9528c2ecf20Sopenharmony_ci{ 9538c2ecf20Sopenharmony_ci 9548c2ecf20Sopenharmony_ci /* 9558c2ecf20Sopenharmony_ci * pre-enable those clock items which are not yet appropriately 9568c2ecf20Sopenharmony_ci * acquired by their peripheral driver 9578c2ecf20Sopenharmony_ci * 9588c2ecf20Sopenharmony_ci * the PCI clock cannot get acquired by its peripheral driver, 9598c2ecf20Sopenharmony_ci * because for this platform the driver won't probe(), instead 9608c2ecf20Sopenharmony_ci * initialization is done from within the .setup_arch() routine 9618c2ecf20Sopenharmony_ci * at a point in time where the clock provider has not been 9628c2ecf20Sopenharmony_ci * setup yet and thus isn't available yet 9638c2ecf20Sopenharmony_ci * 9648c2ecf20Sopenharmony_ci * so we "pre-enable" the clock here, to not have the clock 9658c2ecf20Sopenharmony_ci * subsystem automatically disable this item in a late init call 9668c2ecf20Sopenharmony_ci * 9678c2ecf20Sopenharmony_ci * this PCI clock pre-enable workaround only applies when there 9688c2ecf20Sopenharmony_ci * are device tree nodes for PCI and thus the peripheral driver 9698c2ecf20Sopenharmony_ci * has attached to bridges, otherwise the PCI clock remains 9708c2ecf20Sopenharmony_ci * unused and so it gets disabled 9718c2ecf20Sopenharmony_ci */ 9728c2ecf20Sopenharmony_ci clk_prepare_enable(clks[MPC512x_CLK_PSC3_MCLK]);/* serial console */ 9738c2ecf20Sopenharmony_ci if (of_find_compatible_node(NULL, "pci", "fsl,mpc5121-pci")) 9748c2ecf20Sopenharmony_ci clk_prepare_enable(clks[MPC512x_CLK_PCI]); 9758c2ecf20Sopenharmony_ci} 9768c2ecf20Sopenharmony_ci 9778c2ecf20Sopenharmony_ci/* 9788c2ecf20Sopenharmony_ci * those macros are not exactly pretty, but they encapsulate a lot 9798c2ecf20Sopenharmony_ci * of copy'n'paste heavy code which is even more ugly, and reduce 9808c2ecf20Sopenharmony_ci * the potential for inconsistencies in those many code copies 9818c2ecf20Sopenharmony_ci */ 9828c2ecf20Sopenharmony_ci#define FOR_NODES(compatname) \ 9838c2ecf20Sopenharmony_ci for_each_compatible_node(np, NULL, compatname) 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ci#define NODE_PREP do { \ 9868c2ecf20Sopenharmony_ci of_address_to_resource(np, 0, &res); \ 9878c2ecf20Sopenharmony_ci snprintf(devname, sizeof(devname), "%pa.%s", &res.start, np->name); \ 9888c2ecf20Sopenharmony_ci} while (0) 9898c2ecf20Sopenharmony_ci 9908c2ecf20Sopenharmony_ci#define NODE_CHK(clkname, clkitem, regnode, regflag) do { \ 9918c2ecf20Sopenharmony_ci struct clk *clk; \ 9928c2ecf20Sopenharmony_ci clk = of_clk_get_by_name(np, clkname); \ 9938c2ecf20Sopenharmony_ci if (IS_ERR(clk)) { \ 9948c2ecf20Sopenharmony_ci clk = clkitem; \ 9958c2ecf20Sopenharmony_ci clk_register_clkdev(clk, clkname, devname); \ 9968c2ecf20Sopenharmony_ci if (regnode) \ 9978c2ecf20Sopenharmony_ci clk_register_clkdev(clk, clkname, np->name); \ 9988c2ecf20Sopenharmony_ci did_register |= DID_REG_ ## regflag; \ 9998c2ecf20Sopenharmony_ci pr_debug("clock alias name '%s' for dev '%s' pointer %p\n", \ 10008c2ecf20Sopenharmony_ci clkname, devname, clk); \ 10018c2ecf20Sopenharmony_ci } else { \ 10028c2ecf20Sopenharmony_ci clk_put(clk); \ 10038c2ecf20Sopenharmony_ci } \ 10048c2ecf20Sopenharmony_ci} while (0) 10058c2ecf20Sopenharmony_ci 10068c2ecf20Sopenharmony_ci/* 10078c2ecf20Sopenharmony_ci * register source code provided fallback results for clock lookups, 10088c2ecf20Sopenharmony_ci * these get consulted when OF based clock lookup fails (that is in the 10098c2ecf20Sopenharmony_ci * case of not yet adjusted device tree data, where clock related specs 10108c2ecf20Sopenharmony_ci * are missing) 10118c2ecf20Sopenharmony_ci */ 10128c2ecf20Sopenharmony_cistatic void mpc5121_clk_provide_backwards_compat(void) 10138c2ecf20Sopenharmony_ci{ 10148c2ecf20Sopenharmony_ci enum did_reg_flags { 10158c2ecf20Sopenharmony_ci DID_REG_PSC = BIT(0), 10168c2ecf20Sopenharmony_ci DID_REG_PSCFIFO = BIT(1), 10178c2ecf20Sopenharmony_ci DID_REG_NFC = BIT(2), 10188c2ecf20Sopenharmony_ci DID_REG_CAN = BIT(3), 10198c2ecf20Sopenharmony_ci DID_REG_I2C = BIT(4), 10208c2ecf20Sopenharmony_ci DID_REG_DIU = BIT(5), 10218c2ecf20Sopenharmony_ci DID_REG_VIU = BIT(6), 10228c2ecf20Sopenharmony_ci DID_REG_FEC = BIT(7), 10238c2ecf20Sopenharmony_ci DID_REG_USB = BIT(8), 10248c2ecf20Sopenharmony_ci DID_REG_PATA = BIT(9), 10258c2ecf20Sopenharmony_ci }; 10268c2ecf20Sopenharmony_ci 10278c2ecf20Sopenharmony_ci int did_register; 10288c2ecf20Sopenharmony_ci struct device_node *np; 10298c2ecf20Sopenharmony_ci struct resource res; 10308c2ecf20Sopenharmony_ci int idx; 10318c2ecf20Sopenharmony_ci char devname[32]; 10328c2ecf20Sopenharmony_ci 10338c2ecf20Sopenharmony_ci did_register = 0; 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_ci FOR_NODES(mpc512x_select_psc_compat()) { 10368c2ecf20Sopenharmony_ci NODE_PREP; 10378c2ecf20Sopenharmony_ci idx = (res.start >> 8) & 0xf; 10388c2ecf20Sopenharmony_ci NODE_CHK("ipg", clks[MPC512x_CLK_PSC0 + idx], 0, PSC); 10398c2ecf20Sopenharmony_ci NODE_CHK("mclk", clks[MPC512x_CLK_PSC0_MCLK + idx], 0, PSC); 10408c2ecf20Sopenharmony_ci } 10418c2ecf20Sopenharmony_ci 10428c2ecf20Sopenharmony_ci FOR_NODES("fsl,mpc5121-psc-fifo") { 10438c2ecf20Sopenharmony_ci NODE_PREP; 10448c2ecf20Sopenharmony_ci NODE_CHK("ipg", clks[MPC512x_CLK_PSC_FIFO], 1, PSCFIFO); 10458c2ecf20Sopenharmony_ci } 10468c2ecf20Sopenharmony_ci 10478c2ecf20Sopenharmony_ci FOR_NODES("fsl,mpc5121-nfc") { 10488c2ecf20Sopenharmony_ci NODE_PREP; 10498c2ecf20Sopenharmony_ci NODE_CHK("ipg", clks[MPC512x_CLK_NFC], 0, NFC); 10508c2ecf20Sopenharmony_ci } 10518c2ecf20Sopenharmony_ci 10528c2ecf20Sopenharmony_ci FOR_NODES("fsl,mpc5121-mscan") { 10538c2ecf20Sopenharmony_ci NODE_PREP; 10548c2ecf20Sopenharmony_ci idx = 0; 10558c2ecf20Sopenharmony_ci idx += (res.start & 0x2000) ? 2 : 0; 10568c2ecf20Sopenharmony_ci idx += (res.start & 0x0080) ? 1 : 0; 10578c2ecf20Sopenharmony_ci NODE_CHK("ipg", clks[MPC512x_CLK_BDLC], 0, CAN); 10588c2ecf20Sopenharmony_ci NODE_CHK("mclk", clks[MPC512x_CLK_MSCAN0_MCLK + idx], 0, CAN); 10598c2ecf20Sopenharmony_ci } 10608c2ecf20Sopenharmony_ci 10618c2ecf20Sopenharmony_ci /* 10628c2ecf20Sopenharmony_ci * do register the 'ips', 'sys', and 'ref' names globally 10638c2ecf20Sopenharmony_ci * instead of inside each individual CAN node, as there is no 10648c2ecf20Sopenharmony_ci * potential for a name conflict (in contrast to 'ipg' and 'mclk') 10658c2ecf20Sopenharmony_ci */ 10668c2ecf20Sopenharmony_ci if (did_register & DID_REG_CAN) { 10678c2ecf20Sopenharmony_ci clk_register_clkdev(clks[MPC512x_CLK_IPS], "ips", NULL); 10688c2ecf20Sopenharmony_ci clk_register_clkdev(clks[MPC512x_CLK_SYS], "sys", NULL); 10698c2ecf20Sopenharmony_ci clk_register_clkdev(clks[MPC512x_CLK_REF], "ref", NULL); 10708c2ecf20Sopenharmony_ci } 10718c2ecf20Sopenharmony_ci 10728c2ecf20Sopenharmony_ci FOR_NODES("fsl,mpc5121-i2c") { 10738c2ecf20Sopenharmony_ci NODE_PREP; 10748c2ecf20Sopenharmony_ci NODE_CHK("ipg", clks[MPC512x_CLK_I2C], 0, I2C); 10758c2ecf20Sopenharmony_ci } 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_ci /* 10788c2ecf20Sopenharmony_ci * workaround for the fact that the I2C driver does an "anonymous" 10798c2ecf20Sopenharmony_ci * lookup (NULL name spec, which yields the first clock spec) for 10808c2ecf20Sopenharmony_ci * which we cannot register an alias -- a _global_ 'ipg' alias that 10818c2ecf20Sopenharmony_ci * is not bound to any device name and returns the I2C clock item 10828c2ecf20Sopenharmony_ci * is not a good idea 10838c2ecf20Sopenharmony_ci * 10848c2ecf20Sopenharmony_ci * so we have the lookup in the peripheral driver fail, which is 10858c2ecf20Sopenharmony_ci * silent and non-fatal, and pre-enable the clock item here such 10868c2ecf20Sopenharmony_ci * that register access is possible 10878c2ecf20Sopenharmony_ci * 10888c2ecf20Sopenharmony_ci * see commit b3bfce2b "i2c: mpc: cleanup clock API use" for 10898c2ecf20Sopenharmony_ci * details, adjusting s/NULL/"ipg"/ in i2c-mpc.c would make this 10908c2ecf20Sopenharmony_ci * workaround obsolete 10918c2ecf20Sopenharmony_ci */ 10928c2ecf20Sopenharmony_ci if (did_register & DID_REG_I2C) 10938c2ecf20Sopenharmony_ci clk_prepare_enable(clks[MPC512x_CLK_I2C]); 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_ci FOR_NODES("fsl,mpc5121-diu") { 10968c2ecf20Sopenharmony_ci NODE_PREP; 10978c2ecf20Sopenharmony_ci NODE_CHK("ipg", clks[MPC512x_CLK_DIU], 1, DIU); 10988c2ecf20Sopenharmony_ci } 10998c2ecf20Sopenharmony_ci 11008c2ecf20Sopenharmony_ci FOR_NODES("fsl,mpc5121-viu") { 11018c2ecf20Sopenharmony_ci NODE_PREP; 11028c2ecf20Sopenharmony_ci NODE_CHK("ipg", clks[MPC512x_CLK_VIU], 0, VIU); 11038c2ecf20Sopenharmony_ci } 11048c2ecf20Sopenharmony_ci 11058c2ecf20Sopenharmony_ci /* 11068c2ecf20Sopenharmony_ci * note that 2771399a "fs_enet: cleanup clock API use" did use the 11078c2ecf20Sopenharmony_ci * "per" string for the clock lookup in contrast to the "ipg" name 11088c2ecf20Sopenharmony_ci * which most other nodes are using -- this is not a fatal thing 11098c2ecf20Sopenharmony_ci * but just something to keep in mind when doing compatibility 11108c2ecf20Sopenharmony_ci * registration, it's a non-issue with up-to-date device tree data 11118c2ecf20Sopenharmony_ci */ 11128c2ecf20Sopenharmony_ci FOR_NODES("fsl,mpc5121-fec") { 11138c2ecf20Sopenharmony_ci NODE_PREP; 11148c2ecf20Sopenharmony_ci NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC); 11158c2ecf20Sopenharmony_ci } 11168c2ecf20Sopenharmony_ci FOR_NODES("fsl,mpc5121-fec-mdio") { 11178c2ecf20Sopenharmony_ci NODE_PREP; 11188c2ecf20Sopenharmony_ci NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC); 11198c2ecf20Sopenharmony_ci } 11208c2ecf20Sopenharmony_ci /* 11218c2ecf20Sopenharmony_ci * MPC5125 has two FECs: FEC1 at 0x2800, FEC2 at 0x4800; 11228c2ecf20Sopenharmony_ci * the clock items don't "form an array" since FEC2 was 11238c2ecf20Sopenharmony_ci * added only later and was not allowed to shift all other 11248c2ecf20Sopenharmony_ci * clock item indices, so the numbers aren't adjacent 11258c2ecf20Sopenharmony_ci */ 11268c2ecf20Sopenharmony_ci FOR_NODES("fsl,mpc5125-fec") { 11278c2ecf20Sopenharmony_ci NODE_PREP; 11288c2ecf20Sopenharmony_ci if (res.start & 0x4000) 11298c2ecf20Sopenharmony_ci idx = MPC512x_CLK_FEC2; 11308c2ecf20Sopenharmony_ci else 11318c2ecf20Sopenharmony_ci idx = MPC512x_CLK_FEC; 11328c2ecf20Sopenharmony_ci NODE_CHK("per", clks[idx], 0, FEC); 11338c2ecf20Sopenharmony_ci } 11348c2ecf20Sopenharmony_ci 11358c2ecf20Sopenharmony_ci FOR_NODES("fsl,mpc5121-usb2-dr") { 11368c2ecf20Sopenharmony_ci NODE_PREP; 11378c2ecf20Sopenharmony_ci idx = (res.start & 0x4000) ? 1 : 0; 11388c2ecf20Sopenharmony_ci NODE_CHK("ipg", clks[MPC512x_CLK_USB1 + idx], 0, USB); 11398c2ecf20Sopenharmony_ci } 11408c2ecf20Sopenharmony_ci 11418c2ecf20Sopenharmony_ci FOR_NODES("fsl,mpc5121-pata") { 11428c2ecf20Sopenharmony_ci NODE_PREP; 11438c2ecf20Sopenharmony_ci NODE_CHK("ipg", clks[MPC512x_CLK_PATA], 0, PATA); 11448c2ecf20Sopenharmony_ci } 11458c2ecf20Sopenharmony_ci 11468c2ecf20Sopenharmony_ci /* 11478c2ecf20Sopenharmony_ci * try to collapse diagnostics into a single line of output yet 11488c2ecf20Sopenharmony_ci * provide a full list of what is missing, to avoid noise in the 11498c2ecf20Sopenharmony_ci * absence of up-to-date device tree data -- backwards 11508c2ecf20Sopenharmony_ci * compatibility to old DTBs is a requirement, updates may be 11518c2ecf20Sopenharmony_ci * desirable or preferrable but are not at all mandatory 11528c2ecf20Sopenharmony_ci */ 11538c2ecf20Sopenharmony_ci if (did_register) { 11548c2ecf20Sopenharmony_ci pr_notice("device tree lacks clock specs, adding fallbacks (0x%x,%s%s%s%s%s%s%s%s%s%s)\n", 11558c2ecf20Sopenharmony_ci did_register, 11568c2ecf20Sopenharmony_ci (did_register & DID_REG_PSC) ? " PSC" : "", 11578c2ecf20Sopenharmony_ci (did_register & DID_REG_PSCFIFO) ? " PSCFIFO" : "", 11588c2ecf20Sopenharmony_ci (did_register & DID_REG_NFC) ? " NFC" : "", 11598c2ecf20Sopenharmony_ci (did_register & DID_REG_CAN) ? " CAN" : "", 11608c2ecf20Sopenharmony_ci (did_register & DID_REG_I2C) ? " I2C" : "", 11618c2ecf20Sopenharmony_ci (did_register & DID_REG_DIU) ? " DIU" : "", 11628c2ecf20Sopenharmony_ci (did_register & DID_REG_VIU) ? " VIU" : "", 11638c2ecf20Sopenharmony_ci (did_register & DID_REG_FEC) ? " FEC" : "", 11648c2ecf20Sopenharmony_ci (did_register & DID_REG_USB) ? " USB" : "", 11658c2ecf20Sopenharmony_ci (did_register & DID_REG_PATA) ? " PATA" : ""); 11668c2ecf20Sopenharmony_ci } else { 11678c2ecf20Sopenharmony_ci pr_debug("device tree has clock specs, no fallbacks added\n"); 11688c2ecf20Sopenharmony_ci } 11698c2ecf20Sopenharmony_ci} 11708c2ecf20Sopenharmony_ci 11718c2ecf20Sopenharmony_ci/* 11728c2ecf20Sopenharmony_ci * The "fixed-clock" nodes (which includes the oscillator node if the board's 11738c2ecf20Sopenharmony_ci * DT provides one) has already been scanned by the of_clk_init() in 11748c2ecf20Sopenharmony_ci * time_init(). 11758c2ecf20Sopenharmony_ci */ 11768c2ecf20Sopenharmony_ciint __init mpc5121_clk_init(void) 11778c2ecf20Sopenharmony_ci{ 11788c2ecf20Sopenharmony_ci struct device_node *clk_np; 11798c2ecf20Sopenharmony_ci int busfreq; 11808c2ecf20Sopenharmony_ci 11818c2ecf20Sopenharmony_ci /* map the clock control registers */ 11828c2ecf20Sopenharmony_ci clk_np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock"); 11838c2ecf20Sopenharmony_ci if (!clk_np) 11848c2ecf20Sopenharmony_ci return -ENODEV; 11858c2ecf20Sopenharmony_ci clkregs = of_iomap(clk_np, 0); 11868c2ecf20Sopenharmony_ci WARN_ON(!clkregs); 11878c2ecf20Sopenharmony_ci 11888c2ecf20Sopenharmony_ci /* determine the SoC variant we run on */ 11898c2ecf20Sopenharmony_ci mpc512x_clk_determine_soc(); 11908c2ecf20Sopenharmony_ci 11918c2ecf20Sopenharmony_ci /* invalidate all not yet registered clock slots */ 11928c2ecf20Sopenharmony_ci mpc512x_clk_preset_data(); 11938c2ecf20Sopenharmony_ci 11948c2ecf20Sopenharmony_ci /* 11958c2ecf20Sopenharmony_ci * add a dummy clock for those situations where a clock spec is 11968c2ecf20Sopenharmony_ci * required yet no real clock is involved 11978c2ecf20Sopenharmony_ci */ 11988c2ecf20Sopenharmony_ci clks[MPC512x_CLK_DUMMY] = mpc512x_clk_fixed("dummy", 0); 11998c2ecf20Sopenharmony_ci 12008c2ecf20Sopenharmony_ci /* 12018c2ecf20Sopenharmony_ci * have all the real nodes in the clock tree populated from REF 12028c2ecf20Sopenharmony_ci * down to all leaves, either starting from the OSC node or from 12038c2ecf20Sopenharmony_ci * a REF root that was created from the IPS bus clock input 12048c2ecf20Sopenharmony_ci */ 12058c2ecf20Sopenharmony_ci busfreq = get_freq_from_dt("bus-frequency"); 12068c2ecf20Sopenharmony_ci mpc512x_clk_setup_clock_tree(clk_np, busfreq); 12078c2ecf20Sopenharmony_ci 12088c2ecf20Sopenharmony_ci /* register as an OF clock provider */ 12098c2ecf20Sopenharmony_ci mpc5121_clk_register_of_provider(clk_np); 12108c2ecf20Sopenharmony_ci 12118c2ecf20Sopenharmony_ci /* 12128c2ecf20Sopenharmony_ci * unbreak not yet adjusted peripheral drivers during migration 12138c2ecf20Sopenharmony_ci * towards fully operational common clock support, and allow 12148c2ecf20Sopenharmony_ci * operation in the absence of clock related device tree specs 12158c2ecf20Sopenharmony_ci */ 12168c2ecf20Sopenharmony_ci mpc5121_clk_provide_migration_support(); 12178c2ecf20Sopenharmony_ci mpc5121_clk_provide_backwards_compat(); 12188c2ecf20Sopenharmony_ci 12198c2ecf20Sopenharmony_ci return 0; 12208c2ecf20Sopenharmony_ci} 1221