/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright (c) 2019 Rockchip Electronics Co. Ltd.
 * Author: Finley Xiao <finley.xiao@rock-chips.com>
 */

#ifndef _ASM_ARCH_CRU_RV1126_H
#define _ASM_ARCH_CRU_RV1126_H

#define MHz		1000000
#define KHz		1000
#define OSC_HZ		(24 * MHz)

#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_KERNEL_BOOT)
#define APLL_HZ		(1008 * MHz)
#else
#define APLL_HZ		(816 * MHz)
#endif
#define GPLL_HZ		(1188 * MHz)
#define CPLL_HZ		(500 * MHz)
#define HPLL_HZ		(1400 * MHz)
#define PCLK_PDPMU_HZ	(100 * MHz)
#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_KERNEL_BOOT)
#define ACLK_PDBUS_HZ	(396 * MHz)
#else
#define ACLK_PDBUS_HZ	(500 * MHz)
#endif
#define HCLK_PDBUS_HZ	(200 * MHz)
#define PCLK_PDBUS_HZ	(100 * MHz)
#define ACLK_PDPHP_HZ	(300 * MHz)
#define HCLK_PDPHP_HZ	(200 * MHz)
#define HCLK_PDCORE_HZ	(200 * MHz)
#define HCLK_PDAUDIO_HZ	(150 * MHz)
#define CLK_OSC0_DIV_HZ	(32768)
#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_KERNEL_BOOT)
#define ACLK_PDVI_HZ	(297 * MHz)
#define CLK_ISP_HZ	(297 * MHz)
#define ACLK_PDISPP_HZ	(297 * MHz)
#define CLK_ISPP_HZ	(237 * MHz)
#define ACLK_VOP_HZ	(300 * MHz)
#define DCLK_VOP_HZ	(65 * MHz)
#endif

/* RV1126 pll id */
enum rv1126_pll_id {
	APLL,
	DPLL,
	CPLL,
	HPLL,
	GPLL,
	PLL_COUNT,
};

struct rv1126_clk_info {
	unsigned long id;
	char *name;
	bool is_cru;
};

/* Private data for the clock driver - used by rockchip_get_cru() */
struct rv1126_pmuclk_priv {
	struct rv1126_pmucru *pmucru;
	ulong gpll_hz;
};

struct rv1126_clk_priv {
	struct rv1126_cru *cru;
	struct rv1126_grf *grf;
	ulong gpll_hz;
	ulong cpll_hz;
	ulong hpll_hz;
	ulong armclk_hz;
	ulong armclk_enter_hz;
	ulong armclk_init_hz;
	bool sync_kernel;
	bool set_armclk_rate;
};

struct rv1126_pll {
	unsigned int con0;
	unsigned int con1;
	unsigned int con2;
	unsigned int con3;
	unsigned int con4;
	unsigned int con5;
	unsigned int con6;
	unsigned int reserved0[1];
};

struct rv1126_pmucru {
	unsigned int pmu_mode;
	unsigned int reserved1[3];
	struct rv1126_pll pll;
	unsigned int offsetcal_status;
	unsigned int reserved2[51];
	unsigned int pmu_clksel_con[14];
	unsigned int reserved3[18];
	unsigned int pmu_clkgate_con[3];
	unsigned int reserved4[29];
	unsigned int pmu_softrst_con[2];
	unsigned int reserved5[14];
	unsigned int pmu_autocs_con[2];
};

check_member(rv1126_pmucru, pmu_autocs_con[1], 0x244);

struct rv1126_cru {
	struct rv1126_pll pll[4];
	unsigned int offsetcal_status[4];
	unsigned int mode;
	unsigned int reserved1[27];
	unsigned int clksel_con[78];
	unsigned int reserved2[18];
	unsigned int clkgate_con[25];
	unsigned int reserved3[7];
	unsigned int softrst_con[15];
	unsigned int reserved4[17];
	unsigned int ssgtbl[32];
	unsigned int glb_cnt_th;
	unsigned int glb_rst_st;
	unsigned int glb_srst_fst;
	unsigned int glb_srst_snd;
	unsigned int glb_rst_con;
	unsigned int reserved5[11];
	unsigned int sdmmc_con[2];
	unsigned int sdio_con[2];
	unsigned int emmc_con[2];
	unsigned int reserved6[2];
	unsigned int gmac_con;
	unsigned int misc[2];
	unsigned int reserved7[45];
	unsigned int autocs_con[26];
};

check_member(rv1126_cru, autocs_con[25], 0x584);

struct pll_rate_table {
	unsigned long rate;
	unsigned int fbdiv;
	unsigned int postdiv1;
	unsigned int refdiv;
	unsigned int postdiv2;
	unsigned int dsmpd;
	unsigned int frac;
};

struct cpu_rate_table {
	unsigned long rate;
	unsigned int aclk_div;
	unsigned int pclk_div;
};

#define RV1126_PMU_MODE			0x0
#define RV1126_PMU_PLL_CON(x)		((x) * 0x4 + 0x10)
#define RV1126_PLL_CON(x)		((x) * 0x4)
#define RV1126_MODE_CON			0x90

enum {
	/* CRU_PMU_CLK_SEL0_CON */
	RTC32K_SEL_SHIFT	= 7,
	RTC32K_SEL_MASK		= 0x3 << RTC32K_SEL_SHIFT,
	RTC32K_SEL_PMUPVTM	= 0,
	RTC32K_SEL_OSC1_32K,
	RTC32K_SEL_OSC0_DIV32K,

	/* CRU_PMU_CLK_SEL1_CON */
	PCLK_PDPMU_DIV_SHIFT	= 0,
	PCLK_PDPMU_DIV_MASK	= 0x1f,

	/* CRU_PMU_CLK_SEL2_CON */
	CLK_I2C0_DIV_SHIFT	= 0,
	CLK_I2C0_DIV_MASK	= 0x7f,

	/* CRU_PMU_CLK_SEL3_CON */
	CLK_I2C2_DIV_SHIFT	= 0,
	CLK_I2C2_DIV_MASK	= 0x7f,

	/* CRU_PMU_CLK_SEL6_CON */
	CLK_PWM1_SEL_SHIFT	= 15,
	CLK_PWM1_SEL_MASK	= 1 << CLK_PWM1_SEL_SHIFT,
	CLK_PWM1_SEL_XIN24M	= 0,
	CLK_PWM1_SEL_GPLL,
	CLK_PWM1_DIV_SHIFT	= 8,
	CLK_PWM1_DIV_MASK	= 0x7f << CLK_PWM1_DIV_SHIFT,
	CLK_PWM0_SEL_SHIFT	= 7,
	CLK_PWM0_SEL_MASK	= 1 << CLK_PWM0_SEL_SHIFT,
	CLK_PWM0_SEL_XIN24M	= 0,
	CLK_PWM0_SEL_GPLL,
	CLK_PWM0_DIV_SHIFT	= 0,
	CLK_PWM0_DIV_MASK	= 0x7f,

	/* CRU_PMU_CLK_SEL9_CON */
	CLK_SPI0_SEL_SHIFT	= 7,
	CLK_SPI0_SEL_MASK	= 1 << CLK_SPI0_SEL_SHIFT,
	CLK_SPI0_SEL_GPLL	= 0,
	CLK_SPI0_SEL_XIN24M,
	CLK_SPI0_DIV_SH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