xref: /netbsd-src/sys/dev/fdt/fdt_syscon.c (revision 5375fcbde97192f15f1ba8bd7484c1f1eda93a58)
1 /* $NetBSD: fdt_syscon.c,v 1.3 2018/06/30 20:16:56 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fdt_syscon.c,v 1.3 2018/06/30 20:16:56 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kmem.h>
35 #include <sys/queue.h>
36 
37 #include <libfdt.h>
38 #include <dev/fdt/fdtvar.h>
39 
40 struct syscon;
41 
42 struct fdtbus_syscon {
43 	device_t sc_dev;
44 	int sc_phandle;
45 	struct syscon *sc_syscon;
46 
47 	LIST_ENTRY(fdtbus_syscon) sc_next;
48 };
49 
50 static LIST_HEAD(, fdtbus_syscon) fdtbus_syscons =
51     LIST_HEAD_INITIALIZER(fdtbus_syscons);
52 
53 int
fdtbus_register_syscon(device_t dev,int phandle,struct syscon * syscon)54 fdtbus_register_syscon(device_t dev, int phandle,
55     struct syscon *syscon)
56 {
57 	struct fdtbus_syscon *sc;
58 
59 	sc = kmem_alloc(sizeof(*sc), KM_SLEEP);
60 	sc->sc_dev = dev;
61 	sc->sc_phandle = phandle;
62 	sc->sc_syscon = syscon;
63 
64 	LIST_INSERT_HEAD(&fdtbus_syscons, sc, sc_next);
65 
66 	return 0;
67 }
68 
69 static struct fdtbus_syscon *
fdtbus_get_syscon(int phandle)70 fdtbus_get_syscon(int phandle)
71 {
72 	struct fdtbus_syscon *sc;
73 
74 	LIST_FOREACH(sc, &fdtbus_syscons, sc_next) {
75 		if (sc->sc_phandle == phandle)
76 			return sc;
77 	}
78 
79 	return NULL;
80 }
81 
82 struct syscon *
fdtbus_syscon_acquire(int phandle,const char * prop)83 fdtbus_syscon_acquire(int phandle, const char *prop)
84 {
85 	struct fdtbus_syscon *sc;
86 	int sc_phandle;
87 
88 	sc_phandle = fdtbus_get_phandle(phandle, prop);
89 	if (sc_phandle < 0)
90 		return NULL;
91 
92 	sc = fdtbus_get_syscon(sc_phandle);
93 	if (sc == NULL)
94 		return NULL;
95 
96 	return sc->sc_syscon;
97 }
98 
99 struct syscon *
fdtbus_syscon_lookup(int phandle)100 fdtbus_syscon_lookup(int phandle)
101 {
102 	struct fdtbus_syscon *sc;
103 
104 	sc = fdtbus_get_syscon(phandle);
105 	if (sc == NULL)
106 		return NULL;
107 
108 	return sc->sc_syscon;
109 }
110