summaryrefslogtreecommitdiff
path: root/chip/npcx/lct.c
blob: 8f720e13760ae63960cfe382f8ac25b1a965b57f (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* Copyright 2020 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* LCT (Long Countdown Timer) module for Chrome EC */
#include "console.h"
#include "hooks.h"
#include "lct_chip.h"
#include "registers.h"
#include "rtc.h"
#include "task.h"
#include "timer.h"
#include "util.h"

#define LCT_CLK_ENABLE_DELAY_USEC 150

#define CPRINTF(format, args...) cprintf(CC_CLOCK, format, ##args)
#define CPRINTS(format, args...) cprints(CC_CLOCK, format, ##args)

void npcx_lct_sel_power_src(enum NPCX_LCT_PWR_SRC pwr_src)
{
	if (IS_BIT_SET(NPCX_LCTCONT, NPCX_LCTCONT_EN)) {
		CPRINTS("Don't set power source when LCT is enabled");
		return;
	}

	if (pwr_src == NPCX_LCT_PWR_SRC_VSBY)
		SET_BIT(NPCX_LCTCONT, NPCX_LCTCONT_VSBY_PWR);
	else
		CLEAR_BIT(NPCX_LCTCONT, NPCX_LCTCONT_VSBY_PWR);
}

void npcx_lct_enable_clk(uint8_t enable)
{
	if (IS_BIT_SET(NPCX_LCTCONT, NPCX_LCTCONT_EN)) {
		CPRINTS("Don't set/unset clock when LCT is enabled");
		return;
	}

	if (enable) {
		SET_BIT(NPCX_LCTCONT, NPCX_LCTCONT_CLK_EN);
		/*
		 * This bit must be set to 1 at least tLCTCKEN (150 us)
		 * before the LCT is enabled.
		 */
		udelay(LCT_CLK_ENABLE_DELAY_USEC);
	} else {
		CLEAR_BIT(NPCX_LCTCONT, NPCX_LCTCONT_CLK_EN);
	}
}

void npcx_lct_enable(uint8_t enable)
{
	enable = !!enable;
	SET_FIELD(NPCX_LCTCONT, NPCX_LCTCONT_EN_FIELD, enable);
	/* Wait until the bit value equals to what is set */
	while (IS_BIT_SET(NPCX_LCTCONT, NPCX_LCTCONT_EN) != enable)
		;
}

void npcx_lct_config(int seconds, int psl_ena, int int_ena)
{
	if (IS_BIT_SET(NPCX_LCTCONT, NPCX_LCTCONT_EN)) {
		CPRINTS("Don't config LCT when LCT is enabled");
		return;
	}

	/* LCT can count max to (16 weeks - 1 second) */
	if (seconds > NPCX_LCT_MAX) {
		CPRINTS("LCT time is out of range");
		return;
	}

	/* Clear pending LCT event first */
	NPCX_LCTSTAT = BIT(NPCX_LCTSTAT_EVST);

	NPCX_LCTWEEK = seconds / SECS_PER_WEEK;
	seconds %= SECS_PER_WEEK;
	NPCX_LCTDAY = seconds / SECS_PER_DAY;
	seconds %= SECS_PER_DAY;
	NPCX_LCTHOUR = seconds / SECS_PER_HOUR;
	seconds %= SECS_PER_HOUR;
	NPCX_LCTMINUTE = seconds / SECS_PER_MINUTE;
	NPCX_LCTSECOND = seconds % SECS_PER_MINUTE;

	if (psl_ena) {
		if (IS_BIT_SET(NPCX_LCTCONT, NPCX_LCTCONT_VSBY_PWR))
			SET_BIT(NPCX_LCTCONT, NPCX_LCTCONT_PSL_EN);
		else
			CPRINTS("LCT must source VSBY to support PSL wakeup");
	}

	if (int_ena)
		SET_BIT(NPCX_LCTCONT, NPCX_LCTCONT_EVEN);
}

uint32_t npcx_lct_get_time(void)
{
	uint32_t second;
	uint8_t week, day, hour, minute;

	do {
		week = NPCX_LCTWEEK;
		day = NPCX_LCTDAY;
		hour = NPCX_LCTHOUR;
		minute = NPCX_LCTMINUTE;
		second = NPCX_LCTSECOND;
	} while (week != NPCX_LCTWEEK || day != NPCX_LCTDAY ||
		 hour != NPCX_LCTHOUR || minute != NPCX_LCTMINUTE ||
		 second != NPCX_LCTSECOND);

	second += minute * SECS_PER_MINUTE + hour * SECS_PER_HOUR +
		  day * SECS_PER_DAY + week * SECS_PER_WEEK;

	return second;
}

void npcx_lct_clear_event(void)
{
	NPCX_LCTSTAT = BIT(NPCX_LCTSTAT_EVST);
}

int npcx_lct_is_event_set(void)
{
	return IS_BIT_SET(NPCX_LCTSTAT, NPCX_LCTSTAT_EVST);
}

static void npcx_lct_init(void)
{
	/* Disable LCT */
	npcx_lct_enable(0);
	/* Clear control and status registers */
	NPCX_LCTCONT = 0x0;
	npcx_lct_clear_event();
	/* Clear all timer registers */
	NPCX_LCTSECOND = 0x0;
	NPCX_LCTMINUTE = 0x0;
	NPCX_LCTHOUR = 0x0;
	NPCX_LCTDAY = 0x0;
	NPCX_LCTWEEK = 0x0;
}
DECLARE_HOOK(HOOK_INIT, npcx_lct_init, HOOK_PRIO_DEFAULT);

#ifdef CONFIG_CMD_RTC_ALARM
static int command_lctalarm(int argc, const char **argv)
{
	char *e;
	int seconds;

	seconds = strtoi(argv[1], &e, 0);
	if (*e)
		return EC_ERROR_PARAM2;

	npcx_lct_enable(0);
	npcx_lct_sel_power_src(NPCX_LCT_PWR_SRC_VSBY);
	npcx_lct_enable_clk(1);
	/* Enable LCT event interrupt and MIWU */
	npcx_lct_config(seconds, 0, 1);
	task_disable_irq(NPCX_IRQ_LCT_WKINTF_2);
	/* Enable wake-up input sources & clear pending bit */
	NPCX_WKPCL(MIWU_TABLE_2, LCT_WUI_GROUP) |= LCT_WUI_MASK;
	NPCX_WKINEN(MIWU_TABLE_2, LCT_WUI_GROUP) |= LCT_WUI_MASK;
	NPCX_WKEN(MIWU_TABLE_2, LCT_WUI_GROUP) |= LCT_WUI_MASK;
	task_enable_irq(NPCX_IRQ_LCT_WKINTF_2);
	npcx_lct_enable(1);

	return 0;
}
DECLARE_CONSOLE_COMMAND(lctalarm, command_lctalarm, "", "");
#endif