1 /* $NetBSD: pgctrl.c,v 1.1 2026/07/19 01:48:24 thorpej 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 Jason R. Thorpe. 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 /* 33 * Flattened Device Tree glue for pg68k "control space". 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: pgctrl.c,v 1.1 2026/07/19 01:48:24 thorpej Exp $"); 38 39 #include <sys/param.h> 40 #include <sys/bus.h> 41 #include <sys/device.h> 42 #include <sys/systm.h> 43 #include <sys/mutex.h> 44 45 #include <dev/fdt/fdtvar.h> 46 #include <dev/fdt/syscon.h> 47 48 #include <hb68k/pg68k/control.h> 49 50 struct pgctrl_softc { 51 device_t sc_dev; 52 kmutex_t sc_lock; 53 struct syscon sc_syscon; 54 }; 55 56 static const struct device_compatible_entry compat_data[] = { 57 { .compat = "pg68k,control-space" }, 58 DEVICE_COMPAT_EOL 59 }; 60 61 static void 62 pgctrl_lock(void *v) 63 { 64 struct pgctrl_softc * const sc = v; 65 66 mutex_enter(&sc->sc_lock); 67 } 68 69 static void 70 pgctrl_unlock(void *v) 71 { 72 struct pgctrl_softc * const sc = v; 73 74 mutex_exit(&sc->sc_lock); 75 } 76 77 static uint32_t 78 pgctrl_read_4(void *v, bus_size_t reg) 79 { 80 struct pgctrl_softc * const sc = v; 81 82 KASSERT(mutex_owned(&sc->sc_lock)); 83 84 return control_inb(reg); 85 } 86 87 static void 88 pgctrl_write_4(void *v, bus_size_t reg, uint32_t val) 89 { 90 struct pgctrl_softc * const sc = v; 91 92 KASSERT(mutex_owned(&sc->sc_lock)); 93 94 control_outb(reg, (uint8_t)val); 95 } 96 97 static int 98 pgctrl_match(device_t parent, cfdata_t cf, void *aux) 99 { 100 struct fdt_attach_args * const faa = aux; 101 102 return of_compatible_match(faa->faa_phandle, compat_data); 103 } 104 105 static void 106 pgctrl_attach(device_t parent, device_t self, void *aux) 107 { 108 struct pgctrl_softc * const sc = device_private(self); 109 struct fdt_attach_args * const faa = aux; 110 const int phandle = faa->faa_phandle; 111 112 aprint_naive("\n"); 113 aprint_normal("\n"); 114 115 sc->sc_dev = self; 116 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 117 118 sc->sc_syscon.priv = sc; 119 sc->sc_syscon.lock = pgctrl_lock; 120 sc->sc_syscon.unlock = pgctrl_unlock; 121 sc->sc_syscon.read_4 = pgctrl_read_4; 122 sc->sc_syscon.write_4 = pgctrl_write_4; 123 124 fdtbus_register_syscon(self, phandle, &sc->sc_syscon); 125 } 126 127 CFATTACH_DECL_NEW(pgctrl, sizeof(struct pgctrl_softc), 128 pgctrl_match, pgctrl_attach, NULL, NULL); 129