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