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