Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_bsc_acpi.c revision 1.5
      1 /* $NetBSD: bcm2835_bsc_acpi.c,v 1.5 2025/09/15 15:18:42 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.5 2025/09/15 15:18:42 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
     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
     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 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 	iicbus_attach(self, &sc->sc_i2c);
    161 
    162 done:
    163 	acpi_resource_cleanup(&res);
    164 }
    165 
    166 static u_int
    167 bsciic_acpi_vpu_clock_rate(void)
    168 {
    169 	uint32_t res;
    170 	int error;
    171 
    172 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb_vpu,
    173 	    sizeof(vb_vpu), &res);
    174 	if (error != 0) {
    175 		printf("%s: mbox request failed (%d)\n", __func__, error);
    176 		return 0;
    177 	}
    178 
    179 	if (!vcprop_buffer_success_p(&vb_vpu.vb_hdr) ||
    180 	    !vcprop_tag_success_p(&vb_vpu.vbt_vpuclockrate.tag) ||
    181 	    vb_vpu.vbt_vpuclockrate.rate < 0)
    182 		return 0;
    183 
    184 	return vb_vpu.vbt_vpuclockrate.rate;
    185 }
    186