obio.c revision 1.1 1 1.1 rkujawa /* $NetBSD: obio.c,v 1.1 2026/06/27 13:28:35 rkujawa Exp $ */
2 1.1 rkujawa
3 1.1 rkujawa /*-
4 1.1 rkujawa * Copyright (c) 2026 The NetBSD Foundation, Inc.
5 1.1 rkujawa * All rights reserved.
6 1.1 rkujawa *
7 1.1 rkujawa * This code is derived from software contributed to The NetBSD Foundation
8 1.1 rkujawa * by Radoslaw Kujawa and Robert Swindells.
9 1.1 rkujawa *
10 1.1 rkujawa * Redistribution and use in source and binary forms, with or without
11 1.1 rkujawa * modification, are permitted provided that the following conditions
12 1.1 rkujawa * are met:
13 1.1 rkujawa * 1. Redistributions of source code must retain the above copyright
14 1.1 rkujawa * notice, this list of conditions and the following disclaimer.
15 1.1 rkujawa * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rkujawa * notice, this list of conditions and the following disclaimer in the
17 1.1 rkujawa * documentation and/or other materials provided with the distribution.
18 1.1 rkujawa *
19 1.1 rkujawa * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rkujawa * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rkujawa * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rkujawa * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rkujawa * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rkujawa * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rkujawa * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rkujawa * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rkujawa * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rkujawa * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rkujawa * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rkujawa */
31 1.1 rkujawa
32 1.1 rkujawa /* Expose the powerpc _bus_dma* back-end functions for our DMA tag. */
33 1.1 rkujawa #define _POWERPC_BUS_DMA_PRIVATE
34 1.1 rkujawa
35 1.1 rkujawa #include <sys/cdefs.h>
36 1.1 rkujawa __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.1 2026/06/27 13:28:35 rkujawa Exp $");
37 1.1 rkujawa
38 1.1 rkujawa #include <sys/param.h>
39 1.1 rkujawa #include <sys/device.h>
40 1.1 rkujawa #include <sys/systm.h>
41 1.1 rkujawa
42 1.1 rkujawa #include <dev/ofw/openfirm.h>
43 1.1 rkujawa
44 1.1 rkujawa #include <machine/autoconf.h>
45 1.1 rkujawa
46 1.1 rkujawa #include <powerpc/mpc5200/obiovar.h>
47 1.1 rkujawa #include <powerpc/mpc5200/mpc5200reg.h>
48 1.1 rkujawa
49 1.1 rkujawa static int obio_match(device_t, cfdata_t, void *);
50 1.1 rkujawa static void obio_attach(device_t, device_t, void *);
51 1.1 rkujawa static int obio_print(void *, const char *);
52 1.1 rkujawa
53 1.1 rkujawa CFATTACH_DECL_NEW(mpcobio, sizeof(struct obio_softc),
54 1.1 rkujawa obio_match, obio_attach, NULL, NULL);
55 1.1 rkujawa
56 1.1 rkujawa /*
57 1.1 rkujawa * DMA tag shared by all on-chip devices.
58 1.1 rkujawa */
59 1.1 rkujawa static struct powerpc_bus_dma_tag obio_bus_dma_tag = {
60 1.1 rkujawa ._dmamap_create = _bus_dmamap_create,
61 1.1 rkujawa ._dmamap_destroy = _bus_dmamap_destroy,
62 1.1 rkujawa ._dmamap_load = _bus_dmamap_load,
63 1.1 rkujawa ._dmamap_load_mbuf = _bus_dmamap_load_mbuf,
64 1.1 rkujawa ._dmamap_load_uio = _bus_dmamap_load_uio,
65 1.1 rkujawa ._dmamap_load_raw = _bus_dmamap_load_raw,
66 1.1 rkujawa ._dmamap_unload = _bus_dmamap_unload,
67 1.1 rkujawa ._dmamap_sync = _bus_dmamap_sync,
68 1.1 rkujawa ._dmamem_alloc = _bus_dmamem_alloc,
69 1.1 rkujawa ._dmamem_free = _bus_dmamem_free,
70 1.1 rkujawa ._dmamem_map = _bus_dmamem_map,
71 1.1 rkujawa ._dmamem_unmap = _bus_dmamem_unmap,
72 1.1 rkujawa ._dmamem_mmap = _bus_dmamem_mmap,
73 1.1 rkujawa };
74 1.1 rkujawa
75 1.1 rkujawa /*
76 1.1 rkujawa * Essential on-chip blocks that have no node of their own in some firmware
77 1.1 rkujawa * builds.
78 1.1 rkujawa */
79 1.1 rkujawa static const struct obio_builtin {
80 1.1 rkujawa const char *ob_name;
81 1.1 rkujawa bus_size_t ob_offset;
82 1.1 rkujawa bus_size_t ob_size;
83 1.1 rkujawa } obio_builtins[] = {
84 1.1 rkujawa { "cdm", MPC5200_REG_CDM, 0x38 },
85 1.1 rkujawa };
86 1.1 rkujawa
87 1.1 rkujawa static int
88 1.1 rkujawa obio_match(device_t parent, cfdata_t cf, void *aux)
89 1.1 rkujawa {
90 1.1 rkujawa struct confargs *ca = aux;
91 1.1 rkujawa
92 1.1 rkujawa if (strcmp(ca->ca_name, "builtin") == 0)
93 1.1 rkujawa return 1;
94 1.1 rkujawa
95 1.1 rkujawa return 0;
96 1.1 rkujawa }
97 1.1 rkujawa
98 1.1 rkujawa static void
99 1.1 rkujawa obio_attach(device_t parent, device_t self, void *aux)
100 1.1 rkujawa {
101 1.1 rkujawa struct obio_softc *sc = device_private(self);
102 1.1 rkujawa struct confargs *ca = aux;
103 1.1 rkujawa struct obio_attach_args oba;
104 1.1 rkujawa uint32_t reg[2];
105 1.1 rkujawa bus_addr_t mbar;
106 1.1 rkujawa bus_size_t mbarsz;
107 1.1 rkujawa int node, len;
108 1.1 rkujawa char name[32];
109 1.1 rkujawa bool found[__arraycount(obio_builtins)];
110 1.1 rkujawa size_t i;
111 1.1 rkujawa
112 1.1 rkujawa memset(found, 0, sizeof(found));
113 1.1 rkujawa
114 1.1 rkujawa sc->sc_dev = self;
115 1.1 rkujawa sc->sc_node = ca->ca_node;
116 1.1 rkujawa
117 1.1 rkujawa /*
118 1.1 rkujawa * The "/builtin" node's "reg" describes the MBAR peripheral window.
119 1.1 rkujawa * Fall back to the architectural default if the firmware omits it.
120 1.1 rkujawa */
121 1.1 rkujawa mbar = MPC5200_MBAR_DEFAULT;
122 1.1 rkujawa mbarsz = MPC5200_MBAR_SIZE;
123 1.1 rkujawa len = OF_getprop(ca->ca_node, "reg", reg, sizeof(reg));
124 1.1 rkujawa if (len >= (int)sizeof(reg)) {
125 1.1 rkujawa mbar = reg[0];
126 1.1 rkujawa mbarsz = reg[1];
127 1.1 rkujawa }
128 1.1 rkujawa
129 1.1 rkujawa aprint_normal(": MPC5200B on-chip peripherals at 0x%08jx\n",
130 1.1 rkujawa (uintmax_t)mbar);
131 1.1 rkujawa
132 1.1 rkujawa /* Build the shared big-endian register-space tag over the window. */
133 1.1 rkujawa sc->sc_bst.pbs_flags = _BUS_SPACE_BIG_ENDIAN | _BUS_SPACE_MEM_TYPE;
134 1.1 rkujawa sc->sc_bst.pbs_offset = 0;
135 1.1 rkujawa sc->sc_bst.pbs_base = mbar;
136 1.1 rkujawa sc->sc_bst.pbs_limit = mbar + mbarsz;
137 1.1 rkujawa sc->sc_bst.pbs_extent = NULL;
138 1.1 rkujawa if (bus_space_init(&sc->sc_bst, "obio", NULL, 0) != 0) {
139 1.1 rkujawa aprint_error_dev(self, "can't init bus space tag\n");
140 1.1 rkujawa return;
141 1.1 rkujawa }
142 1.1 rkujawa
143 1.1 rkujawa /* Enumerate and attach the on-chip devices from the OF tree. */
144 1.1 rkujawa for (node = OF_child(ca->ca_node); node != 0; node = OF_peer(node)) {
145 1.1 rkujawa memset(name, 0, sizeof(name));
146 1.1 rkujawa if (OF_getprop(node, "name", name, sizeof(name)) <= 0)
147 1.1 rkujawa continue;
148 1.1 rkujawa
149 1.1 rkujawa oba.obio_name = name;
150 1.1 rkujawa oba.obio_node = node;
151 1.1 rkujawa oba.obio_bst = &sc->sc_bst;
152 1.1 rkujawa oba.obio_dmat = &obio_bus_dma_tag;
153 1.1 rkujawa oba.obio_addr = 0;
154 1.1 rkujawa oba.obio_size = 0;
155 1.1 rkujawa len = OF_getprop(node, "reg", reg, sizeof(reg));
156 1.1 rkujawa if (len >= (int)sizeof(reg)) {
157 1.1 rkujawa oba.obio_addr = reg[0];
158 1.1 rkujawa oba.obio_size = reg[1];
159 1.1 rkujawa }
160 1.1 rkujawa
161 1.1 rkujawa for (i = 0; i < __arraycount(obio_builtins); i++)
162 1.1 rkujawa if (strcmp(name, obio_builtins[i].ob_name) == 0)
163 1.1 rkujawa found[i] = true;
164 1.1 rkujawa
165 1.1 rkujawa config_found(self, &oba, obio_print, CFARGS_NONE);
166 1.1 rkujawa }
167 1.1 rkujawa
168 1.1 rkujawa /* Attach any essential built-in block the OF tree did not list. */
169 1.1 rkujawa for (i = 0; i < __arraycount(obio_builtins); i++) {
170 1.1 rkujawa if (found[i])
171 1.1 rkujawa continue;
172 1.1 rkujawa oba.obio_name = obio_builtins[i].ob_name;
173 1.1 rkujawa oba.obio_node = sc->sc_node; /* no node of its own */
174 1.1 rkujawa oba.obio_bst = &sc->sc_bst;
175 1.1 rkujawa oba.obio_dmat = &obio_bus_dma_tag;
176 1.1 rkujawa oba.obio_addr = mbar + obio_builtins[i].ob_offset;
177 1.1 rkujawa oba.obio_size = obio_builtins[i].ob_size;
178 1.1 rkujawa config_found(self, &oba, obio_print, CFARGS_NONE);
179 1.1 rkujawa }
180 1.1 rkujawa }
181 1.1 rkujawa
182 1.1 rkujawa static int
183 1.1 rkujawa obio_print(void *aux, const char *pnp)
184 1.1 rkujawa {
185 1.1 rkujawa struct obio_attach_args *oba = aux;
186 1.1 rkujawa
187 1.1 rkujawa if (pnp != NULL)
188 1.1 rkujawa aprint_normal("%s at %s", oba->obio_name, pnp);
189 1.1 rkujawa if (oba->obio_size != 0)
190 1.1 rkujawa aprint_normal(" addr 0x%08jx", (uintmax_t)oba->obio_addr);
191 1.1 rkujawa
192 1.1 rkujawa return UNCONF;
193 1.1 rkujawa }
194