xref: /openbsd-src/sys/arch/octeon/dev/ogxnexus.c (revision 3ec638c914bf706bde3e7b3f0d3afc63c7d6b784)
1*3ec638c9Svisa /*	$OpenBSD: ogxnexus.c,v 1.1 2019/11/04 14:58:40 visa Exp $	*/
2*3ec638c9Svisa 
3*3ec638c9Svisa /*
4*3ec638c9Svisa  * Copyright (c) 2019 Visa Hankala
5*3ec638c9Svisa  *
6*3ec638c9Svisa  * Permission to use, copy, modify, and/or distribute this software for any
7*3ec638c9Svisa  * purpose with or without fee is hereby granted, provided that the above
8*3ec638c9Svisa  * copyright notice and this permission notice appear in all copies.
9*3ec638c9Svisa  *
10*3ec638c9Svisa  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*3ec638c9Svisa  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*3ec638c9Svisa  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*3ec638c9Svisa  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*3ec638c9Svisa  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*3ec638c9Svisa  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*3ec638c9Svisa  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*3ec638c9Svisa  */
18*3ec638c9Svisa 
19*3ec638c9Svisa /*
20*3ec638c9Svisa  * Driver for OCTEON BGX nexus.
21*3ec638c9Svisa  */
22*3ec638c9Svisa 
23*3ec638c9Svisa #include <sys/param.h>
24*3ec638c9Svisa #include <sys/systm.h>
25*3ec638c9Svisa #include <sys/device.h>
26*3ec638c9Svisa 
27*3ec638c9Svisa #include <dev/ofw/fdt.h>
28*3ec638c9Svisa #include <dev/ofw/openfirm.h>
29*3ec638c9Svisa 
30*3ec638c9Svisa #include <machine/bus.h>
31*3ec638c9Svisa #include <machine/fdt.h>
32*3ec638c9Svisa 
33*3ec638c9Svisa #include <octeon/dev/ogxreg.h>
34*3ec638c9Svisa #include <octeon/dev/ogxvar.h>
35*3ec638c9Svisa 
36*3ec638c9Svisa struct ogxnexus_softc {
37*3ec638c9Svisa 	struct device		 sc_dev;
38*3ec638c9Svisa 	bus_space_tag_t		 sc_iot;
39*3ec638c9Svisa 	bus_space_handle_t	 sc_ioh;
40*3ec638c9Svisa 	int			 sc_bgxid;
41*3ec638c9Svisa };
42*3ec638c9Svisa 
43*3ec638c9Svisa #define NEXUS_RD_8(sc, reg) \
44*3ec638c9Svisa 	bus_space_write_8((sc)->sc_iot, (sc)->sc_ioh, (reg))
45*3ec638c9Svisa #define NEXUS_WR_8(sc, reg, val) \
46*3ec638c9Svisa 	bus_space_write_8((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
47*3ec638c9Svisa 
48*3ec638c9Svisa int	ogxnexus_match(struct device *, void *, void *);
49*3ec638c9Svisa void	ogxnexus_attach(struct device *, struct device *, void *);
50*3ec638c9Svisa 
51*3ec638c9Svisa const struct cfattach ogxnexus_ca = {
52*3ec638c9Svisa 	sizeof(struct ogxnexus_softc), ogxnexus_match, ogxnexus_attach
53*3ec638c9Svisa };
54*3ec638c9Svisa 
55*3ec638c9Svisa struct cfdriver ogxnexus_cd = {
56*3ec638c9Svisa 	NULL, "ogxnexus", DV_DULL
57*3ec638c9Svisa };
58*3ec638c9Svisa 
59*3ec638c9Svisa int
ogxnexus_match(struct device * parent,void * match,void * aux)60*3ec638c9Svisa ogxnexus_match(struct device *parent, void *match, void *aux)
61*3ec638c9Svisa {
62*3ec638c9Svisa 	struct fdt_attach_args *faa = aux;
63*3ec638c9Svisa 
64*3ec638c9Svisa 	return OF_is_compatible(faa->fa_node, "cavium,octeon-7890-bgx");
65*3ec638c9Svisa }
66*3ec638c9Svisa 
67*3ec638c9Svisa void
ogxnexus_attach(struct device * parent,struct device * self,void * aux)68*3ec638c9Svisa ogxnexus_attach(struct device *parent, struct device *self, void *aux)
69*3ec638c9Svisa {
70*3ec638c9Svisa 	struct ogx_attach_args oaa;
71*3ec638c9Svisa 	struct fdt_attach_args *faa = aux;
72*3ec638c9Svisa 	struct ogxnexus_softc *sc = (struct ogxnexus_softc *)self;
73*3ec638c9Svisa 	int i, node;
74*3ec638c9Svisa 
75*3ec638c9Svisa 	if (faa->fa_nreg < 1) {
76*3ec638c9Svisa 		printf(": no registers\n");
77*3ec638c9Svisa 		return;
78*3ec638c9Svisa 	}
79*3ec638c9Svisa 
80*3ec638c9Svisa 	sc->sc_bgxid = (faa->fa_reg[0].addr >> 24) & 7;
81*3ec638c9Svisa 
82*3ec638c9Svisa 	sc->sc_iot = faa->fa_iot;
83*3ec638c9Svisa 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, faa->fa_reg[0].size,
84*3ec638c9Svisa 	    0, &sc->sc_ioh)) {
85*3ec638c9Svisa 		printf(": can't map IO space\n");
86*3ec638c9Svisa 		return;
87*3ec638c9Svisa 	}
88*3ec638c9Svisa 
89*3ec638c9Svisa 	/* Disable all CAM entries. */
90*3ec638c9Svisa 	for (i = 0; i < BGX_NCAM; i++)
91*3ec638c9Svisa 		NEXUS_WR_8(sc, BGX_CMR_RX_ADR_CAM(i), 0);
92*3ec638c9Svisa 
93*3ec638c9Svisa 	/* Set the number of LMACs per BGX. */
94*3ec638c9Svisa 	NEXUS_WR_8(sc, BGX_CMR_RX_LMACS, BGX_NLMAC);
95*3ec638c9Svisa 	NEXUS_WR_8(sc, BGX_CMR_TX_LMACS, BGX_NLMAC);
96*3ec638c9Svisa 
97*3ec638c9Svisa 	printf("\n");
98*3ec638c9Svisa 
99*3ec638c9Svisa 	for (node = OF_child(faa->fa_node); node != 0; node = OF_peer(node)) {
100*3ec638c9Svisa 		memset(&oaa, 0, sizeof(oaa));
101*3ec638c9Svisa 		oaa.oaa_node = node;
102*3ec638c9Svisa 		oaa.oaa_bgxid = sc->sc_bgxid;
103*3ec638c9Svisa 		oaa.oaa_iot = sc->sc_iot;
104*3ec638c9Svisa 		oaa.oaa_ioh = sc->sc_ioh;
105*3ec638c9Svisa 		oaa.oaa_dmat = faa->fa_dmat;
106*3ec638c9Svisa 
107*3ec638c9Svisa 		config_found(&sc->sc_dev, &oaa, NULL);
108*3ec638c9Svisa 	}
109*3ec638c9Svisa }
110