162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2013 Broadcom Corporation
462306a36Sopenharmony_ci * Copyright 2013 Linaro Limited
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef _CLK_KONA_H
862306a36Sopenharmony_ci#define _CLK_KONA_H
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/kernel.h>
1162306a36Sopenharmony_ci#include <linux/list.h>
1262306a36Sopenharmony_ci#include <linux/spinlock.h>
1362306a36Sopenharmony_ci#include <linux/slab.h>
1462306a36Sopenharmony_ci#include <linux/device.h>
1562306a36Sopenharmony_ci#include <linux/of.h>
1662306a36Sopenharmony_ci#include <linux/clk-provider.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define	BILLION		1000000000
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/* The common clock framework uses u8 to represent a parent index */
2162306a36Sopenharmony_ci#define PARENT_COUNT_MAX	((u32)U8_MAX)
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define BAD_CLK_INDEX		U8_MAX	/* Can't ever be valid */
2462306a36Sopenharmony_ci#define BAD_CLK_NAME		((const char *)-1)
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define BAD_SCALED_DIV_VALUE	U64_MAX
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/*
2962306a36Sopenharmony_ci * Utility macros for object flag management.  If possible, flags
3062306a36Sopenharmony_ci * should be defined such that 0 is the desired default value.
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_ci#define FLAG(type, flag)		BCM_CLK_ ## type ## _FLAGS_ ## flag
3362306a36Sopenharmony_ci#define FLAG_SET(obj, type, flag)	((obj)->flags |= FLAG(type, flag))
3462306a36Sopenharmony_ci#define FLAG_CLEAR(obj, type, flag)	((obj)->flags &= ~(FLAG(type, flag)))
3562306a36Sopenharmony_ci#define FLAG_FLIP(obj, type, flag)	((obj)->flags ^= FLAG(type, flag))
3662306a36Sopenharmony_ci#define FLAG_TEST(obj, type, flag)	(!!((obj)->flags & FLAG(type, flag)))
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/* CCU field state tests */
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#define ccu_policy_exists(ccu_policy)	((ccu_policy)->enable.offset != 0)
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/* Clock field state tests */
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci#define policy_exists(policy)		((policy)->offset != 0)
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define gate_exists(gate)		FLAG_TEST(gate, GATE, EXISTS)
4762306a36Sopenharmony_ci#define gate_is_enabled(gate)		FLAG_TEST(gate, GATE, ENABLED)
4862306a36Sopenharmony_ci#define gate_is_hw_controllable(gate)	FLAG_TEST(gate, GATE, HW)
4962306a36Sopenharmony_ci#define gate_is_sw_controllable(gate)	FLAG_TEST(gate, GATE, SW)
5062306a36Sopenharmony_ci#define gate_is_sw_managed(gate)	FLAG_TEST(gate, GATE, SW_MANAGED)
5162306a36Sopenharmony_ci#define gate_is_no_disable(gate)	FLAG_TEST(gate, GATE, NO_DISABLE)
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci#define gate_flip_enabled(gate)		FLAG_FLIP(gate, GATE, ENABLED)
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define hyst_exists(hyst)		((hyst)->offset != 0)
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci#define divider_exists(div)		FLAG_TEST(div, DIV, EXISTS)
5862306a36Sopenharmony_ci#define divider_is_fixed(div)		FLAG_TEST(div, DIV, FIXED)
5962306a36Sopenharmony_ci#define divider_has_fraction(div)	(!divider_is_fixed(div) && \
6062306a36Sopenharmony_ci						(div)->u.s.frac_width > 0)
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci#define selector_exists(sel)		((sel)->width != 0)
6362306a36Sopenharmony_ci#define trigger_exists(trig)		FLAG_TEST(trig, TRIG, EXISTS)
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci#define policy_lvm_en_exists(enable)	((enable)->offset != 0)
6662306a36Sopenharmony_ci#define policy_ctl_exists(control)	((control)->offset != 0)
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci/* Clock type, used to tell common block what it's part of */
6962306a36Sopenharmony_cienum bcm_clk_type {
7062306a36Sopenharmony_ci	bcm_clk_none,		/* undefined clock type */
7162306a36Sopenharmony_ci	bcm_clk_bus,
7262306a36Sopenharmony_ci	bcm_clk_core,
7362306a36Sopenharmony_ci	bcm_clk_peri
7462306a36Sopenharmony_ci};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci/*
7762306a36Sopenharmony_ci * CCU policy control for clocks.  Clocks can be enabled or disabled
7862306a36Sopenharmony_ci * based on the CCU policy in effect.  One bit in each policy mask
7962306a36Sopenharmony_ci * register (one per CCU policy) represents whether the clock is
8062306a36Sopenharmony_ci * enabled when that policy is effect or not.  The CCU policy engine
8162306a36Sopenharmony_ci * must be stopped to update these bits, and must be restarted again
8262306a36Sopenharmony_ci * afterward.
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_cistruct bcm_clk_policy {
8562306a36Sopenharmony_ci	u32 offset;		/* first policy mask register offset */
8662306a36Sopenharmony_ci	u32 bit;		/* bit used in all mask registers */
8762306a36Sopenharmony_ci};
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci/* Policy initialization macro */
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci#define POLICY(_offset, _bit)						\
9262306a36Sopenharmony_ci	{								\
9362306a36Sopenharmony_ci		.offset = (_offset),					\
9462306a36Sopenharmony_ci		.bit = (_bit),						\
9562306a36Sopenharmony_ci	}
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci/*
9862306a36Sopenharmony_ci * Gating control and status is managed by a 32-bit gate register.
9962306a36Sopenharmony_ci *
10062306a36Sopenharmony_ci * There are several types of gating available:
10162306a36Sopenharmony_ci * - (no gate)
10262306a36Sopenharmony_ci *     A clock with no gate is assumed to be always enabled.
10362306a36Sopenharmony_ci * - hardware-only gating (auto-gating)
10462306a36Sopenharmony_ci *     Enabling or disabling clocks with this type of gate is
10562306a36Sopenharmony_ci *     managed automatically by the hardware.  Such clocks can be
10662306a36Sopenharmony_ci *     considered by the software to be enabled.  The current status
10762306a36Sopenharmony_ci *     of auto-gated clocks can be read from the gate status bit.
10862306a36Sopenharmony_ci * - software-only gating
10962306a36Sopenharmony_ci *     Auto-gating is not available for this type of clock.
11062306a36Sopenharmony_ci *     Instead, software manages whether it's enabled by setting or
11162306a36Sopenharmony_ci *     clearing the enable bit.  The current gate status of a gate
11262306a36Sopenharmony_ci *     under software control can be read from the gate status bit.
11362306a36Sopenharmony_ci *     To ensure a change to the gating status is complete, the
11462306a36Sopenharmony_ci *     status bit can be polled to verify that the gate has entered
11562306a36Sopenharmony_ci *     the desired state.
11662306a36Sopenharmony_ci * - selectable hardware or software gating
11762306a36Sopenharmony_ci *     Gating for this type of clock can be configured to be either
11862306a36Sopenharmony_ci *     under software or hardware control.  Which type is in use is
11962306a36Sopenharmony_ci *     determined by the hw_sw_sel bit of the gate register.
12062306a36Sopenharmony_ci */
12162306a36Sopenharmony_cistruct bcm_clk_gate {
12262306a36Sopenharmony_ci	u32 offset;		/* gate register offset */
12362306a36Sopenharmony_ci	u32 status_bit;		/* 0: gate is disabled; 0: gatge is enabled */
12462306a36Sopenharmony_ci	u32 en_bit;		/* 0: disable; 1: enable */
12562306a36Sopenharmony_ci	u32 hw_sw_sel_bit;	/* 0: hardware gating; 1: software gating */
12662306a36Sopenharmony_ci	u32 flags;		/* BCM_CLK_GATE_FLAGS_* below */
12762306a36Sopenharmony_ci};
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci/*
13062306a36Sopenharmony_ci * Gate flags:
13162306a36Sopenharmony_ci *   HW         means this gate can be auto-gated
13262306a36Sopenharmony_ci *   SW         means the state of this gate can be software controlled
13362306a36Sopenharmony_ci *   NO_DISABLE means this gate is (only) enabled if under software control
13462306a36Sopenharmony_ci *   SW_MANAGED means the status of this gate is under software control
13562306a36Sopenharmony_ci *   ENABLED    means this software-managed gate is *supposed* to be enabled
13662306a36Sopenharmony_ci */
13762306a36Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_EXISTS	((u32)1 << 0)	/* Gate is valid */
13862306a36Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_HW		((u32)1 << 1)	/* Can auto-gate */
13962306a36Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_SW		((u32)1 << 2)	/* Software control */
14062306a36Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_NO_DISABLE	((u32)1 << 3)	/* HW or enabled */
14162306a36Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_SW_MANAGED	((u32)1 << 4)	/* SW now in control */
14262306a36Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_ENABLED	((u32)1 << 5)	/* If SW_MANAGED */
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/*
14562306a36Sopenharmony_ci * Gate initialization macros.
14662306a36Sopenharmony_ci *
14762306a36Sopenharmony_ci * Any gate initially under software control will be enabled.
14862306a36Sopenharmony_ci */
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci/* A hardware/software gate initially under software control */
15162306a36Sopenharmony_ci#define HW_SW_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
15262306a36Sopenharmony_ci	{								\
15362306a36Sopenharmony_ci		.offset = (_offset),					\
15462306a36Sopenharmony_ci		.status_bit = (_status_bit),				\
15562306a36Sopenharmony_ci		.en_bit = (_en_bit),					\
15662306a36Sopenharmony_ci		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
15762306a36Sopenharmony_ci		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
15862306a36Sopenharmony_ci			FLAG(GATE, SW_MANAGED)|FLAG(GATE, ENABLED)|	\
15962306a36Sopenharmony_ci			FLAG(GATE, EXISTS),				\
16062306a36Sopenharmony_ci	}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci/* A hardware/software gate initially under hardware control */
16362306a36Sopenharmony_ci#define HW_SW_GATE_AUTO(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
16462306a36Sopenharmony_ci	{								\
16562306a36Sopenharmony_ci		.offset = (_offset),					\
16662306a36Sopenharmony_ci		.status_bit = (_status_bit),				\
16762306a36Sopenharmony_ci		.en_bit = (_en_bit),					\
16862306a36Sopenharmony_ci		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
16962306a36Sopenharmony_ci		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
17062306a36Sopenharmony_ci			FLAG(GATE, EXISTS),				\
17162306a36Sopenharmony_ci	}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci/* A hardware-or-enabled gate (enabled if not under hardware control) */
17462306a36Sopenharmony_ci#define HW_ENABLE_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
17562306a36Sopenharmony_ci	{								\
17662306a36Sopenharmony_ci		.offset = (_offset),					\
17762306a36Sopenharmony_ci		.status_bit = (_status_bit),				\
17862306a36Sopenharmony_ci		.en_bit = (_en_bit),					\
17962306a36Sopenharmony_ci		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
18062306a36Sopenharmony_ci		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
18162306a36Sopenharmony_ci			FLAG(GATE, NO_DISABLE)|FLAG(GATE, EXISTS),	\
18262306a36Sopenharmony_ci	}
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci/* A software-only gate */
18562306a36Sopenharmony_ci#define SW_ONLY_GATE(_offset, _status_bit, _en_bit)			\
18662306a36Sopenharmony_ci	{								\
18762306a36Sopenharmony_ci		.offset = (_offset),					\
18862306a36Sopenharmony_ci		.status_bit = (_status_bit),				\
18962306a36Sopenharmony_ci		.en_bit = (_en_bit),					\
19062306a36Sopenharmony_ci		.flags = FLAG(GATE, SW)|FLAG(GATE, SW_MANAGED)|		\
19162306a36Sopenharmony_ci			FLAG(GATE, ENABLED)|FLAG(GATE, EXISTS),		\
19262306a36Sopenharmony_ci	}
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci/* A hardware-only gate */
19562306a36Sopenharmony_ci#define HW_ONLY_GATE(_offset, _status_bit)				\
19662306a36Sopenharmony_ci	{								\
19762306a36Sopenharmony_ci		.offset = (_offset),					\
19862306a36Sopenharmony_ci		.status_bit = (_status_bit),				\
19962306a36Sopenharmony_ci		.flags = FLAG(GATE, HW)|FLAG(GATE, EXISTS),		\
20062306a36Sopenharmony_ci	}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci/* Gate hysteresis for clocks */
20362306a36Sopenharmony_cistruct bcm_clk_hyst {
20462306a36Sopenharmony_ci	u32 offset;		/* hyst register offset (normally CLKGATE) */
20562306a36Sopenharmony_ci	u32 en_bit;		/* bit used to enable hysteresis */
20662306a36Sopenharmony_ci	u32 val_bit;		/* if enabled: 0 = low delay; 1 = high delay */
20762306a36Sopenharmony_ci};
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci/* Hysteresis initialization macro */
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci#define HYST(_offset, _en_bit, _val_bit)				\
21262306a36Sopenharmony_ci	{								\
21362306a36Sopenharmony_ci		.offset = (_offset),					\
21462306a36Sopenharmony_ci		.en_bit = (_en_bit),					\
21562306a36Sopenharmony_ci		.val_bit = (_val_bit),					\
21662306a36Sopenharmony_ci	}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci/*
21962306a36Sopenharmony_ci * Each clock can have zero, one, or two dividers which change the
22062306a36Sopenharmony_ci * output rate of the clock.  Each divider can be either fixed or
22162306a36Sopenharmony_ci * variable.  If there are two dividers, they are the "pre-divider"
22262306a36Sopenharmony_ci * and the "regular" or "downstream" divider.  If there is only one,
22362306a36Sopenharmony_ci * there is no pre-divider.
22462306a36Sopenharmony_ci *
22562306a36Sopenharmony_ci * A fixed divider is any non-zero (positive) value, and it
22662306a36Sopenharmony_ci * indicates how the input rate is affected by the divider.
22762306a36Sopenharmony_ci *
22862306a36Sopenharmony_ci * The value of a variable divider is maintained in a sub-field of a
22962306a36Sopenharmony_ci * 32-bit divider register.  The position of the field in the
23062306a36Sopenharmony_ci * register is defined by its offset and width.  The value recorded
23162306a36Sopenharmony_ci * in this field is always 1 less than the value it represents.
23262306a36Sopenharmony_ci *
23362306a36Sopenharmony_ci * In addition, a variable divider can indicate that some subset
23462306a36Sopenharmony_ci * of its bits represent a "fractional" part of the divider.  Such
23562306a36Sopenharmony_ci * bits comprise the low-order portion of the divider field, and can
23662306a36Sopenharmony_ci * be viewed as representing the portion of the divider that lies to
23762306a36Sopenharmony_ci * the right of the decimal point.  Most variable dividers have zero
23862306a36Sopenharmony_ci * fractional bits.  Variable dividers with non-zero fraction width
23962306a36Sopenharmony_ci * still record a value 1 less than the value they represent; the
24062306a36Sopenharmony_ci * added 1 does *not* affect the low-order bit in this case, it
24162306a36Sopenharmony_ci * affects the bits above the fractional part only.  (Often in this
24262306a36Sopenharmony_ci * code a divider field value is distinguished from the value it
24362306a36Sopenharmony_ci * represents by referring to the latter as a "divisor".)
24462306a36Sopenharmony_ci *
24562306a36Sopenharmony_ci * In order to avoid dealing with fractions, divider arithmetic is
24662306a36Sopenharmony_ci * performed using "scaled" values.  A scaled value is one that's
24762306a36Sopenharmony_ci * been left-shifted by the fractional width of a divider.  Dividing
24862306a36Sopenharmony_ci * a scaled value by a scaled divisor produces the desired quotient
24962306a36Sopenharmony_ci * without loss of precision and without any other special handling
25062306a36Sopenharmony_ci * for fractions.
25162306a36Sopenharmony_ci *
25262306a36Sopenharmony_ci * The recorded value of a variable divider can be modified.  To
25362306a36Sopenharmony_ci * modify either divider (or both), a clock must be enabled (i.e.,
25462306a36Sopenharmony_ci * using its gate).  In addition, a trigger register (described
25562306a36Sopenharmony_ci * below) must be used to commit the change, and polled to verify
25662306a36Sopenharmony_ci * the change is complete.
25762306a36Sopenharmony_ci */
25862306a36Sopenharmony_cistruct bcm_clk_div {
25962306a36Sopenharmony_ci	union {
26062306a36Sopenharmony_ci		struct {	/* variable divider */
26162306a36Sopenharmony_ci			u32 offset;	/* divider register offset */
26262306a36Sopenharmony_ci			u32 shift;	/* field shift */
26362306a36Sopenharmony_ci			u32 width;	/* field width */
26462306a36Sopenharmony_ci			u32 frac_width;	/* field fraction width */
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci			u64 scaled_div;	/* scaled divider value */
26762306a36Sopenharmony_ci		} s;
26862306a36Sopenharmony_ci		u32 fixed;	/* non-zero fixed divider value */
26962306a36Sopenharmony_ci	} u;
27062306a36Sopenharmony_ci	u32 flags;		/* BCM_CLK_DIV_FLAGS_* below */
27162306a36Sopenharmony_ci};
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci/*
27462306a36Sopenharmony_ci * Divider flags:
27562306a36Sopenharmony_ci *   EXISTS means this divider exists
27662306a36Sopenharmony_ci *   FIXED means it is a fixed-rate divider
27762306a36Sopenharmony_ci */
27862306a36Sopenharmony_ci#define BCM_CLK_DIV_FLAGS_EXISTS	((u32)1 << 0)	/* Divider is valid */
27962306a36Sopenharmony_ci#define BCM_CLK_DIV_FLAGS_FIXED		((u32)1 << 1)	/* Fixed-value */
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci/* Divider initialization macros */
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci/* A fixed (non-zero) divider */
28462306a36Sopenharmony_ci#define FIXED_DIVIDER(_value)						\
28562306a36Sopenharmony_ci	{								\
28662306a36Sopenharmony_ci		.u.fixed = (_value),					\
28762306a36Sopenharmony_ci		.flags = FLAG(DIV, EXISTS)|FLAG(DIV, FIXED),		\
28862306a36Sopenharmony_ci	}
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci/* A divider with an integral divisor */
29162306a36Sopenharmony_ci#define DIVIDER(_offset, _shift, _width)				\
29262306a36Sopenharmony_ci	{								\
29362306a36Sopenharmony_ci		.u.s.offset = (_offset),				\
29462306a36Sopenharmony_ci		.u.s.shift = (_shift),					\
29562306a36Sopenharmony_ci		.u.s.width = (_width),					\
29662306a36Sopenharmony_ci		.u.s.scaled_div = BAD_SCALED_DIV_VALUE,			\
29762306a36Sopenharmony_ci		.flags = FLAG(DIV, EXISTS),				\
29862306a36Sopenharmony_ci	}
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci/* A divider whose divisor has an integer and fractional part */
30162306a36Sopenharmony_ci#define FRAC_DIVIDER(_offset, _shift, _width, _frac_width)		\
30262306a36Sopenharmony_ci	{								\
30362306a36Sopenharmony_ci		.u.s.offset = (_offset),				\
30462306a36Sopenharmony_ci		.u.s.shift = (_shift),					\
30562306a36Sopenharmony_ci		.u.s.width = (_width),					\
30662306a36Sopenharmony_ci		.u.s.frac_width = (_frac_width),			\
30762306a36Sopenharmony_ci		.u.s.scaled_div = BAD_SCALED_DIV_VALUE,			\
30862306a36Sopenharmony_ci		.flags = FLAG(DIV, EXISTS),				\
30962306a36Sopenharmony_ci	}
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci/*
31262306a36Sopenharmony_ci * Clocks may have multiple "parent" clocks.  If there is more than
31362306a36Sopenharmony_ci * one, a selector must be specified to define which of the parent
31462306a36Sopenharmony_ci * clocks is currently in use.  The selected clock is indicated in a
31562306a36Sopenharmony_ci * sub-field of a 32-bit selector register.  The range of
31662306a36Sopenharmony_ci * representable selector values typically exceeds the number of
31762306a36Sopenharmony_ci * available parent clocks.  Occasionally the reset value of a
31862306a36Sopenharmony_ci * selector field is explicitly set to a (specific) value that does
31962306a36Sopenharmony_ci * not correspond to a defined input clock.
32062306a36Sopenharmony_ci *
32162306a36Sopenharmony_ci * We register all known parent clocks with the common clock code
32262306a36Sopenharmony_ci * using a packed array (i.e., no empty slots) of (parent) clock
32362306a36Sopenharmony_ci * names, and refer to them later using indexes into that array.
32462306a36Sopenharmony_ci * We maintain an array of selector values indexed by common clock
32562306a36Sopenharmony_ci * index values in order to map between these common clock indexes
32662306a36Sopenharmony_ci * and the selector values used by the hardware.
32762306a36Sopenharmony_ci *
32862306a36Sopenharmony_ci * Like dividers, a selector can be modified, but to do so a clock
32962306a36Sopenharmony_ci * must be enabled, and a trigger must be used to commit the change.
33062306a36Sopenharmony_ci */
33162306a36Sopenharmony_cistruct bcm_clk_sel {
33262306a36Sopenharmony_ci	u32 offset;		/* selector register offset */
33362306a36Sopenharmony_ci	u32 shift;		/* field shift */
33462306a36Sopenharmony_ci	u32 width;		/* field width */
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	u32 parent_count;	/* number of entries in parent_sel[] */
33762306a36Sopenharmony_ci	u32 *parent_sel;	/* array of parent selector values */
33862306a36Sopenharmony_ci	u8 clk_index;		/* current selected index in parent_sel[] */
33962306a36Sopenharmony_ci};
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci/* Selector initialization macro */
34262306a36Sopenharmony_ci#define SELECTOR(_offset, _shift, _width)				\
34362306a36Sopenharmony_ci	{								\
34462306a36Sopenharmony_ci		.offset = (_offset),					\
34562306a36Sopenharmony_ci		.shift = (_shift),					\
34662306a36Sopenharmony_ci		.width = (_width),					\
34762306a36Sopenharmony_ci		.clk_index = BAD_CLK_INDEX,				\
34862306a36Sopenharmony_ci	}
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci/*
35162306a36Sopenharmony_ci * Making changes to a variable divider or a selector for a clock
35262306a36Sopenharmony_ci * requires the use of a trigger.  A trigger is defined by a single
35362306a36Sopenharmony_ci * bit within a register.  To signal a change, a 1 is written into
35462306a36Sopenharmony_ci * that bit.  To determine when the change has been completed, that
35562306a36Sopenharmony_ci * trigger bit is polled; the read value will be 1 while the change
35662306a36Sopenharmony_ci * is in progress, and 0 when it is complete.
35762306a36Sopenharmony_ci *
35862306a36Sopenharmony_ci * Occasionally a clock will have more than one trigger.  In this
35962306a36Sopenharmony_ci * case, the "pre-trigger" will be used when changing a clock's
36062306a36Sopenharmony_ci * selector and/or its pre-divider.
36162306a36Sopenharmony_ci */
36262306a36Sopenharmony_cistruct bcm_clk_trig {
36362306a36Sopenharmony_ci	u32 offset;		/* trigger register offset */
36462306a36Sopenharmony_ci	u32 bit;		/* trigger bit */
36562306a36Sopenharmony_ci	u32 flags;		/* BCM_CLK_TRIG_FLAGS_* below */
36662306a36Sopenharmony_ci};
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci/*
36962306a36Sopenharmony_ci * Trigger flags:
37062306a36Sopenharmony_ci *   EXISTS means this trigger exists
37162306a36Sopenharmony_ci */
37262306a36Sopenharmony_ci#define BCM_CLK_TRIG_FLAGS_EXISTS	((u32)1 << 0)	/* Trigger is valid */
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci/* Trigger initialization macro */
37562306a36Sopenharmony_ci#define TRIGGER(_offset, _bit)						\
37662306a36Sopenharmony_ci	{								\
37762306a36Sopenharmony_ci		.offset = (_offset),					\
37862306a36Sopenharmony_ci		.bit = (_bit),						\
37962306a36Sopenharmony_ci		.flags = FLAG(TRIG, EXISTS),				\
38062306a36Sopenharmony_ci	}
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_cistruct peri_clk_data {
38362306a36Sopenharmony_ci	struct bcm_clk_policy policy;
38462306a36Sopenharmony_ci	struct bcm_clk_gate gate;
38562306a36Sopenharmony_ci	struct bcm_clk_hyst hyst;
38662306a36Sopenharmony_ci	struct bcm_clk_trig pre_trig;
38762306a36Sopenharmony_ci	struct bcm_clk_div pre_div;
38862306a36Sopenharmony_ci	struct bcm_clk_trig trig;
38962306a36Sopenharmony_ci	struct bcm_clk_div div;
39062306a36Sopenharmony_ci	struct bcm_clk_sel sel;
39162306a36Sopenharmony_ci	const char *clocks[];	/* must be last; use CLOCKS() to declare */
39262306a36Sopenharmony_ci};
39362306a36Sopenharmony_ci#define CLOCKS(...)	{ __VA_ARGS__, NULL, }
39462306a36Sopenharmony_ci#define NO_CLOCKS	{ NULL, }	/* Must use of no parent clocks */
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_cistruct kona_clk {
39762306a36Sopenharmony_ci	struct clk_hw hw;
39862306a36Sopenharmony_ci	struct clk_init_data init_data;	/* includes name of this clock */
39962306a36Sopenharmony_ci	struct ccu_data *ccu;	/* ccu this clock is associated with */
40062306a36Sopenharmony_ci	enum bcm_clk_type type;
40162306a36Sopenharmony_ci	union {
40262306a36Sopenharmony_ci		void *data;
40362306a36Sopenharmony_ci		struct peri_clk_data *peri;
40462306a36Sopenharmony_ci	} u;
40562306a36Sopenharmony_ci};
40662306a36Sopenharmony_ci#define to_kona_clk(_hw) \
40762306a36Sopenharmony_ci	container_of(_hw, struct kona_clk, hw)
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci/* Initialization macro for an entry in a CCU's kona_clks[] array. */
41062306a36Sopenharmony_ci#define KONA_CLK(_ccu_name, _clk_name, _type)				\
41162306a36Sopenharmony_ci	{								\
41262306a36Sopenharmony_ci		.init_data	= {					\
41362306a36Sopenharmony_ci			.name = #_clk_name,				\
41462306a36Sopenharmony_ci			.ops = &kona_ ## _type ## _clk_ops,		\
41562306a36Sopenharmony_ci		},							\
41662306a36Sopenharmony_ci		.ccu		= &_ccu_name ## _ccu_data,		\
41762306a36Sopenharmony_ci		.type		= bcm_clk_ ## _type,			\
41862306a36Sopenharmony_ci		.u.data		= &_clk_name ## _data,			\
41962306a36Sopenharmony_ci	}
42062306a36Sopenharmony_ci#define LAST_KONA_CLK	{ .type = bcm_clk_none }
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci/*
42362306a36Sopenharmony_ci * CCU policy control.  To enable software update of the policy
42462306a36Sopenharmony_ci * tables the CCU policy engine must be stopped by setting the
42562306a36Sopenharmony_ci * software update enable bit (LVM_EN).  After an update the engine
42662306a36Sopenharmony_ci * is restarted using the GO bit and either the GO_ATL or GO_AC bit.
42762306a36Sopenharmony_ci */
42862306a36Sopenharmony_cistruct bcm_lvm_en {
42962306a36Sopenharmony_ci	u32 offset;		/* LVM_EN register offset */
43062306a36Sopenharmony_ci	u32 bit;		/* POLICY_CONFIG_EN bit in register */
43162306a36Sopenharmony_ci};
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci/* Policy enable initialization macro */
43462306a36Sopenharmony_ci#define CCU_LVM_EN(_offset, _bit)					\
43562306a36Sopenharmony_ci	{								\
43662306a36Sopenharmony_ci		.offset = (_offset),					\
43762306a36Sopenharmony_ci		.bit = (_bit),						\
43862306a36Sopenharmony_ci	}
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_cistruct bcm_policy_ctl {
44162306a36Sopenharmony_ci	u32 offset;		/* POLICY_CTL register offset */
44262306a36Sopenharmony_ci	u32 go_bit;
44362306a36Sopenharmony_ci	u32 atl_bit;		/* GO, GO_ATL, and GO_AC bits */
44462306a36Sopenharmony_ci	u32 ac_bit;
44562306a36Sopenharmony_ci};
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci/* Policy control initialization macro */
44862306a36Sopenharmony_ci#define CCU_POLICY_CTL(_offset, _go_bit, _ac_bit, _atl_bit)		\
44962306a36Sopenharmony_ci	{								\
45062306a36Sopenharmony_ci		.offset = (_offset),					\
45162306a36Sopenharmony_ci		.go_bit = (_go_bit),					\
45262306a36Sopenharmony_ci		.ac_bit = (_ac_bit),					\
45362306a36Sopenharmony_ci		.atl_bit = (_atl_bit),					\
45462306a36Sopenharmony_ci	}
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_cistruct ccu_policy {
45762306a36Sopenharmony_ci	struct bcm_lvm_en enable;
45862306a36Sopenharmony_ci	struct bcm_policy_ctl control;
45962306a36Sopenharmony_ci};
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci/*
46262306a36Sopenharmony_ci * Each CCU defines a mapped area of memory containing registers
46362306a36Sopenharmony_ci * used to manage clocks implemented by the CCU.  Access to memory
46462306a36Sopenharmony_ci * within the CCU's space is serialized by a spinlock.  Before any
46562306a36Sopenharmony_ci * (other) address can be written, a special access "password" value
46662306a36Sopenharmony_ci * must be written to its WR_ACCESS register (located at the base
46762306a36Sopenharmony_ci * address of the range).  We keep track of the name of each CCU as
46862306a36Sopenharmony_ci * it is set up, and maintain them in a list.
46962306a36Sopenharmony_ci */
47062306a36Sopenharmony_cistruct ccu_data {
47162306a36Sopenharmony_ci	void __iomem *base;	/* base of mapped address space */
47262306a36Sopenharmony_ci	spinlock_t lock;	/* serialization lock */
47362306a36Sopenharmony_ci	bool write_enabled;	/* write access is currently enabled */
47462306a36Sopenharmony_ci	struct ccu_policy policy;
47562306a36Sopenharmony_ci	struct device_node *node;
47662306a36Sopenharmony_ci	size_t clk_num;
47762306a36Sopenharmony_ci	const char *name;
47862306a36Sopenharmony_ci	u32 range;		/* byte range of address space */
47962306a36Sopenharmony_ci	struct kona_clk kona_clks[];	/* must be last */
48062306a36Sopenharmony_ci};
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci/* Initialization for common fields in a Kona ccu_data structure */
48362306a36Sopenharmony_ci#define KONA_CCU_COMMON(_prefix, _name, _ccuname)			    \
48462306a36Sopenharmony_ci	.name		= #_name "_ccu",				    \
48562306a36Sopenharmony_ci	.lock		= __SPIN_LOCK_UNLOCKED(_name ## _ccu_data.lock),    \
48662306a36Sopenharmony_ci	.clk_num	= _prefix ## _ ## _ccuname ## _CCU_CLOCK_COUNT
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci/* Exported globals */
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ciextern struct clk_ops kona_peri_clk_ops;
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci/* Externally visible functions */
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ciextern u64 scaled_div_max(struct bcm_clk_div *div);
49562306a36Sopenharmony_ciextern u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value,
49662306a36Sopenharmony_ci				u32 billionths);
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ciextern void __init kona_dt_ccu_setup(struct ccu_data *ccu,
49962306a36Sopenharmony_ci				struct device_node *node);
50062306a36Sopenharmony_ciextern bool __init kona_ccu_init(struct ccu_data *ccu);
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ci#endif /* _CLK_KONA_H */
503