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