genet_fdt.c revision 1.4
11.4Sthorpej/* $NetBSD: genet_fdt.c,v 1.4 2021/01/27 03:10:21 thorpej 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 "opt_net_mpsafe.h"
301.1Sjmcneill
311.1Sjmcneill#include <sys/cdefs.h>
321.4Sthorpej__KERNEL_RCSID(0, "$NetBSD: genet_fdt.c,v 1.4 2021/01/27 03:10:21 thorpej Exp $");
331.1Sjmcneill
341.1Sjmcneill#include <sys/param.h>
351.1Sjmcneill#include <sys/bus.h>
361.1Sjmcneill#include <sys/device.h>
371.1Sjmcneill#include <sys/intr.h>
381.1Sjmcneill#include <sys/systm.h>
391.1Sjmcneill#include <sys/kernel.h>
401.1Sjmcneill
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.1Sjmcneill#ifdef NET_MPSAFE
531.1Sjmcneill#define	FDT_INTR_FLAGS	FDT_INTR_MPSAFE
541.1Sjmcneill#else
551.1Sjmcneill#define	FDT_INTR_FLAGS	0
561.1Sjmcneill#endif
571.1Sjmcneill
581.1Sjmcneillstatic int	genet_fdt_match(device_t, cfdata_t, void *);
591.1Sjmcneillstatic void	genet_fdt_attach(device_t, device_t, void *);
601.1Sjmcneill
611.1SjmcneillCFATTACH_DECL_NEW(genet_fdt, sizeof(struct genet_softc),
621.1Sjmcneill	genet_fdt_match, genet_fdt_attach, NULL, NULL);
631.1Sjmcneill
641.4Sthorpejstatic const struct device_compatible_entry compat_data[] = {
651.4Sthorpej	{ .compat = "brcm,bcm2711-genet-v5" },
661.4Sthorpej	DEVICE_COMPAT_EOL
671.4Sthorpej};
681.4Sthorpej
691.1Sjmcneillstatic int
701.1Sjmcneillgenet_fdt_match(device_t parent, cfdata_t cf, void *aux)
711.1Sjmcneill{
721.1Sjmcneill	struct fdt_attach_args * const faa = aux;
731.1Sjmcneill
741.4Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
751.1Sjmcneill}
761.1Sjmcneill
771.1Sjmcneillstatic void
781.1Sjmcneillgenet_fdt_attach(device_t parent, device_t self, void *aux)
791.1Sjmcneill{
801.1Sjmcneill	struct genet_softc * const sc = device_private(self);
811.1Sjmcneill	struct fdt_attach_args * const faa = aux;
821.1Sjmcneill	const int phandle = faa->faa_phandle;
831.1Sjmcneill	char intrstr[128];
841.1Sjmcneill	bus_addr_t addr;
851.1Sjmcneill	bus_size_t size;
861.1Sjmcneill	void *ih;
871.1Sjmcneill
881.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
891.1Sjmcneill		aprint_error(": couldn't get registers\n");
901.1Sjmcneill		return;
911.1Sjmcneill	}
921.1Sjmcneill
931.1Sjmcneill	sc->sc_dev = self;
941.1Sjmcneill	sc->sc_bst = faa->faa_bst;
951.1Sjmcneill	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
961.1Sjmcneill		aprint_error(": couldn't map registers\n");
971.1Sjmcneill		return;
981.1Sjmcneill	}
991.1Sjmcneill	sc->sc_dmat = faa->faa_dmat;
1001.1Sjmcneill	sc->sc_phy_id = MII_PHY_ANY;
1011.1Sjmcneill
1021.1Sjmcneill	const char *phy_mode = fdtbus_get_string(phandle, "phy-mode");
1031.1Sjmcneill	if (phy_mode == NULL) {
1041.1Sjmcneill		aprint_error(": missing 'phy-mode' property\n");
1051.1Sjmcneill		return;
1061.1Sjmcneill	}
1071.1Sjmcneill
1081.1Sjmcneill	if (strcmp(phy_mode, "rgmii-rxid") == 0)
1091.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_RXID;
1101.1Sjmcneill	else if (strcmp(phy_mode, "rgmii-txid") == 0)
1111.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_TXID;
1121.2Sjmcneill	else if (strcmp(phy_mode, "rgmii-id") == 0)
1131.2Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_ID;
1141.1Sjmcneill	else if (strcmp(phy_mode, "rgmii") == 0)
1151.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII;
1161.1Sjmcneill	else {
1171.1Sjmcneill		aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
1181.1Sjmcneill		return;
1191.1Sjmcneill	}
1201.1Sjmcneill
1211.1Sjmcneill	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1221.1Sjmcneill		aprint_error(": failed to decode interrupt\n");
1231.1Sjmcneill		return;
1241.1Sjmcneill	}
1251.1Sjmcneill
1261.1Sjmcneill	if (genet_attach(sc) != 0)
1271.1Sjmcneill		return;
1281.1Sjmcneill
1291.3Sjmcneill	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_NET, FDT_INTR_FLAGS,
1301.3Sjmcneill	    genet_intr, sc, device_xname(self));
1311.1Sjmcneill	if (ih == NULL) {
1321.1Sjmcneill		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
1331.1Sjmcneill		    intrstr);
1341.1Sjmcneill		return;
1351.1Sjmcneill	}
1361.1Sjmcneill	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1371.1Sjmcneill}
138