vchiq_netbsd_acpi.c revision 1.3
1/* $NetBSD: vchiq_netbsd_acpi.c,v 1.3 2020/12/01 04:19:04 rin Exp $ */
2
3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared D. McNeill
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: vchiq_netbsd_acpi.c,v 1.3 2020/12/01 04:19:04 rin 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/bus.h>
40#include <sys/sysctl.h>
41
42#include <arm/broadcom/bcm2835reg.h>
43#include <arm/broadcom/bcm2835_intr.h>
44
45#include <dev/acpi/acpivar.h>
46#include <dev/acpi/acpi_intr.h>
47
48#include "vchiq_arm.h"
49#include "vchiq_2835.h"
50#include "vchiq_netbsd.h"
51
52struct vchiq_acpi_softc {
53	struct vchiq_softc sc_vchiq;
54	ACPI_HANDLE sc_handle;
55};
56
57static int vchiq_acpi_match(device_t, cfdata_t, void *);
58static void vchiq_acpi_attach(device_t, device_t, void *);
59
60static void vchiq_acpi_defer(device_t);
61
62/* External functions */
63int vchiq_init(void);
64
65
66CFATTACH_DECL_NEW(vchiq_acpi, sizeof(struct vchiq_acpi_softc),
67    vchiq_acpi_match, vchiq_acpi_attach, NULL, NULL);
68
69static int
70vchiq_acpi_match(device_t parent, cfdata_t match, void *aux)
71{
72	const char * const compatible[] = { "BCM2835", NULL };
73	struct acpi_attach_args * const aa = aux;
74
75	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
76		return 0;
77
78	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
79}
80
81static void
82vchiq_acpi_attach(device_t parent, device_t self, void *aux)
83{
84	struct vchiq_acpi_softc *asc = device_private(self);
85	struct vchiq_softc *sc = &asc->sc_vchiq;
86	struct acpi_attach_args * const aa = aux;
87	struct acpi_resources res;
88	struct acpi_mem *mem;
89	struct acpi_irq *irq;
90	ACPI_STATUS rv;
91
92	sc->sc_dev = self;
93	sc->sc_iot = aa->aa_memt;
94	asc->sc_handle = aa->aa_node->ad_handle;
95
96#if BYTE_ORDER == BIG_ENDIAN
97	aprint_error_dev(sc->sc_dev, "not supported yet in big-endian mode\n");
98	return;
99#endif
100
101	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
102	    &res, &acpi_resource_parse_ops_default);
103	if (ACPI_FAILURE(rv))
104		return;
105
106	mem = acpi_res_mem(&res, 0);
107	irq = acpi_res_irq(&res, 0);
108	if (mem == NULL || irq == NULL) {
109		aprint_error_dev(self, "couldn't find resources\n");
110		return;
111	}
112
113	if (bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0,
114	    &sc->sc_ioh) != 0) {
115		aprint_error_dev(sc->sc_dev, "unable to map device\n");
116		return;
117	}
118
119	vchiq_platform_attach(aa->aa_dmat);
120
121	vchiq_set_softc(sc);
122
123	config_mountroot(self, vchiq_acpi_defer);
124}
125
126static void
127vchiq_acpi_defer(device_t self)
128{
129	struct vchiq_attach_args vaa;
130	struct vchiq_acpi_softc *asc = device_private(self);
131	struct vchiq_softc *sc = &asc->sc_vchiq;
132	ACPI_HANDLE handle = asc->sc_handle;
133
134	vchiq_core_initialize();
135
136	sc->sc_ih = acpi_intr_establish(sc->sc_dev, (uint64_t)handle,
137	    IPL_VM, true, vchiq_intr, sc, device_xname(self));
138	if (sc->sc_ih == NULL) {
139		aprint_error_dev(sc->sc_dev, "failed to establish interrupt\n");
140		return;
141	}
142
143	vchiq_init();
144
145	vaa.vaa_name = "AUDS";
146	config_found_ia(self, "vchiqbus", &vaa, vchiq_print);
147}
148