/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Copyright (C) 2013 Broadcom Corporation3* Copyright 2013 Linaro Limited4*/56#ifndef _CLK_KONA_H7#define _CLK_KONA_H89#include <linux/kernel.h>10#include <linux/list.h>11#include <linux/spinlock.h>12#include <linux/slab.h>13#include <linux/device.h>14#include <linux/of.h>15#include <linux/clk-provider.h>1617#define BILLION 10000000001819/* The common clock framework uses u8 to represent a parent index */20#define PARENT_COUNT_MAX ((u32)U8_MAX)2122#define BAD_CLK_INDEX U8_MAX /* Can't ever be valid */23#define BAD_CLK_NAME ((const char *)-1)2425#define BAD_SCALED_DIV_VALUE U64_MAX2627/*28* Utility macros for object flag management. If possible, flags29* should be defined such that 0 is the desired default value.30*/31#define FLAG(type, flag) BCM_CLK_ ## type ## _FLAGS_ ## flag32#define FLAG_SET(obj, type, flag) ((obj)->flags |= FLAG(type, flag))33#define FLAG_CLEAR(obj, type, flag) ((obj)->flags &= ~(FLAG(type, flag)))34#define FLAG_FLIP(obj, type, flag) ((obj)->flags ^= FLAG(type, flag))35#define FLAG_TEST(obj, type, flag) (!!((obj)->flags & FLAG(type, flag)))3637/* CCU field state tests */3839#define ccu_policy_exists(ccu_policy) ((ccu_policy)->enable.offset != 0)4041/* Clock field state tests */4243#define policy_exists(policy) ((policy)->offset != 0)4445#define gate_exists(gate) FLAG_TEST(gate, GATE, EXISTS)46#define gate_is_enabled(gate) FLAG_TEST(gate, GATE, ENABLED)47#define gate_is_hw_controllable(gate) FLAG_TEST(gate, GATE, HW)48#define gate_is_sw_controllable(gate) FLAG_TEST(gate, GATE, SW)49#define gate_is_sw_managed(gate) FLAG_TEST(gate, GATE, SW_MANAGED)50#define gate_is_no_disable(gate) FLAG_TEST(gate, GATE, NO_DISABLE)5152#define gate_flip_enabled(gate) FLAG_FLIP(gate, GATE, ENABLED)5354#define hyst_exists(hyst) ((hyst)->offset != 0)5556#define divider_exists(div) FLAG_TEST(div, DIV, EXISTS)57#define divider_is_fixed(div) FLAG_TEST(div, DIV, FIXED)58#define divider_has_fraction(div) (!divider_is_fixed(div) && \59(div)->u.s.frac_width > 0)6061#define selector_exists(sel) ((sel)->width != 0)62#define trigger_exists(trig) FLAG_TEST(trig, TRIG, EXISTS)6364#define policy_lvm_en_exists(enable) ((enable)->offset != 0)65#define policy_ctl_exists(control) ((control)->offset != 0)6667/* Clock type, used to tell common block what it's part of */68enum bcm_clk_type {69bcm_clk_none, /* undefined clock type */70bcm_clk_bus,71bcm_clk_core,72bcm_clk_peri73};7475/*76* CCU policy control for clocks. Clocks can be enabled or disabled77* based on the CCU policy in effect. One bit in each policy mask78* register (one per CCU policy) represents whether the clock is79* enabled when that policy is effect or not. The CCU policy engine80* must be stopped to update these bits, and must be restarted again81* afterward.82*/83struct bcm_clk_policy {84u32 offset; /* first policy mask register offset */85u32 bit; /* bit used in all mask registers */86};8788/* Policy initialization macro */8990#define POLICY(_offset, _bit) \91{ \92.offset = (_offset), \93.bit = (_bit), \94}9596/*97* Gating control and status is managed by a 32-bit gate register.98*99* There are several types of gating available:100* - (no gate)101* A clock with no gate is assumed to be always enabled.102* - hardware-only gating (auto-gating)103* Enabling or disabling clocks with this type of gate is104* managed automatically by the hardware. Such clocks can be105* considered by the software to be enabled. The current status106* of auto-gated clocks can be read from the gate status bit.107* - software-only gating108* Auto-gating is not available for this type of clock.109* Instead, software manages whether it's enabled by setting or110* clearing the enable bit. The current gate status of a gate111* under software control can be read from the gate status bit.112* To ensure a change to the gating status is complete, the113* status bit can be polled to verify that the gate has entered114* the desired state.115* - selectable hardware or software gating116* Gating for this type of clock can be configured to be either117* under software or hardware control. Which type is in use is118* determined by the hw_sw_sel bit of the gate register.119*/120struct bcm_clk_gate {121u32 offset; /* gate register offset */122u32 status_bit; /* 0: gate is disabled; 0: gatge is enabled */123u32 en_bit; /* 0: disable; 1: enable */124u32 hw_sw_sel_bit; /* 0: hardware gating; 1: software gating */125u32 flags; /* BCM_CLK_GATE_FLAGS_* below */126};127128/*129* Gate flags:130* HW means this gate can be auto-gated131* SW means the state of this gate can be software controlled132* NO_DISABLE means this gate is (only) enabled if under software control133* SW_MANAGED means the status of this gate is under software control134* ENABLED means this software-managed gate is *supposed* to be enabled135*/136#define BCM_CLK_GATE_FLAGS_EXISTS ((u32)1 << 0) /* Gate is valid */137#define BCM_CLK_GATE_FLAGS_HW ((u32)1 << 1) /* Can auto-gate */138#define BCM_CLK_GATE_FLAGS_SW ((u32)1 << 2) /* Software control */139#define BCM_CLK_GATE_FLAGS_NO_DISABLE ((u32)1 << 3) /* HW or enabled */140#define BCM_CLK_GATE_FLAGS_SW_MANAGED ((u32)1 << 4) /* SW now in control */141#define BCM_CLK_GATE_FLAGS_ENABLED ((u32)1 << 5) /* If SW_MANAGED */142143/*144* Gate initialization macros.145*146* Any gate initially under software control will be enabled.147*/148149/* A hardware/software gate initially under software control */150#define HW_SW_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \151{ \152.offset = (_offset), \153.status_bit = (_status_bit), \154.en_bit = (_en_bit), \155.hw_sw_sel_bit = (_hw_sw_sel_bit), \156.flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \157FLAG(GATE, SW_MANAGED)|FLAG(GATE, ENABLED)| \158FLAG(GATE, EXISTS), \159}160161/* A hardware/software gate initially under hardware control */162#define HW_SW_GATE_AUTO(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \163{ \164.offset = (_offset), \165.status_bit = (_status_bit), \166.en_bit = (_en_bit), \167.hw_sw_sel_bit = (_hw_sw_sel_bit), \168.flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \169FLAG(GATE, EXISTS), \170}171172/* A hardware-or-enabled gate (enabled if not under hardware control) */173#define HW_ENABLE_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \174{ \175.offset = (_offset), \176.status_bit = (_status_bit), \177.en_bit = (_en_bit), \178.hw_sw_sel_bit = (_hw_sw_sel_bit), \179.flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \180FLAG(GATE, NO_DISABLE)|FLAG(GATE, EXISTS), \181}182183/* A software-only gate */184#define SW_ONLY_GATE(_offset, _status_bit, _en_bit) \185{ \186.offset = (_offset), \187.status_bit = (_status_bit), \188.en_bit = (_en_bit), \189.flags = FLAG(GATE, SW)|FLAG(GATE, SW_MANAGED)| \190FLAG(GATE, ENABLED)|FLAG(GATE, EXISTS), \191}192193/* A hardware-only gate */194#define HW_ONLY_GATE(_offset, _status_bit) \195{ \196.offset = (_offset), \197.status_bit = (_status_bit), \198.flags = FLAG(GATE, HW)|FLAG(GATE, EXISTS), \199}200201/* Gate hysteresis for clocks */202struct bcm_clk_hyst {203u32 offset; /* hyst register offset (normally CLKGATE) */204u32 en_bit; /* bit used to enable hysteresis */205u32 val_bit; /* if enabled: 0 = low delay; 1 = high delay */206};207208/* Hysteresis initialization macro */209210#define HYST(_offset, _en_bit, _val_bit) \211{ \212.offset = (_offset), \213.en_bit = (_en_bit), \214.val_bit = (_val_bit), \215}216217/*218* Each clock can have zero, one, or two dividers which change the219* output rate of the clock. Each divider can be either fixed or220* variable. If there are two dividers, they are the "pre-divider"221* and the "regular" or "downstream" divider. If there is only one,222* there is no pre-divider.223*224* A fixed divider is any non-zero (positive) value, and it225* indicates how the input rate is affected by the divider.226*227* The value of a variable divider is maintained in a sub-field of a228* 32-bit divider register. The position of the field in the229* register is defined by its offset and width. The value recorded230* in this field is always 1 less than the value it represents.231*232* In addition, a variable divider can indicate that some subset233* of its bits represent a "fractional" part of the divider. Such234* bits comprise the low-order portion of the divider field, and can235* be viewed as representing the portion of the divider that lies to236* the right of the decimal point. Most variable dividers have zero237* fractional bits. Variable dividers with non-zero fraction width238* still record a value 1 less than the value they represent; the239* added 1 does *not* affect the low-order bit in this case, it240* affects the bits above the fractional part only. (Often in this241* code a divider field value is distinguished from the value it242* represents by referring to the latter as a "divisor".)243*244* In order to avoid dealing with fractions, divider arithmetic is245* performed using "scaled" values. A scaled value is one that's246* been left-shifted by the fractional width of a divider. Dividing247* a scaled value by a scaled divisor produces the desired quotient248* without loss of precision and without any other special handling249* for fractions.250*251* The recorded value of a variable divider can be modified. To252* modify either divider (or both), a clock must be enabled (i.e.,253* using its gate). In addition, a trigger register (described254* below) must be used to commit the change, and polled to verify255* the change is complete.256*/257struct bcm_clk_div {258union {259struct { /* variable divider */260u32 offset; /* divider register offset */261u32 shift; /* field shift */262u32 width; /* field width */263u32 frac_width; /* field fraction width */264265u64 scaled_div; /* scaled divider value */266} s;267u32 fixed; /* non-zero fixed divider value */268} u;269u32 flags; /* BCM_CLK_DIV_FLAGS_* below */270};271272/*273* Divider flags:274* EXISTS means this divider exists275* FIXED means it is a fixed-rate divider276*/277#define BCM_CLK_DIV_FLAGS_EXISTS ((u32)1 << 0) /* Divider is valid */278#define BCM_CLK_DIV_FLAGS_FIXED ((u32)1 << 1) /* Fixed-value */279280/* Divider initialization macros */281282/* A fixed (non-zero) divider */283#define FIXED_DIVIDER(_value) \284{ \285.u.fixed = (_value), \286.flags = FLAG(DIV, EXISTS)|FLAG(DIV, FIXED), \287}288289/* A divider with an integral divisor */290#define DIVIDER(_offset, _shift, _width) \291{ \292.u.s.offset = (_offset), \293.u.s.shift = (_shift), \294.u.s.width = (_width), \295.u.s.scaled_div = BAD_SCALED_DIV_VALUE, \296.flags = FLAG(DIV, EXISTS), \297}298299/* A divider whose divisor has an integer and fractional part */300#define FRAC_DIVIDER(_offset, _shift, _width, _frac_width) \301{ \302.u.s.offset = (_offset), \303.u.s.shift = (_shift), \304.u.s.width = (_width), \305.u.s.frac_width = (_frac_width), \306.u.s.scaled_div = BAD_SCALED_DIV_VALUE, \307.flags = FLAG(DIV, EXISTS), \308}309310/*311* Clocks may have multiple "parent" clocks. If there is more than312* one, a selector must be specified to define which of the parent313* clocks is currently in use. The selected clock is indicated in a314* sub-field of a 32-bit selector register. The range of315* representable selector values typically exceeds the number of316* available parent clocks. Occasionally the reset value of a317* selector field is explicitly set to a (specific) value that does318* not correspond to a defined input clock.319*320* We register all known parent clocks with the common clock code321* using a packed array (i.e., no empty slots) of (parent) clock322* names, and refer to them later using indexes into that array.323* We maintain an array of selector values indexed by common clock324* index values in order to map between these common clock indexes325* and the selector values used by the hardware.326*327* Like dividers, a selector can be modified, but to do so a clock328* must be enabled, and a trigger must be used to commit the change.329*/330struct bcm_clk_sel {331u32 offset; /* selector register offset */332u32 shift; /* field shift */333u32 width; /* field width */334335u32 parent_count; /* number of entries in parent_sel[] */336u32 *parent_sel; /* array of parent selector values */337u8 clk_index; /* current selected index in parent_sel[] */338};339340/* Selector initialization macro */341#define SELECTOR(_offset, _shift, _width) \342{ \343.offset = (_offset), \344.shift = (_shift), \345.width = (_width), \346.clk_index = BAD_CLK_INDEX, \347}348349/*350* Making changes to a variable divider or a selector for a clock351* requires the use of a trigger. A trigger is defined by a single352* bit within a register. To signal a change, a 1 is written into353* that bit. To determine when the change has been completed, that354* trigger bit is polled; the read value will be 1 while the change355* is in progress, and 0 when it is complete.356*357* Occasionally a clock will have more than one trigger. In this358* case, the "pre-trigger" will be used when changing a clock's359* selector and/or its pre-divider.360*/361struct bcm_clk_trig {362u32 offset; /* trigger register offset */363u32 bit; /* trigger bit */364u32 flags; /* BCM_CLK_TRIG_FLAGS_* below */365};366367/*368* Trigger flags:369* EXISTS means this trigger exists370*/371#define BCM_CLK_TRIG_FLAGS_EXISTS ((u32)1 << 0) /* Trigger is valid */372373/* Trigger initialization macro */374#define TRIGGER(_offset, _bit) \375{ \376.offset = (_offset), \377.bit = (_bit), \378.flags = FLAG(TRIG, EXISTS), \379}380381struct peri_clk_data {382struct bcm_clk_policy policy;383struct bcm_clk_gate gate;384struct bcm_clk_hyst hyst;385struct bcm_clk_trig pre_trig;386struct bcm_clk_div pre_div;387struct bcm_clk_trig trig;388struct bcm_clk_div div;389struct bcm_clk_sel sel;390const char *clocks[]; /* must be last; use CLOCKS() to declare */391};392#define CLOCKS(...) { __VA_ARGS__, NULL, }393#define NO_CLOCKS { NULL, } /* Must use of no parent clocks */394395struct kona_clk {396struct clk_hw hw;397struct clk_init_data init_data; /* includes name of this clock */398struct ccu_data *ccu; /* ccu this clock is associated with */399enum bcm_clk_type type;400union {401void *data;402struct peri_clk_data *peri;403} u;404};405#define to_kona_clk(_hw) \406container_of(_hw, struct kona_clk, hw)407408/* Initialization macro for an entry in a CCU's kona_clks[] array. */409#define KONA_CLK(_ccu_name, _clk_name, _type) \410{ \411.init_data = { \412.name = #_clk_name, \413.ops = &kona_ ## _type ## _clk_ops, \414}, \415.ccu = &_ccu_name ## _ccu_data, \416.type = bcm_clk_ ## _type, \417.u.data = &_clk_name ## _data, \418}419#define LAST_KONA_CLK { .type = bcm_clk_none }420421/*422* CCU policy control. To enable software update of the policy423* tables the CCU policy engine must be stopped by setting the424* software update enable bit (LVM_EN). After an update the engine425* is restarted using the GO bit and either the GO_ATL or GO_AC bit.426*/427struct bcm_lvm_en {428u32 offset; /* LVM_EN register offset */429u32 bit; /* POLICY_CONFIG_EN bit in register */430};431432/* Policy enable initialization macro */433#define CCU_LVM_EN(_offset, _bit) \434{ \435.offset = (_offset), \436.bit = (_bit), \437}438439struct bcm_policy_ctl {440u32 offset; /* POLICY_CTL register offset */441u32 go_bit;442u32 atl_bit; /* GO, GO_ATL, and GO_AC bits */443u32 ac_bit;444};445446/* Policy control initialization macro */447#define CCU_POLICY_CTL(_offset, _go_bit, _ac_bit, _atl_bit) \448{ \449.offset = (_offset), \450.go_bit = (_go_bit), \451.ac_bit = (_ac_bit), \452.atl_bit = (_atl_bit), \453}454455struct ccu_policy {456struct bcm_lvm_en enable;457struct bcm_policy_ctl control;458};459460/*461* Each CCU defines a mapped area of memory containing registers462* used to manage clocks implemented by the CCU. Access to memory463* within the CCU's space is serialized by a spinlock. Before any464* (other) address can be written, a special access "password" value465* must be written to its WR_ACCESS register (located at the base466* address of the range). We keep track of the name of each CCU as467* it is set up, and maintain them in a list.468*/469struct ccu_data {470void __iomem *base; /* base of mapped address space */471spinlock_t lock; /* serialization lock */472bool write_enabled; /* write access is currently enabled */473struct ccu_policy policy;474struct device_node *node;475size_t clk_num;476const char *name;477u32 range; /* byte range of address space */478struct kona_clk kona_clks[]; /* must be last */479};480481/* Initialization for common fields in a Kona ccu_data structure */482#define KONA_CCU_COMMON(_prefix, _name, _ccuname) \483.name = #_name "_ccu", \484.lock = __SPIN_LOCK_UNLOCKED(_name ## _ccu_data.lock), \485.clk_num = _prefix ## _ ## _ccuname ## _CCU_CLOCK_COUNT486487/* Exported globals */488489extern struct clk_ops kona_peri_clk_ops;490491/* Externally visible functions */492493extern u64 scaled_div_max(struct bcm_clk_div *div);494495extern void __init kona_dt_ccu_setup(struct ccu_data *ccu,496struct device_node *node);497extern bool __init kona_ccu_init(struct ccu_data *ccu);498499#endif /* _CLK_KONA_H */500501502