1 /*	$OpenBSD: if_hme_sbus.c,v 1.7 2003/07/07 15:37:07 jason Exp $	*/
2 /*	$NetBSD: if_hme_sbus.c,v 1.6 2001/02/28 14:52:48 mrg Exp $	*/
3 
4 /*-
5  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Paul Kranenburg.
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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * SBus front-end device driver for the HME ethernet device.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/syslog.h>
47 #include <sys/device.h>
48 #include <sys/malloc.h>
49 #include <sys/socket.h>
50 
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 
55 #ifdef INET
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/if_ether.h>
61 #endif
62 
63 #include <dev/mii/mii.h>
64 #include <dev/mii/miivar.h>
65 
66 #include <machine/bus.h>
67 #include <machine/intr.h>
68 #include <machine/autoconf.h>
69 
70 #include <dev/sbus/sbusvar.h>
71 #include <dev/ic/hmevar.h>
72 #include <dev/ofw/openfirm.h>
73 
74 struct hmesbus_softc {
75 	struct	hme_softc	hsc_hme;	/* HME device */
76 	struct	sbusdev		hsc_sbus;	/* SBus device */
77 };
78 
79 int	hmematch_sbus(struct device *, void *, void *);
80 void	hmeattach_sbus(struct device *, struct device *, void *);
81 
82 struct cfattach hme_sbus_ca = {
83 	sizeof(struct hmesbus_softc), hmematch_sbus, hmeattach_sbus
84 };
85 
86 int
hmematch_sbus(struct device * parent,void * vcf,void * aux)87 hmematch_sbus(struct device *parent, void *vcf, void *aux)
88 {
89 	struct cfdata *cf = vcf;
90 	struct sbus_attach_args *sa = aux;
91 
92 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
93 	    strcmp("SUNW,qfe", sa->sa_name) == 0 ||
94 	    strcmp("SUNW,hme", sa->sa_name) == 0);
95 }
96 
97 void
hmeattach_sbus(struct device * parent,struct device * self,void * aux)98 hmeattach_sbus(struct device *parent, struct device *self, void *aux)
99 {
100 	struct sbus_attach_args *sa = aux;
101 	struct hmesbus_softc *hsc = (void *)self;
102 	struct hme_softc *sc = &hsc->hsc_hme;
103 	struct sbusdev *sd = &hsc->hsc_sbus;
104 	u_int32_t burst, sbusburst;
105 	int node;
106 	/* XXX the following declarations should be elsewhere */
107 	extern void myetheraddr(u_char *);
108 
109 	node = sa->sa_node;
110 
111 	/* Pass on the bus tags */
112 	sc->sc_bustag = sa->sa_bustag;
113 	sc->sc_dmatag = sa->sa_dmatag;
114 
115 	if (sa->sa_nreg < 5) {
116 		printf("%s: only %d register sets\n",
117 			self->dv_xname, sa->sa_nreg);
118 		return;
119 	}
120 
121 	/*
122 	 * Map five register banks:
123 	 *
124 	 *	bank 0: HME SEB registers
125 	 *	bank 1: HME ETX registers
126 	 *	bank 2: HME ERX registers
127 	 *	bank 3: HME MAC registers
128 	 *	bank 4: HME MIF registers
129 	 *
130 	 */
131 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
132 	    (bus_addr_t)sa->sa_reg[0].sbr_offset,
133 	    (bus_size_t)sa->sa_reg[0].sbr_size, 0, 0, &sc->sc_seb) != 0) {
134 		printf("%s @ sbus: cannot map registers\n", self->dv_xname);
135 		return;
136 	}
137 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[1].sbr_slot,
138 	    (bus_addr_t)sa->sa_reg[1].sbr_offset,
139 	    (bus_size_t)sa->sa_reg[1].sbr_size, 0, 0, &sc->sc_etx) != 0) {
140 		printf("%s @ sbus: cannot map registers\n", self->dv_xname);
141 		return;
142 	}
143 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[2].sbr_slot,
144 	    (bus_addr_t)sa->sa_reg[2].sbr_offset,
145 	    (bus_size_t)sa->sa_reg[2].sbr_size, 0, 0, &sc->sc_erx) != 0) {
146 		printf("%s @ sbus: cannot map registers\n", self->dv_xname);
147 		return;
148 	}
149 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[3].sbr_slot,
150 	    (bus_addr_t)sa->sa_reg[3].sbr_offset,
151 	    (bus_size_t)sa->sa_reg[3].sbr_size, 0, 0, &sc->sc_mac) != 0) {
152 		printf("%s @ sbus: cannot map registers\n", self->dv_xname);
153 		return;
154 	}
155 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[4].sbr_slot,
156 	    (bus_addr_t)sa->sa_reg[4].sbr_offset,
157 	    (bus_size_t)sa->sa_reg[4].sbr_size, 0, 0, &sc->sc_mif) != 0) {
158 		printf("%s @ sbus: cannot map registers\n", self->dv_xname);
159 		return;
160 	}
161 
162 	sd->sd_reset = (void *)hme_reset;
163 	sbus_establish(sd, self);
164 
165 	if (OF_getprop(sa->sa_node, "local-mac-address",
166 	    sc->sc_enaddr, ETHER_ADDR_LEN) <= 0)
167 		myetheraddr(sc->sc_enaddr);
168 
169 	/*
170 	 * Get transfer burst size from PROM and pass it on
171 	 * to the back-end driver.
172 	 */
173 	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
174 	if (sbusburst == 0)
175 		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
176 
177 	burst = getpropint(node, "burst-sizes", -1);
178 	if (burst == -1)
179 		/* take SBus burst sizes */
180 		burst = sbusburst;
181 
182 	/* Clamp at parent's burst sizes */
183 	burst &= sbusburst;
184 
185 	/* Translate into plain numerical format */
186 	sc->sc_burst =  (burst & SBUS_BURST_32) ? 32 :
187 			(burst & SBUS_BURST_16) ? 16 : 0;
188 
189 	sc->sc_pci = 0; /* XXXXX should all be done in bus_dma. */
190 	hme_config(sc);
191 
192 	/* Establish interrupt handler */
193 	if (sa->sa_nintr != 0)
194 		(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET, 0,
195 					 hme_intr, sc, self->dv_xname);
196 }
197