genet_fdt.c revision 1.6
11.6Srin/* $NetBSD: genet_fdt.c,v 1.6 2021/05/03 10:28:26 rin 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.6Srin__KERNEL_RCSID(0, "$NetBSD: genet_fdt.c,v 1.6 2021/05/03 10:28:26 rin 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.6Srin#include <sys/rndsource.h>
421.6Srin
431.1Sjmcneill#include <net/if.h>
441.1Sjmcneill#include <net/if_dl.h>
451.1Sjmcneill#include <net/if_ether.h>
461.1Sjmcneill#include <net/if_media.h>
471.1Sjmcneill
481.1Sjmcneill#include <dev/mii/miivar.h>
491.1Sjmcneill
501.1Sjmcneill#include <dev/ic/bcmgenetvar.h>
511.1Sjmcneill
521.1Sjmcneill#include <dev/fdt/fdtvar.h>
531.1Sjmcneill
541.1Sjmcneill#ifdef NET_MPSAFE
551.1Sjmcneill#define	FDT_INTR_FLAGS	FDT_INTR_MPSAFE
561.1Sjmcneill#else
571.1Sjmcneill#define	FDT_INTR_FLAGS	0
581.1Sjmcneill#endif
591.1Sjmcneill
601.1Sjmcneillstatic int	genet_fdt_match(device_t, cfdata_t, void *);
611.1Sjmcneillstatic void	genet_fdt_attach(device_t, device_t, void *);
621.1Sjmcneill
631.1SjmcneillCFATTACH_DECL_NEW(genet_fdt, sizeof(struct genet_softc),
641.1Sjmcneill	genet_fdt_match, genet_fdt_attach, NULL, NULL);
651.1Sjmcneill
661.4Sthorpejstatic const struct device_compatible_entry compat_data[] = {
671.4Sthorpej	{ .compat = "brcm,bcm2711-genet-v5" },
681.4Sthorpej	DEVICE_COMPAT_EOL
691.4Sthorpej};
701.4Sthorpej
711.1Sjmcneillstatic int
721.1Sjmcneillgenet_fdt_match(device_t parent, cfdata_t cf, void *aux)
731.1Sjmcneill{
741.1Sjmcneill	struct fdt_attach_args * const faa = aux;
751.1Sjmcneill
761.4Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
771.1Sjmcneill}
781.1Sjmcneill
791.1Sjmcneillstatic void
801.1Sjmcneillgenet_fdt_attach(device_t parent, device_t self, void *aux)
811.1Sjmcneill{
821.1Sjmcneill	struct genet_softc * const sc = device_private(self);
831.1Sjmcneill	struct fdt_attach_args * const faa = aux;
841.1Sjmcneill	const int phandle = faa->faa_phandle;
851.1Sjmcneill	char intrstr[128];
861.1Sjmcneill	bus_addr_t addr;
871.1Sjmcneill	bus_size_t size;
881.1Sjmcneill	void *ih;
891.1Sjmcneill
901.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
911.1Sjmcneill		aprint_error(": couldn't get registers\n");
921.1Sjmcneill		return;
931.1Sjmcneill	}
941.1Sjmcneill
951.1Sjmcneill	sc->sc_dev = self;
961.1Sjmcneill	sc->sc_bst = faa->faa_bst;
971.1Sjmcneill	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
981.1Sjmcneill		aprint_error(": couldn't map registers\n");
991.1Sjmcneill		return;
1001.1Sjmcneill	}
1011.1Sjmcneill	sc->sc_dmat = faa->faa_dmat;
1021.1Sjmcneill	sc->sc_phy_id = MII_PHY_ANY;
1031.1Sjmcneill
1041.1Sjmcneill	const char *phy_mode = fdtbus_get_string(phandle, "phy-mode");
1051.1Sjmcneill	if (phy_mode == NULL) {
1061.1Sjmcneill		aprint_error(": missing 'phy-mode' property\n");
1071.1Sjmcneill		return;
1081.1Sjmcneill	}
1091.1Sjmcneill
1101.1Sjmcneill	if (strcmp(phy_mode, "rgmii-rxid") == 0)
1111.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_RXID;
1121.1Sjmcneill	else if (strcmp(phy_mode, "rgmii-txid") == 0)
1131.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_TXID;
1141.2Sjmcneill	else if (strcmp(phy_mode, "rgmii-id") == 0)
1151.2Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_ID;
1161.1Sjmcneill	else if (strcmp(phy_mode, "rgmii") == 0)
1171.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII;
1181.1Sjmcneill	else {
1191.1Sjmcneill		aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
1201.1Sjmcneill		return;
1211.1Sjmcneill	}
1221.1Sjmcneill
1231.1Sjmcneill	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1241.1Sjmcneill		aprint_error(": failed to decode interrupt\n");
1251.1Sjmcneill		return;
1261.1Sjmcneill	}
1271.1Sjmcneill
1281.1Sjmcneill	if (genet_attach(sc) != 0)
1291.1Sjmcneill		return;
1301.1Sjmcneill
1311.5Smlelstv	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_NET, FDT_INTR_MPSAFE,
1321.3Sjmcneill	    genet_intr, sc, device_xname(self));
1331.1Sjmcneill	if (ih == NULL) {
1341.1Sjmcneill		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
1351.1Sjmcneill		    intrstr);
1361.1Sjmcneill		return;
1371.1Sjmcneill	}
1381.1Sjmcneill	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1391.1Sjmcneill}
140