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