summaryrefslogtreecommitdiff
path: root/zephyr/drivers/cros_rtc/cros_rtc_npcx.c
blob: 0ecbe5f47c06d92741a023628b5d6f4527163f8e (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright 2021 Google LLC
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#define DT_DRV_COMPAT nuvoton_npcx_cros_mtc

#include <assert.h>
#include <drivers/cros_rtc.h>
#include <drivers/gpio.h>
#include <kernel.h>
#include <soc.h>
#include <soc/nuvoton_npcx/reg_def_cros.h>

#include "ec_tasks.h"
#include "soc_miwu.h"
#include "task.h"

#include <logging/log.h>
LOG_MODULE_REGISTER(cros_rtc, LOG_LEVEL_ERR);

#define NPCX_MTC_TTC_LOAD_DELAY_US 250 /* Delay after writing TTC */
#define NPCX_MTC_ALARM_MASK GENMASK(24, 0) /* Valid field of alarm in WTC */

/* Driver config */
struct cros_rtc_npcx_config {
	/* Monotonic counter base address */
	uintptr_t base;
	/* Monotonic counter wake-up input source configuration */
	const struct npcx_wui mtc_alarm;
};

/* Driver data */
struct cros_rtc_npcx_data {
	/* Monotonic counter wake-up callback object */
	struct miwu_dev_callback miwu_mtc_cb;
	cros_rtc_alarm_callback_t alarm_callback;
};

/* Driver convenience defines */
#define DRV_CONFIG(dev) ((const struct cros_rtc_npcx_config *)(dev)->config)

#define DRV_DATA(dev) ((struct cros_rtc_npcx_data *)(dev)->data)

#define HAL_INSTANCE(dev) (struct mtc_reg *)(DRV_CONFIG(dev)->base)

/* Counter internal local functions */
static uint32_t counter_npcx_get_val(const struct device *dev)
{
	struct mtc_reg *const inst = HAL_INSTANCE(dev);

	/*
	 * Get value of monotonic counter which keeps counting when VCC1 power
	 * domain exists (Unit:sec)
	 */
	return inst->TTC;
}

static void counter_npcx_set_val(const struct device *dev, uint32_t val)
{
	struct mtc_reg *const inst = HAL_INSTANCE(dev);

	/*
	 * Set monotonic counter. Write it twice to ensure the value latch to
	 * TTC register. A delay (~250 us) is also needed before writing again.
	 */
	inst->TTC = val;
	k_busy_wait(NPCX_MTC_TTC_LOAD_DELAY_US);

	inst->TTC = val;
	k_busy_wait(NPCX_MTC_TTC_LOAD_DELAY_US);
}

static uint32_t counter_npcx_get_alarm_val(const struct device *dev)
{
	struct mtc_reg *const inst = HAL_INSTANCE(dev);

	/*
	 * If alarm is not set or it is set and has already gone off, return
	 * zero directly.
	 */
	if (!IS_BIT_SET(inst->WTC, NPCX_WTC_WIE) ||
	    IS_BIT_SET(inst->WTC, NPCX_WTC_PTO)) {
		return 0;
	}

	/* Return 25-bit alarm value */
	return inst->WTC & NPCX_MTC_ALARM_MASK;
}

static void counter_npcx_set_alarm_val(const struct device *dev, uint32_t val)
{
	struct mtc_reg *const inst = HAL_INSTANCE(dev);

	/* Disable alarm interrupt */
	inst->WTC &= ~BIT(NPCX_WTC_WIE);

	/* Set new alarm value */
	inst->WTC = val & NPCX_MTC_ALARM_MASK;

	/* Enable alarm interrupt */
	inst->WTC |= BIT(NPCX_WTC_WIE);
}

static void counter_npcx_reset_alarm(const struct device *dev)
{
	struct mtc_reg *const inst = HAL_INSTANCE(dev);

	/* Disable alarm interrupt first */
	if (IS_BIT_SET(inst->WTC, NPCX_WTC_WIE)) {
		inst->WTC &= ~BIT(NPCX_WTC_WIE);
	}

	/* Set alarm to maximum value and clear its pending bit */
	if (IS_BIT_SET(inst->WTC, NPCX_WTC_PTO)) {
		inst->WTC = NPCX_MTC_ALARM_MASK;
		inst->WTC |= BIT(NPCX_WTC_PTO);
	}
}

/* Counter local functions */
static void counter_npcx_isr(const struct device *dev, struct npcx_wui *wui)
{
	struct cros_rtc_npcx_data *data = DRV_DATA(dev);

	LOG_DBG("%s", __func__);

	/* Alarm is one-shot, so reset alarm to default */
	counter_npcx_reset_alarm(dev);

	/* Call callback function */
	if (data->alarm_callback) {
		data->alarm_callback(dev);
	}
}

/* cros ec RTC api functions */
static int cros_rtc_npcx_configure(const struct device *dev,
				   cros_rtc_alarm_callback_t callback)
{
	struct cros_rtc_npcx_data *data = DRV_DATA(dev);

	if (callback == NULL) {
		return -EINVAL;
	}

	data->alarm_callback = callback;
	return 0;
}

static int cros_rtc_npcx_get_value(const struct device *dev, uint32_t *value)
{
	*value = counter_npcx_get_val(dev);

	return 0;
}

static int cros_rtc_npcx_set_value(const struct device *dev, uint32_t value)
{
	counter_npcx_set_val(dev, value);

	return 0;
}
static int cros_rtc_npcx_get_alarm(const struct device *dev, uint32_t *seconds,
				   uint32_t *microseconds)
{
	*seconds = counter_npcx_get_alarm_val(dev);
	*microseconds = 0;

	return 0;
}
static int cros_rtc_npcx_set_alarm(const struct device *dev, uint32_t seconds,
				   uint32_t microseconds)
{
	const struct cros_rtc_npcx_config *config = DRV_CONFIG(dev);
	ARG_UNUSED(microseconds);

	/* Enable interrupt of the MTC alarm wake-up input source */
	npcx_miwu_irq_enable(&config->mtc_alarm);

	/* Make sure alarm restore to default state */
	counter_npcx_reset_alarm(dev);
	counter_npcx_set_alarm_val(dev, seconds);

	return 0;
}

static int cros_rtc_npcx_reset_alarm(const struct device *dev)
{
	const struct cros_rtc_npcx_config *config = DRV_CONFIG(dev);

	/* Disable interrupt of the MTC alarm wake-up input source */
	npcx_miwu_irq_disable(&config->mtc_alarm);

	counter_npcx_reset_alarm(dev);

	return 0;
}

/* cros ec RTC driver registration */
static const struct cros_rtc_driver_api cros_rtc_npcx_driver_api = {
	.configure = cros_rtc_npcx_configure,
	.get_value = cros_rtc_npcx_get_value,
	.set_value = cros_rtc_npcx_set_value,
	.get_alarm = cros_rtc_npcx_get_alarm,
	.set_alarm = cros_rtc_npcx_set_alarm,
	.reset_alarm = cros_rtc_npcx_reset_alarm,
};

static int cros_rtc_npcx_init(const struct device *dev)
{
	const struct cros_rtc_npcx_config *config = DRV_CONFIG(dev);
	struct cros_rtc_npcx_data *data = DRV_DATA(dev);

	/* Initialize the miwu input and its callback for monotonic counter */
	npcx_miwu_init_dev_callback(&data->miwu_mtc_cb, &config->mtc_alarm,
				    counter_npcx_isr, dev);
	npcx_miwu_manage_dev_callback(&data->miwu_mtc_cb, true);

	/*
	 * Configure the monotonic counter wake-up event triggered from a rising
	 * edge on its signal.
	 */
	npcx_miwu_interrupt_configure(&config->mtc_alarm, NPCX_MIWU_MODE_EDGE,
				      NPCX_MIWU_TRIG_HIGH);

	return 0;
}

static const struct cros_rtc_npcx_config cros_rtc_npcx_cfg_0 = {
	.base = DT_INST_REG_ADDR(0),
	.mtc_alarm = NPCX_DT_WUI_ITEM_BY_NAME(0, mtc_alarm)
};

static struct cros_rtc_npcx_data cros_rtc_npcx_data_0;

DEVICE_DT_INST_DEFINE(0, cros_rtc_npcx_init, /* pm_control_fn= */ NULL,
		      &cros_rtc_npcx_data_0, &cros_rtc_npcx_cfg_0, POST_KERNEL,
		      CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		      &cros_rtc_npcx_driver_api);