Home | History | Annotate | Line # | Download | only in amlogic
meson_dwmac.c revision 1.8.6.2
      1  1.8.6.2   thorpej /* $NetBSD: meson_dwmac.c,v 1.8.6.2 2021/04/03 22:28:15 thorpej 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.8.6.2   thorpej __KERNEL_RCSID(0, "$NetBSD: meson_dwmac.c,v 1.8.6.2 2021/04/03 22:28:15 thorpej 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 #include <sys/gpio.h>
     39      1.6   msaitoh #include <sys/rndsource.h>
     40      1.1  jmcneill 
     41      1.1  jmcneill #include <net/if.h>
     42      1.1  jmcneill #include <net/if_ether.h>
     43      1.1  jmcneill #include <net/if_media.h>
     44      1.1  jmcneill 
     45      1.1  jmcneill #include <dev/mii/miivar.h>
     46      1.1  jmcneill 
     47      1.1  jmcneill #include <dev/ic/dwc_gmac_var.h>
     48      1.1  jmcneill #include <dev/ic/dwc_gmac_reg.h>
     49      1.1  jmcneill 
     50      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     51      1.1  jmcneill 
     52      1.4  jmcneill #define	PRG_ETHERNET_ADDR0		0x00
     53      1.4  jmcneill #define	 CLKGEN_ENABLE			__BIT(12)
     54      1.5  jmcneill #define	 RMII_CLK_I_INVERTED		__BIT(11)
     55      1.4  jmcneill #define	 PHY_CLK_ENABLE			__BIT(10)
     56      1.4  jmcneill #define	 MP2_CLK_OUT_DIV		__BITS(9,7)
     57      1.4  jmcneill #define	 TX_CLK_DELAY			__BITS(6,5)
     58      1.4  jmcneill #define	 PHY_INTERFACE_SEL		__BIT(0)
     59      1.1  jmcneill 
     60  1.8.6.2   thorpej static const struct device_compatible_entry compat_data[] = {
     61  1.8.6.2   thorpej 	{ .compat = "amlogic,meson8b-dwmac" },
     62  1.8.6.2   thorpej 	{ .compat = "amlogic,meson-gx-dwmac" },
     63  1.8.6.2   thorpej 	{ .compat = "amlogic,meson-gxbb-dwmac" },
     64  1.8.6.2   thorpej 	{ .compat = "amlogic,meson-axg-dwmac" },
     65  1.8.6.2   thorpej 	DEVICE_COMPAT_EOL
     66      1.1  jmcneill };
     67      1.1  jmcneill 
     68      1.1  jmcneill static int
     69      1.1  jmcneill meson_dwmac_reset(const int phandle)
     70      1.1  jmcneill {
     71      1.1  jmcneill 	struct fdtbus_gpio_pin *pin_reset;
     72      1.1  jmcneill 	const u_int *reset_delay_us;
     73      1.1  jmcneill 	bool reset_active_low;
     74      1.1  jmcneill 	int len, val;
     75      1.1  jmcneill 
     76      1.1  jmcneill 	pin_reset = fdtbus_gpio_acquire(phandle, "snps,reset-gpio", GPIO_PIN_OUTPUT);
     77      1.1  jmcneill 	if (pin_reset == NULL)
     78      1.1  jmcneill 		return 0;
     79      1.1  jmcneill 
     80      1.1  jmcneill 	reset_delay_us = fdtbus_get_prop(phandle, "snps,reset-delays-us", &len);
     81      1.1  jmcneill 	if (reset_delay_us == NULL || len != 12)
     82      1.1  jmcneill 		return ENXIO;
     83      1.1  jmcneill 
     84      1.1  jmcneill 	reset_active_low = of_hasprop(phandle, "snps,reset-active-low");
     85      1.1  jmcneill 
     86      1.1  jmcneill 	val = reset_active_low ? 1 : 0;
     87      1.1  jmcneill 
     88      1.1  jmcneill 	fdtbus_gpio_write(pin_reset, val);
     89      1.1  jmcneill 	delay(be32toh(reset_delay_us[0]));
     90      1.1  jmcneill 	fdtbus_gpio_write(pin_reset, !val);
     91      1.1  jmcneill 	delay(be32toh(reset_delay_us[1]));
     92      1.1  jmcneill 	fdtbus_gpio_write(pin_reset, val);
     93      1.1  jmcneill 	delay(be32toh(reset_delay_us[2]));
     94      1.1  jmcneill 
     95      1.1  jmcneill 	return 0;
     96      1.1  jmcneill }
     97      1.1  jmcneill 
     98      1.4  jmcneill static void
     99      1.4  jmcneill meson_dwmac_set_mode_rgmii(int phandle, bus_space_tag_t bst,
    100      1.4  jmcneill     bus_space_handle_t bsh, struct clk *clkin)
    101      1.4  jmcneill {
    102      1.4  jmcneill 	u_int tx_delay;
    103      1.4  jmcneill 	uint32_t val;
    104      1.4  jmcneill 
    105  1.8.6.1   thorpej #define DIV_ROUND_OFF(x, y)	(((x) + (y) / 2) / (y))
    106  1.8.6.1   thorpej 	const u_int div = DIV_ROUND_OFF(clk_get_rate(clkin), 250000000);
    107      1.4  jmcneill 
    108      1.4  jmcneill 	if (of_getprop_uint32(phandle, "amlogic,tx-delay-ns", &tx_delay) != 0)
    109      1.4  jmcneill 		tx_delay = 2;
    110      1.4  jmcneill 
    111      1.4  jmcneill 	val = bus_space_read_4(bst, bsh, PRG_ETHERNET_ADDR0);
    112      1.4  jmcneill 	val |= PHY_INTERFACE_SEL;
    113      1.4  jmcneill 	val &= ~TX_CLK_DELAY;
    114      1.4  jmcneill 	val |= __SHIFTIN((tx_delay >> 1), TX_CLK_DELAY);
    115      1.4  jmcneill 	val &= ~MP2_CLK_OUT_DIV;
    116      1.4  jmcneill 	val |= __SHIFTIN(div, MP2_CLK_OUT_DIV);
    117      1.4  jmcneill 	val |= PHY_CLK_ENABLE;
    118      1.4  jmcneill 	val |= CLKGEN_ENABLE;
    119      1.4  jmcneill 	bus_space_write_4(bst, bsh, PRG_ETHERNET_ADDR0, val);
    120      1.4  jmcneill }
    121      1.4  jmcneill 
    122      1.5  jmcneill static void
    123      1.5  jmcneill meson_dwmac_set_mode_rmii(int phandle, bus_space_tag_t bst,
    124      1.5  jmcneill     bus_space_handle_t bsh)
    125      1.5  jmcneill {
    126      1.5  jmcneill 	uint32_t val;
    127      1.5  jmcneill 
    128      1.5  jmcneill 	val = bus_space_read_4(bst, bsh, PRG_ETHERNET_ADDR0);
    129      1.5  jmcneill 	val &= ~PHY_INTERFACE_SEL;
    130      1.5  jmcneill 	val |= RMII_CLK_I_INVERTED;
    131      1.5  jmcneill 	val &= ~TX_CLK_DELAY;
    132      1.5  jmcneill 	val |= CLKGEN_ENABLE;
    133      1.5  jmcneill 	bus_space_write_4(bst, bsh, PRG_ETHERNET_ADDR0, val);
    134      1.5  jmcneill }
    135      1.5  jmcneill 
    136      1.1  jmcneill static int
    137      1.1  jmcneill meson_dwmac_intr(void *arg)
    138      1.1  jmcneill {
    139      1.1  jmcneill 	struct dwc_gmac_softc * const sc = arg;
    140      1.1  jmcneill 
    141      1.1  jmcneill 	return dwc_gmac_intr(sc);
    142      1.1  jmcneill }
    143      1.1  jmcneill 
    144      1.1  jmcneill static int
    145      1.1  jmcneill meson_dwmac_match(device_t parent, cfdata_t cf, void *aux)
    146      1.1  jmcneill {
    147      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    148      1.1  jmcneill 
    149  1.8.6.2   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    150      1.1  jmcneill }
    151      1.1  jmcneill 
    152      1.1  jmcneill static void
    153      1.1  jmcneill meson_dwmac_attach(device_t parent, device_t self, void *aux)
    154      1.1  jmcneill {
    155      1.1  jmcneill 	struct dwc_gmac_softc * const sc = device_private(self);
    156      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    157      1.1  jmcneill 	const int phandle = faa->faa_phandle;
    158  1.8.6.1   thorpej 	int miiclk, phandle_phy, phy = MII_PHY_ANY;
    159  1.8.6.1   thorpej 	u_int miiclk_rate;
    160      1.4  jmcneill 	bus_space_handle_t prgeth_bsh;
    161      1.1  jmcneill 	struct fdtbus_reset *rst_gmac;
    162      1.4  jmcneill 	struct clk *clk_gmac, *clk_in[2];
    163      1.1  jmcneill 	const char *phy_mode;
    164      1.1  jmcneill 	char intrstr[128];
    165      1.4  jmcneill 	bus_addr_t addr[2];
    166      1.4  jmcneill 	bus_size_t size[2];
    167      1.1  jmcneill 
    168      1.4  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr[0], &size[0]) != 0 ||
    169      1.4  jmcneill 	    fdtbus_get_reg(phandle, 1, &addr[1], &size[1]) != 0) {
    170      1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    171      1.1  jmcneill 		return;
    172      1.1  jmcneill 	}
    173      1.1  jmcneill 
    174      1.1  jmcneill 	sc->sc_dev = self;
    175      1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    176      1.4  jmcneill 	if (bus_space_map(sc->sc_bst, addr[0], size[0], 0, &sc->sc_bsh) != 0 ||
    177      1.4  jmcneill 	    bus_space_map(sc->sc_bst, addr[1], size[1], 0, &prgeth_bsh) != 0) {
    178      1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    179      1.1  jmcneill 		return;
    180      1.1  jmcneill 	}
    181      1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    182      1.1  jmcneill 
    183      1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    184      1.1  jmcneill 		aprint_error(": failed to decode interrupt\n");
    185      1.1  jmcneill 		return;
    186      1.1  jmcneill 	}
    187      1.1  jmcneill 
    188      1.1  jmcneill 	clk_gmac = fdtbus_clock_get(phandle, "stmmaceth");
    189      1.4  jmcneill 	clk_in[0] = fdtbus_clock_get(phandle, "clkin0");
    190      1.4  jmcneill 	clk_in[1] = fdtbus_clock_get(phandle, "clkin1");
    191      1.4  jmcneill 	if (clk_gmac == NULL || clk_in[0] == NULL || clk_in[1] == NULL) {
    192      1.1  jmcneill 		aprint_error(": couldn't get clocks\n");
    193      1.1  jmcneill 		return;
    194      1.1  jmcneill 	}
    195      1.1  jmcneill 
    196      1.1  jmcneill 	rst_gmac = fdtbus_reset_get(phandle, "stmmaceth");
    197      1.1  jmcneill 
    198      1.1  jmcneill 	phy_mode = fdtbus_get_string(phandle, "phy-mode");
    199      1.1  jmcneill 	if (phy_mode == NULL) {
    200      1.1  jmcneill 		aprint_error(": missing 'phy-mode' property\n");
    201      1.1  jmcneill 		return;
    202      1.1  jmcneill 	}
    203  1.8.6.1   thorpej 	phandle_phy = fdtbus_get_phandle(phandle, "phy-handle");
    204  1.8.6.1   thorpej 	if (phandle_phy > 0) {
    205  1.8.6.1   thorpej 		of_getprop_uint32(phandle_phy, "reg", &phy);
    206  1.8.6.1   thorpej 	} else {
    207  1.8.6.1   thorpej 		phandle_phy = phandle;
    208  1.8.6.1   thorpej 	}
    209      1.4  jmcneill 
    210      1.4  jmcneill 	if (strcmp(phy_mode, "rgmii") == 0) {
    211      1.4  jmcneill 		meson_dwmac_set_mode_rgmii(phandle, sc->sc_bst, prgeth_bsh, clk_in[0]);
    212      1.5  jmcneill 	} else if (strcmp(phy_mode, "rmii") == 0) {
    213      1.5  jmcneill 		meson_dwmac_set_mode_rmii(phandle, sc->sc_bst, prgeth_bsh);
    214      1.1  jmcneill 	} else {
    215      1.1  jmcneill 		aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
    216      1.1  jmcneill 		return;
    217      1.1  jmcneill 	}
    218      1.1  jmcneill 
    219      1.1  jmcneill 	if (clk_enable(clk_gmac) != 0) {
    220      1.1  jmcneill 		aprint_error(": couldn't enable clock\n");
    221      1.1  jmcneill 		return;
    222      1.1  jmcneill 	}
    223      1.1  jmcneill 
    224      1.1  jmcneill 	if (rst_gmac != NULL && fdtbus_reset_deassert(rst_gmac) != 0) {
    225      1.1  jmcneill 		aprint_error(": couldn't de-assert reset\n");
    226      1.1  jmcneill 		return;
    227      1.1  jmcneill 	}
    228      1.1  jmcneill 
    229      1.1  jmcneill 	aprint_naive("\n");
    230      1.1  jmcneill 	aprint_normal(": Gigabit Ethernet Controller\n");
    231      1.1  jmcneill 
    232  1.8.6.2   thorpej 	if (fdtbus_intr_establish_xname(phandle, 0, IPL_NET,
    233  1.8.6.2   thorpej 	    DWCGMAC_FDT_INTR_MPSAFE, meson_dwmac_intr, sc,
    234  1.8.6.2   thorpej 	    device_xname(sc->sc_dev)) == NULL) {
    235      1.1  jmcneill 		aprint_error_dev(self, "failed to establish interrupt on %s\n", intrstr);
    236      1.1  jmcneill 		return;
    237      1.1  jmcneill 	}
    238      1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    239      1.1  jmcneill 
    240  1.8.6.1   thorpej 	if (meson_dwmac_reset(phandle_phy) != 0)
    241      1.1  jmcneill 		aprint_error_dev(self, "PHY reset failed\n");
    242      1.1  jmcneill 
    243  1.8.6.1   thorpej 	miiclk_rate = clk_get_rate(clk_gmac);
    244  1.8.6.1   thorpej 	if (miiclk_rate > 250 * 1000 * 1000)
    245  1.8.6.1   thorpej 		miiclk = GMAC_MII_CLK_250_300M_DIV124;
    246  1.8.6.1   thorpej 	else if (miiclk_rate > 150 * 1000 * 1000)
    247  1.8.6.1   thorpej 		miiclk = GMAC_MII_CLK_150_250M_DIV102;
    248  1.8.6.1   thorpej 	else if (miiclk_rate > 100 * 1000 * 1000)
    249  1.8.6.1   thorpej 		miiclk = GMAC_MII_CLK_100_150M_DIV62;
    250  1.8.6.1   thorpej 	else if (miiclk_rate > 60 * 1000 * 1000)
    251  1.8.6.1   thorpej 		miiclk = GMAC_MII_CLK_60_100M_DIV42;
    252  1.8.6.1   thorpej 	else if (miiclk_rate > 35 * 1000 * 1000)
    253  1.8.6.1   thorpej 		miiclk = GMAC_MII_CLK_35_60M_DIV26;
    254  1.8.6.1   thorpej 	else
    255  1.8.6.1   thorpej 		miiclk = GMAC_MII_CLK_25_35M_DIV16;
    256  1.8.6.1   thorpej 
    257  1.8.6.1   thorpej 	dwc_gmac_attach(sc, phy, miiclk);
    258      1.1  jmcneill }
    259      1.1  jmcneill 
    260      1.1  jmcneill CFATTACH_DECL_NEW(meson_dwmac, sizeof(struct dwc_gmac_softc),
    261      1.1  jmcneill 	meson_dwmac_match, meson_dwmac_attach, NULL, NULL);
    262