xref: /openbsd-src/sys/dev/sbus/if_gem_sbus.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: if_gem_sbus.c,v 1.6 2009/07/12 21:27:09 kettenis 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_dl.h>
46 #include <net/if_media.h>
47 
48 #ifdef INET
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/in_var.h>
52 #include <netinet/ip.h>
53 #include <netinet/if_ether.h>
54 #endif
55 
56 #include <dev/mii/mii.h>
57 #include <dev/mii/miivar.h>
58 
59 #include <machine/bus.h>
60 #include <machine/intr.h>
61 #include <machine/autoconf.h>
62 
63 #include <dev/sbus/sbusvar.h>
64 
65 #include <dev/ic/gemreg.h>
66 #include <dev/ic/gemvar.h>
67 
68 #include <dev/ofw/openfirm.h>
69 
70 struct gem_sbus_softc {
71 	struct	gem_softc	gsc_gem;	/* GEM device */
72 };
73 
74 int	gemmatch_sbus(struct device *, void *, void *);
75 void	gemattach_sbus(struct device *, struct device *, void *);
76 
77 struct cfattach gem_sbus_ca = {
78 	sizeof(struct gem_sbus_softc), gemmatch_sbus, gemattach_sbus
79 };
80 
81 int
82 gemmatch_sbus(struct device *parent, void *vcf, void *aux)
83 {
84 	struct sbus_attach_args *sa = aux;
85 
86 	return (strcmp("network", sa->sa_name) == 0);
87 }
88 
89 void
90 gemattach_sbus(struct device *parent, struct device *self, void *aux)
91 {
92 	struct sbus_attach_args *sa = aux;
93 	struct gem_sbus_softc *gsc = (void *)self;
94 	struct gem_softc *sc = &gsc->gsc_gem;
95 	/* XXX the following declaration should be elsewhere */
96 	extern void myetheraddr(u_char *);
97 
98 	/* Pass on the bus tags */
99 	sc->sc_bustag = sa->sa_bustag;
100 	sc->sc_dmatag = sa->sa_dmatag;
101 
102 	if (sa->sa_nintr < 1) {
103 		printf(": no interrupt\n");
104 		return;
105 	}
106 
107 	if (sa->sa_nreg < 2) {
108 		printf(": only %d register sets\n", sa->sa_nreg);
109 		return;
110 	}
111 
112 	sc->sc_variant = GEM_SUN_GEM;
113 
114 	/*
115 	 * Map two register banks:
116 	 *
117 	 *	bank 0: status, config, reset
118 	 *	bank 1: various gem parts
119 	 *
120 	 */
121 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
122 			 (bus_addr_t)sa->sa_reg[0].sbr_offset,
123 			 (bus_size_t)sa->sa_reg[0].sbr_size, 0, 0,
124 			 &sc->sc_h2) != 0) {
125 		printf(": can't map registers\n");
126 		return;
127 	}
128 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
129 			 (bus_addr_t)sa->sa_reg[1].sbr_offset,
130 			 (bus_size_t)sa->sa_reg[1].sbr_size, 0, 0,
131 			 &sc->sc_h1) != 0) {
132 		printf(": can't map registers\n");
133 		return;
134 	}
135 
136 	if (OF_getprop(sa->sa_node, "local-mac-address",
137 	    sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN) <= 0)
138 		myetheraddr(sc->sc_arpcom.ac_enaddr);
139 
140 	/*
141 	 * SBUS config
142 	 */
143 	(void) bus_space_read_4(sa->sa_bustag, sc->sc_h2, GEM_SBUS_RESET);
144 	delay(100);
145 	bus_space_write_4(sa->sa_bustag, sc->sc_h2, GEM_SBUS_CONFIG,
146 	    GEM_SBUS_CFG_BSIZE128|GEM_SBUS_CFG_PARITY|GEM_SBUS_CFG_BMODE64);
147 
148 	/* Establish interrupt handler */
149 	bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET, 0, gem_intr,
150 	    sc, self->dv_xname);
151 
152 	gem_config(sc);
153 }
154