]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/extres/syscon/syscon.c
cxgbe(4): Make sure bundled_fw is always initialized before use.
[FreeBSD/FreeBSD.git] / sys / dev / extres / syscon / syscon.c
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  */
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
71 MALLOC_DEFINE(M_SYSCON, "syscon", "Syscon driver");
72
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");
76
77 /*
78  * Syscon methods.
79  */
80 static syscon_method_t syscon_methods[] = {
81         SYSCONMETHOD(syscon_init,       syscon_method_init),
82         SYSCONMETHOD(syscon_uninit,     syscon_method_uninit),
83
84         SYSCONMETHOD_END
85 };
86 DEFINE_CLASS_0(syscon, syscon_class, syscon_methods, 0);
87
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)
93
94 /*
95  * Default syscon methods for base class.
96  */
97 static int
98 syscon_method_init(struct syscon *syscon)
99 {
100
101         return (0);
102 };
103
104 static int
105 syscon_method_uninit(struct syscon *syscon)
106 {
107
108         return (0);
109 };
110
111 void *
112 syscon_get_softc(struct syscon *syscon)
113 {
114
115         return (syscon->softc);
116 };
117
118 /*
119  * Create and initialize syscon object, but do not register it.
120  */
121 struct syscon *
122 syscon_create(device_t pdev, syscon_class_t syscon_class)
123 {
124         struct syscon *syscon;
125
126         /* Create object and initialize it. */
127         syscon = malloc(sizeof(struct syscon), M_SYSCON,
128             M_WAITOK | M_ZERO);
129         kobj_init((kobj_t)syscon, (kobj_class_t)syscon_class);
130
131         /* Allocate softc if required. */
132         if (syscon_class->size > 0)
133                 syscon->softc = malloc(syscon_class->size, M_SYSCON,
134                     M_WAITOK | M_ZERO);
135
136         /* Rest of init. */
137         syscon->pdev = pdev;
138         return (syscon);
139 }
140
141 /* Register syscon object. */
142 struct syscon *
143 syscon_register(struct syscon *syscon)
144 {
145         int rv;
146
147 #ifdef FDT
148         if (syscon->ofw_node <= 0)
149                 syscon->ofw_node = ofw_bus_get_node(syscon->pdev);
150         if (syscon->ofw_node <= 0)
151                 return (NULL);
152 #endif
153
154         rv = SYSCON_INIT(syscon);
155         if (rv != 0) {
156                 printf("SYSCON_INIT failed: %d\n", rv);
157                 return (NULL);
158         }
159
160 #ifdef FDT
161         OF_device_register_xref(OF_xref_from_node(syscon->ofw_node),
162             syscon->pdev);
163 #endif
164         SYSCON_TOPO_XLOCK();
165         TAILQ_INSERT_TAIL(&syscon_list, syscon, syscon_link);
166         SYSCON_TOPO_UNLOCK();
167         return (syscon);
168 }
169
170 int
171 syscon_unregister(struct syscon *syscon)
172 {
173
174         SYSCON_TOPO_XLOCK();
175         TAILQ_REMOVE(&syscon_list, syscon, syscon_link);
176         SYSCON_TOPO_UNLOCK();
177 #ifdef FDT
178         OF_device_register_xref(OF_xref_from_node(syscon->ofw_node), NULL);
179 #endif
180         return (SYSCON_UNINIT(syscon));
181 }
182
183 /**
184  * Provider methods
185  */
186 #ifdef FDT
187 static struct syscon *
188 syscon_find_by_ofw_node(phandle_t node)
189 {
190         struct syscon *entry;
191
192         SYSCON_TOPO_ASSERT();
193
194         TAILQ_FOREACH(entry, &syscon_list, syscon_link) {
195                 if (entry->ofw_node == node)
196                         return (entry);
197         }
198
199         return (NULL);
200 }
201
202 struct syscon *
203 syscon_create_ofw_node(device_t pdev, syscon_class_t syscon_class,
204     phandle_t node)
205 {
206         struct syscon *syscon;
207
208         syscon = syscon_create(pdev, syscon_class);
209         if (syscon == NULL)
210                 return (NULL);
211         syscon->ofw_node = node;
212         if (syscon_register(syscon) == NULL)
213                 return (NULL);
214         return (syscon);
215 }
216
217 phandle_t
218 syscon_get_ofw_node(struct syscon *syscon)
219 {
220
221         return (syscon->ofw_node);
222 }
223
224 int
225 syscon_get_by_ofw_property(device_t cdev, phandle_t cnode, char *name,
226     struct syscon **syscon)
227 {
228         pcell_t *cells;
229         int ncells;
230
231         if (cnode <= 0)
232                 cnode = ofw_bus_get_node(cdev);
233         if (cnode <= 0) {
234                 device_printf(cdev,
235                     "%s called on not ofw based device\n", __func__);
236                 return (ENXIO);
237         }
238         ncells = OF_getencprop_alloc_multi(cnode, name, sizeof(pcell_t),
239             (void **)&cells);
240         if (ncells < 1)
241                 return (ENOENT);
242
243         /* Translate to syscon node. */
244         SYSCON_TOPO_SLOCK();
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");
249                 OF_prop_free(cells);
250                 return (ENODEV);
251         }
252         SYSCON_TOPO_UNLOCK();
253         OF_prop_free(cells);
254         return (0);
255 }
256 #endif