1/*	$NetBSD: bcm2835_mbox_fdt.c,v 1.3 2021/01/29 14:11:14 skrll Exp $	*/
2
3/*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nick Hudson
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: bcm2835_mbox_fdt.c,v 1.3 2021/01/29 14:11:14 skrll Exp $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/device.h>
38#include <sys/kernel.h>
39#include <sys/timetc.h>
40#include <sys/bus.h>
41#include <sys/mutex.h>
42
43#include <arm/broadcom/bcm2835_mbox.h>
44#include <arm/broadcom/bcm2835_mboxreg.h>
45#include <arm/broadcom/bcm2835reg.h>
46#include <arm/broadcom/bcm2835var.h>
47
48#include <dev/fdt/fdtvar.h>
49
50static int bcmmbox_fdt_match(device_t, cfdata_t, void *);
51static void bcmmbox_fdt_attach(device_t, device_t, void *);
52
53CFATTACH_DECL_NEW(bcmmbox_fdt, sizeof(struct bcm2835mbox_softc),
54    bcmmbox_fdt_match, bcmmbox_fdt_attach, NULL, NULL);
55
56static const struct device_compatible_entry compat_data[] = {
57	{ .compat = "brcm,bcm2835-mbox" },
58	DEVICE_COMPAT_EOL
59};
60
61/* ARGSUSED */
62static int
63bcmmbox_fdt_match(device_t parent, cfdata_t match, void *aux)
64{
65	struct fdt_attach_args * const faa = aux;
66
67	return of_compatible_match(faa->faa_phandle, compat_data);
68}
69
70static void
71bcmmbox_fdt_attach(device_t parent, device_t self, void *aux)
72{
73	struct bcm2835mbox_softc *sc = device_private(self);
74	struct fdt_attach_args * const faa = aux;
75	const int phandle = faa->faa_phandle;
76	char intrstr[128];
77	bus_addr_t addr;
78	bus_size_t size;
79
80	sc->sc_dev = self;
81	sc->sc_iot = faa->faa_bst;
82	sc->sc_dmat = faa->faa_dmat;
83
84	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
85		aprint_error(": couldn't get register address\n");
86		return;
87	}
88
89	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh) != 0) {
90		aprint_error_dev(sc->sc_dev, "unable to map device\n");
91		return;
92	}
93
94	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
95		aprint_error(": failed to decode interrupt\n");
96		return;
97	}
98
99	aprint_naive("\n");
100	aprint_normal(": VC mailbox\n");
101
102	sc->sc_intrh = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
103	    bcmmbox_intr, sc, device_xname(self));
104	if (sc->sc_intrh == NULL) {
105		aprint_error_dev(self, "failed to establish interrupt %s\n",
106		    intrstr);
107		return;
108	}
109	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
110
111	bcmmbox_attach(sc);
112}
113