1*52e6676eSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 21f27f152SAlex Elder /* 31f27f152SAlex Elder * Copyright (C) 2013 Broadcom Corporation 41f27f152SAlex Elder * Copyright 2013 Linaro Limited 51f27f152SAlex Elder */ 61f27f152SAlex Elder 71f27f152SAlex Elder #ifndef _CLK_KONA_H 81f27f152SAlex Elder #define _CLK_KONA_H 91f27f152SAlex Elder 101f27f152SAlex Elder #include <linux/kernel.h> 111f27f152SAlex Elder #include <linux/list.h> 121f27f152SAlex Elder #include <linux/spinlock.h> 131f27f152SAlex Elder #include <linux/slab.h> 141f27f152SAlex Elder #include <linux/device.h> 151f27f152SAlex Elder #include <linux/of.h> 161f27f152SAlex Elder #include <linux/clk-provider.h> 171f27f152SAlex Elder 181f27f152SAlex Elder #define BILLION 1000000000 191f27f152SAlex Elder 201f27f152SAlex Elder /* The common clock framework uses u8 to represent a parent index */ 211f27f152SAlex Elder #define PARENT_COUNT_MAX ((u32)U8_MAX) 221f27f152SAlex Elder 231f27f152SAlex Elder #define BAD_CLK_INDEX U8_MAX /* Can't ever be valid */ 241f27f152SAlex Elder #define BAD_CLK_NAME ((const char *)-1) 251f27f152SAlex Elder 261f27f152SAlex Elder #define BAD_SCALED_DIV_VALUE U64_MAX 271f27f152SAlex Elder 281f27f152SAlex Elder /* 291f27f152SAlex Elder * Utility macros for object flag management. If possible, flags 301f27f152SAlex Elder * should be defined such that 0 is the desired default value. 311f27f152SAlex Elder */ 321f27f152SAlex Elder #define FLAG(type, flag) BCM_CLK_ ## type ## _FLAGS_ ## flag 331f27f152SAlex Elder #define FLAG_SET(obj, type, flag) ((obj)->flags |= FLAG(type, flag)) 341f27f152SAlex Elder #define FLAG_CLEAR(obj, type, flag) ((obj)->flags &= ~(FLAG(type, flag))) 351f27f152SAlex Elder #define FLAG_FLIP(obj, type, flag) ((obj)->flags ^= FLAG(type, flag)) 361f27f152SAlex Elder #define FLAG_TEST(obj, type, flag) (!!((obj)->flags & FLAG(type, flag))) 371f27f152SAlex Elder 38a597faccSAlex Elder /* CCU field state tests */ 39a597faccSAlex Elder 40a597faccSAlex Elder #define ccu_policy_exists(ccu_policy) ((ccu_policy)->enable.offset != 0) 41a597faccSAlex Elder 421f27f152SAlex Elder /* Clock field state tests */ 431f27f152SAlex Elder 44a597faccSAlex Elder #define policy_exists(policy) ((policy)->offset != 0) 45a597faccSAlex Elder 461f27f152SAlex Elder #define gate_exists(gate) FLAG_TEST(gate, GATE, EXISTS) 471f27f152SAlex Elder #define gate_is_enabled(gate) FLAG_TEST(gate, GATE, ENABLED) 481f27f152SAlex Elder #define gate_is_hw_controllable(gate) FLAG_TEST(gate, GATE, HW) 491f27f152SAlex Elder #define gate_is_sw_controllable(gate) FLAG_TEST(gate, GATE, SW) 501f27f152SAlex Elder #define gate_is_sw_managed(gate) FLAG_TEST(gate, GATE, SW_MANAGED) 511f27f152SAlex Elder #define gate_is_no_disable(gate) FLAG_TEST(gate, GATE, NO_DISABLE) 521f27f152SAlex Elder 531f27f152SAlex Elder #define gate_flip_enabled(gate) FLAG_FLIP(gate, GATE, ENABLED) 541f27f152SAlex Elder 55dc613840SAlex Elder #define hyst_exists(hyst) ((hyst)->offset != 0) 56dc613840SAlex Elder 571f27f152SAlex Elder #define divider_exists(div) FLAG_TEST(div, DIV, EXISTS) 581f27f152SAlex Elder #define divider_is_fixed(div) FLAG_TEST(div, DIV, FIXED) 591f27f152SAlex Elder #define divider_has_fraction(div) (!divider_is_fixed(div) && \ 60e813d49dSAlex Elder (div)->u.s.frac_width > 0) 611f27f152SAlex Elder 621f27f152SAlex Elder #define selector_exists(sel) ((sel)->width != 0) 631f27f152SAlex Elder #define trigger_exists(trig) FLAG_TEST(trig, TRIG, EXISTS) 641f27f152SAlex Elder 65a597faccSAlex Elder #define policy_lvm_en_exists(enable) ((enable)->offset != 0) 66a597faccSAlex Elder #define policy_ctl_exists(control) ((control)->offset != 0) 67a597faccSAlex Elder 681f27f152SAlex Elder /* Clock type, used to tell common block what it's part of */ 691f27f152SAlex Elder enum bcm_clk_type { 701f27f152SAlex Elder bcm_clk_none, /* undefined clock type */ 711f27f152SAlex Elder bcm_clk_bus, 721f27f152SAlex Elder bcm_clk_core, 731f27f152SAlex Elder bcm_clk_peri 741f27f152SAlex Elder }; 751f27f152SAlex Elder 761f27f152SAlex Elder /* 77a597faccSAlex Elder * CCU policy control for clocks. Clocks can be enabled or disabled 78a597faccSAlex Elder * based on the CCU policy in effect. One bit in each policy mask 79a597faccSAlex Elder * register (one per CCU policy) represents whether the clock is 80a597faccSAlex Elder * enabled when that policy is effect or not. The CCU policy engine 81a597faccSAlex Elder * must be stopped to update these bits, and must be restarted again 82a597faccSAlex Elder * afterward. 83a597faccSAlex Elder */ 84a597faccSAlex Elder struct bcm_clk_policy { 85a597faccSAlex Elder u32 offset; /* first policy mask register offset */ 86a597faccSAlex Elder u32 bit; /* bit used in all mask registers */ 87a597faccSAlex Elder }; 88a597faccSAlex Elder 89a597faccSAlex Elder /* Policy initialization macro */ 90a597faccSAlex Elder 91a597faccSAlex Elder #define POLICY(_offset, _bit) \ 92a597faccSAlex Elder { \ 93a597faccSAlex Elder .offset = (_offset), \ 94a597faccSAlex Elder .bit = (_bit), \ 95a597faccSAlex Elder } 96a597faccSAlex Elder 97a597faccSAlex Elder /* 981f27f152SAlex Elder * Gating control and status is managed by a 32-bit gate register. 991f27f152SAlex Elder * 1001f27f152SAlex Elder * There are several types of gating available: 1011f27f152SAlex Elder * - (no gate) 1021f27f152SAlex Elder * A clock with no gate is assumed to be always enabled. 1031f27f152SAlex Elder * - hardware-only gating (auto-gating) 1041f27f152SAlex Elder * Enabling or disabling clocks with this type of gate is 1051f27f152SAlex Elder * managed automatically by the hardware. Such clocks can be 1061f27f152SAlex Elder * considered by the software to be enabled. The current status 1071f27f152SAlex Elder * of auto-gated clocks can be read from the gate status bit. 1081f27f152SAlex Elder * - software-only gating 1091f27f152SAlex Elder * Auto-gating is not available for this type of clock. 1101f27f152SAlex Elder * Instead, software manages whether it's enabled by setting or 1111f27f152SAlex Elder * clearing the enable bit. The current gate status of a gate 1121f27f152SAlex Elder * under software control can be read from the gate status bit. 1131f27f152SAlex Elder * To ensure a change to the gating status is complete, the 1141f27f152SAlex Elder * status bit can be polled to verify that the gate has entered 1151f27f152SAlex Elder * the desired state. 1161f27f152SAlex Elder * - selectable hardware or software gating 1171f27f152SAlex Elder * Gating for this type of clock can be configured to be either 1181f27f152SAlex Elder * under software or hardware control. Which type is in use is 1191f27f152SAlex Elder * determined by the hw_sw_sel bit of the gate register. 1201f27f152SAlex Elder */ 1211f27f152SAlex Elder struct bcm_clk_gate { 1221f27f152SAlex Elder u32 offset; /* gate register offset */ 1231f27f152SAlex Elder u32 status_bit; /* 0: gate is disabled; 0: gatge is enabled */ 1241f27f152SAlex Elder u32 en_bit; /* 0: disable; 1: enable */ 1251f27f152SAlex Elder u32 hw_sw_sel_bit; /* 0: hardware gating; 1: software gating */ 1261f27f152SAlex Elder u32 flags; /* BCM_CLK_GATE_FLAGS_* below */ 1271f27f152SAlex Elder }; 1281f27f152SAlex Elder 1291f27f152SAlex Elder /* 1301f27f152SAlex Elder * Gate flags: 1311f27f152SAlex Elder * HW means this gate can be auto-gated 1321f27f152SAlex Elder * SW means the state of this gate can be software controlled 1331f27f152SAlex Elder * NO_DISABLE means this gate is (only) enabled if under software control 1341f27f152SAlex Elder * SW_MANAGED means the status of this gate is under software control 1351f27f152SAlex Elder * ENABLED means this software-managed gate is *supposed* to be enabled 1361f27f152SAlex Elder */ 1371f27f152SAlex Elder #define BCM_CLK_GATE_FLAGS_EXISTS ((u32)1 << 0) /* Gate is valid */ 1381f27f152SAlex Elder #define BCM_CLK_GATE_FLAGS_HW ((u32)1 << 1) /* Can auto-gate */ 1391f27f152SAlex Elder #define BCM_CLK_GATE_FLAGS_SW ((u32)1 << 2) /* Software control */ 1401f27f152SAlex Elder #define BCM_CLK_GATE_FLAGS_NO_DISABLE ((u32)1 << 3) /* HW or enabled */ 1411f27f152SAlex Elder #define BCM_CLK_GATE_FLAGS_SW_MANAGED ((u32)1 << 4) /* SW now in control */ 1421f27f152SAlex Elder #define BCM_CLK_GATE_FLAGS_ENABLED ((u32)1 << 5) /* If SW_MANAGED */ 1431f27f152SAlex Elder 1441f27f152SAlex Elder /* 1451f27f152SAlex Elder * Gate initialization macros. 1461f27f152SAlex Elder * 1471f27f152SAlex Elder * Any gate initially under software control will be enabled. 1481f27f152SAlex Elder */ 1491f27f152SAlex Elder 1501f27f152SAlex Elder /* A hardware/software gate initially under software control */ 1511f27f152SAlex Elder #define HW_SW_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \ 1521f27f152SAlex Elder { \ 1531f27f152SAlex Elder .offset = (_offset), \ 1541f27f152SAlex Elder .status_bit = (_status_bit), \ 1551f27f152SAlex Elder .en_bit = (_en_bit), \ 1561f27f152SAlex Elder .hw_sw_sel_bit = (_hw_sw_sel_bit), \ 1571f27f152SAlex Elder .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \ 1581f27f152SAlex Elder FLAG(GATE, SW_MANAGED)|FLAG(GATE, ENABLED)| \ 1591f27f152SAlex Elder FLAG(GATE, EXISTS), \ 1601f27f152SAlex Elder } 1611f27f152SAlex Elder 1621f27f152SAlex Elder /* A hardware/software gate initially under hardware control */ 1631f27f152SAlex Elder #define HW_SW_GATE_AUTO(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \ 1641f27f152SAlex Elder { \ 1651f27f152SAlex Elder .offset = (_offset), \ 1661f27f152SAlex Elder .status_bit = (_status_bit), \ 1671f27f152SAlex Elder .en_bit = (_en_bit), \ 1681f27f152SAlex Elder .hw_sw_sel_bit = (_hw_sw_sel_bit), \ 1691f27f152SAlex Elder .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \ 1701f27f152SAlex Elder FLAG(GATE, EXISTS), \ 1711f27f152SAlex Elder } 1721f27f152SAlex Elder 1731f27f152SAlex Elder /* A hardware-or-enabled gate (enabled if not under hardware control) */ 1741f27f152SAlex Elder #define HW_ENABLE_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \ 1751f27f152SAlex Elder { \ 1761f27f152SAlex Elder .offset = (_offset), \ 1771f27f152SAlex Elder .status_bit = (_status_bit), \ 1781f27f152SAlex Elder .en_bit = (_en_bit), \ 1791f27f152SAlex Elder .hw_sw_sel_bit = (_hw_sw_sel_bit), \ 1801f27f152SAlex Elder .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \ 1811f27f152SAlex Elder FLAG(GATE, NO_DISABLE)|FLAG(GATE, EXISTS), \ 1821f27f152SAlex Elder } 1831f27f152SAlex Elder 1841f27f152SAlex Elder /* A software-only gate */ 1851f27f152SAlex Elder #define SW_ONLY_GATE(_offset, _status_bit, _en_bit) \ 1861f27f152SAlex Elder { \ 1871f27f152SAlex Elder .offset = (_offset), \ 1881f27f152SAlex Elder .status_bit = (_status_bit), \ 1891f27f152SAlex Elder .en_bit = (_en_bit), \ 1901f27f152SAlex Elder .flags = FLAG(GATE, SW)|FLAG(GATE, SW_MANAGED)| \ 1911f27f152SAlex Elder FLAG(GATE, ENABLED)|FLAG(GATE, EXISTS), \ 1921f27f152SAlex Elder } 1931f27f152SAlex Elder 1941f27f152SAlex Elder /* A hardware-only gate */ 1951f27f152SAlex Elder #define HW_ONLY_GATE(_offset, _status_bit) \ 1961f27f152SAlex Elder { \ 1971f27f152SAlex Elder .offset = (_offset), \ 1981f27f152SAlex Elder .status_bit = (_status_bit), \ 1991f27f152SAlex Elder .flags = FLAG(GATE, HW)|FLAG(GATE, EXISTS), \ 2001f27f152SAlex Elder } 2011f27f152SAlex Elder 202dc613840SAlex Elder /* Gate hysteresis for clocks */ 203dc613840SAlex Elder struct bcm_clk_hyst { 204dc613840SAlex Elder u32 offset; /* hyst register offset (normally CLKGATE) */ 205dc613840SAlex Elder u32 en_bit; /* bit used to enable hysteresis */ 206dc613840SAlex Elder u32 val_bit; /* if enabled: 0 = low delay; 1 = high delay */ 207dc613840SAlex Elder }; 208dc613840SAlex Elder 209dc613840SAlex Elder /* Hysteresis initialization macro */ 210dc613840SAlex Elder 211dc613840SAlex Elder #define HYST(_offset, _en_bit, _val_bit) \ 212dc613840SAlex Elder { \ 213dc613840SAlex Elder .offset = (_offset), \ 214dc613840SAlex Elder .en_bit = (_en_bit), \ 215dc613840SAlex Elder .val_bit = (_val_bit), \ 216dc613840SAlex Elder } 217dc613840SAlex Elder 2181f27f152SAlex Elder /* 2191f27f152SAlex Elder * Each clock can have zero, one, or two dividers which change the 2201f27f152SAlex Elder * output rate of the clock. Each divider can be either fixed or 2211f27f152SAlex Elder * variable. If there are two dividers, they are the "pre-divider" 2221f27f152SAlex Elder * and the "regular" or "downstream" divider. If there is only one, 2231f27f152SAlex Elder * there is no pre-divider. 2241f27f152SAlex Elder * 2251f27f152SAlex Elder * A fixed divider is any non-zero (positive) value, and it 2261f27f152SAlex Elder * indicates how the input rate is affected by the divider. 2271f27f152SAlex Elder * 2281f27f152SAlex Elder * The value of a variable divider is maintained in a sub-field of a 2291f27f152SAlex Elder * 32-bit divider register. The position of the field in the 2301f27f152SAlex Elder * register is defined by its offset and width. The value recorded 2311f27f152SAlex Elder * in this field is always 1 less than the value it represents. 2321f27f152SAlex Elder * 2331f27f152SAlex Elder * In addition, a variable divider can indicate that some subset 2341f27f152SAlex Elder * of its bits represent a "fractional" part of the divider. Such 2351f27f152SAlex Elder * bits comprise the low-order portion of the divider field, and can 2361f27f152SAlex Elder * be viewed as representing the portion of the divider that lies to 2371f27f152SAlex Elder * the right of the decimal point. Most variable dividers have zero 2381f27f152SAlex Elder * fractional bits. Variable dividers with non-zero fraction width 2391f27f152SAlex Elder * still record a value 1 less than the value they represent; the 2401f27f152SAlex Elder * added 1 does *not* affect the low-order bit in this case, it 2411f27f152SAlex Elder * affects the bits above the fractional part only. (Often in this 2421f27f152SAlex Elder * code a divider field value is distinguished from the value it 2431f27f152SAlex Elder * represents by referring to the latter as a "divisor".) 2441f27f152SAlex Elder * 2451f27f152SAlex Elder * In order to avoid dealing with fractions, divider arithmetic is 2461f27f152SAlex Elder * performed using "scaled" values. A scaled value is one that's 2471f27f152SAlex Elder * been left-shifted by the fractional width of a divider. Dividing 2481f27f152SAlex Elder * a scaled value by a scaled divisor produces the desired quotient 2491f27f152SAlex Elder * without loss of precision and without any other special handling 2501f27f152SAlex Elder * for fractions. 2511f27f152SAlex Elder * 2521f27f152SAlex Elder * The recorded value of a variable divider can be modified. To 2531f27f152SAlex Elder * modify either divider (or both), a clock must be enabled (i.e., 2541f27f152SAlex Elder * using its gate). In addition, a trigger register (described 2551f27f152SAlex Elder * below) must be used to commit the change, and polled to verify 2561f27f152SAlex Elder * the change is complete. 2571f27f152SAlex Elder */ 2581f27f152SAlex Elder struct bcm_clk_div { 2591f27f152SAlex Elder union { 2601f27f152SAlex Elder struct { /* variable divider */ 2611f27f152SAlex Elder u32 offset; /* divider register offset */ 2621f27f152SAlex Elder u32 shift; /* field shift */ 2631f27f152SAlex Elder u32 width; /* field width */ 2641f27f152SAlex Elder u32 frac_width; /* field fraction width */ 2651f27f152SAlex Elder 2661f27f152SAlex Elder u64 scaled_div; /* scaled divider value */ 267e813d49dSAlex Elder } s; 2681f27f152SAlex Elder u32 fixed; /* non-zero fixed divider value */ 269e813d49dSAlex Elder } u; 2701f27f152SAlex Elder u32 flags; /* BCM_CLK_DIV_FLAGS_* below */ 2711f27f152SAlex Elder }; 2721f27f152SAlex Elder 2731f27f152SAlex Elder /* 2741f27f152SAlex Elder * Divider flags: 2751f27f152SAlex Elder * EXISTS means this divider exists 2761f27f152SAlex Elder * FIXED means it is a fixed-rate divider 2771f27f152SAlex Elder */ 2781f27f152SAlex Elder #define BCM_CLK_DIV_FLAGS_EXISTS ((u32)1 << 0) /* Divider is valid */ 2791f27f152SAlex Elder #define BCM_CLK_DIV_FLAGS_FIXED ((u32)1 << 1) /* Fixed-value */ 2801f27f152SAlex Elder 2811f27f152SAlex Elder /* Divider initialization macros */ 2821f27f152SAlex Elder 2831f27f152SAlex Elder /* A fixed (non-zero) divider */ 2841f27f152SAlex Elder #define FIXED_DIVIDER(_value) \ 2851f27f152SAlex Elder { \ 286e813d49dSAlex Elder .u.fixed = (_value), \ 2871f27f152SAlex Elder .flags = FLAG(DIV, EXISTS)|FLAG(DIV, FIXED), \ 2881f27f152SAlex Elder } 2891f27f152SAlex Elder 2901f27f152SAlex Elder /* A divider with an integral divisor */ 2911f27f152SAlex Elder #define DIVIDER(_offset, _shift, _width) \ 2921f27f152SAlex Elder { \ 293e813d49dSAlex Elder .u.s.offset = (_offset), \ 294e813d49dSAlex Elder .u.s.shift = (_shift), \ 295e813d49dSAlex Elder .u.s.width = (_width), \ 296e813d49dSAlex Elder .u.s.scaled_div = BAD_SCALED_DIV_VALUE, \ 2971f27f152SAlex Elder .flags = FLAG(DIV, EXISTS), \ 2981f27f152SAlex Elder } 2991f27f152SAlex Elder 3001f27f152SAlex Elder /* A divider whose divisor has an integer and fractional part */ 3011f27f152SAlex Elder #define FRAC_DIVIDER(_offset, _shift, _width, _frac_width) \ 3021f27f152SAlex Elder { \ 303e813d49dSAlex Elder .u.s.offset = (_offset), \ 304e813d49dSAlex Elder .u.s.shift = (_shift), \ 305e813d49dSAlex Elder .u.s.width = (_width), \ 306e813d49dSAlex Elder .u.s.frac_width = (_frac_width), \ 307e813d49dSAlex Elder .u.s.scaled_div = BAD_SCALED_DIV_VALUE, \ 3081f27f152SAlex Elder .flags = FLAG(DIV, EXISTS), \ 3091f27f152SAlex Elder } 3101f27f152SAlex Elder 3111f27f152SAlex Elder /* 3121f27f152SAlex Elder * Clocks may have multiple "parent" clocks. If there is more than 3131f27f152SAlex Elder * one, a selector must be specified to define which of the parent 3141f27f152SAlex Elder * clocks is currently in use. The selected clock is indicated in a 3151f27f152SAlex Elder * sub-field of a 32-bit selector register. The range of 3161f27f152SAlex Elder * representable selector values typically exceeds the number of 3171f27f152SAlex Elder * available parent clocks. Occasionally the reset value of a 3181f27f152SAlex Elder * selector field is explicitly set to a (specific) value that does 3191f27f152SAlex Elder * not correspond to a defined input clock. 3201f27f152SAlex Elder * 3211f27f152SAlex Elder * We register all known parent clocks with the common clock code 3221f27f152SAlex Elder * using a packed array (i.e., no empty slots) of (parent) clock 3231f27f152SAlex Elder * names, and refer to them later using indexes into that array. 3241f27f152SAlex Elder * We maintain an array of selector values indexed by common clock 3251f27f152SAlex Elder * index values in order to map between these common clock indexes 3261f27f152SAlex Elder * and the selector values used by the hardware. 3271f27f152SAlex Elder * 3281f27f152SAlex Elder * Like dividers, a selector can be modified, but to do so a clock 3291f27f152SAlex Elder * must be enabled, and a trigger must be used to commit the change. 3301f27f152SAlex Elder */ 3311f27f152SAlex Elder struct bcm_clk_sel { 3321f27f152SAlex Elder u32 offset; /* selector register offset */ 3331f27f152SAlex Elder u32 shift; /* field shift */ 3341f27f152SAlex Elder u32 width; /* field width */ 3351f27f152SAlex Elder 3361f27f152SAlex Elder u32 parent_count; /* number of entries in parent_sel[] */ 3371f27f152SAlex Elder u32 *parent_sel; /* array of parent selector values */ 3381f27f152SAlex Elder u8 clk_index; /* current selected index in parent_sel[] */ 3391f27f152SAlex Elder }; 3401f27f152SAlex Elder 3411f27f152SAlex Elder /* Selector initialization macro */ 3421f27f152SAlex Elder #define SELECTOR(_offset, _shift, _width) \ 3431f27f152SAlex Elder { \ 3441f27f152SAlex Elder .offset = (_offset), \ 3451f27f152SAlex Elder .shift = (_shift), \ 3461f27f152SAlex Elder .width = (_width), \ 3471f27f152SAlex Elder .clk_index = BAD_CLK_INDEX, \ 3481f27f152SAlex Elder } 3491f27f152SAlex Elder 3501f27f152SAlex Elder /* 3511f27f152SAlex Elder * Making changes to a variable divider or a selector for a clock 3521f27f152SAlex Elder * requires the use of a trigger. A trigger is defined by a single 3531f27f152SAlex Elder * bit within a register. To signal a change, a 1 is written into 3541f27f152SAlex Elder * that bit. To determine when the change has been completed, that 3551f27f152SAlex Elder * trigger bit is polled; the read value will be 1 while the change 3561f27f152SAlex Elder * is in progress, and 0 when it is complete. 3571f27f152SAlex Elder * 3581f27f152SAlex Elder * Occasionally a clock will have more than one trigger. In this 3591f27f152SAlex Elder * case, the "pre-trigger" will be used when changing a clock's 3601f27f152SAlex Elder * selector and/or its pre-divider. 3611f27f152SAlex Elder */ 3621f27f152SAlex Elder struct bcm_clk_trig { 3631f27f152SAlex Elder u32 offset; /* trigger register offset */ 3641f27f152SAlex Elder u32 bit; /* trigger bit */ 3651f27f152SAlex Elder u32 flags; /* BCM_CLK_TRIG_FLAGS_* below */ 3661f27f152SAlex Elder }; 3671f27f152SAlex Elder 3681f27f152SAlex Elder /* 3691f27f152SAlex Elder * Trigger flags: 3701f27f152SAlex Elder * EXISTS means this trigger exists 3711f27f152SAlex Elder */ 3721f27f152SAlex Elder #define BCM_CLK_TRIG_FLAGS_EXISTS ((u32)1 << 0) /* Trigger is valid */ 3731f27f152SAlex Elder 3741f27f152SAlex Elder /* Trigger initialization macro */ 3751f27f152SAlex Elder #define TRIGGER(_offset, _bit) \ 3761f27f152SAlex Elder { \ 3771f27f152SAlex Elder .offset = (_offset), \ 3781f27f152SAlex Elder .bit = (_bit), \ 3791f27f152SAlex Elder .flags = FLAG(TRIG, EXISTS), \ 3801f27f152SAlex Elder } 3811f27f152SAlex Elder 3821f27f152SAlex Elder struct peri_clk_data { 383a597faccSAlex Elder struct bcm_clk_policy policy; 3841f27f152SAlex Elder struct bcm_clk_gate gate; 385dc613840SAlex Elder struct bcm_clk_hyst hyst; 3861f27f152SAlex Elder struct bcm_clk_trig pre_trig; 3871f27f152SAlex Elder struct bcm_clk_div pre_div; 3881f27f152SAlex Elder struct bcm_clk_trig trig; 3891f27f152SAlex Elder struct bcm_clk_div div; 3901f27f152SAlex Elder struct bcm_clk_sel sel; 3911f27f152SAlex Elder const char *clocks[]; /* must be last; use CLOCKS() to declare */ 3921f27f152SAlex Elder }; 3931f27f152SAlex Elder #define CLOCKS(...) { __VA_ARGS__, NULL, } 3941f27f152SAlex Elder #define NO_CLOCKS { NULL, } /* Must use of no parent clocks */ 3951f27f152SAlex Elder 3961f27f152SAlex Elder struct kona_clk { 3971f27f152SAlex Elder struct clk_hw hw; 398e7563252SAlex Elder struct clk_init_data init_data; /* includes name of this clock */ 3991f27f152SAlex Elder struct ccu_data *ccu; /* ccu this clock is associated with */ 4001f27f152SAlex Elder enum bcm_clk_type type; 4011f27f152SAlex Elder union { 4021f27f152SAlex Elder void *data; 4031f27f152SAlex Elder struct peri_clk_data *peri; 404e813d49dSAlex Elder } u; 4051f27f152SAlex Elder }; 4061f27f152SAlex Elder #define to_kona_clk(_hw) \ 4071f27f152SAlex Elder container_of(_hw, struct kona_clk, hw) 4081f27f152SAlex Elder 40903548ec0SAlex Elder /* Initialization macro for an entry in a CCU's kona_clks[] array. */ 41003548ec0SAlex Elder #define KONA_CLK(_ccu_name, _clk_name, _type) \ 41103548ec0SAlex Elder { \ 41203548ec0SAlex Elder .init_data = { \ 41303548ec0SAlex Elder .name = #_clk_name, \ 41403548ec0SAlex Elder .ops = &kona_ ## _type ## _clk_ops, \ 41503548ec0SAlex Elder }, \ 41603548ec0SAlex Elder .ccu = &_ccu_name ## _ccu_data, \ 41703548ec0SAlex Elder .type = bcm_clk_ ## _type, \ 41803548ec0SAlex Elder .u.data = &_clk_name ## _data, \ 41903548ec0SAlex Elder } 42003548ec0SAlex Elder #define LAST_KONA_CLK { .type = bcm_clk_none } 42103548ec0SAlex Elder 42203548ec0SAlex Elder /* 423a597faccSAlex Elder * CCU policy control. To enable software update of the policy 424a597faccSAlex Elder * tables the CCU policy engine must be stopped by setting the 425a597faccSAlex Elder * software update enable bit (LVM_EN). After an update the engine 426a597faccSAlex Elder * is restarted using the GO bit and either the GO_ATL or GO_AC bit. 427a597faccSAlex Elder */ 428a597faccSAlex Elder struct bcm_lvm_en { 429a597faccSAlex Elder u32 offset; /* LVM_EN register offset */ 430a597faccSAlex Elder u32 bit; /* POLICY_CONFIG_EN bit in register */ 431a597faccSAlex Elder }; 432a597faccSAlex Elder 433a597faccSAlex Elder /* Policy enable initialization macro */ 434a597faccSAlex Elder #define CCU_LVM_EN(_offset, _bit) \ 435a597faccSAlex Elder { \ 436a597faccSAlex Elder .offset = (_offset), \ 437a597faccSAlex Elder .bit = (_bit), \ 438a597faccSAlex Elder } 439a597faccSAlex Elder 440a597faccSAlex Elder struct bcm_policy_ctl { 441a597faccSAlex Elder u32 offset; /* POLICY_CTL register offset */ 442a597faccSAlex Elder u32 go_bit; 443a597faccSAlex Elder u32 atl_bit; /* GO, GO_ATL, and GO_AC bits */ 444a597faccSAlex Elder u32 ac_bit; 445a597faccSAlex Elder }; 446a597faccSAlex Elder 447a597faccSAlex Elder /* Policy control initialization macro */ 448a597faccSAlex Elder #define CCU_POLICY_CTL(_offset, _go_bit, _ac_bit, _atl_bit) \ 449a597faccSAlex Elder { \ 450a597faccSAlex Elder .offset = (_offset), \ 451a597faccSAlex Elder .go_bit = (_go_bit), \ 452a597faccSAlex Elder .ac_bit = (_ac_bit), \ 453a597faccSAlex Elder .atl_bit = (_atl_bit), \ 454a597faccSAlex Elder } 455a597faccSAlex Elder 456a597faccSAlex Elder struct ccu_policy { 457a597faccSAlex Elder struct bcm_lvm_en enable; 458a597faccSAlex Elder struct bcm_policy_ctl control; 459a597faccSAlex Elder }; 460a597faccSAlex Elder 461a597faccSAlex Elder /* 46203548ec0SAlex Elder * Each CCU defines a mapped area of memory containing registers 46303548ec0SAlex Elder * used to manage clocks implemented by the CCU. Access to memory 46403548ec0SAlex Elder * within the CCU's space is serialized by a spinlock. Before any 46503548ec0SAlex Elder * (other) address can be written, a special access "password" value 46603548ec0SAlex Elder * must be written to its WR_ACCESS register (located at the base 46703548ec0SAlex Elder * address of the range). We keep track of the name of each CCU as 46803548ec0SAlex Elder * it is set up, and maintain them in a list. 46903548ec0SAlex Elder */ 47003548ec0SAlex Elder struct ccu_data { 47103548ec0SAlex Elder void __iomem *base; /* base of mapped address space */ 47203548ec0SAlex Elder spinlock_t lock; /* serialization lock */ 47303548ec0SAlex Elder bool write_enabled; /* write access is currently enabled */ 474a597faccSAlex Elder struct ccu_policy policy; 47503548ec0SAlex Elder struct device_node *node; 476f37fccceSStephen Boyd size_t clk_num; 47703548ec0SAlex Elder const char *name; 47803548ec0SAlex Elder u32 range; /* byte range of address space */ 47903548ec0SAlex Elder struct kona_clk kona_clks[]; /* must be last */ 48003548ec0SAlex Elder }; 48103548ec0SAlex Elder 48203548ec0SAlex Elder /* Initialization for common fields in a Kona ccu_data structure */ 48303548ec0SAlex Elder #define KONA_CCU_COMMON(_prefix, _name, _ccuname) \ 48403548ec0SAlex Elder .name = #_name "_ccu", \ 48503548ec0SAlex Elder .lock = __SPIN_LOCK_UNLOCKED(_name ## _ccu_data.lock), \ 486f37fccceSStephen Boyd .clk_num = _prefix ## _ ## _ccuname ## _CCU_CLOCK_COUNT 48703548ec0SAlex Elder 4881f27f152SAlex Elder /* Exported globals */ 4891f27f152SAlex Elder 4901f27f152SAlex Elder extern struct clk_ops kona_peri_clk_ops; 4911f27f152SAlex Elder 4921f27f152SAlex Elder /* Externally visible functions */ 4931f27f152SAlex Elder 4941f27f152SAlex Elder extern u64 scaled_div_max(struct bcm_clk_div *div); 4951f27f152SAlex Elder extern u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value, 4961f27f152SAlex Elder u32 billionths); 4971f27f152SAlex Elder 498b12151caSAlex Elder extern void __init kona_dt_ccu_setup(struct ccu_data *ccu, 49903548ec0SAlex Elder struct device_node *node); 5001f27f152SAlex Elder extern bool __init kona_ccu_init(struct ccu_data *ccu); 5011f27f152SAlex Elder 5021f27f152SAlex Elder #endif /* _CLK_KONA_H */ 503