Home | History | Annotate | Line # | Download | only in amlogic
meson_dwmac.c revision 1.5.4.2
      1  1.5.4.2  christos /* $NetBSD: meson_dwmac.c,v 1.5.4.2 2019/06/10 22:05:51 christos Exp $ */
      2  1.5.4.2  christos 
      3  1.5.4.2  christos /*-
      4  1.5.4.2  christos  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.5.4.2  christos  * All rights reserved.
      6  1.5.4.2  christos  *
      7  1.5.4.2  christos  * Redistribution and use in source and binary forms, with or without
      8  1.5.4.2  christos  * modification, are permitted provided that the following conditions
      9  1.5.4.2  christos  * are met:
     10  1.5.4.2  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.5.4.2  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.5.4.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.5.4.2  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.5.4.2  christos  *    documentation and/or other materials provided with the distribution.
     15  1.5.4.2  christos  *
     16  1.5.4.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.5.4.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.5.4.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.5.4.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.5.4.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.5.4.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.5.4.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.5.4.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.5.4.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.5.4.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.5.4.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     27  1.5.4.2  christos  */
     28  1.5.4.2  christos 
     29  1.5.4.2  christos #include <sys/cdefs.h>
     30  1.5.4.2  christos 
     31  1.5.4.2  christos __KERNEL_RCSID(0, "$NetBSD: meson_dwmac.c,v 1.5.4.2 2019/06/10 22:05:51 christos Exp $");
     32  1.5.4.2  christos 
     33  1.5.4.2  christos #include <sys/param.h>
     34  1.5.4.2  christos #include <sys/bus.h>
     35  1.5.4.2  christos #include <sys/device.h>
     36  1.5.4.2  christos #include <sys/intr.h>
     37  1.5.4.2  christos #include <sys/systm.h>
     38  1.5.4.2  christos #include <sys/gpio.h>
     39  1.5.4.2  christos 
     40  1.5.4.2  christos #include <net/if.h>
     41  1.5.4.2  christos #include <net/if_ether.h>
     42  1.5.4.2  christos #include <net/if_media.h>
     43  1.5.4.2  christos 
     44  1.5.4.2  christos #include <dev/mii/miivar.h>
     45  1.5.4.2  christos 
     46  1.5.4.2  christos #include <dev/ic/dwc_gmac_var.h>
     47  1.5.4.2  christos #include <dev/ic/dwc_gmac_reg.h>
     48  1.5.4.2  christos 
     49  1.5.4.2  christos #include <dev/fdt/fdtvar.h>
     50  1.5.4.2  christos 
     51  1.5.4.2  christos #define	PRG_ETHERNET_ADDR0		0x00
     52  1.5.4.2  christos #define	 CLKGEN_ENABLE			__BIT(12)
     53  1.5.4.2  christos #define	 RMII_CLK_I_INVERTED		__BIT(11)
     54  1.5.4.2  christos #define	 PHY_CLK_ENABLE			__BIT(10)
     55  1.5.4.2  christos #define	 MP2_CLK_OUT_DIV		__BITS(9,7)
     56  1.5.4.2  christos #define	 TX_CLK_DELAY			__BITS(6,5)
     57  1.5.4.2  christos #define	 PHY_INTERFACE_SEL		__BIT(0)
     58  1.5.4.2  christos 
     59  1.5.4.2  christos static const char * compatible[] = {
     60  1.5.4.2  christos 	"amlogic,meson8b-dwmac",
     61  1.5.4.2  christos 	"amlogic,meson-gx-dwmac",
     62  1.5.4.2  christos 	NULL
     63  1.5.4.2  christos };
     64  1.5.4.2  christos 
     65  1.5.4.2  christos static int
     66  1.5.4.2  christos meson_dwmac_reset(const int phandle)
     67  1.5.4.2  christos {
     68  1.5.4.2  christos 	struct fdtbus_gpio_pin *pin_reset;
     69  1.5.4.2  christos 	const u_int *reset_delay_us;
     70  1.5.4.2  christos 	bool reset_active_low;
     71  1.5.4.2  christos 	int len, val;
     72  1.5.4.2  christos 
     73  1.5.4.2  christos 	pin_reset = fdtbus_gpio_acquire(phandle, "snps,reset-gpio", GPIO_PIN_OUTPUT);
     74  1.5.4.2  christos 	if (pin_reset == NULL)
     75  1.5.4.2  christos 		return 0;
     76  1.5.4.2  christos 
     77  1.5.4.2  christos 	reset_delay_us = fdtbus_get_prop(phandle, "snps,reset-delays-us", &len);
     78  1.5.4.2  christos 	if (reset_delay_us == NULL || len != 12)
     79  1.5.4.2  christos 		return ENXIO;
     80  1.5.4.2  christos 
     81  1.5.4.2  christos 	reset_active_low = of_hasprop(phandle, "snps,reset-active-low");
     82  1.5.4.2  christos 
     83  1.5.4.2  christos 	val = reset_active_low ? 1 : 0;
     84  1.5.4.2  christos 
     85  1.5.4.2  christos 	fdtbus_gpio_write(pin_reset, val);
     86  1.5.4.2  christos 	delay(be32toh(reset_delay_us[0]));
     87  1.5.4.2  christos 	fdtbus_gpio_write(pin_reset, !val);
     88  1.5.4.2  christos 	delay(be32toh(reset_delay_us[1]));
     89  1.5.4.2  christos 	fdtbus_gpio_write(pin_reset, val);
     90  1.5.4.2  christos 	delay(be32toh(reset_delay_us[2]));
     91  1.5.4.2  christos 
     92  1.5.4.2  christos 	return 0;
     93  1.5.4.2  christos }
     94  1.5.4.2  christos 
     95  1.5.4.2  christos static void
     96  1.5.4.2  christos meson_dwmac_set_mode_rgmii(int phandle, bus_space_tag_t bst,
     97  1.5.4.2  christos     bus_space_handle_t bsh, struct clk *clkin)
     98  1.5.4.2  christos {
     99  1.5.4.2  christos 	u_int tx_delay;
    100  1.5.4.2  christos 	uint32_t val;
    101  1.5.4.2  christos 
    102  1.5.4.2  christos 	const u_int div = clk_get_rate(clkin) / 250000000;
    103  1.5.4.2  christos 
    104  1.5.4.2  christos 	if (of_getprop_uint32(phandle, "amlogic,tx-delay-ns", &tx_delay) != 0)
    105  1.5.4.2  christos 		tx_delay = 2;
    106  1.5.4.2  christos 
    107  1.5.4.2  christos 	val = bus_space_read_4(bst, bsh, PRG_ETHERNET_ADDR0);
    108  1.5.4.2  christos 	val |= PHY_INTERFACE_SEL;
    109  1.5.4.2  christos 	val &= ~TX_CLK_DELAY;
    110  1.5.4.2  christos 	val |= __SHIFTIN((tx_delay >> 1), TX_CLK_DELAY);
    111  1.5.4.2  christos 	val &= ~MP2_CLK_OUT_DIV;
    112  1.5.4.2  christos 	val |= __SHIFTIN(div, MP2_CLK_OUT_DIV);
    113  1.5.4.2  christos 	val |= PHY_CLK_ENABLE;
    114  1.5.4.2  christos 	val |= CLKGEN_ENABLE;
    115  1.5.4.2  christos 	bus_space_write_4(bst, bsh, PRG_ETHERNET_ADDR0, val);
    116  1.5.4.2  christos }
    117  1.5.4.2  christos 
    118  1.5.4.2  christos static void
    119  1.5.4.2  christos meson_dwmac_set_mode_rmii(int phandle, bus_space_tag_t bst,
    120  1.5.4.2  christos     bus_space_handle_t bsh)
    121  1.5.4.2  christos {
    122  1.5.4.2  christos 	uint32_t val;
    123  1.5.4.2  christos 
    124  1.5.4.2  christos 	val = bus_space_read_4(bst, bsh, PRG_ETHERNET_ADDR0);
    125  1.5.4.2  christos 	val &= ~PHY_INTERFACE_SEL;
    126  1.5.4.2  christos 	val |= RMII_CLK_I_INVERTED;
    127  1.5.4.2  christos 	val &= ~TX_CLK_DELAY;
    128  1.5.4.2  christos 	val |= CLKGEN_ENABLE;
    129  1.5.4.2  christos 	bus_space_write_4(bst, bsh, PRG_ETHERNET_ADDR0, val);
    130  1.5.4.2  christos }
    131  1.5.4.2  christos 
    132  1.5.4.2  christos static int
    133  1.5.4.2  christos meson_dwmac_intr(void *arg)
    134  1.5.4.2  christos {
    135  1.5.4.2  christos 	struct dwc_gmac_softc * const sc = arg;
    136  1.5.4.2  christos 
    137  1.5.4.2  christos 	return dwc_gmac_intr(sc);
    138  1.5.4.2  christos }
    139  1.5.4.2  christos 
    140  1.5.4.2  christos static int
    141  1.5.4.2  christos meson_dwmac_match(device_t parent, cfdata_t cf, void *aux)
    142  1.5.4.2  christos {
    143  1.5.4.2  christos 	struct fdt_attach_args * const faa = aux;
    144  1.5.4.2  christos 
    145  1.5.4.2  christos 	return of_match_compatible(faa->faa_phandle, compatible);
    146  1.5.4.2  christos }
    147  1.5.4.2  christos 
    148  1.5.4.2  christos static void
    149  1.5.4.2  christos meson_dwmac_attach(device_t parent, device_t self, void *aux)
    150  1.5.4.2  christos {
    151  1.5.4.2  christos 	struct dwc_gmac_softc * const sc = device_private(self);
    152  1.5.4.2  christos 	struct fdt_attach_args * const faa = aux;
    153  1.5.4.2  christos 	const int phandle = faa->faa_phandle;
    154  1.5.4.2  christos 	bus_space_handle_t prgeth_bsh;
    155  1.5.4.2  christos 	struct fdtbus_reset *rst_gmac;
    156  1.5.4.2  christos 	struct clk *clk_gmac, *clk_in[2];
    157  1.5.4.2  christos 	const char *phy_mode;
    158  1.5.4.2  christos 	char intrstr[128];
    159  1.5.4.2  christos 	bus_addr_t addr[2];
    160  1.5.4.2  christos 	bus_size_t size[2];
    161  1.5.4.2  christos 
    162  1.5.4.2  christos 	if (fdtbus_get_reg(phandle, 0, &addr[0], &size[0]) != 0 ||
    163  1.5.4.2  christos 	    fdtbus_get_reg(phandle, 1, &addr[1], &size[1]) != 0) {
    164  1.5.4.2  christos 		aprint_error(": couldn't get registers\n");
    165  1.5.4.2  christos 		return;
    166  1.5.4.2  christos 	}
    167  1.5.4.2  christos 
    168  1.5.4.2  christos 	sc->sc_dev = self;
    169  1.5.4.2  christos 	sc->sc_bst = faa->faa_bst;
    170  1.5.4.2  christos 	if (bus_space_map(sc->sc_bst, addr[0], size[0], 0, &sc->sc_bsh) != 0 ||
    171  1.5.4.2  christos 	    bus_space_map(sc->sc_bst, addr[1], size[1], 0, &prgeth_bsh) != 0) {
    172  1.5.4.2  christos 		aprint_error(": couldn't map registers\n");
    173  1.5.4.2  christos 		return;
    174  1.5.4.2  christos 	}
    175  1.5.4.2  christos 	sc->sc_dmat = faa->faa_dmat;
    176  1.5.4.2  christos 
    177  1.5.4.2  christos 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    178  1.5.4.2  christos 		aprint_error(": failed to decode interrupt\n");
    179  1.5.4.2  christos 		return;
    180  1.5.4.2  christos 	}
    181  1.5.4.2  christos 
    182  1.5.4.2  christos 	clk_gmac = fdtbus_clock_get(phandle, "stmmaceth");
    183  1.5.4.2  christos 	clk_in[0] = fdtbus_clock_get(phandle, "clkin0");
    184  1.5.4.2  christos 	clk_in[1] = fdtbus_clock_get(phandle, "clkin1");
    185  1.5.4.2  christos 	if (clk_gmac == NULL || clk_in[0] == NULL || clk_in[1] == NULL) {
    186  1.5.4.2  christos 		aprint_error(": couldn't get clocks\n");
    187  1.5.4.2  christos 		return;
    188  1.5.4.2  christos 	}
    189  1.5.4.2  christos 
    190  1.5.4.2  christos 	rst_gmac = fdtbus_reset_get(phandle, "stmmaceth");
    191  1.5.4.2  christos 
    192  1.5.4.2  christos 	phy_mode = fdtbus_get_string(phandle, "phy-mode");
    193  1.5.4.2  christos 	if (phy_mode == NULL) {
    194  1.5.4.2  christos 		aprint_error(": missing 'phy-mode' property\n");
    195  1.5.4.2  christos 		return;
    196  1.5.4.2  christos 	}
    197  1.5.4.2  christos 
    198  1.5.4.2  christos 	if (strcmp(phy_mode, "rgmii") == 0) {
    199  1.5.4.2  christos 		meson_dwmac_set_mode_rgmii(phandle, sc->sc_bst, prgeth_bsh, clk_in[0]);
    200  1.5.4.2  christos 	} else if (strcmp(phy_mode, "rmii") == 0) {
    201  1.5.4.2  christos 		meson_dwmac_set_mode_rmii(phandle, sc->sc_bst, prgeth_bsh);
    202  1.5.4.2  christos 	} else {
    203  1.5.4.2  christos 		aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
    204  1.5.4.2  christos 		return;
    205  1.5.4.2  christos 	}
    206  1.5.4.2  christos 
    207  1.5.4.2  christos 	if (clk_enable(clk_gmac) != 0) {
    208  1.5.4.2  christos 		aprint_error(": couldn't enable clock\n");
    209  1.5.4.2  christos 		return;
    210  1.5.4.2  christos 	}
    211  1.5.4.2  christos 
    212  1.5.4.2  christos 	if (rst_gmac != NULL && fdtbus_reset_deassert(rst_gmac) != 0) {
    213  1.5.4.2  christos 		aprint_error(": couldn't de-assert reset\n");
    214  1.5.4.2  christos 		return;
    215  1.5.4.2  christos 	}
    216  1.5.4.2  christos 
    217  1.5.4.2  christos 	aprint_naive("\n");
    218  1.5.4.2  christos 	aprint_normal(": Gigabit Ethernet Controller\n");
    219  1.5.4.2  christos 
    220  1.5.4.2  christos 	if (fdtbus_intr_establish(phandle, 0, IPL_NET, 0, meson_dwmac_intr, sc) == NULL) {
    221  1.5.4.2  christos 		aprint_error_dev(self, "failed to establish interrupt on %s\n", intrstr);
    222  1.5.4.2  christos 		return;
    223  1.5.4.2  christos 	}
    224  1.5.4.2  christos 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    225  1.5.4.2  christos 
    226  1.5.4.2  christos 	if (meson_dwmac_reset(phandle) != 0)
    227  1.5.4.2  christos 		aprint_error_dev(self, "PHY reset failed\n");
    228  1.5.4.2  christos 
    229  1.5.4.2  christos 	dwc_gmac_attach(sc, MII_PHY_ANY, GMAC_MII_CLK_100_150M_DIV62);
    230  1.5.4.2  christos }
    231  1.5.4.2  christos 
    232  1.5.4.2  christos CFATTACH_DECL_NEW(meson_dwmac, sizeof(struct dwc_gmac_softc),
    233  1.5.4.2  christos 	meson_dwmac_match, meson_dwmac_attach, NULL, NULL);
    234