Home | History | Annotate | Line # | Download | only in sunxi
sunxi_gmac.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: sunxi_gmac.c,v 1.1 2017/10/07 13:28:59 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.1  jmcneill 
     31  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_gmac.c,v 1.1 2017/10/07 13:28:59 jmcneill Exp $");
     32  1.1  jmcneill 
     33  1.1  jmcneill #include <sys/param.h>
     34  1.1  jmcneill #include <sys/bus.h>
     35  1.1  jmcneill #include <sys/device.h>
     36  1.1  jmcneill #include <sys/intr.h>
     37  1.1  jmcneill #include <sys/systm.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <net/if.h>
     40  1.1  jmcneill #include <net/if_ether.h>
     41  1.1  jmcneill #include <net/if_media.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <dev/mii/miivar.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #include <dev/ic/dwc_gmac_var.h>
     46  1.1  jmcneill #include <dev/ic/dwc_gmac_reg.h>
     47  1.1  jmcneill 
     48  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     49  1.1  jmcneill 
     50  1.1  jmcneill #define	GMAC_TX_RATE_MII		25000000
     51  1.1  jmcneill #define	GMAC_TX_RATE_RGMII		125000000
     52  1.1  jmcneill 
     53  1.1  jmcneill static const char * compatible[] = {
     54  1.1  jmcneill 	"allwinner,sun7i-a20-gmac",
     55  1.1  jmcneill 	NULL
     56  1.1  jmcneill };
     57  1.1  jmcneill 
     58  1.1  jmcneill static int
     59  1.1  jmcneill sunxi_gmac_intr(void *arg)
     60  1.1  jmcneill {
     61  1.1  jmcneill 	return dwc_gmac_intr(arg);
     62  1.1  jmcneill }
     63  1.1  jmcneill 
     64  1.1  jmcneill static int
     65  1.1  jmcneill sunxi_gmac_match(device_t parent, cfdata_t cf, void *aux)
     66  1.1  jmcneill {
     67  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     68  1.1  jmcneill 
     69  1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
     70  1.1  jmcneill }
     71  1.1  jmcneill 
     72  1.1  jmcneill static void
     73  1.1  jmcneill sunxi_gmac_attach(device_t parent, device_t self, void *aux)
     74  1.1  jmcneill {
     75  1.1  jmcneill 	struct dwc_gmac_softc * const sc = device_private(self);
     76  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     77  1.1  jmcneill 	const int phandle = faa->faa_phandle;
     78  1.1  jmcneill 	struct clk *clk_gmac, *clk_gmac_tx;
     79  1.1  jmcneill 	const char *phy_mode;
     80  1.1  jmcneill 	char intrstr[128];
     81  1.1  jmcneill 	bus_addr_t addr;
     82  1.1  jmcneill 	bus_size_t size;
     83  1.1  jmcneill 
     84  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     85  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
     86  1.1  jmcneill 		return;
     87  1.1  jmcneill 	}
     88  1.1  jmcneill 
     89  1.1  jmcneill 	sc->sc_dev = self;
     90  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
     91  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
     92  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
     93  1.1  jmcneill 		return;
     94  1.1  jmcneill 	}
     95  1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
     96  1.1  jmcneill 
     97  1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
     98  1.1  jmcneill 		aprint_error(": failed to decode interrupt\n");
     99  1.1  jmcneill 		return;
    100  1.1  jmcneill 	}
    101  1.1  jmcneill 
    102  1.1  jmcneill 	clk_gmac = fdtbus_clock_get(phandle, "stmmaceth");
    103  1.1  jmcneill 	clk_gmac_tx = fdtbus_clock_get(phandle, "allwinner_gmac_tx");
    104  1.1  jmcneill 	if (clk_gmac == NULL || clk_gmac_tx == NULL) {
    105  1.1  jmcneill 		aprint_error(": couldn't get clocks\n");
    106  1.1  jmcneill 		return;
    107  1.1  jmcneill 	}
    108  1.1  jmcneill 
    109  1.1  jmcneill 	phy_mode = fdtbus_get_string(phandle, "phy-mode");
    110  1.1  jmcneill 	if (phy_mode == NULL) {
    111  1.1  jmcneill 		aprint_error(": missing 'phy-mode' property\n");
    112  1.1  jmcneill 		return;
    113  1.1  jmcneill 	}
    114  1.1  jmcneill 	if (strcmp(phy_mode, "mii") == 0) {
    115  1.1  jmcneill 		if (clk_set_rate(clk_gmac_tx, GMAC_TX_RATE_MII) != 0) {
    116  1.1  jmcneill 			aprint_error(": failed to set TX clock rate (MII)\n");
    117  1.1  jmcneill 			return;
    118  1.1  jmcneill 		}
    119  1.1  jmcneill 	} else if (strcmp(phy_mode, "rgmii") == 0) {
    120  1.1  jmcneill 		if (clk_set_rate(clk_gmac_tx, GMAC_TX_RATE_RGMII) != 0) {
    121  1.1  jmcneill 			aprint_error(": failed to set TX clock rate (RGMII)\n");
    122  1.1  jmcneill 			return;
    123  1.1  jmcneill 		}
    124  1.1  jmcneill 	} else {
    125  1.1  jmcneill 		aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
    126  1.1  jmcneill 		return;
    127  1.1  jmcneill 	}
    128  1.1  jmcneill 
    129  1.1  jmcneill 	if (clk_enable(clk_gmac_tx) != 0 || clk_enable(clk_gmac) != 0) {
    130  1.1  jmcneill 		aprint_error(": couldn't enable clocks\n");
    131  1.1  jmcneill 		return;
    132  1.1  jmcneill 	}
    133  1.1  jmcneill 
    134  1.1  jmcneill 	aprint_naive("\n");
    135  1.1  jmcneill 	aprint_normal(": GMAC\n");
    136  1.1  jmcneill 
    137  1.1  jmcneill 	if (fdtbus_intr_establish(phandle, 0, IPL_NET, 0, sunxi_gmac_intr, sc) == NULL) {
    138  1.1  jmcneill 		aprint_error_dev(self, "failed to establish interrupt on %s\n", intrstr);
    139  1.1  jmcneill 		return;
    140  1.1  jmcneill 	}
    141  1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    142  1.1  jmcneill 
    143  1.1  jmcneill 	dwc_gmac_attach(sc, GMAC_MII_CLK_150_250M_DIV102);
    144  1.1  jmcneill }
    145  1.1  jmcneill 
    146  1.1  jmcneill CFATTACH_DECL_NEW(sunxi_gmac, sizeof(struct dwc_gmac_softc),
    147  1.1  jmcneill 	sunxi_gmac_match, sunxi_gmac_attach, NULL, NULL);
    148