if_ae_nubus.c revision 1.14 1 /* $NetBSD: if_ae_nubus.c,v 1.14 1997/04/29 04:40:24 scottr Exp $ */
2
3 /*
4 * Copyright (C) 1997 Scott Reynolds
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Scott Reynolds for
18 * the NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 /*
34 * Some parts are derived from code adapted for MacBSD by Brad Parker
35 * <brad (at) fcr.com>.
36 *
37 * Currently supports:
38 * Apple NB Ethernet Card
39 * Apple NB Ethernet Card II
40 * Interlan A310 NuBus Ethernet card
41 * Cayman Systems GatorCard
42 * Asante MacCon II/E
43 * Kinetics EtherPort SE/30
44 */
45
46 #include <sys/param.h>
47 #include <sys/device.h>
48 #include <sys/errno.h>
49 #include <sys/ioctl.h>
50 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/syslog.h>
53 #include <sys/systm.h>
54
55 #include <net/if.h>
56 #include <net/if_ether.h>
57
58 #include <machine/bus.h>
59 #include <machine/viareg.h>
60
61 #include "nubus.h"
62 #include <dev/ic/dp8390reg.h>
63 #include <dev/ic/dp8390var.h>
64 #include <arch/mac68k/dev/if_aevar.h>
65 #include <arch/mac68k/dev/if_aereg.h>
66
67 static int ae_nubus_match __P((struct device *, struct cfdata *, void *));
68 static void ae_nubus_attach __P((struct device *, struct device *, void *));
69 static int ae_nb_card_vendor __P((struct nubus_attach_args *));
70 static int ae_nb_get_enaddr __P((struct nubus_attach_args *, u_int8_t *));
71 #ifdef DEBUG
72 static void ae_nb_watchdog __P((struct ifnet *));
73 #endif
74
75 struct cfattach ae_nubus_ca = {
76 sizeof(struct dp8390_softc), ae_nubus_match, ae_nubus_attach
77 };
78
79 static int
80 ae_nubus_match(parent, cf, aux)
81 struct device *parent;
82 struct cfdata *cf;
83 void *aux;
84 {
85 struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
86 bus_space_handle_t bsh;
87 int rv;
88
89 if (bus_space_map(na->na_tag, NUBUS_SLOT2PA(na->slot), NBMEMSIZE,
90 0, &bsh))
91 return (0);
92
93 rv = 0;
94
95 if (na->category == NUBUS_CATEGORY_NETWORK &&
96 na->type == NUBUS_TYPE_ETHERNET) {
97 switch (ae_nb_card_vendor(na)) {
98 case DP8390_VENDOR_APPLE:
99 case DP8390_VENDOR_ASANTE:
100 case DP8390_VENDOR_FARALLON:
101 case DP8390_VENDOR_INTERLAN:
102 case DP8390_VENDOR_KINETICS:
103 rv = 1;
104 break;
105 case DP8390_VENDOR_DAYNA:
106 case DP8390_VENDOR_FOCUS:
107 rv = UNSUPP;
108 break;
109 default:
110 break;
111 }
112 }
113
114 bus_space_unmap(na->na_tag, bsh, NBMEMSIZE);
115
116 return rv;
117 }
118
119 /*
120 * Install interface into kernel networking data structures
121 */
122 static void
123 ae_nubus_attach(parent, self, aux)
124 struct device *parent, *self;
125 void *aux;
126 {
127 struct dp8390_softc *sc = (struct dp8390_softc *)self;
128 struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
129 #ifdef DEBUG
130 struct ifnet *ifp = &sc->sc_ec.ec_if;
131 #endif
132 bus_space_tag_t bst;
133 bus_space_handle_t bsh;
134 int i, success;
135 char *cardtype;
136
137 bst = na->na_tag;
138 if (bus_space_map(bst, NUBUS_SLOT2PA(na->slot), NBMEMSIZE,
139 0, &bsh)) {
140 printf(": can't map memory space\n");
141 return;
142 }
143
144 sc->sc_regt = sc->sc_buft = bst;
145 sc->sc_flags = self->dv_cfdata->cf_flags;
146
147 sc->vendor = ae_nb_card_vendor(na);
148 sc->type = 0;
149 cardtype = nubus_get_card_name(na->fmt);
150 i = strlen(cardtype);
151 if ((sc->type_str = malloc((u_long)(i + 1), M_DEVBUF, M_NOWAIT))) {
152 strncpy(sc->type_str, cardtype, i);
153 sc->type_str[i] = '\0';
154 }
155
156 sc->use16bit = 0;
157 sc->is790 = 0;
158
159 sc->mem_start = 0;
160 sc->mem_size = 0;
161
162 success = 0;
163
164 switch (sc->vendor) {
165 case DP8390_VENDOR_APPLE: /* Apple-compatible cards */
166 case DP8390_VENDOR_ASANTE:
167 /* Map register offsets */
168 for (i = 0; i < 16; i++) /* reverse order, longword aligned */
169 sc->sc_reg_map[i] = (15 - i) << 2;
170
171 sc->use16bit = 1;
172 if (bus_space_subregion(bst, bsh,
173 AE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
174 printf(": failed to map register space\n");
175 break;
176 }
177 if ((sc->mem_size = ae_size_card_memory(bst, bsh,
178 AE_DATA_OFFSET)) == 0) {
179 printf(": failed to determine size of RAM.\n");
180 break;
181 }
182 if (bus_space_subregion(bst, bsh,
183 AE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
184 printf(": failed to map register space\n");
185 break;
186 }
187 #ifdef AE_OLD_GET_ENADDR
188 /* Get station address from on-board ROM */
189 for (i = 0; i < ETHER_ADDR_LEN; ++i)
190 sc->sc_enaddr[i] =
191 bus_space_read_1(bst, bsh, (AE_ROM_OFFSET + i * 2));
192 #else
193 if (ae_nb_get_enaddr(na, sc->sc_enaddr)) {
194 printf(": can't find MAC address\n");
195 break;
196 }
197 #endif
198
199 success = 1;
200 break;
201
202 case DP8390_VENDOR_DAYNA:
203 /* Map register offsets */
204 for (i = 0; i < 16; i++) /* normal order, longword aligned */
205 sc->sc_reg_map[i] = i << 2;
206
207 sc->use16bit = 1;
208 if (bus_space_subregion(bst, bsh,
209 DP_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
210 printf(": failed to map register space\n");
211 break;
212 }
213 sc->mem_size = 8192;
214 if (bus_space_subregion(bst, bsh,
215 DP_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
216 printf(": failed to map register space\n");
217 break;
218 }
219 #ifdef AE_OLD_GET_ENADDR
220 /* Get station address from on-board ROM */
221 for (i = 0; i < ETHER_ADDR_LEN; ++i)
222 sc->sc_enaddr[i] =
223 bus_space_read_1(bst, bsh, (DP_ROM_OFFSET + i * 2));
224 #else
225 if (ae_nb_get_enaddr(na, sc->sc_enaddr)) {
226 printf(": can't find MAC address\n");
227 break;
228 }
229 #endif
230
231 printf(": unsupported Dayna hardware\n");
232 break;
233
234 case DP8390_VENDOR_FARALLON:
235 /* Map register offsets */
236 for (i = 0; i < 16; i++) /* reverse order, longword aligned */
237 sc->sc_reg_map[i] = (15 - i) << 2;
238
239 sc->use16bit = 1;
240 if (bus_space_subregion(bst, bsh,
241 AE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
242 printf(": failed to map register space\n");
243 break;
244 }
245 if ((sc->mem_size = ae_size_card_memory(bst, bsh,
246 AE_DATA_OFFSET)) == 0) {
247 printf(": failed to determine size of RAM.\n");
248 break;
249 }
250 if (bus_space_subregion(bst, bsh,
251 AE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
252 printf(": failed to map register space\n");
253 break;
254 }
255 #ifdef AE_OLD_GET_ENADDR
256 /* Get station address from on-board ROM */
257 for (i = 0; i < ETHER_ADDR_LEN; ++i)
258 sc->sc_enaddr[i] =
259 bus_space_read_1(bst, bsh, (FE_ROM_OFFSET + i));
260 #else
261 if (ae_nb_get_enaddr(na, sc->sc_enaddr)) {
262 printf(": can't find MAC address\n");
263 break;
264 }
265 #endif
266
267 success = 1;
268 break;
269
270 case DP8390_VENDOR_FOCUS:
271 printf(": unsupported Focus hardware\n");
272 break;
273
274 case DP8390_VENDOR_INTERLAN:
275 /* Map register offsets */
276 for (i = 0; i < 16; i++) /* normal order, longword aligned */
277 sc->sc_reg_map[i] = i << 2;
278
279 sc->use16bit = 1;
280 if (bus_space_subregion(bst, bsh,
281 GC_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
282 printf(": failed to map register space\n");
283 break;
284 }
285 if ((sc->mem_size = ae_size_card_memory(bst, bsh,
286 GC_DATA_OFFSET)) == 0) {
287 printf(": failed to determine size of RAM.\n");
288 break;
289 }
290 if (bus_space_subregion(bst, bsh,
291 GC_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
292 printf(": failed to map register space\n");
293 break;
294 }
295
296 /* reset the NIC chip */
297 bus_space_write_1(bst, bsh, GC_RESET_OFFSET, 0);
298
299 if (ae_nb_get_enaddr(na, sc->sc_enaddr)) {
300 /* Fall back to snarf directly from ROM. Ick. */
301 for (i = 0; i < ETHER_ADDR_LEN; ++i)
302 sc->sc_enaddr[i] =
303 bus_space_read_1(bst, bsh,
304 (GC_ROM_OFFSET + i * 4));
305 }
306
307 success = 1;
308 break;
309
310 case DP8390_VENDOR_KINETICS:
311 /* Map register offsets */
312 for (i = 0; i < 16; i++) /* normal order, longword aligned */
313 sc->sc_reg_map[i] = i << 2;
314
315 sc->use16bit = 0;
316 if (bus_space_subregion(bst, bsh,
317 KE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
318 printf(": failed to map register space\n");
319 break;
320 }
321 if ((sc->mem_size = ae_size_card_memory(bst, bsh,
322 KE_DATA_OFFSET)) == 0) {
323 printf(": failed to determine size of RAM.\n");
324 break;
325 }
326 if (bus_space_subregion(bst, bsh,
327 KE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
328 printf(": failed to map register space\n");
329 break;
330 }
331 if (ae_nb_get_enaddr(na, sc->sc_enaddr)) {
332 printf(": can't find MAC address\n");
333 break;
334 }
335
336 success = 1;
337 break;
338
339 default:
340 break;
341 }
342
343 if (!success) {
344 bus_space_unmap(bst, bsh, NBMEMSIZE);
345 return;
346 }
347
348 /*
349 * Override test_mem and write_mbuf functions; other defaults
350 * already work properly.
351 */
352 sc->test_mem = ae_test_mem;
353 sc->write_mbuf = ae_write_mbuf;
354 #ifdef DEBUG
355 ifp->if_watchdog = ae_nb_watchdog; /* Override watchdog */
356 #endif
357
358 if (dp8390_config(sc)) {
359 bus_space_unmap(bst, bsh, NBMEMSIZE);
360 return;
361 }
362
363 printf("%dKB memory\n", sc->mem_size / 1024);
364
365 /* make sure interrupts are vectored to us */
366 add_nubus_intr(na->slot, dp8390_intr, sc);
367 }
368
369 static int
370 ae_nb_card_vendor(na)
371 struct nubus_attach_args *na;
372 {
373 int vendor;
374
375 switch (na->drsw) {
376 case NUBUS_DRSW_3COM:
377 switch (na->drhw) {
378 case NUBUS_DRHW_APPLE_SN:
379 vendor = DP8390_VENDOR_UNKNOWN;
380 break;
381 default:
382 vendor = DP8390_VENDOR_APPLE;
383 break;
384 }
385 break;
386 case NUBUS_DRSW_APPLE:
387 case NUBUS_DRSW_TECHWORKS:
388 vendor = DP8390_VENDOR_APPLE;
389 break;
390 case NUBUS_DRSW_ASANTE:
391 vendor = DP8390_VENDOR_ASANTE;
392 break;
393 case NUBUS_DRSW_FARALLON:
394 vendor = DP8390_VENDOR_FARALLON;
395 break;
396 case NUBUS_DRSW_FOCUS:
397 vendor = DP8390_VENDOR_FOCUS;
398 break;
399 case NUBUS_DRSW_GATOR:
400 switch (na->drhw) {
401 default:
402 case NUBUS_DRHW_INTERLAN:
403 vendor = DP8390_VENDOR_INTERLAN;
404 break;
405 case NUBUS_DRHW_KINETICS:
406 if (strncmp(
407 nubus_get_card_name(na->fmt), "EtherPort", 9) == 0)
408 vendor = DP8390_VENDOR_KINETICS;
409 else
410 vendor = DP8390_VENDOR_DAYNA;
411 break;
412 }
413 break;
414 default:
415 vendor = DP8390_VENDOR_UNKNOWN;
416 }
417 return vendor;
418 }
419
420 static int
421 ae_nb_get_enaddr(na, ep)
422 struct nubus_attach_args *na;
423 u_int8_t *ep;
424 {
425 nubus_dir dir;
426 nubus_dirent dirent;
427
428 /*
429 * XXX - note hardwired resource IDs here (0x80); these are
430 * assumed to be used by all cards, but should be fixed when
431 * we find out more about Ethernet card resources.
432 */
433 nubus_get_main_dir(na->fmt, &dir);
434 if (nubus_find_rsrc(na->fmt, &dir, 0x80, &dirent) <= 0)
435 return 1;
436 nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
437 if (nubus_find_rsrc(na->fmt, &dir, 0x80, &dirent) <= 0)
438 return 1;
439 if (nubus_get_ind_data(na->fmt, &dirent, ep, ETHER_ADDR_LEN) <= 0)
440 return 1;
441
442 return 0;
443 }
444
445 #ifdef DEBUG
446 static void
447 ae_nb_watchdog(ifp)
448 struct ifnet *ifp;
449 {
450 struct dp8390_softc *sc = ifp->if_softc;
451
452 /*
453 * This is a kludge! The via code seems to miss slot interrupts
454 * sometimes. This kludges around that by calling the handler
455 * by hand if the watchdog is activated. -- XXX (akb)
456 */
457 (*via2itab[1])((void *) 1);
458
459 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
460 ++ifp->if_oerrors;
461
462 dp8390_reset(sc);
463 }
464 #endif
465