xref: /netbsd-src/sys/dev/fdt/qemufwcfg_fdt.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: qemufwcfg_fdt.c,v 1.2 2021/01/27 03:10:21 thorpej Exp $ */
278ca7017Sjakllsch 
378ca7017Sjakllsch /*-
478ca7017Sjakllsch  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
578ca7017Sjakllsch  * All rights reserved.
678ca7017Sjakllsch  *
778ca7017Sjakllsch  * Redistribution and use in source and binary forms, with or without
878ca7017Sjakllsch  * modification, are permitted provided that the following conditions
978ca7017Sjakllsch  * are met:
1078ca7017Sjakllsch  * 1. Redistributions of source code must retain the above copyright
1178ca7017Sjakllsch  *    notice, this list of conditions and the following disclaimer.
1278ca7017Sjakllsch  * 2. Redistributions in binary form must reproduce the above copyright
1378ca7017Sjakllsch  *    notice, this list of conditions and the following disclaimer in the
1478ca7017Sjakllsch  *    documentation and/or other materials provided with the distribution.
1578ca7017Sjakllsch  *
1678ca7017Sjakllsch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1778ca7017Sjakllsch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1878ca7017Sjakllsch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1978ca7017Sjakllsch  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2078ca7017Sjakllsch  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2178ca7017Sjakllsch  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2278ca7017Sjakllsch  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2378ca7017Sjakllsch  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2478ca7017Sjakllsch  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2578ca7017Sjakllsch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2678ca7017Sjakllsch  * SUCH DAMAGE.
2778ca7017Sjakllsch  */
2878ca7017Sjakllsch 
2978ca7017Sjakllsch #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: qemufwcfg_fdt.c,v 1.2 2021/01/27 03:10:21 thorpej Exp $");
3178ca7017Sjakllsch 
3278ca7017Sjakllsch #include <sys/param.h>
3378ca7017Sjakllsch #include <sys/device.h>
3478ca7017Sjakllsch #include <sys/systm.h>
3578ca7017Sjakllsch #include <sys/mutex.h>
3678ca7017Sjakllsch #include <sys/bus.h>
3778ca7017Sjakllsch 
3878ca7017Sjakllsch #include <dev/fdt/fdtvar.h>
3978ca7017Sjakllsch 
4078ca7017Sjakllsch #include <dev/ic/qemufwcfgvar.h>
4178ca7017Sjakllsch 
4278ca7017Sjakllsch static int	fwcfg_fdt_match(device_t, cfdata_t, void *);
4378ca7017Sjakllsch static void	fwcfg_fdt_attach(device_t, device_t, void *);
4478ca7017Sjakllsch 
4578ca7017Sjakllsch CFATTACH_DECL_NEW(qemufwcfg_fdt, sizeof(struct fwcfg_softc),
4678ca7017Sjakllsch     fwcfg_fdt_match,
4778ca7017Sjakllsch     fwcfg_fdt_attach,
4878ca7017Sjakllsch     NULL,
4978ca7017Sjakllsch     NULL
5078ca7017Sjakllsch );
5178ca7017Sjakllsch 
52*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
53*6e54367aSthorpej 	{ .compat = "qemu,fw-cfg-mmio" },
54*6e54367aSthorpej 	DEVICE_COMPAT_EOL
5578ca7017Sjakllsch };
5678ca7017Sjakllsch 
5778ca7017Sjakllsch 
5878ca7017Sjakllsch static int
fwcfg_fdt_match(device_t parent,cfdata_t match,void * opaque)5978ca7017Sjakllsch fwcfg_fdt_match(device_t parent, cfdata_t match, void *opaque)
6078ca7017Sjakllsch {
6178ca7017Sjakllsch 	struct fdt_attach_args * const faa = opaque;
6278ca7017Sjakllsch 
63*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
6478ca7017Sjakllsch }
6578ca7017Sjakllsch 
6678ca7017Sjakllsch static void
fwcfg_fdt_attach(device_t parent,device_t self,void * opaque)6778ca7017Sjakllsch fwcfg_fdt_attach(device_t parent, device_t self, void *opaque)
6878ca7017Sjakllsch {
6978ca7017Sjakllsch 	struct fwcfg_softc *sc = device_private(self);
7078ca7017Sjakllsch 	struct fdt_attach_args * const faa = opaque;
7178ca7017Sjakllsch 	bus_addr_t base;
7278ca7017Sjakllsch 	bus_size_t size;
7378ca7017Sjakllsch 
7478ca7017Sjakllsch 	if (fdtbus_get_reg(faa->faa_phandle, 0, &base, &size) != 0) {
7578ca7017Sjakllsch 		aprint_error_dev(self, "couldn't get registers\n");
7678ca7017Sjakllsch 		return;
7778ca7017Sjakllsch 	}
7878ca7017Sjakllsch 
7978ca7017Sjakllsch 	sc->sc_dev = self;
8078ca7017Sjakllsch 	sc->sc_bst = faa->faa_bst;
8178ca7017Sjakllsch 
8278ca7017Sjakllsch 	if (bus_space_map(sc->sc_bst, base, size, 0, &sc->sc_bsh) != 0) {
8378ca7017Sjakllsch 		aprint_error_dev(self, "couldn't map registers\n");
8478ca7017Sjakllsch 		return;
8578ca7017Sjakllsch 	}
8678ca7017Sjakllsch 
8778ca7017Sjakllsch 	aprint_naive("\n");
8878ca7017Sjakllsch 	aprint_normal("\n");
8978ca7017Sjakllsch 
9078ca7017Sjakllsch 	fwcfg_attach(sc);
9178ca7017Sjakllsch 
9278ca7017Sjakllsch 	pmf_device_register(self, NULL, NULL);
9378ca7017Sjakllsch }
94