1*13d4bb4cSthorpej /* $NetBSD: if_gem_sbus.c,v 1.14 2022/09/25 18:03:04 thorpej Exp $ */
2381d97cfSmartin
3381d97cfSmartin /*-
4381d97cfSmartin * Copyright (c) 2006 The NetBSD Foundation, Inc.
5381d97cfSmartin * All rights reserved.
6381d97cfSmartin *
7381d97cfSmartin * This code is derived from software contributed to The NetBSD Foundation
8381d97cfSmartin * by Martin Husemann.
9381d97cfSmartin *
10381d97cfSmartin * Redistribution and use in source and binary forms, with or without
11381d97cfSmartin * modification, are permitted provided that the following conditions
12381d97cfSmartin * are met:
13381d97cfSmartin * 1. Redistributions of source code must retain the above copyright
14381d97cfSmartin * notice, this list of conditions and the following disclaimer.
15381d97cfSmartin * 2. Redistributions in binary form must reproduce the above copyright
16381d97cfSmartin * notice, this list of conditions and the following disclaimer in the
17381d97cfSmartin * documentation and/or other materials provided with the distribution.
18381d97cfSmartin *
19381d97cfSmartin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20381d97cfSmartin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21381d97cfSmartin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22381d97cfSmartin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23381d97cfSmartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24381d97cfSmartin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25381d97cfSmartin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26381d97cfSmartin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27381d97cfSmartin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28381d97cfSmartin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29381d97cfSmartin * POSSIBILITY OF SUCH DAMAGE.
30381d97cfSmartin */
31381d97cfSmartin
32381d97cfSmartin /*
33381d97cfSmartin * SBus front-end for the GEM network driver
34381d97cfSmartin */
35381d97cfSmartin
36381d97cfSmartin #include <sys/cdefs.h>
37*13d4bb4cSthorpej __KERNEL_RCSID(0, "$NetBSD: if_gem_sbus.c,v 1.14 2022/09/25 18:03:04 thorpej Exp $");
38381d97cfSmartin
39381d97cfSmartin #include <sys/param.h>
40381d97cfSmartin #include <sys/systm.h>
41381d97cfSmartin #include <sys/syslog.h>
42381d97cfSmartin #include <sys/device.h>
43381d97cfSmartin #include <sys/socket.h>
44381d97cfSmartin
45381d97cfSmartin #include <net/if.h>
46381d97cfSmartin #include <net/if_dl.h>
47381d97cfSmartin #include <net/if_ether.h>
48381d97cfSmartin #include <net/if_media.h>
49381d97cfSmartin
50381d97cfSmartin #include <dev/mii/mii.h>
51381d97cfSmartin #include <dev/mii/miivar.h>
52381d97cfSmartin
53a2a38285Sad #include <sys/bus.h>
54a2a38285Sad #include <sys/intr.h>
55381d97cfSmartin #include <machine/autoconf.h>
56381d97cfSmartin
57381d97cfSmartin #include <dev/sbus/sbusvar.h>
58381d97cfSmartin
59381d97cfSmartin #include <dev/ic/gemreg.h>
60381d97cfSmartin #include <dev/ic/gemvar.h>
61381d97cfSmartin
62381d97cfSmartin struct gem_sbus_softc {
63381d97cfSmartin struct gem_softc gsc_gem; /* GEM device */
64381d97cfSmartin void *gsc_ih;
65381d97cfSmartin bus_space_handle_t gsc_sbus_regs_h;
66381d97cfSmartin };
67381d97cfSmartin
687cf29912Scegger int gemmatch_sbus(device_t, cfdata_t, void *);
697cf29912Scegger void gemattach_sbus(device_t, device_t, void *);
70381d97cfSmartin
719c3d3743Smartin CFATTACH_DECL3_NEW(gem_sbus, sizeof(struct gem_sbus_softc),
729c3d3743Smartin gemmatch_sbus, gemattach_sbus, NULL, NULL, NULL, NULL, 0);
73381d97cfSmartin
74381d97cfSmartin int
gemmatch_sbus(device_t parent,cfdata_t cf,void * aux)757cf29912Scegger gemmatch_sbus(device_t parent, cfdata_t cf, void *aux)
76381d97cfSmartin {
77381d97cfSmartin struct sbus_attach_args *sa = aux;
78381d97cfSmartin
79381d97cfSmartin return (strcmp("network", sa->sa_name) == 0);
80381d97cfSmartin }
81381d97cfSmartin
82381d97cfSmartin void
gemattach_sbus(device_t parent,device_t self,void * aux)837cf29912Scegger gemattach_sbus(device_t parent, device_t self, void *aux)
84381d97cfSmartin {
85381d97cfSmartin struct sbus_attach_args *sa = aux;
861b6947b9Sdyoung struct gem_sbus_softc *gsc = device_private(self);
87381d97cfSmartin struct gem_softc *sc = &gsc->gsc_gem;
88381d97cfSmartin uint8_t enaddr[ETHER_ADDR_LEN];
89381d97cfSmartin
901b6947b9Sdyoung sc->sc_dev = self;
911b6947b9Sdyoung
92381d97cfSmartin /* Pass on the bus tags */
93381d97cfSmartin sc->sc_bustag = sa->sa_bustag;
94381d97cfSmartin sc->sc_dmatag = sa->sa_dmatag;
95381d97cfSmartin
96381d97cfSmartin if (sa->sa_nreg < 2) {
97381d97cfSmartin printf("%s: only %d register sets\n",
984c5fa20dScegger device_xname(self), sa->sa_nreg);
99381d97cfSmartin return;
100381d97cfSmartin }
101381d97cfSmartin
102381d97cfSmartin /*
103381d97cfSmartin * Map two register banks:
104381d97cfSmartin *
105381d97cfSmartin * bank 0: status, config, reset
106381d97cfSmartin * bank 1: various gem parts
107381d97cfSmartin *
108381d97cfSmartin */
109381d97cfSmartin if (sbus_bus_map(sa->sa_bustag,
110381d97cfSmartin sa->sa_reg[0].oa_space,
111381d97cfSmartin sa->sa_reg[0].oa_base,
112381d97cfSmartin (bus_size_t)sa->sa_reg[0].oa_size,
113381d97cfSmartin 0, &sc->sc_h2) != 0) {
1144c5fa20dScegger aprint_error_dev(self, "cannot map registers\n");
115381d97cfSmartin return;
116381d97cfSmartin }
117381d97cfSmartin if (sbus_bus_map(sa->sa_bustag,
118381d97cfSmartin sa->sa_reg[1].oa_space,
119381d97cfSmartin sa->sa_reg[1].oa_base,
120381d97cfSmartin (bus_size_t)sa->sa_reg[1].oa_size,
121381d97cfSmartin 0, &sc->sc_h1) != 0) {
1224c5fa20dScegger aprint_error_dev(self, "cannot map registers\n");
123381d97cfSmartin return;
124381d97cfSmartin }
125381d97cfSmartin prom_getether(sa->sa_node, enaddr);
126381d97cfSmartin
127479872d6Sjdc if (!strcmp("serdes", prom_getpropstring(sa->sa_node, "shared-pins")))
128479872d6Sjdc sc->sc_flags |= GEM_SERDES;
129313a56c0Sjdc sc->sc_variant = GEM_SUN_GEM;
1307892af0dSjdc sc->sc_flags &= ~GEM_PCI;
131479872d6Sjdc
132381d97cfSmartin /*
133381d97cfSmartin * SBUS config
134381d97cfSmartin */
13593d3122aSjdc (void) bus_space_read_4(sa->sa_bustag, sc->sc_h2, GEM_SBUS_RESET);
13693d3122aSjdc delay(100);
137381d97cfSmartin bus_space_write_4(sa->sa_bustag, sc->sc_h2, GEM_SBUS_CONFIG,
13893d3122aSjdc GEM_SBUS_CFG_BSIZE128|GEM_SBUS_CFG_PARITY|GEM_SBUS_CFG_BMODE64);
1397892af0dSjdc sc->sc_chiprev = bus_space_read_4(sa->sa_bustag, sc->sc_h2,
1407892af0dSjdc GEM_SBUS_REVISION);
1417892af0dSjdc
1427892af0dSjdc printf(": GEM Ethernet controller (%s), version %s (rev 0x%02x)\n",
1437892af0dSjdc sa->sa_name, prom_getpropstring(sa->sa_node, "version"),
1447892af0dSjdc sc->sc_chiprev);
145381d97cfSmartin
146381d97cfSmartin gem_attach(sc, enaddr);
147381d97cfSmartin
148381d97cfSmartin /* Establish interrupt handler */
149381d97cfSmartin if (sa->sa_nintr != 0)
150381d97cfSmartin gsc->gsc_ih = bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET,
151381d97cfSmartin gem_intr, sc);
152381d97cfSmartin }
153