genet_fdt.c revision 1.2
11.2Sjmcneill/* $NetBSD: genet_fdt.c,v 1.2 2020/05/25 19:49:28 jmcneill 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.2Sjmcneill__KERNEL_RCSID(0, "$NetBSD: genet_fdt.c,v 1.2 2020/05/25 19:49:28 jmcneill 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.1Sjmcneillstatic int
651.1Sjmcneillgenet_fdt_match(device_t parent, cfdata_t cf, void *aux)
661.1Sjmcneill{
671.1Sjmcneill	const char * const compatible[] = {
681.1Sjmcneill		"brcm,bcm2711-genet-v5",
691.1Sjmcneill		NULL
701.1Sjmcneill	};
711.1Sjmcneill	struct fdt_attach_args * const faa = aux;
721.1Sjmcneill
731.1Sjmcneill	return of_match_compatible(faa->faa_phandle, compatible);
741.1Sjmcneill}
751.1Sjmcneill
761.1Sjmcneillstatic void
771.1Sjmcneillgenet_fdt_attach(device_t parent, device_t self, void *aux)
781.1Sjmcneill{
791.1Sjmcneill	struct genet_softc * const sc = device_private(self);
801.1Sjmcneill	struct fdt_attach_args * const faa = aux;
811.1Sjmcneill	const int phandle = faa->faa_phandle;
821.1Sjmcneill	char intrstr[128];
831.1Sjmcneill	bus_addr_t addr;
841.1Sjmcneill	bus_size_t size;
851.1Sjmcneill	void *ih;
861.1Sjmcneill
871.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
881.1Sjmcneill		aprint_error(": couldn't get registers\n");
891.1Sjmcneill		return;
901.1Sjmcneill	}
911.1Sjmcneill
921.1Sjmcneill	sc->sc_dev = self;
931.1Sjmcneill	sc->sc_bst = faa->faa_bst;
941.1Sjmcneill	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
951.1Sjmcneill		aprint_error(": couldn't map registers\n");
961.1Sjmcneill		return;
971.1Sjmcneill	}
981.1Sjmcneill	sc->sc_dmat = faa->faa_dmat;
991.1Sjmcneill	sc->sc_phy_id = MII_PHY_ANY;
1001.1Sjmcneill
1011.1Sjmcneill	const char *phy_mode = fdtbus_get_string(phandle, "phy-mode");
1021.1Sjmcneill	if (phy_mode == NULL) {
1031.1Sjmcneill		aprint_error(": missing 'phy-mode' property\n");
1041.1Sjmcneill		return;
1051.1Sjmcneill	}
1061.1Sjmcneill
1071.1Sjmcneill	if (strcmp(phy_mode, "rgmii-rxid") == 0)
1081.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_RXID;
1091.1Sjmcneill	else if (strcmp(phy_mode, "rgmii-txid") == 0)
1101.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_TXID;
1111.2Sjmcneill	else if (strcmp(phy_mode, "rgmii-id") == 0)
1121.2Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_ID;
1131.1Sjmcneill	else if (strcmp(phy_mode, "rgmii") == 0)
1141.1Sjmcneill		sc->sc_phy_mode = GENET_PHY_MODE_RGMII;
1151.1Sjmcneill	else {
1161.1Sjmcneill		aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
1171.1Sjmcneill		return;
1181.1Sjmcneill	}
1191.1Sjmcneill
1201.1Sjmcneill	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1211.1Sjmcneill		aprint_error(": failed to decode interrupt\n");
1221.1Sjmcneill		return;
1231.1Sjmcneill	}
1241.1Sjmcneill
1251.1Sjmcneill	if (genet_attach(sc) != 0)
1261.1Sjmcneill		return;
1271.1Sjmcneill
1281.1Sjmcneill	ih = fdtbus_intr_establish(phandle, 0, IPL_NET, FDT_INTR_FLAGS,
1291.1Sjmcneill	    genet_intr, sc);
1301.1Sjmcneill	if (ih == NULL) {
1311.1Sjmcneill		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
1321.1Sjmcneill		    intrstr);
1331.1Sjmcneill		return;
1341.1Sjmcneill	}
1351.1Sjmcneill	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1361.1Sjmcneill}
137