xref: /netbsd/sys/arch/sun3/dev/if_ie_vme.c (revision bf9ec67e)
1 /*	$NetBSD: if_ie_vme.c,v 1.13 1998/10/01 20:05:11 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  */
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 
71 static void ie_vmereset __P((struct ie_softc *));
72 static void ie_vmeattend __P((struct ie_softc *));
73 static void ie_vmerun __P((struct ie_softc *));
74 
75 /*
76  * zero/copy functions: OBIO can use the normal functions, but VME
77  *    must do only byte or half-word (16 bit) accesses...
78  */
79 static void *wmemcpy __P((void *dst, const void *src, size_t size));
80 static void *wmemset __P((void *dst, int val, size_t size));
81 
82 
83 /*
84  * New-style autoconfig attachment
85  */
86 
87 static int  ie_vme_match __P((struct device *, struct cfdata *, void *));
88 static void ie_vme_attach __P((struct device *, struct device *, void *));
89 
90 struct cfattach ie_vme_ca = {
91 	sizeof(struct ie_softc), ie_vme_match, ie_vme_attach
92 };
93 
94 
95 static int
96 ie_vme_match(parent, cf, args)
97 	struct device *parent;
98 	struct cfdata *cf;
99 	void *args;
100 {
101 	struct confargs *ca = args;
102 
103 	/* No default VME address. */
104 	if (ca->ca_paddr == -1)
105 		return(0);
106 
107 	/* Make sure something is there... */
108 	if (bus_peek(ca->ca_bustype, ca->ca_paddr, 2) == -1)
109 		return (0);
110 
111 	/* Default interrupt priority. */
112 	if (ca->ca_intpri == -1)
113 		ca->ca_intpri = 3;
114 
115 	return (1);
116 }
117 
118 /*
119  * *note*: we don't detect the difference between a VME3E and
120  * a multibus/vme card.   if you want to use a 3E you'll have
121  * to fix this.
122  */
123 void
124 ie_vme_attach(parent, self, args)
125 	struct device *parent;
126 	struct device *self;
127 	void *args;
128 {
129 	struct ie_softc *sc = (void *) self;
130 	struct confargs *ca = args;
131 	volatile struct ievme *iev;
132 	u_long  rampaddr;
133 	int     lcv, off;
134 
135 	sc->hard_type = IE_VME;
136 	sc->reset_586 = ie_vmereset;
137 	sc->chan_attn = ie_vmeattend;
138 	sc->run_586 = ie_vmerun;
139 	sc->sc_memcpy = wmemcpy;
140 	sc->sc_memset = wmemset;
141 
142 	/*
143 	 * There is 64K of memory on the VME board.
144 	 * (determined by hardware - NOT configurable!)
145 	 */
146 	sc->sc_msize = 0x10000; /* MEMSIZE 64K */
147 
148 	/* Map in the board control regs. */
149 	sc->sc_reg = bus_mapin(ca->ca_bustype, ca->ca_paddr,
150 						   sizeof(struct ievme));
151 	iev = (volatile struct ievme *) sc->sc_reg;
152 
153 	/*
154 	 * Find and map in the board memory.
155 	 */
156 	/* top 12 bits */
157 	rampaddr = ca->ca_paddr & 0xfff00000;
158 	/* 4 more */
159 	rampaddr |= ((iev->status & IEVME_HADDR) << 16);
160 	sc->sc_maddr = bus_mapin(ca->ca_bustype, rampaddr, sc->sc_msize);
161 
162 	/*
163 	 * On this hardware, the i82586 address is just
164 	 * masked to 16 bits, so sc_iobase == sc_maddr
165 	 */
166 	sc->sc_iobase = sc->sc_maddr;
167 
168 	/*
169 	 * Set up on-board mapping registers for linear map.
170 	 */
171 	iev->pectrl |= IEVME_PARACK; /* clear to start */
172 	for (lcv = 0; lcv < IEVME_MAPSZ; lcv++)
173 		iev->pgmap[lcv] = IEVME_SBORDR | IEVME_OBMEM | lcv;
174 	(sc->sc_memset)(sc->sc_maddr, 0, sc->sc_msize);
175 
176 	/*
177 	 * Set the System Configuration Pointer (SCP).
178 	 * Its location is system-dependent because the
179 	 * i82586 reads it from a fixed physical address.
180 	 * On this hardware, the i82586 address is just
181 	 * masked down to 16 bits, so the SCP is found
182 	 * at the end of the RAM on the VME board.
183 	 */
184 	off = IE_SCP_ADDR & 0xFFFF;
185 	sc->scp = (volatile void *) (sc->sc_maddr + off);
186 
187 	/*
188 	 * The rest of ram is used for buffers, etc.
189 	 */
190 	sc->buf_area = sc->sc_maddr;
191 	sc->buf_area_sz = off;
192 
193 	/* Set the ethernet address. */
194 	idprom_etheraddr(sc->sc_addr);
195 
196 	/* Do machine-independent parts of attach. */
197 	ie_attach(sc);
198 
199 	/* Install interrupt handler. */
200 	isr_add_vectored(ie_intr, (void *)sc,
201 		ca->ca_intpri, ca->ca_intvec);
202 }
203 
204 
205 /*
206  * MULTIBUS/VME support
207  */
208 
209 void
210 ie_vmeattend(sc)
211 	struct ie_softc *sc;
212 {
213 	volatile struct ievme *iev = (struct ievme *) sc->sc_reg;
214 
215 	iev->status |= IEVME_ATTEN;	/* flag! */
216 	iev->status &= ~IEVME_ATTEN;	/* down. */
217 }
218 
219 void
220 ie_vmereset(sc)
221 	struct ie_softc *sc;
222 {
223 	volatile struct ievme *iev = (struct ievme *) sc->sc_reg;
224 
225 	iev->status = IEVME_RESET;
226 	delay(20);
227 	iev->status = (IEVME_ONAIR | IEVME_IENAB | IEVME_PEINT);
228 }
229 
230 void
231 ie_vmerun(sc)
232 	struct ie_softc *sc;
233 {
234 	/* do it all in reset */
235 }
236 
237 /*
238  * wmemcpy/wmemset - like memcpy/memset but largest access is 16-bits,
239  * and also does byte swaps...
240  * XXX - Would be nice to have asm versions in some library...
241  */
242 
243 static void *
244 wmemset(vb, val, l)
245 	void *vb;
246 	int val;
247 	size_t l;
248 {
249 	u_char *b = vb;
250 	u_char *be = b + l;
251 	u_short *sp;
252 
253 	if (l == 0)
254 		return (vb);
255 
256 	/* front, */
257 	if ((u_long)b & 1)
258 		*b++ = val;
259 
260 	/* back, */
261 	if (b != be && ((u_long)be & 1) != 0) {
262 		be--;
263 		*be = val;
264 	}
265 
266 	/* and middle. */
267 	sp = (u_short *)b;
268 	while (sp != (u_short *)be)
269 		*sp++ = val;
270 
271 	return (vb);
272 }
273 
274 static void *
275 wmemcpy(dst, src, l)
276 	void *dst;
277 	const void *src;
278 	size_t l;
279 {
280 	const u_char *b1e, *b1 = src;
281 	u_char *b2 = dst;
282 	u_short *sp;
283 	int bstore = 0;
284 
285 	if (l == 0)
286 		return (dst);
287 
288 	/* front, */
289 	if ((u_long)b1 & 1) {
290 		*b2++ = *b1++;
291 		l--;
292 	}
293 
294 	/* middle, */
295 	sp = (u_short *)b1;
296 	b1e = b1 + l;
297 	if (l & 1)
298 		b1e--;
299 	bstore = (u_long)b2 & 1;
300 
301 	while (sp < (u_short *)b1e) {
302 		if (bstore) {
303 			b2[1] = *sp & 0xff;
304 			b2[0] = *sp >> 8;
305 		} else
306 			*((short *)b2) = *sp;
307 		sp++;
308 		b2 += 2;
309 	}
310 
311 	/* and back. */
312 	if (l & 1)
313 		*b2 = *b1e;
314 
315 	return (dst);
316 }
317