xref: /netbsd-src/sys/arch/sun3/dev/if_ie_sebuf.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: if_ie_sebuf.c,v 1.6 1998/10/01 20:05:10 thorpej 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 #include "opt_inet.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <net/if.h>
51 #include <net/if_ether.h>
52 
53 #ifdef INET
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/in_var.h>
57 #include <netinet/ip.h>
58 #include <netinet/if_inarp.h>
59 #endif
60 
61 #include <machine/autoconf.h>
62 #include <machine/cpu.h>
63 #include <machine/dvma.h>
64 #include <machine/idprom.h>
65 #include <machine/vmparam.h>
66 
67 #include "i82586.h"
68 #include "if_iereg.h"
69 #include "if_ievar.h"
70 #include "sereg.h"
71 #include "sevar.h"
72 
73 static void ie_sebuf_reset __P((struct ie_softc *));
74 static void ie_sebuf_attend __P((struct ie_softc *));
75 static void ie_sebuf_run __P((struct ie_softc *));
76 
77 /*
78  * zero/copy functions: OBIO can use the normal functions, but VME
79  *    must do only byte or half-word (16 bit) accesses...
80  */
81 static void *wmemcpy __P((void *dst, const void *src, size_t size));
82 static void *wmemset __P((void *dst, int val, size_t size));
83 
84 
85 /*
86  * New-style autoconfig attachment
87  */
88 
89 static int  ie_sebuf_match __P((struct device *, struct cfdata *, void *));
90 static void ie_sebuf_attach __P((struct device *, struct device *, void *));
91 
92 struct cfattach ie_sebuf_ca = {
93 	sizeof(struct ie_softc), ie_sebuf_match, ie_sebuf_attach
94 };
95 
96 
97 static int
98 ie_sebuf_match(parent, cf, args)
99 	struct device *parent;
100 	struct cfdata *cf;
101 	void *args;
102 {
103 	struct sebuf_attach_args *aa = args;
104 
105 	/* Match by name. */
106 	if (strcmp(aa->name, "ie"))
107 		return (0);
108 
109 	/* Anyting else to check? */
110 
111 	return (1);
112 }
113 
114 static void
115 ie_sebuf_attach(parent, self, args)
116 	struct device *parent;
117 	struct device *self;
118 	void *args;
119 {
120 	struct ie_softc *sc = (void *) self;
121 	struct sebuf_attach_args *aa = args;
122 	volatile struct ie_regs *regs;
123 	int     off;
124 
125 	sc->hard_type = IE_VME3E;
126 	sc->reset_586 = ie_sebuf_reset;
127 	sc->chan_attn = ie_sebuf_attend;
128 	sc->run_586   = ie_sebuf_run;
129 	sc->sc_memcpy = wmemcpy;
130 	sc->sc_memset = wmemset;
131 
132 	/* Control regs mapped by parent. */
133 	sc->sc_reg = aa->regs;
134 	regs = (struct ie_regs *) sc->sc_reg;
135 
136 	/*
137 	 * On this hardware, the i82586 address zero
138 	 * maps to the start of the board, which we
139 	 * happen to know is 128K below (XXX).
140 	 */
141 	sc->sc_iobase = aa->buf - 0x20000;
142 
143 	/*
144 	 * There is 128K of memory for the i82586 on
145 	 * the Sun3/E SCSI/Ethernet board, and the
146 	 * "sebuf" driver has mapped it in for us.
147 	 * (Fixed in hardware; NOT configurable!)
148 	 */
149 	if (aa->blen < SE_IEBUFSIZE)
150 		panic("ie_sebuf: bad size");
151 	sc->sc_msize = SE_IEBUFSIZE;
152 	sc->sc_maddr = aa->buf;
153 
154 	/* Clear the memory. */
155 	(sc->sc_memset)(sc->sc_maddr, 0, sc->sc_msize);
156 
157 	/*
158 	 * XXX:  Unfortunately, the common driver code
159 	 * is not ready to deal with more than 64K of
160 	 * memory, so skip the first 64K.  Too bad.
161 	 * Note: device needs the SCP at the end.
162 	 */
163 	sc->sc_msize -= 0x10000;
164 	sc->sc_maddr += 0x10000;
165 
166 	/*
167 	 * Set the System Configuration Pointer (SCP).
168 	 * Its location is system-dependent because the
169 	 * i82586 reads it from a fixed physical address.
170 	 * On this hardware, the i82586 address is just
171 	 * masked down to 17 bits, so the SCP is found
172 	 * at the end of the RAM on the VME board.
173 	 */
174 	off = IE_SCP_ADDR & 0xFFFF;
175 	sc->scp = (volatile void *) (sc->sc_maddr + off);
176 
177 	/*
178 	 * The rest of ram is used for buffers, etc.
179 	 */
180 	sc->buf_area = sc->sc_maddr;
181 	sc->buf_area_sz = off;
182 
183 	/* Install interrupt handler. */
184 	regs->ie_ivec = aa->ca.ca_intvec;
185 	isr_add_vectored(ie_intr, (void *)sc,
186 		aa->ca.ca_intpri, aa->ca.ca_intvec);
187 
188 	/* Set the ethernet address. */
189 	idprom_etheraddr(sc->sc_addr);
190 
191 	/* Do machine-independent parts of attach. */
192 	ie_attach(sc);
193 
194 }
195 
196 /* Whack the "channel attetion" line. */
197 void
198 ie_sebuf_attend(sc)
199 	struct ie_softc *sc;
200 {
201 	volatile struct ie_regs *regs = (struct ie_regs *) sc->sc_reg;
202 
203 	regs->ie_csr |= IE_CSR_ATTEN;	/* flag! */
204 	regs->ie_csr &= ~IE_CSR_ATTEN;	/* down. */
205 }
206 
207 /*
208  * This is called during driver attach.
209  * Reset and initialize.
210  */
211 void
212 ie_sebuf_reset(sc)
213 	struct ie_softc *sc;
214 {
215 	volatile struct ie_regs *regs = (struct ie_regs *) sc->sc_reg;
216 	regs->ie_csr = IE_CSR_RESET;
217 	delay(20);
218 	regs->ie_csr = (IE_CSR_NOLOOP | IE_CSR_IENAB);
219 }
220 
221 /*
222  * This is called at the end of ieinit().
223  * optional.
224  */
225 void
226 ie_sebuf_run(sc)
227 	struct ie_softc *sc;
228 {
229 	/* do it all in reset */
230 }
231 
232 /*
233  * wmemcpy/wmemset - like memcpy/memset but largest access is 16-bits,
234  * and also does byte swaps...
235  * XXX - Would be nice to have asm versions in some library...
236  */
237 
238 static void *
239 wmemset(vb, val, l)
240 	void *vb;
241 	int val;
242 	size_t l;
243 {
244 	u_char *b = vb;
245 	u_char *be = b + l;
246 	u_short *sp;
247 
248 	if (l == 0)
249 		return (vb);
250 
251 	/* front, */
252 	if ((u_long)b & 1)
253 		*b++ = val;
254 
255 	/* back, */
256 	if (b != be && ((u_long)be & 1) != 0) {
257 		be--;
258 		*be = val;
259 	}
260 
261 	/* and middle. */
262 	sp = (u_short *)b;
263 	while (sp != (u_short *)be)
264 		*sp++ = val;
265 
266 	return (vb);
267 }
268 
269 static void *
270 wmemcpy(dst, src, l)
271 	void *dst;
272 	const void *src;
273 	size_t l;
274 {
275 	const u_char *b1e, *b1 = src;
276 	u_char *b2 = dst;
277 	u_short *sp;
278 	int bstore = 0;
279 
280 	if (l == 0)
281 		return (dst);
282 
283 	/* front, */
284 	if ((u_long)b1 & 1) {
285 		*b2++ = *b1++;
286 		l--;
287 	}
288 
289 	/* middle, */
290 	sp = (u_short *)b1;
291 	b1e = b1 + l;
292 	if (l & 1)
293 		b1e--;
294 	bstore = (u_long)b2 & 1;
295 
296 	while (sp < (u_short *)b1e) {
297 		if (bstore) {
298 			b2[1] = *sp & 0xff;
299 			b2[0] = *sp >> 8;
300 		} else
301 			*((short *)b2) = *sp;
302 		sp++;
303 		b2 += 2;
304 	}
305 
306 	/* and back. */
307 	if (l & 1)
308 		*b2 = *b1e;
309 
310 	return (dst);
311 }
312