1 1.7 skrll /* $NetBSD: cycv_gmac.c,v 1.7 2024/08/10 12:16:46 skrll Exp $ */ 2 1.1 aymeric 3 1.1 aymeric /*- 4 1.1 aymeric * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca> 5 1.1 aymeric * All rights reserved. 6 1.1 aymeric * 7 1.1 aymeric * Redistribution and use in source and binary forms, with or without 8 1.1 aymeric * modification, are permitted provided that the following conditions 9 1.1 aymeric * are met: 10 1.1 aymeric * 1. Redistributions of source code must retain the above copyright 11 1.1 aymeric * notice, this list of conditions and the following disclaimer. 12 1.1 aymeric * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 aymeric * notice, this list of conditions and the following disclaimer in the 14 1.1 aymeric * documentation and/or other materials provided with the distribution. 15 1.1 aymeric * 16 1.1 aymeric * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 aymeric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 aymeric * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 aymeric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 aymeric * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 aymeric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 aymeric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 aymeric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 aymeric * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 aymeric * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 aymeric * POSSIBILITY OF SUCH DAMAGE. 27 1.1 aymeric */ 28 1.1 aymeric 29 1.1 aymeric /* Taken from sunxi_gmac.c */ 30 1.1 aymeric 31 1.1 aymeric #include <sys/cdefs.h> 32 1.1 aymeric 33 1.7 skrll __KERNEL_RCSID(0, "$NetBSD: cycv_gmac.c,v 1.7 2024/08/10 12:16:46 skrll Exp $"); 34 1.1 aymeric 35 1.1 aymeric #include <sys/param.h> 36 1.1 aymeric #include <sys/bus.h> 37 1.1 aymeric #include <sys/device.h> 38 1.1 aymeric #include <sys/intr.h> 39 1.1 aymeric #include <sys/systm.h> 40 1.1 aymeric #include <sys/gpio.h> 41 1.3 msaitoh #include <sys/rndsource.h> 42 1.1 aymeric 43 1.1 aymeric #include <net/if.h> 44 1.1 aymeric #include <net/if_ether.h> 45 1.1 aymeric #include <net/if_media.h> 46 1.1 aymeric 47 1.1 aymeric #include <dev/mii/miivar.h> 48 1.1 aymeric 49 1.1 aymeric #include <dev/ic/dwc_gmac_var.h> 50 1.1 aymeric #include <dev/ic/dwc_gmac_reg.h> 51 1.1 aymeric 52 1.1 aymeric #include <dev/fdt/fdtvar.h> 53 1.1 aymeric 54 1.5 thorpej static const struct device_compatible_entry compat_data[] = { 55 1.5 thorpej { .compat = "altr,socfpga-stmmac" }, 56 1.5 thorpej DEVICE_COMPAT_EOL 57 1.5 thorpej }; 58 1.1 aymeric 59 1.1 aymeric static int 60 1.1 aymeric cycv_gmac_intr(void *arg) 61 1.1 aymeric { 62 1.1 aymeric return dwc_gmac_intr(arg); 63 1.1 aymeric } 64 1.1 aymeric 65 1.1 aymeric static int 66 1.1 aymeric cycv_gmac_match(device_t parent, cfdata_t cf, void *aux) 67 1.1 aymeric { 68 1.1 aymeric struct fdt_attach_args * const faa = aux; 69 1.1 aymeric 70 1.5 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 71 1.1 aymeric } 72 1.1 aymeric 73 1.1 aymeric static void 74 1.1 aymeric cycv_gmac_attach(device_t parent, device_t self, void *aux) 75 1.1 aymeric { 76 1.1 aymeric struct dwc_gmac_softc * const sc = device_private(self); 77 1.1 aymeric struct fdt_attach_args * const faa = aux; 78 1.1 aymeric const int phandle = faa->faa_phandle; 79 1.1 aymeric struct clk *clk_gmac; 80 1.1 aymeric struct fdtbus_reset *rst_gmac; 81 1.1 aymeric const char *phy_mode; 82 1.1 aymeric char intrstr[128]; 83 1.1 aymeric bus_addr_t addr; 84 1.1 aymeric bus_size_t size; 85 1.1 aymeric 86 1.1 aymeric if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 87 1.1 aymeric aprint_error(": couldn't get registers\n"); 88 1.1 aymeric return; 89 1.1 aymeric } 90 1.1 aymeric 91 1.1 aymeric sc->sc_dev = self; 92 1.1 aymeric sc->sc_bst = faa->faa_bst; 93 1.1 aymeric if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 94 1.1 aymeric aprint_error(": couldn't map registers\n"); 95 1.1 aymeric return; 96 1.1 aymeric } 97 1.1 aymeric sc->sc_dmat = faa->faa_dmat; 98 1.1 aymeric 99 1.1 aymeric if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof intrstr)) { 100 1.1 aymeric aprint_error(": failed to decode interrupt\n"); 101 1.1 aymeric return; 102 1.1 aymeric } 103 1.1 aymeric 104 1.1 aymeric clk_gmac = fdtbus_clock_get(phandle, "stmmaceth"); 105 1.1 aymeric if (clk_gmac == NULL) { 106 1.1 aymeric aprint_error(": couldn't get clock\n"); 107 1.1 aymeric return; 108 1.1 aymeric } 109 1.1 aymeric 110 1.1 aymeric rst_gmac = fdtbus_reset_get(phandle, "stmmaceth"); 111 1.1 aymeric if (rst_gmac == NULL) { 112 1.1 aymeric aprint_error(": couldn't get reset\n"); 113 1.1 aymeric return; 114 1.1 aymeric } 115 1.1 aymeric 116 1.1 aymeric phy_mode = fdtbus_get_string(phandle, "phy-mode"); 117 1.1 aymeric if (phy_mode == NULL) { 118 1.1 aymeric aprint_error(": missing 'phy-mode' property\n"); 119 1.1 aymeric return; 120 1.1 aymeric } 121 1.1 aymeric 122 1.1 aymeric if (clk_enable(clk_gmac) != 0) { 123 1.1 aymeric aprint_error(": couldn't enable clocks\n"); 124 1.1 aymeric return; 125 1.1 aymeric } 126 1.1 aymeric 127 1.1 aymeric if (rst_gmac != NULL && fdtbus_reset_deassert(rst_gmac) != 0) { 128 1.1 aymeric aprint_error(": couldn't de-assert reset\n"); 129 1.1 aymeric return; 130 1.1 aymeric } 131 1.1 aymeric 132 1.1 aymeric aprint_naive("\n"); 133 1.1 aymeric aprint_normal(": GMAC\n"); 134 1.1 aymeric 135 1.7 skrll if (fdtbus_intr_establish_xname(phandle, 0, IPL_NET, FDT_INTR_MPSAFE, 136 1.6 skrll cycv_gmac_intr, sc, device_xname(sc->sc_dev)) == NULL) { 137 1.1 aymeric aprint_error_dev(self, "failed to establish interrupt on %s\n", 138 1.1 aymeric intrstr); 139 1.1 aymeric return; 140 1.1 aymeric } 141 1.1 aymeric aprint_normal_dev(self, "interrupting on %s\n", intrstr); 142 1.1 aymeric 143 1.2 martin dwc_gmac_attach(sc, MII_PHY_ANY, GMAC_MII_CLK_150_250M_DIV102); 144 1.1 aymeric } 145 1.1 aymeric 146 1.1 aymeric CFATTACH_DECL_NEW(cycv_gmac, sizeof(struct dwc_gmac_softc), 147 1.1 aymeric cycv_gmac_match, cycv_gmac_attach, NULL, NULL); 148