summaryrefslogtreecommitdiff
path: root/zephyr/shim/src/system.c
blob: b1a443fbf43fe0fbe3178b074dcb7bd0603b267a (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Copyright 2020 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.
 */

#include <device.h>
#include <drivers/cros_bbram.h>
#include <drivers/cros_system.h>
#include <logging/log.h>

#include "bbram.h"
#include "common.h"
#include "cros_version.h"
#include "system.h"
#include "watchdog.h"

#define BBRAM_REGION_PD0	DT_PATH(named_bbram_regions, pd0)
#define BBRAM_REGION_PD1	DT_PATH(named_bbram_regions, pd1)
#define BBRAM_REGION_PD2	DT_PATH(named_bbram_regions, pd2)
#define BBRAM_REGION_TRY_SLOT	DT_PATH(named_bbram_regions, try_slot)

LOG_MODULE_REGISTER(shim_system, LOG_LEVEL_ERR);

STATIC_IF_NOT(CONFIG_ZTEST) const struct device *bbram_dev;
STATIC_IF_NOT(CONFIG_ZTEST) const struct device *sys_dev;

#if DT_NODE_EXISTS(DT_NODELABEL(bbram))
static int system_init(const struct device *unused)
{
	ARG_UNUSED(unused);

	bbram_dev = device_get_binding(DT_LABEL(DT_NODELABEL(bbram)));
	return 0;
}

SYS_INIT(system_init, PRE_KERNEL_1, 50);
#endif

/* Map idx to a bbram offset/size, or return -1 on invalid idx */
static int bbram_lookup(enum system_bbram_idx idx, int *offset_out,
			int *size_out)
{
	switch (idx) {
	case SYSTEM_BBRAM_IDX_PD0:
		*offset_out = DT_PROP(BBRAM_REGION_PD0, offset);
		*size_out = DT_PROP(BBRAM_REGION_PD0, size);
		break;
	case SYSTEM_BBRAM_IDX_PD1:
		*offset_out = DT_PROP(BBRAM_REGION_PD1, offset);
		*size_out = DT_PROP(BBRAM_REGION_PD1, size);
		break;
	case SYSTEM_BBRAM_IDX_PD2:
		*offset_out = DT_PROP(BBRAM_REGION_PD2, offset);
		*size_out = DT_PROP(BBRAM_REGION_PD2, size);
		break;
	case SYSTEM_BBRAM_IDX_TRY_SLOT:
		*offset_out = DT_PROP(BBRAM_REGION_TRY_SLOT, offset);
		*size_out = DT_PROP(BBRAM_REGION_TRY_SLOT, size);
		break;
	default:
		return EC_ERROR_INVAL;
	}
	return EC_SUCCESS;
}

int system_get_bbram(enum system_bbram_idx idx, uint8_t *value)
{
	int offset, size, rc;

	if (bbram_dev == NULL)
		return EC_ERROR_INVAL;

	rc = bbram_lookup(idx, &offset, &size);
	if (rc)
		return rc;

	rc = cros_bbram_read(bbram_dev, offset, size, value);

	return rc ? EC_ERROR_INVAL : EC_SUCCESS;
}

void system_hibernate(uint32_t seconds, uint32_t microseconds)
{
	const struct device *sys_dev = device_get_binding("CROS_SYSTEM");
	int err;

	/* Flush console before hibernating */
	cflush();

	if (board_hibernate)
		board_hibernate();

	/* Save 'wake-up from hibernate' reset flag */
	chip_save_reset_flags(chip_read_reset_flags() |
			      EC_RESET_FLAG_HIBERNATE);

	err = cros_system_hibernate(sys_dev, seconds, microseconds);
	if (err < 0) {
		LOG_ERR("hibernate failed %d", err);
		return;
	}

	/* should never reach this point */
	while (1)
		continue;
}

const char *system_get_chip_vendor(void)
{
	const struct device *sys_dev = device_get_binding("CROS_SYSTEM");

	return cros_system_chip_vendor(sys_dev);
}

const char *system_get_chip_name(void)
{
	const struct device *sys_dev = device_get_binding("CROS_SYSTEM");

	return cros_system_chip_name(sys_dev);
}

const char *system_get_chip_revision(void)
{
	const struct device *sys_dev = device_get_binding("CROS_SYSTEM");

	return cros_system_chip_revision(sys_dev);
}

void system_reset(int flags)
{
	int err;
	uint32_t save_flags;

	if (!sys_dev)
		LOG_ERR("sys_dev get binding failed");

	/* Disable interrupts to avoid task swaps during reboot */
	interrupt_disable_all();

	/*  Get flags to be saved in BBRAM */
	system_encode_save_flags(flags, &save_flags);

	/* Store flags to battery backed RAM. */
	chip_save_reset_flags(save_flags);

	/* If WAIT_EXT is set, then allow 10 seconds for external reset */
	if (flags & SYSTEM_RESET_WAIT_EXT) {
		int i;

		/* Wait 10 seconds for external reset */
		for (i = 0; i < 1000; i++) {
			watchdog_reload();
			udelay(10000);
		}
	}

	err = cros_system_soc_reset(sys_dev);

	if (err < 0)
		LOG_ERR("soc reset failed");

	/* should never return */
	while (1)
		continue;
}

static int check_reset_cause(void)
{
	uint32_t chip_flags = 0; /* used to write back to the BBRAM */
	uint32_t system_flags = chip_read_reset_flags(); /* system reset flag */
	int chip_reset_cause = 0; /* chip-level reset cause */

	chip_reset_cause = cros_system_get_reset_cause(sys_dev);
	if (chip_reset_cause < 0)
		return -1;

	/*
	 * TODO(b/182876692): Implement CONFIG_POWER_BUTTON_INIT_IDLE &
	 * CONFIG_BOARD_FORCE_RESET_PIN. Also, check the hibernate flow if PSL
	 * merge into tot.
	 */

	switch (chip_reset_cause) {
	case POWERUP:
		system_flags |= EC_RESET_FLAG_POWER_ON;
		/*
		 * Power-on restart, so set a flag and save it for the next
		 * imminent reset. Later code will check for this flag and wait
		 * for the second reset. Waking from PSL hibernate is power-on
		 * for EC but not for H1, so do not wait for the second reset.
		 */
		if (IS_ENABLED(CONFIG_BOARD_RESET_AFTER_POWER_ON) &&
		    ((system_flags & EC_RESET_FLAG_HIBERNATE) == 0)) {
			system_flags |= EC_RESET_FLAG_INITIAL_PWR;
			chip_flags |= EC_RESET_FLAG_INITIAL_PWR;
		}
		break;

	case VCC1_RST_PIN:
		/*
		 * If configured, check the saved flags to see whether the
		 * previous restart was a power-on, in which case treat this
		 * restart as a power-on as well. This is to workaround the fact
		 * that the H1 will reset the EC at power up.
		 */
		if (IS_ENABLED(CONFIG_BOARD_RESET_AFTER_POWER_ON)) {
			if (system_flags & EC_RESET_FLAG_INITIAL_PWR) {
				/*
				 * The previous restart was a power-on so treat
				 * this restart as that, and clear the flag so
				 * later code will not wait for the second
				 * reset.
				 */
				system_flags = (system_flags &
						~EC_RESET_FLAG_INITIAL_PWR) |
					       EC_RESET_FLAG_POWER_ON;
			} else {
				/*
				 * No previous reset flag, so this is a
				 * subsequent restart i.e any restarts after the
				 * second restart caused by the H1.
				 */
				system_flags |= EC_RESET_FLAG_RESET_PIN;
			}
		} else {
			system_flags |= EC_RESET_FLAG_RESET_PIN;
		}
		break;

	case DEBUG_RST:
		system_flags |= EC_RESET_FLAG_SOFT;
		break;

	case WATCHDOG_RST:
		/*
		 * Don't set EC_RESET_FLAG_WATCHDOG flag if watchdog is issued
		 * by system_reset in order to distinguish reset cause is panic
		 * reason or not.
		 */
		if (!(system_flags & (EC_RESET_FLAG_SOFT | EC_RESET_FLAG_HARD)))
			system_flags |= EC_RESET_FLAG_WATCHDOG;
		break;
	}

	/* Clear & set the reset flags for the following reset. */
	chip_save_reset_flags(chip_flags);

	/* Set the system reset flags. */
	system_set_reset_flags(system_flags);

	return 0;
}

static int system_preinitialize(const struct device *unused)
{
	ARG_UNUSED(unused);

	sys_dev = device_get_binding("CROS_SYSTEM");
	if (!sys_dev) {
		/*
		 * TODO(b/183022804): This should not happen in normal
		 * operation. Check whether the error check can be change to
		 * build-time error, or at least a fatal run-time error.
		 */
		LOG_ERR("sys_dev gets binding failed");
		return -1;
	}

	/* check the reset cause */
	if (check_reset_cause() != 0) {
		LOG_ERR("check the reset cause failed");
		return -1;
	}

	/*
	 * For some boards on power-on, the EC is reset by the H1 after
	 * power-on, so the EC sees 2 resets. This config enables the EC to save
	 * a flag on the first power-up restart, and then wait for the second
	 * reset before any other setup is done (such as GPIOs, timers, UART
	 * etc.) On the second reset, the saved flag is used to detect the
	 * previous power-on, and treat the second reset as a power-on instead
	 * of a reset.
	 */
	if (IS_ENABLED(CONFIG_BOARD_RESET_AFTER_POWER_ON) &&
	    system_get_reset_flags() & EC_RESET_FLAG_INITIAL_PWR) {
		/* TODO(b/182875520): Change to use 2 second delay. */
		while (1)
			continue;
	}

	return 0;
}
#if (!defined(CONFIG_ZTEST))
SYS_INIT(system_preinitialize, PRE_KERNEL_1,
	 CONFIG_PLATFORM_EC_SYSTEM_PRE_INIT_PRIORITY);
#endif