1/* $NetBSD: genet_fdt.c,v 1.6 2021/05/03 10:28:26 rin 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.6 2021/05/03 10:28:26 rin 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 <sys/rndsource.h> 42 43#include <net/if.h> 44#include <net/if_dl.h> 45#include <net/if_ether.h> 46#include <net/if_media.h> 47 48#include <dev/mii/miivar.h> 49 50#include <dev/ic/bcmgenetvar.h> 51 52#include <dev/fdt/fdtvar.h> 53 54#ifdef NET_MPSAFE 55#define FDT_INTR_FLAGS FDT_INTR_MPSAFE 56#else 57#define FDT_INTR_FLAGS 0 58#endif 59 60static int genet_fdt_match(device_t, cfdata_t, void *); 61static void genet_fdt_attach(device_t, device_t, void *); 62 63CFATTACH_DECL_NEW(genet_fdt, sizeof(struct genet_softc), 64 genet_fdt_match, genet_fdt_attach, NULL, NULL); 65 66static const struct device_compatible_entry compat_data[] = { 67 { .compat = "brcm,bcm2711-genet-v5" }, 68 DEVICE_COMPAT_EOL 69}; 70 71static int 72genet_fdt_match(device_t parent, cfdata_t cf, void *aux) 73{ 74 struct fdt_attach_args * const faa = aux; 75 76 return of_compatible_match(faa->faa_phandle, compat_data); 77} 78 79static void 80genet_fdt_attach(device_t parent, device_t self, void *aux) 81{ 82 struct genet_softc * const sc = device_private(self); 83 struct fdt_attach_args * const faa = aux; 84 const int phandle = faa->faa_phandle; 85 char intrstr[128]; 86 bus_addr_t addr; 87 bus_size_t size; 88 void *ih; 89 90 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 91 aprint_error(": couldn't get registers\n"); 92 return; 93 } 94 95 sc->sc_dev = self; 96 sc->sc_bst = faa->faa_bst; 97 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 98 aprint_error(": couldn't map registers\n"); 99 return; 100 } 101 sc->sc_dmat = faa->faa_dmat; 102 sc->sc_phy_id = MII_PHY_ANY; 103 104 const char *phy_mode = fdtbus_get_string(phandle, "phy-mode"); 105 if (phy_mode == NULL) { 106 aprint_error(": missing 'phy-mode' property\n"); 107 return; 108 } 109 110 if (strcmp(phy_mode, "rgmii-rxid") == 0) 111 sc->sc_phy_mode = GENET_PHY_MODE_RGMII_RXID; 112 else if (strcmp(phy_mode, "rgmii-txid") == 0) 113 sc->sc_phy_mode = GENET_PHY_MODE_RGMII_TXID; 114 else if (strcmp(phy_mode, "rgmii-id") == 0) 115 sc->sc_phy_mode = GENET_PHY_MODE_RGMII_ID; 116 else if (strcmp(phy_mode, "rgmii") == 0) 117 sc->sc_phy_mode = GENET_PHY_MODE_RGMII; 118 else { 119 aprint_error(": unsupported phy-mode '%s'\n", phy_mode); 120 return; 121 } 122 123 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 124 aprint_error(": failed to decode interrupt\n"); 125 return; 126 } 127 128 if (genet_attach(sc) != 0) 129 return; 130 131 ih = fdtbus_intr_establish_xname(phandle, 0, IPL_NET, FDT_INTR_MPSAFE, 132 genet_intr, sc, device_xname(self)); 133 if (ih == NULL) { 134 aprint_error_dev(self, "couldn't establish interrupt on %s\n", 135 intrstr); 136 return; 137 } 138 aprint_normal_dev(self, "interrupting on %s\n", intrstr); 139} 140