Home | History | Annotate | Line # | Download | only in starfive
jh7110_eqos.c revision 1.2.4.2
      1  1.2.4.2  perseant /* $NetBSD: jh7110_eqos.c,v 1.2.4.2 2025/08/02 05:56:05 perseant Exp $ */
      2  1.2.4.2  perseant 
      3  1.2.4.2  perseant /*-
      4  1.2.4.2  perseant  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  1.2.4.2  perseant  * All rights reserved.
      6  1.2.4.2  perseant  *
      7  1.2.4.2  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.4.2  perseant  * by Nick Hudson
      9  1.2.4.2  perseant  *
     10  1.2.4.2  perseant  * Redistribution and use in source and binary forms, with or without
     11  1.2.4.2  perseant  * modification, are permitted provided that the following conditions
     12  1.2.4.2  perseant  * are met:
     13  1.2.4.2  perseant  * 1. Redistributions of source code must retain the above copyright
     14  1.2.4.2  perseant  *    notice, this list of conditions and the following disclaimer.
     15  1.2.4.2  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.4.2  perseant  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.4.2  perseant  *    documentation and/or other materials provided with the distribution.
     18  1.2.4.2  perseant  *
     19  1.2.4.2  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2.4.2  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2.4.2  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2.4.2  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2.4.2  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2.4.2  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2.4.2  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2.4.2  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2.4.2  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2.4.2  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2.4.2  perseant  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2.4.2  perseant  */
     31  1.2.4.2  perseant 
     32  1.2.4.2  perseant #include <sys/cdefs.h>
     33  1.2.4.2  perseant __KERNEL_RCSID(0, "$NetBSD: jh7110_eqos.c,v 1.2.4.2 2025/08/02 05:56:05 perseant Exp $");
     34  1.2.4.2  perseant 
     35  1.2.4.2  perseant #include <sys/param.h>
     36  1.2.4.2  perseant 
     37  1.2.4.2  perseant #include <sys/param.h>
     38  1.2.4.2  perseant #include <sys/bus.h>
     39  1.2.4.2  perseant #include <sys/device.h>
     40  1.2.4.2  perseant #include <sys/rndsource.h>
     41  1.2.4.2  perseant 
     42  1.2.4.2  perseant #include <net/if_ether.h>
     43  1.2.4.2  perseant #include <net/if_media.h>
     44  1.2.4.2  perseant 
     45  1.2.4.2  perseant #include <dev/fdt/fdtvar.h>
     46  1.2.4.2  perseant 
     47  1.2.4.2  perseant #include <dev/mii/miivar.h>
     48  1.2.4.2  perseant 
     49  1.2.4.2  perseant #include <dev/ic/dwc_eqos_var.h>
     50  1.2.4.2  perseant 
     51  1.2.4.2  perseant #include <prop/proplib.h>
     52  1.2.4.2  perseant 
     53  1.2.4.2  perseant #include <riscv/starfive/jh71x0_eth.h>
     54  1.2.4.2  perseant 
     55  1.2.4.2  perseant struct jh7110_eqos_softc {
     56  1.2.4.2  perseant 	struct eqos_softc	sc_eqos;
     57  1.2.4.2  perseant 	struct jh71x0_eth_softc	sc_jh71x0eth;
     58  1.2.4.2  perseant };
     59  1.2.4.2  perseant 
     60  1.2.4.2  perseant static void
     61  1.2.4.2  perseant jh7110_parse_phyprops(struct jh7110_eqos_softc *jh_sc, int phandle)
     62  1.2.4.2  perseant {
     63  1.2.4.2  perseant 	struct eqos_softc * const sc = &jh_sc->sc_eqos;
     64  1.2.4.2  perseant 	prop_dictionary_t dict = device_properties(sc->sc_dev);
     65  1.2.4.2  perseant 
     66  1.2.4.2  perseant 	if (of_hasprop(phandle, "motorcomm,tx-clk-adj-enabled")) {
     67  1.2.4.2  perseant 		prop_dictionary_set_bool(dict, "motorcomm,tx-clk-adj-enabled",
     68  1.2.4.2  perseant 		    true);
     69  1.2.4.2  perseant 	}
     70  1.2.4.2  perseant 	if (of_hasprop(phandle, "motorcomm,tx-clk-100-inverted")) {
     71  1.2.4.2  perseant 		prop_dictionary_set_bool(dict, "motorcomm,tx-clk-100-inverted",
     72  1.2.4.2  perseant 		    true);
     73  1.2.4.2  perseant 	}
     74  1.2.4.2  perseant 	if (of_hasprop(phandle, "motorcomm,tx-clk-1000-inverted")) {
     75  1.2.4.2  perseant 		prop_dictionary_set_bool(dict, "motorcomm,tx-clk-1000-inverted",
     76  1.2.4.2  perseant 		    true);
     77  1.2.4.2  perseant 	}
     78  1.2.4.2  perseant 
     79  1.2.4.2  perseant 	uint32_t val;
     80  1.2.4.2  perseant 	if (of_getprop_uint32(phandle,
     81  1.2.4.2  perseant 	    "motorcomm,rx-clk-drv-microamp", &val) == 0) {
     82  1.2.4.2  perseant 		prop_dictionary_set_uint32(dict,
     83  1.2.4.2  perseant 		    "motorcomm,rx-clk-drv-microamp", val);
     84  1.2.4.2  perseant 	}
     85  1.2.4.2  perseant 	if (of_getprop_uint32(phandle,
     86  1.2.4.2  perseant 	    "motorcomm,rx-data-drv-microamp", &val) == 0) {
     87  1.2.4.2  perseant 		prop_dictionary_set_uint32(dict,
     88  1.2.4.2  perseant 		    "motorcomm,rx-data-drv-microamp", val);
     89  1.2.4.2  perseant 	}
     90  1.2.4.2  perseant 	if (of_getprop_uint32(phandle,
     91  1.2.4.2  perseant 	    "rx-internal-delay-ps", &val) == 0) {
     92  1.2.4.2  perseant 		prop_dictionary_set_uint32(dict,
     93  1.2.4.2  perseant 		    "rx-internal-delay-ps", val);
     94  1.2.4.2  perseant 	}
     95  1.2.4.2  perseant 	if (of_getprop_uint32(phandle,
     96  1.2.4.2  perseant 	    "tx-internal-delay-ps", &val) == 0) {
     97  1.2.4.2  perseant 		prop_dictionary_set_uint32(dict,
     98  1.2.4.2  perseant 		    "tx-internal-delay-ps", val);
     99  1.2.4.2  perseant 	}
    100  1.2.4.2  perseant }
    101  1.2.4.2  perseant 
    102  1.2.4.2  perseant /* Compat string(s) */
    103  1.2.4.2  perseant static const struct device_compatible_entry compat_data[] = {
    104  1.2.4.2  perseant 	{ .compat = "starfive,jh7110-dwmac" },
    105  1.2.4.2  perseant 	DEVICE_COMPAT_EOL
    106  1.2.4.2  perseant };
    107  1.2.4.2  perseant 
    108  1.2.4.2  perseant static int
    109  1.2.4.2  perseant jh7110_eqos_match(device_t parent, cfdata_t cf, void *aux)
    110  1.2.4.2  perseant {
    111  1.2.4.2  perseant 	struct fdt_attach_args * const faa = aux;
    112  1.2.4.2  perseant 
    113  1.2.4.2  perseant 	return of_compatible_match(faa->faa_phandle, compat_data);
    114  1.2.4.2  perseant }
    115  1.2.4.2  perseant 
    116  1.2.4.2  perseant static void
    117  1.2.4.2  perseant jh7110_eqos_attach(device_t parent, device_t self, void *aux)
    118  1.2.4.2  perseant {
    119  1.2.4.2  perseant 	struct jh7110_eqos_softc * const sc = device_private(self);
    120  1.2.4.2  perseant 	struct jh71x0_eth_softc * const jheth_sc = &sc->sc_jh71x0eth;
    121  1.2.4.2  perseant 	struct eqos_softc * const eqos_sc = &sc->sc_eqos;
    122  1.2.4.2  perseant 	struct fdt_attach_args * const faa = aux;
    123  1.2.4.2  perseant 	const int phandle = faa->faa_phandle;
    124  1.2.4.2  perseant 	char intrstr[128];
    125  1.2.4.2  perseant 	int error;
    126  1.2.4.2  perseant 
    127  1.2.4.2  perseant 	sc->sc_eqos.sc_dev = self;
    128  1.2.4.2  perseant 	sc->sc_eqos.sc_bst = faa->faa_bst;
    129  1.2.4.2  perseant 	sc->sc_eqos.sc_dmat = faa->faa_dmat;
    130  1.2.4.2  perseant 	sc->sc_jh71x0eth.sc_dev = self;
    131  1.2.4.2  perseant 	sc->sc_jh71x0eth.sc_phy = MII_PHY_ANY;
    132  1.2.4.2  perseant 	sc->sc_jh71x0eth.sc_type = JH71X0ETH_EQOS;
    133  1.2.4.2  perseant 
    134  1.2.4.2  perseant 	error = jh71x0_eth_attach(jheth_sc, faa, &sc->sc_eqos.sc_bsh);
    135  1.2.4.2  perseant 	if (error)
    136  1.2.4.2  perseant 		return;
    137  1.2.4.2  perseant 
    138  1.2.4.2  perseant 	jh7110_parse_phyprops(sc, sc->sc_jh71x0eth.sc_phandle_phy);
    139  1.2.4.2  perseant 
    140  1.2.4.2  perseant //	aprint_naive("\n");
    141  1.2.4.2  perseant //	aprint_normal(": Gigabit Ethernet Controller\n");
    142  1.2.4.2  perseant 
    143  1.2.4.2  perseant #define CSR_RATE_RGMII	125000000	/* default */
    144  1.2.4.2  perseant 	eqos_sc->sc_csr_clock = CSR_RATE_RGMII;
    145  1.2.4.2  perseant 	error = eqos_attach(eqos_sc);
    146  1.2.4.2  perseant 	if (error)
    147  1.2.4.2  perseant 		return;
    148  1.2.4.2  perseant 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    149  1.2.4.2  perseant 		aprint_error(": failed to decode interrupt\n");
    150  1.2.4.2  perseant 		return;
    151  1.2.4.2  perseant 	}
    152  1.2.4.2  perseant 	void *ih = fdtbus_intr_establish_xname(phandle, 0, IPL_NET,
    153  1.2.4.2  perseant 	    FDT_INTR_MPSAFE, eqos_intr, eqos_sc, device_xname(self));
    154  1.2.4.2  perseant 	if (ih == NULL) {
    155  1.2.4.2  perseant 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    156  1.2.4.2  perseant 		    intrstr);
    157  1.2.4.2  perseant 		// XXXNH unwind
    158  1.2.4.2  perseant 		return;
    159  1.2.4.2  perseant 	}
    160  1.2.4.2  perseant 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    161  1.2.4.2  perseant }
    162  1.2.4.2  perseant 
    163  1.2.4.2  perseant CFATTACH_DECL_NEW(jh7110_eqos, sizeof(struct jh7110_eqos_softc),
    164  1.2.4.2  perseant     jh7110_eqos_match, jh7110_eqos_attach, NULL, NULL);
    165