1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2021, Microchip
4  */
5 
6 #include <drivers/atmel_rstc.h>
7 #include <io.h>
8 #include <kernel/dt.h>
9 #include <matrix.h>
10 #include <sama5d2.h>
11 #include <tee_api_defines.h>
12 #include <tee_api_types.h>
13 #include <types_ext.h>
14 
15 #define AT91_RSTC_CR		0x0
16 #define AT91_RSTC_CR_KEY	SHIFT_U32(0xA5, 24)
17 #define AT91_RSTC_CR_PROCRST	BIT32(0)
18 #define AT91_RSTC_CR_PERRST	BIT32(2)
19 
20 static vaddr_t rstc_base;
21 
atmel_rstc_available(void)22 bool atmel_rstc_available(void)
23 {
24 	return rstc_base != 0;
25 }
26 
atmel_rstc_reset(void)27 void __noreturn atmel_rstc_reset(void)
28 {
29 	uint32_t val = AT91_RSTC_CR_KEY | AT91_RSTC_CR_PROCRST |
30 		       AT91_RSTC_CR_PERRST;
31 
32 	io_write32(rstc_base + AT91_RSTC_CR, val);
33 
34 	/*
35 	 * After the previous write, the CPU will reset so we will never hit
36 	 * this loop.
37 	 */
38 	while (true)
39 		;
40 }
41 
atmel_rstc_probe(const void * fdt,int node,const void * compat_data __unused)42 static TEE_Result atmel_rstc_probe(const void *fdt, int node,
43 				   const void *compat_data __unused)
44 
45 {
46 	size_t size = 0;
47 
48 	if (_fdt_get_status(fdt, node) != DT_STATUS_OK_SEC)
49 		return TEE_ERROR_BAD_PARAMETERS;
50 
51 	matrix_configure_periph_secure(AT91C_ID_SYS);
52 
53 	if (dt_map_dev(fdt, node, &rstc_base, &size, DT_MAP_AUTO) < 0)
54 		return TEE_ERROR_GENERIC;
55 
56 	return TEE_SUCCESS;
57 }
58 
59 static const struct dt_device_match atmel_rstc_match_table[] = {
60 	{ .compatible = "atmel,sama5d3-rstc" },
61 	{ }
62 };
63 
64 DEFINE_DT_DRIVER(atmel_rstc_dt_driver) = {
65 	.name = "atmel_rstc",
66 	.type = DT_DRIVER_NOTYPE,
67 	.match_table = atmel_rstc_match_table,
68 	.probe = atmel_rstc_probe,
69 };
70