fdt_syscon.c revision 1.3.8.2 1 1.3.8.2 christos /* $NetBSD: fdt_syscon.c,v 1.3.8.2 2019/06/10 22:07:08 christos Exp $ */
2 1.3.8.2 christos
3 1.3.8.2 christos /*-
4 1.3.8.2 christos * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.3.8.2 christos * All rights reserved.
6 1.3.8.2 christos *
7 1.3.8.2 christos * Redistribution and use in source and binary forms, with or without
8 1.3.8.2 christos * modification, are permitted provided that the following conditions
9 1.3.8.2 christos * are met:
10 1.3.8.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.3.8.2 christos * notice, this list of conditions and the following disclaimer.
12 1.3.8.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.8.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.3.8.2 christos * documentation and/or other materials provided with the distribution.
15 1.3.8.2 christos *
16 1.3.8.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.3.8.2 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.3.8.2 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.3.8.2 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.3.8.2 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.3.8.2 christos * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.3.8.2 christos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.3.8.2 christos * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.3.8.2 christos * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.3.8.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.3.8.2 christos * SUCH DAMAGE.
27 1.3.8.2 christos */
28 1.3.8.2 christos
29 1.3.8.2 christos #include <sys/cdefs.h>
30 1.3.8.2 christos __KERNEL_RCSID(0, "$NetBSD: fdt_syscon.c,v 1.3.8.2 2019/06/10 22:07:08 christos Exp $");
31 1.3.8.2 christos
32 1.3.8.2 christos #include <sys/param.h>
33 1.3.8.2 christos #include <sys/bus.h>
34 1.3.8.2 christos #include <sys/kmem.h>
35 1.3.8.2 christos #include <sys/queue.h>
36 1.3.8.2 christos
37 1.3.8.2 christos #include <libfdt.h>
38 1.3.8.2 christos #include <dev/fdt/fdtvar.h>
39 1.3.8.2 christos
40 1.3.8.2 christos struct syscon;
41 1.3.8.2 christos
42 1.3.8.2 christos struct fdtbus_syscon {
43 1.3.8.2 christos device_t sc_dev;
44 1.3.8.2 christos int sc_phandle;
45 1.3.8.2 christos struct syscon *sc_syscon;
46 1.3.8.2 christos
47 1.3.8.2 christos LIST_ENTRY(fdtbus_syscon) sc_next;
48 1.3.8.2 christos };
49 1.3.8.2 christos
50 1.3.8.2 christos static LIST_HEAD(, fdtbus_syscon) fdtbus_syscons =
51 1.3.8.2 christos LIST_HEAD_INITIALIZER(fdtbus_syscons);
52 1.3.8.2 christos
53 1.3.8.2 christos int
54 1.3.8.2 christos fdtbus_register_syscon(device_t dev, int phandle,
55 1.3.8.2 christos struct syscon *syscon)
56 1.3.8.2 christos {
57 1.3.8.2 christos struct fdtbus_syscon *sc;
58 1.3.8.2 christos
59 1.3.8.2 christos sc = kmem_alloc(sizeof(*sc), KM_SLEEP);
60 1.3.8.2 christos sc->sc_dev = dev;
61 1.3.8.2 christos sc->sc_phandle = phandle;
62 1.3.8.2 christos sc->sc_syscon = syscon;
63 1.3.8.2 christos
64 1.3.8.2 christos LIST_INSERT_HEAD(&fdtbus_syscons, sc, sc_next);
65 1.3.8.2 christos
66 1.3.8.2 christos return 0;
67 1.3.8.2 christos }
68 1.3.8.2 christos
69 1.3.8.2 christos static struct fdtbus_syscon *
70 1.3.8.2 christos fdtbus_get_syscon(int phandle)
71 1.3.8.2 christos {
72 1.3.8.2 christos struct fdtbus_syscon *sc;
73 1.3.8.2 christos
74 1.3.8.2 christos LIST_FOREACH(sc, &fdtbus_syscons, sc_next) {
75 1.3.8.2 christos if (sc->sc_phandle == phandle)
76 1.3.8.2 christos return sc;
77 1.3.8.2 christos }
78 1.3.8.2 christos
79 1.3.8.2 christos return NULL;
80 1.3.8.2 christos }
81 1.3.8.2 christos
82 1.3.8.2 christos struct syscon *
83 1.3.8.2 christos fdtbus_syscon_acquire(int phandle, const char *prop)
84 1.3.8.2 christos {
85 1.3.8.2 christos struct fdtbus_syscon *sc;
86 1.3.8.2 christos int sc_phandle;
87 1.3.8.2 christos
88 1.3.8.2 christos sc_phandle = fdtbus_get_phandle(phandle, prop);
89 1.3.8.2 christos if (sc_phandle < 0)
90 1.3.8.2 christos return NULL;
91 1.3.8.2 christos
92 1.3.8.2 christos sc = fdtbus_get_syscon(sc_phandle);
93 1.3.8.2 christos if (sc == NULL)
94 1.3.8.2 christos return NULL;
95 1.3.8.2 christos
96 1.3.8.2 christos return sc->sc_syscon;
97 1.3.8.2 christos }
98 1.3.8.2 christos
99 1.3.8.2 christos struct syscon *
100 1.3.8.2 christos fdtbus_syscon_lookup(int phandle)
101 1.3.8.2 christos {
102 1.3.8.2 christos struct fdtbus_syscon *sc;
103 1.3.8.2 christos
104 1.3.8.2 christos sc = fdtbus_get_syscon(phandle);
105 1.3.8.2 christos if (sc == NULL)
106 1.3.8.2 christos return NULL;
107 1.3.8.2 christos
108 1.3.8.2 christos return sc->sc_syscon;
109 1.3.8.2 christos }
110