2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Kyle Evans <kevans@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 #include "opt_platform.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/kernel.h>
46 #include <sys/module.h>
49 #include <sys/queue.h>
51 #include <machine/bus.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_bus_subr.h>
58 #include "syscon_if.h"
62 * Syscon interface details
64 typedef TAILQ_HEAD(syscon_list, syscon) syscon_list_t;
69 static int syscon_method_init(struct syscon *syscon);
70 static int syscon_method_uninit(struct syscon *syscon);
72 MALLOC_DEFINE(M_SYSCON, "syscon", "Syscon driver");
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");
81 static syscon_method_t syscon_methods[] = {
82 SYSCONMETHOD(syscon_init, syscon_method_init),
83 SYSCONMETHOD(syscon_uninit, syscon_method_uninit),
87 DEFINE_CLASS_0(syscon, syscon_class, syscon_methods, 0);
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)
96 * Default syscon methods for base class.
99 syscon_method_init(struct syscon *syscon)
106 syscon_method_uninit(struct syscon *syscon)
113 syscon_get_softc(struct syscon *syscon)
116 return (syscon->softc);
120 * Create and initialize syscon object, but do not register it.
123 syscon_create(device_t pdev, syscon_class_t syscon_class)
125 struct syscon *syscon;
127 /* Create object and initialize it. */
128 syscon = malloc(sizeof(struct syscon), M_SYSCON,
130 kobj_init((kobj_t)syscon, (kobj_class_t)syscon_class);
132 /* Allocate softc if required. */
133 if (syscon_class->size > 0)
134 syscon->softc = malloc(syscon_class->size, M_SYSCON,
142 /* Register syscon object. */
144 syscon_register(struct syscon *syscon)
149 if (syscon->ofw_node <= 0)
150 syscon->ofw_node = ofw_bus_get_node(syscon->pdev);
151 if (syscon->ofw_node <= 0)
155 rv = SYSCON_INIT(syscon);
157 printf("SYSCON_INIT failed: %d\n", rv);
162 OF_device_register_xref(OF_xref_from_node(syscon->ofw_node),
166 TAILQ_INSERT_TAIL(&syscon_list, syscon, syscon_link);
167 SYSCON_TOPO_UNLOCK();
172 syscon_unregister(struct syscon *syscon)
176 TAILQ_REMOVE(&syscon_list, syscon, syscon_link);
177 SYSCON_TOPO_UNLOCK();
179 OF_device_register_xref(OF_xref_from_node(syscon->ofw_node), NULL);
181 return (SYSCON_UNINIT(syscon));
188 static struct syscon *
189 syscon_find_by_ofw_node(phandle_t node)
191 struct syscon *entry;
193 SYSCON_TOPO_ASSERT();
195 TAILQ_FOREACH(entry, &syscon_list, syscon_link) {
196 if (entry->ofw_node == node)
204 syscon_create_ofw_node(device_t pdev, syscon_class_t syscon_class,
207 struct syscon *syscon;
209 syscon = syscon_create(pdev, syscon_class);
212 syscon->ofw_node = node;
213 if (syscon_register(syscon) == NULL)
219 syscon_get_ofw_node(struct syscon *syscon)
222 return (syscon->ofw_node);
226 syscon_get_by_ofw_property(device_t cdev, phandle_t cnode, char *name,
227 struct syscon **syscon)
233 cnode = ofw_bus_get_node(cdev);
236 "%s called on not ofw based device\n", __func__);
239 ncells = OF_getencprop_alloc_multi(cnode, name, sizeof(pcell_t),
244 /* Translate to syscon node. */
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");
253 SYSCON_TOPO_UNLOCK();