summaryrefslogtreecommitdiff
path: root/zephyr/shim/chip/mchp/clock.c
blob: 6ee4cd931c2955bcea4c82d73b4d88e23cb8979a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <zephyr/device.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/dt-bindings/clock/mchp_xec_pcr.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <soc.h>

#include "clock_chip.h"
#include "module_id.h"

LOG_MODULE_REGISTER(shim_clock, LOG_LEVEL_ERR);

#define PCR_NODE DT_INST(0, microchip_xec_pcr)
#define HAL_PCR_REG_BASE_ADDR \
	((struct pcr_regs *)DT_REG_ADDR_BY_IDX(PCR_NODE, 0))

int clock_get_freq(void)
{
	const struct device *clk_dev = DEVICE_DT_GET(PCR_NODE);
	uint32_t bus = MCHP_XEC_PCR_CLK_CPU;
	uint32_t rate;

	if (clock_control_get_rate(clk_dev, (clock_control_subsys_t)bus,
				   &rate) != 0) {
		LOG_ERR("Get %s clock rate error", clk_dev->name);
		return -EIO;
	}

	return rate;
}

void clock_turbo(void)
{
	struct pcr_regs *const pcr = HAL_PCR_REG_BASE_ADDR;

	pcr->PROC_CLK_CTRL = 1;
	__DSB();
	__ISB();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
}

void clock_normal(void)
{
	struct pcr_regs *const pcr = HAL_PCR_REG_BASE_ADDR;

	pcr->PROC_CLK_CTRL = 4;
	__DSB();
	__ISB();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
}

void clock_enable_module(enum module_id module, int enable)
{
	/* Assume we have a single task using MODULE_FAST_CPU */
	if (module == MODULE_FAST_CPU) {
		if (enable)
			clock_turbo();
		else
			clock_normal();
	}
}