1 1.9 skrll /* $NetBSD: zynq_cemac.c,v 1.10 2024/10/15 00:58:15 lloyd Exp $ */ 2 1.1 skrll /*- 3 1.1 skrll * Copyright (c) 2015 Genetec Corporation. All rights reserved. 4 1.1 skrll * Written by Hashimoto Kenichi for Genetec Corporation. 5 1.1 skrll * 6 1.1 skrll * Redistribution and use in source and binary forms, with or without 7 1.1 skrll * modification, are permitted provided that the following conditions 8 1.1 skrll * are met: 9 1.1 skrll * 1. Redistributions of source code must retain the above copyright 10 1.1 skrll * notice, this list of conditions and the following disclaimer. 11 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 skrll * notice, this list of conditions and the following disclaimer in the 13 1.1 skrll * documentation and/or other materials provided with the distribution. 14 1.1 skrll * 15 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 1.1 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 1.1 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 1.1 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 1.1 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 1.1 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 1.1 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 1.1 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 1.1 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 1.1 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 1.1 skrll * POSSIBILITY OF SUCH DAMAGE. 26 1.1 skrll */ 27 1.1 skrll 28 1.1 skrll #include <sys/cdefs.h> 29 1.9 skrll __KERNEL_RCSID(0, "$NetBSD: zynq_cemac.c,v 1.10 2024/10/15 00:58:15 lloyd Exp $"); 30 1.1 skrll 31 1.1 skrll #include <sys/param.h> 32 1.6 skrll 33 1.1 skrll #include <sys/bus.h> 34 1.1 skrll #include <sys/conf.h> 35 1.1 skrll #include <sys/device.h> 36 1.1 skrll #include <sys/intr.h> 37 1.1 skrll #include <sys/systm.h> 38 1.1 skrll 39 1.1 skrll #include <dev/cadence/if_cemacvar.h> 40 1.1 skrll 41 1.10 lloyd #include <net/if.h> 42 1.10 lloyd #include <net/if_media.h> 43 1.4 jmcneill #include <net/if_ether.h> 44 1.4 jmcneill 45 1.1 skrll #include <dev/fdt/fdtvar.h> 46 1.1 skrll 47 1.10 lloyd #include <dev/mii/mii.h> 48 1.10 lloyd #include <dev/mii/miivar.h> 49 1.10 lloyd 50 1.2 thorpej static const struct device_compatible_entry compat_data[] = { 51 1.2 thorpej { .compat = "cdns,zynq-gem" }, 52 1.2 thorpej DEVICE_COMPAT_EOL 53 1.1 skrll }; 54 1.1 skrll 55 1.9 skrll static int 56 1.10 lloyd cemac_get_phyid(const int phandle) 57 1.10 lloyd { 58 1.10 lloyd bus_addr_t addr; 59 1.10 lloyd int phy_phandle; 60 1.10 lloyd 61 1.10 lloyd phy_phandle = fdtbus_get_phandle(phandle, "phy"); 62 1.10 lloyd if (phy_phandle == -1) 63 1.10 lloyd phy_phandle = fdtbus_get_phandle(phandle, "phy-handle"); 64 1.10 lloyd if (phy_phandle == -1) 65 1.10 lloyd return MII_PHY_ANY; 66 1.10 lloyd 67 1.10 lloyd if (fdtbus_get_reg(phy_phandle, 0, &addr, NULL) != 0) 68 1.10 lloyd return MII_PHY_ANY; 69 1.10 lloyd 70 1.10 lloyd return (int)addr; 71 1.10 lloyd } 72 1.10 lloyd 73 1.10 lloyd static int 74 1.1 skrll cemac_match(device_t parent, cfdata_t cfdata, void *aux) 75 1.1 skrll { 76 1.1 skrll struct fdt_attach_args * const faa = aux; 77 1.1 skrll 78 1.2 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 79 1.1 skrll } 80 1.1 skrll 81 1.9 skrll static void 82 1.1 skrll cemac_attach(device_t parent, device_t self, void *aux) 83 1.1 skrll { 84 1.1 skrll struct fdt_attach_args * const faa = aux; 85 1.9 skrll struct cemac_softc *sc = device_private(self); 86 1.1 skrll const int phandle = faa->faa_phandle; 87 1.4 jmcneill prop_dictionary_t prop = device_properties(self); 88 1.1 skrll char intrstr[128]; 89 1.4 jmcneill const char *macaddr; 90 1.1 skrll bus_addr_t addr; 91 1.1 skrll bus_size_t size; 92 1.4 jmcneill int error, len; 93 1.1 skrll 94 1.1 skrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 95 1.1 skrll aprint_error(": couldn't get registers\n"); 96 1.1 skrll return; 97 1.1 skrll } 98 1.1 skrll 99 1.9 skrll error = bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh); 100 1.1 skrll if (error) { 101 1.1 skrll aprint_error(": failed to map register %#lx@%#lx: %d\n", 102 1.1 skrll size, addr, error); 103 1.1 skrll return; 104 1.1 skrll } 105 1.1 skrll 106 1.1 skrll if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 107 1.1 skrll aprint_error(": failed to decode interrupt\n"); 108 1.1 skrll return; 109 1.1 skrll } 110 1.1 skrll 111 1.1 skrll if (fdtbus_intr_establish(phandle, 0, IPL_NET, 0, cemac_intr, 112 1.3 jmcneill device_private(self)) == NULL) { 113 1.7 skrll aprint_error(": failed to establish interrupt on %s\n", 114 1.7 skrll intrstr); 115 1.1 skrll return; 116 1.1 skrll } 117 1.1 skrll 118 1.4 jmcneill macaddr = fdtbus_get_prop(phandle, "local-mac-address", &len); 119 1.4 jmcneill if (macaddr != NULL && len == ETHER_ADDR_LEN) { 120 1.4 jmcneill prop_dictionary_set_data(prop, "mac-address", macaddr, len); 121 1.4 jmcneill } 122 1.4 jmcneill 123 1.9 skrll sc->sc_dev = self; 124 1.9 skrll sc->sc_iot = faa->faa_bst; 125 1.9 skrll sc->sc_dmat = faa->faa_dmat; 126 1.10 lloyd sc->sc_phyno = cemac_get_phyid(phandle); 127 1.9 skrll sc->cemac_flags = CEMAC_FLAG_GEM; 128 1.9 skrll 129 1.9 skrll cemac_attach_common(sc); 130 1.3 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr); 131 1.1 skrll } 132 1.1 skrll 133 1.9 skrll 134 1.9 skrll CFATTACH_DECL_NEW(cemac, sizeof(struct cemac_softc), 135 1.9 skrll cemac_match, cemac_attach, NULL, NULL); 136