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