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