1 1.3 skrll /* $NetBSD: qemufwcfg.c,v 1.3 2024/04/06 13:42:18 skrll Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jmcneill * SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include <sys/cdefs.h> 30 1.3 skrll __KERNEL_RCSID(0, "$NetBSD: qemufwcfg.c,v 1.3 2024/04/06 13:42:18 skrll Exp $"); 31 1.1 jmcneill 32 1.1 jmcneill #include <sys/param.h> 33 1.1 jmcneill #include <sys/device.h> 34 1.1 jmcneill #include <sys/systm.h> 35 1.1 jmcneill #include <sys/conf.h> 36 1.1 jmcneill #include <sys/mutex.h> 37 1.1 jmcneill #include <sys/bus.h> 38 1.1 jmcneill 39 1.1 jmcneill #include <dev/ic/qemufwcfgvar.h> 40 1.1 jmcneill #include <dev/ic/qemufwcfgio.h> 41 1.1 jmcneill 42 1.1 jmcneill #include "ioconf.h" 43 1.1 jmcneill 44 1.1 jmcneill /* Register locations are MD */ 45 1.1 jmcneill #if defined(__i386__) || defined(__x86_64__) 46 1.1 jmcneill #define FWCFG_SEL_REG 0x00 47 1.1 jmcneill #define FWCFG_SEL_SWAP htole16 48 1.1 jmcneill #define FWCFG_DATA_REG 0x01 49 1.1 jmcneill #define FWCFG_DMA_ADDR 0x04 50 1.1 jmcneill #elif defined(__arm__) || defined(__aarch64__) 51 1.1 jmcneill #define FWCFG_SEL_REG 0x08 52 1.1 jmcneill #define FWCFG_SEL_SWAP htobe16 53 1.1 jmcneill #define FWCFG_DATA_REG 0x00 54 1.1 jmcneill #define FWCFG_DMA_ADDR 0x10 55 1.3 skrll #elif defined(__riscv) 56 1.3 skrll #define FWCFG_SEL_REG 0x08 57 1.3 skrll #define FWCFG_SEL_SWAP htobe16 58 1.3 skrll #define FWCFG_DATA_REG 0x00 59 1.3 skrll #define FWCFG_DMA_ADDR 0x10 60 1.1 jmcneill #else 61 1.1 jmcneill #error driver does not support this architecture 62 1.1 jmcneill #endif 63 1.1 jmcneill 64 1.1 jmcneill static dev_type_open(fwcfg_open); 65 1.1 jmcneill static dev_type_close(fwcfg_close); 66 1.1 jmcneill static dev_type_read(fwcfg_read); 67 1.1 jmcneill static dev_type_ioctl(fwcfg_ioctl); 68 1.1 jmcneill 69 1.1 jmcneill #define FWCFGUNIT(d) minor(d) 70 1.1 jmcneill 71 1.1 jmcneill static void 72 1.1 jmcneill fwcfg_select(struct fwcfg_softc *sc, uint16_t index) 73 1.1 jmcneill { 74 1.1 jmcneill bus_space_write_2(sc->sc_bst, sc->sc_bsh, FWCFG_SEL_REG, FWCFG_SEL_SWAP(index)); 75 1.1 jmcneill } 76 1.1 jmcneill 77 1.1 jmcneill static int 78 1.1 jmcneill fwcfg_open(dev_t dev, int flag, int mode, lwp_t *l) 79 1.1 jmcneill { 80 1.1 jmcneill struct fwcfg_softc * const sc = device_lookup_private(&qemufwcfg_cd, FWCFGUNIT(dev)); 81 1.1 jmcneill int error; 82 1.1 jmcneill 83 1.1 jmcneill if (sc == NULL) 84 1.1 jmcneill return ENXIO; 85 1.1 jmcneill 86 1.1 jmcneill mutex_enter(&sc->sc_lock); 87 1.1 jmcneill if (!sc->sc_open) { 88 1.1 jmcneill error = 0; 89 1.1 jmcneill sc->sc_open = true; 90 1.1 jmcneill } else 91 1.1 jmcneill error = EBUSY; 92 1.1 jmcneill mutex_exit(&sc->sc_lock); 93 1.1 jmcneill 94 1.1 jmcneill return error; 95 1.1 jmcneill } 96 1.1 jmcneill 97 1.1 jmcneill static int 98 1.1 jmcneill fwcfg_close(dev_t dev, int flag, int mode, lwp_t *l) 99 1.1 jmcneill { 100 1.1 jmcneill struct fwcfg_softc * const sc = device_lookup_private(&qemufwcfg_cd, FWCFGUNIT(dev)); 101 1.1 jmcneill int error; 102 1.1 jmcneill 103 1.1 jmcneill if (sc == NULL) 104 1.1 jmcneill return ENXIO; 105 1.1 jmcneill 106 1.1 jmcneill mutex_enter(&sc->sc_lock); 107 1.1 jmcneill if (sc->sc_open) { 108 1.1 jmcneill error = 0; 109 1.1 jmcneill sc->sc_open = false; 110 1.1 jmcneill } else 111 1.1 jmcneill error = EINVAL; 112 1.1 jmcneill mutex_exit(&sc->sc_lock); 113 1.1 jmcneill 114 1.1 jmcneill return error; 115 1.1 jmcneill } 116 1.1 jmcneill 117 1.1 jmcneill static int 118 1.1 jmcneill fwcfg_read(dev_t dev, struct uio *uio, int flags) 119 1.1 jmcneill { 120 1.1 jmcneill struct fwcfg_softc * const sc = device_lookup_private(&qemufwcfg_cd, FWCFGUNIT(dev)); 121 1.1 jmcneill uint8_t buf[64]; 122 1.1 jmcneill size_t count; 123 1.1 jmcneill int error = 0; 124 1.1 jmcneill 125 1.1 jmcneill if (sc == NULL) 126 1.1 jmcneill return ENXIO; 127 1.1 jmcneill 128 1.1 jmcneill while (uio->uio_resid > 0) { 129 1.2 riastrad count = uimin(sizeof(buf), uio->uio_resid); 130 1.1 jmcneill bus_space_read_multi_1(sc->sc_bst, sc->sc_bsh, FWCFG_DATA_REG, buf, count); 131 1.1 jmcneill error = uiomove(buf, count, uio); 132 1.1 jmcneill if (error != 0) 133 1.1 jmcneill break; 134 1.1 jmcneill } 135 1.1 jmcneill 136 1.1 jmcneill return error; 137 1.1 jmcneill } 138 1.1 jmcneill 139 1.1 jmcneill static int 140 1.1 jmcneill fwcfg_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l) 141 1.1 jmcneill { 142 1.1 jmcneill struct fwcfg_softc * const sc = device_lookup_private(&qemufwcfg_cd, FWCFGUNIT(dev)); 143 1.1 jmcneill uint16_t index; 144 1.1 jmcneill 145 1.1 jmcneill if (sc == NULL) 146 1.1 jmcneill return ENXIO; 147 1.1 jmcneill 148 1.1 jmcneill switch (cmd) { 149 1.1 jmcneill case FWCFGIO_SET_INDEX: 150 1.1 jmcneill index = *(uint16_t *)data; 151 1.1 jmcneill fwcfg_select(sc, index); 152 1.1 jmcneill return 0; 153 1.1 jmcneill default: 154 1.1 jmcneill return ENOTTY; 155 1.1 jmcneill } 156 1.1 jmcneill } 157 1.1 jmcneill 158 1.1 jmcneill const struct cdevsw qemufwcfg_cdevsw = { 159 1.1 jmcneill .d_open = fwcfg_open, 160 1.1 jmcneill .d_close = fwcfg_close, 161 1.1 jmcneill .d_read = fwcfg_read, 162 1.1 jmcneill .d_write = nowrite, 163 1.1 jmcneill .d_ioctl = fwcfg_ioctl, 164 1.1 jmcneill .d_stop = nostop, 165 1.1 jmcneill .d_tty = notty, 166 1.1 jmcneill .d_poll = nopoll, 167 1.1 jmcneill .d_mmap = nommap, 168 1.1 jmcneill .d_kqfilter = nokqfilter, 169 1.1 jmcneill .d_discard = nodiscard, 170 1.1 jmcneill .d_flag = D_OTHER | D_MPSAFE 171 1.1 jmcneill }; 172 1.1 jmcneill 173 1.1 jmcneill void 174 1.1 jmcneill fwcfg_attach(struct fwcfg_softc *sc) 175 1.1 jmcneill { 176 1.1 jmcneill char sig[4]; 177 1.1 jmcneill 178 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 179 1.1 jmcneill 180 1.1 jmcneill /* Read signature */ 181 1.1 jmcneill fwcfg_select(sc, FW_CFG_SIGNATURE); 182 1.1 jmcneill bus_space_read_multi_1(sc->sc_bst, sc->sc_bsh, FWCFG_DATA_REG, sig, sizeof(sig)); 183 1.1 jmcneill aprint_verbose_dev(sc->sc_dev, "<%c%c%c%c>\n", sig[0], sig[1], sig[2], sig[3]); 184 1.1 jmcneill } 185