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