xref: /netbsd-src/sys/arch/sun3/dev/if_ie_sebuf.c (revision c41a4eebefede43f6950f838a387dc18c6a431bf)
1 /*	$NetBSD: if_ie_sebuf.c,v 1.4 1997/12/08 19:22:52 gwr Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Machine-dependent glue for the Intel Ethernet (ie) driver,
41  * as found on the Sun3/E SCSI/Ethernet board.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <net/if.h>
50 #include <net/if_ether.h>
51 
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip.h>
57 #include <netinet/if_inarp.h>
58 #endif
59 
60 #include <machine/autoconf.h>
61 #include <machine/cpu.h>
62 #include <machine/dvma.h>
63 #include <machine/idprom.h>
64 #include <machine/vmparam.h>
65 
66 #include "i82586.h"
67 #include "if_iereg.h"
68 #include "if_ievar.h"
69 #include "sereg.h"
70 #include "sevar.h"
71 
72 static void ie_sebuf_reset __P((struct ie_softc *));
73 static void ie_sebuf_attend __P((struct ie_softc *));
74 static void ie_sebuf_run __P((struct ie_softc *));
75 
76 /*
77  * zero/copy functions: OBIO can use the normal functions, but VME
78  *    must do only byte or half-word (16 bit) accesses...
79  */
80 static void wcopy __P((const void *vb1, void *vb2, u_int l));
81 static void wzero __P((void *vb, u_int l));
82 
83 
84 /*
85  * New-style autoconfig attachment
86  */
87 
88 static int  ie_sebuf_match __P((struct device *, struct cfdata *, void *));
89 static void ie_sebuf_attach __P((struct device *, struct device *, void *));
90 
91 struct cfattach ie_sebuf_ca = {
92 	sizeof(struct ie_softc), ie_sebuf_match, ie_sebuf_attach
93 };
94 
95 
96 static int
97 ie_sebuf_match(parent, cf, args)
98 	struct device *parent;
99 	struct cfdata *cf;
100 	void *args;
101 {
102 	struct sebuf_attach_args *aa = args;
103 
104 	/* Match by name. */
105 	if (strcmp(aa->name, "ie"))
106 		return (0);
107 
108 	/* Anyting else to check? */
109 
110 	return (1);
111 }
112 
113 static void
114 ie_sebuf_attach(parent, self, args)
115 	struct device *parent;
116 	struct device *self;
117 	void *args;
118 {
119 	struct ie_softc *sc = (void *) self;
120 	struct sebuf_attach_args *aa = args;
121 	volatile struct ie_regs *regs;
122 	int     off;
123 
124 	sc->hard_type = IE_VME3E;
125 	sc->reset_586 = ie_sebuf_reset;
126 	sc->chan_attn = ie_sebuf_attend;
127 	sc->run_586   = ie_sebuf_run;
128 	sc->sc_bcopy = wcopy;
129 	sc->sc_bzero = wzero;
130 
131 	/* Control regs mapped by parent. */
132 	sc->sc_reg = aa->regs;
133 	regs = (struct ie_regs *) sc->sc_reg;
134 
135 	/*
136 	 * On this hardware, the i82586 address zero
137 	 * maps to the start of the board, which we
138 	 * happen to know is 128K below (XXX).
139 	 */
140 	sc->sc_iobase = aa->buf - 0x20000;
141 
142 	/*
143 	 * There is 128K of memory for the i82586 on
144 	 * the Sun3/E SCSI/Ethernet board, and the
145 	 * "sebuf" driver has mapped it in for us.
146 	 * (Fixed in hardware; NOT configurable!)
147 	 */
148 	if (aa->blen < SE_IEBUFSIZE)
149 		panic("ie_sebuf: bad size");
150 	sc->sc_msize = SE_IEBUFSIZE;
151 	sc->sc_maddr = aa->buf;
152 
153 	/* Clear the memory. */
154 	(sc->sc_bzero)(sc->sc_maddr, sc->sc_msize);
155 
156 	/*
157 	 * XXX:  Unfortunately, the common driver code
158 	 * is not ready to deal with more than 64K of
159 	 * memory, so skip the first 64K.  Too bad.
160 	 * Note: device needs the SCP at the end.
161 	 */
162 	sc->sc_msize -= 0x10000;
163 	sc->sc_maddr += 0x10000;
164 
165 	/*
166 	 * Set the System Configuration Pointer (SCP).
167 	 * Its location is system-dependent because the
168 	 * i82586 reads it from a fixed physical address.
169 	 * On this hardware, the i82586 address is just
170 	 * masked down to 17 bits, so the SCP is found
171 	 * at the end of the RAM on the VME board.
172 	 */
173 	off = IE_SCP_ADDR & 0xFFFF;
174 	sc->scp = (volatile void *) (sc->sc_maddr + off);
175 
176 	/*
177 	 * The rest of ram is used for buffers, etc.
178 	 */
179 	sc->buf_area = sc->sc_maddr;
180 	sc->buf_area_sz = off;
181 
182 	/* Install interrupt handler. */
183 	regs->ie_ivec = aa->ca.ca_intvec;
184 	isr_add_vectored(ie_intr, (void *)sc,
185 		aa->ca.ca_intpri, aa->ca.ca_intvec);
186 
187 	/* Set the ethernet address. */
188 	idprom_etheraddr(sc->sc_addr);
189 
190 	/* Do machine-independent parts of attach. */
191 	ie_attach(sc);
192 
193 }
194 
195 /* Whack the "channel attetion" line. */
196 void
197 ie_sebuf_attend(sc)
198 	struct ie_softc *sc;
199 {
200 	volatile struct ie_regs *regs = (struct ie_regs *) sc->sc_reg;
201 
202 	regs->ie_csr |= IE_CSR_ATTEN;	/* flag! */
203 	regs->ie_csr &= ~IE_CSR_ATTEN;	/* down. */
204 }
205 
206 /*
207  * This is called during driver attach.
208  * Reset and initialize.
209  */
210 void
211 ie_sebuf_reset(sc)
212 	struct ie_softc *sc;
213 {
214 	volatile struct ie_regs *regs = (struct ie_regs *) sc->sc_reg;
215 	regs->ie_csr = IE_CSR_RESET;
216 	delay(20);
217 	regs->ie_csr = (IE_CSR_NOLOOP | IE_CSR_IENAB);
218 }
219 
220 /*
221  * This is called at the end of ieinit().
222  * optional.
223  */
224 void
225 ie_sebuf_run(sc)
226 	struct ie_softc *sc;
227 {
228 	/* do it all in reset */
229 }
230 
231 /*
232  * wcopy/wzero - like bcopy/bzero but largest access is 16-bits,
233  * and also does byte swaps...
234  * XXX - Would be nice to have asm versions in some library...
235  */
236 
237 static void
238 wzero(vb, l)
239 	void *vb;
240 	u_int l;
241 {
242 	u_char *b = vb;
243 	u_char *be = b + l;
244 	u_short *sp;
245 
246 	if (l == 0)
247 		return;
248 
249 	/* front, */
250 	if ((u_long)b & 1)
251 		*b++ = 0;
252 
253 	/* back, */
254 	if (b != be && ((u_long)be & 1) != 0) {
255 		be--;
256 		*be = 0;
257 	}
258 
259 	/* and middle. */
260 	sp = (u_short *)b;
261 	while (sp != (u_short *)be)
262 		*sp++ = 0;
263 }
264 
265 static void
266 wcopy(vb1, vb2, l)
267 	const void *vb1;
268 	void *vb2;
269 	u_int l;
270 {
271 	const u_char *b1e, *b1 = vb1;
272 	u_char *b2 = vb2;
273 	u_short *sp;
274 	int bstore = 0;
275 
276 	if (l == 0)
277 		return;
278 
279 	/* front, */
280 	if ((u_long)b1 & 1) {
281 		*b2++ = *b1++;
282 		l--;
283 	}
284 
285 	/* middle, */
286 	sp = (u_short *)b1;
287 	b1e = b1 + l;
288 	if (l & 1)
289 		b1e--;
290 	bstore = (u_long)b2 & 1;
291 
292 	while (sp < (u_short *)b1e) {
293 		if (bstore) {
294 			b2[1] = *sp & 0xff;
295 			b2[0] = *sp >> 8;
296 		} else
297 			*((short *)b2) = *sp;
298 		sp++;
299 		b2 += 2;
300 	}
301 
302 	/* and back. */
303 	if (l & 1)
304 		*b2 = *b1e;
305 }
306