bcm2835_bsc_acpi.c revision 1.3.2.2 1 /* $NetBSD: bcm2835_bsc_acpi.c,v 1.3.2.2 2021/04/25 22:02:59 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2020 Jared McNeill <jmcneill (at) 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.3.2.2 2021/04/25 22:02:59 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
41 #include <arm/broadcom/bcm2835var.h>
42 #include <arm/broadcom/bcm2835_mbox.h>
43 #include <arm/broadcom/bcm2835_bscvar.h>
44
45 #include <evbarm/rpi/vcio.h>
46 #include <evbarm/rpi/vcpm.h>
47 #include <evbarm/rpi/vcprop.h>
48
49 static int bsciic_acpi_match(device_t, cfdata_t, void *);
50 static void bsciic_acpi_attach(device_t, device_t, void *);
51
52 static u_int bsciic_acpi_vpu_clock_rate(void);
53
54 CFATTACH_DECL_NEW(bsciic_acpi, sizeof(struct bsciic_softc), bsciic_acpi_match, bsciic_acpi_attach, NULL, NULL);
55
56 static const char * const compatible[] = {
57 "BCM2841",
58 NULL
59 };
60
61 static struct {
62 struct vcprop_buffer_hdr vb_hdr;
63 struct vcprop_tag_clockrate vbt_vpuclockrate;
64 struct vcprop_tag end;
65 } vb_vpu __cacheline_aligned = {
66 .vb_hdr = {
67 .vpb_len = sizeof(vb_vpu),
68 .vpb_rcode = VCPROP_PROCESS_REQUEST
69 },
70 .vbt_vpuclockrate = {
71 .tag = {
72 .vpt_tag = VCPROPTAG_GET_CLOCKRATE,
73 .vpt_len = VCPROPTAG_LEN(vb_vpu.vbt_vpuclockrate),
74 .vpt_rcode = VCPROPTAG_REQUEST
75 },
76 .id = VCPROP_CLK_CORE
77 },
78 .end = {
79 .vpt_tag = VCPROPTAG_NULL
80 }
81 };
82
83 static int
84 bsciic_acpi_match(device_t parent, cfdata_t cf, void *aux)
85 {
86 struct acpi_attach_args *aa = aux;
87
88 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
89 return 0;
90
91 return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
92 }
93
94 static void
95 bsciic_acpi_attach(device_t parent, device_t self, void *aux)
96 {
97 struct bsciic_softc * const sc = device_private(self);
98 struct acpi_attach_args *aa = aux;
99 struct i2cbus_attach_args iba;
100 struct acpi_resources res;
101 struct acpi_mem *mem;
102 struct acpi_irq *irq;
103 ACPI_STATUS rv;
104 ACPI_INTEGER clock_freq;
105 void *ih;
106
107 sc->sc_dev = self;
108
109 rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
110 &res, &acpi_resource_parse_ops_default);
111 if (ACPI_FAILURE(rv))
112 return;
113
114 mem = acpi_res_mem(&res, 0);
115 if (mem == NULL) {
116 aprint_error_dev(self, "couldn't find mem resource\n");
117 goto done;
118 }
119
120 irq = acpi_res_irq(&res, 0);
121 if (irq == NULL) {
122 aprint_error_dev(self, "couldn't find irq resource\n");
123 goto done;
124 }
125
126 sc->sc_dev = self;
127 sc->sc_iot = aa->aa_memt;
128 if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0, &sc->sc_ioh) != 0) {
129 aprint_error_dev(self, "couldn't map registers\n");
130 goto done;
131 }
132
133 sc->sc_frequency = bsciic_acpi_vpu_clock_rate();
134 if (sc->sc_frequency == 0) {
135 aprint_error_dev(self, "couldn't determine parent clock rate\n");
136 goto done;
137 }
138
139 rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency", &clock_freq);
140 if (ACPI_SUCCESS(rv))
141 sc->sc_clkrate = clock_freq;
142 else
143 sc->sc_clkrate = 100000;
144
145 bsciic_attach(sc);
146
147 ih = acpi_intr_establish(self, (uint64_t)aa->aa_node->ad_handle,
148 IPL_VM, true, bsciic_intr, sc, device_xname(self));
149 if (ih == NULL) {
150 aprint_error_dev(self, "couldn't install interrupt handler\n");
151 goto done;
152 }
153
154 iic_tag_init(&sc->sc_i2c);
155 sc->sc_i2c.ic_cookie = sc;
156 sc->sc_i2c.ic_acquire_bus = bsciic_acquire_bus;
157 sc->sc_i2c.ic_release_bus = bsciic_release_bus;
158 sc->sc_i2c.ic_exec = bsciic_exec;
159
160 memset(&iba, 0, sizeof(iba));
161 iba.iba_tag = &sc->sc_i2c;
162 config_found(self, &iba, iicbus_print,
163 CFARG_DEVHANDLE, device_handle(self),
164 CFARG_EOL);
165
166 done:
167 acpi_resource_cleanup(&res);
168 }
169
170 static u_int
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