xref: /optee_os/core/drivers/rstctrl/rstctrl.c (revision 48952fd403d867dbf13675e062cd8a7d2e5260a9)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2021, Linaro Limited
4  */
5 
6 #include <assert.h>
7 #include <drivers/rstctrl.h>
8 #include <io.h>
9 #include <kernel/spinlock.h>
10 #include <libfdt.h>
11 #include <stdint.h>
12 
13 /* Global reset controller access lock */
14 
15 TEE_Result rstctrl_get_exclusive(struct rstctrl *rstctrl)
16 {
17 	uint32_t exceptions = 0;
18 	TEE_Result res = TEE_ERROR_ACCESS_CONFLICT;
19 	static unsigned int rstctrl_lock = SPINLOCK_UNLOCK;
20 
21 	exceptions = cpu_spin_lock_xsave(&rstctrl_lock);
22 
23 	if (!rstctrl->exclusive) {
24 		rstctrl->exclusive = true;
25 		res = TEE_SUCCESS;
26 	}
27 
28 	cpu_spin_unlock_xrestore(&rstctrl_lock, exceptions);
29 
30 	return res;
31 }
32 
33 void rstctrl_put_exclusive(struct rstctrl *rstctrl)
34 {
35 	assert(rstctrl->exclusive);
36 
37 	WRITE_ONCE(rstctrl->exclusive, false);
38 }
39 
40 TEE_Result rstctrl_dt_get_by_name(const void *fdt, int nodeoffset,
41 				  const char *name, struct rstctrl **rstctrl)
42 {
43 	int index = 0;
44 
45 	index = fdt_stringlist_search(fdt, nodeoffset, "reset-names", name);
46 	if (index < 0)
47 		return TEE_ERROR_ITEM_NOT_FOUND;
48 
49 	return rstctrl_dt_get_by_index(fdt, nodeoffset, index, rstctrl);
50 }
51