18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2008 Cavium Networks
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/init.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/io.h>
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/atomic.h>
118c2ecf20Sopenharmony_ci#include "cns3xxx.h"
128c2ecf20Sopenharmony_ci#include "pm.h"
138c2ecf20Sopenharmony_ci#include "core.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_civoid cns3xxx_pwr_clk_en(unsigned int block)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	u32 reg = __raw_readl(PM_CLK_GATE_REG);
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	reg |= (block & PM_CLK_GATE_REG_MASK);
208c2ecf20Sopenharmony_ci	__raw_writel(reg, PM_CLK_GATE_REG);
218c2ecf20Sopenharmony_ci}
228c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cns3xxx_pwr_clk_en);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_civoid cns3xxx_pwr_clk_dis(unsigned int block)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	u32 reg = __raw_readl(PM_CLK_GATE_REG);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	reg &= ~(block & PM_CLK_GATE_REG_MASK);
298c2ecf20Sopenharmony_ci	__raw_writel(reg, PM_CLK_GATE_REG);
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cns3xxx_pwr_clk_dis);
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_civoid cns3xxx_pwr_power_up(unsigned int block)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	u32 reg = __raw_readl(PM_PLL_HM_PD_CTRL_REG);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	reg &= ~(block & CNS3XXX_PWR_PLL_ALL);
388c2ecf20Sopenharmony_ci	__raw_writel(reg, PM_PLL_HM_PD_CTRL_REG);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	/* Wait for 300us for the PLL output clock locked. */
418c2ecf20Sopenharmony_ci	udelay(300);
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cns3xxx_pwr_power_up);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_civoid cns3xxx_pwr_power_down(unsigned int block)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	u32 reg = __raw_readl(PM_PLL_HM_PD_CTRL_REG);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	/* write '1' to power down */
508c2ecf20Sopenharmony_ci	reg |= (block & CNS3XXX_PWR_PLL_ALL);
518c2ecf20Sopenharmony_ci	__raw_writel(reg, PM_PLL_HM_PD_CTRL_REG);
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cns3xxx_pwr_power_down);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic void cns3xxx_pwr_soft_rst_force(unsigned int block)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	u32 reg = __raw_readl(PM_SOFT_RST_REG);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	/*
608c2ecf20Sopenharmony_ci	 * bit 0, 28, 29 => program low to reset,
618c2ecf20Sopenharmony_ci	 * the other else program low and then high
628c2ecf20Sopenharmony_ci	 */
638c2ecf20Sopenharmony_ci	if (block & 0x30000001) {
648c2ecf20Sopenharmony_ci		reg &= ~(block & PM_SOFT_RST_REG_MASK);
658c2ecf20Sopenharmony_ci	} else {
668c2ecf20Sopenharmony_ci		reg &= ~(block & PM_SOFT_RST_REG_MASK);
678c2ecf20Sopenharmony_ci		__raw_writel(reg, PM_SOFT_RST_REG);
688c2ecf20Sopenharmony_ci		reg |= (block & PM_SOFT_RST_REG_MASK);
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	__raw_writel(reg, PM_SOFT_RST_REG);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_civoid cns3xxx_pwr_soft_rst(unsigned int block)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	static unsigned int soft_reset;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (soft_reset & block) {
798c2ecf20Sopenharmony_ci		/* SPI/I2C/GPIO use the same block, reset once. */
808c2ecf20Sopenharmony_ci		return;
818c2ecf20Sopenharmony_ci	} else {
828c2ecf20Sopenharmony_ci		soft_reset |= block;
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci	cns3xxx_pwr_soft_rst_force(block);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cns3xxx_pwr_soft_rst);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_civoid cns3xxx_restart(enum reboot_mode mode, const char *cmd)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	/*
918c2ecf20Sopenharmony_ci	 * To reset, we hit the on-board reset register
928c2ecf20Sopenharmony_ci	 * in the system FPGA.
938c2ecf20Sopenharmony_ci	 */
948c2ecf20Sopenharmony_ci	cns3xxx_pwr_soft_rst(CNS3XXX_PWR_SOFTWARE_RST(GLOBAL));
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/*
988c2ecf20Sopenharmony_ci * cns3xxx_cpu_clock - return CPU/L2 clock
998c2ecf20Sopenharmony_ci *  aclk: cpu clock/2
1008c2ecf20Sopenharmony_ci *  hclk: cpu clock/4
1018c2ecf20Sopenharmony_ci *  pclk: cpu clock/8
1028c2ecf20Sopenharmony_ci */
1038c2ecf20Sopenharmony_ciint cns3xxx_cpu_clock(void)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	u32 reg = __raw_readl(PM_CLK_CTRL_REG);
1068c2ecf20Sopenharmony_ci	int cpu;
1078c2ecf20Sopenharmony_ci	int cpu_sel;
1088c2ecf20Sopenharmony_ci	int div_sel;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	cpu_sel = (reg >> PM_CLK_CTRL_REG_OFFSET_PLL_CPU_SEL) & 0xf;
1118c2ecf20Sopenharmony_ci	div_sel = (reg >> PM_CLK_CTRL_REG_OFFSET_CPU_CLK_DIV) & 0x3;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	cpu = (300 + ((cpu_sel / 3) * 100) + ((cpu_sel % 3) * 33)) >> div_sel;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return cpu;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cns3xxx_cpu_clock);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ciatomic_t usb_pwr_ref = ATOMIC_INIT(0);
1208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(usb_pwr_ref);
121