if_le_vme.c revision 1.6 1 /* $NetBSD: if_le_vme.c,v 1.6 1998/07/21 17:36:02 drochner Exp $ */
2
3 /*-
4 * Copyright (c) 1997 Leo Weppelman. All rights reserved.
5 * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Ralph Campbell and Rick Macklem.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
41 */
42
43 #include "opt_inet.h"
44 #include "bpfilter.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/syslog.h>
50 #include <sys/socket.h>
51 #include <sys/device.h>
52
53 #include <net/if.h>
54 #include <net/if_media.h>
55 #include <net/if_ether.h>
56
57 #ifdef INET
58 #include <netinet/in.h>
59 #include <netinet/if_inarp.h>
60 #endif
61
62 #include <machine/cpu.h>
63 #include <machine/bus.h>
64 #include <machine/iomap.h>
65 #include <machine/scu.h>
66
67 #include <atari/atari/device.h>
68 #include <atari/atari/intr.h>
69
70 #include <dev/ic/lancereg.h>
71 #include <dev/ic/lancevar.h>
72 #include <dev/ic/am7990reg.h>
73 #include <dev/ic/am7990var.h>
74
75 #include <atari/vme/vmevar.h>
76 #include <atari/vme/if_levar.h>
77
78 struct le_addresses {
79 u_long reg_addr;
80 u_long mem_addr;
81 int irq;
82 } lestd[] = {
83 { 0xfe00fff0, 0xfe010000, IRQUNK }, /* Riebl VME */
84 { 0xffcffff0, 0xffcf0000, 5 }, /* PAM VME */
85 { 0xfecffff0, 0xfecf0000, 5 } /* Rhotron VME */
86 };
87
88 #define NLESTD (sizeof(lestd) / sizeof(lestd[0]))
89
90 /*
91 * All cards have 64KB RAM. However.... On the Riebl cards the area
92 * between the offsets 0xee70-0xeec0 is used to store config data.
93 */
94 #define MEMSIZE (64*1024)
95
96 /*
97 * Default mac for RIEBL cards without a (working) battery. The first 4 bytes
98 * are the manufacturer id.
99 */
100 static u_char riebl_def_mac[] = {
101 0x00, 0x00, 0x36, 0x04, 0x00, 0x00
102 };
103
104 static int le_intr __P((struct le_softc *, int));
105 static void lepseudointr __P((struct le_softc *, void *));
106 static int le_vme_match __P((struct device *, struct cfdata *, void *));
107 static void le_vme_attach __P((struct device *, struct device *, void *));
108 static int probe_addresses __P((bus_space_tag_t *, bus_space_tag_t *,
109 bus_space_handle_t *, bus_space_handle_t *));
110 static void riebl_skip_reserved_area __P((struct lance_softc *));
111
112 struct cfattach le_vme_ca = {
113 sizeof(struct le_softc), le_vme_match, le_vme_attach
114 };
115
116 #if defined(_KERNEL) && !defined(_LKM)
117 #include "opt_ddb.h"
118 #endif
119
120 #ifdef DDB
121 #define integrate
122 #define hide
123 #else
124 #define integrate static __inline
125 #define hide static
126 #endif
127
128 hide void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
129 hide u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
130
131 hide void
132 lewrcsr(sc, port, val)
133 struct lance_softc *sc;
134 u_int16_t port, val;
135 {
136 struct le_softc *lesc = (struct le_softc *)sc;
137 int s;
138
139 s = splhigh();
140 bus_space_write_2(lesc->sc_iot, lesc->sc_ioh, LER_RAP, port);
141 bus_space_write_2(lesc->sc_iot, lesc->sc_ioh, LER_RDP, val);
142 splx(s);
143 }
144
145 hide u_int16_t
146 lerdcsr(sc, port)
147 struct lance_softc *sc;
148 u_int16_t port;
149 {
150 struct le_softc *lesc = (struct le_softc *)sc;
151 u_int16_t val;
152 int s;
153
154 s = splhigh();
155 bus_space_write_2(lesc->sc_iot, lesc->sc_ioh, LER_RAP, port);
156 val = bus_space_read_2(lesc->sc_iot, lesc->sc_ioh, LER_RDP);
157 splx(s);
158
159 return (val);
160 }
161
162 static int
163 le_vme_match(parent, cfp, aux)
164 struct device *parent;
165 struct cfdata *cfp;
166 void *aux;
167 {
168 struct vme_attach_args *va = aux;
169 int i;
170 bus_space_tag_t iot;
171 bus_space_tag_t memt;
172 bus_space_handle_t ioh;
173 bus_space_handle_t memh;
174
175 iot = va->va_iot;
176 memt = va->va_memt;
177
178 for (i = 0; i < NLESTD; i++) {
179 struct le_addresses *le_ap = &lestd[i];
180 int found = 0;
181
182 if ((va->va_iobase != IOBASEUNK)
183 && (va->va_iobase != le_ap->reg_addr))
184 continue;
185
186 if ((va->va_maddr != MADDRUNK)
187 && (va->va_maddr != le_ap->mem_addr))
188 continue;
189
190 if ((le_ap->irq != IRQUNK) && (va->va_irq != le_ap->irq))
191 continue;
192
193 if (bus_space_map(iot, le_ap->reg_addr, 16, 0, &ioh)) {
194 printf("leprobe: cannot map io-area\n");
195 return (0);
196 }
197 if (bus_space_map(memt, le_ap->mem_addr, MEMSIZE, 0, &memh)) {
198 bus_space_unmap(iot, (caddr_t)le_ap->reg_addr, 16);
199 printf("leprobe: cannot map memory-area\n");
200 return (0);
201 }
202 found = probe_addresses(&iot, &memt, &ioh, &memh);
203 bus_space_unmap(iot, (caddr_t)le_ap->reg_addr, 16);
204 bus_space_unmap(memt, (caddr_t)le_ap->mem_addr, 8*NBPG);
205
206 if (found) {
207 va->va_iobase = le_ap->reg_addr;
208 va->va_iosize = 16;
209 va->va_maddr = le_ap->mem_addr;
210 va->va_msize = MEMSIZE;
211 if (va->va_irq == IRQUNK)
212 va->va_irq = le_ap->irq;
213 return 1;
214 }
215 }
216 return (0);
217 }
218
219 static int
220 probe_addresses(iot, memt, ioh, memh)
221 bus_space_tag_t *iot;
222 bus_space_tag_t *memt;
223 bus_space_handle_t *ioh;
224 bus_space_handle_t *memh;
225 {
226 /*
227 * Test accesibility of register and memory area
228 */
229 if(!bus_space_peek_2(*iot, *ioh, LER_RDP))
230 return 0;
231 if(!bus_space_peek_1(*memt, *memh, 0))
232 return 0;
233
234 /*
235 * Test for writable memory
236 */
237 bus_space_write_2(*memt, *memh, 0, 0xa5a5);
238 if (bus_space_read_2(*memt, *memh, 0) != 0xa5a5)
239 return 0;
240
241 /*
242 * Test writability of selector port.
243 */
244 bus_space_write_2(*iot, *ioh, LER_RAP, LE_CSR1);
245 if (bus_space_read_2(*iot, *ioh, LER_RAP) != LE_CSR1)
246 return 0;
247
248 /*
249 * Do a small register test
250 */
251 bus_space_write_2(*iot, *ioh, LER_RAP, LE_CSR0);
252 bus_space_write_2(*iot, *ioh, LER_RDP, LE_C0_INIT | LE_C0_STOP);
253 if (bus_space_read_2(*iot, *ioh, LER_RDP) != LE_C0_STOP)
254 return 0;
255
256 bus_space_write_2(*iot, *ioh, LER_RDP, LE_C0_STOP);
257 if (bus_space_read_2(*iot, *ioh, LER_RDP) != LE_C0_STOP)
258 return 0;
259
260 return 1;
261 }
262
263 /*
264 * Interrupt mess. Because the card's interrupt is hardwired to either
265 * ipl5 or ipl3 (mostly on ipl5) and raising splnet to spl5() just won't do
266 * (it kills the serial at the least), we use a 2-level interrupt sceme. The
267 * card interrupt is routed to 'le_intr'. If the previous ipl was below
268 * splnet, just call the mi-function. If not, save the interrupt status,
269 * turn off card interrupts (the card is *very* persistent) and arrange
270 * for a softint 'callback' through 'lepseudointr'.
271 */
272 static int
273 le_intr(lesc, sr)
274 struct le_softc *lesc;
275 int sr;
276 {
277 struct lance_softc *sc = &lesc->sc_am7990.lsc;
278 u_int16_t csr0;
279
280 if ((sr & PSL_IPL) < IPL_NET)
281 am7990_intr(sc);
282 else {
283 sc->sc_saved_csr0 = csr0 = lerdcsr(sc, LE_CSR0);
284 lewrcsr(sc, LE_CSR0, csr0 & ~LE_C0_INEA);
285 add_sicallback((si_farg)lepseudointr, lesc, sc);
286 }
287 return 1;
288 }
289
290
291 static void
292 lepseudointr(lesc, sc)
293 struct le_softc *lesc;
294 void *sc;
295 {
296 int s;
297
298 s = splx(lesc->sc_splval);
299 am7990_intr(sc);
300 splx(s);
301 }
302
303 static void
304 le_vme_attach(parent, self, aux)
305 struct device *parent, *self;
306 void *aux;
307 {
308 struct le_softc *lesc = (struct le_softc *)self;
309 struct lance_softc *sc = &lesc->sc_am7990.lsc;
310 struct vme_attach_args *va = aux;
311 bus_space_handle_t ioh;
312 bus_space_handle_t memh;
313 int i;
314
315 printf("\n%s: ", sc->sc_dev.dv_xname);
316
317 if (bus_space_map(va->va_iot, va->va_iobase, va->va_iosize, 0, &ioh))
318 panic("leattach: cannot map io-area\n");
319 if (bus_space_map(va->va_memt, va->va_maddr, va->va_msize, 0, &memh))
320 panic("leattach: cannot map mem-area\n");
321
322 lesc->sc_iot = va->va_iot;
323 lesc->sc_ioh = ioh;
324 lesc->sc_memt = va->va_memt;
325 lesc->sc_memh = memh;
326 lesc->sc_splval = (va->va_irq << 8) | PSL_S; /* XXX */
327
328 /*
329 * Go on to find board type
330 */
331 if (bus_space_peek_1(va->va_iot, ioh, LER_EEPROM)) {
332 printf("PAM card");
333 lesc->sc_type = LE_PAM;
334 bus_space_read_1(va->va_iot, ioh, LER_MEME);
335 }
336 else {
337 printf("Riebl card");
338 if(bus_space_read_4(va->va_memt, memh, RIEBL_MAGIC_ADDR)
339 == RIEBL_MAGIC)
340 lesc->sc_type = LE_NEW_RIEBL;
341 else {
342 printf("(without battery) ");
343 lesc->sc_type = LE_OLD_RIEBL;
344 }
345 }
346
347 sc->sc_copytodesc = lance_copytobuf_contig;
348 sc->sc_copyfromdesc = lance_copyfrombuf_contig;
349 sc->sc_copytobuf = lance_copytobuf_contig;
350 sc->sc_copyfrombuf = lance_copyfrombuf_contig;
351 sc->sc_zerobuf = lance_zerobuf_contig;
352
353 sc->sc_rdcsr = lerdcsr;
354 sc->sc_wrcsr = lewrcsr;
355 sc->sc_hwinit = NULL;
356 sc->sc_conf3 = LE_C3_BSWP;
357 sc->sc_addr = 0;
358 sc->sc_memsize = va->va_msize;
359 sc->sc_mem = (void *)memh; /* XXX */
360
361 /*
362 * Get MAC address
363 */
364 switch (lesc->sc_type) {
365 case LE_OLD_RIEBL:
366 bcopy(riebl_def_mac, sc->sc_enaddr,
367 sizeof(sc->sc_enaddr));
368 break;
369 case LE_NEW_RIEBL:
370 for (i = 0; i < sizeof(sc->sc_enaddr); i++)
371 sc->sc_enaddr[i] =
372 bus_space_read_1(va->va_memt, memh, i + RIEBL_MAC_ADDR);
373 break;
374 case LE_PAM:
375 i = bus_space_read_1(va->va_iot, ioh, LER_EEPROM);
376 for (i = 0; i < sizeof(sc->sc_enaddr); i++) {
377 sc->sc_enaddr[i] =
378 (bus_space_read_2(va->va_memt, memh, 2 * i) << 4) |
379 (bus_space_read_2(va->va_memt, memh, 2 * i + 1) & 0xf);
380 }
381 i = bus_space_read_1(va->va_iot, ioh, LER_MEME);
382 break;
383 }
384
385 am7990_config(&lesc->sc_am7990);
386
387 if ((lesc->sc_type == LE_OLD_RIEBL) || (lesc->sc_type == LE_NEW_RIEBL))
388 riebl_skip_reserved_area(sc);
389
390 /*
391 * XXX: We always use uservector 64....
392 */
393 if ((lesc->sc_intr = intr_establish(64, USER_VEC, 0,
394 (hw_ifun_t)le_intr, lesc)) == NULL) {
395 printf("le_vme_attach: Can't establish interrupt\n");
396 return;
397 }
398
399 /*
400 * Notify the card of the vector
401 */
402 switch (lesc->sc_type) {
403 case LE_OLD_RIEBL:
404 case LE_NEW_RIEBL:
405 bus_space_write_2(va->va_memt, memh, RIEBL_IVEC_ADDR,
406 64 + 64);
407 break;
408 case LE_PAM:
409 bus_space_write_1(va->va_iot, ioh, LER_IVEC, 64 + 64);
410 break;
411 }
412
413 /*
414 * Unmask the VME-interrupt we're on
415 */
416 if (machineid & ATARI_TT)
417 SCU->vme_mask |= 1 << va->va_irq;
418 }
419
420 /*
421 * True if 'addr' containe within [start,len]
422 */
423 #define WITHIN(start, len, addr) \
424 ((addr >= start) && ((addr) <= ((start) + (len))))
425 static void
426 riebl_skip_reserved_area(sc)
427 struct lance_softc *sc;
428 {
429 int offset = 0;
430 int i;
431
432 for(i = 0; i < sc->sc_nrbuf; i++) {
433 if (WITHIN(sc->sc_rbufaddr[i], LEBLEN, RIEBL_RES_START)
434 || WITHIN(sc->sc_rbufaddr[i], LEBLEN, RIEBL_RES_END)) {
435 offset = RIEBL_RES_END - sc->sc_rbufaddr[i];
436 }
437 sc->sc_rbufaddr[i] += offset;
438 }
439
440 for(i = 0; i < sc->sc_ntbuf; i++) {
441 if (WITHIN(sc->sc_tbufaddr[i], LEBLEN, RIEBL_RES_START)
442 || WITHIN(sc->sc_tbufaddr[i], LEBLEN, RIEBL_RES_END)) {
443 offset = RIEBL_RES_END - sc->sc_tbufaddr[i];
444 }
445 sc->sc_tbufaddr[i] += offset;
446 }
447 }
448