Home | History | Annotate | Line # | Download | only in nxp
if_enet_imx.c revision 1.3
      1 /*	$NetBSD: if_enet_imx.c,v 1.3 2021/01/18 02:35:48 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2019 Genetec Corporation.  All rights reserved.
      5  * Written by Hashimoto Kenichi for Genetec Corporation.
      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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: if_enet_imx.c,v 1.3 2021/01/18 02:35:48 thorpej Exp $");
     31 
     32 #include "opt_fdt.h"
     33 
     34 #include <sys/param.h>
     35 #include <sys/bus.h>
     36 #include <sys/device.h>
     37 
     38 #include <arm/imx/if_enetreg.h>
     39 #include <arm/imx/if_enetvar.h>
     40 
     41 #include <dev/fdt/fdtvar.h>
     42 
     43 struct enet_fdt_softc {
     44 	struct enet_softc sc_enet;
     45 
     46 	struct fdtbus_gpio_pin *sc_pin_reset;
     47 };
     48 
     49 CFATTACH_DECL_NEW(enet_fdt, sizeof(struct enet_fdt_softc),
     50     enet_match, enet_attach, NULL, NULL);
     51 
     52 static const struct device_compatible_entry compat_data[] = {
     53 	/* compatible			imxtype */
     54 	{ .compat = "fsl,imx6q-fec",	.value = 6 },
     55 	{ .compat = "fsl,imx6sx-fec",	.value = 7 },
     56 
     57 	{ 0 }
     58 };
     59 
     60 static int enet_init_clocks(struct enet_softc *);
     61 static void enet_phy_reset(struct enet_fdt_softc *, const int);
     62 static int enet_phy_id(struct enet_softc *, const int);
     63 static void *enet_intr_establish(struct enet_softc *, int, u_int);
     64 
     65 int
     66 enet_match(device_t parent, cfdata_t cf, void *aux)
     67 {
     68 	struct fdt_attach_args * const faa = aux;
     69 
     70 	return of_match_compat_data(faa->faa_phandle, compat_data);
     71 }
     72 
     73 void
     74 enet_attach(device_t parent, device_t self, void *aux)
     75 {
     76 	struct enet_fdt_softc * const efsc = device_private(self);
     77 	struct enet_softc *sc = &efsc->sc_enet;
     78 	struct fdt_attach_args * const faa = aux;
     79 	prop_dictionary_t prop = device_properties(self);
     80 	const int phandle = faa->faa_phandle;
     81 	bus_space_tag_t bst = faa->faa_bst;
     82 	bus_space_handle_t bsh;
     83 	bus_addr_t addr;
     84 	bus_size_t size;
     85 	int error;
     86 
     87 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     88 		aprint_error(": couldn't get enet registers\n");
     89 		return;
     90 	}
     91 
     92 	error = bus_space_map(bst, addr, size, 0, &bsh);
     93 	if (error) {
     94 		aprint_error(": couldn't map enet registers: %d\n", error);
     95 		return;
     96 	}
     97 
     98 	sc->sc_clk_ipg = fdtbus_clock_get(phandle, "ipg");
     99 	if (sc->sc_clk_ipg == NULL) {
    100 		aprint_error(": couldn't get clock ipg\n");
    101 		goto failure;
    102 	}
    103 	sc->sc_clk_enet = fdtbus_clock_get(phandle, "ahb");
    104 	if (sc->sc_clk_enet == NULL) {
    105 		aprint_error(": couldn't get clock ahb\n");
    106 		goto failure;
    107 	}
    108 	sc->sc_clk_enet_ref = fdtbus_clock_get(phandle, "ptp");
    109 	if (sc->sc_clk_enet_ref == NULL) {
    110 		aprint_error(": couldn't get clock ptp\n");
    111 		goto failure;
    112 	}
    113 
    114 	if (fdtbus_clock_enable(phandle, "enet_clk_ref", false) != 0) {
    115 		aprint_error(": couldn't enable clock enet_clk_ref\n");
    116 		goto failure;
    117 	}
    118 	if (fdtbus_clock_enable(phandle, "enet_out", false) != 0) {
    119 		aprint_error(": couldn't enable clock enet_out\n");
    120 		goto failure;
    121 	}
    122 
    123 	aprint_naive("\n");
    124 	aprint_normal(": Gigabit Ethernet Controller\n");
    125 
    126 	sc->sc_dev = self;
    127 	sc->sc_iot = bst;
    128 	sc->sc_ioh = bsh;
    129 	sc->sc_dmat = faa->faa_dmat;
    130 
    131 	sc->sc_imxtype = of_search_compatible(phandle, compat_data)->value;
    132 	sc->sc_unit = 0;
    133 	sc->sc_phyid = enet_phy_id(sc, phandle);
    134 
    135 	const char *phy_mode = fdtbus_get_string(phandle, "phy-mode");
    136 	if (phy_mode == NULL) {
    137 		aprint_error(": missing 'phy-mode' property\n");
    138 		goto failure;
    139 	}
    140 
    141 	if (strcmp(phy_mode, "rgmii-txid") == 0) {
    142 		prop_dictionary_set_bool(prop, "tx_internal_delay", true);
    143 		sc->sc_rgmii = 1;
    144 	} else if (strcmp(phy_mode, "rgmii-rxid") == 0) {
    145 		prop_dictionary_set_bool(prop, "rx_internal_delay", true);
    146 		sc->sc_rgmii = 1;
    147 	} else if (strcmp(phy_mode, "rgmii-id") == 0) {
    148 		prop_dictionary_set_bool(prop, "tx_internal_delay", true);
    149 		prop_dictionary_set_bool(prop, "rx_internal_delay", true);
    150 		sc->sc_rgmii = 1;
    151 	} else if (strcmp(phy_mode, "rgmii") == 0) {
    152 		sc->sc_rgmii = 1;
    153 	} else {
    154 		sc->sc_rgmii = 0;
    155 	}
    156 
    157 	sc->sc_ih = enet_intr_establish(sc, phandle, 0);
    158 	if (sc->sc_ih == NULL)
    159 		goto failure;
    160 
    161 	if (sc->sc_imxtype == 7) {
    162 		sc->sc_ih2 = enet_intr_establish(sc, phandle, 1);
    163 		sc->sc_ih3 = enet_intr_establish(sc, phandle, 2);
    164 		if (sc->sc_ih2 == NULL || sc->sc_ih3 == NULL)
    165 			goto failure;
    166 	}
    167 
    168 	enet_init_clocks(sc);
    169 	sc->sc_clock = clk_get_rate(sc->sc_clk_ipg);
    170 
    171 	enet_phy_reset(efsc, phandle);
    172 
    173 	if (enet_attach_common(self) != 0)
    174 		goto failure;
    175 
    176 	return;
    177 
    178 failure:
    179 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
    180 	return;
    181 }
    182 
    183 static void *
    184 enet_intr_establish(struct enet_softc *sc, int phandle, u_int index)
    185 {
    186 	char intrstr[128];
    187 	char xname[16];
    188 	void *ih;
    189 
    190 	if (!fdtbus_intr_str(phandle, index, intrstr, sizeof(intrstr))) {
    191 		aprint_error_dev(sc->sc_dev, "failed to decode interrupt %d\n",
    192 		    index);
    193 		return NULL;
    194 	}
    195 
    196 	snprintf(xname, sizeof(xname), "%s #%u", device_xname(sc->sc_dev),
    197 	    index);
    198 	ih = fdtbus_intr_establish_xname(phandle, index, IPL_NET, 0,
    199 	    enet_intr, sc, xname);
    200 	if (ih == NULL) {
    201 		aprint_error_dev(sc->sc_dev, "failed to establish interrupt on %s\n",
    202 		    intrstr);
    203 		return NULL;
    204 	}
    205 	aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
    206 
    207 	return ih;
    208 }
    209 
    210 static int
    211 enet_init_clocks(struct enet_softc *sc)
    212 {
    213 	int error;
    214 
    215 	error = clk_enable(sc->sc_clk_ipg);
    216 	if (error) {
    217 		aprint_error_dev(sc->sc_dev, "couldn't enable ipg: %d\n", error);
    218 		return error;
    219 	}
    220 	error = clk_enable(sc->sc_clk_enet);
    221 	if (error) {
    222 		aprint_error_dev(sc->sc_dev, "couldn't enable enet: %d\n", error);
    223 		return error;
    224 	}
    225 	error = clk_enable(sc->sc_clk_enet_ref);
    226 	if (error) {
    227 		aprint_error_dev(sc->sc_dev, "couldn't enable enet_ref: %d\n", error);
    228 		return error;
    229 	}
    230 
    231 	return 0;
    232 }
    233 
    234 static void
    235 enet_phy_reset(struct enet_fdt_softc *sc, const int phandle)
    236 {
    237 	u_int msec;
    238 
    239 	sc->sc_pin_reset = fdtbus_gpio_acquire(phandle, "phy-reset-gpios", GPIO_PIN_OUTPUT);
    240 	if (sc->sc_pin_reset == NULL) {
    241 		aprint_error_dev(sc->sc_enet.sc_dev, "couldn't find phy reset gpios\n");
    242 		return;
    243 	}
    244 
    245 	if (of_getprop_uint32(phandle, "phy-reset-duration", &msec))
    246 		msec = 1;
    247 
    248 	/* Reset */
    249 	fdtbus_gpio_write(sc->sc_pin_reset, 1);
    250 	delay(msec * 1000);
    251 	fdtbus_gpio_write(sc->sc_pin_reset, 0);
    252 
    253 	/* Post delay */
    254 	if (of_getprop_uint32(phandle, "phy-reset-post-delay", &msec))
    255 		msec = 0;
    256 
    257 	delay(msec * 1000);
    258 }
    259 
    260 static int
    261 enet_phy_id(struct enet_softc *sc, const int phandle)
    262 {
    263 	int phy_phandle;
    264 	bus_addr_t addr;
    265 
    266 	phy_phandle = fdtbus_get_phandle(phandle, "phy-handle");
    267 	if (phy_phandle == -1)
    268 		return MII_PHY_ANY;
    269 
    270 	if (fdtbus_get_reg(phy_phandle, 0, &addr, NULL) != 0)
    271 		return MII_PHY_ANY;
    272 
    273 	return (int)addr;
    274 }
    275