1 1.1 skrll /* $NetBSD: jh7100_gmac.c,v 1.1 2024/10/26 15:49:43 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.1 skrll __KERNEL_RCSID(0, "$NetBSD: jh7100_gmac.c,v 1.1 2024/10/26 15:49:43 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_gmac_var.h> 50 1.1 skrll #include <dev/ic/dwc_gmac_reg.h> 51 1.1 skrll 52 1.1 skrll #include <prop/proplib.h> 53 1.1 skrll 54 1.1 skrll #include <riscv/starfive/jh71x0_eth.h> 55 1.1 skrll 56 1.1 skrll struct jh7100_gmac_softc { 57 1.1 skrll struct dwc_gmac_softc sc_gmac; 58 1.1 skrll struct jh71x0_eth_softc sc_jh71x0eth; 59 1.1 skrll }; 60 1.1 skrll 61 1.1 skrll static void 62 1.1 skrll jh7100_gmac_set_speed(struct dwc_gmac_softc *gmac_sc, int speed) 63 1.1 skrll { 64 1.1 skrll struct jh7100_gmac_softc * const sc = 65 1.1 skrll container_of(gmac_sc, struct jh7100_gmac_softc, sc_gmac); 66 1.1 skrll struct jh71x0_eth_softc * const jheth_sc = &sc->sc_jh71x0eth; 67 1.1 skrll 68 1.1 skrll u_int rate = clk_get_rate(jheth_sc->sc_clk_tx); 69 1.1 skrll switch (speed) { 70 1.1 skrll case IFM_10_T: 71 1.1 skrll rate = 2500000; 72 1.1 skrll break; 73 1.1 skrll case IFM_100_TX: 74 1.1 skrll rate = 25000000; 75 1.1 skrll break; 76 1.1 skrll case IFM_1000_T: 77 1.1 skrll rate = 125000000; 78 1.1 skrll default: 79 1.1 skrll // error unsupported 80 1.1 skrll break; 81 1.1 skrll } 82 1.1 skrll 83 1.1 skrll int error = clk_set_rate(jheth_sc->sc_clk_tx, rate); 84 1.1 skrll if (error) 85 1.1 skrll aprint_error_dev(jheth_sc->sc_dev, "failed to set TX rate %u", 86 1.1 skrll rate); 87 1.1 skrll } 88 1.1 skrll 89 1.1 skrll static int 90 1.1 skrll jh7100_gmac_intr(void *arg) 91 1.1 skrll { 92 1.1 skrll return dwc_gmac_intr(arg); 93 1.1 skrll } 94 1.1 skrll 95 1.1 skrll /* Compat string(s) */ 96 1.1 skrll static const struct device_compatible_entry compat_data[] = { 97 1.1 skrll { .compat = "starfive,jh7100-dwmac" }, 98 1.1 skrll DEVICE_COMPAT_EOL 99 1.1 skrll }; 100 1.1 skrll 101 1.1 skrll static int 102 1.1 skrll jh7100_gmac_match(device_t parent, cfdata_t cf, void *aux) 103 1.1 skrll { 104 1.1 skrll struct fdt_attach_args * const faa = aux; 105 1.1 skrll 106 1.1 skrll return of_compatible_match(faa->faa_phandle, compat_data); 107 1.1 skrll } 108 1.1 skrll 109 1.1 skrll static void 110 1.1 skrll jh7100_gmac_attach(device_t parent, device_t self, void *aux) 111 1.1 skrll { 112 1.1 skrll struct jh7100_gmac_softc * const sc = device_private(self); 113 1.1 skrll struct jh71x0_eth_softc * const jheth_sc = &sc->sc_jh71x0eth; 114 1.1 skrll struct dwc_gmac_softc * const gmac_sc = &sc->sc_gmac; 115 1.1 skrll struct fdt_attach_args * const faa = aux; 116 1.1 skrll const int phandle = faa->faa_phandle; 117 1.1 skrll char intrstr[128]; 118 1.1 skrll int error; 119 1.1 skrll 120 1.1 skrll sc->sc_gmac.sc_dev = self; 121 1.1 skrll sc->sc_gmac.sc_bst = faa->faa_bst; 122 1.1 skrll sc->sc_gmac.sc_dmat = faa->faa_dmat; 123 1.1 skrll sc->sc_jh71x0eth.sc_dev = self; 124 1.1 skrll sc->sc_jh71x0eth.sc_phy = MII_PHY_ANY; 125 1.1 skrll sc->sc_jh71x0eth.sc_type = JH71X0ETH_GMAC; 126 1.1 skrll 127 1.1 skrll error = jh71x0_eth_attach(jheth_sc, faa, &sc->sc_gmac.sc_bsh); 128 1.1 skrll if (error) 129 1.1 skrll return; 130 1.1 skrll 131 1.1 skrll if (sc->sc_jh71x0eth.sc_clk_tx) { 132 1.1 skrll sc->sc_gmac.sc_set_speed = jh7100_gmac_set_speed; 133 1.1 skrll } 134 1.1 skrll 135 1.1 skrll aprint_naive("\n"); 136 1.1 skrll aprint_normal(": Gigabit Ethernet Controller\n"); 137 1.1 skrll 138 1.1 skrll error = dwc_gmac_attach(gmac_sc, sc->sc_jh71x0eth.sc_phy, 139 1.1 skrll GMAC_MII_CLK_150_250M_DIV102); 140 1.1 skrll if (error) 141 1.1 skrll return; 142 1.1 skrll 143 1.1 skrll if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 144 1.1 skrll aprint_error(": failed to decode interrupt\n"); 145 1.1 skrll return; 146 1.1 skrll } 147 1.1 skrll void *ih = fdtbus_intr_establish_xname(phandle, 0, IPL_NET, 148 1.1 skrll FDT_INTR_MPSAFE, jh7100_gmac_intr, sc, device_xname(self)); 149 1.1 skrll if (ih == NULL) { 150 1.1 skrll aprint_error_dev(self, "failed to establish interrupt on %s\n", 151 1.1 skrll intrstr); 152 1.1 skrll // XXXNH unwind 153 1.1 skrll return; 154 1.1 skrll } 155 1.1 skrll aprint_normal_dev(self, "interrupting on %s\n", intrstr); 156 1.1 skrll } 157 1.1 skrll 158 1.1 skrll 159 1.1 skrll CFATTACH_DECL_NEW(jh7100_gmac, sizeof(struct jh7100_gmac_softc), 160 1.1 skrll jh7100_gmac_match, jh7100_gmac_attach, NULL, NULL); 161