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