2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Kyle Evans <kevans@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 #include "opt_platform.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/kernel.h>
45 #include <sys/module.h>
48 #include <sys/queue.h>
50 #include <machine/bus.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
57 #include "syscon_if.h"
61 * Syscon interface details
63 typedef TAILQ_HEAD(syscon_list, syscon) syscon_list_t;
68 static int syscon_method_init(struct syscon *syscon);
69 static int syscon_method_uninit(struct syscon *syscon);
71 MALLOC_DEFINE(M_SYSCON, "syscon", "Syscon driver");
73 static syscon_list_t syscon_list = TAILQ_HEAD_INITIALIZER(syscon_list);
74 static struct sx syscon_topo_lock;
75 SX_SYSINIT(syscon_topology, &syscon_topo_lock, "Syscon topology lock");
80 static syscon_method_t syscon_methods[] = {
81 SYSCONMETHOD(syscon_init, syscon_method_init),
82 SYSCONMETHOD(syscon_uninit, syscon_method_uninit),
86 DEFINE_CLASS_0(syscon, syscon_class, syscon_methods, 0);
88 #define SYSCON_TOPO_SLOCK() sx_slock(&syscon_topo_lock)
89 #define SYSCON_TOPO_XLOCK() sx_xlock(&syscon_topo_lock)
90 #define SYSCON_TOPO_UNLOCK() sx_unlock(&syscon_topo_lock)
91 #define SYSCON_TOPO_ASSERT() sx_assert(&syscon_topo_lock, SA_LOCKED)
92 #define SYSCON_TOPO_XASSERT() sx_assert(&syscon_topo_lock, SA_XLOCKED)
95 * Default syscon methods for base class.
98 syscon_method_init(struct syscon *syscon)
105 syscon_method_uninit(struct syscon *syscon)
112 syscon_get_softc(struct syscon *syscon)
115 return (syscon->softc);
119 * Create and initialize syscon object, but do not register it.
122 syscon_create(device_t pdev, syscon_class_t syscon_class)
124 struct syscon *syscon;
126 /* Create object and initialize it. */
127 syscon = malloc(sizeof(struct syscon), M_SYSCON,
129 kobj_init((kobj_t)syscon, (kobj_class_t)syscon_class);
131 /* Allocate softc if required. */
132 if (syscon_class->size > 0)
133 syscon->softc = malloc(syscon_class->size, M_SYSCON,
141 /* Register syscon object. */
143 syscon_register(struct syscon *syscon)
148 if (syscon->ofw_node <= 0)
149 syscon->ofw_node = ofw_bus_get_node(syscon->pdev);
150 if (syscon->ofw_node <= 0)
154 rv = SYSCON_INIT(syscon);
156 printf("SYSCON_INIT failed: %d\n", rv);
161 OF_device_register_xref(OF_xref_from_node(syscon->ofw_node),
165 TAILQ_INSERT_TAIL(&syscon_list, syscon, syscon_link);
166 SYSCON_TOPO_UNLOCK();
171 syscon_unregister(struct syscon *syscon)
175 TAILQ_REMOVE(&syscon_list, syscon, syscon_link);
176 SYSCON_TOPO_UNLOCK();
178 OF_device_register_xref(OF_xref_from_node(syscon->ofw_node), NULL);
180 return (SYSCON_UNINIT(syscon));
187 static struct syscon *
188 syscon_find_by_ofw_node(phandle_t node)
190 struct syscon *entry;
192 SYSCON_TOPO_ASSERT();
194 TAILQ_FOREACH(entry, &syscon_list, syscon_link) {
195 if (entry->ofw_node == node)
203 syscon_create_ofw_node(device_t pdev, syscon_class_t syscon_class,
206 struct syscon *syscon;
208 syscon = syscon_create(pdev, syscon_class);
211 syscon->ofw_node = node;
212 if (syscon_register(syscon) == NULL)
218 syscon_get_ofw_node(struct syscon *syscon)
221 return (syscon->ofw_node);
225 syscon_get_by_ofw_property(device_t cdev, phandle_t cnode, char *name,
226 struct syscon **syscon)
232 cnode = ofw_bus_get_node(cdev);
235 "%s called on not ofw based device\n", __func__);
238 ncells = OF_getencprop_alloc_multi(cnode, name, sizeof(pcell_t),
243 /* Translate to syscon node. */
245 *syscon = syscon_find_by_ofw_node(OF_node_from_xref(cells[0]));
246 if (*syscon == NULL) {
247 SYSCON_TOPO_UNLOCK();
248 device_printf(cdev, "Failed to find syscon node\n");
252 SYSCON_TOPO_UNLOCK();