Home | History | Annotate | Line # | Download | only in altera
cycv_gmac.c revision 1.2
      1 /* $NetBSD: cycv_gmac.c,v 1.2 2019/02/23 17:18:38 martin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared McNeill <jmcneill (at) 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /* Taken from sunxi_gmac.c */
     30 
     31 #include <sys/cdefs.h>
     32 
     33 __KERNEL_RCSID(0, "$NetBSD: cycv_gmac.c,v 1.2 2019/02/23 17:18:38 martin Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/device.h>
     38 #include <sys/intr.h>
     39 #include <sys/systm.h>
     40 #include <sys/gpio.h>
     41 
     42 #include <net/if.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/dwc_gmac_var.h>
     49 #include <dev/ic/dwc_gmac_reg.h>
     50 
     51 #include <dev/fdt/fdtvar.h>
     52 
     53 static const char * compatible[] = { "altr,socfpga-stmmac", NULL };
     54 
     55 static int
     56 cycv_gmac_intr(void *arg)
     57 {
     58 	return dwc_gmac_intr(arg);
     59 }
     60 
     61 static int
     62 cycv_gmac_match(device_t parent, cfdata_t cf, void *aux)
     63 {
     64 	struct fdt_attach_args * const faa = aux;
     65 
     66 	return of_match_compatible(faa->faa_phandle, compatible);
     67 }
     68 
     69 static void
     70 cycv_gmac_attach(device_t parent, device_t self, void *aux)
     71 {
     72 	struct dwc_gmac_softc * const sc = device_private(self);
     73 	struct fdt_attach_args * const faa = aux;
     74 	const int phandle = faa->faa_phandle;
     75 	struct clk *clk_gmac;
     76 	struct fdtbus_reset *rst_gmac;
     77 	const char *phy_mode;
     78 	char intrstr[128];
     79 	bus_addr_t addr;
     80 	bus_size_t size;
     81 
     82 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     83 		aprint_error(": couldn't get registers\n");
     84 		return;
     85 	}
     86 
     87 	sc->sc_dev = self;
     88 	sc->sc_bst = faa->faa_bst;
     89 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
     90 		aprint_error(": couldn't map registers\n");
     91 		return;
     92 	}
     93 	sc->sc_dmat = faa->faa_dmat;
     94 
     95 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof intrstr)) {
     96 		aprint_error(": failed to decode interrupt\n");
     97 		return;
     98 	}
     99 
    100 	clk_gmac = fdtbus_clock_get(phandle, "stmmaceth");
    101 	if (clk_gmac == NULL) {
    102 		aprint_error(": couldn't get clock\n");
    103 		return;
    104 	}
    105 
    106 	rst_gmac = fdtbus_reset_get(phandle, "stmmaceth");
    107 	if (rst_gmac == NULL) {
    108 		aprint_error(": couldn't get reset\n");
    109 		return;
    110 	}
    111 
    112 	phy_mode = fdtbus_get_string(phandle, "phy-mode");
    113 	if (phy_mode == NULL) {
    114 		aprint_error(": missing 'phy-mode' property\n");
    115 		return;
    116 	}
    117 
    118 	if (clk_enable(clk_gmac) != 0) {
    119 		aprint_error(": couldn't enable clocks\n");
    120 		return;
    121 	}
    122 
    123 	if (rst_gmac != NULL && fdtbus_reset_deassert(rst_gmac) != 0) {
    124 		aprint_error(": couldn't de-assert reset\n");
    125 		return;
    126 	}
    127 
    128 	aprint_naive("\n");
    129 	aprint_normal(": GMAC\n");
    130 
    131 	if (fdtbus_intr_establish(phandle, 0, IPL_NET, 0,
    132 				  cycv_gmac_intr, sc) == NULL) {
    133 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    134 				 intrstr);
    135 		return;
    136 	}
    137 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    138 
    139 	dwc_gmac_attach(sc, MII_PHY_ANY, GMAC_MII_CLK_150_250M_DIV102);
    140 }
    141 
    142 CFATTACH_DECL_NEW(cycv_gmac, sizeof(struct dwc_gmac_softc),
    143 	cycv_gmac_match, cycv_gmac_attach, NULL, NULL);
    144