xref: /netbsd-src/sys/arch/arm/broadcom/bcm2835_mbox_fdt.c (revision bd99f6a84c5892c96ec1dad1839ea46f7e809c7a)
1*bd99f6a8Sskrll /*	$NetBSD: bcm2835_mbox_fdt.c,v 1.3 2021/01/29 14:11:14 skrll Exp $	*/
28f3c29f9Sjmcneill 
38f3c29f9Sjmcneill /*-
48f3c29f9Sjmcneill  * Copyright (c) 2012 The NetBSD Foundation, Inc.
58f3c29f9Sjmcneill  * All rights reserved.
68f3c29f9Sjmcneill  *
78f3c29f9Sjmcneill  * This code is derived from software contributed to The NetBSD Foundation
88f3c29f9Sjmcneill  * by Nick Hudson
98f3c29f9Sjmcneill  *
108f3c29f9Sjmcneill  * Redistribution and use in source and binary forms, with or without
118f3c29f9Sjmcneill  * modification, are permitted provided that the following conditions
128f3c29f9Sjmcneill  * are met:
138f3c29f9Sjmcneill  * 1. Redistributions of source code must retain the above copyright
148f3c29f9Sjmcneill  *    notice, this list of conditions and the following disclaimer.
158f3c29f9Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
168f3c29f9Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
178f3c29f9Sjmcneill  *    documentation and/or other materials provided with the distribution.
188f3c29f9Sjmcneill  *
198f3c29f9Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
208f3c29f9Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
218f3c29f9Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
228f3c29f9Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
238f3c29f9Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
248f3c29f9Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
258f3c29f9Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
268f3c29f9Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
278f3c29f9Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
288f3c29f9Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
298f3c29f9Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
308f3c29f9Sjmcneill  */
318f3c29f9Sjmcneill 
328f3c29f9Sjmcneill #include <sys/cdefs.h>
33*bd99f6a8Sskrll __KERNEL_RCSID(0, "$NetBSD: bcm2835_mbox_fdt.c,v 1.3 2021/01/29 14:11:14 skrll Exp $");
348f3c29f9Sjmcneill 
358f3c29f9Sjmcneill #include <sys/param.h>
368f3c29f9Sjmcneill #include <sys/systm.h>
378f3c29f9Sjmcneill #include <sys/device.h>
388f3c29f9Sjmcneill #include <sys/kernel.h>
398f3c29f9Sjmcneill #include <sys/timetc.h>
408f3c29f9Sjmcneill #include <sys/bus.h>
418f3c29f9Sjmcneill #include <sys/mutex.h>
428f3c29f9Sjmcneill 
438f3c29f9Sjmcneill #include <arm/broadcom/bcm2835_mbox.h>
448f3c29f9Sjmcneill #include <arm/broadcom/bcm2835_mboxreg.h>
458f3c29f9Sjmcneill #include <arm/broadcom/bcm2835reg.h>
468f3c29f9Sjmcneill #include <arm/broadcom/bcm2835var.h>
478f3c29f9Sjmcneill 
488f3c29f9Sjmcneill #include <dev/fdt/fdtvar.h>
498f3c29f9Sjmcneill 
508f3c29f9Sjmcneill static int bcmmbox_fdt_match(device_t, cfdata_t, void *);
518f3c29f9Sjmcneill static void bcmmbox_fdt_attach(device_t, device_t, void *);
528f3c29f9Sjmcneill 
538f3c29f9Sjmcneill CFATTACH_DECL_NEW(bcmmbox_fdt, sizeof(struct bcm2835mbox_softc),
548f3c29f9Sjmcneill     bcmmbox_fdt_match, bcmmbox_fdt_attach, NULL, NULL);
558f3c29f9Sjmcneill 
566e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
576e54367aSthorpej 	{ .compat = "brcm,bcm2835-mbox" },
586e54367aSthorpej 	DEVICE_COMPAT_EOL
596e54367aSthorpej };
606e54367aSthorpej 
618f3c29f9Sjmcneill /* ARGSUSED */
628f3c29f9Sjmcneill static int
bcmmbox_fdt_match(device_t parent,cfdata_t match,void * aux)638f3c29f9Sjmcneill bcmmbox_fdt_match(device_t parent, cfdata_t match, void *aux)
648f3c29f9Sjmcneill {
658f3c29f9Sjmcneill 	struct fdt_attach_args * const faa = aux;
668f3c29f9Sjmcneill 
676e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
688f3c29f9Sjmcneill }
698f3c29f9Sjmcneill 
708f3c29f9Sjmcneill static void
bcmmbox_fdt_attach(device_t parent,device_t self,void * aux)718f3c29f9Sjmcneill bcmmbox_fdt_attach(device_t parent, device_t self, void *aux)
728f3c29f9Sjmcneill {
738f3c29f9Sjmcneill 	struct bcm2835mbox_softc *sc = device_private(self);
748f3c29f9Sjmcneill 	struct fdt_attach_args * const faa = aux;
758f3c29f9Sjmcneill 	const int phandle = faa->faa_phandle;
768f3c29f9Sjmcneill 	char intrstr[128];
778f3c29f9Sjmcneill 	bus_addr_t addr;
788f3c29f9Sjmcneill 	bus_size_t size;
798f3c29f9Sjmcneill 
808f3c29f9Sjmcneill 	sc->sc_dev = self;
818f3c29f9Sjmcneill 	sc->sc_iot = faa->faa_bst;
828f3c29f9Sjmcneill 	sc->sc_dmat = faa->faa_dmat;
838f3c29f9Sjmcneill 
848f3c29f9Sjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
858f3c29f9Sjmcneill 		aprint_error(": couldn't get register address\n");
868f3c29f9Sjmcneill 		return;
878f3c29f9Sjmcneill 	}
888f3c29f9Sjmcneill 
898f3c29f9Sjmcneill 	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh) != 0) {
908f3c29f9Sjmcneill 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
918f3c29f9Sjmcneill 		return;
928f3c29f9Sjmcneill 	}
938f3c29f9Sjmcneill 
948f3c29f9Sjmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
958f3c29f9Sjmcneill 		aprint_error(": failed to decode interrupt\n");
968f3c29f9Sjmcneill 		return;
978f3c29f9Sjmcneill 	}
988f3c29f9Sjmcneill 
998f3c29f9Sjmcneill 	aprint_naive("\n");
1008f3c29f9Sjmcneill 	aprint_normal(": VC mailbox\n");
1018f3c29f9Sjmcneill 
102*bd99f6a8Sskrll 	sc->sc_intrh = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
103*bd99f6a8Sskrll 	    bcmmbox_intr, sc, device_xname(self));
1048f3c29f9Sjmcneill 	if (sc->sc_intrh == NULL) {
1058f3c29f9Sjmcneill 		aprint_error_dev(self, "failed to establish interrupt %s\n",
1068f3c29f9Sjmcneill 		    intrstr);
1078f3c29f9Sjmcneill 		return;
1088f3c29f9Sjmcneill 	}
1098f3c29f9Sjmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1108f3c29f9Sjmcneill 
1118f3c29f9Sjmcneill 	bcmmbox_attach(sc);
1128f3c29f9Sjmcneill }
113