1 /* $NetBSD: genet_fdt.c,v 1.2 2020/05/25 19:49:28 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2020 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include "opt_net_mpsafe.h" 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: genet_fdt.c,v 1.2 2020/05/25 19:49:28 jmcneill Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/bus.h> 36 #include <sys/device.h> 37 #include <sys/intr.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 41 #include <net/if.h> 42 #include <net/if_dl.h> 43 #include <net/if_ether.h> 44 #include <net/if_media.h> 45 46 #include <dev/mii/miivar.h> 47 48 #include <dev/ic/bcmgenetvar.h> 49 50 #include <dev/fdt/fdtvar.h> 51 52 #ifdef NET_MPSAFE 53 #define FDT_INTR_FLAGS FDT_INTR_MPSAFE 54 #else 55 #define FDT_INTR_FLAGS 0 56 #endif 57 58 static int genet_fdt_match(device_t, cfdata_t, void *); 59 static void genet_fdt_attach(device_t, device_t, void *); 60 61 CFATTACH_DECL_NEW(genet_fdt, sizeof(struct genet_softc), 62 genet_fdt_match, genet_fdt_attach, NULL, NULL); 63 64 static int 65 genet_fdt_match(device_t parent, cfdata_t cf, void *aux) 66 { 67 const char * const compatible[] = { 68 "brcm,bcm2711-genet-v5", 69 NULL 70 }; 71 struct fdt_attach_args * const faa = aux; 72 73 return of_match_compatible(faa->faa_phandle, compatible); 74 } 75 76 static void 77 genet_fdt_attach(device_t parent, device_t self, void *aux) 78 { 79 struct genet_softc * const sc = device_private(self); 80 struct fdt_attach_args * const faa = aux; 81 const int phandle = faa->faa_phandle; 82 char intrstr[128]; 83 bus_addr_t addr; 84 bus_size_t size; 85 void *ih; 86 87 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 88 aprint_error(": couldn't get registers\n"); 89 return; 90 } 91 92 sc->sc_dev = self; 93 sc->sc_bst = faa->faa_bst; 94 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 95 aprint_error(": couldn't map registers\n"); 96 return; 97 } 98 sc->sc_dmat = faa->faa_dmat; 99 sc->sc_phy_id = MII_PHY_ANY; 100 101 const char *phy_mode = fdtbus_get_string(phandle, "phy-mode"); 102 if (phy_mode == NULL) { 103 aprint_error(": missing 'phy-mode' property\n"); 104 return; 105 } 106 107 if (strcmp(phy_mode, "rgmii-rxid") == 0) 108 sc->sc_phy_mode = GENET_PHY_MODE_RGMII_RXID; 109 else if (strcmp(phy_mode, "rgmii-txid") == 0) 110 sc->sc_phy_mode = GENET_PHY_MODE_RGMII_TXID; 111 else if (strcmp(phy_mode, "rgmii-id") == 0) 112 sc->sc_phy_mode = GENET_PHY_MODE_RGMII_ID; 113 else if (strcmp(phy_mode, "rgmii") == 0) 114 sc->sc_phy_mode = GENET_PHY_MODE_RGMII; 115 else { 116 aprint_error(": unsupported phy-mode '%s'\n", phy_mode); 117 return; 118 } 119 120 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 121 aprint_error(": failed to decode interrupt\n"); 122 return; 123 } 124 125 if (genet_attach(sc) != 0) 126 return; 127 128 ih = fdtbus_intr_establish(phandle, 0, IPL_NET, FDT_INTR_FLAGS, 129 genet_intr, sc); 130 if (ih == NULL) { 131 aprint_error_dev(self, "couldn't establish interrupt on %s\n", 132 intrstr); 133 return; 134 } 135 aprint_normal_dev(self, "interrupting on %s\n", intrstr); 136 } 137