bcm2835_com_acpi.c revision 1.1 1 1.1 jmcneill /* $NetBSD: bcm2835_com_acpi.c,v 1.1 2021/08/08 18:43:21 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*
4 1.1 jmcneill * Copyright (c) 2021 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. The name of the author may not be used to endorse or promote products
13 1.1 jmcneill * derived from this software without specific prior written permission.
14 1.1 jmcneill *
15 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 jmcneill * SUCH DAMAGE.
26 1.1 jmcneill */
27 1.1 jmcneill
28 1.1 jmcneill #include <sys/cdefs.h>
29 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: bcm2835_com_acpi.c,v 1.1 2021/08/08 18:43:21 jmcneill Exp $");
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/param.h>
32 1.1 jmcneill #include <sys/device.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.1 jmcneill #include <sys/termios.h>
35 1.1 jmcneill
36 1.1 jmcneill #include <dev/acpi/acpivar.h>
37 1.1 jmcneill #include <dev/acpi/acpi_intr.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <dev/ic/comvar.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <arm/broadcom/bcm2835_mbox.h>
42 1.1 jmcneill #include <evbarm/rpi/vcio.h>
43 1.1 jmcneill #include <evbarm/rpi/vcprop.h>
44 1.1 jmcneill
45 1.1 jmcneill static int bcmcom_acpi_match(device_t, cfdata_t , void *);
46 1.1 jmcneill static void bcmcom_acpi_attach(device_t, device_t, void *);
47 1.1 jmcneill
48 1.1 jmcneill static u_int bcmcom_acpi_get_clockrate(device_t);
49 1.1 jmcneill
50 1.1 jmcneill struct vcmbox_clockrate_request {
51 1.1 jmcneill struct vcprop_buffer_hdr vb_hdr;
52 1.1 jmcneill struct vcprop_tag_clockrate vbt_clockrate;
53 1.1 jmcneill struct vcprop_tag end;
54 1.1 jmcneill } __packed;
55 1.1 jmcneill
56 1.1 jmcneill CFATTACH_DECL_NEW(bcmcom_acpi, sizeof(struct com_softc), bcmcom_acpi_match,
57 1.1 jmcneill bcmcom_acpi_attach, NULL, NULL);
58 1.1 jmcneill
59 1.1 jmcneill static const struct device_compatible_entry compat_data[] = {
60 1.1 jmcneill { .compat = "BCM2836", .value = COM_TYPE_BCMAUXUART },
61 1.1 jmcneill DEVICE_COMPAT_EOL
62 1.1 jmcneill };
63 1.1 jmcneill
64 1.1 jmcneill static int
65 1.1 jmcneill bcmcom_acpi_match(device_t parent, cfdata_t match, void *aux)
66 1.1 jmcneill {
67 1.1 jmcneill struct acpi_attach_args *aa = aux;
68 1.1 jmcneill
69 1.1 jmcneill return acpi_compatible_match(aa, compat_data);
70 1.1 jmcneill }
71 1.1 jmcneill
72 1.1 jmcneill static void
73 1.1 jmcneill bcmcom_acpi_attach(device_t parent, device_t self, void *aux)
74 1.1 jmcneill {
75 1.1 jmcneill struct com_softc *sc = device_private(self);
76 1.1 jmcneill struct acpi_attach_args *aa = aux;
77 1.1 jmcneill const struct device_compatible_entry *dce;
78 1.1 jmcneill struct acpi_resources res;
79 1.1 jmcneill struct acpi_mem *mem;
80 1.1 jmcneill struct acpi_irq *irq;
81 1.1 jmcneill bus_space_tag_t iot;
82 1.1 jmcneill bus_space_handle_t ioh;
83 1.1 jmcneill bus_addr_t base;
84 1.1 jmcneill bus_size_t size;
85 1.1 jmcneill ACPI_STATUS rv;
86 1.1 jmcneill void *ih;
87 1.1 jmcneill
88 1.1 jmcneill sc->sc_dev = self;
89 1.1 jmcneill
90 1.1 jmcneill rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
91 1.1 jmcneill &res, &acpi_resource_parse_ops_default);
92 1.1 jmcneill if (ACPI_FAILURE(rv)) {
93 1.1 jmcneill return;
94 1.1 jmcneill }
95 1.1 jmcneill
96 1.1 jmcneill mem = acpi_res_mem(&res, 0);
97 1.1 jmcneill if (mem == NULL) {
98 1.1 jmcneill aprint_error_dev(self, "couldn't find mem resource\n");
99 1.1 jmcneill goto cleanup;
100 1.1 jmcneill }
101 1.1 jmcneill
102 1.1 jmcneill iot = aa->aa_memt;
103 1.1 jmcneill base = mem->ar_base;
104 1.1 jmcneill size = mem->ar_length;
105 1.1 jmcneill
106 1.1 jmcneill irq = acpi_res_irq(&res, 0);
107 1.1 jmcneill if (irq == NULL) {
108 1.1 jmcneill aprint_error_dev(self, "couldn't find irq resource\n");
109 1.1 jmcneill goto cleanup;
110 1.1 jmcneill }
111 1.1 jmcneill
112 1.1 jmcneill dce = acpi_compatible_lookup(aa, compat_data);
113 1.1 jmcneill KASSERT(dce != NULL);
114 1.1 jmcneill
115 1.1 jmcneill sc->sc_type = dce->value;
116 1.1 jmcneill
117 1.1 jmcneill if (!com_is_console(iot, base, &ioh)) {
118 1.1 jmcneill if (bus_space_map(iot, base, size, 0, &ioh)) {
119 1.1 jmcneill aprint_error_dev(self, "can't map mem space\n");
120 1.1 jmcneill goto cleanup;
121 1.1 jmcneill }
122 1.1 jmcneill }
123 1.1 jmcneill
124 1.1 jmcneill com_init_regs_stride(&sc->sc_regs, iot, ioh, base, 2);
125 1.1 jmcneill
126 1.1 jmcneill aprint_normal("%s", device_xname(self));
127 1.1 jmcneill
128 1.1 jmcneill sc->sc_frequency = bcmcom_acpi_get_clockrate(self);
129 1.1 jmcneill
130 1.1 jmcneill com_attach_subr(sc);
131 1.1 jmcneill
132 1.1 jmcneill ih = acpi_intr_establish(self,
133 1.1 jmcneill (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
134 1.1 jmcneill IPL_SERIAL, true, comintr, sc, device_xname(self));
135 1.1 jmcneill if (ih == NULL) {
136 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt\n");
137 1.1 jmcneill goto cleanup;
138 1.1 jmcneill }
139 1.1 jmcneill
140 1.1 jmcneill if (!pmf_device_register(self, NULL, com_resume))
141 1.1 jmcneill aprint_error_dev(self, "couldn't establish a power handler\n");
142 1.1 jmcneill
143 1.1 jmcneill cleanup:
144 1.1 jmcneill acpi_resource_cleanup(&res);
145 1.1 jmcneill }
146 1.1 jmcneill
147 1.1 jmcneill static u_int
148 1.1 jmcneill bcmcom_acpi_get_clockrate(device_t dev)
149 1.1 jmcneill {
150 1.1 jmcneill struct vcmbox_clockrate_request vb;
151 1.1 jmcneill uint32_t res;
152 1.1 jmcneill int error;
153 1.1 jmcneill
154 1.1 jmcneill VCPROP_INIT_REQUEST(vb);
155 1.1 jmcneill VCPROP_INIT_TAG(vb.vbt_clockrate, VCPROPTAG_GET_CLOCKRATE);
156 1.1 jmcneill vb.vbt_clockrate.id = htole32(VCPROP_CLK_CORE);
157 1.1 jmcneill error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb, sizeof(vb), &res);
158 1.1 jmcneill if (error != 0) {
159 1.1 jmcneill aprint_error_dev(dev, "MBOX request failed: %d\n", error);
160 1.1 jmcneill return 0;
161 1.1 jmcneill }
162 1.1 jmcneill if (!vcprop_buffer_success_p(&vb.vb_hdr) ||
163 1.1 jmcneill !vcprop_tag_success_p(&vb.vbt_clockrate.tag)) {
164 1.1 jmcneill return 0;
165 1.1 jmcneill }
166 1.1 jmcneill
167 1.1 jmcneill return le32toh(vb.vbt_clockrate.rate) * 2;
168 1.1 jmcneill }
169