Home | History | Annotate | Line # | Download | only in ic
qemufwcfg.c revision 1.2
      1  1.1  jmcneill /* $NetBSD: qemufwcfg.c,v 1.2 2018/09/03 16:29:31 riastradh 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.2  riastrad __KERNEL_RCSID(0, "$NetBSD: qemufwcfg.c,v 1.2 2018/09/03 16:29:31 riastradh 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.1  jmcneill #else
     56  1.1  jmcneill #error driver does not support this architecture
     57  1.1  jmcneill #endif
     58  1.1  jmcneill 
     59  1.1  jmcneill static dev_type_open(fwcfg_open);
     60  1.1  jmcneill static dev_type_close(fwcfg_close);
     61  1.1  jmcneill static dev_type_read(fwcfg_read);
     62  1.1  jmcneill static dev_type_ioctl(fwcfg_ioctl);
     63  1.1  jmcneill 
     64  1.1  jmcneill #define	FWCFGUNIT(d)	minor(d)
     65  1.1  jmcneill 
     66  1.1  jmcneill static void
     67  1.1  jmcneill fwcfg_select(struct fwcfg_softc *sc, uint16_t index)
     68  1.1  jmcneill {
     69  1.1  jmcneill 	bus_space_write_2(sc->sc_bst, sc->sc_bsh, FWCFG_SEL_REG, FWCFG_SEL_SWAP(index));
     70  1.1  jmcneill }
     71  1.1  jmcneill 
     72  1.1  jmcneill static int
     73  1.1  jmcneill fwcfg_open(dev_t dev, int flag, int mode, lwp_t *l)
     74  1.1  jmcneill {
     75  1.1  jmcneill 	struct fwcfg_softc * const sc = device_lookup_private(&qemufwcfg_cd, FWCFGUNIT(dev));
     76  1.1  jmcneill 	int error;
     77  1.1  jmcneill 
     78  1.1  jmcneill 	if (sc == NULL)
     79  1.1  jmcneill 		return ENXIO;
     80  1.1  jmcneill 
     81  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
     82  1.1  jmcneill 	if (!sc->sc_open) {
     83  1.1  jmcneill 		error = 0;
     84  1.1  jmcneill 		sc->sc_open = true;
     85  1.1  jmcneill 	} else
     86  1.1  jmcneill 		error = EBUSY;
     87  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
     88  1.1  jmcneill 
     89  1.1  jmcneill 	return error;
     90  1.1  jmcneill }
     91  1.1  jmcneill 
     92  1.1  jmcneill static int
     93  1.1  jmcneill fwcfg_close(dev_t dev, int flag, int mode, lwp_t *l)
     94  1.1  jmcneill {
     95  1.1  jmcneill 	struct fwcfg_softc * const sc = device_lookup_private(&qemufwcfg_cd, FWCFGUNIT(dev));
     96  1.1  jmcneill 	int error;
     97  1.1  jmcneill 
     98  1.1  jmcneill 	if (sc == NULL)
     99  1.1  jmcneill 		return ENXIO;
    100  1.1  jmcneill 
    101  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    102  1.1  jmcneill 	if (sc->sc_open) {
    103  1.1  jmcneill 		error = 0;
    104  1.1  jmcneill 		sc->sc_open = false;
    105  1.1  jmcneill 	} else
    106  1.1  jmcneill 		error = EINVAL;
    107  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    108  1.1  jmcneill 
    109  1.1  jmcneill 	return error;
    110  1.1  jmcneill }
    111  1.1  jmcneill 
    112  1.1  jmcneill static int
    113  1.1  jmcneill fwcfg_read(dev_t dev, struct uio *uio, int flags)
    114  1.1  jmcneill {
    115  1.1  jmcneill 	struct fwcfg_softc * const sc = device_lookup_private(&qemufwcfg_cd, FWCFGUNIT(dev));
    116  1.1  jmcneill 	uint8_t buf[64];
    117  1.1  jmcneill 	size_t count;
    118  1.1  jmcneill 	int error = 0;
    119  1.1  jmcneill 
    120  1.1  jmcneill 	if (sc == NULL)
    121  1.1  jmcneill 		return ENXIO;
    122  1.1  jmcneill 
    123  1.1  jmcneill 	while (uio->uio_resid > 0) {
    124  1.2  riastrad 		count = uimin(sizeof(buf), uio->uio_resid);
    125  1.1  jmcneill 		bus_space_read_multi_1(sc->sc_bst, sc->sc_bsh, FWCFG_DATA_REG, buf, count);
    126  1.1  jmcneill 		error = uiomove(buf, count, uio);
    127  1.1  jmcneill 		if (error != 0)
    128  1.1  jmcneill 			break;
    129  1.1  jmcneill 	}
    130  1.1  jmcneill 
    131  1.1  jmcneill 	return error;
    132  1.1  jmcneill }
    133  1.1  jmcneill 
    134  1.1  jmcneill static int
    135  1.1  jmcneill fwcfg_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    136  1.1  jmcneill {
    137  1.1  jmcneill 	struct fwcfg_softc * const sc = device_lookup_private(&qemufwcfg_cd, FWCFGUNIT(dev));
    138  1.1  jmcneill 	uint16_t index;
    139  1.1  jmcneill 
    140  1.1  jmcneill 	if (sc == NULL)
    141  1.1  jmcneill 		return ENXIO;
    142  1.1  jmcneill 
    143  1.1  jmcneill 	switch (cmd) {
    144  1.1  jmcneill 	case FWCFGIO_SET_INDEX:
    145  1.1  jmcneill 		index = *(uint16_t *)data;
    146  1.1  jmcneill 		fwcfg_select(sc, index);
    147  1.1  jmcneill 		return 0;
    148  1.1  jmcneill 	default:
    149  1.1  jmcneill 		return ENOTTY;
    150  1.1  jmcneill 	}
    151  1.1  jmcneill }
    152  1.1  jmcneill 
    153  1.1  jmcneill const struct cdevsw qemufwcfg_cdevsw = {
    154  1.1  jmcneill 	.d_open = fwcfg_open,
    155  1.1  jmcneill 	.d_close = fwcfg_close,
    156  1.1  jmcneill 	.d_read = fwcfg_read,
    157  1.1  jmcneill 	.d_write = nowrite,
    158  1.1  jmcneill 	.d_ioctl = fwcfg_ioctl,
    159  1.1  jmcneill 	.d_stop = nostop,
    160  1.1  jmcneill 	.d_tty = notty,
    161  1.1  jmcneill 	.d_poll = nopoll,
    162  1.1  jmcneill 	.d_mmap = nommap,
    163  1.1  jmcneill 	.d_kqfilter = nokqfilter,
    164  1.1  jmcneill 	.d_discard = nodiscard,
    165  1.1  jmcneill 	.d_flag = D_OTHER | D_MPSAFE
    166  1.1  jmcneill };
    167  1.1  jmcneill 
    168  1.1  jmcneill void
    169  1.1  jmcneill fwcfg_attach(struct fwcfg_softc *sc)
    170  1.1  jmcneill {
    171  1.1  jmcneill 	char sig[4];
    172  1.1  jmcneill 
    173  1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    174  1.1  jmcneill 
    175  1.1  jmcneill 	/* Read signature */
    176  1.1  jmcneill 	fwcfg_select(sc, FW_CFG_SIGNATURE);
    177  1.1  jmcneill 	bus_space_read_multi_1(sc->sc_bst, sc->sc_bsh, FWCFG_DATA_REG, sig, sizeof(sig));
    178  1.1  jmcneill 	aprint_verbose_dev(sc->sc_dev, "<%c%c%c%c>\n", sig[0], sig[1], sig[2], sig[3]);
    179  1.1  jmcneill }
    180