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