1 1.1 thorpej /* $NetBSD: pgctrl.c,v 1.1 2026/07/19 01:48:24 thorpej Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /*- 4 1.1 thorpej * Copyright (c) 2026 The NetBSD Foundation, Inc. 5 1.1 thorpej * All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation 8 1.1 thorpej * by Jason R. Thorpe. 9 1.1 thorpej * 10 1.1 thorpej * Redistribution and use in source and binary forms, with or without 11 1.1 thorpej * modification, are permitted provided that the following conditions 12 1.1 thorpej * are met: 13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 14 1.1 thorpej * notice, this list of conditions and the following disclaimer. 15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 17 1.1 thorpej * documentation and/or other materials provided with the distribution. 18 1.1 thorpej * 19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE. 30 1.1 thorpej */ 31 1.1 thorpej 32 1.1 thorpej /* 33 1.1 thorpej * Flattened Device Tree glue for pg68k "control space". 34 1.1 thorpej */ 35 1.1 thorpej 36 1.1 thorpej #include <sys/cdefs.h> 37 1.1 thorpej __KERNEL_RCSID(0, "$NetBSD: pgctrl.c,v 1.1 2026/07/19 01:48:24 thorpej Exp $"); 38 1.1 thorpej 39 1.1 thorpej #include <sys/param.h> 40 1.1 thorpej #include <sys/bus.h> 41 1.1 thorpej #include <sys/device.h> 42 1.1 thorpej #include <sys/systm.h> 43 1.1 thorpej #include <sys/mutex.h> 44 1.1 thorpej 45 1.1 thorpej #include <dev/fdt/fdtvar.h> 46 1.1 thorpej #include <dev/fdt/syscon.h> 47 1.1 thorpej 48 1.1 thorpej #include <hb68k/pg68k/control.h> 49 1.1 thorpej 50 1.1 thorpej struct pgctrl_softc { 51 1.1 thorpej device_t sc_dev; 52 1.1 thorpej kmutex_t sc_lock; 53 1.1 thorpej struct syscon sc_syscon; 54 1.1 thorpej }; 55 1.1 thorpej 56 1.1 thorpej static const struct device_compatible_entry compat_data[] = { 57 1.1 thorpej { .compat = "pg68k,control-space" }, 58 1.1 thorpej DEVICE_COMPAT_EOL 59 1.1 thorpej }; 60 1.1 thorpej 61 1.1 thorpej static void 62 1.1 thorpej pgctrl_lock(void *v) 63 1.1 thorpej { 64 1.1 thorpej struct pgctrl_softc * const sc = v; 65 1.1 thorpej 66 1.1 thorpej mutex_enter(&sc->sc_lock); 67 1.1 thorpej } 68 1.1 thorpej 69 1.1 thorpej static void 70 1.1 thorpej pgctrl_unlock(void *v) 71 1.1 thorpej { 72 1.1 thorpej struct pgctrl_softc * const sc = v; 73 1.1 thorpej 74 1.1 thorpej mutex_exit(&sc->sc_lock); 75 1.1 thorpej } 76 1.1 thorpej 77 1.1 thorpej static uint32_t 78 1.1 thorpej pgctrl_read_4(void *v, bus_size_t reg) 79 1.1 thorpej { 80 1.1 thorpej struct pgctrl_softc * const sc = v; 81 1.1 thorpej 82 1.1 thorpej KASSERT(mutex_owned(&sc->sc_lock)); 83 1.1 thorpej 84 1.1 thorpej return control_inb(reg); 85 1.1 thorpej } 86 1.1 thorpej 87 1.1 thorpej static void 88 1.1 thorpej pgctrl_write_4(void *v, bus_size_t reg, uint32_t val) 89 1.1 thorpej { 90 1.1 thorpej struct pgctrl_softc * const sc = v; 91 1.1 thorpej 92 1.1 thorpej KASSERT(mutex_owned(&sc->sc_lock)); 93 1.1 thorpej 94 1.1 thorpej control_outb(reg, (uint8_t)val); 95 1.1 thorpej } 96 1.1 thorpej 97 1.1 thorpej static int 98 1.1 thorpej pgctrl_match(device_t parent, cfdata_t cf, void *aux) 99 1.1 thorpej { 100 1.1 thorpej struct fdt_attach_args * const faa = aux; 101 1.1 thorpej 102 1.1 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 103 1.1 thorpej } 104 1.1 thorpej 105 1.1 thorpej static void 106 1.1 thorpej pgctrl_attach(device_t parent, device_t self, void *aux) 107 1.1 thorpej { 108 1.1 thorpej struct pgctrl_softc * const sc = device_private(self); 109 1.1 thorpej struct fdt_attach_args * const faa = aux; 110 1.1 thorpej const int phandle = faa->faa_phandle; 111 1.1 thorpej 112 1.1 thorpej aprint_naive("\n"); 113 1.1 thorpej aprint_normal("\n"); 114 1.1 thorpej 115 1.1 thorpej sc->sc_dev = self; 116 1.1 thorpej mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 117 1.1 thorpej 118 1.1 thorpej sc->sc_syscon.priv = sc; 119 1.1 thorpej sc->sc_syscon.lock = pgctrl_lock; 120 1.1 thorpej sc->sc_syscon.unlock = pgctrl_unlock; 121 1.1 thorpej sc->sc_syscon.read_4 = pgctrl_read_4; 122 1.1 thorpej sc->sc_syscon.write_4 = pgctrl_write_4; 123 1.1 thorpej 124 1.1 thorpej fdtbus_register_syscon(self, phandle, &sc->sc_syscon); 125 1.1 thorpej } 126 1.1 thorpej 127 1.1 thorpej CFATTACH_DECL_NEW(pgctrl, sizeof(struct pgctrl_softc), 128 1.1 thorpej pgctrl_match, pgctrl_attach, NULL, NULL); 129