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/dt.h>
10 #include <kernel/dt_driver.h>
11 #include <kernel/spinlock.h>
12 #include <libfdt.h>
13 #include <stdint.h>
14 
15 /* Global reset controller access lock */
16 
rstctrl_get_exclusive(struct rstctrl * rstctrl)17 TEE_Result rstctrl_get_exclusive(struct rstctrl *rstctrl)
18 {
19 	uint32_t exceptions = 0;
20 	TEE_Result res = TEE_ERROR_ACCESS_CONFLICT;
21 	static unsigned int rstctrl_lock = SPINLOCK_UNLOCK;
22 
23 	exceptions = cpu_spin_lock_xsave(&rstctrl_lock);
24 
25 	if (!rstctrl->exclusive) {
26 		rstctrl->exclusive = true;
27 		res = TEE_SUCCESS;
28 	}
29 
30 	cpu_spin_unlock_xrestore(&rstctrl_lock, exceptions);
31 
32 	return res;
33 }
34 
rstctrl_put_exclusive(struct rstctrl * rstctrl)35 void rstctrl_put_exclusive(struct rstctrl *rstctrl)
36 {
37 	assert(rstctrl->exclusive);
38 
39 	WRITE_ONCE(rstctrl->exclusive, false);
40 }
41 
rstctrl_dt_get_by_name(const void * fdt,int nodeoffset,const char * name,struct rstctrl ** rstctrl)42 TEE_Result rstctrl_dt_get_by_name(const void *fdt, int nodeoffset,
43 				  const char *name, struct rstctrl **rstctrl)
44 {
45 	int index = 0;
46 
47 	index = fdt_stringlist_search(fdt, nodeoffset, "reset-names", name);
48 	if (index < 0)
49 		return TEE_ERROR_ITEM_NOT_FOUND;
50 
51 	return rstctrl_dt_get_by_index(fdt, nodeoffset, index, rstctrl);
52 }
53