18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (C) 2013 Broadcom Corporation
38c2ecf20Sopenharmony_ci * Copyright 2013 Linaro Limited
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
68c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License as
78c2ecf20Sopenharmony_ci * published by the Free Software Foundation version 2.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This program is distributed "as is" WITHOUT ANY WARRANTY of any
108c2ecf20Sopenharmony_ci * kind, whether express or implied; without even the implied warranty
118c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
128c2ecf20Sopenharmony_ci * GNU General Public License for more details.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#ifndef _CLK_KONA_H
168c2ecf20Sopenharmony_ci#define _CLK_KONA_H
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/kernel.h>
198c2ecf20Sopenharmony_ci#include <linux/list.h>
208c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
218c2ecf20Sopenharmony_ci#include <linux/slab.h>
228c2ecf20Sopenharmony_ci#include <linux/device.h>
238c2ecf20Sopenharmony_ci#include <linux/of.h>
248c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define	BILLION		1000000000
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* The common clock framework uses u8 to represent a parent index */
298c2ecf20Sopenharmony_ci#define PARENT_COUNT_MAX	((u32)U8_MAX)
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define BAD_CLK_INDEX		U8_MAX	/* Can't ever be valid */
328c2ecf20Sopenharmony_ci#define BAD_CLK_NAME		((const char *)-1)
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define BAD_SCALED_DIV_VALUE	U64_MAX
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * Utility macros for object flag management.  If possible, flags
388c2ecf20Sopenharmony_ci * should be defined such that 0 is the desired default value.
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_ci#define FLAG(type, flag)		BCM_CLK_ ## type ## _FLAGS_ ## flag
418c2ecf20Sopenharmony_ci#define FLAG_SET(obj, type, flag)	((obj)->flags |= FLAG(type, flag))
428c2ecf20Sopenharmony_ci#define FLAG_CLEAR(obj, type, flag)	((obj)->flags &= ~(FLAG(type, flag)))
438c2ecf20Sopenharmony_ci#define FLAG_FLIP(obj, type, flag)	((obj)->flags ^= FLAG(type, flag))
448c2ecf20Sopenharmony_ci#define FLAG_TEST(obj, type, flag)	(!!((obj)->flags & FLAG(type, flag)))
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* CCU field state tests */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define ccu_policy_exists(ccu_policy)	((ccu_policy)->enable.offset != 0)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* Clock field state tests */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define policy_exists(policy)		((policy)->offset != 0)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define gate_exists(gate)		FLAG_TEST(gate, GATE, EXISTS)
558c2ecf20Sopenharmony_ci#define gate_is_enabled(gate)		FLAG_TEST(gate, GATE, ENABLED)
568c2ecf20Sopenharmony_ci#define gate_is_hw_controllable(gate)	FLAG_TEST(gate, GATE, HW)
578c2ecf20Sopenharmony_ci#define gate_is_sw_controllable(gate)	FLAG_TEST(gate, GATE, SW)
588c2ecf20Sopenharmony_ci#define gate_is_sw_managed(gate)	FLAG_TEST(gate, GATE, SW_MANAGED)
598c2ecf20Sopenharmony_ci#define gate_is_no_disable(gate)	FLAG_TEST(gate, GATE, NO_DISABLE)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define gate_flip_enabled(gate)		FLAG_FLIP(gate, GATE, ENABLED)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define hyst_exists(hyst)		((hyst)->offset != 0)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define divider_exists(div)		FLAG_TEST(div, DIV, EXISTS)
668c2ecf20Sopenharmony_ci#define divider_is_fixed(div)		FLAG_TEST(div, DIV, FIXED)
678c2ecf20Sopenharmony_ci#define divider_has_fraction(div)	(!divider_is_fixed(div) && \
688c2ecf20Sopenharmony_ci						(div)->u.s.frac_width > 0)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define selector_exists(sel)		((sel)->width != 0)
718c2ecf20Sopenharmony_ci#define trigger_exists(trig)		FLAG_TEST(trig, TRIG, EXISTS)
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define policy_lvm_en_exists(enable)	((enable)->offset != 0)
748c2ecf20Sopenharmony_ci#define policy_ctl_exists(control)	((control)->offset != 0)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/* Clock type, used to tell common block what it's part of */
778c2ecf20Sopenharmony_cienum bcm_clk_type {
788c2ecf20Sopenharmony_ci	bcm_clk_none,		/* undefined clock type */
798c2ecf20Sopenharmony_ci	bcm_clk_bus,
808c2ecf20Sopenharmony_ci	bcm_clk_core,
818c2ecf20Sopenharmony_ci	bcm_clk_peri
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/*
858c2ecf20Sopenharmony_ci * CCU policy control for clocks.  Clocks can be enabled or disabled
868c2ecf20Sopenharmony_ci * based on the CCU policy in effect.  One bit in each policy mask
878c2ecf20Sopenharmony_ci * register (one per CCU policy) represents whether the clock is
888c2ecf20Sopenharmony_ci * enabled when that policy is effect or not.  The CCU policy engine
898c2ecf20Sopenharmony_ci * must be stopped to update these bits, and must be restarted again
908c2ecf20Sopenharmony_ci * afterward.
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_cistruct bcm_clk_policy {
938c2ecf20Sopenharmony_ci	u32 offset;		/* first policy mask register offset */
948c2ecf20Sopenharmony_ci	u32 bit;		/* bit used in all mask registers */
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/* Policy initialization macro */
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define POLICY(_offset, _bit)						\
1008c2ecf20Sopenharmony_ci	{								\
1018c2ecf20Sopenharmony_ci		.offset = (_offset),					\
1028c2ecf20Sopenharmony_ci		.bit = (_bit),						\
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/*
1068c2ecf20Sopenharmony_ci * Gating control and status is managed by a 32-bit gate register.
1078c2ecf20Sopenharmony_ci *
1088c2ecf20Sopenharmony_ci * There are several types of gating available:
1098c2ecf20Sopenharmony_ci * - (no gate)
1108c2ecf20Sopenharmony_ci *     A clock with no gate is assumed to be always enabled.
1118c2ecf20Sopenharmony_ci * - hardware-only gating (auto-gating)
1128c2ecf20Sopenharmony_ci *     Enabling or disabling clocks with this type of gate is
1138c2ecf20Sopenharmony_ci *     managed automatically by the hardware.  Such clocks can be
1148c2ecf20Sopenharmony_ci *     considered by the software to be enabled.  The current status
1158c2ecf20Sopenharmony_ci *     of auto-gated clocks can be read from the gate status bit.
1168c2ecf20Sopenharmony_ci * - software-only gating
1178c2ecf20Sopenharmony_ci *     Auto-gating is not available for this type of clock.
1188c2ecf20Sopenharmony_ci *     Instead, software manages whether it's enabled by setting or
1198c2ecf20Sopenharmony_ci *     clearing the enable bit.  The current gate status of a gate
1208c2ecf20Sopenharmony_ci *     under software control can be read from the gate status bit.
1218c2ecf20Sopenharmony_ci *     To ensure a change to the gating status is complete, the
1228c2ecf20Sopenharmony_ci *     status bit can be polled to verify that the gate has entered
1238c2ecf20Sopenharmony_ci *     the desired state.
1248c2ecf20Sopenharmony_ci * - selectable hardware or software gating
1258c2ecf20Sopenharmony_ci *     Gating for this type of clock can be configured to be either
1268c2ecf20Sopenharmony_ci *     under software or hardware control.  Which type is in use is
1278c2ecf20Sopenharmony_ci *     determined by the hw_sw_sel bit of the gate register.
1288c2ecf20Sopenharmony_ci */
1298c2ecf20Sopenharmony_cistruct bcm_clk_gate {
1308c2ecf20Sopenharmony_ci	u32 offset;		/* gate register offset */
1318c2ecf20Sopenharmony_ci	u32 status_bit;		/* 0: gate is disabled; 0: gatge is enabled */
1328c2ecf20Sopenharmony_ci	u32 en_bit;		/* 0: disable; 1: enable */
1338c2ecf20Sopenharmony_ci	u32 hw_sw_sel_bit;	/* 0: hardware gating; 1: software gating */
1348c2ecf20Sopenharmony_ci	u32 flags;		/* BCM_CLK_GATE_FLAGS_* below */
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci/*
1388c2ecf20Sopenharmony_ci * Gate flags:
1398c2ecf20Sopenharmony_ci *   HW         means this gate can be auto-gated
1408c2ecf20Sopenharmony_ci *   SW         means the state of this gate can be software controlled
1418c2ecf20Sopenharmony_ci *   NO_DISABLE means this gate is (only) enabled if under software control
1428c2ecf20Sopenharmony_ci *   SW_MANAGED means the status of this gate is under software control
1438c2ecf20Sopenharmony_ci *   ENABLED    means this software-managed gate is *supposed* to be enabled
1448c2ecf20Sopenharmony_ci */
1458c2ecf20Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_EXISTS	((u32)1 << 0)	/* Gate is valid */
1468c2ecf20Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_HW		((u32)1 << 1)	/* Can auto-gate */
1478c2ecf20Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_SW		((u32)1 << 2)	/* Software control */
1488c2ecf20Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_NO_DISABLE	((u32)1 << 3)	/* HW or enabled */
1498c2ecf20Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_SW_MANAGED	((u32)1 << 4)	/* SW now in control */
1508c2ecf20Sopenharmony_ci#define BCM_CLK_GATE_FLAGS_ENABLED	((u32)1 << 5)	/* If SW_MANAGED */
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/*
1538c2ecf20Sopenharmony_ci * Gate initialization macros.
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci * Any gate initially under software control will be enabled.
1568c2ecf20Sopenharmony_ci */
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/* A hardware/software gate initially under software control */
1598c2ecf20Sopenharmony_ci#define HW_SW_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
1608c2ecf20Sopenharmony_ci	{								\
1618c2ecf20Sopenharmony_ci		.offset = (_offset),					\
1628c2ecf20Sopenharmony_ci		.status_bit = (_status_bit),				\
1638c2ecf20Sopenharmony_ci		.en_bit = (_en_bit),					\
1648c2ecf20Sopenharmony_ci		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
1658c2ecf20Sopenharmony_ci		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
1668c2ecf20Sopenharmony_ci			FLAG(GATE, SW_MANAGED)|FLAG(GATE, ENABLED)|	\
1678c2ecf20Sopenharmony_ci			FLAG(GATE, EXISTS),				\
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci/* A hardware/software gate initially under hardware control */
1718c2ecf20Sopenharmony_ci#define HW_SW_GATE_AUTO(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
1728c2ecf20Sopenharmony_ci	{								\
1738c2ecf20Sopenharmony_ci		.offset = (_offset),					\
1748c2ecf20Sopenharmony_ci		.status_bit = (_status_bit),				\
1758c2ecf20Sopenharmony_ci		.en_bit = (_en_bit),					\
1768c2ecf20Sopenharmony_ci		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
1778c2ecf20Sopenharmony_ci		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
1788c2ecf20Sopenharmony_ci			FLAG(GATE, EXISTS),				\
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/* A hardware-or-enabled gate (enabled if not under hardware control) */
1828c2ecf20Sopenharmony_ci#define HW_ENABLE_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
1838c2ecf20Sopenharmony_ci	{								\
1848c2ecf20Sopenharmony_ci		.offset = (_offset),					\
1858c2ecf20Sopenharmony_ci		.status_bit = (_status_bit),				\
1868c2ecf20Sopenharmony_ci		.en_bit = (_en_bit),					\
1878c2ecf20Sopenharmony_ci		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
1888c2ecf20Sopenharmony_ci		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
1898c2ecf20Sopenharmony_ci			FLAG(GATE, NO_DISABLE)|FLAG(GATE, EXISTS),	\
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/* A software-only gate */
1938c2ecf20Sopenharmony_ci#define SW_ONLY_GATE(_offset, _status_bit, _en_bit)			\
1948c2ecf20Sopenharmony_ci	{								\
1958c2ecf20Sopenharmony_ci		.offset = (_offset),					\
1968c2ecf20Sopenharmony_ci		.status_bit = (_status_bit),				\
1978c2ecf20Sopenharmony_ci		.en_bit = (_en_bit),					\
1988c2ecf20Sopenharmony_ci		.flags = FLAG(GATE, SW)|FLAG(GATE, SW_MANAGED)|		\
1998c2ecf20Sopenharmony_ci			FLAG(GATE, ENABLED)|FLAG(GATE, EXISTS),		\
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci/* A hardware-only gate */
2038c2ecf20Sopenharmony_ci#define HW_ONLY_GATE(_offset, _status_bit)				\
2048c2ecf20Sopenharmony_ci	{								\
2058c2ecf20Sopenharmony_ci		.offset = (_offset),					\
2068c2ecf20Sopenharmony_ci		.status_bit = (_status_bit),				\
2078c2ecf20Sopenharmony_ci		.flags = FLAG(GATE, HW)|FLAG(GATE, EXISTS),		\
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci/* Gate hysteresis for clocks */
2118c2ecf20Sopenharmony_cistruct bcm_clk_hyst {
2128c2ecf20Sopenharmony_ci	u32 offset;		/* hyst register offset (normally CLKGATE) */
2138c2ecf20Sopenharmony_ci	u32 en_bit;		/* bit used to enable hysteresis */
2148c2ecf20Sopenharmony_ci	u32 val_bit;		/* if enabled: 0 = low delay; 1 = high delay */
2158c2ecf20Sopenharmony_ci};
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/* Hysteresis initialization macro */
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci#define HYST(_offset, _en_bit, _val_bit)				\
2208c2ecf20Sopenharmony_ci	{								\
2218c2ecf20Sopenharmony_ci		.offset = (_offset),					\
2228c2ecf20Sopenharmony_ci		.en_bit = (_en_bit),					\
2238c2ecf20Sopenharmony_ci		.val_bit = (_val_bit),					\
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/*
2278c2ecf20Sopenharmony_ci * Each clock can have zero, one, or two dividers which change the
2288c2ecf20Sopenharmony_ci * output rate of the clock.  Each divider can be either fixed or
2298c2ecf20Sopenharmony_ci * variable.  If there are two dividers, they are the "pre-divider"
2308c2ecf20Sopenharmony_ci * and the "regular" or "downstream" divider.  If there is only one,
2318c2ecf20Sopenharmony_ci * there is no pre-divider.
2328c2ecf20Sopenharmony_ci *
2338c2ecf20Sopenharmony_ci * A fixed divider is any non-zero (positive) value, and it
2348c2ecf20Sopenharmony_ci * indicates how the input rate is affected by the divider.
2358c2ecf20Sopenharmony_ci *
2368c2ecf20Sopenharmony_ci * The value of a variable divider is maintained in a sub-field of a
2378c2ecf20Sopenharmony_ci * 32-bit divider register.  The position of the field in the
2388c2ecf20Sopenharmony_ci * register is defined by its offset and width.  The value recorded
2398c2ecf20Sopenharmony_ci * in this field is always 1 less than the value it represents.
2408c2ecf20Sopenharmony_ci *
2418c2ecf20Sopenharmony_ci * In addition, a variable divider can indicate that some subset
2428c2ecf20Sopenharmony_ci * of its bits represent a "fractional" part of the divider.  Such
2438c2ecf20Sopenharmony_ci * bits comprise the low-order portion of the divider field, and can
2448c2ecf20Sopenharmony_ci * be viewed as representing the portion of the divider that lies to
2458c2ecf20Sopenharmony_ci * the right of the decimal point.  Most variable dividers have zero
2468c2ecf20Sopenharmony_ci * fractional bits.  Variable dividers with non-zero fraction width
2478c2ecf20Sopenharmony_ci * still record a value 1 less than the value they represent; the
2488c2ecf20Sopenharmony_ci * added 1 does *not* affect the low-order bit in this case, it
2498c2ecf20Sopenharmony_ci * affects the bits above the fractional part only.  (Often in this
2508c2ecf20Sopenharmony_ci * code a divider field value is distinguished from the value it
2518c2ecf20Sopenharmony_ci * represents by referring to the latter as a "divisor".)
2528c2ecf20Sopenharmony_ci *
2538c2ecf20Sopenharmony_ci * In order to avoid dealing with fractions, divider arithmetic is
2548c2ecf20Sopenharmony_ci * performed using "scaled" values.  A scaled value is one that's
2558c2ecf20Sopenharmony_ci * been left-shifted by the fractional width of a divider.  Dividing
2568c2ecf20Sopenharmony_ci * a scaled value by a scaled divisor produces the desired quotient
2578c2ecf20Sopenharmony_ci * without loss of precision and without any other special handling
2588c2ecf20Sopenharmony_ci * for fractions.
2598c2ecf20Sopenharmony_ci *
2608c2ecf20Sopenharmony_ci * The recorded value of a variable divider can be modified.  To
2618c2ecf20Sopenharmony_ci * modify either divider (or both), a clock must be enabled (i.e.,
2628c2ecf20Sopenharmony_ci * using its gate).  In addition, a trigger register (described
2638c2ecf20Sopenharmony_ci * below) must be used to commit the change, and polled to verify
2648c2ecf20Sopenharmony_ci * the change is complete.
2658c2ecf20Sopenharmony_ci */
2668c2ecf20Sopenharmony_cistruct bcm_clk_div {
2678c2ecf20Sopenharmony_ci	union {
2688c2ecf20Sopenharmony_ci		struct {	/* variable divider */
2698c2ecf20Sopenharmony_ci			u32 offset;	/* divider register offset */
2708c2ecf20Sopenharmony_ci			u32 shift;	/* field shift */
2718c2ecf20Sopenharmony_ci			u32 width;	/* field width */
2728c2ecf20Sopenharmony_ci			u32 frac_width;	/* field fraction width */
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci			u64 scaled_div;	/* scaled divider value */
2758c2ecf20Sopenharmony_ci		} s;
2768c2ecf20Sopenharmony_ci		u32 fixed;	/* non-zero fixed divider value */
2778c2ecf20Sopenharmony_ci	} u;
2788c2ecf20Sopenharmony_ci	u32 flags;		/* BCM_CLK_DIV_FLAGS_* below */
2798c2ecf20Sopenharmony_ci};
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci/*
2828c2ecf20Sopenharmony_ci * Divider flags:
2838c2ecf20Sopenharmony_ci *   EXISTS means this divider exists
2848c2ecf20Sopenharmony_ci *   FIXED means it is a fixed-rate divider
2858c2ecf20Sopenharmony_ci */
2868c2ecf20Sopenharmony_ci#define BCM_CLK_DIV_FLAGS_EXISTS	((u32)1 << 0)	/* Divider is valid */
2878c2ecf20Sopenharmony_ci#define BCM_CLK_DIV_FLAGS_FIXED		((u32)1 << 1)	/* Fixed-value */
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci/* Divider initialization macros */
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci/* A fixed (non-zero) divider */
2928c2ecf20Sopenharmony_ci#define FIXED_DIVIDER(_value)						\
2938c2ecf20Sopenharmony_ci	{								\
2948c2ecf20Sopenharmony_ci		.u.fixed = (_value),					\
2958c2ecf20Sopenharmony_ci		.flags = FLAG(DIV, EXISTS)|FLAG(DIV, FIXED),		\
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci/* A divider with an integral divisor */
2998c2ecf20Sopenharmony_ci#define DIVIDER(_offset, _shift, _width)				\
3008c2ecf20Sopenharmony_ci	{								\
3018c2ecf20Sopenharmony_ci		.u.s.offset = (_offset),				\
3028c2ecf20Sopenharmony_ci		.u.s.shift = (_shift),					\
3038c2ecf20Sopenharmony_ci		.u.s.width = (_width),					\
3048c2ecf20Sopenharmony_ci		.u.s.scaled_div = BAD_SCALED_DIV_VALUE,			\
3058c2ecf20Sopenharmony_ci		.flags = FLAG(DIV, EXISTS),				\
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci/* A divider whose divisor has an integer and fractional part */
3098c2ecf20Sopenharmony_ci#define FRAC_DIVIDER(_offset, _shift, _width, _frac_width)		\
3108c2ecf20Sopenharmony_ci	{								\
3118c2ecf20Sopenharmony_ci		.u.s.offset = (_offset),				\
3128c2ecf20Sopenharmony_ci		.u.s.shift = (_shift),					\
3138c2ecf20Sopenharmony_ci		.u.s.width = (_width),					\
3148c2ecf20Sopenharmony_ci		.u.s.frac_width = (_frac_width),			\
3158c2ecf20Sopenharmony_ci		.u.s.scaled_div = BAD_SCALED_DIV_VALUE,			\
3168c2ecf20Sopenharmony_ci		.flags = FLAG(DIV, EXISTS),				\
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci/*
3208c2ecf20Sopenharmony_ci * Clocks may have multiple "parent" clocks.  If there is more than
3218c2ecf20Sopenharmony_ci * one, a selector must be specified to define which of the parent
3228c2ecf20Sopenharmony_ci * clocks is currently in use.  The selected clock is indicated in a
3238c2ecf20Sopenharmony_ci * sub-field of a 32-bit selector register.  The range of
3248c2ecf20Sopenharmony_ci * representable selector values typically exceeds the number of
3258c2ecf20Sopenharmony_ci * available parent clocks.  Occasionally the reset value of a
3268c2ecf20Sopenharmony_ci * selector field is explicitly set to a (specific) value that does
3278c2ecf20Sopenharmony_ci * not correspond to a defined input clock.
3288c2ecf20Sopenharmony_ci *
3298c2ecf20Sopenharmony_ci * We register all known parent clocks with the common clock code
3308c2ecf20Sopenharmony_ci * using a packed array (i.e., no empty slots) of (parent) clock
3318c2ecf20Sopenharmony_ci * names, and refer to them later using indexes into that array.
3328c2ecf20Sopenharmony_ci * We maintain an array of selector values indexed by common clock
3338c2ecf20Sopenharmony_ci * index values in order to map between these common clock indexes
3348c2ecf20Sopenharmony_ci * and the selector values used by the hardware.
3358c2ecf20Sopenharmony_ci *
3368c2ecf20Sopenharmony_ci * Like dividers, a selector can be modified, but to do so a clock
3378c2ecf20Sopenharmony_ci * must be enabled, and a trigger must be used to commit the change.
3388c2ecf20Sopenharmony_ci */
3398c2ecf20Sopenharmony_cistruct bcm_clk_sel {
3408c2ecf20Sopenharmony_ci	u32 offset;		/* selector register offset */
3418c2ecf20Sopenharmony_ci	u32 shift;		/* field shift */
3428c2ecf20Sopenharmony_ci	u32 width;		/* field width */
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	u32 parent_count;	/* number of entries in parent_sel[] */
3458c2ecf20Sopenharmony_ci	u32 *parent_sel;	/* array of parent selector values */
3468c2ecf20Sopenharmony_ci	u8 clk_index;		/* current selected index in parent_sel[] */
3478c2ecf20Sopenharmony_ci};
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci/* Selector initialization macro */
3508c2ecf20Sopenharmony_ci#define SELECTOR(_offset, _shift, _width)				\
3518c2ecf20Sopenharmony_ci	{								\
3528c2ecf20Sopenharmony_ci		.offset = (_offset),					\
3538c2ecf20Sopenharmony_ci		.shift = (_shift),					\
3548c2ecf20Sopenharmony_ci		.width = (_width),					\
3558c2ecf20Sopenharmony_ci		.clk_index = BAD_CLK_INDEX,				\
3568c2ecf20Sopenharmony_ci	}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci/*
3598c2ecf20Sopenharmony_ci * Making changes to a variable divider or a selector for a clock
3608c2ecf20Sopenharmony_ci * requires the use of a trigger.  A trigger is defined by a single
3618c2ecf20Sopenharmony_ci * bit within a register.  To signal a change, a 1 is written into
3628c2ecf20Sopenharmony_ci * that bit.  To determine when the change has been completed, that
3638c2ecf20Sopenharmony_ci * trigger bit is polled; the read value will be 1 while the change
3648c2ecf20Sopenharmony_ci * is in progress, and 0 when it is complete.
3658c2ecf20Sopenharmony_ci *
3668c2ecf20Sopenharmony_ci * Occasionally a clock will have more than one trigger.  In this
3678c2ecf20Sopenharmony_ci * case, the "pre-trigger" will be used when changing a clock's
3688c2ecf20Sopenharmony_ci * selector and/or its pre-divider.
3698c2ecf20Sopenharmony_ci */
3708c2ecf20Sopenharmony_cistruct bcm_clk_trig {
3718c2ecf20Sopenharmony_ci	u32 offset;		/* trigger register offset */
3728c2ecf20Sopenharmony_ci	u32 bit;		/* trigger bit */
3738c2ecf20Sopenharmony_ci	u32 flags;		/* BCM_CLK_TRIG_FLAGS_* below */
3748c2ecf20Sopenharmony_ci};
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci/*
3778c2ecf20Sopenharmony_ci * Trigger flags:
3788c2ecf20Sopenharmony_ci *   EXISTS means this trigger exists
3798c2ecf20Sopenharmony_ci */
3808c2ecf20Sopenharmony_ci#define BCM_CLK_TRIG_FLAGS_EXISTS	((u32)1 << 0)	/* Trigger is valid */
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci/* Trigger initialization macro */
3838c2ecf20Sopenharmony_ci#define TRIGGER(_offset, _bit)						\
3848c2ecf20Sopenharmony_ci	{								\
3858c2ecf20Sopenharmony_ci		.offset = (_offset),					\
3868c2ecf20Sopenharmony_ci		.bit = (_bit),						\
3878c2ecf20Sopenharmony_ci		.flags = FLAG(TRIG, EXISTS),				\
3888c2ecf20Sopenharmony_ci	}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_cistruct peri_clk_data {
3918c2ecf20Sopenharmony_ci	struct bcm_clk_policy policy;
3928c2ecf20Sopenharmony_ci	struct bcm_clk_gate gate;
3938c2ecf20Sopenharmony_ci	struct bcm_clk_hyst hyst;
3948c2ecf20Sopenharmony_ci	struct bcm_clk_trig pre_trig;
3958c2ecf20Sopenharmony_ci	struct bcm_clk_div pre_div;
3968c2ecf20Sopenharmony_ci	struct bcm_clk_trig trig;
3978c2ecf20Sopenharmony_ci	struct bcm_clk_div div;
3988c2ecf20Sopenharmony_ci	struct bcm_clk_sel sel;
3998c2ecf20Sopenharmony_ci	const char *clocks[];	/* must be last; use CLOCKS() to declare */
4008c2ecf20Sopenharmony_ci};
4018c2ecf20Sopenharmony_ci#define CLOCKS(...)	{ __VA_ARGS__, NULL, }
4028c2ecf20Sopenharmony_ci#define NO_CLOCKS	{ NULL, }	/* Must use of no parent clocks */
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistruct kona_clk {
4058c2ecf20Sopenharmony_ci	struct clk_hw hw;
4068c2ecf20Sopenharmony_ci	struct clk_init_data init_data;	/* includes name of this clock */
4078c2ecf20Sopenharmony_ci	struct ccu_data *ccu;	/* ccu this clock is associated with */
4088c2ecf20Sopenharmony_ci	enum bcm_clk_type type;
4098c2ecf20Sopenharmony_ci	union {
4108c2ecf20Sopenharmony_ci		void *data;
4118c2ecf20Sopenharmony_ci		struct peri_clk_data *peri;
4128c2ecf20Sopenharmony_ci	} u;
4138c2ecf20Sopenharmony_ci};
4148c2ecf20Sopenharmony_ci#define to_kona_clk(_hw) \
4158c2ecf20Sopenharmony_ci	container_of(_hw, struct kona_clk, hw)
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci/* Initialization macro for an entry in a CCU's kona_clks[] array. */
4188c2ecf20Sopenharmony_ci#define KONA_CLK(_ccu_name, _clk_name, _type)				\
4198c2ecf20Sopenharmony_ci	{								\
4208c2ecf20Sopenharmony_ci		.init_data	= {					\
4218c2ecf20Sopenharmony_ci			.name = #_clk_name,				\
4228c2ecf20Sopenharmony_ci			.ops = &kona_ ## _type ## _clk_ops,		\
4238c2ecf20Sopenharmony_ci		},							\
4248c2ecf20Sopenharmony_ci		.ccu		= &_ccu_name ## _ccu_data,		\
4258c2ecf20Sopenharmony_ci		.type		= bcm_clk_ ## _type,			\
4268c2ecf20Sopenharmony_ci		.u.data		= &_clk_name ## _data,			\
4278c2ecf20Sopenharmony_ci	}
4288c2ecf20Sopenharmony_ci#define LAST_KONA_CLK	{ .type = bcm_clk_none }
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci/*
4318c2ecf20Sopenharmony_ci * CCU policy control.  To enable software update of the policy
4328c2ecf20Sopenharmony_ci * tables the CCU policy engine must be stopped by setting the
4338c2ecf20Sopenharmony_ci * software update enable bit (LVM_EN).  After an update the engine
4348c2ecf20Sopenharmony_ci * is restarted using the GO bit and either the GO_ATL or GO_AC bit.
4358c2ecf20Sopenharmony_ci */
4368c2ecf20Sopenharmony_cistruct bcm_lvm_en {
4378c2ecf20Sopenharmony_ci	u32 offset;		/* LVM_EN register offset */
4388c2ecf20Sopenharmony_ci	u32 bit;		/* POLICY_CONFIG_EN bit in register */
4398c2ecf20Sopenharmony_ci};
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci/* Policy enable initialization macro */
4428c2ecf20Sopenharmony_ci#define CCU_LVM_EN(_offset, _bit)					\
4438c2ecf20Sopenharmony_ci	{								\
4448c2ecf20Sopenharmony_ci		.offset = (_offset),					\
4458c2ecf20Sopenharmony_ci		.bit = (_bit),						\
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cistruct bcm_policy_ctl {
4498c2ecf20Sopenharmony_ci	u32 offset;		/* POLICY_CTL register offset */
4508c2ecf20Sopenharmony_ci	u32 go_bit;
4518c2ecf20Sopenharmony_ci	u32 atl_bit;		/* GO, GO_ATL, and GO_AC bits */
4528c2ecf20Sopenharmony_ci	u32 ac_bit;
4538c2ecf20Sopenharmony_ci};
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci/* Policy control initialization macro */
4568c2ecf20Sopenharmony_ci#define CCU_POLICY_CTL(_offset, _go_bit, _ac_bit, _atl_bit)		\
4578c2ecf20Sopenharmony_ci	{								\
4588c2ecf20Sopenharmony_ci		.offset = (_offset),					\
4598c2ecf20Sopenharmony_ci		.go_bit = (_go_bit),					\
4608c2ecf20Sopenharmony_ci		.ac_bit = (_ac_bit),					\
4618c2ecf20Sopenharmony_ci		.atl_bit = (_atl_bit),					\
4628c2ecf20Sopenharmony_ci	}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistruct ccu_policy {
4658c2ecf20Sopenharmony_ci	struct bcm_lvm_en enable;
4668c2ecf20Sopenharmony_ci	struct bcm_policy_ctl control;
4678c2ecf20Sopenharmony_ci};
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci/*
4708c2ecf20Sopenharmony_ci * Each CCU defines a mapped area of memory containing registers
4718c2ecf20Sopenharmony_ci * used to manage clocks implemented by the CCU.  Access to memory
4728c2ecf20Sopenharmony_ci * within the CCU's space is serialized by a spinlock.  Before any
4738c2ecf20Sopenharmony_ci * (other) address can be written, a special access "password" value
4748c2ecf20Sopenharmony_ci * must be written to its WR_ACCESS register (located at the base
4758c2ecf20Sopenharmony_ci * address of the range).  We keep track of the name of each CCU as
4768c2ecf20Sopenharmony_ci * it is set up, and maintain them in a list.
4778c2ecf20Sopenharmony_ci */
4788c2ecf20Sopenharmony_cistruct ccu_data {
4798c2ecf20Sopenharmony_ci	void __iomem *base;	/* base of mapped address space */
4808c2ecf20Sopenharmony_ci	spinlock_t lock;	/* serialization lock */
4818c2ecf20Sopenharmony_ci	bool write_enabled;	/* write access is currently enabled */
4828c2ecf20Sopenharmony_ci	struct ccu_policy policy;
4838c2ecf20Sopenharmony_ci	struct device_node *node;
4848c2ecf20Sopenharmony_ci	size_t clk_num;
4858c2ecf20Sopenharmony_ci	const char *name;
4868c2ecf20Sopenharmony_ci	u32 range;		/* byte range of address space */
4878c2ecf20Sopenharmony_ci	struct kona_clk kona_clks[];	/* must be last */
4888c2ecf20Sopenharmony_ci};
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci/* Initialization for common fields in a Kona ccu_data structure */
4918c2ecf20Sopenharmony_ci#define KONA_CCU_COMMON(_prefix, _name, _ccuname)			    \
4928c2ecf20Sopenharmony_ci	.name		= #_name "_ccu",				    \
4938c2ecf20Sopenharmony_ci	.lock		= __SPIN_LOCK_UNLOCKED(_name ## _ccu_data.lock),    \
4948c2ecf20Sopenharmony_ci	.clk_num	= _prefix ## _ ## _ccuname ## _CCU_CLOCK_COUNT
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci/* Exported globals */
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ciextern struct clk_ops kona_peri_clk_ops;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci/* Externally visible functions */
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ciextern u64 scaled_div_max(struct bcm_clk_div *div);
5038c2ecf20Sopenharmony_ciextern u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value,
5048c2ecf20Sopenharmony_ci				u32 billionths);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ciextern void __init kona_dt_ccu_setup(struct ccu_data *ccu,
5078c2ecf20Sopenharmony_ci				struct device_node *node);
5088c2ecf20Sopenharmony_ciextern bool __init kona_ccu_init(struct ccu_data *ccu);
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci#endif /* _CLK_KONA_H */
511