xref: /freebsd/sys/dev/syscon/syscon.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2017 Kyle Evans <kevans@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * This is a generic syscon driver, whose purpose is to provide access to
30  * various unrelated bits packed in a single register space. It is usually used
31  * as a fallback to more specific driver, but works well enough for simple
32  * access.
33  */
34 
35 #include <sys/cdefs.h>
36 #include "opt_platform.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/kobj.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/rman.h>
47 #include <sys/sx.h>
48 #include <sys/queue.h>
49 
50 #include <machine/bus.h>
51 
52 #ifdef FDT
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55 #endif
56 
57 #include "syscon_if.h"
58 #include "syscon.h"
59 
60 /*
61  * Syscon interface details
62  */
63 typedef TAILQ_HEAD(syscon_list, syscon) syscon_list_t;
64 
65 /*
66  * Declarations
67  */
68 static int syscon_method_init(struct syscon *syscon);
69 static int syscon_method_uninit(struct syscon *syscon);
70 static uint32_t syscon_method_read_4(struct syscon *syscon, bus_size_t offset);
71 static int syscon_method_write_4(struct syscon *syscon, bus_size_t offset,
72     uint32_t val);
73 static int syscon_method_modify_4(struct syscon *syscon, bus_size_t offset,
74     uint32_t clear_bits, uint32_t set_bits);
75 
76 
77 MALLOC_DEFINE(M_SYSCON, "syscon", "Syscon driver");
78 
79 static syscon_list_t syscon_list = TAILQ_HEAD_INITIALIZER(syscon_list);
80 static struct sx		syscon_topo_lock;
81 SX_SYSINIT(syscon_topology, &syscon_topo_lock, "Syscon topology lock");
82 
83 /*
84  * Syscon methods.
85  */
86 static syscon_method_t syscon_methods[] = {
87 	SYSCONMETHOD(syscon_init,	syscon_method_init),
88 	SYSCONMETHOD(syscon_uninit,	syscon_method_uninit),
89 	SYSCONMETHOD(syscon_read_4,	syscon_method_read_4),
90 	SYSCONMETHOD(syscon_write_4,	syscon_method_write_4),
91 	SYSCONMETHOD(syscon_modify_4,	syscon_method_modify_4),
92 
93 	SYSCONMETHOD_END
94 };
95 DEFINE_CLASS_0(syscon, syscon_class, syscon_methods, 0);
96 
97 #define SYSCON_TOPO_SLOCK()	sx_slock(&syscon_topo_lock)
98 #define SYSCON_TOPO_XLOCK()	sx_xlock(&syscon_topo_lock)
99 #define SYSCON_TOPO_UNLOCK()	sx_unlock(&syscon_topo_lock)
100 #define SYSCON_TOPO_ASSERT()	sx_assert(&syscon_topo_lock, SA_LOCKED)
101 #define SYSCON_TOPO_XASSERT()	sx_assert(&syscon_topo_lock, SA_XLOCKED)
102 
103 /*
104  * Default syscon methods for base class.
105  */
106 static int
107 syscon_method_init(struct syscon *syscon)
108 {
109 
110 	return (0);
111 };
112 
113 static int
114 syscon_method_uninit(struct syscon *syscon)
115 {
116 
117 	return (0);
118 };
119 
120 void *
121 syscon_get_softc(struct syscon *syscon)
122 {
123 
124 	return (syscon->softc);
125 };
126 
127 static uint32_t
128 syscon_method_read_4(struct syscon *syscon, bus_size_t offset)
129 {
130 	uint32_t val;
131 
132 	SYSCON_DEVICE_LOCK(syscon->pdev);
133 	val = SYSCON_UNLOCKED_READ_4(syscon, offset);
134 	SYSCON_DEVICE_UNLOCK(syscon->pdev);
135 	return(val);
136 }
137 
138 static int
139 syscon_method_write_4(struct syscon *syscon, bus_size_t offset, uint32_t val)
140 {
141 	int	rv;
142 
143 	SYSCON_DEVICE_LOCK(syscon->pdev);
144 	rv = SYSCON_UNLOCKED_WRITE_4(syscon, offset, val);
145 	SYSCON_DEVICE_UNLOCK(syscon->pdev);
146 	return(rv);
147 }
148 
149 static int
150 syscon_method_modify_4(struct syscon *syscon, bus_size_t offset,
151     uint32_t clear_bits, uint32_t set_bits)
152 {
153 	int	rv;
154 
155 	SYSCON_DEVICE_LOCK(syscon->pdev);
156 	rv = SYSCON_UNLOCKED_MODIFY_4(syscon, offset, clear_bits, set_bits);
157 	SYSCON_DEVICE_UNLOCK(syscon->pdev);
158 	return(rv);
159 }
160 /*
161  * Create and initialize syscon object, but do not register it.
162  */
163 struct syscon *
164 syscon_create(device_t pdev, syscon_class_t syscon_class)
165 {
166 	struct syscon *syscon;
167 
168 	/* Create object and initialize it. */
169 	syscon = malloc(sizeof(struct syscon), M_SYSCON,
170 	    M_WAITOK | M_ZERO);
171 	kobj_init((kobj_t)syscon, (kobj_class_t)syscon_class);
172 
173 	/* Allocate softc if required. */
174 	if (syscon_class->size > 0)
175 		syscon->softc = malloc(syscon_class->size, M_SYSCON,
176 		    M_WAITOK | M_ZERO);
177 
178 	/* Rest of init. */
179 	syscon->pdev = pdev;
180 	return (syscon);
181 }
182 
183 /* Register syscon object. */
184 struct syscon *
185 syscon_register(struct syscon *syscon)
186 {
187 	int rv;
188 
189 #ifdef FDT
190 	if (syscon->ofw_node <= 0)
191 		syscon->ofw_node = ofw_bus_get_node(syscon->pdev);
192 	if (syscon->ofw_node <= 0)
193 		return (NULL);
194 #endif
195 
196 	rv = SYSCON_INIT(syscon);
197 	if (rv != 0) {
198 		printf("SYSCON_INIT failed: %d\n", rv);
199 		return (NULL);
200 	}
201 
202 #ifdef FDT
203 	OF_device_register_xref(OF_xref_from_node(syscon->ofw_node),
204 	    syscon->pdev);
205 #endif
206 	SYSCON_TOPO_XLOCK();
207 	TAILQ_INSERT_TAIL(&syscon_list, syscon, syscon_link);
208 	SYSCON_TOPO_UNLOCK();
209 	return (syscon);
210 }
211 
212 int
213 syscon_unregister(struct syscon *syscon)
214 {
215 
216 	SYSCON_TOPO_XLOCK();
217 	TAILQ_REMOVE(&syscon_list, syscon, syscon_link);
218 	SYSCON_TOPO_UNLOCK();
219 #ifdef FDT
220 	OF_device_register_xref(OF_xref_from_node(syscon->ofw_node), NULL);
221 #endif
222 	return (SYSCON_UNINIT(syscon));
223 }
224 
225 /**
226  * Provider methods
227  */
228 #ifdef FDT
229 static struct syscon *
230 syscon_find_by_ofw_node(phandle_t node)
231 {
232 	struct syscon *entry;
233 
234 	SYSCON_TOPO_ASSERT();
235 
236 	TAILQ_FOREACH(entry, &syscon_list, syscon_link) {
237 		if (entry->ofw_node == node)
238 			return (entry);
239 	}
240 
241 	return (NULL);
242 }
243 
244 struct syscon *
245 syscon_create_ofw_node(device_t pdev, syscon_class_t syscon_class,
246     phandle_t node)
247 {
248 	struct syscon *syscon;
249 
250 	syscon = syscon_create(pdev, syscon_class);
251 	if (syscon == NULL)
252 		return (NULL);
253 	syscon->ofw_node = node;
254 	if (syscon_register(syscon) == NULL)
255 		return (NULL);
256 	return (syscon);
257 }
258 
259 phandle_t
260 syscon_get_ofw_node(struct syscon *syscon)
261 {
262 
263 	return (syscon->ofw_node);
264 }
265 
266 int
267 syscon_get_by_ofw_node(device_t cdev, phandle_t node, struct syscon **syscon)
268 {
269 
270 	SYSCON_TOPO_SLOCK();
271 	*syscon = syscon_find_by_ofw_node(node);
272 	if (*syscon == NULL) {
273 		SYSCON_TOPO_UNLOCK();
274 		device_printf(cdev, "Failed to find syscon node\n");
275 		return (ENODEV);
276 	}
277 	SYSCON_TOPO_UNLOCK();
278 	return (0);
279 }
280 
281 int
282 syscon_get_by_ofw_property(device_t cdev, phandle_t cnode, char *name,
283     struct syscon **syscon)
284 {
285 	pcell_t *cells;
286 	int ncells;
287 
288 	if (cnode <= 0)
289 		cnode = ofw_bus_get_node(cdev);
290 	if (cnode <= 0) {
291 		device_printf(cdev,
292 		    "%s called on not ofw based device\n", __func__);
293 		return (ENXIO);
294 	}
295 	ncells = OF_getencprop_alloc_multi(cnode, name, sizeof(pcell_t),
296 	    (void **)&cells);
297 	if (ncells < 1)
298 		return (ENOENT);
299 
300 	/* Translate to syscon node. */
301 	SYSCON_TOPO_SLOCK();
302 	*syscon = syscon_find_by_ofw_node(OF_node_from_xref(cells[0]));
303 	if (*syscon == NULL) {
304 		SYSCON_TOPO_UNLOCK();
305 		device_printf(cdev, "Failed to find syscon node\n");
306 		OF_prop_free(cells);
307 		return (ENODEV);
308 	}
309 	SYSCON_TOPO_UNLOCK();
310 	OF_prop_free(cells);
311 	return (0);
312 }
313 #endif
314