xref: /netbsd-src/sys/arch/arm/broadcom/bcm2835_bsc_acpi.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /* $NetBSD: bcm2835_bsc_acpi.c,v 1.4 2021/08/07 16:18:43 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2020 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: bcm2835_bsc_acpi.c,v 1.4 2021/08/07 16:18:43 thorpej Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/cpu.h>
35 #include <sys/device.h>
36 
37 #include <dev/acpi/acpireg.h>
38 #include <dev/acpi/acpivar.h>
39 #include <dev/acpi/acpi_intr.h>
40 #include <dev/acpi/acpi_i2c.h>
41 
42 #include <arm/broadcom/bcm2835var.h>
43 #include <arm/broadcom/bcm2835_mbox.h>
44 #include <arm/broadcom/bcm2835_bscvar.h>
45 
46 #include <evbarm/rpi/vcio.h>
47 #include <evbarm/rpi/vcpm.h>
48 #include <evbarm/rpi/vcprop.h>
49 
50 static int	bsciic_acpi_match(device_t, cfdata_t, void *);
51 static void	bsciic_acpi_attach(device_t, device_t, void *);
52 
53 static u_int	bsciic_acpi_vpu_clock_rate(void);
54 
55 CFATTACH_DECL_NEW(bsciic_acpi, sizeof(struct bsciic_softc), bsciic_acpi_match, bsciic_acpi_attach, NULL, NULL);
56 
57 static const char * const compatible[] = {
58 	"BCM2841",
59 	NULL
60 };
61 
62 static struct {
63 	struct vcprop_buffer_hdr	vb_hdr;
64 	struct vcprop_tag_clockrate	vbt_vpuclockrate;
65 	struct vcprop_tag end;
66 } vb_vpu __cacheline_aligned = {
67 	.vb_hdr = {
68 		.vpb_len = sizeof(vb_vpu),
69 		.vpb_rcode = VCPROP_PROCESS_REQUEST
70 	},
71 	.vbt_vpuclockrate = {
72 		.tag = {
73 			.vpt_tag = VCPROPTAG_GET_CLOCKRATE,
74 			.vpt_len = VCPROPTAG_LEN(vb_vpu.vbt_vpuclockrate),
75 			.vpt_rcode = VCPROPTAG_REQUEST
76 		},
77 		.id = VCPROP_CLK_CORE
78 	},
79 	.end = {
80 		.vpt_tag = VCPROPTAG_NULL
81 	}
82 };
83 
84 static int
bsciic_acpi_match(device_t parent,cfdata_t cf,void * aux)85 bsciic_acpi_match(device_t parent, cfdata_t cf, void *aux)
86 {
87 	struct acpi_attach_args *aa = aux;
88 
89 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
90 		return 0;
91 
92 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
93 }
94 
95 static void
bsciic_acpi_attach(device_t parent,device_t self,void * aux)96 bsciic_acpi_attach(device_t parent, device_t self, void *aux)
97 {
98 	struct bsciic_softc * const sc = device_private(self);
99 	struct acpi_attach_args *aa = aux;
100 	struct i2cbus_attach_args iba;
101 	struct acpi_resources res;
102 	struct acpi_mem *mem;
103 	struct acpi_irq *irq;
104 	ACPI_STATUS rv;
105 	ACPI_INTEGER clock_freq;
106 	void *ih;
107 
108 	sc->sc_dev = self;
109 
110 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
111 	    &res, &acpi_resource_parse_ops_default);
112 	if (ACPI_FAILURE(rv))
113 		return;
114 
115 	mem = acpi_res_mem(&res, 0);
116 	if (mem == NULL) {
117 		aprint_error_dev(self, "couldn't find mem resource\n");
118 		goto done;
119 	}
120 
121 	irq = acpi_res_irq(&res, 0);
122 	if (irq == NULL) {
123 		aprint_error_dev(self, "couldn't find irq resource\n");
124 		goto done;
125 	}
126 
127 	sc->sc_dev = self;
128 	sc->sc_iot = aa->aa_memt;
129 	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0, &sc->sc_ioh) != 0) {
130 		aprint_error_dev(self, "couldn't map registers\n");
131 		goto done;
132 	}
133 
134 	sc->sc_frequency = bsciic_acpi_vpu_clock_rate();
135 	if (sc->sc_frequency == 0) {
136 		aprint_error_dev(self, "couldn't determine parent clock rate\n");
137 		goto done;
138 	}
139 
140 	rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency", &clock_freq);
141 	if (ACPI_SUCCESS(rv))
142 		sc->sc_clkrate = clock_freq;
143 	else
144 		sc->sc_clkrate = 100000;
145 
146 	bsciic_attach(sc);
147 
148 	ih = acpi_intr_establish(self, (uint64_t)aa->aa_node->ad_handle,
149 	    IPL_VM, true, bsciic_intr, sc, device_xname(self));
150 	if (ih == NULL) {
151 		aprint_error_dev(self, "couldn't install interrupt handler\n");
152 		goto done;
153 	}
154 
155 	iic_tag_init(&sc->sc_i2c);
156 	sc->sc_i2c.ic_cookie = sc;
157 	sc->sc_i2c.ic_acquire_bus = bsciic_acquire_bus;
158 	sc->sc_i2c.ic_release_bus = bsciic_release_bus;
159 	sc->sc_i2c.ic_exec = bsciic_exec;
160 
161 	memset(&iba, 0, sizeof(iba));
162 	iba.iba_tag = &sc->sc_i2c;
163 	iba.iba_child_devices = acpi_enter_i2c_devs(self, aa->aa_node);
164 	config_found(self, &iba, iicbus_print, CFARGS_NONE);
165 
166 done:
167 	acpi_resource_cleanup(&res);
168 }
169 
170 static u_int
bsciic_acpi_vpu_clock_rate(void)171 bsciic_acpi_vpu_clock_rate(void)
172 {
173 	uint32_t res;
174 	int error;
175 
176 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb_vpu,
177 	    sizeof(vb_vpu), &res);
178 	if (error != 0) {
179 		printf("%s: mbox request failed (%d)\n", __func__, error);
180 		return 0;
181 	}
182 
183 	if (!vcprop_buffer_success_p(&vb_vpu.vb_hdr) ||
184 	    !vcprop_tag_success_p(&vb_vpu.vbt_vpuclockrate.tag) ||
185 	    vb_vpu.vbt_vpuclockrate.rate < 0)
186 		return 0;
187 
188 	return vb_vpu.vbt_vpuclockrate.rate;
189 }
190