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