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 <kernel/dt_driver.h> 10 #include <matrix.h> 11 #include <sama5d2.h> 12 #include <tee_api_defines.h> 13 #include <tee_api_types.h> 14 #include <types_ext.h> 15 16 #define AT91_RSTC_CR 0x0 17 #define AT91_RSTC_CR_KEY SHIFT_U32(0xA5, 24) 18 #define AT91_RSTC_CR_PROCRST BIT32(0) 19 #define AT91_RSTC_CR_PERRST BIT32(2) 20 21 static vaddr_t rstc_base; 22 23 bool atmel_rstc_available(void) 24 { 25 return rstc_base != 0; 26 } 27 28 void __noreturn atmel_rstc_reset(void) 29 { 30 uint32_t val = AT91_RSTC_CR_KEY | AT91_RSTC_CR_PROCRST | 31 AT91_RSTC_CR_PERRST; 32 33 io_write32(rstc_base + AT91_RSTC_CR, val); 34 35 /* 36 * After the previous write, the CPU will reset so we will never hit 37 * this loop. 38 */ 39 while (true) 40 ; 41 } 42 43 /* Non-null reference for compat data */ 44 static const uint8_t rstc_always_secure; 45 46 static TEE_Result atmel_rstc_probe(const void *fdt, int node, 47 const void *compat_data) 48 49 { 50 size_t size = 0; 51 52 if (fdt_get_status(fdt, node) != DT_STATUS_OK_SEC) 53 return TEE_ERROR_BAD_PARAMETERS; 54 55 if (compat_data != &rstc_always_secure) 56 matrix_configure_periph_secure(AT91C_ID_SYS); 57 58 if (dt_map_dev(fdt, node, &rstc_base, &size, DT_MAP_AUTO) < 0) 59 return TEE_ERROR_GENERIC; 60 61 return TEE_SUCCESS; 62 } 63 64 static const struct dt_device_match atmel_rstc_match_table[] = { 65 { .compatible = "atmel,sama5d3-rstc" }, 66 { 67 .compatible = "microchip,sama7g5-rstc", 68 .compat_data = &rstc_always_secure, 69 }, 70 { } 71 }; 72 73 DEFINE_DT_DRIVER(atmel_rstc_dt_driver) = { 74 .name = "atmel_rstc", 75 .type = DT_DRIVER_NOTYPE, 76 .match_table = atmel_rstc_match_table, 77 .probe = atmel_rstc_probe, 78 }; 79