1 /*
2  * Copyright (c) 2019 Vestas Wind Systems A/S
3  *
4  * Based on clock_control_rv32m1_pcc.c, which is:
5  * Copyright (c) 2018 Foundries.io
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #define DT_DRV_COMPAT nxp_kinetis_pcc
11 
12 #include <errno.h>
13 #include <soc.h>
14 #include <zephyr/drivers/clock_control.h>
15 #include <fsl_clock.h>
16 
17 #define LOG_LEVEL CONFIG_CLOCK_CONTROL_LOG_LEVEL
18 #include <zephyr/logging/log.h>
19 LOG_MODULE_REGISTER(clock_control_mcux_pcc);
20 
21 struct mcux_pcc_config {
22 	uint32_t base_address;
23 };
24 
25 #define DEV_BASE(dev) (((struct mcux_pcc_config *)(dev->config))->base_address)
26 #ifndef MAKE_PCC_REGADDR
27 #define MAKE_PCC_REGADDR(base, offset) ((base) + (offset))
28 #endif
29 
clock_ip(const struct device * dev,clock_control_subsys_t sub_system)30 static inline clock_ip_name_t clock_ip(const struct device *dev,
31 				       clock_control_subsys_t sub_system)
32 {
33 	uint32_t offset = POINTER_TO_UINT(sub_system);
34 
35 	return MAKE_PCC_REGADDR(DEV_BASE(dev), offset);
36 }
37 
mcux_pcc_on(const struct device * dev,clock_control_subsys_t sub_system)38 static int mcux_pcc_on(const struct device *dev,
39 		       clock_control_subsys_t sub_system)
40 {
41 	CLOCK_EnableClock(clock_ip(dev, sub_system));
42 	return 0;
43 }
44 
mcux_pcc_off(const struct device * dev,clock_control_subsys_t sub_system)45 static int mcux_pcc_off(const struct device *dev,
46 			clock_control_subsys_t sub_system)
47 {
48 	CLOCK_DisableClock(clock_ip(dev, sub_system));
49 	return 0;
50 }
51 
mcux_pcc_get_rate(const struct device * dev,clock_control_subsys_t sub_system,uint32_t * rate)52 static int mcux_pcc_get_rate(const struct device *dev,
53 			       clock_control_subsys_t sub_system,
54 			       uint32_t *rate)
55 {
56 	*rate = CLOCK_GetIpFreq(clock_ip(dev, sub_system));
57 	return 0;
58 }
59 
60 static const struct clock_control_driver_api mcux_pcc_api = {
61 	.on = mcux_pcc_on,
62 	.off = mcux_pcc_off,
63 	.get_rate = mcux_pcc_get_rate,
64 };
65 
66 #define MCUX_PCC_INIT(inst)						\
67 	static const struct mcux_pcc_config mcux_pcc##inst##_config = {	\
68 		.base_address = DT_INST_REG_ADDR(inst)			\
69 	};								\
70 									\
71 	DEVICE_DT_INST_DEFINE(inst,					\
72 			    NULL,					\
73 			    NULL,					\
74 			    NULL, &mcux_pcc##inst##_config,		\
75 			    PRE_KERNEL_1,				\
76 			    CONFIG_CLOCK_CONTROL_INIT_PRIORITY,		\
77 			    &mcux_pcc_api);
78 
79 DT_INST_FOREACH_STATUS_OKAY(MCUX_PCC_INIT)
80