1/*
2 * Copyright (c) 2021 Henrik Brix Andersen <henrik@brixandersen.dk>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include <zephyr/toolchain.h>
8
9/* exports */
10GTEXT(__reset)
11
12/* imports */
13GTEXT(__initialize)
14
15SECTION_FUNC(reset, __reset)
16	/* Zerorize zero register */
17	lui x0, 0
18
19	/* Disable interrupts */
20	csrw mstatus, x0
21	csrw mie, x0
22
23#ifdef CONFIG_USERSPACE
24	/* Disable counter access outside M-mode */
25	csrw mcounteren, x0
26#endif /* CONFIG_USERSPACE */
27
28	/* Allow mcycle and minstret counters to increment */
29	li x11, ~2
30	csrw mcountinhibit, x11
31
32	/* Zerorize counters */
33	csrw mcycle, x0
34	csrw mcycleh, x0
35	csrw minstret, x0
36	csrw minstreth, x0
37
38	/*
39	 * Simplify dummy machine trap code by not having to decode
40	 * instruction width.
41	 */
42	.option push
43	.option norvc
44
45	/*
46	 * Temporarily setup a dummy machine trap vector to catch (and ignore)
47	 * Store Access faults due to unimplemented peripherals.
48	 */
49	csrr x6, mtvec
50	la x7, __dummy_trap_handler
51	csrw mtvec, x7
52
53	/* Attempt to zerorize all IO peripheral registers */
54	la x8, __io_start
55	la x9, __io_end
561:	sw x0, 0(x8)
57	addi x8, x8, 4
58	bne x8, x9, 1b
59
60	/* Restore previous machine trap vector */
61	csrw mtvec, x6
62
63	.option pop
64
65	/* Jump to __initialize */
66	call __initialize
67
68.balign 4
69SECTION_FUNC(reset, __dummy_trap_handler)
70	csrr x5, mepc
71	addi x5, x5, 4
72	csrw mepc, x5
73	mret
74