xref: /netbsd-src/sys/dev/sbus/if_gem_sbus.c (revision 13d4bb4cc874de96add7fc4227d38a1d656b03d1)
1 /*	$NetBSD: if_gem_sbus.c,v 1.14 2022/09/25 18:03:04 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Martin Husemann.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * SBus front-end for the GEM network driver
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: if_gem_sbus.c,v 1.14 2022/09/25 18:03:04 thorpej Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/syslog.h>
42 #include <sys/device.h>
43 #include <sys/socket.h>
44 
45 #include <net/if.h>
46 #include <net/if_dl.h>
47 #include <net/if_ether.h>
48 #include <net/if_media.h>
49 
50 #include <dev/mii/mii.h>
51 #include <dev/mii/miivar.h>
52 
53 #include <sys/bus.h>
54 #include <sys/intr.h>
55 #include <machine/autoconf.h>
56 
57 #include <dev/sbus/sbusvar.h>
58 
59 #include <dev/ic/gemreg.h>
60 #include <dev/ic/gemvar.h>
61 
62 struct gem_sbus_softc {
63 	struct	gem_softc	gsc_gem;	/* GEM device */
64 	void			*gsc_ih;
65 	bus_space_handle_t	gsc_sbus_regs_h;
66 };
67 
68 int	gemmatch_sbus(device_t, cfdata_t, void *);
69 void	gemattach_sbus(device_t, device_t, void *);
70 
71 CFATTACH_DECL3_NEW(gem_sbus, sizeof(struct gem_sbus_softc),
72     gemmatch_sbus, gemattach_sbus, NULL, NULL, NULL, NULL, 0);
73 
74 int
gemmatch_sbus(device_t parent,cfdata_t cf,void * aux)75 gemmatch_sbus(device_t parent, cfdata_t cf, void *aux)
76 {
77 	struct sbus_attach_args *sa = aux;
78 
79 	return (strcmp("network", sa->sa_name) == 0);
80 }
81 
82 void
gemattach_sbus(device_t parent,device_t self,void * aux)83 gemattach_sbus(device_t parent, device_t self, void *aux)
84 {
85 	struct sbus_attach_args *sa = aux;
86 	struct gem_sbus_softc *gsc = device_private(self);
87 	struct gem_softc *sc = &gsc->gsc_gem;
88 	uint8_t enaddr[ETHER_ADDR_LEN];
89 
90 	sc->sc_dev = self;
91 
92 	/* Pass on the bus tags */
93 	sc->sc_bustag = sa->sa_bustag;
94 	sc->sc_dmatag = sa->sa_dmatag;
95 
96 	if (sa->sa_nreg < 2) {
97 		printf("%s: only %d register sets\n",
98 			device_xname(self), sa->sa_nreg);
99 		return;
100 	}
101 
102 	/*
103 	 * Map two register banks:
104 	 *
105 	 *	bank 0: status, config, reset
106 	 *	bank 1: various gem parts
107 	 *
108 	 */
109 	if (sbus_bus_map(sa->sa_bustag,
110 			 sa->sa_reg[0].oa_space,
111 			 sa->sa_reg[0].oa_base,
112 			 (bus_size_t)sa->sa_reg[0].oa_size,
113 			 0, &sc->sc_h2) != 0) {
114 		aprint_error_dev(self, "cannot map registers\n");
115 		return;
116 	}
117 	if (sbus_bus_map(sa->sa_bustag,
118 			 sa->sa_reg[1].oa_space,
119 			 sa->sa_reg[1].oa_base,
120 			 (bus_size_t)sa->sa_reg[1].oa_size,
121 			 0, &sc->sc_h1) != 0) {
122 		aprint_error_dev(self, "cannot map registers\n");
123 		return;
124 	}
125 	prom_getether(sa->sa_node, enaddr);
126 
127 	if (!strcmp("serdes", prom_getpropstring(sa->sa_node, "shared-pins")))
128 		sc->sc_flags |= GEM_SERDES;
129 	sc->sc_variant = GEM_SUN_GEM;
130 	sc->sc_flags &= ~GEM_PCI;
131 
132 	/*
133 	 * SBUS config
134 	 */
135 	(void) bus_space_read_4(sa->sa_bustag, sc->sc_h2, GEM_SBUS_RESET);
136 	delay(100);
137 	bus_space_write_4(sa->sa_bustag, sc->sc_h2, GEM_SBUS_CONFIG,
138 	    GEM_SBUS_CFG_BSIZE128|GEM_SBUS_CFG_PARITY|GEM_SBUS_CFG_BMODE64);
139 	sc->sc_chiprev = bus_space_read_4(sa->sa_bustag, sc->sc_h2,
140 	    GEM_SBUS_REVISION);
141 
142 	printf(": GEM Ethernet controller (%s), version %s (rev 0x%02x)\n",
143 	    sa->sa_name, prom_getpropstring(sa->sa_node, "version"),
144 	    sc->sc_chiprev);
145 
146 	gem_attach(sc, enaddr);
147 
148 	/* Establish interrupt handler */
149 	if (sa->sa_nintr != 0)
150 		gsc->gsc_ih = bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET,
151 					gem_intr, sc);
152 }
153