xref: /openbsd/sys/dev/sbus/if_gem_sbus.c (revision 771fbea0)
1 /*	$OpenBSD: if_gem_sbus.c,v 1.10 2015/11/28 09:42:10 jmatthew Exp $	*/
2 /*	$NetBSD: if_gem_sbus.c,v 1.1 2006/11/24 13:23:32 martin Exp $	*/
3 
4 /*-
5  * Copyright (c) 2006 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Martin Husemann.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * SBus front-end for the GEM network driver
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/syslog.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 
44 #include <net/if.h>
45 #include <net/if_media.h>
46 
47 #include <netinet/in.h>
48 #include <netinet/if_ether.h>
49 
50 #include <dev/mii/mii.h>
51 #include <dev/mii/miivar.h>
52 
53 #include <machine/bus.h>
54 #include <machine/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 #include <dev/ofw/openfirm.h>
63 
64 struct gem_sbus_softc {
65 	struct	gem_softc	gsc_gem;	/* GEM device */
66 };
67 
68 int	gemmatch_sbus(struct device *, void *, void *);
69 void	gemattach_sbus(struct device *, struct device *, void *);
70 
71 struct cfattach gem_sbus_ca = {
72 	sizeof(struct gem_sbus_softc), gemmatch_sbus, gemattach_sbus
73 };
74 
75 int
76 gemmatch_sbus(struct device *parent, void *vcf, void *aux)
77 {
78 	struct sbus_attach_args *sa = aux;
79 
80 	return (strcmp("network", sa->sa_name) == 0);
81 }
82 
83 void
84 gemattach_sbus(struct device *parent, struct device *self, void *aux)
85 {
86 	struct sbus_attach_args *sa = aux;
87 	struct gem_sbus_softc *gsc = (void *)self;
88 	struct gem_softc *sc = &gsc->gsc_gem;
89 	/* XXX the following declaration should be elsewhere */
90 	extern void myetheraddr(u_char *);
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_nintr < 1) {
97 		printf(": no interrupt\n");
98 		return;
99 	}
100 
101 	if (sa->sa_nreg < 2) {
102 		printf(": only %d register sets\n", sa->sa_nreg);
103 		return;
104 	}
105 
106 	sc->sc_variant = GEM_SUN_GEM;
107 
108 	/*
109 	 * Map two register banks:
110 	 *
111 	 *	bank 0: status, config, reset
112 	 *	bank 1: various gem parts
113 	 *
114 	 */
115 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
116 			 (bus_addr_t)sa->sa_reg[0].sbr_offset,
117 			 (bus_size_t)sa->sa_reg[0].sbr_size, 0, 0,
118 			 &sc->sc_h2) != 0) {
119 		printf(": can't map registers\n");
120 		return;
121 	}
122 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
123 			 (bus_addr_t)sa->sa_reg[1].sbr_offset,
124 			 (bus_size_t)sa->sa_reg[1].sbr_size, 0, 0,
125 			 &sc->sc_h1) != 0) {
126 		printf(": can't map registers\n");
127 		return;
128 	}
129 
130 	if (OF_getprop(sa->sa_node, "local-mac-address",
131 	    sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN) <= 0)
132 		myetheraddr(sc->sc_arpcom.ac_enaddr);
133 
134 	/*
135 	 * SBUS config
136 	 */
137 	(void) bus_space_read_4(sa->sa_bustag, sc->sc_h2, GEM_SBUS_RESET);
138 	delay(100);
139 	bus_space_write_4(sa->sa_bustag, sc->sc_h2, GEM_SBUS_CONFIG,
140 	    GEM_SBUS_CFG_BSIZE128|GEM_SBUS_CFG_PARITY|GEM_SBUS_CFG_BMODE64);
141 
142 	/* Establish interrupt handler */
143 	sc->sc_ih = bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET, 0,
144 	    gem_intr, sc, self->dv_xname);
145 
146 	gem_config(sc);
147 }
148