1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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__FBSDID("$FreeBSD$");
37#include "opt_platform.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/bus.h>
42#include <sys/kernel.h>
43#include <sys/kobj.h>
44#include <sys/lock.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 */
63typedef TAILQ_HEAD(syscon_list, syscon) syscon_list_t;
64
65/*
66 * Declarations
67 */
68static int syscon_method_init(struct syscon *syscon);
69static int syscon_method_uninit(struct syscon *syscon);
70static uint32_t syscon_method_read_4(struct syscon *syscon, bus_size_t offset);
71static int syscon_method_write_4(struct syscon *syscon, bus_size_t offset,
72    uint32_t val);
73static int syscon_method_modify_4(struct syscon *syscon, bus_size_t offset,
74    uint32_t clear_bits, uint32_t set_bits);
75
76
77MALLOC_DEFINE(M_SYSCON, "syscon", "Syscon driver");
78
79static syscon_list_t syscon_list = TAILQ_HEAD_INITIALIZER(syscon_list);
80static struct sx		syscon_topo_lock;
81SX_SYSINIT(syscon_topology, &syscon_topo_lock, "Syscon topology lock");
82
83/*
84 * Syscon methods.
85 */
86static 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};
95DEFINE_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 */
106static int
107syscon_method_init(struct syscon *syscon)
108{
109
110	return (0);
111};
112
113static int
114syscon_method_uninit(struct syscon *syscon)
115{
116
117	return (0);
118};
119
120void *
121syscon_get_softc(struct syscon *syscon)
122{
123
124	return (syscon->softc);
125};
126
127static uint32_t
128syscon_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
138static int
139syscon_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
149static int
150syscon_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 */
163struct syscon *
164syscon_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. */
184struct syscon *
185syscon_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
212int
213syscon_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
229static struct syscon *
230syscon_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
244struct syscon *
245syscon_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
259phandle_t
260syscon_get_ofw_node(struct syscon *syscon)
261{
262
263	return (syscon->ofw_node);
264}
265
266int
267syscon_get_by_ofw_property(device_t cdev, phandle_t cnode, char *name,
268    struct syscon **syscon)
269{
270	pcell_t *cells;
271	int ncells;
272
273	if (cnode <= 0)
274		cnode = ofw_bus_get_node(cdev);
275	if (cnode <= 0) {
276		device_printf(cdev,
277		    "%s called on not ofw based device\n", __func__);
278		return (ENXIO);
279	}
280	ncells = OF_getencprop_alloc_multi(cnode, name, sizeof(pcell_t),
281	    (void **)&cells);
282	if (ncells < 1)
283		return (ENOENT);
284
285	/* Translate to syscon node. */
286	SYSCON_TOPO_SLOCK();
287	*syscon = syscon_find_by_ofw_node(OF_node_from_xref(cells[0]));
288	if (*syscon == NULL) {
289		SYSCON_TOPO_UNLOCK();
290		device_printf(cdev, "Failed to find syscon node\n");
291		OF_prop_free(cells);
292		return (ENODEV);
293	}
294	SYSCON_TOPO_UNLOCK();
295	OF_prop_free(cells);
296	return (0);
297}
298#endif
299