xref: /freebsd-14.2/sys/dev/extres/syscon/syscon.c (revision 052d3c12)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017 Kyle Evans <[email protected]>
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, 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 /*
30  * This is a generic syscon driver, whose purpose is to provide access to
31  * various unrelated bits packed in a single register space. It is usually used
32  * as a fallback to more specific driver, but works well enough for simple
33  * access.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 #include "opt_platform.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/bus.h>
43 #include <sys/kernel.h>
44 #include <sys/kobj.h>
45 #include <sys/lock.h>
46 #include <sys/module.h>
47 #include <sys/rman.h>
48 #include <sys/sx.h>
49 #include <sys/queue.h>
50 
51 #include <machine/bus.h>
52 
53 #ifdef FDT
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_bus_subr.h>
56 #endif
57 
58 #include "syscon_if.h"
59 #include "syscon.h"
60 
61 /*
62  * Syscon interface details
63  */
64 typedef TAILQ_HEAD(syscon_list, syscon) syscon_list_t;
65 
66 /*
67  * Declarations
68  */
69 static int syscon_method_init(struct syscon *syscon);
70 static int syscon_method_uninit(struct syscon *syscon);
71 
72 MALLOC_DEFINE(M_SYSCON, "syscon", "Syscon driver");
73 
74 static syscon_list_t syscon_list = TAILQ_HEAD_INITIALIZER(syscon_list);
75 static struct sx		syscon_topo_lock;
76 SX_SYSINIT(syscon_topology, &syscon_topo_lock, "Syscon topology lock");
77 
78 /*
79  * Syscon methods.
80  */
81 static syscon_method_t syscon_methods[] = {
82 	SYSCONMETHOD(syscon_init,	syscon_method_init),
83 	SYSCONMETHOD(syscon_uninit,	syscon_method_uninit),
84 
85 	SYSCONMETHOD_END
86 };
87 DEFINE_CLASS_0(syscon, syscon_class, syscon_methods, 0);
88 
89 #define SYSCON_TOPO_SLOCK()	sx_slock(&syscon_topo_lock)
90 #define SYSCON_TOPO_XLOCK()	sx_xlock(&syscon_topo_lock)
91 #define SYSCON_TOPO_UNLOCK()	sx_unlock(&syscon_topo_lock)
92 #define SYSCON_TOPO_ASSERT()	sx_assert(&syscon_topo_lock, SA_LOCKED)
93 #define SYSCON_TOPO_XASSERT()	sx_assert(&syscon_topo_lock, SA_XLOCKED)
94 
95 /*
96  * Default syscon methods for base class.
97  */
98 static int
99 syscon_method_init(struct syscon *syscon)
100 {
101 
102 	return (0);
103 };
104 
105 static int
106 syscon_method_uninit(struct syscon *syscon)
107 {
108 
109 	return (0);
110 };
111 
112 void *
113 syscon_get_softc(struct syscon *syscon)
114 {
115 
116 	return (syscon->softc);
117 };
118 
119 /*
120  * Create and initialize syscon object, but do not register it.
121  */
122 struct syscon *
123 syscon_create(device_t pdev, syscon_class_t syscon_class)
124 {
125 	struct syscon *syscon;
126 
127 	/* Create object and initialize it. */
128 	syscon = malloc(sizeof(struct syscon), M_SYSCON,
129 	    M_WAITOK | M_ZERO);
130 	kobj_init((kobj_t)syscon, (kobj_class_t)syscon_class);
131 
132 	/* Allocate softc if required. */
133 	if (syscon_class->size > 0)
134 		syscon->softc = malloc(syscon_class->size, M_SYSCON,
135 		    M_WAITOK | M_ZERO);
136 
137 	/* Rest of init. */
138 	syscon->pdev = pdev;
139 	return (syscon);
140 }
141 
142 /* Register syscon object. */
143 struct syscon *
144 syscon_register(struct syscon *syscon)
145 {
146 	int rv;
147 
148 #ifdef FDT
149 	if (syscon->ofw_node <= 0)
150 		syscon->ofw_node = ofw_bus_get_node(syscon->pdev);
151 	if (syscon->ofw_node <= 0)
152 		return (NULL);
153 #endif
154 
155 	rv = SYSCON_INIT(syscon);
156 	if (rv != 0) {
157 		printf("SYSCON_INIT failed: %d\n", rv);
158 		return (NULL);
159 	}
160 
161 #ifdef FDT
162 	OF_device_register_xref(OF_xref_from_node(syscon->ofw_node),
163 	    syscon->pdev);
164 #endif
165 	SYSCON_TOPO_XLOCK();
166 	TAILQ_INSERT_TAIL(&syscon_list, syscon, syscon_link);
167 	SYSCON_TOPO_UNLOCK();
168 	return (syscon);
169 }
170 
171 int
172 syscon_unregister(struct syscon *syscon)
173 {
174 
175 	SYSCON_TOPO_XLOCK();
176 	TAILQ_REMOVE(&syscon_list, syscon, syscon_link);
177 	SYSCON_TOPO_UNLOCK();
178 #ifdef FDT
179 	OF_device_register_xref(OF_xref_from_node(syscon->ofw_node), NULL);
180 #endif
181 	return (SYSCON_UNINIT(syscon));
182 }
183 
184 /**
185  * Provider methods
186  */
187 #ifdef FDT
188 static struct syscon *
189 syscon_find_by_ofw_node(phandle_t node)
190 {
191 	struct syscon *entry;
192 
193 	SYSCON_TOPO_ASSERT();
194 
195 	TAILQ_FOREACH(entry, &syscon_list, syscon_link) {
196 		if (entry->ofw_node == node)
197 			return (entry);
198 	}
199 
200 	return (NULL);
201 }
202 
203 struct syscon *
204 syscon_create_ofw_node(device_t pdev, syscon_class_t syscon_class,
205     phandle_t node)
206 {
207 	struct syscon *syscon;
208 
209 	syscon = syscon_create(pdev, syscon_class);
210 	if (syscon == NULL)
211 		return (NULL);
212 	syscon->ofw_node = node;
213 	if (syscon_register(syscon) == NULL)
214 		return (NULL);
215 	return (syscon);
216 }
217 
218 phandle_t
219 syscon_get_ofw_node(struct syscon *syscon)
220 {
221 
222 	return (syscon->ofw_node);
223 }
224 
225 int
226 syscon_get_by_ofw_property(device_t cdev, phandle_t cnode, char *name,
227     struct syscon **syscon)
228 {
229 	pcell_t *cells;
230 	int ncells;
231 
232 	if (cnode <= 0)
233 		cnode = ofw_bus_get_node(cdev);
234 	if (cnode <= 0) {
235 		device_printf(cdev,
236 		    "%s called on not ofw based device\n", __func__);
237 		return (ENXIO);
238 	}
239 	ncells = OF_getencprop_alloc(cnode, name, sizeof(pcell_t),
240 	    (void **)&cells);
241 	if (ncells < 1)
242 		return (ENXIO);
243 
244 	/* Translate to syscon node. */
245 	SYSCON_TOPO_SLOCK();
246 	*syscon = syscon_find_by_ofw_node(OF_node_from_xref(cells[0]));
247 	if (*syscon == NULL) {
248 		SYSCON_TOPO_UNLOCK();
249 		device_printf(cdev, "Failed to find syscon node\n");
250 		OF_prop_free(cells);
251 		return (ENODEV);
252 	}
253 	SYSCON_TOPO_UNLOCK();
254 	OF_prop_free(cells);
255 	return (0);
256 }
257 #endif
258