summaryrefslogtreecommitdiff
path: root/chip/stm32/clock-l4.h
blob: d237b84580e5f2ec6add3033d9c1ef40fc6f2cac (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Copyright 2021 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Clocks and power management settings */

#ifndef __CROS_EC_CLOCK_L4_H
#define __CROS_EC_CLOCK_L4_H

#include "chipset.h"
#include "clock.h"
#include "common.h"
#include "console.h"
#include "cpu.h"
#include "hooks.h"
#include "hwtimer.h"
#include "registers.h"
#include "system.h"
#include "task.h"
#include "timer.h"
#include "util.h"

#define STM32L4_RTC_REQ 1000000
#define STM32L4_LSI_CLOCK 32000

/* Lock and unlock RTC write access */
static inline void rtc_lock_regs(void)
{
	STM32_RTC_WPR = 0xff;
}
static inline void rtc_unlock_regs(void)
{
	STM32_RTC_WPR = 0xca;
	STM32_RTC_WPR = 0x53;
}

struct rtc_time_reg {
	uint32_t rtc_ssr; /* subseconds */
	uint32_t rtc_tr; /* hours, minutes, seconds */
	uint32_t rtc_dr; /* years, months, dates, week days */
};

/* Save the RTC alarm wake time */
struct wake_time {
	timestamp_t ts;
	uint32_t rtc_alrmar; /* the value of register STM32_RTC_ALRMAR */
};

/* Convert between RTC regs in BCD and seconds */
uint32_t rtc_to_sec(const struct rtc_time_reg *rtc);

/* Convert between seconds and RTC regs */
void sec_to_rtc(uint32_t sec, struct rtc_time_reg *rtc);

/* Calculate microseconds from rtc sub-second register. */
uint32_t rtcss_to_us(uint32_t rtcss);

/* Calculate rtc sub-second register value from microseconds. */
uint32_t us_to_rtcss(uint32_t us);

/* Return sub-10-sec time diff between two rtc readings */
uint32_t get_rtc_diff(const struct rtc_time_reg *rtc0,
		      const struct rtc_time_reg *rtc1);

/* Read RTC values */
void rtc_read(struct rtc_time_reg *rtc);

/* Set RTC value */
void rtc_set(uint32_t sec);

/* Set RTC wakeup, save alarm wakeup time when save_alarm != 0 */
void set_rtc_alarm(uint32_t delay_s, uint32_t delay_us,
		   struct rtc_time_reg *rtc, uint8_t save_alarm);

/* Clear RTC wakeup */
void reset_rtc_alarm(struct rtc_time_reg *rtc);

/*
 * Return the remaining seconds before the RTC alarm goes off.
 * Sub-seconds are ignored. Returns 0 if alarm is not set.
 */
uint32_t get_rtc_alarm(void);

/* RTC init */
void rtc_init(void);

/* Init clock blocks and functionality */
void clock_init(void);

/* Init high speed clock config */
void config_hispeed_clock(void);

/* Get timer clock frequency (for STM32 only) */
int clock_get_timer_freq(void);

/*
 * Return 1 if host_wake_time is nonzero and the saved host_wake_time
 * is expired at a given time, ts.
 */
bool is_host_wake_alarm_expired(timestamp_t ts);

/* Set RTC wakeup based on the value saved in host_wake_time */
void restore_host_wake_alarm(void);

#ifdef CONFIG_LOW_POWER_IDLE
void low_power_init(void);
#endif

#endif  /* __CROS_EC_CLOCK_L4_H */