summaryrefslogtreecommitdiff
path: root/chip/it83xx/system.c
blob: 28a2de1b56117a4dd43827e15377dd02569c893b (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/* Copyright 2013 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.
 */

/* System module for Chrome EC : hardware specific implementation */

#include "console.h"
#include "cpu.h"
#include "cros_version.h"
#include "ec2i_chip.h"
#include "flash.h"
#include "hooks.h"
#include "host_command.h"
#include "intc.h"
#include "link_defs.h"
#include "panic.h"
#include "registers.h"
#include "system.h"
#include "task.h"
#include "util.h"
#include "watchdog.h"

void system_hibernate(uint32_t seconds, uint32_t microseconds)
{
#ifdef CONFIG_HOSTCMD_PD
	/* Inform the PD MCU that we are going to hibernate. */
	host_command_pd_request_hibernate();
	/* Wait to ensure exchange with PD before hibernating. */
	msleep(100);
#endif

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

	if (board_hibernate)
		board_hibernate();

	/* chip specific standby mode */
	__enter_hibernate(seconds, microseconds);
}

/* Clear reset flags if it's not cleared in check_reset_cause() */
static int delayed_clear_reset_flags;
static void clear_reset_flags(void)
{
	if (IS_ENABLED(CONFIG_BOARD_RESET_AFTER_POWER_ON) &&
	    delayed_clear_reset_flags) {
		chip_save_reset_flags(0);
	}
}
DECLARE_HOOK(HOOK_INIT, clear_reset_flags, HOOK_PRIO_LAST);

#if !defined(CONFIG_HOST_INTERFACE_LPC) && !defined(CONFIG_HOST_INTERFACE_ESPI)
static void system_save_panic_data_to_bram(void)
{
	uint8_t *ptr = (uint8_t *)PANIC_DATA_PTR;

	for (int i = 0; i < CONFIG_PANIC_DATA_SIZE; i++)
		IT83XX_BRAM_BANK0(i + BRAM_PANIC_DATA_START) = ptr[i];
}

static void system_restore_panic_data_from_bram(void)
{
	uint8_t *ptr = (uint8_t *)PANIC_DATA_PTR;

	for (int i = 0; i < CONFIG_PANIC_DATA_SIZE; i++)
		ptr[i] = IT83XX_BRAM_BANK0(i + BRAM_PANIC_DATA_START);
}
BUILD_ASSERT(BRAM_PANIC_LEN >= CONFIG_PANIC_DATA_SIZE);
#else
static void system_save_panic_data_to_bram(void)
{
}
static void system_restore_panic_data_from_bram(void)
{
}
#endif

static void system_reset_ec_by_gpg1(void)
{
	system_save_panic_data_to_bram();
	/* Set GPG1 as output high and wait until EC reset. */
	IT83XX_GPIO_CTRL(GPIO_G, 1) = GPCR_PORT_PIN_MODE_OUTPUT;
	IT83XX_GPIO_DATA(GPIO_G) |= BIT(1);
	while (1)
		;
}

static void check_reset_cause(void)
{
	uint32_t flags;
	uint8_t raw_reset_cause = IT83XX_GCTRL_RSTS & 0x03;
	uint8_t raw_reset_cause2 = IT83XX_GCTRL_SPCTRL4 & 0x07;

	/* Restore saved reset flags. */
	flags = chip_read_reset_flags();

	/* Clear reset cause. */
	IT83XX_GCTRL_RSTS |= 0x03;
	IT83XX_GCTRL_SPCTRL4 |= 0x07;

	/* Determine if watchdog reset or power on reset. */
	if (raw_reset_cause & 0x02) {
		flags |= EC_RESET_FLAG_WATCHDOG;
		if (IS_ENABLED(CONFIG_IT83XX_HARD_RESET_BY_GPG1)) {
			/*
			 * Save watchdog reset flag to BRAM so we can restore
			 * the flag on next reboot.
			 */
			chip_save_reset_flags(EC_RESET_FLAG_WATCHDOG);
			/*
			 * Assert GPG1 to reset EC and then EC_RST_ODL will be
			 * toggled.
			 */
			system_reset_ec_by_gpg1();
		}
	} else if (raw_reset_cause & 0x01) {
		flags |= EC_RESET_FLAG_POWER_ON;
	} else {
		if ((IT83XX_GCTRL_RSTS & 0xC0) == 0x80)
			flags |= EC_RESET_FLAG_POWER_ON;
	}

	if (raw_reset_cause2 & 0x04)
		flags |= EC_RESET_FLAG_RESET_PIN;

	/* watchdog module triggers these reset */
	if (flags & (EC_RESET_FLAG_HARD | EC_RESET_FLAG_SOFT))
		flags &= ~EC_RESET_FLAG_WATCHDOG;

	/*
	 * On power-on of some boards, H1 releases the EC from reset but then
	 * quickly asserts and releases the reset a second time. This means the
	 * EC sees 2 resets. In order to carry over some important flags (e.g.
	 * HIBERNATE) to the second resets, the reset flag will not be wiped if
	 * we know this is the first reset.
	 */
	if (IS_ENABLED(CONFIG_BOARD_RESET_AFTER_POWER_ON) &&
	    (flags & EC_RESET_FLAG_POWER_ON)) {
		if (flags & EC_RESET_FLAG_INITIAL_PWR) {
			/* Second boot, clear the flag immediately */
			chip_save_reset_flags(0);
		} else {
			/*
			 * First boot, Keep current flags and set INITIAL_PWR
			 * flag. EC reset should happen soon.
			 *
			 * It's possible that H1 never trigger EC reset, or
			 * reset happens before this line. Both cases should be
			 * fine because we will have the correct flag anyway.
			 */
			chip_save_reset_flags(chip_read_reset_flags() |
					      EC_RESET_FLAG_INITIAL_PWR);

			/*
			 * Schedule chip_save_reset_flags(0) later.
			 * Wait until end of HOOK_INIT should be long enough.
			 */
			delayed_clear_reset_flags = 1;
		}
	} else {
		/* Clear saved reset flags. */
		chip_save_reset_flags(0);
	}

	system_set_reset_flags(flags);

	/* Clear PD contract recorded in bram if this is a power-on reset. */
	if (IS_ENABLED(CONFIG_IT83XX_RESET_PD_CONTRACT_IN_BRAM) &&
	    (flags == (EC_RESET_FLAG_POWER_ON | EC_RESET_FLAG_RESET_PIN))) {
		for (int i = 0; i < MAX_SYSTEM_BBRAM_IDX_PD_PORTS; i++)
			system_set_bbram((SYSTEM_BBRAM_IDX_PD0 + i), 0);
	}

	if ((IS_ENABLED(CONFIG_IT83XX_HARD_RESET_BY_GPG1)) &&
	    (flags & ~(EC_RESET_FLAG_POWER_ON | EC_RESET_FLAG_RESET_PIN)))
		system_restore_panic_data_from_bram();
}

static void system_reset_cause_is_unknown(void)
{
	/* No reset cause and not sysjump. */
	if (!system_get_reset_flags() && !system_jumped_to_this_image())
		/*
		 * We decrease 4 or 2 for "ec_reset_lp" here, that depend on
		 * which jump and link instruction has executed.
		 * eg: Andes core (jral5: LP=PC+2, jal: LP=PC+4)
		 */
		ccprintf("===Unknown reset! jump from %x or %x===\n",
			 ec_reset_lp - 4, ec_reset_lp - 2);
}
DECLARE_HOOK(HOOK_INIT, system_reset_cause_is_unknown, HOOK_PRIO_FIRST);

int system_is_reboot_warm(void)
{
	uint32_t reset_flags;
	/*
	 * Check reset cause here,
	 * gpio_pre_init is executed faster than system_pre_init
	 */
	check_reset_cause();
	reset_flags = system_get_reset_flags();

	if ((reset_flags & EC_RESET_FLAG_RESET_PIN) ||
	    (reset_flags & EC_RESET_FLAG_POWER_ON) ||
	    (reset_flags & EC_RESET_FLAG_WATCHDOG) ||
	    (reset_flags & EC_RESET_FLAG_HARD) ||
	    (reset_flags & EC_RESET_FLAG_SOFT) ||
	    (reset_flags & EC_RESET_FLAG_HIBERNATE))
		return 0;
	else
		return 1;
}

void chip_pre_init(void)
{
	/* bit1=0: disable pre-defined command */
	IT83XX_SMB_SFFCTL &= ~IT83XX_SMB_HSAPE;

	/* bit0, EC received the special waveform from iteflash */
	if (IT83XX_GCTRL_DBGROS & IT83XX_SMB_DBGR) {
		/*
		 * Wait ~200ms, so iteflash will have enough time to let
		 * EC enter follow mode. And once EC goes into follow mode, EC
		 * will be stayed here (no following sequences, eg:
		 * enable watchdog/write protect/power-on sequence...) until
		 * we reset it.
		 */
		for (int i = 0; i < (200 * MSEC / 15); i++)
			/* delay ~15.25us */
			IT83XX_GCTRL_WNCKR = 0;
	}

	if (IS_ENABLED(IT83XX_ETWD_HW_RESET_SUPPORT))
		/* System triggers a soft reset by default (command: reboot). */
		IT83XX_GCTRL_ETWDUARTCR &= ~ETWD_HW_RST_EN;

	if (IS_ENABLED(IT83XX_RISCV_WAKEUP_CPU_WITHOUT_INT_ENABLED))
		/*
		 * bit7: wake up CPU if it is in low power mode and
		 * an interrupt is pending.
		 */
		IT83XX_GCTRL_WMCR |= BIT(7);
}

#define BRAM_VALID_MAGIC 0x4252414D /* "BRAM" */
#define BRAM_VALID_MAGIC_FIELD0 (BRAM_VALID_MAGIC & 0xff)
#define BRAM_VALID_MAGIC_FIELD1 ((BRAM_VALID_MAGIC >> 8) & 0xff)
#define BRAM_VALID_MAGIC_FIELD2 ((BRAM_VALID_MAGIC >> 16) & 0xff)
#define BRAM_VALID_MAGIC_FIELD3 ((BRAM_VALID_MAGIC >> 24) & 0xff)
void chip_bram_valid(void)
{
	int i;

	if ((BRAM_VALID_FLAGS0 != BRAM_VALID_MAGIC_FIELD0) ||
	    (BRAM_VALID_FLAGS1 != BRAM_VALID_MAGIC_FIELD1) ||
	    (BRAM_VALID_FLAGS2 != BRAM_VALID_MAGIC_FIELD2) ||
	    (BRAM_VALID_FLAGS3 != BRAM_VALID_MAGIC_FIELD3)) {
		/*
		 * Magic does not match, so BRAM must be uninitialized. Clear
		 * entire Bank0 BRAM, and set magic value.
		 */
		for (i = 0; i < BRAM_IDX_VALID_FLAGS0; i++)
			IT83XX_BRAM_BANK0(i) = 0;

		BRAM_VALID_FLAGS0 = BRAM_VALID_MAGIC_FIELD0;
		BRAM_VALID_FLAGS1 = BRAM_VALID_MAGIC_FIELD1;
		BRAM_VALID_FLAGS2 = BRAM_VALID_MAGIC_FIELD2;
		BRAM_VALID_FLAGS3 = BRAM_VALID_MAGIC_FIELD3;
	}

#if defined(CONFIG_PRESERVE_LOGS) && defined(CONFIG_IT83XX_HARD_RESET_BY_GPG1)
	if (BRAM_EC_LOG_STATUS == EC_LOG_SAVED_IN_FLASH) {
		/* Restore EC logs from flash. */
		memcpy((void *)__preserved_logs_start,
		       (const void *)CHIP_FLASH_PRESERVE_LOGS_BASE,
		       (uintptr_t)__preserved_logs_size);
	}
	BRAM_EC_LOG_STATUS = 0;
#endif
}

void system_pre_init(void)
{
	/* No initialization required */
}

uint32_t chip_read_reset_flags(void)
{
	uint32_t flags = 0;
	flags |= BRAM_RESET_FLAGS0 << 24;
	flags |= BRAM_RESET_FLAGS1 << 16;
	flags |= BRAM_RESET_FLAGS2 << 8;
	flags |= BRAM_RESET_FLAGS3;
	return flags;
}

void chip_save_reset_flags(uint32_t save_flags)
{
	BRAM_RESET_FLAGS0 = save_flags >> 24;
	BRAM_RESET_FLAGS1 = (save_flags >> 16) & 0xff;
	BRAM_RESET_FLAGS2 = (save_flags >> 8) & 0xff;
	BRAM_RESET_FLAGS3 = save_flags & 0xff;
}

void system_reset(int flags)
{
	uint32_t save_flags = 0;

	/* We never get this warning message in normal case. */
	if (IT83XX_GCTRL_DBGROS & IT83XX_SMB_DBGR) {
		ccprintf("!Reset will be failed due to EC is in debug mode!\n");
		cflush();
	}

#if defined(CONFIG_PRESERVE_LOGS) && defined(CONFIG_IT83XX_HARD_RESET_BY_GPG1)
	/* Saving EC logs into flash before reset. */
	crec_flash_physical_erase(CHIP_FLASH_PRESERVE_LOGS_BASE,
				  CHIP_FLASH_PRESERVE_LOGS_SIZE);
	crec_flash_physical_write(CHIP_FLASH_PRESERVE_LOGS_BASE,
				  (uintptr_t)__preserved_logs_size,
				  __preserved_logs_start);
	BRAM_EC_LOG_STATUS = EC_LOG_SAVED_IN_FLASH;
#endif

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

	/* Handle saving common reset flags. */
	system_encode_save_flags(flags, &save_flags);

	if (clock_ec_wake_from_sleep())
		save_flags |= EC_RESET_FLAG_HIBERNATE;

	/* 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);
		}
	}

	/* bit0: enable watchdog hardware reset. */
#ifdef IT83XX_ETWD_HW_RESET_SUPPORT
	if (flags & SYSTEM_RESET_HARD)
		IT83XX_GCTRL_ETWDUARTCR |= ETWD_HW_RST_EN;
#endif
	/* Set GPG1 as output high and wait until EC reset. */
	if (IS_ENABLED(CONFIG_IT83XX_HARD_RESET_BY_GPG1))
		system_reset_ec_by_gpg1();

	/*
	 * Writing invalid key to watchdog module triggers a soft or hardware
	 * reset. It depends on the setting of bit0 at ETWDUARTCR register.
	 */
	IT83XX_ETWD_ETWCFG |= 0x20;
	IT83XX_ETWD_EWDKEYR = 0x00;

	/* Spin and wait for reboot; should never return */
	while (1)
		;
}

int system_set_scratchpad(uint32_t value)
{
	BRAM_SCRATCHPAD3 = (value >> 24) & 0xff;
	BRAM_SCRATCHPAD2 = (value >> 16) & 0xff;
	BRAM_SCRATCHPAD1 = (value >> 8) & 0xff;
	BRAM_SCRATCHPAD0 = value & 0xff;

	return EC_SUCCESS;
}

int system_get_scratchpad(uint32_t *value)
{
	*value = (BRAM_SCRATCHPAD3 << 24) | (BRAM_SCRATCHPAD2 << 16) |
		 (BRAM_SCRATCHPAD1 << 8) | (BRAM_SCRATCHPAD0);
	return EC_SUCCESS;
}

static uint32_t system_get_chip_id(void)
{
#ifdef IT83XX_CHIP_ID_3BYTES
	return (IT83XX_GCTRL_CHIPID1 << 16) | (IT83XX_GCTRL_CHIPID2 << 8) |
	       IT83XX_GCTRL_CHIPID3;
#else
	return (IT83XX_GCTRL_CHIPID1 << 8) | IT83XX_GCTRL_CHIPID2;
#endif
}

static uint8_t system_get_chip_version(void)
{
	/* bit[3-0], chip version */
	return IT83XX_GCTRL_CHIPVER & 0x0F;
}

static char to_hex(int x)
{
	if (x >= 0 && x <= 9)
		return '0' + x;
	return 'a' + x - 10;
}

const char *system_get_chip_vendor(void)
{
	return "ite";
}

const char *system_get_chip_name(void)
{
	static char buf[8] = { 'i', 't' };
	int num = (IS_ENABLED(IT83XX_CHIP_ID_3BYTES) ? 4 : 3);
	uint32_t chip_id = system_get_chip_id();

	for (int n = 2; num >= 0; n++, num--)
		buf[n] = to_hex(chip_id >> (num * 4) & 0xF);

	return buf;
}

const char *system_get_chip_revision(void)
{
	static char buf[3];
	uint8_t rev = system_get_chip_version();

	buf[0] = to_hex(rev + 0xa);
	buf[1] = 'x';
	buf[2] = '\0';
	return buf;
}

static int bram_idx_lookup(enum system_bbram_idx idx)
{
	if (idx == SYSTEM_BBRAM_IDX_PD0)
		return BRAM_IDX_PD0;
	if (idx == SYSTEM_BBRAM_IDX_PD1)
		return BRAM_IDX_PD1;
	if (idx == SYSTEM_BBRAM_IDX_PD2)
		return BRAM_IDX_PD2;
	return -1;
}

int system_get_bbram(enum system_bbram_idx idx, uint8_t *value)
{
	int bram_idx = bram_idx_lookup(idx);

	if (bram_idx < 0)
		return EC_ERROR_INVAL;

	*value = IT83XX_BRAM_BANK0(bram_idx);
	return EC_SUCCESS;
}

int system_set_bbram(enum system_bbram_idx idx, uint8_t value)
{
	int bram_idx = bram_idx_lookup(idx);

	if (bram_idx < 0)
		return EC_ERROR_INVAL;

	IT83XX_BRAM_BANK0(bram_idx) = value;
	return EC_SUCCESS;
}

uintptr_t system_get_fw_reset_vector(uintptr_t base)
{
	/*
	 * Because our reset vector is at the beginning of image copy
	 * (see init.S). So I just need to return 'base' here and EC will jump
	 * to the reset vector.
	 */
	return base;
}