Home | History | Annotate | Line # | Download | only in broadcom
      1 /* $NetBSD: bcm2835_bsc_acpi.c,v 1.6 2025/09/15 15:31:44 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.6 2025/09/15 15:31:44 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 acpi_resources res;
    100 	struct acpi_mem *mem;
    101 	struct acpi_irq *irq;
    102 	ACPI_STATUS rv;
    103 	ACPI_INTEGER clock_freq;
    104 	void *ih;
    105 
    106 	sc->sc_dev = self;
    107 
    108 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
    109 	    &res, &acpi_resource_parse_ops_default);
    110 	if (ACPI_FAILURE(rv))
    111 		return;
    112 
    113 	mem = acpi_res_mem(&res, 0);
    114 	if (mem == NULL) {
    115 		aprint_error_dev(self, "couldn't find mem resource\n");
    116 		goto done;
    117 	}
    118 
    119 	irq = acpi_res_irq(&res, 0);
    120 	if (irq == NULL) {
    121 		aprint_error_dev(self, "couldn't find irq resource\n");
    122 		goto done;
    123 	}
    124 
    125 	sc->sc_dev = self;
    126 	sc->sc_iot = aa->aa_memt;
    127 	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0, &sc->sc_ioh) != 0) {
    128 		aprint_error_dev(self, "couldn't map registers\n");
    129 		goto done;
    130 	}
    131 
    132 	sc->sc_frequency = bsciic_acpi_vpu_clock_rate();
    133 	if (sc->sc_frequency == 0) {
    134 		aprint_error_dev(self, "couldn't determine parent clock rate\n");
    135 		goto done;
    136 	}
    137 
    138 	rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency", &clock_freq);
    139 	if (ACPI_SUCCESS(rv))
    140 		sc->sc_clkrate = clock_freq;
    141 	else
    142 		sc->sc_clkrate = 100000;
    143 
    144 	bsciic_attach(sc);
    145 
    146 	ih = acpi_intr_establish(self, (uint64_t)aa->aa_node->ad_handle,
    147 	    IPL_VM, true, bsciic_intr, sc, device_xname(self));
    148 	if (ih == NULL) {
    149 		aprint_error_dev(self, "couldn't install interrupt handler\n");
    150 		goto done;
    151 	}
    152 
    153 	iic_tag_init(&sc->sc_i2c);
    154 	sc->sc_i2c.ic_cookie = sc;
    155 	sc->sc_i2c.ic_acquire_bus = bsciic_acquire_bus;
    156 	sc->sc_i2c.ic_release_bus = bsciic_release_bus;
    157 	sc->sc_i2c.ic_exec = bsciic_exec;
    158 
    159 	iicbus_attach(self, &sc->sc_i2c);
    160 
    161 done:
    162 	acpi_resource_cleanup(&res);
    163 }
    164 
    165 static u_int
    166 bsciic_acpi_vpu_clock_rate(void)
    167 {
    168 	uint32_t res;
    169 	int error;
    170 
    171 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb_vpu,
    172 	    sizeof(vb_vpu), &res);
    173 	if (error != 0) {
    174 		printf("%s: mbox request failed (%d)\n", __func__, error);
    175 		return 0;
    176 	}
    177 
    178 	if (!vcprop_buffer_success_p(&vb_vpu.vb_hdr) ||
    179 	    !vcprop_tag_success_p(&vb_vpu.vbt_vpuclockrate.tag) ||
    180 	    vb_vpu.vbt_vpuclockrate.rate < 0)
    181 		return 0;
    182 
    183 	return vb_vpu.vbt_vpuclockrate.rate;
    184 }
    185