genet_fdt.c revision 1.7
11.7Sskrll/* $NetBSD: genet_fdt.c,v 1.7 2024/09/15 08:30:01 skrll Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2020 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.7Sskrll__KERNEL_RCSID(0, "$NetBSD: genet_fdt.c,v 1.7 2024/09/15 08:30:01 skrll Exp $");
311.1Sjmcneill
321.1Sjmcneill#include <sys/param.h>
331.1Sjmcneill#include <sys/bus.h>
341.1Sjmcneill#include <sys/device.h>
351.1Sjmcneill#include <sys/intr.h>
361.1Sjmcneill#include <sys/systm.h>
371.1Sjmcneill#include <sys/kernel.h>
381.1Sjmcneill
391.6Srin#include <sys/rndsource.h>
401.6Srin
411.1Sjmcneill#include <net/if.h>
421.1Sjmcneill#include <net/if_dl.h>
431.1Sjmcneill#include <net/if_ether.h>
441.1Sjmcneill#include <net/if_media.h>
451.1Sjmcneill
461.1Sjmcneill#include <dev/mii/miivar.h>
471.1Sjmcneill
481.1Sjmcneill#include <dev/ic/bcmgenetvar.h>
491.1Sjmcneill
501.1Sjmcneill#include <dev/fdt/fdtvar.h>
511.1Sjmcneill
521.1Sjmcneillstatic int	genet_fdt_match(device_t, cfdata_t, void *);
531.1Sjmcneillstatic void	genet_fdt_attach(device_t, device_t, void *);
541.1Sjmcneill
551.1SjmcneillCFATTACH_DECL_NEW(genet_fdt, sizeof(struct genet_softc),
561.1Sjmcneill	genet_fdt_match, genet_fdt_attach, NULL, NULL);
571.1Sjmcneill
581.4Sthorpejstatic const struct device_compatible_entry compat_data[] = {
591.4Sthorpej	{ .compat = "brcm,bcm2711-genet-v5" },
601.4Sthorpej	DEVICE_COMPAT_EOL
611.4Sthorpej};
621.4Sthorpej
631.1Sjmcneillstatic int
641.1Sjmcneillgenet_fdt_match(device_t parent, cfdata_t cf, void *aux)
651.1Sjmcneill{
661.1Sjmcneill	struct fdt_attach_args * const faa = aux;
671.1Sjmcneill
681.4Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
691.1Sjmcneill}
701.1Sjmcneill
711.1Sjmcneillstatic void
721.1Sjmcneillgenet_fdt_attach(device_t parent, device_t self, void *aux)
731.1Sjmcneill{
741.1Sjmcneill	struct genet_softc * const sc = device_private(self);
751.1Sjmcneill	struct fdt_attach_args * const faa = aux;
761.1Sjmcneill	const int phandle = faa->faa_phandle;
771.1Sjmcneill	char intrstr[128];
781.1Sjmcneill	bus_addr_t addr;
791.1Sjmcneill	bus_size_t size;
801.1Sjmcneill	void *ih;
811.1Sjmcneill
821.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
831.1Sjmcneill		aprint_error(": couldn't get registers\n");
841.1Sjmcneill		return;
851.1Sjmcneill	}
861.1Sjmcneill
871.1Sjmcneill	sc->sc_dev = self;
881.1Sjmcneill	sc->sc_bst = faa->faa_bst;
891.1Sjmcneill	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
901.1Sjmcneill		aprint_error(": couldn't map registers\n");
911.1Sjmcneill		return;
921.1Sjmcneill	}
931.1Sjmcneill	sc->sc_dmat = faa->faa_dmat;
941.1Sjmcneill	sc->sc_phy_id = MII_PHY_ANY;
951.1Sjmcneill
961.1Sjmcneill	const char *phy_mode = fdtbus_get_string(phandle, "phy-mode");
971.1Sjmcneill	if (phy_mode == NULL) {
981.1Sjmcneill		aprint_error(": missing 'phy-mode' property\n");
991.1Sjmcneill		return;
1001.1Sjmcneill	}
1011.1Sjmcneill
1021.1Sjmcneill	if (strcmp(phy_mode, "rgmii-rxid") == 0)
1031.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_RXID;
1041.1Sjmcneill	else if (strcmp(phy_mode, "rgmii-txid") == 0)
1051.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_TXID;
1061.2Sjmcneill	else if (strcmp(phy_mode, "rgmii-id") == 0)
1071.2Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_ID;
1081.1Sjmcneill	else if (strcmp(phy_mode, "rgmii") == 0)
1091.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII;
1101.1Sjmcneill	else {
1111.1Sjmcneill		aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
1121.1Sjmcneill		return;
1131.1Sjmcneill	}
1141.1Sjmcneill
1151.1Sjmcneill	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1161.1Sjmcneill		aprint_error(": failed to decode interrupt\n");
1171.1Sjmcneill		return;
1181.1Sjmcneill	}
1191.1Sjmcneill
1201.1Sjmcneill	if (genet_attach(sc) != 0)
1211.1Sjmcneill		return;
1221.1Sjmcneill
1231.5Smlelstv	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_NET, FDT_INTR_MPSAFE,
1241.3Sjmcneill	    genet_intr, sc, device_xname(self));
1251.1Sjmcneill	if (ih == NULL) {
1261.1Sjmcneill		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
1271.1Sjmcneill		    intrstr);
1281.1Sjmcneill		return;
1291.1Sjmcneill	}
1301.1Sjmcneill	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1311.1Sjmcneill}
132