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