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