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