Home | History | Annotate | Line # | Download | only in acpi
      1 /* $NetBSD: eqos_acpi.c,v 1.2 2023/12/24 16:12:54 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2022 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: eqos_acpi.c,v 1.2 2023/12/24 16:12:54 skrll Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/cpu.h>
     35 #include <sys/device.h>
     36 #include <sys/kmem.h>
     37 
     38 #include <sys/rndsource.h>
     39 
     40 #include <net/if.h>
     41 #include <net/if_dl.h>
     42 #include <net/if_ether.h>
     43 #include <net/if_media.h>
     44 
     45 #include <dev/mii/miivar.h>
     46 
     47 #include <dev/ic/dwc_eqos_var.h>
     48 
     49 #include <dev/acpi/acpireg.h>
     50 #include <dev/acpi/acpivar.h>
     51 #include <dev/acpi/acpi_intr.h>
     52 
     53 #define	CSR_RATE_RGMII		125000000
     54 
     55 static const struct device_compatible_entry compat_data[] = {
     56 	{ .compat = "snps,dwmac-4.20a" },	/* DT link */
     57 	DEVICE_COMPAT_EOL
     58 };
     59 
     60 static int	eqos_acpi_match(device_t, cfdata_t, void *);
     61 static void	eqos_acpi_attach(device_t, device_t, void *);
     62 
     63 static void	eqos_acpi_init_props(struct eqos_softc *, ACPI_HANDLE);
     64 
     65 CFATTACH_DECL_NEW(eqos_acpi, sizeof(struct eqos_softc),
     66     eqos_acpi_match, eqos_acpi_attach, NULL, NULL);
     67 
     68 static int
     69 eqos_acpi_match(device_t parent, cfdata_t cf, void *aux)
     70 {
     71 	struct acpi_attach_args *aa = aux;
     72 
     73 	return acpi_compatible_match(aa, compat_data);
     74 }
     75 
     76 static void
     77 eqos_acpi_attach(device_t parent, device_t self, void *aux)
     78 {
     79 	struct eqos_softc * const sc = device_private(self);
     80 	struct acpi_attach_args *aa = aux;
     81 	ACPI_HANDLE handle = aa->aa_node->ad_handle;
     82 	struct acpi_resources res;
     83 	struct acpi_mem *mem;
     84 	struct acpi_irq *irq;
     85 	ACPI_STATUS rv;
     86 	int error;
     87 	void *ih;
     88 
     89 	sc->sc_dev = self;
     90 
     91 	rv = acpi_resource_parse(sc->sc_dev, handle, "_CRS",
     92 	    &res, &acpi_resource_parse_ops_default);
     93 	if (ACPI_FAILURE(rv))
     94 		return;
     95 
     96 	mem = acpi_res_mem(&res, 0);
     97 	if (mem == NULL) {
     98 		aprint_error_dev(self, "couldn't find mem resource\n");
     99 		goto done;
    100 	}
    101 
    102 	irq = acpi_res_irq(&res, 0);
    103 	if (irq == NULL) {
    104 		aprint_error_dev(self, "couldn't find irq resource\n");
    105 		goto done;
    106 	}
    107 
    108 	sc->sc_bst = aa->aa_memt;
    109 	error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length,
    110 	    0, &sc->sc_bsh);
    111 	if (error != 0) {
    112 		aprint_error_dev(self, "couldn't map registers\n");
    113 		goto done;
    114 	}
    115 	sc->sc_dmat = BUS_DMA_TAG_VALID(aa->aa_dmat64) ?
    116 	    aa->aa_dmat64 : aa->aa_dmat;
    117 	sc->sc_phy_id = MII_PHY_ANY;
    118 	sc->sc_csr_clock = CSR_RATE_RGMII;
    119 
    120 	eqos_acpi_init_props(sc, handle);
    121 
    122 	aprint_normal("%s", device_xname(self));
    123 	if (eqos_attach(sc) != 0)
    124 		goto done;
    125 
    126         ih = acpi_intr_establish(self, (uint64_t)(uintptr_t)handle, IPL_NET,
    127 	    true, eqos_intr, sc, device_xname(self));
    128 	if (ih == NULL) {
    129 		aprint_error_dev(self, "couldn't establish interrupt\n");
    130 		goto done;
    131 	}
    132 
    133 done:
    134 	acpi_resource_cleanup(&res);
    135 }
    136 
    137 static void
    138 eqos_acpi_init_props(struct eqos_softc *sc, ACPI_HANDLE handle)
    139 {
    140 	prop_dictionary_t prop = device_properties(sc->sc_dev);
    141 	ACPI_STATUS rv;
    142 	ACPI_INTEGER ival;
    143 #if notyet
    144 	ACPI_HANDLE axi;
    145 	char *sval;
    146 #endif
    147 
    148 	/* Defaults */
    149 	prop_dictionary_set_uint(prop, "snps,wr_osr_lmt", 4);
    150 	prop_dictionary_set_uint(prop, "snps,rd_osr_lmt", 8);
    151 
    152 	/* Get settings from DSD properties */
    153 	rv = acpi_dsd_integer(handle, "snps,mixed-burst", &ival);
    154 	if (ACPI_SUCCESS(rv) && ival) {
    155 		prop_dictionary_set_bool(prop, "snps,mixed-burst", true);
    156 	}
    157 	rv = acpi_dsd_integer(handle, "snps,tso", &ival);
    158 	if (ACPI_SUCCESS(rv) && ival) {
    159 		prop_dictionary_set_bool(prop, "snps,tso", true);
    160 	}
    161 
    162 #if notyet
    163 	rv = acpi_dsd_string(handle, "snps,axi-config", &sval);
    164 	if (ACPI_SUCCESS(rv)) {
    165 		rv = AcpiGetHandle(handle, sval, &axi);
    166 		if (ACPI_SUCCESS(rv)) {
    167 			rv = acpi_dsd_integer(axi, "snps,wr_osr_lmt", &ival);
    168 			if (ACPI_SUCCESS(rv) && ival > 0) {
    169 				prop_dictionary_set_uint(prop,
    170 				    "snps,wr_osr_lmt", ival);
    171 			}
    172 			rv = acpi_dsd_integer(axi, "snps,rd_osr_lmt", &ival);
    173 			if (ACPI_SUCCESS(rv) && ival > 0) {
    174 				prop_dictionary_set_uint(prop,
    175 				    "snps,rd_osr_lmt", ival);
    176 			}
    177 		}
    178 		kmem_strfree(sval);
    179 	}
    180 #endif
    181 }
    182