Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (C) 2013 Broadcom Corporation
0004  * Copyright 2013 Linaro Limited
0005  */
0006 
0007 #ifndef _CLK_KONA_H
0008 #define _CLK_KONA_H
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/list.h>
0012 #include <linux/spinlock.h>
0013 #include <linux/slab.h>
0014 #include <linux/device.h>
0015 #include <linux/of.h>
0016 #include <linux/clk-provider.h>
0017 
0018 #define BILLION     1000000000
0019 
0020 /* The common clock framework uses u8 to represent a parent index */
0021 #define PARENT_COUNT_MAX    ((u32)U8_MAX)
0022 
0023 #define BAD_CLK_INDEX       U8_MAX  /* Can't ever be valid */
0024 #define BAD_CLK_NAME        ((const char *)-1)
0025 
0026 #define BAD_SCALED_DIV_VALUE    U64_MAX
0027 
0028 /*
0029  * Utility macros for object flag management.  If possible, flags
0030  * should be defined such that 0 is the desired default value.
0031  */
0032 #define FLAG(type, flag)        BCM_CLK_ ## type ## _FLAGS_ ## flag
0033 #define FLAG_SET(obj, type, flag)   ((obj)->flags |= FLAG(type, flag))
0034 #define FLAG_CLEAR(obj, type, flag) ((obj)->flags &= ~(FLAG(type, flag)))
0035 #define FLAG_FLIP(obj, type, flag)  ((obj)->flags ^= FLAG(type, flag))
0036 #define FLAG_TEST(obj, type, flag)  (!!((obj)->flags & FLAG(type, flag)))
0037 
0038 /* CCU field state tests */
0039 
0040 #define ccu_policy_exists(ccu_policy)   ((ccu_policy)->enable.offset != 0)
0041 
0042 /* Clock field state tests */
0043 
0044 #define policy_exists(policy)       ((policy)->offset != 0)
0045 
0046 #define gate_exists(gate)       FLAG_TEST(gate, GATE, EXISTS)
0047 #define gate_is_enabled(gate)       FLAG_TEST(gate, GATE, ENABLED)
0048 #define gate_is_hw_controllable(gate)   FLAG_TEST(gate, GATE, HW)
0049 #define gate_is_sw_controllable(gate)   FLAG_TEST(gate, GATE, SW)
0050 #define gate_is_sw_managed(gate)    FLAG_TEST(gate, GATE, SW_MANAGED)
0051 #define gate_is_no_disable(gate)    FLAG_TEST(gate, GATE, NO_DISABLE)
0052 
0053 #define gate_flip_enabled(gate)     FLAG_FLIP(gate, GATE, ENABLED)
0054 
0055 #define hyst_exists(hyst)       ((hyst)->offset != 0)
0056 
0057 #define divider_exists(div)     FLAG_TEST(div, DIV, EXISTS)
0058 #define divider_is_fixed(div)       FLAG_TEST(div, DIV, FIXED)
0059 #define divider_has_fraction(div)   (!divider_is_fixed(div) && \
0060                         (div)->u.s.frac_width > 0)
0061 
0062 #define selector_exists(sel)        ((sel)->width != 0)
0063 #define trigger_exists(trig)        FLAG_TEST(trig, TRIG, EXISTS)
0064 
0065 #define policy_lvm_en_exists(enable)    ((enable)->offset != 0)
0066 #define policy_ctl_exists(control)  ((control)->offset != 0)
0067 
0068 /* Clock type, used to tell common block what it's part of */
0069 enum bcm_clk_type {
0070     bcm_clk_none,       /* undefined clock type */
0071     bcm_clk_bus,
0072     bcm_clk_core,
0073     bcm_clk_peri
0074 };
0075 
0076 /*
0077  * CCU policy control for clocks.  Clocks can be enabled or disabled
0078  * based on the CCU policy in effect.  One bit in each policy mask
0079  * register (one per CCU policy) represents whether the clock is
0080  * enabled when that policy is effect or not.  The CCU policy engine
0081  * must be stopped to update these bits, and must be restarted again
0082  * afterward.
0083  */
0084 struct bcm_clk_policy {
0085     u32 offset;     /* first policy mask register offset */
0086     u32 bit;        /* bit used in all mask registers */
0087 };
0088 
0089 /* Policy initialization macro */
0090 
0091 #define POLICY(_offset, _bit)                       \
0092     {                               \
0093         .offset = (_offset),                    \
0094         .bit = (_bit),                      \
0095     }
0096 
0097 /*
0098  * Gating control and status is managed by a 32-bit gate register.
0099  *
0100  * There are several types of gating available:
0101  * - (no gate)
0102  *     A clock with no gate is assumed to be always enabled.
0103  * - hardware-only gating (auto-gating)
0104  *     Enabling or disabling clocks with this type of gate is
0105  *     managed automatically by the hardware.  Such clocks can be
0106  *     considered by the software to be enabled.  The current status
0107  *     of auto-gated clocks can be read from the gate status bit.
0108  * - software-only gating
0109  *     Auto-gating is not available for this type of clock.
0110  *     Instead, software manages whether it's enabled by setting or
0111  *     clearing the enable bit.  The current gate status of a gate
0112  *     under software control can be read from the gate status bit.
0113  *     To ensure a change to the gating status is complete, the
0114  *     status bit can be polled to verify that the gate has entered
0115  *     the desired state.
0116  * - selectable hardware or software gating
0117  *     Gating for this type of clock can be configured to be either
0118  *     under software or hardware control.  Which type is in use is
0119  *     determined by the hw_sw_sel bit of the gate register.
0120  */
0121 struct bcm_clk_gate {
0122     u32 offset;     /* gate register offset */
0123     u32 status_bit;     /* 0: gate is disabled; 0: gatge is enabled */
0124     u32 en_bit;     /* 0: disable; 1: enable */
0125     u32 hw_sw_sel_bit;  /* 0: hardware gating; 1: software gating */
0126     u32 flags;      /* BCM_CLK_GATE_FLAGS_* below */
0127 };
0128 
0129 /*
0130  * Gate flags:
0131  *   HW         means this gate can be auto-gated
0132  *   SW         means the state of this gate can be software controlled
0133  *   NO_DISABLE means this gate is (only) enabled if under software control
0134  *   SW_MANAGED means the status of this gate is under software control
0135  *   ENABLED    means this software-managed gate is *supposed* to be enabled
0136  */
0137 #define BCM_CLK_GATE_FLAGS_EXISTS   ((u32)1 << 0)   /* Gate is valid */
0138 #define BCM_CLK_GATE_FLAGS_HW       ((u32)1 << 1)   /* Can auto-gate */
0139 #define BCM_CLK_GATE_FLAGS_SW       ((u32)1 << 2)   /* Software control */
0140 #define BCM_CLK_GATE_FLAGS_NO_DISABLE   ((u32)1 << 3)   /* HW or enabled */
0141 #define BCM_CLK_GATE_FLAGS_SW_MANAGED   ((u32)1 << 4)   /* SW now in control */
0142 #define BCM_CLK_GATE_FLAGS_ENABLED  ((u32)1 << 5)   /* If SW_MANAGED */
0143 
0144 /*
0145  * Gate initialization macros.
0146  *
0147  * Any gate initially under software control will be enabled.
0148  */
0149 
0150 /* A hardware/software gate initially under software control */
0151 #define HW_SW_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)   \
0152     {                               \
0153         .offset = (_offset),                    \
0154         .status_bit = (_status_bit),                \
0155         .en_bit = (_en_bit),                    \
0156         .hw_sw_sel_bit = (_hw_sw_sel_bit),          \
0157         .flags = FLAG(GATE, HW)|FLAG(GATE, SW)|         \
0158             FLAG(GATE, SW_MANAGED)|FLAG(GATE, ENABLED)| \
0159             FLAG(GATE, EXISTS),             \
0160     }
0161 
0162 /* A hardware/software gate initially under hardware control */
0163 #define HW_SW_GATE_AUTO(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)  \
0164     {                               \
0165         .offset = (_offset),                    \
0166         .status_bit = (_status_bit),                \
0167         .en_bit = (_en_bit),                    \
0168         .hw_sw_sel_bit = (_hw_sw_sel_bit),          \
0169         .flags = FLAG(GATE, HW)|FLAG(GATE, SW)|         \
0170             FLAG(GATE, EXISTS),             \
0171     }
0172 
0173 /* A hardware-or-enabled gate (enabled if not under hardware control) */
0174 #define HW_ENABLE_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)   \
0175     {                               \
0176         .offset = (_offset),                    \
0177         .status_bit = (_status_bit),                \
0178         .en_bit = (_en_bit),                    \
0179         .hw_sw_sel_bit = (_hw_sw_sel_bit),          \
0180         .flags = FLAG(GATE, HW)|FLAG(GATE, SW)|         \
0181             FLAG(GATE, NO_DISABLE)|FLAG(GATE, EXISTS),  \
0182     }
0183 
0184 /* A software-only gate */
0185 #define SW_ONLY_GATE(_offset, _status_bit, _en_bit)         \
0186     {                               \
0187         .offset = (_offset),                    \
0188         .status_bit = (_status_bit),                \
0189         .en_bit = (_en_bit),                    \
0190         .flags = FLAG(GATE, SW)|FLAG(GATE, SW_MANAGED)|     \
0191             FLAG(GATE, ENABLED)|FLAG(GATE, EXISTS),     \
0192     }
0193 
0194 /* A hardware-only gate */
0195 #define HW_ONLY_GATE(_offset, _status_bit)              \
0196     {                               \
0197         .offset = (_offset),                    \
0198         .status_bit = (_status_bit),                \
0199         .flags = FLAG(GATE, HW)|FLAG(GATE, EXISTS),     \
0200     }
0201 
0202 /* Gate hysteresis for clocks */
0203 struct bcm_clk_hyst {
0204     u32 offset;     /* hyst register offset (normally CLKGATE) */
0205     u32 en_bit;     /* bit used to enable hysteresis */
0206     u32 val_bit;        /* if enabled: 0 = low delay; 1 = high delay */
0207 };
0208 
0209 /* Hysteresis initialization macro */
0210 
0211 #define HYST(_offset, _en_bit, _val_bit)                \
0212     {                               \
0213         .offset = (_offset),                    \
0214         .en_bit = (_en_bit),                    \
0215         .val_bit = (_val_bit),                  \
0216     }
0217 
0218 /*
0219  * Each clock can have zero, one, or two dividers which change the
0220  * output rate of the clock.  Each divider can be either fixed or
0221  * variable.  If there are two dividers, they are the "pre-divider"
0222  * and the "regular" or "downstream" divider.  If there is only one,
0223  * there is no pre-divider.
0224  *
0225  * A fixed divider is any non-zero (positive) value, and it
0226  * indicates how the input rate is affected by the divider.
0227  *
0228  * The value of a variable divider is maintained in a sub-field of a
0229  * 32-bit divider register.  The position of the field in the
0230  * register is defined by its offset and width.  The value recorded
0231  * in this field is always 1 less than the value it represents.
0232  *
0233  * In addition, a variable divider can indicate that some subset
0234  * of its bits represent a "fractional" part of the divider.  Such
0235  * bits comprise the low-order portion of the divider field, and can
0236  * be viewed as representing the portion of the divider that lies to
0237  * the right of the decimal point.  Most variable dividers have zero
0238  * fractional bits.  Variable dividers with non-zero fraction width
0239  * still record a value 1 less than the value they represent; the
0240  * added 1 does *not* affect the low-order bit in this case, it
0241  * affects the bits above the fractional part only.  (Often in this
0242  * code a divider field value is distinguished from the value it
0243  * represents by referring to the latter as a "divisor".)
0244  *
0245  * In order to avoid dealing with fractions, divider arithmetic is
0246  * performed using "scaled" values.  A scaled value is one that's
0247  * been left-shifted by the fractional width of a divider.  Dividing
0248  * a scaled value by a scaled divisor produces the desired quotient
0249  * without loss of precision and without any other special handling
0250  * for fractions.
0251  *
0252  * The recorded value of a variable divider can be modified.  To
0253  * modify either divider (or both), a clock must be enabled (i.e.,
0254  * using its gate).  In addition, a trigger register (described
0255  * below) must be used to commit the change, and polled to verify
0256  * the change is complete.
0257  */
0258 struct bcm_clk_div {
0259     union {
0260         struct {    /* variable divider */
0261             u32 offset; /* divider register offset */
0262             u32 shift;  /* field shift */
0263             u32 width;  /* field width */
0264             u32 frac_width; /* field fraction width */
0265 
0266             u64 scaled_div; /* scaled divider value */
0267         } s;
0268         u32 fixed;  /* non-zero fixed divider value */
0269     } u;
0270     u32 flags;      /* BCM_CLK_DIV_FLAGS_* below */
0271 };
0272 
0273 /*
0274  * Divider flags:
0275  *   EXISTS means this divider exists
0276  *   FIXED means it is a fixed-rate divider
0277  */
0278 #define BCM_CLK_DIV_FLAGS_EXISTS    ((u32)1 << 0)   /* Divider is valid */
0279 #define BCM_CLK_DIV_FLAGS_FIXED     ((u32)1 << 1)   /* Fixed-value */
0280 
0281 /* Divider initialization macros */
0282 
0283 /* A fixed (non-zero) divider */
0284 #define FIXED_DIVIDER(_value)                       \
0285     {                               \
0286         .u.fixed = (_value),                    \
0287         .flags = FLAG(DIV, EXISTS)|FLAG(DIV, FIXED),        \
0288     }
0289 
0290 /* A divider with an integral divisor */
0291 #define DIVIDER(_offset, _shift, _width)                \
0292     {                               \
0293         .u.s.offset = (_offset),                \
0294         .u.s.shift = (_shift),                  \
0295         .u.s.width = (_width),                  \
0296         .u.s.scaled_div = BAD_SCALED_DIV_VALUE,         \
0297         .flags = FLAG(DIV, EXISTS),             \
0298     }
0299 
0300 /* A divider whose divisor has an integer and fractional part */
0301 #define FRAC_DIVIDER(_offset, _shift, _width, _frac_width)      \
0302     {                               \
0303         .u.s.offset = (_offset),                \
0304         .u.s.shift = (_shift),                  \
0305         .u.s.width = (_width),                  \
0306         .u.s.frac_width = (_frac_width),            \
0307         .u.s.scaled_div = BAD_SCALED_DIV_VALUE,         \
0308         .flags = FLAG(DIV, EXISTS),             \
0309     }
0310 
0311 /*
0312  * Clocks may have multiple "parent" clocks.  If there is more than
0313  * one, a selector must be specified to define which of the parent
0314  * clocks is currently in use.  The selected clock is indicated in a
0315  * sub-field of a 32-bit selector register.  The range of
0316  * representable selector values typically exceeds the number of
0317  * available parent clocks.  Occasionally the reset value of a
0318  * selector field is explicitly set to a (specific) value that does
0319  * not correspond to a defined input clock.
0320  *
0321  * We register all known parent clocks with the common clock code
0322  * using a packed array (i.e., no empty slots) of (parent) clock
0323  * names, and refer to them later using indexes into that array.
0324  * We maintain an array of selector values indexed by common clock
0325  * index values in order to map between these common clock indexes
0326  * and the selector values used by the hardware.
0327  *
0328  * Like dividers, a selector can be modified, but to do so a clock
0329  * must be enabled, and a trigger must be used to commit the change.
0330  */
0331 struct bcm_clk_sel {
0332     u32 offset;     /* selector register offset */
0333     u32 shift;      /* field shift */
0334     u32 width;      /* field width */
0335 
0336     u32 parent_count;   /* number of entries in parent_sel[] */
0337     u32 *parent_sel;    /* array of parent selector values */
0338     u8 clk_index;       /* current selected index in parent_sel[] */
0339 };
0340 
0341 /* Selector initialization macro */
0342 #define SELECTOR(_offset, _shift, _width)               \
0343     {                               \
0344         .offset = (_offset),                    \
0345         .shift = (_shift),                  \
0346         .width = (_width),                  \
0347         .clk_index = BAD_CLK_INDEX,             \
0348     }
0349 
0350 /*
0351  * Making changes to a variable divider or a selector for a clock
0352  * requires the use of a trigger.  A trigger is defined by a single
0353  * bit within a register.  To signal a change, a 1 is written into
0354  * that bit.  To determine when the change has been completed, that
0355  * trigger bit is polled; the read value will be 1 while the change
0356  * is in progress, and 0 when it is complete.
0357  *
0358  * Occasionally a clock will have more than one trigger.  In this
0359  * case, the "pre-trigger" will be used when changing a clock's
0360  * selector and/or its pre-divider.
0361  */
0362 struct bcm_clk_trig {
0363     u32 offset;     /* trigger register offset */
0364     u32 bit;        /* trigger bit */
0365     u32 flags;      /* BCM_CLK_TRIG_FLAGS_* below */
0366 };
0367 
0368 /*
0369  * Trigger flags:
0370  *   EXISTS means this trigger exists
0371  */
0372 #define BCM_CLK_TRIG_FLAGS_EXISTS   ((u32)1 << 0)   /* Trigger is valid */
0373 
0374 /* Trigger initialization macro */
0375 #define TRIGGER(_offset, _bit)                      \
0376     {                               \
0377         .offset = (_offset),                    \
0378         .bit = (_bit),                      \
0379         .flags = FLAG(TRIG, EXISTS),                \
0380     }
0381 
0382 struct peri_clk_data {
0383     struct bcm_clk_policy policy;
0384     struct bcm_clk_gate gate;
0385     struct bcm_clk_hyst hyst;
0386     struct bcm_clk_trig pre_trig;
0387     struct bcm_clk_div pre_div;
0388     struct bcm_clk_trig trig;
0389     struct bcm_clk_div div;
0390     struct bcm_clk_sel sel;
0391     const char *clocks[];   /* must be last; use CLOCKS() to declare */
0392 };
0393 #define CLOCKS(...) { __VA_ARGS__, NULL, }
0394 #define NO_CLOCKS   { NULL, }   /* Must use of no parent clocks */
0395 
0396 struct kona_clk {
0397     struct clk_hw hw;
0398     struct clk_init_data init_data; /* includes name of this clock */
0399     struct ccu_data *ccu;   /* ccu this clock is associated with */
0400     enum bcm_clk_type type;
0401     union {
0402         void *data;
0403         struct peri_clk_data *peri;
0404     } u;
0405 };
0406 #define to_kona_clk(_hw) \
0407     container_of(_hw, struct kona_clk, hw)
0408 
0409 /* Initialization macro for an entry in a CCU's kona_clks[] array. */
0410 #define KONA_CLK(_ccu_name, _clk_name, _type)               \
0411     {                               \
0412         .init_data  = {                 \
0413             .name = #_clk_name,             \
0414             .ops = &kona_ ## _type ## _clk_ops,     \
0415         },                          \
0416         .ccu        = &_ccu_name ## _ccu_data,      \
0417         .type       = bcm_clk_ ## _type,            \
0418         .u.data     = &_clk_name ## _data,          \
0419     }
0420 #define LAST_KONA_CLK   { .type = bcm_clk_none }
0421 
0422 /*
0423  * CCU policy control.  To enable software update of the policy
0424  * tables the CCU policy engine must be stopped by setting the
0425  * software update enable bit (LVM_EN).  After an update the engine
0426  * is restarted using the GO bit and either the GO_ATL or GO_AC bit.
0427  */
0428 struct bcm_lvm_en {
0429     u32 offset;     /* LVM_EN register offset */
0430     u32 bit;        /* POLICY_CONFIG_EN bit in register */
0431 };
0432 
0433 /* Policy enable initialization macro */
0434 #define CCU_LVM_EN(_offset, _bit)                   \
0435     {                               \
0436         .offset = (_offset),                    \
0437         .bit = (_bit),                      \
0438     }
0439 
0440 struct bcm_policy_ctl {
0441     u32 offset;     /* POLICY_CTL register offset */
0442     u32 go_bit;
0443     u32 atl_bit;        /* GO, GO_ATL, and GO_AC bits */
0444     u32 ac_bit;
0445 };
0446 
0447 /* Policy control initialization macro */
0448 #define CCU_POLICY_CTL(_offset, _go_bit, _ac_bit, _atl_bit)     \
0449     {                               \
0450         .offset = (_offset),                    \
0451         .go_bit = (_go_bit),                    \
0452         .ac_bit = (_ac_bit),                    \
0453         .atl_bit = (_atl_bit),                  \
0454     }
0455 
0456 struct ccu_policy {
0457     struct bcm_lvm_en enable;
0458     struct bcm_policy_ctl control;
0459 };
0460 
0461 /*
0462  * Each CCU defines a mapped area of memory containing registers
0463  * used to manage clocks implemented by the CCU.  Access to memory
0464  * within the CCU's space is serialized by a spinlock.  Before any
0465  * (other) address can be written, a special access "password" value
0466  * must be written to its WR_ACCESS register (located at the base
0467  * address of the range).  We keep track of the name of each CCU as
0468  * it is set up, and maintain them in a list.
0469  */
0470 struct ccu_data {
0471     void __iomem *base; /* base of mapped address space */
0472     spinlock_t lock;    /* serialization lock */
0473     bool write_enabled; /* write access is currently enabled */
0474     struct ccu_policy policy;
0475     struct device_node *node;
0476     size_t clk_num;
0477     const char *name;
0478     u32 range;      /* byte range of address space */
0479     struct kona_clk kona_clks[];    /* must be last */
0480 };
0481 
0482 /* Initialization for common fields in a Kona ccu_data structure */
0483 #define KONA_CCU_COMMON(_prefix, _name, _ccuname)               \
0484     .name       = #_name "_ccu",                    \
0485     .lock       = __SPIN_LOCK_UNLOCKED(_name ## _ccu_data.lock),    \
0486     .clk_num    = _prefix ## _ ## _ccuname ## _CCU_CLOCK_COUNT
0487 
0488 /* Exported globals */
0489 
0490 extern struct clk_ops kona_peri_clk_ops;
0491 
0492 /* Externally visible functions */
0493 
0494 extern u64 scaled_div_max(struct bcm_clk_div *div);
0495 extern u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value,
0496                 u32 billionths);
0497 
0498 extern void __init kona_dt_ccu_setup(struct ccu_data *ccu,
0499                 struct device_node *node);
0500 extern bool __init kona_ccu_init(struct ccu_data *ccu);
0501 
0502 #endif /* _CLK_KONA_H */