xref: /openbsd/sys/dev/fdt/if_bse_fdt.c (revision 4bdff4be)
1 /*	$OpenBSD: if_bse_fdt.c,v 1.2 2022/04/06 18:59:28 naddy Exp $	*/
2 /*
3  * Copyright (c) 2020 Mark Kettenis <kettenis@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 
22 #include <machine/bus.h>
23 #include <machine/fdt.h>
24 
25 #include <net/if.h>
26 #include <net/if_media.h>
27 #include <netinet/in.h>
28 #include <netinet/if_ether.h>
29 
30 #include <dev/ofw/openfirm.h>
31 #include <dev/ofw/fdt.h>
32 
33 #include <dev/mii/miivar.h>
34 #include <dev/ic/bcmgenetvar.h>
35 
36 int	bse_fdt_match(struct device *, void *, void *);
37 void	bse_fdt_attach(struct device *, struct device *, void *);
38 
39 const struct cfattach bse_fdt_ca = {
40 	sizeof (struct genet_softc), bse_fdt_match, bse_fdt_attach
41 };
42 
43 int
44 bse_fdt_match(struct device *parent, void *match, void *aux)
45 {
46 	struct fdt_attach_args *faa = aux;
47 
48 	return (OF_is_compatible(faa->fa_node, "brcm,bcm2711-genet-v5") ||
49 	    OF_is_compatible(faa->fa_node, "brcm,genet-v5"));
50 }
51 
52 void
53 bse_fdt_attach(struct device *parent, struct device *self, void *aux)
54 {
55 	struct genet_softc *sc = (struct genet_softc *)self;
56 	struct fdt_attach_args *faa = aux;
57 	char phy_mode[16] = { 0 };
58 	uint32_t phy;
59 	int node, error;
60 
61 	if (faa->fa_nreg < 1) {
62 		printf(": no registers\n");
63 		return;
64 	}
65 
66 	sc->sc_bst = faa->fa_iot;
67 	sc->sc_dmat = faa->fa_dmat;
68 
69 	if (bus_space_map(sc->sc_bst, faa->fa_reg[0].addr,
70 	    faa->fa_reg[0].size, 0, &sc->sc_bsh)) {
71 		printf(": can't map registers\n");
72 		return;
73 	}
74 
75 	sc->sc_ih = fdt_intr_establish(faa->fa_node, IPL_NET,
76 	    genet_intr, sc, sc->sc_dev.dv_xname);
77 	if (sc->sc_ih == NULL) {
78 		printf(": can't establish interrupt\n");
79 		goto unmap;
80 	}
81 
82 	if (OF_getprop(faa->fa_node, "local-mac-address",
83 	    &sc->sc_lladdr, ETHER_ADDR_LEN) != ETHER_ADDR_LEN)
84 		genet_lladdr_read(sc, sc->sc_lladdr);
85 
86 	OF_getprop(faa->fa_node, "phy-mode", phy_mode, sizeof(phy_mode));
87 	if (strcmp(phy_mode, "rgmii-id") == 0)
88 		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_ID;
89 	else if (strcmp(phy_mode, "rgmii-rxid") == 0)
90 		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_RXID;
91 	else if (strcmp(phy_mode, "rgmii-txid") == 0)
92 		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_TXID;
93 	else
94 		sc->sc_phy_mode = GENET_PHY_MODE_RGMII;
95 
96 	/* Lookup PHY. */
97 	phy = OF_getpropint(faa->fa_node, "phy-handle", 0);
98 	node = OF_getnodebyphandle(phy);
99 	if (node)
100 		sc->sc_phy_id = OF_getpropint(node, "reg", MII_PHY_ANY);
101 	else
102 		sc->sc_phy_id = MII_PHY_ANY;
103 
104 	error = genet_attach(sc);
105 	if (error)
106 		goto disestablish;
107 
108 	return;
109 
110 disestablish:
111 	fdt_intr_disestablish(sc->sc_ih);
112 unmap:
113 	bus_space_unmap(sc->sc_bst, sc->sc_bsh, faa->fa_reg[0].size);
114 }
115