if_le_vme.c revision 1.4 1 /* $NetBSD: if_le_vme.c,v 1.4 1997/03/17 13:29:05 leo 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 "bpfilter.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/syslog.h>
49 #include <sys/socket.h>
50 #include <sys/device.h>
51
52 #include <net/if.h>
53 #include <net/if_media.h>
54 #include <net/if_ether.h>
55
56 #ifdef INET
57 #include <netinet/in.h>
58 #include <netinet/if_inarp.h>
59 #endif
60
61 #include <machine/cpu.h>
62 #include <machine/bus.h>
63 #include <machine/iomap.h>
64 #include <machine/scu.h>
65
66 #include <atari/atari/device.h>
67 #include <atari/atari/intr.h>
68
69 #include <dev/ic/am7990reg.h>
70 #include <dev/ic/am7990var.h>
71
72 #include <atari/vme/vmevar.h>
73 #include <atari/vme/if_levar.h>
74
75 struct le_addresses {
76 u_long reg_addr;
77 u_long mem_addr;
78 int irq;
79 } lestd[] = {
80 { 0xfe00fff0, 0xfe010000, IRQUNK }, /* Riebl VME */
81 { 0xffcffff0, 0xffcf0000, 5 }, /* PAM VME */
82 { 0xfecffff0, 0xfecf0000, 5 } /* Rhotron VME */
83 };
84
85 #define NLESTD (sizeof(lestd) / sizeof(lestd[0]))
86
87 /*
88 * All cards have 64KB RAM. However.... On the Riebl cards the area
89 * between the offsets 0xee70-0xeec0 is used to store config data.
90 */
91 #define MEMSIZE (64*1024)
92
93 /*
94 * Default mac for RIEBL cards without a (working) battery. The first 4 bytes
95 * are the manufacturer id.
96 */
97 static u_char riebl_def_mac[] = {
98 0x00, 0x00, 0x36, 0x04, 0x00, 0x00
99 };
100
101 static int le_intr __P((struct le_softc *, int));
102 static void lepseudointr __P((struct le_softc *, void *));
103 static int le_vme_match __P((struct device *, struct cfdata *, void *));
104 static void le_vme_attach __P((struct device *, struct device *, void *));
105 static int probe_addresses __P((bus_space_tag_t *, bus_space_tag_t *,
106 bus_space_handle_t *, bus_space_handle_t *));
107 static void riebl_skip_reserved_area __P((struct am7990_softc *));
108
109 struct cfattach le_vme_ca = {
110 sizeof(struct le_softc), le_vme_match, le_vme_attach
111 };
112
113 hide void lewrcsr __P((struct am7990_softc *, u_int16_t, u_int16_t));
114 hide u_int16_t lerdcsr __P((struct am7990_softc *, u_int16_t));
115
116 hide void
117 lewrcsr(sc, port, val)
118 struct am7990_softc *sc;
119 u_int16_t port, val;
120 {
121 struct le_softc *lesc = (struct le_softc *)sc;
122 int s;
123
124 s = splhigh();
125 bus_space_write_2(lesc->sc_iot, lesc->sc_ioh, LER_RAP, port);
126 bus_space_write_2(lesc->sc_iot, lesc->sc_ioh, LER_RDP, val);
127 splx(s);
128 }
129
130 hide u_int16_t
131 lerdcsr(sc, port)
132 struct am7990_softc *sc;
133 u_int16_t port;
134 {
135 struct le_softc *lesc = (struct le_softc *)sc;
136 u_int16_t val;
137 int s;
138
139 s = splhigh();
140 bus_space_write_2(lesc->sc_iot, lesc->sc_ioh, LER_RAP, port);
141 val = bus_space_read_2(lesc->sc_iot, lesc->sc_ioh, LER_RDP);
142 splx(s);
143
144 return (val);
145 }
146
147 static int
148 le_vme_match(parent, cfp, aux)
149 struct device *parent;
150 struct cfdata *cfp;
151 void *aux;
152 {
153 struct vme_attach_args *va = aux;
154 int i;
155 bus_space_tag_t iot;
156 bus_space_tag_t memt;
157 bus_space_handle_t ioh;
158 bus_space_handle_t memh;
159
160 iot = va->va_iot;
161 memt = va->va_memt;
162
163 for (i = 0; i < NLESTD; i++) {
164 struct le_addresses *le_ap = &lestd[i];
165 int found = 0;
166
167 if ((va->va_iobase != IOBASEUNK)
168 && (va->va_iobase != le_ap->reg_addr))
169 continue;
170
171 if ((va->va_maddr != MADDRUNK)
172 && (va->va_maddr != le_ap->mem_addr))
173 continue;
174
175 if ((le_ap->irq != IRQUNK) && (va->va_irq != le_ap->irq))
176 continue;
177
178 if (bus_space_map(iot, le_ap->reg_addr, 16, 0, &ioh)) {
179 printf("leprobe: cannot map io-area\n");
180 return (0);
181 }
182 if (bus_space_map(memt, le_ap->mem_addr, MEMSIZE, 0, &memh)) {
183 bus_space_unmap(iot, (caddr_t)le_ap->reg_addr, 16);
184 printf("leprobe: cannot map memory-area\n");
185 return (0);
186 }
187 found = probe_addresses(&iot, &memt, &ioh, &memh);
188 bus_space_unmap(iot, (caddr_t)le_ap->reg_addr, 16);
189 bus_space_unmap(memt, (caddr_t)le_ap->mem_addr, 8*NBPG);
190
191 if (found) {
192 va->va_iobase = le_ap->reg_addr;
193 va->va_iosize = 16;
194 va->va_maddr = le_ap->mem_addr;
195 va->va_msize = MEMSIZE;
196 if (va->va_irq == IRQUNK)
197 va->va_irq = le_ap->irq;
198 return 1;
199 }
200 }
201 return (0);
202 }
203
204 static int
205 probe_addresses(iot, memt, ioh, memh)
206 bus_space_tag_t *iot;
207 bus_space_tag_t *memt;
208 bus_space_handle_t *ioh;
209 bus_space_handle_t *memh;
210 {
211 /*
212 * Test accesibility of register and memory area
213 */
214 if(!bus_space_peek_2(*iot, *ioh, LER_RDP))
215 return 0;
216 if(!bus_space_peek_1(*memt, *memh, 0))
217 return 0;
218
219 /*
220 * Test for writable memory
221 */
222 bus_space_write_2(*memt, *memh, 0, 0xa5a5);
223 if (bus_space_read_2(*memt, *memh, 0) != 0xa5a5)
224 return 0;
225
226 /*
227 * Test writability of selector port.
228 */
229 bus_space_write_2(*iot, *ioh, LER_RAP, LE_CSR1);
230 if (bus_space_read_2(*iot, *ioh, LER_RAP) != LE_CSR1)
231 return 0;
232
233 /*
234 * Do a small register test
235 */
236 bus_space_write_2(*iot, *ioh, LER_RAP, LE_CSR0);
237 bus_space_write_2(*iot, *ioh, LER_RDP, LE_C0_INIT | LE_C0_STOP);
238 if (bus_space_read_2(*iot, *ioh, LER_RDP) != LE_C0_STOP)
239 return 0;
240
241 bus_space_write_2(*iot, *ioh, LER_RDP, LE_C0_STOP);
242 if (bus_space_read_2(*iot, *ioh, LER_RDP) != LE_C0_STOP)
243 return 0;
244
245 return 1;
246 }
247
248 /*
249 * Interrupt mess. Because the card's interrupt is hardwired to either
250 * ipl5 or ipl3 (mostly on ipl5) and raising splnet to spl5() just won't do
251 * (it kills the serial at the least), we use a 2-level interrupt sceme. The
252 * card interrupt is routed to 'le_intr'. If the previous ipl was below
253 * splnet, just call the mi-function. If not, save the interrupt status,
254 * turn off card interrupts (the card is *very* persistent) and arrange
255 * for a softint 'callback' through 'lepseudointr'.
256 */
257 static int
258 le_intr(lesc, sr)
259 struct le_softc *lesc;
260 int sr;
261 {
262 struct am7990_softc *sc = &lesc->sc_am7990;
263 u_int16_t csr0;
264
265 if ((sr & PSL_IPL) < IPL_NET)
266 am7990_intr(sc);
267 else {
268 sc->sc_saved_csr0 = csr0 = lerdcsr(sc, LE_CSR0);
269 lewrcsr(sc, LE_CSR0, csr0 & ~LE_C0_INEA);
270 add_sicallback((si_farg)lepseudointr, lesc, sc);
271 }
272 return 1;
273 }
274
275
276 static void
277 lepseudointr(lesc, sc)
278 struct le_softc *lesc;
279 void *sc;
280 {
281 int s;
282
283 s = splx(lesc->sc_splval);
284 am7990_intr(sc);
285 splx(s);
286 }
287
288 static void
289 le_vme_attach(parent, self, aux)
290 struct device *parent, *self;
291 void *aux;
292 {
293 struct le_softc *lesc = (struct le_softc *)self;
294 struct am7990_softc *sc = &lesc->sc_am7990;
295 struct vme_attach_args *va = aux;
296 bus_space_handle_t ioh;
297 bus_space_handle_t memh;
298 int i;
299
300 printf("\n%s: ", sc->sc_dev.dv_xname);
301
302 if (bus_space_map(va->va_iot, va->va_iobase, va->va_iosize, 0, &ioh))
303 panic("leattach: cannot map io-area\n");
304 if (bus_space_map(va->va_memt, va->va_maddr, va->va_msize, 0, &memh))
305 panic("leattach: cannot map mem-area\n");
306
307 lesc->sc_iot = va->va_iot;
308 lesc->sc_ioh = ioh;
309 lesc->sc_memt = va->va_memt;
310 lesc->sc_memh = memh;
311 lesc->sc_splval = (va->va_irq << 8) | PSL_S; /* XXX */
312
313 /*
314 * Go on to find board type
315 */
316 if (bus_space_peek_1(va->va_iot, ioh, LER_EEPROM)) {
317 printf("PAM card");
318 lesc->sc_type = LE_PAM;
319 bus_space_read_1(va->va_iot, ioh, LER_MEME);
320 }
321 else {
322 printf("Riebl card");
323 if(bus_space_read_4(va->va_memt, memh, RIEBL_MAGIC_ADDR)
324 == RIEBL_MAGIC)
325 lesc->sc_type = LE_NEW_RIEBL;
326 else {
327 printf("(without battery) ");
328 lesc->sc_type = LE_OLD_RIEBL;
329 }
330 }
331
332 sc->sc_copytodesc = am7990_copytobuf_contig;
333 sc->sc_copyfromdesc = am7990_copyfrombuf_contig;
334 sc->sc_copytobuf = am7990_copytobuf_contig;
335 sc->sc_copyfrombuf = am7990_copyfrombuf_contig;
336 sc->sc_zerobuf = am7990_zerobuf_contig;
337
338 sc->sc_rdcsr = lerdcsr;
339 sc->sc_wrcsr = lewrcsr;
340 sc->sc_hwinit = NULL;
341 sc->sc_conf3 = LE_C3_BSWP;
342 sc->sc_addr = 0;
343 sc->sc_memsize = va->va_msize;
344 sc->sc_mem = (void *)memh; /* XXX */
345
346 /*
347 * Get MAC address
348 */
349 switch (lesc->sc_type) {
350 case LE_OLD_RIEBL:
351 bcopy(riebl_def_mac, sc->sc_enaddr,
352 sizeof(sc->sc_enaddr));
353 break;
354 case LE_NEW_RIEBL:
355 for (i = 0; i < sizeof(sc->sc_enaddr); i++)
356 sc->sc_enaddr[i] =
357 bus_space_read_1(va->va_memt, memh, i + RIEBL_MAC_ADDR);
358 break;
359 case LE_PAM:
360 i = bus_space_read_1(va->va_iot, ioh, LER_EEPROM);
361 for (i = 0; i < sizeof(sc->sc_enaddr); i++) {
362 sc->sc_enaddr[i] =
363 (bus_space_read_2(va->va_memt, memh, 2 * i) << 4) |
364 (bus_space_read_2(va->va_memt, memh, 2 * i + 1) & 0xf);
365 }
366 i = bus_space_read_1(va->va_iot, ioh, LER_MEME);
367 break;
368 }
369
370 am7990_config(sc);
371
372 if ((lesc->sc_type == LE_OLD_RIEBL) || (lesc->sc_type == LE_NEW_RIEBL))
373 riebl_skip_reserved_area(sc);
374
375 /*
376 * XXX: We always use uservector 64....
377 */
378 if ((lesc->sc_intr = intr_establish(64, USER_VEC, 0,
379 (hw_ifun_t)le_intr, lesc)) == NULL) {
380 printf("le_vme_attach: Can't establish interrupt\n");
381 return;
382 }
383
384 /*
385 * Notify the card of the vector
386 */
387 switch (lesc->sc_type) {
388 case LE_OLD_RIEBL:
389 case LE_NEW_RIEBL:
390 bus_space_write_2(va->va_memt, memh, RIEBL_IVEC_ADDR,
391 64 + 64);
392 break;
393 case LE_PAM:
394 bus_space_write_1(va->va_iot, ioh, LER_IVEC, 64 + 64);
395 break;
396 }
397
398 /*
399 * Unmask the VME-interrupt we're on
400 */
401 if (machineid & ATARI_TT)
402 SCU->vme_mask |= 1 << va->va_irq;
403 }
404
405 /*
406 * True if 'addr' containe within [start,len]
407 */
408 #define WITHIN(start, len, addr) \
409 ((addr >= start) && ((addr) <= ((start) + (len))))
410 static void
411 riebl_skip_reserved_area(sc)
412 struct am7990_softc *sc;
413 {
414 int offset = 0;
415 int i;
416
417 for(i = 0; i < sc->sc_nrbuf; i++) {
418 if (WITHIN(sc->sc_rbufaddr[i], LEBLEN, RIEBL_RES_START)
419 || WITHIN(sc->sc_rbufaddr[i], LEBLEN, RIEBL_RES_END)) {
420 offset = RIEBL_RES_END - sc->sc_rbufaddr[i];
421 }
422 sc->sc_rbufaddr[i] += offset;
423 }
424
425 for(i = 0; i < sc->sc_ntbuf; i++) {
426 if (WITHIN(sc->sc_tbufaddr[i], LEBLEN, RIEBL_RES_START)
427 || WITHIN(sc->sc_tbufaddr[i], LEBLEN, RIEBL_RES_END)) {
428 offset = RIEBL_RES_END - sc->sc_tbufaddr[i];
429 }
430 sc->sc_tbufaddr[i] += offset;
431 }
432 }
433